Branch data Line data Source code
1 : : /* Expansion pass for OMP directives. Outlines regions of certain OMP
2 : : directives to separate functions, converts others into explicit calls to the
3 : : runtime library (libgomp) and so forth
4 : :
5 : : Copyright (C) 2005-2025 Free Software Foundation, Inc.
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 "memmodel.h"
27 : : #include "backend.h"
28 : : #include "target.h"
29 : : #include "rtl.h"
30 : : #include "tree.h"
31 : : #include "gimple.h"
32 : : #include "cfghooks.h"
33 : : #include "tree-pass.h"
34 : : #include "ssa.h"
35 : : #include "optabs.h"
36 : : #include "cgraph.h"
37 : : #include "pretty-print.h"
38 : : #include "diagnostic-core.h"
39 : : #include "fold-const.h"
40 : : #include "stor-layout.h"
41 : : #include "cfganal.h"
42 : : #include "internal-fn.h"
43 : : #include "gimplify.h"
44 : : #include "gimple-iterator.h"
45 : : #include "gimplify-me.h"
46 : : #include "gimple-walk.h"
47 : : #include "tree-cfg.h"
48 : : #include "tree-into-ssa.h"
49 : : #include "tree-ssa.h"
50 : : #include "splay-tree.h"
51 : : #include "cfgloop.h"
52 : : #include "omp-general.h"
53 : : #include "omp-offload.h"
54 : : #include "tree-cfgcleanup.h"
55 : : #include "alloc-pool.h"
56 : : #include "symbol-summary.h"
57 : : #include "gomp-constants.h"
58 : : #include "gimple-pretty-print.h"
59 : : #include "stringpool.h"
60 : : #include "attribs.h"
61 : : #include "tree-eh.h"
62 : : #include "opts.h"
63 : :
64 : : /* OMP region information. Every parallel and workshare
65 : : directive is enclosed between two markers, the OMP_* directive
66 : : and a corresponding GIMPLE_OMP_RETURN statement. */
67 : :
68 : : struct omp_region
69 : : {
70 : : /* The enclosing region. */
71 : : struct omp_region *outer;
72 : :
73 : : /* First child region. */
74 : : struct omp_region *inner;
75 : :
76 : : /* Next peer region. */
77 : : struct omp_region *next;
78 : :
79 : : /* Block containing the omp directive as its last stmt. */
80 : : basic_block entry;
81 : :
82 : : /* Block containing the GIMPLE_OMP_RETURN as its last stmt. */
83 : : basic_block exit;
84 : :
85 : : /* Block containing the GIMPLE_OMP_CONTINUE as its last stmt. */
86 : : basic_block cont;
87 : :
88 : : /* If this is a combined parallel+workshare region, this is a list
89 : : of additional arguments needed by the combined parallel+workshare
90 : : library call. */
91 : : vec<tree, va_gc> *ws_args;
92 : :
93 : : /* The code for the omp directive of this region. */
94 : : enum gimple_code type;
95 : :
96 : : /* Schedule kind, only used for GIMPLE_OMP_FOR type regions. */
97 : : enum omp_clause_schedule_kind sched_kind;
98 : :
99 : : /* Schedule modifiers. */
100 : : unsigned char sched_modifiers;
101 : :
102 : : /* True if this is a combined parallel+workshare region. */
103 : : bool is_combined_parallel;
104 : :
105 : : /* Copy of fd.lastprivate_conditional != 0. */
106 : : bool has_lastprivate_conditional;
107 : :
108 : : /* The ordered stmt if type is GIMPLE_OMP_ORDERED and it has
109 : : a depend clause. */
110 : : gomp_ordered *ord_stmt;
111 : : };
112 : :
113 : : static struct omp_region *root_omp_region;
114 : : static bool omp_any_child_fn_dumped;
115 : :
116 : : static void expand_omp_build_assign (gimple_stmt_iterator *, tree, tree,
117 : : bool = false);
118 : : static gphi *find_phi_with_arg_on_edge (tree, edge);
119 : : static void expand_omp (struct omp_region *region);
120 : :
121 : : /* Return true if REGION is a combined parallel+workshare region. */
122 : :
123 : : static inline bool
124 : 43263 : is_combined_parallel (struct omp_region *region)
125 : : {
126 : 43263 : return region->is_combined_parallel;
127 : : }
128 : :
129 : : /* Return true is REGION is or is contained within an offload region. */
130 : :
131 : : static bool
132 : 11136 : is_in_offload_region (struct omp_region *region)
133 : : {
134 : 29987 : gimple *entry_stmt = last_nondebug_stmt (region->entry);
135 : 29987 : if (is_gimple_omp (entry_stmt)
136 : 29035 : && is_gimple_omp_offloaded (entry_stmt))
137 : : return true;
138 : 25677 : else if (region->outer)
139 : : return is_in_offload_region (region->outer);
140 : : else
141 : 6826 : return (lookup_attribute ("omp declare target",
142 : 6826 : DECL_ATTRIBUTES (current_function_decl))
143 : 6826 : != NULL);
144 : : }
145 : :
146 : : /* Given two blocks PAR_ENTRY_BB and WS_ENTRY_BB such that WS_ENTRY_BB
147 : : is the immediate dominator of PAR_ENTRY_BB, return true if there
148 : : are no data dependencies that would prevent expanding the parallel
149 : : directive at PAR_ENTRY_BB as a combined parallel+workshare region.
150 : :
151 : : When expanding a combined parallel+workshare region, the call to
152 : : the child function may need additional arguments in the case of
153 : : GIMPLE_OMP_FOR regions. In some cases, these arguments are
154 : : computed out of variables passed in from the parent to the child
155 : : via 'struct .omp_data_s'. For instance:
156 : :
157 : : #pragma omp parallel for schedule (guided, i * 4)
158 : : for (j ...)
159 : :
160 : : Is lowered into:
161 : :
162 : : # BLOCK 2 (PAR_ENTRY_BB)
163 : : .omp_data_o.i = i;
164 : : #pragma omp parallel [child fn: bar.omp_fn.0 ( ..., D.1598)
165 : :
166 : : # BLOCK 3 (WS_ENTRY_BB)
167 : : .omp_data_i = &.omp_data_o;
168 : : D.1667 = .omp_data_i->i;
169 : : D.1598 = D.1667 * 4;
170 : : #pragma omp for schedule (guided, D.1598)
171 : :
172 : : When we outline the parallel region, the call to the child function
173 : : 'bar.omp_fn.0' will need the value D.1598 in its argument list, but
174 : : that value is computed *after* the call site. So, in principle we
175 : : cannot do the transformation.
176 : :
177 : : To see whether the code in WS_ENTRY_BB blocks the combined
178 : : parallel+workshare call, we collect all the variables used in the
179 : : GIMPLE_OMP_FOR header check whether they appear on the LHS of any
180 : : statement in WS_ENTRY_BB. If so, then we cannot emit the combined
181 : : call.
182 : :
183 : : FIXME. If we had the SSA form built at this point, we could merely
184 : : hoist the code in block 3 into block 2 and be done with it. But at
185 : : this point we don't have dataflow information and though we could
186 : : hack something up here, it is really not worth the aggravation. */
187 : :
188 : : static bool
189 : 9089 : workshare_safe_to_combine_p (basic_block ws_entry_bb)
190 : : {
191 : 9089 : struct omp_for_data fd;
192 : 9089 : gimple *ws_stmt = last_nondebug_stmt (ws_entry_bb);
193 : :
194 : 9089 : if (gimple_code (ws_stmt) == GIMPLE_OMP_SECTIONS)
195 : : return true;
196 : :
197 : 8970 : gcc_assert (gimple_code (ws_stmt) == GIMPLE_OMP_FOR);
198 : 8970 : if (gimple_omp_for_kind (ws_stmt) != GF_OMP_FOR_KIND_FOR)
199 : : return false;
200 : :
201 : 8953 : omp_extract_for_data (as_a <gomp_for *> (ws_stmt), &fd, NULL);
202 : :
203 : 8953 : if (fd.collapse > 1 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
204 : : return false;
205 : 7180 : if (fd.iter_type != long_integer_type_node)
206 : : return false;
207 : :
208 : : /* FIXME. We give up too easily here. If any of these arguments
209 : : are not constants, they will likely involve variables that have
210 : : been mapped into fields of .omp_data_s for sharing with the child
211 : : function. With appropriate data flow, it would be possible to
212 : : see through this. */
213 : 1508 : if (!is_gimple_min_invariant (fd.loop.n1)
214 : 1278 : || !is_gimple_min_invariant (fd.loop.n2)
215 : 1149 : || !is_gimple_min_invariant (fd.loop.step)
216 : 2642 : || (fd.chunk_size && !is_gimple_min_invariant (fd.chunk_size)))
217 : 376 : return false;
218 : :
219 : : return true;
220 : : }
221 : :
222 : : /* Adjust CHUNK_SIZE from SCHEDULE clause, depending on simd modifier
223 : : presence (SIMD_SCHEDULE). */
224 : :
225 : : static tree
226 : 8313 : omp_adjust_chunk_size (tree chunk_size, bool simd_schedule, bool offload)
227 : : {
228 : 8313 : if (!simd_schedule || integer_zerop (chunk_size))
229 : 8279 : return chunk_size;
230 : :
231 : 34 : tree vf;
232 : 34 : tree type = TREE_TYPE (chunk_size);
233 : :
234 : 34 : if (offload)
235 : : {
236 : 2 : cfun->curr_properties &= ~PROP_gimple_lomp_dev;
237 : 2 : vf = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_GOMP_MAX_VF,
238 : : unsigned_type_node, 0);
239 : 2 : vf = fold_convert (type, vf);
240 : : }
241 : : else
242 : : {
243 : 32 : poly_uint64 vf_num = omp_max_vf (false);
244 : 32 : if (known_eq (vf_num, 1U))
245 : 8280 : return chunk_size;
246 : 31 : vf = build_int_cst (type, vf_num);
247 : : }
248 : :
249 : 33 : tree vf_minus_one = fold_build2 (MINUS_EXPR, type, vf,
250 : : build_int_cst (type, 1));
251 : 33 : tree negative_vf = fold_build1 (NEGATE_EXPR, type, vf);
252 : 33 : chunk_size = fold_build2 (PLUS_EXPR, type, chunk_size, vf_minus_one);
253 : 33 : return fold_build2 (BIT_AND_EXPR, type, chunk_size, negative_vf);
254 : : }
255 : :
256 : : /* Collect additional arguments needed to emit a combined
257 : : parallel+workshare call. WS_STMT is the workshare directive being
258 : : expanded. */
259 : :
260 : : static vec<tree, va_gc> *
261 : 1196 : get_ws_args_for (gimple *par_stmt, gimple *ws_stmt, bool offload)
262 : : {
263 : 1196 : tree t;
264 : 1196 : location_t loc = gimple_location (ws_stmt);
265 : 1196 : vec<tree, va_gc> *ws_args;
266 : :
267 : 1196 : if (gomp_for *for_stmt = dyn_cast <gomp_for *> (ws_stmt))
268 : : {
269 : 1084 : struct omp_for_data fd;
270 : 1084 : tree n1, n2;
271 : :
272 : 1084 : omp_extract_for_data (for_stmt, &fd, NULL);
273 : 1084 : n1 = fd.loop.n1;
274 : 1084 : n2 = fd.loop.n2;
275 : :
276 : 1084 : if (gimple_omp_for_combined_into_p (for_stmt))
277 : : {
278 : 769 : tree innerc
279 : 769 : = omp_find_clause (gimple_omp_parallel_clauses (par_stmt),
280 : : OMP_CLAUSE__LOOPTEMP_);
281 : 769 : gcc_assert (innerc);
282 : 769 : n1 = OMP_CLAUSE_DECL (innerc);
283 : 769 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
284 : : OMP_CLAUSE__LOOPTEMP_);
285 : 769 : gcc_assert (innerc);
286 : 769 : n2 = OMP_CLAUSE_DECL (innerc);
287 : : }
288 : :
289 : 1084 : vec_alloc (ws_args, 3 + (fd.chunk_size != 0));
290 : :
291 : 1084 : t = fold_convert_loc (loc, long_integer_type_node, n1);
292 : 1084 : ws_args->quick_push (t);
293 : :
294 : 1084 : t = fold_convert_loc (loc, long_integer_type_node, n2);
295 : 1084 : ws_args->quick_push (t);
296 : :
297 : 1084 : t = fold_convert_loc (loc, long_integer_type_node, fd.loop.step);
298 : 1084 : ws_args->quick_push (t);
299 : :
300 : 1084 : if (fd.chunk_size)
301 : : {
302 : 538 : t = fold_convert_loc (loc, long_integer_type_node, fd.chunk_size);
303 : 538 : t = omp_adjust_chunk_size (t, fd.simd_schedule, offload);
304 : 538 : ws_args->quick_push (t);
305 : : }
306 : :
307 : 1084 : return ws_args;
308 : : }
309 : 112 : else if (gimple_code (ws_stmt) == GIMPLE_OMP_SECTIONS)
310 : : {
311 : : /* Number of sections is equal to the number of edges from the
312 : : GIMPLE_OMP_SECTIONS_SWITCH statement, except for the one to
313 : : the exit of the sections region. */
314 : 112 : basic_block bb = single_succ (gimple_bb (ws_stmt));
315 : 224 : t = build_int_cst (unsigned_type_node, EDGE_COUNT (bb->succs) - 1);
316 : 112 : vec_alloc (ws_args, 1);
317 : 112 : ws_args->quick_push (t);
318 : 112 : return ws_args;
319 : : }
320 : :
321 : 0 : gcc_unreachable ();
322 : : }
323 : :
324 : : /* Discover whether REGION is a combined parallel+workshare region. */
325 : :
326 : : static void
327 : 16266 : determine_parallel_type (struct omp_region *region)
328 : : {
329 : 16266 : basic_block par_entry_bb, par_exit_bb;
330 : 16266 : basic_block ws_entry_bb, ws_exit_bb;
331 : :
332 : 16266 : if (region == NULL || region->inner == NULL
333 : 14795 : || region->exit == NULL || region->inner->exit == NULL
334 : 14784 : || region->inner->cont == NULL)
335 : : return;
336 : :
337 : : /* We only support parallel+for and parallel+sections. */
338 : 10351 : if (region->type != GIMPLE_OMP_PARALLEL
339 : 10351 : || (region->inner->type != GIMPLE_OMP_FOR
340 : 291 : && region->inner->type != GIMPLE_OMP_SECTIONS))
341 : : return;
342 : :
343 : : /* Check for perfect nesting PAR_ENTRY_BB -> WS_ENTRY_BB and
344 : : WS_EXIT_BB -> PAR_EXIT_BB. */
345 : 10233 : par_entry_bb = region->entry;
346 : 10233 : par_exit_bb = region->exit;
347 : 10233 : ws_entry_bb = region->inner->entry;
348 : 10233 : ws_exit_bb = region->inner->exit;
349 : :
350 : : /* Give up for task reductions on the parallel, while it is implementable,
351 : : adding another big set of APIs or slowing down the normal paths is
352 : : not acceptable. */
353 : 10233 : tree pclauses
354 : 10233 : = gimple_omp_parallel_clauses (last_nondebug_stmt (par_entry_bb));
355 : 10233 : if (omp_find_clause (pclauses, OMP_CLAUSE__REDUCTEMP_))
356 : : return;
357 : :
358 : 10210 : if (single_succ (par_entry_bb) == ws_entry_bb
359 : 9125 : && single_succ (ws_exit_bb) == par_exit_bb
360 : 9089 : && workshare_safe_to_combine_p (ws_entry_bb)
361 : 11461 : && (gimple_omp_parallel_combined_p (last_nondebug_stmt (par_entry_bb))
362 : 7 : || (last_and_only_stmt (ws_entry_bb)
363 : 2 : && last_and_only_stmt (par_exit_bb))))
364 : : {
365 : 1244 : gimple *par_stmt = last_nondebug_stmt (par_entry_bb);
366 : 1244 : gimple *ws_stmt = last_nondebug_stmt (ws_entry_bb);
367 : :
368 : 1244 : if (region->inner->type == GIMPLE_OMP_FOR)
369 : : {
370 : : /* If this is a combined parallel loop, we need to determine
371 : : whether or not to use the combined library calls. There
372 : : are two cases where we do not apply the transformation:
373 : : static loops and any kind of ordered loop. In the first
374 : : case, we already open code the loop so there is no need
375 : : to do anything else. In the latter case, the combined
376 : : parallel loop call would still need extra synchronization
377 : : to implement ordered semantics, so there would not be any
378 : : gain in using the combined call. */
379 : 1128 : tree clauses = gimple_omp_for_clauses (ws_stmt);
380 : 1128 : tree c = omp_find_clause (clauses, OMP_CLAUSE_SCHEDULE);
381 : 1128 : if (c == NULL
382 : 1110 : || ((OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
383 : : == OMP_CLAUSE_SCHEDULE_STATIC)
384 : 1096 : || omp_find_clause (clauses, OMP_CLAUSE_ORDERED)
385 : 1084 : || omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_)
386 : 2212 : || ((c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_))
387 : 14 : && POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
388 : 44 : return;
389 : : }
390 : 116 : else if (region->inner->type == GIMPLE_OMP_SECTIONS
391 : 232 : && (omp_find_clause (gimple_omp_sections_clauses (ws_stmt),
392 : : OMP_CLAUSE__REDUCTEMP_)
393 : 116 : || omp_find_clause (gimple_omp_sections_clauses (ws_stmt),
394 : : OMP_CLAUSE__CONDTEMP_)))
395 : 4 : return;
396 : :
397 : 1196 : region->is_combined_parallel = true;
398 : 1196 : region->inner->is_combined_parallel = true;
399 : 1196 : region->ws_args = get_ws_args_for (par_stmt, ws_stmt,
400 : 1196 : is_in_offload_region (region));
401 : : }
402 : : }
403 : :
404 : : /* Debugging dumps for parallel regions. */
405 : : void dump_omp_region (FILE *, struct omp_region *, int);
406 : : void debug_omp_region (struct omp_region *);
407 : : void debug_all_omp_regions (void);
408 : :
409 : : /* Dump the parallel region tree rooted at REGION. */
410 : :
411 : : void
412 : 203 : dump_omp_region (FILE *file, struct omp_region *region, int indent)
413 : : {
414 : 248 : fprintf (file, "%*sbb %d: %s\n", indent, "", region->entry->index,
415 : 248 : gimple_code_name[region->type]);
416 : :
417 : 248 : if (region->inner)
418 : 39 : dump_omp_region (file, region->inner, indent + 4);
419 : :
420 : 248 : if (region->cont)
421 : : {
422 : 99 : fprintf (file, "%*sbb %d: GIMPLE_OMP_CONTINUE\n", indent, "",
423 : : region->cont->index);
424 : : }
425 : :
426 : 248 : if (region->exit)
427 : 225 : fprintf (file, "%*sbb %d: GIMPLE_OMP_RETURN\n", indent, "",
428 : : region->exit->index);
429 : : else
430 : 23 : fprintf (file, "%*s[no exit marker]\n", indent, "");
431 : :
432 : 248 : if (region->next)
433 : : dump_omp_region (file, region->next, indent);
434 : 203 : }
435 : :
436 : : DEBUG_FUNCTION void
437 : 0 : debug_omp_region (struct omp_region *region)
438 : : {
439 : 0 : dump_omp_region (stderr, region, 0);
440 : 0 : }
441 : :
442 : : DEBUG_FUNCTION void
443 : 0 : debug_all_omp_regions (void)
444 : : {
445 : 0 : dump_omp_region (stderr, root_omp_region, 0);
446 : 0 : }
447 : :
448 : : /* Create a new parallel region starting at STMT inside region PARENT. */
449 : :
450 : : static struct omp_region *
451 : 244089 : new_omp_region (basic_block bb, enum gimple_code type,
452 : : struct omp_region *parent)
453 : : {
454 : 244089 : struct omp_region *region = XCNEW (struct omp_region);
455 : :
456 : 244089 : region->outer = parent;
457 : 244089 : region->entry = bb;
458 : 244089 : region->type = type;
459 : :
460 : 244089 : if (parent)
461 : : {
462 : : /* This is a nested region. Add it to the list of inner
463 : : regions in PARENT. */
464 : 127207 : region->next = parent->inner;
465 : 127207 : parent->inner = region;
466 : : }
467 : : else
468 : : {
469 : : /* This is a toplevel region. Add it to the list of toplevel
470 : : regions in ROOT_OMP_REGION. */
471 : 116882 : region->next = root_omp_region;
472 : 116882 : root_omp_region = region;
473 : : }
474 : :
475 : 244089 : return region;
476 : : }
477 : :
478 : : /* Release the memory associated with the region tree rooted at REGION. */
479 : :
480 : : static void
481 : 244089 : free_omp_region_1 (struct omp_region *region)
482 : : {
483 : 244089 : struct omp_region *i, *n;
484 : :
485 : 371296 : for (i = region->inner; i ; i = n)
486 : : {
487 : 127207 : n = i->next;
488 : 127207 : free_omp_region_1 (i);
489 : : }
490 : :
491 : 244089 : free (region);
492 : 244089 : }
493 : :
494 : : /* Release the memory for the entire omp region tree. */
495 : :
496 : : void
497 : 3003816 : omp_free_regions (void)
498 : : {
499 : 3003816 : struct omp_region *r, *n;
500 : 3120698 : for (r = root_omp_region; r ; r = n)
501 : : {
502 : 116882 : n = r->next;
503 : 116882 : free_omp_region_1 (r);
504 : : }
505 : 3003816 : root_omp_region = NULL;
506 : 3003816 : }
507 : :
508 : : /* A convenience function to build an empty GIMPLE_COND with just the
509 : : condition. */
510 : :
511 : : static gcond *
512 : 112179 : gimple_build_cond_empty (tree cond)
513 : : {
514 : 112179 : enum tree_code pred_code;
515 : 112179 : tree lhs, rhs;
516 : :
517 : 112179 : gimple_cond_get_ops_from_tree (cond, &pred_code, &lhs, &rhs);
518 : 112179 : return gimple_build_cond (pred_code, lhs, rhs, NULL_TREE, NULL_TREE);
519 : : }
520 : :
521 : : /* Change DECL_CONTEXT of CHILD_FNDECL to that of the parent function.
522 : : Add CHILD_FNDECL to decl chain of the supercontext of the block
523 : : ENTRY_BLOCK - this is the block which originally contained the
524 : : code from which CHILD_FNDECL was created.
525 : :
526 : : Together, these actions ensure that the debug info for the outlined
527 : : function will be emitted with the correct lexical scope. */
528 : :
529 : : static void
530 : 42893 : adjust_context_and_scope (struct omp_region *region, tree entry_block,
531 : : tree child_fndecl)
532 : : {
533 : 42893 : tree parent_fndecl = NULL_TREE;
534 : 42893 : gimple *entry_stmt;
535 : : /* OMP expansion expands inner regions before outer ones, so if
536 : : we e.g. have explicit task region nested in parallel region, when
537 : : expanding the task region current_function_decl will be the original
538 : : source function, but we actually want to use as context the child
539 : : function of the parallel. */
540 : 42893 : for (region = region->outer;
541 : 64024 : region && parent_fndecl == NULL_TREE; region = region->outer)
542 : 21131 : switch (region->type)
543 : : {
544 : 6857 : case GIMPLE_OMP_PARALLEL:
545 : 6857 : case GIMPLE_OMP_TASK:
546 : 6857 : case GIMPLE_OMP_TEAMS:
547 : 6857 : entry_stmt = last_nondebug_stmt (region->entry);
548 : 6857 : parent_fndecl = gimple_omp_taskreg_child_fn (entry_stmt);
549 : 6857 : break;
550 : 4547 : case GIMPLE_OMP_TARGET:
551 : 4547 : entry_stmt = last_nondebug_stmt (region->entry);
552 : 4547 : parent_fndecl
553 : 4547 : = gimple_omp_target_child_fn (as_a <gomp_target *> (entry_stmt));
554 : 4547 : break;
555 : : default:
556 : : break;
557 : : }
558 : :
559 : 42893 : if (parent_fndecl == NULL_TREE)
560 : 34598 : parent_fndecl = current_function_decl;
561 : 42893 : DECL_CONTEXT (child_fndecl) = parent_fndecl;
562 : :
563 : 42893 : if (entry_block != NULL_TREE && TREE_CODE (entry_block) == BLOCK)
564 : : {
565 : 42857 : tree b = BLOCK_SUPERCONTEXT (entry_block);
566 : 42857 : if (TREE_CODE (b) == BLOCK)
567 : : {
568 : 42726 : DECL_CHAIN (child_fndecl) = BLOCK_VARS (b);
569 : 42726 : BLOCK_VARS (b) = child_fndecl;
570 : : }
571 : : }
572 : 42893 : }
573 : :
574 : : /* Build the function calls to GOMP_parallel etc to actually
575 : : generate the parallel operation. REGION is the parallel region
576 : : being expanded. BB is the block where to insert the code. WS_ARGS
577 : : will be set if this is a call to a combined parallel+workshare
578 : : construct, it contains the list of additional arguments needed by
579 : : the workshare construct. */
580 : :
581 : : static void
582 : 16266 : expand_parallel_call (struct omp_region *region, basic_block bb,
583 : : gomp_parallel *entry_stmt,
584 : : vec<tree, va_gc> *ws_args)
585 : : {
586 : 16266 : tree t, t1, t2, val, cond, c, clauses, flags;
587 : 16266 : gimple_stmt_iterator gsi;
588 : 16266 : gimple *stmt;
589 : 16266 : enum built_in_function start_ix;
590 : 16266 : int start_ix2;
591 : 16266 : location_t clause_loc;
592 : 16266 : vec<tree, va_gc> *args;
593 : :
594 : 16266 : clauses = gimple_omp_parallel_clauses (entry_stmt);
595 : :
596 : : /* Determine what flavor of GOMP_parallel we will be
597 : : emitting. */
598 : 16266 : start_ix = BUILT_IN_GOMP_PARALLEL;
599 : 16266 : tree rtmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
600 : 16266 : if (rtmp)
601 : : start_ix = BUILT_IN_GOMP_PARALLEL_REDUCTIONS;
602 : 16207 : else if (is_combined_parallel (region))
603 : : {
604 : 1196 : switch (region->inner->type)
605 : : {
606 : 1084 : case GIMPLE_OMP_FOR:
607 : 1084 : gcc_assert (region->inner->sched_kind != OMP_CLAUSE_SCHEDULE_AUTO);
608 : 1084 : switch (region->inner->sched_kind)
609 : : {
610 : 546 : case OMP_CLAUSE_SCHEDULE_RUNTIME:
611 : : /* For lastprivate(conditional:), our implementation
612 : : requires monotonic behavior. */
613 : 546 : if (region->inner->has_lastprivate_conditional != 0)
614 : : start_ix2 = 3;
615 : 544 : else if ((region->inner->sched_modifiers
616 : : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC) != 0)
617 : : start_ix2 = 6;
618 : 530 : else if ((region->inner->sched_modifiers
619 : : & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0)
620 : : start_ix2 = 7;
621 : : else
622 : 16 : start_ix2 = 3;
623 : : break;
624 : 538 : case OMP_CLAUSE_SCHEDULE_DYNAMIC:
625 : 538 : case OMP_CLAUSE_SCHEDULE_GUIDED:
626 : 538 : if ((region->inner->sched_modifiers
627 : : & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
628 : 534 : && !region->inner->has_lastprivate_conditional)
629 : : {
630 : 522 : start_ix2 = 3 + region->inner->sched_kind;
631 : 522 : break;
632 : : }
633 : : /* FALLTHRU */
634 : 16 : default:
635 : 16 : start_ix2 = region->inner->sched_kind;
636 : 16 : break;
637 : : }
638 : 1084 : start_ix2 += (int) BUILT_IN_GOMP_PARALLEL_LOOP_STATIC;
639 : 1084 : start_ix = (enum built_in_function) start_ix2;
640 : 1084 : break;
641 : : case GIMPLE_OMP_SECTIONS:
642 : : start_ix = BUILT_IN_GOMP_PARALLEL_SECTIONS;
643 : : break;
644 : 0 : default:
645 : 0 : gcc_unreachable ();
646 : : }
647 : : }
648 : :
649 : : /* By default, the value of NUM_THREADS is zero (selected at run time)
650 : : and there is no conditional. */
651 : 16266 : cond = NULL_TREE;
652 : 16266 : val = build_int_cst (unsigned_type_node, 0);
653 : 16266 : flags = build_int_cst (unsigned_type_node, 0);
654 : :
655 : 16266 : c = omp_find_clause (clauses, OMP_CLAUSE_IF);
656 : 16266 : if (c)
657 : 945 : cond = OMP_CLAUSE_IF_EXPR (c);
658 : :
659 : 16266 : c = omp_find_clause (clauses, OMP_CLAUSE_NUM_THREADS);
660 : 16266 : if (c)
661 : : {
662 : 2267 : val = OMP_CLAUSE_NUM_THREADS_EXPR (c);
663 : 2267 : clause_loc = OMP_CLAUSE_LOCATION (c);
664 : : }
665 : : else
666 : 13999 : clause_loc = gimple_location (entry_stmt);
667 : :
668 : 16266 : c = omp_find_clause (clauses, OMP_CLAUSE_PROC_BIND);
669 : 16266 : if (c)
670 : 767 : flags = build_int_cst (unsigned_type_node, OMP_CLAUSE_PROC_BIND_KIND (c));
671 : :
672 : : /* Ensure 'val' is of the correct type. */
673 : 16266 : val = fold_convert_loc (clause_loc, unsigned_type_node, val);
674 : :
675 : : /* If we found the clause 'if (cond)', build either
676 : : (cond != 0) or (cond ? val : 1u). */
677 : 16266 : if (cond)
678 : : {
679 : 945 : cond = gimple_boolify (cond);
680 : :
681 : 945 : if (integer_zerop (val))
682 : 158 : val = fold_build2_loc (clause_loc,
683 : : EQ_EXPR, unsigned_type_node, cond,
684 : 158 : build_int_cst (TREE_TYPE (cond), 0));
685 : : else
686 : : {
687 : 787 : basic_block cond_bb, then_bb, else_bb;
688 : 787 : edge e, e_then, e_else;
689 : 787 : tree tmp_then, tmp_else, tmp_join, tmp_var;
690 : :
691 : 787 : tmp_var = create_tmp_var (TREE_TYPE (val));
692 : 787 : if (gimple_in_ssa_p (cfun))
693 : : {
694 : 0 : tmp_then = make_ssa_name (tmp_var);
695 : 0 : tmp_else = make_ssa_name (tmp_var);
696 : 0 : tmp_join = make_ssa_name (tmp_var);
697 : : }
698 : : else
699 : : {
700 : : tmp_then = tmp_var;
701 : : tmp_else = tmp_var;
702 : : tmp_join = tmp_var;
703 : : }
704 : :
705 : 787 : e = split_block_after_labels (bb);
706 : 787 : cond_bb = e->src;
707 : 787 : bb = e->dest;
708 : 787 : remove_edge (e);
709 : :
710 : 787 : then_bb = create_empty_bb (cond_bb);
711 : 787 : else_bb = create_empty_bb (then_bb);
712 : 787 : set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
713 : 787 : set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
714 : :
715 : 787 : stmt = gimple_build_cond_empty (cond);
716 : 787 : gsi = gsi_start_bb (cond_bb);
717 : 787 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
718 : :
719 : 787 : gsi = gsi_start_bb (then_bb);
720 : 787 : expand_omp_build_assign (&gsi, tmp_then, val, true);
721 : :
722 : 787 : gsi = gsi_start_bb (else_bb);
723 : 787 : expand_omp_build_assign (&gsi, tmp_else,
724 : : build_int_cst (unsigned_type_node, 1),
725 : : true);
726 : :
727 : 787 : make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
728 : 787 : make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
729 : 787 : add_bb_to_loop (then_bb, cond_bb->loop_father);
730 : 787 : add_bb_to_loop (else_bb, cond_bb->loop_father);
731 : 787 : e_then = make_edge (then_bb, bb, EDGE_FALLTHRU);
732 : 787 : e_else = make_edge (else_bb, bb, EDGE_FALLTHRU);
733 : :
734 : 787 : if (gimple_in_ssa_p (cfun))
735 : : {
736 : 0 : gphi *phi = create_phi_node (tmp_join, bb);
737 : 0 : add_phi_arg (phi, tmp_then, e_then, UNKNOWN_LOCATION);
738 : 0 : add_phi_arg (phi, tmp_else, e_else, UNKNOWN_LOCATION);
739 : : }
740 : :
741 : 787 : val = tmp_join;
742 : : }
743 : :
744 : 945 : gsi = gsi_start_bb (bb);
745 : 945 : val = force_gimple_operand_gsi (&gsi, val, true, NULL_TREE,
746 : : false, GSI_CONTINUE_LINKING);
747 : : }
748 : :
749 : 16266 : gsi = gsi_last_nondebug_bb (bb);
750 : 16266 : t = gimple_omp_parallel_data_arg (entry_stmt);
751 : 16266 : if (t == NULL)
752 : 2361 : t1 = null_pointer_node;
753 : : else
754 : 13905 : t1 = build_fold_addr_expr (t);
755 : 16266 : tree child_fndecl = gimple_omp_parallel_child_fn (entry_stmt);
756 : 16266 : t2 = build_fold_addr_expr (child_fndecl);
757 : :
758 : 17462 : vec_alloc (args, 4 + vec_safe_length (ws_args));
759 : 16266 : args->quick_push (t2);
760 : 16266 : args->quick_push (t1);
761 : 16266 : args->quick_push (val);
762 : 16266 : if (ws_args)
763 : 1196 : args->splice (*ws_args);
764 : 16266 : args->quick_push (flags);
765 : :
766 : 16266 : t = build_call_expr_loc_vec (UNKNOWN_LOCATION,
767 : : builtin_decl_explicit (start_ix), args);
768 : :
769 : 16266 : if (rtmp)
770 : : {
771 : 59 : tree type = TREE_TYPE (OMP_CLAUSE_DECL (rtmp));
772 : 59 : t = build2 (MODIFY_EXPR, type, OMP_CLAUSE_DECL (rtmp),
773 : : fold_convert (type,
774 : : fold_convert (pointer_sized_int_node, t)));
775 : : }
776 : 16266 : force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
777 : : false, GSI_CONTINUE_LINKING);
778 : 16266 : }
779 : :
780 : : /* Build the function call to GOMP_task to actually
781 : : generate the task operation. BB is the block where to insert the code. */
782 : :
783 : : static void
784 : 3766 : expand_task_call (struct omp_region *region, basic_block bb,
785 : : gomp_task *entry_stmt)
786 : : {
787 : 3766 : tree t1, t2, t3;
788 : 3766 : gimple_stmt_iterator gsi;
789 : 3766 : location_t loc = gimple_location (entry_stmt);
790 : :
791 : 3766 : tree clauses = gimple_omp_task_clauses (entry_stmt);
792 : :
793 : 3766 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
794 : 3766 : tree untied = omp_find_clause (clauses, OMP_CLAUSE_UNTIED);
795 : 3766 : tree mergeable = omp_find_clause (clauses, OMP_CLAUSE_MERGEABLE);
796 : 3766 : tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
797 : 3766 : tree finalc = omp_find_clause (clauses, OMP_CLAUSE_FINAL);
798 : 3766 : tree priority = omp_find_clause (clauses, OMP_CLAUSE_PRIORITY);
799 : 3766 : tree detach = omp_find_clause (clauses, OMP_CLAUSE_DETACH);
800 : :
801 : 7532 : unsigned int iflags
802 : 3766 : = (untied ? GOMP_TASK_FLAG_UNTIED : 0)
803 : 3766 : | (mergeable ? GOMP_TASK_FLAG_MERGEABLE : 0)
804 : 3766 : | (depend ? GOMP_TASK_FLAG_DEPEND : 0);
805 : :
806 : 3766 : bool taskloop_p = gimple_omp_task_taskloop_p (entry_stmt);
807 : 3766 : tree startvar = NULL_TREE, endvar = NULL_TREE, step = NULL_TREE;
808 : 3766 : tree num_tasks = NULL_TREE;
809 : 3766 : bool ull = false;
810 : 3766 : if (taskloop_p)
811 : : {
812 : 1326 : gimple *g = last_nondebug_stmt (region->outer->entry);
813 : 1326 : gcc_assert (gimple_code (g) == GIMPLE_OMP_FOR
814 : : && gimple_omp_for_kind (g) == GF_OMP_FOR_KIND_TASKLOOP);
815 : 1326 : struct omp_for_data fd;
816 : 1326 : omp_extract_for_data (as_a <gomp_for *> (g), &fd, NULL);
817 : 1326 : startvar = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
818 : 1326 : endvar = omp_find_clause (OMP_CLAUSE_CHAIN (startvar),
819 : : OMP_CLAUSE__LOOPTEMP_);
820 : 1326 : startvar = OMP_CLAUSE_DECL (startvar);
821 : 1326 : endvar = OMP_CLAUSE_DECL (endvar);
822 : 1326 : step = fold_convert_loc (loc, fd.iter_type, fd.loop.step);
823 : 1326 : if (fd.loop.cond_code == LT_EXPR)
824 : 1274 : iflags |= GOMP_TASK_FLAG_UP;
825 : 1326 : tree tclauses = gimple_omp_for_clauses (g);
826 : 1326 : num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_NUM_TASKS);
827 : 1326 : if (num_tasks)
828 : : {
829 : 172 : if (OMP_CLAUSE_NUM_TASKS_STRICT (num_tasks))
830 : 3 : iflags |= GOMP_TASK_FLAG_STRICT;
831 : 172 : num_tasks = OMP_CLAUSE_NUM_TASKS_EXPR (num_tasks);
832 : : }
833 : : else
834 : : {
835 : 1154 : num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_GRAINSIZE);
836 : 1154 : if (num_tasks)
837 : : {
838 : 220 : iflags |= GOMP_TASK_FLAG_GRAINSIZE;
839 : 220 : if (OMP_CLAUSE_GRAINSIZE_STRICT (num_tasks))
840 : 3 : iflags |= GOMP_TASK_FLAG_STRICT;
841 : 220 : num_tasks = OMP_CLAUSE_GRAINSIZE_EXPR (num_tasks);
842 : : }
843 : : else
844 : 934 : num_tasks = integer_zero_node;
845 : : }
846 : 1326 : num_tasks = fold_convert_loc (loc, long_integer_type_node, num_tasks);
847 : 1326 : if (ifc == NULL_TREE)
848 : 959 : iflags |= GOMP_TASK_FLAG_IF;
849 : 1326 : if (omp_find_clause (tclauses, OMP_CLAUSE_NOGROUP))
850 : 57 : iflags |= GOMP_TASK_FLAG_NOGROUP;
851 : 1326 : ull = fd.iter_type == long_long_unsigned_type_node;
852 : 1326 : if (omp_find_clause (clauses, OMP_CLAUSE_REDUCTION))
853 : 493 : iflags |= GOMP_TASK_FLAG_REDUCTION;
854 : : }
855 : : else
856 : : {
857 : 2440 : if (priority)
858 : 18 : iflags |= GOMP_TASK_FLAG_PRIORITY;
859 : 2440 : if (detach)
860 : 166 : iflags |= GOMP_TASK_FLAG_DETACH;
861 : : }
862 : :
863 : 3766 : tree flags = build_int_cst (unsigned_type_node, iflags);
864 : :
865 : 3766 : tree cond = boolean_true_node;
866 : 3766 : if (ifc)
867 : : {
868 : 480 : if (taskloop_p)
869 : : {
870 : 367 : tree t = gimple_boolify (OMP_CLAUSE_IF_EXPR (ifc));
871 : 367 : t = fold_build3_loc (loc, COND_EXPR, unsigned_type_node, t,
872 : : build_int_cst (unsigned_type_node,
873 : : GOMP_TASK_FLAG_IF),
874 : : build_int_cst (unsigned_type_node, 0));
875 : 367 : flags = fold_build2_loc (loc, PLUS_EXPR, unsigned_type_node,
876 : : flags, t);
877 : : }
878 : : else
879 : 113 : cond = gimple_boolify (OMP_CLAUSE_IF_EXPR (ifc));
880 : : }
881 : :
882 : 3766 : if (finalc)
883 : : {
884 : 406 : tree t = gimple_boolify (OMP_CLAUSE_FINAL_EXPR (finalc));
885 : 406 : t = fold_build3_loc (loc, COND_EXPR, unsigned_type_node, t,
886 : : build_int_cst (unsigned_type_node,
887 : : GOMP_TASK_FLAG_FINAL),
888 : : build_int_cst (unsigned_type_node, 0));
889 : 406 : flags = fold_build2_loc (loc, PLUS_EXPR, unsigned_type_node, flags, t);
890 : : }
891 : 3766 : if (depend)
892 : 1101 : depend = OMP_CLAUSE_DECL (depend);
893 : : else
894 : 2665 : depend = build_int_cst (ptr_type_node, 0);
895 : 3766 : if (priority)
896 : 368 : priority = fold_convert (integer_type_node,
897 : : OMP_CLAUSE_PRIORITY_EXPR (priority));
898 : : else
899 : 3398 : priority = integer_zero_node;
900 : :
901 : 3766 : gsi = gsi_last_nondebug_bb (bb);
902 : :
903 : 3600 : detach = (detach
904 : 3932 : ? build_fold_addr_expr (OMP_CLAUSE_DECL (detach))
905 : : : null_pointer_node);
906 : :
907 : 3766 : tree t = gimple_omp_task_data_arg (entry_stmt);
908 : 3766 : if (t == NULL)
909 : 678 : t2 = null_pointer_node;
910 : : else
911 : 3088 : t2 = build_fold_addr_expr_loc (loc, t);
912 : 3766 : t1 = build_fold_addr_expr_loc (loc, gimple_omp_task_child_fn (entry_stmt));
913 : 3766 : t = gimple_omp_task_copy_fn (entry_stmt);
914 : 3766 : if (t == NULL)
915 : 3248 : t3 = null_pointer_node;
916 : : else
917 : 518 : t3 = build_fold_addr_expr_loc (loc, t);
918 : :
919 : 3766 : if (taskloop_p)
920 : 2652 : t = build_call_expr (ull
921 : 42 : ? builtin_decl_explicit (BUILT_IN_GOMP_TASKLOOP_ULL)
922 : 1284 : : builtin_decl_explicit (BUILT_IN_GOMP_TASKLOOP),
923 : : 11, t1, t2, t3,
924 : : gimple_omp_task_arg_size (entry_stmt),
925 : : gimple_omp_task_arg_align (entry_stmt), flags,
926 : : num_tasks, priority, startvar, endvar, step);
927 : : else
928 : 2440 : t = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_TASK),
929 : : 10, t1, t2, t3,
930 : : gimple_omp_task_arg_size (entry_stmt),
931 : : gimple_omp_task_arg_align (entry_stmt), cond, flags,
932 : : depend, priority, detach);
933 : :
934 : 3766 : force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
935 : : false, GSI_CONTINUE_LINKING);
936 : 3766 : }
937 : :
938 : : /* Build the function call to GOMP_taskwait_depend to actually
939 : : generate the taskwait operation. BB is the block where to insert the
940 : : code. */
941 : :
942 : : static void
943 : 81 : expand_taskwait_call (basic_block bb, gomp_task *entry_stmt)
944 : : {
945 : 81 : tree clauses = gimple_omp_task_clauses (entry_stmt);
946 : 81 : tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
947 : 81 : if (depend == NULL_TREE)
948 : 0 : return;
949 : :
950 : 81 : depend = OMP_CLAUSE_DECL (depend);
951 : :
952 : 81 : bool nowait = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT) != NULL_TREE;
953 : 81 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
954 : 48 : enum built_in_function f = (nowait
955 : 81 : ? BUILT_IN_GOMP_TASKWAIT_DEPEND_NOWAIT
956 : : : BUILT_IN_GOMP_TASKWAIT_DEPEND);
957 : 81 : tree t = build_call_expr (builtin_decl_explicit (f), 1, depend);
958 : :
959 : 81 : force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
960 : : false, GSI_CONTINUE_LINKING);
961 : : }
962 : :
963 : : /* Build the function call to GOMP_teams_reg to actually
964 : : generate the host teams operation. REGION is the teams region
965 : : being expanded. BB is the block where to insert the code. */
966 : :
967 : : static void
968 : 2535 : expand_teams_call (basic_block bb, gomp_teams *entry_stmt)
969 : : {
970 : 2535 : tree clauses = gimple_omp_teams_clauses (entry_stmt);
971 : 2535 : tree num_teams = omp_find_clause (clauses, OMP_CLAUSE_NUM_TEAMS);
972 : 2535 : if (num_teams == NULL_TREE)
973 : 2252 : num_teams = build_int_cst (unsigned_type_node, 0);
974 : : else
975 : : {
976 : 283 : num_teams = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (num_teams);
977 : 283 : num_teams = fold_convert (unsigned_type_node, num_teams);
978 : : }
979 : 2535 : tree thread_limit = omp_find_clause (clauses, OMP_CLAUSE_THREAD_LIMIT);
980 : 2535 : if (thread_limit == NULL_TREE)
981 : 2394 : thread_limit = build_int_cst (unsigned_type_node, 0);
982 : : else
983 : : {
984 : 141 : thread_limit = OMP_CLAUSE_THREAD_LIMIT_EXPR (thread_limit);
985 : 141 : thread_limit = fold_convert (unsigned_type_node, thread_limit);
986 : : }
987 : :
988 : 2535 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
989 : 2535 : tree t = gimple_omp_teams_data_arg (entry_stmt), t1;
990 : 2535 : if (t == NULL)
991 : 1372 : t1 = null_pointer_node;
992 : : else
993 : 1163 : t1 = build_fold_addr_expr (t);
994 : 2535 : tree child_fndecl = gimple_omp_teams_child_fn (entry_stmt);
995 : 2535 : tree t2 = build_fold_addr_expr (child_fndecl);
996 : :
997 : 2535 : vec<tree, va_gc> *args;
998 : 2535 : vec_alloc (args, 5);
999 : 2535 : args->quick_push (t2);
1000 : 2535 : args->quick_push (t1);
1001 : 2535 : args->quick_push (num_teams);
1002 : 2535 : args->quick_push (thread_limit);
1003 : : /* For future extensibility. */
1004 : 2535 : args->quick_push (build_zero_cst (unsigned_type_node));
1005 : :
1006 : 2535 : t = build_call_expr_loc_vec (UNKNOWN_LOCATION,
1007 : : builtin_decl_explicit (BUILT_IN_GOMP_TEAMS_REG),
1008 : : args);
1009 : :
1010 : 2535 : force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
1011 : : false, GSI_CONTINUE_LINKING);
1012 : 2535 : }
1013 : :
1014 : : /* Chain all the DECLs in LIST by their TREE_CHAIN fields. */
1015 : :
1016 : : static tree
1017 : 42893 : vec2chain (vec<tree, va_gc> *v)
1018 : : {
1019 : 42893 : tree chain = NULL_TREE, t;
1020 : 42893 : unsigned ix;
1021 : :
1022 : 436328 : FOR_EACH_VEC_SAFE_ELT_REVERSE (v, ix, t)
1023 : : {
1024 : 355965 : DECL_CHAIN (t) = chain;
1025 : 355965 : chain = t;
1026 : : }
1027 : :
1028 : 42893 : return chain;
1029 : : }
1030 : :
1031 : : /* Remove barriers in REGION->EXIT's block. Note that this is only
1032 : : valid for GIMPLE_OMP_PARALLEL regions. Since the end of a parallel region
1033 : : is an implicit barrier, any workshare inside the GIMPLE_OMP_PARALLEL that
1034 : : left a barrier at the end of the GIMPLE_OMP_PARALLEL region can now be
1035 : : removed. */
1036 : :
1037 : : static void
1038 : 11768 : remove_exit_barrier (struct omp_region *region)
1039 : : {
1040 : 11768 : gimple_stmt_iterator gsi;
1041 : 11768 : basic_block exit_bb;
1042 : 11768 : edge_iterator ei;
1043 : 11768 : edge e;
1044 : 11768 : gimple *stmt;
1045 : 11768 : int any_addressable_vars = -1;
1046 : :
1047 : 11768 : exit_bb = region->exit;
1048 : :
1049 : : /* If the parallel region doesn't return, we don't have REGION->EXIT
1050 : : block at all. */
1051 : 11768 : if (! exit_bb)
1052 : 1195 : return;
1053 : :
1054 : : /* The last insn in the block will be the parallel's GIMPLE_OMP_RETURN. The
1055 : : workshare's GIMPLE_OMP_RETURN will be in a preceding block. The kinds of
1056 : : statements that can appear in between are extremely limited -- no
1057 : : memory operations at all. Here, we allow nothing at all, so the
1058 : : only thing we allow to precede this GIMPLE_OMP_RETURN is a label. */
1059 : 11741 : gsi = gsi_last_nondebug_bb (exit_bb);
1060 : 11741 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
1061 : 11741 : gsi_prev_nondebug (&gsi);
1062 : 11741 : if (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1063 : : return;
1064 : :
1065 : 21509 : FOR_EACH_EDGE (e, ei, exit_bb->preds)
1066 : : {
1067 : 10936 : gsi = gsi_last_nondebug_bb (e->src);
1068 : 10936 : if (gsi_end_p (gsi))
1069 : 9 : continue;
1070 : 10927 : stmt = gsi_stmt (gsi);
1071 : 10927 : if (gimple_code (stmt) == GIMPLE_OMP_RETURN
1072 : 10927 : && !gimple_omp_return_nowait_p (stmt))
1073 : : {
1074 : : /* OpenMP 3.0 tasks unfortunately prevent this optimization
1075 : : in many cases. If there could be tasks queued, the barrier
1076 : : might be needed to let the tasks run before some local
1077 : : variable of the parallel that the task uses as shared
1078 : : runs out of scope. The task can be spawned either
1079 : : from within current function (this would be easy to check)
1080 : : or from some function it calls and gets passed an address
1081 : : of such a variable. */
1082 : 1047 : if (any_addressable_vars < 0)
1083 : : {
1084 : 1039 : gomp_parallel *parallel_stmt
1085 : 1039 : = as_a <gomp_parallel *> (last_nondebug_stmt (region->entry));
1086 : 1039 : tree child_fun = gimple_omp_parallel_child_fn (parallel_stmt);
1087 : 1039 : tree local_decls, block, decl;
1088 : 1039 : unsigned ix;
1089 : :
1090 : 1039 : any_addressable_vars = 0;
1091 : 8558 : FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (child_fun), ix, decl)
1092 : 7091 : if (TREE_ADDRESSABLE (decl))
1093 : : {
1094 : : any_addressable_vars = 1;
1095 : : break;
1096 : : }
1097 : 1040 : for (block = gimple_block (stmt);
1098 : 1040 : !any_addressable_vars
1099 : 1040 : && block
1100 : 1040 : && TREE_CODE (block) == BLOCK;
1101 : 1 : block = BLOCK_SUPERCONTEXT (block))
1102 : : {
1103 : 2 : for (local_decls = BLOCK_VARS (block);
1104 : 2 : local_decls;
1105 : 0 : local_decls = DECL_CHAIN (local_decls))
1106 : 0 : if (TREE_ADDRESSABLE (local_decls))
1107 : : {
1108 : : any_addressable_vars = 1;
1109 : : break;
1110 : : }
1111 : 2 : if (block == gimple_block (parallel_stmt))
1112 : : break;
1113 : : }
1114 : : }
1115 : 1047 : if (!any_addressable_vars)
1116 : 518 : gimple_omp_return_set_nowait (stmt);
1117 : : }
1118 : : }
1119 : : }
1120 : :
1121 : : static void
1122 : 68077 : remove_exit_barriers (struct omp_region *region)
1123 : : {
1124 : 68077 : if (region->type == GIMPLE_OMP_PARALLEL)
1125 : 11768 : remove_exit_barrier (region);
1126 : :
1127 : 68077 : if (region->inner)
1128 : : {
1129 : 37794 : region = region->inner;
1130 : 37794 : remove_exit_barriers (region);
1131 : 81853 : while (region->next)
1132 : : {
1133 : 6265 : region = region->next;
1134 : 6265 : remove_exit_barriers (region);
1135 : : }
1136 : : }
1137 : 68077 : }
1138 : :
1139 : : /* Optimize omp_get_thread_num () and omp_get_num_threads ()
1140 : : calls. These can't be declared as const functions, but
1141 : : within one parallel body they are constant, so they can be
1142 : : transformed there into __builtin_omp_get_{thread_num,num_threads} ()
1143 : : which are declared const. Similarly for task body, except
1144 : : that in untied task omp_get_thread_num () can change at any task
1145 : : scheduling point. */
1146 : :
1147 : : static void
1148 : 16863 : optimize_omp_library_calls (gimple *entry_stmt)
1149 : : {
1150 : 16863 : basic_block bb;
1151 : 16863 : gimple_stmt_iterator gsi;
1152 : 16863 : tree thr_num_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
1153 : 16863 : tree thr_num_id = DECL_ASSEMBLER_NAME (thr_num_tree);
1154 : 16863 : tree num_thr_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
1155 : 16863 : tree num_thr_id = DECL_ASSEMBLER_NAME (num_thr_tree);
1156 : 16863 : bool untied_task = (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
1157 : 19333 : && omp_find_clause (gimple_omp_task_clauses (entry_stmt),
1158 : 16863 : OMP_CLAUSE_UNTIED) != NULL);
1159 : :
1160 : 294145 : FOR_EACH_BB_FN (bb, cfun)
1161 : 1545331 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1162 : : {
1163 : 990767 : gimple *call = gsi_stmt (gsi);
1164 : 990767 : tree decl;
1165 : :
1166 : 990767 : if (is_gimple_call (call)
1167 : 83498 : && (decl = gimple_call_fndecl (call))
1168 : 76184 : && DECL_EXTERNAL (decl)
1169 : 64434 : && TREE_PUBLIC (decl)
1170 : 1055201 : && DECL_INITIAL (decl) == NULL)
1171 : : {
1172 : 64433 : tree built_in;
1173 : :
1174 : 64433 : if (DECL_NAME (decl) == thr_num_id)
1175 : : {
1176 : : /* In #pragma omp task untied omp_get_thread_num () can change
1177 : : during the execution of the task region. */
1178 : 1305 : if (untied_task)
1179 : 0 : continue;
1180 : 1305 : built_in = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
1181 : : }
1182 : 63128 : else if (DECL_NAME (decl) == num_thr_id)
1183 : 409 : built_in = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
1184 : : else
1185 : 62719 : continue;
1186 : :
1187 : 1714 : if (DECL_ASSEMBLER_NAME (decl) != DECL_ASSEMBLER_NAME (built_in)
1188 : 1714 : || gimple_call_num_args (call) != 0)
1189 : 1188 : continue;
1190 : :
1191 : 526 : if (flag_exceptions && !TREE_NOTHROW (decl))
1192 : 0 : continue;
1193 : :
1194 : 526 : if (TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE
1195 : 1052 : || !types_compatible_p (TREE_TYPE (TREE_TYPE (decl)),
1196 : 526 : TREE_TYPE (TREE_TYPE (built_in))))
1197 : 0 : continue;
1198 : :
1199 : 526 : gimple_call_set_fndecl (call, built_in);
1200 : : }
1201 : : }
1202 : 16863 : }
1203 : :
1204 : : /* Callback for expand_omp_build_assign. Return non-NULL if *tp needs to be
1205 : : regimplified. */
1206 : :
1207 : : static tree
1208 : 262563 : expand_omp_regimplify_p (tree *tp, int *walk_subtrees, void *)
1209 : : {
1210 : 262563 : tree t = *tp;
1211 : :
1212 : : /* Any variable with DECL_VALUE_EXPR needs to be regimplified. */
1213 : 262563 : if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
1214 : : return t;
1215 : :
1216 : 262467 : if (TREE_CODE (t) == ADDR_EXPR)
1217 : 1720 : recompute_tree_invariant_for_addr_expr (t);
1218 : :
1219 : 262467 : *walk_subtrees = !TYPE_P (t) && !DECL_P (t);
1220 : 262467 : return NULL_TREE;
1221 : : }
1222 : :
1223 : : /* Prepend or append TO = FROM assignment before or after *GSI_P. */
1224 : :
1225 : : static void
1226 : 64629 : expand_omp_build_assign (gimple_stmt_iterator *gsi_p, tree to, tree from,
1227 : : bool after)
1228 : : {
1229 : 64629 : bool simple_p = DECL_P (to) && TREE_ADDRESSABLE (to);
1230 : 64629 : from = force_gimple_operand_gsi (gsi_p, from, simple_p, NULL_TREE,
1231 : 64629 : !after, after ? GSI_CONTINUE_LINKING
1232 : : : GSI_SAME_STMT);
1233 : 64629 : gimple *stmt = gimple_build_assign (to, from);
1234 : 64629 : if (after)
1235 : 3489 : gsi_insert_after (gsi_p, stmt, GSI_CONTINUE_LINKING);
1236 : : else
1237 : 61140 : gsi_insert_before (gsi_p, stmt, GSI_SAME_STMT);
1238 : 64629 : if (walk_tree (&from, expand_omp_regimplify_p, NULL, NULL)
1239 : 64629 : || walk_tree (&to, expand_omp_regimplify_p, NULL, NULL))
1240 : : {
1241 : 80 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1242 : 80 : gimple_regimplify_operands (stmt, &gsi);
1243 : : }
1244 : 64629 : }
1245 : :
1246 : : /* Prepend or append LHS CODE RHS condition before or after *GSI_P. */
1247 : :
1248 : : static gcond *
1249 : 8413 : expand_omp_build_cond (gimple_stmt_iterator *gsi_p, enum tree_code code,
1250 : : tree lhs, tree rhs, bool after = false)
1251 : : {
1252 : 8413 : gcond *cond_stmt = gimple_build_cond (code, lhs, rhs, NULL_TREE, NULL_TREE);
1253 : 8413 : if (after)
1254 : 236 : gsi_insert_after (gsi_p, cond_stmt, GSI_CONTINUE_LINKING);
1255 : : else
1256 : 8177 : gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT);
1257 : 8413 : if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
1258 : : NULL, NULL)
1259 : 8413 : || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p,
1260 : : NULL, NULL))
1261 : : {
1262 : 6 : gimple_stmt_iterator gsi = gsi_for_stmt (cond_stmt);
1263 : 6 : gimple_regimplify_operands (cond_stmt, &gsi);
1264 : : }
1265 : 8413 : return cond_stmt;
1266 : : }
1267 : :
1268 : : /* Expand the OpenMP parallel or task directive starting at REGION. */
1269 : :
1270 : : static void
1271 : 22648 : expand_omp_taskreg (struct omp_region *region)
1272 : : {
1273 : 22648 : basic_block entry_bb, exit_bb, new_bb;
1274 : 22648 : struct function *child_cfun;
1275 : 22648 : tree child_fn, block, t;
1276 : 22648 : gimple_stmt_iterator gsi;
1277 : 22648 : gimple *entry_stmt, *stmt;
1278 : 22648 : edge e;
1279 : 22648 : vec<tree, va_gc> *ws_args;
1280 : :
1281 : 22648 : entry_stmt = last_nondebug_stmt (region->entry);
1282 : 22648 : if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
1283 : 22648 : && gimple_omp_task_taskwait_p (entry_stmt))
1284 : : {
1285 : 81 : new_bb = region->entry;
1286 : 81 : gsi = gsi_last_nondebug_bb (region->entry);
1287 : 81 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TASK);
1288 : 81 : gsi_remove (&gsi, true);
1289 : 81 : expand_taskwait_call (new_bb, as_a <gomp_task *> (entry_stmt));
1290 : 81 : return;
1291 : : }
1292 : :
1293 : 22567 : child_fn = gimple_omp_taskreg_child_fn (entry_stmt);
1294 : 22567 : child_cfun = DECL_STRUCT_FUNCTION (child_fn);
1295 : :
1296 : 22567 : entry_bb = region->entry;
1297 : 22567 : if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK)
1298 : 3766 : exit_bb = region->cont;
1299 : : else
1300 : 18801 : exit_bb = region->exit;
1301 : :
1302 : 22567 : if (is_combined_parallel (region))
1303 : 1196 : ws_args = region->ws_args;
1304 : : else
1305 : : ws_args = NULL;
1306 : :
1307 : 22567 : if (child_cfun->cfg)
1308 : : {
1309 : : /* Due to inlining, it may happen that we have already outlined
1310 : : the region, in which case all we need to do is make the
1311 : : sub-graph unreachable and emit the parallel call. */
1312 : 0 : edge entry_succ_e, exit_succ_e;
1313 : :
1314 : 0 : entry_succ_e = single_succ_edge (entry_bb);
1315 : :
1316 : 0 : gsi = gsi_last_nondebug_bb (entry_bb);
1317 : 0 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_PARALLEL
1318 : : || gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TASK
1319 : : || gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TEAMS);
1320 : 0 : gsi_remove (&gsi, true);
1321 : :
1322 : 0 : new_bb = entry_bb;
1323 : 0 : if (exit_bb)
1324 : : {
1325 : 0 : exit_succ_e = single_succ_edge (exit_bb);
1326 : 0 : make_edge (new_bb, exit_succ_e->dest, EDGE_FALLTHRU);
1327 : : }
1328 : 0 : remove_edge_and_dominated_blocks (entry_succ_e);
1329 : : }
1330 : : else
1331 : : {
1332 : 22567 : unsigned srcidx, dstidx, num;
1333 : :
1334 : : /* If the parallel region needs data sent from the parent
1335 : : function, then the very first statement (except possible
1336 : : tree profile counter updates) of the parallel body
1337 : : is a copy assignment .OMP_DATA_I = &.OMP_DATA_O. Since
1338 : : &.OMP_DATA_O is passed as an argument to the child function,
1339 : : we need to replace it with the argument as seen by the child
1340 : : function.
1341 : :
1342 : : In most cases, this will end up being the identity assignment
1343 : : .OMP_DATA_I = .OMP_DATA_I. However, if the parallel body had
1344 : : a function call that has been inlined, the original PARM_DECL
1345 : : .OMP_DATA_I may have been converted into a different local
1346 : : variable. In which case, we need to keep the assignment. */
1347 : 22567 : if (gimple_omp_taskreg_data_arg (entry_stmt))
1348 : : {
1349 : 18156 : basic_block entry_succ_bb
1350 : 33224 : = single_succ_p (entry_bb) ? single_succ (entry_bb)
1351 : 3088 : : FALLTHRU_EDGE (entry_bb)->dest;
1352 : 18156 : tree arg;
1353 : 18156 : gimple *parcopy_stmt = NULL;
1354 : :
1355 : 36312 : for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
1356 : : {
1357 : 18156 : gimple *stmt;
1358 : :
1359 : 18156 : gcc_assert (!gsi_end_p (gsi));
1360 : 18156 : stmt = gsi_stmt (gsi);
1361 : 18156 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
1362 : 0 : continue;
1363 : :
1364 : 18156 : if (gimple_num_ops (stmt) == 2)
1365 : : {
1366 : 18156 : tree arg = gimple_assign_rhs1 (stmt);
1367 : :
1368 : : /* We're ignore the subcode because we're
1369 : : effectively doing a STRIP_NOPS. */
1370 : :
1371 : 18156 : if (TREE_CODE (arg) == ADDR_EXPR
1372 : 18156 : && (TREE_OPERAND (arg, 0)
1373 : 18156 : == gimple_omp_taskreg_data_arg (entry_stmt)))
1374 : : {
1375 : 18156 : parcopy_stmt = stmt;
1376 : 18156 : break;
1377 : : }
1378 : : }
1379 : : }
1380 : :
1381 : 18156 : gcc_assert (parcopy_stmt != NULL);
1382 : 18156 : arg = DECL_ARGUMENTS (child_fn);
1383 : :
1384 : 18156 : if (!gimple_in_ssa_p (cfun))
1385 : : {
1386 : 17975 : if (gimple_assign_lhs (parcopy_stmt) == arg)
1387 : 17975 : gsi_remove (&gsi, true);
1388 : : else
1389 : : {
1390 : : /* ?? Is setting the subcode really necessary ?? */
1391 : 0 : gimple_omp_set_subcode (parcopy_stmt, TREE_CODE (arg));
1392 : 0 : gimple_assign_set_rhs1 (parcopy_stmt, arg);
1393 : : }
1394 : : }
1395 : : else
1396 : : {
1397 : 181 : tree lhs = gimple_assign_lhs (parcopy_stmt);
1398 : 181 : gcc_assert (SSA_NAME_VAR (lhs) == arg);
1399 : : /* We'd like to set the rhs to the default def in the child_fn,
1400 : : but it's too early to create ssa names in the child_fn.
1401 : : Instead, we set the rhs to the parm. In
1402 : : move_sese_region_to_fn, we introduce a default def for the
1403 : : parm, map the parm to it's default def, and once we encounter
1404 : : this stmt, replace the parm with the default def. */
1405 : 181 : gimple_assign_set_rhs1 (parcopy_stmt, arg);
1406 : 181 : update_stmt (parcopy_stmt);
1407 : : }
1408 : : }
1409 : :
1410 : : /* Declare local variables needed in CHILD_CFUN. */
1411 : 22567 : block = DECL_INITIAL (child_fn);
1412 : 22567 : BLOCK_VARS (block) = vec2chain (child_cfun->local_decls);
1413 : : /* The gimplifier could record temporaries in parallel/task block
1414 : : rather than in containing function's local_decls chain,
1415 : : which would mean cgraph missed finalizing them. Do it now. */
1416 : 253162 : for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
1417 : 230595 : if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
1418 : 0 : varpool_node::finalize_decl (t);
1419 : 22567 : DECL_SAVED_TREE (child_fn) = NULL;
1420 : : /* We'll create a CFG for child_fn, so no gimple body is needed. */
1421 : 22567 : gimple_set_body (child_fn, NULL);
1422 : 22567 : TREE_USED (block) = 1;
1423 : :
1424 : : /* Reset DECL_CONTEXT on function arguments. */
1425 : 45134 : for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
1426 : 22567 : DECL_CONTEXT (t) = child_fn;
1427 : :
1428 : : /* Split ENTRY_BB at GIMPLE_OMP_PARALLEL or GIMPLE_OMP_TASK,
1429 : : so that it can be moved to the child function. */
1430 : 22567 : gsi = gsi_last_nondebug_bb (entry_bb);
1431 : 22567 : stmt = gsi_stmt (gsi);
1432 : 22567 : gcc_assert (stmt && (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
1433 : : || gimple_code (stmt) == GIMPLE_OMP_TASK
1434 : : || gimple_code (stmt) == GIMPLE_OMP_TEAMS));
1435 : 22567 : e = split_block (entry_bb, stmt);
1436 : 22567 : gsi_remove (&gsi, true);
1437 : 22567 : entry_bb = e->dest;
1438 : 22567 : edge e2 = NULL;
1439 : 22567 : if (gimple_code (entry_stmt) != GIMPLE_OMP_TASK)
1440 : 18801 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
1441 : : else
1442 : : {
1443 : 3766 : e2 = make_edge (e->src, BRANCH_EDGE (entry_bb)->dest, EDGE_ABNORMAL);
1444 : 3766 : gcc_assert (e2->dest == region->exit);
1445 : 3766 : remove_edge (BRANCH_EDGE (entry_bb));
1446 : 3766 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e->src);
1447 : 3766 : gsi = gsi_last_nondebug_bb (region->exit);
1448 : 3766 : gcc_assert (!gsi_end_p (gsi)
1449 : : && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
1450 : 3766 : gsi_remove (&gsi, true);
1451 : : }
1452 : :
1453 : : /* Convert GIMPLE_OMP_{RETURN,CONTINUE} into a RETURN_EXPR. */
1454 : 22567 : if (exit_bb)
1455 : : {
1456 : 22523 : gsi = gsi_last_nondebug_bb (exit_bb);
1457 : 41297 : gcc_assert (!gsi_end_p (gsi)
1458 : : && (gimple_code (gsi_stmt (gsi))
1459 : : == (e2 ? GIMPLE_OMP_CONTINUE : GIMPLE_OMP_RETURN)));
1460 : 22523 : stmt = gimple_build_return (NULL);
1461 : 22523 : gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
1462 : 22523 : gsi_remove (&gsi, true);
1463 : : }
1464 : :
1465 : : /* Move the parallel region into CHILD_CFUN. */
1466 : :
1467 : 22567 : if (gimple_in_ssa_p (cfun))
1468 : : {
1469 : 193 : init_tree_ssa (child_cfun);
1470 : 193 : init_ssa_operands (child_cfun);
1471 : 193 : child_cfun->gimple_df->in_ssa_p = true;
1472 : 193 : block = NULL_TREE;
1473 : : }
1474 : : else
1475 : 22374 : block = gimple_block (entry_stmt);
1476 : :
1477 : 22567 : new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
1478 : 22567 : if (exit_bb)
1479 : 22523 : single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
1480 : 22567 : if (e2)
1481 : : {
1482 : 3766 : basic_block dest_bb = e2->dest;
1483 : 3766 : if (!exit_bb)
1484 : 17 : make_edge (new_bb, dest_bb, EDGE_FALLTHRU);
1485 : 3766 : remove_edge (e2);
1486 : 3766 : set_immediate_dominator (CDI_DOMINATORS, dest_bb, new_bb);
1487 : : }
1488 : : /* When the OMP expansion process cannot guarantee an up-to-date
1489 : : loop tree arrange for the child function to fixup loops. */
1490 : 22567 : if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1491 : 22374 : child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
1492 : :
1493 : : /* Remove non-local VAR_DECLs from child_cfun->local_decls list. */
1494 : 22567 : num = vec_safe_length (child_cfun->local_decls);
1495 : 1056925 : for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
1496 : : {
1497 : 1034358 : t = (*child_cfun->local_decls)[srcidx];
1498 : 1034358 : if (DECL_CONTEXT (t) == cfun->decl)
1499 : 230595 : continue;
1500 : 803763 : if (srcidx != dstidx)
1501 : 800223 : (*child_cfun->local_decls)[dstidx] = t;
1502 : 803763 : dstidx++;
1503 : : }
1504 : 22567 : if (dstidx != num)
1505 : 20440 : vec_safe_truncate (child_cfun->local_decls, dstidx);
1506 : :
1507 : : /* Inform the callgraph about the new function. */
1508 : 22567 : child_cfun->curr_properties = cfun->curr_properties;
1509 : 22567 : child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
1510 : 22567 : child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
1511 : 22567 : cgraph_node *node = cgraph_node::get_create (child_fn);
1512 : 22567 : node->parallelized_function = 1;
1513 : 45134 : node->has_omp_variant_constructs
1514 : 22567 : |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
1515 : 22567 : cgraph_node::add_new_function (child_fn, true);
1516 : :
1517 : 22567 : bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
1518 : 22567 : && !DECL_ASSEMBLER_NAME_SET_P (child_fn);
1519 : :
1520 : : /* Fix the callgraph edges for child_cfun. Those for cfun will be
1521 : : fixed in a following pass. */
1522 : 22567 : push_cfun (child_cfun);
1523 : 22567 : if (need_asm)
1524 : 22374 : assign_assembler_name_if_needed (child_fn);
1525 : :
1526 : 22567 : if (optimize)
1527 : 16863 : optimize_omp_library_calls (entry_stmt);
1528 : 22567 : update_max_bb_count ();
1529 : 22567 : cgraph_edge::rebuild_edges ();
1530 : :
1531 : : /* Some EH regions might become dead, see PR34608. If
1532 : : pass_cleanup_cfg isn't the first pass to happen with the
1533 : : new child, these dead EH edges might cause problems.
1534 : : Clean them up now. */
1535 : 22567 : if (flag_exceptions)
1536 : : {
1537 : 10177 : basic_block bb;
1538 : 10177 : bool changed = false;
1539 : :
1540 : 135427 : FOR_EACH_BB_FN (bb, cfun)
1541 : 125250 : changed |= gimple_purge_dead_eh_edges (bb);
1542 : 10177 : if (changed)
1543 : 0 : cleanup_tree_cfg ();
1544 : : }
1545 : 22567 : if (gimple_in_ssa_p (cfun))
1546 : 193 : update_ssa (TODO_update_ssa);
1547 : 22567 : if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1548 : 193 : verify_loop_structure ();
1549 : 22567 : pop_cfun ();
1550 : :
1551 : 22567 : if (dump_file && !gimple_in_ssa_p (cfun))
1552 : : {
1553 : 20 : omp_any_child_fn_dumped = true;
1554 : 20 : dump_function_header (dump_file, child_fn, dump_flags);
1555 : 20 : dump_function_to_file (child_fn, dump_file, dump_flags);
1556 : : }
1557 : : }
1558 : :
1559 : 22567 : adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
1560 : :
1561 : 22567 : if (gimple_code (entry_stmt) == GIMPLE_OMP_PARALLEL)
1562 : 16266 : expand_parallel_call (region, new_bb,
1563 : : as_a <gomp_parallel *> (entry_stmt), ws_args);
1564 : 6301 : else if (gimple_code (entry_stmt) == GIMPLE_OMP_TEAMS)
1565 : 2535 : expand_teams_call (new_bb, as_a <gomp_teams *> (entry_stmt));
1566 : : else
1567 : 3766 : expand_task_call (region, new_bb, as_a <gomp_task *> (entry_stmt));
1568 : : }
1569 : :
1570 : : /* Information about members of an OpenACC collapsed loop nest. */
1571 : :
1572 : : struct oacc_collapse
1573 : : {
1574 : : tree base; /* Base value. */
1575 : : tree iters; /* Number of steps. */
1576 : : tree step; /* Step size. */
1577 : : tree tile; /* Tile increment (if tiled). */
1578 : : tree outer; /* Tile iterator var. */
1579 : : };
1580 : :
1581 : : /* Helper for expand_oacc_for. Determine collapsed loop information.
1582 : : Fill in COUNTS array. Emit any initialization code before GSI.
1583 : : Return the calculated outer loop bound of BOUND_TYPE. */
1584 : :
1585 : : static tree
1586 : 594 : expand_oacc_collapse_init (const struct omp_for_data *fd,
1587 : : gimple_stmt_iterator *gsi,
1588 : : oacc_collapse *counts, tree diff_type,
1589 : : tree bound_type, location_t loc)
1590 : : {
1591 : 594 : tree tiling = fd->tiling;
1592 : 594 : tree total = build_int_cst (bound_type, 1);
1593 : 594 : int ix;
1594 : :
1595 : 594 : gcc_assert (integer_onep (fd->loop.step));
1596 : 594 : gcc_assert (integer_zerop (fd->loop.n1));
1597 : :
1598 : : /* When tiling, the first operand of the tile clause applies to the
1599 : : innermost loop, and we work outwards from there. Seems
1600 : : backwards, but whatever. */
1601 : 1890 : for (ix = fd->collapse; ix--;)
1602 : : {
1603 : 1296 : const omp_for_data_loop *loop = &fd->loops[ix];
1604 : :
1605 : 1296 : tree iter_type = TREE_TYPE (loop->v);
1606 : 1296 : tree plus_type = iter_type;
1607 : :
1608 : 1296 : gcc_assert (loop->cond_code == LT_EXPR || loop->cond_code == GT_EXPR);
1609 : :
1610 : 1296 : if (POINTER_TYPE_P (iter_type))
1611 : 0 : plus_type = sizetype;
1612 : :
1613 : 1296 : if (tiling)
1614 : : {
1615 : 284 : tree num = build_int_cst (integer_type_node, fd->collapse);
1616 : 284 : tree loop_no = build_int_cst (integer_type_node, ix);
1617 : 284 : tree tile = TREE_VALUE (tiling);
1618 : 284 : gcall *call
1619 : 284 : = gimple_build_call_internal (IFN_GOACC_TILE, 5, num, loop_no, tile,
1620 : : /* gwv-outer=*/integer_zero_node,
1621 : : /* gwv-inner=*/integer_zero_node);
1622 : :
1623 : 284 : counts[ix].outer = create_tmp_var (iter_type, ".outer");
1624 : 284 : counts[ix].tile = create_tmp_var (diff_type, ".tile");
1625 : 284 : gimple_call_set_lhs (call, counts[ix].tile);
1626 : 284 : gimple_set_location (call, loc);
1627 : 284 : gsi_insert_before (gsi, call, GSI_SAME_STMT);
1628 : :
1629 : 284 : tiling = TREE_CHAIN (tiling);
1630 : : }
1631 : : else
1632 : : {
1633 : 1012 : counts[ix].tile = NULL;
1634 : 1012 : counts[ix].outer = loop->v;
1635 : : }
1636 : :
1637 : 1296 : tree b = loop->n1;
1638 : 1296 : tree e = loop->n2;
1639 : 1296 : tree s = loop->step;
1640 : 1296 : bool up = loop->cond_code == LT_EXPR;
1641 : 1344 : tree dir = build_int_cst (diff_type, up ? +1 : -1);
1642 : 1296 : bool negating;
1643 : 1296 : tree expr;
1644 : :
1645 : 1296 : b = force_gimple_operand_gsi (gsi, b, true, NULL_TREE,
1646 : : true, GSI_SAME_STMT);
1647 : 1296 : e = force_gimple_operand_gsi (gsi, e, true, NULL_TREE,
1648 : : true, GSI_SAME_STMT);
1649 : :
1650 : : /* Convert the step, avoiding possible unsigned->signed overflow. */
1651 : 1296 : negating = !up && TYPE_UNSIGNED (TREE_TYPE (s));
1652 : 0 : if (negating)
1653 : 0 : s = fold_build1 (NEGATE_EXPR, TREE_TYPE (s), s);
1654 : 1296 : s = fold_convert (diff_type, s);
1655 : 1296 : if (negating)
1656 : 0 : s = fold_build1 (NEGATE_EXPR, diff_type, s);
1657 : 1296 : s = force_gimple_operand_gsi (gsi, s, true, NULL_TREE,
1658 : : true, GSI_SAME_STMT);
1659 : :
1660 : : /* Determine the range, avoiding possible unsigned->signed overflow. */
1661 : 1296 : negating = !up && TYPE_UNSIGNED (iter_type);
1662 : 2592 : expr = fold_build2 (MINUS_EXPR, plus_type,
1663 : : fold_convert (plus_type, negating ? b : e),
1664 : : fold_convert (plus_type, negating ? e : b));
1665 : 1296 : expr = fold_convert (diff_type, expr);
1666 : 1296 : if (negating)
1667 : 0 : expr = fold_build1 (NEGATE_EXPR, diff_type, expr);
1668 : 1296 : tree range = force_gimple_operand_gsi
1669 : 1296 : (gsi, expr, true, NULL_TREE, true, GSI_SAME_STMT);
1670 : :
1671 : : /* Determine number of iterations. */
1672 : 1296 : expr = fold_build2 (MINUS_EXPR, diff_type, range, dir);
1673 : 1296 : expr = fold_build2 (PLUS_EXPR, diff_type, expr, s);
1674 : 1296 : expr = fold_build2 (TRUNC_DIV_EXPR, diff_type, expr, s);
1675 : :
1676 : 1296 : tree iters = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE,
1677 : : true, GSI_SAME_STMT);
1678 : :
1679 : 1296 : counts[ix].base = b;
1680 : 1296 : counts[ix].iters = iters;
1681 : 1296 : counts[ix].step = s;
1682 : :
1683 : 1296 : total = fold_build2 (MULT_EXPR, bound_type, total,
1684 : : fold_convert (bound_type, iters));
1685 : : }
1686 : :
1687 : 594 : return total;
1688 : : }
1689 : :
1690 : : /* Emit initializers for collapsed loop members. INNER is true if
1691 : : this is for the element loop of a TILE. IVAR is the outer
1692 : : loop iteration variable, from which collapsed loop iteration values
1693 : : are calculated. COUNTS array has been initialized by
1694 : : expand_oacc_collapse_inits. */
1695 : :
1696 : : static void
1697 : 771 : expand_oacc_collapse_vars (const struct omp_for_data *fd, bool inner,
1698 : : gimple_stmt_iterator *gsi,
1699 : : const oacc_collapse *counts, tree ivar,
1700 : : tree diff_type)
1701 : : {
1702 : 771 : tree ivar_type = TREE_TYPE (ivar);
1703 : :
1704 : : /* The most rapidly changing iteration variable is the innermost
1705 : : one. */
1706 : 2351 : for (int ix = fd->collapse; ix--;)
1707 : : {
1708 : 1580 : const omp_for_data_loop *loop = &fd->loops[ix];
1709 : 1580 : const oacc_collapse *collapse = &counts[ix];
1710 : 1580 : tree v = inner ? loop->v : collapse->outer;
1711 : 1580 : tree iter_type = TREE_TYPE (v);
1712 : 1580 : tree plus_type = iter_type;
1713 : 1580 : enum tree_code plus_code = PLUS_EXPR;
1714 : 1580 : tree expr;
1715 : :
1716 : 1580 : if (POINTER_TYPE_P (iter_type))
1717 : : {
1718 : 0 : plus_code = POINTER_PLUS_EXPR;
1719 : 0 : plus_type = sizetype;
1720 : : }
1721 : :
1722 : 1580 : expr = ivar;
1723 : 1580 : if (ix)
1724 : : {
1725 : 809 : tree mod = fold_convert (ivar_type, collapse->iters);
1726 : 809 : ivar = fold_build2 (TRUNC_DIV_EXPR, ivar_type, expr, mod);
1727 : 809 : expr = fold_build2 (TRUNC_MOD_EXPR, ivar_type, expr, mod);
1728 : 809 : ivar = force_gimple_operand_gsi (gsi, ivar, true, NULL_TREE,
1729 : : true, GSI_SAME_STMT);
1730 : : }
1731 : :
1732 : 1580 : expr = fold_build2 (MULT_EXPR, diff_type, fold_convert (diff_type, expr),
1733 : : fold_convert (diff_type, collapse->step));
1734 : 1580 : expr = fold_build2 (plus_code, iter_type,
1735 : : inner ? collapse->outer : collapse->base,
1736 : : fold_convert (plus_type, expr));
1737 : 1580 : expr = force_gimple_operand_gsi (gsi, expr, false, NULL_TREE,
1738 : : true, GSI_SAME_STMT);
1739 : 1580 : gassign *ass = gimple_build_assign (v, expr);
1740 : 1580 : gsi_insert_before (gsi, ass, GSI_SAME_STMT);
1741 : : }
1742 : 771 : }
1743 : :
1744 : : /* Helper function for expand_omp_{for_*,simd}. If this is the outermost
1745 : : of the combined collapse > 1 loop constructs, generate code like:
1746 : : if (__builtin_expect (N32 cond3 N31, 0)) goto ZERO_ITER_BB;
1747 : : if (cond3 is <)
1748 : : adj = STEP3 - 1;
1749 : : else
1750 : : adj = STEP3 + 1;
1751 : : count3 = (adj + N32 - N31) / STEP3;
1752 : : if (__builtin_expect (N22 cond2 N21, 0)) goto ZERO_ITER_BB;
1753 : : if (cond2 is <)
1754 : : adj = STEP2 - 1;
1755 : : else
1756 : : adj = STEP2 + 1;
1757 : : count2 = (adj + N22 - N21) / STEP2;
1758 : : if (__builtin_expect (N12 cond1 N11, 0)) goto ZERO_ITER_BB;
1759 : : if (cond1 is <)
1760 : : adj = STEP1 - 1;
1761 : : else
1762 : : adj = STEP1 + 1;
1763 : : count1 = (adj + N12 - N11) / STEP1;
1764 : : count = count1 * count2 * count3;
1765 : : Furthermore, if ZERO_ITER_BB is NULL, create a BB which does:
1766 : : count = 0;
1767 : : and set ZERO_ITER_BB to that bb. If this isn't the outermost
1768 : : of the combined loop constructs, just initialize COUNTS array
1769 : : from the _looptemp_ clauses. For loop nests with non-rectangular
1770 : : loops, do this only for the rectangular loops. Then pick
1771 : : the loops which reference outer vars in their bound expressions
1772 : : and the loops which they refer to and for this sub-nest compute
1773 : : number of iterations. For triangular loops use Faulhaber's formula,
1774 : : otherwise as a fallback, compute by iterating the loops.
1775 : : If e.g. the sub-nest is
1776 : : for (I = N11; I COND1 N12; I += STEP1)
1777 : : for (J = M21 * I + N21; J COND2 M22 * I + N22; J += STEP2)
1778 : : for (K = M31 * J + N31; K COND3 M32 * J + N32; K += STEP3)
1779 : : do:
1780 : : COUNT = 0;
1781 : : for (tmpi = N11; tmpi COND1 N12; tmpi += STEP1)
1782 : : for (tmpj = M21 * tmpi + N21;
1783 : : tmpj COND2 M22 * tmpi + N22; tmpj += STEP2)
1784 : : {
1785 : : int tmpk1 = M31 * tmpj + N31;
1786 : : int tmpk2 = M32 * tmpj + N32;
1787 : : if (tmpk1 COND3 tmpk2)
1788 : : {
1789 : : if (COND3 is <)
1790 : : adj = STEP3 - 1;
1791 : : else
1792 : : adj = STEP3 + 1;
1793 : : COUNT += (adj + tmpk2 - tmpk1) / STEP3;
1794 : : }
1795 : : }
1796 : : and finally multiply the counts of the rectangular loops not
1797 : : in the sub-nest with COUNT. Also, as counts[fd->last_nonrect]
1798 : : store number of iterations of the loops from fd->first_nonrect
1799 : : to fd->last_nonrect inclusive, i.e. the above COUNT multiplied
1800 : : by the counts of rectangular loops not referenced in any non-rectangular
1801 : : loops sandwitched in between those. */
1802 : :
1803 : : /* NOTE: It *could* be better to moosh all of the BBs together,
1804 : : creating one larger BB with all the computation and the unexpected
1805 : : jump at the end. I.e.
1806 : :
1807 : : bool zero3, zero2, zero1, zero;
1808 : :
1809 : : zero3 = N32 c3 N31;
1810 : : count3 = (N32 - N31) /[cl] STEP3;
1811 : : zero2 = N22 c2 N21;
1812 : : count2 = (N22 - N21) /[cl] STEP2;
1813 : : zero1 = N12 c1 N11;
1814 : : count1 = (N12 - N11) /[cl] STEP1;
1815 : : zero = zero3 || zero2 || zero1;
1816 : : count = count1 * count2 * count3;
1817 : : if (__builtin_expect(zero, false)) goto zero_iter_bb;
1818 : :
1819 : : After all, we expect the zero=false, and thus we expect to have to
1820 : : evaluate all of the comparison expressions, so short-circuiting
1821 : : oughtn't be a win. Since the condition isn't protecting a
1822 : : denominator, we're not concerned about divide-by-zero, so we can
1823 : : fully evaluate count even if a numerator turned out to be wrong.
1824 : :
1825 : : It seems like putting this all together would create much better
1826 : : scheduling opportunities, and less pressure on the chip's branch
1827 : : predictor. */
1828 : :
1829 : : static void
1830 : 10407 : expand_omp_for_init_counts (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
1831 : : basic_block &entry_bb, tree *counts,
1832 : : basic_block &zero_iter1_bb, int &first_zero_iter1,
1833 : : basic_block &zero_iter2_bb, int &first_zero_iter2,
1834 : : basic_block &l2_dom_bb)
1835 : : {
1836 : 10407 : tree t, type = TREE_TYPE (fd->loop.v);
1837 : 10407 : edge e, ne;
1838 : 10407 : int i;
1839 : :
1840 : : /* Collapsed loops need work for expansion into SSA form. */
1841 : 10407 : gcc_assert (!gimple_in_ssa_p (cfun));
1842 : :
1843 : 10407 : if (gimple_omp_for_combined_into_p (fd->for_stmt)
1844 : 10407 : && TREE_CODE (fd->loop.n2) != INTEGER_CST)
1845 : : {
1846 : 2681 : gcc_assert (fd->ordered == 0);
1847 : : /* First two _looptemp_ clauses are for istart/iend, counts[0]
1848 : : isn't supposed to be handled, as the inner loop doesn't
1849 : : use it. */
1850 : 2681 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
1851 : : OMP_CLAUSE__LOOPTEMP_);
1852 : 2681 : gcc_assert (innerc);
1853 : 10520 : for (i = 0; i < fd->collapse; i++)
1854 : : {
1855 : 7839 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
1856 : : OMP_CLAUSE__LOOPTEMP_);
1857 : 7839 : gcc_assert (innerc);
1858 : 7839 : if (i)
1859 : 5158 : counts[i] = OMP_CLAUSE_DECL (innerc);
1860 : : else
1861 : 2681 : counts[0] = NULL_TREE;
1862 : : }
1863 : 2681 : if (fd->non_rect
1864 : 112 : && fd->last_nonrect == fd->first_nonrect + 1
1865 : 2745 : && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
1866 : : {
1867 : : tree c[4];
1868 : 300 : for (i = 0; i < 4; i++)
1869 : : {
1870 : 240 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
1871 : : OMP_CLAUSE__LOOPTEMP_);
1872 : 240 : gcc_assert (innerc);
1873 : 240 : c[i] = OMP_CLAUSE_DECL (innerc);
1874 : : }
1875 : 60 : counts[0] = c[0];
1876 : 60 : fd->first_inner_iterations = c[1];
1877 : 60 : fd->factor = c[2];
1878 : 60 : fd->adjn1 = c[3];
1879 : : }
1880 : 2681 : return;
1881 : : }
1882 : :
1883 : 8619 : for (i = fd->collapse; i < fd->ordered; i++)
1884 : : {
1885 : 893 : tree itype = TREE_TYPE (fd->loops[i].v);
1886 : 893 : counts[i] = NULL_TREE;
1887 : 893 : t = fold_binary (fd->loops[i].cond_code, boolean_type_node,
1888 : : fold_convert (itype, fd->loops[i].n1),
1889 : : fold_convert (itype, fd->loops[i].n2));
1890 : 893 : if (t && integer_zerop (t))
1891 : : {
1892 : 0 : for (i = fd->collapse; i < fd->ordered; i++)
1893 : 0 : counts[i] = build_int_cst (type, 0);
1894 : : break;
1895 : : }
1896 : : }
1897 : 7726 : bool rect_count_seen = false;
1898 : 7726 : bool init_n2 = SSA_VAR_P (fd->loop.n2) && zero_iter1_bb;
1899 : 30024 : for (i = 0; i < (fd->ordered ? fd->ordered : fd->collapse); i++)
1900 : : {
1901 : 22298 : tree itype = TREE_TYPE (fd->loops[i].v);
1902 : :
1903 : 22298 : if (i >= fd->collapse && counts[i])
1904 : 0 : continue;
1905 : 22298 : if (fd->non_rect)
1906 : : {
1907 : : /* Skip loops that use outer iterators in their expressions
1908 : : during this phase. */
1909 : 1050 : if (fd->loops[i].m1 || fd->loops[i].m2)
1910 : : {
1911 : 432 : counts[i] = build_zero_cst (type);
1912 : 432 : continue;
1913 : : }
1914 : : }
1915 : 21866 : if ((SSA_VAR_P (fd->loop.n2) || i >= fd->collapse)
1916 : 9502 : && ((t = fold_binary (fd->loops[i].cond_code, boolean_type_node,
1917 : : fold_convert (itype, fd->loops[i].n1),
1918 : : fold_convert (itype, fd->loops[i].n2)))
1919 : 5881 : == NULL_TREE || !integer_onep (t)))
1920 : : {
1921 : 5777 : gcond *cond_stmt;
1922 : 5777 : tree n1, n2;
1923 : 5777 : if (init_n2 && i < fd->collapse && !rect_count_seen)
1924 : : {
1925 : : /* When called with non-NULL zero_iter1_bb, we won't clear
1926 : : fd->loop.n2 in the if (zero_iter_bb == NULL) code below
1927 : : and if it is prior to storing fd->loop.n2 where
1928 : : rect_count_seen is set, it could be used uninitialized.
1929 : : As zero_iter1_bb in that case can be reached also if there
1930 : : are non-zero iterations, the clearing can't be emitted
1931 : : to the zero_iter1_bb, but needs to be done before the
1932 : : condition. */
1933 : 1499 : gassign *assign_stmt
1934 : 1499 : = gimple_build_assign (fd->loop.n2, build_zero_cst (type));
1935 : 1499 : gsi_insert_before (gsi, assign_stmt, GSI_SAME_STMT);
1936 : 1499 : init_n2 = false;
1937 : : }
1938 : 5777 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].n1));
1939 : 5777 : n1 = force_gimple_operand_gsi (gsi, n1, true, NULL_TREE,
1940 : : true, GSI_SAME_STMT);
1941 : 5777 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].n2));
1942 : 5777 : n2 = force_gimple_operand_gsi (gsi, n2, true, NULL_TREE,
1943 : : true, GSI_SAME_STMT);
1944 : 5777 : cond_stmt = expand_omp_build_cond (gsi, fd->loops[i].cond_code,
1945 : : n1, n2);
1946 : 5777 : e = split_block (entry_bb, cond_stmt);
1947 : 216 : basic_block &zero_iter_bb
1948 : 5777 : = i < fd->collapse ? zero_iter1_bb : zero_iter2_bb;
1949 : 216 : int &first_zero_iter
1950 : 5777 : = i < fd->collapse ? first_zero_iter1 : first_zero_iter2;
1951 : 5777 : if (zero_iter_bb == NULL)
1952 : : {
1953 : 626 : gassign *assign_stmt;
1954 : 626 : first_zero_iter = i;
1955 : 626 : zero_iter_bb = create_empty_bb (entry_bb);
1956 : 626 : add_bb_to_loop (zero_iter_bb, entry_bb->loop_father);
1957 : 626 : *gsi = gsi_after_labels (zero_iter_bb);
1958 : 626 : if (i < fd->collapse)
1959 : 508 : assign_stmt = gimple_build_assign (fd->loop.n2,
1960 : : build_zero_cst (type));
1961 : : else
1962 : : {
1963 : 118 : counts[i] = create_tmp_reg (type, ".count");
1964 : 118 : assign_stmt
1965 : 118 : = gimple_build_assign (counts[i], build_zero_cst (type));
1966 : : }
1967 : 626 : gsi_insert_before (gsi, assign_stmt, GSI_SAME_STMT);
1968 : 626 : set_immediate_dominator (CDI_DOMINATORS, zero_iter_bb,
1969 : : entry_bb);
1970 : : }
1971 : 5777 : ne = make_edge (entry_bb, zero_iter_bb, EDGE_FALSE_VALUE);
1972 : 5777 : ne->probability = profile_probability::very_unlikely ();
1973 : 5777 : e->flags = EDGE_TRUE_VALUE;
1974 : 5777 : e->probability = ne->probability.invert ();
1975 : 5777 : if (l2_dom_bb == NULL)
1976 : 2585 : l2_dom_bb = entry_bb;
1977 : 5777 : entry_bb = e->dest;
1978 : 5777 : *gsi = gsi_last_nondebug_bb (entry_bb);
1979 : : }
1980 : :
1981 : 21866 : if (POINTER_TYPE_P (itype))
1982 : 1661 : itype = signed_type_for (itype);
1983 : 21866 : t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
1984 : 26962 : ? -1 : 1));
1985 : 21866 : t = fold_build2 (PLUS_EXPR, itype,
1986 : : fold_convert (itype, fd->loops[i].step), t);
1987 : 21866 : t = fold_build2 (PLUS_EXPR, itype, t,
1988 : : fold_convert (itype, fd->loops[i].n2));
1989 : 21866 : t = fold_build2 (MINUS_EXPR, itype, t,
1990 : : fold_convert (itype, fd->loops[i].n1));
1991 : : /* ?? We could probably use CEIL_DIV_EXPR instead of
1992 : : TRUNC_DIV_EXPR and adjusting by hand. Unless we can't
1993 : : generate the same code in the end because generically we
1994 : : don't know that the values involved must be negative for
1995 : : GT?? */
1996 : 21866 : if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
1997 : 2240 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
1998 : : fold_build1 (NEGATE_EXPR, itype, t),
1999 : : fold_build1 (NEGATE_EXPR, itype,
2000 : : fold_convert (itype,
2001 : : fd->loops[i].step)));
2002 : : else
2003 : 19626 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
2004 : : fold_convert (itype, fd->loops[i].step));
2005 : 21866 : t = fold_convert (type, t);
2006 : 21866 : if (TREE_CODE (t) == INTEGER_CST)
2007 : 16019 : counts[i] = t;
2008 : : else
2009 : : {
2010 : 5847 : if (i < fd->collapse || i != first_zero_iter2)
2011 : 5729 : counts[i] = create_tmp_reg (type, ".count");
2012 : 5847 : expand_omp_build_assign (gsi, counts[i], t);
2013 : : }
2014 : 21866 : if (SSA_VAR_P (fd->loop.n2) && i < fd->collapse)
2015 : : {
2016 : 8609 : if (fd->non_rect && i >= fd->first_nonrect && i <= fd->last_nonrect)
2017 : 376 : continue;
2018 : 8233 : if (!rect_count_seen)
2019 : : {
2020 : 2989 : t = counts[i];
2021 : 2989 : rect_count_seen = true;
2022 : : }
2023 : : else
2024 : 5244 : t = fold_build2 (MULT_EXPR, type, fd->loop.n2, counts[i]);
2025 : 8233 : expand_omp_build_assign (gsi, fd->loop.n2, t);
2026 : : }
2027 : : }
2028 : 7726 : if (fd->non_rect && SSA_VAR_P (fd->loop.n2))
2029 : : {
2030 : 284 : gcc_assert (fd->last_nonrect != -1);
2031 : :
2032 : 284 : counts[fd->last_nonrect] = create_tmp_reg (type, ".count");
2033 : 284 : expand_omp_build_assign (gsi, counts[fd->last_nonrect],
2034 : : build_zero_cst (type));
2035 : 366 : for (i = fd->first_nonrect + 1; i < fd->last_nonrect; i++)
2036 : 113 : if (fd->loops[i].m1
2037 : 83 : || fd->loops[i].m2
2038 : 82 : || fd->loops[i].non_rect_referenced)
2039 : : break;
2040 : 284 : if (i == fd->last_nonrect
2041 : 253 : && fd->loops[i].outer == fd->last_nonrect - fd->first_nonrect
2042 : 253 : && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
2043 : 511 : && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[i].v)))
2044 : : {
2045 : 225 : int o = fd->first_nonrect;
2046 : 225 : tree itype = TREE_TYPE (fd->loops[o].v);
2047 : 225 : tree n1o = create_tmp_reg (itype, ".n1o");
2048 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[o].n1));
2049 : 225 : expand_omp_build_assign (gsi, n1o, t);
2050 : 225 : tree n2o = create_tmp_reg (itype, ".n2o");
2051 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[o].n2));
2052 : 225 : expand_omp_build_assign (gsi, n2o, t);
2053 : 225 : if (fd->loops[i].m1 && fd->loops[i].m2)
2054 : 43 : t = fold_build2 (MINUS_EXPR, itype, unshare_expr (fd->loops[i].m2),
2055 : : unshare_expr (fd->loops[i].m1));
2056 : 182 : else if (fd->loops[i].m1)
2057 : 162 : t = fold_build1 (NEGATE_EXPR, itype,
2058 : : unshare_expr (fd->loops[i].m1));
2059 : : else
2060 : 20 : t = unshare_expr (fd->loops[i].m2);
2061 : 225 : tree m2minusm1
2062 : 225 : = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
2063 : : true, GSI_SAME_STMT);
2064 : :
2065 : 225 : gimple_stmt_iterator gsi2 = *gsi;
2066 : 225 : gsi_prev (&gsi2);
2067 : 225 : e = split_block (entry_bb, gsi_stmt (gsi2));
2068 : 225 : e = split_block (e->dest, (gimple *) NULL);
2069 : 225 : basic_block bb1 = e->src;
2070 : 225 : entry_bb = e->dest;
2071 : 225 : *gsi = gsi_after_labels (entry_bb);
2072 : :
2073 : 225 : gsi2 = gsi_after_labels (bb1);
2074 : 225 : tree ostep = fold_convert (itype, fd->loops[o].step);
2075 : 225 : t = build_int_cst (itype, (fd->loops[o].cond_code
2076 : 241 : == LT_EXPR ? -1 : 1));
2077 : 225 : t = fold_build2 (PLUS_EXPR, itype, ostep, t);
2078 : 225 : t = fold_build2 (PLUS_EXPR, itype, t, n2o);
2079 : 225 : t = fold_build2 (MINUS_EXPR, itype, t, n1o);
2080 : 225 : if (TYPE_UNSIGNED (itype)
2081 : 225 : && fd->loops[o].cond_code == GT_EXPR)
2082 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2083 : : fold_build1 (NEGATE_EXPR, itype, t),
2084 : : fold_build1 (NEGATE_EXPR, itype, ostep));
2085 : : else
2086 : 225 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, ostep);
2087 : 225 : tree outer_niters
2088 : 225 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2089 : : true, GSI_SAME_STMT);
2090 : 225 : t = fold_build2 (MINUS_EXPR, itype, outer_niters,
2091 : : build_one_cst (itype));
2092 : 225 : t = fold_build2 (MULT_EXPR, itype, t, ostep);
2093 : 225 : t = fold_build2 (PLUS_EXPR, itype, n1o, t);
2094 : 225 : tree last = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2095 : : true, GSI_SAME_STMT);
2096 : 225 : tree n1, n2, n1e, n2e;
2097 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2098 : 225 : if (fd->loops[i].m1)
2099 : : {
2100 : 205 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2101 : 205 : n1 = fold_build2 (MULT_EXPR, itype, n1o, n1);
2102 : 205 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2103 : : }
2104 : : else
2105 : : n1 = t;
2106 : 225 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2107 : : true, GSI_SAME_STMT);
2108 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2109 : 225 : if (fd->loops[i].m2)
2110 : : {
2111 : 63 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2112 : 63 : n2 = fold_build2 (MULT_EXPR, itype, n1o, n2);
2113 : 63 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2114 : : }
2115 : : else
2116 : : n2 = t;
2117 : 225 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2118 : : true, GSI_SAME_STMT);
2119 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2120 : 225 : if (fd->loops[i].m1)
2121 : : {
2122 : 205 : n1e = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2123 : 205 : n1e = fold_build2 (MULT_EXPR, itype, last, n1e);
2124 : 205 : n1e = fold_build2 (PLUS_EXPR, itype, n1e, t);
2125 : : }
2126 : : else
2127 : : n1e = t;
2128 : 225 : n1e = force_gimple_operand_gsi (&gsi2, n1e, true, NULL_TREE,
2129 : : true, GSI_SAME_STMT);
2130 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2131 : 225 : if (fd->loops[i].m2)
2132 : : {
2133 : 63 : n2e = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2134 : 63 : n2e = fold_build2 (MULT_EXPR, itype, last, n2e);
2135 : 63 : n2e = fold_build2 (PLUS_EXPR, itype, n2e, t);
2136 : : }
2137 : : else
2138 : : n2e = t;
2139 : 225 : n2e = force_gimple_operand_gsi (&gsi2, n2e, true, NULL_TREE,
2140 : : true, GSI_SAME_STMT);
2141 : 225 : gcond *cond_stmt
2142 : 225 : = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2143 : : n1, n2);
2144 : 225 : e = split_block (bb1, cond_stmt);
2145 : 225 : e->flags = EDGE_TRUE_VALUE;
2146 : 225 : e->probability = profile_probability::likely ().guessed ();
2147 : 225 : basic_block bb2 = e->dest;
2148 : 225 : gsi2 = gsi_after_labels (bb2);
2149 : :
2150 : 225 : cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2151 : : n1e, n2e);
2152 : 225 : e = split_block (bb2, cond_stmt);
2153 : 225 : e->flags = EDGE_TRUE_VALUE;
2154 : 225 : e->probability = profile_probability::likely ().guessed ();
2155 : 225 : gsi2 = gsi_after_labels (e->dest);
2156 : :
2157 : 225 : tree step = fold_convert (itype, fd->loops[i].step);
2158 : 225 : t = build_int_cst (itype, (fd->loops[i].cond_code
2159 : 241 : == LT_EXPR ? -1 : 1));
2160 : 225 : t = fold_build2 (PLUS_EXPR, itype, step, t);
2161 : 225 : t = fold_build2 (PLUS_EXPR, itype, t, n2);
2162 : 225 : t = fold_build2 (MINUS_EXPR, itype, t, n1);
2163 : 225 : if (TYPE_UNSIGNED (itype)
2164 : 225 : && fd->loops[i].cond_code == GT_EXPR)
2165 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2166 : : fold_build1 (NEGATE_EXPR, itype, t),
2167 : : fold_build1 (NEGATE_EXPR, itype, step));
2168 : : else
2169 : 225 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2170 : 225 : tree first_inner_iterations
2171 : 225 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2172 : : true, GSI_SAME_STMT);
2173 : 225 : t = fold_build2 (MULT_EXPR, itype, m2minusm1, ostep);
2174 : 225 : if (TYPE_UNSIGNED (itype)
2175 : 225 : && fd->loops[i].cond_code == GT_EXPR)
2176 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2177 : : fold_build1 (NEGATE_EXPR, itype, t),
2178 : : fold_build1 (NEGATE_EXPR, itype, step));
2179 : : else
2180 : 225 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2181 : 225 : tree factor
2182 : 225 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2183 : : true, GSI_SAME_STMT);
2184 : 225 : t = fold_build2 (MINUS_EXPR, itype, outer_niters,
2185 : : build_one_cst (itype));
2186 : 225 : t = fold_build2 (MULT_EXPR, itype, t, outer_niters);
2187 : 225 : t = fold_build2 (RSHIFT_EXPR, itype, t, integer_one_node);
2188 : 225 : t = fold_build2 (MULT_EXPR, itype, factor, t);
2189 : 225 : t = fold_build2 (PLUS_EXPR, itype,
2190 : : fold_build2 (MULT_EXPR, itype, outer_niters,
2191 : : first_inner_iterations), t);
2192 : 225 : expand_omp_build_assign (&gsi2, counts[fd->last_nonrect],
2193 : : fold_convert (type, t));
2194 : :
2195 : 225 : basic_block bb3 = create_empty_bb (bb1);
2196 : 225 : add_bb_to_loop (bb3, bb1->loop_father);
2197 : :
2198 : 225 : e = make_edge (bb1, bb3, EDGE_FALSE_VALUE);
2199 : 225 : e->probability = profile_probability::unlikely ().guessed ();
2200 : :
2201 : 225 : gsi2 = gsi_after_labels (bb3);
2202 : 225 : cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2203 : : n1e, n2e);
2204 : 225 : e = split_block (bb3, cond_stmt);
2205 : 225 : e->flags = EDGE_TRUE_VALUE;
2206 : 225 : e->probability = profile_probability::likely ().guessed ();
2207 : 225 : basic_block bb4 = e->dest;
2208 : :
2209 : 225 : ne = make_edge (bb3, entry_bb, EDGE_FALSE_VALUE);
2210 : 225 : ne->probability = e->probability.invert ();
2211 : :
2212 : 225 : basic_block bb5 = create_empty_bb (bb2);
2213 : 225 : add_bb_to_loop (bb5, bb2->loop_father);
2214 : :
2215 : 225 : ne = make_edge (bb2, bb5, EDGE_FALSE_VALUE);
2216 : 225 : ne->probability = profile_probability::unlikely ().guessed ();
2217 : :
2218 : 675 : for (int j = 0; j < 2; j++)
2219 : : {
2220 : 450 : gsi2 = gsi_after_labels (j ? bb5 : bb4);
2221 : 450 : t = fold_build2 (MINUS_EXPR, itype,
2222 : : unshare_expr (fd->loops[i].n1),
2223 : : unshare_expr (fd->loops[i].n2));
2224 : 450 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, m2minusm1);
2225 : 450 : tree tem
2226 : 450 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2227 : : true, GSI_SAME_STMT);
2228 : 450 : t = fold_build2 (MINUS_EXPR, itype, tem, n1o);
2229 : 450 : t = fold_build2 (TRUNC_MOD_EXPR, itype, t, ostep);
2230 : 450 : t = fold_build2 (MINUS_EXPR, itype, tem, t);
2231 : 450 : tem = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2232 : : true, GSI_SAME_STMT);
2233 : 450 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2234 : 450 : if (fd->loops[i].m1)
2235 : : {
2236 : 410 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2237 : 410 : n1 = fold_build2 (MULT_EXPR, itype, tem, n1);
2238 : 410 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2239 : : }
2240 : : else
2241 : : n1 = t;
2242 : 450 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2243 : : true, GSI_SAME_STMT);
2244 : 450 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2245 : 450 : if (fd->loops[i].m2)
2246 : : {
2247 : 126 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2248 : 126 : n2 = fold_build2 (MULT_EXPR, itype, tem, n2);
2249 : 126 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2250 : : }
2251 : : else
2252 : : n2 = t;
2253 : 450 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2254 : : true, GSI_SAME_STMT);
2255 : 675 : expand_omp_build_assign (&gsi2, j ? n2o : n1o, tem);
2256 : :
2257 : 450 : cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2258 : : n1, n2);
2259 : 450 : e = split_block (gsi_bb (gsi2), cond_stmt);
2260 : 450 : e->flags = j ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE;
2261 : 450 : e->probability = profile_probability::unlikely ().guessed ();
2262 : 675 : ne = make_edge (e->src, bb1,
2263 : : j ? EDGE_FALSE_VALUE : EDGE_TRUE_VALUE);
2264 : 450 : ne->probability = e->probability.invert ();
2265 : 450 : gsi2 = gsi_after_labels (e->dest);
2266 : :
2267 : 450 : t = fold_build2 (PLUS_EXPR, itype, tem, ostep);
2268 : 450 : expand_omp_build_assign (&gsi2, j ? n2o : n1o, t);
2269 : :
2270 : 450 : make_edge (e->dest, bb1, EDGE_FALLTHRU);
2271 : : }
2272 : :
2273 : 225 : set_immediate_dominator (CDI_DOMINATORS, bb3, bb1);
2274 : 225 : set_immediate_dominator (CDI_DOMINATORS, bb5, bb2);
2275 : 225 : set_immediate_dominator (CDI_DOMINATORS, entry_bb, bb1);
2276 : :
2277 : 225 : if (fd->first_nonrect + 1 == fd->last_nonrect)
2278 : : {
2279 : 153 : fd->first_inner_iterations = first_inner_iterations;
2280 : 153 : fd->factor = factor;
2281 : 153 : fd->adjn1 = n1o;
2282 : : }
2283 : : }
2284 : : else
2285 : : {
2286 : : /* Fallback implementation. Evaluate the loops with m1/m2
2287 : : non-NULL as well as their outer loops at runtime using temporaries
2288 : : instead of the original iteration variables, and in the
2289 : : body just bump the counter. */
2290 : 59 : gimple_stmt_iterator gsi2 = *gsi;
2291 : 59 : gsi_prev (&gsi2);
2292 : 59 : e = split_block (entry_bb, gsi_stmt (gsi2));
2293 : 59 : e = split_block (e->dest, (gimple *) NULL);
2294 : 59 : basic_block cur_bb = e->src;
2295 : 59 : basic_block next_bb = e->dest;
2296 : 59 : entry_bb = e->dest;
2297 : 59 : *gsi = gsi_after_labels (entry_bb);
2298 : :
2299 : 59 : tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
2300 : 59 : memset (vs, 0, fd->last_nonrect * sizeof (tree));
2301 : :
2302 : 189 : for (i = 0; i <= fd->last_nonrect; i++)
2303 : : {
2304 : 189 : if (fd->loops[i].m1 == NULL_TREE
2305 : 108 : && fd->loops[i].m2 == NULL_TREE
2306 : 99 : && !fd->loops[i].non_rect_referenced)
2307 : 40 : continue;
2308 : :
2309 : 149 : tree itype = TREE_TYPE (fd->loops[i].v);
2310 : :
2311 : 149 : gsi2 = gsi_after_labels (cur_bb);
2312 : 149 : tree n1, n2;
2313 : 149 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2314 : 149 : if (fd->loops[i].m1 == NULL_TREE)
2315 : : n1 = t;
2316 : 81 : else if (POINTER_TYPE_P (itype))
2317 : : {
2318 : 30 : gcc_assert (integer_onep (fd->loops[i].m1));
2319 : 30 : t = unshare_expr (fd->loops[i].n1);
2320 : 30 : n1 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
2321 : : }
2322 : : else
2323 : : {
2324 : 51 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2325 : 51 : n1 = fold_build2 (MULT_EXPR, itype,
2326 : : vs[i - fd->loops[i].outer], n1);
2327 : 51 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2328 : : }
2329 : 149 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2330 : : true, GSI_SAME_STMT);
2331 : 149 : if (i < fd->last_nonrect)
2332 : : {
2333 : 90 : vs[i] = create_tmp_reg (itype, ".it");
2334 : 90 : expand_omp_build_assign (&gsi2, vs[i], n1);
2335 : : }
2336 : 149 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2337 : 149 : if (fd->loops[i].m2 == NULL_TREE)
2338 : : n2 = t;
2339 : 85 : else if (POINTER_TYPE_P (itype))
2340 : : {
2341 : 34 : gcc_assert (integer_onep (fd->loops[i].m2));
2342 : 34 : t = unshare_expr (fd->loops[i].n2);
2343 : 34 : n2 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
2344 : : }
2345 : : else
2346 : : {
2347 : 51 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2348 : 51 : n2 = fold_build2 (MULT_EXPR, itype,
2349 : : vs[i - fd->loops[i].outer], n2);
2350 : 51 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2351 : : }
2352 : 149 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2353 : : true, GSI_SAME_STMT);
2354 : 149 : if (POINTER_TYPE_P (itype))
2355 : 70 : itype = signed_type_for (itype);
2356 : 149 : if (i == fd->last_nonrect)
2357 : : {
2358 : 59 : gcond *cond_stmt
2359 : 59 : = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2360 : : n1, n2);
2361 : 59 : e = split_block (cur_bb, cond_stmt);
2362 : 59 : e->flags = EDGE_TRUE_VALUE;
2363 : 59 : ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
2364 : 59 : e->probability = profile_probability::likely ().guessed ();
2365 : 59 : ne->probability = e->probability.invert ();
2366 : 59 : gsi2 = gsi_after_labels (e->dest);
2367 : :
2368 : 59 : t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
2369 : 79 : ? -1 : 1));
2370 : 59 : t = fold_build2 (PLUS_EXPR, itype,
2371 : : fold_convert (itype, fd->loops[i].step), t);
2372 : 59 : t = fold_build2 (PLUS_EXPR, itype, t,
2373 : : fold_convert (itype, n2));
2374 : 59 : t = fold_build2 (MINUS_EXPR, itype, t,
2375 : : fold_convert (itype, n1));
2376 : 59 : tree step = fold_convert (itype, fd->loops[i].step);
2377 : 59 : if (TYPE_UNSIGNED (itype)
2378 : 59 : && fd->loops[i].cond_code == GT_EXPR)
2379 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2380 : : fold_build1 (NEGATE_EXPR, itype, t),
2381 : : fold_build1 (NEGATE_EXPR, itype, step));
2382 : : else
2383 : 59 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2384 : 59 : t = fold_convert (type, t);
2385 : 59 : t = fold_build2 (PLUS_EXPR, type,
2386 : : counts[fd->last_nonrect], t);
2387 : 59 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2388 : : true, GSI_SAME_STMT);
2389 : 59 : expand_omp_build_assign (&gsi2, counts[fd->last_nonrect], t);
2390 : 59 : e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
2391 : 59 : set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
2392 : 59 : break;
2393 : : }
2394 : 90 : e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
2395 : :
2396 : 90 : basic_block new_cur_bb = create_empty_bb (cur_bb);
2397 : 90 : add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
2398 : :
2399 : 90 : gsi2 = gsi_after_labels (e->dest);
2400 : 90 : tree step = fold_convert (itype,
2401 : : unshare_expr (fd->loops[i].step));
2402 : 90 : if (POINTER_TYPE_P (TREE_TYPE (vs[i])))
2403 : 38 : t = fold_build_pointer_plus (vs[i], step);
2404 : : else
2405 : 52 : t = fold_build2 (PLUS_EXPR, itype, vs[i], step);
2406 : 90 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2407 : : true, GSI_SAME_STMT);
2408 : 90 : expand_omp_build_assign (&gsi2, vs[i], t);
2409 : :
2410 : 90 : ne = split_block (e->dest, last_nondebug_stmt (e->dest));
2411 : 90 : gsi2 = gsi_after_labels (ne->dest);
2412 : :
2413 : 90 : expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, vs[i], n2);
2414 : 90 : edge e3, e4;
2415 : 90 : if (next_bb == entry_bb)
2416 : : {
2417 : 59 : e3 = find_edge (ne->dest, next_bb);
2418 : 59 : e3->flags = EDGE_FALSE_VALUE;
2419 : : }
2420 : : else
2421 : 31 : e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
2422 : 90 : e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
2423 : 90 : e4->probability = profile_probability::likely ().guessed ();
2424 : 90 : e3->probability = e4->probability.invert ();
2425 : 90 : basic_block esrc = e->src;
2426 : 90 : make_edge (e->src, ne->dest, EDGE_FALLTHRU);
2427 : 90 : cur_bb = new_cur_bb;
2428 : 90 : basic_block latch_bb = next_bb;
2429 : 90 : next_bb = e->dest;
2430 : 90 : remove_edge (e);
2431 : 90 : set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
2432 : 90 : set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
2433 : 90 : set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
2434 : : }
2435 : : }
2436 : 284 : t = NULL_TREE;
2437 : 691 : for (i = fd->first_nonrect; i < fd->last_nonrect; i++)
2438 : 407 : if (!fd->loops[i].non_rect_referenced
2439 : 92 : && fd->loops[i].m1 == NULL_TREE
2440 : 92 : && fd->loops[i].m2 == NULL_TREE)
2441 : : {
2442 : 92 : if (t == NULL_TREE)
2443 : 82 : t = counts[i];
2444 : : else
2445 : 10 : t = fold_build2 (MULT_EXPR, type, t, counts[i]);
2446 : : }
2447 : 284 : if (t)
2448 : : {
2449 : 82 : t = fold_build2 (MULT_EXPR, type, counts[fd->last_nonrect], t);
2450 : 82 : expand_omp_build_assign (gsi, counts[fd->last_nonrect], t);
2451 : : }
2452 : 284 : if (!rect_count_seen)
2453 : 186 : t = counts[fd->last_nonrect];
2454 : : else
2455 : 98 : t = fold_build2 (MULT_EXPR, type, fd->loop.n2,
2456 : : counts[fd->last_nonrect]);
2457 : 284 : expand_omp_build_assign (gsi, fd->loop.n2, t);
2458 : 284 : }
2459 : 7442 : else if (fd->non_rect)
2460 : : {
2461 : 117 : tree t = fd->loop.n2;
2462 : 117 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
2463 : : int non_rect_referenced = 0, non_rect = 0;
2464 : 358 : for (i = 0; i < fd->collapse; i++)
2465 : : {
2466 : 240 : if ((i < fd->first_nonrect || i > fd->last_nonrect)
2467 : 246 : && !integer_zerop (counts[i]))
2468 : 6 : t = fold_build2 (TRUNC_DIV_EXPR, type, t, counts[i]);
2469 : 241 : if (fd->loops[i].non_rect_referenced)
2470 : 117 : non_rect_referenced++;
2471 : 241 : if (fd->loops[i].m1 || fd->loops[i].m2)
2472 : 117 : non_rect++;
2473 : : }
2474 : 117 : gcc_assert (non_rect == 1 && non_rect_referenced == 1);
2475 : 117 : counts[fd->last_nonrect] = t;
2476 : : }
2477 : : }
2478 : :
2479 : : /* Helper function for expand_omp_{for_*,simd}. Generate code like:
2480 : : T = V;
2481 : : V3 = N31 + (T % count3) * STEP3;
2482 : : T = T / count3;
2483 : : V2 = N21 + (T % count2) * STEP2;
2484 : : T = T / count2;
2485 : : V1 = N11 + T * STEP1;
2486 : : if this loop doesn't have an inner loop construct combined with it.
2487 : : If it does have an inner loop construct combined with it and the
2488 : : iteration count isn't known constant, store values from counts array
2489 : : into its _looptemp_ temporaries instead.
2490 : : For non-rectangular loops (between fd->first_nonrect and fd->last_nonrect
2491 : : inclusive), use the count of all those loops together, and either
2492 : : find quadratic etc. equation roots, or as a fallback, do:
2493 : : COUNT = 0;
2494 : : for (tmpi = N11; tmpi COND1 N12; tmpi += STEP1)
2495 : : for (tmpj = M21 * tmpi + N21;
2496 : : tmpj COND2 M22 * tmpi + N22; tmpj += STEP2)
2497 : : {
2498 : : int tmpk1 = M31 * tmpj + N31;
2499 : : int tmpk2 = M32 * tmpj + N32;
2500 : : if (tmpk1 COND3 tmpk2)
2501 : : {
2502 : : if (COND3 is <)
2503 : : adj = STEP3 - 1;
2504 : : else
2505 : : adj = STEP3 + 1;
2506 : : int temp = (adj + tmpk2 - tmpk1) / STEP3;
2507 : : if (COUNT + temp > T)
2508 : : {
2509 : : V1 = tmpi;
2510 : : V2 = tmpj;
2511 : : V3 = tmpk1 + (T - COUNT) * STEP3;
2512 : : goto done;
2513 : : }
2514 : : else
2515 : : COUNT += temp;
2516 : : }
2517 : : }
2518 : : done:;
2519 : : but for optional innermost or outermost rectangular loops that aren't
2520 : : referenced by other loop expressions keep doing the division/modulo. */
2521 : :
2522 : : static void
2523 : 10223 : expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
2524 : : tree *counts, tree *nonrect_bounds,
2525 : : gimple *inner_stmt, tree startvar)
2526 : : {
2527 : 10223 : int i;
2528 : 10223 : if (gimple_omp_for_combined_p (fd->for_stmt))
2529 : : {
2530 : : /* If fd->loop.n2 is constant, then no propagation of the counts
2531 : : is needed, they are constant. */
2532 : 4841 : if (TREE_CODE (fd->loop.n2) == INTEGER_CST)
2533 : : return;
2534 : :
2535 : 2681 : tree clauses = gimple_code (inner_stmt) != GIMPLE_OMP_FOR
2536 : 4141 : ? gimple_omp_taskreg_clauses (inner_stmt)
2537 : 1221 : : gimple_omp_for_clauses (inner_stmt);
2538 : : /* First two _looptemp_ clauses are for istart/iend, counts[0]
2539 : : isn't supposed to be handled, as the inner loop doesn't
2540 : : use it. */
2541 : 2681 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
2542 : 2681 : gcc_assert (innerc);
2543 : 2681 : int count = 0;
2544 : 2681 : if (fd->non_rect
2545 : 112 : && fd->last_nonrect == fd->first_nonrect + 1
2546 : 2745 : && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
2547 : : count = 4;
2548 : 10760 : for (i = 0; i < fd->collapse + count; i++)
2549 : : {
2550 : 8079 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
2551 : : OMP_CLAUSE__LOOPTEMP_);
2552 : 8079 : gcc_assert (innerc);
2553 : 8079 : if (i)
2554 : : {
2555 : 5398 : tree tem = OMP_CLAUSE_DECL (innerc);
2556 : 5398 : tree t;
2557 : 5398 : if (i < fd->collapse)
2558 : 5158 : t = counts[i];
2559 : : else
2560 : 240 : switch (i - fd->collapse)
2561 : : {
2562 : 60 : case 0: t = counts[0]; break;
2563 : 60 : case 1: t = fd->first_inner_iterations; break;
2564 : 60 : case 2: t = fd->factor; break;
2565 : 60 : case 3: t = fd->adjn1; break;
2566 : 0 : default: gcc_unreachable ();
2567 : : }
2568 : 5398 : t = fold_convert (TREE_TYPE (tem), t);
2569 : 5398 : t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
2570 : : false, GSI_CONTINUE_LINKING);
2571 : 5398 : gassign *stmt = gimple_build_assign (tem, t);
2572 : 5398 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
2573 : : }
2574 : : }
2575 : : return;
2576 : : }
2577 : :
2578 : 5382 : tree type = TREE_TYPE (fd->loop.v);
2579 : 5382 : tree tem = create_tmp_reg (type, ".tem");
2580 : 5382 : gassign *stmt = gimple_build_assign (tem, startvar);
2581 : 5382 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
2582 : :
2583 : 20030 : for (i = fd->collapse - 1; i >= 0; i--)
2584 : : {
2585 : 14648 : tree vtype = TREE_TYPE (fd->loops[i].v), itype, t;
2586 : 14648 : itype = vtype;
2587 : 14648 : if (POINTER_TYPE_P (vtype))
2588 : 1649 : itype = signed_type_for (vtype);
2589 : 14648 : if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
2590 : 9266 : t = fold_build2 (TRUNC_MOD_EXPR, type, tem, counts[i]);
2591 : : else
2592 : : t = tem;
2593 : 14648 : if (i == fd->last_nonrect)
2594 : : {
2595 : 376 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
2596 : : false, GSI_CONTINUE_LINKING);
2597 : 376 : tree stopval = t;
2598 : 376 : tree idx = create_tmp_reg (type, ".count");
2599 : 376 : expand_omp_build_assign (gsi, idx,
2600 : : build_zero_cst (type), true);
2601 : 376 : basic_block bb_triang = NULL, bb_triang_dom = NULL;
2602 : 376 : if (fd->first_nonrect + 1 == fd->last_nonrect
2603 : 272 : && (TREE_CODE (fd->loop.n2) == INTEGER_CST
2604 : 181 : || fd->first_inner_iterations)
2605 : 488 : && (optab_handler (sqrt_optab, TYPE_MODE (double_type_node))
2606 : : != CODE_FOR_nothing)
2607 : 620 : && !integer_zerop (fd->loop.n2))
2608 : : {
2609 : 236 : tree outer_n1 = fd->adjn1 ? fd->adjn1 : fd->loops[i - 1].n1;
2610 : 236 : tree itype = TREE_TYPE (fd->loops[i].v);
2611 : 236 : tree first_inner_iterations = fd->first_inner_iterations;
2612 : 236 : tree factor = fd->factor;
2613 : 236 : gcond *cond_stmt
2614 : 236 : = expand_omp_build_cond (gsi, NE_EXPR, factor,
2615 : 236 : build_zero_cst (TREE_TYPE (factor)),
2616 : : true);
2617 : 236 : edge e = split_block (gsi_bb (*gsi), cond_stmt);
2618 : 236 : basic_block bb0 = e->src;
2619 : 236 : e->flags = EDGE_TRUE_VALUE;
2620 : 236 : e->probability = profile_probability::likely ();
2621 : 236 : bb_triang_dom = bb0;
2622 : 236 : *gsi = gsi_after_labels (e->dest);
2623 : 236 : tree slltype = long_long_integer_type_node;
2624 : 236 : tree ulltype = long_long_unsigned_type_node;
2625 : 236 : tree stopvalull = fold_convert (ulltype, stopval);
2626 : 236 : stopvalull
2627 : 236 : = force_gimple_operand_gsi (gsi, stopvalull, true, NULL_TREE,
2628 : : false, GSI_CONTINUE_LINKING);
2629 : 236 : first_inner_iterations
2630 : 236 : = fold_convert (slltype, first_inner_iterations);
2631 : 236 : first_inner_iterations
2632 : 236 : = force_gimple_operand_gsi (gsi, first_inner_iterations, true,
2633 : : NULL_TREE, false,
2634 : : GSI_CONTINUE_LINKING);
2635 : 236 : factor = fold_convert (slltype, factor);
2636 : 236 : factor
2637 : 236 : = force_gimple_operand_gsi (gsi, factor, true, NULL_TREE,
2638 : : false, GSI_CONTINUE_LINKING);
2639 : 236 : tree first_inner_iterationsd
2640 : 236 : = fold_build1 (FLOAT_EXPR, double_type_node,
2641 : : first_inner_iterations);
2642 : 236 : first_inner_iterationsd
2643 : 236 : = force_gimple_operand_gsi (gsi, first_inner_iterationsd, true,
2644 : : NULL_TREE, false,
2645 : : GSI_CONTINUE_LINKING);
2646 : 236 : tree factord = fold_build1 (FLOAT_EXPR, double_type_node,
2647 : : factor);
2648 : 236 : factord = force_gimple_operand_gsi (gsi, factord, true,
2649 : : NULL_TREE, false,
2650 : : GSI_CONTINUE_LINKING);
2651 : 236 : tree stopvald = fold_build1 (FLOAT_EXPR, double_type_node,
2652 : : stopvalull);
2653 : 236 : stopvald = force_gimple_operand_gsi (gsi, stopvald, true,
2654 : : NULL_TREE, false,
2655 : : GSI_CONTINUE_LINKING);
2656 : : /* Temporarily disable flag_rounding_math, values will be
2657 : : decimal numbers divided by 2 and worst case imprecisions
2658 : : due to too large values ought to be caught later by the
2659 : : checks for fallback. */
2660 : 236 : int save_flag_rounding_math = flag_rounding_math;
2661 : 236 : flag_rounding_math = 0;
2662 : 236 : t = fold_build2 (RDIV_EXPR, double_type_node, factord,
2663 : : build_real (double_type_node, dconst2));
2664 : 236 : tree t3 = fold_build2 (MINUS_EXPR, double_type_node,
2665 : : first_inner_iterationsd, t);
2666 : 236 : t3 = force_gimple_operand_gsi (gsi, t3, true, NULL_TREE, false,
2667 : : GSI_CONTINUE_LINKING);
2668 : 236 : t = fold_build2 (MULT_EXPR, double_type_node, factord,
2669 : : build_real (double_type_node, dconst2));
2670 : 236 : t = fold_build2 (MULT_EXPR, double_type_node, t, stopvald);
2671 : 236 : t = fold_build2 (PLUS_EXPR, double_type_node, t,
2672 : : fold_build2 (MULT_EXPR, double_type_node,
2673 : : t3, t3));
2674 : 236 : flag_rounding_math = save_flag_rounding_math;
2675 : 236 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2676 : : GSI_CONTINUE_LINKING);
2677 : 236 : if (flag_exceptions
2678 : 34 : && cfun->can_throw_non_call_exceptions
2679 : 244 : && operation_could_trap_p (LT_EXPR, true, false, NULL_TREE))
2680 : : {
2681 : 8 : tree tem = fold_build2 (LT_EXPR, boolean_type_node, t,
2682 : : build_zero_cst (double_type_node));
2683 : 8 : tem = force_gimple_operand_gsi (gsi, tem, true, NULL_TREE,
2684 : : false, GSI_CONTINUE_LINKING);
2685 : 8 : cond_stmt = gimple_build_cond (NE_EXPR, tem,
2686 : : boolean_false_node,
2687 : : NULL_TREE, NULL_TREE);
2688 : : }
2689 : : else
2690 : 228 : cond_stmt
2691 : 228 : = gimple_build_cond (LT_EXPR, t,
2692 : : build_zero_cst (double_type_node),
2693 : : NULL_TREE, NULL_TREE);
2694 : 236 : gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2695 : 236 : e = split_block (gsi_bb (*gsi), cond_stmt);
2696 : 236 : basic_block bb1 = e->src;
2697 : 236 : e->flags = EDGE_FALSE_VALUE;
2698 : 236 : e->probability = profile_probability::very_likely ();
2699 : 236 : *gsi = gsi_after_labels (e->dest);
2700 : 236 : gcall *call = gimple_build_call_internal (IFN_SQRT, 1, t);
2701 : 236 : tree sqrtr = create_tmp_var (double_type_node);
2702 : 236 : gimple_call_set_lhs (call, sqrtr);
2703 : 236 : gsi_insert_after (gsi, call, GSI_CONTINUE_LINKING);
2704 : 236 : t = fold_build2 (MINUS_EXPR, double_type_node, sqrtr, t3);
2705 : 236 : t = fold_build2 (RDIV_EXPR, double_type_node, t, factord);
2706 : 236 : t = fold_build1 (FIX_TRUNC_EXPR, ulltype, t);
2707 : 236 : tree c = create_tmp_var (ulltype);
2708 : 236 : tree d = create_tmp_var (ulltype);
2709 : 236 : expand_omp_build_assign (gsi, c, t, true);
2710 : 236 : t = fold_build2 (MINUS_EXPR, ulltype, c,
2711 : : build_one_cst (ulltype));
2712 : 236 : t = fold_build2 (MULT_EXPR, ulltype, c, t);
2713 : 236 : t = fold_build2 (RSHIFT_EXPR, ulltype, t, integer_one_node);
2714 : 236 : t = fold_build2 (MULT_EXPR, ulltype,
2715 : : fold_convert (ulltype, fd->factor), t);
2716 : 236 : tree t2
2717 : 236 : = fold_build2 (MULT_EXPR, ulltype, c,
2718 : : fold_convert (ulltype,
2719 : : fd->first_inner_iterations));
2720 : 236 : t = fold_build2 (PLUS_EXPR, ulltype, t, t2);
2721 : 236 : expand_omp_build_assign (gsi, d, t, true);
2722 : 236 : t = fold_build2 (MULT_EXPR, ulltype,
2723 : : fold_convert (ulltype, fd->factor), c);
2724 : 236 : t = fold_build2 (PLUS_EXPR, ulltype,
2725 : : t, fold_convert (ulltype,
2726 : : fd->first_inner_iterations));
2727 : 236 : t2 = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2728 : : GSI_CONTINUE_LINKING);
2729 : 236 : cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, d,
2730 : : NULL_TREE, NULL_TREE);
2731 : 236 : gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2732 : 236 : e = split_block (gsi_bb (*gsi), cond_stmt);
2733 : 236 : basic_block bb2 = e->src;
2734 : 236 : e->flags = EDGE_TRUE_VALUE;
2735 : 236 : e->probability = profile_probability::very_likely ();
2736 : 236 : *gsi = gsi_after_labels (e->dest);
2737 : 236 : t = fold_build2 (PLUS_EXPR, ulltype, d, t2);
2738 : 236 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2739 : : GSI_CONTINUE_LINKING);
2740 : 236 : cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, t,
2741 : : NULL_TREE, NULL_TREE);
2742 : 236 : gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2743 : 236 : e = split_block (gsi_bb (*gsi), cond_stmt);
2744 : 236 : basic_block bb3 = e->src;
2745 : 236 : e->flags = EDGE_FALSE_VALUE;
2746 : 236 : e->probability = profile_probability::very_likely ();
2747 : 236 : *gsi = gsi_after_labels (e->dest);
2748 : 236 : t = fold_convert (itype, c);
2749 : 236 : t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i - 1].step);
2750 : 236 : t = fold_build2 (PLUS_EXPR, itype, outer_n1, t);
2751 : 236 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2752 : : GSI_CONTINUE_LINKING);
2753 : 236 : expand_omp_build_assign (gsi, fd->loops[i - 1].v, t, true);
2754 : 236 : t2 = fold_build2 (MINUS_EXPR, ulltype, stopvalull, d);
2755 : 236 : t2 = fold_convert (itype, t2);
2756 : 236 : t2 = fold_build2 (MULT_EXPR, itype, t2, fd->loops[i].step);
2757 : 236 : t2 = fold_build2 (PLUS_EXPR, itype, t2, fd->loops[i].n1);
2758 : 236 : if (fd->loops[i].m1)
2759 : : {
2760 : 195 : t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i].m1);
2761 : 195 : t2 = fold_build2 (PLUS_EXPR, itype, t2, t);
2762 : : }
2763 : 236 : expand_omp_build_assign (gsi, fd->loops[i].v, t2, true);
2764 : 236 : e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
2765 : 236 : bb_triang = e->src;
2766 : 236 : *gsi = gsi_after_labels (e->dest);
2767 : 236 : remove_edge (e);
2768 : 236 : e = make_edge (bb1, gsi_bb (*gsi), EDGE_TRUE_VALUE);
2769 : 236 : e->probability = profile_probability::very_unlikely ();
2770 : 236 : e = make_edge (bb2, gsi_bb (*gsi), EDGE_FALSE_VALUE);
2771 : 236 : e->probability = profile_probability::very_unlikely ();
2772 : 236 : e = make_edge (bb3, gsi_bb (*gsi), EDGE_TRUE_VALUE);
2773 : 236 : e->probability = profile_probability::very_unlikely ();
2774 : :
2775 : 236 : basic_block bb4 = create_empty_bb (bb0);
2776 : 236 : add_bb_to_loop (bb4, bb0->loop_father);
2777 : 236 : e = make_edge (bb0, bb4, EDGE_FALSE_VALUE);
2778 : 236 : e->probability = profile_probability::unlikely ();
2779 : 236 : make_edge (bb4, gsi_bb (*gsi), EDGE_FALLTHRU);
2780 : 236 : set_immediate_dominator (CDI_DOMINATORS, bb4, bb0);
2781 : 236 : set_immediate_dominator (CDI_DOMINATORS, gsi_bb (*gsi), bb0);
2782 : 236 : gimple_stmt_iterator gsi2 = gsi_after_labels (bb4);
2783 : 236 : t2 = fold_build2 (TRUNC_DIV_EXPR, type,
2784 : : counts[i], counts[i - 1]);
2785 : 236 : t2 = force_gimple_operand_gsi (&gsi2, t2, true, NULL_TREE, false,
2786 : : GSI_CONTINUE_LINKING);
2787 : 236 : t = fold_build2 (TRUNC_MOD_EXPR, type, stopval, t2);
2788 : 236 : t2 = fold_build2 (TRUNC_DIV_EXPR, type, stopval, t2);
2789 : 236 : t = fold_convert (itype, t);
2790 : 236 : t2 = fold_convert (itype, t2);
2791 : 236 : t = fold_build2 (MULT_EXPR, itype, t,
2792 : : fold_convert (itype, fd->loops[i].step));
2793 : 236 : t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
2794 : 236 : t2 = fold_build2 (MULT_EXPR, itype, t2,
2795 : : fold_convert (itype, fd->loops[i - 1].step));
2796 : 236 : t2 = fold_build2 (PLUS_EXPR, itype, fd->loops[i - 1].n1, t2);
2797 : 236 : t2 = force_gimple_operand_gsi (&gsi2, t2, false, NULL_TREE,
2798 : : false, GSI_CONTINUE_LINKING);
2799 : 236 : stmt = gimple_build_assign (fd->loops[i - 1].v, t2);
2800 : 236 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
2801 : 236 : if (fd->loops[i].m1)
2802 : : {
2803 : 195 : t2 = fold_build2 (MULT_EXPR, itype, fd->loops[i].m1,
2804 : : fd->loops[i - 1].v);
2805 : 195 : t = fold_build2 (PLUS_EXPR, itype, t, t2);
2806 : : }
2807 : 236 : t = force_gimple_operand_gsi (&gsi2, t, false, NULL_TREE,
2808 : : false, GSI_CONTINUE_LINKING);
2809 : 236 : stmt = gimple_build_assign (fd->loops[i].v, t);
2810 : 236 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
2811 : : }
2812 : : /* Fallback implementation. Evaluate the loops in between
2813 : : (inclusive) fd->first_nonrect and fd->last_nonrect at
2814 : : runtime unsing temporaries instead of the original iteration
2815 : : variables, in the body just bump the counter and compare
2816 : : with the desired value. */
2817 : 376 : gimple_stmt_iterator gsi2 = *gsi;
2818 : 376 : basic_block entry_bb = gsi_bb (gsi2);
2819 : 376 : edge e = split_block (entry_bb, gsi_stmt (gsi2));
2820 : 376 : e = split_block (e->dest, (gimple *) NULL);
2821 : 376 : basic_block dom_bb = NULL;
2822 : 376 : basic_block cur_bb = e->src;
2823 : 376 : basic_block next_bb = e->dest;
2824 : 376 : entry_bb = e->dest;
2825 : 376 : *gsi = gsi_after_labels (entry_bb);
2826 : :
2827 : 376 : tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
2828 : 376 : tree n1 = NULL_TREE, n2 = NULL_TREE;
2829 : 376 : memset (vs, 0, fd->last_nonrect * sizeof (tree));
2830 : :
2831 : 876 : for (int j = fd->first_nonrect; j <= fd->last_nonrect; j++)
2832 : : {
2833 : 876 : tree itype = TREE_TYPE (fd->loops[j].v);
2834 : 1752 : bool rect_p = (fd->loops[j].m1 == NULL_TREE
2835 : 527 : && fd->loops[j].m2 == NULL_TREE
2836 : 1345 : && !fd->loops[j].non_rect_referenced);
2837 : 876 : gsi2 = gsi_after_labels (cur_bb);
2838 : 876 : t = fold_convert (itype, unshare_expr (fd->loops[j].n1));
2839 : 876 : if (fd->loops[j].m1 == NULL_TREE)
2840 : 527 : n1 = rect_p ? build_zero_cst (type) : t;
2841 : 349 : else if (POINTER_TYPE_P (itype))
2842 : : {
2843 : 30 : gcc_assert (integer_onep (fd->loops[j].m1));
2844 : 30 : t = unshare_expr (fd->loops[j].n1);
2845 : 30 : n1 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
2846 : : }
2847 : : else
2848 : : {
2849 : 319 : n1 = fold_convert (itype, unshare_expr (fd->loops[j].m1));
2850 : 319 : n1 = fold_build2 (MULT_EXPR, itype,
2851 : : vs[j - fd->loops[j].outer], n1);
2852 : 319 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2853 : : }
2854 : 876 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2855 : : true, GSI_SAME_STMT);
2856 : 876 : if (j < fd->last_nonrect)
2857 : : {
2858 : 907 : vs[j] = create_tmp_reg (rect_p ? type : itype, ".it");
2859 : 500 : expand_omp_build_assign (&gsi2, vs[j], n1);
2860 : : }
2861 : 876 : t = fold_convert (itype, unshare_expr (fd->loops[j].n2));
2862 : 876 : if (fd->loops[j].m2 == NULL_TREE)
2863 : 646 : n2 = rect_p ? counts[j] : t;
2864 : 230 : else if (POINTER_TYPE_P (itype))
2865 : : {
2866 : 34 : gcc_assert (integer_onep (fd->loops[j].m2));
2867 : 34 : t = unshare_expr (fd->loops[j].n2);
2868 : 34 : n2 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
2869 : : }
2870 : : else
2871 : : {
2872 : 196 : n2 = fold_convert (itype, unshare_expr (fd->loops[j].m2));
2873 : 196 : n2 = fold_build2 (MULT_EXPR, itype,
2874 : : vs[j - fd->loops[j].outer], n2);
2875 : 196 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2876 : : }
2877 : 876 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2878 : : true, GSI_SAME_STMT);
2879 : 876 : if (POINTER_TYPE_P (itype))
2880 : 74 : itype = signed_type_for (itype);
2881 : 876 : if (j == fd->last_nonrect)
2882 : : {
2883 : 376 : gcond *cond_stmt
2884 : 376 : = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2885 : : n1, n2);
2886 : 376 : e = split_block (cur_bb, cond_stmt);
2887 : 376 : e->flags = EDGE_TRUE_VALUE;
2888 : 376 : edge ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
2889 : 376 : e->probability = profile_probability::likely ().guessed ();
2890 : 376 : ne->probability = e->probability.invert ();
2891 : 376 : gsi2 = gsi_after_labels (e->dest);
2892 : :
2893 : 376 : t = build_int_cst (itype, (fd->loops[j].cond_code == LT_EXPR
2894 : 428 : ? -1 : 1));
2895 : 376 : t = fold_build2 (PLUS_EXPR, itype,
2896 : : fold_convert (itype, fd->loops[j].step), t);
2897 : 376 : t = fold_build2 (PLUS_EXPR, itype, t,
2898 : : fold_convert (itype, n2));
2899 : 376 : t = fold_build2 (MINUS_EXPR, itype, t,
2900 : : fold_convert (itype, n1));
2901 : 376 : tree step = fold_convert (itype, fd->loops[j].step);
2902 : 376 : if (TYPE_UNSIGNED (itype)
2903 : 376 : && fd->loops[j].cond_code == GT_EXPR)
2904 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2905 : : fold_build1 (NEGATE_EXPR, itype, t),
2906 : : fold_build1 (NEGATE_EXPR, itype, step));
2907 : : else
2908 : 376 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2909 : 376 : t = fold_convert (type, t);
2910 : 376 : t = fold_build2 (PLUS_EXPR, type, idx, t);
2911 : 376 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2912 : : true, GSI_SAME_STMT);
2913 : 376 : e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
2914 : 376 : set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
2915 : 376 : cond_stmt
2916 : 376 : = gimple_build_cond (LE_EXPR, t, stopval, NULL_TREE,
2917 : : NULL_TREE);
2918 : 376 : gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
2919 : 376 : e = split_block (gsi_bb (gsi2), cond_stmt);
2920 : 376 : e->flags = EDGE_TRUE_VALUE;
2921 : 376 : e->probability = profile_probability::likely ().guessed ();
2922 : 376 : ne = make_edge (e->src, entry_bb, EDGE_FALSE_VALUE);
2923 : 376 : ne->probability = e->probability.invert ();
2924 : 376 : gsi2 = gsi_after_labels (e->dest);
2925 : 376 : expand_omp_build_assign (&gsi2, idx, t);
2926 : 376 : set_immediate_dominator (CDI_DOMINATORS, entry_bb, dom_bb);
2927 : 376 : break;
2928 : : }
2929 : 500 : e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
2930 : :
2931 : 500 : basic_block new_cur_bb = create_empty_bb (cur_bb);
2932 : 500 : add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
2933 : :
2934 : 500 : gsi2 = gsi_after_labels (e->dest);
2935 : 500 : if (rect_p)
2936 : 93 : t = fold_build2 (PLUS_EXPR, type, vs[j],
2937 : : build_one_cst (type));
2938 : : else
2939 : : {
2940 : 407 : tree step
2941 : 407 : = fold_convert (itype, unshare_expr (fd->loops[j].step));
2942 : 407 : if (POINTER_TYPE_P (vtype))
2943 : 38 : t = fold_build_pointer_plus (vs[j], step);
2944 : : else
2945 : 369 : t = fold_build2 (PLUS_EXPR, itype, vs[j], step);
2946 : : }
2947 : 500 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2948 : : true, GSI_SAME_STMT);
2949 : 500 : expand_omp_build_assign (&gsi2, vs[j], t);
2950 : :
2951 : 500 : edge ne = split_block (e->dest, last_nondebug_stmt (e->dest));
2952 : 500 : gsi2 = gsi_after_labels (ne->dest);
2953 : :
2954 : 500 : gcond *cond_stmt;
2955 : 500 : if (next_bb == entry_bb)
2956 : : /* No need to actually check the outermost condition. */
2957 : 376 : cond_stmt
2958 : 376 : = gimple_build_cond (EQ_EXPR, boolean_true_node,
2959 : : boolean_true_node,
2960 : : NULL_TREE, NULL_TREE);
2961 : : else
2962 : 124 : cond_stmt
2963 : 155 : = gimple_build_cond (rect_p ? LT_EXPR
2964 : 31 : : fd->loops[j].cond_code,
2965 : : vs[j], n2, NULL_TREE, NULL_TREE);
2966 : 500 : gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
2967 : 500 : edge e3, e4;
2968 : 500 : if (next_bb == entry_bb)
2969 : : {
2970 : 376 : e3 = find_edge (ne->dest, next_bb);
2971 : 376 : e3->flags = EDGE_FALSE_VALUE;
2972 : 376 : dom_bb = ne->dest;
2973 : : }
2974 : : else
2975 : 124 : e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
2976 : 500 : e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
2977 : 500 : e4->probability = profile_probability::likely ().guessed ();
2978 : 500 : e3->probability = e4->probability.invert ();
2979 : 500 : basic_block esrc = e->src;
2980 : 500 : make_edge (e->src, ne->dest, EDGE_FALLTHRU);
2981 : 500 : cur_bb = new_cur_bb;
2982 : 500 : basic_block latch_bb = next_bb;
2983 : 500 : next_bb = e->dest;
2984 : 500 : remove_edge (e);
2985 : 500 : set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
2986 : 500 : set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
2987 : 500 : set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
2988 : : }
2989 : 1252 : for (int j = fd->last_nonrect; j >= fd->first_nonrect; j--)
2990 : : {
2991 : 876 : tree vtype = TREE_TYPE (fd->loops[j].v);
2992 : 876 : tree itype = vtype;
2993 : 876 : if (POINTER_TYPE_P (itype))
2994 : 74 : itype = signed_type_for (itype);
2995 : 1752 : bool rect_p = (fd->loops[j].m1 == NULL_TREE
2996 : 527 : && fd->loops[j].m2 == NULL_TREE
2997 : 1345 : && !fd->loops[j].non_rect_referenced);
2998 : 876 : if (j == fd->last_nonrect)
2999 : : {
3000 : 376 : t = fold_build2 (MINUS_EXPR, type, stopval, idx);
3001 : 376 : t = fold_convert (itype, t);
3002 : 376 : tree t2
3003 : 376 : = fold_convert (itype, unshare_expr (fd->loops[j].step));
3004 : 376 : t = fold_build2 (MULT_EXPR, itype, t, t2);
3005 : 376 : if (POINTER_TYPE_P (vtype))
3006 : 32 : t = fold_build_pointer_plus (n1, t);
3007 : : else
3008 : 344 : t = fold_build2 (PLUS_EXPR, itype, n1, t);
3009 : : }
3010 : 500 : else if (rect_p)
3011 : : {
3012 : 93 : t = fold_convert (itype, vs[j]);
3013 : 93 : t = fold_build2 (MULT_EXPR, itype, t,
3014 : : fold_convert (itype, fd->loops[j].step));
3015 : 93 : if (POINTER_TYPE_P (vtype))
3016 : 4 : t = fold_build_pointer_plus (fd->loops[j].n1, t);
3017 : : else
3018 : 89 : t = fold_build2 (PLUS_EXPR, itype, fd->loops[j].n1, t);
3019 : : }
3020 : : else
3021 : 407 : t = vs[j];
3022 : 876 : t = force_gimple_operand_gsi (gsi, t, false,
3023 : : NULL_TREE, true,
3024 : : GSI_SAME_STMT);
3025 : 876 : stmt = gimple_build_assign (fd->loops[j].v, t);
3026 : 876 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3027 : : }
3028 : 376 : if (gsi_end_p (*gsi))
3029 : 752 : *gsi = gsi_last_bb (gsi_bb (*gsi));
3030 : : else
3031 : 0 : gsi_prev (gsi);
3032 : 376 : if (bb_triang)
3033 : : {
3034 : 236 : e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
3035 : 236 : make_edge (bb_triang, e->dest, EDGE_FALLTHRU);
3036 : 236 : *gsi = gsi_after_labels (e->dest);
3037 : 236 : if (!gsi_end_p (*gsi))
3038 : 0 : gsi_insert_before (gsi, gimple_build_nop (), GSI_NEW_STMT);
3039 : 236 : set_immediate_dominator (CDI_DOMINATORS, e->dest, bb_triang_dom);
3040 : : }
3041 : : }
3042 : : else
3043 : : {
3044 : 14272 : t = fold_convert (itype, t);
3045 : 14272 : t = fold_build2 (MULT_EXPR, itype, t,
3046 : : fold_convert (itype, fd->loops[i].step));
3047 : 14272 : if (POINTER_TYPE_P (vtype))
3048 : 1617 : t = fold_build_pointer_plus (fd->loops[i].n1, t);
3049 : : else
3050 : 12655 : t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
3051 : 14272 : t = force_gimple_operand_gsi (gsi, t,
3052 : 14272 : DECL_P (fd->loops[i].v)
3053 : 14272 : && TREE_ADDRESSABLE (fd->loops[i].v),
3054 : : NULL_TREE, false,
3055 : : GSI_CONTINUE_LINKING);
3056 : 14272 : stmt = gimple_build_assign (fd->loops[i].v, t);
3057 : 14272 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3058 : : }
3059 : 14648 : if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
3060 : : {
3061 : 9266 : t = fold_build2 (TRUNC_DIV_EXPR, type, tem, counts[i]);
3062 : 9266 : t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
3063 : : false, GSI_CONTINUE_LINKING);
3064 : 9266 : stmt = gimple_build_assign (tem, t);
3065 : 9266 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3066 : : }
3067 : 14648 : if (i == fd->last_nonrect)
3068 : 376 : i = fd->first_nonrect;
3069 : : }
3070 : 5382 : if (fd->non_rect)
3071 : 1351 : for (i = 0; i <= fd->last_nonrect; i++)
3072 : 975 : if (fd->loops[i].m2)
3073 : : {
3074 : 230 : tree itype = TREE_TYPE (fd->loops[i].v);
3075 : :
3076 : 230 : tree t;
3077 : 230 : if (POINTER_TYPE_P (itype))
3078 : : {
3079 : 34 : gcc_assert (integer_onep (fd->loops[i].m2));
3080 : 34 : t = fold_build_pointer_plus (fd->loops[i - fd->loops[i].outer].v,
3081 : : unshare_expr (fd->loops[i].n2));
3082 : : }
3083 : : else
3084 : : {
3085 : 196 : t = fold_convert (itype, unshare_expr (fd->loops[i].m2));
3086 : 196 : t = fold_build2 (MULT_EXPR, itype,
3087 : : fd->loops[i - fd->loops[i].outer].v, t);
3088 : 196 : t = fold_build2 (PLUS_EXPR, itype, t,
3089 : : fold_convert (itype,
3090 : : unshare_expr (fd->loops[i].n2)));
3091 : : }
3092 : 230 : nonrect_bounds[i] = create_tmp_reg (itype, ".bound");
3093 : 230 : t = force_gimple_operand_gsi (gsi, t, false,
3094 : : NULL_TREE, false,
3095 : : GSI_CONTINUE_LINKING);
3096 : 230 : stmt = gimple_build_assign (nonrect_bounds[i], t);
3097 : 230 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3098 : : }
3099 : : }
3100 : :
3101 : : /* Helper function for expand_omp_for_*. Generate code like:
3102 : : L10:
3103 : : V3 += STEP3;
3104 : : if (V3 cond3 N32) goto BODY_BB; else goto L11;
3105 : : L11:
3106 : : V3 = N31;
3107 : : V2 += STEP2;
3108 : : if (V2 cond2 N22) goto BODY_BB; else goto L12;
3109 : : L12:
3110 : : V2 = N21;
3111 : : V1 += STEP1;
3112 : : goto BODY_BB;
3113 : : For non-rectangular loops, use temporaries stored in nonrect_bounds
3114 : : for the upper bounds if M?2 multiplier is present. Given e.g.
3115 : : for (V1 = N11; V1 cond1 N12; V1 += STEP1)
3116 : : for (V2 = N21; V2 cond2 N22; V2 += STEP2)
3117 : : for (V3 = N31; V3 cond3 N32; V3 += STEP3)
3118 : : for (V4 = N41 + M41 * V2; V4 cond4 N42 + M42 * V2; V4 += STEP4)
3119 : : do:
3120 : : L10:
3121 : : V4 += STEP4;
3122 : : if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L11;
3123 : : L11:
3124 : : V4 = N41 + M41 * V2; // This can be left out if the loop
3125 : : // refers to the immediate parent loop
3126 : : V3 += STEP3;
3127 : : if (V3 cond3 N32) goto BODY_BB; else goto L12;
3128 : : L12:
3129 : : V3 = N31;
3130 : : V2 += STEP2;
3131 : : if (V2 cond2 N22) goto L120; else goto L13;
3132 : : L120:
3133 : : V4 = N41 + M41 * V2;
3134 : : NONRECT_BOUND4 = N42 + M42 * V2;
3135 : : if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L12;
3136 : : L13:
3137 : : V2 = N21;
3138 : : V1 += STEP1;
3139 : : goto L120; */
3140 : :
3141 : : static basic_block
3142 : 2582 : extract_omp_for_update_vars (struct omp_for_data *fd, tree *nonrect_bounds,
3143 : : basic_block cont_bb, basic_block body_bb)
3144 : : {
3145 : 2582 : basic_block last_bb, bb, collapse_bb = NULL;
3146 : 2582 : int i;
3147 : 2582 : gimple_stmt_iterator gsi;
3148 : 2582 : edge e;
3149 : 2582 : tree t;
3150 : 2582 : gimple *stmt;
3151 : :
3152 : 2582 : last_bb = cont_bb;
3153 : 9699 : for (i = fd->collapse - 1; i >= 0; i--)
3154 : : {
3155 : 7117 : tree vtype = TREE_TYPE (fd->loops[i].v);
3156 : :
3157 : 7117 : bb = create_empty_bb (last_bb);
3158 : 7117 : add_bb_to_loop (bb, last_bb->loop_father);
3159 : 7117 : gsi = gsi_start_bb (bb);
3160 : :
3161 : 7117 : if (i < fd->collapse - 1)
3162 : : {
3163 : 4535 : e = make_edge (last_bb, bb, EDGE_FALSE_VALUE);
3164 : 4535 : e->probability = profile_probability::guessed_always () / 8;
3165 : :
3166 : 4535 : struct omp_for_data_loop *l = &fd->loops[i + 1];
3167 : 4535 : if (l->m1 == NULL_TREE || l->outer != 1)
3168 : : {
3169 : 4327 : t = l->n1;
3170 : 4327 : if (l->m1)
3171 : : {
3172 : 56 : if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3173 : 3 : t = fold_build_pointer_plus (fd->loops[i + 1 - l->outer].v,
3174 : : t);
3175 : : else
3176 : : {
3177 : 53 : tree t2
3178 : 53 : = fold_build2 (MULT_EXPR, TREE_TYPE (t),
3179 : : fd->loops[i + 1 - l->outer].v, l->m1);
3180 : 53 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t2, t);
3181 : : }
3182 : : }
3183 : 4327 : t = force_gimple_operand_gsi (&gsi, t,
3184 : 4327 : DECL_P (l->v)
3185 : 4327 : && TREE_ADDRESSABLE (l->v),
3186 : : NULL_TREE, false,
3187 : : GSI_CONTINUE_LINKING);
3188 : 4327 : stmt = gimple_build_assign (l->v, t);
3189 : 4327 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3190 : : }
3191 : : }
3192 : : else
3193 : : collapse_bb = bb;
3194 : :
3195 : 7117 : set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
3196 : :
3197 : 7117 : if (POINTER_TYPE_P (vtype))
3198 : 902 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
3199 : : else
3200 : 6215 : t = fold_build2 (PLUS_EXPR, vtype, fd->loops[i].v, fd->loops[i].step);
3201 : 7117 : t = force_gimple_operand_gsi (&gsi, t,
3202 : 7117 : DECL_P (fd->loops[i].v)
3203 : 7117 : && TREE_ADDRESSABLE (fd->loops[i].v),
3204 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
3205 : 7117 : stmt = gimple_build_assign (fd->loops[i].v, t);
3206 : 7117 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3207 : :
3208 : 7117 : if (fd->loops[i].non_rect_referenced)
3209 : : {
3210 : 308 : basic_block update_bb = NULL, prev_bb = NULL;
3211 : 714 : for (int j = i + 1; j <= fd->last_nonrect; j++)
3212 : 406 : if (j - fd->loops[j].outer == i)
3213 : : {
3214 : 308 : tree n1, n2;
3215 : 308 : struct omp_for_data_loop *l = &fd->loops[j];
3216 : 308 : basic_block this_bb = create_empty_bb (last_bb);
3217 : 308 : add_bb_to_loop (this_bb, last_bb->loop_father);
3218 : 308 : gimple_stmt_iterator gsi2 = gsi_start_bb (this_bb);
3219 : 308 : if (prev_bb)
3220 : : {
3221 : 0 : e = make_edge (prev_bb, this_bb, EDGE_TRUE_VALUE);
3222 : 0 : e->probability
3223 : 0 : = profile_probability::guessed_always ().apply_scale (7,
3224 : : 8);
3225 : 0 : set_immediate_dominator (CDI_DOMINATORS, this_bb, prev_bb);
3226 : : }
3227 : 308 : if (l->m1)
3228 : : {
3229 : 264 : if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3230 : 26 : t = fold_build_pointer_plus (fd->loops[i].v, l->n1);
3231 : : else
3232 : : {
3233 : 238 : t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m1), l->m1,
3234 : : fd->loops[i].v);
3235 : 238 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (l->v),
3236 : : t, l->n1);
3237 : : }
3238 : 264 : n1 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3239 : : false,
3240 : : GSI_CONTINUE_LINKING);
3241 : 264 : stmt = gimple_build_assign (l->v, n1);
3242 : 264 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
3243 : 264 : n1 = l->v;
3244 : : }
3245 : : else
3246 : 44 : n1 = force_gimple_operand_gsi (&gsi2, l->n1, true,
3247 : : NULL_TREE, false,
3248 : : GSI_CONTINUE_LINKING);
3249 : 308 : if (l->m2)
3250 : : {
3251 : 204 : if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3252 : 30 : t = fold_build_pointer_plus (fd->loops[i].v, l->n2);
3253 : : else
3254 : : {
3255 : 174 : t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m2), l->m2,
3256 : : fd->loops[i].v);
3257 : 174 : t = fold_build2 (PLUS_EXPR,
3258 : : TREE_TYPE (nonrect_bounds[j]),
3259 : : t, unshare_expr (l->n2));
3260 : : }
3261 : 204 : n2 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3262 : : false,
3263 : : GSI_CONTINUE_LINKING);
3264 : 204 : stmt = gimple_build_assign (nonrect_bounds[j], n2);
3265 : 204 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
3266 : 204 : n2 = nonrect_bounds[j];
3267 : : }
3268 : : else
3269 : 104 : n2 = force_gimple_operand_gsi (&gsi2, unshare_expr (l->n2),
3270 : : true, NULL_TREE, false,
3271 : : GSI_CONTINUE_LINKING);
3272 : 308 : gcond *cond_stmt
3273 : 308 : = gimple_build_cond (l->cond_code, n1, n2,
3274 : : NULL_TREE, NULL_TREE);
3275 : 308 : gsi_insert_after (&gsi2, cond_stmt, GSI_CONTINUE_LINKING);
3276 : 308 : if (update_bb == NULL)
3277 : 308 : update_bb = this_bb;
3278 : 308 : e = make_edge (this_bb, bb, EDGE_FALSE_VALUE);
3279 : 308 : e->probability = profile_probability::guessed_always () / 8;
3280 : 308 : if (prev_bb == NULL)
3281 : 308 : set_immediate_dominator (CDI_DOMINATORS, this_bb, bb);
3282 : 308 : prev_bb = this_bb;
3283 : : }
3284 : 308 : e = make_edge (prev_bb, body_bb, EDGE_TRUE_VALUE);
3285 : 308 : e->probability
3286 : 308 : = profile_probability::guessed_always ().apply_scale (7, 8);
3287 : 308 : body_bb = update_bb;
3288 : : }
3289 : :
3290 : 7117 : if (i > 0)
3291 : : {
3292 : 4535 : if (fd->loops[i].m2)
3293 : 204 : t = nonrect_bounds[i];
3294 : : else
3295 : 4331 : t = unshare_expr (fd->loops[i].n2);
3296 : 4535 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
3297 : : false, GSI_CONTINUE_LINKING);
3298 : 4535 : tree v = fd->loops[i].v;
3299 : 4535 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
3300 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
3301 : : false, GSI_CONTINUE_LINKING);
3302 : 4535 : t = fold_build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
3303 : 4535 : stmt = gimple_build_cond_empty (t);
3304 : 4535 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3305 : 4535 : if (walk_tree (gimple_cond_lhs_ptr (as_a <gcond *> (stmt)),
3306 : : expand_omp_regimplify_p, NULL, NULL)
3307 : 4535 : || walk_tree (gimple_cond_rhs_ptr (as_a <gcond *> (stmt)),
3308 : : expand_omp_regimplify_p, NULL, NULL))
3309 : 4 : gimple_regimplify_operands (stmt, &gsi);
3310 : 4535 : e = make_edge (bb, body_bb, EDGE_TRUE_VALUE);
3311 : 4535 : e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
3312 : : }
3313 : : else
3314 : 2582 : make_edge (bb, body_bb, EDGE_FALLTHRU);
3315 : 7117 : set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
3316 : 7117 : last_bb = bb;
3317 : : }
3318 : :
3319 : 2582 : return collapse_bb;
3320 : : }
3321 : :
3322 : : /* Expand #pragma omp ordered depend(source). */
3323 : :
3324 : : static void
3325 : 331 : expand_omp_ordered_source (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
3326 : : tree *counts, location_t loc)
3327 : : {
3328 : 56 : enum built_in_function source_ix
3329 : 331 : = fd->iter_type == long_integer_type_node
3330 : 331 : ? BUILT_IN_GOMP_DOACROSS_POST : BUILT_IN_GOMP_DOACROSS_ULL_POST;
3331 : 331 : gimple *g
3332 : 331 : = gimple_build_call (builtin_decl_explicit (source_ix), 1,
3333 : 331 : build_fold_addr_expr (counts[fd->ordered]));
3334 : 331 : gimple_set_location (g, loc);
3335 : 331 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3336 : 331 : }
3337 : :
3338 : : /* Expand a single depend from #pragma omp ordered depend(sink:...). */
3339 : :
3340 : : static void
3341 : 462 : expand_omp_ordered_sink (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
3342 : : tree *counts, tree c, location_t loc,
3343 : : basic_block cont_bb)
3344 : : {
3345 : 462 : auto_vec<tree, 10> args;
3346 : 73 : enum built_in_function sink_ix
3347 : 462 : = fd->iter_type == long_integer_type_node
3348 : 462 : ? BUILT_IN_GOMP_DOACROSS_WAIT : BUILT_IN_GOMP_DOACROSS_ULL_WAIT;
3349 : 462 : tree t, off, coff = NULL_TREE, deps = OMP_CLAUSE_DECL (c), cond = NULL_TREE;
3350 : 462 : int i;
3351 : 462 : gimple_stmt_iterator gsi2 = *gsi;
3352 : 462 : bool warned_step = false;
3353 : :
3354 : 462 : if (deps == NULL)
3355 : : {
3356 : : /* Handle doacross(sink: omp_cur_iteration - 1). */
3357 : 52 : gsi_prev (&gsi2);
3358 : 52 : edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
3359 : 52 : edge e2 = split_block_after_labels (e1->dest);
3360 : 52 : gsi2 = gsi_after_labels (e1->dest);
3361 : 52 : *gsi = gsi_last_bb (e1->src);
3362 : 52 : gimple_stmt_iterator gsi3 = *gsi;
3363 : :
3364 : 52 : if (counts[fd->collapse - 1])
3365 : : {
3366 : 8 : gcc_assert (fd->collapse == 1);
3367 : 8 : t = counts[fd->collapse - 1];
3368 : : }
3369 : 44 : else if (fd->collapse > 1)
3370 : 28 : t = fd->loop.v;
3371 : : else
3372 : : {
3373 : 16 : t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
3374 : : fd->loops[0].v, fd->loops[0].n1);
3375 : 16 : t = fold_convert (fd->iter_type, t);
3376 : : }
3377 : :
3378 : 52 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
3379 : : false, GSI_CONTINUE_LINKING);
3380 : 52 : gsi_insert_after (gsi, gimple_build_cond (NE_EXPR, t,
3381 : 52 : build_zero_cst (TREE_TYPE (t)),
3382 : : NULL_TREE, NULL_TREE),
3383 : : GSI_NEW_STMT);
3384 : :
3385 : 52 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
3386 : : build_minus_one_cst (TREE_TYPE (t)));
3387 : 52 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3388 : : true, GSI_SAME_STMT);
3389 : 52 : args.safe_push (t);
3390 : 281 : for (i = fd->collapse; i < fd->ordered; i++)
3391 : : {
3392 : 229 : t = counts[fd->ordered + 2 + (i - fd->collapse)];
3393 : 229 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
3394 : : build_minus_one_cst (TREE_TYPE (t)));
3395 : 229 : t = fold_convert (fd->iter_type, t);
3396 : 229 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3397 : : true, GSI_SAME_STMT);
3398 : 229 : args.safe_push (t);
3399 : : }
3400 : :
3401 : 52 : gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix),
3402 : : args);
3403 : 52 : gimple_set_location (g, loc);
3404 : 52 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
3405 : :
3406 : 52 : edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
3407 : 52 : e3->probability = profile_probability::guessed_always () / 8;
3408 : 52 : e1->probability = e3->probability.invert ();
3409 : 52 : e1->flags = EDGE_TRUE_VALUE;
3410 : 52 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
3411 : :
3412 : 52 : if (fd->ordered > fd->collapse && cont_bb)
3413 : : {
3414 : 33 : if (counts[fd->ordered + 1] == NULL_TREE)
3415 : 33 : counts[fd->ordered + 1]
3416 : 33 : = create_tmp_var (boolean_type_node, ".first");
3417 : :
3418 : 33 : edge e4;
3419 : 33 : if (gsi_end_p (gsi3))
3420 : 25 : e4 = split_block_after_labels (e1->src);
3421 : : else
3422 : : {
3423 : 8 : gsi_prev (&gsi3);
3424 : 8 : e4 = split_block (gsi_bb (gsi3), gsi_stmt (gsi3));
3425 : : }
3426 : 33 : gsi3 = gsi_last_bb (e4->src);
3427 : :
3428 : 33 : gsi_insert_after (&gsi3,
3429 : 33 : gimple_build_cond (NE_EXPR,
3430 : 33 : counts[fd->ordered + 1],
3431 : : boolean_false_node,
3432 : : NULL_TREE, NULL_TREE),
3433 : : GSI_NEW_STMT);
3434 : :
3435 : 33 : edge e5 = make_edge (e4->src, e2->dest, EDGE_FALSE_VALUE);
3436 : 33 : e4->probability = profile_probability::guessed_always () / 8;
3437 : 33 : e5->probability = e4->probability.invert ();
3438 : 33 : e4->flags = EDGE_TRUE_VALUE;
3439 : 33 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e4->src);
3440 : : }
3441 : :
3442 : 52 : *gsi = gsi_after_labels (e2->dest);
3443 : 52 : return;
3444 : : }
3445 : 500 : for (i = 0; i < fd->ordered; i++)
3446 : : {
3447 : 498 : tree step = NULL_TREE;
3448 : 498 : off = TREE_PURPOSE (deps);
3449 : 498 : if (TREE_CODE (off) == TRUNC_DIV_EXPR)
3450 : : {
3451 : 83 : step = TREE_OPERAND (off, 1);
3452 : 83 : off = TREE_OPERAND (off, 0);
3453 : : }
3454 : 498 : if (!integer_zerop (off))
3455 : : {
3456 : 408 : gcc_assert (fd->loops[i].cond_code == LT_EXPR
3457 : : || fd->loops[i].cond_code == GT_EXPR);
3458 : 408 : bool forward = fd->loops[i].cond_code == LT_EXPR;
3459 : 408 : if (step)
3460 : : {
3461 : : /* Non-simple Fortran DO loops. If step is variable,
3462 : : we don't know at compile even the direction, so can't
3463 : : warn. */
3464 : 83 : if (TREE_CODE (step) != INTEGER_CST)
3465 : : break;
3466 : 0 : forward = tree_int_cst_sgn (step) != -1;
3467 : : }
3468 : 325 : if (forward ^ OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3469 : 17 : warning_at (loc, OPT_Wopenmp,
3470 : : "%qs clause with %<sink%> modifier "
3471 : : "waiting for lexically later iteration",
3472 : 17 : OMP_CLAUSE_DOACROSS_DEPEND (c)
3473 : : ? "depend" : "doacross");
3474 : : break;
3475 : : }
3476 : 90 : deps = TREE_CHAIN (deps);
3477 : : }
3478 : : /* If all offsets corresponding to the collapsed loops are zero,
3479 : : this depend clause can be ignored. FIXME: but there is still a
3480 : : flush needed. We need to emit one __sync_synchronize () for it
3481 : : though (perhaps conditionally)? Solve this together with the
3482 : : conservative dependence folding optimization.
3483 : : if (i >= fd->collapse)
3484 : : return; */
3485 : :
3486 : 410 : deps = OMP_CLAUSE_DECL (c);
3487 : 410 : gsi_prev (&gsi2);
3488 : 410 : edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
3489 : 410 : edge e2 = split_block_after_labels (e1->dest);
3490 : :
3491 : 410 : gsi2 = gsi_after_labels (e1->dest);
3492 : 410 : *gsi = gsi_last_bb (e1->src);
3493 : 2798 : for (i = 0; i < fd->ordered; i++)
3494 : : {
3495 : 2388 : tree itype = TREE_TYPE (fd->loops[i].v);
3496 : 2388 : tree step = NULL_TREE;
3497 : 2388 : tree orig_off = NULL_TREE;
3498 : 2388 : if (POINTER_TYPE_P (itype))
3499 : 9 : itype = sizetype;
3500 : 2388 : if (i)
3501 : 1978 : deps = TREE_CHAIN (deps);
3502 : 2388 : off = TREE_PURPOSE (deps);
3503 : 2388 : if (TREE_CODE (off) == TRUNC_DIV_EXPR)
3504 : : {
3505 : 204 : step = TREE_OPERAND (off, 1);
3506 : 204 : off = TREE_OPERAND (off, 0);
3507 : 204 : gcc_assert (fd->loops[i].cond_code == LT_EXPR
3508 : : && integer_onep (fd->loops[i].step)
3509 : : && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)));
3510 : : }
3511 : 2388 : tree s = fold_convert_loc (loc, itype, step ? step : fd->loops[i].step);
3512 : 2388 : if (step)
3513 : : {
3514 : 204 : off = fold_convert_loc (loc, itype, off);
3515 : 204 : orig_off = off;
3516 : 204 : off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
3517 : : }
3518 : :
3519 : 2388 : if (integer_zerop (off))
3520 : 1516 : t = boolean_true_node;
3521 : : else
3522 : : {
3523 : 872 : tree a;
3524 : 872 : tree co = fold_convert_loc (loc, itype, off);
3525 : 872 : if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
3526 : : {
3527 : 9 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3528 : 0 : co = fold_build1_loc (loc, NEGATE_EXPR, itype, co);
3529 : 9 : a = fold_build2_loc (loc, POINTER_PLUS_EXPR,
3530 : 9 : TREE_TYPE (fd->loops[i].v), fd->loops[i].v,
3531 : : co);
3532 : : }
3533 : 863 : else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3534 : 636 : a = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3535 : : fd->loops[i].v, co);
3536 : : else
3537 : 227 : a = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
3538 : : fd->loops[i].v, co);
3539 : 872 : if (step)
3540 : : {
3541 : 204 : tree t1, t2;
3542 : 204 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3543 : 149 : t1 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3544 : 149 : fd->loops[i].n1);
3545 : : else
3546 : 55 : t1 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3547 : 55 : fd->loops[i].n2);
3548 : 204 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3549 : 149 : t2 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3550 : 149 : fd->loops[i].n2);
3551 : : else
3552 : 55 : t2 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3553 : 55 : fd->loops[i].n1);
3554 : 204 : t = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
3555 : 204 : step, build_int_cst (TREE_TYPE (step), 0));
3556 : 204 : if (TREE_CODE (step) != INTEGER_CST)
3557 : : {
3558 : 204 : t1 = unshare_expr (t1);
3559 : 204 : t1 = force_gimple_operand_gsi (gsi, t1, true, NULL_TREE,
3560 : : false, GSI_CONTINUE_LINKING);
3561 : 204 : t2 = unshare_expr (t2);
3562 : 204 : t2 = force_gimple_operand_gsi (gsi, t2, true, NULL_TREE,
3563 : : false, GSI_CONTINUE_LINKING);
3564 : : }
3565 : 204 : t = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
3566 : : t, t2, t1);
3567 : : }
3568 : 668 : else if (fd->loops[i].cond_code == LT_EXPR)
3569 : : {
3570 : 516 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3571 : 429 : t = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3572 : : fd->loops[i].n1);
3573 : : else
3574 : 87 : t = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3575 : : fd->loops[i].n2);
3576 : : }
3577 : 152 : else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3578 : 58 : t = fold_build2_loc (loc, GT_EXPR, boolean_type_node, a,
3579 : : fd->loops[i].n2);
3580 : : else
3581 : 94 : t = fold_build2_loc (loc, LE_EXPR, boolean_type_node, a,
3582 : : fd->loops[i].n1);
3583 : : }
3584 : 2388 : if (cond)
3585 : 1978 : cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node, cond, t);
3586 : : else
3587 : 410 : cond = t;
3588 : :
3589 : 2388 : off = fold_convert_loc (loc, itype, off);
3590 : :
3591 : 2388 : if (step
3592 : 4403 : || (fd->loops[i].cond_code == LT_EXPR
3593 : 2184 : ? !integer_onep (fd->loops[i].step)
3594 : 169 : : !integer_minus_onep (fd->loops[i].step)))
3595 : : {
3596 : 382 : if (step == NULL_TREE
3597 : 178 : && TYPE_UNSIGNED (itype)
3598 : 393 : && fd->loops[i].cond_code == GT_EXPR)
3599 : 9 : t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype, off,
3600 : : fold_build1_loc (loc, NEGATE_EXPR, itype,
3601 : : s));
3602 : : else
3603 : 542 : t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype,
3604 : : orig_off ? orig_off : off, s);
3605 : 382 : t = fold_build2_loc (loc, EQ_EXPR, boolean_type_node, t,
3606 : : build_int_cst (itype, 0));
3607 : 382 : if (integer_zerop (t) && !warned_step)
3608 : : {
3609 : 5 : warning_at (loc, OPT_Wopenmp,
3610 : : "%qs clause with %<sink%> modifier refers to "
3611 : : "iteration never in the iteration space",
3612 : 5 : OMP_CLAUSE_DOACROSS_DEPEND (c)
3613 : : ? "depend" : "doacross");
3614 : 5 : warned_step = true;
3615 : : }
3616 : 382 : cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node,
3617 : : cond, t);
3618 : : }
3619 : :
3620 : 2388 : if (i <= fd->collapse - 1 && fd->collapse > 1)
3621 : 322 : t = fd->loop.v;
3622 : 2066 : else if (counts[i])
3623 : 166 : t = counts[i];
3624 : : else
3625 : : {
3626 : 1900 : t = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3627 : 1900 : fd->loops[i].v, fd->loops[i].n1);
3628 : 1900 : t = fold_convert_loc (loc, fd->iter_type, t);
3629 : : }
3630 : 2388 : if (step)
3631 : : /* We have divided off by step already earlier. */;
3632 : 2184 : else if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
3633 : 9 : off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off,
3634 : : fold_build1_loc (loc, NEGATE_EXPR, itype,
3635 : : s));
3636 : : else
3637 : 2175 : off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
3638 : 2388 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3639 : 636 : off = fold_build1_loc (loc, NEGATE_EXPR, itype, off);
3640 : 2388 : off = fold_convert_loc (loc, fd->iter_type, off);
3641 : 2388 : if (i <= fd->collapse - 1 && fd->collapse > 1)
3642 : : {
3643 : 322 : if (i)
3644 : 161 : off = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, coff,
3645 : : off);
3646 : 322 : if (i < fd->collapse - 1)
3647 : : {
3648 : 322 : coff = fold_build2_loc (loc, MULT_EXPR, fd->iter_type, off,
3649 : 161 : counts[i]);
3650 : 161 : continue;
3651 : : }
3652 : : }
3653 : 2227 : off = unshare_expr (off);
3654 : 2227 : t = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, t, off);
3655 : 2227 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3656 : : true, GSI_SAME_STMT);
3657 : 2227 : args.safe_push (t);
3658 : : }
3659 : 410 : gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix), args);
3660 : 410 : gimple_set_location (g, loc);
3661 : 410 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
3662 : :
3663 : 410 : cond = unshare_expr (cond);
3664 : 410 : cond = force_gimple_operand_gsi (gsi, cond, true, NULL_TREE, false,
3665 : : GSI_CONTINUE_LINKING);
3666 : 410 : gsi_insert_after (gsi, gimple_build_cond_empty (cond), GSI_NEW_STMT);
3667 : 410 : edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
3668 : 410 : e3->probability = profile_probability::guessed_always () / 8;
3669 : 410 : e1->probability = e3->probability.invert ();
3670 : 410 : e1->flags = EDGE_TRUE_VALUE;
3671 : 410 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
3672 : :
3673 : 410 : *gsi = gsi_after_labels (e2->dest);
3674 : 462 : }
3675 : :
3676 : : /* Expand all #pragma omp ordered depend(source) and
3677 : : #pragma omp ordered depend(sink:...) constructs in the current
3678 : : #pragma omp for ordered(n) region. */
3679 : :
3680 : : static void
3681 : 331 : expand_omp_ordered_source_sink (struct omp_region *region,
3682 : : struct omp_for_data *fd, tree *counts,
3683 : : basic_block cont_bb)
3684 : : {
3685 : 331 : struct omp_region *inner;
3686 : 331 : int i;
3687 : 1555 : for (i = fd->collapse - 1; i < fd->ordered; i++)
3688 : 1224 : if (i == fd->collapse - 1 && fd->collapse > 1)
3689 : 147 : counts[i] = NULL_TREE;
3690 : 1077 : else if (i >= fd->collapse && !cont_bb)
3691 : 0 : counts[i] = build_zero_cst (fd->iter_type);
3692 : 2146 : else if (!POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
3693 : 2146 : && integer_onep (fd->loops[i].step))
3694 : 983 : counts[i] = NULL_TREE;
3695 : : else
3696 : 94 : counts[i] = create_tmp_var (fd->iter_type, ".orditer");
3697 : 331 : tree atype
3698 : 331 : = build_array_type_nelts (fd->iter_type, fd->ordered - fd->collapse + 1);
3699 : 331 : counts[fd->ordered] = create_tmp_var (atype, ".orditera");
3700 : 331 : TREE_ADDRESSABLE (counts[fd->ordered]) = 1;
3701 : 331 : counts[fd->ordered + 1] = NULL_TREE;
3702 : :
3703 : 1603 : for (inner = region->inner; inner; inner = inner->next)
3704 : 1272 : if (inner->type == GIMPLE_OMP_ORDERED)
3705 : : {
3706 : 701 : gomp_ordered *ord_stmt = inner->ord_stmt;
3707 : 701 : gimple_stmt_iterator gsi = gsi_for_stmt (ord_stmt);
3708 : 701 : location_t loc = gimple_location (ord_stmt);
3709 : 701 : tree c;
3710 : 1163 : for (c = gimple_omp_ordered_clauses (ord_stmt);
3711 : 1163 : c; c = OMP_CLAUSE_CHAIN (c))
3712 : 793 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
3713 : : break;
3714 : 701 : if (c)
3715 : 331 : expand_omp_ordered_source (&gsi, fd, counts, loc);
3716 : 1494 : for (c = gimple_omp_ordered_clauses (ord_stmt);
3717 : 1494 : c; c = OMP_CLAUSE_CHAIN (c))
3718 : 793 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
3719 : 462 : expand_omp_ordered_sink (&gsi, fd, counts, c, loc, cont_bb);
3720 : 701 : gsi_remove (&gsi, true);
3721 : : }
3722 : 331 : }
3723 : :
3724 : : /* Wrap the body into fd->ordered - fd->collapse loops that aren't
3725 : : collapsed. */
3726 : :
3727 : : static basic_block
3728 : 331 : expand_omp_for_ordered_loops (struct omp_for_data *fd, tree *counts,
3729 : : basic_block cont_bb, basic_block body_bb,
3730 : : basic_block l0_bb, bool ordered_lastprivate)
3731 : : {
3732 : 331 : if (fd->ordered == fd->collapse)
3733 : : return cont_bb;
3734 : :
3735 : 171 : if (!cont_bb)
3736 : : {
3737 : 0 : gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3738 : 0 : for (int i = fd->collapse; i < fd->ordered; i++)
3739 : : {
3740 : 0 : tree type = TREE_TYPE (fd->loops[i].v);
3741 : 0 : tree n1 = fold_convert (type, fd->loops[i].n1);
3742 : 0 : expand_omp_build_assign (&gsi, fd->loops[i].v, n1);
3743 : 0 : tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3744 : 0 : size_int (i - fd->collapse + 1),
3745 : : NULL_TREE, NULL_TREE);
3746 : 0 : expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
3747 : : }
3748 : 0 : return NULL;
3749 : : }
3750 : :
3751 : 1064 : for (int i = fd->ordered - 1; i >= fd->collapse; i--)
3752 : : {
3753 : 893 : tree t, type = TREE_TYPE (fd->loops[i].v);
3754 : 893 : gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3755 : 893 : if (counts[fd->ordered + 1] && i == fd->collapse)
3756 : 33 : expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
3757 : : boolean_true_node);
3758 : 893 : expand_omp_build_assign (&gsi, fd->loops[i].v,
3759 : 893 : fold_convert (type, fd->loops[i].n1));
3760 : 893 : if (counts[i])
3761 : 57 : expand_omp_build_assign (&gsi, counts[i],
3762 : : build_zero_cst (fd->iter_type));
3763 : 893 : tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3764 : 893 : size_int (i - fd->collapse + 1),
3765 : : NULL_TREE, NULL_TREE);
3766 : 893 : expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
3767 : 893 : if (!gsi_end_p (gsi))
3768 : 893 : gsi_prev (&gsi);
3769 : : else
3770 : 0 : gsi = gsi_last_bb (body_bb);
3771 : 893 : edge e1 = split_block (body_bb, gsi_stmt (gsi));
3772 : 893 : basic_block new_body = e1->dest;
3773 : 893 : if (body_bb == cont_bb)
3774 : 0 : cont_bb = new_body;
3775 : 893 : edge e2 = NULL;
3776 : 893 : basic_block new_header;
3777 : 893 : if (EDGE_COUNT (cont_bb->preds) > 0)
3778 : : {
3779 : 864 : gsi = gsi_last_bb (cont_bb);
3780 : 864 : if (POINTER_TYPE_P (type))
3781 : 0 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
3782 : : else
3783 : 864 : t = fold_build2 (PLUS_EXPR, type, fd->loops[i].v,
3784 : : fold_convert (type, fd->loops[i].step));
3785 : 864 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
3786 : 864 : if (counts[i])
3787 : : {
3788 : 57 : t = fold_build2 (PLUS_EXPR, fd->iter_type, counts[i],
3789 : : build_int_cst (fd->iter_type, 1));
3790 : 57 : expand_omp_build_assign (&gsi, counts[i], t);
3791 : 57 : t = counts[i];
3792 : : }
3793 : : else
3794 : : {
3795 : 807 : t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3796 : : fd->loops[i].v, fd->loops[i].n1);
3797 : 807 : t = fold_convert (fd->iter_type, t);
3798 : 807 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
3799 : : true, GSI_SAME_STMT);
3800 : : }
3801 : 864 : aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3802 : 864 : size_int (i - fd->collapse + 1),
3803 : : NULL_TREE, NULL_TREE);
3804 : 864 : expand_omp_build_assign (&gsi, aref, t);
3805 : 864 : if (counts[fd->ordered + 1] && i == fd->ordered - 1)
3806 : 30 : expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
3807 : : boolean_false_node);
3808 : 864 : gsi_prev (&gsi);
3809 : 864 : e2 = split_block (cont_bb, gsi_stmt (gsi));
3810 : 864 : new_header = e2->dest;
3811 : : }
3812 : : else
3813 : : new_header = cont_bb;
3814 : 893 : gsi = gsi_after_labels (new_header);
3815 : 893 : tree v = force_gimple_operand_gsi (&gsi, fd->loops[i].v, true, NULL_TREE,
3816 : : true, GSI_SAME_STMT);
3817 : 893 : tree n2
3818 : 893 : = force_gimple_operand_gsi (&gsi, fold_convert (type, fd->loops[i].n2),
3819 : : true, NULL_TREE, true, GSI_SAME_STMT);
3820 : 893 : t = build2 (fd->loops[i].cond_code, boolean_type_node, v, n2);
3821 : 893 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_NEW_STMT);
3822 : 893 : edge e3 = split_block (new_header, gsi_stmt (gsi));
3823 : 893 : cont_bb = e3->dest;
3824 : 893 : remove_edge (e1);
3825 : 893 : make_edge (body_bb, new_header, EDGE_FALLTHRU);
3826 : 893 : e3->flags = EDGE_FALSE_VALUE;
3827 : 893 : e3->probability = profile_probability::guessed_always () / 8;
3828 : 893 : e1 = make_edge (new_header, new_body, EDGE_TRUE_VALUE);
3829 : 893 : e1->probability = e3->probability.invert ();
3830 : :
3831 : 893 : set_immediate_dominator (CDI_DOMINATORS, new_header, body_bb);
3832 : 893 : set_immediate_dominator (CDI_DOMINATORS, new_body, new_header);
3833 : :
3834 : 893 : if (e2)
3835 : : {
3836 : 864 : class loop *loop = alloc_loop ();
3837 : 864 : loop->header = new_header;
3838 : 864 : loop->latch = e2->src;
3839 : 864 : add_loop (loop, l0_bb->loop_father);
3840 : : }
3841 : : }
3842 : :
3843 : : /* If there are any lastprivate clauses and it is possible some loops
3844 : : might have zero iterations, ensure all the decls are initialized,
3845 : : otherwise we could crash evaluating C++ class iterators with lastprivate
3846 : : clauses. */
3847 : : bool need_inits = false;
3848 : 171 : for (int i = fd->collapse; ordered_lastprivate && i < fd->ordered; i++)
3849 : 0 : if (need_inits)
3850 : : {
3851 : 0 : tree type = TREE_TYPE (fd->loops[i].v);
3852 : 0 : gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3853 : 0 : expand_omp_build_assign (&gsi, fd->loops[i].v,
3854 : : fold_convert (type, fd->loops[i].n1));
3855 : : }
3856 : : else
3857 : : {
3858 : 0 : tree type = TREE_TYPE (fd->loops[i].v);
3859 : 0 : tree this_cond = fold_build2 (fd->loops[i].cond_code,
3860 : : boolean_type_node,
3861 : : fold_convert (type, fd->loops[i].n1),
3862 : : fold_convert (type, fd->loops[i].n2));
3863 : 0 : if (!integer_onep (this_cond))
3864 : 0 : need_inits = true;
3865 : : }
3866 : :
3867 : : return cont_bb;
3868 : : }
3869 : :
3870 : : /* A subroutine of expand_omp_for. Generate code for a parallel
3871 : : loop with any schedule. Given parameters:
3872 : :
3873 : : for (V = N1; V cond N2; V += STEP) BODY;
3874 : :
3875 : : where COND is "<" or ">", we generate pseudocode
3876 : :
3877 : : more = GOMP_loop_foo_start (N1, N2, STEP, CHUNK, &istart0, &iend0);
3878 : : if (more) goto L0; else goto L3;
3879 : : L0:
3880 : : V = istart0;
3881 : : iend = iend0;
3882 : : L1:
3883 : : BODY;
3884 : : V += STEP;
3885 : : if (V cond iend) goto L1; else goto L2;
3886 : : L2:
3887 : : if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
3888 : : L3:
3889 : :
3890 : : If this is a combined omp parallel loop, instead of the call to
3891 : : GOMP_loop_foo_start, we call GOMP_loop_foo_next.
3892 : : If this is gimple_omp_for_combined_p loop, then instead of assigning
3893 : : V and iend in L0 we assign the first two _looptemp_ clause decls of the
3894 : : inner GIMPLE_OMP_FOR and V += STEP; and
3895 : : if (V cond iend) goto L1; else goto L2; are removed.
3896 : :
3897 : : For collapsed loops, given parameters:
3898 : : collapse(3)
3899 : : for (V1 = N11; V1 cond1 N12; V1 += STEP1)
3900 : : for (V2 = N21; V2 cond2 N22; V2 += STEP2)
3901 : : for (V3 = N31; V3 cond3 N32; V3 += STEP3)
3902 : : BODY;
3903 : :
3904 : : we generate pseudocode
3905 : :
3906 : : if (__builtin_expect (N32 cond3 N31, 0)) goto Z0;
3907 : : if (cond3 is <)
3908 : : adj = STEP3 - 1;
3909 : : else
3910 : : adj = STEP3 + 1;
3911 : : count3 = (adj + N32 - N31) / STEP3;
3912 : : if (__builtin_expect (N22 cond2 N21, 0)) goto Z0;
3913 : : if (cond2 is <)
3914 : : adj = STEP2 - 1;
3915 : : else
3916 : : adj = STEP2 + 1;
3917 : : count2 = (adj + N22 - N21) / STEP2;
3918 : : if (__builtin_expect (N12 cond1 N11, 0)) goto Z0;
3919 : : if (cond1 is <)
3920 : : adj = STEP1 - 1;
3921 : : else
3922 : : adj = STEP1 + 1;
3923 : : count1 = (adj + N12 - N11) / STEP1;
3924 : : count = count1 * count2 * count3;
3925 : : goto Z1;
3926 : : Z0:
3927 : : count = 0;
3928 : : Z1:
3929 : : more = GOMP_loop_foo_start (0, count, 1, CHUNK, &istart0, &iend0);
3930 : : if (more) goto L0; else goto L3;
3931 : : L0:
3932 : : V = istart0;
3933 : : T = V;
3934 : : V3 = N31 + (T % count3) * STEP3;
3935 : : T = T / count3;
3936 : : V2 = N21 + (T % count2) * STEP2;
3937 : : T = T / count2;
3938 : : V1 = N11 + T * STEP1;
3939 : : iend = iend0;
3940 : : L1:
3941 : : BODY;
3942 : : V += 1;
3943 : : if (V < iend) goto L10; else goto L2;
3944 : : L10:
3945 : : V3 += STEP3;
3946 : : if (V3 cond3 N32) goto L1; else goto L11;
3947 : : L11:
3948 : : V3 = N31;
3949 : : V2 += STEP2;
3950 : : if (V2 cond2 N22) goto L1; else goto L12;
3951 : : L12:
3952 : : V2 = N21;
3953 : : V1 += STEP1;
3954 : : goto L1;
3955 : : L2:
3956 : : if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
3957 : : L3:
3958 : :
3959 : : */
3960 : :
3961 : : static void
3962 : 4129 : expand_omp_for_generic (struct omp_region *region,
3963 : : struct omp_for_data *fd,
3964 : : enum built_in_function start_fn,
3965 : : enum built_in_function next_fn,
3966 : : tree sched_arg,
3967 : : gimple *inner_stmt)
3968 : : {
3969 : 4129 : tree type, istart0, iend0, iend;
3970 : 4129 : tree t, vmain, vback, bias = NULL_TREE;
3971 : 4129 : basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, collapse_bb;
3972 : 4129 : basic_block l2_bb = NULL, l3_bb = NULL;
3973 : 4129 : gimple_stmt_iterator gsi;
3974 : 4129 : gassign *assign_stmt;
3975 : 4129 : bool in_combined_parallel = is_combined_parallel (region);
3976 : 4129 : bool broken_loop = region->cont == NULL;
3977 : 4129 : edge e, ne;
3978 : 4129 : tree *counts = NULL;
3979 : 4129 : int i;
3980 : 4129 : bool ordered_lastprivate = false;
3981 : 4129 : bool offload = is_in_offload_region (region);
3982 : :
3983 : 4129 : gcc_assert (!broken_loop || !in_combined_parallel);
3984 : 4129 : gcc_assert (fd->iter_type == long_integer_type_node
3985 : : || !in_combined_parallel);
3986 : :
3987 : 4129 : entry_bb = region->entry;
3988 : 4129 : cont_bb = region->cont;
3989 : 4129 : collapse_bb = NULL;
3990 : 4129 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
3991 : 4129 : gcc_assert (broken_loop
3992 : : || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
3993 : 4129 : l0_bb = split_edge (FALLTHRU_EDGE (entry_bb));
3994 : 4129 : l1_bb = single_succ (l0_bb);
3995 : 4129 : if (!broken_loop)
3996 : : {
3997 : 3722 : l2_bb = create_empty_bb (cont_bb);
3998 : 3722 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == l1_bb
3999 : : || (single_succ_edge (BRANCH_EDGE (cont_bb)->dest)->dest
4000 : : == l1_bb));
4001 : 3722 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
4002 : : }
4003 : : else
4004 : : l2_bb = NULL;
4005 : 4129 : l3_bb = BRANCH_EDGE (entry_bb)->dest;
4006 : 4129 : exit_bb = region->exit;
4007 : :
4008 : 4129 : gsi = gsi_last_nondebug_bb (entry_bb);
4009 : :
4010 : 4129 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
4011 : 4129 : if (fd->ordered
4012 : 4129 : && omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4013 : : OMP_CLAUSE_LASTPRIVATE))
4014 : 4129 : ordered_lastprivate = false;
4015 : 4129 : tree reductions = NULL_TREE;
4016 : 4129 : tree mem = NULL_TREE, cond_var = NULL_TREE, condtemp = NULL_TREE;
4017 : 4129 : tree memv = NULL_TREE;
4018 : 4129 : if (fd->lastprivate_conditional)
4019 : : {
4020 : 46 : tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4021 : : OMP_CLAUSE__CONDTEMP_);
4022 : 46 : if (fd->have_pointer_condtemp)
4023 : 28 : condtemp = OMP_CLAUSE_DECL (c);
4024 : 46 : c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
4025 : 46 : cond_var = OMP_CLAUSE_DECL (c);
4026 : : }
4027 : 4129 : if (sched_arg)
4028 : : {
4029 : 169 : if (fd->have_reductemp)
4030 : : {
4031 : 149 : tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4032 : : OMP_CLAUSE__REDUCTEMP_);
4033 : 149 : reductions = OMP_CLAUSE_DECL (c);
4034 : 149 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
4035 : 149 : gimple *g = SSA_NAME_DEF_STMT (reductions);
4036 : 149 : reductions = gimple_assign_rhs1 (g);
4037 : 149 : OMP_CLAUSE_DECL (c) = reductions;
4038 : 149 : entry_bb = gimple_bb (g);
4039 : 149 : edge e = split_block (entry_bb, g);
4040 : 149 : if (region->entry == entry_bb)
4041 : 8 : region->entry = e->dest;
4042 : 298 : gsi = gsi_last_bb (entry_bb);
4043 : : }
4044 : : else
4045 : 20 : reductions = null_pointer_node;
4046 : 169 : if (fd->have_pointer_condtemp)
4047 : : {
4048 : 28 : tree type = TREE_TYPE (condtemp);
4049 : 28 : memv = create_tmp_var (type);
4050 : 28 : TREE_ADDRESSABLE (memv) = 1;
4051 : 28 : unsigned HOST_WIDE_INT sz
4052 : 28 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
4053 : 28 : sz *= fd->lastprivate_conditional;
4054 : 28 : expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
4055 : : false);
4056 : 28 : mem = build_fold_addr_expr (memv);
4057 : : }
4058 : : else
4059 : 141 : mem = null_pointer_node;
4060 : : }
4061 : 4129 : if (fd->collapse > 1 || fd->ordered)
4062 : : {
4063 : 1665 : int first_zero_iter1 = -1, first_zero_iter2 = -1;
4064 : 1665 : basic_block zero_iter1_bb = NULL, zero_iter2_bb = NULL, l2_dom_bb = NULL;
4065 : :
4066 : 1665 : counts = XALLOCAVEC (tree, fd->ordered
4067 : : ? fd->ordered + 2
4068 : : + (fd->ordered - fd->collapse)
4069 : : : fd->collapse);
4070 : 1665 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
4071 : : zero_iter1_bb, first_zero_iter1,
4072 : : zero_iter2_bb, first_zero_iter2, l2_dom_bb);
4073 : :
4074 : 1665 : if (zero_iter1_bb)
4075 : : {
4076 : : /* Some counts[i] vars might be uninitialized if
4077 : : some loop has zero iterations. But the body shouldn't
4078 : : be executed in that case, so just avoid uninit warnings. */
4079 : 1913 : for (i = first_zero_iter1;
4080 : 1913 : i < (fd->ordered ? fd->ordered : fd->collapse); i++)
4081 : 1502 : if (SSA_VAR_P (counts[i]))
4082 : 950 : suppress_warning (counts[i], OPT_Wuninitialized);
4083 : 411 : gsi_prev (&gsi);
4084 : 411 : e = split_block (entry_bb, gsi_stmt (gsi));
4085 : 411 : entry_bb = e->dest;
4086 : 411 : make_edge (zero_iter1_bb, entry_bb, EDGE_FALLTHRU);
4087 : 411 : gsi = gsi_last_nondebug_bb (entry_bb);
4088 : 411 : set_immediate_dominator (CDI_DOMINATORS, entry_bb,
4089 : : get_immediate_dominator (CDI_DOMINATORS,
4090 : : zero_iter1_bb));
4091 : : }
4092 : 1665 : if (zero_iter2_bb)
4093 : : {
4094 : : /* Some counts[i] vars might be uninitialized if
4095 : : some loop has zero iterations. But the body shouldn't
4096 : : be executed in that case, so just avoid uninit warnings. */
4097 : 394 : for (i = first_zero_iter2; i < fd->ordered; i++)
4098 : 276 : if (SSA_VAR_P (counts[i]))
4099 : 216 : suppress_warning (counts[i], OPT_Wuninitialized);
4100 : 118 : if (zero_iter1_bb)
4101 : 103 : make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
4102 : : else
4103 : : {
4104 : 15 : gsi_prev (&gsi);
4105 : 15 : e = split_block (entry_bb, gsi_stmt (gsi));
4106 : 15 : entry_bb = e->dest;
4107 : 15 : make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
4108 : 15 : gsi = gsi_last_nondebug_bb (entry_bb);
4109 : 15 : set_immediate_dominator (CDI_DOMINATORS, entry_bb,
4110 : : get_immediate_dominator
4111 : : (CDI_DOMINATORS, zero_iter2_bb));
4112 : : }
4113 : : }
4114 : 1665 : if (fd->collapse == 1)
4115 : : {
4116 : 184 : counts[0] = fd->loop.n2;
4117 : 184 : fd->loop = fd->loops[0];
4118 : : }
4119 : : }
4120 : :
4121 : 4129 : type = TREE_TYPE (fd->loop.v);
4122 : 4129 : istart0 = create_tmp_var (fd->iter_type, ".istart0");
4123 : 4129 : iend0 = create_tmp_var (fd->iter_type, ".iend0");
4124 : 4129 : TREE_ADDRESSABLE (istart0) = 1;
4125 : 4129 : TREE_ADDRESSABLE (iend0) = 1;
4126 : :
4127 : : /* See if we need to bias by LLONG_MIN. */
4128 : 4129 : if (fd->iter_type == long_long_unsigned_type_node
4129 : 778 : && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
4130 : 530 : && !TYPE_UNSIGNED (type)
4131 : 4129 : && fd->ordered == 0)
4132 : : {
4133 : 0 : tree n1, n2;
4134 : :
4135 : 0 : if (fd->loop.cond_code == LT_EXPR)
4136 : : {
4137 : 0 : n1 = fd->loop.n1;
4138 : 0 : n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
4139 : : }
4140 : : else
4141 : : {
4142 : 0 : n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
4143 : 0 : n2 = fd->loop.n1;
4144 : : }
4145 : 0 : if (TREE_CODE (n1) != INTEGER_CST
4146 : 0 : || TREE_CODE (n2) != INTEGER_CST
4147 : 0 : || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
4148 : 0 : bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
4149 : : }
4150 : :
4151 : 4129 : gimple_stmt_iterator gsif = gsi;
4152 : 4129 : gsi_prev (&gsif);
4153 : :
4154 : 4129 : tree arr = NULL_TREE;
4155 : 4129 : if (in_combined_parallel)
4156 : : {
4157 : 1084 : gcc_assert (fd->ordered == 0);
4158 : : /* In a combined parallel loop, emit a call to
4159 : : GOMP_loop_foo_next. */
4160 : 1084 : t = build_call_expr (builtin_decl_explicit (next_fn), 2,
4161 : : build_fold_addr_expr (istart0),
4162 : : build_fold_addr_expr (iend0));
4163 : : }
4164 : : else
4165 : : {
4166 : 3045 : tree t0, t1, t2, t3, t4;
4167 : : /* If this is not a combined parallel loop, emit a call to
4168 : : GOMP_loop_foo_start in ENTRY_BB. */
4169 : 3045 : t4 = build_fold_addr_expr (iend0);
4170 : 3045 : t3 = build_fold_addr_expr (istart0);
4171 : 3045 : if (fd->ordered)
4172 : : {
4173 : 662 : t0 = build_int_cst (unsigned_type_node,
4174 : 331 : fd->ordered - fd->collapse + 1);
4175 : 331 : arr = create_tmp_var (build_array_type_nelts (fd->iter_type,
4176 : 331 : fd->ordered
4177 : 331 : - fd->collapse + 1),
4178 : : ".omp_counts");
4179 : 331 : DECL_NAMELESS (arr) = 1;
4180 : 331 : TREE_ADDRESSABLE (arr) = 1;
4181 : 331 : TREE_STATIC (arr) = 1;
4182 : 331 : vec<constructor_elt, va_gc> *v;
4183 : 331 : vec_alloc (v, fd->ordered - fd->collapse + 1);
4184 : 331 : int idx;
4185 : :
4186 : 1555 : for (idx = 0; idx < fd->ordered - fd->collapse + 1; idx++)
4187 : : {
4188 : 1224 : tree c;
4189 : 1224 : if (idx == 0 && fd->collapse > 1)
4190 : 147 : c = fd->loop.n2;
4191 : : else
4192 : 1077 : c = counts[idx + fd->collapse - 1];
4193 : 1224 : tree purpose = size_int (idx);
4194 : 1224 : CONSTRUCTOR_APPEND_ELT (v, purpose, c);
4195 : 1224 : if (TREE_CODE (c) != INTEGER_CST)
4196 : 433 : TREE_STATIC (arr) = 0;
4197 : : }
4198 : :
4199 : 331 : DECL_INITIAL (arr) = build_constructor (TREE_TYPE (arr), v);
4200 : 331 : if (!TREE_STATIC (arr))
4201 : 232 : force_gimple_operand_gsi (&gsi, build1 (DECL_EXPR,
4202 : : void_type_node, arr),
4203 : : true, NULL_TREE, true, GSI_SAME_STMT);
4204 : 331 : t1 = build_fold_addr_expr (arr);
4205 : 331 : t2 = NULL_TREE;
4206 : : }
4207 : : else
4208 : : {
4209 : 2714 : t2 = fold_convert (fd->iter_type, fd->loop.step);
4210 : 2714 : t1 = fd->loop.n2;
4211 : 2714 : t0 = fd->loop.n1;
4212 : 2714 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
4213 : : {
4214 : 1152 : tree innerc
4215 : 1152 : = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4216 : : OMP_CLAUSE__LOOPTEMP_);
4217 : 1152 : gcc_assert (innerc);
4218 : 1152 : t0 = OMP_CLAUSE_DECL (innerc);
4219 : 1152 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4220 : : OMP_CLAUSE__LOOPTEMP_);
4221 : 1152 : gcc_assert (innerc);
4222 : 1152 : t1 = OMP_CLAUSE_DECL (innerc);
4223 : : }
4224 : 5316 : if (POINTER_TYPE_P (TREE_TYPE (t0))
4225 : 2714 : && TYPE_PRECISION (TREE_TYPE (t0))
4226 : 112 : != TYPE_PRECISION (fd->iter_type))
4227 : : {
4228 : : /* Avoid casting pointers to integer of a different size. */
4229 : 0 : tree itype = signed_type_for (type);
4230 : 0 : t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
4231 : 0 : t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
4232 : : }
4233 : : else
4234 : : {
4235 : 2714 : t1 = fold_convert (fd->iter_type, t1);
4236 : 2714 : t0 = fold_convert (fd->iter_type, t0);
4237 : : }
4238 : 2714 : if (bias)
4239 : : {
4240 : 0 : t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
4241 : 0 : t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
4242 : : }
4243 : : }
4244 : 3045 : if (fd->iter_type == long_integer_type_node || fd->ordered)
4245 : : {
4246 : 2323 : if (fd->chunk_size)
4247 : : {
4248 : 1574 : t = fold_convert (fd->iter_type, fd->chunk_size);
4249 : 1574 : t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
4250 : 1574 : if (sched_arg)
4251 : : {
4252 : 135 : if (fd->ordered)
4253 : 36 : t = build_call_expr (builtin_decl_explicit (start_fn),
4254 : : 8, t0, t1, sched_arg, t, t3, t4,
4255 : : reductions, mem);
4256 : : else
4257 : 99 : t = build_call_expr (builtin_decl_explicit (start_fn),
4258 : : 9, t0, t1, t2, sched_arg, t, t3, t4,
4259 : : reductions, mem);
4260 : : }
4261 : 1439 : else if (fd->ordered)
4262 : 295 : t = build_call_expr (builtin_decl_explicit (start_fn),
4263 : : 5, t0, t1, t, t3, t4);
4264 : : else
4265 : 1144 : t = build_call_expr (builtin_decl_explicit (start_fn),
4266 : : 6, t0, t1, t2, t, t3, t4);
4267 : : }
4268 : 749 : else if (fd->ordered)
4269 : 0 : t = build_call_expr (builtin_decl_explicit (start_fn),
4270 : : 4, t0, t1, t3, t4);
4271 : : else
4272 : 749 : t = build_call_expr (builtin_decl_explicit (start_fn),
4273 : : 5, t0, t1, t2, t3, t4);
4274 : : }
4275 : : else
4276 : : {
4277 : 722 : tree t5;
4278 : 722 : tree c_bool_type;
4279 : 722 : tree bfn_decl;
4280 : :
4281 : : /* The GOMP_loop_ull_*start functions have additional boolean
4282 : : argument, true for < loops and false for > loops.
4283 : : In Fortran, the C bool type can be different from
4284 : : boolean_type_node. */
4285 : 722 : bfn_decl = builtin_decl_explicit (start_fn);
4286 : 722 : c_bool_type = TREE_TYPE (TREE_TYPE (bfn_decl));
4287 : 722 : t5 = build_int_cst (c_bool_type,
4288 : 942 : fd->loop.cond_code == LT_EXPR ? 1 : 0);
4289 : 722 : if (fd->chunk_size)
4290 : : {
4291 : 390 : tree bfn_decl = builtin_decl_explicit (start_fn);
4292 : 390 : t = fold_convert (fd->iter_type, fd->chunk_size);
4293 : 390 : t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
4294 : 390 : if (sched_arg)
4295 : 34 : t = build_call_expr (bfn_decl, 10, t5, t0, t1, t2, sched_arg,
4296 : : t, t3, t4, reductions, mem);
4297 : : else
4298 : 356 : t = build_call_expr (bfn_decl, 7, t5, t0, t1, t2, t, t3, t4);
4299 : : }
4300 : : else
4301 : 332 : t = build_call_expr (builtin_decl_explicit (start_fn),
4302 : : 6, t5, t0, t1, t2, t3, t4);
4303 : : }
4304 : : }
4305 : 4129 : if (TREE_TYPE (t) != boolean_type_node)
4306 : 0 : t = fold_build2 (NE_EXPR, boolean_type_node,
4307 : : t, build_int_cst (TREE_TYPE (t), 0));
4308 : 4129 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4309 : : true, GSI_SAME_STMT);
4310 : 4129 : if (arr && !TREE_STATIC (arr))
4311 : : {
4312 : 232 : tree clobber = build_clobber (TREE_TYPE (arr));
4313 : 232 : gsi_insert_before (&gsi, gimple_build_assign (arr, clobber),
4314 : : GSI_SAME_STMT);
4315 : : }
4316 : 4129 : if (fd->have_pointer_condtemp)
4317 : 28 : expand_omp_build_assign (&gsi, condtemp, memv, false);
4318 : 4129 : if (fd->have_reductemp)
4319 : : {
4320 : 149 : gimple *g = gsi_stmt (gsi);
4321 : 149 : gsi_remove (&gsi, true);
4322 : 149 : release_ssa_name (gimple_assign_lhs (g));
4323 : :
4324 : 149 : entry_bb = region->entry;
4325 : 149 : gsi = gsi_last_nondebug_bb (entry_bb);
4326 : :
4327 : 149 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
4328 : : }
4329 : 4129 : gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
4330 : :
4331 : : /* Remove the GIMPLE_OMP_FOR statement. */
4332 : 4129 : gsi_remove (&gsi, true);
4333 : :
4334 : 4129 : if (gsi_end_p (gsif))
4335 : 1208 : gsif = gsi_after_labels (gsi_bb (gsif));
4336 : 4129 : gsi_next (&gsif);
4337 : :
4338 : : /* Iteration setup for sequential loop goes in L0_BB. */
4339 : 4129 : tree startvar = fd->loop.v;
4340 : 4129 : tree endvar = NULL_TREE;
4341 : :
4342 : 4129 : if (gimple_omp_for_combined_p (fd->for_stmt))
4343 : : {
4344 : 1389 : gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_FOR
4345 : : && gimple_omp_for_kind (inner_stmt)
4346 : : == GF_OMP_FOR_KIND_SIMD);
4347 : 1389 : tree innerc = omp_find_clause (gimple_omp_for_clauses (inner_stmt),
4348 : : OMP_CLAUSE__LOOPTEMP_);
4349 : 1389 : gcc_assert (innerc);
4350 : 1389 : startvar = OMP_CLAUSE_DECL (innerc);
4351 : 1389 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4352 : : OMP_CLAUSE__LOOPTEMP_);
4353 : 1389 : gcc_assert (innerc);
4354 : 1389 : endvar = OMP_CLAUSE_DECL (innerc);
4355 : : }
4356 : :
4357 : 4129 : gsi = gsi_start_bb (l0_bb);
4358 : 4129 : t = istart0;
4359 : 4129 : if (fd->ordered && fd->collapse == 1)
4360 : 184 : t = fold_build2 (MULT_EXPR, fd->iter_type, t,
4361 : : fold_convert (fd->iter_type, fd->loop.step));
4362 : 3945 : else if (bias)
4363 : 0 : t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
4364 : 4129 : if (fd->ordered && fd->collapse == 1)
4365 : : {
4366 : 184 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4367 : 8 : t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
4368 : : fd->loop.n1, fold_convert (sizetype, t));
4369 : : else
4370 : : {
4371 : 176 : t = fold_convert (TREE_TYPE (startvar), t);
4372 : 176 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
4373 : : fd->loop.n1, t);
4374 : : }
4375 : : }
4376 : : else
4377 : : {
4378 : 3945 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4379 : 240 : t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
4380 : 3945 : t = fold_convert (TREE_TYPE (startvar), t);
4381 : : }
4382 : 4129 : t = force_gimple_operand_gsi (&gsi, t,
4383 : 4129 : DECL_P (startvar)
4384 : 4129 : && TREE_ADDRESSABLE (startvar),
4385 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
4386 : 4129 : assign_stmt = gimple_build_assign (startvar, t);
4387 : 4129 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4388 : 4129 : if (cond_var)
4389 : : {
4390 : 46 : tree itype = TREE_TYPE (cond_var);
4391 : : /* For lastprivate(conditional:) itervar, we need some iteration
4392 : : counter that starts at unsigned non-zero and increases.
4393 : : Prefer as few IVs as possible, so if we can use startvar
4394 : : itself, use that, or startvar + constant (those would be
4395 : : incremented with step), and as last resort use the s0 + 1
4396 : : incremented by 1. */
4397 : 46 : if ((fd->ordered && fd->collapse == 1)
4398 : 46 : || bias
4399 : 46 : || POINTER_TYPE_P (type)
4400 : 46 : || TREE_CODE (fd->loop.n1) != INTEGER_CST
4401 : 40 : || fd->loop.cond_code != LT_EXPR)
4402 : 6 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, istart0),
4403 : : build_int_cst (itype, 1));
4404 : 40 : else if (tree_int_cst_sgn (fd->loop.n1) == 1)
4405 : 9 : t = fold_convert (itype, t);
4406 : : else
4407 : : {
4408 : 31 : tree c = fold_convert (itype, fd->loop.n1);
4409 : 31 : c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
4410 : 31 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
4411 : : }
4412 : 46 : t = force_gimple_operand_gsi (&gsi, t, false,
4413 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
4414 : 46 : assign_stmt = gimple_build_assign (cond_var, t);
4415 : 46 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4416 : : }
4417 : :
4418 : 4129 : t = iend0;
4419 : 4129 : if (fd->ordered && fd->collapse == 1)
4420 : 184 : t = fold_build2 (MULT_EXPR, fd->iter_type, t,
4421 : : fold_convert (fd->iter_type, fd->loop.step));
4422 : 3945 : else if (bias)
4423 : 0 : t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
4424 : 4129 : if (fd->ordered && fd->collapse == 1)
4425 : : {
4426 : 184 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4427 : 8 : t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
4428 : : fd->loop.n1, fold_convert (sizetype, t));
4429 : : else
4430 : : {
4431 : 176 : t = fold_convert (TREE_TYPE (startvar), t);
4432 : 176 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
4433 : : fd->loop.n1, t);
4434 : : }
4435 : : }
4436 : : else
4437 : : {
4438 : 3945 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4439 : 240 : t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
4440 : 3945 : t = fold_convert (TREE_TYPE (startvar), t);
4441 : : }
4442 : 4129 : iend = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4443 : : false, GSI_CONTINUE_LINKING);
4444 : 4129 : if (endvar)
4445 : : {
4446 : 1389 : assign_stmt = gimple_build_assign (endvar, iend);
4447 : 1389 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4448 : 1389 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (iend)))
4449 : 857 : assign_stmt = gimple_build_assign (fd->loop.v, iend);
4450 : : else
4451 : 532 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, iend);
4452 : 1389 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4453 : : }
4454 : : /* Handle linear clause adjustments. */
4455 : 4129 : tree itercnt = NULL_TREE;
4456 : 4129 : if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
4457 : 27005 : for (tree c = gimple_omp_for_clauses (fd->for_stmt);
4458 : 27005 : c; c = OMP_CLAUSE_CHAIN (c))
4459 : 22876 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
4460 : 22876 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
4461 : : {
4462 : 72 : tree d = OMP_CLAUSE_DECL (c);
4463 : 72 : tree t = d, a, dest;
4464 : 72 : if (omp_privatize_by_reference (t))
4465 : 2 : t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
4466 : 72 : tree type = TREE_TYPE (t);
4467 : 72 : if (POINTER_TYPE_P (type))
4468 : 0 : type = sizetype;
4469 : 72 : dest = unshare_expr (t);
4470 : 72 : tree v = create_tmp_var (TREE_TYPE (t), NULL);
4471 : 72 : expand_omp_build_assign (&gsif, v, t);
4472 : 72 : if (itercnt == NULL_TREE)
4473 : : {
4474 : 72 : itercnt = startvar;
4475 : 72 : tree n1 = fd->loop.n1;
4476 : 72 : if (POINTER_TYPE_P (TREE_TYPE (itercnt)))
4477 : : {
4478 : 0 : itercnt
4479 : 0 : = fold_convert (signed_type_for (TREE_TYPE (itercnt)),
4480 : : itercnt);
4481 : 0 : n1 = fold_convert (TREE_TYPE (itercnt), n1);
4482 : : }
4483 : 72 : itercnt = fold_build2 (MINUS_EXPR, TREE_TYPE (itercnt),
4484 : : itercnt, n1);
4485 : 72 : itercnt = fold_build2 (EXACT_DIV_EXPR, TREE_TYPE (itercnt),
4486 : : itercnt, fd->loop.step);
4487 : 72 : itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
4488 : : NULL_TREE, false,
4489 : : GSI_CONTINUE_LINKING);
4490 : : }
4491 : 72 : a = fold_build2 (MULT_EXPR, type,
4492 : : fold_convert (type, itercnt),
4493 : : fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
4494 : 72 : t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
4495 : : : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
4496 : 72 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4497 : : false, GSI_CONTINUE_LINKING);
4498 : 72 : expand_omp_build_assign (&gsi, dest, t, true);
4499 : : }
4500 : 4129 : if (fd->collapse > 1)
4501 : 1481 : expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
4502 : :
4503 : 4129 : if (fd->ordered)
4504 : : {
4505 : : /* Until now, counts array contained number of iterations or
4506 : : variable containing it for ith loop. From now on, we usually need
4507 : : those counts only for collapsed loops, and only for the 2nd
4508 : : till the last collapsed one. Move those one element earlier,
4509 : : we'll use counts[fd->collapse - 1] for the first source/sink
4510 : : iteration counter and so on and counts[fd->ordered]
4511 : : as the array holding the current counter values for
4512 : : depend(source). For doacross(sink:omp_cur_iteration - 1) we need
4513 : : the counts from fd->collapse to fd->ordered - 1; make a copy of
4514 : : those to counts[fd->ordered + 2] and onwards.
4515 : : counts[fd->ordered + 1] can be a flag whether it is the first
4516 : : iteration with a new collapsed counter (used only if
4517 : : fd->ordered > fd->collapse). */
4518 : 331 : if (fd->ordered > fd->collapse)
4519 : 171 : memcpy (counts + fd->ordered + 2, counts + fd->collapse,
4520 : 171 : (fd->ordered - fd->collapse) * sizeof (counts[0]));
4521 : 331 : if (fd->collapse > 1)
4522 : 147 : memmove (counts, counts + 1, (fd->collapse - 1) * sizeof (counts[0]));
4523 : 331 : if (broken_loop)
4524 : : {
4525 : : int i;
4526 : 41 : for (i = fd->collapse; i < fd->ordered; i++)
4527 : : {
4528 : 33 : tree type = TREE_TYPE (fd->loops[i].v);
4529 : 33 : tree this_cond
4530 : 33 : = fold_build2 (fd->loops[i].cond_code, boolean_type_node,
4531 : : fold_convert (type, fd->loops[i].n1),
4532 : : fold_convert (type, fd->loops[i].n2));
4533 : 33 : if (!integer_onep (this_cond))
4534 : : break;
4535 : : }
4536 : 37 : if (i < fd->ordered)
4537 : : {
4538 : 29 : if (entry_bb->loop_father != l0_bb->loop_father)
4539 : : {
4540 : 4 : remove_bb_from_loops (l0_bb);
4541 : 4 : add_bb_to_loop (l0_bb, entry_bb->loop_father);
4542 : 4 : gcc_assert (single_succ (l0_bb) == l1_bb);
4543 : : }
4544 : 29 : cont_bb
4545 : 29 : = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
4546 : 29 : add_bb_to_loop (cont_bb, l0_bb->loop_father);
4547 : 29 : gimple_stmt_iterator gsi = gsi_after_labels (cont_bb);
4548 : 29 : gimple *g = gimple_build_omp_continue (fd->loop.v, fd->loop.v);
4549 : 29 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4550 : 29 : make_edge (cont_bb, l3_bb, EDGE_FALLTHRU);
4551 : 29 : make_edge (cont_bb, l1_bb, 0);
4552 : 29 : l2_bb = create_empty_bb (cont_bb);
4553 : 29 : broken_loop = false;
4554 : : }
4555 : : }
4556 : 331 : expand_omp_ordered_source_sink (region, fd, counts, cont_bb);
4557 : 331 : cont_bb = expand_omp_for_ordered_loops (fd, counts, cont_bb, l1_bb,
4558 : : l0_bb, ordered_lastprivate);
4559 : 331 : if (counts[fd->collapse - 1])
4560 : : {
4561 : 37 : gcc_assert (fd->collapse == 1);
4562 : 37 : gsi = gsi_last_bb (l0_bb);
4563 : 37 : expand_omp_build_assign (&gsi, counts[fd->collapse - 1],
4564 : : istart0, true);
4565 : 37 : if (cont_bb)
4566 : : {
4567 : 33 : gsi = gsi_last_bb (cont_bb);
4568 : 33 : t = fold_build2 (PLUS_EXPR, fd->iter_type,
4569 : : counts[fd->collapse - 1],
4570 : : build_int_cst (fd->iter_type, 1));
4571 : 33 : expand_omp_build_assign (&gsi, counts[fd->collapse - 1], t);
4572 : 66 : tree aref = build4 (ARRAY_REF, fd->iter_type,
4573 : 33 : counts[fd->ordered], size_zero_node,
4574 : : NULL_TREE, NULL_TREE);
4575 : 33 : expand_omp_build_assign (&gsi, aref, counts[fd->collapse - 1]);
4576 : : }
4577 : 37 : t = counts[fd->collapse - 1];
4578 : : }
4579 : 294 : else if (fd->collapse > 1)
4580 : 147 : t = fd->loop.v;
4581 : : else
4582 : : {
4583 : 147 : t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
4584 : : fd->loops[0].v, fd->loops[0].n1);
4585 : 147 : t = fold_convert (fd->iter_type, t);
4586 : : }
4587 : 331 : gsi = gsi_last_bb (l0_bb);
4588 : 331 : tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
4589 : : size_zero_node, NULL_TREE, NULL_TREE);
4590 : 331 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4591 : : false, GSI_CONTINUE_LINKING);
4592 : 331 : expand_omp_build_assign (&gsi, aref, t, true);
4593 : : }
4594 : :
4595 : 4129 : if (!broken_loop)
4596 : : {
4597 : : /* Code to control the increment and predicate for the sequential
4598 : : loop goes in the CONT_BB. */
4599 : 3751 : gsi = gsi_last_nondebug_bb (cont_bb);
4600 : 3751 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
4601 : 3751 : gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
4602 : 3751 : vmain = gimple_omp_continue_control_use (cont_stmt);
4603 : 3751 : vback = gimple_omp_continue_control_def (cont_stmt);
4604 : :
4605 : 3751 : if (cond_var)
4606 : : {
4607 : 46 : tree itype = TREE_TYPE (cond_var);
4608 : 46 : tree t2;
4609 : 46 : if ((fd->ordered && fd->collapse == 1)
4610 : 46 : || bias
4611 : 46 : || POINTER_TYPE_P (type)
4612 : 46 : || TREE_CODE (fd->loop.n1) != INTEGER_CST
4613 : 40 : || fd->loop.cond_code != LT_EXPR)
4614 : 6 : t2 = build_int_cst (itype, 1);
4615 : : else
4616 : 40 : t2 = fold_convert (itype, fd->loop.step);
4617 : 46 : t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
4618 : 46 : t2 = force_gimple_operand_gsi (&gsi, t2, false,
4619 : : NULL_TREE, true, GSI_SAME_STMT);
4620 : 46 : assign_stmt = gimple_build_assign (cond_var, t2);
4621 : 46 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
4622 : : }
4623 : :
4624 : 3751 : if (!gimple_omp_for_combined_p (fd->for_stmt))
4625 : : {
4626 : 2362 : if (POINTER_TYPE_P (type))
4627 : 160 : t = fold_build_pointer_plus (vmain, fd->loop.step);
4628 : : else
4629 : 2202 : t = fold_build2 (PLUS_EXPR, type, vmain, fd->loop.step);
4630 : 2362 : t = force_gimple_operand_gsi (&gsi, t,
4631 : 2362 : DECL_P (vback)
4632 : 2362 : && TREE_ADDRESSABLE (vback),
4633 : : NULL_TREE, true, GSI_SAME_STMT);
4634 : 2362 : assign_stmt = gimple_build_assign (vback, t);
4635 : 2362 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
4636 : :
4637 : 2362 : if (fd->ordered && counts[fd->collapse - 1] == NULL_TREE)
4638 : : {
4639 : 290 : tree tem;
4640 : 290 : if (fd->collapse > 1)
4641 : 143 : tem = fd->loop.v;
4642 : : else
4643 : : {
4644 : 147 : tem = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
4645 : : fd->loops[0].v, fd->loops[0].n1);
4646 : 147 : tem = fold_convert (fd->iter_type, tem);
4647 : : }
4648 : 580 : tree aref = build4 (ARRAY_REF, fd->iter_type,
4649 : 290 : counts[fd->ordered], size_zero_node,
4650 : : NULL_TREE, NULL_TREE);
4651 : 290 : tem = force_gimple_operand_gsi (&gsi, tem, true, NULL_TREE,
4652 : : true, GSI_SAME_STMT);
4653 : 290 : expand_omp_build_assign (&gsi, aref, tem);
4654 : : }
4655 : :
4656 : 2362 : t = build2 (fd->loop.cond_code, boolean_type_node,
4657 : 2362 : DECL_P (vback) && TREE_ADDRESSABLE (vback) ? t : vback,
4658 : : iend);
4659 : 2362 : gcond *cond_stmt = gimple_build_cond_empty (t);
4660 : 2362 : gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
4661 : : }
4662 : :
4663 : : /* Remove GIMPLE_OMP_CONTINUE. */
4664 : 3751 : gsi_remove (&gsi, true);
4665 : :
4666 : 3751 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
4667 : 681 : collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, l1_bb);
4668 : :
4669 : : /* Emit code to get the next parallel iteration in L2_BB. */
4670 : 3751 : gsi = gsi_start_bb (l2_bb);
4671 : :
4672 : 3751 : t = build_call_expr (builtin_decl_explicit (next_fn), 2,
4673 : : build_fold_addr_expr (istart0),
4674 : : build_fold_addr_expr (iend0));
4675 : 3751 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4676 : : false, GSI_CONTINUE_LINKING);
4677 : 3751 : if (TREE_TYPE (t) != boolean_type_node)
4678 : 0 : t = fold_build2 (NE_EXPR, boolean_type_node,
4679 : : t, build_int_cst (TREE_TYPE (t), 0));
4680 : 3751 : gcond *cond_stmt = gimple_build_cond_empty (t);
4681 : 3751 : gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
4682 : : }
4683 : :
4684 : : /* Add the loop cleanup function. */
4685 : 4129 : gsi = gsi_last_nondebug_bb (exit_bb);
4686 : 4129 : if (gimple_omp_return_nowait_p (gsi_stmt (gsi)))
4687 : 3055 : t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
4688 : 1074 : else if (gimple_omp_return_lhs (gsi_stmt (gsi)))
4689 : 2 : t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
4690 : : else
4691 : 1072 : t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
4692 : 4129 : gcall *call_stmt = gimple_build_call (t, 0);
4693 : 4129 : if (fd->ordered)
4694 : : {
4695 : 331 : tree arr = counts[fd->ordered];
4696 : 331 : tree clobber = build_clobber (TREE_TYPE (arr));
4697 : 331 : gsi_insert_after (&gsi, gimple_build_assign (arr, clobber),
4698 : : GSI_SAME_STMT);
4699 : : }
4700 : 4129 : if (gimple_omp_return_lhs (gsi_stmt (gsi)))
4701 : : {
4702 : 2 : gimple_call_set_lhs (call_stmt, gimple_omp_return_lhs (gsi_stmt (gsi)));
4703 : 2 : if (fd->have_reductemp)
4704 : : {
4705 : 0 : gimple *g = gimple_build_assign (reductions, NOP_EXPR,
4706 : : gimple_call_lhs (call_stmt));
4707 : 0 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
4708 : : }
4709 : : }
4710 : 4129 : gsi_insert_after (&gsi, call_stmt, GSI_SAME_STMT);
4711 : 4129 : gsi_remove (&gsi, true);
4712 : :
4713 : : /* Connect the new blocks. */
4714 : 4129 : find_edge (entry_bb, l0_bb)->flags = EDGE_TRUE_VALUE;
4715 : 4129 : find_edge (entry_bb, l3_bb)->flags = EDGE_FALSE_VALUE;
4716 : :
4717 : 4129 : if (!broken_loop)
4718 : : {
4719 : 3751 : gimple_seq phis;
4720 : :
4721 : 3751 : e = find_edge (cont_bb, l3_bb);
4722 : 3751 : ne = make_edge (l2_bb, l3_bb, EDGE_FALSE_VALUE);
4723 : :
4724 : 3751 : phis = phi_nodes (l3_bb);
4725 : 3773 : for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
4726 : : {
4727 : 11 : gimple *phi = gsi_stmt (gsi);
4728 : 11 : SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, ne),
4729 : : PHI_ARG_DEF_FROM_EDGE (phi, e));
4730 : : }
4731 : 3751 : remove_edge (e);
4732 : :
4733 : 3751 : make_edge (cont_bb, l2_bb, EDGE_FALSE_VALUE);
4734 : 3751 : e = find_edge (cont_bb, l1_bb);
4735 : 3751 : if (e == NULL)
4736 : : {
4737 : 13 : e = BRANCH_EDGE (cont_bb);
4738 : 13 : gcc_assert (single_succ (e->dest) == l1_bb);
4739 : : }
4740 : 3751 : if (gimple_omp_for_combined_p (fd->for_stmt))
4741 : : {
4742 : 1389 : remove_edge (e);
4743 : 1389 : e = NULL;
4744 : : }
4745 : 2362 : else if (fd->collapse > 1)
4746 : : {
4747 : 681 : remove_edge (e);
4748 : 681 : e = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
4749 : : }
4750 : : else
4751 : 1681 : e->flags = EDGE_TRUE_VALUE;
4752 : 3751 : if (e)
4753 : : {
4754 : 2362 : e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
4755 : 2362 : find_edge (cont_bb, l2_bb)->probability = e->probability.invert ();
4756 : : }
4757 : : else
4758 : : {
4759 : 1389 : e = find_edge (cont_bb, l2_bb);
4760 : 1389 : e->flags = EDGE_FALLTHRU;
4761 : : }
4762 : 3751 : make_edge (l2_bb, l0_bb, EDGE_TRUE_VALUE);
4763 : :
4764 : 3751 : if (gimple_in_ssa_p (cfun))
4765 : : {
4766 : : /* Add phis to the outer loop that connect to the phis in the inner,
4767 : : original loop, and move the loop entry value of the inner phi to
4768 : : the loop entry value of the outer phi. */
4769 : 13 : gphi_iterator psi;
4770 : 24 : for (psi = gsi_start_phis (l3_bb); !gsi_end_p (psi); gsi_next (&psi))
4771 : : {
4772 : 11 : location_t locus;
4773 : 11 : gphi *nphi;
4774 : 11 : gphi *exit_phi = psi.phi ();
4775 : :
4776 : 22 : if (virtual_operand_p (gimple_phi_result (exit_phi)))
4777 : 6 : continue;
4778 : :
4779 : 5 : edge l2_to_l3 = find_edge (l2_bb, l3_bb);
4780 : 5 : tree exit_res = PHI_ARG_DEF_FROM_EDGE (exit_phi, l2_to_l3);
4781 : :
4782 : 5 : basic_block latch = BRANCH_EDGE (cont_bb)->dest;
4783 : 5 : edge latch_to_l1 = find_edge (latch, l1_bb);
4784 : 5 : gphi *inner_phi
4785 : 5 : = find_phi_with_arg_on_edge (exit_res, latch_to_l1);
4786 : :
4787 : 5 : tree t = gimple_phi_result (exit_phi);
4788 : 5 : tree new_res = copy_ssa_name (t, NULL);
4789 : 5 : nphi = create_phi_node (new_res, l0_bb);
4790 : :
4791 : 5 : edge l0_to_l1 = find_edge (l0_bb, l1_bb);
4792 : 5 : t = PHI_ARG_DEF_FROM_EDGE (inner_phi, l0_to_l1);
4793 : 5 : locus = gimple_phi_arg_location_from_edge (inner_phi, l0_to_l1);
4794 : 5 : edge entry_to_l0 = find_edge (entry_bb, l0_bb);
4795 : 5 : add_phi_arg (nphi, t, entry_to_l0, locus);
4796 : :
4797 : 5 : edge l2_to_l0 = find_edge (l2_bb, l0_bb);
4798 : 5 : add_phi_arg (nphi, exit_res, l2_to_l0, UNKNOWN_LOCATION);
4799 : :
4800 : 5 : add_phi_arg (inner_phi, new_res, l0_to_l1, UNKNOWN_LOCATION);
4801 : : }
4802 : : }
4803 : :
4804 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l2_bb,
4805 : : recompute_dominator (CDI_DOMINATORS, l2_bb));
4806 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l3_bb,
4807 : : recompute_dominator (CDI_DOMINATORS, l3_bb));
4808 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l0_bb,
4809 : : recompute_dominator (CDI_DOMINATORS, l0_bb));
4810 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l1_bb,
4811 : : recompute_dominator (CDI_DOMINATORS, l1_bb));
4812 : :
4813 : : /* We enter expand_omp_for_generic with a loop. This original loop may
4814 : : have its own loop struct, or it may be part of an outer loop struct
4815 : : (which may be the fake loop). */
4816 : 3751 : class loop *outer_loop = entry_bb->loop_father;
4817 : 3751 : bool orig_loop_has_loop_struct = l1_bb->loop_father != outer_loop;
4818 : :
4819 : 3751 : add_bb_to_loop (l2_bb, outer_loop);
4820 : :
4821 : : /* We've added a new loop around the original loop. Allocate the
4822 : : corresponding loop struct. */
4823 : 3751 : class loop *new_loop = alloc_loop ();
4824 : 3751 : new_loop->header = l0_bb;
4825 : 3751 : new_loop->latch = l2_bb;
4826 : 3751 : add_loop (new_loop, outer_loop);
4827 : :
4828 : : /* Allocate a loop structure for the original loop unless we already
4829 : : had one. */
4830 : 3751 : if (!orig_loop_has_loop_struct
4831 : 3751 : && !gimple_omp_for_combined_p (fd->for_stmt))
4832 : : {
4833 : 2345 : class loop *orig_loop = alloc_loop ();
4834 : 2345 : orig_loop->header = l1_bb;
4835 : : /* The loop may have multiple latches. */
4836 : 2345 : add_loop (orig_loop, new_loop);
4837 : : }
4838 : : }
4839 : 4129 : }
4840 : :
4841 : : /* Helper function for expand_omp_for_static_nochunk. If PTR is NULL,
4842 : : compute needed allocation size. If !ALLOC of team allocations,
4843 : : if ALLOC of thread allocation. SZ is the initial needed size for
4844 : : other purposes, ALLOC_ALIGN guaranteed alignment of allocation in bytes,
4845 : : CNT number of elements of each array, for !ALLOC this is
4846 : : omp_get_num_threads (), for ALLOC number of iterations handled by the
4847 : : current thread. If PTR is non-NULL, it is the start of the allocation
4848 : : and this routine shall assign to OMP_CLAUSE_DECL (c) of those _scantemp_
4849 : : clauses pointers to the corresponding arrays. */
4850 : :
4851 : : static tree
4852 : 692 : expand_omp_scantemp_alloc (tree clauses, tree ptr, unsigned HOST_WIDE_INT sz,
4853 : : unsigned HOST_WIDE_INT alloc_align, tree cnt,
4854 : : gimple_stmt_iterator *gsi, bool alloc)
4855 : : {
4856 : 692 : tree eltsz = NULL_TREE;
4857 : 692 : unsigned HOST_WIDE_INT preval = 0;
4858 : 692 : if (ptr && sz)
4859 : 5 : ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
4860 : : ptr, size_int (sz));
4861 : 5720 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
4862 : 5028 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
4863 : 3024 : && !OMP_CLAUSE__SCANTEMP__CONTROL (c)
4864 : 6668 : && (!OMP_CLAUSE__SCANTEMP__ALLOC (c)) != alloc)
4865 : : {
4866 : 820 : tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
4867 : 820 : unsigned HOST_WIDE_INT al = TYPE_ALIGN_UNIT (pointee_type);
4868 : 820 : if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
4869 : : {
4870 : 820 : unsigned HOST_WIDE_INT szl
4871 : 820 : = tree_to_uhwi (TYPE_SIZE_UNIT (pointee_type));
4872 : 820 : szl = least_bit_hwi (szl);
4873 : 820 : if (szl)
4874 : 820 : al = MIN (al, szl);
4875 : : }
4876 : 820 : if (ptr == NULL_TREE)
4877 : : {
4878 : 410 : if (eltsz == NULL_TREE)
4879 : 346 : eltsz = TYPE_SIZE_UNIT (pointee_type);
4880 : : else
4881 : 64 : eltsz = size_binop (PLUS_EXPR, eltsz,
4882 : : TYPE_SIZE_UNIT (pointee_type));
4883 : : }
4884 : 820 : if (preval == 0 && al <= alloc_align)
4885 : : {
4886 : 692 : unsigned HOST_WIDE_INT diff = ROUND_UP (sz, al) - sz;
4887 : 692 : sz += diff;
4888 : 692 : if (diff && ptr)
4889 : 0 : ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
4890 : : ptr, size_int (diff));
4891 : : }
4892 : 128 : else if (al > preval)
4893 : : {
4894 : 32 : if (ptr)
4895 : : {
4896 : 16 : ptr = fold_convert (pointer_sized_int_node, ptr);
4897 : 16 : ptr = fold_build2 (PLUS_EXPR, pointer_sized_int_node, ptr,
4898 : : build_int_cst (pointer_sized_int_node,
4899 : : al - 1));
4900 : 16 : ptr = fold_build2 (BIT_AND_EXPR, pointer_sized_int_node, ptr,
4901 : : build_int_cst (pointer_sized_int_node,
4902 : : -(HOST_WIDE_INT) al));
4903 : 16 : ptr = fold_convert (ptr_type_node, ptr);
4904 : : }
4905 : : else
4906 : 16 : sz += al - 1;
4907 : : }
4908 : 820 : if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
4909 : : preval = al;
4910 : : else
4911 : 0 : preval = 1;
4912 : 820 : if (ptr)
4913 : : {
4914 : 410 : expand_omp_build_assign (gsi, OMP_CLAUSE_DECL (c), ptr, false);
4915 : 410 : ptr = OMP_CLAUSE_DECL (c);
4916 : 410 : ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
4917 : : size_binop (MULT_EXPR, cnt,
4918 : : TYPE_SIZE_UNIT (pointee_type)));
4919 : : }
4920 : : }
4921 : :
4922 : 692 : if (ptr == NULL_TREE)
4923 : : {
4924 : 346 : eltsz = size_binop (MULT_EXPR, eltsz, cnt);
4925 : 346 : if (sz)
4926 : 13 : eltsz = size_binop (PLUS_EXPR, eltsz, size_int (sz));
4927 : 346 : return eltsz;
4928 : : }
4929 : : else
4930 : : return ptr;
4931 : : }
4932 : :
4933 : : /* Return the last _looptemp_ clause if one has been created for
4934 : : lastprivate on distribute parallel for{, simd} or taskloop.
4935 : : FD is the loop data and INNERC should be the second _looptemp_
4936 : : clause (the one holding the end of the range).
4937 : : This is followed by collapse - 1 _looptemp_ clauses for the
4938 : : counts[1] and up, and for triangular loops followed by 4
4939 : : further _looptemp_ clauses (one for counts[0], one first_inner_iterations,
4940 : : one factor and one adjn1). After this there is optionally one
4941 : : _looptemp_ clause that this function returns. */
4942 : :
4943 : : static tree
4944 : 1628 : find_lastprivate_looptemp (struct omp_for_data *fd, tree innerc)
4945 : : {
4946 : 1628 : gcc_assert (innerc);
4947 : 1628 : int count = fd->collapse - 1;
4948 : 1628 : if (fd->non_rect
4949 : 24 : && fd->last_nonrect == fd->first_nonrect + 1
4950 : 1640 : && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
4951 : 12 : count += 4;
4952 : 4799 : for (int i = 0; i < count; i++)
4953 : : {
4954 : 3171 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4955 : : OMP_CLAUSE__LOOPTEMP_);
4956 : 3171 : gcc_assert (innerc);
4957 : : }
4958 : 1628 : return omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4959 : 1628 : OMP_CLAUSE__LOOPTEMP_);
4960 : : }
4961 : :
4962 : : /* A subroutine of expand_omp_for. Generate code for a parallel
4963 : : loop with static schedule and no specified chunk size. Given
4964 : : parameters:
4965 : :
4966 : : for (V = N1; V cond N2; V += STEP) BODY;
4967 : :
4968 : : where COND is "<" or ">", we generate pseudocode
4969 : :
4970 : : if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
4971 : : if (cond is <)
4972 : : adj = STEP - 1;
4973 : : else
4974 : : adj = STEP + 1;
4975 : : if ((__typeof (V)) -1 > 0 && cond is >)
4976 : : n = -(adj + N2 - N1) / -STEP;
4977 : : else
4978 : : n = (adj + N2 - N1) / STEP;
4979 : : q = n / nthreads;
4980 : : tt = n % nthreads;
4981 : : if (threadid < tt) goto L3; else goto L4;
4982 : : L3:
4983 : : tt = 0;
4984 : : q = q + 1;
4985 : : L4:
4986 : : s0 = q * threadid + tt;
4987 : : e0 = s0 + q;
4988 : : V = s0 * STEP + N1;
4989 : : if (s0 >= e0) goto L2; else goto L0;
4990 : : L0:
4991 : : e = e0 * STEP + N1;
4992 : : L1:
4993 : : BODY;
4994 : : V += STEP;
4995 : : if (V cond e) goto L1;
4996 : : L2:
4997 : : */
4998 : :
4999 : : static void
5000 : 13976 : expand_omp_for_static_nochunk (struct omp_region *region,
5001 : : struct omp_for_data *fd,
5002 : : gimple *inner_stmt)
5003 : : {
5004 : 13976 : tree n, q, s0, e0, e, t, tt, nthreads = NULL_TREE, threadid;
5005 : 13976 : tree type, itype, vmain, vback;
5006 : 13976 : basic_block entry_bb, second_bb, third_bb, exit_bb, seq_start_bb;
5007 : 13976 : basic_block body_bb, cont_bb, collapse_bb = NULL;
5008 : 13976 : basic_block fin_bb, fourth_bb = NULL, fifth_bb = NULL, sixth_bb = NULL;
5009 : 13976 : basic_block exit1_bb = NULL, exit2_bb = NULL, exit3_bb = NULL;
5010 : 13976 : gimple_stmt_iterator gsi, gsip;
5011 : 13976 : edge ep;
5012 : 13976 : bool broken_loop = region->cont == NULL;
5013 : 13976 : tree *counts = NULL;
5014 : 13976 : tree n1, n2, step;
5015 : 13976 : tree reductions = NULL_TREE;
5016 : 13976 : tree cond_var = NULL_TREE, condtemp = NULL_TREE;
5017 : :
5018 : 13976 : itype = type = TREE_TYPE (fd->loop.v);
5019 : 13976 : if (POINTER_TYPE_P (type))
5020 : 606 : itype = signed_type_for (type);
5021 : :
5022 : 13976 : entry_bb = region->entry;
5023 : 13976 : cont_bb = region->cont;
5024 : 13976 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
5025 : 13976 : fin_bb = BRANCH_EDGE (entry_bb)->dest;
5026 : 13976 : gcc_assert (broken_loop
5027 : : || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
5028 : 13976 : seq_start_bb = split_edge (FALLTHRU_EDGE (entry_bb));
5029 : 13976 : body_bb = single_succ (seq_start_bb);
5030 : 13976 : if (!broken_loop)
5031 : : {
5032 : 13480 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
5033 : : || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
5034 : 13480 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
5035 : : }
5036 : 13976 : exit_bb = region->exit;
5037 : :
5038 : : /* Iteration space partitioning goes in ENTRY_BB. */
5039 : 13976 : gsi = gsi_last_nondebug_bb (entry_bb);
5040 : 13976 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5041 : 13976 : gsip = gsi;
5042 : 13976 : gsi_prev (&gsip);
5043 : :
5044 : 13976 : if (fd->collapse > 1)
5045 : : {
5046 : 3888 : int first_zero_iter = -1, dummy = -1;
5047 : 3888 : basic_block l2_dom_bb = NULL, dummy_bb = NULL;
5048 : :
5049 : 3888 : counts = XALLOCAVEC (tree, fd->collapse);
5050 : 3888 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
5051 : : fin_bb, first_zero_iter,
5052 : : dummy_bb, dummy, l2_dom_bb);
5053 : 3888 : t = NULL_TREE;
5054 : : }
5055 : 10088 : else if (gimple_omp_for_combined_into_p (fd->for_stmt))
5056 : 2036 : t = integer_one_node;
5057 : : else
5058 : 8052 : t = fold_binary (fd->loop.cond_code, boolean_type_node,
5059 : : fold_convert (type, fd->loop.n1),
5060 : : fold_convert (type, fd->loop.n2));
5061 : 13976 : if (fd->collapse == 1
5062 : 10088 : && TYPE_UNSIGNED (type)
5063 : 15981 : && (t == NULL_TREE || !integer_onep (t)))
5064 : : {
5065 : 518 : n1 = fold_convert (type, unshare_expr (fd->loop.n1));
5066 : 518 : n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
5067 : : true, GSI_SAME_STMT);
5068 : 518 : n2 = fold_convert (type, unshare_expr (fd->loop.n2));
5069 : 518 : n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
5070 : : true, GSI_SAME_STMT);
5071 : 518 : gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
5072 : : n1, n2);
5073 : 518 : ep = split_block (entry_bb, cond_stmt);
5074 : 518 : ep->flags = EDGE_TRUE_VALUE;
5075 : 518 : entry_bb = ep->dest;
5076 : 518 : ep->probability = profile_probability::very_likely ();
5077 : 518 : ep = make_edge (ep->src, fin_bb, EDGE_FALSE_VALUE);
5078 : 518 : ep->probability = profile_probability::very_unlikely ();
5079 : 518 : if (gimple_in_ssa_p (cfun))
5080 : : {
5081 : 20 : int dest_idx = find_edge (entry_bb, fin_bb)->dest_idx;
5082 : 20 : for (gphi_iterator gpi = gsi_start_phis (fin_bb);
5083 : 42 : !gsi_end_p (gpi); gsi_next (&gpi))
5084 : : {
5085 : 22 : gphi *phi = gpi.phi ();
5086 : 22 : add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
5087 : : ep, UNKNOWN_LOCATION);
5088 : : }
5089 : : }
5090 : 1036 : gsi = gsi_last_bb (entry_bb);
5091 : : }
5092 : :
5093 : 13976 : if (fd->lastprivate_conditional)
5094 : : {
5095 : 108 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5096 : 108 : tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
5097 : 108 : if (fd->have_pointer_condtemp)
5098 : 38 : condtemp = OMP_CLAUSE_DECL (c);
5099 : 108 : c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
5100 : 108 : cond_var = OMP_CLAUSE_DECL (c);
5101 : : }
5102 : 13976 : if (fd->have_reductemp
5103 : : /* For scan, we don't want to reinitialize condtemp before the
5104 : : second loop. */
5105 : 13923 : || (fd->have_pointer_condtemp && !fd->have_scantemp)
5106 : 13905 : || fd->have_nonctrl_scantemp)
5107 : : {
5108 : 244 : tree t1 = build_int_cst (long_integer_type_node, 0);
5109 : 244 : tree t2 = build_int_cst (long_integer_type_node, 1);
5110 : 244 : tree t3 = build_int_cstu (long_integer_type_node,
5111 : : (HOST_WIDE_INT_1U << 31) + 1);
5112 : 244 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5113 : 244 : gimple_stmt_iterator gsi2 = gsi_none ();
5114 : 244 : gimple *g = NULL;
5115 : 244 : tree mem = null_pointer_node, memv = NULL_TREE;
5116 : 244 : unsigned HOST_WIDE_INT condtemp_sz = 0;
5117 : 244 : unsigned HOST_WIDE_INT alloc_align = 0;
5118 : 244 : if (fd->have_reductemp)
5119 : : {
5120 : 53 : gcc_assert (!fd->have_nonctrl_scantemp);
5121 : 53 : tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
5122 : 53 : reductions = OMP_CLAUSE_DECL (c);
5123 : 53 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
5124 : 53 : g = SSA_NAME_DEF_STMT (reductions);
5125 : 53 : reductions = gimple_assign_rhs1 (g);
5126 : 53 : OMP_CLAUSE_DECL (c) = reductions;
5127 : 53 : gsi2 = gsi_for_stmt (g);
5128 : : }
5129 : : else
5130 : : {
5131 : 191 : if (gsi_end_p (gsip))
5132 : 0 : gsi2 = gsi_after_labels (region->entry);
5133 : : else
5134 : 191 : gsi2 = gsip;
5135 : : reductions = null_pointer_node;
5136 : : }
5137 : 244 : if (fd->have_pointer_condtemp || fd->have_nonctrl_scantemp)
5138 : : {
5139 : 201 : tree type;
5140 : 201 : if (fd->have_pointer_condtemp)
5141 : 33 : type = TREE_TYPE (condtemp);
5142 : : else
5143 : 168 : type = ptr_type_node;
5144 : 201 : memv = create_tmp_var (type);
5145 : 201 : TREE_ADDRESSABLE (memv) = 1;
5146 : 201 : unsigned HOST_WIDE_INT sz = 0;
5147 : 201 : tree size = NULL_TREE;
5148 : 201 : if (fd->have_pointer_condtemp)
5149 : : {
5150 : 33 : sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
5151 : 33 : sz *= fd->lastprivate_conditional;
5152 : 33 : condtemp_sz = sz;
5153 : : }
5154 : 201 : if (fd->have_nonctrl_scantemp)
5155 : : {
5156 : 173 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5157 : 173 : gimple *g = gimple_build_call (nthreads, 0);
5158 : 173 : nthreads = create_tmp_var (integer_type_node);
5159 : 173 : gimple_call_set_lhs (g, nthreads);
5160 : 173 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
5161 : 173 : nthreads = fold_convert (sizetype, nthreads);
5162 : 173 : alloc_align = TYPE_ALIGN_UNIT (long_long_integer_type_node);
5163 : 173 : size = expand_omp_scantemp_alloc (clauses, NULL_TREE, sz,
5164 : : alloc_align, nthreads, NULL,
5165 : : false);
5166 : 173 : size = fold_convert (type, size);
5167 : : }
5168 : : else
5169 : 28 : size = build_int_cst (type, sz);
5170 : 201 : expand_omp_build_assign (&gsi2, memv, size, false);
5171 : 201 : mem = build_fold_addr_expr (memv);
5172 : : }
5173 : 244 : tree t
5174 : 244 : = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
5175 : : 9, t1, t2, t2, t3, t1, null_pointer_node,
5176 : : null_pointer_node, reductions, mem);
5177 : 244 : force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
5178 : : true, GSI_SAME_STMT);
5179 : 244 : if (fd->have_pointer_condtemp)
5180 : 33 : expand_omp_build_assign (&gsi2, condtemp, memv, false);
5181 : 244 : if (fd->have_nonctrl_scantemp)
5182 : : {
5183 : 173 : tree ptr = fd->have_pointer_condtemp ? condtemp : memv;
5184 : 173 : expand_omp_scantemp_alloc (clauses, ptr, condtemp_sz,
5185 : : alloc_align, nthreads, &gsi2, false);
5186 : : }
5187 : 244 : if (fd->have_reductemp)
5188 : : {
5189 : 53 : gsi_remove (&gsi2, true);
5190 : 53 : release_ssa_name (gimple_assign_lhs (g));
5191 : : }
5192 : : }
5193 : 13976 : switch (gimple_omp_for_kind (fd->for_stmt))
5194 : : {
5195 : 9427 : case GF_OMP_FOR_KIND_FOR:
5196 : 9427 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5197 : 9427 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
5198 : 9427 : break;
5199 : 4549 : case GF_OMP_FOR_KIND_DISTRIBUTE:
5200 : 4549 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_TEAMS);
5201 : 4549 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_TEAM_NUM);
5202 : 4549 : break;
5203 : 0 : default:
5204 : 0 : gcc_unreachable ();
5205 : : }
5206 : 13976 : nthreads = build_call_expr (nthreads, 0);
5207 : 13976 : nthreads = fold_convert (itype, nthreads);
5208 : 13976 : nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE,
5209 : : true, GSI_SAME_STMT);
5210 : 13976 : threadid = build_call_expr (threadid, 0);
5211 : 13976 : threadid = fold_convert (itype, threadid);
5212 : 13976 : threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE,
5213 : : true, GSI_SAME_STMT);
5214 : :
5215 : 13976 : n1 = fd->loop.n1;
5216 : 13976 : n2 = fd->loop.n2;
5217 : 13976 : step = fd->loop.step;
5218 : 13976 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
5219 : : {
5220 : 3067 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
5221 : : OMP_CLAUSE__LOOPTEMP_);
5222 : 3067 : gcc_assert (innerc);
5223 : 3067 : n1 = OMP_CLAUSE_DECL (innerc);
5224 : 3067 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5225 : : OMP_CLAUSE__LOOPTEMP_);
5226 : 3067 : gcc_assert (innerc);
5227 : 3067 : n2 = OMP_CLAUSE_DECL (innerc);
5228 : : }
5229 : 13976 : n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
5230 : : true, NULL_TREE, true, GSI_SAME_STMT);
5231 : 13976 : n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
5232 : : true, NULL_TREE, true, GSI_SAME_STMT);
5233 : 13976 : step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
5234 : : true, NULL_TREE, true, GSI_SAME_STMT);
5235 : :
5236 : 14995 : t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
5237 : 13976 : t = fold_build2 (PLUS_EXPR, itype, step, t);
5238 : 13976 : t = fold_build2 (PLUS_EXPR, itype, t, n2);
5239 : 13976 : t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
5240 : 13976 : if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
5241 : 414 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
5242 : : fold_build1 (NEGATE_EXPR, itype, t),
5243 : : fold_build1 (NEGATE_EXPR, itype, step));
5244 : : else
5245 : 13562 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
5246 : 13976 : t = fold_convert (itype, t);
5247 : 13976 : n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5248 : :
5249 : 13976 : q = create_tmp_reg (itype, "q");
5250 : 13976 : t = fold_build2 (TRUNC_DIV_EXPR, itype, n, nthreads);
5251 : 13976 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
5252 : 13976 : gsi_insert_before (&gsi, gimple_build_assign (q, t), GSI_SAME_STMT);
5253 : :
5254 : 13976 : tt = create_tmp_reg (itype, "tt");
5255 : 13976 : t = fold_build2 (TRUNC_MOD_EXPR, itype, n, nthreads);
5256 : 13976 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
5257 : 13976 : gsi_insert_before (&gsi, gimple_build_assign (tt, t), GSI_SAME_STMT);
5258 : :
5259 : 13976 : t = build2 (LT_EXPR, boolean_type_node, threadid, tt);
5260 : 13976 : gcond *cond_stmt = gimple_build_cond_empty (t);
5261 : 13976 : gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
5262 : :
5263 : 13976 : second_bb = split_block (entry_bb, cond_stmt)->dest;
5264 : 13976 : gsi = gsi_last_nondebug_bb (second_bb);
5265 : 13976 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5266 : :
5267 : 13976 : gsi_insert_before (&gsi, gimple_build_assign (tt, build_int_cst (itype, 0)),
5268 : : GSI_SAME_STMT);
5269 : 13976 : gassign *assign_stmt
5270 : 13976 : = gimple_build_assign (q, PLUS_EXPR, q, build_int_cst (itype, 1));
5271 : 13976 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5272 : :
5273 : 13976 : third_bb = split_block (second_bb, assign_stmt)->dest;
5274 : 13976 : gsi = gsi_last_nondebug_bb (third_bb);
5275 : 13976 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5276 : :
5277 : 13976 : if (fd->have_nonctrl_scantemp)
5278 : : {
5279 : 173 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5280 : 173 : tree controlp = NULL_TREE, controlb = NULL_TREE;
5281 : 756 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5282 : 756 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5283 : 756 : && OMP_CLAUSE__SCANTEMP__CONTROL (c))
5284 : : {
5285 : 346 : if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
5286 : 173 : controlb = OMP_CLAUSE_DECL (c);
5287 : : else
5288 : 173 : controlp = OMP_CLAUSE_DECL (c);
5289 : 346 : if (controlb && controlp)
5290 : : break;
5291 : : }
5292 : 173 : gcc_assert (controlp && controlb);
5293 : 173 : tree cnt = create_tmp_var (sizetype);
5294 : 173 : gimple *g = gimple_build_assign (cnt, NOP_EXPR, q);
5295 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5296 : 173 : unsigned HOST_WIDE_INT alloc_align = TYPE_ALIGN_UNIT (ptr_type_node);
5297 : 173 : tree sz = expand_omp_scantemp_alloc (clauses, NULL_TREE, 0,
5298 : : alloc_align, cnt, NULL, true);
5299 : 173 : tree size = create_tmp_var (sizetype);
5300 : 173 : expand_omp_build_assign (&gsi, size, sz, false);
5301 : 173 : tree cmp = fold_build2 (GT_EXPR, boolean_type_node,
5302 : : size, size_int (16384));
5303 : 173 : expand_omp_build_assign (&gsi, controlb, cmp);
5304 : 173 : g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
5305 : : NULL_TREE, NULL_TREE);
5306 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5307 : 173 : fourth_bb = split_block (third_bb, g)->dest;
5308 : 173 : gsi = gsi_last_nondebug_bb (fourth_bb);
5309 : : /* FIXME: Once we have allocators, this should use allocator. */
5310 : 173 : g = gimple_build_call (builtin_decl_explicit (BUILT_IN_MALLOC), 1, size);
5311 : 173 : gimple_call_set_lhs (g, controlp);
5312 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5313 : 173 : expand_omp_scantemp_alloc (clauses, controlp, 0, alloc_align, cnt,
5314 : : &gsi, true);
5315 : 173 : gsi_prev (&gsi);
5316 : 173 : g = gsi_stmt (gsi);
5317 : 173 : fifth_bb = split_block (fourth_bb, g)->dest;
5318 : 173 : gsi = gsi_last_nondebug_bb (fifth_bb);
5319 : :
5320 : 346 : g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
5321 : 173 : gimple_call_set_lhs (g, controlp);
5322 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5323 : 173 : tree alloca_decl = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5324 : 1430 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5325 : 1257 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5326 : 1257 : && OMP_CLAUSE__SCANTEMP__ALLOC (c))
5327 : : {
5328 : 205 : tree tmp = create_tmp_var (sizetype);
5329 : 205 : tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
5330 : 205 : g = gimple_build_assign (tmp, MULT_EXPR, cnt,
5331 : 205 : TYPE_SIZE_UNIT (pointee_type));
5332 : 205 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5333 : 205 : g = gimple_build_call (alloca_decl, 2, tmp,
5334 : 205 : size_int (TYPE_ALIGN (pointee_type)));
5335 : 205 : gimple_call_set_lhs (g, OMP_CLAUSE_DECL (c));
5336 : 205 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5337 : : }
5338 : :
5339 : 173 : sixth_bb = split_block (fifth_bb, g)->dest;
5340 : 173 : gsi = gsi_last_nondebug_bb (sixth_bb);
5341 : : }
5342 : :
5343 : 13976 : t = build2 (MULT_EXPR, itype, q, threadid);
5344 : 13976 : t = build2 (PLUS_EXPR, itype, t, tt);
5345 : 13976 : s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5346 : :
5347 : 13976 : t = fold_build2 (PLUS_EXPR, itype, s0, q);
5348 : 13976 : e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5349 : :
5350 : 13976 : t = build2 (GE_EXPR, boolean_type_node, s0, e0);
5351 : 13976 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
5352 : :
5353 : : /* Remove the GIMPLE_OMP_FOR statement. */
5354 : 13976 : gsi_remove (&gsi, true);
5355 : :
5356 : : /* Setup code for sequential iteration goes in SEQ_START_BB. */
5357 : 13976 : gsi = gsi_start_bb (seq_start_bb);
5358 : :
5359 : 13976 : tree startvar = fd->loop.v;
5360 : 13976 : tree endvar = NULL_TREE;
5361 : :
5362 : 13976 : if (gimple_omp_for_combined_p (fd->for_stmt))
5363 : : {
5364 : 7081 : tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
5365 : 10629 : ? gimple_omp_parallel_clauses (inner_stmt)
5366 : 3533 : : gimple_omp_for_clauses (inner_stmt);
5367 : 7081 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
5368 : 7081 : gcc_assert (innerc);
5369 : 7081 : startvar = OMP_CLAUSE_DECL (innerc);
5370 : 7081 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5371 : : OMP_CLAUSE__LOOPTEMP_);
5372 : 7081 : gcc_assert (innerc);
5373 : 7081 : endvar = OMP_CLAUSE_DECL (innerc);
5374 : 2285 : if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
5375 : 8392 : && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
5376 : : {
5377 : 823 : innerc = find_lastprivate_looptemp (fd, innerc);
5378 : 823 : if (innerc)
5379 : : {
5380 : : /* If needed (distribute parallel for with lastprivate),
5381 : : propagate down the total number of iterations. */
5382 : 371 : tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
5383 : : fd->loop.n2);
5384 : 371 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
5385 : : GSI_CONTINUE_LINKING);
5386 : 371 : assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
5387 : 371 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5388 : : }
5389 : : }
5390 : : }
5391 : 13976 : t = fold_convert (itype, s0);
5392 : 13976 : t = fold_build2 (MULT_EXPR, itype, t, step);
5393 : 13976 : if (POINTER_TYPE_P (type))
5394 : : {
5395 : 606 : t = fold_build_pointer_plus (n1, t);
5396 : 670 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
5397 : 670 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
5398 : 0 : t = fold_convert (signed_type_for (type), t);
5399 : : }
5400 : : else
5401 : 13370 : t = fold_build2 (PLUS_EXPR, type, t, n1);
5402 : 13976 : t = fold_convert (TREE_TYPE (startvar), t);
5403 : 13976 : t = force_gimple_operand_gsi (&gsi, t,
5404 : 13976 : DECL_P (startvar)
5405 : 13976 : && TREE_ADDRESSABLE (startvar),
5406 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
5407 : 13976 : assign_stmt = gimple_build_assign (startvar, t);
5408 : 13976 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5409 : 13976 : if (cond_var)
5410 : : {
5411 : 108 : tree itype = TREE_TYPE (cond_var);
5412 : : /* For lastprivate(conditional:) itervar, we need some iteration
5413 : : counter that starts at unsigned non-zero and increases.
5414 : : Prefer as few IVs as possible, so if we can use startvar
5415 : : itself, use that, or startvar + constant (those would be
5416 : : incremented with step), and as last resort use the s0 + 1
5417 : : incremented by 1. */
5418 : 108 : if (POINTER_TYPE_P (type)
5419 : 108 : || TREE_CODE (n1) != INTEGER_CST
5420 : 102 : || fd->loop.cond_code != LT_EXPR)
5421 : 6 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
5422 : : build_int_cst (itype, 1));
5423 : 102 : else if (tree_int_cst_sgn (n1) == 1)
5424 : 12 : t = fold_convert (itype, t);
5425 : : else
5426 : : {
5427 : 90 : tree c = fold_convert (itype, n1);
5428 : 90 : c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
5429 : 90 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
5430 : : }
5431 : 108 : t = force_gimple_operand_gsi (&gsi, t, false,
5432 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
5433 : 108 : assign_stmt = gimple_build_assign (cond_var, t);
5434 : 108 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5435 : : }
5436 : :
5437 : 13976 : t = fold_convert (itype, e0);
5438 : 13976 : t = fold_build2 (MULT_EXPR, itype, t, step);
5439 : 13976 : if (POINTER_TYPE_P (type))
5440 : : {
5441 : 606 : t = fold_build_pointer_plus (n1, t);
5442 : 670 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
5443 : 670 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
5444 : 0 : t = fold_convert (signed_type_for (type), t);
5445 : : }
5446 : : else
5447 : 13370 : t = fold_build2 (PLUS_EXPR, type, t, n1);
5448 : 13976 : t = fold_convert (TREE_TYPE (startvar), t);
5449 : 13976 : e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
5450 : : false, GSI_CONTINUE_LINKING);
5451 : 13976 : if (endvar)
5452 : : {
5453 : 7081 : assign_stmt = gimple_build_assign (endvar, e);
5454 : 7081 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5455 : 7081 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
5456 : 6312 : assign_stmt = gimple_build_assign (fd->loop.v, e);
5457 : : else
5458 : 769 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
5459 : 7081 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5460 : : }
5461 : : /* Handle linear clause adjustments. */
5462 : 13976 : tree itercnt = NULL_TREE;
5463 : 13976 : tree *nonrect_bounds = NULL;
5464 : 13976 : if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
5465 : 45132 : for (tree c = gimple_omp_for_clauses (fd->for_stmt);
5466 : 45132 : c; c = OMP_CLAUSE_CHAIN (c))
5467 : 35705 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5468 : 35705 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
5469 : : {
5470 : 87 : tree d = OMP_CLAUSE_DECL (c);
5471 : 87 : tree t = d, a, dest;
5472 : 87 : if (omp_privatize_by_reference (t))
5473 : 13 : t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
5474 : 87 : if (itercnt == NULL_TREE)
5475 : : {
5476 : 87 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
5477 : : {
5478 : 0 : itercnt = fold_build2 (MINUS_EXPR, itype,
5479 : : fold_convert (itype, n1),
5480 : : fold_convert (itype, fd->loop.n1));
5481 : 0 : itercnt = fold_build2 (EXACT_DIV_EXPR, itype, itercnt, step);
5482 : 0 : itercnt = fold_build2 (PLUS_EXPR, itype, itercnt, s0);
5483 : 0 : itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
5484 : : NULL_TREE, false,
5485 : : GSI_CONTINUE_LINKING);
5486 : : }
5487 : : else
5488 : : itercnt = s0;
5489 : : }
5490 : 87 : tree type = TREE_TYPE (t);
5491 : 87 : if (POINTER_TYPE_P (type))
5492 : 0 : type = sizetype;
5493 : 87 : a = fold_build2 (MULT_EXPR, type,
5494 : : fold_convert (type, itercnt),
5495 : : fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
5496 : 87 : dest = unshare_expr (t);
5497 : 87 : t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
5498 : : : POINTER_PLUS_EXPR, TREE_TYPE (t), t, a);
5499 : 87 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
5500 : : false, GSI_CONTINUE_LINKING);
5501 : 87 : expand_omp_build_assign (&gsi, dest, t, true);
5502 : : }
5503 : 13976 : if (fd->collapse > 1)
5504 : : {
5505 : 3888 : if (fd->non_rect)
5506 : : {
5507 : 376 : nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
5508 : 376 : memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
5509 : : }
5510 : 3888 : expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
5511 : : startvar);
5512 : : }
5513 : :
5514 : 13976 : if (!broken_loop)
5515 : : {
5516 : : /* The code controlling the sequential loop replaces the
5517 : : GIMPLE_OMP_CONTINUE. */
5518 : 13480 : gsi = gsi_last_nondebug_bb (cont_bb);
5519 : 13480 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
5520 : 13480 : gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
5521 : 13480 : vmain = gimple_omp_continue_control_use (cont_stmt);
5522 : 13480 : vback = gimple_omp_continue_control_def (cont_stmt);
5523 : :
5524 : 13480 : if (cond_var)
5525 : : {
5526 : 108 : tree itype = TREE_TYPE (cond_var);
5527 : 108 : tree t2;
5528 : 108 : if (POINTER_TYPE_P (type)
5529 : 108 : || TREE_CODE (n1) != INTEGER_CST
5530 : 102 : || fd->loop.cond_code != LT_EXPR)
5531 : 6 : t2 = build_int_cst (itype, 1);
5532 : : else
5533 : 102 : t2 = fold_convert (itype, step);
5534 : 108 : t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
5535 : 108 : t2 = force_gimple_operand_gsi (&gsi, t2, false,
5536 : : NULL_TREE, true, GSI_SAME_STMT);
5537 : 108 : assign_stmt = gimple_build_assign (cond_var, t2);
5538 : 108 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5539 : : }
5540 : :
5541 : 13480 : if (!gimple_omp_for_combined_p (fd->for_stmt))
5542 : : {
5543 : 6399 : if (POINTER_TYPE_P (type))
5544 : 316 : t = fold_build_pointer_plus (vmain, step);
5545 : : else
5546 : 6083 : t = fold_build2 (PLUS_EXPR, type, vmain, step);
5547 : 6399 : t = force_gimple_operand_gsi (&gsi, t,
5548 : 6399 : DECL_P (vback)
5549 : 6399 : && TREE_ADDRESSABLE (vback),
5550 : : NULL_TREE, true, GSI_SAME_STMT);
5551 : 6399 : assign_stmt = gimple_build_assign (vback, t);
5552 : 6399 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5553 : :
5554 : 6399 : t = build2 (fd->loop.cond_code, boolean_type_node,
5555 : 6399 : DECL_P (vback) && TREE_ADDRESSABLE (vback)
5556 : : ? t : vback, e);
5557 : 6399 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
5558 : : }
5559 : :
5560 : : /* Remove the GIMPLE_OMP_CONTINUE statement. */
5561 : 13480 : gsi_remove (&gsi, true);
5562 : :
5563 : 13480 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
5564 : 1375 : collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
5565 : : cont_bb, body_bb);
5566 : : }
5567 : :
5568 : : /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing. */
5569 : 13976 : gsi = gsi_last_nondebug_bb (exit_bb);
5570 : 13976 : if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
5571 : : {
5572 : 2374 : t = gimple_omp_return_lhs (gsi_stmt (gsi));
5573 : 2374 : if (fd->have_reductemp
5574 : 2321 : || ((fd->have_pointer_condtemp || fd->have_scantemp)
5575 : 59 : && !fd->have_nonctrl_scantemp))
5576 : : {
5577 : 112 : tree fn;
5578 : 112 : if (t)
5579 : 4 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
5580 : : else
5581 : 108 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
5582 : 112 : gcall *g = gimple_build_call (fn, 0);
5583 : 112 : if (t)
5584 : : {
5585 : 4 : gimple_call_set_lhs (g, t);
5586 : 4 : if (fd->have_reductemp)
5587 : 4 : gsi_insert_after (&gsi, gimple_build_assign (reductions,
5588 : : NOP_EXPR, t),
5589 : : GSI_SAME_STMT);
5590 : : }
5591 : 112 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
5592 : 112 : }
5593 : : else
5594 : 2262 : gsi_insert_after (&gsi, omp_build_barrier (t), GSI_SAME_STMT);
5595 : : }
5596 : 11602 : else if ((fd->have_pointer_condtemp || fd->have_scantemp)
5597 : 305 : && !fd->have_nonctrl_scantemp)
5598 : : {
5599 : 132 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
5600 : 132 : gcall *g = gimple_build_call (fn, 0);
5601 : 132 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
5602 : : }
5603 : 13976 : if (fd->have_scantemp && !fd->have_nonctrl_scantemp)
5604 : : {
5605 : 173 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5606 : 173 : tree controlp = NULL_TREE, controlb = NULL_TREE;
5607 : 346 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5608 : 346 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5609 : 346 : && OMP_CLAUSE__SCANTEMP__CONTROL (c))
5610 : : {
5611 : 346 : if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
5612 : 173 : controlb = OMP_CLAUSE_DECL (c);
5613 : : else
5614 : 173 : controlp = OMP_CLAUSE_DECL (c);
5615 : 346 : if (controlb && controlp)
5616 : : break;
5617 : : }
5618 : 173 : gcc_assert (controlp && controlb);
5619 : 173 : gimple *g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
5620 : : NULL_TREE, NULL_TREE);
5621 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5622 : 173 : exit1_bb = split_block (exit_bb, g)->dest;
5623 : 173 : gsi = gsi_after_labels (exit1_bb);
5624 : 173 : g = gimple_build_call (builtin_decl_explicit (BUILT_IN_FREE), 1,
5625 : : controlp);
5626 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5627 : 173 : exit2_bb = split_block (exit1_bb, g)->dest;
5628 : 173 : gsi = gsi_after_labels (exit2_bb);
5629 : 346 : g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE), 1,
5630 : : controlp);
5631 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5632 : 173 : exit3_bb = split_block (exit2_bb, g)->dest;
5633 : 173 : gsi = gsi_after_labels (exit3_bb);
5634 : : }
5635 : 13976 : gsi_remove (&gsi, true);
5636 : :
5637 : : /* Connect all the blocks. */
5638 : 13976 : ep = make_edge (entry_bb, third_bb, EDGE_FALSE_VALUE);
5639 : 13976 : ep->probability = profile_probability::guessed_always ().apply_scale (3, 4);
5640 : 13976 : ep = find_edge (entry_bb, second_bb);
5641 : 13976 : ep->flags = EDGE_TRUE_VALUE;
5642 : 13976 : ep->probability = profile_probability::guessed_always () / 4;
5643 : 13976 : if (fourth_bb)
5644 : : {
5645 : 173 : ep = make_edge (third_bb, fifth_bb, EDGE_FALSE_VALUE);
5646 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5647 : 173 : ep = find_edge (third_bb, fourth_bb);
5648 : 173 : ep->flags = EDGE_TRUE_VALUE;
5649 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5650 : 173 : ep = find_edge (fourth_bb, fifth_bb);
5651 : 173 : redirect_edge_and_branch (ep, sixth_bb);
5652 : : }
5653 : : else
5654 : : sixth_bb = third_bb;
5655 : 13976 : find_edge (sixth_bb, seq_start_bb)->flags = EDGE_FALSE_VALUE;
5656 : 13976 : find_edge (sixth_bb, fin_bb)->flags = EDGE_TRUE_VALUE;
5657 : 13976 : if (exit1_bb)
5658 : : {
5659 : 173 : ep = make_edge (exit_bb, exit2_bb, EDGE_FALSE_VALUE);
5660 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5661 : 173 : ep = find_edge (exit_bb, exit1_bb);
5662 : 173 : ep->flags = EDGE_TRUE_VALUE;
5663 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5664 : 173 : ep = find_edge (exit1_bb, exit2_bb);
5665 : 173 : redirect_edge_and_branch (ep, exit3_bb);
5666 : : }
5667 : :
5668 : 13976 : if (!broken_loop)
5669 : : {
5670 : 13480 : ep = find_edge (cont_bb, body_bb);
5671 : 13480 : if (ep == NULL)
5672 : : {
5673 : 168 : ep = BRANCH_EDGE (cont_bb);
5674 : 168 : gcc_assert (single_succ (ep->dest) == body_bb);
5675 : : }
5676 : 13480 : if (gimple_omp_for_combined_p (fd->for_stmt))
5677 : : {
5678 : 7081 : remove_edge (ep);
5679 : 7081 : ep = NULL;
5680 : : }
5681 : 6399 : else if (fd->collapse > 1)
5682 : : {
5683 : 1375 : remove_edge (ep);
5684 : 1375 : ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
5685 : : }
5686 : : else
5687 : 5024 : ep->flags = EDGE_TRUE_VALUE;
5688 : 25585 : find_edge (cont_bb, fin_bb)->flags
5689 : 14855 : = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
5690 : : }
5691 : :
5692 : 13976 : set_immediate_dominator (CDI_DOMINATORS, second_bb, entry_bb);
5693 : 13976 : set_immediate_dominator (CDI_DOMINATORS, third_bb, entry_bb);
5694 : 13976 : if (fourth_bb)
5695 : : {
5696 : 173 : set_immediate_dominator (CDI_DOMINATORS, fifth_bb, third_bb);
5697 : 173 : set_immediate_dominator (CDI_DOMINATORS, sixth_bb, third_bb);
5698 : : }
5699 : 13976 : set_immediate_dominator (CDI_DOMINATORS, seq_start_bb, sixth_bb);
5700 : :
5701 : 13976 : set_immediate_dominator (CDI_DOMINATORS, body_bb,
5702 : : recompute_dominator (CDI_DOMINATORS, body_bb));
5703 : 13976 : set_immediate_dominator (CDI_DOMINATORS, fin_bb,
5704 : : recompute_dominator (CDI_DOMINATORS, fin_bb));
5705 : 13976 : if (exit1_bb)
5706 : : {
5707 : 173 : set_immediate_dominator (CDI_DOMINATORS, exit2_bb, exit_bb);
5708 : 173 : set_immediate_dominator (CDI_DOMINATORS, exit3_bb, exit_bb);
5709 : : }
5710 : :
5711 : 13976 : class loop *loop = body_bb->loop_father;
5712 : 13976 : if (loop != entry_bb->loop_father)
5713 : : {
5714 : 330 : gcc_assert (broken_loop || loop->header == body_bb);
5715 : 168 : gcc_assert (broken_loop
5716 : : || loop->latch == region->cont
5717 : : || single_pred (loop->latch) == region->cont);
5718 : 330 : return;
5719 : : }
5720 : :
5721 : 13646 : if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
5722 : : {
5723 : 6231 : loop = alloc_loop ();
5724 : 6231 : loop->header = body_bb;
5725 : 6231 : if (collapse_bb == NULL)
5726 : 4856 : loop->latch = cont_bb;
5727 : 6231 : add_loop (loop, body_bb->loop_father);
5728 : : }
5729 : : }
5730 : :
5731 : : /* Return phi in E->DEST with ARG on edge E. */
5732 : :
5733 : : static gphi *
5734 : 14 : find_phi_with_arg_on_edge (tree arg, edge e)
5735 : : {
5736 : 14 : basic_block bb = e->dest;
5737 : :
5738 : 14 : for (gphi_iterator gpi = gsi_start_phis (bb);
5739 : 14 : !gsi_end_p (gpi);
5740 : 0 : gsi_next (&gpi))
5741 : : {
5742 : 14 : gphi *phi = gpi.phi ();
5743 : 14 : if (PHI_ARG_DEF_FROM_EDGE (phi, e) == arg)
5744 : 14 : return phi;
5745 : : }
5746 : :
5747 : 0 : return NULL;
5748 : : }
5749 : :
5750 : : /* A subroutine of expand_omp_for. Generate code for a parallel
5751 : : loop with static schedule and a specified chunk size. Given
5752 : : parameters:
5753 : :
5754 : : for (V = N1; V cond N2; V += STEP) BODY;
5755 : :
5756 : : where COND is "<" or ">", we generate pseudocode
5757 : :
5758 : : if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
5759 : : if (cond is <)
5760 : : adj = STEP - 1;
5761 : : else
5762 : : adj = STEP + 1;
5763 : : if ((__typeof (V)) -1 > 0 && cond is >)
5764 : : n = -(adj + N2 - N1) / -STEP;
5765 : : else
5766 : : n = (adj + N2 - N1) / STEP;
5767 : : trip = 0;
5768 : : V = threadid * CHUNK * STEP + N1; -- this extra definition of V is
5769 : : here so that V is defined
5770 : : if the loop is not entered
5771 : : L0:
5772 : : s0 = (trip * nthreads + threadid) * CHUNK;
5773 : : e0 = min (s0 + CHUNK, n);
5774 : : if (s0 < n) goto L1; else goto L4;
5775 : : L1:
5776 : : V = s0 * STEP + N1;
5777 : : e = e0 * STEP + N1;
5778 : : L2:
5779 : : BODY;
5780 : : V += STEP;
5781 : : if (V cond e) goto L2; else goto L3;
5782 : : L3:
5783 : : trip += 1;
5784 : : goto L0;
5785 : : L4:
5786 : : */
5787 : :
5788 : : static void
5789 : 5811 : expand_omp_for_static_chunk (struct omp_region *region,
5790 : : struct omp_for_data *fd, gimple *inner_stmt)
5791 : : {
5792 : 5811 : tree n, s0, e0, e, t;
5793 : 5811 : tree trip_var, trip_init, trip_main, trip_back, nthreads, threadid;
5794 : 5811 : tree type, itype, vmain, vback, vextra;
5795 : 5811 : basic_block entry_bb, exit_bb, body_bb, seq_start_bb, iter_part_bb;
5796 : 5811 : basic_block trip_update_bb = NULL, cont_bb, collapse_bb = NULL, fin_bb;
5797 : 5811 : gimple_stmt_iterator gsi, gsip;
5798 : 5811 : edge se;
5799 : 5811 : bool broken_loop = region->cont == NULL;
5800 : 5811 : tree *counts = NULL;
5801 : 5811 : tree n1, n2, step;
5802 : 5811 : tree reductions = NULL_TREE;
5803 : 5811 : tree cond_var = NULL_TREE, condtemp = NULL_TREE;
5804 : :
5805 : 5811 : itype = type = TREE_TYPE (fd->loop.v);
5806 : 5811 : if (POINTER_TYPE_P (type))
5807 : 312 : itype = signed_type_for (type);
5808 : :
5809 : 5811 : entry_bb = region->entry;
5810 : 5811 : se = split_block (entry_bb, last_nondebug_stmt (entry_bb));
5811 : 5811 : entry_bb = se->src;
5812 : 5811 : iter_part_bb = se->dest;
5813 : 5811 : cont_bb = region->cont;
5814 : 5811 : gcc_assert (EDGE_COUNT (iter_part_bb->succs) == 2);
5815 : 5811 : fin_bb = BRANCH_EDGE (iter_part_bb)->dest;
5816 : 5811 : gcc_assert (broken_loop
5817 : : || fin_bb == FALLTHRU_EDGE (cont_bb)->dest);
5818 : 5811 : seq_start_bb = split_edge (FALLTHRU_EDGE (iter_part_bb));
5819 : 5811 : body_bb = single_succ (seq_start_bb);
5820 : 5811 : if (!broken_loop)
5821 : : {
5822 : 5554 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
5823 : : || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
5824 : 5554 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
5825 : 5554 : trip_update_bb = split_edge (FALLTHRU_EDGE (cont_bb));
5826 : : }
5827 : 5811 : exit_bb = region->exit;
5828 : :
5829 : : /* Trip and adjustment setup goes in ENTRY_BB. */
5830 : 5811 : gsi = gsi_last_nondebug_bb (entry_bb);
5831 : 5811 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5832 : 5811 : gsip = gsi;
5833 : 5811 : gsi_prev (&gsip);
5834 : :
5835 : 5811 : if (fd->collapse > 1)
5836 : : {
5837 : 2228 : int first_zero_iter = -1, dummy = -1;
5838 : 2228 : basic_block l2_dom_bb = NULL, dummy_bb = NULL;
5839 : :
5840 : 2228 : counts = XALLOCAVEC (tree, fd->collapse);
5841 : 2228 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
5842 : : fin_bb, first_zero_iter,
5843 : : dummy_bb, dummy, l2_dom_bb);
5844 : 2228 : t = NULL_TREE;
5845 : : }
5846 : 3583 : else if (gimple_omp_for_combined_into_p (fd->for_stmt))
5847 : 813 : t = integer_one_node;
5848 : : else
5849 : 2770 : t = fold_binary (fd->loop.cond_code, boolean_type_node,
5850 : : fold_convert (type, fd->loop.n1),
5851 : : fold_convert (type, fd->loop.n2));
5852 : 5811 : if (fd->collapse == 1
5853 : 3583 : && TYPE_UNSIGNED (type)
5854 : 6994 : && (t == NULL_TREE || !integer_onep (t)))
5855 : : {
5856 : 232 : n1 = fold_convert (type, unshare_expr (fd->loop.n1));
5857 : 232 : n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
5858 : : true, GSI_SAME_STMT);
5859 : 232 : n2 = fold_convert (type, unshare_expr (fd->loop.n2));
5860 : 232 : n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
5861 : : true, GSI_SAME_STMT);
5862 : 232 : gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
5863 : : n1, n2);
5864 : 232 : se = split_block (entry_bb, cond_stmt);
5865 : 232 : se->flags = EDGE_TRUE_VALUE;
5866 : 232 : entry_bb = se->dest;
5867 : 232 : se->probability = profile_probability::very_likely ();
5868 : 232 : se = make_edge (se->src, fin_bb, EDGE_FALSE_VALUE);
5869 : 232 : se->probability = profile_probability::very_unlikely ();
5870 : 232 : if (gimple_in_ssa_p (cfun))
5871 : : {
5872 : 1 : int dest_idx = find_edge (iter_part_bb, fin_bb)->dest_idx;
5873 : 1 : for (gphi_iterator gpi = gsi_start_phis (fin_bb);
5874 : 2 : !gsi_end_p (gpi); gsi_next (&gpi))
5875 : : {
5876 : 1 : gphi *phi = gpi.phi ();
5877 : 1 : add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
5878 : : se, UNKNOWN_LOCATION);
5879 : : }
5880 : : }
5881 : 464 : gsi = gsi_last_bb (entry_bb);
5882 : : }
5883 : :
5884 : 5811 : if (fd->lastprivate_conditional)
5885 : : {
5886 : 49 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5887 : 49 : tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
5888 : 49 : if (fd->have_pointer_condtemp)
5889 : 29 : condtemp = OMP_CLAUSE_DECL (c);
5890 : 49 : c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
5891 : 49 : cond_var = OMP_CLAUSE_DECL (c);
5892 : : }
5893 : 5811 : if (fd->have_reductemp || fd->have_pointer_condtemp)
5894 : : {
5895 : 44 : tree t1 = build_int_cst (long_integer_type_node, 0);
5896 : 44 : tree t2 = build_int_cst (long_integer_type_node, 1);
5897 : 44 : tree t3 = build_int_cstu (long_integer_type_node,
5898 : : (HOST_WIDE_INT_1U << 31) + 1);
5899 : 44 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5900 : 44 : gimple_stmt_iterator gsi2 = gsi_none ();
5901 : 44 : gimple *g = NULL;
5902 : 44 : tree mem = null_pointer_node, memv = NULL_TREE;
5903 : 44 : if (fd->have_reductemp)
5904 : : {
5905 : 25 : tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
5906 : 25 : reductions = OMP_CLAUSE_DECL (c);
5907 : 25 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
5908 : 25 : g = SSA_NAME_DEF_STMT (reductions);
5909 : 25 : reductions = gimple_assign_rhs1 (g);
5910 : 25 : OMP_CLAUSE_DECL (c) = reductions;
5911 : 25 : gsi2 = gsi_for_stmt (g);
5912 : : }
5913 : : else
5914 : : {
5915 : 19 : if (gsi_end_p (gsip))
5916 : 0 : gsi2 = gsi_after_labels (region->entry);
5917 : : else
5918 : 19 : gsi2 = gsip;
5919 : : reductions = null_pointer_node;
5920 : : }
5921 : 44 : if (fd->have_pointer_condtemp)
5922 : : {
5923 : 29 : tree type = TREE_TYPE (condtemp);
5924 : 29 : memv = create_tmp_var (type);
5925 : 29 : TREE_ADDRESSABLE (memv) = 1;
5926 : 29 : unsigned HOST_WIDE_INT sz
5927 : 29 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
5928 : 29 : sz *= fd->lastprivate_conditional;
5929 : 29 : expand_omp_build_assign (&gsi2, memv, build_int_cst (type, sz),
5930 : : false);
5931 : 29 : mem = build_fold_addr_expr (memv);
5932 : : }
5933 : 44 : tree t
5934 : 44 : = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
5935 : : 9, t1, t2, t2, t3, t1, null_pointer_node,
5936 : : null_pointer_node, reductions, mem);
5937 : 44 : force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
5938 : : true, GSI_SAME_STMT);
5939 : 44 : if (fd->have_pointer_condtemp)
5940 : 29 : expand_omp_build_assign (&gsi2, condtemp, memv, false);
5941 : 44 : if (fd->have_reductemp)
5942 : : {
5943 : 25 : gsi_remove (&gsi2, true);
5944 : 25 : release_ssa_name (gimple_assign_lhs (g));
5945 : : }
5946 : : }
5947 : 5811 : switch (gimple_omp_for_kind (fd->for_stmt))
5948 : : {
5949 : 2357 : case GF_OMP_FOR_KIND_FOR:
5950 : 2357 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5951 : 2357 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
5952 : 2357 : break;
5953 : 3454 : case GF_OMP_FOR_KIND_DISTRIBUTE:
5954 : 3454 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_TEAMS);
5955 : 3454 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_TEAM_NUM);
5956 : 3454 : break;
5957 : 0 : default:
5958 : 0 : gcc_unreachable ();
5959 : : }
5960 : 5811 : nthreads = build_call_expr (nthreads, 0);
5961 : 5811 : nthreads = fold_convert (itype, nthreads);
5962 : 5811 : nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE,
5963 : : true, GSI_SAME_STMT);
5964 : 5811 : threadid = build_call_expr (threadid, 0);
5965 : 5811 : threadid = fold_convert (itype, threadid);
5966 : 5811 : threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE,
5967 : : true, GSI_SAME_STMT);
5968 : :
5969 : 5811 : n1 = fd->loop.n1;
5970 : 5811 : n2 = fd->loop.n2;
5971 : 5811 : step = fd->loop.step;
5972 : 5811 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
5973 : : {
5974 : 1301 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
5975 : : OMP_CLAUSE__LOOPTEMP_);
5976 : 1301 : gcc_assert (innerc);
5977 : 1301 : n1 = OMP_CLAUSE_DECL (innerc);
5978 : 1301 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5979 : : OMP_CLAUSE__LOOPTEMP_);
5980 : 1301 : gcc_assert (innerc);
5981 : 1301 : n2 = OMP_CLAUSE_DECL (innerc);
5982 : : }
5983 : 5811 : n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
5984 : : true, NULL_TREE, true, GSI_SAME_STMT);
5985 : 5811 : n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
5986 : : true, NULL_TREE, true, GSI_SAME_STMT);
5987 : 5811 : step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
5988 : : true, NULL_TREE, true, GSI_SAME_STMT);
5989 : 5811 : tree chunk_size = fold_convert (itype, fd->chunk_size);
5990 : 5811 : chunk_size = omp_adjust_chunk_size (chunk_size, fd->simd_schedule,
5991 : 5811 : is_in_offload_region (region));
5992 : 5811 : chunk_size
5993 : 5811 : = force_gimple_operand_gsi (&gsi, chunk_size, true, NULL_TREE, true,
5994 : : GSI_SAME_STMT);
5995 : :
5996 : 6417 : t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
5997 : 5811 : t = fold_build2 (PLUS_EXPR, itype, step, t);
5998 : 5811 : t = fold_build2 (PLUS_EXPR, itype, t, n2);
5999 : 5811 : t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
6000 : 5811 : if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
6001 : 284 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
6002 : : fold_build1 (NEGATE_EXPR, itype, t),
6003 : : fold_build1 (NEGATE_EXPR, itype, step));
6004 : : else
6005 : 5527 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
6006 : 5811 : t = fold_convert (itype, t);
6007 : 5811 : n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6008 : : true, GSI_SAME_STMT);
6009 : :
6010 : 5811 : trip_var = create_tmp_reg (itype, ".trip");
6011 : 5811 : if (gimple_in_ssa_p (cfun))
6012 : : {
6013 : 12 : trip_init = make_ssa_name (trip_var);
6014 : 12 : trip_main = make_ssa_name (trip_var);
6015 : 12 : trip_back = make_ssa_name (trip_var);
6016 : : }
6017 : : else
6018 : : {
6019 : : trip_init = trip_var;
6020 : : trip_main = trip_var;
6021 : : trip_back = trip_var;
6022 : : }
6023 : :
6024 : 5811 : gassign *assign_stmt
6025 : 5811 : = gimple_build_assign (trip_init, build_int_cst (itype, 0));
6026 : 5811 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6027 : :
6028 : 5811 : t = fold_build2 (MULT_EXPR, itype, threadid, chunk_size);
6029 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, step);
6030 : 5811 : if (POINTER_TYPE_P (type))
6031 : 312 : t = fold_build_pointer_plus (n1, t);
6032 : : else
6033 : 5499 : t = fold_build2 (PLUS_EXPR, type, t, n1);
6034 : 5811 : vextra = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6035 : : true, GSI_SAME_STMT);
6036 : :
6037 : : /* Remove the GIMPLE_OMP_FOR. */
6038 : 5811 : gsi_remove (&gsi, true);
6039 : :
6040 : 5811 : gimple_stmt_iterator gsif = gsi;
6041 : :
6042 : : /* Iteration space partitioning goes in ITER_PART_BB. */
6043 : 5811 : gsi = gsi_last_bb (iter_part_bb);
6044 : :
6045 : 5811 : t = fold_build2 (MULT_EXPR, itype, trip_main, nthreads);
6046 : 5811 : t = fold_build2 (PLUS_EXPR, itype, t, threadid);
6047 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, chunk_size);
6048 : 5811 : s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6049 : : false, GSI_CONTINUE_LINKING);
6050 : :
6051 : 5811 : t = fold_build2 (PLUS_EXPR, itype, s0, chunk_size);
6052 : 5811 : t = fold_build2 (MIN_EXPR, itype, t, n);
6053 : 5811 : e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6054 : : false, GSI_CONTINUE_LINKING);
6055 : :
6056 : 5811 : t = build2 (LT_EXPR, boolean_type_node, s0, n);
6057 : 5811 : gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_CONTINUE_LINKING);
6058 : :
6059 : : /* Setup code for sequential iteration goes in SEQ_START_BB. */
6060 : 5811 : gsi = gsi_start_bb (seq_start_bb);
6061 : :
6062 : 5811 : tree startvar = fd->loop.v;
6063 : 5811 : tree endvar = NULL_TREE;
6064 : :
6065 : 5811 : if (gimple_omp_for_combined_p (fd->for_stmt))
6066 : : {
6067 : 4202 : tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
6068 : 6943 : ? gimple_omp_parallel_clauses (inner_stmt)
6069 : 1461 : : gimple_omp_for_clauses (inner_stmt);
6070 : 4202 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
6071 : 4202 : gcc_assert (innerc);
6072 : 4202 : startvar = OMP_CLAUSE_DECL (innerc);
6073 : 4202 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
6074 : : OMP_CLAUSE__LOOPTEMP_);
6075 : 4202 : gcc_assert (innerc);
6076 : 4202 : endvar = OMP_CLAUSE_DECL (innerc);
6077 : 1663 : if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
6078 : 5087 : && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
6079 : : {
6080 : 704 : innerc = find_lastprivate_looptemp (fd, innerc);
6081 : 704 : if (innerc)
6082 : : {
6083 : : /* If needed (distribute parallel for with lastprivate),
6084 : : propagate down the total number of iterations. */
6085 : 320 : tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
6086 : : fd->loop.n2);
6087 : 320 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
6088 : : GSI_CONTINUE_LINKING);
6089 : 320 : assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
6090 : 320 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6091 : : }
6092 : : }
6093 : : }
6094 : :
6095 : 5811 : t = fold_convert (itype, s0);
6096 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, step);
6097 : 5811 : if (POINTER_TYPE_P (type))
6098 : : {
6099 : 312 : t = fold_build_pointer_plus (n1, t);
6100 : 376 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
6101 : 376 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
6102 : 0 : t = fold_convert (signed_type_for (type), t);
6103 : : }
6104 : : else
6105 : 5499 : t = fold_build2 (PLUS_EXPR, type, t, n1);
6106 : 5811 : t = fold_convert (TREE_TYPE (startvar), t);
6107 : 5811 : t = force_gimple_operand_gsi (&gsi, t,
6108 : 5811 : DECL_P (startvar)
6109 : 5811 : && TREE_ADDRESSABLE (startvar),
6110 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
6111 : 5811 : assign_stmt = gimple_build_assign (startvar, t);
6112 : 5811 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6113 : 5811 : if (cond_var)
6114 : : {
6115 : 49 : tree itype = TREE_TYPE (cond_var);
6116 : : /* For lastprivate(conditional:) itervar, we need some iteration
6117 : : counter that starts at unsigned non-zero and increases.
6118 : : Prefer as few IVs as possible, so if we can use startvar
6119 : : itself, use that, or startvar + constant (those would be
6120 : : incremented with step), and as last resort use the s0 + 1
6121 : : incremented by 1. */
6122 : 49 : if (POINTER_TYPE_P (type)
6123 : 49 : || TREE_CODE (n1) != INTEGER_CST
6124 : 43 : || fd->loop.cond_code != LT_EXPR)
6125 : 6 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
6126 : : build_int_cst (itype, 1));
6127 : 43 : else if (tree_int_cst_sgn (n1) == 1)
6128 : 21 : t = fold_convert (itype, t);
6129 : : else
6130 : : {
6131 : 22 : tree c = fold_convert (itype, n1);
6132 : 22 : c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
6133 : 22 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
6134 : : }
6135 : 49 : t = force_gimple_operand_gsi (&gsi, t, false,
6136 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
6137 : 49 : assign_stmt = gimple_build_assign (cond_var, t);
6138 : 49 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6139 : : }
6140 : :
6141 : 5811 : t = fold_convert (itype, e0);
6142 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, step);
6143 : 5811 : if (POINTER_TYPE_P (type))
6144 : : {
6145 : 312 : t = fold_build_pointer_plus (n1, t);
6146 : 376 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
6147 : 376 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
6148 : 0 : t = fold_convert (signed_type_for (type), t);
6149 : : }
6150 : : else
6151 : 5499 : t = fold_build2 (PLUS_EXPR, type, t, n1);
6152 : 5811 : t = fold_convert (TREE_TYPE (startvar), t);
6153 : 5811 : e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6154 : : false, GSI_CONTINUE_LINKING);
6155 : 5811 : if (endvar)
6156 : : {
6157 : 4202 : assign_stmt = gimple_build_assign (endvar, e);
6158 : 4202 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6159 : 4202 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
6160 : 3434 : assign_stmt = gimple_build_assign (fd->loop.v, e);
6161 : : else
6162 : 768 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
6163 : 4202 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6164 : : }
6165 : : /* Handle linear clause adjustments. */
6166 : 5811 : tree itercnt = NULL_TREE, itercntbias = NULL_TREE;
6167 : 5811 : if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
6168 : 15766 : for (tree c = gimple_omp_for_clauses (fd->for_stmt);
6169 : 15766 : c; c = OMP_CLAUSE_CHAIN (c))
6170 : 13409 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6171 : 13409 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6172 : : {
6173 : 68 : tree d = OMP_CLAUSE_DECL (c);
6174 : 68 : tree t = d, a, dest;
6175 : 68 : if (omp_privatize_by_reference (t))
6176 : 3 : t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
6177 : 68 : tree type = TREE_TYPE (t);
6178 : 68 : if (POINTER_TYPE_P (type))
6179 : 0 : type = sizetype;
6180 : 68 : dest = unshare_expr (t);
6181 : 68 : tree v = create_tmp_var (TREE_TYPE (t), NULL);
6182 : 68 : expand_omp_build_assign (&gsif, v, t);
6183 : 68 : if (itercnt == NULL_TREE)
6184 : : {
6185 : 68 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
6186 : : {
6187 : 0 : itercntbias
6188 : 0 : = fold_build2 (MINUS_EXPR, itype, fold_convert (itype, n1),
6189 : : fold_convert (itype, fd->loop.n1));
6190 : 0 : itercntbias = fold_build2 (EXACT_DIV_EXPR, itype,
6191 : : itercntbias, step);
6192 : 0 : itercntbias
6193 : 0 : = force_gimple_operand_gsi (&gsif, itercntbias, true,
6194 : : NULL_TREE, true,
6195 : : GSI_SAME_STMT);
6196 : 0 : itercnt = fold_build2 (PLUS_EXPR, itype, itercntbias, s0);
6197 : 0 : itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
6198 : : NULL_TREE, false,
6199 : : GSI_CONTINUE_LINKING);
6200 : : }
6201 : : else
6202 : : itercnt = s0;
6203 : : }
6204 : 68 : a = fold_build2 (MULT_EXPR, type,
6205 : : fold_convert (type, itercnt),
6206 : : fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
6207 : 68 : t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
6208 : : : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
6209 : 68 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6210 : : false, GSI_CONTINUE_LINKING);
6211 : 68 : expand_omp_build_assign (&gsi, dest, t, true);
6212 : : }
6213 : 5811 : if (fd->collapse > 1)
6214 : 2228 : expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
6215 : :
6216 : 5811 : if (!broken_loop)
6217 : : {
6218 : : /* The code controlling the sequential loop goes in CONT_BB,
6219 : : replacing the GIMPLE_OMP_CONTINUE. */
6220 : 5554 : gsi = gsi_last_nondebug_bb (cont_bb);
6221 : 5554 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
6222 : 5554 : vmain = gimple_omp_continue_control_use (cont_stmt);
6223 : 5554 : vback = gimple_omp_continue_control_def (cont_stmt);
6224 : :
6225 : 5554 : if (cond_var)
6226 : : {
6227 : 49 : tree itype = TREE_TYPE (cond_var);
6228 : 49 : tree t2;
6229 : 49 : if (POINTER_TYPE_P (type)
6230 : 49 : || TREE_CODE (n1) != INTEGER_CST
6231 : 43 : || fd->loop.cond_code != LT_EXPR)
6232 : 6 : t2 = build_int_cst (itype, 1);
6233 : : else
6234 : 43 : t2 = fold_convert (itype, step);
6235 : 49 : t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
6236 : 49 : t2 = force_gimple_operand_gsi (&gsi, t2, false,
6237 : : NULL_TREE, true, GSI_SAME_STMT);
6238 : 49 : assign_stmt = gimple_build_assign (cond_var, t2);
6239 : 49 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6240 : : }
6241 : :
6242 : 5554 : if (!gimple_omp_for_combined_p (fd->for_stmt))
6243 : : {
6244 : 1352 : if (POINTER_TYPE_P (type))
6245 : 92 : t = fold_build_pointer_plus (vmain, step);
6246 : : else
6247 : 1260 : t = fold_build2 (PLUS_EXPR, type, vmain, step);
6248 : 1352 : if (DECL_P (vback) && TREE_ADDRESSABLE (vback))
6249 : 0 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6250 : : true, GSI_SAME_STMT);
6251 : 1352 : assign_stmt = gimple_build_assign (vback, t);
6252 : 1352 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6253 : :
6254 : 1352 : if (tree_int_cst_equal (fd->chunk_size, integer_one_node))
6255 : 94 : t = build2 (EQ_EXPR, boolean_type_node,
6256 : : build_int_cst (itype, 0),
6257 : : build_int_cst (itype, 1));
6258 : : else
6259 : 1258 : t = build2 (fd->loop.cond_code, boolean_type_node,
6260 : 1258 : DECL_P (vback) && TREE_ADDRESSABLE (vback)
6261 : : ? t : vback, e);
6262 : 1352 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
6263 : : }
6264 : :
6265 : : /* Remove GIMPLE_OMP_CONTINUE. */
6266 : 5554 : gsi_remove (&gsi, true);
6267 : :
6268 : 5554 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
6269 : 445 : collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, body_bb);
6270 : :
6271 : : /* Trip update code goes into TRIP_UPDATE_BB. */
6272 : 5554 : gsi = gsi_start_bb (trip_update_bb);
6273 : :
6274 : 5554 : t = build_int_cst (itype, 1);
6275 : 5554 : t = build2 (PLUS_EXPR, itype, trip_main, t);
6276 : 5554 : assign_stmt = gimple_build_assign (trip_back, t);
6277 : 5554 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6278 : : }
6279 : :
6280 : : /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing. */
6281 : 5811 : gsi = gsi_last_nondebug_bb (exit_bb);
6282 : 5811 : if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
6283 : : {
6284 : 296 : t = gimple_omp_return_lhs (gsi_stmt (gsi));
6285 : 296 : if (fd->have_reductemp || fd->have_pointer_condtemp)
6286 : : {
6287 : 35 : tree fn;
6288 : 35 : if (t)
6289 : 0 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
6290 : : else
6291 : 35 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
6292 : 35 : gcall *g = gimple_build_call (fn, 0);
6293 : 35 : if (t)
6294 : : {
6295 : 0 : gimple_call_set_lhs (g, t);
6296 : 0 : if (fd->have_reductemp)
6297 : 0 : gsi_insert_after (&gsi, gimple_build_assign (reductions,
6298 : : NOP_EXPR, t),
6299 : : GSI_SAME_STMT);
6300 : : }
6301 : 35 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
6302 : 35 : }
6303 : : else
6304 : 261 : gsi_insert_after (&gsi, omp_build_barrier (t), GSI_SAME_STMT);
6305 : : }
6306 : 5515 : else if (fd->have_pointer_condtemp)
6307 : : {
6308 : 9 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
6309 : 9 : gcall *g = gimple_build_call (fn, 0);
6310 : 9 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
6311 : : }
6312 : 5811 : gsi_remove (&gsi, true);
6313 : :
6314 : : /* Connect the new blocks. */
6315 : 5811 : find_edge (iter_part_bb, seq_start_bb)->flags = EDGE_TRUE_VALUE;
6316 : 5811 : find_edge (iter_part_bb, fin_bb)->flags = EDGE_FALSE_VALUE;
6317 : :
6318 : 5811 : if (!broken_loop)
6319 : : {
6320 : 5554 : se = find_edge (cont_bb, body_bb);
6321 : 5554 : if (se == NULL)
6322 : : {
6323 : 12 : se = BRANCH_EDGE (cont_bb);
6324 : 12 : gcc_assert (single_succ (se->dest) == body_bb);
6325 : : }
6326 : 5554 : if (gimple_omp_for_combined_p (fd->for_stmt))
6327 : : {
6328 : 4202 : remove_edge (se);
6329 : 4202 : se = NULL;
6330 : : }
6331 : 1352 : else if (fd->collapse > 1)
6332 : : {
6333 : 445 : remove_edge (se);
6334 : 445 : se = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
6335 : : }
6336 : : else
6337 : 907 : se->flags = EDGE_TRUE_VALUE;
6338 : 10663 : find_edge (cont_bb, trip_update_bb)->flags
6339 : 445 : = se ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
6340 : :
6341 : 5554 : redirect_edge_and_branch (single_succ_edge (trip_update_bb),
6342 : : iter_part_bb);
6343 : : }
6344 : :
6345 : 5811 : if (gimple_in_ssa_p (cfun))
6346 : : {
6347 : 12 : gphi_iterator psi;
6348 : 12 : gphi *phi;
6349 : 12 : edge re, ene;
6350 : 12 : edge_var_map *vm;
6351 : 12 : size_t i;
6352 : :
6353 : 12 : gcc_assert (fd->collapse == 1 && !broken_loop);
6354 : :
6355 : : /* When we redirect the edge from trip_update_bb to iter_part_bb, we
6356 : : remove arguments of the phi nodes in fin_bb. We need to create
6357 : : appropriate phi nodes in iter_part_bb instead. */
6358 : 12 : se = find_edge (iter_part_bb, fin_bb);
6359 : 12 : re = single_succ_edge (trip_update_bb);
6360 : 12 : vec<edge_var_map> *head = redirect_edge_var_map_vector (re);
6361 : 12 : ene = single_succ_edge (entry_bb);
6362 : :
6363 : 12 : psi = gsi_start_phis (fin_bb);
6364 : 21 : for (i = 0; !gsi_end_p (psi) && head->iterate (i, &vm);
6365 : 9 : gsi_next (&psi), ++i)
6366 : : {
6367 : 9 : gphi *nphi;
6368 : 9 : location_t locus;
6369 : :
6370 : 9 : phi = psi.phi ();
6371 : 9 : if (operand_equal_p (gimple_phi_arg_def (phi, 0),
6372 : 9 : redirect_edge_var_map_def (vm), 0))
6373 : 0 : continue;
6374 : :
6375 : 9 : t = gimple_phi_result (phi);
6376 : 9 : gcc_assert (t == redirect_edge_var_map_result (vm));
6377 : :
6378 : 9 : if (!single_pred_p (fin_bb))
6379 : 1 : t = copy_ssa_name (t, phi);
6380 : :
6381 : 9 : nphi = create_phi_node (t, iter_part_bb);
6382 : :
6383 : 9 : t = PHI_ARG_DEF_FROM_EDGE (phi, se);
6384 : 9 : locus = gimple_phi_arg_location_from_edge (phi, se);
6385 : :
6386 : : /* A special case -- fd->loop.v is not yet computed in
6387 : : iter_part_bb, we need to use vextra instead. */
6388 : 9 : if (t == fd->loop.v)
6389 : 0 : t = vextra;
6390 : 9 : add_phi_arg (nphi, t, ene, locus);
6391 : 9 : locus = redirect_edge_var_map_location (vm);
6392 : 9 : tree back_arg = redirect_edge_var_map_def (vm);
6393 : 9 : add_phi_arg (nphi, back_arg, re, locus);
6394 : 9 : edge ce = find_edge (cont_bb, body_bb);
6395 : 9 : if (ce == NULL)
6396 : : {
6397 : 9 : ce = BRANCH_EDGE (cont_bb);
6398 : 9 : gcc_assert (single_succ (ce->dest) == body_bb);
6399 : : ce = single_succ_edge (ce->dest);
6400 : : }
6401 : 9 : gphi *inner_loop_phi = find_phi_with_arg_on_edge (back_arg, ce);
6402 : 9 : gcc_assert (inner_loop_phi != NULL);
6403 : 9 : add_phi_arg (inner_loop_phi, gimple_phi_result (nphi),
6404 : : find_edge (seq_start_bb, body_bb), locus);
6405 : :
6406 : 9 : if (!single_pred_p (fin_bb))
6407 : 1 : add_phi_arg (phi, gimple_phi_result (nphi), se, locus);
6408 : : }
6409 : 21 : gcc_assert (gsi_end_p (psi) && (head == NULL || i == head->length ()));
6410 : 12 : redirect_edge_var_map_clear (re);
6411 : 12 : if (single_pred_p (fin_bb))
6412 : 27 : while (1)
6413 : : {
6414 : 19 : psi = gsi_start_phis (fin_bb);
6415 : 19 : if (gsi_end_p (psi))
6416 : : break;
6417 : 8 : remove_phi_node (&psi, false);
6418 : : }
6419 : :
6420 : : /* Make phi node for trip. */
6421 : 12 : phi = create_phi_node (trip_main, iter_part_bb);
6422 : 12 : add_phi_arg (phi, trip_back, single_succ_edge (trip_update_bb),
6423 : : UNKNOWN_LOCATION);
6424 : 12 : add_phi_arg (phi, trip_init, single_succ_edge (entry_bb),
6425 : : UNKNOWN_LOCATION);
6426 : : }
6427 : :
6428 : 5811 : if (!broken_loop)
6429 : 5554 : set_immediate_dominator (CDI_DOMINATORS, trip_update_bb, cont_bb);
6430 : 5811 : set_immediate_dominator (CDI_DOMINATORS, iter_part_bb,
6431 : : recompute_dominator (CDI_DOMINATORS, iter_part_bb));
6432 : 5811 : set_immediate_dominator (CDI_DOMINATORS, fin_bb,
6433 : : recompute_dominator (CDI_DOMINATORS, fin_bb));
6434 : 5811 : set_immediate_dominator (CDI_DOMINATORS, seq_start_bb,
6435 : : recompute_dominator (CDI_DOMINATORS, seq_start_bb));
6436 : 5811 : set_immediate_dominator (CDI_DOMINATORS, body_bb,
6437 : : recompute_dominator (CDI_DOMINATORS, body_bb));
6438 : :
6439 : 5811 : if (!broken_loop)
6440 : : {
6441 : 5554 : class loop *loop = body_bb->loop_father;
6442 : 5554 : class loop *trip_loop = alloc_loop ();
6443 : 5554 : trip_loop->header = iter_part_bb;
6444 : 5554 : trip_loop->latch = trip_update_bb;
6445 : 5554 : add_loop (trip_loop, iter_part_bb->loop_father);
6446 : :
6447 : 5554 : if (loop != entry_bb->loop_father)
6448 : : {
6449 : 12 : gcc_assert (loop->header == body_bb);
6450 : 12 : gcc_assert (loop->latch == region->cont
6451 : : || single_pred (loop->latch) == region->cont);
6452 : 12 : trip_loop->inner = loop;
6453 : 12 : return;
6454 : : }
6455 : :
6456 : 5542 : if (!gimple_omp_for_combined_p (fd->for_stmt))
6457 : : {
6458 : 1340 : loop = alloc_loop ();
6459 : 1340 : loop->header = body_bb;
6460 : 1340 : if (collapse_bb == NULL)
6461 : 895 : loop->latch = cont_bb;
6462 : 1340 : add_loop (loop, trip_loop);
6463 : : }
6464 : : }
6465 : : }
6466 : :
6467 : : /* A subroutine of expand_omp_for. Generate code for a simd non-worksharing
6468 : : loop. Given parameters:
6469 : :
6470 : : for (V = N1; V cond N2; V += STEP) BODY;
6471 : :
6472 : : where COND is "<" or ">", we generate pseudocode
6473 : :
6474 : : V = N1;
6475 : : goto L1;
6476 : : L0:
6477 : : BODY;
6478 : : V += STEP;
6479 : : L1:
6480 : : if (V cond N2) goto L0; else goto L2;
6481 : : L2:
6482 : :
6483 : : For collapsed loops, emit the outer loops as scalar
6484 : : and only try to vectorize the innermost loop. */
6485 : :
6486 : : static void
6487 : 9347 : expand_omp_simd (struct omp_region *region, struct omp_for_data *fd)
6488 : : {
6489 : 9347 : tree type, t;
6490 : 9347 : basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, l2_bb, l2_dom_bb;
6491 : 9347 : gimple_stmt_iterator gsi;
6492 : 9347 : gimple *stmt;
6493 : 9347 : gcond *cond_stmt;
6494 : 9347 : bool broken_loop = region->cont == NULL;
6495 : 9347 : edge e, ne;
6496 : 9347 : tree *counts = NULL;
6497 : 9347 : int i;
6498 : 9347 : int safelen_int = INT_MAX;
6499 : 9347 : bool dont_vectorize = false;
6500 : 9347 : tree safelen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6501 : : OMP_CLAUSE_SAFELEN);
6502 : 9347 : tree simduid = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6503 : : OMP_CLAUSE__SIMDUID_);
6504 : 9347 : tree ifc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6505 : : OMP_CLAUSE_IF);
6506 : 9347 : tree simdlen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6507 : : OMP_CLAUSE_SIMDLEN);
6508 : 9347 : tree condtemp = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6509 : : OMP_CLAUSE__CONDTEMP_);
6510 : 9347 : tree n1, n2;
6511 : 9424 : tree cond_var = condtemp ? OMP_CLAUSE_DECL (condtemp) : NULL_TREE;
6512 : :
6513 : 9347 : if (safelen)
6514 : : {
6515 : 5345 : poly_uint64 val;
6516 : 5345 : safelen = OMP_CLAUSE_SAFELEN_EXPR (safelen);
6517 : 5345 : if (!poly_int_tree_p (safelen, &val))
6518 : : safelen_int = 0;
6519 : : else
6520 : 5330 : safelen_int = MIN (constant_lower_bound (val), INT_MAX);
6521 : 5330 : if (safelen_int == 1)
6522 : 1778 : safelen_int = 0;
6523 : : }
6524 : 759 : if ((ifc && integer_zerop (OMP_CLAUSE_IF_EXPR (ifc)))
6525 : 9982 : || (simdlen && integer_onep (OMP_CLAUSE_SIMDLEN_EXPR (simdlen))))
6526 : : {
6527 : : safelen_int = 0;
6528 : : dont_vectorize = true;
6529 : : }
6530 : 9347 : type = TREE_TYPE (fd->loop.v);
6531 : 9347 : entry_bb = region->entry;
6532 : 9347 : cont_bb = region->cont;
6533 : 9347 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
6534 : 9347 : gcc_assert (broken_loop
6535 : : || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
6536 : 9347 : l0_bb = FALLTHRU_EDGE (entry_bb)->dest;
6537 : 9347 : if (!broken_loop)
6538 : : {
6539 : 8275 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == l0_bb);
6540 : 8275 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
6541 : 8275 : l1_bb = split_block (cont_bb, last_nondebug_stmt (cont_bb))->dest;
6542 : 8275 : l2_bb = BRANCH_EDGE (entry_bb)->dest;
6543 : : }
6544 : : else
6545 : : {
6546 : 1072 : BRANCH_EDGE (entry_bb)->flags &= ~EDGE_ABNORMAL;
6547 : 1072 : l1_bb = split_edge (BRANCH_EDGE (entry_bb));
6548 : 1072 : l2_bb = single_succ (l1_bb);
6549 : : }
6550 : 9347 : exit_bb = region->exit;
6551 : 9347 : l2_dom_bb = NULL;
6552 : :
6553 : 9347 : gsi = gsi_last_nondebug_bb (entry_bb);
6554 : :
6555 : 9347 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
6556 : : /* Not needed in SSA form right now. */
6557 : 9347 : gcc_assert (!gimple_in_ssa_p (cfun));
6558 : 9347 : if (fd->collapse > 1
6559 : 9347 : && (gimple_omp_for_combined_into_p (fd->for_stmt)
6560 : 494 : || broken_loop))
6561 : : {
6562 : 2264 : int first_zero_iter = -1, dummy = -1;
6563 : 2264 : basic_block zero_iter_bb = l2_bb, dummy_bb = NULL;
6564 : :
6565 : 2264 : counts = XALLOCAVEC (tree, fd->collapse);
6566 : 2264 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
6567 : : zero_iter_bb, first_zero_iter,
6568 : : dummy_bb, dummy, l2_dom_bb);
6569 : : }
6570 : 9347 : if (l2_dom_bb == NULL)
6571 : 9339 : l2_dom_bb = l1_bb;
6572 : :
6573 : 9347 : n1 = fd->loop.n1;
6574 : 9347 : n2 = fd->loop.n2;
6575 : 9347 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
6576 : : {
6577 : 7000 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6578 : : OMP_CLAUSE__LOOPTEMP_);
6579 : 7000 : gcc_assert (innerc);
6580 : 7000 : n1 = OMP_CLAUSE_DECL (innerc);
6581 : 7000 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
6582 : : OMP_CLAUSE__LOOPTEMP_);
6583 : 7000 : gcc_assert (innerc);
6584 : 7000 : n2 = OMP_CLAUSE_DECL (innerc);
6585 : : }
6586 : 9347 : tree step = fd->loop.step;
6587 : 9347 : tree orig_step = step; /* May be different from step if is_simt. */
6588 : :
6589 : 9347 : bool is_simt = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6590 : 9347 : OMP_CLAUSE__SIMT_);
6591 : 9347 : if (is_simt)
6592 : : {
6593 : 0 : cfun->curr_properties &= ~PROP_gimple_lomp_dev;
6594 : 0 : is_simt = safelen_int > 1;
6595 : : }
6596 : 9347 : tree simt_lane = NULL_TREE, simt_maxlane = NULL_TREE;
6597 : 9347 : if (is_simt)
6598 : : {
6599 : 0 : simt_lane = create_tmp_var (unsigned_type_node);
6600 : 0 : gimple *g = gimple_build_call_internal (IFN_GOMP_SIMT_LANE, 0);
6601 : 0 : gimple_call_set_lhs (g, simt_lane);
6602 : 0 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6603 : 0 : tree offset = fold_build2 (MULT_EXPR, TREE_TYPE (step), step,
6604 : : fold_convert (TREE_TYPE (step), simt_lane));
6605 : 0 : n1 = fold_convert (type, n1);
6606 : 0 : if (POINTER_TYPE_P (type))
6607 : 0 : n1 = fold_build_pointer_plus (n1, offset);
6608 : : else
6609 : 0 : n1 = fold_build2 (PLUS_EXPR, type, n1, fold_convert (type, offset));
6610 : :
6611 : : /* Collapsed loops not handled for SIMT yet: limit to one lane only. */
6612 : 0 : if (fd->collapse > 1)
6613 : 0 : simt_maxlane = build_one_cst (unsigned_type_node);
6614 : 0 : else if (safelen_int < omp_max_simt_vf ())
6615 : 0 : simt_maxlane = build_int_cst (unsigned_type_node, safelen_int);
6616 : 0 : tree vf
6617 : 0 : = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_GOMP_SIMT_VF,
6618 : : unsigned_type_node, 0);
6619 : 0 : if (simt_maxlane)
6620 : 0 : vf = fold_build2 (MIN_EXPR, unsigned_type_node, vf, simt_maxlane);
6621 : 0 : vf = fold_convert (TREE_TYPE (step), vf);
6622 : 0 : step = fold_build2 (MULT_EXPR, TREE_TYPE (step), step, vf);
6623 : : }
6624 : :
6625 : 9347 : tree n2var = NULL_TREE;
6626 : 9347 : tree n2v = NULL_TREE;
6627 : 9347 : tree *nonrect_bounds = NULL;
6628 : 9347 : tree min_arg1 = NULL_TREE, min_arg2 = NULL_TREE;
6629 : 9347 : if (fd->collapse > 1)
6630 : : {
6631 : 2738 : if (broken_loop || gimple_omp_for_combined_into_p (fd->for_stmt))
6632 : : {
6633 : 2264 : if (fd->non_rect)
6634 : : {
6635 : 99 : nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
6636 : 99 : memset (nonrect_bounds, 0,
6637 : : sizeof (tree) * (fd->last_nonrect + 1));
6638 : : }
6639 : 2264 : expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
6640 : 2264 : gcc_assert (entry_bb == gsi_bb (gsi));
6641 : 2264 : gcc_assert (fd->for_stmt == gsi_stmt (gsi));
6642 : 2264 : gsi_prev (&gsi);
6643 : 2264 : entry_bb = split_block (entry_bb, gsi_stmt (gsi))->dest;
6644 : 2264 : expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds,
6645 : : NULL, n1);
6646 : 2264 : gsi = gsi_for_stmt (fd->for_stmt);
6647 : : }
6648 : 2738 : if (broken_loop)
6649 : : ;
6650 : 2202 : else if (gimple_omp_for_combined_into_p (fd->for_stmt))
6651 : : {
6652 : : /* Compute in n2var the limit for the first innermost loop,
6653 : : i.e. fd->loop.v + MIN (n2 - fd->loop.v, cnt)
6654 : : where cnt is how many iterations would the loop have if
6655 : : all further iterations were assigned to the current task. */
6656 : 1728 : n2var = create_tmp_var (type);
6657 : 1728 : i = fd->collapse - 1;
6658 : 1728 : tree itype = TREE_TYPE (fd->loops[i].v);
6659 : 1728 : if (POINTER_TYPE_P (itype))
6660 : 266 : itype = signed_type_for (itype);
6661 : 1728 : t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
6662 : 2498 : ? -1 : 1));
6663 : 1728 : t = fold_build2 (PLUS_EXPR, itype,
6664 : : fold_convert (itype, fd->loops[i].step), t);
6665 : 1728 : t = fold_build2 (PLUS_EXPR, itype, t,
6666 : : fold_convert (itype, fd->loops[i].n2));
6667 : 1728 : if (fd->loops[i].m2)
6668 : : {
6669 : 26 : tree t2 = fold_convert (itype,
6670 : : fd->loops[i - fd->loops[i].outer].v);
6671 : 26 : tree t3 = fold_convert (itype, fd->loops[i].m2);
6672 : 26 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
6673 : 26 : t = fold_build2 (PLUS_EXPR, itype, t, t2);
6674 : : }
6675 : 1728 : t = fold_build2 (MINUS_EXPR, itype, t,
6676 : : fold_convert (itype, fd->loops[i].v));
6677 : 1728 : if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
6678 : 256 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
6679 : : fold_build1 (NEGATE_EXPR, itype, t),
6680 : : fold_build1 (NEGATE_EXPR, itype,
6681 : : fold_convert (itype,
6682 : : fd->loops[i].step)));
6683 : : else
6684 : 1472 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
6685 : : fold_convert (itype, fd->loops[i].step));
6686 : 1728 : t = fold_convert (type, t);
6687 : 1728 : tree t2 = fold_build2 (MINUS_EXPR, type, n2, n1);
6688 : 1728 : min_arg1 = create_tmp_var (type);
6689 : 1728 : expand_omp_build_assign (&gsi, min_arg1, t2);
6690 : 1728 : min_arg2 = create_tmp_var (type);
6691 : 1728 : expand_omp_build_assign (&gsi, min_arg2, t);
6692 : : }
6693 : : else
6694 : : {
6695 : 474 : if (TREE_CODE (n2) == INTEGER_CST)
6696 : : {
6697 : : /* Indicate for lastprivate handling that at least one iteration
6698 : : has been performed, without wasting runtime. */
6699 : 136 : if (integer_nonzerop (n2))
6700 : 128 : expand_omp_build_assign (&gsi, fd->loop.v,
6701 : : fold_convert (type, n2));
6702 : : else
6703 : : /* Indicate that no iteration has been performed. */
6704 : 8 : expand_omp_build_assign (&gsi, fd->loop.v,
6705 : : build_one_cst (type));
6706 : : }
6707 : : else
6708 : : {
6709 : 338 : expand_omp_build_assign (&gsi, fd->loop.v,
6710 : : build_zero_cst (type));
6711 : 338 : expand_omp_build_assign (&gsi, n2, build_one_cst (type));
6712 : : }
6713 : 474 : for (i = 0; i < fd->collapse; i++)
6714 : : {
6715 : 474 : t = fold_convert (TREE_TYPE (fd->loops[i].v), fd->loops[i].n1);
6716 : 474 : if (fd->loops[i].m1)
6717 : : {
6718 : 0 : tree t2
6719 : 0 : = fold_convert (TREE_TYPE (t),
6720 : : fd->loops[i - fd->loops[i].outer].v);
6721 : 0 : tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i].m1);
6722 : 0 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
6723 : 0 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
6724 : : }
6725 : 474 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6726 : : /* For normal non-combined collapsed loops just initialize
6727 : : the outermost iterator in the entry_bb. */
6728 : 474 : if (!broken_loop)
6729 : : break;
6730 : : }
6731 : : }
6732 : : }
6733 : : else
6734 : 6609 : expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
6735 : 9347 : tree altv = NULL_TREE, altn2 = NULL_TREE;
6736 : 9347 : if (fd->collapse == 1
6737 : 6609 : && !broken_loop
6738 : 6073 : && TREE_CODE (orig_step) != INTEGER_CST)
6739 : : {
6740 : : /* The vectorizer currently punts on loops with non-constant steps
6741 : : for the main IV (can't compute number of iterations and gives up
6742 : : because of that). As for OpenMP loops it is always possible to
6743 : : compute the number of iterations upfront, use an alternate IV
6744 : : as the loop iterator:
6745 : : altn2 = n1 < n2 ? (n2 - n1 + step - 1) / step : 0;
6746 : : for (i = n1, altv = 0; altv < altn2; altv++, i += step) */
6747 : 176 : altv = create_tmp_var (unsigned_type_for (TREE_TYPE (fd->loops[0].v)));
6748 : 176 : expand_omp_build_assign (&gsi, altv, build_zero_cst (TREE_TYPE (altv)));
6749 : 176 : tree itype = TREE_TYPE (fd->loop.v);
6750 : 176 : if (POINTER_TYPE_P (itype))
6751 : 0 : itype = signed_type_for (itype);
6752 : 334 : t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
6753 : 176 : t = fold_build2 (PLUS_EXPR, itype,
6754 : : fold_convert (itype, step), t);
6755 : 176 : t = fold_build2 (PLUS_EXPR, itype, t, fold_convert (itype, n2));
6756 : 176 : t = fold_build2 (MINUS_EXPR, itype, t,
6757 : : fold_convert (itype, fd->loop.v));
6758 : 176 : if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
6759 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
6760 : : fold_build1 (NEGATE_EXPR, itype, t),
6761 : : fold_build1 (NEGATE_EXPR, itype,
6762 : : fold_convert (itype, step)));
6763 : : else
6764 : 176 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
6765 : : fold_convert (itype, step));
6766 : 176 : t = fold_convert (TREE_TYPE (altv), t);
6767 : 176 : altn2 = create_tmp_var (TREE_TYPE (altv));
6768 : 176 : expand_omp_build_assign (&gsi, altn2, t);
6769 : 176 : tree t2 = fold_convert (TREE_TYPE (fd->loop.v), n2);
6770 : 176 : t2 = fold_build2 (fd->loop.cond_code, boolean_type_node, fd->loop.v, t2);
6771 : 176 : t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
6772 : : true, GSI_SAME_STMT);
6773 : 176 : gassign *g = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
6774 : 176 : build_zero_cst (TREE_TYPE (altv)));
6775 : 176 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6776 : 176 : }
6777 : 9171 : else if (fd->collapse > 1
6778 : 2738 : && !broken_loop
6779 : 2202 : && !gimple_omp_for_combined_into_p (fd->for_stmt)
6780 : 9645 : && TREE_CODE (fd->loops[fd->collapse - 1].step) != INTEGER_CST)
6781 : : {
6782 : 22 : altv = create_tmp_var (unsigned_type_for (TREE_TYPE (fd->loops[0].v)));
6783 : 22 : altn2 = create_tmp_var (TREE_TYPE (altv));
6784 : : }
6785 : 9347 : if (cond_var)
6786 : : {
6787 : 77 : if (POINTER_TYPE_P (type)
6788 : 77 : || TREE_CODE (n1) != INTEGER_CST
6789 : 15 : || fd->loop.cond_code != LT_EXPR
6790 : 92 : || tree_int_cst_sgn (n1) != 1)
6791 : 72 : expand_omp_build_assign (&gsi, cond_var,
6792 : 72 : build_one_cst (TREE_TYPE (cond_var)));
6793 : : else
6794 : 5 : expand_omp_build_assign (&gsi, cond_var,
6795 : 5 : fold_convert (TREE_TYPE (cond_var), n1));
6796 : : }
6797 : :
6798 : : /* Remove the GIMPLE_OMP_FOR statement. */
6799 : 9347 : gsi_remove (&gsi, true);
6800 : :
6801 : 9347 : if (!broken_loop)
6802 : : {
6803 : : /* Code to control the increment goes in the CONT_BB. */
6804 : 8275 : gsi = gsi_last_nondebug_bb (cont_bb);
6805 : 8275 : stmt = gsi_stmt (gsi);
6806 : 8275 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_CONTINUE);
6807 : :
6808 : 8275 : if (fd->collapse == 1
6809 : 8275 : || gimple_omp_for_combined_into_p (fd->for_stmt))
6810 : : {
6811 : 7801 : if (POINTER_TYPE_P (type))
6812 : 286 : t = fold_build_pointer_plus (fd->loop.v, step);
6813 : : else
6814 : 7515 : t = fold_build2 (PLUS_EXPR, type, fd->loop.v, step);
6815 : 7801 : expand_omp_build_assign (&gsi, fd->loop.v, t);
6816 : : }
6817 : 474 : else if (TREE_CODE (n2) != INTEGER_CST)
6818 : 338 : expand_omp_build_assign (&gsi, fd->loop.v, build_one_cst (type));
6819 : 8275 : if (altv)
6820 : : {
6821 : 198 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (altv), altv,
6822 : : build_one_cst (TREE_TYPE (altv)));
6823 : 198 : expand_omp_build_assign (&gsi, altv, t);
6824 : : }
6825 : :
6826 : 8275 : if (fd->collapse > 1)
6827 : : {
6828 : 2202 : i = fd->collapse - 1;
6829 : 2202 : if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
6830 : 280 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
6831 : : else
6832 : : {
6833 : 1922 : t = fold_convert (TREE_TYPE (fd->loops[i].v),
6834 : : fd->loops[i].step);
6835 : 1922 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
6836 : : fd->loops[i].v, t);
6837 : : }
6838 : 2202 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6839 : : }
6840 : 8275 : if (cond_var)
6841 : : {
6842 : 77 : if (POINTER_TYPE_P (type)
6843 : 77 : || TREE_CODE (n1) != INTEGER_CST
6844 : 15 : || fd->loop.cond_code != LT_EXPR
6845 : 92 : || tree_int_cst_sgn (n1) != 1)
6846 : 72 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
6847 : : build_one_cst (TREE_TYPE (cond_var)));
6848 : : else
6849 : 5 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
6850 : : fold_convert (TREE_TYPE (cond_var), step));
6851 : 77 : expand_omp_build_assign (&gsi, cond_var, t);
6852 : : }
6853 : :
6854 : : /* Remove GIMPLE_OMP_CONTINUE. */
6855 : 8275 : gsi_remove (&gsi, true);
6856 : : }
6857 : :
6858 : : /* Emit the condition in L1_BB. */
6859 : 9347 : gsi = gsi_start_bb (l1_bb);
6860 : :
6861 : 9347 : if (altv)
6862 : 198 : t = build2 (LT_EXPR, boolean_type_node, altv, altn2);
6863 : 9149 : else if (fd->collapse > 1
6864 : 2716 : && !gimple_omp_for_combined_into_p (fd->for_stmt)
6865 : 9621 : && !broken_loop)
6866 : : {
6867 : 452 : i = fd->collapse - 1;
6868 : 452 : tree itype = TREE_TYPE (fd->loops[i].v);
6869 : 452 : if (fd->loops[i].m2)
6870 : 203 : t = n2v = create_tmp_var (itype);
6871 : : else
6872 : 249 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
6873 : 452 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6874 : : false, GSI_CONTINUE_LINKING);
6875 : 452 : tree v = fd->loops[i].v;
6876 : 452 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
6877 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
6878 : : false, GSI_CONTINUE_LINKING);
6879 : 452 : t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
6880 : : }
6881 : : else
6882 : : {
6883 : 8697 : if (fd->collapse > 1 && !broken_loop)
6884 : : t = n2var;
6885 : : else
6886 : 6969 : t = fold_convert (type, unshare_expr (n2));
6887 : 8697 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6888 : : false, GSI_CONTINUE_LINKING);
6889 : 8697 : tree v = fd->loop.v;
6890 : 8697 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
6891 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
6892 : : false, GSI_CONTINUE_LINKING);
6893 : 8697 : t = build2 (fd->loop.cond_code, boolean_type_node, v, t);
6894 : : }
6895 : 9347 : cond_stmt = gimple_build_cond_empty (t);
6896 : 9347 : gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
6897 : 9347 : if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
6898 : : NULL, NULL)
6899 : 9347 : || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p,
6900 : : NULL, NULL))
6901 : : {
6902 : 0 : gsi = gsi_for_stmt (cond_stmt);
6903 : 0 : gimple_regimplify_operands (cond_stmt, &gsi);
6904 : : }
6905 : :
6906 : : /* Add 'V -= STEP * (SIMT_VF - 1)' after the loop. */
6907 : 9347 : if (is_simt)
6908 : : {
6909 : 0 : gsi = gsi_start_bb (l2_bb);
6910 : 0 : step = fold_build2 (MINUS_EXPR, TREE_TYPE (step), orig_step, step);
6911 : 0 : if (POINTER_TYPE_P (type))
6912 : 0 : t = fold_build_pointer_plus (fd->loop.v, step);
6913 : : else
6914 : 0 : t = fold_build2 (PLUS_EXPR, type, fd->loop.v, step);
6915 : 0 : expand_omp_build_assign (&gsi, fd->loop.v, t);
6916 : : }
6917 : :
6918 : : /* Remove GIMPLE_OMP_RETURN. */
6919 : 9347 : gsi = gsi_last_nondebug_bb (exit_bb);
6920 : 9347 : gsi_remove (&gsi, true);
6921 : :
6922 : : /* Connect the new blocks. */
6923 : 9347 : remove_edge (FALLTHRU_EDGE (entry_bb));
6924 : :
6925 : 9347 : if (!broken_loop)
6926 : : {
6927 : 8275 : remove_edge (BRANCH_EDGE (entry_bb));
6928 : 8275 : make_edge (entry_bb, l1_bb, EDGE_FALLTHRU);
6929 : :
6930 : 8275 : e = BRANCH_EDGE (l1_bb);
6931 : 8275 : ne = FALLTHRU_EDGE (l1_bb);
6932 : 8275 : e->flags = EDGE_TRUE_VALUE;
6933 : : }
6934 : : else
6935 : : {
6936 : 1072 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
6937 : :
6938 : 1072 : ne = single_succ_edge (l1_bb);
6939 : 1072 : e = make_edge (l1_bb, l0_bb, EDGE_TRUE_VALUE);
6940 : :
6941 : : }
6942 : 9347 : ne->flags = EDGE_FALSE_VALUE;
6943 : 9347 : e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
6944 : 9347 : ne->probability = e->probability.invert ();
6945 : :
6946 : 9347 : set_immediate_dominator (CDI_DOMINATORS, l1_bb, entry_bb);
6947 : 9347 : set_immediate_dominator (CDI_DOMINATORS, l0_bb, l1_bb);
6948 : :
6949 : 9347 : if (simt_maxlane)
6950 : : {
6951 : 0 : cond_stmt = gimple_build_cond (LT_EXPR, simt_lane, simt_maxlane,
6952 : : NULL_TREE, NULL_TREE);
6953 : 0 : gsi = gsi_last_bb (entry_bb);
6954 : 0 : gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
6955 : 0 : make_edge (entry_bb, l2_bb, EDGE_FALSE_VALUE);
6956 : 0 : FALLTHRU_EDGE (entry_bb)->flags = EDGE_TRUE_VALUE;
6957 : 0 : FALLTHRU_EDGE (entry_bb)->probability
6958 : 0 : = profile_probability::guessed_always ().apply_scale (7, 8);
6959 : 0 : BRANCH_EDGE (entry_bb)->probability
6960 : 0 : = FALLTHRU_EDGE (entry_bb)->probability.invert ();
6961 : 0 : l2_dom_bb = entry_bb;
6962 : : }
6963 : 9347 : set_immediate_dominator (CDI_DOMINATORS, l2_bb, l2_dom_bb);
6964 : :
6965 : 9347 : if (!broken_loop && fd->collapse > 1)
6966 : : {
6967 : 2202 : basic_block last_bb = l1_bb;
6968 : 2202 : basic_block init_bb = NULL;
6969 : 6078 : for (i = fd->collapse - 2; i >= 0; i--)
6970 : : {
6971 : 3876 : tree nextn2v = NULL_TREE;
6972 : 3876 : if (EDGE_SUCC (last_bb, 0)->flags & EDGE_FALSE_VALUE)
6973 : : e = EDGE_SUCC (last_bb, 0);
6974 : : else
6975 : 2202 : e = EDGE_SUCC (last_bb, 1);
6976 : 3876 : basic_block bb = split_edge (e);
6977 : 3876 : if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
6978 : 553 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
6979 : : else
6980 : : {
6981 : 3323 : t = fold_convert (TREE_TYPE (fd->loops[i].v),
6982 : : fd->loops[i].step);
6983 : 3323 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
6984 : : fd->loops[i].v, t);
6985 : : }
6986 : 3876 : gsi = gsi_after_labels (bb);
6987 : 3876 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6988 : :
6989 : 3876 : bb = split_block (bb, last_nondebug_stmt (bb))->dest;
6990 : 3876 : gsi = gsi_start_bb (bb);
6991 : 3876 : tree itype = TREE_TYPE (fd->loops[i].v);
6992 : 3876 : if (fd->loops[i].m2)
6993 : 0 : t = nextn2v = create_tmp_var (itype);
6994 : : else
6995 : 3876 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
6996 : 3876 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6997 : : false, GSI_CONTINUE_LINKING);
6998 : 3876 : tree v = fd->loops[i].v;
6999 : 3876 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
7000 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
7001 : : false, GSI_CONTINUE_LINKING);
7002 : 3876 : t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
7003 : 3876 : cond_stmt = gimple_build_cond_empty (t);
7004 : 3876 : gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
7005 : 3876 : if (walk_tree (gimple_cond_lhs_ptr (cond_stmt),
7006 : : expand_omp_regimplify_p, NULL, NULL)
7007 : 3876 : || walk_tree (gimple_cond_rhs_ptr (cond_stmt),
7008 : : expand_omp_regimplify_p, NULL, NULL))
7009 : : {
7010 : 6 : gsi = gsi_for_stmt (cond_stmt);
7011 : 6 : gimple_regimplify_operands (cond_stmt, &gsi);
7012 : : }
7013 : 3876 : ne = single_succ_edge (bb);
7014 : 3876 : ne->flags = EDGE_FALSE_VALUE;
7015 : :
7016 : 3876 : init_bb = create_empty_bb (bb);
7017 : 3876 : set_immediate_dominator (CDI_DOMINATORS, init_bb, bb);
7018 : 3876 : add_bb_to_loop (init_bb, bb->loop_father);
7019 : 3876 : e = make_edge (bb, init_bb, EDGE_TRUE_VALUE);
7020 : 3876 : e->probability
7021 : 3876 : = profile_probability::guessed_always ().apply_scale (7, 8);
7022 : 3876 : ne->probability = e->probability.invert ();
7023 : :
7024 : 3876 : gsi = gsi_after_labels (init_bb);
7025 : 3876 : if (fd->loops[i + 1].m1)
7026 : : {
7027 : 242 : tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7028 : : fd->loops[i + 1
7029 : : - fd->loops[i + 1].outer].v);
7030 : 242 : if (POINTER_TYPE_P (TREE_TYPE (t2)))
7031 : 6 : t = fold_build_pointer_plus (t2, fd->loops[i + 1].n1);
7032 : : else
7033 : : {
7034 : 236 : t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7035 : : fd->loops[i + 1].n1);
7036 : 236 : tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m1);
7037 : 236 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
7038 : 236 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
7039 : : }
7040 : : }
7041 : : else
7042 : 3634 : t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7043 : : fd->loops[i + 1].n1);
7044 : 3876 : expand_omp_build_assign (&gsi, fd->loops[i + 1].v, t);
7045 : 3876 : if (fd->loops[i + 1].m2)
7046 : : {
7047 : 241 : if (i + 2 == fd->collapse && (n2var || altv))
7048 : : {
7049 : 38 : gcc_assert (n2v == NULL_TREE);
7050 : 38 : n2v = create_tmp_var (TREE_TYPE (fd->loops[i + 1].v));
7051 : : }
7052 : 241 : tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7053 : : fd->loops[i + 1
7054 : : - fd->loops[i + 1].outer].v);
7055 : 241 : if (POINTER_TYPE_P (TREE_TYPE (t2)))
7056 : 6 : t = fold_build_pointer_plus (t2, fd->loops[i + 1].n2);
7057 : : else
7058 : : {
7059 : 235 : t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7060 : : fd->loops[i + 1].n2);
7061 : 235 : tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m2);
7062 : 235 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
7063 : 235 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
7064 : : }
7065 : 241 : expand_omp_build_assign (&gsi, n2v, t);
7066 : : }
7067 : 3876 : if (i + 2 == fd->collapse && n2var)
7068 : : {
7069 : : /* For composite simd, n2 is the first iteration the current
7070 : : task shouldn't already handle, so we effectively want to use
7071 : : for (V3 = N31; V < N2 && V3 < N32; V++, V3 += STEP3)
7072 : : as the vectorized loop. Except the vectorizer will not
7073 : : vectorize that, so instead compute N2VAR as
7074 : : N2VAR = V + MIN (N2 - V, COUNTS3) and use
7075 : : for (V3 = N31; V < N2VAR; V++, V3 += STEP3)
7076 : : as the loop to vectorize. */
7077 : 1728 : tree t2 = fold_build2 (MINUS_EXPR, type, n2, fd->loop.v);
7078 : 1728 : if (fd->loops[i + 1].m1 || fd->loops[i + 1].m2)
7079 : : {
7080 : 99 : tree itype = TREE_TYPE (fd->loops[i].v);
7081 : 99 : if (POINTER_TYPE_P (itype))
7082 : 4 : itype = signed_type_for (itype);
7083 : 99 : t = build_int_cst (itype, (fd->loops[i + 1].cond_code
7084 : 99 : == LT_EXPR ? -1 : 1));
7085 : 99 : t = fold_build2 (PLUS_EXPR, itype,
7086 : : fold_convert (itype,
7087 : : fd->loops[i + 1].step), t);
7088 : 99 : if (fd->loops[i + 1].m2 == NULL_TREE)
7089 : 73 : t = fold_build2 (PLUS_EXPR, itype, t,
7090 : : fold_convert (itype,
7091 : : fd->loops[i + 1].n2));
7092 : 26 : else if (POINTER_TYPE_P (TREE_TYPE (n2v)))
7093 : : {
7094 : 4 : t = fold_build_pointer_plus (n2v, t);
7095 : 4 : t = fold_convert (itype, t);
7096 : : }
7097 : : else
7098 : 22 : t = fold_build2 (PLUS_EXPR, itype, t, n2v);
7099 : 99 : t = fold_build2 (MINUS_EXPR, itype, t,
7100 : : fold_convert (itype, fd->loops[i + 1].v));
7101 : 99 : tree step = fold_convert (itype, fd->loops[i + 1].step);
7102 : 99 : if (TYPE_UNSIGNED (itype)
7103 : 99 : && fd->loops[i + 1].cond_code == GT_EXPR)
7104 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
7105 : : fold_build1 (NEGATE_EXPR, itype, t),
7106 : : fold_build1 (NEGATE_EXPR, itype, step));
7107 : : else
7108 : 99 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
7109 : 99 : t = fold_convert (type, t);
7110 : 99 : }
7111 : : else
7112 : 1629 : t = counts[i + 1];
7113 : 1728 : expand_omp_build_assign (&gsi, min_arg1, t2);
7114 : 1728 : expand_omp_build_assign (&gsi, min_arg2, t);
7115 : 1728 : e = split_block (init_bb, last_nondebug_stmt (init_bb));
7116 : 1728 : gsi = gsi_after_labels (e->dest);
7117 : 1728 : init_bb = e->dest;
7118 : 1728 : remove_edge (FALLTHRU_EDGE (entry_bb));
7119 : 1728 : make_edge (entry_bb, init_bb, EDGE_FALLTHRU);
7120 : 1728 : set_immediate_dominator (CDI_DOMINATORS, init_bb, entry_bb);
7121 : 1728 : set_immediate_dominator (CDI_DOMINATORS, l1_bb, init_bb);
7122 : 1728 : t = fold_build2 (MIN_EXPR, type, min_arg1, min_arg2);
7123 : 1728 : t = fold_build2 (PLUS_EXPR, type, fd->loop.v, t);
7124 : 1728 : expand_omp_build_assign (&gsi, n2var, t);
7125 : : }
7126 : 3876 : if (i + 2 == fd->collapse && altv)
7127 : : {
7128 : : /* The vectorizer currently punts on loops with non-constant
7129 : : steps for the main IV (can't compute number of iterations
7130 : : and gives up because of that). As for OpenMP loops it is
7131 : : always possible to compute the number of iterations upfront,
7132 : : use an alternate IV as the loop iterator. */
7133 : 22 : expand_omp_build_assign (&gsi, altv,
7134 : 22 : build_zero_cst (TREE_TYPE (altv)));
7135 : 22 : tree itype = TREE_TYPE (fd->loops[i + 1].v);
7136 : 22 : if (POINTER_TYPE_P (itype))
7137 : 0 : itype = signed_type_for (itype);
7138 : 22 : t = build_int_cst (itype, (fd->loops[i + 1].cond_code == LT_EXPR
7139 : 26 : ? -1 : 1));
7140 : 22 : t = fold_build2 (PLUS_EXPR, itype,
7141 : : fold_convert (itype, fd->loops[i + 1].step), t);
7142 : 22 : t = fold_build2 (PLUS_EXPR, itype, t,
7143 : : fold_convert (itype,
7144 : : fd->loops[i + 1].m2
7145 : : ? n2v : fd->loops[i + 1].n2));
7146 : 22 : t = fold_build2 (MINUS_EXPR, itype, t,
7147 : : fold_convert (itype, fd->loops[i + 1].v));
7148 : 22 : tree step = fold_convert (itype, fd->loops[i + 1].step);
7149 : 22 : if (TYPE_UNSIGNED (itype)
7150 : 22 : && fd->loops[i + 1].cond_code == GT_EXPR)
7151 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
7152 : : fold_build1 (NEGATE_EXPR, itype, t),
7153 : : fold_build1 (NEGATE_EXPR, itype, step));
7154 : : else
7155 : 22 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
7156 : 22 : t = fold_convert (TREE_TYPE (altv), t);
7157 : 22 : expand_omp_build_assign (&gsi, altn2, t);
7158 : 22 : tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7159 : : fd->loops[i + 1].m2
7160 : : ? n2v : fd->loops[i + 1].n2);
7161 : 22 : t2 = fold_build2 (fd->loops[i + 1].cond_code, boolean_type_node,
7162 : : fd->loops[i + 1].v, t2);
7163 : 22 : t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
7164 : : true, GSI_SAME_STMT);
7165 : 22 : gassign *g
7166 : 22 : = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
7167 : 22 : build_zero_cst (TREE_TYPE (altv)));
7168 : 22 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
7169 : : }
7170 : 3876 : n2v = nextn2v;
7171 : :
7172 : 3876 : make_edge (init_bb, last_bb, EDGE_FALLTHRU);
7173 : 3876 : if (!gimple_omp_for_combined_into_p (fd->for_stmt))
7174 : : {
7175 : 785 : e = find_edge (entry_bb, last_bb);
7176 : 785 : redirect_edge_succ (e, bb);
7177 : 785 : set_immediate_dominator (CDI_DOMINATORS, bb, entry_bb);
7178 : 785 : set_immediate_dominator (CDI_DOMINATORS, last_bb, init_bb);
7179 : : }
7180 : :
7181 : 3876 : last_bb = bb;
7182 : : }
7183 : : }
7184 : 9347 : if (!broken_loop)
7185 : : {
7186 : 8275 : class loop *loop = alloc_loop ();
7187 : 8275 : loop->header = l1_bb;
7188 : 8275 : loop->latch = cont_bb;
7189 : 8275 : add_loop (loop, l1_bb->loop_father);
7190 : 8275 : loop->safelen = safelen_int;
7191 : 8275 : if (simduid)
7192 : : {
7193 : 3476 : loop->simduid = OMP_CLAUSE__SIMDUID__DECL (simduid);
7194 : 3476 : cfun->has_simduid_loops = true;
7195 : : }
7196 : : /* If not -fno-tree-loop-vectorize, hint that we want to vectorize
7197 : : the loop. */
7198 : 8275 : if ((flag_tree_loop_vectorize
7199 : 3279 : || !OPTION_SET_P (flag_tree_loop_vectorize))
7200 : 8275 : && flag_tree_loop_optimize
7201 : 8274 : && loop->safelen > 1)
7202 : : {
7203 : 6496 : loop->force_vectorize = true;
7204 : 6496 : if (simdlen && tree_fits_uhwi_p (OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
7205 : : {
7206 : 60 : unsigned HOST_WIDE_INT v
7207 : 60 : = tree_to_uhwi (OMP_CLAUSE_SIMDLEN_EXPR (simdlen));
7208 : 60 : if (v < INT_MAX && v <= (unsigned HOST_WIDE_INT) loop->safelen)
7209 : 60 : loop->simdlen = v;
7210 : : }
7211 : 6496 : cfun->has_force_vectorize_loops = true;
7212 : 6496 : }
7213 : 1779 : else if (dont_vectorize)
7214 : 234 : loop->dont_vectorize = true;
7215 : : }
7216 : 1072 : else if (simduid)
7217 : 532 : cfun->has_simduid_loops = true;
7218 : 9347 : }
7219 : :
7220 : : /* Taskloop construct is represented after gimplification with
7221 : : two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
7222 : : in between them. This routine expands the outer GIMPLE_OMP_FOR,
7223 : : which should just compute all the needed loop temporaries
7224 : : for GIMPLE_OMP_TASK. */
7225 : :
7226 : : static void
7227 : 1326 : expand_omp_taskloop_for_outer (struct omp_region *region,
7228 : : struct omp_for_data *fd,
7229 : : gimple *inner_stmt)
7230 : : {
7231 : 1326 : tree type, bias = NULL_TREE;
7232 : 1326 : basic_block entry_bb, cont_bb, exit_bb;
7233 : 1326 : gimple_stmt_iterator gsi;
7234 : 1326 : gassign *assign_stmt;
7235 : 1326 : tree *counts = NULL;
7236 : 1326 : int i;
7237 : :
7238 : 1326 : gcc_assert (inner_stmt);
7239 : 1326 : gcc_assert (region->cont);
7240 : 1326 : gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_TASK
7241 : : && gimple_omp_task_taskloop_p (inner_stmt));
7242 : 1326 : type = TREE_TYPE (fd->loop.v);
7243 : :
7244 : : /* See if we need to bias by LLONG_MIN. */
7245 : 1326 : if (fd->iter_type == long_long_unsigned_type_node
7246 : 42 : && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
7247 : 1354 : && !TYPE_UNSIGNED (type))
7248 : : {
7249 : 0 : tree n1, n2;
7250 : :
7251 : 0 : if (fd->loop.cond_code == LT_EXPR)
7252 : : {
7253 : 0 : n1 = fd->loop.n1;
7254 : 0 : n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
7255 : : }
7256 : : else
7257 : : {
7258 : 0 : n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
7259 : 0 : n2 = fd->loop.n1;
7260 : : }
7261 : 0 : if (TREE_CODE (n1) != INTEGER_CST
7262 : 0 : || TREE_CODE (n2) != INTEGER_CST
7263 : 0 : || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
7264 : 0 : bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
7265 : : }
7266 : :
7267 : 1326 : entry_bb = region->entry;
7268 : 1326 : cont_bb = region->cont;
7269 : 1326 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
7270 : 1326 : gcc_assert (BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
7271 : 1326 : exit_bb = region->exit;
7272 : :
7273 : 1326 : gsi = gsi_last_nondebug_bb (entry_bb);
7274 : 1326 : gimple *for_stmt = gsi_stmt (gsi);
7275 : 1326 : gcc_assert (gimple_code (for_stmt) == GIMPLE_OMP_FOR);
7276 : 1326 : if (fd->collapse > 1)
7277 : : {
7278 : 181 : int first_zero_iter = -1, dummy = -1;
7279 : 181 : basic_block zero_iter_bb = NULL, dummy_bb = NULL, l2_dom_bb = NULL;
7280 : :
7281 : 181 : counts = XALLOCAVEC (tree, fd->collapse);
7282 : 181 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
7283 : : zero_iter_bb, first_zero_iter,
7284 : : dummy_bb, dummy, l2_dom_bb);
7285 : :
7286 : 181 : if (zero_iter_bb)
7287 : : {
7288 : : /* Some counts[i] vars might be uninitialized if
7289 : : some loop has zero iterations. But the body shouldn't
7290 : : be executed in that case, so just avoid uninit warnings. */
7291 : 336 : for (i = first_zero_iter; i < fd->collapse; i++)
7292 : 239 : if (SSA_VAR_P (counts[i]))
7293 : 226 : suppress_warning (counts[i], OPT_Wuninitialized);
7294 : 97 : gsi_prev (&gsi);
7295 : 97 : edge e = split_block (entry_bb, gsi_stmt (gsi));
7296 : 97 : entry_bb = e->dest;
7297 : 97 : make_edge (zero_iter_bb, entry_bb, EDGE_FALLTHRU);
7298 : 97 : gsi = gsi_last_bb (entry_bb);
7299 : 97 : set_immediate_dominator (CDI_DOMINATORS, entry_bb,
7300 : : get_immediate_dominator (CDI_DOMINATORS,
7301 : : zero_iter_bb));
7302 : : }
7303 : : }
7304 : :
7305 : 1326 : tree t0, t1;
7306 : 1326 : t1 = fd->loop.n2;
7307 : 1326 : t0 = fd->loop.n1;
7308 : 2638 : if (POINTER_TYPE_P (TREE_TYPE (t0))
7309 : 1326 : && TYPE_PRECISION (TREE_TYPE (t0))
7310 : 14 : != TYPE_PRECISION (fd->iter_type))
7311 : : {
7312 : : /* Avoid casting pointers to integer of a different size. */
7313 : 0 : tree itype = signed_type_for (type);
7314 : 0 : t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
7315 : 0 : t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
7316 : : }
7317 : : else
7318 : : {
7319 : 1326 : t1 = fold_convert (fd->iter_type, t1);
7320 : 1326 : t0 = fold_convert (fd->iter_type, t0);
7321 : : }
7322 : 1326 : if (bias)
7323 : : {
7324 : 0 : t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
7325 : 0 : t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
7326 : : }
7327 : :
7328 : 1326 : tree innerc = omp_find_clause (gimple_omp_task_clauses (inner_stmt),
7329 : : OMP_CLAUSE__LOOPTEMP_);
7330 : 1326 : gcc_assert (innerc);
7331 : 1326 : tree startvar = OMP_CLAUSE_DECL (innerc);
7332 : 1326 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
7333 : 1326 : gcc_assert (innerc);
7334 : 1326 : tree endvar = OMP_CLAUSE_DECL (innerc);
7335 : 1326 : if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST)
7336 : : {
7337 : 101 : innerc = find_lastprivate_looptemp (fd, innerc);
7338 : 101 : if (innerc)
7339 : : {
7340 : : /* If needed (inner taskloop has lastprivate clause), propagate
7341 : : down the total number of iterations. */
7342 : 31 : tree t = force_gimple_operand_gsi (&gsi, fd->loop.n2, false,
7343 : : NULL_TREE, false,
7344 : : GSI_CONTINUE_LINKING);
7345 : 31 : assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
7346 : 31 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7347 : : }
7348 : : }
7349 : :
7350 : 1326 : t0 = force_gimple_operand_gsi (&gsi, t0, false, NULL_TREE, false,
7351 : : GSI_CONTINUE_LINKING);
7352 : 1326 : assign_stmt = gimple_build_assign (startvar, t0);
7353 : 1326 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7354 : :
7355 : 1326 : t1 = force_gimple_operand_gsi (&gsi, t1, false, NULL_TREE, false,
7356 : : GSI_CONTINUE_LINKING);
7357 : 1326 : assign_stmt = gimple_build_assign (endvar, t1);
7358 : 1326 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7359 : 1326 : if (fd->collapse > 1)
7360 : 181 : expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
7361 : :
7362 : : /* Remove the GIMPLE_OMP_FOR statement. */
7363 : 1326 : gsi = gsi_for_stmt (for_stmt);
7364 : 1326 : gsi_remove (&gsi, true);
7365 : :
7366 : 1326 : gsi = gsi_last_nondebug_bb (cont_bb);
7367 : 1326 : gsi_remove (&gsi, true);
7368 : :
7369 : 1326 : gsi = gsi_last_nondebug_bb (exit_bb);
7370 : 1326 : gsi_remove (&gsi, true);
7371 : :
7372 : 1326 : FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
7373 : 1326 : remove_edge (BRANCH_EDGE (entry_bb));
7374 : 1326 : FALLTHRU_EDGE (cont_bb)->probability = profile_probability::always ();
7375 : 1326 : remove_edge (BRANCH_EDGE (cont_bb));
7376 : 1326 : set_immediate_dominator (CDI_DOMINATORS, exit_bb, cont_bb);
7377 : 1326 : set_immediate_dominator (CDI_DOMINATORS, region->entry,
7378 : : recompute_dominator (CDI_DOMINATORS, region->entry));
7379 : 1326 : }
7380 : :
7381 : : /* Taskloop construct is represented after gimplification with
7382 : : two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
7383 : : in between them. This routine expands the inner GIMPLE_OMP_FOR.
7384 : : GOMP_taskloop{,_ull} function arranges for each task to be given just
7385 : : a single range of iterations. */
7386 : :
7387 : : static void
7388 : 1326 : expand_omp_taskloop_for_inner (struct omp_region *region,
7389 : : struct omp_for_data *fd,
7390 : : gimple *inner_stmt)
7391 : : {
7392 : 1326 : tree e, t, type, itype, vmain, vback, bias = NULL_TREE;
7393 : 1326 : basic_block entry_bb, exit_bb, body_bb, cont_bb, collapse_bb = NULL;
7394 : 1326 : basic_block fin_bb;
7395 : 1326 : gimple_stmt_iterator gsi;
7396 : 1326 : edge ep;
7397 : 1326 : bool broken_loop = region->cont == NULL;
7398 : 1326 : tree *counts = NULL;
7399 : 1326 : tree n1, n2, step;
7400 : :
7401 : 1326 : itype = type = TREE_TYPE (fd->loop.v);
7402 : 1326 : if (POINTER_TYPE_P (type))
7403 : 14 : itype = signed_type_for (type);
7404 : :
7405 : : /* See if we need to bias by LLONG_MIN. */
7406 : 1326 : if (fd->iter_type == long_long_unsigned_type_node
7407 : 42 : && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
7408 : 1354 : && !TYPE_UNSIGNED (type))
7409 : : {
7410 : 0 : tree n1, n2;
7411 : :
7412 : 0 : if (fd->loop.cond_code == LT_EXPR)
7413 : : {
7414 : 0 : n1 = fd->loop.n1;
7415 : 0 : n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
7416 : : }
7417 : : else
7418 : : {
7419 : 0 : n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
7420 : 0 : n2 = fd->loop.n1;
7421 : : }
7422 : 0 : if (TREE_CODE (n1) != INTEGER_CST
7423 : 0 : || TREE_CODE (n2) != INTEGER_CST
7424 : 0 : || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
7425 : 0 : bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
7426 : : }
7427 : :
7428 : 1326 : entry_bb = region->entry;
7429 : 1326 : cont_bb = region->cont;
7430 : 1326 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
7431 : 1326 : fin_bb = BRANCH_EDGE (entry_bb)->dest;
7432 : 1326 : gcc_assert (broken_loop
7433 : : || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
7434 : 1326 : body_bb = FALLTHRU_EDGE (entry_bb)->dest;
7435 : 1326 : if (!broken_loop)
7436 : : {
7437 : 1310 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb);
7438 : 1310 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
7439 : : }
7440 : 1326 : exit_bb = region->exit;
7441 : :
7442 : : /* Iteration space partitioning goes in ENTRY_BB. */
7443 : 1326 : gsi = gsi_last_nondebug_bb (entry_bb);
7444 : 1326 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
7445 : :
7446 : 1326 : if (fd->collapse > 1)
7447 : : {
7448 : 181 : int first_zero_iter = -1, dummy = -1;
7449 : 181 : basic_block l2_dom_bb = NULL, dummy_bb = NULL;
7450 : :
7451 : 181 : counts = XALLOCAVEC (tree, fd->collapse);
7452 : 181 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
7453 : : fin_bb, first_zero_iter,
7454 : : dummy_bb, dummy, l2_dom_bb);
7455 : 181 : t = NULL_TREE;
7456 : : }
7457 : : else
7458 : 1326 : t = integer_one_node;
7459 : :
7460 : 1326 : step = fd->loop.step;
7461 : 1326 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
7462 : : OMP_CLAUSE__LOOPTEMP_);
7463 : 1326 : gcc_assert (innerc);
7464 : 1326 : n1 = OMP_CLAUSE_DECL (innerc);
7465 : 1326 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
7466 : 1326 : gcc_assert (innerc);
7467 : 1326 : n2 = OMP_CLAUSE_DECL (innerc);
7468 : 1326 : if (bias)
7469 : : {
7470 : 0 : n1 = fold_build2 (PLUS_EXPR, fd->iter_type, n1, bias);
7471 : 0 : n2 = fold_build2 (PLUS_EXPR, fd->iter_type, n2, bias);
7472 : : }
7473 : 1326 : n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
7474 : : true, NULL_TREE, true, GSI_SAME_STMT);
7475 : 1326 : n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
7476 : : true, NULL_TREE, true, GSI_SAME_STMT);
7477 : 1326 : step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
7478 : : true, NULL_TREE, true, GSI_SAME_STMT);
7479 : :
7480 : 1326 : tree startvar = fd->loop.v;
7481 : 1326 : tree endvar = NULL_TREE;
7482 : :
7483 : 1326 : if (gimple_omp_for_combined_p (fd->for_stmt))
7484 : : {
7485 : 617 : tree clauses = gimple_omp_for_clauses (inner_stmt);
7486 : 617 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
7487 : 617 : gcc_assert (innerc);
7488 : 617 : startvar = OMP_CLAUSE_DECL (innerc);
7489 : 617 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
7490 : : OMP_CLAUSE__LOOPTEMP_);
7491 : 617 : gcc_assert (innerc);
7492 : 617 : endvar = OMP_CLAUSE_DECL (innerc);
7493 : : }
7494 : 1326 : t = fold_convert (TREE_TYPE (startvar), n1);
7495 : 1326 : t = force_gimple_operand_gsi (&gsi, t,
7496 : 1326 : DECL_P (startvar)
7497 : 1326 : && TREE_ADDRESSABLE (startvar),
7498 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
7499 : 1326 : gimple *assign_stmt = gimple_build_assign (startvar, t);
7500 : 1326 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7501 : :
7502 : 1326 : t = fold_convert (TREE_TYPE (startvar), n2);
7503 : 1326 : e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
7504 : : false, GSI_CONTINUE_LINKING);
7505 : 1326 : if (endvar)
7506 : : {
7507 : 617 : assign_stmt = gimple_build_assign (endvar, e);
7508 : 617 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7509 : 617 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
7510 : 529 : assign_stmt = gimple_build_assign (fd->loop.v, e);
7511 : : else
7512 : 88 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
7513 : 617 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7514 : : }
7515 : :
7516 : 1326 : tree *nonrect_bounds = NULL;
7517 : 1326 : if (fd->collapse > 1)
7518 : : {
7519 : 181 : if (fd->non_rect)
7520 : : {
7521 : 19 : nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
7522 : 19 : memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
7523 : : }
7524 : 181 : gcc_assert (gsi_bb (gsi) == entry_bb);
7525 : 181 : expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
7526 : : startvar);
7527 : 181 : entry_bb = gsi_bb (gsi);
7528 : : }
7529 : :
7530 : 1326 : if (!broken_loop)
7531 : : {
7532 : : /* The code controlling the sequential loop replaces the
7533 : : GIMPLE_OMP_CONTINUE. */
7534 : 1310 : gsi = gsi_last_nondebug_bb (cont_bb);
7535 : 1310 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
7536 : 1310 : gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
7537 : 1310 : vmain = gimple_omp_continue_control_use (cont_stmt);
7538 : 1310 : vback = gimple_omp_continue_control_def (cont_stmt);
7539 : :
7540 : 1310 : if (!gimple_omp_for_combined_p (fd->for_stmt))
7541 : : {
7542 : 693 : if (POINTER_TYPE_P (type))
7543 : 8 : t = fold_build_pointer_plus (vmain, step);
7544 : : else
7545 : 685 : t = fold_build2 (PLUS_EXPR, type, vmain, step);
7546 : 693 : t = force_gimple_operand_gsi (&gsi, t,
7547 : 693 : DECL_P (vback)
7548 : 693 : && TREE_ADDRESSABLE (vback),
7549 : : NULL_TREE, true, GSI_SAME_STMT);
7550 : 693 : assign_stmt = gimple_build_assign (vback, t);
7551 : 693 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
7552 : :
7553 : 693 : t = build2 (fd->loop.cond_code, boolean_type_node,
7554 : 693 : DECL_P (vback) && TREE_ADDRESSABLE (vback)
7555 : : ? t : vback, e);
7556 : 693 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
7557 : : }
7558 : :
7559 : : /* Remove the GIMPLE_OMP_CONTINUE statement. */
7560 : 1310 : gsi_remove (&gsi, true);
7561 : :
7562 : 1310 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
7563 : 81 : collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
7564 : : cont_bb, body_bb);
7565 : : }
7566 : :
7567 : : /* Remove the GIMPLE_OMP_FOR statement. */
7568 : 1326 : gsi = gsi_for_stmt (fd->for_stmt);
7569 : 1326 : gsi_remove (&gsi, true);
7570 : :
7571 : : /* Remove the GIMPLE_OMP_RETURN statement. */
7572 : 1326 : gsi = gsi_last_nondebug_bb (exit_bb);
7573 : 1326 : gsi_remove (&gsi, true);
7574 : :
7575 : 1326 : FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
7576 : 1326 : if (!broken_loop)
7577 : 1310 : remove_edge (BRANCH_EDGE (entry_bb));
7578 : : else
7579 : : {
7580 : 16 : remove_edge_and_dominated_blocks (BRANCH_EDGE (entry_bb));
7581 : 16 : region->outer->cont = NULL;
7582 : : }
7583 : :
7584 : : /* Connect all the blocks. */
7585 : 1326 : if (!broken_loop)
7586 : : {
7587 : 1310 : ep = find_edge (cont_bb, body_bb);
7588 : 1310 : if (gimple_omp_for_combined_p (fd->for_stmt))
7589 : : {
7590 : 617 : remove_edge (ep);
7591 : 617 : ep = NULL;
7592 : : }
7593 : 693 : else if (fd->collapse > 1)
7594 : : {
7595 : 81 : remove_edge (ep);
7596 : 81 : ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
7597 : : }
7598 : : else
7599 : 612 : ep->flags = EDGE_TRUE_VALUE;
7600 : 2539 : find_edge (cont_bb, fin_bb)->flags
7601 : 1391 : = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
7602 : : }
7603 : :
7604 : 1326 : set_immediate_dominator (CDI_DOMINATORS, body_bb,
7605 : : recompute_dominator (CDI_DOMINATORS, body_bb));
7606 : 1326 : if (!broken_loop)
7607 : 1310 : set_immediate_dominator (CDI_DOMINATORS, fin_bb,
7608 : : recompute_dominator (CDI_DOMINATORS, fin_bb));
7609 : :
7610 : 1310 : if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
7611 : : {
7612 : 693 : class loop *loop = alloc_loop ();
7613 : 693 : loop->header = body_bb;
7614 : 693 : if (collapse_bb == NULL)
7615 : 612 : loop->latch = cont_bb;
7616 : 693 : add_loop (loop, body_bb->loop_father);
7617 : : }
7618 : 1326 : }
7619 : :
7620 : : /* A subroutine of expand_omp_for. Generate code for an OpenACC
7621 : : partitioned loop. The lowering here is abstracted, in that the
7622 : : loop parameters are passed through internal functions, which are
7623 : : further lowered by oacc_device_lower, once we get to the target
7624 : : compiler. The loop is of the form:
7625 : :
7626 : : for (V = B; V LTGT E; V += S) {BODY}
7627 : :
7628 : : where LTGT is < or >. We may have a specified chunking size, CHUNKING
7629 : : (constant 0 for no chunking) and we will have a GWV partitioning
7630 : : mask, specifying dimensions over which the loop is to be
7631 : : partitioned (see note below). We generate code that looks like
7632 : : (this ignores tiling):
7633 : :
7634 : : <entry_bb> [incoming FALL->body, BRANCH->exit]
7635 : : typedef signedintify (typeof (V)) T; // underlying signed integral type
7636 : : T range = E - B;
7637 : : T chunk_no = 0;
7638 : : T DIR = LTGT == '<' ? +1 : -1;
7639 : : T chunk_max = GOACC_LOOP_CHUNK (dir, range, S, CHUNK_SIZE, GWV);
7640 : : T step = GOACC_LOOP_STEP (dir, range, S, CHUNK_SIZE, GWV);
7641 : :
7642 : : <head_bb> [created by splitting end of entry_bb]
7643 : : T offset = GOACC_LOOP_OFFSET (dir, range, S, CHUNK_SIZE, GWV, chunk_no);
7644 : : T bound = GOACC_LOOP_BOUND (dir, range, S, CHUNK_SIZE, GWV, offset);
7645 : : if (!(offset LTGT bound)) goto bottom_bb;
7646 : :
7647 : : <body_bb> [incoming]
7648 : : V = B + offset;
7649 : : {BODY}
7650 : :
7651 : : <cont_bb> [incoming, may == body_bb FALL->exit_bb, BRANCH->body_bb]
7652 : : offset += step;
7653 : : if (offset LTGT bound) goto body_bb; [*]
7654 : :
7655 : : <bottom_bb> [created by splitting start of exit_bb] insert BRANCH->head_bb
7656 : : chunk_no++;
7657 : : if (chunk < chunk_max) goto head_bb;
7658 : :
7659 : : <exit_bb> [incoming]
7660 : : V = B + ((range -/+ 1) / S +/- 1) * S [*]
7661 : :
7662 : : [*] Needed if V live at end of loop. */
7663 : :
7664 : : static void
7665 : 11565 : expand_oacc_for (struct omp_region *region, struct omp_for_data *fd)
7666 : : {
7667 : 11565 : bool is_oacc_kernels_parallelized
7668 : 11565 : = (lookup_attribute ("oacc kernels parallelized",
7669 : 11565 : DECL_ATTRIBUTES (current_function_decl)) != NULL);
7670 : 11565 : {
7671 : 11565 : bool is_oacc_kernels
7672 : 11565 : = (lookup_attribute ("oacc kernels",
7673 : 11565 : DECL_ATTRIBUTES (current_function_decl)) != NULL);
7674 : 11565 : if (is_oacc_kernels_parallelized)
7675 : 388 : gcc_checking_assert (is_oacc_kernels);
7676 : : }
7677 : 23130 : gcc_assert (gimple_in_ssa_p (cfun) == is_oacc_kernels_parallelized);
7678 : : /* In the following, some of the 'gimple_in_ssa_p (cfun)' conditionals are
7679 : : for SSA specifics, and some are for 'parloops' OpenACC
7680 : : 'kernels'-parallelized specifics. */
7681 : :
7682 : 11565 : tree v = fd->loop.v;
7683 : 11565 : enum tree_code cond_code = fd->loop.cond_code;
7684 : 11565 : enum tree_code plus_code = PLUS_EXPR;
7685 : :
7686 : 11565 : tree chunk_size = integer_minus_one_node;
7687 : 11565 : tree gwv = integer_zero_node;
7688 : 11565 : tree iter_type = TREE_TYPE (v);
7689 : 11565 : tree diff_type = iter_type;
7690 : 11565 : tree plus_type = iter_type;
7691 : 11565 : struct oacc_collapse *counts = NULL;
7692 : :
7693 : 11565 : gcc_checking_assert (gimple_omp_for_kind (fd->for_stmt)
7694 : : == GF_OMP_FOR_KIND_OACC_LOOP);
7695 : 11565 : gcc_assert (!gimple_omp_for_combined_into_p (fd->for_stmt));
7696 : 11565 : gcc_assert (cond_code == LT_EXPR || cond_code == GT_EXPR);
7697 : :
7698 : 11565 : if (POINTER_TYPE_P (iter_type))
7699 : : {
7700 : 52 : plus_code = POINTER_PLUS_EXPR;
7701 : 52 : plus_type = sizetype;
7702 : : }
7703 : 23832 : for (int ix = fd->collapse; ix--;)
7704 : : {
7705 : 12267 : tree diff_type2 = TREE_TYPE (fd->loops[ix].step);
7706 : 12267 : if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (diff_type2))
7707 : 0 : diff_type = diff_type2;
7708 : : }
7709 : 11565 : if (POINTER_TYPE_P (diff_type) || TYPE_UNSIGNED (diff_type))
7710 : 1002 : diff_type = signed_type_for (diff_type);
7711 : 11565 : if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (integer_type_node))
7712 : 23 : diff_type = integer_type_node;
7713 : :
7714 : 11565 : basic_block entry_bb = region->entry; /* BB ending in OMP_FOR */
7715 : 11565 : basic_block exit_bb = region->exit; /* BB ending in OMP_RETURN */
7716 : 11565 : basic_block cont_bb = region->cont; /* BB ending in OMP_CONTINUE */
7717 : 11565 : basic_block bottom_bb = NULL;
7718 : :
7719 : : /* entry_bb has two successors; the branch edge is to the exit
7720 : : block, fallthrough edge to body. */
7721 : 11565 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2
7722 : : && BRANCH_EDGE (entry_bb)->dest == exit_bb);
7723 : :
7724 : : /* If cont_bb non-NULL, it has 2 successors. The branch successor is
7725 : : body_bb, or to a block whose only successor is the body_bb. Its
7726 : : fallthrough successor is the final block (same as the branch
7727 : : successor of the entry_bb). */
7728 : 11565 : if (cont_bb)
7729 : : {
7730 : 11526 : basic_block body_bb = FALLTHRU_EDGE (entry_bb)->dest;
7731 : 11526 : basic_block bed = BRANCH_EDGE (cont_bb)->dest;
7732 : :
7733 : 11526 : gcc_assert (FALLTHRU_EDGE (cont_bb)->dest == exit_bb);
7734 : 11526 : gcc_assert (bed == body_bb || single_succ_edge (bed)->dest == body_bb);
7735 : : }
7736 : : else
7737 : 39 : gcc_assert (!gimple_in_ssa_p (cfun));
7738 : :
7739 : : /* The exit block only has entry_bb and cont_bb as predecessors. */
7740 : 23169 : gcc_assert (EDGE_COUNT (exit_bb->preds) == 1 + (cont_bb != NULL));
7741 : :
7742 : 11565 : tree chunk_no;
7743 : 11565 : tree chunk_max = NULL_TREE;
7744 : 11565 : tree bound, offset;
7745 : 11565 : tree step = create_tmp_var (diff_type, ".step");
7746 : 11565 : bool up = cond_code == LT_EXPR;
7747 : 11725 : tree dir = build_int_cst (diff_type, up ? +1 : -1);
7748 : 11565 : bool chunking = !gimple_in_ssa_p (cfun);
7749 : 11565 : bool negating;
7750 : :
7751 : : /* Tiling vars. */
7752 : 11565 : tree tile_size = NULL_TREE;
7753 : 11565 : tree element_s = NULL_TREE;
7754 : 11565 : tree e_bound = NULL_TREE, e_offset = NULL_TREE, e_step = NULL_TREE;
7755 : 11565 : basic_block elem_body_bb = NULL;
7756 : 11565 : basic_block elem_cont_bb = NULL;
7757 : :
7758 : : /* SSA instances. */
7759 : 11565 : tree offset_incr = NULL_TREE;
7760 : 11565 : tree offset_init = NULL_TREE;
7761 : :
7762 : 11565 : gimple_stmt_iterator gsi;
7763 : 11565 : gassign *ass;
7764 : 11565 : gcall *call;
7765 : 11565 : gimple *stmt;
7766 : 11565 : tree expr;
7767 : 11565 : location_t loc;
7768 : 11565 : edge split, be, fte;
7769 : :
7770 : : /* Split the end of entry_bb to create head_bb. */
7771 : 11565 : split = split_block (entry_bb, last_nondebug_stmt (entry_bb));
7772 : 11565 : basic_block head_bb = split->dest;
7773 : 11565 : entry_bb = split->src;
7774 : :
7775 : : /* Chunk setup goes at end of entry_bb, replacing the omp_for. */
7776 : 11565 : gsi = gsi_last_nondebug_bb (entry_bb);
7777 : 11565 : gomp_for *for_stmt = as_a <gomp_for *> (gsi_stmt (gsi));
7778 : 11565 : loc = gimple_location (for_stmt);
7779 : :
7780 : 11565 : if (gimple_in_ssa_p (cfun))
7781 : : {
7782 : 388 : offset_init = gimple_omp_for_index (for_stmt, 0);
7783 : 388 : gcc_assert (integer_zerop (fd->loop.n1));
7784 : : /* The SSA parallelizer does gang parallelism. */
7785 : 388 : gwv = build_int_cst (integer_type_node, GOMP_DIM_MASK (GOMP_DIM_GANG));
7786 : : }
7787 : :
7788 : 11565 : if (fd->collapse > 1 || fd->tiling)
7789 : : {
7790 : 1188 : gcc_assert (!gimple_in_ssa_p (cfun) && up);
7791 : 594 : counts = XALLOCAVEC (struct oacc_collapse, fd->collapse);
7792 : 594 : tree total = expand_oacc_collapse_init (fd, &gsi, counts, diff_type,
7793 : 594 : TREE_TYPE (fd->loop.n2), loc);
7794 : :
7795 : 594 : if (SSA_VAR_P (fd->loop.n2))
7796 : : {
7797 : 107 : total = force_gimple_operand_gsi (&gsi, total, false, NULL_TREE,
7798 : : true, GSI_SAME_STMT);
7799 : 107 : ass = gimple_build_assign (fd->loop.n2, total);
7800 : 107 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7801 : : }
7802 : : }
7803 : :
7804 : 11565 : tree b = fd->loop.n1;
7805 : 11565 : tree e = fd->loop.n2;
7806 : 11565 : tree s = fd->loop.step;
7807 : :
7808 : 11565 : b = force_gimple_operand_gsi (&gsi, b, true, NULL_TREE, true, GSI_SAME_STMT);
7809 : 11565 : e = force_gimple_operand_gsi (&gsi, e, true, NULL_TREE, true, GSI_SAME_STMT);
7810 : :
7811 : : /* Convert the step, avoiding possible unsigned->signed overflow. */
7812 : 11565 : negating = !up && TYPE_UNSIGNED (TREE_TYPE (s));
7813 : 32 : if (negating)
7814 : 32 : s = fold_build1 (NEGATE_EXPR, TREE_TYPE (s), s);
7815 : 11565 : s = fold_convert (diff_type, s);
7816 : 11565 : if (negating)
7817 : 32 : s = fold_build1 (NEGATE_EXPR, diff_type, s);
7818 : 11565 : s = force_gimple_operand_gsi (&gsi, s, true, NULL_TREE, true, GSI_SAME_STMT);
7819 : :
7820 : 11565 : if (!chunking)
7821 : 388 : chunk_size = integer_zero_node;
7822 : 11565 : expr = fold_convert (diff_type, chunk_size);
7823 : 11565 : chunk_size = force_gimple_operand_gsi (&gsi, expr, true,
7824 : : NULL_TREE, true, GSI_SAME_STMT);
7825 : :
7826 : 11565 : if (fd->tiling)
7827 : : {
7828 : : /* Determine the tile size and element step,
7829 : : modify the outer loop step size. */
7830 : 177 : tile_size = create_tmp_var (diff_type, ".tile_size");
7831 : 177 : expr = build_int_cst (diff_type, 1);
7832 : 461 : for (int ix = 0; ix < fd->collapse; ix++)
7833 : 284 : expr = fold_build2 (MULT_EXPR, diff_type, counts[ix].tile, expr);
7834 : 177 : expr = force_gimple_operand_gsi (&gsi, expr, true,
7835 : : NULL_TREE, true, GSI_SAME_STMT);
7836 : 177 : ass = gimple_build_assign (tile_size, expr);
7837 : 177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7838 : :
7839 : 177 : element_s = create_tmp_var (diff_type, ".element_s");
7840 : 177 : ass = gimple_build_assign (element_s, s);
7841 : 177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7842 : :
7843 : 177 : expr = fold_build2 (MULT_EXPR, diff_type, s, tile_size);
7844 : 177 : s = force_gimple_operand_gsi (&gsi, expr, true,
7845 : : NULL_TREE, true, GSI_SAME_STMT);
7846 : : }
7847 : :
7848 : : /* Determine the range, avoiding possible unsigned->signed overflow. */
7849 : 11565 : negating = !up && TYPE_UNSIGNED (iter_type);
7850 : 23082 : expr = fold_build2 (MINUS_EXPR, plus_type,
7851 : : fold_convert (plus_type, negating ? b : e),
7852 : : fold_convert (plus_type, negating ? e : b));
7853 : 11565 : expr = fold_convert (diff_type, expr);
7854 : 11565 : if (negating)
7855 : 48 : expr = fold_build1 (NEGATE_EXPR, diff_type, expr);
7856 : 11565 : tree range = force_gimple_operand_gsi (&gsi, expr, true,
7857 : : NULL_TREE, true, GSI_SAME_STMT);
7858 : :
7859 : 11565 : chunk_no = build_int_cst (diff_type, 0);
7860 : 11565 : if (chunking)
7861 : : {
7862 : 11177 : gcc_assert (!gimple_in_ssa_p (cfun));
7863 : :
7864 : 11177 : expr = chunk_no;
7865 : 11177 : chunk_max = create_tmp_var (diff_type, ".chunk_max");
7866 : 11177 : chunk_no = create_tmp_var (diff_type, ".chunk_no");
7867 : :
7868 : 11177 : ass = gimple_build_assign (chunk_no, expr);
7869 : 11177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7870 : :
7871 : 11177 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
7872 : : build_int_cst (integer_type_node,
7873 : : IFN_GOACC_LOOP_CHUNKS),
7874 : : dir, range, s, chunk_size, gwv);
7875 : 11177 : gimple_call_set_lhs (call, chunk_max);
7876 : 11177 : gimple_set_location (call, loc);
7877 : 11177 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7878 : : }
7879 : : else
7880 : : chunk_size = chunk_no;
7881 : :
7882 : 11565 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
7883 : : build_int_cst (integer_type_node,
7884 : : IFN_GOACC_LOOP_STEP),
7885 : : dir, range, s, chunk_size, gwv);
7886 : 11565 : gimple_call_set_lhs (call, step);
7887 : 11565 : gimple_set_location (call, loc);
7888 : 11565 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7889 : :
7890 : : /* Remove the GIMPLE_OMP_FOR. */
7891 : 11565 : gsi_remove (&gsi, true);
7892 : :
7893 : : /* Fixup edges from head_bb. */
7894 : 11565 : be = BRANCH_EDGE (head_bb);
7895 : 11565 : fte = FALLTHRU_EDGE (head_bb);
7896 : 11565 : be->flags |= EDGE_FALSE_VALUE;
7897 : 11565 : fte->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
7898 : :
7899 : 11565 : basic_block body_bb = fte->dest;
7900 : :
7901 : 11565 : if (gimple_in_ssa_p (cfun))
7902 : : {
7903 : 388 : gsi = gsi_last_nondebug_bb (cont_bb);
7904 : 388 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
7905 : :
7906 : 388 : offset = gimple_omp_continue_control_use (cont_stmt);
7907 : 388 : offset_incr = gimple_omp_continue_control_def (cont_stmt);
7908 : : }
7909 : : else
7910 : : {
7911 : 11177 : offset = create_tmp_var (diff_type, ".offset");
7912 : 11177 : offset_init = offset_incr = offset;
7913 : : }
7914 : 11565 : bound = create_tmp_var (TREE_TYPE (offset), ".bound");
7915 : :
7916 : : /* Loop offset & bound go into head_bb. */
7917 : 11565 : gsi = gsi_start_bb (head_bb);
7918 : :
7919 : 11565 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
7920 : : build_int_cst (integer_type_node,
7921 : : IFN_GOACC_LOOP_OFFSET),
7922 : : dir, range, s,
7923 : : chunk_size, gwv, chunk_no);
7924 : 11565 : gimple_call_set_lhs (call, offset_init);
7925 : 11565 : gimple_set_location (call, loc);
7926 : 11565 : gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
7927 : :
7928 : 11565 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
7929 : : build_int_cst (integer_type_node,
7930 : : IFN_GOACC_LOOP_BOUND),
7931 : : dir, range, s,
7932 : : chunk_size, gwv, offset_init);
7933 : 11565 : gimple_call_set_lhs (call, bound);
7934 : 11565 : gimple_set_location (call, loc);
7935 : 11565 : gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
7936 : :
7937 : 11565 : expr = build2 (cond_code, boolean_type_node, offset_init, bound);
7938 : 11565 : gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
7939 : : GSI_CONTINUE_LINKING);
7940 : :
7941 : : /* V assignment goes into body_bb. */
7942 : 11565 : if (!gimple_in_ssa_p (cfun))
7943 : : {
7944 : 11177 : gsi = gsi_start_bb (body_bb);
7945 : :
7946 : 11177 : expr = build2 (plus_code, iter_type, b,
7947 : : fold_convert (plus_type, offset));
7948 : 11177 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
7949 : : true, GSI_SAME_STMT);
7950 : 11177 : ass = gimple_build_assign (v, expr);
7951 : 11177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7952 : :
7953 : 11177 : if (fd->collapse > 1 || fd->tiling)
7954 : 594 : expand_oacc_collapse_vars (fd, false, &gsi, counts, v, diff_type);
7955 : :
7956 : 11177 : if (fd->tiling)
7957 : : {
7958 : : /* Determine the range of the element loop -- usually simply
7959 : : the tile_size, but could be smaller if the final
7960 : : iteration of the outer loop is a partial tile. */
7961 : 177 : tree e_range = create_tmp_var (diff_type, ".e_range");
7962 : :
7963 : 177 : expr = build2 (MIN_EXPR, diff_type,
7964 : : build2 (MINUS_EXPR, diff_type, bound, offset),
7965 : : build2 (MULT_EXPR, diff_type, tile_size,
7966 : : element_s));
7967 : 177 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
7968 : : true, GSI_SAME_STMT);
7969 : 177 : ass = gimple_build_assign (e_range, expr);
7970 : 177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7971 : :
7972 : : /* Determine bound, offset & step of inner loop. */
7973 : 177 : e_bound = create_tmp_var (diff_type, ".e_bound");
7974 : 177 : e_offset = create_tmp_var (diff_type, ".e_offset");
7975 : 177 : e_step = create_tmp_var (diff_type, ".e_step");
7976 : :
7977 : : /* Mark these as element loops. */
7978 : 177 : tree t, e_gwv = integer_minus_one_node;
7979 : 177 : tree chunk = build_int_cst (diff_type, 0); /* Never chunked. */
7980 : :
7981 : 177 : t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_OFFSET);
7982 : 177 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
7983 : : element_s, chunk, e_gwv, chunk);
7984 : 177 : gimple_call_set_lhs (call, e_offset);
7985 : 177 : gimple_set_location (call, loc);
7986 : 177 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7987 : :
7988 : 177 : t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_BOUND);
7989 : 177 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
7990 : : element_s, chunk, e_gwv, e_offset);
7991 : 177 : gimple_call_set_lhs (call, e_bound);
7992 : 177 : gimple_set_location (call, loc);
7993 : 177 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7994 : :
7995 : 177 : t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_STEP);
7996 : 177 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 6, t, dir, e_range,
7997 : : element_s, chunk, e_gwv);
7998 : 177 : gimple_call_set_lhs (call, e_step);
7999 : 177 : gimple_set_location (call, loc);
8000 : 177 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8001 : :
8002 : : /* Add test and split block. */
8003 : 177 : expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
8004 : 177 : stmt = gimple_build_cond_empty (expr);
8005 : 177 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8006 : 177 : split = split_block (body_bb, stmt);
8007 : 177 : elem_body_bb = split->dest;
8008 : 177 : if (cont_bb == body_bb)
8009 : 146 : cont_bb = elem_body_bb;
8010 : 177 : body_bb = split->src;
8011 : :
8012 : 177 : split->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
8013 : :
8014 : : /* Add a dummy exit for the tiled block when cont_bb is missing. */
8015 : 177 : if (cont_bb == NULL)
8016 : : {
8017 : 5 : edge e = make_edge (body_bb, exit_bb, EDGE_FALSE_VALUE);
8018 : 5 : e->probability = profile_probability::even ();
8019 : 5 : split->probability = profile_probability::even ();
8020 : : }
8021 : :
8022 : : /* Initialize the user's loop vars. */
8023 : 177 : gsi = gsi_start_bb (elem_body_bb);
8024 : 177 : expand_oacc_collapse_vars (fd, true, &gsi, counts, e_offset,
8025 : : diff_type);
8026 : : }
8027 : : }
8028 : :
8029 : : /* Loop increment goes into cont_bb. If this is not a loop, we
8030 : : will have spawned threads as if it was, and each one will
8031 : : execute one iteration. The specification is not explicit about
8032 : : whether such constructs are ill-formed or not, and they can
8033 : : occur, especially when noreturn routines are involved. */
8034 : 11565 : if (cont_bb)
8035 : : {
8036 : 11526 : gsi = gsi_last_nondebug_bb (cont_bb);
8037 : 11526 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
8038 : 11526 : loc = gimple_location (cont_stmt);
8039 : :
8040 : 11526 : if (fd->tiling)
8041 : : {
8042 : : /* Insert element loop increment and test. */
8043 : 172 : expr = build2 (PLUS_EXPR, diff_type, e_offset, e_step);
8044 : 172 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8045 : : true, GSI_SAME_STMT);
8046 : 172 : ass = gimple_build_assign (e_offset, expr);
8047 : 172 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8048 : 172 : expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
8049 : :
8050 : 172 : stmt = gimple_build_cond_empty (expr);
8051 : 172 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8052 : 172 : split = split_block (cont_bb, stmt);
8053 : 172 : elem_cont_bb = split->src;
8054 : 172 : cont_bb = split->dest;
8055 : :
8056 : 172 : split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8057 : 172 : split->probability = profile_probability::unlikely ().guessed ();
8058 : 172 : edge latch_edge
8059 : 172 : = make_edge (elem_cont_bb, elem_body_bb, EDGE_TRUE_VALUE);
8060 : 172 : latch_edge->probability = profile_probability::likely ().guessed ();
8061 : :
8062 : 172 : edge skip_edge = make_edge (body_bb, cont_bb, EDGE_FALSE_VALUE);
8063 : 172 : skip_edge->probability = profile_probability::unlikely ().guessed ();
8064 : 172 : edge loop_entry_edge = EDGE_SUCC (body_bb, 1 - skip_edge->dest_idx);
8065 : 172 : loop_entry_edge->probability
8066 : 172 : = profile_probability::likely ().guessed ();
8067 : :
8068 : 172 : gsi = gsi_for_stmt (cont_stmt);
8069 : : }
8070 : :
8071 : : /* Increment offset. */
8072 : 11526 : if (gimple_in_ssa_p (cfun))
8073 : 388 : expr = build2 (plus_code, iter_type, offset,
8074 : : fold_convert (plus_type, step));
8075 : : else
8076 : 11138 : expr = build2 (PLUS_EXPR, diff_type, offset, step);
8077 : 11526 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8078 : : true, GSI_SAME_STMT);
8079 : 11526 : ass = gimple_build_assign (offset_incr, expr);
8080 : 11526 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8081 : 11526 : expr = build2 (cond_code, boolean_type_node, offset_incr, bound);
8082 : 11526 : gsi_insert_before (&gsi, gimple_build_cond_empty (expr), GSI_SAME_STMT);
8083 : :
8084 : : /* Remove the GIMPLE_OMP_CONTINUE. */
8085 : 11526 : gsi_remove (&gsi, true);
8086 : :
8087 : : /* Fixup edges from cont_bb. */
8088 : 11526 : be = BRANCH_EDGE (cont_bb);
8089 : 11526 : fte = FALLTHRU_EDGE (cont_bb);
8090 : 11526 : be->flags |= EDGE_TRUE_VALUE;
8091 : 11526 : fte->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8092 : :
8093 : 11526 : if (chunking)
8094 : : {
8095 : : /* Split the beginning of exit_bb to make bottom_bb. We
8096 : : need to insert a nop at the start, because splitting is
8097 : : after a stmt, not before. */
8098 : 11138 : gsi = gsi_start_bb (exit_bb);
8099 : 11138 : stmt = gimple_build_nop ();
8100 : 11138 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8101 : 11138 : split = split_block (exit_bb, stmt);
8102 : 11138 : bottom_bb = split->src;
8103 : 11138 : exit_bb = split->dest;
8104 : 11138 : gsi = gsi_last_bb (bottom_bb);
8105 : :
8106 : : /* Chunk increment and test goes into bottom_bb. */
8107 : 11138 : expr = build2 (PLUS_EXPR, diff_type, chunk_no,
8108 : : build_int_cst (diff_type, 1));
8109 : 11138 : ass = gimple_build_assign (chunk_no, expr);
8110 : 11138 : gsi_insert_after (&gsi, ass, GSI_CONTINUE_LINKING);
8111 : :
8112 : : /* Chunk test at end of bottom_bb. */
8113 : 11138 : expr = build2 (LT_EXPR, boolean_type_node, chunk_no, chunk_max);
8114 : 11138 : gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
8115 : : GSI_CONTINUE_LINKING);
8116 : :
8117 : : /* Fixup edges from bottom_bb. */
8118 : 11138 : split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8119 : 11138 : split->probability = profile_probability::unlikely ().guessed ();
8120 : 11138 : edge latch_edge = make_edge (bottom_bb, head_bb, EDGE_TRUE_VALUE);
8121 : 11138 : latch_edge->probability = profile_probability::likely ().guessed ();
8122 : : }
8123 : : }
8124 : :
8125 : 11565 : gsi = gsi_last_nondebug_bb (exit_bb);
8126 : 11565 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
8127 : 11565 : loc = gimple_location (gsi_stmt (gsi));
8128 : :
8129 : 11565 : if (!gimple_in_ssa_p (cfun))
8130 : : {
8131 : : /* Insert the final value of V, in case it is live. This is the
8132 : : value for the only thread that survives past the join. */
8133 : 11177 : expr = fold_build2 (MINUS_EXPR, diff_type, range, dir);
8134 : 11177 : expr = fold_build2 (PLUS_EXPR, diff_type, expr, s);
8135 : 11177 : expr = fold_build2 (TRUNC_DIV_EXPR, diff_type, expr, s);
8136 : 11177 : expr = fold_build2 (MULT_EXPR, diff_type, expr, s);
8137 : 11177 : expr = build2 (plus_code, iter_type, b, fold_convert (plus_type, expr));
8138 : 11177 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8139 : : true, GSI_SAME_STMT);
8140 : 11177 : ass = gimple_build_assign (v, expr);
8141 : 11177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8142 : : }
8143 : :
8144 : : /* Remove the OMP_RETURN. */
8145 : 11565 : gsi_remove (&gsi, true);
8146 : :
8147 : 11565 : if (cont_bb)
8148 : : {
8149 : : /* We now have one, two or three nested loops. Update the loop
8150 : : structures. */
8151 : 11526 : class loop *parent = entry_bb->loop_father;
8152 : 11526 : class loop *body = body_bb->loop_father;
8153 : :
8154 : 11526 : if (chunking)
8155 : : {
8156 : 11138 : class loop *chunk_loop = alloc_loop ();
8157 : 11138 : chunk_loop->header = head_bb;
8158 : 11138 : chunk_loop->latch = bottom_bb;
8159 : 11138 : add_loop (chunk_loop, parent);
8160 : 11138 : parent = chunk_loop;
8161 : : }
8162 : 388 : else if (parent != body)
8163 : : {
8164 : 388 : gcc_assert (body->header == body_bb);
8165 : 388 : gcc_assert (body->latch == cont_bb
8166 : : || single_pred (body->latch) == cont_bb);
8167 : : parent = NULL;
8168 : : }
8169 : :
8170 : 11138 : if (parent)
8171 : : {
8172 : 11138 : class loop *body_loop = alloc_loop ();
8173 : 11138 : body_loop->header = body_bb;
8174 : 11138 : body_loop->latch = cont_bb;
8175 : 11138 : add_loop (body_loop, parent);
8176 : :
8177 : 11138 : if (fd->tiling)
8178 : : {
8179 : : /* Insert tiling's element loop. */
8180 : 172 : class loop *inner_loop = alloc_loop ();
8181 : 172 : inner_loop->header = elem_body_bb;
8182 : 172 : inner_loop->latch = elem_cont_bb;
8183 : 172 : add_loop (inner_loop, body_loop);
8184 : : }
8185 : : }
8186 : : }
8187 : 11565 : }
8188 : :
8189 : : /* Expand the OMP loop defined by REGION. */
8190 : :
8191 : : static void
8192 : 47480 : expand_omp_for (struct omp_region *region, gimple *inner_stmt)
8193 : : {
8194 : 47480 : struct omp_for_data fd;
8195 : 47480 : struct omp_for_data_loop *loops;
8196 : :
8197 : 47480 : loops = XALLOCAVEC (struct omp_for_data_loop,
8198 : : gimple_omp_for_collapse
8199 : : (last_nondebug_stmt (region->entry)));
8200 : 47480 : omp_extract_for_data (as_a <gomp_for *> (last_nondebug_stmt (region->entry)),
8201 : : &fd, loops);
8202 : 47480 : region->sched_kind = fd.sched_kind;
8203 : 47480 : region->sched_modifiers = fd.sched_modifiers;
8204 : 47480 : region->has_lastprivate_conditional = fd.lastprivate_conditional != 0;
8205 : 47480 : if (fd.non_rect && !gimple_omp_for_combined_into_p (fd.for_stmt))
8206 : : {
8207 : 2344 : for (int i = fd.first_nonrect; i <= fd.last_nonrect; i++)
8208 : 1644 : if ((loops[i].m1 || loops[i].m2)
8209 : 731 : && (loops[i].m1 == NULL_TREE
8210 : 506 : || TREE_CODE (loops[i].m1) == INTEGER_CST)
8211 : 612 : && (loops[i].m2 == NULL_TREE
8212 : 343 : || TREE_CODE (loops[i].m2) == INTEGER_CST)
8213 : 596 : && TREE_CODE (loops[i].step) == INTEGER_CST
8214 : 586 : && TREE_CODE (loops[i - loops[i].outer].step) == INTEGER_CST)
8215 : : {
8216 : 586 : tree t;
8217 : 586 : tree itype = TREE_TYPE (loops[i].v);
8218 : 586 : if (loops[i].m1 && loops[i].m2)
8219 : 108 : t = fold_build2 (MINUS_EXPR, itype, loops[i].m2, loops[i].m1);
8220 : 478 : else if (loops[i].m1)
8221 : 267 : t = fold_build1 (NEGATE_EXPR, itype, loops[i].m1);
8222 : : else
8223 : : t = loops[i].m2;
8224 : 586 : t = fold_build2 (MULT_EXPR, itype, t,
8225 : : fold_convert (itype,
8226 : : loops[i - loops[i].outer].step));
8227 : 586 : if (TYPE_UNSIGNED (itype) && loops[i].cond_code == GT_EXPR)
8228 : 3 : t = fold_build2 (TRUNC_MOD_EXPR, itype,
8229 : : fold_build1 (NEGATE_EXPR, itype, t),
8230 : : fold_build1 (NEGATE_EXPR, itype,
8231 : : fold_convert (itype,
8232 : : loops[i].step)));
8233 : : else
8234 : 583 : t = fold_build2 (TRUNC_MOD_EXPR, itype, t,
8235 : : fold_convert (itype, loops[i].step));
8236 : 586 : if (integer_nonzerop (t))
8237 : 8 : error_at (gimple_location (fd.for_stmt),
8238 : : "invalid OpenMP non-rectangular loop step; "
8239 : : "%<(%E - %E) * %E%> is not a multiple of loop %d "
8240 : : "step %qE",
8241 : 8 : loops[i].m2 ? loops[i].m2 : integer_zero_node,
8242 : 8 : loops[i].m1 ? loops[i].m1 : integer_zero_node,
8243 : 8 : loops[i - loops[i].outer].step, i + 1,
8244 : : loops[i].step);
8245 : : }
8246 : : }
8247 : :
8248 : 47480 : gcc_assert (EDGE_COUNT (region->entry->succs) == 2);
8249 : 47480 : BRANCH_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
8250 : 47480 : FALLTHRU_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
8251 : 47480 : if (region->cont)
8252 : : {
8253 : 45193 : gcc_assert (EDGE_COUNT (region->cont->succs) == 2);
8254 : 45193 : BRANCH_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
8255 : 45193 : FALLTHRU_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
8256 : : }
8257 : : else
8258 : : /* If there isn't a continue then this is a degerate case where
8259 : : the introduction of abnormal edges during lowering will prevent
8260 : : original loops from being detected. Fix that up. */
8261 : 2287 : loops_state_set (LOOPS_NEED_FIXUP);
8262 : :
8263 : 47480 : if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_SIMD)
8264 : 9347 : expand_omp_simd (region, &fd);
8265 : 38133 : else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_OACC_LOOP)
8266 : : {
8267 : 11565 : gcc_assert (!inner_stmt && !fd.non_rect);
8268 : 11565 : expand_oacc_for (region, &fd);
8269 : : }
8270 : 26568 : else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_TASKLOOP)
8271 : : {
8272 : 2652 : if (gimple_omp_for_combined_into_p (fd.for_stmt))
8273 : 1326 : expand_omp_taskloop_for_inner (region, &fd, inner_stmt);
8274 : : else
8275 : 1326 : expand_omp_taskloop_for_outer (region, &fd, inner_stmt);
8276 : : }
8277 : 23916 : else if (fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC
8278 : 20234 : && !fd.have_ordered)
8279 : : {
8280 : 19787 : if (fd.chunk_size == NULL)
8281 : 13976 : expand_omp_for_static_nochunk (region, &fd, inner_stmt);
8282 : : else
8283 : 5811 : expand_omp_for_static_chunk (region, &fd, inner_stmt);
8284 : : }
8285 : : else
8286 : : {
8287 : 4129 : int fn_index, start_ix, next_ix;
8288 : 4129 : unsigned HOST_WIDE_INT sched = 0;
8289 : 4129 : tree sched_arg = NULL_TREE;
8290 : :
8291 : 4129 : gcc_assert (gimple_omp_for_kind (fd.for_stmt)
8292 : : == GF_OMP_FOR_KIND_FOR && !fd.non_rect);
8293 : 4129 : if (fd.chunk_size == NULL
8294 : 1665 : && fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC)
8295 : 0 : fd.chunk_size = integer_zero_node;
8296 : 4129 : switch (fd.sched_kind)
8297 : : {
8298 : 1665 : case OMP_CLAUSE_SCHEDULE_RUNTIME:
8299 : 1665 : if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_NONMONOTONIC) != 0
8300 : 28 : && fd.lastprivate_conditional == 0)
8301 : : {
8302 : 28 : gcc_assert (!fd.have_ordered);
8303 : : fn_index = 6;
8304 : : sched = 4;
8305 : : }
8306 : 1637 : else if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
8307 : 1605 : && !fd.have_ordered
8308 : 1561 : && fd.lastprivate_conditional == 0)
8309 : : fn_index = 7;
8310 : : else
8311 : : {
8312 : 87 : fn_index = 3;
8313 : 87 : sched = (HOST_WIDE_INT_1U << 31);
8314 : : }
8315 : : break;
8316 : 2017 : case OMP_CLAUSE_SCHEDULE_DYNAMIC:
8317 : 2017 : case OMP_CLAUSE_SCHEDULE_GUIDED:
8318 : 2017 : if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
8319 : 1961 : && !fd.have_ordered
8320 : 1798 : && fd.lastprivate_conditional == 0)
8321 : : {
8322 : 1771 : fn_index = 3 + fd.sched_kind;
8323 : 1771 : sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
8324 : : break;
8325 : : }
8326 : 246 : fn_index = fd.sched_kind;
8327 : 246 : sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
8328 : 246 : sched += (HOST_WIDE_INT_1U << 31);
8329 : 246 : break;
8330 : 447 : case OMP_CLAUSE_SCHEDULE_STATIC:
8331 : 447 : gcc_assert (fd.have_ordered);
8332 : : fn_index = 0;
8333 : : sched = (HOST_WIDE_INT_1U << 31) + 1;
8334 : : break;
8335 : 0 : default:
8336 : 0 : gcc_unreachable ();
8337 : : }
8338 : 4129 : if (!fd.ordered)
8339 : 3798 : fn_index += fd.have_ordered * 8;
8340 : 4129 : if (fd.ordered)
8341 : 331 : start_ix = ((int)BUILT_IN_GOMP_LOOP_DOACROSS_STATIC_START) + fn_index;
8342 : : else
8343 : 3798 : start_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_START) + fn_index;
8344 : 4129 : next_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_NEXT) + fn_index;
8345 : 4129 : if (fd.have_reductemp || fd.have_pointer_condtemp)
8346 : : {
8347 : 169 : if (fd.ordered)
8348 : : start_ix = (int)BUILT_IN_GOMP_LOOP_DOACROSS_START;
8349 : 133 : else if (fd.have_ordered)
8350 : : start_ix = (int)BUILT_IN_GOMP_LOOP_ORDERED_START;
8351 : : else
8352 : 95 : start_ix = (int)BUILT_IN_GOMP_LOOP_START;
8353 : 169 : sched_arg = build_int_cstu (long_integer_type_node, sched);
8354 : 169 : if (!fd.chunk_size)
8355 : 38 : fd.chunk_size = integer_zero_node;
8356 : : }
8357 : 4129 : if (fd.iter_type == long_long_unsigned_type_node)
8358 : : {
8359 : 778 : start_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_START
8360 : : - (int)BUILT_IN_GOMP_LOOP_STATIC_START);
8361 : 778 : next_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_NEXT
8362 : : - (int)BUILT_IN_GOMP_LOOP_STATIC_NEXT);
8363 : : }
8364 : 4129 : expand_omp_for_generic (region, &fd, (enum built_in_function) start_ix,
8365 : : (enum built_in_function) next_ix, sched_arg,
8366 : : inner_stmt);
8367 : : }
8368 : 47480 : }
8369 : :
8370 : : /* Expand code for an OpenMP sections directive. In pseudo code, we generate
8371 : :
8372 : : v = GOMP_sections_start (n);
8373 : : L0:
8374 : : switch (v)
8375 : : {
8376 : : case 0:
8377 : : goto L2;
8378 : : case 1:
8379 : : section 1;
8380 : : goto L1;
8381 : : case 2:
8382 : : ...
8383 : : case n:
8384 : : ...
8385 : : default:
8386 : : abort ();
8387 : : }
8388 : : L1:
8389 : : v = GOMP_sections_next ();
8390 : : goto L0;
8391 : : L2:
8392 : : reduction;
8393 : :
8394 : : If this is a combined parallel sections, replace the call to
8395 : : GOMP_sections_start with call to GOMP_sections_next. */
8396 : :
8397 : : static void
8398 : 378 : expand_omp_sections (struct omp_region *region)
8399 : : {
8400 : 378 : tree t, u, vin = NULL, vmain, vnext, l2;
8401 : 378 : unsigned len;
8402 : 378 : basic_block entry_bb, l0_bb, l1_bb, l2_bb, default_bb;
8403 : 378 : gimple_stmt_iterator si, switch_si;
8404 : 378 : gomp_sections *sections_stmt;
8405 : 378 : gimple *stmt;
8406 : 378 : gomp_continue *cont;
8407 : 378 : edge_iterator ei;
8408 : 378 : edge e;
8409 : 378 : struct omp_region *inner;
8410 : 378 : unsigned i, casei;
8411 : 378 : bool exit_reachable = region->cont != NULL;
8412 : :
8413 : 378 : gcc_assert (region->exit != NULL);
8414 : 378 : entry_bb = region->entry;
8415 : 378 : l0_bb = single_succ (entry_bb);
8416 : 378 : l1_bb = region->cont;
8417 : 378 : l2_bb = region->exit;
8418 : 719 : if (single_pred_p (l2_bb) && single_pred (l2_bb) == l0_bb)
8419 : 314 : l2 = gimple_block_label (l2_bb);
8420 : : else
8421 : : {
8422 : : /* This can happen if there are reductions. */
8423 : 64 : len = EDGE_COUNT (l0_bb->succs);
8424 : 64 : gcc_assert (len > 0);
8425 : 64 : e = EDGE_SUCC (l0_bb, len - 1);
8426 : 64 : si = gsi_last_nondebug_bb (e->dest);
8427 : 64 : l2 = NULL_TREE;
8428 : 64 : if (gsi_end_p (si)
8429 : 64 : || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
8430 : 64 : l2 = gimple_block_label (e->dest);
8431 : : else
8432 : 0 : FOR_EACH_EDGE (e, ei, l0_bb->succs)
8433 : : {
8434 : 0 : si = gsi_last_nondebug_bb (e->dest);
8435 : 0 : if (gsi_end_p (si)
8436 : 0 : || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
8437 : : {
8438 : 0 : l2 = gimple_block_label (e->dest);
8439 : 0 : break;
8440 : : }
8441 : : }
8442 : : }
8443 : 378 : if (exit_reachable)
8444 : 332 : default_bb = create_empty_bb (l1_bb->prev_bb);
8445 : : else
8446 : 46 : default_bb = create_empty_bb (l0_bb);
8447 : :
8448 : : /* We will build a switch() with enough cases for all the
8449 : : GIMPLE_OMP_SECTION regions, a '0' case to handle the end of more work
8450 : : and a default case to abort if something goes wrong. */
8451 : 378 : len = EDGE_COUNT (l0_bb->succs);
8452 : :
8453 : : /* Use vec::quick_push on label_vec throughout, since we know the size
8454 : : in advance. */
8455 : 378 : auto_vec<tree> label_vec (len);
8456 : :
8457 : : /* The call to GOMP_sections_start goes in ENTRY_BB, replacing the
8458 : : GIMPLE_OMP_SECTIONS statement. */
8459 : 378 : si = gsi_last_nondebug_bb (entry_bb);
8460 : 378 : sections_stmt = as_a <gomp_sections *> (gsi_stmt (si));
8461 : 378 : gcc_assert (gimple_code (sections_stmt) == GIMPLE_OMP_SECTIONS);
8462 : 378 : vin = gimple_omp_sections_control (sections_stmt);
8463 : 378 : tree clauses = gimple_omp_sections_clauses (sections_stmt);
8464 : 378 : tree reductmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
8465 : 378 : tree condtmp = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
8466 : 378 : tree cond_var = NULL_TREE;
8467 : 378 : if (reductmp || condtmp)
8468 : : {
8469 : 18 : tree reductions = null_pointer_node, mem = null_pointer_node;
8470 : 18 : tree memv = NULL_TREE, condtemp = NULL_TREE;
8471 : 18 : gimple_stmt_iterator gsi = gsi_none ();
8472 : 18 : gimple *g = NULL;
8473 : 18 : if (reductmp)
8474 : : {
8475 : 8 : reductions = OMP_CLAUSE_DECL (reductmp);
8476 : 8 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
8477 : 8 : g = SSA_NAME_DEF_STMT (reductions);
8478 : 8 : reductions = gimple_assign_rhs1 (g);
8479 : 8 : OMP_CLAUSE_DECL (reductmp) = reductions;
8480 : 8 : gsi = gsi_for_stmt (g);
8481 : : }
8482 : : else
8483 : 10 : gsi = si;
8484 : 18 : if (condtmp)
8485 : : {
8486 : 12 : condtemp = OMP_CLAUSE_DECL (condtmp);
8487 : 12 : tree c = omp_find_clause (OMP_CLAUSE_CHAIN (condtmp),
8488 : : OMP_CLAUSE__CONDTEMP_);
8489 : 12 : cond_var = OMP_CLAUSE_DECL (c);
8490 : 12 : tree type = TREE_TYPE (condtemp);
8491 : 12 : memv = create_tmp_var (type);
8492 : 12 : TREE_ADDRESSABLE (memv) = 1;
8493 : 12 : unsigned cnt = 0;
8494 : 74 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
8495 : 62 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8496 : 62 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8497 : 24 : ++cnt;
8498 : 12 : unsigned HOST_WIDE_INT sz
8499 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type))) * cnt;
8500 : 12 : expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
8501 : : false);
8502 : 12 : mem = build_fold_addr_expr (memv);
8503 : : }
8504 : 18 : t = build_int_cst (unsigned_type_node, len - 1);
8505 : 18 : u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS2_START);
8506 : 18 : stmt = gimple_build_call (u, 3, t, reductions, mem);
8507 : 18 : gimple_call_set_lhs (stmt, vin);
8508 : 18 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8509 : 18 : if (condtmp)
8510 : : {
8511 : 12 : expand_omp_build_assign (&gsi, condtemp, memv, false);
8512 : 12 : tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
8513 : 12 : vin, build_one_cst (TREE_TYPE (cond_var)));
8514 : 12 : expand_omp_build_assign (&gsi, cond_var, t, false);
8515 : : }
8516 : 18 : if (reductmp)
8517 : : {
8518 : 8 : gsi_remove (&gsi, true);
8519 : 8 : release_ssa_name (gimple_assign_lhs (g));
8520 : : }
8521 : : }
8522 : 360 : else if (!is_combined_parallel (region))
8523 : : {
8524 : : /* If we are not inside a combined parallel+sections region,
8525 : : call GOMP_sections_start. */
8526 : 248 : t = build_int_cst (unsigned_type_node, len - 1);
8527 : 248 : u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_START);
8528 : 248 : stmt = gimple_build_call (u, 1, t);
8529 : : }
8530 : : else
8531 : : {
8532 : : /* Otherwise, call GOMP_sections_next. */
8533 : 112 : u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
8534 : 112 : stmt = gimple_build_call (u, 0);
8535 : : }
8536 : 378 : if (!reductmp && !condtmp)
8537 : : {
8538 : 360 : gimple_call_set_lhs (stmt, vin);
8539 : 360 : gsi_insert_after (&si, stmt, GSI_SAME_STMT);
8540 : : }
8541 : 378 : gsi_remove (&si, true);
8542 : :
8543 : : /* The switch() statement replacing GIMPLE_OMP_SECTIONS_SWITCH goes in
8544 : : L0_BB. */
8545 : 378 : switch_si = gsi_last_nondebug_bb (l0_bb);
8546 : 378 : gcc_assert (gimple_code (gsi_stmt (switch_si)) == GIMPLE_OMP_SECTIONS_SWITCH);
8547 : 378 : if (exit_reachable)
8548 : : {
8549 : 332 : cont = as_a <gomp_continue *> (last_nondebug_stmt (l1_bb));
8550 : 332 : gcc_assert (gimple_code (cont) == GIMPLE_OMP_CONTINUE);
8551 : 332 : vmain = gimple_omp_continue_control_use (cont);
8552 : 332 : vnext = gimple_omp_continue_control_def (cont);
8553 : : }
8554 : : else
8555 : : {
8556 : : vmain = vin;
8557 : : vnext = NULL_TREE;
8558 : : }
8559 : :
8560 : 378 : t = build_case_label (build_int_cst (unsigned_type_node, 0), NULL, l2);
8561 : 378 : label_vec.quick_push (t);
8562 : 378 : i = 1;
8563 : :
8564 : : /* Convert each GIMPLE_OMP_SECTION into a CASE_LABEL_EXPR. */
8565 : 378 : for (inner = region->inner, casei = 1;
8566 : 1260 : inner;
8567 : 882 : inner = inner->next, i++, casei++)
8568 : : {
8569 : 882 : basic_block s_entry_bb, s_exit_bb;
8570 : :
8571 : : /* Skip optional reduction region. */
8572 : 882 : if (inner->type == GIMPLE_OMP_ATOMIC_LOAD)
8573 : : {
8574 : 27 : --i;
8575 : 27 : --casei;
8576 : 27 : continue;
8577 : : }
8578 : :
8579 : 855 : s_entry_bb = inner->entry;
8580 : 855 : s_exit_bb = inner->exit;
8581 : :
8582 : 855 : t = gimple_block_label (s_entry_bb);
8583 : 855 : u = build_int_cst (unsigned_type_node, casei);
8584 : 855 : u = build_case_label (u, NULL, t);
8585 : 855 : label_vec.quick_push (u);
8586 : :
8587 : 855 : si = gsi_last_nondebug_bb (s_entry_bb);
8588 : 855 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SECTION);
8589 : 855 : gcc_assert (i < len || gimple_omp_section_last_p (gsi_stmt (si)));
8590 : 855 : gsi_remove (&si, true);
8591 : 855 : single_succ_edge (s_entry_bb)->flags = EDGE_FALLTHRU;
8592 : :
8593 : 855 : if (s_exit_bb == NULL)
8594 : 122 : continue;
8595 : :
8596 : 733 : si = gsi_last_nondebug_bb (s_exit_bb);
8597 : 733 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
8598 : 733 : gsi_remove (&si, true);
8599 : :
8600 : 733 : single_succ_edge (s_exit_bb)->flags = EDGE_FALLTHRU;
8601 : : }
8602 : :
8603 : : /* Error handling code goes in DEFAULT_BB. */
8604 : 378 : t = gimple_block_label (default_bb);
8605 : 378 : u = build_case_label (NULL, NULL, t);
8606 : 378 : make_edge (l0_bb, default_bb, 0);
8607 : 378 : add_bb_to_loop (default_bb, current_loops->tree_root);
8608 : :
8609 : 378 : stmt = gimple_build_switch (vmain, u, label_vec);
8610 : 378 : gsi_insert_after (&switch_si, stmt, GSI_SAME_STMT);
8611 : 378 : gsi_remove (&switch_si, true);
8612 : :
8613 : 378 : si = gsi_start_bb (default_bb);
8614 : 378 : stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
8615 : 378 : gsi_insert_after (&si, stmt, GSI_CONTINUE_LINKING);
8616 : :
8617 : 378 : if (exit_reachable)
8618 : : {
8619 : 332 : tree bfn_decl;
8620 : :
8621 : : /* Code to get the next section goes in L1_BB. */
8622 : 332 : si = gsi_last_nondebug_bb (l1_bb);
8623 : 332 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CONTINUE);
8624 : :
8625 : 332 : bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
8626 : 332 : stmt = gimple_build_call (bfn_decl, 0);
8627 : 332 : gimple_call_set_lhs (stmt, vnext);
8628 : 332 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
8629 : 332 : if (cond_var)
8630 : : {
8631 : 12 : tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
8632 : 12 : vnext, build_one_cst (TREE_TYPE (cond_var)));
8633 : 12 : expand_omp_build_assign (&si, cond_var, t, false);
8634 : : }
8635 : 332 : gsi_remove (&si, true);
8636 : :
8637 : 332 : single_succ_edge (l1_bb)->flags = EDGE_FALLTHRU;
8638 : : }
8639 : :
8640 : : /* Cleanup function replaces GIMPLE_OMP_RETURN in EXIT_BB. */
8641 : 378 : si = gsi_last_nondebug_bb (l2_bb);
8642 : 378 : if (gimple_omp_return_nowait_p (gsi_stmt (si)))
8643 : 251 : t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_NOWAIT);
8644 : 127 : else if (gimple_omp_return_lhs (gsi_stmt (si)))
8645 : 0 : t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_CANCEL);
8646 : : else
8647 : 127 : t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END);
8648 : 378 : stmt = gimple_build_call (t, 0);
8649 : 378 : if (gimple_omp_return_lhs (gsi_stmt (si)))
8650 : 0 : gimple_call_set_lhs (stmt, gimple_omp_return_lhs (gsi_stmt (si)));
8651 : 378 : gsi_insert_after (&si, stmt, GSI_SAME_STMT);
8652 : 378 : gsi_remove (&si, true);
8653 : :
8654 : 378 : set_immediate_dominator (CDI_DOMINATORS, default_bb, l0_bb);
8655 : 378 : }
8656 : :
8657 : : /* Expand code for an OpenMP single or scope directive. We've already expanded
8658 : : much of the code, here we simply place the GOMP_barrier call. */
8659 : :
8660 : : static void
8661 : 1251 : expand_omp_single (struct omp_region *region)
8662 : : {
8663 : 1251 : basic_block entry_bb, exit_bb;
8664 : 1251 : gimple_stmt_iterator si;
8665 : :
8666 : 1251 : entry_bb = region->entry;
8667 : 1251 : exit_bb = region->exit;
8668 : :
8669 : 1251 : si = gsi_last_nondebug_bb (entry_bb);
8670 : 1251 : enum gimple_code code = gimple_code (gsi_stmt (si));
8671 : 1251 : gcc_assert (code == GIMPLE_OMP_SINGLE || code == GIMPLE_OMP_SCOPE);
8672 : 1251 : gsi_remove (&si, true);
8673 : 1251 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
8674 : :
8675 : 1251 : if (exit_bb == NULL)
8676 : : {
8677 : 8 : gcc_assert (code == GIMPLE_OMP_SCOPE);
8678 : 8 : return;
8679 : : }
8680 : :
8681 : 1243 : si = gsi_last_nondebug_bb (exit_bb);
8682 : 1243 : if (!gimple_omp_return_nowait_p (gsi_stmt (si)))
8683 : : {
8684 : 972 : tree t = gimple_omp_return_lhs (gsi_stmt (si));
8685 : 972 : gsi_insert_after (&si, omp_build_barrier (t), GSI_SAME_STMT);
8686 : : }
8687 : 1243 : gsi_remove (&si, true);
8688 : 1243 : single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
8689 : : }
8690 : :
8691 : : /* Generic expansion for OpenMP synchronization directives: master,
8692 : : ordered and critical. All we need to do here is remove the entry
8693 : : and exit markers for REGION. */
8694 : :
8695 : : static void
8696 : 10794 : expand_omp_synch (struct omp_region *region)
8697 : : {
8698 : 10794 : basic_block entry_bb, exit_bb;
8699 : 10794 : gimple_stmt_iterator si;
8700 : :
8701 : 10794 : entry_bb = region->entry;
8702 : 10794 : exit_bb = region->exit;
8703 : :
8704 : 10794 : si = gsi_last_nondebug_bb (entry_bb);
8705 : 10794 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SINGLE
8706 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASTER
8707 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASKED
8708 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TASKGROUP
8709 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ORDERED
8710 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CRITICAL
8711 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS);
8712 : 10794 : if (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS
8713 : 10794 : && gimple_omp_teams_host (as_a <gomp_teams *> (gsi_stmt (si))))
8714 : : {
8715 : 2535 : expand_omp_taskreg (region);
8716 : 2535 : return;
8717 : : }
8718 : 8259 : gsi_remove (&si, true);
8719 : 8259 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
8720 : :
8721 : 8259 : if (exit_bb)
8722 : : {
8723 : 7701 : si = gsi_last_nondebug_bb (exit_bb);
8724 : 7701 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
8725 : 7701 : gsi_remove (&si, true);
8726 : 7701 : single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
8727 : : }
8728 : : }
8729 : :
8730 : : /* Translate enum omp_memory_order to enum memmodel for the embedded
8731 : : fail clause in there. */
8732 : :
8733 : : static enum memmodel
8734 : 2836 : omp_memory_order_to_fail_memmodel (enum omp_memory_order mo)
8735 : : {
8736 : 2836 : switch (mo & OMP_FAIL_MEMORY_ORDER_MASK)
8737 : : {
8738 : 2565 : case OMP_FAIL_MEMORY_ORDER_UNSPECIFIED:
8739 : 2565 : switch (mo & OMP_MEMORY_ORDER_MASK)
8740 : : {
8741 : : case OMP_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
8742 : : case OMP_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
8743 : : case OMP_MEMORY_ORDER_RELEASE: return MEMMODEL_RELAXED;
8744 : : case OMP_MEMORY_ORDER_ACQ_REL: return MEMMODEL_ACQUIRE;
8745 : : case OMP_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
8746 : 0 : default: break;
8747 : : }
8748 : 0 : gcc_unreachable ();
8749 : : case OMP_FAIL_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
8750 : : case OMP_FAIL_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
8751 : : case OMP_FAIL_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
8752 : 0 : default: gcc_unreachable ();
8753 : : }
8754 : : }
8755 : :
8756 : : /* Translate enum omp_memory_order to enum memmodel. The two enums
8757 : : are using different numbers so that OMP_MEMORY_ORDER_UNSPECIFIED
8758 : : is 0 and omp_memory_order has the fail mode encoded in it too. */
8759 : :
8760 : : static enum memmodel
8761 : 9856 : omp_memory_order_to_memmodel (enum omp_memory_order mo)
8762 : : {
8763 : 9856 : enum memmodel ret, fail_ret;
8764 : 9856 : switch (mo & OMP_MEMORY_ORDER_MASK)
8765 : : {
8766 : : case OMP_MEMORY_ORDER_RELAXED: ret = MEMMODEL_RELAXED; break;
8767 : : case OMP_MEMORY_ORDER_ACQUIRE: ret = MEMMODEL_ACQUIRE; break;
8768 : : case OMP_MEMORY_ORDER_RELEASE: ret = MEMMODEL_RELEASE; break;
8769 : : case OMP_MEMORY_ORDER_ACQ_REL: ret = MEMMODEL_ACQ_REL; break;
8770 : : case OMP_MEMORY_ORDER_SEQ_CST: ret = MEMMODEL_SEQ_CST; break;
8771 : 0 : default: gcc_unreachable ();
8772 : : }
8773 : : /* If we drop the -Winvalid-memory-model warning for C++17 P0418R2,
8774 : : we can just return ret here unconditionally. Otherwise, work around
8775 : : it here and make sure fail memmodel is not stronger. */
8776 : 9856 : if ((mo & OMP_FAIL_MEMORY_ORDER_MASK) == OMP_FAIL_MEMORY_ORDER_UNSPECIFIED)
8777 : : return ret;
8778 : 142 : fail_ret = omp_memory_order_to_fail_memmodel (mo);
8779 : 142 : if (fail_ret > ret)
8780 : 13 : return fail_ret;
8781 : : return ret;
8782 : : }
8783 : :
8784 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8785 : : operation as a normal volatile load. */
8786 : :
8787 : : static bool
8788 : 1183 : expand_omp_atomic_load (basic_block load_bb, tree addr,
8789 : : tree loaded_val, int index)
8790 : : {
8791 : 1183 : enum built_in_function tmpbase;
8792 : 1183 : gimple_stmt_iterator gsi;
8793 : 1183 : basic_block store_bb;
8794 : 1183 : location_t loc;
8795 : 1183 : gimple *stmt;
8796 : 1183 : tree decl, type, itype;
8797 : :
8798 : 1183 : gsi = gsi_last_nondebug_bb (load_bb);
8799 : 1183 : stmt = gsi_stmt (gsi);
8800 : 1183 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
8801 : 1183 : loc = gimple_location (stmt);
8802 : :
8803 : : /* ??? If the target does not implement atomic_load_optab[mode], and mode
8804 : : is smaller than word size, then expand_atomic_load assumes that the load
8805 : : is atomic. We could avoid the builtin entirely in this case. */
8806 : :
8807 : 1183 : tmpbase = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
8808 : 1183 : decl = builtin_decl_explicit (tmpbase);
8809 : 1183 : if (decl == NULL_TREE)
8810 : : return false;
8811 : :
8812 : 1183 : type = TREE_TYPE (loaded_val);
8813 : 1183 : itype = TREE_TYPE (TREE_TYPE (decl));
8814 : :
8815 : 1183 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
8816 : 1183 : tree mo = build_int_cst (integer_type_node,
8817 : 1183 : omp_memory_order_to_memmodel (omo));
8818 : 1183 : gcall *call = gimple_build_call (decl, 2, addr, mo);
8819 : 1183 : gimple_set_location (call, loc);
8820 : 2366 : gimple_set_vuse (call, gimple_vuse (stmt));
8821 : 1183 : gimple *repl;
8822 : 1183 : if (!useless_type_conversion_p (type, itype))
8823 : : {
8824 : 1181 : tree lhs = make_ssa_name (itype);
8825 : 1181 : gimple_call_set_lhs (call, lhs);
8826 : 1181 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8827 : 1181 : repl = gimple_build_assign (loaded_val,
8828 : : build1 (VIEW_CONVERT_EXPR, type, lhs));
8829 : 1181 : gimple_set_location (repl, loc);
8830 : : }
8831 : : else
8832 : : {
8833 : 2 : gimple_call_set_lhs (call, loaded_val);
8834 : : repl = call;
8835 : : }
8836 : 1183 : gsi_replace (&gsi, repl, true);
8837 : :
8838 : 1183 : store_bb = single_succ (load_bb);
8839 : 1183 : gsi = gsi_last_nondebug_bb (store_bb);
8840 : 1183 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
8841 : 1183 : gsi_remove (&gsi, true);
8842 : :
8843 : 1183 : return true;
8844 : : }
8845 : :
8846 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8847 : : operation as a normal volatile store. */
8848 : :
8849 : : static bool
8850 : 861 : expand_omp_atomic_store (basic_block load_bb, tree addr,
8851 : : tree loaded_val, tree stored_val, int index)
8852 : : {
8853 : 861 : enum built_in_function tmpbase;
8854 : 861 : gimple_stmt_iterator gsi;
8855 : 861 : basic_block store_bb = single_succ (load_bb);
8856 : 861 : location_t loc;
8857 : 861 : gimple *stmt;
8858 : 861 : tree decl, type, itype;
8859 : 861 : machine_mode imode;
8860 : 861 : bool exchange;
8861 : :
8862 : 861 : gsi = gsi_last_nondebug_bb (load_bb);
8863 : 861 : stmt = gsi_stmt (gsi);
8864 : 861 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
8865 : :
8866 : : /* If the load value is needed, then this isn't a store but an exchange. */
8867 : 861 : exchange = gimple_omp_atomic_need_value_p (stmt);
8868 : :
8869 : 861 : gsi = gsi_last_nondebug_bb (store_bb);
8870 : 861 : stmt = gsi_stmt (gsi);
8871 : 861 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE);
8872 : 861 : loc = gimple_location (stmt);
8873 : :
8874 : : /* ??? If the target does not implement atomic_store_optab[mode], and mode
8875 : : is smaller than word size, then expand_atomic_store assumes that the store
8876 : : is atomic. We could avoid the builtin entirely in this case. */
8877 : :
8878 : 861 : tmpbase = (exchange ? BUILT_IN_ATOMIC_EXCHANGE_N : BUILT_IN_ATOMIC_STORE_N);
8879 : 861 : tmpbase = (enum built_in_function) ((int) tmpbase + index + 1);
8880 : 861 : decl = builtin_decl_explicit (tmpbase);
8881 : 861 : if (decl == NULL_TREE)
8882 : : return false;
8883 : :
8884 : 861 : type = TREE_TYPE (stored_val);
8885 : :
8886 : : /* Dig out the type of the function's second argument. */
8887 : 861 : itype = TREE_TYPE (decl);
8888 : 861 : itype = TYPE_ARG_TYPES (itype);
8889 : 861 : itype = TREE_CHAIN (itype);
8890 : 861 : itype = TREE_VALUE (itype);
8891 : 861 : imode = TYPE_MODE (itype);
8892 : :
8893 : 861 : if (exchange && !can_atomic_exchange_p (imode, true))
8894 : : return false;
8895 : :
8896 : 861 : if (!useless_type_conversion_p (itype, type))
8897 : 861 : stored_val = fold_build1_loc (loc, VIEW_CONVERT_EXPR, itype, stored_val);
8898 : 861 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
8899 : 861 : tree mo = build_int_cst (integer_type_node,
8900 : 861 : omp_memory_order_to_memmodel (omo));
8901 : 861 : stored_val = force_gimple_operand_gsi (&gsi, stored_val, true, NULL_TREE,
8902 : : true, GSI_SAME_STMT);
8903 : 861 : gcall *call = gimple_build_call (decl, 3, addr, stored_val, mo);
8904 : 861 : gimple_set_location (call, loc);
8905 : 1722 : gimple_set_vuse (call, gimple_vuse (stmt));
8906 : 1722 : gimple_set_vdef (call, gimple_vdef (stmt));
8907 : :
8908 : 861 : gimple *repl = call;
8909 : 861 : if (exchange)
8910 : : {
8911 : 88 : if (!useless_type_conversion_p (type, itype))
8912 : : {
8913 : 88 : tree lhs = make_ssa_name (itype);
8914 : 88 : gimple_call_set_lhs (call, lhs);
8915 : 88 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8916 : 88 : repl = gimple_build_assign (loaded_val,
8917 : : build1 (VIEW_CONVERT_EXPR, type, lhs));
8918 : 88 : gimple_set_location (repl, loc);
8919 : : }
8920 : : else
8921 : 0 : gimple_call_set_lhs (call, loaded_val);
8922 : : }
8923 : 861 : gsi_replace (&gsi, repl, true);
8924 : :
8925 : : /* Remove the GIMPLE_OMP_ATOMIC_LOAD that we verified above. */
8926 : 861 : gsi = gsi_last_nondebug_bb (load_bb);
8927 : 861 : gsi_remove (&gsi, true);
8928 : :
8929 : 861 : return true;
8930 : : }
8931 : :
8932 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8933 : : operation as a __atomic_fetch_op builtin. INDEX is log2 of the
8934 : : size of the data type, and thus usable to find the index of the builtin
8935 : : decl. Returns false if the expression is not of the proper form. */
8936 : :
8937 : : static bool
8938 : 5761 : expand_omp_atomic_fetch_op (basic_block load_bb,
8939 : : tree addr, tree loaded_val,
8940 : : tree stored_val, int index)
8941 : : {
8942 : 5761 : enum built_in_function oldbase, newbase, tmpbase;
8943 : 5761 : tree decl, itype, call;
8944 : 5761 : tree lhs, rhs;
8945 : 5761 : basic_block store_bb = single_succ (load_bb);
8946 : 5761 : gimple_stmt_iterator gsi;
8947 : 5761 : gimple *stmt;
8948 : 5761 : location_t loc;
8949 : 5761 : enum tree_code code;
8950 : 5761 : bool need_old, need_new;
8951 : 5761 : machine_mode imode;
8952 : :
8953 : : /* We expect to find the following sequences:
8954 : :
8955 : : load_bb:
8956 : : GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
8957 : :
8958 : : store_bb:
8959 : : val = tmp OP something; (or: something OP tmp)
8960 : : GIMPLE_OMP_STORE (val)
8961 : :
8962 : : ???FIXME: Allow a more flexible sequence.
8963 : : Perhaps use data flow to pick the statements.
8964 : :
8965 : : */
8966 : :
8967 : 5761 : gsi = gsi_after_labels (store_bb);
8968 : 5761 : stmt = gsi_stmt (gsi);
8969 : 5761 : if (is_gimple_debug (stmt))
8970 : : {
8971 : 0 : gsi_next_nondebug (&gsi);
8972 : 0 : if (gsi_end_p (gsi))
8973 : : return false;
8974 : 5761 : stmt = gsi_stmt (gsi);
8975 : : }
8976 : 5761 : loc = gimple_location (stmt);
8977 : 5761 : if (!is_gimple_assign (stmt))
8978 : : return false;
8979 : 5761 : gsi_next_nondebug (&gsi);
8980 : 5761 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_OMP_ATOMIC_STORE)
8981 : : return false;
8982 : 5118 : need_new = gimple_omp_atomic_need_value_p (gsi_stmt (gsi));
8983 : 5118 : need_old = gimple_omp_atomic_need_value_p (last_nondebug_stmt (load_bb));
8984 : 5118 : enum omp_memory_order omo
8985 : 5118 : = gimple_omp_atomic_memory_order (last_nondebug_stmt (load_bb));
8986 : 5118 : enum memmodel mo = omp_memory_order_to_memmodel (omo);
8987 : 5118 : gcc_checking_assert (!need_old || !need_new);
8988 : :
8989 : 5118 : if (!operand_equal_p (gimple_assign_lhs (stmt), stored_val, 0))
8990 : : return false;
8991 : :
8992 : : /* Check for one of the supported fetch-op operations. */
8993 : 5118 : code = gimple_assign_rhs_code (stmt);
8994 : 5118 : switch (code)
8995 : : {
8996 : : case PLUS_EXPR:
8997 : : case POINTER_PLUS_EXPR:
8998 : : oldbase = BUILT_IN_ATOMIC_FETCH_ADD_N;
8999 : : newbase = BUILT_IN_ATOMIC_ADD_FETCH_N;
9000 : : break;
9001 : 67 : case MINUS_EXPR:
9002 : 67 : oldbase = BUILT_IN_ATOMIC_FETCH_SUB_N;
9003 : 67 : newbase = BUILT_IN_ATOMIC_SUB_FETCH_N;
9004 : 67 : break;
9005 : 100 : case BIT_AND_EXPR:
9006 : 100 : oldbase = BUILT_IN_ATOMIC_FETCH_AND_N;
9007 : 100 : newbase = BUILT_IN_ATOMIC_AND_FETCH_N;
9008 : 100 : break;
9009 : 215 : case BIT_IOR_EXPR:
9010 : 215 : oldbase = BUILT_IN_ATOMIC_FETCH_OR_N;
9011 : 215 : newbase = BUILT_IN_ATOMIC_OR_FETCH_N;
9012 : 215 : break;
9013 : 99 : case BIT_XOR_EXPR:
9014 : 99 : oldbase = BUILT_IN_ATOMIC_FETCH_XOR_N;
9015 : 99 : newbase = BUILT_IN_ATOMIC_XOR_FETCH_N;
9016 : 99 : break;
9017 : : default:
9018 : : return false;
9019 : : }
9020 : :
9021 : : /* Make sure the expression is of the proper form. */
9022 : 4507 : if (operand_equal_p (gimple_assign_rhs1 (stmt), loaded_val, 0))
9023 : 4329 : rhs = gimple_assign_rhs2 (stmt);
9024 : 178 : else if (commutative_tree_code (gimple_assign_rhs_code (stmt))
9025 : 178 : && operand_equal_p (gimple_assign_rhs2 (stmt), loaded_val, 0))
9026 : 139 : rhs = gimple_assign_rhs1 (stmt);
9027 : : else
9028 : 39 : return false;
9029 : :
9030 : 8936 : tmpbase = ((enum built_in_function)
9031 : 4468 : ((need_new ? newbase : oldbase) + index + 1));
9032 : 4468 : decl = builtin_decl_explicit (tmpbase);
9033 : 4468 : if (decl == NULL_TREE)
9034 : : return false;
9035 : 4468 : itype = TREE_TYPE (TREE_TYPE (decl));
9036 : 4468 : imode = TYPE_MODE (itype);
9037 : :
9038 : : /* We could test all of the various optabs involved, but the fact of the
9039 : : matter is that (with the exception of i486 vs i586 and xadd) all targets
9040 : : that support any atomic operaton optab also implements compare-and-swap.
9041 : : Let optabs.cc take care of expanding any compare-and-swap loop. */
9042 : 4468 : if (!can_compare_and_swap_p (imode, true) || !can_atomic_load_p (imode))
9043 : 0 : return false;
9044 : :
9045 : 4468 : gsi = gsi_last_nondebug_bb (load_bb);
9046 : 4468 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_LOAD);
9047 : :
9048 : : /* OpenMP does not imply any barrier-like semantics on its atomic ops.
9049 : : It only requires that the operation happen atomically. Thus we can
9050 : : use the RELAXED memory model. */
9051 : 4468 : call = build_call_expr_loc (loc, decl, 3, addr,
9052 : : fold_convert_loc (loc, itype, rhs),
9053 : 4468 : build_int_cst (NULL, mo));
9054 : :
9055 : 4468 : if (need_old || need_new)
9056 : : {
9057 : 480 : lhs = need_old ? loaded_val : stored_val;
9058 : 480 : call = fold_convert_loc (loc, TREE_TYPE (lhs), call);
9059 : 480 : call = build2_loc (loc, MODIFY_EXPR, void_type_node, lhs, call);
9060 : : }
9061 : : else
9062 : 3988 : call = fold_convert_loc (loc, void_type_node, call);
9063 : 4468 : force_gimple_operand_gsi (&gsi, call, true, NULL_TREE, true, GSI_SAME_STMT);
9064 : 4468 : gsi_remove (&gsi, true);
9065 : :
9066 : 4468 : gsi = gsi_last_nondebug_bb (store_bb);
9067 : 4468 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
9068 : 4468 : gsi_remove (&gsi, true);
9069 : 4468 : gsi = gsi_last_nondebug_bb (store_bb);
9070 : 4468 : stmt = gsi_stmt (gsi);
9071 : 4468 : gsi_remove (&gsi, true);
9072 : :
9073 : 4468 : if (gimple_in_ssa_p (cfun))
9074 : 29 : release_defs (stmt);
9075 : :
9076 : : return true;
9077 : : }
9078 : :
9079 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
9080 : : compare and exchange as an ATOMIC_COMPARE_EXCHANGE internal function.
9081 : : Returns false if the expression is not of the proper form. */
9082 : :
9083 : : static bool
9084 : 2113 : expand_omp_atomic_cas (basic_block load_bb, tree addr,
9085 : : tree loaded_val, tree stored_val, int index)
9086 : : {
9087 : : /* We expect to find the following sequences:
9088 : :
9089 : : load_bb:
9090 : : GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
9091 : :
9092 : : store_bb:
9093 : : val = tmp == e ? d : tmp;
9094 : : GIMPLE_OMP_ATOMIC_STORE (val)
9095 : :
9096 : : or in store_bb instead:
9097 : : tmp2 = tmp == e;
9098 : : val = tmp2 ? d : tmp;
9099 : : GIMPLE_OMP_ATOMIC_STORE (val)
9100 : :
9101 : : or:
9102 : : tmp3 = VIEW_CONVERT_EXPR<integral_type>(tmp);
9103 : : val = e == tmp3 ? d : tmp;
9104 : : GIMPLE_OMP_ATOMIC_STORE (val)
9105 : :
9106 : : etc. */
9107 : :
9108 : :
9109 : 2113 : basic_block store_bb = single_succ (load_bb);
9110 : 2113 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (store_bb);
9111 : 2113 : gimple *store_stmt = gsi_stmt (gsi);
9112 : 2113 : if (!store_stmt || gimple_code (store_stmt) != GIMPLE_OMP_ATOMIC_STORE)
9113 : : return false;
9114 : 2113 : gsi_prev_nondebug (&gsi);
9115 : 2113 : if (gsi_end_p (gsi))
9116 : : return false;
9117 : 2107 : gimple *condexpr_stmt = gsi_stmt (gsi);
9118 : 2107 : if (!is_gimple_assign (condexpr_stmt)
9119 : 2107 : || gimple_assign_rhs_code (condexpr_stmt) != COND_EXPR)
9120 : : return false;
9121 : 408 : if (!operand_equal_p (gimple_assign_lhs (condexpr_stmt), stored_val, 0))
9122 : : return false;
9123 : 408 : gimple *cond_stmt = NULL;
9124 : 408 : gimple *vce_stmt = NULL;
9125 : 408 : gsi_prev_nondebug (&gsi);
9126 : 408 : if (!gsi_end_p (gsi))
9127 : : {
9128 : 408 : cond_stmt = gsi_stmt (gsi);
9129 : 408 : if (!is_gimple_assign (cond_stmt))
9130 : : return false;
9131 : 408 : if (gimple_assign_rhs_code (cond_stmt) == EQ_EXPR)
9132 : : {
9133 : 323 : gsi_prev_nondebug (&gsi);
9134 : 323 : if (!gsi_end_p (gsi))
9135 : : {
9136 : 89 : vce_stmt = gsi_stmt (gsi);
9137 : 89 : if (!is_gimple_assign (vce_stmt)
9138 : 89 : || gimple_assign_rhs_code (vce_stmt) != VIEW_CONVERT_EXPR)
9139 : : return false;
9140 : : }
9141 : : }
9142 : 85 : else if (gimple_assign_rhs_code (cond_stmt) == VIEW_CONVERT_EXPR)
9143 : : std::swap (vce_stmt, cond_stmt);
9144 : : else
9145 : : return false;
9146 : 69 : if (vce_stmt)
9147 : : {
9148 : 69 : tree vce_rhs = gimple_assign_rhs1 (vce_stmt);
9149 : 69 : if (TREE_CODE (vce_rhs) != VIEW_CONVERT_EXPR
9150 : 69 : || !operand_equal_p (TREE_OPERAND (vce_rhs, 0), loaded_val))
9151 : 12 : return false;
9152 : 114 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vce_rhs))
9153 : 57 : || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (loaded_val))
9154 : 114 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (vce_rhs)),
9155 : 57 : TYPE_SIZE (TREE_TYPE (loaded_val))))
9156 : 0 : return false;
9157 : 57 : gsi_prev_nondebug (&gsi);
9158 : 57 : if (!gsi_end_p (gsi))
9159 : : return false;
9160 : : }
9161 : : }
9162 : 291 : tree cond = gimple_assign_rhs1 (condexpr_stmt);
9163 : 291 : tree cond_op1, cond_op2;
9164 : 291 : if (cond_stmt)
9165 : : {
9166 : : /* We should now always get a separate cond_stmt. */
9167 : 291 : if (!operand_equal_p (cond, gimple_assign_lhs (cond_stmt)))
9168 : : return false;
9169 : 291 : cond_op1 = gimple_assign_rhs1 (cond_stmt);
9170 : 291 : cond_op2 = gimple_assign_rhs2 (cond_stmt);
9171 : : }
9172 : 0 : else if (TREE_CODE (cond) != EQ_EXPR && TREE_CODE (cond) != NE_EXPR)
9173 : : return false;
9174 : : else
9175 : : {
9176 : 0 : cond_op1 = TREE_OPERAND (cond, 0);
9177 : 0 : cond_op2 = TREE_OPERAND (cond, 1);
9178 : : }
9179 : 291 : tree d;
9180 : 291 : if (TREE_CODE (cond) == NE_EXPR)
9181 : : {
9182 : 0 : if (!operand_equal_p (gimple_assign_rhs2 (condexpr_stmt), loaded_val))
9183 : : return false;
9184 : 0 : d = gimple_assign_rhs3 (condexpr_stmt);
9185 : : }
9186 : 291 : else if (!operand_equal_p (gimple_assign_rhs3 (condexpr_stmt), loaded_val))
9187 : : return false;
9188 : : else
9189 : 291 : d = gimple_assign_rhs2 (condexpr_stmt);
9190 : 291 : tree e = vce_stmt ? gimple_assign_lhs (vce_stmt) : loaded_val;
9191 : 291 : if (operand_equal_p (e, cond_op1))
9192 : : e = cond_op2;
9193 : 21 : else if (operand_equal_p (e, cond_op2))
9194 : : e = cond_op1;
9195 : : else
9196 : : return false;
9197 : :
9198 : 291 : location_t loc = gimple_location (store_stmt);
9199 : 291 : gimple *load_stmt = last_nondebug_stmt (load_bb);
9200 : 291 : bool need_new = gimple_omp_atomic_need_value_p (store_stmt);
9201 : 291 : bool need_old = gimple_omp_atomic_need_value_p (load_stmt);
9202 : 291 : bool weak = gimple_omp_atomic_weak_p (load_stmt);
9203 : 291 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (load_stmt);
9204 : 291 : tree mo = build_int_cst (NULL, omp_memory_order_to_memmodel (omo));
9205 : 291 : tree fmo = build_int_cst (NULL, omp_memory_order_to_fail_memmodel (omo));
9206 : 291 : gcc_checking_assert (!need_old || !need_new);
9207 : :
9208 : 291 : enum built_in_function fncode
9209 : : = (enum built_in_function) ((int) BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
9210 : 291 : + index + 1);
9211 : 291 : tree cmpxchg = builtin_decl_explicit (fncode);
9212 : 291 : if (cmpxchg == NULL_TREE)
9213 : : return false;
9214 : 291 : tree itype = TREE_TYPE (TREE_TYPE (cmpxchg));
9215 : :
9216 : 291 : if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
9217 : 291 : || !can_atomic_load_p (TYPE_MODE (itype)))
9218 : 1 : return false;
9219 : :
9220 : 290 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9221 : 290 : if (SCALAR_FLOAT_TYPE_P (type) && !vce_stmt)
9222 : : return false;
9223 : :
9224 : 289 : gsi = gsi_for_stmt (store_stmt);
9225 : 289 : if (!useless_type_conversion_p (itype, TREE_TYPE (e)))
9226 : : {
9227 : 232 : tree ne = create_tmp_reg (itype);
9228 : 232 : gimple *g = gimple_build_assign (ne, NOP_EXPR, e);
9229 : 232 : gimple_set_location (g, loc);
9230 : 232 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9231 : 232 : e = ne;
9232 : : }
9233 : 289 : if (!useless_type_conversion_p (itype, TREE_TYPE (d)))
9234 : : {
9235 : 289 : tree nd = create_tmp_reg (itype);
9236 : 289 : enum tree_code code;
9237 : 289 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (d)))
9238 : : {
9239 : 57 : code = VIEW_CONVERT_EXPR;
9240 : 57 : d = build1 (VIEW_CONVERT_EXPR, itype, d);
9241 : : }
9242 : : else
9243 : : code = NOP_EXPR;
9244 : 289 : gimple *g = gimple_build_assign (nd, code, d);
9245 : 289 : gimple_set_location (g, loc);
9246 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9247 : 289 : d = nd;
9248 : : }
9249 : :
9250 : 289 : tree ctype = build_complex_type (itype);
9251 : 289 : int flag = int_size_in_bytes (itype) + (weak ? 256 : 0);
9252 : 289 : gimple *g
9253 : 289 : = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6, addr, e, d,
9254 : 289 : build_int_cst (integer_type_node, flag),
9255 : : mo, fmo);
9256 : 289 : tree cres = create_tmp_reg (ctype);
9257 : 289 : gimple_call_set_lhs (g, cres);
9258 : 289 : gimple_set_location (g, loc);
9259 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9260 : :
9261 : 289 : if (cond_stmt || need_old || need_new)
9262 : : {
9263 : 289 : tree im = create_tmp_reg (itype);
9264 : 289 : g = gimple_build_assign (im, IMAGPART_EXPR,
9265 : : build1 (IMAGPART_EXPR, itype, cres));
9266 : 289 : gimple_set_location (g, loc);
9267 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9268 : :
9269 : 289 : tree re = NULL_TREE;
9270 : 289 : if (need_old || need_new)
9271 : : {
9272 : 199 : re = create_tmp_reg (itype);
9273 : 199 : g = gimple_build_assign (re, REALPART_EXPR,
9274 : : build1 (REALPART_EXPR, itype, cres));
9275 : 199 : gimple_set_location (g, loc);
9276 : 199 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9277 : : }
9278 : :
9279 : 289 : if (cond_stmt)
9280 : : {
9281 : 289 : g = gimple_build_assign (cond, NOP_EXPR, im);
9282 : 289 : gimple_set_location (g, loc);
9283 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9284 : : }
9285 : :
9286 : 289 : if (need_new)
9287 : : {
9288 : 44 : g = gimple_build_assign (create_tmp_reg (itype), COND_EXPR,
9289 : : cond_stmt
9290 : 0 : ? cond : build2 (NE_EXPR, boolean_type_node,
9291 : : im, build_zero_cst (itype)),
9292 : : d, re);
9293 : 44 : gimple_set_location (g, loc);
9294 : 44 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9295 : 44 : re = gimple_assign_lhs (g);
9296 : : }
9297 : :
9298 : 289 : if (need_old || need_new)
9299 : : {
9300 : 199 : tree v = need_old ? loaded_val : stored_val;
9301 : 199 : enum tree_code code;
9302 : 199 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (v)))
9303 : : {
9304 : 38 : code = VIEW_CONVERT_EXPR;
9305 : 38 : re = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (v), re);
9306 : : }
9307 : 161 : else if (!useless_type_conversion_p (TREE_TYPE (v), itype))
9308 : : code = NOP_EXPR;
9309 : : else
9310 : 0 : code = TREE_CODE (re);
9311 : 199 : g = gimple_build_assign (v, code, re);
9312 : 199 : gimple_set_location (g, loc);
9313 : 199 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9314 : : }
9315 : : }
9316 : :
9317 : 289 : gsi_remove (&gsi, true);
9318 : 289 : gsi = gsi_for_stmt (load_stmt);
9319 : 289 : gsi_remove (&gsi, true);
9320 : 289 : gsi = gsi_for_stmt (condexpr_stmt);
9321 : 289 : gsi_remove (&gsi, true);
9322 : 289 : if (cond_stmt)
9323 : : {
9324 : 289 : gsi = gsi_for_stmt (cond_stmt);
9325 : 289 : gsi_remove (&gsi, true);
9326 : : }
9327 : 289 : if (vce_stmt)
9328 : : {
9329 : 57 : gsi = gsi_for_stmt (vce_stmt);
9330 : 57 : gsi_remove (&gsi, true);
9331 : : }
9332 : :
9333 : : return true;
9334 : : }
9335 : :
9336 : : /* A subroutine of expand_omp_atomic. Implement the atomic operation as:
9337 : :
9338 : : oldval = *addr;
9339 : : repeat:
9340 : : newval = rhs; // with oldval replacing *addr in rhs
9341 : : oldval = __sync_val_compare_and_swap (addr, oldval, newval);
9342 : : if (oldval != newval)
9343 : : goto repeat;
9344 : :
9345 : : INDEX is log2 of the size of the data type, and thus usable to find the
9346 : : index of the builtin decl. */
9347 : :
9348 : : static bool
9349 : 2482 : expand_omp_atomic_pipeline (basic_block load_bb, basic_block store_bb,
9350 : : tree addr, tree loaded_val, tree stored_val,
9351 : : int index)
9352 : : {
9353 : 2482 : tree loadedi, storedi, initial, new_storedi, old_vali;
9354 : 2482 : tree type, itype, cmpxchg, iaddr, atype;
9355 : 2482 : gimple_stmt_iterator si;
9356 : 2482 : basic_block loop_header = single_succ (load_bb);
9357 : 2482 : gimple *phi, *stmt;
9358 : 2482 : edge e;
9359 : 2482 : enum built_in_function fncode;
9360 : :
9361 : 2482 : fncode = (enum built_in_function)((int)BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
9362 : 2482 : + index + 1);
9363 : 2482 : cmpxchg = builtin_decl_explicit (fncode);
9364 : 2482 : if (cmpxchg == NULL_TREE)
9365 : : return false;
9366 : 2482 : type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9367 : 2482 : atype = type;
9368 : 2482 : itype = TREE_TYPE (TREE_TYPE (cmpxchg));
9369 : :
9370 : 2482 : if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
9371 : 2482 : || !can_atomic_load_p (TYPE_MODE (itype)))
9372 : 79 : return false;
9373 : :
9374 : : /* Load the initial value, replacing the GIMPLE_OMP_ATOMIC_LOAD. */
9375 : 2403 : si = gsi_last_nondebug_bb (load_bb);
9376 : 2403 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
9377 : 2403 : location_t loc = gimple_location (gsi_stmt (si));
9378 : 2403 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (gsi_stmt (si));
9379 : 2403 : tree mo = build_int_cst (NULL, omp_memory_order_to_memmodel (omo));
9380 : 2403 : tree fmo = build_int_cst (NULL, omp_memory_order_to_fail_memmodel (omo));
9381 : :
9382 : : /* For floating-point values, we'll need to view-convert them to integers
9383 : : so that we can perform the atomic compare and swap. Simplify the
9384 : : following code by always setting up the "i"ntegral variables. */
9385 : 2403 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9386 : : {
9387 : 799 : tree iaddr_val;
9388 : :
9389 : 799 : iaddr = create_tmp_reg (build_pointer_type_for_mode (itype, ptr_mode,
9390 : : true));
9391 : 799 : atype = itype;
9392 : 799 : iaddr_val
9393 : 799 : = force_gimple_operand_gsi (&si,
9394 : 799 : fold_convert (TREE_TYPE (iaddr), addr),
9395 : : false, NULL_TREE, true, GSI_SAME_STMT);
9396 : 799 : stmt = gimple_build_assign (iaddr, iaddr_val);
9397 : 799 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9398 : 799 : loadedi = create_tmp_var (itype);
9399 : 799 : if (gimple_in_ssa_p (cfun))
9400 : 15 : loadedi = make_ssa_name (loadedi);
9401 : : }
9402 : : else
9403 : : {
9404 : : iaddr = addr;
9405 : : loadedi = loaded_val;
9406 : : }
9407 : :
9408 : 2403 : fncode = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
9409 : 2403 : tree loaddecl = builtin_decl_explicit (fncode);
9410 : 2403 : if (loaddecl)
9411 : 2403 : initial
9412 : 2403 : = fold_convert (atype,
9413 : : build_call_expr (loaddecl, 2, iaddr,
9414 : : build_int_cst (NULL_TREE,
9415 : : MEMMODEL_RELAXED)));
9416 : : else
9417 : : {
9418 : 0 : tree off
9419 : 0 : = build_int_cst (build_pointer_type_for_mode (atype, ptr_mode,
9420 : : true), 0);
9421 : 0 : initial = build2 (MEM_REF, atype, iaddr, off);
9422 : : }
9423 : :
9424 : 2403 : initial
9425 : 2403 : = force_gimple_operand_gsi (&si, initial, true, NULL_TREE, true,
9426 : : GSI_SAME_STMT);
9427 : :
9428 : : /* Move the value to the LOADEDI temporary. */
9429 : 2403 : if (gimple_in_ssa_p (cfun))
9430 : : {
9431 : 39 : gcc_assert (gimple_seq_empty_p (phi_nodes (loop_header)));
9432 : 39 : phi = create_phi_node (loadedi, loop_header);
9433 : 39 : SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (load_bb)),
9434 : : initial);
9435 : : }
9436 : : else
9437 : 2364 : gsi_insert_before (&si,
9438 : 2364 : gimple_build_assign (loadedi, initial),
9439 : : GSI_SAME_STMT);
9440 : 2403 : if (loadedi != loaded_val)
9441 : : {
9442 : 799 : gimple_stmt_iterator gsi2;
9443 : 799 : tree x;
9444 : :
9445 : 799 : x = build1 (VIEW_CONVERT_EXPR, type, loadedi);
9446 : 799 : gsi2 = gsi_start_bb (loop_header);
9447 : 799 : if (gimple_in_ssa_p (cfun))
9448 : : {
9449 : 15 : gassign *stmt;
9450 : 15 : x = force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
9451 : : true, GSI_SAME_STMT);
9452 : 15 : stmt = gimple_build_assign (loaded_val, x);
9453 : 15 : gsi_insert_before (&gsi2, stmt, GSI_SAME_STMT);
9454 : : }
9455 : : else
9456 : : {
9457 : 784 : x = build2 (MODIFY_EXPR, TREE_TYPE (loaded_val), loaded_val, x);
9458 : 784 : force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
9459 : : true, GSI_SAME_STMT);
9460 : : }
9461 : : }
9462 : 2403 : gsi_remove (&si, true);
9463 : :
9464 : 2403 : si = gsi_last_nondebug_bb (store_bb);
9465 : 2403 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
9466 : :
9467 : 2403 : if (iaddr == addr)
9468 : : storedi = stored_val;
9469 : : else
9470 : 799 : storedi
9471 : 799 : = force_gimple_operand_gsi (&si,
9472 : : build1 (VIEW_CONVERT_EXPR, itype,
9473 : : stored_val), true, NULL_TREE, true,
9474 : : GSI_SAME_STMT);
9475 : :
9476 : : /* Build the compare&swap statement. */
9477 : 2403 : tree ctype = build_complex_type (itype);
9478 : 2403 : int flag = int_size_in_bytes (itype);
9479 : 2403 : new_storedi = build_call_expr_internal_loc (loc, IFN_ATOMIC_COMPARE_EXCHANGE,
9480 : : ctype, 6, iaddr, loadedi,
9481 : : storedi,
9482 : : build_int_cst (integer_type_node,
9483 : 2403 : flag),
9484 : : mo, fmo);
9485 : 2403 : new_storedi = build1 (REALPART_EXPR, itype, new_storedi);
9486 : 2403 : new_storedi = force_gimple_operand_gsi (&si,
9487 : 2403 : fold_convert (TREE_TYPE (loadedi),
9488 : : new_storedi),
9489 : : true, NULL_TREE,
9490 : : true, GSI_SAME_STMT);
9491 : :
9492 : 2403 : if (gimple_in_ssa_p (cfun))
9493 : : old_vali = loadedi;
9494 : : else
9495 : : {
9496 : 2364 : old_vali = create_tmp_var (TREE_TYPE (loadedi));
9497 : 2364 : stmt = gimple_build_assign (old_vali, loadedi);
9498 : 2364 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9499 : :
9500 : 2364 : stmt = gimple_build_assign (loadedi, new_storedi);
9501 : 2364 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9502 : : }
9503 : :
9504 : : /* Note that we always perform the comparison as an integer, even for
9505 : : floating point. This allows the atomic operation to properly
9506 : : succeed even with NaNs and -0.0. */
9507 : 2403 : tree ne = build2 (NE_EXPR, boolean_type_node, new_storedi, old_vali);
9508 : 2403 : stmt = gimple_build_cond_empty (ne);
9509 : 2403 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9510 : :
9511 : : /* Update cfg. */
9512 : 2403 : e = single_succ_edge (store_bb);
9513 : 2403 : e->flags &= ~EDGE_FALLTHRU;
9514 : 2403 : e->flags |= EDGE_FALSE_VALUE;
9515 : : /* Expect no looping. */
9516 : 2403 : e->probability = profile_probability::guessed_always ();
9517 : :
9518 : 2403 : e = make_edge (store_bb, loop_header, EDGE_TRUE_VALUE);
9519 : 2403 : e->probability = profile_probability::guessed_never ();
9520 : :
9521 : : /* Copy the new value to loadedi (we already did that before the condition
9522 : : if we are not in SSA). */
9523 : 2403 : if (gimple_in_ssa_p (cfun))
9524 : : {
9525 : 39 : phi = gimple_seq_first_stmt (phi_nodes (loop_header));
9526 : 39 : SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), new_storedi);
9527 : : }
9528 : :
9529 : : /* Remove GIMPLE_OMP_ATOMIC_STORE. */
9530 : 2403 : stmt = gsi_stmt (si);
9531 : 2403 : gsi_remove (&si, true);
9532 : 2403 : if (gimple_in_ssa_p (cfun))
9533 : 39 : release_defs (stmt);
9534 : :
9535 : 2403 : class loop *loop = alloc_loop ();
9536 : 2403 : loop->header = loop_header;
9537 : 2403 : loop->latch = store_bb;
9538 : 2403 : add_loop (loop, loop_header->loop_father);
9539 : :
9540 : 2403 : return true;
9541 : : }
9542 : :
9543 : : /* A subroutine of expand_omp_atomic. Implement the atomic operation as:
9544 : :
9545 : : GOMP_atomic_start ();
9546 : : *addr = rhs;
9547 : : GOMP_atomic_end ();
9548 : :
9549 : : The result is not globally atomic, but works so long as all parallel
9550 : : references are within #pragma omp atomic directives. According to
9551 : : responses received from omp@openmp.org, appears to be within spec.
9552 : : Which makes sense, since that's how several other compilers handle
9553 : : this situation as well.
9554 : : LOADED_VAL and ADDR are the operands of GIMPLE_OMP_ATOMIC_LOAD we're
9555 : : expanding. STORED_VAL is the operand of the matching
9556 : : GIMPLE_OMP_ATOMIC_STORE.
9557 : :
9558 : : We replace
9559 : : GIMPLE_OMP_ATOMIC_LOAD (loaded_val, addr) with
9560 : : loaded_val = *addr;
9561 : :
9562 : : and replace
9563 : : GIMPLE_OMP_ATOMIC_STORE (stored_val) with
9564 : : *addr = stored_val;
9565 : : */
9566 : :
9567 : : static bool
9568 : 289 : expand_omp_atomic_mutex (basic_block load_bb, basic_block store_bb,
9569 : : tree addr, tree loaded_val, tree stored_val)
9570 : : {
9571 : 289 : gimple_stmt_iterator si;
9572 : 289 : gassign *stmt;
9573 : 289 : tree t;
9574 : :
9575 : 289 : si = gsi_last_nondebug_bb (load_bb);
9576 : 289 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
9577 : :
9578 : 289 : t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
9579 : 289 : t = build_call_expr (t, 0);
9580 : 289 : force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
9581 : :
9582 : 289 : tree mem = build_simple_mem_ref (addr);
9583 : 289 : TREE_TYPE (mem) = TREE_TYPE (loaded_val);
9584 : 289 : TREE_OPERAND (mem, 1)
9585 : 289 : = fold_convert (build_pointer_type_for_mode (TREE_TYPE (mem), ptr_mode,
9586 : : true),
9587 : : TREE_OPERAND (mem, 1));
9588 : 289 : stmt = gimple_build_assign (loaded_val, mem);
9589 : 289 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9590 : 289 : gsi_remove (&si, true);
9591 : :
9592 : 289 : si = gsi_last_nondebug_bb (store_bb);
9593 : 289 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
9594 : :
9595 : 289 : stmt = gimple_build_assign (unshare_expr (mem), stored_val);
9596 : 578 : gimple_set_vuse (stmt, gimple_vuse (gsi_stmt (si)));
9597 : 578 : gimple_set_vdef (stmt, gimple_vdef (gsi_stmt (si)));
9598 : 289 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9599 : :
9600 : 289 : t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
9601 : 289 : t = build_call_expr (t, 0);
9602 : 289 : force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
9603 : 289 : gsi_remove (&si, true);
9604 : 289 : return true;
9605 : : }
9606 : :
9607 : : /* Expand an GIMPLE_OMP_ATOMIC statement. We try to expand
9608 : : using expand_omp_atomic_fetch_op. If it failed, we try to
9609 : : call expand_omp_atomic_pipeline, and if it fails too, the
9610 : : ultimate fallback is wrapping the operation in a mutex
9611 : : (expand_omp_atomic_mutex). REGION is the atomic region built
9612 : : by build_omp_regions_1(). */
9613 : :
9614 : : static void
9615 : 9493 : expand_omp_atomic (struct omp_region *region)
9616 : : {
9617 : 9493 : basic_block load_bb = region->entry, store_bb = region->exit;
9618 : 9493 : gomp_atomic_load *load
9619 : 9493 : = as_a <gomp_atomic_load *> (last_nondebug_stmt (load_bb));
9620 : 9493 : gomp_atomic_store *store
9621 : 9493 : = as_a <gomp_atomic_store *> (last_nondebug_stmt (store_bb));
9622 : 9493 : tree loaded_val = gimple_omp_atomic_load_lhs (load);
9623 : 9493 : tree addr = gimple_omp_atomic_load_rhs (load);
9624 : 9493 : tree stored_val = gimple_omp_atomic_store_val (store);
9625 : 9493 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9626 : 9493 : HOST_WIDE_INT index;
9627 : :
9628 : : /* Make sure the type is one of the supported sizes. */
9629 : 9493 : index = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9630 : 9493 : index = exact_log2 (index);
9631 : 9459 : if (index >= 0 && index <= 4)
9632 : : {
9633 : 9459 : unsigned int align = TYPE_ALIGN_UNIT (type);
9634 : :
9635 : : /* __sync builtins require strict data alignment. */
9636 : 18918 : if (exact_log2 (align) >= index)
9637 : : {
9638 : : /* Atomic load. */
9639 : 9283 : scalar_mode smode;
9640 : 9283 : if (loaded_val == stored_val
9641 : 1187 : && (is_int_mode (TYPE_MODE (type), &smode)
9642 : 1337 : || is_float_mode (TYPE_MODE (type), &smode))
9643 : 1187 : && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
9644 : 10466 : && expand_omp_atomic_load (load_bb, addr, loaded_val, index))
9645 : 9493 : return;
9646 : :
9647 : : /* Atomic store. */
9648 : 8100 : if ((is_int_mode (TYPE_MODE (type), &smode)
9649 : 9099 : || is_float_mode (TYPE_MODE (type), &smode))
9650 : 8100 : && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
9651 : 8021 : && store_bb == single_succ (load_bb)
9652 : 7406 : && first_stmt (store_bb) == store
9653 : 861 : && expand_omp_atomic_store (load_bb, addr, loaded_val,
9654 : : stored_val, index))
9655 : : return;
9656 : :
9657 : : /* When possible, use specialized atomic update functions. */
9658 : 939 : if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
9659 : 6308 : && store_bb == single_succ (load_bb)
9660 : 13000 : && expand_omp_atomic_fetch_op (load_bb, addr,
9661 : : loaded_val, stored_val, index))
9662 : : return;
9663 : :
9664 : : /* When possible, use ATOMIC_COMPARE_EXCHANGE ifn without a loop. */
9665 : 2771 : if (store_bb == single_succ (load_bb)
9666 : 2152 : && !gimple_in_ssa_p (cfun)
9667 : 4884 : && expand_omp_atomic_cas (load_bb, addr, loaded_val, stored_val,
9668 : : index))
9669 : : return;
9670 : :
9671 : : /* If we don't have specialized __sync builtins, try and implement
9672 : : as a compare and swap loop. */
9673 : 2482 : if (expand_omp_atomic_pipeline (load_bb, store_bb, addr,
9674 : : loaded_val, stored_val, index))
9675 : : return;
9676 : : }
9677 : : }
9678 : :
9679 : : /* The ultimate fallback is wrapping the operation in a mutex. */
9680 : 289 : expand_omp_atomic_mutex (load_bb, store_bb, addr, loaded_val, stored_val);
9681 : : }
9682 : :
9683 : : /* Mark the loops inside the kernels region starting at REGION_ENTRY and ending
9684 : : at REGION_EXIT. */
9685 : :
9686 : : static void
9687 : 1664 : mark_loops_in_oacc_kernels_region (basic_block region_entry,
9688 : : basic_block region_exit)
9689 : : {
9690 : 1664 : class loop *outer = region_entry->loop_father;
9691 : 1664 : gcc_assert (region_exit == NULL || outer == region_exit->loop_father);
9692 : :
9693 : : /* Don't parallelize the kernels region if it contains more than one outer
9694 : : loop. */
9695 : 1664 : unsigned int nr_outer_loops = 0;
9696 : 1664 : class loop *single_outer = NULL;
9697 : 33094 : for (class loop *loop = outer->inner; loop != NULL; loop = loop->next)
9698 : : {
9699 : 31430 : gcc_assert (loop_outer (loop) == outer);
9700 : :
9701 : 31430 : if (!dominated_by_p (CDI_DOMINATORS, loop->header, region_entry))
9702 : 23386 : continue;
9703 : :
9704 : 14600 : if (region_exit != NULL
9705 : 8044 : && dominated_by_p (CDI_DOMINATORS, loop->header, region_exit))
9706 : 6556 : continue;
9707 : :
9708 : 1488 : nr_outer_loops++;
9709 : 1488 : single_outer = loop;
9710 : : }
9711 : 1664 : if (nr_outer_loops != 1)
9712 : : return;
9713 : :
9714 : 1350 : for (class loop *loop = single_outer->inner;
9715 : 2319 : loop != NULL;
9716 : 969 : loop = loop->inner)
9717 : 1057 : if (loop->next)
9718 : : return;
9719 : :
9720 : : /* Mark the loops in the region. */
9721 : 3309 : for (class loop *loop = single_outer; loop != NULL; loop = loop->inner)
9722 : 2047 : loop->in_oacc_kernels_region = true;
9723 : : }
9724 : :
9725 : : /* Build target argument identifier from the DEVICE identifier, value
9726 : : identifier ID and whether the element also has a SUBSEQUENT_PARAM. */
9727 : :
9728 : : static tree
9729 : 22134 : get_target_argument_identifier_1 (int device, bool subseqent_param, int id)
9730 : : {
9731 : 22134 : tree t = build_int_cst (integer_type_node, device);
9732 : 22134 : if (subseqent_param)
9733 : 703 : t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9734 : : build_int_cst (integer_type_node,
9735 : : GOMP_TARGET_ARG_SUBSEQUENT_PARAM));
9736 : 22134 : t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9737 : : build_int_cst (integer_type_node, id));
9738 : 22134 : return t;
9739 : : }
9740 : :
9741 : : /* Like above but return it in type that can be directly stored as an element
9742 : : of the argument array. */
9743 : :
9744 : : static tree
9745 : 703 : get_target_argument_identifier (int device, bool subseqent_param, int id)
9746 : : {
9747 : 703 : tree t = get_target_argument_identifier_1 (device, subseqent_param, id);
9748 : 703 : return fold_convert (ptr_type_node, t);
9749 : : }
9750 : :
9751 : : /* Return a target argument consisting of DEVICE identifier, value identifier
9752 : : ID, and the actual VALUE. */
9753 : :
9754 : : static tree
9755 : 21431 : get_target_argument_value (gimple_stmt_iterator *gsi, int device, int id,
9756 : : tree value)
9757 : : {
9758 : 21431 : tree t = fold_build2 (LSHIFT_EXPR, integer_type_node,
9759 : : fold_convert (integer_type_node, value),
9760 : : build_int_cst (unsigned_type_node,
9761 : : GOMP_TARGET_ARG_VALUE_SHIFT));
9762 : 21431 : t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9763 : : get_target_argument_identifier_1 (device, false, id));
9764 : 21431 : t = fold_convert (ptr_type_node, t);
9765 : 21431 : return force_gimple_operand_gsi (gsi, t, true, NULL, true, GSI_SAME_STMT);
9766 : : }
9767 : :
9768 : : /* If VALUE is an integer constant greater than -2^15 and smaller than 2^15,
9769 : : push one argument to ARGS with both the DEVICE, ID and VALUE embedded in it,
9770 : : otherwise push an identifier (with DEVICE and ID) and the VALUE in two
9771 : : arguments. */
9772 : :
9773 : : static void
9774 : 22134 : push_target_argument_according_to_value (gimple_stmt_iterator *gsi, int device,
9775 : : int id, tree value, vec <tree> *args)
9776 : : {
9777 : 22134 : if (tree_fits_shwi_p (value)
9778 : 21431 : && tree_to_shwi (value) > -(1 << 15)
9779 : 21431 : && tree_to_shwi (value) < (1 << 15))
9780 : 21431 : args->quick_push (get_target_argument_value (gsi, device, id, value));
9781 : : else
9782 : : {
9783 : 703 : args->quick_push (get_target_argument_identifier (device, true, id));
9784 : 703 : value = fold_convert (ptr_type_node, value);
9785 : 703 : value = force_gimple_operand_gsi (gsi, value, true, NULL, true,
9786 : : GSI_SAME_STMT);
9787 : 703 : args->quick_push (value);
9788 : : }
9789 : 22134 : }
9790 : :
9791 : : /* Create an array of arguments that is then passed to GOMP_target. */
9792 : :
9793 : : static tree
9794 : 11067 : get_target_arguments (gimple_stmt_iterator *gsi, gomp_target *tgt_stmt)
9795 : : {
9796 : 11067 : auto_vec <tree, 6> args;
9797 : 11067 : tree clauses = gimple_omp_target_clauses (tgt_stmt);
9798 : 11067 : tree t, c = omp_find_clause (clauses, OMP_CLAUSE_NUM_TEAMS);
9799 : 11067 : if (c)
9800 : 11067 : t = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
9801 : : else
9802 : 0 : t = integer_minus_one_node;
9803 : 11067 : push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
9804 : : GOMP_TARGET_ARG_NUM_TEAMS, t, &args);
9805 : :
9806 : 11067 : c = omp_find_clause (clauses, OMP_CLAUSE_THREAD_LIMIT);
9807 : 11067 : if (c)
9808 : 11067 : t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
9809 : : else
9810 : 0 : t = integer_minus_one_node;
9811 : 11067 : push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
9812 : : GOMP_TARGET_ARG_THREAD_LIMIT, t,
9813 : : &args);
9814 : :
9815 : : /* Produce more, perhaps device specific, arguments here. */
9816 : :
9817 : 22134 : tree argarray = create_tmp_var (build_array_type_nelts (ptr_type_node,
9818 : 11067 : args.length () + 1),
9819 : : ".omp_target_args");
9820 : 33904 : for (unsigned i = 0; i < args.length (); i++)
9821 : : {
9822 : 22837 : tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
9823 : 22837 : build_int_cst (integer_type_node, i),
9824 : : NULL_TREE, NULL_TREE);
9825 : 22837 : gsi_insert_before (gsi, gimple_build_assign (ref, args[i]),
9826 : : GSI_SAME_STMT);
9827 : : }
9828 : 22134 : tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
9829 : 11067 : build_int_cst (integer_type_node, args.length ()),
9830 : : NULL_TREE, NULL_TREE);
9831 : 11067 : gsi_insert_before (gsi, gimple_build_assign (ref, null_pointer_node),
9832 : : GSI_SAME_STMT);
9833 : 11067 : TREE_ADDRESSABLE (argarray) = 1;
9834 : 11067 : return build_fold_addr_expr (argarray);
9835 : 11067 : }
9836 : :
9837 : : /* Expand the GIMPLE_OMP_TARGET starting at REGION. */
9838 : :
9839 : : static void
9840 : 35807 : expand_omp_target (struct omp_region *region)
9841 : : {
9842 : 35807 : basic_block entry_bb, exit_bb, new_bb;
9843 : 35807 : struct function *child_cfun;
9844 : 35807 : tree child_fn, child_fn2, block, t, c;
9845 : 35807 : gimple_stmt_iterator gsi;
9846 : 35807 : gomp_target *entry_stmt;
9847 : 35807 : gimple *stmt;
9848 : 35807 : edge e;
9849 : 35807 : bool offloaded;
9850 : 35807 : int target_kind;
9851 : :
9852 : 35807 : entry_stmt = as_a <gomp_target *> (last_nondebug_stmt (region->entry));
9853 : 35807 : target_kind = gimple_omp_target_kind (entry_stmt);
9854 : 35807 : new_bb = region->entry;
9855 : :
9856 : 35807 : offloaded = is_gimple_omp_offloaded (entry_stmt);
9857 : 35807 : switch (target_kind)
9858 : : {
9859 : 35807 : case GF_OMP_TARGET_KIND_REGION:
9860 : 35807 : case GF_OMP_TARGET_KIND_UPDATE:
9861 : 35807 : case GF_OMP_TARGET_KIND_ENTER_DATA:
9862 : 35807 : case GF_OMP_TARGET_KIND_EXIT_DATA:
9863 : 35807 : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
9864 : 35807 : case GF_OMP_TARGET_KIND_OACC_KERNELS:
9865 : 35807 : case GF_OMP_TARGET_KIND_OACC_SERIAL:
9866 : 35807 : case GF_OMP_TARGET_KIND_OACC_UPDATE:
9867 : 35807 : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
9868 : 35807 : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
9869 : 35807 : case GF_OMP_TARGET_KIND_OACC_DECLARE:
9870 : 35807 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
9871 : 35807 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
9872 : 35807 : case GF_OMP_TARGET_KIND_DATA:
9873 : 35807 : case GF_OMP_TARGET_KIND_OACC_DATA:
9874 : 35807 : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
9875 : 35807 : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
9876 : 35807 : break;
9877 : 0 : default:
9878 : 0 : gcc_unreachable ();
9879 : : }
9880 : :
9881 : 35807 : tree clauses = gimple_omp_target_clauses (entry_stmt);
9882 : :
9883 : 35807 : bool is_ancestor = false;
9884 : 35807 : child_fn = child_fn2 = NULL_TREE;
9885 : 35807 : child_cfun = NULL;
9886 : 35807 : if (offloaded)
9887 : : {
9888 : 20326 : c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE);
9889 : 20326 : if (ENABLE_OFFLOADING && c)
9890 : : is_ancestor = OMP_CLAUSE_DEVICE_ANCESTOR (c);
9891 : 20326 : child_fn = gimple_omp_target_child_fn (entry_stmt);
9892 : 20326 : child_cfun = DECL_STRUCT_FUNCTION (child_fn);
9893 : : }
9894 : :
9895 : : /* Supported by expand_omp_taskreg, but not here. */
9896 : 20326 : if (child_cfun != NULL)
9897 : 20326 : gcc_checking_assert (!child_cfun->cfg);
9898 : 35807 : gcc_checking_assert (!gimple_in_ssa_p (cfun));
9899 : :
9900 : 35807 : entry_bb = region->entry;
9901 : 35807 : exit_bb = region->exit;
9902 : :
9903 : 35807 : if (target_kind == GF_OMP_TARGET_KIND_OACC_KERNELS)
9904 : 1664 : mark_loops_in_oacc_kernels_region (region->entry, region->exit);
9905 : :
9906 : : /* Going on, all OpenACC compute constructs are mapped to
9907 : : 'BUILT_IN_GOACC_PARALLEL', and get their compute regions outlined.
9908 : : To distinguish between them, we attach attributes. */
9909 : 35807 : switch (target_kind)
9910 : : {
9911 : 6655 : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
9912 : 6655 : DECL_ATTRIBUTES (child_fn)
9913 : 6655 : = tree_cons (get_identifier ("oacc parallel"),
9914 : 6655 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9915 : 6655 : break;
9916 : 1664 : case GF_OMP_TARGET_KIND_OACC_KERNELS:
9917 : 1664 : DECL_ATTRIBUTES (child_fn)
9918 : 1664 : = tree_cons (get_identifier ("oacc kernels"),
9919 : 1664 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9920 : 1664 : break;
9921 : 757 : case GF_OMP_TARGET_KIND_OACC_SERIAL:
9922 : 757 : DECL_ATTRIBUTES (child_fn)
9923 : 757 : = tree_cons (get_identifier ("oacc serial"),
9924 : 757 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9925 : 757 : break;
9926 : 62 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
9927 : 62 : DECL_ATTRIBUTES (child_fn)
9928 : 62 : = tree_cons (get_identifier ("oacc parallel_kernels_parallelized"),
9929 : 62 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9930 : 62 : break;
9931 : 121 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
9932 : 121 : DECL_ATTRIBUTES (child_fn)
9933 : 121 : = tree_cons (get_identifier ("oacc parallel_kernels_gang_single"),
9934 : 121 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9935 : 121 : break;
9936 : 26548 : default:
9937 : : /* Make sure we don't miss any. */
9938 : 26548 : gcc_checking_assert (!(is_gimple_omp_oacc (entry_stmt)
9939 : : && is_gimple_omp_offloaded (entry_stmt)));
9940 : : break;
9941 : : }
9942 : :
9943 : 35807 : if (offloaded)
9944 : : {
9945 : 20326 : unsigned srcidx, dstidx, num;
9946 : :
9947 : : /* If the offloading region needs data sent from the parent
9948 : : function, then the very first statement (except possible
9949 : : tree profile counter updates) of the offloading body
9950 : : is a copy assignment .OMP_DATA_I = &.OMP_DATA_O. Since
9951 : : &.OMP_DATA_O is passed as an argument to the child function,
9952 : : we need to replace it with the argument as seen by the child
9953 : : function.
9954 : :
9955 : : In most cases, this will end up being the identity assignment
9956 : : .OMP_DATA_I = .OMP_DATA_I. However, if the offloading body had
9957 : : a function call that has been inlined, the original PARM_DECL
9958 : : .OMP_DATA_I may have been converted into a different local
9959 : : variable. In which case, we need to keep the assignment. */
9960 : 20326 : tree data_arg = gimple_omp_target_data_arg (entry_stmt);
9961 : 20326 : if (data_arg)
9962 : : {
9963 : 15749 : basic_block entry_succ_bb = single_succ (entry_bb);
9964 : 15749 : gimple_stmt_iterator gsi;
9965 : 15749 : tree arg;
9966 : 15749 : gimple *tgtcopy_stmt = NULL;
9967 : 15749 : tree sender = TREE_VEC_ELT (data_arg, 0);
9968 : :
9969 : 31498 : for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
9970 : : {
9971 : 15749 : gcc_assert (!gsi_end_p (gsi));
9972 : 15749 : stmt = gsi_stmt (gsi);
9973 : 15749 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
9974 : 0 : continue;
9975 : :
9976 : 15749 : if (gimple_num_ops (stmt) == 2)
9977 : : {
9978 : 15749 : tree arg = gimple_assign_rhs1 (stmt);
9979 : :
9980 : : /* We're ignoring the subcode because we're
9981 : : effectively doing a STRIP_NOPS. */
9982 : :
9983 : 15749 : if ((TREE_CODE (arg) == ADDR_EXPR
9984 : 15672 : && TREE_OPERAND (arg, 0) == sender)
9985 : 15749 : || arg == sender)
9986 : : {
9987 : 15749 : tgtcopy_stmt = stmt;
9988 : 15749 : break;
9989 : : }
9990 : : }
9991 : : }
9992 : :
9993 : 15749 : gcc_assert (tgtcopy_stmt != NULL);
9994 : 15749 : arg = DECL_ARGUMENTS (child_fn);
9995 : :
9996 : 15749 : gcc_assert (gimple_assign_lhs (tgtcopy_stmt) == arg);
9997 : 15749 : gsi_remove (&gsi, true);
9998 : : }
9999 : :
10000 : : /* Declare local variables needed in CHILD_CFUN. */
10001 : 20326 : block = DECL_INITIAL (child_fn);
10002 : 20326 : BLOCK_VARS (block) = vec2chain (child_cfun->local_decls);
10003 : : /* The gimplifier could record temporaries in the offloading block
10004 : : rather than in containing function's local_decls chain,
10005 : : which would mean cgraph missed finalizing them. Do it now. */
10006 : 145696 : for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
10007 : 125370 : if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
10008 : 0 : varpool_node::finalize_decl (t);
10009 : 20326 : DECL_SAVED_TREE (child_fn) = NULL;
10010 : : /* We'll create a CFG for child_fn, so no gimple body is needed. */
10011 : 20326 : gimple_set_body (child_fn, NULL);
10012 : 20326 : TREE_USED (block) = 1;
10013 : :
10014 : : /* Reset DECL_CONTEXT on function arguments. */
10015 : 40652 : for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
10016 : 20326 : DECL_CONTEXT (t) = child_fn;
10017 : :
10018 : : /* Split ENTRY_BB at GIMPLE_*,
10019 : : so that it can be moved to the child function. */
10020 : 20326 : gsi = gsi_last_nondebug_bb (entry_bb);
10021 : 20326 : stmt = gsi_stmt (gsi);
10022 : 20326 : gcc_assert (stmt
10023 : : && gimple_code (stmt) == gimple_code (entry_stmt));
10024 : 20326 : e = split_block (entry_bb, stmt);
10025 : 20326 : gsi_remove (&gsi, true);
10026 : 20326 : entry_bb = e->dest;
10027 : 20326 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
10028 : :
10029 : : /* Convert GIMPLE_OMP_RETURN into a RETURN_EXPR. */
10030 : 20326 : if (exit_bb)
10031 : : {
10032 : 20249 : gsi = gsi_last_nondebug_bb (exit_bb);
10033 : 20249 : gcc_assert (!gsi_end_p (gsi)
10034 : : && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
10035 : 20249 : stmt = gimple_build_return (NULL);
10036 : 20249 : gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
10037 : 20249 : gsi_remove (&gsi, true);
10038 : : }
10039 : :
10040 : : /* Move the offloading region into CHILD_CFUN. */
10041 : :
10042 : 20326 : block = gimple_block (entry_stmt);
10043 : :
10044 : 20326 : new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
10045 : 20326 : if (exit_bb)
10046 : 20249 : single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
10047 : : /* When the OMP expansion process cannot guarantee an up-to-date
10048 : : loop tree arrange for the child function to fixup loops. */
10049 : 20326 : if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
10050 : 20326 : child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
10051 : :
10052 : : /* Remove non-local VAR_DECLs from child_cfun->local_decls list. */
10053 : 20326 : num = vec_safe_length (child_cfun->local_decls);
10054 : 604233 : for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
10055 : : {
10056 : 583907 : t = (*child_cfun->local_decls)[srcidx];
10057 : 583907 : if (DECL_CONTEXT (t) == cfun->decl)
10058 : 125370 : continue;
10059 : 458537 : if (srcidx != dstidx)
10060 : 443569 : (*child_cfun->local_decls)[dstidx] = t;
10061 : 458537 : dstidx++;
10062 : : }
10063 : 20326 : if (dstidx != num)
10064 : 17030 : vec_safe_truncate (child_cfun->local_decls, dstidx);
10065 : :
10066 : : /* Inform the callgraph about the new function. */
10067 : 20326 : child_cfun->curr_properties = cfun->curr_properties;
10068 : 20326 : child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
10069 : 20326 : child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
10070 : 20326 : cgraph_node *node = cgraph_node::get_create (child_fn);
10071 : 20326 : node->parallelized_function = 1;
10072 : 40652 : node->has_omp_variant_constructs
10073 : 20326 : |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
10074 : 20326 : cgraph_node::add_new_function (child_fn, true);
10075 : :
10076 : : /* Add the new function to the offload table. */
10077 : 20326 : if (ENABLE_OFFLOADING)
10078 : : {
10079 : : if (in_lto_p)
10080 : : DECL_PRESERVE_P (child_fn) = 1;
10081 : : if (!is_ancestor)
10082 : : vec_safe_push (offload_funcs, child_fn);
10083 : : }
10084 : :
10085 : 20326 : bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
10086 : 20326 : && !DECL_ASSEMBLER_NAME_SET_P (child_fn);
10087 : :
10088 : : /* Fix the callgraph edges for child_cfun. Those for cfun will be
10089 : : fixed in a following pass. */
10090 : 20326 : push_cfun (child_cfun);
10091 : 20326 : if (need_asm)
10092 : 20320 : assign_assembler_name_if_needed (child_fn);
10093 : 20326 : cgraph_edge::rebuild_edges ();
10094 : :
10095 : : /* Some EH regions might become dead, see PR34608. If
10096 : : pass_cleanup_cfg isn't the first pass to happen with the
10097 : : new child, these dead EH edges might cause problems.
10098 : : Clean them up now. */
10099 : 20326 : if (flag_exceptions)
10100 : : {
10101 : 8087 : basic_block bb;
10102 : 8087 : bool changed = false;
10103 : :
10104 : 114023 : FOR_EACH_BB_FN (bb, cfun)
10105 : 105936 : changed |= gimple_purge_dead_eh_edges (bb);
10106 : 8087 : if (changed)
10107 : 0 : cleanup_tree_cfg ();
10108 : : }
10109 : 20326 : if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
10110 : 0 : verify_loop_structure ();
10111 : 20326 : pop_cfun ();
10112 : :
10113 : 20326 : if (dump_file && !gimple_in_ssa_p (cfun))
10114 : : {
10115 : 31 : omp_any_child_fn_dumped = true;
10116 : 31 : dump_function_header (dump_file, child_fn, dump_flags);
10117 : 31 : dump_function_to_file (child_fn, dump_file, dump_flags);
10118 : : }
10119 : :
10120 : 20326 : adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
10121 : :
10122 : : /* Handle the case that an inner ancestor:1 target is called by an outer
10123 : : target region. */
10124 : 20326 : if (is_ancestor)
10125 : : {
10126 : : cgraph_node *fn2_node;
10127 : : child_fn2 = build_decl (DECL_SOURCE_LOCATION (child_fn),
10128 : : FUNCTION_DECL,
10129 : : clone_function_name (child_fn, "nohost"),
10130 : : TREE_TYPE (child_fn));
10131 : : if (in_lto_p)
10132 : : DECL_PRESERVE_P (child_fn2) = 1;
10133 : : TREE_STATIC (child_fn2) = 1;
10134 : : DECL_ARTIFICIAL (child_fn2) = 1;
10135 : : DECL_IGNORED_P (child_fn2) = 0;
10136 : : TREE_PUBLIC (child_fn2) = 0;
10137 : : DECL_UNINLINABLE (child_fn2) = 1;
10138 : : DECL_EXTERNAL (child_fn2) = 0;
10139 : : DECL_CONTEXT (child_fn2) = DECL_CONTEXT (child_fn);
10140 : : DECL_INITIAL (child_fn2) = make_node (BLOCK);
10141 : : BLOCK_SUPERCONTEXT (DECL_INITIAL (child_fn2)) = child_fn2;
10142 : : DECL_ATTRIBUTES (child_fn)
10143 : : = remove_attribute ("omp target entrypoint",
10144 : : DECL_ATTRIBUTES (child_fn));
10145 : : DECL_ATTRIBUTES (child_fn2)
10146 : : = tree_cons (get_identifier ("omp target device_ancestor_nohost"),
10147 : : NULL_TREE, copy_list (DECL_ATTRIBUTES (child_fn)));
10148 : : DECL_ATTRIBUTES (child_fn)
10149 : : = tree_cons (get_identifier ("omp target device_ancestor_host"),
10150 : : NULL_TREE, DECL_ATTRIBUTES (child_fn));
10151 : : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (child_fn2)
10152 : : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
10153 : : DECL_FUNCTION_SPECIFIC_TARGET (child_fn2)
10154 : : = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
10155 : : DECL_FUNCTION_VERSIONED (child_fn2)
10156 : : = DECL_FUNCTION_VERSIONED (current_function_decl);
10157 : :
10158 : : fn2_node = cgraph_node::get_create (child_fn2);
10159 : : fn2_node->offloadable = 1;
10160 : : fn2_node->force_output = 1;
10161 : : node->offloadable = 0;
10162 : :
10163 : : /* Enable pass_omp_device_lower pass. */
10164 : : fn2_node = cgraph_node::get (DECL_CONTEXT (child_fn));
10165 : : fn2_node->has_omp_variant_constructs = 1;
10166 : :
10167 : : t = build_decl (DECL_SOURCE_LOCATION (child_fn),
10168 : : RESULT_DECL, NULL_TREE, void_type_node);
10169 : : DECL_ARTIFICIAL (t) = 1;
10170 : : DECL_IGNORED_P (t) = 1;
10171 : : DECL_CONTEXT (t) = child_fn2;
10172 : : DECL_RESULT (child_fn2) = t;
10173 : : DECL_SAVED_TREE (child_fn2) = build1 (RETURN_EXPR,
10174 : : void_type_node, NULL);
10175 : : tree tmp = DECL_ARGUMENTS (child_fn);
10176 : : t = build_decl (DECL_SOURCE_LOCATION (child_fn), PARM_DECL,
10177 : : DECL_NAME (tmp), TREE_TYPE (tmp));
10178 : : DECL_ARTIFICIAL (t) = 1;
10179 : : DECL_NAMELESS (t) = 1;
10180 : : DECL_ARG_TYPE (t) = ptr_type_node;
10181 : : DECL_CONTEXT (t) = current_function_decl;
10182 : : TREE_USED (t) = 1;
10183 : : TREE_READONLY (t) = 1;
10184 : : DECL_ARGUMENTS (child_fn2) = t;
10185 : : gcc_assert (TREE_CHAIN (tmp) == NULL_TREE);
10186 : :
10187 : : gimplify_function_tree (child_fn2);
10188 : : cgraph_node::add_new_function (child_fn2, true);
10189 : :
10190 : : vec_safe_push (offload_funcs, child_fn2);
10191 : : if (dump_file && !gimple_in_ssa_p (cfun))
10192 : : {
10193 : : dump_function_header (dump_file, child_fn2, dump_flags);
10194 : : dump_function_to_file (child_fn2, dump_file, dump_flags);
10195 : : }
10196 : : }
10197 : : }
10198 : :
10199 : : /* Emit a library call to launch the offloading region, or do data
10200 : : transfers. */
10201 : 35807 : tree t1, t2, t3, t4, depend;
10202 : 35807 : enum built_in_function start_ix;
10203 : 35807 : unsigned int flags_i = 0;
10204 : :
10205 : 35807 : switch (gimple_omp_target_kind (entry_stmt))
10206 : : {
10207 : : case GF_OMP_TARGET_KIND_REGION:
10208 : : start_ix = BUILT_IN_GOMP_TARGET;
10209 : : break;
10210 : : case GF_OMP_TARGET_KIND_DATA:
10211 : : start_ix = BUILT_IN_GOMP_TARGET_DATA;
10212 : : break;
10213 : : case GF_OMP_TARGET_KIND_UPDATE:
10214 : : start_ix = BUILT_IN_GOMP_TARGET_UPDATE;
10215 : : break;
10216 : : case GF_OMP_TARGET_KIND_ENTER_DATA:
10217 : : start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
10218 : : break;
10219 : : case GF_OMP_TARGET_KIND_EXIT_DATA:
10220 : : start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
10221 : : flags_i |= GOMP_TARGET_FLAG_EXIT_DATA;
10222 : : break;
10223 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
10224 : : case GF_OMP_TARGET_KIND_OACC_KERNELS:
10225 : : case GF_OMP_TARGET_KIND_OACC_SERIAL:
10226 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
10227 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
10228 : : start_ix = BUILT_IN_GOACC_PARALLEL;
10229 : : break;
10230 : : case GF_OMP_TARGET_KIND_OACC_DATA:
10231 : : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
10232 : : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
10233 : : start_ix = BUILT_IN_GOACC_DATA_START;
10234 : : break;
10235 : : case GF_OMP_TARGET_KIND_OACC_UPDATE:
10236 : : start_ix = BUILT_IN_GOACC_UPDATE;
10237 : : break;
10238 : : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
10239 : : start_ix = BUILT_IN_GOACC_ENTER_DATA;
10240 : : break;
10241 : : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
10242 : : start_ix = BUILT_IN_GOACC_EXIT_DATA;
10243 : : break;
10244 : : case GF_OMP_TARGET_KIND_OACC_DECLARE:
10245 : : start_ix = BUILT_IN_GOACC_DECLARE;
10246 : : break;
10247 : 0 : default:
10248 : 0 : gcc_unreachable ();
10249 : : }
10250 : :
10251 : 35807 : tree device = NULL_TREE;
10252 : 35807 : location_t device_loc = UNKNOWN_LOCATION;
10253 : 35807 : tree goacc_flags = NULL_TREE;
10254 : 35807 : bool need_device_adjustment = false;
10255 : 35807 : gimple_stmt_iterator adj_gsi;
10256 : 35807 : if (is_gimple_omp_oacc (entry_stmt))
10257 : : {
10258 : : /* By default, no GOACC_FLAGs are set. */
10259 : 14517 : goacc_flags = integer_zero_node;
10260 : : }
10261 : : else
10262 : : {
10263 : 21290 : c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE);
10264 : 21290 : if (c)
10265 : : {
10266 : 886 : device = OMP_CLAUSE_DEVICE_ID (c);
10267 : : /* Ensure 'device' is of the correct type. */
10268 : 886 : device = fold_convert_loc (device_loc, integer_type_node, device);
10269 : 886 : if (TREE_CODE (device) == INTEGER_CST)
10270 : : {
10271 : 50 : if (wi::to_wide (device) == GOMP_DEVICE_ICV)
10272 : 2 : device = build_int_cst (integer_type_node,
10273 : : GOMP_DEVICE_HOST_FALLBACK);
10274 : 48 : else if (wi::to_wide (device) == GOMP_DEVICE_HOST_FALLBACK)
10275 : 0 : device = build_int_cst (integer_type_node,
10276 : : GOMP_DEVICE_HOST_FALLBACK - 1);
10277 : : }
10278 : : else
10279 : : need_device_adjustment = true;
10280 : 886 : device_loc = OMP_CLAUSE_LOCATION (c);
10281 : 886 : if (OMP_CLAUSE_DEVICE_ANCESTOR (c))
10282 : 41 : device = build_int_cst (integer_type_node,
10283 : : GOMP_DEVICE_HOST_FALLBACK);
10284 : : }
10285 : : else
10286 : : {
10287 : : /* By default, the value of DEVICE is GOMP_DEVICE_ICV (let runtime
10288 : : library choose). */
10289 : 20404 : device = build_int_cst (integer_type_node, GOMP_DEVICE_ICV);
10290 : 20404 : device_loc = gimple_location (entry_stmt);
10291 : : }
10292 : :
10293 : 21290 : c = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT);
10294 : : /* FIXME: in_reduction(...) nowait is unimplemented yet, pretend
10295 : : nowait doesn't appear. */
10296 : 21290 : if (c && omp_find_clause (clauses, OMP_CLAUSE_IN_REDUCTION))
10297 : : c = NULL;
10298 : 21026 : if (c)
10299 : 137 : flags_i |= GOMP_TARGET_FLAG_NOWAIT;
10300 : : }
10301 : :
10302 : : /* By default, there is no conditional. */
10303 : 35807 : tree cond = NULL_TREE;
10304 : 35807 : c = omp_find_clause (clauses, OMP_CLAUSE_IF);
10305 : 35807 : if (c)
10306 : 1741 : cond = OMP_CLAUSE_IF_EXPR (c);
10307 : : /* If we found the clause 'if (cond)', build:
10308 : : OpenACC: goacc_flags = (cond ? goacc_flags
10309 : : : goacc_flags | GOACC_FLAG_HOST_FALLBACK)
10310 : : OpenMP: device = (cond ? device : GOMP_DEVICE_HOST_FALLBACK) */
10311 : 1741 : if (cond)
10312 : : {
10313 : 1741 : tree *tp;
10314 : 1741 : if (is_gimple_omp_oacc (entry_stmt))
10315 : : tp = &goacc_flags;
10316 : : else
10317 : 666 : tp = &device;
10318 : :
10319 : 1741 : cond = gimple_boolify (cond);
10320 : :
10321 : 1741 : basic_block cond_bb, then_bb, else_bb;
10322 : 1741 : edge e;
10323 : 1741 : tree tmp_var = create_tmp_var (TREE_TYPE (*tp));
10324 : 1741 : if (offloaded)
10325 : 806 : e = split_block_after_labels (new_bb);
10326 : : else
10327 : : {
10328 : 935 : gsi = gsi_last_nondebug_bb (new_bb);
10329 : 935 : gsi_prev (&gsi);
10330 : 935 : e = split_block (new_bb, gsi_stmt (gsi));
10331 : : }
10332 : 1741 : cond_bb = e->src;
10333 : 1741 : new_bb = e->dest;
10334 : 1741 : remove_edge (e);
10335 : :
10336 : 1741 : then_bb = create_empty_bb (cond_bb);
10337 : 1741 : else_bb = create_empty_bb (then_bb);
10338 : 1741 : set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10339 : 1741 : set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
10340 : :
10341 : 1741 : stmt = gimple_build_cond_empty (cond);
10342 : 1741 : gsi = gsi_last_bb (cond_bb);
10343 : 1741 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10344 : :
10345 : 1741 : gsi = gsi_start_bb (then_bb);
10346 : 1741 : stmt = gimple_build_assign (tmp_var, *tp);
10347 : 1741 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10348 : 1741 : adj_gsi = gsi;
10349 : :
10350 : 1741 : gsi = gsi_start_bb (else_bb);
10351 : 1741 : if (is_gimple_omp_oacc (entry_stmt))
10352 : 1075 : stmt = gimple_build_assign (tmp_var,
10353 : : BIT_IOR_EXPR,
10354 : : *tp,
10355 : : build_int_cst (integer_type_node,
10356 : : GOACC_FLAG_HOST_FALLBACK));
10357 : : else
10358 : 666 : stmt = gimple_build_assign (tmp_var,
10359 : : build_int_cst (integer_type_node,
10360 : : GOMP_DEVICE_HOST_FALLBACK));
10361 : 1741 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10362 : :
10363 : 1741 : make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10364 : 1741 : make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
10365 : 1741 : add_bb_to_loop (then_bb, cond_bb->loop_father);
10366 : 1741 : add_bb_to_loop (else_bb, cond_bb->loop_father);
10367 : 1741 : make_edge (then_bb, new_bb, EDGE_FALLTHRU);
10368 : 1741 : make_edge (else_bb, new_bb, EDGE_FALLTHRU);
10369 : :
10370 : 1741 : *tp = tmp_var;
10371 : :
10372 : 1741 : gsi = gsi_last_nondebug_bb (new_bb);
10373 : : }
10374 : : else
10375 : : {
10376 : 34066 : gsi = gsi_last_nondebug_bb (new_bb);
10377 : :
10378 : 34066 : if (device != NULL_TREE)
10379 : 20624 : device = force_gimple_operand_gsi (&gsi, device, true, NULL_TREE,
10380 : : true, GSI_SAME_STMT);
10381 : 34066 : if (need_device_adjustment)
10382 : : {
10383 : 362 : tree tmp_var = create_tmp_var (TREE_TYPE (device));
10384 : 362 : stmt = gimple_build_assign (tmp_var, device);
10385 : 362 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
10386 : 362 : adj_gsi = gsi_for_stmt (stmt);
10387 : 362 : device = tmp_var;
10388 : : }
10389 : : }
10390 : :
10391 : 35807 : if ((c = omp_find_clause (clauses, OMP_CLAUSE_SELF)) != NULL_TREE)
10392 : : {
10393 : 324 : gcc_assert ((is_gimple_omp_oacc (entry_stmt) && offloaded)
10394 : : || (gimple_omp_target_kind (entry_stmt)
10395 : : == GF_OMP_TARGET_KIND_OACC_DATA_KERNELS));
10396 : :
10397 : 87 : edge e;
10398 : 87 : if (offloaded)
10399 : 237 : e = split_block_after_labels (new_bb);
10400 : : else
10401 : : {
10402 : 87 : gsi = gsi_last_nondebug_bb (new_bb);
10403 : 87 : gsi_prev (&gsi);
10404 : 87 : e = split_block (new_bb, gsi_stmt (gsi));
10405 : : }
10406 : 324 : basic_block cond_bb = e->src;
10407 : 324 : new_bb = e->dest;
10408 : 324 : remove_edge (e);
10409 : :
10410 : 324 : basic_block then_bb = create_empty_bb (cond_bb);
10411 : 324 : basic_block else_bb = create_empty_bb (then_bb);
10412 : 324 : set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10413 : 324 : set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
10414 : :
10415 : 324 : tree self_cond = gimple_boolify (OMP_CLAUSE_SELF_EXPR (c));
10416 : 324 : stmt = gimple_build_cond_empty (self_cond);
10417 : 324 : gsi = gsi_last_bb (cond_bb);
10418 : 324 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10419 : :
10420 : 324 : tree tmp_var = create_tmp_var (TREE_TYPE (goacc_flags));
10421 : 324 : stmt = gimple_build_assign (tmp_var, BIT_IOR_EXPR, goacc_flags,
10422 : : build_int_cst (integer_type_node,
10423 : : GOACC_FLAG_LOCAL_DEVICE));
10424 : 324 : gsi = gsi_start_bb (then_bb);
10425 : 324 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10426 : :
10427 : 324 : gsi = gsi_start_bb (else_bb);
10428 : 324 : stmt = gimple_build_assign (tmp_var, goacc_flags);
10429 : 324 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10430 : :
10431 : 324 : make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10432 : 324 : make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
10433 : 324 : add_bb_to_loop (then_bb, cond_bb->loop_father);
10434 : 324 : add_bb_to_loop (else_bb, cond_bb->loop_father);
10435 : 324 : make_edge (then_bb, new_bb, EDGE_FALLTHRU);
10436 : 324 : make_edge (else_bb, new_bb, EDGE_FALLTHRU);
10437 : :
10438 : 324 : goacc_flags = tmp_var;
10439 : 324 : gsi = gsi_last_nondebug_bb (new_bb);
10440 : : }
10441 : :
10442 : 35807 : if (need_device_adjustment)
10443 : : {
10444 : 836 : tree uns = fold_convert (unsigned_type_node, device);
10445 : 836 : uns = force_gimple_operand_gsi (&adj_gsi, uns, true, NULL_TREE,
10446 : : false, GSI_CONTINUE_LINKING);
10447 : 836 : edge e = split_block (gsi_bb (adj_gsi), gsi_stmt (adj_gsi));
10448 : 836 : basic_block cond_bb = e->src;
10449 : 836 : basic_block else_bb = e->dest;
10450 : 836 : if (gsi_bb (adj_gsi) == new_bb)
10451 : : {
10452 : 362 : new_bb = else_bb;
10453 : 362 : gsi = gsi_last_nondebug_bb (new_bb);
10454 : : }
10455 : :
10456 : 836 : basic_block then_bb = create_empty_bb (cond_bb);
10457 : 836 : set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10458 : :
10459 : 836 : cond = build2 (GT_EXPR, boolean_type_node, uns,
10460 : : build_int_cst (unsigned_type_node,
10461 : : GOMP_DEVICE_HOST_FALLBACK - 1));
10462 : 836 : stmt = gimple_build_cond_empty (cond);
10463 : 836 : adj_gsi = gsi_last_bb (cond_bb);
10464 : 836 : gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
10465 : :
10466 : 836 : adj_gsi = gsi_start_bb (then_bb);
10467 : 836 : tree add = build2 (PLUS_EXPR, integer_type_node, device,
10468 : : build_int_cst (integer_type_node, -1));
10469 : 836 : stmt = gimple_build_assign (device, add);
10470 : 836 : gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
10471 : :
10472 : 836 : make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10473 : 836 : e->flags = EDGE_FALSE_VALUE;
10474 : 836 : add_bb_to_loop (then_bb, cond_bb->loop_father);
10475 : 836 : make_edge (then_bb, else_bb, EDGE_FALLTHRU);
10476 : : }
10477 : :
10478 : 35807 : t = gimple_omp_target_data_arg (entry_stmt);
10479 : 35807 : if (t == NULL)
10480 : : {
10481 : 4811 : t1 = size_zero_node;
10482 : 4811 : t2 = build_zero_cst (ptr_type_node);
10483 : 4811 : t3 = t2;
10484 : 4811 : t4 = t2;
10485 : : }
10486 : 30996 : else if (TREE_VEC_LENGTH (t) == 3 || is_gimple_omp_oacc (entry_stmt))
10487 : : {
10488 : 30874 : t1 = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (TREE_VEC_ELT (t, 1))));
10489 : 30874 : t1 = size_binop (PLUS_EXPR, t1, size_int (1));
10490 : 30874 : t2 = build_fold_addr_expr (TREE_VEC_ELT (t, 0));
10491 : 30874 : t3 = build_fold_addr_expr (TREE_VEC_ELT (t, 1));
10492 : 30874 : t4 = build_fold_addr_expr (TREE_VEC_ELT (t, 2));
10493 : : }
10494 : : else
10495 : : {
10496 : 122 : t1 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 3), true, NULL_TREE,
10497 : : true, GSI_SAME_STMT);
10498 : 122 : t2 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 0), true, NULL_TREE,
10499 : : true, GSI_SAME_STMT);
10500 : 122 : t3 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 1), true, NULL_TREE,
10501 : : true, GSI_SAME_STMT);
10502 : 122 : t4 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 2), true, NULL_TREE,
10503 : : true, GSI_SAME_STMT);
10504 : : }
10505 : :
10506 : 35807 : gimple *g;
10507 : 35807 : bool tagging = false;
10508 : : /* The maximum number used by any start_ix, without varargs. */
10509 : 35807 : auto_vec<tree, 11> args;
10510 : 35807 : if (is_gimple_omp_oacc (entry_stmt))
10511 : : {
10512 : 14517 : tree goacc_flags_m = fold_build1 (GOACC_FLAGS_MARSHAL_OP,
10513 : : TREE_TYPE (goacc_flags), goacc_flags);
10514 : 14517 : goacc_flags_m = force_gimple_operand_gsi (&gsi, goacc_flags_m, true,
10515 : : NULL_TREE, true,
10516 : : GSI_SAME_STMT);
10517 : 14517 : args.quick_push (goacc_flags_m);
10518 : : }
10519 : : else
10520 : 21290 : args.quick_push (device);
10521 : 35807 : if (offloaded)
10522 : 20326 : args.quick_push (build_fold_addr_expr (child_fn2 ? child_fn2 : child_fn));
10523 : 35807 : args.quick_push (t1);
10524 : 35807 : args.quick_push (t2);
10525 : 35807 : args.quick_push (t3);
10526 : 35807 : args.quick_push (t4);
10527 : 35807 : switch (start_ix)
10528 : : {
10529 : : case BUILT_IN_GOACC_DATA_START:
10530 : : case BUILT_IN_GOACC_DECLARE:
10531 : : case BUILT_IN_GOMP_TARGET_DATA:
10532 : : break;
10533 : 19665 : case BUILT_IN_GOMP_TARGET:
10534 : 19665 : case BUILT_IN_GOMP_TARGET_UPDATE:
10535 : 19665 : case BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA:
10536 : 19665 : args.quick_push (build_int_cst (unsigned_type_node, flags_i));
10537 : 19665 : c = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
10538 : 19665 : if (c)
10539 : 329 : depend = OMP_CLAUSE_DECL (c);
10540 : : else
10541 : 19336 : depend = build_int_cst (ptr_type_node, 0);
10542 : 19665 : args.quick_push (depend);
10543 : 19665 : if (start_ix == BUILT_IN_GOMP_TARGET)
10544 : 11067 : args.quick_push (get_target_arguments (&gsi, entry_stmt));
10545 : : break;
10546 : 9259 : case BUILT_IN_GOACC_PARALLEL:
10547 : 9259 : if (lookup_attribute ("oacc serial", DECL_ATTRIBUTES (child_fn)) != NULL)
10548 : : {
10549 : : tree dims = NULL_TREE;
10550 : : unsigned int ix;
10551 : :
10552 : : /* For serial constructs we set all dimensions to 1. */
10553 : 3028 : for (ix = GOMP_DIM_MAX; ix--;)
10554 : 2271 : dims = tree_cons (NULL_TREE, integer_one_node, dims);
10555 : 757 : oacc_replace_fn_attrib (child_fn, dims);
10556 : : }
10557 : : else
10558 : 8502 : oacc_set_fn_attrib (child_fn, clauses, &args);
10559 : : tagging = true;
10560 : : /* FALLTHRU */
10561 : 12005 : case BUILT_IN_GOACC_ENTER_DATA:
10562 : 12005 : case BUILT_IN_GOACC_EXIT_DATA:
10563 : 12005 : case BUILT_IN_GOACC_UPDATE:
10564 : 12005 : {
10565 : 12005 : tree t_async = NULL_TREE;
10566 : :
10567 : : /* If present, use the value specified by the respective
10568 : : clause, making sure that is of the correct type. */
10569 : 12005 : c = omp_find_clause (clauses, OMP_CLAUSE_ASYNC);
10570 : 12005 : if (c)
10571 : 1715 : t_async = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
10572 : : integer_type_node,
10573 : 1715 : OMP_CLAUSE_ASYNC_EXPR (c));
10574 : 10290 : else if (!tagging)
10575 : : /* Default values for t_async. */
10576 : 2436 : t_async = fold_convert_loc (gimple_location (entry_stmt),
10577 : : integer_type_node,
10578 : : build_int_cst (integer_type_node,
10579 : : GOMP_ASYNC_SYNC));
10580 : 12005 : if (tagging && t_async)
10581 : : {
10582 : 1405 : unsigned HOST_WIDE_INT i_async = GOMP_LAUNCH_OP_MAX;
10583 : :
10584 : 1405 : if (TREE_CODE (t_async) == INTEGER_CST)
10585 : : {
10586 : : /* See if we can pack the async arg in to the tag's
10587 : : operand. */
10588 : 1365 : i_async = TREE_INT_CST_LOW (t_async);
10589 : 1365 : if (i_async < GOMP_LAUNCH_OP_MAX)
10590 : : t_async = NULL_TREE;
10591 : : else
10592 : 859 : i_async = GOMP_LAUNCH_OP_MAX;
10593 : : }
10594 : 1405 : args.safe_push (oacc_launch_pack (GOMP_LAUNCH_ASYNC, NULL_TREE,
10595 : : i_async));
10596 : : }
10597 : 12005 : if (t_async)
10598 : 3605 : args.safe_push (force_gimple_operand_gsi (&gsi, t_async, true,
10599 : : NULL_TREE, true,
10600 : : GSI_SAME_STMT));
10601 : :
10602 : : /* Save the argument index, and ... */
10603 : 12005 : unsigned t_wait_idx = args.length ();
10604 : 12005 : unsigned num_waits = 0;
10605 : 12005 : c = omp_find_clause (clauses, OMP_CLAUSE_WAIT);
10606 : 12005 : if (!tagging || c)
10607 : : /* ... push a placeholder. */
10608 : 2949 : args.safe_push (integer_zero_node);
10609 : :
10610 : 13831 : for (; c; c = OMP_CLAUSE_CHAIN (c))
10611 : 1826 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_WAIT)
10612 : : {
10613 : 406 : tree arg = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
10614 : : integer_type_node,
10615 : 406 : OMP_CLAUSE_WAIT_EXPR (c));
10616 : 406 : arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE, true,
10617 : : GSI_SAME_STMT);
10618 : 406 : args.safe_push (arg);
10619 : 406 : num_waits++;
10620 : : }
10621 : :
10622 : 12005 : if (!tagging || num_waits)
10623 : : {
10624 : 203 : tree len;
10625 : :
10626 : : /* Now that we know the number, update the placeholder. */
10627 : 203 : if (tagging)
10628 : 203 : len = oacc_launch_pack (GOMP_LAUNCH_WAIT, NULL_TREE, num_waits);
10629 : : else
10630 : 2746 : len = build_int_cst (integer_type_node, num_waits);
10631 : 2949 : len = fold_convert_loc (gimple_location (entry_stmt),
10632 : : unsigned_type_node, len);
10633 : 2949 : args[t_wait_idx] = len;
10634 : : }
10635 : : }
10636 : 12005 : break;
10637 : 0 : default:
10638 : 0 : gcc_unreachable ();
10639 : : }
10640 : 23072 : if (tagging)
10641 : : /* Push terminal marker - zero. */
10642 : 9259 : args.safe_push (oacc_launch_pack (0, NULL_TREE, 0));
10643 : :
10644 : 35807 : if (child_fn2)
10645 : : {
10646 : : g = gimple_build_call_internal (IFN_GOMP_TARGET_REV, 1,
10647 : : build_fold_addr_expr (child_fn));
10648 : : gimple_set_location (g, gimple_location (entry_stmt));
10649 : : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
10650 : : }
10651 : :
10652 : 35807 : g = gimple_build_call_vec (builtin_decl_explicit (start_ix), args);
10653 : 35807 : gimple_set_location (g, gimple_location (entry_stmt));
10654 : 35807 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
10655 : 35807 : if (!offloaded)
10656 : : {
10657 : 15481 : g = gsi_stmt (gsi);
10658 : 15481 : gcc_assert (g && gimple_code (g) == GIMPLE_OMP_TARGET);
10659 : 15481 : gsi_remove (&gsi, true);
10660 : : }
10661 : 35807 : }
10662 : :
10663 : : /* Expand the parallel region tree rooted at REGION. Expansion
10664 : : proceeds in depth-first order. Innermost regions are expanded
10665 : : first. This way, parallel regions that require a new function to
10666 : : be created (e.g., GIMPLE_OMP_PARALLEL) can be expanded without having any
10667 : : internal dependencies in their body. */
10668 : :
10669 : : static void
10670 : 80992 : expand_omp (struct omp_region *region)
10671 : : {
10672 : 80992 : omp_any_child_fn_dumped = false;
10673 : 207864 : while (region)
10674 : : {
10675 : 126872 : location_t saved_location;
10676 : 126872 : gimple *inner_stmt = NULL;
10677 : :
10678 : : /* First, determine whether this is a combined parallel+workshare
10679 : : region. */
10680 : 126872 : if (region->type == GIMPLE_OMP_PARALLEL)
10681 : 16266 : determine_parallel_type (region);
10682 : :
10683 : 126872 : if (region->type == GIMPLE_OMP_FOR
10684 : 126872 : && gimple_omp_for_combined_p (last_nondebug_stmt (region->entry)))
10685 : 14615 : inner_stmt = last_nondebug_stmt (region->inner->entry);
10686 : :
10687 : 126872 : if (region->inner)
10688 : 56974 : expand_omp (region->inner);
10689 : :
10690 : 126872 : saved_location = input_location;
10691 : 126872 : if (gimple_has_location (last_nondebug_stmt (region->entry)))
10692 : 126684 : input_location = gimple_location (last_nondebug_stmt (region->entry));
10693 : :
10694 : 126872 : switch (region->type)
10695 : : {
10696 : 20113 : case GIMPLE_OMP_PARALLEL:
10697 : 20113 : case GIMPLE_OMP_TASK:
10698 : 20113 : expand_omp_taskreg (region);
10699 : 20113 : break;
10700 : :
10701 : 47480 : case GIMPLE_OMP_FOR:
10702 : 47480 : expand_omp_for (region, inner_stmt);
10703 : 47480 : break;
10704 : :
10705 : 378 : case GIMPLE_OMP_SECTIONS:
10706 : 378 : expand_omp_sections (region);
10707 : 378 : break;
10708 : :
10709 : : case GIMPLE_OMP_SECTION:
10710 : : /* Individual omp sections are handled together with their
10711 : : parent GIMPLE_OMP_SECTIONS region. */
10712 : : break;
10713 : :
10714 : 0 : case GIMPLE_OMP_STRUCTURED_BLOCK:
10715 : : /* We should have gotten rid of these in gimple lowering. */
10716 : 0 : gcc_unreachable ();
10717 : :
10718 : 1251 : case GIMPLE_OMP_SINGLE:
10719 : 1251 : case GIMPLE_OMP_SCOPE:
10720 : 1251 : expand_omp_single (region);
10721 : 1251 : break;
10722 : :
10723 : 1112 : case GIMPLE_OMP_ORDERED:
10724 : 1112 : {
10725 : 1112 : gomp_ordered *ord_stmt
10726 : 1112 : = as_a <gomp_ordered *> (last_nondebug_stmt (region->entry));
10727 : 1112 : if (gimple_omp_ordered_standalone_p (ord_stmt))
10728 : : {
10729 : : /* We'll expand these when expanding corresponding
10730 : : worksharing region with ordered(n) clause. */
10731 : 701 : gcc_assert (region->outer
10732 : : && region->outer->type == GIMPLE_OMP_FOR);
10733 : 701 : region->ord_stmt = ord_stmt;
10734 : 701 : break;
10735 : : }
10736 : : }
10737 : : /* FALLTHRU */
10738 : 10794 : case GIMPLE_OMP_MASTER:
10739 : 10794 : case GIMPLE_OMP_MASKED:
10740 : 10794 : case GIMPLE_OMP_TASKGROUP:
10741 : 10794 : case GIMPLE_OMP_CRITICAL:
10742 : 10794 : case GIMPLE_OMP_TEAMS:
10743 : 10794 : expand_omp_synch (region);
10744 : 10794 : break;
10745 : :
10746 : 9493 : case GIMPLE_OMP_ATOMIC_LOAD:
10747 : 9493 : expand_omp_atomic (region);
10748 : 9493 : break;
10749 : :
10750 : 35807 : case GIMPLE_OMP_TARGET:
10751 : 35807 : expand_omp_target (region);
10752 : 35807 : break;
10753 : :
10754 : 0 : default:
10755 : 0 : gcc_unreachable ();
10756 : : }
10757 : :
10758 : 126872 : input_location = saved_location;
10759 : 126872 : region = region->next;
10760 : : }
10761 : 80992 : if (omp_any_child_fn_dumped)
10762 : : {
10763 : 45 : if (dump_file)
10764 : 45 : dump_function_header (dump_file, current_function_decl, dump_flags);
10765 : 45 : omp_any_child_fn_dumped = false;
10766 : : }
10767 : 80992 : }
10768 : :
10769 : : /* Helper for build_omp_regions. Scan the dominator tree starting at
10770 : : block BB. PARENT is the region that contains BB. If SINGLE_TREE is
10771 : : true, the function ends once a single tree is built (otherwise, whole
10772 : : forest of OMP constructs may be built). */
10773 : :
10774 : : static void
10775 : 1368207 : build_omp_regions_1 (basic_block bb, struct omp_region *parent,
10776 : : bool single_tree)
10777 : : {
10778 : 1368207 : gimple_stmt_iterator gsi;
10779 : 1368207 : gimple *stmt;
10780 : 1368207 : basic_block son;
10781 : :
10782 : 1368207 : gsi = gsi_last_nondebug_bb (bb);
10783 : 1368207 : if (!gsi_end_p (gsi) && is_gimple_omp (gsi_stmt (gsi)))
10784 : : {
10785 : 286357 : struct omp_region *region;
10786 : 286357 : enum gimple_code code;
10787 : :
10788 : 286357 : stmt = gsi_stmt (gsi);
10789 : 286357 : code = gimple_code (stmt);
10790 : 286357 : if (code == GIMPLE_OMP_RETURN)
10791 : : {
10792 : : /* STMT is the return point out of region PARENT. Mark it
10793 : : as the exit point and make PARENT the immediately
10794 : : enclosing region. */
10795 : 100324 : gcc_assert (parent);
10796 : 100324 : region = parent;
10797 : 100324 : region->exit = bb;
10798 : 100324 : parent = parent->outer;
10799 : : }
10800 : 186033 : else if (code == GIMPLE_OMP_ATOMIC_STORE)
10801 : : {
10802 : : /* GIMPLE_OMP_ATOMIC_STORE is analogous to
10803 : : GIMPLE_OMP_RETURN, but matches with
10804 : : GIMPLE_OMP_ATOMIC_LOAD. */
10805 : 9493 : gcc_assert (parent);
10806 : 9493 : gcc_assert (parent->type == GIMPLE_OMP_ATOMIC_LOAD);
10807 : 9493 : region = parent;
10808 : 9493 : region->exit = bb;
10809 : 9493 : parent = parent->outer;
10810 : : }
10811 : 176540 : else if (code == GIMPLE_OMP_CONTINUE)
10812 : : {
10813 : 49290 : gcc_assert (parent);
10814 : 49290 : parent->cont = bb;
10815 : : }
10816 : 127250 : else if (code == GIMPLE_OMP_SECTIONS_SWITCH)
10817 : : {
10818 : : /* GIMPLE_OMP_SECTIONS_SWITCH is part of
10819 : : GIMPLE_OMP_SECTIONS, and we do nothing for it. */
10820 : : }
10821 : : else
10822 : : {
10823 : 126872 : region = new_omp_region (bb, code, parent);
10824 : : /* Otherwise... */
10825 : 126872 : if (code == GIMPLE_OMP_TARGET)
10826 : : {
10827 : 35807 : switch (gimple_omp_target_kind (stmt))
10828 : : {
10829 : : case GF_OMP_TARGET_KIND_REGION:
10830 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
10831 : : case GF_OMP_TARGET_KIND_OACC_KERNELS:
10832 : : case GF_OMP_TARGET_KIND_OACC_SERIAL:
10833 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
10834 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
10835 : : break;
10836 : : case GF_OMP_TARGET_KIND_UPDATE:
10837 : : case GF_OMP_TARGET_KIND_ENTER_DATA:
10838 : : case GF_OMP_TARGET_KIND_EXIT_DATA:
10839 : : case GF_OMP_TARGET_KIND_DATA:
10840 : : case GF_OMP_TARGET_KIND_OACC_DATA:
10841 : : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
10842 : : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
10843 : : case GF_OMP_TARGET_KIND_OACC_UPDATE:
10844 : : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
10845 : : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
10846 : : case GF_OMP_TARGET_KIND_OACC_DECLARE:
10847 : : /* ..., other than for those stand-alone directives...
10848 : : To be precise, target data isn't stand-alone, but
10849 : : gimplifier put the end API call into try finally block
10850 : : for it, so omp expansion can treat it as such. */
10851 : : region = NULL;
10852 : : break;
10853 : 0 : default:
10854 : 0 : gcc_unreachable ();
10855 : : }
10856 : : }
10857 : 91065 : else if (code == GIMPLE_OMP_ORDERED
10858 : 91065 : && gimple_omp_ordered_standalone_p (stmt))
10859 : : /* #pragma omp ordered depend is also just a stand-alone
10860 : : directive. */
10861 : : region = NULL;
10862 : 90364 : else if (code == GIMPLE_OMP_TASK
10863 : 90364 : && gimple_omp_task_taskwait_p (stmt))
10864 : : /* #pragma omp taskwait depend(...) is a stand-alone directive. */
10865 : : region = NULL;
10866 : 90283 : else if (code == GIMPLE_OMP_TASKGROUP)
10867 : : /* #pragma omp taskgroup isn't a stand-alone directive, but
10868 : : gimplifier put the end API call into try finall block
10869 : : for it, so omp expansion can treat it as such. */
10870 : : region = NULL;
10871 : : /* ..., this directive becomes the parent for a new region. */
10872 : : if (region)
10873 : : parent = region;
10874 : : }
10875 : : }
10876 : :
10877 : 1368207 : if (single_tree && !parent)
10878 : 0 : return;
10879 : :
10880 : 1368207 : for (son = first_dom_son (CDI_DOMINATORS, bb);
10881 : 2661109 : son;
10882 : 1292902 : son = next_dom_son (CDI_DOMINATORS, son))
10883 : 1292902 : build_omp_regions_1 (son, parent, single_tree);
10884 : : }
10885 : :
10886 : : /* Builds the tree of OMP regions rooted at ROOT, storing it to
10887 : : root_omp_region. */
10888 : :
10889 : : static void
10890 : 0 : build_omp_regions_root (basic_block root)
10891 : : {
10892 : 0 : gcc_assert (root_omp_region == NULL);
10893 : 0 : build_omp_regions_1 (root, NULL, true);
10894 : 0 : gcc_assert (root_omp_region != NULL);
10895 : 0 : }
10896 : :
10897 : : /* Expands omp construct (and its subconstructs) starting in HEAD. */
10898 : :
10899 : : void
10900 : 0 : omp_expand_local (basic_block head)
10901 : : {
10902 : 0 : build_omp_regions_root (head);
10903 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
10904 : : {
10905 : 0 : fprintf (dump_file, "\nOMP region tree\n\n");
10906 : 0 : dump_omp_region (dump_file, root_omp_region, 0);
10907 : 0 : fprintf (dump_file, "\n");
10908 : : }
10909 : :
10910 : 0 : remove_exit_barriers (root_omp_region);
10911 : 0 : expand_omp (root_omp_region);
10912 : :
10913 : 0 : omp_free_regions ();
10914 : 0 : }
10915 : :
10916 : : /* Scan the CFG and build a tree of OMP regions. Return the root of
10917 : : the OMP region tree. */
10918 : :
10919 : : static void
10920 : 75305 : build_omp_regions (void)
10921 : : {
10922 : 75305 : gcc_assert (root_omp_region == NULL);
10923 : 75305 : calculate_dominance_info (CDI_DOMINATORS);
10924 : 75305 : build_omp_regions_1 (ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, false);
10925 : 75305 : }
10926 : :
10927 : : /* Main entry point for expanding OMP-GIMPLE into runtime calls. */
10928 : :
10929 : : static unsigned int
10930 : 75305 : execute_expand_omp (void)
10931 : : {
10932 : 75305 : build_omp_regions ();
10933 : :
10934 : 75305 : if (!root_omp_region)
10935 : : return 0;
10936 : :
10937 : 24018 : if (dump_file)
10938 : : {
10939 : 164 : fprintf (dump_file, "\nOMP region tree\n\n");
10940 : 164 : dump_omp_region (dump_file, root_omp_region, 0);
10941 : 164 : fprintf (dump_file, "\n");
10942 : : }
10943 : :
10944 : 24018 : remove_exit_barriers (root_omp_region);
10945 : :
10946 : 24018 : expand_omp (root_omp_region);
10947 : :
10948 : 24018 : omp_free_regions ();
10949 : :
10950 : 24018 : return (TODO_cleanup_cfg
10951 : 47488 : | (gimple_in_ssa_p (cfun) ? TODO_update_ssa_only_virtuals : 0));
10952 : : }
10953 : :
10954 : : /* OMP expansion -- the default pass, run before creation of SSA form. */
10955 : :
10956 : : namespace {
10957 : :
10958 : : const pass_data pass_data_expand_omp =
10959 : : {
10960 : : GIMPLE_PASS, /* type */
10961 : : "ompexp", /* name */
10962 : : OPTGROUP_OMP, /* optinfo_flags */
10963 : : TV_NONE, /* tv_id */
10964 : : PROP_gimple_any, /* properties_required */
10965 : : PROP_gimple_eomp, /* properties_provided */
10966 : : 0, /* properties_destroyed */
10967 : : 0, /* todo_flags_start */
10968 : : 0, /* todo_flags_finish */
10969 : : };
10970 : :
10971 : : class pass_expand_omp : public gimple_opt_pass
10972 : : {
10973 : : public:
10974 : 285689 : pass_expand_omp (gcc::context *ctxt)
10975 : 571378 : : gimple_opt_pass (pass_data_expand_omp, ctxt)
10976 : : {}
10977 : :
10978 : : /* opt_pass methods: */
10979 : 2979782 : unsigned int execute (function *) final override
10980 : : {
10981 : 2972589 : bool gate = ((flag_openacc != 0 || flag_openmp != 0
10982 : 2926411 : || flag_openmp_simd != 0)
10983 : 3028195 : && !seen_error ());
10984 : :
10985 : : /* This pass always runs, to provide PROP_gimple_eomp.
10986 : : But often, there is nothing to do. */
10987 : 2979782 : if (!gate)
10988 : 2927921 : return 0;
10989 : :
10990 : 51861 : return execute_expand_omp ();
10991 : : }
10992 : :
10993 : : }; // class pass_expand_omp
10994 : :
10995 : : } // anon namespace
10996 : :
10997 : : gimple_opt_pass *
10998 : 285689 : make_pass_expand_omp (gcc::context *ctxt)
10999 : : {
11000 : 285689 : return new pass_expand_omp (ctxt);
11001 : : }
11002 : :
11003 : : namespace {
11004 : :
11005 : : const pass_data pass_data_expand_omp_ssa =
11006 : : {
11007 : : GIMPLE_PASS, /* type */
11008 : : "ompexpssa", /* name */
11009 : : OPTGROUP_OMP, /* optinfo_flags */
11010 : : TV_NONE, /* tv_id */
11011 : : PROP_cfg | PROP_ssa, /* properties_required */
11012 : : PROP_gimple_eomp, /* properties_provided */
11013 : : 0, /* properties_destroyed */
11014 : : 0, /* todo_flags_start */
11015 : : TODO_cleanup_cfg | TODO_rebuild_alias, /* todo_flags_finish */
11016 : : };
11017 : :
11018 : : class pass_expand_omp_ssa : public gimple_opt_pass
11019 : : {
11020 : : public:
11021 : 571378 : pass_expand_omp_ssa (gcc::context *ctxt)
11022 : 1142756 : : gimple_opt_pass (pass_data_expand_omp_ssa, ctxt)
11023 : : {}
11024 : :
11025 : : /* opt_pass methods: */
11026 : 242768 : bool gate (function *fun) final override
11027 : : {
11028 : 242768 : return !(fun->curr_properties & PROP_gimple_eomp);
11029 : : }
11030 : 23444 : unsigned int execute (function *) final override
11031 : : {
11032 : 23444 : return execute_expand_omp ();
11033 : : }
11034 : 285689 : opt_pass * clone () final override
11035 : : {
11036 : 285689 : return new pass_expand_omp_ssa (m_ctxt);
11037 : : }
11038 : :
11039 : : }; // class pass_expand_omp_ssa
11040 : :
11041 : : } // anon namespace
11042 : :
11043 : : gimple_opt_pass *
11044 : 285689 : make_pass_expand_omp_ssa (gcc::context *ctxt)
11045 : : {
11046 : 285689 : return new pass_expand_omp_ssa (ctxt);
11047 : : }
11048 : :
11049 : : /* Called from tree-cfg.cc::make_edges to create cfg edges for all relevant
11050 : : GIMPLE_* codes. */
11051 : :
11052 : : bool
11053 : 288795 : omp_make_gimple_edges (basic_block bb, struct omp_region **region,
11054 : : int *region_idx)
11055 : : {
11056 : 288795 : gimple *last = last_nondebug_stmt (bb);
11057 : 288795 : enum gimple_code code = gimple_code (last);
11058 : 288795 : struct omp_region *cur_region = *region;
11059 : 288795 : bool fallthru = false;
11060 : :
11061 : 288795 : switch (code)
11062 : : {
11063 : 75159 : case GIMPLE_OMP_PARALLEL:
11064 : 75159 : case GIMPLE_OMP_FOR:
11065 : 75159 : case GIMPLE_OMP_SINGLE:
11066 : 75159 : case GIMPLE_OMP_TEAMS:
11067 : 75159 : case GIMPLE_OMP_MASTER:
11068 : 75159 : case GIMPLE_OMP_MASKED:
11069 : 75159 : case GIMPLE_OMP_SCOPE:
11070 : 75159 : case GIMPLE_OMP_CRITICAL:
11071 : 75159 : case GIMPLE_OMP_SECTION:
11072 : 75159 : cur_region = new_omp_region (bb, code, cur_region);
11073 : 75159 : fallthru = true;
11074 : 75159 : break;
11075 : :
11076 : 536 : case GIMPLE_OMP_TASKGROUP:
11077 : 536 : cur_region = new_omp_region (bb, code, cur_region);
11078 : 536 : fallthru = true;
11079 : 536 : cur_region = cur_region->outer;
11080 : 536 : break;
11081 : :
11082 : 3847 : case GIMPLE_OMP_TASK:
11083 : 3847 : cur_region = new_omp_region (bb, code, cur_region);
11084 : 3847 : fallthru = true;
11085 : 3847 : if (gimple_omp_task_taskwait_p (last))
11086 : 81 : cur_region = cur_region->outer;
11087 : : break;
11088 : :
11089 : 1116 : case GIMPLE_OMP_ORDERED:
11090 : 1116 : cur_region = new_omp_region (bb, code, cur_region);
11091 : 1116 : fallthru = true;
11092 : 1116 : if (gimple_omp_ordered_standalone_p (last))
11093 : 705 : cur_region = cur_region->outer;
11094 : : break;
11095 : :
11096 : 36181 : case GIMPLE_OMP_TARGET:
11097 : 36181 : cur_region = new_omp_region (bb, code, cur_region);
11098 : 36181 : fallthru = true;
11099 : 36181 : switch (gimple_omp_target_kind (last))
11100 : : {
11101 : : case GF_OMP_TARGET_KIND_REGION:
11102 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
11103 : : case GF_OMP_TARGET_KIND_OACC_KERNELS:
11104 : : case GF_OMP_TARGET_KIND_OACC_SERIAL:
11105 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
11106 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
11107 : : break;
11108 : 15655 : case GF_OMP_TARGET_KIND_UPDATE:
11109 : 15655 : case GF_OMP_TARGET_KIND_ENTER_DATA:
11110 : 15655 : case GF_OMP_TARGET_KIND_EXIT_DATA:
11111 : 15655 : case GF_OMP_TARGET_KIND_DATA:
11112 : 15655 : case GF_OMP_TARGET_KIND_OACC_DATA:
11113 : 15655 : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
11114 : 15655 : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
11115 : 15655 : case GF_OMP_TARGET_KIND_OACC_UPDATE:
11116 : 15655 : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
11117 : 15655 : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
11118 : 15655 : case GF_OMP_TARGET_KIND_OACC_DECLARE:
11119 : 15655 : cur_region = cur_region->outer;
11120 : 15655 : break;
11121 : 0 : default:
11122 : 0 : gcc_unreachable ();
11123 : : }
11124 : : break;
11125 : :
11126 : 378 : case GIMPLE_OMP_SECTIONS:
11127 : 378 : cur_region = new_omp_region (bb, code, cur_region);
11128 : 378 : fallthru = true;
11129 : 378 : break;
11130 : :
11131 : : case GIMPLE_OMP_SECTIONS_SWITCH:
11132 : : fallthru = false;
11133 : : break;
11134 : :
11135 : 19714 : case GIMPLE_OMP_ATOMIC_LOAD:
11136 : 19714 : case GIMPLE_OMP_ATOMIC_STORE:
11137 : 19714 : fallthru = true;
11138 : 19714 : break;
11139 : :
11140 : 100240 : case GIMPLE_OMP_RETURN:
11141 : : /* In the case of a GIMPLE_OMP_SECTION, the edge will go
11142 : : somewhere other than the next block. This will be
11143 : : created later. */
11144 : 100240 : cur_region->exit = bb;
11145 : 100240 : if (cur_region->type == GIMPLE_OMP_TASK)
11146 : : /* Add an edge corresponding to not scheduling the task
11147 : : immediately. */
11148 : 3766 : make_edge (cur_region->entry, bb, EDGE_ABNORMAL);
11149 : 100240 : fallthru = cur_region->type != GIMPLE_OMP_SECTION;
11150 : 100240 : cur_region = cur_region->outer;
11151 : 100240 : break;
11152 : :
11153 : 51246 : case GIMPLE_OMP_CONTINUE:
11154 : 51246 : cur_region->cont = bb;
11155 : 51246 : switch (cur_region->type)
11156 : : {
11157 : 47102 : case GIMPLE_OMP_FOR:
11158 : : /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
11159 : : succs edges as abnormal to prevent splitting
11160 : : them. */
11161 : 47102 : single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
11162 : : /* Make the loopback edge. */
11163 : 47102 : make_edge (bb, single_succ (cur_region->entry),
11164 : : EDGE_ABNORMAL);
11165 : :
11166 : : /* Create an edge from GIMPLE_OMP_FOR to exit, which
11167 : : corresponds to the case that the body of the loop
11168 : : is not executed at all. */
11169 : 47102 : make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
11170 : 47102 : make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
11171 : 47102 : fallthru = false;
11172 : 47102 : break;
11173 : :
11174 : 378 : case GIMPLE_OMP_SECTIONS:
11175 : : /* Wire up the edges into and out of the nested sections. */
11176 : 378 : {
11177 : 378 : basic_block switch_bb = single_succ (cur_region->entry);
11178 : :
11179 : 378 : struct omp_region *i;
11180 : 1233 : for (i = cur_region->inner; i ; i = i->next)
11181 : : {
11182 : 855 : gcc_assert (i->type == GIMPLE_OMP_SECTION);
11183 : 855 : make_edge (switch_bb, i->entry, 0);
11184 : 855 : make_edge (i->exit, bb, EDGE_FALLTHRU);
11185 : : }
11186 : :
11187 : : /* Make the loopback edge to the block with
11188 : : GIMPLE_OMP_SECTIONS_SWITCH. */
11189 : 378 : make_edge (bb, switch_bb, 0);
11190 : :
11191 : : /* Make the edge from the switch to exit. */
11192 : 378 : make_edge (switch_bb, bb->next_bb, 0);
11193 : 378 : fallthru = false;
11194 : : }
11195 : 378 : break;
11196 : :
11197 : : case GIMPLE_OMP_TASK:
11198 : : fallthru = true;
11199 : : break;
11200 : :
11201 : 0 : default:
11202 : 0 : gcc_unreachable ();
11203 : : }
11204 : : break;
11205 : :
11206 : 0 : default:
11207 : 0 : gcc_unreachable ();
11208 : : }
11209 : :
11210 : 288795 : if (*region != cur_region)
11211 : : {
11212 : 200480 : *region = cur_region;
11213 : 200480 : if (cur_region)
11214 : 160011 : *region_idx = cur_region->entry->index;
11215 : : else
11216 : 40469 : *region_idx = 0;
11217 : : }
11218 : :
11219 : 288795 : return fallthru;
11220 : : }
|