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 : 43264 : is_combined_parallel (struct omp_region *region)
125 : : {
126 : 43264 : 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 : 9075 : workshare_safe_to_combine_p (basic_block ws_entry_bb)
190 : : {
191 : 9075 : struct omp_for_data fd;
192 : 9075 : gimple *ws_stmt = last_nondebug_stmt (ws_entry_bb);
193 : :
194 : 9075 : if (gimple_code (ws_stmt) == GIMPLE_OMP_SECTIONS)
195 : : return true;
196 : :
197 : 8956 : gcc_assert (gimple_code (ws_stmt) == GIMPLE_OMP_FOR);
198 : 8956 : if (gimple_omp_for_kind (ws_stmt) != GF_OMP_FOR_KIND_FOR)
199 : : return false;
200 : :
201 : 8942 : omp_extract_for_data (as_a <gomp_for *> (ws_stmt), &fd, NULL);
202 : :
203 : 8942 : if (fd.collapse > 1 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
204 : : return false;
205 : 7170 : 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 : 16258 : determine_parallel_type (struct omp_region *region)
328 : : {
329 : 16258 : basic_block par_entry_bb, par_exit_bb;
330 : 16258 : basic_block ws_entry_bb, ws_exit_bb;
331 : :
332 : 16258 : if (region == NULL || region->inner == NULL
333 : 14787 : || region->exit == NULL || region->inner->exit == NULL
334 : 14776 : || region->inner->cont == NULL)
335 : : return;
336 : :
337 : : /* We only support parallel+for and parallel+sections. */
338 : 10343 : if (region->type != GIMPLE_OMP_PARALLEL
339 : 10343 : || (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 : 10225 : par_entry_bb = region->entry;
346 : 10225 : par_exit_bb = region->exit;
347 : 10225 : ws_entry_bb = region->inner->entry;
348 : 10225 : 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 : 10225 : tree pclauses
354 : 10225 : = gimple_omp_parallel_clauses (last_nondebug_stmt (par_entry_bb));
355 : 10225 : if (omp_find_clause (pclauses, OMP_CLAUSE__REDUCTEMP_))
356 : : return;
357 : :
358 : 10202 : if (single_succ (par_entry_bb) == ws_entry_bb
359 : 9111 : && single_succ (ws_exit_bb) == par_exit_bb
360 : 9075 : && workshare_safe_to_combine_p (ws_entry_bb)
361 : 11453 : && (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 : 242270 : new_omp_region (basic_block bb, enum gimple_code type,
452 : : struct omp_region *parent)
453 : : {
454 : 242270 : struct omp_region *region = XCNEW (struct omp_region);
455 : :
456 : 242270 : region->outer = parent;
457 : 242270 : region->entry = bb;
458 : 242270 : region->type = type;
459 : :
460 : 242270 : if (parent)
461 : : {
462 : : /* This is a nested region. Add it to the list of inner
463 : : regions in PARENT. */
464 : 127037 : region->next = parent->inner;
465 : 127037 : 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 : 115233 : region->next = root_omp_region;
472 : 115233 : root_omp_region = region;
473 : : }
474 : :
475 : 242270 : return region;
476 : : }
477 : :
478 : : /* Release the memory associated with the region tree rooted at REGION. */
479 : :
480 : : static void
481 : 242270 : free_omp_region_1 (struct omp_region *region)
482 : : {
483 : 242270 : struct omp_region *i, *n;
484 : :
485 : 369307 : for (i = region->inner; i ; i = n)
486 : : {
487 : 127037 : n = i->next;
488 : 127037 : free_omp_region_1 (i);
489 : : }
490 : :
491 : 242270 : free (region);
492 : 242270 : }
493 : :
494 : : /* Release the memory for the entire omp region tree. */
495 : :
496 : : void
497 : 2788842 : omp_free_regions (void)
498 : : {
499 : 2788842 : struct omp_region *r, *n;
500 : 2904075 : for (r = root_omp_region; r ; r = n)
501 : : {
502 : 115233 : n = r->next;
503 : 115233 : free_omp_region_1 (r);
504 : : }
505 : 2788842 : root_omp_region = NULL;
506 : 2788842 : }
507 : :
508 : : /* A convenience function to build an empty GIMPLE_COND with just the
509 : : condition. */
510 : :
511 : : static gcond *
512 : 112076 : gimple_build_cond_empty (tree cond)
513 : : {
514 : 112076 : enum tree_code pred_code;
515 : 112076 : tree lhs, rhs;
516 : :
517 : 112076 : gimple_cond_get_ops_from_tree (cond, &pred_code, &lhs, &rhs);
518 : 112076 : 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 : 42256 : adjust_context_and_scope (struct omp_region *region, tree entry_block,
531 : : tree child_fndecl)
532 : : {
533 : 42256 : tree parent_fndecl = NULL_TREE;
534 : 42256 : 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 : 42256 : for (region = region->outer;
541 : 63304 : region && parent_fndecl == NULL_TREE; region = region->outer)
542 : 21048 : switch (region->type)
543 : : {
544 : 6818 : case GIMPLE_OMP_PARALLEL:
545 : 6818 : case GIMPLE_OMP_TASK:
546 : 6818 : case GIMPLE_OMP_TEAMS:
547 : 6818 : entry_stmt = last_nondebug_stmt (region->entry);
548 : 6818 : parent_fndecl = gimple_omp_taskreg_child_fn (entry_stmt);
549 : 6818 : break;
550 : 4522 : case GIMPLE_OMP_TARGET:
551 : 4522 : entry_stmt = last_nondebug_stmt (region->entry);
552 : 4522 : parent_fndecl
553 : 4522 : = gimple_omp_target_child_fn (as_a <gomp_target *> (entry_stmt));
554 : 4522 : break;
555 : : default:
556 : : break;
557 : : }
558 : :
559 : 42256 : if (parent_fndecl == NULL_TREE)
560 : 33995 : parent_fndecl = current_function_decl;
561 : 42256 : DECL_CONTEXT (child_fndecl) = parent_fndecl;
562 : :
563 : 42256 : if (entry_block != NULL_TREE && TREE_CODE (entry_block) == BLOCK)
564 : : {
565 : 42222 : tree b = BLOCK_SUPERCONTEXT (entry_block);
566 : 42222 : if (TREE_CODE (b) == BLOCK)
567 : : {
568 : 42091 : DECL_CHAIN (child_fndecl) = BLOCK_VARS (b);
569 : 42091 : BLOCK_VARS (b) = child_fndecl;
570 : : }
571 : : }
572 : 42256 : }
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 : 16258 : expand_parallel_call (struct omp_region *region, basic_block bb,
583 : : gomp_parallel *entry_stmt,
584 : : vec<tree, va_gc> *ws_args)
585 : : {
586 : 16258 : tree t, t1, t2, val, cond, c, clauses, flags;
587 : 16258 : gimple_stmt_iterator gsi;
588 : 16258 : gimple *stmt;
589 : 16258 : enum built_in_function start_ix;
590 : 16258 : int start_ix2;
591 : 16258 : location_t clause_loc;
592 : 16258 : vec<tree, va_gc> *args;
593 : :
594 : 16258 : clauses = gimple_omp_parallel_clauses (entry_stmt);
595 : :
596 : : /* Determine what flavor of GOMP_parallel we will be
597 : : emitting. */
598 : 16258 : start_ix = BUILT_IN_GOMP_PARALLEL;
599 : 16258 : tree rtmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
600 : 16258 : if (rtmp)
601 : : start_ix = BUILT_IN_GOMP_PARALLEL_REDUCTIONS;
602 : 16199 : 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 : 16258 : cond = NULL_TREE;
652 : 16258 : val = build_int_cst (unsigned_type_node, 0);
653 : 16258 : flags = build_int_cst (unsigned_type_node, 0);
654 : :
655 : 16258 : c = omp_find_clause (clauses, OMP_CLAUSE_IF);
656 : 16258 : if (c)
657 : 945 : cond = OMP_CLAUSE_IF_EXPR (c);
658 : :
659 : 16258 : c = omp_find_clause (clauses, OMP_CLAUSE_NUM_THREADS);
660 : 16258 : if (c)
661 : : {
662 : 2278 : val = OMP_CLAUSE_NUM_THREADS_EXPR (c);
663 : 2278 : clause_loc = OMP_CLAUSE_LOCATION (c);
664 : : }
665 : : else
666 : 13980 : clause_loc = gimple_location (entry_stmt);
667 : :
668 : 16258 : c = omp_find_clause (clauses, OMP_CLAUSE_PROC_BIND);
669 : 16258 : 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 : 16258 : 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 : 16258 : 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 : 787 : 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 : 16258 : gsi = gsi_last_nondebug_bb (bb);
750 : 16258 : t = gimple_omp_parallel_data_arg (entry_stmt);
751 : 16258 : if (t == NULL)
752 : 2369 : t1 = null_pointer_node;
753 : : else
754 : 13889 : t1 = build_fold_addr_expr (t);
755 : 16258 : tree child_fndecl = gimple_omp_parallel_child_fn (entry_stmt);
756 : 16258 : t2 = build_fold_addr_expr (child_fndecl);
757 : :
758 : 17454 : vec_alloc (args, 4 + vec_safe_length (ws_args));
759 : 16258 : args->quick_push (t2);
760 : 16258 : args->quick_push (t1);
761 : 16258 : args->quick_push (val);
762 : 16258 : if (ws_args)
763 : 1196 : args->splice (*ws_args);
764 : 16258 : args->quick_push (flags);
765 : :
766 : 16258 : t = build_call_expr_loc_vec (UNKNOWN_LOCATION,
767 : : builtin_decl_explicit (start_ix), args);
768 : :
769 : 16258 : 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 : 16258 : force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
777 : : false, GSI_CONTINUE_LINKING);
778 : 16258 : }
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 : 3783 : expand_task_call (struct omp_region *region, basic_block bb,
785 : : gomp_task *entry_stmt)
786 : : {
787 : 3783 : tree t1, t2, t3;
788 : 3783 : gimple_stmt_iterator gsi;
789 : 3783 : location_t loc = gimple_location (entry_stmt);
790 : :
791 : 3783 : tree clauses = gimple_omp_task_clauses (entry_stmt);
792 : :
793 : 3783 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
794 : 3783 : tree untied = omp_find_clause (clauses, OMP_CLAUSE_UNTIED);
795 : 3783 : tree mergeable = omp_find_clause (clauses, OMP_CLAUSE_MERGEABLE);
796 : 3783 : tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
797 : 3783 : tree finalc = omp_find_clause (clauses, OMP_CLAUSE_FINAL);
798 : 3783 : tree priority = omp_find_clause (clauses, OMP_CLAUSE_PRIORITY);
799 : 3783 : tree detach = omp_find_clause (clauses, OMP_CLAUSE_DETACH);
800 : :
801 : 7566 : unsigned int iflags
802 : 3783 : = (untied ? GOMP_TASK_FLAG_UNTIED : 0)
803 : 3783 : | (mergeable ? GOMP_TASK_FLAG_MERGEABLE : 0)
804 : 3783 : | (depend ? GOMP_TASK_FLAG_DEPEND : 0);
805 : :
806 : 3783 : bool taskloop_p = gimple_omp_task_taskloop_p (entry_stmt);
807 : 3783 : tree startvar = NULL_TREE, endvar = NULL_TREE, step = NULL_TREE;
808 : 3783 : tree num_tasks = NULL_TREE;
809 : 3783 : bool ull = false;
810 : 3783 : if (taskloop_p)
811 : : {
812 : 1343 : gimple *g = last_nondebug_stmt (region->outer->entry);
813 : 1343 : gcc_assert (gimple_code (g) == GIMPLE_OMP_FOR
814 : : && gimple_omp_for_kind (g) == GF_OMP_FOR_KIND_TASKLOOP);
815 : 1343 : struct omp_for_data fd;
816 : 1343 : omp_extract_for_data (as_a <gomp_for *> (g), &fd, NULL);
817 : 1343 : startvar = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
818 : 1343 : endvar = omp_find_clause (OMP_CLAUSE_CHAIN (startvar),
819 : : OMP_CLAUSE__LOOPTEMP_);
820 : 1343 : startvar = OMP_CLAUSE_DECL (startvar);
821 : 1343 : endvar = OMP_CLAUSE_DECL (endvar);
822 : 1343 : step = fold_convert_loc (loc, fd.iter_type, fd.loop.step);
823 : 1343 : if (fd.loop.cond_code == LT_EXPR)
824 : 1291 : iflags |= GOMP_TASK_FLAG_UP;
825 : 1343 : tree tclauses = gimple_omp_for_clauses (g);
826 : 1343 : num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_NUM_TASKS);
827 : 1343 : if (num_tasks)
828 : : {
829 : 182 : if (OMP_CLAUSE_NUM_TASKS_STRICT (num_tasks))
830 : 8 : iflags |= GOMP_TASK_FLAG_STRICT;
831 : 182 : num_tasks = OMP_CLAUSE_NUM_TASKS_EXPR (num_tasks);
832 : : }
833 : : else
834 : : {
835 : 1161 : num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_GRAINSIZE);
836 : 1161 : if (num_tasks)
837 : : {
838 : 230 : iflags |= GOMP_TASK_FLAG_GRAINSIZE;
839 : 230 : if (OMP_CLAUSE_GRAINSIZE_STRICT (num_tasks))
840 : 8 : iflags |= GOMP_TASK_FLAG_STRICT;
841 : 230 : num_tasks = OMP_CLAUSE_GRAINSIZE_EXPR (num_tasks);
842 : : }
843 : : else
844 : 931 : num_tasks = integer_zero_node;
845 : : }
846 : 1343 : num_tasks = fold_convert_loc (loc, long_integer_type_node, num_tasks);
847 : 1343 : if (ifc == NULL_TREE)
848 : 976 : iflags |= GOMP_TASK_FLAG_IF;
849 : 1343 : if (omp_find_clause (tclauses, OMP_CLAUSE_NOGROUP))
850 : 57 : iflags |= GOMP_TASK_FLAG_NOGROUP;
851 : 1343 : ull = fd.iter_type == long_long_unsigned_type_node;
852 : 1343 : 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 : 3783 : tree flags = build_int_cst (unsigned_type_node, iflags);
864 : :
865 : 3783 : tree cond = boolean_true_node;
866 : 3783 : 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 : 367 : build_int_cst (unsigned_type_node,
873 : 367 : GOMP_TASK_FLAG_IF),
874 : 367 : 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 : 3783 : 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 : 406 : build_int_cst (unsigned_type_node,
887 : 406 : GOMP_TASK_FLAG_FINAL),
888 : 406 : build_int_cst (unsigned_type_node, 0));
889 : 406 : flags = fold_build2_loc (loc, PLUS_EXPR, unsigned_type_node, flags, t);
890 : : }
891 : 3783 : if (depend)
892 : 1101 : depend = OMP_CLAUSE_DECL (depend);
893 : : else
894 : 2682 : depend = build_int_cst (ptr_type_node, 0);
895 : 3783 : if (priority)
896 : 368 : priority = fold_convert (integer_type_node,
897 : : OMP_CLAUSE_PRIORITY_EXPR (priority));
898 : : else
899 : 3415 : priority = integer_zero_node;
900 : :
901 : 3783 : gsi = gsi_last_nondebug_bb (bb);
902 : :
903 : 7566 : detach = (detach
904 : 3949 : ? build_fold_addr_expr (OMP_CLAUSE_DECL (detach))
905 : : : null_pointer_node);
906 : :
907 : 3783 : tree t = gimple_omp_task_data_arg (entry_stmt);
908 : 3783 : if (t == NULL)
909 : 678 : t2 = null_pointer_node;
910 : : else
911 : 3105 : t2 = build_fold_addr_expr_loc (loc, t);
912 : 3783 : t1 = build_fold_addr_expr_loc (loc, gimple_omp_task_child_fn (entry_stmt));
913 : 3783 : t = gimple_omp_task_copy_fn (entry_stmt);
914 : 3783 : if (t == NULL)
915 : 3265 : t3 = null_pointer_node;
916 : : else
917 : 518 : t3 = build_fold_addr_expr_loc (loc, t);
918 : :
919 : 3783 : if (taskloop_p)
920 : 2686 : t = build_call_expr (ull
921 : 42 : ? builtin_decl_explicit (BUILT_IN_GOMP_TASKLOOP_ULL)
922 : 1301 : : 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 : 3783 : force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
935 : : false, GSI_CONTINUE_LINKING);
936 : 3783 : }
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 : 78 : expand_taskwait_call (basic_block bb, gomp_task *entry_stmt)
944 : : {
945 : 78 : tree clauses = gimple_omp_task_clauses (entry_stmt);
946 : 78 : tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
947 : 78 : if (depend == NULL_TREE)
948 : 0 : return;
949 : :
950 : 78 : depend = OMP_CLAUSE_DECL (depend);
951 : :
952 : 78 : bool nowait = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT) != NULL_TREE;
953 : 78 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
954 : 156 : enum built_in_function f = (nowait
955 : 78 : ? BUILT_IN_GOMP_TASKWAIT_DEPEND_NOWAIT
956 : : : BUILT_IN_GOMP_TASKWAIT_DEPEND);
957 : 78 : tree t = build_call_expr (builtin_decl_explicit (f), 1, depend);
958 : :
959 : 78 : 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 : 42256 : vec2chain (vec<tree, va_gc> *v)
1018 : : {
1019 : 42256 : tree chain = NULL_TREE, t;
1020 : 42256 : unsigned ix;
1021 : :
1022 : 428870 : FOR_EACH_VEC_SAFE_ELT_REVERSE (v, ix, t)
1023 : : {
1024 : 349788 : DECL_CHAIN (t) = chain;
1025 : 349788 : chain = t;
1026 : : }
1027 : :
1028 : 42256 : 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 : 11775 : remove_exit_barrier (struct omp_region *region)
1039 : : {
1040 : 11775 : gimple_stmt_iterator gsi;
1041 : 11775 : basic_block exit_bb;
1042 : 11775 : edge_iterator ei;
1043 : 11775 : edge e;
1044 : 11775 : gimple *stmt;
1045 : 11775 : int any_addressable_vars = -1;
1046 : :
1047 : 11775 : exit_bb = region->exit;
1048 : :
1049 : : /* If the parallel region doesn't return, we don't have REGION->EXIT
1050 : : block at all. */
1051 : 11775 : if (! exit_bb)
1052 : 1186 : 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 : 11748 : gsi = gsi_last_nondebug_bb (exit_bb);
1060 : 11748 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
1061 : 11748 : gsi_prev_nondebug (&gsi);
1062 : 11748 : if (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1063 : : return;
1064 : :
1065 : 21541 : FOR_EACH_EDGE (e, ei, exit_bb->preds)
1066 : : {
1067 : 10952 : gsi = gsi_last_nondebug_bb (e->src);
1068 : 10952 : if (gsi_end_p (gsi))
1069 : 9 : continue;
1070 : 10943 : stmt = gsi_stmt (gsi);
1071 : 10943 : if (gimple_code (stmt) == GIMPLE_OMP_RETURN
1072 : 10943 : && !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 : 1054 : if (any_addressable_vars < 0)
1083 : : {
1084 : 1046 : gomp_parallel *parallel_stmt
1085 : 1046 : = as_a <gomp_parallel *> (last_nondebug_stmt (region->entry));
1086 : 1046 : tree child_fun = gimple_omp_parallel_child_fn (parallel_stmt);
1087 : 1046 : tree local_decls, block, decl;
1088 : 1046 : unsigned ix;
1089 : :
1090 : 1046 : any_addressable_vars = 0;
1091 : 8646 : FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (child_fun), ix, decl)
1092 : 7165 : if (TREE_ADDRESSABLE (decl))
1093 : : {
1094 : : any_addressable_vars = 1;
1095 : : break;
1096 : : }
1097 : 1389 : for (block = gimple_block (stmt);
1098 : 1389 : !any_addressable_vars
1099 : 1389 : && block
1100 : 1389 : && TREE_CODE (block) == BLOCK;
1101 : 343 : block = BLOCK_SUPERCONTEXT (block))
1102 : : {
1103 : 523 : for (local_decls = BLOCK_VARS (block);
1104 : 1834 : local_decls;
1105 : 1311 : local_decls = DECL_CHAIN (local_decls))
1106 : 1335 : if (TREE_ADDRESSABLE (local_decls))
1107 : : {
1108 : : any_addressable_vars = 1;
1109 : : break;
1110 : : }
1111 : 523 : if (block == gimple_block (parallel_stmt))
1112 : : break;
1113 : : }
1114 : : }
1115 : 1054 : if (!any_addressable_vars)
1116 : 501 : gimple_omp_return_set_nowait (stmt);
1117 : : }
1118 : : }
1119 : : }
1120 : :
1121 : : static void
1122 : 67721 : remove_exit_barriers (struct omp_region *region)
1123 : : {
1124 : 67721 : if (region->type == GIMPLE_OMP_PARALLEL)
1125 : 11775 : remove_exit_barrier (region);
1126 : :
1127 : 67721 : if (region->inner)
1128 : : {
1129 : 37810 : region = region->inner;
1130 : 37810 : remove_exit_barriers (region);
1131 : 81864 : while (region->next)
1132 : : {
1133 : 6244 : region = region->next;
1134 : 6244 : remove_exit_barriers (region);
1135 : : }
1136 : : }
1137 : 67721 : }
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 : 16874 : optimize_omp_library_calls (gimple *entry_stmt)
1149 : : {
1150 : 16874 : basic_block bb;
1151 : 16874 : gimple_stmt_iterator gsi;
1152 : 16874 : tree thr_num_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
1153 : 16874 : tree thr_num_id = DECL_ASSEMBLER_NAME (thr_num_tree);
1154 : 16874 : tree num_thr_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
1155 : 16874 : tree num_thr_id = DECL_ASSEMBLER_NAME (num_thr_tree);
1156 : 16874 : bool untied_task = (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
1157 : 19364 : && omp_find_clause (gimple_omp_task_clauses (entry_stmt),
1158 : 16874 : OMP_CLAUSE_UNTIED) != NULL);
1159 : :
1160 : 294246 : FOR_EACH_BB_FN (bb, cfun)
1161 : 1544329 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1162 : : {
1163 : 989585 : gimple *call = gsi_stmt (gsi);
1164 : 989585 : tree decl;
1165 : :
1166 : 989585 : if (is_gimple_call (call)
1167 : 83612 : && (decl = gimple_call_fndecl (call))
1168 : 76272 : && DECL_EXTERNAL (decl)
1169 : 64582 : && TREE_PUBLIC (decl)
1170 : 1054167 : && DECL_INITIAL (decl) == NULL)
1171 : : {
1172 : 64578 : tree built_in;
1173 : :
1174 : 64578 : 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 : 63273 : else if (DECL_NAME (decl) == num_thr_id)
1183 : 409 : built_in = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
1184 : : else
1185 : 62864 : 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 : 16874 : }
1203 : :
1204 : : /* Callback for expand_omp_build_assign. Return non-NULL if *tp needs to be
1205 : : regimplified. */
1206 : :
1207 : : static tree
1208 : 262609 : expand_omp_regimplify_p (tree *tp, int *walk_subtrees, void *)
1209 : : {
1210 : 262609 : tree t = *tp;
1211 : :
1212 : : /* Any variable with DECL_VALUE_EXPR needs to be regimplified. */
1213 : 262609 : if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
1214 : : return t;
1215 : :
1216 : 262513 : if (TREE_CODE (t) == ADDR_EXPR)
1217 : 1720 : recompute_tree_invariant_for_addr_expr (t);
1218 : :
1219 : 262513 : *walk_subtrees = !TYPE_P (t) && !DECL_P (t);
1220 : 262513 : return NULL_TREE;
1221 : : }
1222 : :
1223 : : /* Prepend or append TO = FROM assignment before or after *GSI_P. */
1224 : :
1225 : : static void
1226 : 64641 : expand_omp_build_assign (gimple_stmt_iterator *gsi_p, tree to, tree from,
1227 : : bool after)
1228 : : {
1229 : 64641 : bool simple_p = DECL_P (to) && TREE_ADDRESSABLE (to);
1230 : 64641 : from = force_gimple_operand_gsi (gsi_p, from, simple_p, NULL_TREE,
1231 : 64641 : !after, after ? GSI_CONTINUE_LINKING
1232 : : : GSI_SAME_STMT);
1233 : 64641 : gimple *stmt = gimple_build_assign (to, from);
1234 : 64641 : if (after)
1235 : 3489 : gsi_insert_after (gsi_p, stmt, GSI_CONTINUE_LINKING);
1236 : : else
1237 : 61152 : gsi_insert_before (gsi_p, stmt, GSI_SAME_STMT);
1238 : 64641 : if (walk_tree (&from, expand_omp_regimplify_p, NULL, NULL)
1239 : 64641 : || 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 : 64641 : }
1245 : :
1246 : : /* Prepend or append LHS CODE RHS condition before or after *GSI_P. */
1247 : :
1248 : : static gcond *
1249 : 8410 : expand_omp_build_cond (gimple_stmt_iterator *gsi_p, enum tree_code code,
1250 : : tree lhs, tree rhs, bool after = false)
1251 : : {
1252 : 8410 : gcond *cond_stmt = gimple_build_cond (code, lhs, rhs, NULL_TREE, NULL_TREE);
1253 : 8410 : if (after)
1254 : 236 : gsi_insert_after (gsi_p, cond_stmt, GSI_CONTINUE_LINKING);
1255 : : else
1256 : 8174 : gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT);
1257 : 8410 : if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
1258 : : NULL, NULL)
1259 : 8410 : || 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 : 8410 : return cond_stmt;
1266 : : }
1267 : :
1268 : : /* Expand the OpenMP parallel or task directive starting at REGION. */
1269 : :
1270 : : static void
1271 : 22654 : expand_omp_taskreg (struct omp_region *region)
1272 : : {
1273 : 22654 : basic_block entry_bb, exit_bb, new_bb;
1274 : 22654 : struct function *child_cfun;
1275 : 22654 : tree child_fn, block, t;
1276 : 22654 : gimple_stmt_iterator gsi;
1277 : 22654 : gimple *entry_stmt, *stmt;
1278 : 22654 : edge e;
1279 : 22654 : vec<tree, va_gc> *ws_args;
1280 : :
1281 : 22654 : entry_stmt = last_nondebug_stmt (region->entry);
1282 : 22654 : if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
1283 : 22654 : && gimple_omp_task_taskwait_p (entry_stmt))
1284 : : {
1285 : 78 : new_bb = region->entry;
1286 : 78 : gsi = gsi_last_nondebug_bb (region->entry);
1287 : 78 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TASK);
1288 : 78 : gsi_remove (&gsi, true);
1289 : 78 : expand_taskwait_call (new_bb, as_a <gomp_task *> (entry_stmt));
1290 : 78 : return;
1291 : : }
1292 : :
1293 : 22576 : child_fn = gimple_omp_taskreg_child_fn (entry_stmt);
1294 : 22576 : child_cfun = DECL_STRUCT_FUNCTION (child_fn);
1295 : :
1296 : 22576 : entry_bb = region->entry;
1297 : 22576 : if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK)
1298 : 3783 : exit_bb = region->cont;
1299 : : else
1300 : 18793 : exit_bb = region->exit;
1301 : :
1302 : 22576 : if (is_combined_parallel (region))
1303 : 1196 : ws_args = region->ws_args;
1304 : : else
1305 : : ws_args = NULL;
1306 : :
1307 : 22576 : 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 : 22576 : 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 : 22576 : if (gimple_omp_taskreg_data_arg (entry_stmt))
1348 : : {
1349 : 18157 : basic_block entry_succ_bb
1350 : 36314 : = single_succ_p (entry_bb) ? single_succ (entry_bb)
1351 : 3105 : : FALLTHRU_EDGE (entry_bb)->dest;
1352 : 18157 : tree arg;
1353 : 18157 : gimple *parcopy_stmt = NULL;
1354 : :
1355 : 36314 : for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
1356 : : {
1357 : 18157 : gimple *stmt;
1358 : :
1359 : 18157 : gcc_assert (!gsi_end_p (gsi));
1360 : 18157 : stmt = gsi_stmt (gsi);
1361 : 18157 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
1362 : 0 : continue;
1363 : :
1364 : 18157 : if (gimple_num_ops (stmt) == 2)
1365 : : {
1366 : 18157 : tree arg = gimple_assign_rhs1 (stmt);
1367 : :
1368 : : /* We're ignore the subcode because we're
1369 : : effectively doing a STRIP_NOPS. */
1370 : :
1371 : 18157 : if (TREE_CODE (arg) == ADDR_EXPR
1372 : 18157 : && (TREE_OPERAND (arg, 0)
1373 : 18157 : == gimple_omp_taskreg_data_arg (entry_stmt)))
1374 : : {
1375 : 18157 : parcopy_stmt = stmt;
1376 : 18157 : break;
1377 : : }
1378 : : }
1379 : : }
1380 : :
1381 : 18157 : gcc_assert (parcopy_stmt != NULL);
1382 : 18157 : arg = DECL_ARGUMENTS (child_fn);
1383 : :
1384 : 18157 : if (!gimple_in_ssa_p (cfun))
1385 : : {
1386 : 17965 : if (gimple_assign_lhs (parcopy_stmt) == arg)
1387 : 17965 : 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 : 192 : tree lhs = gimple_assign_lhs (parcopy_stmt);
1398 : 192 : 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 : 192 : gimple_assign_set_rhs1 (parcopy_stmt, arg);
1406 : 192 : update_stmt (parcopy_stmt);
1407 : : }
1408 : : }
1409 : :
1410 : : /* Declare local variables needed in CHILD_CFUN. */
1411 : 22576 : block = DECL_INITIAL (child_fn);
1412 : 22576 : 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 : 253075 : for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
1417 : 230499 : if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
1418 : 0 : varpool_node::finalize_decl (t);
1419 : 22576 : DECL_SAVED_TREE (child_fn) = NULL;
1420 : : /* We'll create a CFG for child_fn, so no gimple body is needed. */
1421 : 22576 : gimple_set_body (child_fn, NULL);
1422 : 22576 : TREE_USED (block) = 1;
1423 : :
1424 : : /* Reset DECL_CONTEXT on function arguments. */
1425 : 45152 : for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
1426 : 22576 : 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 : 22576 : gsi = gsi_last_nondebug_bb (entry_bb);
1431 : 22576 : stmt = gsi_stmt (gsi);
1432 : 22576 : gcc_assert (stmt && (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
1433 : : || gimple_code (stmt) == GIMPLE_OMP_TASK
1434 : : || gimple_code (stmt) == GIMPLE_OMP_TEAMS));
1435 : 22576 : e = split_block (entry_bb, stmt);
1436 : 22576 : gsi_remove (&gsi, true);
1437 : 22576 : entry_bb = e->dest;
1438 : 22576 : edge e2 = NULL;
1439 : 22576 : if (gimple_code (entry_stmt) != GIMPLE_OMP_TASK)
1440 : 18793 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
1441 : : else
1442 : : {
1443 : 3783 : e2 = make_edge (e->src, BRANCH_EDGE (entry_bb)->dest, EDGE_ABNORMAL);
1444 : 3783 : gcc_assert (e2->dest == region->exit);
1445 : 3783 : remove_edge (BRANCH_EDGE (entry_bb));
1446 : 3783 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e->src);
1447 : 3783 : gsi = gsi_last_nondebug_bb (region->exit);
1448 : 3783 : gcc_assert (!gsi_end_p (gsi)
1449 : : && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
1450 : 3783 : gsi_remove (&gsi, true);
1451 : : }
1452 : :
1453 : : /* Convert GIMPLE_OMP_{RETURN,CONTINUE} into a RETURN_EXPR. */
1454 : 22576 : if (exit_bb)
1455 : : {
1456 : 22532 : gsi = gsi_last_nondebug_bb (exit_bb);
1457 : 41298 : gcc_assert (!gsi_end_p (gsi)
1458 : : && (gimple_code (gsi_stmt (gsi))
1459 : : == (e2 ? GIMPLE_OMP_CONTINUE : GIMPLE_OMP_RETURN)));
1460 : 22532 : stmt = gimple_build_return (NULL);
1461 : 22532 : gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
1462 : 22532 : gsi_remove (&gsi, true);
1463 : : }
1464 : :
1465 : : /* Move the parallel region into CHILD_CFUN. */
1466 : :
1467 : 22576 : if (gimple_in_ssa_p (cfun))
1468 : : {
1469 : 204 : init_tree_ssa (child_cfun);
1470 : 204 : init_ssa_operands (child_cfun);
1471 : 204 : child_cfun->gimple_df->in_ssa_p = true;
1472 : 204 : block = NULL_TREE;
1473 : : }
1474 : : else
1475 : 22372 : block = gimple_block (entry_stmt);
1476 : :
1477 : 22576 : new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
1478 : 22576 : if (exit_bb)
1479 : 22532 : single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
1480 : 22576 : if (e2)
1481 : : {
1482 : 3783 : basic_block dest_bb = e2->dest;
1483 : 3783 : if (!exit_bb)
1484 : 17 : make_edge (new_bb, dest_bb, EDGE_FALLTHRU);
1485 : 3783 : remove_edge (e2);
1486 : 3783 : 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 : 22576 : if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1491 : 22372 : child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
1492 : :
1493 : : /* Remove non-local VAR_DECLs from child_cfun->local_decls list. */
1494 : 22576 : num = vec_safe_length (child_cfun->local_decls);
1495 : 1056652 : for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
1496 : : {
1497 : 1034076 : t = (*child_cfun->local_decls)[srcidx];
1498 : 1034076 : if (DECL_CONTEXT (t) == cfun->decl)
1499 : 230499 : continue;
1500 : 803577 : if (srcidx != dstidx)
1501 : 799933 : (*child_cfun->local_decls)[dstidx] = t;
1502 : 803577 : dstidx++;
1503 : : }
1504 : 22576 : if (dstidx != num)
1505 : 20438 : vec_safe_truncate (child_cfun->local_decls, dstidx);
1506 : :
1507 : : /* Inform the callgraph about the new function. */
1508 : 22576 : child_cfun->curr_properties = cfun->curr_properties;
1509 : 22576 : child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
1510 : 22576 : child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
1511 : 22576 : cgraph_node *node = cgraph_node::get_create (child_fn);
1512 : 22576 : node->parallelized_function = 1;
1513 : 45152 : node->has_omp_variant_constructs
1514 : 22576 : |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
1515 : 22576 : cgraph_node::add_new_function (child_fn, true);
1516 : :
1517 : 22576 : bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
1518 : 22576 : && !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 : 22576 : push_cfun (child_cfun);
1523 : 22576 : if (need_asm)
1524 : 22372 : assign_assembler_name_if_needed (child_fn);
1525 : :
1526 : 22576 : if (optimize)
1527 : 16874 : optimize_omp_library_calls (entry_stmt);
1528 : 22576 : update_max_bb_count ();
1529 : 22576 : 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 : 22576 : if (flag_exceptions)
1536 : : {
1537 : 10138 : basic_block bb;
1538 : 10138 : bool changed = false;
1539 : :
1540 : 135034 : FOR_EACH_BB_FN (bb, cfun)
1541 : 124896 : changed |= gimple_purge_dead_eh_edges (bb);
1542 : 10138 : if (changed)
1543 : 0 : cleanup_tree_cfg ();
1544 : : }
1545 : 22576 : if (gimple_in_ssa_p (cfun))
1546 : 204 : update_ssa (TODO_update_ssa);
1547 : 22576 : if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1548 : 204 : verify_loop_structure ();
1549 : 22576 : pop_cfun ();
1550 : :
1551 : 22576 : 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 : 22576 : adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
1560 : :
1561 : 22576 : if (gimple_code (entry_stmt) == GIMPLE_OMP_PARALLEL)
1562 : 16258 : expand_parallel_call (region, new_bb,
1563 : : as_a <gomp_parallel *> (entry_stmt), ws_args);
1564 : 6318 : 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 : 3783 : 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 : 10405 : 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 : 10405 : tree t, type = TREE_TYPE (fd->loop.v);
1837 : 10405 : edge e, ne;
1838 : 10405 : int i;
1839 : :
1840 : : /* Collapsed loops need work for expansion into SSA form. */
1841 : 10405 : gcc_assert (!gimple_in_ssa_p (cfun));
1842 : :
1843 : 10405 : if (gimple_omp_for_combined_into_p (fd->for_stmt)
1844 : 10405 : && TREE_CODE (fd->loop.n2) != INTEGER_CST)
1845 : : {
1846 : 2680 : 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 : 2680 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
1851 : : OMP_CLAUSE__LOOPTEMP_);
1852 : 2680 : gcc_assert (innerc);
1853 : 10516 : for (i = 0; i < fd->collapse; i++)
1854 : : {
1855 : 7836 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
1856 : : OMP_CLAUSE__LOOPTEMP_);
1857 : 7836 : gcc_assert (innerc);
1858 : 7836 : if (i)
1859 : 5156 : counts[i] = OMP_CLAUSE_DECL (innerc);
1860 : : else
1861 : 2680 : counts[0] = NULL_TREE;
1862 : : }
1863 : 2680 : if (fd->non_rect
1864 : 112 : && fd->last_nonrect == fd->first_nonrect + 1
1865 : 2744 : && !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 : 2680 : return;
1881 : : }
1882 : :
1883 : 8618 : 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 : : bool rect_count_seen = false;
1898 : 30020 : for (i = 0; i < (fd->ordered ? fd->ordered : fd->collapse); i++)
1899 : : {
1900 : 22295 : tree itype = TREE_TYPE (fd->loops[i].v);
1901 : :
1902 : 22295 : if (i >= fd->collapse && counts[i])
1903 : 0 : continue;
1904 : 22295 : if (fd->non_rect)
1905 : : {
1906 : : /* Skip loops that use outer iterators in their expressions
1907 : : during this phase. */
1908 : 1050 : if (fd->loops[i].m1 || fd->loops[i].m2)
1909 : : {
1910 : 432 : counts[i] = build_zero_cst (type);
1911 : 432 : continue;
1912 : : }
1913 : : }
1914 : 21863 : if ((SSA_VAR_P (fd->loop.n2) || i >= fd->collapse)
1915 : 9499 : && ((t = fold_binary (fd->loops[i].cond_code, boolean_type_node,
1916 : : fold_convert (itype, fd->loops[i].n1),
1917 : : fold_convert (itype, fd->loops[i].n2)))
1918 : 5876 : == NULL_TREE || !integer_onep (t)))
1919 : : {
1920 : 5774 : gcond *cond_stmt;
1921 : 5774 : tree n1, n2;
1922 : 5774 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].n1));
1923 : 5774 : n1 = force_gimple_operand_gsi (gsi, n1, true, NULL_TREE,
1924 : : true, GSI_SAME_STMT);
1925 : 5774 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].n2));
1926 : 5774 : n2 = force_gimple_operand_gsi (gsi, n2, true, NULL_TREE,
1927 : : true, GSI_SAME_STMT);
1928 : 5774 : cond_stmt = expand_omp_build_cond (gsi, fd->loops[i].cond_code,
1929 : : n1, n2);
1930 : 5774 : e = split_block (entry_bb, cond_stmt);
1931 : 11548 : basic_block &zero_iter_bb
1932 : 5774 : = i < fd->collapse ? zero_iter1_bb : zero_iter2_bb;
1933 : 11548 : int &first_zero_iter
1934 : 5774 : = i < fd->collapse ? first_zero_iter1 : first_zero_iter2;
1935 : 5774 : if (zero_iter_bb == NULL)
1936 : : {
1937 : 626 : gassign *assign_stmt;
1938 : 626 : first_zero_iter = i;
1939 : 626 : zero_iter_bb = create_empty_bb (entry_bb);
1940 : 626 : add_bb_to_loop (zero_iter_bb, entry_bb->loop_father);
1941 : 626 : *gsi = gsi_after_labels (zero_iter_bb);
1942 : 626 : if (i < fd->collapse)
1943 : 508 : assign_stmt = gimple_build_assign (fd->loop.n2,
1944 : : build_zero_cst (type));
1945 : : else
1946 : : {
1947 : 118 : counts[i] = create_tmp_reg (type, ".count");
1948 : 118 : assign_stmt
1949 : 118 : = gimple_build_assign (counts[i], build_zero_cst (type));
1950 : : }
1951 : 626 : gsi_insert_before (gsi, assign_stmt, GSI_SAME_STMT);
1952 : 626 : set_immediate_dominator (CDI_DOMINATORS, zero_iter_bb,
1953 : : entry_bb);
1954 : : }
1955 : 5774 : ne = make_edge (entry_bb, zero_iter_bb, EDGE_FALSE_VALUE);
1956 : 5774 : ne->probability = profile_probability::very_unlikely ();
1957 : 5774 : e->flags = EDGE_TRUE_VALUE;
1958 : 5774 : e->probability = ne->probability.invert ();
1959 : 5774 : if (l2_dom_bb == NULL)
1960 : 2584 : l2_dom_bb = entry_bb;
1961 : 5774 : entry_bb = e->dest;
1962 : 5774 : *gsi = gsi_last_nondebug_bb (entry_bb);
1963 : : }
1964 : :
1965 : 21863 : if (POINTER_TYPE_P (itype))
1966 : 1661 : itype = signed_type_for (itype);
1967 : 21863 : t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
1968 : 26959 : ? -1 : 1));
1969 : 21863 : t = fold_build2 (PLUS_EXPR, itype,
1970 : : fold_convert (itype, fd->loops[i].step), t);
1971 : 21863 : t = fold_build2 (PLUS_EXPR, itype, t,
1972 : : fold_convert (itype, fd->loops[i].n2));
1973 : 21863 : t = fold_build2 (MINUS_EXPR, itype, t,
1974 : : fold_convert (itype, fd->loops[i].n1));
1975 : : /* ?? We could probably use CEIL_DIV_EXPR instead of
1976 : : TRUNC_DIV_EXPR and adjusting by hand. Unless we can't
1977 : : generate the same code in the end because generically we
1978 : : don't know that the values involved must be negative for
1979 : : GT?? */
1980 : 21863 : if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
1981 : 2240 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
1982 : : fold_build1 (NEGATE_EXPR, itype, t),
1983 : : fold_build1 (NEGATE_EXPR, itype,
1984 : : fold_convert (itype,
1985 : : fd->loops[i].step)));
1986 : : else
1987 : 19623 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
1988 : : fold_convert (itype, fd->loops[i].step));
1989 : 21863 : t = fold_convert (type, t);
1990 : 21863 : if (TREE_CODE (t) == INTEGER_CST)
1991 : 16019 : counts[i] = t;
1992 : : else
1993 : : {
1994 : 5844 : if (i < fd->collapse || i != first_zero_iter2)
1995 : 5726 : counts[i] = create_tmp_reg (type, ".count");
1996 : 5844 : expand_omp_build_assign (gsi, counts[i], t);
1997 : : }
1998 : 21863 : if (SSA_VAR_P (fd->loop.n2) && i < fd->collapse)
1999 : : {
2000 : 8606 : if (fd->non_rect && i >= fd->first_nonrect && i <= fd->last_nonrect)
2001 : 376 : continue;
2002 : 8230 : if (!rect_count_seen)
2003 : : {
2004 : 2988 : t = counts[i];
2005 : 2988 : rect_count_seen = true;
2006 : : }
2007 : : else
2008 : 5242 : t = fold_build2 (MULT_EXPR, type, fd->loop.n2, counts[i]);
2009 : 8230 : expand_omp_build_assign (gsi, fd->loop.n2, t);
2010 : : }
2011 : : }
2012 : 7725 : if (fd->non_rect && SSA_VAR_P (fd->loop.n2))
2013 : : {
2014 : 284 : gcc_assert (fd->last_nonrect != -1);
2015 : :
2016 : 284 : counts[fd->last_nonrect] = create_tmp_reg (type, ".count");
2017 : 284 : expand_omp_build_assign (gsi, counts[fd->last_nonrect],
2018 : : build_zero_cst (type));
2019 : 366 : for (i = fd->first_nonrect + 1; i < fd->last_nonrect; i++)
2020 : 113 : if (fd->loops[i].m1
2021 : 83 : || fd->loops[i].m2
2022 : 82 : || fd->loops[i].non_rect_referenced)
2023 : : break;
2024 : 284 : if (i == fd->last_nonrect
2025 : 253 : && fd->loops[i].outer == fd->last_nonrect - fd->first_nonrect
2026 : 253 : && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
2027 : 511 : && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[i].v)))
2028 : : {
2029 : 225 : int o = fd->first_nonrect;
2030 : 225 : tree itype = TREE_TYPE (fd->loops[o].v);
2031 : 225 : tree n1o = create_tmp_reg (itype, ".n1o");
2032 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[o].n1));
2033 : 225 : expand_omp_build_assign (gsi, n1o, t);
2034 : 225 : tree n2o = create_tmp_reg (itype, ".n2o");
2035 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[o].n2));
2036 : 225 : expand_omp_build_assign (gsi, n2o, t);
2037 : 225 : if (fd->loops[i].m1 && fd->loops[i].m2)
2038 : 43 : t = fold_build2 (MINUS_EXPR, itype, unshare_expr (fd->loops[i].m2),
2039 : : unshare_expr (fd->loops[i].m1));
2040 : 182 : else if (fd->loops[i].m1)
2041 : 162 : t = fold_build1 (NEGATE_EXPR, itype,
2042 : : unshare_expr (fd->loops[i].m1));
2043 : : else
2044 : 20 : t = unshare_expr (fd->loops[i].m2);
2045 : 225 : tree m2minusm1
2046 : 225 : = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
2047 : : true, GSI_SAME_STMT);
2048 : :
2049 : 225 : gimple_stmt_iterator gsi2 = *gsi;
2050 : 225 : gsi_prev (&gsi2);
2051 : 225 : e = split_block (entry_bb, gsi_stmt (gsi2));
2052 : 225 : e = split_block (e->dest, (gimple *) NULL);
2053 : 225 : basic_block bb1 = e->src;
2054 : 225 : entry_bb = e->dest;
2055 : 225 : *gsi = gsi_after_labels (entry_bb);
2056 : :
2057 : 225 : gsi2 = gsi_after_labels (bb1);
2058 : 225 : tree ostep = fold_convert (itype, fd->loops[o].step);
2059 : 225 : t = build_int_cst (itype, (fd->loops[o].cond_code
2060 : 241 : == LT_EXPR ? -1 : 1));
2061 : 225 : t = fold_build2 (PLUS_EXPR, itype, ostep, t);
2062 : 225 : t = fold_build2 (PLUS_EXPR, itype, t, n2o);
2063 : 225 : t = fold_build2 (MINUS_EXPR, itype, t, n1o);
2064 : 225 : if (TYPE_UNSIGNED (itype)
2065 : 225 : && fd->loops[o].cond_code == GT_EXPR)
2066 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2067 : : fold_build1 (NEGATE_EXPR, itype, t),
2068 : : fold_build1 (NEGATE_EXPR, itype, ostep));
2069 : : else
2070 : 225 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, ostep);
2071 : 225 : tree outer_niters
2072 : 225 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2073 : : true, GSI_SAME_STMT);
2074 : 225 : t = fold_build2 (MINUS_EXPR, itype, outer_niters,
2075 : : build_one_cst (itype));
2076 : 225 : t = fold_build2 (MULT_EXPR, itype, t, ostep);
2077 : 225 : t = fold_build2 (PLUS_EXPR, itype, n1o, t);
2078 : 225 : tree last = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2079 : : true, GSI_SAME_STMT);
2080 : 225 : tree n1, n2, n1e, n2e;
2081 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2082 : 225 : if (fd->loops[i].m1)
2083 : : {
2084 : 205 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2085 : 205 : n1 = fold_build2 (MULT_EXPR, itype, n1o, n1);
2086 : 205 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2087 : : }
2088 : : else
2089 : : n1 = t;
2090 : 225 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2091 : : true, GSI_SAME_STMT);
2092 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2093 : 225 : if (fd->loops[i].m2)
2094 : : {
2095 : 63 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2096 : 63 : n2 = fold_build2 (MULT_EXPR, itype, n1o, n2);
2097 : 63 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2098 : : }
2099 : : else
2100 : : n2 = t;
2101 : 225 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2102 : : true, GSI_SAME_STMT);
2103 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2104 : 225 : if (fd->loops[i].m1)
2105 : : {
2106 : 205 : n1e = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2107 : 205 : n1e = fold_build2 (MULT_EXPR, itype, last, n1e);
2108 : 205 : n1e = fold_build2 (PLUS_EXPR, itype, n1e, t);
2109 : : }
2110 : : else
2111 : : n1e = t;
2112 : 225 : n1e = force_gimple_operand_gsi (&gsi2, n1e, true, NULL_TREE,
2113 : : true, GSI_SAME_STMT);
2114 : 225 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2115 : 225 : if (fd->loops[i].m2)
2116 : : {
2117 : 63 : n2e = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2118 : 63 : n2e = fold_build2 (MULT_EXPR, itype, last, n2e);
2119 : 63 : n2e = fold_build2 (PLUS_EXPR, itype, n2e, t);
2120 : : }
2121 : : else
2122 : : n2e = t;
2123 : 225 : n2e = force_gimple_operand_gsi (&gsi2, n2e, true, NULL_TREE,
2124 : : true, GSI_SAME_STMT);
2125 : 225 : gcond *cond_stmt
2126 : 225 : = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2127 : : n1, n2);
2128 : 225 : e = split_block (bb1, cond_stmt);
2129 : 225 : e->flags = EDGE_TRUE_VALUE;
2130 : 225 : e->probability = profile_probability::likely ().guessed ();
2131 : 225 : basic_block bb2 = e->dest;
2132 : 225 : gsi2 = gsi_after_labels (bb2);
2133 : :
2134 : 225 : cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2135 : : n1e, n2e);
2136 : 225 : e = split_block (bb2, cond_stmt);
2137 : 225 : e->flags = EDGE_TRUE_VALUE;
2138 : 225 : e->probability = profile_probability::likely ().guessed ();
2139 : 225 : gsi2 = gsi_after_labels (e->dest);
2140 : :
2141 : 225 : tree step = fold_convert (itype, fd->loops[i].step);
2142 : 225 : t = build_int_cst (itype, (fd->loops[i].cond_code
2143 : 241 : == LT_EXPR ? -1 : 1));
2144 : 225 : t = fold_build2 (PLUS_EXPR, itype, step, t);
2145 : 225 : t = fold_build2 (PLUS_EXPR, itype, t, n2);
2146 : 225 : t = fold_build2 (MINUS_EXPR, itype, t, n1);
2147 : 225 : if (TYPE_UNSIGNED (itype)
2148 : 225 : && fd->loops[i].cond_code == GT_EXPR)
2149 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2150 : : fold_build1 (NEGATE_EXPR, itype, t),
2151 : : fold_build1 (NEGATE_EXPR, itype, step));
2152 : : else
2153 : 225 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2154 : 225 : tree first_inner_iterations
2155 : 225 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2156 : : true, GSI_SAME_STMT);
2157 : 225 : t = fold_build2 (MULT_EXPR, itype, m2minusm1, ostep);
2158 : 225 : if (TYPE_UNSIGNED (itype)
2159 : 225 : && fd->loops[i].cond_code == GT_EXPR)
2160 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2161 : : fold_build1 (NEGATE_EXPR, itype, t),
2162 : : fold_build1 (NEGATE_EXPR, itype, step));
2163 : : else
2164 : 225 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2165 : 225 : tree factor
2166 : 225 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2167 : : true, GSI_SAME_STMT);
2168 : 225 : t = fold_build2 (MINUS_EXPR, itype, outer_niters,
2169 : : build_one_cst (itype));
2170 : 225 : t = fold_build2 (MULT_EXPR, itype, t, outer_niters);
2171 : 225 : t = fold_build2 (RSHIFT_EXPR, itype, t, integer_one_node);
2172 : 225 : t = fold_build2 (MULT_EXPR, itype, factor, t);
2173 : 225 : t = fold_build2 (PLUS_EXPR, itype,
2174 : : fold_build2 (MULT_EXPR, itype, outer_niters,
2175 : : first_inner_iterations), t);
2176 : 225 : expand_omp_build_assign (&gsi2, counts[fd->last_nonrect],
2177 : : fold_convert (type, t));
2178 : :
2179 : 225 : basic_block bb3 = create_empty_bb (bb1);
2180 : 225 : add_bb_to_loop (bb3, bb1->loop_father);
2181 : :
2182 : 225 : e = make_edge (bb1, bb3, EDGE_FALSE_VALUE);
2183 : 225 : e->probability = profile_probability::unlikely ().guessed ();
2184 : :
2185 : 225 : gsi2 = gsi_after_labels (bb3);
2186 : 225 : cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2187 : : n1e, n2e);
2188 : 225 : e = split_block (bb3, cond_stmt);
2189 : 225 : e->flags = EDGE_TRUE_VALUE;
2190 : 225 : e->probability = profile_probability::likely ().guessed ();
2191 : 225 : basic_block bb4 = e->dest;
2192 : :
2193 : 225 : ne = make_edge (bb3, entry_bb, EDGE_FALSE_VALUE);
2194 : 225 : ne->probability = e->probability.invert ();
2195 : :
2196 : 225 : basic_block bb5 = create_empty_bb (bb2);
2197 : 225 : add_bb_to_loop (bb5, bb2->loop_father);
2198 : :
2199 : 225 : ne = make_edge (bb2, bb5, EDGE_FALSE_VALUE);
2200 : 225 : ne->probability = profile_probability::unlikely ().guessed ();
2201 : :
2202 : 675 : for (int j = 0; j < 2; j++)
2203 : : {
2204 : 450 : gsi2 = gsi_after_labels (j ? bb5 : bb4);
2205 : 450 : t = fold_build2 (MINUS_EXPR, itype,
2206 : : unshare_expr (fd->loops[i].n1),
2207 : : unshare_expr (fd->loops[i].n2));
2208 : 450 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, m2minusm1);
2209 : 450 : tree tem
2210 : 450 : = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2211 : : true, GSI_SAME_STMT);
2212 : 450 : t = fold_build2 (MINUS_EXPR, itype, tem, n1o);
2213 : 450 : t = fold_build2 (TRUNC_MOD_EXPR, itype, t, ostep);
2214 : 450 : t = fold_build2 (MINUS_EXPR, itype, tem, t);
2215 : 450 : tem = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2216 : : true, GSI_SAME_STMT);
2217 : 450 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2218 : 450 : if (fd->loops[i].m1)
2219 : : {
2220 : 410 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2221 : 410 : n1 = fold_build2 (MULT_EXPR, itype, tem, n1);
2222 : 410 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2223 : : }
2224 : : else
2225 : : n1 = t;
2226 : 450 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2227 : : true, GSI_SAME_STMT);
2228 : 450 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2229 : 450 : if (fd->loops[i].m2)
2230 : : {
2231 : 126 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2232 : 126 : n2 = fold_build2 (MULT_EXPR, itype, tem, n2);
2233 : 126 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2234 : : }
2235 : : else
2236 : : n2 = t;
2237 : 450 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2238 : : true, GSI_SAME_STMT);
2239 : 675 : expand_omp_build_assign (&gsi2, j ? n2o : n1o, tem);
2240 : :
2241 : 450 : cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2242 : : n1, n2);
2243 : 450 : e = split_block (gsi_bb (gsi2), cond_stmt);
2244 : 450 : e->flags = j ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE;
2245 : 450 : e->probability = profile_probability::unlikely ().guessed ();
2246 : 675 : ne = make_edge (e->src, bb1,
2247 : : j ? EDGE_FALSE_VALUE : EDGE_TRUE_VALUE);
2248 : 450 : ne->probability = e->probability.invert ();
2249 : 450 : gsi2 = gsi_after_labels (e->dest);
2250 : :
2251 : 450 : t = fold_build2 (PLUS_EXPR, itype, tem, ostep);
2252 : 450 : expand_omp_build_assign (&gsi2, j ? n2o : n1o, t);
2253 : :
2254 : 450 : make_edge (e->dest, bb1, EDGE_FALLTHRU);
2255 : : }
2256 : :
2257 : 225 : set_immediate_dominator (CDI_DOMINATORS, bb3, bb1);
2258 : 225 : set_immediate_dominator (CDI_DOMINATORS, bb5, bb2);
2259 : 225 : set_immediate_dominator (CDI_DOMINATORS, entry_bb, bb1);
2260 : :
2261 : 225 : if (fd->first_nonrect + 1 == fd->last_nonrect)
2262 : : {
2263 : 153 : fd->first_inner_iterations = first_inner_iterations;
2264 : 153 : fd->factor = factor;
2265 : 153 : fd->adjn1 = n1o;
2266 : : }
2267 : : }
2268 : : else
2269 : : {
2270 : : /* Fallback implementation. Evaluate the loops with m1/m2
2271 : : non-NULL as well as their outer loops at runtime using temporaries
2272 : : instead of the original iteration variables, and in the
2273 : : body just bump the counter. */
2274 : 59 : gimple_stmt_iterator gsi2 = *gsi;
2275 : 59 : gsi_prev (&gsi2);
2276 : 59 : e = split_block (entry_bb, gsi_stmt (gsi2));
2277 : 59 : e = split_block (e->dest, (gimple *) NULL);
2278 : 59 : basic_block cur_bb = e->src;
2279 : 59 : basic_block next_bb = e->dest;
2280 : 59 : entry_bb = e->dest;
2281 : 59 : *gsi = gsi_after_labels (entry_bb);
2282 : :
2283 : 59 : tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
2284 : 59 : memset (vs, 0, fd->last_nonrect * sizeof (tree));
2285 : :
2286 : 189 : for (i = 0; i <= fd->last_nonrect; i++)
2287 : : {
2288 : 189 : if (fd->loops[i].m1 == NULL_TREE
2289 : 108 : && fd->loops[i].m2 == NULL_TREE
2290 : 99 : && !fd->loops[i].non_rect_referenced)
2291 : 40 : continue;
2292 : :
2293 : 149 : tree itype = TREE_TYPE (fd->loops[i].v);
2294 : :
2295 : 149 : gsi2 = gsi_after_labels (cur_bb);
2296 : 149 : tree n1, n2;
2297 : 149 : t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2298 : 149 : if (fd->loops[i].m1 == NULL_TREE)
2299 : : n1 = t;
2300 : 81 : else if (POINTER_TYPE_P (itype))
2301 : : {
2302 : 30 : gcc_assert (integer_onep (fd->loops[i].m1));
2303 : 30 : t = unshare_expr (fd->loops[i].n1);
2304 : 30 : n1 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
2305 : : }
2306 : : else
2307 : : {
2308 : 51 : n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2309 : 51 : n1 = fold_build2 (MULT_EXPR, itype,
2310 : : vs[i - fd->loops[i].outer], n1);
2311 : 51 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2312 : : }
2313 : 149 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2314 : : true, GSI_SAME_STMT);
2315 : 149 : if (i < fd->last_nonrect)
2316 : : {
2317 : 90 : vs[i] = create_tmp_reg (itype, ".it");
2318 : 90 : expand_omp_build_assign (&gsi2, vs[i], n1);
2319 : : }
2320 : 149 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2321 : 149 : if (fd->loops[i].m2 == NULL_TREE)
2322 : : n2 = t;
2323 : 85 : else if (POINTER_TYPE_P (itype))
2324 : : {
2325 : 34 : gcc_assert (integer_onep (fd->loops[i].m2));
2326 : 34 : t = unshare_expr (fd->loops[i].n2);
2327 : 34 : n2 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
2328 : : }
2329 : : else
2330 : : {
2331 : 51 : n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2332 : 51 : n2 = fold_build2 (MULT_EXPR, itype,
2333 : : vs[i - fd->loops[i].outer], n2);
2334 : 51 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2335 : : }
2336 : 149 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2337 : : true, GSI_SAME_STMT);
2338 : 149 : if (POINTER_TYPE_P (itype))
2339 : 70 : itype = signed_type_for (itype);
2340 : 149 : if (i == fd->last_nonrect)
2341 : : {
2342 : 59 : gcond *cond_stmt
2343 : 59 : = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2344 : : n1, n2);
2345 : 59 : e = split_block (cur_bb, cond_stmt);
2346 : 59 : e->flags = EDGE_TRUE_VALUE;
2347 : 59 : ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
2348 : 59 : e->probability = profile_probability::likely ().guessed ();
2349 : 59 : ne->probability = e->probability.invert ();
2350 : 59 : gsi2 = gsi_after_labels (e->dest);
2351 : :
2352 : 59 : t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
2353 : 79 : ? -1 : 1));
2354 : 59 : t = fold_build2 (PLUS_EXPR, itype,
2355 : : fold_convert (itype, fd->loops[i].step), t);
2356 : 59 : t = fold_build2 (PLUS_EXPR, itype, t,
2357 : : fold_convert (itype, n2));
2358 : 59 : t = fold_build2 (MINUS_EXPR, itype, t,
2359 : : fold_convert (itype, n1));
2360 : 59 : tree step = fold_convert (itype, fd->loops[i].step);
2361 : 59 : if (TYPE_UNSIGNED (itype)
2362 : 59 : && fd->loops[i].cond_code == GT_EXPR)
2363 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2364 : : fold_build1 (NEGATE_EXPR, itype, t),
2365 : : fold_build1 (NEGATE_EXPR, itype, step));
2366 : : else
2367 : 59 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2368 : 59 : t = fold_convert (type, t);
2369 : 59 : t = fold_build2 (PLUS_EXPR, type,
2370 : : counts[fd->last_nonrect], t);
2371 : 59 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2372 : : true, GSI_SAME_STMT);
2373 : 59 : expand_omp_build_assign (&gsi2, counts[fd->last_nonrect], t);
2374 : 59 : e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
2375 : 59 : set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
2376 : 59 : break;
2377 : : }
2378 : 90 : e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
2379 : :
2380 : 90 : basic_block new_cur_bb = create_empty_bb (cur_bb);
2381 : 90 : add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
2382 : :
2383 : 90 : gsi2 = gsi_after_labels (e->dest);
2384 : 90 : tree step = fold_convert (itype,
2385 : : unshare_expr (fd->loops[i].step));
2386 : 90 : if (POINTER_TYPE_P (TREE_TYPE (vs[i])))
2387 : 38 : t = fold_build_pointer_plus (vs[i], step);
2388 : : else
2389 : 52 : t = fold_build2 (PLUS_EXPR, itype, vs[i], step);
2390 : 90 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2391 : : true, GSI_SAME_STMT);
2392 : 90 : expand_omp_build_assign (&gsi2, vs[i], t);
2393 : :
2394 : 90 : ne = split_block (e->dest, last_nondebug_stmt (e->dest));
2395 : 90 : gsi2 = gsi_after_labels (ne->dest);
2396 : :
2397 : 90 : expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, vs[i], n2);
2398 : 90 : edge e3, e4;
2399 : 90 : if (next_bb == entry_bb)
2400 : : {
2401 : 59 : e3 = find_edge (ne->dest, next_bb);
2402 : 59 : e3->flags = EDGE_FALSE_VALUE;
2403 : : }
2404 : : else
2405 : 31 : e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
2406 : 90 : e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
2407 : 90 : e4->probability = profile_probability::likely ().guessed ();
2408 : 90 : e3->probability = e4->probability.invert ();
2409 : 90 : basic_block esrc = e->src;
2410 : 90 : make_edge (e->src, ne->dest, EDGE_FALLTHRU);
2411 : 90 : cur_bb = new_cur_bb;
2412 : 90 : basic_block latch_bb = next_bb;
2413 : 90 : next_bb = e->dest;
2414 : 90 : remove_edge (e);
2415 : 90 : set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
2416 : 90 : set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
2417 : 90 : set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
2418 : : }
2419 : : }
2420 : 284 : t = NULL_TREE;
2421 : 691 : for (i = fd->first_nonrect; i < fd->last_nonrect; i++)
2422 : 407 : if (!fd->loops[i].non_rect_referenced
2423 : 92 : && fd->loops[i].m1 == NULL_TREE
2424 : 92 : && fd->loops[i].m2 == NULL_TREE)
2425 : : {
2426 : 92 : if (t == NULL_TREE)
2427 : 82 : t = counts[i];
2428 : : else
2429 : 10 : t = fold_build2 (MULT_EXPR, type, t, counts[i]);
2430 : : }
2431 : 284 : if (t)
2432 : : {
2433 : 82 : t = fold_build2 (MULT_EXPR, type, counts[fd->last_nonrect], t);
2434 : 82 : expand_omp_build_assign (gsi, counts[fd->last_nonrect], t);
2435 : : }
2436 : 284 : if (!rect_count_seen)
2437 : 186 : t = counts[fd->last_nonrect];
2438 : : else
2439 : 98 : t = fold_build2 (MULT_EXPR, type, fd->loop.n2,
2440 : : counts[fd->last_nonrect]);
2441 : 284 : expand_omp_build_assign (gsi, fd->loop.n2, t);
2442 : 284 : }
2443 : 7441 : else if (fd->non_rect)
2444 : : {
2445 : 117 : tree t = fd->loop.n2;
2446 : 117 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
2447 : : int non_rect_referenced = 0, non_rect = 0;
2448 : 358 : for (i = 0; i < fd->collapse; i++)
2449 : : {
2450 : 240 : if ((i < fd->first_nonrect || i > fd->last_nonrect)
2451 : 246 : && !integer_zerop (counts[i]))
2452 : 6 : t = fold_build2 (TRUNC_DIV_EXPR, type, t, counts[i]);
2453 : 241 : if (fd->loops[i].non_rect_referenced)
2454 : 117 : non_rect_referenced++;
2455 : 241 : if (fd->loops[i].m1 || fd->loops[i].m2)
2456 : 117 : non_rect++;
2457 : : }
2458 : 117 : gcc_assert (non_rect == 1 && non_rect_referenced == 1);
2459 : 117 : counts[fd->last_nonrect] = t;
2460 : : }
2461 : : }
2462 : :
2463 : : /* Helper function for expand_omp_{for_*,simd}. Generate code like:
2464 : : T = V;
2465 : : V3 = N31 + (T % count3) * STEP3;
2466 : : T = T / count3;
2467 : : V2 = N21 + (T % count2) * STEP2;
2468 : : T = T / count2;
2469 : : V1 = N11 + T * STEP1;
2470 : : if this loop doesn't have an inner loop construct combined with it.
2471 : : If it does have an inner loop construct combined with it and the
2472 : : iteration count isn't known constant, store values from counts array
2473 : : into its _looptemp_ temporaries instead.
2474 : : For non-rectangular loops (between fd->first_nonrect and fd->last_nonrect
2475 : : inclusive), use the count of all those loops together, and either
2476 : : find quadratic etc. equation roots, or as a fallback, do:
2477 : : COUNT = 0;
2478 : : for (tmpi = N11; tmpi COND1 N12; tmpi += STEP1)
2479 : : for (tmpj = M21 * tmpi + N21;
2480 : : tmpj COND2 M22 * tmpi + N22; tmpj += STEP2)
2481 : : {
2482 : : int tmpk1 = M31 * tmpj + N31;
2483 : : int tmpk2 = M32 * tmpj + N32;
2484 : : if (tmpk1 COND3 tmpk2)
2485 : : {
2486 : : if (COND3 is <)
2487 : : adj = STEP3 - 1;
2488 : : else
2489 : : adj = STEP3 + 1;
2490 : : int temp = (adj + tmpk2 - tmpk1) / STEP3;
2491 : : if (COUNT + temp > T)
2492 : : {
2493 : : V1 = tmpi;
2494 : : V2 = tmpj;
2495 : : V3 = tmpk1 + (T - COUNT) * STEP3;
2496 : : goto done;
2497 : : }
2498 : : else
2499 : : COUNT += temp;
2500 : : }
2501 : : }
2502 : : done:;
2503 : : but for optional innermost or outermost rectangular loops that aren't
2504 : : referenced by other loop expressions keep doing the division/modulo. */
2505 : :
2506 : : static void
2507 : 10221 : expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
2508 : : tree *counts, tree *nonrect_bounds,
2509 : : gimple *inner_stmt, tree startvar)
2510 : : {
2511 : 10221 : int i;
2512 : 10221 : if (gimple_omp_for_combined_p (fd->for_stmt))
2513 : : {
2514 : : /* If fd->loop.n2 is constant, then no propagation of the counts
2515 : : is needed, they are constant. */
2516 : 4840 : if (TREE_CODE (fd->loop.n2) == INTEGER_CST)
2517 : : return;
2518 : :
2519 : 2680 : tree clauses = gimple_code (inner_stmt) != GIMPLE_OMP_FOR
2520 : 4140 : ? gimple_omp_taskreg_clauses (inner_stmt)
2521 : 1220 : : gimple_omp_for_clauses (inner_stmt);
2522 : : /* First two _looptemp_ clauses are for istart/iend, counts[0]
2523 : : isn't supposed to be handled, as the inner loop doesn't
2524 : : use it. */
2525 : 2680 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
2526 : 2680 : gcc_assert (innerc);
2527 : 2680 : int count = 0;
2528 : 2680 : if (fd->non_rect
2529 : 112 : && fd->last_nonrect == fd->first_nonrect + 1
2530 : 2744 : && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
2531 : : count = 4;
2532 : 10756 : for (i = 0; i < fd->collapse + count; i++)
2533 : : {
2534 : 8076 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
2535 : : OMP_CLAUSE__LOOPTEMP_);
2536 : 8076 : gcc_assert (innerc);
2537 : 8076 : if (i)
2538 : : {
2539 : 5396 : tree tem = OMP_CLAUSE_DECL (innerc);
2540 : 5396 : tree t;
2541 : 5396 : if (i < fd->collapse)
2542 : 5156 : t = counts[i];
2543 : : else
2544 : 240 : switch (i - fd->collapse)
2545 : : {
2546 : 60 : case 0: t = counts[0]; break;
2547 : 60 : case 1: t = fd->first_inner_iterations; break;
2548 : 60 : case 2: t = fd->factor; break;
2549 : 60 : case 3: t = fd->adjn1; break;
2550 : 0 : default: gcc_unreachable ();
2551 : : }
2552 : 5396 : t = fold_convert (TREE_TYPE (tem), t);
2553 : 5396 : t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
2554 : : false, GSI_CONTINUE_LINKING);
2555 : 5396 : gassign *stmt = gimple_build_assign (tem, t);
2556 : 5396 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
2557 : : }
2558 : : }
2559 : : return;
2560 : : }
2561 : :
2562 : 5381 : tree type = TREE_TYPE (fd->loop.v);
2563 : 5381 : tree tem = create_tmp_reg (type, ".tem");
2564 : 5381 : gassign *stmt = gimple_build_assign (tem, startvar);
2565 : 5381 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
2566 : :
2567 : 20026 : for (i = fd->collapse - 1; i >= 0; i--)
2568 : : {
2569 : 14645 : tree vtype = TREE_TYPE (fd->loops[i].v), itype, t;
2570 : 14645 : itype = vtype;
2571 : 14645 : if (POINTER_TYPE_P (vtype))
2572 : 1649 : itype = signed_type_for (vtype);
2573 : 14645 : if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
2574 : 9264 : t = fold_build2 (TRUNC_MOD_EXPR, type, tem, counts[i]);
2575 : : else
2576 : : t = tem;
2577 : 14645 : if (i == fd->last_nonrect)
2578 : : {
2579 : 376 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
2580 : : false, GSI_CONTINUE_LINKING);
2581 : 376 : tree stopval = t;
2582 : 376 : tree idx = create_tmp_reg (type, ".count");
2583 : 376 : expand_omp_build_assign (gsi, idx,
2584 : : build_zero_cst (type), true);
2585 : 376 : basic_block bb_triang = NULL, bb_triang_dom = NULL;
2586 : 376 : if (fd->first_nonrect + 1 == fd->last_nonrect
2587 : 272 : && (TREE_CODE (fd->loop.n2) == INTEGER_CST
2588 : 181 : || fd->first_inner_iterations)
2589 : 488 : && (optab_handler (sqrt_optab, TYPE_MODE (double_type_node))
2590 : : != CODE_FOR_nothing)
2591 : 620 : && !integer_zerop (fd->loop.n2))
2592 : : {
2593 : 236 : tree outer_n1 = fd->adjn1 ? fd->adjn1 : fd->loops[i - 1].n1;
2594 : 236 : tree itype = TREE_TYPE (fd->loops[i].v);
2595 : 236 : tree first_inner_iterations = fd->first_inner_iterations;
2596 : 236 : tree factor = fd->factor;
2597 : 236 : gcond *cond_stmt
2598 : 236 : = expand_omp_build_cond (gsi, NE_EXPR, factor,
2599 : 236 : build_zero_cst (TREE_TYPE (factor)),
2600 : : true);
2601 : 236 : edge e = split_block (gsi_bb (*gsi), cond_stmt);
2602 : 236 : basic_block bb0 = e->src;
2603 : 236 : e->flags = EDGE_TRUE_VALUE;
2604 : 236 : e->probability = profile_probability::likely ();
2605 : 236 : bb_triang_dom = bb0;
2606 : 236 : *gsi = gsi_after_labels (e->dest);
2607 : 236 : tree slltype = long_long_integer_type_node;
2608 : 236 : tree ulltype = long_long_unsigned_type_node;
2609 : 236 : tree stopvalull = fold_convert (ulltype, stopval);
2610 : 236 : stopvalull
2611 : 236 : = force_gimple_operand_gsi (gsi, stopvalull, true, NULL_TREE,
2612 : : false, GSI_CONTINUE_LINKING);
2613 : 236 : first_inner_iterations
2614 : 236 : = fold_convert (slltype, first_inner_iterations);
2615 : 236 : first_inner_iterations
2616 : 236 : = force_gimple_operand_gsi (gsi, first_inner_iterations, true,
2617 : : NULL_TREE, false,
2618 : : GSI_CONTINUE_LINKING);
2619 : 236 : factor = fold_convert (slltype, factor);
2620 : 236 : factor
2621 : 236 : = force_gimple_operand_gsi (gsi, factor, true, NULL_TREE,
2622 : : false, GSI_CONTINUE_LINKING);
2623 : 236 : tree first_inner_iterationsd
2624 : 236 : = fold_build1 (FLOAT_EXPR, double_type_node,
2625 : : first_inner_iterations);
2626 : 236 : first_inner_iterationsd
2627 : 236 : = force_gimple_operand_gsi (gsi, first_inner_iterationsd, true,
2628 : : NULL_TREE, false,
2629 : : GSI_CONTINUE_LINKING);
2630 : 236 : tree factord = fold_build1 (FLOAT_EXPR, double_type_node,
2631 : : factor);
2632 : 236 : factord = force_gimple_operand_gsi (gsi, factord, true,
2633 : : NULL_TREE, false,
2634 : : GSI_CONTINUE_LINKING);
2635 : 236 : tree stopvald = fold_build1 (FLOAT_EXPR, double_type_node,
2636 : : stopvalull);
2637 : 236 : stopvald = force_gimple_operand_gsi (gsi, stopvald, true,
2638 : : NULL_TREE, false,
2639 : : GSI_CONTINUE_LINKING);
2640 : : /* Temporarily disable flag_rounding_math, values will be
2641 : : decimal numbers divided by 2 and worst case imprecisions
2642 : : due to too large values ought to be caught later by the
2643 : : checks for fallback. */
2644 : 236 : int save_flag_rounding_math = flag_rounding_math;
2645 : 236 : flag_rounding_math = 0;
2646 : 236 : t = fold_build2 (RDIV_EXPR, double_type_node, factord,
2647 : : build_real (double_type_node, dconst2));
2648 : 236 : tree t3 = fold_build2 (MINUS_EXPR, double_type_node,
2649 : : first_inner_iterationsd, t);
2650 : 236 : t3 = force_gimple_operand_gsi (gsi, t3, true, NULL_TREE, false,
2651 : : GSI_CONTINUE_LINKING);
2652 : 236 : t = fold_build2 (MULT_EXPR, double_type_node, factord,
2653 : : build_real (double_type_node, dconst2));
2654 : 236 : t = fold_build2 (MULT_EXPR, double_type_node, t, stopvald);
2655 : 236 : t = fold_build2 (PLUS_EXPR, double_type_node, t,
2656 : : fold_build2 (MULT_EXPR, double_type_node,
2657 : : t3, t3));
2658 : 236 : flag_rounding_math = save_flag_rounding_math;
2659 : 236 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2660 : : GSI_CONTINUE_LINKING);
2661 : 236 : if (flag_exceptions
2662 : 34 : && cfun->can_throw_non_call_exceptions
2663 : 244 : && operation_could_trap_p (LT_EXPR, true, false, NULL_TREE))
2664 : : {
2665 : 8 : tree tem = fold_build2 (LT_EXPR, boolean_type_node, t,
2666 : : build_zero_cst (double_type_node));
2667 : 8 : tem = force_gimple_operand_gsi (gsi, tem, true, NULL_TREE,
2668 : : false, GSI_CONTINUE_LINKING);
2669 : 8 : cond_stmt = gimple_build_cond (NE_EXPR, tem,
2670 : : boolean_false_node,
2671 : : NULL_TREE, NULL_TREE);
2672 : : }
2673 : : else
2674 : 228 : cond_stmt
2675 : 228 : = gimple_build_cond (LT_EXPR, t,
2676 : : build_zero_cst (double_type_node),
2677 : : NULL_TREE, NULL_TREE);
2678 : 236 : gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2679 : 236 : e = split_block (gsi_bb (*gsi), cond_stmt);
2680 : 236 : basic_block bb1 = e->src;
2681 : 236 : e->flags = EDGE_FALSE_VALUE;
2682 : 236 : e->probability = profile_probability::very_likely ();
2683 : 236 : *gsi = gsi_after_labels (e->dest);
2684 : 236 : gcall *call = gimple_build_call_internal (IFN_SQRT, 1, t);
2685 : 236 : tree sqrtr = create_tmp_var (double_type_node);
2686 : 236 : gimple_call_set_lhs (call, sqrtr);
2687 : 236 : gsi_insert_after (gsi, call, GSI_CONTINUE_LINKING);
2688 : 236 : t = fold_build2 (MINUS_EXPR, double_type_node, sqrtr, t3);
2689 : 236 : t = fold_build2 (RDIV_EXPR, double_type_node, t, factord);
2690 : 236 : t = fold_build1 (FIX_TRUNC_EXPR, ulltype, t);
2691 : 236 : tree c = create_tmp_var (ulltype);
2692 : 236 : tree d = create_tmp_var (ulltype);
2693 : 236 : expand_omp_build_assign (gsi, c, t, true);
2694 : 236 : t = fold_build2 (MINUS_EXPR, ulltype, c,
2695 : : build_one_cst (ulltype));
2696 : 236 : t = fold_build2 (MULT_EXPR, ulltype, c, t);
2697 : 236 : t = fold_build2 (RSHIFT_EXPR, ulltype, t, integer_one_node);
2698 : 236 : t = fold_build2 (MULT_EXPR, ulltype,
2699 : : fold_convert (ulltype, fd->factor), t);
2700 : 236 : tree t2
2701 : 236 : = fold_build2 (MULT_EXPR, ulltype, c,
2702 : : fold_convert (ulltype,
2703 : : fd->first_inner_iterations));
2704 : 236 : t = fold_build2 (PLUS_EXPR, ulltype, t, t2);
2705 : 236 : expand_omp_build_assign (gsi, d, t, true);
2706 : 236 : t = fold_build2 (MULT_EXPR, ulltype,
2707 : : fold_convert (ulltype, fd->factor), c);
2708 : 236 : t = fold_build2 (PLUS_EXPR, ulltype,
2709 : : t, fold_convert (ulltype,
2710 : : fd->first_inner_iterations));
2711 : 236 : t2 = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2712 : : GSI_CONTINUE_LINKING);
2713 : 236 : cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, d,
2714 : : NULL_TREE, NULL_TREE);
2715 : 236 : gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2716 : 236 : e = split_block (gsi_bb (*gsi), cond_stmt);
2717 : 236 : basic_block bb2 = e->src;
2718 : 236 : e->flags = EDGE_TRUE_VALUE;
2719 : 236 : e->probability = profile_probability::very_likely ();
2720 : 236 : *gsi = gsi_after_labels (e->dest);
2721 : 236 : t = fold_build2 (PLUS_EXPR, ulltype, d, t2);
2722 : 236 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2723 : : GSI_CONTINUE_LINKING);
2724 : 236 : cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, t,
2725 : : NULL_TREE, NULL_TREE);
2726 : 236 : gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2727 : 236 : e = split_block (gsi_bb (*gsi), cond_stmt);
2728 : 236 : basic_block bb3 = e->src;
2729 : 236 : e->flags = EDGE_FALSE_VALUE;
2730 : 236 : e->probability = profile_probability::very_likely ();
2731 : 236 : *gsi = gsi_after_labels (e->dest);
2732 : 236 : t = fold_convert (itype, c);
2733 : 236 : t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i - 1].step);
2734 : 236 : t = fold_build2 (PLUS_EXPR, itype, outer_n1, t);
2735 : 236 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2736 : : GSI_CONTINUE_LINKING);
2737 : 236 : expand_omp_build_assign (gsi, fd->loops[i - 1].v, t, true);
2738 : 236 : t2 = fold_build2 (MINUS_EXPR, ulltype, stopvalull, d);
2739 : 236 : t2 = fold_convert (itype, t2);
2740 : 236 : t2 = fold_build2 (MULT_EXPR, itype, t2, fd->loops[i].step);
2741 : 236 : t2 = fold_build2 (PLUS_EXPR, itype, t2, fd->loops[i].n1);
2742 : 236 : if (fd->loops[i].m1)
2743 : : {
2744 : 195 : t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i].m1);
2745 : 195 : t2 = fold_build2 (PLUS_EXPR, itype, t2, t);
2746 : : }
2747 : 236 : expand_omp_build_assign (gsi, fd->loops[i].v, t2, true);
2748 : 236 : e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
2749 : 236 : bb_triang = e->src;
2750 : 236 : *gsi = gsi_after_labels (e->dest);
2751 : 236 : remove_edge (e);
2752 : 236 : e = make_edge (bb1, gsi_bb (*gsi), EDGE_TRUE_VALUE);
2753 : 236 : e->probability = profile_probability::very_unlikely ();
2754 : 236 : e = make_edge (bb2, gsi_bb (*gsi), EDGE_FALSE_VALUE);
2755 : 236 : e->probability = profile_probability::very_unlikely ();
2756 : 236 : e = make_edge (bb3, gsi_bb (*gsi), EDGE_TRUE_VALUE);
2757 : 236 : e->probability = profile_probability::very_unlikely ();
2758 : :
2759 : 236 : basic_block bb4 = create_empty_bb (bb0);
2760 : 236 : add_bb_to_loop (bb4, bb0->loop_father);
2761 : 236 : e = make_edge (bb0, bb4, EDGE_FALSE_VALUE);
2762 : 236 : e->probability = profile_probability::unlikely ();
2763 : 236 : make_edge (bb4, gsi_bb (*gsi), EDGE_FALLTHRU);
2764 : 236 : set_immediate_dominator (CDI_DOMINATORS, bb4, bb0);
2765 : 236 : set_immediate_dominator (CDI_DOMINATORS, gsi_bb (*gsi), bb0);
2766 : 236 : gimple_stmt_iterator gsi2 = gsi_after_labels (bb4);
2767 : 236 : t2 = fold_build2 (TRUNC_DIV_EXPR, type,
2768 : : counts[i], counts[i - 1]);
2769 : 236 : t2 = force_gimple_operand_gsi (&gsi2, t2, true, NULL_TREE, false,
2770 : : GSI_CONTINUE_LINKING);
2771 : 236 : t = fold_build2 (TRUNC_MOD_EXPR, type, stopval, t2);
2772 : 236 : t2 = fold_build2 (TRUNC_DIV_EXPR, type, stopval, t2);
2773 : 236 : t = fold_convert (itype, t);
2774 : 236 : t2 = fold_convert (itype, t2);
2775 : 236 : t = fold_build2 (MULT_EXPR, itype, t,
2776 : : fold_convert (itype, fd->loops[i].step));
2777 : 236 : t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
2778 : 236 : t2 = fold_build2 (MULT_EXPR, itype, t2,
2779 : : fold_convert (itype, fd->loops[i - 1].step));
2780 : 236 : t2 = fold_build2 (PLUS_EXPR, itype, fd->loops[i - 1].n1, t2);
2781 : 236 : t2 = force_gimple_operand_gsi (&gsi2, t2, false, NULL_TREE,
2782 : : false, GSI_CONTINUE_LINKING);
2783 : 236 : stmt = gimple_build_assign (fd->loops[i - 1].v, t2);
2784 : 236 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
2785 : 236 : if (fd->loops[i].m1)
2786 : : {
2787 : 195 : t2 = fold_build2 (MULT_EXPR, itype, fd->loops[i].m1,
2788 : : fd->loops[i - 1].v);
2789 : 195 : t = fold_build2 (PLUS_EXPR, itype, t, t2);
2790 : : }
2791 : 236 : t = force_gimple_operand_gsi (&gsi2, t, false, NULL_TREE,
2792 : : false, GSI_CONTINUE_LINKING);
2793 : 236 : stmt = gimple_build_assign (fd->loops[i].v, t);
2794 : 236 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
2795 : : }
2796 : : /* Fallback implementation. Evaluate the loops in between
2797 : : (inclusive) fd->first_nonrect and fd->last_nonrect at
2798 : : runtime unsing temporaries instead of the original iteration
2799 : : variables, in the body just bump the counter and compare
2800 : : with the desired value. */
2801 : 376 : gimple_stmt_iterator gsi2 = *gsi;
2802 : 376 : basic_block entry_bb = gsi_bb (gsi2);
2803 : 376 : edge e = split_block (entry_bb, gsi_stmt (gsi2));
2804 : 376 : e = split_block (e->dest, (gimple *) NULL);
2805 : 376 : basic_block dom_bb = NULL;
2806 : 376 : basic_block cur_bb = e->src;
2807 : 376 : basic_block next_bb = e->dest;
2808 : 376 : entry_bb = e->dest;
2809 : 376 : *gsi = gsi_after_labels (entry_bb);
2810 : :
2811 : 376 : tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
2812 : 376 : tree n1 = NULL_TREE, n2 = NULL_TREE;
2813 : 376 : memset (vs, 0, fd->last_nonrect * sizeof (tree));
2814 : :
2815 : 876 : for (int j = fd->first_nonrect; j <= fd->last_nonrect; j++)
2816 : : {
2817 : 876 : tree itype = TREE_TYPE (fd->loops[j].v);
2818 : 1752 : bool rect_p = (fd->loops[j].m1 == NULL_TREE
2819 : 527 : && fd->loops[j].m2 == NULL_TREE
2820 : 1345 : && !fd->loops[j].non_rect_referenced);
2821 : 876 : gsi2 = gsi_after_labels (cur_bb);
2822 : 876 : t = fold_convert (itype, unshare_expr (fd->loops[j].n1));
2823 : 876 : if (fd->loops[j].m1 == NULL_TREE)
2824 : 527 : n1 = rect_p ? build_zero_cst (type) : t;
2825 : 349 : else if (POINTER_TYPE_P (itype))
2826 : : {
2827 : 30 : gcc_assert (integer_onep (fd->loops[j].m1));
2828 : 30 : t = unshare_expr (fd->loops[j].n1);
2829 : 30 : n1 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
2830 : : }
2831 : : else
2832 : : {
2833 : 319 : n1 = fold_convert (itype, unshare_expr (fd->loops[j].m1));
2834 : 319 : n1 = fold_build2 (MULT_EXPR, itype,
2835 : : vs[j - fd->loops[j].outer], n1);
2836 : 319 : n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2837 : : }
2838 : 876 : n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2839 : : true, GSI_SAME_STMT);
2840 : 876 : if (j < fd->last_nonrect)
2841 : : {
2842 : 907 : vs[j] = create_tmp_reg (rect_p ? type : itype, ".it");
2843 : 500 : expand_omp_build_assign (&gsi2, vs[j], n1);
2844 : : }
2845 : 876 : t = fold_convert (itype, unshare_expr (fd->loops[j].n2));
2846 : 876 : if (fd->loops[j].m2 == NULL_TREE)
2847 : 646 : n2 = rect_p ? counts[j] : t;
2848 : 230 : else if (POINTER_TYPE_P (itype))
2849 : : {
2850 : 34 : gcc_assert (integer_onep (fd->loops[j].m2));
2851 : 34 : t = unshare_expr (fd->loops[j].n2);
2852 : 34 : n2 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
2853 : : }
2854 : : else
2855 : : {
2856 : 196 : n2 = fold_convert (itype, unshare_expr (fd->loops[j].m2));
2857 : 196 : n2 = fold_build2 (MULT_EXPR, itype,
2858 : : vs[j - fd->loops[j].outer], n2);
2859 : 196 : n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2860 : : }
2861 : 876 : n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2862 : : true, GSI_SAME_STMT);
2863 : 876 : if (POINTER_TYPE_P (itype))
2864 : 74 : itype = signed_type_for (itype);
2865 : 876 : if (j == fd->last_nonrect)
2866 : : {
2867 : 376 : gcond *cond_stmt
2868 : 376 : = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2869 : : n1, n2);
2870 : 376 : e = split_block (cur_bb, cond_stmt);
2871 : 376 : e->flags = EDGE_TRUE_VALUE;
2872 : 376 : edge ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
2873 : 376 : e->probability = profile_probability::likely ().guessed ();
2874 : 376 : ne->probability = e->probability.invert ();
2875 : 376 : gsi2 = gsi_after_labels (e->dest);
2876 : :
2877 : 376 : t = build_int_cst (itype, (fd->loops[j].cond_code == LT_EXPR
2878 : 428 : ? -1 : 1));
2879 : 376 : t = fold_build2 (PLUS_EXPR, itype,
2880 : : fold_convert (itype, fd->loops[j].step), t);
2881 : 376 : t = fold_build2 (PLUS_EXPR, itype, t,
2882 : : fold_convert (itype, n2));
2883 : 376 : t = fold_build2 (MINUS_EXPR, itype, t,
2884 : : fold_convert (itype, n1));
2885 : 376 : tree step = fold_convert (itype, fd->loops[j].step);
2886 : 376 : if (TYPE_UNSIGNED (itype)
2887 : 376 : && fd->loops[j].cond_code == GT_EXPR)
2888 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
2889 : : fold_build1 (NEGATE_EXPR, itype, t),
2890 : : fold_build1 (NEGATE_EXPR, itype, step));
2891 : : else
2892 : 376 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2893 : 376 : t = fold_convert (type, t);
2894 : 376 : t = fold_build2 (PLUS_EXPR, type, idx, t);
2895 : 376 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2896 : : true, GSI_SAME_STMT);
2897 : 376 : e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
2898 : 376 : set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
2899 : 376 : cond_stmt
2900 : 376 : = gimple_build_cond (LE_EXPR, t, stopval, NULL_TREE,
2901 : : NULL_TREE);
2902 : 376 : gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
2903 : 376 : e = split_block (gsi_bb (gsi2), cond_stmt);
2904 : 376 : e->flags = EDGE_TRUE_VALUE;
2905 : 376 : e->probability = profile_probability::likely ().guessed ();
2906 : 376 : ne = make_edge (e->src, entry_bb, EDGE_FALSE_VALUE);
2907 : 376 : ne->probability = e->probability.invert ();
2908 : 376 : gsi2 = gsi_after_labels (e->dest);
2909 : 376 : expand_omp_build_assign (&gsi2, idx, t);
2910 : 376 : set_immediate_dominator (CDI_DOMINATORS, entry_bb, dom_bb);
2911 : 376 : break;
2912 : : }
2913 : 500 : e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
2914 : :
2915 : 500 : basic_block new_cur_bb = create_empty_bb (cur_bb);
2916 : 500 : add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
2917 : :
2918 : 500 : gsi2 = gsi_after_labels (e->dest);
2919 : 500 : if (rect_p)
2920 : 93 : t = fold_build2 (PLUS_EXPR, type, vs[j],
2921 : : build_one_cst (type));
2922 : : else
2923 : : {
2924 : 407 : tree step
2925 : 407 : = fold_convert (itype, unshare_expr (fd->loops[j].step));
2926 : 407 : if (POINTER_TYPE_P (vtype))
2927 : 38 : t = fold_build_pointer_plus (vs[j], step);
2928 : : else
2929 : 369 : t = fold_build2 (PLUS_EXPR, itype, vs[j], step);
2930 : : }
2931 : 500 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2932 : : true, GSI_SAME_STMT);
2933 : 500 : expand_omp_build_assign (&gsi2, vs[j], t);
2934 : :
2935 : 500 : edge ne = split_block (e->dest, last_nondebug_stmt (e->dest));
2936 : 500 : gsi2 = gsi_after_labels (ne->dest);
2937 : :
2938 : 500 : gcond *cond_stmt;
2939 : 500 : if (next_bb == entry_bb)
2940 : : /* No need to actually check the outermost condition. */
2941 : 376 : cond_stmt
2942 : 376 : = gimple_build_cond (EQ_EXPR, boolean_true_node,
2943 : : boolean_true_node,
2944 : : NULL_TREE, NULL_TREE);
2945 : : else
2946 : 124 : cond_stmt
2947 : 155 : = gimple_build_cond (rect_p ? LT_EXPR
2948 : 31 : : fd->loops[j].cond_code,
2949 : : vs[j], n2, NULL_TREE, NULL_TREE);
2950 : 500 : gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
2951 : 500 : edge e3, e4;
2952 : 500 : if (next_bb == entry_bb)
2953 : : {
2954 : 376 : e3 = find_edge (ne->dest, next_bb);
2955 : 376 : e3->flags = EDGE_FALSE_VALUE;
2956 : 376 : dom_bb = ne->dest;
2957 : : }
2958 : : else
2959 : 124 : e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
2960 : 500 : e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
2961 : 500 : e4->probability = profile_probability::likely ().guessed ();
2962 : 500 : e3->probability = e4->probability.invert ();
2963 : 500 : basic_block esrc = e->src;
2964 : 500 : make_edge (e->src, ne->dest, EDGE_FALLTHRU);
2965 : 500 : cur_bb = new_cur_bb;
2966 : 500 : basic_block latch_bb = next_bb;
2967 : 500 : next_bb = e->dest;
2968 : 500 : remove_edge (e);
2969 : 500 : set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
2970 : 500 : set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
2971 : 500 : set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
2972 : : }
2973 : 1252 : for (int j = fd->last_nonrect; j >= fd->first_nonrect; j--)
2974 : : {
2975 : 876 : tree vtype = TREE_TYPE (fd->loops[j].v);
2976 : 876 : tree itype = vtype;
2977 : 876 : if (POINTER_TYPE_P (itype))
2978 : 74 : itype = signed_type_for (itype);
2979 : 1752 : bool rect_p = (fd->loops[j].m1 == NULL_TREE
2980 : 527 : && fd->loops[j].m2 == NULL_TREE
2981 : 1345 : && !fd->loops[j].non_rect_referenced);
2982 : 876 : if (j == fd->last_nonrect)
2983 : : {
2984 : 376 : t = fold_build2 (MINUS_EXPR, type, stopval, idx);
2985 : 376 : t = fold_convert (itype, t);
2986 : 376 : tree t2
2987 : 376 : = fold_convert (itype, unshare_expr (fd->loops[j].step));
2988 : 376 : t = fold_build2 (MULT_EXPR, itype, t, t2);
2989 : 376 : if (POINTER_TYPE_P (vtype))
2990 : 32 : t = fold_build_pointer_plus (n1, t);
2991 : : else
2992 : 344 : t = fold_build2 (PLUS_EXPR, itype, n1, t);
2993 : : }
2994 : 500 : else if (rect_p)
2995 : : {
2996 : 93 : t = fold_convert (itype, vs[j]);
2997 : 93 : t = fold_build2 (MULT_EXPR, itype, t,
2998 : : fold_convert (itype, fd->loops[j].step));
2999 : 93 : if (POINTER_TYPE_P (vtype))
3000 : 4 : t = fold_build_pointer_plus (fd->loops[j].n1, t);
3001 : : else
3002 : 89 : t = fold_build2 (PLUS_EXPR, itype, fd->loops[j].n1, t);
3003 : : }
3004 : : else
3005 : 407 : t = vs[j];
3006 : 876 : t = force_gimple_operand_gsi (gsi, t, false,
3007 : : NULL_TREE, true,
3008 : : GSI_SAME_STMT);
3009 : 876 : stmt = gimple_build_assign (fd->loops[j].v, t);
3010 : 876 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3011 : : }
3012 : 376 : if (gsi_end_p (*gsi))
3013 : 752 : *gsi = gsi_last_bb (gsi_bb (*gsi));
3014 : : else
3015 : 0 : gsi_prev (gsi);
3016 : 376 : if (bb_triang)
3017 : : {
3018 : 236 : e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
3019 : 236 : make_edge (bb_triang, e->dest, EDGE_FALLTHRU);
3020 : 236 : *gsi = gsi_after_labels (e->dest);
3021 : 236 : if (!gsi_end_p (*gsi))
3022 : 0 : gsi_insert_before (gsi, gimple_build_nop (), GSI_NEW_STMT);
3023 : 236 : set_immediate_dominator (CDI_DOMINATORS, e->dest, bb_triang_dom);
3024 : : }
3025 : : }
3026 : : else
3027 : : {
3028 : 14269 : t = fold_convert (itype, t);
3029 : 14269 : t = fold_build2 (MULT_EXPR, itype, t,
3030 : : fold_convert (itype, fd->loops[i].step));
3031 : 14269 : if (POINTER_TYPE_P (vtype))
3032 : 1617 : t = fold_build_pointer_plus (fd->loops[i].n1, t);
3033 : : else
3034 : 12652 : t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
3035 : 14269 : t = force_gimple_operand_gsi (gsi, t,
3036 : 14269 : DECL_P (fd->loops[i].v)
3037 : 14269 : && TREE_ADDRESSABLE (fd->loops[i].v),
3038 : : NULL_TREE, false,
3039 : : GSI_CONTINUE_LINKING);
3040 : 14269 : stmt = gimple_build_assign (fd->loops[i].v, t);
3041 : 14269 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3042 : : }
3043 : 14645 : if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
3044 : : {
3045 : 9264 : t = fold_build2 (TRUNC_DIV_EXPR, type, tem, counts[i]);
3046 : 9264 : t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
3047 : : false, GSI_CONTINUE_LINKING);
3048 : 9264 : stmt = gimple_build_assign (tem, t);
3049 : 9264 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3050 : : }
3051 : 14645 : if (i == fd->last_nonrect)
3052 : 376 : i = fd->first_nonrect;
3053 : : }
3054 : 5381 : if (fd->non_rect)
3055 : 1351 : for (i = 0; i <= fd->last_nonrect; i++)
3056 : 975 : if (fd->loops[i].m2)
3057 : : {
3058 : 230 : tree itype = TREE_TYPE (fd->loops[i].v);
3059 : :
3060 : 230 : tree t;
3061 : 230 : if (POINTER_TYPE_P (itype))
3062 : : {
3063 : 34 : gcc_assert (integer_onep (fd->loops[i].m2));
3064 : 34 : t = fold_build_pointer_plus (fd->loops[i - fd->loops[i].outer].v,
3065 : : unshare_expr (fd->loops[i].n2));
3066 : : }
3067 : : else
3068 : : {
3069 : 196 : t = fold_convert (itype, unshare_expr (fd->loops[i].m2));
3070 : 196 : t = fold_build2 (MULT_EXPR, itype,
3071 : : fd->loops[i - fd->loops[i].outer].v, t);
3072 : 196 : t = fold_build2 (PLUS_EXPR, itype, t,
3073 : : fold_convert (itype,
3074 : : unshare_expr (fd->loops[i].n2)));
3075 : : }
3076 : 230 : nonrect_bounds[i] = create_tmp_reg (itype, ".bound");
3077 : 230 : t = force_gimple_operand_gsi (gsi, t, false,
3078 : : NULL_TREE, false,
3079 : : GSI_CONTINUE_LINKING);
3080 : 230 : stmt = gimple_build_assign (nonrect_bounds[i], t);
3081 : 230 : gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3082 : : }
3083 : : }
3084 : :
3085 : : /* Helper function for expand_omp_for_*. Generate code like:
3086 : : L10:
3087 : : V3 += STEP3;
3088 : : if (V3 cond3 N32) goto BODY_BB; else goto L11;
3089 : : L11:
3090 : : V3 = N31;
3091 : : V2 += STEP2;
3092 : : if (V2 cond2 N22) goto BODY_BB; else goto L12;
3093 : : L12:
3094 : : V2 = N21;
3095 : : V1 += STEP1;
3096 : : goto BODY_BB;
3097 : : For non-rectangular loops, use temporaries stored in nonrect_bounds
3098 : : for the upper bounds if M?2 multiplier is present. Given e.g.
3099 : : for (V1 = N11; V1 cond1 N12; V1 += STEP1)
3100 : : for (V2 = N21; V2 cond2 N22; V2 += STEP2)
3101 : : for (V3 = N31; V3 cond3 N32; V3 += STEP3)
3102 : : for (V4 = N41 + M41 * V2; V4 cond4 N42 + M42 * V2; V4 += STEP4)
3103 : : do:
3104 : : L10:
3105 : : V4 += STEP4;
3106 : : if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L11;
3107 : : L11:
3108 : : V4 = N41 + M41 * V2; // This can be left out if the loop
3109 : : // refers to the immediate parent loop
3110 : : V3 += STEP3;
3111 : : if (V3 cond3 N32) goto BODY_BB; else goto L12;
3112 : : L12:
3113 : : V3 = N31;
3114 : : V2 += STEP2;
3115 : : if (V2 cond2 N22) goto L120; else goto L13;
3116 : : L120:
3117 : : V4 = N41 + M41 * V2;
3118 : : NONRECT_BOUND4 = N42 + M42 * V2;
3119 : : if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L12;
3120 : : L13:
3121 : : V2 = N21;
3122 : : V1 += STEP1;
3123 : : goto L120; */
3124 : :
3125 : : static basic_block
3126 : 2582 : extract_omp_for_update_vars (struct omp_for_data *fd, tree *nonrect_bounds,
3127 : : basic_block cont_bb, basic_block body_bb)
3128 : : {
3129 : 2582 : basic_block last_bb, bb, collapse_bb = NULL;
3130 : 2582 : int i;
3131 : 2582 : gimple_stmt_iterator gsi;
3132 : 2582 : edge e;
3133 : 2582 : tree t;
3134 : 2582 : gimple *stmt;
3135 : :
3136 : 2582 : last_bb = cont_bb;
3137 : 9699 : for (i = fd->collapse - 1; i >= 0; i--)
3138 : : {
3139 : 7117 : tree vtype = TREE_TYPE (fd->loops[i].v);
3140 : :
3141 : 7117 : bb = create_empty_bb (last_bb);
3142 : 7117 : add_bb_to_loop (bb, last_bb->loop_father);
3143 : 7117 : gsi = gsi_start_bb (bb);
3144 : :
3145 : 7117 : if (i < fd->collapse - 1)
3146 : : {
3147 : 4535 : e = make_edge (last_bb, bb, EDGE_FALSE_VALUE);
3148 : 4535 : e->probability = profile_probability::guessed_always () / 8;
3149 : :
3150 : 4535 : struct omp_for_data_loop *l = &fd->loops[i + 1];
3151 : 4535 : if (l->m1 == NULL_TREE || l->outer != 1)
3152 : : {
3153 : 4327 : t = l->n1;
3154 : 4327 : if (l->m1)
3155 : : {
3156 : 56 : if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3157 : 3 : t = fold_build_pointer_plus (fd->loops[i + 1 - l->outer].v,
3158 : : t);
3159 : : else
3160 : : {
3161 : 53 : tree t2
3162 : 53 : = fold_build2 (MULT_EXPR, TREE_TYPE (t),
3163 : : fd->loops[i + 1 - l->outer].v, l->m1);
3164 : 53 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t2, t);
3165 : : }
3166 : : }
3167 : 4327 : t = force_gimple_operand_gsi (&gsi, t,
3168 : 4327 : DECL_P (l->v)
3169 : 4327 : && TREE_ADDRESSABLE (l->v),
3170 : : NULL_TREE, false,
3171 : : GSI_CONTINUE_LINKING);
3172 : 4327 : stmt = gimple_build_assign (l->v, t);
3173 : 4327 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3174 : : }
3175 : : }
3176 : : else
3177 : : collapse_bb = bb;
3178 : :
3179 : 7117 : set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
3180 : :
3181 : 7117 : if (POINTER_TYPE_P (vtype))
3182 : 902 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
3183 : : else
3184 : 6215 : t = fold_build2 (PLUS_EXPR, vtype, fd->loops[i].v, fd->loops[i].step);
3185 : 7117 : t = force_gimple_operand_gsi (&gsi, t,
3186 : 7117 : DECL_P (fd->loops[i].v)
3187 : 7117 : && TREE_ADDRESSABLE (fd->loops[i].v),
3188 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
3189 : 7117 : stmt = gimple_build_assign (fd->loops[i].v, t);
3190 : 7117 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3191 : :
3192 : 7117 : if (fd->loops[i].non_rect_referenced)
3193 : : {
3194 : 308 : basic_block update_bb = NULL, prev_bb = NULL;
3195 : 714 : for (int j = i + 1; j <= fd->last_nonrect; j++)
3196 : 406 : if (j - fd->loops[j].outer == i)
3197 : : {
3198 : 308 : tree n1, n2;
3199 : 308 : struct omp_for_data_loop *l = &fd->loops[j];
3200 : 308 : basic_block this_bb = create_empty_bb (last_bb);
3201 : 308 : add_bb_to_loop (this_bb, last_bb->loop_father);
3202 : 308 : gimple_stmt_iterator gsi2 = gsi_start_bb (this_bb);
3203 : 308 : if (prev_bb)
3204 : : {
3205 : 0 : e = make_edge (prev_bb, this_bb, EDGE_TRUE_VALUE);
3206 : 0 : e->probability
3207 : 0 : = profile_probability::guessed_always ().apply_scale (7,
3208 : : 8);
3209 : 0 : set_immediate_dominator (CDI_DOMINATORS, this_bb, prev_bb);
3210 : : }
3211 : 308 : if (l->m1)
3212 : : {
3213 : 264 : if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3214 : 26 : t = fold_build_pointer_plus (fd->loops[i].v, l->n1);
3215 : : else
3216 : : {
3217 : 238 : t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m1), l->m1,
3218 : : fd->loops[i].v);
3219 : 238 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (l->v),
3220 : : t, l->n1);
3221 : : }
3222 : 264 : n1 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3223 : : false,
3224 : : GSI_CONTINUE_LINKING);
3225 : 264 : stmt = gimple_build_assign (l->v, n1);
3226 : 264 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
3227 : 264 : n1 = l->v;
3228 : : }
3229 : : else
3230 : 44 : n1 = force_gimple_operand_gsi (&gsi2, l->n1, true,
3231 : : NULL_TREE, false,
3232 : : GSI_CONTINUE_LINKING);
3233 : 308 : if (l->m2)
3234 : : {
3235 : 204 : if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3236 : 30 : t = fold_build_pointer_plus (fd->loops[i].v, l->n2);
3237 : : else
3238 : : {
3239 : 174 : t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m2), l->m2,
3240 : : fd->loops[i].v);
3241 : 174 : t = fold_build2 (PLUS_EXPR,
3242 : : TREE_TYPE (nonrect_bounds[j]),
3243 : : t, unshare_expr (l->n2));
3244 : : }
3245 : 204 : n2 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3246 : : false,
3247 : : GSI_CONTINUE_LINKING);
3248 : 204 : stmt = gimple_build_assign (nonrect_bounds[j], n2);
3249 : 204 : gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
3250 : 204 : n2 = nonrect_bounds[j];
3251 : : }
3252 : : else
3253 : 104 : n2 = force_gimple_operand_gsi (&gsi2, unshare_expr (l->n2),
3254 : : true, NULL_TREE, false,
3255 : : GSI_CONTINUE_LINKING);
3256 : 308 : gcond *cond_stmt
3257 : 308 : = gimple_build_cond (l->cond_code, n1, n2,
3258 : : NULL_TREE, NULL_TREE);
3259 : 308 : gsi_insert_after (&gsi2, cond_stmt, GSI_CONTINUE_LINKING);
3260 : 308 : if (update_bb == NULL)
3261 : 308 : update_bb = this_bb;
3262 : 308 : e = make_edge (this_bb, bb, EDGE_FALSE_VALUE);
3263 : 308 : e->probability = profile_probability::guessed_always () / 8;
3264 : 308 : if (prev_bb == NULL)
3265 : 308 : set_immediate_dominator (CDI_DOMINATORS, this_bb, bb);
3266 : 308 : prev_bb = this_bb;
3267 : : }
3268 : 308 : e = make_edge (prev_bb, body_bb, EDGE_TRUE_VALUE);
3269 : 308 : e->probability
3270 : 308 : = profile_probability::guessed_always ().apply_scale (7, 8);
3271 : 308 : body_bb = update_bb;
3272 : : }
3273 : :
3274 : 7117 : if (i > 0)
3275 : : {
3276 : 4535 : if (fd->loops[i].m2)
3277 : 204 : t = nonrect_bounds[i];
3278 : : else
3279 : 4331 : t = unshare_expr (fd->loops[i].n2);
3280 : 4535 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
3281 : : false, GSI_CONTINUE_LINKING);
3282 : 4535 : tree v = fd->loops[i].v;
3283 : 4535 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
3284 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
3285 : : false, GSI_CONTINUE_LINKING);
3286 : 4535 : t = fold_build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
3287 : 4535 : stmt = gimple_build_cond_empty (t);
3288 : 4535 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3289 : 4535 : if (walk_tree (gimple_cond_lhs_ptr (as_a <gcond *> (stmt)),
3290 : : expand_omp_regimplify_p, NULL, NULL)
3291 : 4535 : || walk_tree (gimple_cond_rhs_ptr (as_a <gcond *> (stmt)),
3292 : : expand_omp_regimplify_p, NULL, NULL))
3293 : 4 : gimple_regimplify_operands (stmt, &gsi);
3294 : 4535 : e = make_edge (bb, body_bb, EDGE_TRUE_VALUE);
3295 : 4535 : e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
3296 : : }
3297 : : else
3298 : 2582 : make_edge (bb, body_bb, EDGE_FALLTHRU);
3299 : 7117 : set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
3300 : 7117 : last_bb = bb;
3301 : : }
3302 : :
3303 : 2582 : return collapse_bb;
3304 : : }
3305 : :
3306 : : /* Expand #pragma omp ordered depend(source). */
3307 : :
3308 : : static void
3309 : 331 : expand_omp_ordered_source (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
3310 : : tree *counts, location_t loc)
3311 : : {
3312 : 662 : enum built_in_function source_ix
3313 : 331 : = fd->iter_type == long_integer_type_node
3314 : 331 : ? BUILT_IN_GOMP_DOACROSS_POST : BUILT_IN_GOMP_DOACROSS_ULL_POST;
3315 : 331 : gimple *g
3316 : 331 : = gimple_build_call (builtin_decl_explicit (source_ix), 1,
3317 : 331 : build_fold_addr_expr (counts[fd->ordered]));
3318 : 331 : gimple_set_location (g, loc);
3319 : 331 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3320 : 331 : }
3321 : :
3322 : : /* Expand a single depend from #pragma omp ordered depend(sink:...). */
3323 : :
3324 : : static void
3325 : 462 : expand_omp_ordered_sink (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
3326 : : tree *counts, tree c, location_t loc,
3327 : : basic_block cont_bb)
3328 : : {
3329 : 462 : auto_vec<tree, 10> args;
3330 : 924 : enum built_in_function sink_ix
3331 : 462 : = fd->iter_type == long_integer_type_node
3332 : 462 : ? BUILT_IN_GOMP_DOACROSS_WAIT : BUILT_IN_GOMP_DOACROSS_ULL_WAIT;
3333 : 462 : tree t, off, coff = NULL_TREE, deps = OMP_CLAUSE_DECL (c), cond = NULL_TREE;
3334 : 462 : int i;
3335 : 462 : gimple_stmt_iterator gsi2 = *gsi;
3336 : 462 : bool warned_step = false;
3337 : :
3338 : 462 : if (deps == NULL)
3339 : : {
3340 : : /* Handle doacross(sink: omp_cur_iteration - 1). */
3341 : 52 : gsi_prev (&gsi2);
3342 : 52 : edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
3343 : 52 : edge e2 = split_block_after_labels (e1->dest);
3344 : 52 : gsi2 = gsi_after_labels (e1->dest);
3345 : 52 : *gsi = gsi_last_bb (e1->src);
3346 : 52 : gimple_stmt_iterator gsi3 = *gsi;
3347 : :
3348 : 52 : if (counts[fd->collapse - 1])
3349 : : {
3350 : 8 : gcc_assert (fd->collapse == 1);
3351 : 8 : t = counts[fd->collapse - 1];
3352 : : }
3353 : 44 : else if (fd->collapse > 1)
3354 : 28 : t = fd->loop.v;
3355 : : else
3356 : : {
3357 : 16 : t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
3358 : : fd->loops[0].v, fd->loops[0].n1);
3359 : 16 : t = fold_convert (fd->iter_type, t);
3360 : : }
3361 : :
3362 : 52 : t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
3363 : : false, GSI_CONTINUE_LINKING);
3364 : 52 : gsi_insert_after (gsi, gimple_build_cond (NE_EXPR, t,
3365 : 52 : build_zero_cst (TREE_TYPE (t)),
3366 : : NULL_TREE, NULL_TREE),
3367 : : GSI_NEW_STMT);
3368 : :
3369 : 52 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
3370 : : build_minus_one_cst (TREE_TYPE (t)));
3371 : 52 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3372 : : true, GSI_SAME_STMT);
3373 : 52 : args.safe_push (t);
3374 : 281 : for (i = fd->collapse; i < fd->ordered; i++)
3375 : : {
3376 : 229 : t = counts[fd->ordered + 2 + (i - fd->collapse)];
3377 : 229 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
3378 : : build_minus_one_cst (TREE_TYPE (t)));
3379 : 229 : t = fold_convert (fd->iter_type, t);
3380 : 229 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3381 : : true, GSI_SAME_STMT);
3382 : 229 : args.safe_push (t);
3383 : : }
3384 : :
3385 : 52 : gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix),
3386 : : args);
3387 : 52 : gimple_set_location (g, loc);
3388 : 52 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
3389 : :
3390 : 52 : edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
3391 : 52 : e3->probability = profile_probability::guessed_always () / 8;
3392 : 52 : e1->probability = e3->probability.invert ();
3393 : 52 : e1->flags = EDGE_TRUE_VALUE;
3394 : 52 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
3395 : :
3396 : 52 : if (fd->ordered > fd->collapse && cont_bb)
3397 : : {
3398 : 33 : if (counts[fd->ordered + 1] == NULL_TREE)
3399 : 33 : counts[fd->ordered + 1]
3400 : 33 : = create_tmp_var (boolean_type_node, ".first");
3401 : :
3402 : 33 : edge e4;
3403 : 33 : if (gsi_end_p (gsi3))
3404 : 25 : e4 = split_block_after_labels (e1->src);
3405 : : else
3406 : : {
3407 : 8 : gsi_prev (&gsi3);
3408 : 8 : e4 = split_block (gsi_bb (gsi3), gsi_stmt (gsi3));
3409 : : }
3410 : 33 : gsi3 = gsi_last_bb (e4->src);
3411 : :
3412 : 33 : gsi_insert_after (&gsi3,
3413 : 33 : gimple_build_cond (NE_EXPR,
3414 : 33 : counts[fd->ordered + 1],
3415 : : boolean_false_node,
3416 : : NULL_TREE, NULL_TREE),
3417 : : GSI_NEW_STMT);
3418 : :
3419 : 33 : edge e5 = make_edge (e4->src, e2->dest, EDGE_FALSE_VALUE);
3420 : 33 : e4->probability = profile_probability::guessed_always () / 8;
3421 : 33 : e5->probability = e4->probability.invert ();
3422 : 33 : e4->flags = EDGE_TRUE_VALUE;
3423 : 33 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e4->src);
3424 : : }
3425 : :
3426 : 52 : *gsi = gsi_after_labels (e2->dest);
3427 : 52 : return;
3428 : : }
3429 : 500 : for (i = 0; i < fd->ordered; i++)
3430 : : {
3431 : 498 : tree step = NULL_TREE;
3432 : 498 : off = TREE_PURPOSE (deps);
3433 : 498 : if (TREE_CODE (off) == TRUNC_DIV_EXPR)
3434 : : {
3435 : 83 : step = TREE_OPERAND (off, 1);
3436 : 83 : off = TREE_OPERAND (off, 0);
3437 : : }
3438 : 498 : if (!integer_zerop (off))
3439 : : {
3440 : 408 : gcc_assert (fd->loops[i].cond_code == LT_EXPR
3441 : : || fd->loops[i].cond_code == GT_EXPR);
3442 : 408 : bool forward = fd->loops[i].cond_code == LT_EXPR;
3443 : 408 : if (step)
3444 : : {
3445 : : /* Non-simple Fortran DO loops. If step is variable,
3446 : : we don't know at compile even the direction, so can't
3447 : : warn. */
3448 : 83 : if (TREE_CODE (step) != INTEGER_CST)
3449 : : break;
3450 : 0 : forward = tree_int_cst_sgn (step) != -1;
3451 : : }
3452 : 325 : if (forward ^ OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3453 : 17 : warning_at (loc, OPT_Wopenmp,
3454 : : "%qs clause with %<sink%> modifier "
3455 : : "waiting for lexically later iteration",
3456 : 17 : OMP_CLAUSE_DOACROSS_DEPEND (c)
3457 : : ? "depend" : "doacross");
3458 : : break;
3459 : : }
3460 : 90 : deps = TREE_CHAIN (deps);
3461 : : }
3462 : : /* If all offsets corresponding to the collapsed loops are zero,
3463 : : this depend clause can be ignored. FIXME: but there is still a
3464 : : flush needed. We need to emit one __sync_synchronize () for it
3465 : : though (perhaps conditionally)? Solve this together with the
3466 : : conservative dependence folding optimization.
3467 : : if (i >= fd->collapse)
3468 : : return; */
3469 : :
3470 : 410 : deps = OMP_CLAUSE_DECL (c);
3471 : 410 : gsi_prev (&gsi2);
3472 : 410 : edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
3473 : 410 : edge e2 = split_block_after_labels (e1->dest);
3474 : :
3475 : 410 : gsi2 = gsi_after_labels (e1->dest);
3476 : 410 : *gsi = gsi_last_bb (e1->src);
3477 : 2798 : for (i = 0; i < fd->ordered; i++)
3478 : : {
3479 : 2388 : tree itype = TREE_TYPE (fd->loops[i].v);
3480 : 2388 : tree step = NULL_TREE;
3481 : 2388 : tree orig_off = NULL_TREE;
3482 : 2388 : if (POINTER_TYPE_P (itype))
3483 : 9 : itype = sizetype;
3484 : 2388 : if (i)
3485 : 1978 : deps = TREE_CHAIN (deps);
3486 : 2388 : off = TREE_PURPOSE (deps);
3487 : 2388 : if (TREE_CODE (off) == TRUNC_DIV_EXPR)
3488 : : {
3489 : 204 : step = TREE_OPERAND (off, 1);
3490 : 204 : off = TREE_OPERAND (off, 0);
3491 : 204 : gcc_assert (fd->loops[i].cond_code == LT_EXPR
3492 : : && integer_onep (fd->loops[i].step)
3493 : : && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)));
3494 : : }
3495 : 2388 : tree s = fold_convert_loc (loc, itype, step ? step : fd->loops[i].step);
3496 : 2388 : if (step)
3497 : : {
3498 : 204 : off = fold_convert_loc (loc, itype, off);
3499 : 204 : orig_off = off;
3500 : 204 : off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
3501 : : }
3502 : :
3503 : 2388 : if (integer_zerop (off))
3504 : 1516 : t = boolean_true_node;
3505 : : else
3506 : : {
3507 : 872 : tree a;
3508 : 872 : tree co = fold_convert_loc (loc, itype, off);
3509 : 872 : if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
3510 : : {
3511 : 9 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3512 : 0 : co = fold_build1_loc (loc, NEGATE_EXPR, itype, co);
3513 : 9 : a = fold_build2_loc (loc, POINTER_PLUS_EXPR,
3514 : 9 : TREE_TYPE (fd->loops[i].v), fd->loops[i].v,
3515 : : co);
3516 : : }
3517 : 863 : else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3518 : 636 : a = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3519 : : fd->loops[i].v, co);
3520 : : else
3521 : 227 : a = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
3522 : : fd->loops[i].v, co);
3523 : 872 : if (step)
3524 : : {
3525 : 204 : tree t1, t2;
3526 : 204 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3527 : 149 : t1 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3528 : 149 : fd->loops[i].n1);
3529 : : else
3530 : 55 : t1 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3531 : 55 : fd->loops[i].n2);
3532 : 204 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3533 : 149 : t2 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3534 : 149 : fd->loops[i].n2);
3535 : : else
3536 : 55 : t2 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3537 : 55 : fd->loops[i].n1);
3538 : 204 : t = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
3539 : 204 : step, build_int_cst (TREE_TYPE (step), 0));
3540 : 204 : if (TREE_CODE (step) != INTEGER_CST)
3541 : : {
3542 : 204 : t1 = unshare_expr (t1);
3543 : 204 : t1 = force_gimple_operand_gsi (gsi, t1, true, NULL_TREE,
3544 : : false, GSI_CONTINUE_LINKING);
3545 : 204 : t2 = unshare_expr (t2);
3546 : 204 : t2 = force_gimple_operand_gsi (gsi, t2, true, NULL_TREE,
3547 : : false, GSI_CONTINUE_LINKING);
3548 : : }
3549 : 204 : t = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
3550 : : t, t2, t1);
3551 : : }
3552 : 668 : else if (fd->loops[i].cond_code == LT_EXPR)
3553 : : {
3554 : 516 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3555 : 429 : t = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3556 : : fd->loops[i].n1);
3557 : : else
3558 : 87 : t = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3559 : : fd->loops[i].n2);
3560 : : }
3561 : 152 : else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3562 : 58 : t = fold_build2_loc (loc, GT_EXPR, boolean_type_node, a,
3563 : : fd->loops[i].n2);
3564 : : else
3565 : 94 : t = fold_build2_loc (loc, LE_EXPR, boolean_type_node, a,
3566 : : fd->loops[i].n1);
3567 : : }
3568 : 2388 : if (cond)
3569 : 1978 : cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node, cond, t);
3570 : : else
3571 : 410 : cond = t;
3572 : :
3573 : 2388 : off = fold_convert_loc (loc, itype, off);
3574 : :
3575 : 2388 : if (step
3576 : 4403 : || (fd->loops[i].cond_code == LT_EXPR
3577 : 2184 : ? !integer_onep (fd->loops[i].step)
3578 : 169 : : !integer_minus_onep (fd->loops[i].step)))
3579 : : {
3580 : 382 : if (step == NULL_TREE
3581 : 178 : && TYPE_UNSIGNED (itype)
3582 : 393 : && fd->loops[i].cond_code == GT_EXPR)
3583 : 9 : t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype, off,
3584 : : fold_build1_loc (loc, NEGATE_EXPR, itype,
3585 : : s));
3586 : : else
3587 : 542 : t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype,
3588 : : orig_off ? orig_off : off, s);
3589 : 382 : t = fold_build2_loc (loc, EQ_EXPR, boolean_type_node, t,
3590 : 382 : build_int_cst (itype, 0));
3591 : 382 : if (integer_zerop (t) && !warned_step)
3592 : : {
3593 : 5 : warning_at (loc, OPT_Wopenmp,
3594 : : "%qs clause with %<sink%> modifier refers to "
3595 : : "iteration never in the iteration space",
3596 : 5 : OMP_CLAUSE_DOACROSS_DEPEND (c)
3597 : : ? "depend" : "doacross");
3598 : 5 : warned_step = true;
3599 : : }
3600 : 382 : cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node,
3601 : : cond, t);
3602 : : }
3603 : :
3604 : 2388 : if (i <= fd->collapse - 1 && fd->collapse > 1)
3605 : 322 : t = fd->loop.v;
3606 : 2066 : else if (counts[i])
3607 : 166 : t = counts[i];
3608 : : else
3609 : : {
3610 : 1900 : t = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3611 : 1900 : fd->loops[i].v, fd->loops[i].n1);
3612 : 1900 : t = fold_convert_loc (loc, fd->iter_type, t);
3613 : : }
3614 : 2388 : if (step)
3615 : : /* We have divided off by step already earlier. */;
3616 : 2184 : else if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
3617 : 9 : off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off,
3618 : : fold_build1_loc (loc, NEGATE_EXPR, itype,
3619 : : s));
3620 : : else
3621 : 2175 : off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
3622 : 2388 : if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3623 : 636 : off = fold_build1_loc (loc, NEGATE_EXPR, itype, off);
3624 : 2388 : off = fold_convert_loc (loc, fd->iter_type, off);
3625 : 2388 : if (i <= fd->collapse - 1 && fd->collapse > 1)
3626 : : {
3627 : 322 : if (i)
3628 : 161 : off = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, coff,
3629 : : off);
3630 : 322 : if (i < fd->collapse - 1)
3631 : : {
3632 : 322 : coff = fold_build2_loc (loc, MULT_EXPR, fd->iter_type, off,
3633 : 161 : counts[i]);
3634 : 161 : continue;
3635 : : }
3636 : : }
3637 : 2227 : off = unshare_expr (off);
3638 : 2227 : t = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, t, off);
3639 : 2227 : t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3640 : : true, GSI_SAME_STMT);
3641 : 2227 : args.safe_push (t);
3642 : : }
3643 : 410 : gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix), args);
3644 : 410 : gimple_set_location (g, loc);
3645 : 410 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
3646 : :
3647 : 410 : cond = unshare_expr (cond);
3648 : 410 : cond = force_gimple_operand_gsi (gsi, cond, true, NULL_TREE, false,
3649 : : GSI_CONTINUE_LINKING);
3650 : 410 : gsi_insert_after (gsi, gimple_build_cond_empty (cond), GSI_NEW_STMT);
3651 : 410 : edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
3652 : 410 : e3->probability = profile_probability::guessed_always () / 8;
3653 : 410 : e1->probability = e3->probability.invert ();
3654 : 410 : e1->flags = EDGE_TRUE_VALUE;
3655 : 410 : set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
3656 : :
3657 : 410 : *gsi = gsi_after_labels (e2->dest);
3658 : 462 : }
3659 : :
3660 : : /* Expand all #pragma omp ordered depend(source) and
3661 : : #pragma omp ordered depend(sink:...) constructs in the current
3662 : : #pragma omp for ordered(n) region. */
3663 : :
3664 : : static void
3665 : 331 : expand_omp_ordered_source_sink (struct omp_region *region,
3666 : : struct omp_for_data *fd, tree *counts,
3667 : : basic_block cont_bb)
3668 : : {
3669 : 331 : struct omp_region *inner;
3670 : 331 : int i;
3671 : 1555 : for (i = fd->collapse - 1; i < fd->ordered; i++)
3672 : 1224 : if (i == fd->collapse - 1 && fd->collapse > 1)
3673 : 147 : counts[i] = NULL_TREE;
3674 : 1077 : else if (i >= fd->collapse && !cont_bb)
3675 : 0 : counts[i] = build_zero_cst (fd->iter_type);
3676 : 2146 : else if (!POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
3677 : 2146 : && integer_onep (fd->loops[i].step))
3678 : 983 : counts[i] = NULL_TREE;
3679 : : else
3680 : 94 : counts[i] = create_tmp_var (fd->iter_type, ".orditer");
3681 : 331 : tree atype
3682 : 331 : = build_array_type_nelts (fd->iter_type, fd->ordered - fd->collapse + 1);
3683 : 331 : counts[fd->ordered] = create_tmp_var (atype, ".orditera");
3684 : 331 : TREE_ADDRESSABLE (counts[fd->ordered]) = 1;
3685 : 331 : counts[fd->ordered + 1] = NULL_TREE;
3686 : :
3687 : 1603 : for (inner = region->inner; inner; inner = inner->next)
3688 : 1272 : if (inner->type == GIMPLE_OMP_ORDERED)
3689 : : {
3690 : 701 : gomp_ordered *ord_stmt = inner->ord_stmt;
3691 : 701 : gimple_stmt_iterator gsi = gsi_for_stmt (ord_stmt);
3692 : 701 : location_t loc = gimple_location (ord_stmt);
3693 : 701 : tree c;
3694 : 1163 : for (c = gimple_omp_ordered_clauses (ord_stmt);
3695 : 1163 : c; c = OMP_CLAUSE_CHAIN (c))
3696 : 793 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
3697 : : break;
3698 : 701 : if (c)
3699 : 331 : expand_omp_ordered_source (&gsi, fd, counts, loc);
3700 : 1494 : for (c = gimple_omp_ordered_clauses (ord_stmt);
3701 : 1494 : c; c = OMP_CLAUSE_CHAIN (c))
3702 : 793 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
3703 : 462 : expand_omp_ordered_sink (&gsi, fd, counts, c, loc, cont_bb);
3704 : 701 : gsi_remove (&gsi, true);
3705 : : }
3706 : 331 : }
3707 : :
3708 : : /* Wrap the body into fd->ordered - fd->collapse loops that aren't
3709 : : collapsed. */
3710 : :
3711 : : static basic_block
3712 : 331 : expand_omp_for_ordered_loops (struct omp_for_data *fd, tree *counts,
3713 : : basic_block cont_bb, basic_block body_bb,
3714 : : basic_block l0_bb, bool ordered_lastprivate)
3715 : : {
3716 : 331 : if (fd->ordered == fd->collapse)
3717 : : return cont_bb;
3718 : :
3719 : 171 : if (!cont_bb)
3720 : : {
3721 : 0 : gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3722 : 0 : for (int i = fd->collapse; i < fd->ordered; i++)
3723 : : {
3724 : 0 : tree type = TREE_TYPE (fd->loops[i].v);
3725 : 0 : tree n1 = fold_convert (type, fd->loops[i].n1);
3726 : 0 : expand_omp_build_assign (&gsi, fd->loops[i].v, n1);
3727 : 0 : tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3728 : 0 : size_int (i - fd->collapse + 1),
3729 : : NULL_TREE, NULL_TREE);
3730 : 0 : expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
3731 : : }
3732 : 0 : return NULL;
3733 : : }
3734 : :
3735 : 1064 : for (int i = fd->ordered - 1; i >= fd->collapse; i--)
3736 : : {
3737 : 893 : tree t, type = TREE_TYPE (fd->loops[i].v);
3738 : 893 : gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3739 : 893 : if (counts[fd->ordered + 1] && i == fd->collapse)
3740 : 33 : expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
3741 : : boolean_true_node);
3742 : 893 : expand_omp_build_assign (&gsi, fd->loops[i].v,
3743 : 893 : fold_convert (type, fd->loops[i].n1));
3744 : 893 : if (counts[i])
3745 : 57 : expand_omp_build_assign (&gsi, counts[i],
3746 : : build_zero_cst (fd->iter_type));
3747 : 893 : tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3748 : 893 : size_int (i - fd->collapse + 1),
3749 : : NULL_TREE, NULL_TREE);
3750 : 893 : expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
3751 : 893 : if (!gsi_end_p (gsi))
3752 : 893 : gsi_prev (&gsi);
3753 : : else
3754 : 0 : gsi = gsi_last_bb (body_bb);
3755 : 893 : edge e1 = split_block (body_bb, gsi_stmt (gsi));
3756 : 893 : basic_block new_body = e1->dest;
3757 : 893 : if (body_bb == cont_bb)
3758 : 0 : cont_bb = new_body;
3759 : 893 : edge e2 = NULL;
3760 : 893 : basic_block new_header;
3761 : 893 : if (EDGE_COUNT (cont_bb->preds) > 0)
3762 : : {
3763 : 864 : gsi = gsi_last_bb (cont_bb);
3764 : 864 : if (POINTER_TYPE_P (type))
3765 : 0 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
3766 : : else
3767 : 864 : t = fold_build2 (PLUS_EXPR, type, fd->loops[i].v,
3768 : : fold_convert (type, fd->loops[i].step));
3769 : 864 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
3770 : 864 : if (counts[i])
3771 : : {
3772 : 57 : t = fold_build2 (PLUS_EXPR, fd->iter_type, counts[i],
3773 : : build_int_cst (fd->iter_type, 1));
3774 : 57 : expand_omp_build_assign (&gsi, counts[i], t);
3775 : 57 : t = counts[i];
3776 : : }
3777 : : else
3778 : : {
3779 : 807 : t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3780 : : fd->loops[i].v, fd->loops[i].n1);
3781 : 807 : t = fold_convert (fd->iter_type, t);
3782 : 807 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
3783 : : true, GSI_SAME_STMT);
3784 : : }
3785 : 864 : aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3786 : 864 : size_int (i - fd->collapse + 1),
3787 : : NULL_TREE, NULL_TREE);
3788 : 864 : expand_omp_build_assign (&gsi, aref, t);
3789 : 864 : if (counts[fd->ordered + 1] && i == fd->ordered - 1)
3790 : 30 : expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
3791 : : boolean_false_node);
3792 : 864 : gsi_prev (&gsi);
3793 : 864 : e2 = split_block (cont_bb, gsi_stmt (gsi));
3794 : 864 : new_header = e2->dest;
3795 : : }
3796 : : else
3797 : : new_header = cont_bb;
3798 : 893 : gsi = gsi_after_labels (new_header);
3799 : 893 : tree v = force_gimple_operand_gsi (&gsi, fd->loops[i].v, true, NULL_TREE,
3800 : : true, GSI_SAME_STMT);
3801 : 893 : tree n2
3802 : 893 : = force_gimple_operand_gsi (&gsi, fold_convert (type, fd->loops[i].n2),
3803 : : true, NULL_TREE, true, GSI_SAME_STMT);
3804 : 893 : t = build2 (fd->loops[i].cond_code, boolean_type_node, v, n2);
3805 : 893 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_NEW_STMT);
3806 : 893 : edge e3 = split_block (new_header, gsi_stmt (gsi));
3807 : 893 : cont_bb = e3->dest;
3808 : 893 : remove_edge (e1);
3809 : 893 : make_edge (body_bb, new_header, EDGE_FALLTHRU);
3810 : 893 : e3->flags = EDGE_FALSE_VALUE;
3811 : 893 : e3->probability = profile_probability::guessed_always () / 8;
3812 : 893 : e1 = make_edge (new_header, new_body, EDGE_TRUE_VALUE);
3813 : 893 : e1->probability = e3->probability.invert ();
3814 : :
3815 : 893 : set_immediate_dominator (CDI_DOMINATORS, new_header, body_bb);
3816 : 893 : set_immediate_dominator (CDI_DOMINATORS, new_body, new_header);
3817 : :
3818 : 893 : if (e2)
3819 : : {
3820 : 864 : class loop *loop = alloc_loop ();
3821 : 864 : loop->header = new_header;
3822 : 864 : loop->latch = e2->src;
3823 : 864 : add_loop (loop, l0_bb->loop_father);
3824 : : }
3825 : : }
3826 : :
3827 : : /* If there are any lastprivate clauses and it is possible some loops
3828 : : might have zero iterations, ensure all the decls are initialized,
3829 : : otherwise we could crash evaluating C++ class iterators with lastprivate
3830 : : clauses. */
3831 : : bool need_inits = false;
3832 : 171 : for (int i = fd->collapse; ordered_lastprivate && i < fd->ordered; i++)
3833 : 0 : if (need_inits)
3834 : : {
3835 : 0 : tree type = TREE_TYPE (fd->loops[i].v);
3836 : 0 : gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3837 : 0 : expand_omp_build_assign (&gsi, fd->loops[i].v,
3838 : : fold_convert (type, fd->loops[i].n1));
3839 : : }
3840 : : else
3841 : : {
3842 : 0 : tree type = TREE_TYPE (fd->loops[i].v);
3843 : 0 : tree this_cond = fold_build2 (fd->loops[i].cond_code,
3844 : : boolean_type_node,
3845 : : fold_convert (type, fd->loops[i].n1),
3846 : : fold_convert (type, fd->loops[i].n2));
3847 : 0 : if (!integer_onep (this_cond))
3848 : 0 : need_inits = true;
3849 : : }
3850 : :
3851 : : return cont_bb;
3852 : : }
3853 : :
3854 : : /* A subroutine of expand_omp_for. Generate code for a parallel
3855 : : loop with any schedule. Given parameters:
3856 : :
3857 : : for (V = N1; V cond N2; V += STEP) BODY;
3858 : :
3859 : : where COND is "<" or ">", we generate pseudocode
3860 : :
3861 : : more = GOMP_loop_foo_start (N1, N2, STEP, CHUNK, &istart0, &iend0);
3862 : : if (more) goto L0; else goto L3;
3863 : : L0:
3864 : : V = istart0;
3865 : : iend = iend0;
3866 : : L1:
3867 : : BODY;
3868 : : V += STEP;
3869 : : if (V cond iend) goto L1; else goto L2;
3870 : : L2:
3871 : : if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
3872 : : L3:
3873 : :
3874 : : If this is a combined omp parallel loop, instead of the call to
3875 : : GOMP_loop_foo_start, we call GOMP_loop_foo_next.
3876 : : If this is gimple_omp_for_combined_p loop, then instead of assigning
3877 : : V and iend in L0 we assign the first two _looptemp_ clause decls of the
3878 : : inner GIMPLE_OMP_FOR and V += STEP; and
3879 : : if (V cond iend) goto L1; else goto L2; are removed.
3880 : :
3881 : : For collapsed loops, given parameters:
3882 : : collapse(3)
3883 : : for (V1 = N11; V1 cond1 N12; V1 += STEP1)
3884 : : for (V2 = N21; V2 cond2 N22; V2 += STEP2)
3885 : : for (V3 = N31; V3 cond3 N32; V3 += STEP3)
3886 : : BODY;
3887 : :
3888 : : we generate pseudocode
3889 : :
3890 : : if (__builtin_expect (N32 cond3 N31, 0)) goto Z0;
3891 : : if (cond3 is <)
3892 : : adj = STEP3 - 1;
3893 : : else
3894 : : adj = STEP3 + 1;
3895 : : count3 = (adj + N32 - N31) / STEP3;
3896 : : if (__builtin_expect (N22 cond2 N21, 0)) goto Z0;
3897 : : if (cond2 is <)
3898 : : adj = STEP2 - 1;
3899 : : else
3900 : : adj = STEP2 + 1;
3901 : : count2 = (adj + N22 - N21) / STEP2;
3902 : : if (__builtin_expect (N12 cond1 N11, 0)) goto Z0;
3903 : : if (cond1 is <)
3904 : : adj = STEP1 - 1;
3905 : : else
3906 : : adj = STEP1 + 1;
3907 : : count1 = (adj + N12 - N11) / STEP1;
3908 : : count = count1 * count2 * count3;
3909 : : goto Z1;
3910 : : Z0:
3911 : : count = 0;
3912 : : Z1:
3913 : : more = GOMP_loop_foo_start (0, count, 1, CHUNK, &istart0, &iend0);
3914 : : if (more) goto L0; else goto L3;
3915 : : L0:
3916 : : V = istart0;
3917 : : T = V;
3918 : : V3 = N31 + (T % count3) * STEP3;
3919 : : T = T / count3;
3920 : : V2 = N21 + (T % count2) * STEP2;
3921 : : T = T / count2;
3922 : : V1 = N11 + T * STEP1;
3923 : : iend = iend0;
3924 : : L1:
3925 : : BODY;
3926 : : V += 1;
3927 : : if (V < iend) goto L10; else goto L2;
3928 : : L10:
3929 : : V3 += STEP3;
3930 : : if (V3 cond3 N32) goto L1; else goto L11;
3931 : : L11:
3932 : : V3 = N31;
3933 : : V2 += STEP2;
3934 : : if (V2 cond2 N22) goto L1; else goto L12;
3935 : : L12:
3936 : : V2 = N21;
3937 : : V1 += STEP1;
3938 : : goto L1;
3939 : : L2:
3940 : : if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
3941 : : L3:
3942 : :
3943 : : */
3944 : :
3945 : : static void
3946 : 4129 : expand_omp_for_generic (struct omp_region *region,
3947 : : struct omp_for_data *fd,
3948 : : enum built_in_function start_fn,
3949 : : enum built_in_function next_fn,
3950 : : tree sched_arg,
3951 : : gimple *inner_stmt)
3952 : : {
3953 : 4129 : tree type, istart0, iend0, iend;
3954 : 4129 : tree t, vmain, vback, bias = NULL_TREE;
3955 : 4129 : basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, collapse_bb;
3956 : 4129 : basic_block l2_bb = NULL, l3_bb = NULL;
3957 : 4129 : gimple_stmt_iterator gsi;
3958 : 4129 : gassign *assign_stmt;
3959 : 4129 : bool in_combined_parallel = is_combined_parallel (region);
3960 : 4129 : bool broken_loop = region->cont == NULL;
3961 : 4129 : edge e, ne;
3962 : 4129 : tree *counts = NULL;
3963 : 4129 : int i;
3964 : 4129 : bool ordered_lastprivate = false;
3965 : 4129 : bool offload = is_in_offload_region (region);
3966 : :
3967 : 4129 : gcc_assert (!broken_loop || !in_combined_parallel);
3968 : 4129 : gcc_assert (fd->iter_type == long_integer_type_node
3969 : : || !in_combined_parallel);
3970 : :
3971 : 4129 : entry_bb = region->entry;
3972 : 4129 : cont_bb = region->cont;
3973 : 4129 : collapse_bb = NULL;
3974 : 4129 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
3975 : 4129 : gcc_assert (broken_loop
3976 : : || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
3977 : 4129 : l0_bb = split_edge (FALLTHRU_EDGE (entry_bb));
3978 : 4129 : l1_bb = single_succ (l0_bb);
3979 : 4129 : if (!broken_loop)
3980 : : {
3981 : 3722 : l2_bb = create_empty_bb (cont_bb);
3982 : 3722 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == l1_bb
3983 : : || (single_succ_edge (BRANCH_EDGE (cont_bb)->dest)->dest
3984 : : == l1_bb));
3985 : 3722 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
3986 : : }
3987 : : else
3988 : : l2_bb = NULL;
3989 : 4129 : l3_bb = BRANCH_EDGE (entry_bb)->dest;
3990 : 4129 : exit_bb = region->exit;
3991 : :
3992 : 4129 : gsi = gsi_last_nondebug_bb (entry_bb);
3993 : :
3994 : 4129 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
3995 : 4129 : if (fd->ordered
3996 : 4129 : && omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
3997 : : OMP_CLAUSE_LASTPRIVATE))
3998 : 4129 : ordered_lastprivate = false;
3999 : 4129 : tree reductions = NULL_TREE;
4000 : 4129 : tree mem = NULL_TREE, cond_var = NULL_TREE, condtemp = NULL_TREE;
4001 : 4129 : tree memv = NULL_TREE;
4002 : 4129 : if (fd->lastprivate_conditional)
4003 : : {
4004 : 46 : tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4005 : : OMP_CLAUSE__CONDTEMP_);
4006 : 46 : if (fd->have_pointer_condtemp)
4007 : 28 : condtemp = OMP_CLAUSE_DECL (c);
4008 : 46 : c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
4009 : 46 : cond_var = OMP_CLAUSE_DECL (c);
4010 : : }
4011 : 4129 : if (sched_arg)
4012 : : {
4013 : 169 : if (fd->have_reductemp)
4014 : : {
4015 : 149 : tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4016 : : OMP_CLAUSE__REDUCTEMP_);
4017 : 149 : reductions = OMP_CLAUSE_DECL (c);
4018 : 149 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
4019 : 149 : gimple *g = SSA_NAME_DEF_STMT (reductions);
4020 : 149 : reductions = gimple_assign_rhs1 (g);
4021 : 149 : OMP_CLAUSE_DECL (c) = reductions;
4022 : 149 : entry_bb = gimple_bb (g);
4023 : 149 : edge e = split_block (entry_bb, g);
4024 : 149 : if (region->entry == entry_bb)
4025 : 8 : region->entry = e->dest;
4026 : 298 : gsi = gsi_last_bb (entry_bb);
4027 : : }
4028 : : else
4029 : 20 : reductions = null_pointer_node;
4030 : 169 : if (fd->have_pointer_condtemp)
4031 : : {
4032 : 28 : tree type = TREE_TYPE (condtemp);
4033 : 28 : memv = create_tmp_var (type);
4034 : 28 : TREE_ADDRESSABLE (memv) = 1;
4035 : 28 : unsigned HOST_WIDE_INT sz
4036 : 28 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
4037 : 28 : sz *= fd->lastprivate_conditional;
4038 : 28 : expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
4039 : : false);
4040 : 28 : mem = build_fold_addr_expr (memv);
4041 : : }
4042 : : else
4043 : 141 : mem = null_pointer_node;
4044 : : }
4045 : 4129 : if (fd->collapse > 1 || fd->ordered)
4046 : : {
4047 : 1665 : int first_zero_iter1 = -1, first_zero_iter2 = -1;
4048 : 1665 : basic_block zero_iter1_bb = NULL, zero_iter2_bb = NULL, l2_dom_bb = NULL;
4049 : :
4050 : 1665 : counts = XALLOCAVEC (tree, fd->ordered
4051 : : ? fd->ordered + 2
4052 : : + (fd->ordered - fd->collapse)
4053 : : : fd->collapse);
4054 : 1665 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
4055 : : zero_iter1_bb, first_zero_iter1,
4056 : : zero_iter2_bb, first_zero_iter2, l2_dom_bb);
4057 : :
4058 : 1665 : if (zero_iter1_bb)
4059 : : {
4060 : : /* Some counts[i] vars might be uninitialized if
4061 : : some loop has zero iterations. But the body shouldn't
4062 : : be executed in that case, so just avoid uninit warnings. */
4063 : 1913 : for (i = first_zero_iter1;
4064 : 1913 : i < (fd->ordered ? fd->ordered : fd->collapse); i++)
4065 : 1502 : if (SSA_VAR_P (counts[i]))
4066 : 950 : suppress_warning (counts[i], OPT_Wuninitialized);
4067 : 411 : gsi_prev (&gsi);
4068 : 411 : e = split_block (entry_bb, gsi_stmt (gsi));
4069 : 411 : entry_bb = e->dest;
4070 : 411 : make_edge (zero_iter1_bb, entry_bb, EDGE_FALLTHRU);
4071 : 411 : gsi = gsi_last_nondebug_bb (entry_bb);
4072 : 411 : set_immediate_dominator (CDI_DOMINATORS, entry_bb,
4073 : : get_immediate_dominator (CDI_DOMINATORS,
4074 : : zero_iter1_bb));
4075 : : }
4076 : 1665 : if (zero_iter2_bb)
4077 : : {
4078 : : /* Some counts[i] vars might be uninitialized if
4079 : : some loop has zero iterations. But the body shouldn't
4080 : : be executed in that case, so just avoid uninit warnings. */
4081 : 394 : for (i = first_zero_iter2; i < fd->ordered; i++)
4082 : 276 : if (SSA_VAR_P (counts[i]))
4083 : 216 : suppress_warning (counts[i], OPT_Wuninitialized);
4084 : 118 : if (zero_iter1_bb)
4085 : 103 : make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
4086 : : else
4087 : : {
4088 : 15 : gsi_prev (&gsi);
4089 : 15 : e = split_block (entry_bb, gsi_stmt (gsi));
4090 : 15 : entry_bb = e->dest;
4091 : 15 : make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
4092 : 15 : gsi = gsi_last_nondebug_bb (entry_bb);
4093 : 15 : set_immediate_dominator (CDI_DOMINATORS, entry_bb,
4094 : : get_immediate_dominator
4095 : : (CDI_DOMINATORS, zero_iter2_bb));
4096 : : }
4097 : : }
4098 : 1665 : if (fd->collapse == 1)
4099 : : {
4100 : 184 : counts[0] = fd->loop.n2;
4101 : 184 : fd->loop = fd->loops[0];
4102 : : }
4103 : : }
4104 : :
4105 : 4129 : type = TREE_TYPE (fd->loop.v);
4106 : 4129 : istart0 = create_tmp_var (fd->iter_type, ".istart0");
4107 : 4129 : iend0 = create_tmp_var (fd->iter_type, ".iend0");
4108 : 4129 : TREE_ADDRESSABLE (istart0) = 1;
4109 : 4129 : TREE_ADDRESSABLE (iend0) = 1;
4110 : :
4111 : : /* See if we need to bias by LLONG_MIN. */
4112 : 4129 : if (fd->iter_type == long_long_unsigned_type_node
4113 : 778 : && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
4114 : 530 : && !TYPE_UNSIGNED (type)
4115 : 4129 : && fd->ordered == 0)
4116 : : {
4117 : 0 : tree n1, n2;
4118 : :
4119 : 0 : if (fd->loop.cond_code == LT_EXPR)
4120 : : {
4121 : 0 : n1 = fd->loop.n1;
4122 : 0 : n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
4123 : : }
4124 : : else
4125 : : {
4126 : 0 : n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
4127 : 0 : n2 = fd->loop.n1;
4128 : : }
4129 : 0 : if (TREE_CODE (n1) != INTEGER_CST
4130 : 0 : || TREE_CODE (n2) != INTEGER_CST
4131 : 0 : || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
4132 : 0 : bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
4133 : : }
4134 : :
4135 : 4129 : gimple_stmt_iterator gsif = gsi;
4136 : 4129 : gsi_prev (&gsif);
4137 : :
4138 : 4129 : tree arr = NULL_TREE;
4139 : 4129 : if (in_combined_parallel)
4140 : : {
4141 : 1084 : gcc_assert (fd->ordered == 0);
4142 : : /* In a combined parallel loop, emit a call to
4143 : : GOMP_loop_foo_next. */
4144 : 1084 : t = build_call_expr (builtin_decl_explicit (next_fn), 2,
4145 : : build_fold_addr_expr (istart0),
4146 : : build_fold_addr_expr (iend0));
4147 : : }
4148 : : else
4149 : : {
4150 : 3045 : tree t0, t1, t2, t3, t4;
4151 : : /* If this is not a combined parallel loop, emit a call to
4152 : : GOMP_loop_foo_start in ENTRY_BB. */
4153 : 3045 : t4 = build_fold_addr_expr (iend0);
4154 : 3045 : t3 = build_fold_addr_expr (istart0);
4155 : 3045 : if (fd->ordered)
4156 : : {
4157 : 662 : t0 = build_int_cst (unsigned_type_node,
4158 : 331 : fd->ordered - fd->collapse + 1);
4159 : 331 : arr = create_tmp_var (build_array_type_nelts (fd->iter_type,
4160 : 331 : fd->ordered
4161 : 331 : - fd->collapse + 1),
4162 : : ".omp_counts");
4163 : 331 : DECL_NAMELESS (arr) = 1;
4164 : 331 : TREE_ADDRESSABLE (arr) = 1;
4165 : 331 : TREE_STATIC (arr) = 1;
4166 : 331 : vec<constructor_elt, va_gc> *v;
4167 : 331 : vec_alloc (v, fd->ordered - fd->collapse + 1);
4168 : 331 : int idx;
4169 : :
4170 : 1555 : for (idx = 0; idx < fd->ordered - fd->collapse + 1; idx++)
4171 : : {
4172 : 1224 : tree c;
4173 : 1224 : if (idx == 0 && fd->collapse > 1)
4174 : 147 : c = fd->loop.n2;
4175 : : else
4176 : 1077 : c = counts[idx + fd->collapse - 1];
4177 : 1224 : tree purpose = size_int (idx);
4178 : 1224 : CONSTRUCTOR_APPEND_ELT (v, purpose, c);
4179 : 1224 : if (TREE_CODE (c) != INTEGER_CST)
4180 : 433 : TREE_STATIC (arr) = 0;
4181 : : }
4182 : :
4183 : 331 : DECL_INITIAL (arr) = build_constructor (TREE_TYPE (arr), v);
4184 : 331 : if (!TREE_STATIC (arr))
4185 : 232 : force_gimple_operand_gsi (&gsi, build1 (DECL_EXPR,
4186 : : void_type_node, arr),
4187 : : true, NULL_TREE, true, GSI_SAME_STMT);
4188 : 331 : t1 = build_fold_addr_expr (arr);
4189 : 331 : t2 = NULL_TREE;
4190 : : }
4191 : : else
4192 : : {
4193 : 2714 : t2 = fold_convert (fd->iter_type, fd->loop.step);
4194 : 2714 : t1 = fd->loop.n2;
4195 : 2714 : t0 = fd->loop.n1;
4196 : 2714 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
4197 : : {
4198 : 1152 : tree innerc
4199 : 1152 : = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4200 : : OMP_CLAUSE__LOOPTEMP_);
4201 : 1152 : gcc_assert (innerc);
4202 : 1152 : t0 = OMP_CLAUSE_DECL (innerc);
4203 : 1152 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4204 : : OMP_CLAUSE__LOOPTEMP_);
4205 : 1152 : gcc_assert (innerc);
4206 : 1152 : t1 = OMP_CLAUSE_DECL (innerc);
4207 : : }
4208 : 5316 : if (POINTER_TYPE_P (TREE_TYPE (t0))
4209 : 2714 : && TYPE_PRECISION (TREE_TYPE (t0))
4210 : 112 : != TYPE_PRECISION (fd->iter_type))
4211 : : {
4212 : : /* Avoid casting pointers to integer of a different size. */
4213 : 0 : tree itype = signed_type_for (type);
4214 : 0 : t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
4215 : 0 : t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
4216 : : }
4217 : : else
4218 : : {
4219 : 2714 : t1 = fold_convert (fd->iter_type, t1);
4220 : 2714 : t0 = fold_convert (fd->iter_type, t0);
4221 : : }
4222 : 2714 : if (bias)
4223 : : {
4224 : 0 : t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
4225 : 0 : t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
4226 : : }
4227 : : }
4228 : 3045 : if (fd->iter_type == long_integer_type_node || fd->ordered)
4229 : : {
4230 : 2323 : if (fd->chunk_size)
4231 : : {
4232 : 1574 : t = fold_convert (fd->iter_type, fd->chunk_size);
4233 : 1574 : t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
4234 : 1574 : if (sched_arg)
4235 : : {
4236 : 135 : if (fd->ordered)
4237 : 36 : t = build_call_expr (builtin_decl_explicit (start_fn),
4238 : : 8, t0, t1, sched_arg, t, t3, t4,
4239 : : reductions, mem);
4240 : : else
4241 : 99 : t = build_call_expr (builtin_decl_explicit (start_fn),
4242 : : 9, t0, t1, t2, sched_arg, t, t3, t4,
4243 : : reductions, mem);
4244 : : }
4245 : 1439 : else if (fd->ordered)
4246 : 295 : t = build_call_expr (builtin_decl_explicit (start_fn),
4247 : : 5, t0, t1, t, t3, t4);
4248 : : else
4249 : 1144 : t = build_call_expr (builtin_decl_explicit (start_fn),
4250 : : 6, t0, t1, t2, t, t3, t4);
4251 : : }
4252 : 749 : else if (fd->ordered)
4253 : 0 : t = build_call_expr (builtin_decl_explicit (start_fn),
4254 : : 4, t0, t1, t3, t4);
4255 : : else
4256 : 749 : t = build_call_expr (builtin_decl_explicit (start_fn),
4257 : : 5, t0, t1, t2, t3, t4);
4258 : : }
4259 : : else
4260 : : {
4261 : 722 : tree t5;
4262 : 722 : tree c_bool_type;
4263 : 722 : tree bfn_decl;
4264 : :
4265 : : /* The GOMP_loop_ull_*start functions have additional boolean
4266 : : argument, true for < loops and false for > loops.
4267 : : In Fortran, the C bool type can be different from
4268 : : boolean_type_node. */
4269 : 722 : bfn_decl = builtin_decl_explicit (start_fn);
4270 : 722 : c_bool_type = TREE_TYPE (TREE_TYPE (bfn_decl));
4271 : 722 : t5 = build_int_cst (c_bool_type,
4272 : 942 : fd->loop.cond_code == LT_EXPR ? 1 : 0);
4273 : 722 : if (fd->chunk_size)
4274 : : {
4275 : 390 : tree bfn_decl = builtin_decl_explicit (start_fn);
4276 : 390 : t = fold_convert (fd->iter_type, fd->chunk_size);
4277 : 390 : t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
4278 : 390 : if (sched_arg)
4279 : 34 : t = build_call_expr (bfn_decl, 10, t5, t0, t1, t2, sched_arg,
4280 : : t, t3, t4, reductions, mem);
4281 : : else
4282 : 356 : t = build_call_expr (bfn_decl, 7, t5, t0, t1, t2, t, t3, t4);
4283 : : }
4284 : : else
4285 : 332 : t = build_call_expr (builtin_decl_explicit (start_fn),
4286 : : 6, t5, t0, t1, t2, t3, t4);
4287 : : }
4288 : : }
4289 : 4129 : if (TREE_TYPE (t) != boolean_type_node)
4290 : 0 : t = fold_build2 (NE_EXPR, boolean_type_node,
4291 : : t, build_int_cst (TREE_TYPE (t), 0));
4292 : 4129 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4293 : : true, GSI_SAME_STMT);
4294 : 4129 : if (arr && !TREE_STATIC (arr))
4295 : : {
4296 : 232 : tree clobber = build_clobber (TREE_TYPE (arr));
4297 : 232 : gsi_insert_before (&gsi, gimple_build_assign (arr, clobber),
4298 : : GSI_SAME_STMT);
4299 : : }
4300 : 4129 : if (fd->have_pointer_condtemp)
4301 : 28 : expand_omp_build_assign (&gsi, condtemp, memv, false);
4302 : 4129 : if (fd->have_reductemp)
4303 : : {
4304 : 149 : gimple *g = gsi_stmt (gsi);
4305 : 149 : gsi_remove (&gsi, true);
4306 : 149 : release_ssa_name (gimple_assign_lhs (g));
4307 : :
4308 : 149 : entry_bb = region->entry;
4309 : 149 : gsi = gsi_last_nondebug_bb (entry_bb);
4310 : :
4311 : 149 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
4312 : : }
4313 : 4129 : gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
4314 : :
4315 : : /* Remove the GIMPLE_OMP_FOR statement. */
4316 : 4129 : gsi_remove (&gsi, true);
4317 : :
4318 : 4129 : if (gsi_end_p (gsif))
4319 : 1208 : gsif = gsi_after_labels (gsi_bb (gsif));
4320 : 4129 : gsi_next (&gsif);
4321 : :
4322 : : /* Iteration setup for sequential loop goes in L0_BB. */
4323 : 4129 : tree startvar = fd->loop.v;
4324 : 4129 : tree endvar = NULL_TREE;
4325 : :
4326 : 4129 : if (gimple_omp_for_combined_p (fd->for_stmt))
4327 : : {
4328 : 1389 : gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_FOR
4329 : : && gimple_omp_for_kind (inner_stmt)
4330 : : == GF_OMP_FOR_KIND_SIMD);
4331 : 1389 : tree innerc = omp_find_clause (gimple_omp_for_clauses (inner_stmt),
4332 : : OMP_CLAUSE__LOOPTEMP_);
4333 : 1389 : gcc_assert (innerc);
4334 : 1389 : startvar = OMP_CLAUSE_DECL (innerc);
4335 : 1389 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4336 : : OMP_CLAUSE__LOOPTEMP_);
4337 : 1389 : gcc_assert (innerc);
4338 : 1389 : endvar = OMP_CLAUSE_DECL (innerc);
4339 : : }
4340 : :
4341 : 4129 : gsi = gsi_start_bb (l0_bb);
4342 : 4129 : t = istart0;
4343 : 4129 : if (fd->ordered && fd->collapse == 1)
4344 : 184 : t = fold_build2 (MULT_EXPR, fd->iter_type, t,
4345 : : fold_convert (fd->iter_type, fd->loop.step));
4346 : 3945 : else if (bias)
4347 : 0 : t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
4348 : 4129 : if (fd->ordered && fd->collapse == 1)
4349 : : {
4350 : 184 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4351 : 8 : t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
4352 : : fd->loop.n1, fold_convert (sizetype, t));
4353 : : else
4354 : : {
4355 : 176 : t = fold_convert (TREE_TYPE (startvar), t);
4356 : 176 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
4357 : : fd->loop.n1, t);
4358 : : }
4359 : : }
4360 : : else
4361 : : {
4362 : 3945 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4363 : 240 : t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
4364 : 3945 : t = fold_convert (TREE_TYPE (startvar), t);
4365 : : }
4366 : 4129 : t = force_gimple_operand_gsi (&gsi, t,
4367 : 4129 : DECL_P (startvar)
4368 : 4129 : && TREE_ADDRESSABLE (startvar),
4369 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
4370 : 4129 : assign_stmt = gimple_build_assign (startvar, t);
4371 : 4129 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4372 : 4129 : if (cond_var)
4373 : : {
4374 : 46 : tree itype = TREE_TYPE (cond_var);
4375 : : /* For lastprivate(conditional:) itervar, we need some iteration
4376 : : counter that starts at unsigned non-zero and increases.
4377 : : Prefer as few IVs as possible, so if we can use startvar
4378 : : itself, use that, or startvar + constant (those would be
4379 : : incremented with step), and as last resort use the s0 + 1
4380 : : incremented by 1. */
4381 : 46 : if ((fd->ordered && fd->collapse == 1)
4382 : 46 : || bias
4383 : 46 : || POINTER_TYPE_P (type)
4384 : 46 : || TREE_CODE (fd->loop.n1) != INTEGER_CST
4385 : 40 : || fd->loop.cond_code != LT_EXPR)
4386 : 6 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, istart0),
4387 : : build_int_cst (itype, 1));
4388 : 40 : else if (tree_int_cst_sgn (fd->loop.n1) == 1)
4389 : 9 : t = fold_convert (itype, t);
4390 : : else
4391 : : {
4392 : 31 : tree c = fold_convert (itype, fd->loop.n1);
4393 : 31 : c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
4394 : 31 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
4395 : : }
4396 : 46 : t = force_gimple_operand_gsi (&gsi, t, false,
4397 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
4398 : 46 : assign_stmt = gimple_build_assign (cond_var, t);
4399 : 46 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4400 : : }
4401 : :
4402 : 4129 : t = iend0;
4403 : 4129 : if (fd->ordered && fd->collapse == 1)
4404 : 184 : t = fold_build2 (MULT_EXPR, fd->iter_type, t,
4405 : : fold_convert (fd->iter_type, fd->loop.step));
4406 : 3945 : else if (bias)
4407 : 0 : t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
4408 : 4129 : if (fd->ordered && fd->collapse == 1)
4409 : : {
4410 : 184 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4411 : 8 : t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
4412 : : fd->loop.n1, fold_convert (sizetype, t));
4413 : : else
4414 : : {
4415 : 176 : t = fold_convert (TREE_TYPE (startvar), t);
4416 : 176 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
4417 : : fd->loop.n1, t);
4418 : : }
4419 : : }
4420 : : else
4421 : : {
4422 : 3945 : if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4423 : 240 : t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
4424 : 3945 : t = fold_convert (TREE_TYPE (startvar), t);
4425 : : }
4426 : 4129 : iend = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4427 : : false, GSI_CONTINUE_LINKING);
4428 : 4129 : if (endvar)
4429 : : {
4430 : 1389 : assign_stmt = gimple_build_assign (endvar, iend);
4431 : 1389 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4432 : 1389 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (iend)))
4433 : 857 : assign_stmt = gimple_build_assign (fd->loop.v, iend);
4434 : : else
4435 : 532 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, iend);
4436 : 1389 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4437 : : }
4438 : : /* Handle linear clause adjustments. */
4439 : 4129 : tree itercnt = NULL_TREE;
4440 : 4129 : if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
4441 : 27005 : for (tree c = gimple_omp_for_clauses (fd->for_stmt);
4442 : 27005 : c; c = OMP_CLAUSE_CHAIN (c))
4443 : 22876 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
4444 : 22876 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
4445 : : {
4446 : 72 : tree d = OMP_CLAUSE_DECL (c);
4447 : 72 : tree t = d, a, dest;
4448 : 72 : if (omp_privatize_by_reference (t))
4449 : 2 : t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
4450 : 72 : tree type = TREE_TYPE (t);
4451 : 72 : if (POINTER_TYPE_P (type))
4452 : 0 : type = sizetype;
4453 : 72 : dest = unshare_expr (t);
4454 : 72 : tree v = create_tmp_var (TREE_TYPE (t), NULL);
4455 : 72 : expand_omp_build_assign (&gsif, v, t);
4456 : 72 : if (itercnt == NULL_TREE)
4457 : : {
4458 : 72 : itercnt = startvar;
4459 : 72 : tree n1 = fd->loop.n1;
4460 : 72 : if (POINTER_TYPE_P (TREE_TYPE (itercnt)))
4461 : : {
4462 : 0 : itercnt
4463 : 0 : = fold_convert (signed_type_for (TREE_TYPE (itercnt)),
4464 : : itercnt);
4465 : 0 : n1 = fold_convert (TREE_TYPE (itercnt), n1);
4466 : : }
4467 : 72 : itercnt = fold_build2 (MINUS_EXPR, TREE_TYPE (itercnt),
4468 : : itercnt, n1);
4469 : 72 : itercnt = fold_build2 (EXACT_DIV_EXPR, TREE_TYPE (itercnt),
4470 : : itercnt, fd->loop.step);
4471 : 72 : itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
4472 : : NULL_TREE, false,
4473 : : GSI_CONTINUE_LINKING);
4474 : : }
4475 : 72 : a = fold_build2 (MULT_EXPR, type,
4476 : : fold_convert (type, itercnt),
4477 : : fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
4478 : 72 : t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
4479 : : : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
4480 : 72 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4481 : : false, GSI_CONTINUE_LINKING);
4482 : 72 : expand_omp_build_assign (&gsi, dest, t, true);
4483 : : }
4484 : 4129 : if (fd->collapse > 1)
4485 : 1481 : expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
4486 : :
4487 : 4129 : if (fd->ordered)
4488 : : {
4489 : : /* Until now, counts array contained number of iterations or
4490 : : variable containing it for ith loop. From now on, we usually need
4491 : : those counts only for collapsed loops, and only for the 2nd
4492 : : till the last collapsed one. Move those one element earlier,
4493 : : we'll use counts[fd->collapse - 1] for the first source/sink
4494 : : iteration counter and so on and counts[fd->ordered]
4495 : : as the array holding the current counter values for
4496 : : depend(source). For doacross(sink:omp_cur_iteration - 1) we need
4497 : : the counts from fd->collapse to fd->ordered - 1; make a copy of
4498 : : those to counts[fd->ordered + 2] and onwards.
4499 : : counts[fd->ordered + 1] can be a flag whether it is the first
4500 : : iteration with a new collapsed counter (used only if
4501 : : fd->ordered > fd->collapse). */
4502 : 331 : if (fd->ordered > fd->collapse)
4503 : 171 : memcpy (counts + fd->ordered + 2, counts + fd->collapse,
4504 : 171 : (fd->ordered - fd->collapse) * sizeof (counts[0]));
4505 : 331 : if (fd->collapse > 1)
4506 : 147 : memmove (counts, counts + 1, (fd->collapse - 1) * sizeof (counts[0]));
4507 : 331 : if (broken_loop)
4508 : : {
4509 : : int i;
4510 : 41 : for (i = fd->collapse; i < fd->ordered; i++)
4511 : : {
4512 : 33 : tree type = TREE_TYPE (fd->loops[i].v);
4513 : 33 : tree this_cond
4514 : 33 : = fold_build2 (fd->loops[i].cond_code, boolean_type_node,
4515 : : fold_convert (type, fd->loops[i].n1),
4516 : : fold_convert (type, fd->loops[i].n2));
4517 : 33 : if (!integer_onep (this_cond))
4518 : : break;
4519 : : }
4520 : 37 : if (i < fd->ordered)
4521 : : {
4522 : 29 : if (entry_bb->loop_father != l0_bb->loop_father)
4523 : : {
4524 : 4 : remove_bb_from_loops (l0_bb);
4525 : 4 : add_bb_to_loop (l0_bb, entry_bb->loop_father);
4526 : 4 : gcc_assert (single_succ (l0_bb) == l1_bb);
4527 : : }
4528 : 29 : cont_bb
4529 : 29 : = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
4530 : 29 : add_bb_to_loop (cont_bb, l0_bb->loop_father);
4531 : 29 : gimple_stmt_iterator gsi = gsi_after_labels (cont_bb);
4532 : 29 : gimple *g = gimple_build_omp_continue (fd->loop.v, fd->loop.v);
4533 : 29 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4534 : 29 : make_edge (cont_bb, l3_bb, EDGE_FALLTHRU);
4535 : 29 : make_edge (cont_bb, l1_bb, 0);
4536 : 29 : l2_bb = create_empty_bb (cont_bb);
4537 : 29 : broken_loop = false;
4538 : : }
4539 : : }
4540 : 331 : expand_omp_ordered_source_sink (region, fd, counts, cont_bb);
4541 : 331 : cont_bb = expand_omp_for_ordered_loops (fd, counts, cont_bb, l1_bb,
4542 : : l0_bb, ordered_lastprivate);
4543 : 331 : if (counts[fd->collapse - 1])
4544 : : {
4545 : 37 : gcc_assert (fd->collapse == 1);
4546 : 37 : gsi = gsi_last_bb (l0_bb);
4547 : 37 : expand_omp_build_assign (&gsi, counts[fd->collapse - 1],
4548 : : istart0, true);
4549 : 37 : if (cont_bb)
4550 : : {
4551 : 33 : gsi = gsi_last_bb (cont_bb);
4552 : 33 : t = fold_build2 (PLUS_EXPR, fd->iter_type,
4553 : : counts[fd->collapse - 1],
4554 : : build_int_cst (fd->iter_type, 1));
4555 : 33 : expand_omp_build_assign (&gsi, counts[fd->collapse - 1], t);
4556 : 66 : tree aref = build4 (ARRAY_REF, fd->iter_type,
4557 : 33 : counts[fd->ordered], size_zero_node,
4558 : : NULL_TREE, NULL_TREE);
4559 : 33 : expand_omp_build_assign (&gsi, aref, counts[fd->collapse - 1]);
4560 : : }
4561 : 37 : t = counts[fd->collapse - 1];
4562 : : }
4563 : 294 : else if (fd->collapse > 1)
4564 : 147 : t = fd->loop.v;
4565 : : else
4566 : : {
4567 : 147 : t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
4568 : : fd->loops[0].v, fd->loops[0].n1);
4569 : 147 : t = fold_convert (fd->iter_type, t);
4570 : : }
4571 : 331 : gsi = gsi_last_bb (l0_bb);
4572 : 331 : tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
4573 : : size_zero_node, NULL_TREE, NULL_TREE);
4574 : 331 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4575 : : false, GSI_CONTINUE_LINKING);
4576 : 331 : expand_omp_build_assign (&gsi, aref, t, true);
4577 : : }
4578 : :
4579 : 4129 : if (!broken_loop)
4580 : : {
4581 : : /* Code to control the increment and predicate for the sequential
4582 : : loop goes in the CONT_BB. */
4583 : 3751 : gsi = gsi_last_nondebug_bb (cont_bb);
4584 : 3751 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
4585 : 3751 : gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
4586 : 3751 : vmain = gimple_omp_continue_control_use (cont_stmt);
4587 : 3751 : vback = gimple_omp_continue_control_def (cont_stmt);
4588 : :
4589 : 3751 : if (cond_var)
4590 : : {
4591 : 46 : tree itype = TREE_TYPE (cond_var);
4592 : 46 : tree t2;
4593 : 46 : if ((fd->ordered && fd->collapse == 1)
4594 : 46 : || bias
4595 : 46 : || POINTER_TYPE_P (type)
4596 : 46 : || TREE_CODE (fd->loop.n1) != INTEGER_CST
4597 : 40 : || fd->loop.cond_code != LT_EXPR)
4598 : 6 : t2 = build_int_cst (itype, 1);
4599 : : else
4600 : 40 : t2 = fold_convert (itype, fd->loop.step);
4601 : 46 : t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
4602 : 46 : t2 = force_gimple_operand_gsi (&gsi, t2, false,
4603 : : NULL_TREE, true, GSI_SAME_STMT);
4604 : 46 : assign_stmt = gimple_build_assign (cond_var, t2);
4605 : 46 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
4606 : : }
4607 : :
4608 : 3751 : if (!gimple_omp_for_combined_p (fd->for_stmt))
4609 : : {
4610 : 2362 : if (POINTER_TYPE_P (type))
4611 : 160 : t = fold_build_pointer_plus (vmain, fd->loop.step);
4612 : : else
4613 : 2202 : t = fold_build2 (PLUS_EXPR, type, vmain, fd->loop.step);
4614 : 2362 : t = force_gimple_operand_gsi (&gsi, t,
4615 : 2362 : DECL_P (vback)
4616 : 2362 : && TREE_ADDRESSABLE (vback),
4617 : : NULL_TREE, true, GSI_SAME_STMT);
4618 : 2362 : assign_stmt = gimple_build_assign (vback, t);
4619 : 2362 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
4620 : :
4621 : 2362 : if (fd->ordered && counts[fd->collapse - 1] == NULL_TREE)
4622 : : {
4623 : 290 : tree tem;
4624 : 290 : if (fd->collapse > 1)
4625 : 143 : tem = fd->loop.v;
4626 : : else
4627 : : {
4628 : 147 : tem = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
4629 : : fd->loops[0].v, fd->loops[0].n1);
4630 : 147 : tem = fold_convert (fd->iter_type, tem);
4631 : : }
4632 : 580 : tree aref = build4 (ARRAY_REF, fd->iter_type,
4633 : 290 : counts[fd->ordered], size_zero_node,
4634 : : NULL_TREE, NULL_TREE);
4635 : 290 : tem = force_gimple_operand_gsi (&gsi, tem, true, NULL_TREE,
4636 : : true, GSI_SAME_STMT);
4637 : 290 : expand_omp_build_assign (&gsi, aref, tem);
4638 : : }
4639 : :
4640 : 2362 : t = build2 (fd->loop.cond_code, boolean_type_node,
4641 : 2362 : DECL_P (vback) && TREE_ADDRESSABLE (vback) ? t : vback,
4642 : : iend);
4643 : 2362 : gcond *cond_stmt = gimple_build_cond_empty (t);
4644 : 2362 : gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
4645 : : }
4646 : :
4647 : : /* Remove GIMPLE_OMP_CONTINUE. */
4648 : 3751 : gsi_remove (&gsi, true);
4649 : :
4650 : 3751 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
4651 : 681 : collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, l1_bb);
4652 : :
4653 : : /* Emit code to get the next parallel iteration in L2_BB. */
4654 : 3751 : gsi = gsi_start_bb (l2_bb);
4655 : :
4656 : 3751 : t = build_call_expr (builtin_decl_explicit (next_fn), 2,
4657 : : build_fold_addr_expr (istart0),
4658 : : build_fold_addr_expr (iend0));
4659 : 3751 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4660 : : false, GSI_CONTINUE_LINKING);
4661 : 3751 : if (TREE_TYPE (t) != boolean_type_node)
4662 : 0 : t = fold_build2 (NE_EXPR, boolean_type_node,
4663 : : t, build_int_cst (TREE_TYPE (t), 0));
4664 : 3751 : gcond *cond_stmt = gimple_build_cond_empty (t);
4665 : 3751 : gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
4666 : : }
4667 : :
4668 : : /* Add the loop cleanup function. */
4669 : 4129 : gsi = gsi_last_nondebug_bb (exit_bb);
4670 : 4129 : if (gimple_omp_return_nowait_p (gsi_stmt (gsi)))
4671 : 3055 : t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
4672 : 1074 : else if (gimple_omp_return_lhs (gsi_stmt (gsi)))
4673 : 2 : t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
4674 : : else
4675 : 1072 : t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
4676 : 4129 : gcall *call_stmt = gimple_build_call (t, 0);
4677 : 4129 : if (fd->ordered)
4678 : : {
4679 : 331 : tree arr = counts[fd->ordered];
4680 : 331 : tree clobber = build_clobber (TREE_TYPE (arr));
4681 : 331 : gsi_insert_after (&gsi, gimple_build_assign (arr, clobber),
4682 : : GSI_SAME_STMT);
4683 : : }
4684 : 4129 : if (gimple_omp_return_lhs (gsi_stmt (gsi)))
4685 : : {
4686 : 2 : gimple_call_set_lhs (call_stmt, gimple_omp_return_lhs (gsi_stmt (gsi)));
4687 : 2 : if (fd->have_reductemp)
4688 : : {
4689 : 0 : gimple *g = gimple_build_assign (reductions, NOP_EXPR,
4690 : : gimple_call_lhs (call_stmt));
4691 : 0 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
4692 : : }
4693 : : }
4694 : 4129 : gsi_insert_after (&gsi, call_stmt, GSI_SAME_STMT);
4695 : 4129 : gsi_remove (&gsi, true);
4696 : :
4697 : : /* Connect the new blocks. */
4698 : 4129 : find_edge (entry_bb, l0_bb)->flags = EDGE_TRUE_VALUE;
4699 : 4129 : find_edge (entry_bb, l3_bb)->flags = EDGE_FALSE_VALUE;
4700 : :
4701 : 4129 : if (!broken_loop)
4702 : : {
4703 : 3751 : gimple_seq phis;
4704 : :
4705 : 3751 : e = find_edge (cont_bb, l3_bb);
4706 : 3751 : ne = make_edge (l2_bb, l3_bb, EDGE_FALSE_VALUE);
4707 : :
4708 : 3751 : phis = phi_nodes (l3_bb);
4709 : 3773 : for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
4710 : : {
4711 : 11 : gimple *phi = gsi_stmt (gsi);
4712 : 11 : SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, ne),
4713 : : PHI_ARG_DEF_FROM_EDGE (phi, e));
4714 : : }
4715 : 3751 : remove_edge (e);
4716 : :
4717 : 3751 : make_edge (cont_bb, l2_bb, EDGE_FALSE_VALUE);
4718 : 3751 : e = find_edge (cont_bb, l1_bb);
4719 : 3751 : if (e == NULL)
4720 : : {
4721 : 13 : e = BRANCH_EDGE (cont_bb);
4722 : 13 : gcc_assert (single_succ (e->dest) == l1_bb);
4723 : : }
4724 : 3751 : if (gimple_omp_for_combined_p (fd->for_stmt))
4725 : : {
4726 : 1389 : remove_edge (e);
4727 : 1389 : e = NULL;
4728 : : }
4729 : 2362 : else if (fd->collapse > 1)
4730 : : {
4731 : 681 : remove_edge (e);
4732 : 681 : e = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
4733 : : }
4734 : : else
4735 : 1681 : e->flags = EDGE_TRUE_VALUE;
4736 : 3751 : if (e)
4737 : : {
4738 : 2362 : e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
4739 : 2362 : find_edge (cont_bb, l2_bb)->probability = e->probability.invert ();
4740 : : }
4741 : : else
4742 : : {
4743 : 1389 : e = find_edge (cont_bb, l2_bb);
4744 : 1389 : e->flags = EDGE_FALLTHRU;
4745 : : }
4746 : 3751 : make_edge (l2_bb, l0_bb, EDGE_TRUE_VALUE);
4747 : :
4748 : 3751 : if (gimple_in_ssa_p (cfun))
4749 : : {
4750 : : /* Add phis to the outer loop that connect to the phis in the inner,
4751 : : original loop, and move the loop entry value of the inner phi to
4752 : : the loop entry value of the outer phi. */
4753 : 13 : gphi_iterator psi;
4754 : 24 : for (psi = gsi_start_phis (l3_bb); !gsi_end_p (psi); gsi_next (&psi))
4755 : : {
4756 : 11 : location_t locus;
4757 : 11 : gphi *nphi;
4758 : 11 : gphi *exit_phi = psi.phi ();
4759 : :
4760 : 22 : if (virtual_operand_p (gimple_phi_result (exit_phi)))
4761 : 6 : continue;
4762 : :
4763 : 5 : edge l2_to_l3 = find_edge (l2_bb, l3_bb);
4764 : 5 : tree exit_res = PHI_ARG_DEF_FROM_EDGE (exit_phi, l2_to_l3);
4765 : :
4766 : 5 : basic_block latch = BRANCH_EDGE (cont_bb)->dest;
4767 : 5 : edge latch_to_l1 = find_edge (latch, l1_bb);
4768 : 5 : gphi *inner_phi
4769 : 5 : = find_phi_with_arg_on_edge (exit_res, latch_to_l1);
4770 : :
4771 : 5 : tree t = gimple_phi_result (exit_phi);
4772 : 5 : tree new_res = copy_ssa_name (t, NULL);
4773 : 5 : nphi = create_phi_node (new_res, l0_bb);
4774 : :
4775 : 5 : edge l0_to_l1 = find_edge (l0_bb, l1_bb);
4776 : 5 : t = PHI_ARG_DEF_FROM_EDGE (inner_phi, l0_to_l1);
4777 : 5 : locus = gimple_phi_arg_location_from_edge (inner_phi, l0_to_l1);
4778 : 5 : edge entry_to_l0 = find_edge (entry_bb, l0_bb);
4779 : 5 : add_phi_arg (nphi, t, entry_to_l0, locus);
4780 : :
4781 : 5 : edge l2_to_l0 = find_edge (l2_bb, l0_bb);
4782 : 5 : add_phi_arg (nphi, exit_res, l2_to_l0, UNKNOWN_LOCATION);
4783 : :
4784 : 5 : add_phi_arg (inner_phi, new_res, l0_to_l1, UNKNOWN_LOCATION);
4785 : : }
4786 : : }
4787 : :
4788 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l2_bb,
4789 : : recompute_dominator (CDI_DOMINATORS, l2_bb));
4790 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l3_bb,
4791 : : recompute_dominator (CDI_DOMINATORS, l3_bb));
4792 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l0_bb,
4793 : : recompute_dominator (CDI_DOMINATORS, l0_bb));
4794 : 3751 : set_immediate_dominator (CDI_DOMINATORS, l1_bb,
4795 : : recompute_dominator (CDI_DOMINATORS, l1_bb));
4796 : :
4797 : : /* We enter expand_omp_for_generic with a loop. This original loop may
4798 : : have its own loop struct, or it may be part of an outer loop struct
4799 : : (which may be the fake loop). */
4800 : 3751 : class loop *outer_loop = entry_bb->loop_father;
4801 : 3751 : bool orig_loop_has_loop_struct = l1_bb->loop_father != outer_loop;
4802 : :
4803 : 3751 : add_bb_to_loop (l2_bb, outer_loop);
4804 : :
4805 : : /* We've added a new loop around the original loop. Allocate the
4806 : : corresponding loop struct. */
4807 : 3751 : class loop *new_loop = alloc_loop ();
4808 : 3751 : new_loop->header = l0_bb;
4809 : 3751 : new_loop->latch = l2_bb;
4810 : 3751 : add_loop (new_loop, outer_loop);
4811 : :
4812 : : /* Allocate a loop structure for the original loop unless we already
4813 : : had one. */
4814 : 3751 : if (!orig_loop_has_loop_struct
4815 : 3751 : && !gimple_omp_for_combined_p (fd->for_stmt))
4816 : : {
4817 : 2345 : class loop *orig_loop = alloc_loop ();
4818 : 2345 : orig_loop->header = l1_bb;
4819 : : /* The loop may have multiple latches. */
4820 : 2345 : add_loop (orig_loop, new_loop);
4821 : : }
4822 : : }
4823 : 4129 : }
4824 : :
4825 : : /* Helper function for expand_omp_for_static_nochunk. If PTR is NULL,
4826 : : compute needed allocation size. If !ALLOC of team allocations,
4827 : : if ALLOC of thread allocation. SZ is the initial needed size for
4828 : : other purposes, ALLOC_ALIGN guaranteed alignment of allocation in bytes,
4829 : : CNT number of elements of each array, for !ALLOC this is
4830 : : omp_get_num_threads (), for ALLOC number of iterations handled by the
4831 : : current thread. If PTR is non-NULL, it is the start of the allocation
4832 : : and this routine shall assign to OMP_CLAUSE_DECL (c) of those _scantemp_
4833 : : clauses pointers to the corresponding arrays. */
4834 : :
4835 : : static tree
4836 : 692 : expand_omp_scantemp_alloc (tree clauses, tree ptr, unsigned HOST_WIDE_INT sz,
4837 : : unsigned HOST_WIDE_INT alloc_align, tree cnt,
4838 : : gimple_stmt_iterator *gsi, bool alloc)
4839 : : {
4840 : 692 : tree eltsz = NULL_TREE;
4841 : 692 : unsigned HOST_WIDE_INT preval = 0;
4842 : 692 : if (ptr && sz)
4843 : 5 : ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
4844 : : ptr, size_int (sz));
4845 : 5720 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
4846 : 5028 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
4847 : 3024 : && !OMP_CLAUSE__SCANTEMP__CONTROL (c)
4848 : 6668 : && (!OMP_CLAUSE__SCANTEMP__ALLOC (c)) != alloc)
4849 : : {
4850 : 820 : tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
4851 : 820 : unsigned HOST_WIDE_INT al = TYPE_ALIGN_UNIT (pointee_type);
4852 : 820 : if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
4853 : : {
4854 : 820 : unsigned HOST_WIDE_INT szl
4855 : 820 : = tree_to_uhwi (TYPE_SIZE_UNIT (pointee_type));
4856 : 820 : szl = least_bit_hwi (szl);
4857 : 820 : if (szl)
4858 : 820 : al = MIN (al, szl);
4859 : : }
4860 : 820 : if (ptr == NULL_TREE)
4861 : : {
4862 : 410 : if (eltsz == NULL_TREE)
4863 : 346 : eltsz = TYPE_SIZE_UNIT (pointee_type);
4864 : : else
4865 : 64 : eltsz = size_binop (PLUS_EXPR, eltsz,
4866 : : TYPE_SIZE_UNIT (pointee_type));
4867 : : }
4868 : 820 : if (preval == 0 && al <= alloc_align)
4869 : : {
4870 : 692 : unsigned HOST_WIDE_INT diff = ROUND_UP (sz, al) - sz;
4871 : 692 : sz += diff;
4872 : 692 : if (diff && ptr)
4873 : 0 : ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
4874 : : ptr, size_int (diff));
4875 : : }
4876 : 128 : else if (al > preval)
4877 : : {
4878 : 32 : if (ptr)
4879 : : {
4880 : 16 : ptr = fold_convert (pointer_sized_int_node, ptr);
4881 : 16 : ptr = fold_build2 (PLUS_EXPR, pointer_sized_int_node, ptr,
4882 : : build_int_cst (pointer_sized_int_node,
4883 : : al - 1));
4884 : 16 : ptr = fold_build2 (BIT_AND_EXPR, pointer_sized_int_node, ptr,
4885 : : build_int_cst (pointer_sized_int_node,
4886 : : -(HOST_WIDE_INT) al));
4887 : 16 : ptr = fold_convert (ptr_type_node, ptr);
4888 : : }
4889 : : else
4890 : 16 : sz += al - 1;
4891 : : }
4892 : 820 : if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
4893 : : preval = al;
4894 : : else
4895 : 0 : preval = 1;
4896 : 820 : if (ptr)
4897 : : {
4898 : 410 : expand_omp_build_assign (gsi, OMP_CLAUSE_DECL (c), ptr, false);
4899 : 410 : ptr = OMP_CLAUSE_DECL (c);
4900 : 410 : ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
4901 : : size_binop (MULT_EXPR, cnt,
4902 : : TYPE_SIZE_UNIT (pointee_type)));
4903 : : }
4904 : : }
4905 : :
4906 : 692 : if (ptr == NULL_TREE)
4907 : : {
4908 : 346 : eltsz = size_binop (MULT_EXPR, eltsz, cnt);
4909 : 346 : if (sz)
4910 : 13 : eltsz = size_binop (PLUS_EXPR, eltsz, size_int (sz));
4911 : 346 : return eltsz;
4912 : : }
4913 : : else
4914 : : return ptr;
4915 : : }
4916 : :
4917 : : /* Return the last _looptemp_ clause if one has been created for
4918 : : lastprivate on distribute parallel for{, simd} or taskloop.
4919 : : FD is the loop data and INNERC should be the second _looptemp_
4920 : : clause (the one holding the end of the range).
4921 : : This is followed by collapse - 1 _looptemp_ clauses for the
4922 : : counts[1] and up, and for triangular loops followed by 4
4923 : : further _looptemp_ clauses (one for counts[0], one first_inner_iterations,
4924 : : one factor and one adjn1). After this there is optionally one
4925 : : _looptemp_ clause that this function returns. */
4926 : :
4927 : : static tree
4928 : 1628 : find_lastprivate_looptemp (struct omp_for_data *fd, tree innerc)
4929 : : {
4930 : 1628 : gcc_assert (innerc);
4931 : 1628 : int count = fd->collapse - 1;
4932 : 1628 : if (fd->non_rect
4933 : 24 : && fd->last_nonrect == fd->first_nonrect + 1
4934 : 1640 : && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
4935 : 12 : count += 4;
4936 : 4799 : for (int i = 0; i < count; i++)
4937 : : {
4938 : 3171 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4939 : : OMP_CLAUSE__LOOPTEMP_);
4940 : 3171 : gcc_assert (innerc);
4941 : : }
4942 : 1628 : return omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4943 : 1628 : OMP_CLAUSE__LOOPTEMP_);
4944 : : }
4945 : :
4946 : : /* A subroutine of expand_omp_for. Generate code for a parallel
4947 : : loop with static schedule and no specified chunk size. Given
4948 : : parameters:
4949 : :
4950 : : for (V = N1; V cond N2; V += STEP) BODY;
4951 : :
4952 : : where COND is "<" or ">", we generate pseudocode
4953 : :
4954 : : if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
4955 : : if (cond is <)
4956 : : adj = STEP - 1;
4957 : : else
4958 : : adj = STEP + 1;
4959 : : if ((__typeof (V)) -1 > 0 && cond is >)
4960 : : n = -(adj + N2 - N1) / -STEP;
4961 : : else
4962 : : n = (adj + N2 - N1) / STEP;
4963 : : q = n / nthreads;
4964 : : tt = n % nthreads;
4965 : : if (threadid < tt) goto L3; else goto L4;
4966 : : L3:
4967 : : tt = 0;
4968 : : q = q + 1;
4969 : : L4:
4970 : : s0 = q * threadid + tt;
4971 : : e0 = s0 + q;
4972 : : V = s0 * STEP + N1;
4973 : : if (s0 >= e0) goto L2; else goto L0;
4974 : : L0:
4975 : : e = e0 * STEP + N1;
4976 : : L1:
4977 : : BODY;
4978 : : V += STEP;
4979 : : if (V cond e) goto L1;
4980 : : L2:
4981 : : */
4982 : :
4983 : : static void
4984 : 13931 : expand_omp_for_static_nochunk (struct omp_region *region,
4985 : : struct omp_for_data *fd,
4986 : : gimple *inner_stmt)
4987 : : {
4988 : 13931 : tree n, q, s0, e0, e, t, tt, nthreads = NULL_TREE, threadid;
4989 : 13931 : tree type, itype, vmain, vback;
4990 : 13931 : basic_block entry_bb, second_bb, third_bb, exit_bb, seq_start_bb;
4991 : 13931 : basic_block body_bb, cont_bb, collapse_bb = NULL;
4992 : 13931 : basic_block fin_bb, fourth_bb = NULL, fifth_bb = NULL, sixth_bb = NULL;
4993 : 13931 : basic_block exit1_bb = NULL, exit2_bb = NULL, exit3_bb = NULL;
4994 : 13931 : gimple_stmt_iterator gsi, gsip;
4995 : 13931 : edge ep;
4996 : 13931 : bool broken_loop = region->cont == NULL;
4997 : 13931 : tree *counts = NULL;
4998 : 13931 : tree n1, n2, step;
4999 : 13931 : tree reductions = NULL_TREE;
5000 : 13931 : tree cond_var = NULL_TREE, condtemp = NULL_TREE;
5001 : :
5002 : 13931 : itype = type = TREE_TYPE (fd->loop.v);
5003 : 13931 : if (POINTER_TYPE_P (type))
5004 : 606 : itype = signed_type_for (type);
5005 : :
5006 : 13931 : entry_bb = region->entry;
5007 : 13931 : cont_bb = region->cont;
5008 : 13931 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
5009 : 13931 : fin_bb = BRANCH_EDGE (entry_bb)->dest;
5010 : 13931 : gcc_assert (broken_loop
5011 : : || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
5012 : 13931 : seq_start_bb = split_edge (FALLTHRU_EDGE (entry_bb));
5013 : 13931 : body_bb = single_succ (seq_start_bb);
5014 : 13931 : if (!broken_loop)
5015 : : {
5016 : 13435 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
5017 : : || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
5018 : 13435 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
5019 : : }
5020 : 13931 : exit_bb = region->exit;
5021 : :
5022 : : /* Iteration space partitioning goes in ENTRY_BB. */
5023 : 13931 : gsi = gsi_last_nondebug_bb (entry_bb);
5024 : 13931 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5025 : 13931 : gsip = gsi;
5026 : 13931 : gsi_prev (&gsip);
5027 : :
5028 : 13931 : if (fd->collapse > 1)
5029 : : {
5030 : 3887 : int first_zero_iter = -1, dummy = -1;
5031 : 3887 : basic_block l2_dom_bb = NULL, dummy_bb = NULL;
5032 : :
5033 : 3887 : counts = XALLOCAVEC (tree, fd->collapse);
5034 : 3887 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
5035 : : fin_bb, first_zero_iter,
5036 : : dummy_bb, dummy, l2_dom_bb);
5037 : 3887 : t = NULL_TREE;
5038 : : }
5039 : 10044 : else if (gimple_omp_for_combined_into_p (fd->for_stmt))
5040 : 2006 : t = integer_one_node;
5041 : : else
5042 : 8038 : t = fold_binary (fd->loop.cond_code, boolean_type_node,
5043 : : fold_convert (type, fd->loop.n1),
5044 : : fold_convert (type, fd->loop.n2));
5045 : 13931 : if (fd->collapse == 1
5046 : 10044 : && TYPE_UNSIGNED (type)
5047 : 15939 : && (t == NULL_TREE || !integer_onep (t)))
5048 : : {
5049 : 518 : n1 = fold_convert (type, unshare_expr (fd->loop.n1));
5050 : 518 : n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
5051 : : true, GSI_SAME_STMT);
5052 : 518 : n2 = fold_convert (type, unshare_expr (fd->loop.n2));
5053 : 518 : n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
5054 : : true, GSI_SAME_STMT);
5055 : 518 : gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
5056 : : n1, n2);
5057 : 518 : ep = split_block (entry_bb, cond_stmt);
5058 : 518 : ep->flags = EDGE_TRUE_VALUE;
5059 : 518 : entry_bb = ep->dest;
5060 : 518 : ep->probability = profile_probability::very_likely ();
5061 : 518 : ep = make_edge (ep->src, fin_bb, EDGE_FALSE_VALUE);
5062 : 518 : ep->probability = profile_probability::very_unlikely ();
5063 : 518 : if (gimple_in_ssa_p (cfun))
5064 : : {
5065 : 20 : int dest_idx = find_edge (entry_bb, fin_bb)->dest_idx;
5066 : 20 : for (gphi_iterator gpi = gsi_start_phis (fin_bb);
5067 : 42 : !gsi_end_p (gpi); gsi_next (&gpi))
5068 : : {
5069 : 22 : gphi *phi = gpi.phi ();
5070 : 22 : add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
5071 : : ep, UNKNOWN_LOCATION);
5072 : : }
5073 : : }
5074 : 1036 : gsi = gsi_last_bb (entry_bb);
5075 : : }
5076 : :
5077 : 13931 : if (fd->lastprivate_conditional)
5078 : : {
5079 : 108 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5080 : 108 : tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
5081 : 108 : if (fd->have_pointer_condtemp)
5082 : 38 : condtemp = OMP_CLAUSE_DECL (c);
5083 : 108 : c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
5084 : 108 : cond_var = OMP_CLAUSE_DECL (c);
5085 : : }
5086 : 13931 : if (fd->have_reductemp
5087 : : /* For scan, we don't want to reinitialize condtemp before the
5088 : : second loop. */
5089 : 13878 : || (fd->have_pointer_condtemp && !fd->have_scantemp)
5090 : 13860 : || fd->have_nonctrl_scantemp)
5091 : : {
5092 : 244 : tree t1 = build_int_cst (long_integer_type_node, 0);
5093 : 244 : tree t2 = build_int_cst (long_integer_type_node, 1);
5094 : 488 : tree t3 = build_int_cstu (long_integer_type_node,
5095 : 244 : (HOST_WIDE_INT_1U << 31) + 1);
5096 : 244 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5097 : 244 : gimple_stmt_iterator gsi2 = gsi_none ();
5098 : 244 : gimple *g = NULL;
5099 : 244 : tree mem = null_pointer_node, memv = NULL_TREE;
5100 : 244 : unsigned HOST_WIDE_INT condtemp_sz = 0;
5101 : 244 : unsigned HOST_WIDE_INT alloc_align = 0;
5102 : 244 : if (fd->have_reductemp)
5103 : : {
5104 : 53 : gcc_assert (!fd->have_nonctrl_scantemp);
5105 : 53 : tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
5106 : 53 : reductions = OMP_CLAUSE_DECL (c);
5107 : 53 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
5108 : 53 : g = SSA_NAME_DEF_STMT (reductions);
5109 : 53 : reductions = gimple_assign_rhs1 (g);
5110 : 53 : OMP_CLAUSE_DECL (c) = reductions;
5111 : 53 : gsi2 = gsi_for_stmt (g);
5112 : : }
5113 : : else
5114 : : {
5115 : 191 : if (gsi_end_p (gsip))
5116 : 0 : gsi2 = gsi_after_labels (region->entry);
5117 : : else
5118 : 191 : gsi2 = gsip;
5119 : : reductions = null_pointer_node;
5120 : : }
5121 : 244 : if (fd->have_pointer_condtemp || fd->have_nonctrl_scantemp)
5122 : : {
5123 : 201 : tree type;
5124 : 201 : if (fd->have_pointer_condtemp)
5125 : 33 : type = TREE_TYPE (condtemp);
5126 : : else
5127 : 168 : type = ptr_type_node;
5128 : 201 : memv = create_tmp_var (type);
5129 : 201 : TREE_ADDRESSABLE (memv) = 1;
5130 : 201 : unsigned HOST_WIDE_INT sz = 0;
5131 : 201 : tree size = NULL_TREE;
5132 : 201 : if (fd->have_pointer_condtemp)
5133 : : {
5134 : 33 : sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
5135 : 33 : sz *= fd->lastprivate_conditional;
5136 : 33 : condtemp_sz = sz;
5137 : : }
5138 : 201 : if (fd->have_nonctrl_scantemp)
5139 : : {
5140 : 173 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5141 : 173 : gimple *g = gimple_build_call (nthreads, 0);
5142 : 173 : nthreads = create_tmp_var (integer_type_node);
5143 : 173 : gimple_call_set_lhs (g, nthreads);
5144 : 173 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
5145 : 173 : nthreads = fold_convert (sizetype, nthreads);
5146 : 173 : alloc_align = TYPE_ALIGN_UNIT (long_long_integer_type_node);
5147 : 173 : size = expand_omp_scantemp_alloc (clauses, NULL_TREE, sz,
5148 : : alloc_align, nthreads, NULL,
5149 : : false);
5150 : 173 : size = fold_convert (type, size);
5151 : : }
5152 : : else
5153 : 28 : size = build_int_cst (type, sz);
5154 : 201 : expand_omp_build_assign (&gsi2, memv, size, false);
5155 : 201 : mem = build_fold_addr_expr (memv);
5156 : : }
5157 : 244 : tree t
5158 : 244 : = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
5159 : : 9, t1, t2, t2, t3, t1, null_pointer_node,
5160 : : null_pointer_node, reductions, mem);
5161 : 244 : force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
5162 : : true, GSI_SAME_STMT);
5163 : 244 : if (fd->have_pointer_condtemp)
5164 : 33 : expand_omp_build_assign (&gsi2, condtemp, memv, false);
5165 : 244 : if (fd->have_nonctrl_scantemp)
5166 : : {
5167 : 173 : tree ptr = fd->have_pointer_condtemp ? condtemp : memv;
5168 : 173 : expand_omp_scantemp_alloc (clauses, ptr, condtemp_sz,
5169 : : alloc_align, nthreads, &gsi2, false);
5170 : : }
5171 : 244 : if (fd->have_reductemp)
5172 : : {
5173 : 53 : gsi_remove (&gsi2, true);
5174 : 53 : release_ssa_name (gimple_assign_lhs (g));
5175 : : }
5176 : : }
5177 : 13931 : switch (gimple_omp_for_kind (fd->for_stmt))
5178 : : {
5179 : 9412 : case GF_OMP_FOR_KIND_FOR:
5180 : 9412 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5181 : 9412 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
5182 : 9412 : break;
5183 : 4519 : case GF_OMP_FOR_KIND_DISTRIBUTE:
5184 : 4519 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_TEAMS);
5185 : 4519 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_TEAM_NUM);
5186 : 4519 : break;
5187 : 0 : default:
5188 : 0 : gcc_unreachable ();
5189 : : }
5190 : 13931 : nthreads = build_call_expr (nthreads, 0);
5191 : 13931 : nthreads = fold_convert (itype, nthreads);
5192 : 13931 : nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE,
5193 : : true, GSI_SAME_STMT);
5194 : 13931 : threadid = build_call_expr (threadid, 0);
5195 : 13931 : threadid = fold_convert (itype, threadid);
5196 : 13931 : threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE,
5197 : : true, GSI_SAME_STMT);
5198 : :
5199 : 13931 : n1 = fd->loop.n1;
5200 : 13931 : n2 = fd->loop.n2;
5201 : 13931 : step = fd->loop.step;
5202 : 13931 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
5203 : : {
5204 : 3037 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
5205 : : OMP_CLAUSE__LOOPTEMP_);
5206 : 3037 : gcc_assert (innerc);
5207 : 3037 : n1 = OMP_CLAUSE_DECL (innerc);
5208 : 3037 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5209 : : OMP_CLAUSE__LOOPTEMP_);
5210 : 3037 : gcc_assert (innerc);
5211 : 3037 : n2 = OMP_CLAUSE_DECL (innerc);
5212 : : }
5213 : 13931 : n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
5214 : : true, NULL_TREE, true, GSI_SAME_STMT);
5215 : 13931 : n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
5216 : : true, NULL_TREE, true, GSI_SAME_STMT);
5217 : 13931 : step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
5218 : : true, NULL_TREE, true, GSI_SAME_STMT);
5219 : :
5220 : 14950 : t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
5221 : 13931 : t = fold_build2 (PLUS_EXPR, itype, step, t);
5222 : 13931 : t = fold_build2 (PLUS_EXPR, itype, t, n2);
5223 : 13931 : t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
5224 : 13931 : if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
5225 : 414 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
5226 : : fold_build1 (NEGATE_EXPR, itype, t),
5227 : : fold_build1 (NEGATE_EXPR, itype, step));
5228 : : else
5229 : 13517 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
5230 : 13931 : t = fold_convert (itype, t);
5231 : 13931 : n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5232 : :
5233 : 13931 : q = create_tmp_reg (itype, "q");
5234 : 13931 : t = fold_build2 (TRUNC_DIV_EXPR, itype, n, nthreads);
5235 : 13931 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
5236 : 13931 : gsi_insert_before (&gsi, gimple_build_assign (q, t), GSI_SAME_STMT);
5237 : :
5238 : 13931 : tt = create_tmp_reg (itype, "tt");
5239 : 13931 : t = fold_build2 (TRUNC_MOD_EXPR, itype, n, nthreads);
5240 : 13931 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
5241 : 13931 : gsi_insert_before (&gsi, gimple_build_assign (tt, t), GSI_SAME_STMT);
5242 : :
5243 : 13931 : t = build2 (LT_EXPR, boolean_type_node, threadid, tt);
5244 : 13931 : gcond *cond_stmt = gimple_build_cond_empty (t);
5245 : 13931 : gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
5246 : :
5247 : 13931 : second_bb = split_block (entry_bb, cond_stmt)->dest;
5248 : 13931 : gsi = gsi_last_nondebug_bb (second_bb);
5249 : 13931 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5250 : :
5251 : 13931 : gsi_insert_before (&gsi, gimple_build_assign (tt, build_int_cst (itype, 0)),
5252 : : GSI_SAME_STMT);
5253 : 13931 : gassign *assign_stmt
5254 : 13931 : = gimple_build_assign (q, PLUS_EXPR, q, build_int_cst (itype, 1));
5255 : 13931 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5256 : :
5257 : 13931 : third_bb = split_block (second_bb, assign_stmt)->dest;
5258 : 13931 : gsi = gsi_last_nondebug_bb (third_bb);
5259 : 13931 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5260 : :
5261 : 13931 : if (fd->have_nonctrl_scantemp)
5262 : : {
5263 : 173 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5264 : 173 : tree controlp = NULL_TREE, controlb = NULL_TREE;
5265 : 756 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5266 : 756 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5267 : 756 : && OMP_CLAUSE__SCANTEMP__CONTROL (c))
5268 : : {
5269 : 346 : if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
5270 : 173 : controlb = OMP_CLAUSE_DECL (c);
5271 : : else
5272 : 173 : controlp = OMP_CLAUSE_DECL (c);
5273 : 346 : if (controlb && controlp)
5274 : : break;
5275 : : }
5276 : 173 : gcc_assert (controlp && controlb);
5277 : 173 : tree cnt = create_tmp_var (sizetype);
5278 : 173 : gimple *g = gimple_build_assign (cnt, NOP_EXPR, q);
5279 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5280 : 173 : unsigned HOST_WIDE_INT alloc_align = TYPE_ALIGN_UNIT (ptr_type_node);
5281 : 173 : tree sz = expand_omp_scantemp_alloc (clauses, NULL_TREE, 0,
5282 : : alloc_align, cnt, NULL, true);
5283 : 173 : tree size = create_tmp_var (sizetype);
5284 : 173 : expand_omp_build_assign (&gsi, size, sz, false);
5285 : 173 : tree cmp = fold_build2 (GT_EXPR, boolean_type_node,
5286 : : size, size_int (16384));
5287 : 173 : expand_omp_build_assign (&gsi, controlb, cmp);
5288 : 173 : g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
5289 : : NULL_TREE, NULL_TREE);
5290 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5291 : 173 : fourth_bb = split_block (third_bb, g)->dest;
5292 : 173 : gsi = gsi_last_nondebug_bb (fourth_bb);
5293 : : /* FIXME: Once we have allocators, this should use allocator. */
5294 : 173 : g = gimple_build_call (builtin_decl_explicit (BUILT_IN_MALLOC), 1, size);
5295 : 173 : gimple_call_set_lhs (g, controlp);
5296 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5297 : 173 : expand_omp_scantemp_alloc (clauses, controlp, 0, alloc_align, cnt,
5298 : : &gsi, true);
5299 : 173 : gsi_prev (&gsi);
5300 : 173 : g = gsi_stmt (gsi);
5301 : 173 : fifth_bb = split_block (fourth_bb, g)->dest;
5302 : 173 : gsi = gsi_last_nondebug_bb (fifth_bb);
5303 : :
5304 : 346 : g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
5305 : 173 : gimple_call_set_lhs (g, controlp);
5306 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5307 : 173 : tree alloca_decl = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5308 : 1430 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5309 : 1257 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5310 : 1257 : && OMP_CLAUSE__SCANTEMP__ALLOC (c))
5311 : : {
5312 : 205 : tree tmp = create_tmp_var (sizetype);
5313 : 205 : tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
5314 : 205 : g = gimple_build_assign (tmp, MULT_EXPR, cnt,
5315 : 205 : TYPE_SIZE_UNIT (pointee_type));
5316 : 205 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5317 : 205 : g = gimple_build_call (alloca_decl, 2, tmp,
5318 : 205 : size_int (TYPE_ALIGN (pointee_type)));
5319 : 205 : gimple_call_set_lhs (g, OMP_CLAUSE_DECL (c));
5320 : 205 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5321 : : }
5322 : :
5323 : 173 : sixth_bb = split_block (fifth_bb, g)->dest;
5324 : 173 : gsi = gsi_last_nondebug_bb (sixth_bb);
5325 : : }
5326 : :
5327 : 13931 : t = build2 (MULT_EXPR, itype, q, threadid);
5328 : 13931 : t = build2 (PLUS_EXPR, itype, t, tt);
5329 : 13931 : s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5330 : :
5331 : 13931 : t = fold_build2 (PLUS_EXPR, itype, s0, q);
5332 : 13931 : e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5333 : :
5334 : 13931 : t = build2 (GE_EXPR, boolean_type_node, s0, e0);
5335 : 13931 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
5336 : :
5337 : : /* Remove the GIMPLE_OMP_FOR statement. */
5338 : 13931 : gsi_remove (&gsi, true);
5339 : :
5340 : : /* Setup code for sequential iteration goes in SEQ_START_BB. */
5341 : 13931 : gsi = gsi_start_bb (seq_start_bb);
5342 : :
5343 : 13931 : tree startvar = fd->loop.v;
5344 : 13931 : tree endvar = NULL_TREE;
5345 : :
5346 : 13931 : if (gimple_omp_for_combined_p (fd->for_stmt))
5347 : : {
5348 : 7050 : tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
5349 : 10568 : ? gimple_omp_parallel_clauses (inner_stmt)
5350 : 3532 : : gimple_omp_for_clauses (inner_stmt);
5351 : 7050 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
5352 : 7050 : gcc_assert (innerc);
5353 : 7050 : startvar = OMP_CLAUSE_DECL (innerc);
5354 : 7050 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5355 : : OMP_CLAUSE__LOOPTEMP_);
5356 : 7050 : gcc_assert (innerc);
5357 : 7050 : endvar = OMP_CLAUSE_DECL (innerc);
5358 : 2284 : if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
5359 : 8360 : && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
5360 : : {
5361 : 823 : innerc = find_lastprivate_looptemp (fd, innerc);
5362 : 823 : if (innerc)
5363 : : {
5364 : : /* If needed (distribute parallel for with lastprivate),
5365 : : propagate down the total number of iterations. */
5366 : 371 : tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
5367 : : fd->loop.n2);
5368 : 371 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
5369 : : GSI_CONTINUE_LINKING);
5370 : 371 : assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
5371 : 371 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5372 : : }
5373 : : }
5374 : : }
5375 : 13931 : t = fold_convert (itype, s0);
5376 : 13931 : t = fold_build2 (MULT_EXPR, itype, t, step);
5377 : 13931 : if (POINTER_TYPE_P (type))
5378 : : {
5379 : 606 : t = fold_build_pointer_plus (n1, t);
5380 : 670 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
5381 : 670 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
5382 : 0 : t = fold_convert (signed_type_for (type), t);
5383 : : }
5384 : : else
5385 : 13325 : t = fold_build2 (PLUS_EXPR, type, t, n1);
5386 : 13931 : t = fold_convert (TREE_TYPE (startvar), t);
5387 : 13931 : t = force_gimple_operand_gsi (&gsi, t,
5388 : 13931 : DECL_P (startvar)
5389 : 13931 : && TREE_ADDRESSABLE (startvar),
5390 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
5391 : 13931 : assign_stmt = gimple_build_assign (startvar, t);
5392 : 13931 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5393 : 13931 : if (cond_var)
5394 : : {
5395 : 108 : tree itype = TREE_TYPE (cond_var);
5396 : : /* For lastprivate(conditional:) itervar, we need some iteration
5397 : : counter that starts at unsigned non-zero and increases.
5398 : : Prefer as few IVs as possible, so if we can use startvar
5399 : : itself, use that, or startvar + constant (those would be
5400 : : incremented with step), and as last resort use the s0 + 1
5401 : : incremented by 1. */
5402 : 108 : if (POINTER_TYPE_P (type)
5403 : 108 : || TREE_CODE (n1) != INTEGER_CST
5404 : 102 : || fd->loop.cond_code != LT_EXPR)
5405 : 6 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
5406 : : build_int_cst (itype, 1));
5407 : 102 : else if (tree_int_cst_sgn (n1) == 1)
5408 : 12 : t = fold_convert (itype, t);
5409 : : else
5410 : : {
5411 : 90 : tree c = fold_convert (itype, n1);
5412 : 90 : c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
5413 : 90 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
5414 : : }
5415 : 108 : t = force_gimple_operand_gsi (&gsi, t, false,
5416 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
5417 : 108 : assign_stmt = gimple_build_assign (cond_var, t);
5418 : 108 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5419 : : }
5420 : :
5421 : 13931 : t = fold_convert (itype, e0);
5422 : 13931 : t = fold_build2 (MULT_EXPR, itype, t, step);
5423 : 13931 : if (POINTER_TYPE_P (type))
5424 : : {
5425 : 606 : t = fold_build_pointer_plus (n1, t);
5426 : 670 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
5427 : 670 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
5428 : 0 : t = fold_convert (signed_type_for (type), t);
5429 : : }
5430 : : else
5431 : 13325 : t = fold_build2 (PLUS_EXPR, type, t, n1);
5432 : 13931 : t = fold_convert (TREE_TYPE (startvar), t);
5433 : 13931 : e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
5434 : : false, GSI_CONTINUE_LINKING);
5435 : 13931 : if (endvar)
5436 : : {
5437 : 7050 : assign_stmt = gimple_build_assign (endvar, e);
5438 : 7050 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5439 : 7050 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
5440 : 6281 : assign_stmt = gimple_build_assign (fd->loop.v, e);
5441 : : else
5442 : 769 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
5443 : 7050 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5444 : : }
5445 : : /* Handle linear clause adjustments. */
5446 : 13931 : tree itercnt = NULL_TREE;
5447 : 13931 : tree *nonrect_bounds = NULL;
5448 : 13931 : if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
5449 : 45005 : for (tree c = gimple_omp_for_clauses (fd->for_stmt);
5450 : 45005 : c; c = OMP_CLAUSE_CHAIN (c))
5451 : 35593 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5452 : 35593 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
5453 : : {
5454 : 87 : tree d = OMP_CLAUSE_DECL (c);
5455 : 87 : tree t = d, a, dest;
5456 : 87 : if (omp_privatize_by_reference (t))
5457 : 13 : t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
5458 : 87 : if (itercnt == NULL_TREE)
5459 : : {
5460 : 87 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
5461 : : {
5462 : 0 : itercnt = fold_build2 (MINUS_EXPR, itype,
5463 : : fold_convert (itype, n1),
5464 : : fold_convert (itype, fd->loop.n1));
5465 : 0 : itercnt = fold_build2 (EXACT_DIV_EXPR, itype, itercnt, step);
5466 : 0 : itercnt = fold_build2 (PLUS_EXPR, itype, itercnt, s0);
5467 : 0 : itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
5468 : : NULL_TREE, false,
5469 : : GSI_CONTINUE_LINKING);
5470 : : }
5471 : : else
5472 : : itercnt = s0;
5473 : : }
5474 : 87 : tree type = TREE_TYPE (t);
5475 : 87 : if (POINTER_TYPE_P (type))
5476 : 0 : type = sizetype;
5477 : 87 : a = fold_build2 (MULT_EXPR, type,
5478 : : fold_convert (type, itercnt),
5479 : : fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
5480 : 87 : dest = unshare_expr (t);
5481 : 87 : t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
5482 : : : POINTER_PLUS_EXPR, TREE_TYPE (t), t, a);
5483 : 87 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
5484 : : false, GSI_CONTINUE_LINKING);
5485 : 87 : expand_omp_build_assign (&gsi, dest, t, true);
5486 : : }
5487 : 13931 : if (fd->collapse > 1)
5488 : : {
5489 : 3887 : if (fd->non_rect)
5490 : : {
5491 : 376 : nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
5492 : 376 : memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
5493 : : }
5494 : 3887 : expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
5495 : : startvar);
5496 : : }
5497 : :
5498 : 13931 : if (!broken_loop)
5499 : : {
5500 : : /* The code controlling the sequential loop replaces the
5501 : : GIMPLE_OMP_CONTINUE. */
5502 : 13435 : gsi = gsi_last_nondebug_bb (cont_bb);
5503 : 13435 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
5504 : 13435 : gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
5505 : 13435 : vmain = gimple_omp_continue_control_use (cont_stmt);
5506 : 13435 : vback = gimple_omp_continue_control_def (cont_stmt);
5507 : :
5508 : 13435 : if (cond_var)
5509 : : {
5510 : 108 : tree itype = TREE_TYPE (cond_var);
5511 : 108 : tree t2;
5512 : 108 : if (POINTER_TYPE_P (type)
5513 : 108 : || TREE_CODE (n1) != INTEGER_CST
5514 : 102 : || fd->loop.cond_code != LT_EXPR)
5515 : 6 : t2 = build_int_cst (itype, 1);
5516 : : else
5517 : 102 : t2 = fold_convert (itype, step);
5518 : 108 : t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
5519 : 108 : t2 = force_gimple_operand_gsi (&gsi, t2, false,
5520 : : NULL_TREE, true, GSI_SAME_STMT);
5521 : 108 : assign_stmt = gimple_build_assign (cond_var, t2);
5522 : 108 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5523 : : }
5524 : :
5525 : 13435 : if (!gimple_omp_for_combined_p (fd->for_stmt))
5526 : : {
5527 : 6385 : if (POINTER_TYPE_P (type))
5528 : 316 : t = fold_build_pointer_plus (vmain, step);
5529 : : else
5530 : 6069 : t = fold_build2 (PLUS_EXPR, type, vmain, step);
5531 : 6385 : t = force_gimple_operand_gsi (&gsi, t,
5532 : 6385 : DECL_P (vback)
5533 : 6385 : && TREE_ADDRESSABLE (vback),
5534 : : NULL_TREE, true, GSI_SAME_STMT);
5535 : 6385 : assign_stmt = gimple_build_assign (vback, t);
5536 : 6385 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5537 : :
5538 : 6385 : t = build2 (fd->loop.cond_code, boolean_type_node,
5539 : 6385 : DECL_P (vback) && TREE_ADDRESSABLE (vback)
5540 : : ? t : vback, e);
5541 : 6385 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
5542 : : }
5543 : :
5544 : : /* Remove the GIMPLE_OMP_CONTINUE statement. */
5545 : 13435 : gsi_remove (&gsi, true);
5546 : :
5547 : 13435 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
5548 : 1375 : collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
5549 : : cont_bb, body_bb);
5550 : : }
5551 : :
5552 : : /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing. */
5553 : 13931 : gsi = gsi_last_nondebug_bb (exit_bb);
5554 : 13931 : if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
5555 : : {
5556 : 2392 : t = gimple_omp_return_lhs (gsi_stmt (gsi));
5557 : 2392 : if (fd->have_reductemp
5558 : 2339 : || ((fd->have_pointer_condtemp || fd->have_scantemp)
5559 : 66 : && !fd->have_nonctrl_scantemp))
5560 : : {
5561 : 119 : tree fn;
5562 : 119 : if (t)
5563 : 4 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
5564 : : else
5565 : 115 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
5566 : 119 : gcall *g = gimple_build_call (fn, 0);
5567 : 119 : if (t)
5568 : : {
5569 : 4 : gimple_call_set_lhs (g, t);
5570 : 4 : if (fd->have_reductemp)
5571 : 4 : gsi_insert_after (&gsi, gimple_build_assign (reductions,
5572 : : NOP_EXPR, t),
5573 : : GSI_SAME_STMT);
5574 : : }
5575 : 119 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
5576 : 119 : }
5577 : : else
5578 : 2273 : gsi_insert_after (&gsi, omp_build_barrier (t), GSI_SAME_STMT);
5579 : : }
5580 : 11539 : else if ((fd->have_pointer_condtemp || fd->have_scantemp)
5581 : 298 : && !fd->have_nonctrl_scantemp)
5582 : : {
5583 : 125 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
5584 : 125 : gcall *g = gimple_build_call (fn, 0);
5585 : 125 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
5586 : : }
5587 : 13931 : if (fd->have_scantemp && !fd->have_nonctrl_scantemp)
5588 : : {
5589 : 173 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5590 : 173 : tree controlp = NULL_TREE, controlb = NULL_TREE;
5591 : 346 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5592 : 346 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5593 : 346 : && OMP_CLAUSE__SCANTEMP__CONTROL (c))
5594 : : {
5595 : 346 : if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
5596 : 173 : controlb = OMP_CLAUSE_DECL (c);
5597 : : else
5598 : 173 : controlp = OMP_CLAUSE_DECL (c);
5599 : 346 : if (controlb && controlp)
5600 : : break;
5601 : : }
5602 : 173 : gcc_assert (controlp && controlb);
5603 : 173 : gimple *g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
5604 : : NULL_TREE, NULL_TREE);
5605 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5606 : 173 : exit1_bb = split_block (exit_bb, g)->dest;
5607 : 173 : gsi = gsi_after_labels (exit1_bb);
5608 : 173 : g = gimple_build_call (builtin_decl_explicit (BUILT_IN_FREE), 1,
5609 : : controlp);
5610 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5611 : 173 : exit2_bb = split_block (exit1_bb, g)->dest;
5612 : 173 : gsi = gsi_after_labels (exit2_bb);
5613 : 346 : g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE), 1,
5614 : : controlp);
5615 : 173 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5616 : 173 : exit3_bb = split_block (exit2_bb, g)->dest;
5617 : 173 : gsi = gsi_after_labels (exit3_bb);
5618 : : }
5619 : 13931 : gsi_remove (&gsi, true);
5620 : :
5621 : : /* Connect all the blocks. */
5622 : 13931 : ep = make_edge (entry_bb, third_bb, EDGE_FALSE_VALUE);
5623 : 13931 : ep->probability = profile_probability::guessed_always ().apply_scale (3, 4);
5624 : 13931 : ep = find_edge (entry_bb, second_bb);
5625 : 13931 : ep->flags = EDGE_TRUE_VALUE;
5626 : 13931 : ep->probability = profile_probability::guessed_always () / 4;
5627 : 13931 : if (fourth_bb)
5628 : : {
5629 : 173 : ep = make_edge (third_bb, fifth_bb, EDGE_FALSE_VALUE);
5630 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5631 : 173 : ep = find_edge (third_bb, fourth_bb);
5632 : 173 : ep->flags = EDGE_TRUE_VALUE;
5633 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5634 : 173 : ep = find_edge (fourth_bb, fifth_bb);
5635 : 173 : redirect_edge_and_branch (ep, sixth_bb);
5636 : : }
5637 : : else
5638 : : sixth_bb = third_bb;
5639 : 13931 : find_edge (sixth_bb, seq_start_bb)->flags = EDGE_FALSE_VALUE;
5640 : 13931 : find_edge (sixth_bb, fin_bb)->flags = EDGE_TRUE_VALUE;
5641 : 13931 : if (exit1_bb)
5642 : : {
5643 : 173 : ep = make_edge (exit_bb, exit2_bb, EDGE_FALSE_VALUE);
5644 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5645 : 173 : ep = find_edge (exit_bb, exit1_bb);
5646 : 173 : ep->flags = EDGE_TRUE_VALUE;
5647 : 173 : ep->probability = profile_probability::guessed_always () / 2;
5648 : 173 : ep = find_edge (exit1_bb, exit2_bb);
5649 : 173 : redirect_edge_and_branch (ep, exit3_bb);
5650 : : }
5651 : :
5652 : 13931 : if (!broken_loop)
5653 : : {
5654 : 13435 : ep = find_edge (cont_bb, body_bb);
5655 : 13435 : if (ep == NULL)
5656 : : {
5657 : 179 : ep = BRANCH_EDGE (cont_bb);
5658 : 179 : gcc_assert (single_succ (ep->dest) == body_bb);
5659 : : }
5660 : 13435 : if (gimple_omp_for_combined_p (fd->for_stmt))
5661 : : {
5662 : 7050 : remove_edge (ep);
5663 : 7050 : ep = NULL;
5664 : : }
5665 : 6385 : else if (fd->collapse > 1)
5666 : : {
5667 : 1375 : remove_edge (ep);
5668 : 1375 : ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
5669 : : }
5670 : : else
5671 : 5010 : ep->flags = EDGE_TRUE_VALUE;
5672 : 25495 : find_edge (cont_bb, fin_bb)->flags
5673 : 14810 : = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
5674 : : }
5675 : :
5676 : 13931 : set_immediate_dominator (CDI_DOMINATORS, second_bb, entry_bb);
5677 : 13931 : set_immediate_dominator (CDI_DOMINATORS, third_bb, entry_bb);
5678 : 13931 : if (fourth_bb)
5679 : : {
5680 : 173 : set_immediate_dominator (CDI_DOMINATORS, fifth_bb, third_bb);
5681 : 173 : set_immediate_dominator (CDI_DOMINATORS, sixth_bb, third_bb);
5682 : : }
5683 : 13931 : set_immediate_dominator (CDI_DOMINATORS, seq_start_bb, sixth_bb);
5684 : :
5685 : 13931 : set_immediate_dominator (CDI_DOMINATORS, body_bb,
5686 : : recompute_dominator (CDI_DOMINATORS, body_bb));
5687 : 13931 : set_immediate_dominator (CDI_DOMINATORS, fin_bb,
5688 : : recompute_dominator (CDI_DOMINATORS, fin_bb));
5689 : 13931 : if (exit1_bb)
5690 : : {
5691 : 173 : set_immediate_dominator (CDI_DOMINATORS, exit2_bb, exit_bb);
5692 : 173 : set_immediate_dominator (CDI_DOMINATORS, exit3_bb, exit_bb);
5693 : : }
5694 : :
5695 : 13931 : class loop *loop = body_bb->loop_father;
5696 : 13931 : if (loop != entry_bb->loop_father)
5697 : : {
5698 : 341 : gcc_assert (broken_loop || loop->header == body_bb);
5699 : 179 : gcc_assert (broken_loop
5700 : : || loop->latch == region->cont
5701 : : || single_pred (loop->latch) == region->cont);
5702 : 341 : return;
5703 : : }
5704 : :
5705 : 13590 : if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
5706 : : {
5707 : 6206 : loop = alloc_loop ();
5708 : 6206 : loop->header = body_bb;
5709 : 6206 : if (collapse_bb == NULL)
5710 : 4831 : loop->latch = cont_bb;
5711 : 6206 : add_loop (loop, body_bb->loop_father);
5712 : : }
5713 : : }
5714 : :
5715 : : /* Return phi in E->DEST with ARG on edge E. */
5716 : :
5717 : : static gphi *
5718 : 14 : find_phi_with_arg_on_edge (tree arg, edge e)
5719 : : {
5720 : 14 : basic_block bb = e->dest;
5721 : :
5722 : 14 : for (gphi_iterator gpi = gsi_start_phis (bb);
5723 : 14 : !gsi_end_p (gpi);
5724 : 0 : gsi_next (&gpi))
5725 : : {
5726 : 14 : gphi *phi = gpi.phi ();
5727 : 14 : if (PHI_ARG_DEF_FROM_EDGE (phi, e) == arg)
5728 : 14 : return phi;
5729 : : }
5730 : :
5731 : 0 : return NULL;
5732 : : }
5733 : :
5734 : : /* A subroutine of expand_omp_for. Generate code for a parallel
5735 : : loop with static schedule and a specified chunk size. Given
5736 : : parameters:
5737 : :
5738 : : for (V = N1; V cond N2; V += STEP) BODY;
5739 : :
5740 : : where COND is "<" or ">", we generate pseudocode
5741 : :
5742 : : if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
5743 : : if (cond is <)
5744 : : adj = STEP - 1;
5745 : : else
5746 : : adj = STEP + 1;
5747 : : if ((__typeof (V)) -1 > 0 && cond is >)
5748 : : n = -(adj + N2 - N1) / -STEP;
5749 : : else
5750 : : n = (adj + N2 - N1) / STEP;
5751 : : trip = 0;
5752 : : V = threadid * CHUNK * STEP + N1; -- this extra definition of V is
5753 : : here so that V is defined
5754 : : if the loop is not entered
5755 : : L0:
5756 : : s0 = (trip * nthreads + threadid) * CHUNK;
5757 : : e0 = min (s0 + CHUNK, n);
5758 : : if (s0 < n) goto L1; else goto L4;
5759 : : L1:
5760 : : V = s0 * STEP + N1;
5761 : : e = e0 * STEP + N1;
5762 : : L2:
5763 : : BODY;
5764 : : V += STEP;
5765 : : if (V cond e) goto L2; else goto L3;
5766 : : L3:
5767 : : trip += 1;
5768 : : goto L0;
5769 : : L4:
5770 : : */
5771 : :
5772 : : static void
5773 : 5811 : expand_omp_for_static_chunk (struct omp_region *region,
5774 : : struct omp_for_data *fd, gimple *inner_stmt)
5775 : : {
5776 : 5811 : tree n, s0, e0, e, t;
5777 : 5811 : tree trip_var, trip_init, trip_main, trip_back, nthreads, threadid;
5778 : 5811 : tree type, itype, vmain, vback, vextra;
5779 : 5811 : basic_block entry_bb, exit_bb, body_bb, seq_start_bb, iter_part_bb;
5780 : 5811 : basic_block trip_update_bb = NULL, cont_bb, collapse_bb = NULL, fin_bb;
5781 : 5811 : gimple_stmt_iterator gsi, gsip;
5782 : 5811 : edge se;
5783 : 5811 : bool broken_loop = region->cont == NULL;
5784 : 5811 : tree *counts = NULL;
5785 : 5811 : tree n1, n2, step;
5786 : 5811 : tree reductions = NULL_TREE;
5787 : 5811 : tree cond_var = NULL_TREE, condtemp = NULL_TREE;
5788 : :
5789 : 5811 : itype = type = TREE_TYPE (fd->loop.v);
5790 : 5811 : if (POINTER_TYPE_P (type))
5791 : 312 : itype = signed_type_for (type);
5792 : :
5793 : 5811 : entry_bb = region->entry;
5794 : 5811 : se = split_block (entry_bb, last_nondebug_stmt (entry_bb));
5795 : 5811 : entry_bb = se->src;
5796 : 5811 : iter_part_bb = se->dest;
5797 : 5811 : cont_bb = region->cont;
5798 : 5811 : gcc_assert (EDGE_COUNT (iter_part_bb->succs) == 2);
5799 : 5811 : fin_bb = BRANCH_EDGE (iter_part_bb)->dest;
5800 : 5811 : gcc_assert (broken_loop
5801 : : || fin_bb == FALLTHRU_EDGE (cont_bb)->dest);
5802 : 5811 : seq_start_bb = split_edge (FALLTHRU_EDGE (iter_part_bb));
5803 : 5811 : body_bb = single_succ (seq_start_bb);
5804 : 5811 : if (!broken_loop)
5805 : : {
5806 : 5554 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
5807 : : || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
5808 : 5554 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
5809 : 5554 : trip_update_bb = split_edge (FALLTHRU_EDGE (cont_bb));
5810 : : }
5811 : 5811 : exit_bb = region->exit;
5812 : :
5813 : : /* Trip and adjustment setup goes in ENTRY_BB. */
5814 : 5811 : gsi = gsi_last_nondebug_bb (entry_bb);
5815 : 5811 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5816 : 5811 : gsip = gsi;
5817 : 5811 : gsi_prev (&gsip);
5818 : :
5819 : 5811 : if (fd->collapse > 1)
5820 : : {
5821 : 2228 : int first_zero_iter = -1, dummy = -1;
5822 : 2228 : basic_block l2_dom_bb = NULL, dummy_bb = NULL;
5823 : :
5824 : 2228 : counts = XALLOCAVEC (tree, fd->collapse);
5825 : 2228 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
5826 : : fin_bb, first_zero_iter,
5827 : : dummy_bb, dummy, l2_dom_bb);
5828 : 2228 : t = NULL_TREE;
5829 : : }
5830 : 3583 : else if (gimple_omp_for_combined_into_p (fd->for_stmt))
5831 : 813 : t = integer_one_node;
5832 : : else
5833 : 2770 : t = fold_binary (fd->loop.cond_code, boolean_type_node,
5834 : : fold_convert (type, fd->loop.n1),
5835 : : fold_convert (type, fd->loop.n2));
5836 : 5811 : if (fd->collapse == 1
5837 : 3583 : && TYPE_UNSIGNED (type)
5838 : 6994 : && (t == NULL_TREE || !integer_onep (t)))
5839 : : {
5840 : 232 : n1 = fold_convert (type, unshare_expr (fd->loop.n1));
5841 : 232 : n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
5842 : : true, GSI_SAME_STMT);
5843 : 232 : n2 = fold_convert (type, unshare_expr (fd->loop.n2));
5844 : 232 : n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
5845 : : true, GSI_SAME_STMT);
5846 : 232 : gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
5847 : : n1, n2);
5848 : 232 : se = split_block (entry_bb, cond_stmt);
5849 : 232 : se->flags = EDGE_TRUE_VALUE;
5850 : 232 : entry_bb = se->dest;
5851 : 232 : se->probability = profile_probability::very_likely ();
5852 : 232 : se = make_edge (se->src, fin_bb, EDGE_FALSE_VALUE);
5853 : 232 : se->probability = profile_probability::very_unlikely ();
5854 : 232 : if (gimple_in_ssa_p (cfun))
5855 : : {
5856 : 1 : int dest_idx = find_edge (iter_part_bb, fin_bb)->dest_idx;
5857 : 1 : for (gphi_iterator gpi = gsi_start_phis (fin_bb);
5858 : 2 : !gsi_end_p (gpi); gsi_next (&gpi))
5859 : : {
5860 : 1 : gphi *phi = gpi.phi ();
5861 : 1 : add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
5862 : : se, UNKNOWN_LOCATION);
5863 : : }
5864 : : }
5865 : 464 : gsi = gsi_last_bb (entry_bb);
5866 : : }
5867 : :
5868 : 5811 : if (fd->lastprivate_conditional)
5869 : : {
5870 : 49 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5871 : 49 : tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
5872 : 49 : if (fd->have_pointer_condtemp)
5873 : 29 : condtemp = OMP_CLAUSE_DECL (c);
5874 : 49 : c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
5875 : 49 : cond_var = OMP_CLAUSE_DECL (c);
5876 : : }
5877 : 5811 : if (fd->have_reductemp || fd->have_pointer_condtemp)
5878 : : {
5879 : 44 : tree t1 = build_int_cst (long_integer_type_node, 0);
5880 : 44 : tree t2 = build_int_cst (long_integer_type_node, 1);
5881 : 88 : tree t3 = build_int_cstu (long_integer_type_node,
5882 : 44 : (HOST_WIDE_INT_1U << 31) + 1);
5883 : 44 : tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5884 : 44 : gimple_stmt_iterator gsi2 = gsi_none ();
5885 : 44 : gimple *g = NULL;
5886 : 44 : tree mem = null_pointer_node, memv = NULL_TREE;
5887 : 44 : if (fd->have_reductemp)
5888 : : {
5889 : 25 : tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
5890 : 25 : reductions = OMP_CLAUSE_DECL (c);
5891 : 25 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
5892 : 25 : g = SSA_NAME_DEF_STMT (reductions);
5893 : 25 : reductions = gimple_assign_rhs1 (g);
5894 : 25 : OMP_CLAUSE_DECL (c) = reductions;
5895 : 25 : gsi2 = gsi_for_stmt (g);
5896 : : }
5897 : : else
5898 : : {
5899 : 19 : if (gsi_end_p (gsip))
5900 : 0 : gsi2 = gsi_after_labels (region->entry);
5901 : : else
5902 : 19 : gsi2 = gsip;
5903 : : reductions = null_pointer_node;
5904 : : }
5905 : 44 : if (fd->have_pointer_condtemp)
5906 : : {
5907 : 29 : tree type = TREE_TYPE (condtemp);
5908 : 29 : memv = create_tmp_var (type);
5909 : 29 : TREE_ADDRESSABLE (memv) = 1;
5910 : 29 : unsigned HOST_WIDE_INT sz
5911 : 29 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
5912 : 29 : sz *= fd->lastprivate_conditional;
5913 : 29 : expand_omp_build_assign (&gsi2, memv, build_int_cst (type, sz),
5914 : : false);
5915 : 29 : mem = build_fold_addr_expr (memv);
5916 : : }
5917 : 44 : tree t
5918 : 44 : = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
5919 : : 9, t1, t2, t2, t3, t1, null_pointer_node,
5920 : : null_pointer_node, reductions, mem);
5921 : 44 : force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
5922 : : true, GSI_SAME_STMT);
5923 : 44 : if (fd->have_pointer_condtemp)
5924 : 29 : expand_omp_build_assign (&gsi2, condtemp, memv, false);
5925 : 44 : if (fd->have_reductemp)
5926 : : {
5927 : 25 : gsi_remove (&gsi2, true);
5928 : 25 : release_ssa_name (gimple_assign_lhs (g));
5929 : : }
5930 : : }
5931 : 5811 : switch (gimple_omp_for_kind (fd->for_stmt))
5932 : : {
5933 : 2357 : case GF_OMP_FOR_KIND_FOR:
5934 : 2357 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5935 : 2357 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
5936 : 2357 : break;
5937 : 3454 : case GF_OMP_FOR_KIND_DISTRIBUTE:
5938 : 3454 : nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_TEAMS);
5939 : 3454 : threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_TEAM_NUM);
5940 : 3454 : break;
5941 : 0 : default:
5942 : 0 : gcc_unreachable ();
5943 : : }
5944 : 5811 : nthreads = build_call_expr (nthreads, 0);
5945 : 5811 : nthreads = fold_convert (itype, nthreads);
5946 : 5811 : nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE,
5947 : : true, GSI_SAME_STMT);
5948 : 5811 : threadid = build_call_expr (threadid, 0);
5949 : 5811 : threadid = fold_convert (itype, threadid);
5950 : 5811 : threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE,
5951 : : true, GSI_SAME_STMT);
5952 : :
5953 : 5811 : n1 = fd->loop.n1;
5954 : 5811 : n2 = fd->loop.n2;
5955 : 5811 : step = fd->loop.step;
5956 : 5811 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
5957 : : {
5958 : 1301 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
5959 : : OMP_CLAUSE__LOOPTEMP_);
5960 : 1301 : gcc_assert (innerc);
5961 : 1301 : n1 = OMP_CLAUSE_DECL (innerc);
5962 : 1301 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5963 : : OMP_CLAUSE__LOOPTEMP_);
5964 : 1301 : gcc_assert (innerc);
5965 : 1301 : n2 = OMP_CLAUSE_DECL (innerc);
5966 : : }
5967 : 5811 : n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
5968 : : true, NULL_TREE, true, GSI_SAME_STMT);
5969 : 5811 : n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
5970 : : true, NULL_TREE, true, GSI_SAME_STMT);
5971 : 5811 : step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
5972 : : true, NULL_TREE, true, GSI_SAME_STMT);
5973 : 5811 : tree chunk_size = fold_convert (itype, fd->chunk_size);
5974 : 5811 : chunk_size = omp_adjust_chunk_size (chunk_size, fd->simd_schedule,
5975 : 5811 : is_in_offload_region (region));
5976 : 5811 : chunk_size
5977 : 5811 : = force_gimple_operand_gsi (&gsi, chunk_size, true, NULL_TREE, true,
5978 : : GSI_SAME_STMT);
5979 : :
5980 : 6417 : t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
5981 : 5811 : t = fold_build2 (PLUS_EXPR, itype, step, t);
5982 : 5811 : t = fold_build2 (PLUS_EXPR, itype, t, n2);
5983 : 5811 : t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
5984 : 5811 : if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
5985 : 284 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
5986 : : fold_build1 (NEGATE_EXPR, itype, t),
5987 : : fold_build1 (NEGATE_EXPR, itype, step));
5988 : : else
5989 : 5527 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
5990 : 5811 : t = fold_convert (itype, t);
5991 : 5811 : n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
5992 : : true, GSI_SAME_STMT);
5993 : :
5994 : 5811 : trip_var = create_tmp_reg (itype, ".trip");
5995 : 5811 : if (gimple_in_ssa_p (cfun))
5996 : : {
5997 : 12 : trip_init = make_ssa_name (trip_var);
5998 : 12 : trip_main = make_ssa_name (trip_var);
5999 : 12 : trip_back = make_ssa_name (trip_var);
6000 : : }
6001 : : else
6002 : : {
6003 : : trip_init = trip_var;
6004 : : trip_main = trip_var;
6005 : : trip_back = trip_var;
6006 : : }
6007 : :
6008 : 5811 : gassign *assign_stmt
6009 : 5811 : = gimple_build_assign (trip_init, build_int_cst (itype, 0));
6010 : 5811 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6011 : :
6012 : 5811 : t = fold_build2 (MULT_EXPR, itype, threadid, chunk_size);
6013 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, step);
6014 : 5811 : if (POINTER_TYPE_P (type))
6015 : 312 : t = fold_build_pointer_plus (n1, t);
6016 : : else
6017 : 5499 : t = fold_build2 (PLUS_EXPR, type, t, n1);
6018 : 5811 : vextra = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6019 : : true, GSI_SAME_STMT);
6020 : :
6021 : : /* Remove the GIMPLE_OMP_FOR. */
6022 : 5811 : gsi_remove (&gsi, true);
6023 : :
6024 : 5811 : gimple_stmt_iterator gsif = gsi;
6025 : :
6026 : : /* Iteration space partitioning goes in ITER_PART_BB. */
6027 : 5811 : gsi = gsi_last_bb (iter_part_bb);
6028 : :
6029 : 5811 : t = fold_build2 (MULT_EXPR, itype, trip_main, nthreads);
6030 : 5811 : t = fold_build2 (PLUS_EXPR, itype, t, threadid);
6031 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, chunk_size);
6032 : 5811 : s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6033 : : false, GSI_CONTINUE_LINKING);
6034 : :
6035 : 5811 : t = fold_build2 (PLUS_EXPR, itype, s0, chunk_size);
6036 : 5811 : t = fold_build2 (MIN_EXPR, itype, t, n);
6037 : 5811 : e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6038 : : false, GSI_CONTINUE_LINKING);
6039 : :
6040 : 5811 : t = build2 (LT_EXPR, boolean_type_node, s0, n);
6041 : 5811 : gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_CONTINUE_LINKING);
6042 : :
6043 : : /* Setup code for sequential iteration goes in SEQ_START_BB. */
6044 : 5811 : gsi = gsi_start_bb (seq_start_bb);
6045 : :
6046 : 5811 : tree startvar = fd->loop.v;
6047 : 5811 : tree endvar = NULL_TREE;
6048 : :
6049 : 5811 : if (gimple_omp_for_combined_p (fd->for_stmt))
6050 : : {
6051 : 4202 : tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
6052 : 6943 : ? gimple_omp_parallel_clauses (inner_stmt)
6053 : 1461 : : gimple_omp_for_clauses (inner_stmt);
6054 : 4202 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
6055 : 4202 : gcc_assert (innerc);
6056 : 4202 : startvar = OMP_CLAUSE_DECL (innerc);
6057 : 4202 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
6058 : : OMP_CLAUSE__LOOPTEMP_);
6059 : 4202 : gcc_assert (innerc);
6060 : 4202 : endvar = OMP_CLAUSE_DECL (innerc);
6061 : 1663 : if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
6062 : 5087 : && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
6063 : : {
6064 : 704 : innerc = find_lastprivate_looptemp (fd, innerc);
6065 : 704 : if (innerc)
6066 : : {
6067 : : /* If needed (distribute parallel for with lastprivate),
6068 : : propagate down the total number of iterations. */
6069 : 320 : tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
6070 : : fd->loop.n2);
6071 : 320 : t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
6072 : : GSI_CONTINUE_LINKING);
6073 : 320 : assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
6074 : 320 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6075 : : }
6076 : : }
6077 : : }
6078 : :
6079 : 5811 : t = fold_convert (itype, s0);
6080 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, step);
6081 : 5811 : if (POINTER_TYPE_P (type))
6082 : : {
6083 : 312 : t = fold_build_pointer_plus (n1, t);
6084 : 376 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
6085 : 376 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
6086 : 0 : t = fold_convert (signed_type_for (type), t);
6087 : : }
6088 : : else
6089 : 5499 : t = fold_build2 (PLUS_EXPR, type, t, n1);
6090 : 5811 : t = fold_convert (TREE_TYPE (startvar), t);
6091 : 5811 : t = force_gimple_operand_gsi (&gsi, t,
6092 : 5811 : DECL_P (startvar)
6093 : 5811 : && TREE_ADDRESSABLE (startvar),
6094 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
6095 : 5811 : assign_stmt = gimple_build_assign (startvar, t);
6096 : 5811 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6097 : 5811 : if (cond_var)
6098 : : {
6099 : 49 : tree itype = TREE_TYPE (cond_var);
6100 : : /* For lastprivate(conditional:) itervar, we need some iteration
6101 : : counter that starts at unsigned non-zero and increases.
6102 : : Prefer as few IVs as possible, so if we can use startvar
6103 : : itself, use that, or startvar + constant (those would be
6104 : : incremented with step), and as last resort use the s0 + 1
6105 : : incremented by 1. */
6106 : 49 : if (POINTER_TYPE_P (type)
6107 : 49 : || TREE_CODE (n1) != INTEGER_CST
6108 : 43 : || fd->loop.cond_code != LT_EXPR)
6109 : 6 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
6110 : : build_int_cst (itype, 1));
6111 : 43 : else if (tree_int_cst_sgn (n1) == 1)
6112 : 21 : t = fold_convert (itype, t);
6113 : : else
6114 : : {
6115 : 22 : tree c = fold_convert (itype, n1);
6116 : 22 : c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
6117 : 22 : t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
6118 : : }
6119 : 49 : t = force_gimple_operand_gsi (&gsi, t, false,
6120 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
6121 : 49 : assign_stmt = gimple_build_assign (cond_var, t);
6122 : 49 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6123 : : }
6124 : :
6125 : 5811 : t = fold_convert (itype, e0);
6126 : 5811 : t = fold_build2 (MULT_EXPR, itype, t, step);
6127 : 5811 : if (POINTER_TYPE_P (type))
6128 : : {
6129 : 312 : t = fold_build_pointer_plus (n1, t);
6130 : 376 : if (!POINTER_TYPE_P (TREE_TYPE (startvar))
6131 : 376 : && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
6132 : 0 : t = fold_convert (signed_type_for (type), t);
6133 : : }
6134 : : else
6135 : 5499 : t = fold_build2 (PLUS_EXPR, type, t, n1);
6136 : 5811 : t = fold_convert (TREE_TYPE (startvar), t);
6137 : 5811 : e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6138 : : false, GSI_CONTINUE_LINKING);
6139 : 5811 : if (endvar)
6140 : : {
6141 : 4202 : assign_stmt = gimple_build_assign (endvar, e);
6142 : 4202 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6143 : 4202 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
6144 : 3434 : assign_stmt = gimple_build_assign (fd->loop.v, e);
6145 : : else
6146 : 768 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
6147 : 4202 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6148 : : }
6149 : : /* Handle linear clause adjustments. */
6150 : 5811 : tree itercnt = NULL_TREE, itercntbias = NULL_TREE;
6151 : 5811 : if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
6152 : 15766 : for (tree c = gimple_omp_for_clauses (fd->for_stmt);
6153 : 15766 : c; c = OMP_CLAUSE_CHAIN (c))
6154 : 13409 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6155 : 13409 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6156 : : {
6157 : 68 : tree d = OMP_CLAUSE_DECL (c);
6158 : 68 : tree t = d, a, dest;
6159 : 68 : if (omp_privatize_by_reference (t))
6160 : 3 : t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
6161 : 68 : tree type = TREE_TYPE (t);
6162 : 68 : if (POINTER_TYPE_P (type))
6163 : 0 : type = sizetype;
6164 : 68 : dest = unshare_expr (t);
6165 : 68 : tree v = create_tmp_var (TREE_TYPE (t), NULL);
6166 : 68 : expand_omp_build_assign (&gsif, v, t);
6167 : 68 : if (itercnt == NULL_TREE)
6168 : : {
6169 : 68 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
6170 : : {
6171 : 0 : itercntbias
6172 : 0 : = fold_build2 (MINUS_EXPR, itype, fold_convert (itype, n1),
6173 : : fold_convert (itype, fd->loop.n1));
6174 : 0 : itercntbias = fold_build2 (EXACT_DIV_EXPR, itype,
6175 : : itercntbias, step);
6176 : 0 : itercntbias
6177 : 0 : = force_gimple_operand_gsi (&gsif, itercntbias, true,
6178 : : NULL_TREE, true,
6179 : : GSI_SAME_STMT);
6180 : 0 : itercnt = fold_build2 (PLUS_EXPR, itype, itercntbias, s0);
6181 : 0 : itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
6182 : : NULL_TREE, false,
6183 : : GSI_CONTINUE_LINKING);
6184 : : }
6185 : : else
6186 : : itercnt = s0;
6187 : : }
6188 : 68 : a = fold_build2 (MULT_EXPR, type,
6189 : : fold_convert (type, itercnt),
6190 : : fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
6191 : 68 : t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
6192 : : : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
6193 : 68 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6194 : : false, GSI_CONTINUE_LINKING);
6195 : 68 : expand_omp_build_assign (&gsi, dest, t, true);
6196 : : }
6197 : 5811 : if (fd->collapse > 1)
6198 : 2228 : expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
6199 : :
6200 : 5811 : if (!broken_loop)
6201 : : {
6202 : : /* The code controlling the sequential loop goes in CONT_BB,
6203 : : replacing the GIMPLE_OMP_CONTINUE. */
6204 : 5554 : gsi = gsi_last_nondebug_bb (cont_bb);
6205 : 5554 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
6206 : 5554 : vmain = gimple_omp_continue_control_use (cont_stmt);
6207 : 5554 : vback = gimple_omp_continue_control_def (cont_stmt);
6208 : :
6209 : 5554 : if (cond_var)
6210 : : {
6211 : 49 : tree itype = TREE_TYPE (cond_var);
6212 : 49 : tree t2;
6213 : 49 : if (POINTER_TYPE_P (type)
6214 : 49 : || TREE_CODE (n1) != INTEGER_CST
6215 : 43 : || fd->loop.cond_code != LT_EXPR)
6216 : 6 : t2 = build_int_cst (itype, 1);
6217 : : else
6218 : 43 : t2 = fold_convert (itype, step);
6219 : 49 : t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
6220 : 49 : t2 = force_gimple_operand_gsi (&gsi, t2, false,
6221 : : NULL_TREE, true, GSI_SAME_STMT);
6222 : 49 : assign_stmt = gimple_build_assign (cond_var, t2);
6223 : 49 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6224 : : }
6225 : :
6226 : 5554 : if (!gimple_omp_for_combined_p (fd->for_stmt))
6227 : : {
6228 : 1352 : if (POINTER_TYPE_P (type))
6229 : 92 : t = fold_build_pointer_plus (vmain, step);
6230 : : else
6231 : 1260 : t = fold_build2 (PLUS_EXPR, type, vmain, step);
6232 : 1352 : if (DECL_P (vback) && TREE_ADDRESSABLE (vback))
6233 : 0 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6234 : : true, GSI_SAME_STMT);
6235 : 1352 : assign_stmt = gimple_build_assign (vback, t);
6236 : 1352 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6237 : :
6238 : 1352 : if (tree_int_cst_equal (fd->chunk_size, integer_one_node))
6239 : 94 : t = build2 (EQ_EXPR, boolean_type_node,
6240 : 94 : build_int_cst (itype, 0),
6241 : 94 : build_int_cst (itype, 1));
6242 : : else
6243 : 1258 : t = build2 (fd->loop.cond_code, boolean_type_node,
6244 : 1258 : DECL_P (vback) && TREE_ADDRESSABLE (vback)
6245 : : ? t : vback, e);
6246 : 1352 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
6247 : : }
6248 : :
6249 : : /* Remove GIMPLE_OMP_CONTINUE. */
6250 : 5554 : gsi_remove (&gsi, true);
6251 : :
6252 : 5554 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
6253 : 445 : collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, body_bb);
6254 : :
6255 : : /* Trip update code goes into TRIP_UPDATE_BB. */
6256 : 5554 : gsi = gsi_start_bb (trip_update_bb);
6257 : :
6258 : 5554 : t = build_int_cst (itype, 1);
6259 : 5554 : t = build2 (PLUS_EXPR, itype, trip_main, t);
6260 : 5554 : assign_stmt = gimple_build_assign (trip_back, t);
6261 : 5554 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6262 : : }
6263 : :
6264 : : /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing. */
6265 : 5811 : gsi = gsi_last_nondebug_bb (exit_bb);
6266 : 5811 : if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
6267 : : {
6268 : 296 : t = gimple_omp_return_lhs (gsi_stmt (gsi));
6269 : 296 : if (fd->have_reductemp || fd->have_pointer_condtemp)
6270 : : {
6271 : 35 : tree fn;
6272 : 35 : if (t)
6273 : 0 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
6274 : : else
6275 : 35 : fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
6276 : 35 : gcall *g = gimple_build_call (fn, 0);
6277 : 35 : if (t)
6278 : : {
6279 : 0 : gimple_call_set_lhs (g, t);
6280 : 0 : if (fd->have_reductemp)
6281 : 0 : gsi_insert_after (&gsi, gimple_build_assign (reductions,
6282 : : NOP_EXPR, t),
6283 : : GSI_SAME_STMT);
6284 : : }
6285 : 35 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
6286 : 35 : }
6287 : : else
6288 : 261 : gsi_insert_after (&gsi, omp_build_barrier (t), GSI_SAME_STMT);
6289 : : }
6290 : 5515 : else if (fd->have_pointer_condtemp)
6291 : : {
6292 : 9 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
6293 : 9 : gcall *g = gimple_build_call (fn, 0);
6294 : 9 : gsi_insert_after (&gsi, g, GSI_SAME_STMT);
6295 : : }
6296 : 5811 : gsi_remove (&gsi, true);
6297 : :
6298 : : /* Connect the new blocks. */
6299 : 5811 : find_edge (iter_part_bb, seq_start_bb)->flags = EDGE_TRUE_VALUE;
6300 : 5811 : find_edge (iter_part_bb, fin_bb)->flags = EDGE_FALSE_VALUE;
6301 : :
6302 : 5811 : if (!broken_loop)
6303 : : {
6304 : 5554 : se = find_edge (cont_bb, body_bb);
6305 : 5554 : if (se == NULL)
6306 : : {
6307 : 12 : se = BRANCH_EDGE (cont_bb);
6308 : 12 : gcc_assert (single_succ (se->dest) == body_bb);
6309 : : }
6310 : 5554 : if (gimple_omp_for_combined_p (fd->for_stmt))
6311 : : {
6312 : 4202 : remove_edge (se);
6313 : 4202 : se = NULL;
6314 : : }
6315 : 1352 : else if (fd->collapse > 1)
6316 : : {
6317 : 445 : remove_edge (se);
6318 : 445 : se = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
6319 : : }
6320 : : else
6321 : 907 : se->flags = EDGE_TRUE_VALUE;
6322 : 10663 : find_edge (cont_bb, trip_update_bb)->flags
6323 : 445 : = se ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
6324 : :
6325 : 5554 : redirect_edge_and_branch (single_succ_edge (trip_update_bb),
6326 : : iter_part_bb);
6327 : : }
6328 : :
6329 : 5811 : if (gimple_in_ssa_p (cfun))
6330 : : {
6331 : 12 : gphi_iterator psi;
6332 : 12 : gphi *phi;
6333 : 12 : edge re, ene;
6334 : 12 : edge_var_map *vm;
6335 : 12 : size_t i;
6336 : :
6337 : 12 : gcc_assert (fd->collapse == 1 && !broken_loop);
6338 : :
6339 : : /* When we redirect the edge from trip_update_bb to iter_part_bb, we
6340 : : remove arguments of the phi nodes in fin_bb. We need to create
6341 : : appropriate phi nodes in iter_part_bb instead. */
6342 : 12 : se = find_edge (iter_part_bb, fin_bb);
6343 : 12 : re = single_succ_edge (trip_update_bb);
6344 : 12 : vec<edge_var_map> *head = redirect_edge_var_map_vector (re);
6345 : 12 : ene = single_succ_edge (entry_bb);
6346 : :
6347 : 12 : psi = gsi_start_phis (fin_bb);
6348 : 21 : for (i = 0; !gsi_end_p (psi) && head->iterate (i, &vm);
6349 : 9 : gsi_next (&psi), ++i)
6350 : : {
6351 : 9 : gphi *nphi;
6352 : 9 : location_t locus;
6353 : :
6354 : 9 : phi = psi.phi ();
6355 : 9 : if (operand_equal_p (gimple_phi_arg_def (phi, 0),
6356 : 9 : redirect_edge_var_map_def (vm), 0))
6357 : 0 : continue;
6358 : :
6359 : 9 : t = gimple_phi_result (phi);
6360 : 9 : gcc_assert (t == redirect_edge_var_map_result (vm));
6361 : :
6362 : 9 : if (!single_pred_p (fin_bb))
6363 : 1 : t = copy_ssa_name (t, phi);
6364 : :
6365 : 9 : nphi = create_phi_node (t, iter_part_bb);
6366 : :
6367 : 9 : t = PHI_ARG_DEF_FROM_EDGE (phi, se);
6368 : 9 : locus = gimple_phi_arg_location_from_edge (phi, se);
6369 : :
6370 : : /* A special case -- fd->loop.v is not yet computed in
6371 : : iter_part_bb, we need to use vextra instead. */
6372 : 9 : if (t == fd->loop.v)
6373 : 0 : t = vextra;
6374 : 9 : add_phi_arg (nphi, t, ene, locus);
6375 : 9 : locus = redirect_edge_var_map_location (vm);
6376 : 9 : tree back_arg = redirect_edge_var_map_def (vm);
6377 : 9 : add_phi_arg (nphi, back_arg, re, locus);
6378 : 9 : edge ce = find_edge (cont_bb, body_bb);
6379 : 9 : if (ce == NULL)
6380 : : {
6381 : 9 : ce = BRANCH_EDGE (cont_bb);
6382 : 9 : gcc_assert (single_succ (ce->dest) == body_bb);
6383 : : ce = single_succ_edge (ce->dest);
6384 : : }
6385 : 9 : gphi *inner_loop_phi = find_phi_with_arg_on_edge (back_arg, ce);
6386 : 9 : gcc_assert (inner_loop_phi != NULL);
6387 : 9 : add_phi_arg (inner_loop_phi, gimple_phi_result (nphi),
6388 : : find_edge (seq_start_bb, body_bb), locus);
6389 : :
6390 : 9 : if (!single_pred_p (fin_bb))
6391 : 1 : add_phi_arg (phi, gimple_phi_result (nphi), se, locus);
6392 : : }
6393 : 21 : gcc_assert (gsi_end_p (psi) && (head == NULL || i == head->length ()));
6394 : 12 : redirect_edge_var_map_clear (re);
6395 : 12 : if (single_pred_p (fin_bb))
6396 : 27 : while (1)
6397 : : {
6398 : 19 : psi = gsi_start_phis (fin_bb);
6399 : 19 : if (gsi_end_p (psi))
6400 : : break;
6401 : 8 : remove_phi_node (&psi, false);
6402 : : }
6403 : :
6404 : : /* Make phi node for trip. */
6405 : 12 : phi = create_phi_node (trip_main, iter_part_bb);
6406 : 12 : add_phi_arg (phi, trip_back, single_succ_edge (trip_update_bb),
6407 : : UNKNOWN_LOCATION);
6408 : 12 : add_phi_arg (phi, trip_init, single_succ_edge (entry_bb),
6409 : : UNKNOWN_LOCATION);
6410 : : }
6411 : :
6412 : 5811 : if (!broken_loop)
6413 : 5554 : set_immediate_dominator (CDI_DOMINATORS, trip_update_bb, cont_bb);
6414 : 5811 : set_immediate_dominator (CDI_DOMINATORS, iter_part_bb,
6415 : : recompute_dominator (CDI_DOMINATORS, iter_part_bb));
6416 : 5811 : set_immediate_dominator (CDI_DOMINATORS, fin_bb,
6417 : : recompute_dominator (CDI_DOMINATORS, fin_bb));
6418 : 5811 : set_immediate_dominator (CDI_DOMINATORS, seq_start_bb,
6419 : : recompute_dominator (CDI_DOMINATORS, seq_start_bb));
6420 : 5811 : set_immediate_dominator (CDI_DOMINATORS, body_bb,
6421 : : recompute_dominator (CDI_DOMINATORS, body_bb));
6422 : :
6423 : 5811 : if (!broken_loop)
6424 : : {
6425 : 5554 : class loop *loop = body_bb->loop_father;
6426 : 5554 : class loop *trip_loop = alloc_loop ();
6427 : 5554 : trip_loop->header = iter_part_bb;
6428 : 5554 : trip_loop->latch = trip_update_bb;
6429 : 5554 : add_loop (trip_loop, iter_part_bb->loop_father);
6430 : :
6431 : 5554 : if (loop != entry_bb->loop_father)
6432 : : {
6433 : 12 : gcc_assert (loop->header == body_bb);
6434 : 12 : gcc_assert (loop->latch == region->cont
6435 : : || single_pred (loop->latch) == region->cont);
6436 : 12 : trip_loop->inner = loop;
6437 : 12 : return;
6438 : : }
6439 : :
6440 : 5542 : if (!gimple_omp_for_combined_p (fd->for_stmt))
6441 : : {
6442 : 1340 : loop = alloc_loop ();
6443 : 1340 : loop->header = body_bb;
6444 : 1340 : if (collapse_bb == NULL)
6445 : 895 : loop->latch = cont_bb;
6446 : 1340 : add_loop (loop, trip_loop);
6447 : : }
6448 : : }
6449 : : }
6450 : :
6451 : : /* A subroutine of expand_omp_for. Generate code for a simd non-worksharing
6452 : : loop. Given parameters:
6453 : :
6454 : : for (V = N1; V cond N2; V += STEP) BODY;
6455 : :
6456 : : where COND is "<" or ">", we generate pseudocode
6457 : :
6458 : : V = N1;
6459 : : goto L1;
6460 : : L0:
6461 : : BODY;
6462 : : V += STEP;
6463 : : L1:
6464 : : if (V cond N2) goto L0; else goto L2;
6465 : : L2:
6466 : :
6467 : : For collapsed loops, emit the outer loops as scalar
6468 : : and only try to vectorize the innermost loop. */
6469 : :
6470 : : static void
6471 : 9361 : expand_omp_simd (struct omp_region *region, struct omp_for_data *fd)
6472 : : {
6473 : 9361 : tree type, t;
6474 : 9361 : basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, l2_bb, l2_dom_bb;
6475 : 9361 : gimple_stmt_iterator gsi;
6476 : 9361 : gimple *stmt;
6477 : 9361 : gcond *cond_stmt;
6478 : 9361 : bool broken_loop = region->cont == NULL;
6479 : 9361 : edge e, ne;
6480 : 9361 : tree *counts = NULL;
6481 : 9361 : int i;
6482 : 9361 : int safelen_int = INT_MAX;
6483 : 9361 : bool dont_vectorize = false;
6484 : 9361 : tree safelen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6485 : : OMP_CLAUSE_SAFELEN);
6486 : 9361 : tree simduid = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6487 : : OMP_CLAUSE__SIMDUID_);
6488 : 9361 : tree ifc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6489 : : OMP_CLAUSE_IF);
6490 : 9361 : tree simdlen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6491 : : OMP_CLAUSE_SIMDLEN);
6492 : 9361 : tree condtemp = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6493 : : OMP_CLAUSE__CONDTEMP_);
6494 : 9361 : tree n1, n2;
6495 : 9438 : tree cond_var = condtemp ? OMP_CLAUSE_DECL (condtemp) : NULL_TREE;
6496 : :
6497 : 9361 : if (safelen)
6498 : : {
6499 : 5349 : poly_uint64 val;
6500 : 5349 : safelen = OMP_CLAUSE_SAFELEN_EXPR (safelen);
6501 : 5349 : if (!poly_int_tree_p (safelen, &val))
6502 : : safelen_int = 0;
6503 : : else
6504 : 5334 : safelen_int = MIN (constant_lower_bound (val), INT_MAX);
6505 : 5334 : if (safelen_int == 1)
6506 : 1778 : safelen_int = 0;
6507 : : }
6508 : 759 : if ((ifc && integer_zerop (OMP_CLAUSE_IF_EXPR (ifc)))
6509 : 9996 : || (simdlen && integer_onep (OMP_CLAUSE_SIMDLEN_EXPR (simdlen))))
6510 : : {
6511 : : safelen_int = 0;
6512 : : dont_vectorize = true;
6513 : : }
6514 : 9361 : type = TREE_TYPE (fd->loop.v);
6515 : 9361 : entry_bb = region->entry;
6516 : 9361 : cont_bb = region->cont;
6517 : 9361 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
6518 : 9361 : gcc_assert (broken_loop
6519 : : || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
6520 : 9361 : l0_bb = FALLTHRU_EDGE (entry_bb)->dest;
6521 : 9361 : if (!broken_loop)
6522 : : {
6523 : 8289 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == l0_bb);
6524 : 8289 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
6525 : 8289 : l1_bb = split_block (cont_bb, last_nondebug_stmt (cont_bb))->dest;
6526 : 8289 : l2_bb = BRANCH_EDGE (entry_bb)->dest;
6527 : : }
6528 : : else
6529 : : {
6530 : 1072 : BRANCH_EDGE (entry_bb)->flags &= ~EDGE_ABNORMAL;
6531 : 1072 : l1_bb = split_edge (BRANCH_EDGE (entry_bb));
6532 : 1072 : l2_bb = single_succ (l1_bb);
6533 : : }
6534 : 9361 : exit_bb = region->exit;
6535 : 9361 : l2_dom_bb = NULL;
6536 : :
6537 : 9361 : gsi = gsi_last_nondebug_bb (entry_bb);
6538 : :
6539 : 9361 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
6540 : : /* Not needed in SSA form right now. */
6541 : 9361 : gcc_assert (!gimple_in_ssa_p (cfun));
6542 : 9361 : if (fd->collapse > 1
6543 : 9361 : && (gimple_omp_for_combined_into_p (fd->for_stmt)
6544 : 494 : || broken_loop))
6545 : : {
6546 : 2263 : int first_zero_iter = -1, dummy = -1;
6547 : 2263 : basic_block zero_iter_bb = l2_bb, dummy_bb = NULL;
6548 : :
6549 : 2263 : counts = XALLOCAVEC (tree, fd->collapse);
6550 : 2263 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
6551 : : zero_iter_bb, first_zero_iter,
6552 : : dummy_bb, dummy, l2_dom_bb);
6553 : : }
6554 : 9361 : if (l2_dom_bb == NULL)
6555 : 9353 : l2_dom_bb = l1_bb;
6556 : :
6557 : 9361 : n1 = fd->loop.n1;
6558 : 9361 : n2 = fd->loop.n2;
6559 : 9361 : if (gimple_omp_for_combined_into_p (fd->for_stmt))
6560 : : {
6561 : 6999 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6562 : : OMP_CLAUSE__LOOPTEMP_);
6563 : 6999 : gcc_assert (innerc);
6564 : 6999 : n1 = OMP_CLAUSE_DECL (innerc);
6565 : 6999 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
6566 : : OMP_CLAUSE__LOOPTEMP_);
6567 : 6999 : gcc_assert (innerc);
6568 : 6999 : n2 = OMP_CLAUSE_DECL (innerc);
6569 : : }
6570 : 9361 : tree step = fd->loop.step;
6571 : 9361 : tree orig_step = step; /* May be different from step if is_simt. */
6572 : :
6573 : 9361 : bool is_simt = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6574 : 9361 : OMP_CLAUSE__SIMT_);
6575 : 9361 : if (is_simt)
6576 : : {
6577 : 0 : cfun->curr_properties &= ~PROP_gimple_lomp_dev;
6578 : 0 : is_simt = safelen_int > 1;
6579 : : }
6580 : 9361 : tree simt_lane = NULL_TREE, simt_maxlane = NULL_TREE;
6581 : 9361 : if (is_simt)
6582 : : {
6583 : 0 : simt_lane = create_tmp_var (unsigned_type_node);
6584 : 0 : gimple *g = gimple_build_call_internal (IFN_GOMP_SIMT_LANE, 0);
6585 : 0 : gimple_call_set_lhs (g, simt_lane);
6586 : 0 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6587 : 0 : tree offset = fold_build2 (MULT_EXPR, TREE_TYPE (step), step,
6588 : : fold_convert (TREE_TYPE (step), simt_lane));
6589 : 0 : n1 = fold_convert (type, n1);
6590 : 0 : if (POINTER_TYPE_P (type))
6591 : 0 : n1 = fold_build_pointer_plus (n1, offset);
6592 : : else
6593 : 0 : n1 = fold_build2 (PLUS_EXPR, type, n1, fold_convert (type, offset));
6594 : :
6595 : : /* Collapsed loops not handled for SIMT yet: limit to one lane only. */
6596 : 0 : if (fd->collapse > 1)
6597 : 0 : simt_maxlane = build_one_cst (unsigned_type_node);
6598 : 0 : else if (safelen_int < omp_max_simt_vf ())
6599 : 0 : simt_maxlane = build_int_cst (unsigned_type_node, safelen_int);
6600 : 0 : tree vf
6601 : 0 : = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_GOMP_SIMT_VF,
6602 : : unsigned_type_node, 0);
6603 : 0 : if (simt_maxlane)
6604 : 0 : vf = fold_build2 (MIN_EXPR, unsigned_type_node, vf, simt_maxlane);
6605 : 0 : vf = fold_convert (TREE_TYPE (step), vf);
6606 : 0 : step = fold_build2 (MULT_EXPR, TREE_TYPE (step), step, vf);
6607 : : }
6608 : :
6609 : 9361 : tree n2var = NULL_TREE;
6610 : 9361 : tree n2v = NULL_TREE;
6611 : 9361 : tree *nonrect_bounds = NULL;
6612 : 9361 : tree min_arg1 = NULL_TREE, min_arg2 = NULL_TREE;
6613 : 9361 : if (fd->collapse > 1)
6614 : : {
6615 : 2737 : if (broken_loop || gimple_omp_for_combined_into_p (fd->for_stmt))
6616 : : {
6617 : 2263 : if (fd->non_rect)
6618 : : {
6619 : 99 : nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
6620 : 99 : memset (nonrect_bounds, 0,
6621 : : sizeof (tree) * (fd->last_nonrect + 1));
6622 : : }
6623 : 2263 : expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
6624 : 2263 : gcc_assert (entry_bb == gsi_bb (gsi));
6625 : 2263 : gcc_assert (fd->for_stmt == gsi_stmt (gsi));
6626 : 2263 : gsi_prev (&gsi);
6627 : 2263 : entry_bb = split_block (entry_bb, gsi_stmt (gsi))->dest;
6628 : 2263 : expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds,
6629 : : NULL, n1);
6630 : 2263 : gsi = gsi_for_stmt (fd->for_stmt);
6631 : : }
6632 : 2737 : if (broken_loop)
6633 : : ;
6634 : 2201 : else if (gimple_omp_for_combined_into_p (fd->for_stmt))
6635 : : {
6636 : : /* Compute in n2var the limit for the first innermost loop,
6637 : : i.e. fd->loop.v + MIN (n2 - fd->loop.v, cnt)
6638 : : where cnt is how many iterations would the loop have if
6639 : : all further iterations were assigned to the current task. */
6640 : 1727 : n2var = create_tmp_var (type);
6641 : 1727 : i = fd->collapse - 1;
6642 : 1727 : tree itype = TREE_TYPE (fd->loops[i].v);
6643 : 1727 : if (POINTER_TYPE_P (itype))
6644 : 266 : itype = signed_type_for (itype);
6645 : 1727 : t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
6646 : 2497 : ? -1 : 1));
6647 : 1727 : t = fold_build2 (PLUS_EXPR, itype,
6648 : : fold_convert (itype, fd->loops[i].step), t);
6649 : 1727 : t = fold_build2 (PLUS_EXPR, itype, t,
6650 : : fold_convert (itype, fd->loops[i].n2));
6651 : 1727 : if (fd->loops[i].m2)
6652 : : {
6653 : 26 : tree t2 = fold_convert (itype,
6654 : : fd->loops[i - fd->loops[i].outer].v);
6655 : 26 : tree t3 = fold_convert (itype, fd->loops[i].m2);
6656 : 26 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
6657 : 26 : t = fold_build2 (PLUS_EXPR, itype, t, t2);
6658 : : }
6659 : 1727 : t = fold_build2 (MINUS_EXPR, itype, t,
6660 : : fold_convert (itype, fd->loops[i].v));
6661 : 1727 : if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
6662 : 256 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
6663 : : fold_build1 (NEGATE_EXPR, itype, t),
6664 : : fold_build1 (NEGATE_EXPR, itype,
6665 : : fold_convert (itype,
6666 : : fd->loops[i].step)));
6667 : : else
6668 : 1471 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
6669 : : fold_convert (itype, fd->loops[i].step));
6670 : 1727 : t = fold_convert (type, t);
6671 : 1727 : tree t2 = fold_build2 (MINUS_EXPR, type, n2, n1);
6672 : 1727 : min_arg1 = create_tmp_var (type);
6673 : 1727 : expand_omp_build_assign (&gsi, min_arg1, t2);
6674 : 1727 : min_arg2 = create_tmp_var (type);
6675 : 1727 : expand_omp_build_assign (&gsi, min_arg2, t);
6676 : : }
6677 : : else
6678 : : {
6679 : 474 : if (TREE_CODE (n2) == INTEGER_CST)
6680 : : {
6681 : : /* Indicate for lastprivate handling that at least one iteration
6682 : : has been performed, without wasting runtime. */
6683 : 136 : if (integer_nonzerop (n2))
6684 : 128 : expand_omp_build_assign (&gsi, fd->loop.v,
6685 : : fold_convert (type, n2));
6686 : : else
6687 : : /* Indicate that no iteration has been performed. */
6688 : 8 : expand_omp_build_assign (&gsi, fd->loop.v,
6689 : : build_one_cst (type));
6690 : : }
6691 : : else
6692 : : {
6693 : 338 : expand_omp_build_assign (&gsi, fd->loop.v,
6694 : : build_zero_cst (type));
6695 : 338 : expand_omp_build_assign (&gsi, n2, build_one_cst (type));
6696 : : }
6697 : 474 : for (i = 0; i < fd->collapse; i++)
6698 : : {
6699 : 474 : t = fold_convert (TREE_TYPE (fd->loops[i].v), fd->loops[i].n1);
6700 : 474 : if (fd->loops[i].m1)
6701 : : {
6702 : 0 : tree t2
6703 : 0 : = fold_convert (TREE_TYPE (t),
6704 : : fd->loops[i - fd->loops[i].outer].v);
6705 : 0 : tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i].m1);
6706 : 0 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
6707 : 0 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
6708 : : }
6709 : 474 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6710 : : /* For normal non-combined collapsed loops just initialize
6711 : : the outermost iterator in the entry_bb. */
6712 : 474 : if (!broken_loop)
6713 : : break;
6714 : : }
6715 : : }
6716 : : }
6717 : : else
6718 : 6624 : expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
6719 : 9361 : tree altv = NULL_TREE, altn2 = NULL_TREE;
6720 : 9361 : if (fd->collapse == 1
6721 : 6624 : && !broken_loop
6722 : 6088 : && TREE_CODE (orig_step) != INTEGER_CST)
6723 : : {
6724 : : /* The vectorizer currently punts on loops with non-constant steps
6725 : : for the main IV (can't compute number of iterations and gives up
6726 : : because of that). As for OpenMP loops it is always possible to
6727 : : compute the number of iterations upfront, use an alternate IV
6728 : : as the loop iterator:
6729 : : altn2 = n1 < n2 ? (n2 - n1 + step - 1) / step : 0;
6730 : : for (i = n1, altv = 0; altv < altn2; altv++, i += step) */
6731 : 176 : altv = create_tmp_var (unsigned_type_for (TREE_TYPE (fd->loops[0].v)));
6732 : 176 : expand_omp_build_assign (&gsi, altv, build_zero_cst (TREE_TYPE (altv)));
6733 : 176 : tree itype = TREE_TYPE (fd->loop.v);
6734 : 176 : if (POINTER_TYPE_P (itype))
6735 : 0 : itype = signed_type_for (itype);
6736 : 334 : t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
6737 : 176 : t = fold_build2 (PLUS_EXPR, itype,
6738 : : fold_convert (itype, step), t);
6739 : 176 : t = fold_build2 (PLUS_EXPR, itype, t, fold_convert (itype, n2));
6740 : 176 : t = fold_build2 (MINUS_EXPR, itype, t,
6741 : : fold_convert (itype, fd->loop.v));
6742 : 176 : if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
6743 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
6744 : : fold_build1 (NEGATE_EXPR, itype, t),
6745 : : fold_build1 (NEGATE_EXPR, itype,
6746 : : fold_convert (itype, step)));
6747 : : else
6748 : 176 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
6749 : : fold_convert (itype, step));
6750 : 176 : t = fold_convert (TREE_TYPE (altv), t);
6751 : 176 : altn2 = create_tmp_var (TREE_TYPE (altv));
6752 : 176 : expand_omp_build_assign (&gsi, altn2, t);
6753 : 176 : tree t2 = fold_convert (TREE_TYPE (fd->loop.v), n2);
6754 : 176 : t2 = fold_build2 (fd->loop.cond_code, boolean_type_node, fd->loop.v, t2);
6755 : 176 : t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
6756 : : true, GSI_SAME_STMT);
6757 : 176 : gassign *g = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
6758 : 176 : build_zero_cst (TREE_TYPE (altv)));
6759 : 176 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6760 : 176 : }
6761 : 9185 : else if (fd->collapse > 1
6762 : 2737 : && !broken_loop
6763 : 2201 : && !gimple_omp_for_combined_into_p (fd->for_stmt)
6764 : 9659 : && TREE_CODE (fd->loops[fd->collapse - 1].step) != INTEGER_CST)
6765 : : {
6766 : 22 : altv = create_tmp_var (unsigned_type_for (TREE_TYPE (fd->loops[0].v)));
6767 : 22 : altn2 = create_tmp_var (TREE_TYPE (altv));
6768 : : }
6769 : 9361 : if (cond_var)
6770 : : {
6771 : 77 : if (POINTER_TYPE_P (type)
6772 : 77 : || TREE_CODE (n1) != INTEGER_CST
6773 : 15 : || fd->loop.cond_code != LT_EXPR
6774 : 92 : || tree_int_cst_sgn (n1) != 1)
6775 : 72 : expand_omp_build_assign (&gsi, cond_var,
6776 : 72 : build_one_cst (TREE_TYPE (cond_var)));
6777 : : else
6778 : 5 : expand_omp_build_assign (&gsi, cond_var,
6779 : 5 : fold_convert (TREE_TYPE (cond_var), n1));
6780 : : }
6781 : :
6782 : : /* Remove the GIMPLE_OMP_FOR statement. */
6783 : 9361 : gsi_remove (&gsi, true);
6784 : :
6785 : 9361 : if (!broken_loop)
6786 : : {
6787 : : /* Code to control the increment goes in the CONT_BB. */
6788 : 8289 : gsi = gsi_last_nondebug_bb (cont_bb);
6789 : 8289 : stmt = gsi_stmt (gsi);
6790 : 8289 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_CONTINUE);
6791 : :
6792 : 8289 : if (fd->collapse == 1
6793 : 8289 : || gimple_omp_for_combined_into_p (fd->for_stmt))
6794 : : {
6795 : 7815 : if (POINTER_TYPE_P (type))
6796 : 286 : t = fold_build_pointer_plus (fd->loop.v, step);
6797 : : else
6798 : 7529 : t = fold_build2 (PLUS_EXPR, type, fd->loop.v, step);
6799 : 7815 : expand_omp_build_assign (&gsi, fd->loop.v, t);
6800 : : }
6801 : 474 : else if (TREE_CODE (n2) != INTEGER_CST)
6802 : 338 : expand_omp_build_assign (&gsi, fd->loop.v, build_one_cst (type));
6803 : 8289 : if (altv)
6804 : : {
6805 : 198 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (altv), altv,
6806 : : build_one_cst (TREE_TYPE (altv)));
6807 : 198 : expand_omp_build_assign (&gsi, altv, t);
6808 : : }
6809 : :
6810 : 8289 : if (fd->collapse > 1)
6811 : : {
6812 : 2201 : i = fd->collapse - 1;
6813 : 2201 : if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
6814 : 280 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
6815 : : else
6816 : : {
6817 : 1921 : t = fold_convert (TREE_TYPE (fd->loops[i].v),
6818 : : fd->loops[i].step);
6819 : 1921 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
6820 : : fd->loops[i].v, t);
6821 : : }
6822 : 2201 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6823 : : }
6824 : 8289 : if (cond_var)
6825 : : {
6826 : 77 : if (POINTER_TYPE_P (type)
6827 : 77 : || TREE_CODE (n1) != INTEGER_CST
6828 : 15 : || fd->loop.cond_code != LT_EXPR
6829 : 92 : || tree_int_cst_sgn (n1) != 1)
6830 : 72 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
6831 : : build_one_cst (TREE_TYPE (cond_var)));
6832 : : else
6833 : 5 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
6834 : : fold_convert (TREE_TYPE (cond_var), step));
6835 : 77 : expand_omp_build_assign (&gsi, cond_var, t);
6836 : : }
6837 : :
6838 : : /* Remove GIMPLE_OMP_CONTINUE. */
6839 : 8289 : gsi_remove (&gsi, true);
6840 : : }
6841 : :
6842 : : /* Emit the condition in L1_BB. */
6843 : 9361 : gsi = gsi_start_bb (l1_bb);
6844 : :
6845 : 9361 : if (altv)
6846 : 198 : t = build2 (LT_EXPR, boolean_type_node, altv, altn2);
6847 : 9163 : else if (fd->collapse > 1
6848 : 2715 : && !gimple_omp_for_combined_into_p (fd->for_stmt)
6849 : 9635 : && !broken_loop)
6850 : : {
6851 : 452 : i = fd->collapse - 1;
6852 : 452 : tree itype = TREE_TYPE (fd->loops[i].v);
6853 : 452 : if (fd->loops[i].m2)
6854 : 203 : t = n2v = create_tmp_var (itype);
6855 : : else
6856 : 249 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
6857 : 452 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6858 : : false, GSI_CONTINUE_LINKING);
6859 : 452 : tree v = fd->loops[i].v;
6860 : 452 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
6861 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
6862 : : false, GSI_CONTINUE_LINKING);
6863 : 452 : t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
6864 : : }
6865 : : else
6866 : : {
6867 : 8711 : if (fd->collapse > 1 && !broken_loop)
6868 : : t = n2var;
6869 : : else
6870 : 6984 : t = fold_convert (type, unshare_expr (n2));
6871 : 8711 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6872 : : false, GSI_CONTINUE_LINKING);
6873 : 8711 : tree v = fd->loop.v;
6874 : 8711 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
6875 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
6876 : : false, GSI_CONTINUE_LINKING);
6877 : 8711 : t = build2 (fd->loop.cond_code, boolean_type_node, v, t);
6878 : : }
6879 : 9361 : cond_stmt = gimple_build_cond_empty (t);
6880 : 9361 : gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
6881 : 9361 : if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
6882 : : NULL, NULL)
6883 : 9361 : || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p,
6884 : : NULL, NULL))
6885 : : {
6886 : 0 : gsi = gsi_for_stmt (cond_stmt);
6887 : 0 : gimple_regimplify_operands (cond_stmt, &gsi);
6888 : : }
6889 : :
6890 : : /* Add 'V -= STEP * (SIMT_VF - 1)' after the loop. */
6891 : 9361 : if (is_simt)
6892 : : {
6893 : 0 : gsi = gsi_start_bb (l2_bb);
6894 : 0 : step = fold_build2 (MINUS_EXPR, TREE_TYPE (step), orig_step, step);
6895 : 0 : if (POINTER_TYPE_P (type))
6896 : 0 : t = fold_build_pointer_plus (fd->loop.v, step);
6897 : : else
6898 : 0 : t = fold_build2 (PLUS_EXPR, type, fd->loop.v, step);
6899 : 0 : expand_omp_build_assign (&gsi, fd->loop.v, t);
6900 : : }
6901 : :
6902 : : /* Remove GIMPLE_OMP_RETURN. */
6903 : 9361 : gsi = gsi_last_nondebug_bb (exit_bb);
6904 : 9361 : gsi_remove (&gsi, true);
6905 : :
6906 : : /* Connect the new blocks. */
6907 : 9361 : remove_edge (FALLTHRU_EDGE (entry_bb));
6908 : :
6909 : 9361 : if (!broken_loop)
6910 : : {
6911 : 8289 : remove_edge (BRANCH_EDGE (entry_bb));
6912 : 8289 : make_edge (entry_bb, l1_bb, EDGE_FALLTHRU);
6913 : :
6914 : 8289 : e = BRANCH_EDGE (l1_bb);
6915 : 8289 : ne = FALLTHRU_EDGE (l1_bb);
6916 : 8289 : e->flags = EDGE_TRUE_VALUE;
6917 : : }
6918 : : else
6919 : : {
6920 : 1072 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
6921 : :
6922 : 1072 : ne = single_succ_edge (l1_bb);
6923 : 1072 : e = make_edge (l1_bb, l0_bb, EDGE_TRUE_VALUE);
6924 : :
6925 : : }
6926 : 9361 : ne->flags = EDGE_FALSE_VALUE;
6927 : 9361 : e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
6928 : 9361 : ne->probability = e->probability.invert ();
6929 : :
6930 : 9361 : set_immediate_dominator (CDI_DOMINATORS, l1_bb, entry_bb);
6931 : 9361 : set_immediate_dominator (CDI_DOMINATORS, l0_bb, l1_bb);
6932 : :
6933 : 9361 : if (simt_maxlane)
6934 : : {
6935 : 0 : cond_stmt = gimple_build_cond (LT_EXPR, simt_lane, simt_maxlane,
6936 : : NULL_TREE, NULL_TREE);
6937 : 0 : gsi = gsi_last_bb (entry_bb);
6938 : 0 : gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
6939 : 0 : make_edge (entry_bb, l2_bb, EDGE_FALSE_VALUE);
6940 : 0 : FALLTHRU_EDGE (entry_bb)->flags = EDGE_TRUE_VALUE;
6941 : 0 : FALLTHRU_EDGE (entry_bb)->probability
6942 : 0 : = profile_probability::guessed_always ().apply_scale (7, 8);
6943 : 0 : BRANCH_EDGE (entry_bb)->probability
6944 : 0 : = FALLTHRU_EDGE (entry_bb)->probability.invert ();
6945 : 0 : l2_dom_bb = entry_bb;
6946 : : }
6947 : 9361 : set_immediate_dominator (CDI_DOMINATORS, l2_bb, l2_dom_bb);
6948 : :
6949 : 9361 : if (!broken_loop && fd->collapse > 1)
6950 : : {
6951 : 2201 : basic_block last_bb = l1_bb;
6952 : 2201 : basic_block init_bb = NULL;
6953 : 6075 : for (i = fd->collapse - 2; i >= 0; i--)
6954 : : {
6955 : 3874 : tree nextn2v = NULL_TREE;
6956 : 3874 : if (EDGE_SUCC (last_bb, 0)->flags & EDGE_FALSE_VALUE)
6957 : : e = EDGE_SUCC (last_bb, 0);
6958 : : else
6959 : 2201 : e = EDGE_SUCC (last_bb, 1);
6960 : 3874 : basic_block bb = split_edge (e);
6961 : 3874 : if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
6962 : 553 : t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
6963 : : else
6964 : : {
6965 : 3321 : t = fold_convert (TREE_TYPE (fd->loops[i].v),
6966 : : fd->loops[i].step);
6967 : 3321 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
6968 : : fd->loops[i].v, t);
6969 : : }
6970 : 3874 : gsi = gsi_after_labels (bb);
6971 : 3874 : expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6972 : :
6973 : 3874 : bb = split_block (bb, last_nondebug_stmt (bb))->dest;
6974 : 3874 : gsi = gsi_start_bb (bb);
6975 : 3874 : tree itype = TREE_TYPE (fd->loops[i].v);
6976 : 3874 : if (fd->loops[i].m2)
6977 : 0 : t = nextn2v = create_tmp_var (itype);
6978 : : else
6979 : 3874 : t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
6980 : 3874 : t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6981 : : false, GSI_CONTINUE_LINKING);
6982 : 3874 : tree v = fd->loops[i].v;
6983 : 3874 : if (DECL_P (v) && TREE_ADDRESSABLE (v))
6984 : 0 : v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
6985 : : false, GSI_CONTINUE_LINKING);
6986 : 3874 : t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
6987 : 3874 : cond_stmt = gimple_build_cond_empty (t);
6988 : 3874 : gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
6989 : 3874 : if (walk_tree (gimple_cond_lhs_ptr (cond_stmt),
6990 : : expand_omp_regimplify_p, NULL, NULL)
6991 : 3874 : || walk_tree (gimple_cond_rhs_ptr (cond_stmt),
6992 : : expand_omp_regimplify_p, NULL, NULL))
6993 : : {
6994 : 6 : gsi = gsi_for_stmt (cond_stmt);
6995 : 6 : gimple_regimplify_operands (cond_stmt, &gsi);
6996 : : }
6997 : 3874 : ne = single_succ_edge (bb);
6998 : 3874 : ne->flags = EDGE_FALSE_VALUE;
6999 : :
7000 : 3874 : init_bb = create_empty_bb (bb);
7001 : 3874 : set_immediate_dominator (CDI_DOMINATORS, init_bb, bb);
7002 : 3874 : add_bb_to_loop (init_bb, bb->loop_father);
7003 : 3874 : e = make_edge (bb, init_bb, EDGE_TRUE_VALUE);
7004 : 3874 : e->probability
7005 : 3874 : = profile_probability::guessed_always ().apply_scale (7, 8);
7006 : 3874 : ne->probability = e->probability.invert ();
7007 : :
7008 : 3874 : gsi = gsi_after_labels (init_bb);
7009 : 3874 : if (fd->loops[i + 1].m1)
7010 : : {
7011 : 242 : tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7012 : : fd->loops[i + 1
7013 : : - fd->loops[i + 1].outer].v);
7014 : 242 : if (POINTER_TYPE_P (TREE_TYPE (t2)))
7015 : 6 : t = fold_build_pointer_plus (t2, fd->loops[i + 1].n1);
7016 : : else
7017 : : {
7018 : 236 : t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7019 : : fd->loops[i + 1].n1);
7020 : 236 : tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m1);
7021 : 236 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
7022 : 236 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
7023 : : }
7024 : : }
7025 : : else
7026 : 3632 : t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7027 : : fd->loops[i + 1].n1);
7028 : 3874 : expand_omp_build_assign (&gsi, fd->loops[i + 1].v, t);
7029 : 3874 : if (fd->loops[i + 1].m2)
7030 : : {
7031 : 241 : if (i + 2 == fd->collapse && (n2var || altv))
7032 : : {
7033 : 38 : gcc_assert (n2v == NULL_TREE);
7034 : 38 : n2v = create_tmp_var (TREE_TYPE (fd->loops[i + 1].v));
7035 : : }
7036 : 241 : tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7037 : : fd->loops[i + 1
7038 : : - fd->loops[i + 1].outer].v);
7039 : 241 : if (POINTER_TYPE_P (TREE_TYPE (t2)))
7040 : 6 : t = fold_build_pointer_plus (t2, fd->loops[i + 1].n2);
7041 : : else
7042 : : {
7043 : 235 : t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7044 : : fd->loops[i + 1].n2);
7045 : 235 : tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m2);
7046 : 235 : t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
7047 : 235 : t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
7048 : : }
7049 : 241 : expand_omp_build_assign (&gsi, n2v, t);
7050 : : }
7051 : 3874 : if (i + 2 == fd->collapse && n2var)
7052 : : {
7053 : : /* For composite simd, n2 is the first iteration the current
7054 : : task shouldn't already handle, so we effectively want to use
7055 : : for (V3 = N31; V < N2 && V3 < N32; V++, V3 += STEP3)
7056 : : as the vectorized loop. Except the vectorizer will not
7057 : : vectorize that, so instead compute N2VAR as
7058 : : N2VAR = V + MIN (N2 - V, COUNTS3) and use
7059 : : for (V3 = N31; V < N2VAR; V++, V3 += STEP3)
7060 : : as the loop to vectorize. */
7061 : 1727 : tree t2 = fold_build2 (MINUS_EXPR, type, n2, fd->loop.v);
7062 : 1727 : if (fd->loops[i + 1].m1 || fd->loops[i + 1].m2)
7063 : : {
7064 : 99 : tree itype = TREE_TYPE (fd->loops[i].v);
7065 : 99 : if (POINTER_TYPE_P (itype))
7066 : 4 : itype = signed_type_for (itype);
7067 : 99 : t = build_int_cst (itype, (fd->loops[i + 1].cond_code
7068 : 99 : == LT_EXPR ? -1 : 1));
7069 : 99 : t = fold_build2 (PLUS_EXPR, itype,
7070 : : fold_convert (itype,
7071 : : fd->loops[i + 1].step), t);
7072 : 99 : if (fd->loops[i + 1].m2 == NULL_TREE)
7073 : 73 : t = fold_build2 (PLUS_EXPR, itype, t,
7074 : : fold_convert (itype,
7075 : : fd->loops[i + 1].n2));
7076 : 26 : else if (POINTER_TYPE_P (TREE_TYPE (n2v)))
7077 : : {
7078 : 4 : t = fold_build_pointer_plus (n2v, t);
7079 : 4 : t = fold_convert (itype, t);
7080 : : }
7081 : : else
7082 : 22 : t = fold_build2 (PLUS_EXPR, itype, t, n2v);
7083 : 99 : t = fold_build2 (MINUS_EXPR, itype, t,
7084 : : fold_convert (itype, fd->loops[i + 1].v));
7085 : 99 : tree step = fold_convert (itype, fd->loops[i + 1].step);
7086 : 99 : if (TYPE_UNSIGNED (itype)
7087 : 99 : && fd->loops[i + 1].cond_code == GT_EXPR)
7088 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
7089 : : fold_build1 (NEGATE_EXPR, itype, t),
7090 : : fold_build1 (NEGATE_EXPR, itype, step));
7091 : : else
7092 : 99 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
7093 : 99 : t = fold_convert (type, t);
7094 : 99 : }
7095 : : else
7096 : 1628 : t = counts[i + 1];
7097 : 1727 : expand_omp_build_assign (&gsi, min_arg1, t2);
7098 : 1727 : expand_omp_build_assign (&gsi, min_arg2, t);
7099 : 1727 : e = split_block (init_bb, last_nondebug_stmt (init_bb));
7100 : 1727 : gsi = gsi_after_labels (e->dest);
7101 : 1727 : init_bb = e->dest;
7102 : 1727 : remove_edge (FALLTHRU_EDGE (entry_bb));
7103 : 1727 : make_edge (entry_bb, init_bb, EDGE_FALLTHRU);
7104 : 1727 : set_immediate_dominator (CDI_DOMINATORS, init_bb, entry_bb);
7105 : 1727 : set_immediate_dominator (CDI_DOMINATORS, l1_bb, init_bb);
7106 : 1727 : t = fold_build2 (MIN_EXPR, type, min_arg1, min_arg2);
7107 : 1727 : t = fold_build2 (PLUS_EXPR, type, fd->loop.v, t);
7108 : 1727 : expand_omp_build_assign (&gsi, n2var, t);
7109 : : }
7110 : 3874 : if (i + 2 == fd->collapse && altv)
7111 : : {
7112 : : /* The vectorizer currently punts on loops with non-constant
7113 : : steps for the main IV (can't compute number of iterations
7114 : : and gives up because of that). As for OpenMP loops it is
7115 : : always possible to compute the number of iterations upfront,
7116 : : use an alternate IV as the loop iterator. */
7117 : 22 : expand_omp_build_assign (&gsi, altv,
7118 : 22 : build_zero_cst (TREE_TYPE (altv)));
7119 : 22 : tree itype = TREE_TYPE (fd->loops[i + 1].v);
7120 : 22 : if (POINTER_TYPE_P (itype))
7121 : 0 : itype = signed_type_for (itype);
7122 : 22 : t = build_int_cst (itype, (fd->loops[i + 1].cond_code == LT_EXPR
7123 : 26 : ? -1 : 1));
7124 : 22 : t = fold_build2 (PLUS_EXPR, itype,
7125 : : fold_convert (itype, fd->loops[i + 1].step), t);
7126 : 22 : t = fold_build2 (PLUS_EXPR, itype, t,
7127 : : fold_convert (itype,
7128 : : fd->loops[i + 1].m2
7129 : : ? n2v : fd->loops[i + 1].n2));
7130 : 22 : t = fold_build2 (MINUS_EXPR, itype, t,
7131 : : fold_convert (itype, fd->loops[i + 1].v));
7132 : 22 : tree step = fold_convert (itype, fd->loops[i + 1].step);
7133 : 22 : if (TYPE_UNSIGNED (itype)
7134 : 22 : && fd->loops[i + 1].cond_code == GT_EXPR)
7135 : 0 : t = fold_build2 (TRUNC_DIV_EXPR, itype,
7136 : : fold_build1 (NEGATE_EXPR, itype, t),
7137 : : fold_build1 (NEGATE_EXPR, itype, step));
7138 : : else
7139 : 22 : t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
7140 : 22 : t = fold_convert (TREE_TYPE (altv), t);
7141 : 22 : expand_omp_build_assign (&gsi, altn2, t);
7142 : 22 : tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7143 : : fd->loops[i + 1].m2
7144 : : ? n2v : fd->loops[i + 1].n2);
7145 : 22 : t2 = fold_build2 (fd->loops[i + 1].cond_code, boolean_type_node,
7146 : : fd->loops[i + 1].v, t2);
7147 : 22 : t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
7148 : : true, GSI_SAME_STMT);
7149 : 22 : gassign *g
7150 : 22 : = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
7151 : 22 : build_zero_cst (TREE_TYPE (altv)));
7152 : 22 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
7153 : : }
7154 : 3874 : n2v = nextn2v;
7155 : :
7156 : 3874 : make_edge (init_bb, last_bb, EDGE_FALLTHRU);
7157 : 3874 : if (!gimple_omp_for_combined_into_p (fd->for_stmt))
7158 : : {
7159 : 785 : e = find_edge (entry_bb, last_bb);
7160 : 785 : redirect_edge_succ (e, bb);
7161 : 785 : set_immediate_dominator (CDI_DOMINATORS, bb, entry_bb);
7162 : 785 : set_immediate_dominator (CDI_DOMINATORS, last_bb, init_bb);
7163 : : }
7164 : :
7165 : 3874 : last_bb = bb;
7166 : : }
7167 : : }
7168 : 9361 : if (!broken_loop)
7169 : : {
7170 : 8289 : class loop *loop = alloc_loop ();
7171 : 8289 : loop->header = l1_bb;
7172 : 8289 : loop->latch = cont_bb;
7173 : 8289 : add_loop (loop, l1_bb->loop_father);
7174 : 8289 : loop->safelen = safelen_int;
7175 : 8289 : if (simduid)
7176 : : {
7177 : 3480 : loop->simduid = OMP_CLAUSE__SIMDUID__DECL (simduid);
7178 : 3480 : cfun->has_simduid_loops = true;
7179 : : }
7180 : : /* If not -fno-tree-loop-vectorize, hint that we want to vectorize
7181 : : the loop. */
7182 : 8289 : if ((flag_tree_loop_vectorize
7183 : 3279 : || !OPTION_SET_P (flag_tree_loop_vectorize))
7184 : 8289 : && flag_tree_loop_optimize
7185 : 8288 : && loop->safelen > 1)
7186 : : {
7187 : 6510 : loop->force_vectorize = true;
7188 : 6510 : if (simdlen && tree_fits_uhwi_p (OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
7189 : : {
7190 : 60 : unsigned HOST_WIDE_INT v
7191 : 60 : = tree_to_uhwi (OMP_CLAUSE_SIMDLEN_EXPR (simdlen));
7192 : 60 : if (v < INT_MAX && v <= (unsigned HOST_WIDE_INT) loop->safelen)
7193 : 60 : loop->simdlen = v;
7194 : : }
7195 : 6510 : cfun->has_force_vectorize_loops = true;
7196 : 6510 : }
7197 : 1779 : else if (dont_vectorize)
7198 : 234 : loop->dont_vectorize = true;
7199 : : }
7200 : 1072 : else if (simduid)
7201 : 532 : cfun->has_simduid_loops = true;
7202 : 9361 : }
7203 : :
7204 : : /* Taskloop construct is represented after gimplification with
7205 : : two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
7206 : : in between them. This routine expands the outer GIMPLE_OMP_FOR,
7207 : : which should just compute all the needed loop temporaries
7208 : : for GIMPLE_OMP_TASK. */
7209 : :
7210 : : static void
7211 : 1343 : expand_omp_taskloop_for_outer (struct omp_region *region,
7212 : : struct omp_for_data *fd,
7213 : : gimple *inner_stmt)
7214 : : {
7215 : 1343 : tree type, bias = NULL_TREE;
7216 : 1343 : basic_block entry_bb, cont_bb, exit_bb;
7217 : 1343 : gimple_stmt_iterator gsi;
7218 : 1343 : gassign *assign_stmt;
7219 : 1343 : tree *counts = NULL;
7220 : 1343 : int i;
7221 : :
7222 : 1343 : gcc_assert (inner_stmt);
7223 : 1343 : gcc_assert (region->cont);
7224 : 1343 : gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_TASK
7225 : : && gimple_omp_task_taskloop_p (inner_stmt));
7226 : 1343 : type = TREE_TYPE (fd->loop.v);
7227 : :
7228 : : /* See if we need to bias by LLONG_MIN. */
7229 : 1343 : if (fd->iter_type == long_long_unsigned_type_node
7230 : 42 : && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
7231 : 1371 : && !TYPE_UNSIGNED (type))
7232 : : {
7233 : 0 : tree n1, n2;
7234 : :
7235 : 0 : if (fd->loop.cond_code == LT_EXPR)
7236 : : {
7237 : 0 : n1 = fd->loop.n1;
7238 : 0 : n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
7239 : : }
7240 : : else
7241 : : {
7242 : 0 : n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
7243 : 0 : n2 = fd->loop.n1;
7244 : : }
7245 : 0 : if (TREE_CODE (n1) != INTEGER_CST
7246 : 0 : || TREE_CODE (n2) != INTEGER_CST
7247 : 0 : || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
7248 : 0 : bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
7249 : : }
7250 : :
7251 : 1343 : entry_bb = region->entry;
7252 : 1343 : cont_bb = region->cont;
7253 : 1343 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
7254 : 1343 : gcc_assert (BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
7255 : 1343 : exit_bb = region->exit;
7256 : :
7257 : 1343 : gsi = gsi_last_nondebug_bb (entry_bb);
7258 : 1343 : gimple *for_stmt = gsi_stmt (gsi);
7259 : 1343 : gcc_assert (gimple_code (for_stmt) == GIMPLE_OMP_FOR);
7260 : 1343 : if (fd->collapse > 1)
7261 : : {
7262 : 181 : int first_zero_iter = -1, dummy = -1;
7263 : 181 : basic_block zero_iter_bb = NULL, dummy_bb = NULL, l2_dom_bb = NULL;
7264 : :
7265 : 181 : counts = XALLOCAVEC (tree, fd->collapse);
7266 : 181 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
7267 : : zero_iter_bb, first_zero_iter,
7268 : : dummy_bb, dummy, l2_dom_bb);
7269 : :
7270 : 181 : if (zero_iter_bb)
7271 : : {
7272 : : /* Some counts[i] vars might be uninitialized if
7273 : : some loop has zero iterations. But the body shouldn't
7274 : : be executed in that case, so just avoid uninit warnings. */
7275 : 336 : for (i = first_zero_iter; i < fd->collapse; i++)
7276 : 239 : if (SSA_VAR_P (counts[i]))
7277 : 226 : suppress_warning (counts[i], OPT_Wuninitialized);
7278 : 97 : gsi_prev (&gsi);
7279 : 97 : edge e = split_block (entry_bb, gsi_stmt (gsi));
7280 : 97 : entry_bb = e->dest;
7281 : 97 : make_edge (zero_iter_bb, entry_bb, EDGE_FALLTHRU);
7282 : 97 : gsi = gsi_last_bb (entry_bb);
7283 : 97 : set_immediate_dominator (CDI_DOMINATORS, entry_bb,
7284 : : get_immediate_dominator (CDI_DOMINATORS,
7285 : : zero_iter_bb));
7286 : : }
7287 : : }
7288 : :
7289 : 1343 : tree t0, t1;
7290 : 1343 : t1 = fd->loop.n2;
7291 : 1343 : t0 = fd->loop.n1;
7292 : 2672 : if (POINTER_TYPE_P (TREE_TYPE (t0))
7293 : 1343 : && TYPE_PRECISION (TREE_TYPE (t0))
7294 : 14 : != TYPE_PRECISION (fd->iter_type))
7295 : : {
7296 : : /* Avoid casting pointers to integer of a different size. */
7297 : 0 : tree itype = signed_type_for (type);
7298 : 0 : t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
7299 : 0 : t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
7300 : : }
7301 : : else
7302 : : {
7303 : 1343 : t1 = fold_convert (fd->iter_type, t1);
7304 : 1343 : t0 = fold_convert (fd->iter_type, t0);
7305 : : }
7306 : 1343 : if (bias)
7307 : : {
7308 : 0 : t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
7309 : 0 : t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
7310 : : }
7311 : :
7312 : 1343 : tree innerc = omp_find_clause (gimple_omp_task_clauses (inner_stmt),
7313 : : OMP_CLAUSE__LOOPTEMP_);
7314 : 1343 : gcc_assert (innerc);
7315 : 1343 : tree startvar = OMP_CLAUSE_DECL (innerc);
7316 : 1343 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
7317 : 1343 : gcc_assert (innerc);
7318 : 1343 : tree endvar = OMP_CLAUSE_DECL (innerc);
7319 : 1343 : if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST)
7320 : : {
7321 : 101 : innerc = find_lastprivate_looptemp (fd, innerc);
7322 : 101 : if (innerc)
7323 : : {
7324 : : /* If needed (inner taskloop has lastprivate clause), propagate
7325 : : down the total number of iterations. */
7326 : 31 : tree t = force_gimple_operand_gsi (&gsi, fd->loop.n2, false,
7327 : : NULL_TREE, false,
7328 : : GSI_CONTINUE_LINKING);
7329 : 31 : assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
7330 : 31 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7331 : : }
7332 : : }
7333 : :
7334 : 1343 : t0 = force_gimple_operand_gsi (&gsi, t0, false, NULL_TREE, false,
7335 : : GSI_CONTINUE_LINKING);
7336 : 1343 : assign_stmt = gimple_build_assign (startvar, t0);
7337 : 1343 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7338 : :
7339 : 1343 : t1 = force_gimple_operand_gsi (&gsi, t1, false, NULL_TREE, false,
7340 : : GSI_CONTINUE_LINKING);
7341 : 1343 : assign_stmt = gimple_build_assign (endvar, t1);
7342 : 1343 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7343 : 1343 : if (fd->collapse > 1)
7344 : 181 : expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
7345 : :
7346 : : /* Remove the GIMPLE_OMP_FOR statement. */
7347 : 1343 : gsi = gsi_for_stmt (for_stmt);
7348 : 1343 : gsi_remove (&gsi, true);
7349 : :
7350 : 1343 : gsi = gsi_last_nondebug_bb (cont_bb);
7351 : 1343 : gsi_remove (&gsi, true);
7352 : :
7353 : 1343 : gsi = gsi_last_nondebug_bb (exit_bb);
7354 : 1343 : gsi_remove (&gsi, true);
7355 : :
7356 : 1343 : FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
7357 : 1343 : remove_edge (BRANCH_EDGE (entry_bb));
7358 : 1343 : FALLTHRU_EDGE (cont_bb)->probability = profile_probability::always ();
7359 : 1343 : remove_edge (BRANCH_EDGE (cont_bb));
7360 : 1343 : set_immediate_dominator (CDI_DOMINATORS, exit_bb, cont_bb);
7361 : 1343 : set_immediate_dominator (CDI_DOMINATORS, region->entry,
7362 : : recompute_dominator (CDI_DOMINATORS, region->entry));
7363 : 1343 : }
7364 : :
7365 : : /* Taskloop construct is represented after gimplification with
7366 : : two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
7367 : : in between them. This routine expands the inner GIMPLE_OMP_FOR.
7368 : : GOMP_taskloop{,_ull} function arranges for each task to be given just
7369 : : a single range of iterations. */
7370 : :
7371 : : static void
7372 : 1343 : expand_omp_taskloop_for_inner (struct omp_region *region,
7373 : : struct omp_for_data *fd,
7374 : : gimple *inner_stmt)
7375 : : {
7376 : 1343 : tree e, t, type, itype, vmain, vback, bias = NULL_TREE;
7377 : 1343 : basic_block entry_bb, exit_bb, body_bb, cont_bb, collapse_bb = NULL;
7378 : 1343 : basic_block fin_bb;
7379 : 1343 : gimple_stmt_iterator gsi;
7380 : 1343 : edge ep;
7381 : 1343 : bool broken_loop = region->cont == NULL;
7382 : 1343 : tree *counts = NULL;
7383 : 1343 : tree n1, n2, step;
7384 : :
7385 : 1343 : itype = type = TREE_TYPE (fd->loop.v);
7386 : 1343 : if (POINTER_TYPE_P (type))
7387 : 14 : itype = signed_type_for (type);
7388 : :
7389 : : /* See if we need to bias by LLONG_MIN. */
7390 : 1343 : if (fd->iter_type == long_long_unsigned_type_node
7391 : 42 : && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
7392 : 1371 : && !TYPE_UNSIGNED (type))
7393 : : {
7394 : 0 : tree n1, n2;
7395 : :
7396 : 0 : if (fd->loop.cond_code == LT_EXPR)
7397 : : {
7398 : 0 : n1 = fd->loop.n1;
7399 : 0 : n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
7400 : : }
7401 : : else
7402 : : {
7403 : 0 : n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
7404 : 0 : n2 = fd->loop.n1;
7405 : : }
7406 : 0 : if (TREE_CODE (n1) != INTEGER_CST
7407 : 0 : || TREE_CODE (n2) != INTEGER_CST
7408 : 0 : || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
7409 : 0 : bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
7410 : : }
7411 : :
7412 : 1343 : entry_bb = region->entry;
7413 : 1343 : cont_bb = region->cont;
7414 : 1343 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
7415 : 1343 : fin_bb = BRANCH_EDGE (entry_bb)->dest;
7416 : 1343 : gcc_assert (broken_loop
7417 : : || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
7418 : 1343 : body_bb = FALLTHRU_EDGE (entry_bb)->dest;
7419 : 1343 : if (!broken_loop)
7420 : : {
7421 : 1327 : gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb);
7422 : 1327 : gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
7423 : : }
7424 : 1343 : exit_bb = region->exit;
7425 : :
7426 : : /* Iteration space partitioning goes in ENTRY_BB. */
7427 : 1343 : gsi = gsi_last_nondebug_bb (entry_bb);
7428 : 1343 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
7429 : :
7430 : 1343 : if (fd->collapse > 1)
7431 : : {
7432 : 181 : int first_zero_iter = -1, dummy = -1;
7433 : 181 : basic_block l2_dom_bb = NULL, dummy_bb = NULL;
7434 : :
7435 : 181 : counts = XALLOCAVEC (tree, fd->collapse);
7436 : 181 : expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
7437 : : fin_bb, first_zero_iter,
7438 : : dummy_bb, dummy, l2_dom_bb);
7439 : 181 : t = NULL_TREE;
7440 : : }
7441 : : else
7442 : 1343 : t = integer_one_node;
7443 : :
7444 : 1343 : step = fd->loop.step;
7445 : 1343 : tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
7446 : : OMP_CLAUSE__LOOPTEMP_);
7447 : 1343 : gcc_assert (innerc);
7448 : 1343 : n1 = OMP_CLAUSE_DECL (innerc);
7449 : 1343 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
7450 : 1343 : gcc_assert (innerc);
7451 : 1343 : n2 = OMP_CLAUSE_DECL (innerc);
7452 : 1343 : if (bias)
7453 : : {
7454 : 0 : n1 = fold_build2 (PLUS_EXPR, fd->iter_type, n1, bias);
7455 : 0 : n2 = fold_build2 (PLUS_EXPR, fd->iter_type, n2, bias);
7456 : : }
7457 : 1343 : n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
7458 : : true, NULL_TREE, true, GSI_SAME_STMT);
7459 : 1343 : n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
7460 : : true, NULL_TREE, true, GSI_SAME_STMT);
7461 : 1343 : step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
7462 : : true, NULL_TREE, true, GSI_SAME_STMT);
7463 : :
7464 : 1343 : tree startvar = fd->loop.v;
7465 : 1343 : tree endvar = NULL_TREE;
7466 : :
7467 : 1343 : if (gimple_omp_for_combined_p (fd->for_stmt))
7468 : : {
7469 : 617 : tree clauses = gimple_omp_for_clauses (inner_stmt);
7470 : 617 : tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
7471 : 617 : gcc_assert (innerc);
7472 : 617 : startvar = OMP_CLAUSE_DECL (innerc);
7473 : 617 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
7474 : : OMP_CLAUSE__LOOPTEMP_);
7475 : 617 : gcc_assert (innerc);
7476 : 617 : endvar = OMP_CLAUSE_DECL (innerc);
7477 : : }
7478 : 1343 : t = fold_convert (TREE_TYPE (startvar), n1);
7479 : 1343 : t = force_gimple_operand_gsi (&gsi, t,
7480 : 1343 : DECL_P (startvar)
7481 : 1343 : && TREE_ADDRESSABLE (startvar),
7482 : : NULL_TREE, false, GSI_CONTINUE_LINKING);
7483 : 1343 : gimple *assign_stmt = gimple_build_assign (startvar, t);
7484 : 1343 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7485 : :
7486 : 1343 : t = fold_convert (TREE_TYPE (startvar), n2);
7487 : 1343 : e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
7488 : : false, GSI_CONTINUE_LINKING);
7489 : 1343 : if (endvar)
7490 : : {
7491 : 617 : assign_stmt = gimple_build_assign (endvar, e);
7492 : 617 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7493 : 617 : if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
7494 : 529 : assign_stmt = gimple_build_assign (fd->loop.v, e);
7495 : : else
7496 : 88 : assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
7497 : 617 : gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7498 : : }
7499 : :
7500 : 1343 : tree *nonrect_bounds = NULL;
7501 : 1343 : if (fd->collapse > 1)
7502 : : {
7503 : 181 : if (fd->non_rect)
7504 : : {
7505 : 19 : nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
7506 : 19 : memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
7507 : : }
7508 : 181 : gcc_assert (gsi_bb (gsi) == entry_bb);
7509 : 181 : expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
7510 : : startvar);
7511 : 181 : entry_bb = gsi_bb (gsi);
7512 : : }
7513 : :
7514 : 1343 : if (!broken_loop)
7515 : : {
7516 : : /* The code controlling the sequential loop replaces the
7517 : : GIMPLE_OMP_CONTINUE. */
7518 : 1327 : gsi = gsi_last_nondebug_bb (cont_bb);
7519 : 1327 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
7520 : 1327 : gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
7521 : 1327 : vmain = gimple_omp_continue_control_use (cont_stmt);
7522 : 1327 : vback = gimple_omp_continue_control_def (cont_stmt);
7523 : :
7524 : 1327 : if (!gimple_omp_for_combined_p (fd->for_stmt))
7525 : : {
7526 : 710 : if (POINTER_TYPE_P (type))
7527 : 8 : t = fold_build_pointer_plus (vmain, step);
7528 : : else
7529 : 702 : t = fold_build2 (PLUS_EXPR, type, vmain, step);
7530 : 710 : t = force_gimple_operand_gsi (&gsi, t,
7531 : 710 : DECL_P (vback)
7532 : 710 : && TREE_ADDRESSABLE (vback),
7533 : : NULL_TREE, true, GSI_SAME_STMT);
7534 : 710 : assign_stmt = gimple_build_assign (vback, t);
7535 : 710 : gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
7536 : :
7537 : 710 : t = build2 (fd->loop.cond_code, boolean_type_node,
7538 : 710 : DECL_P (vback) && TREE_ADDRESSABLE (vback)
7539 : : ? t : vback, e);
7540 : 710 : gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
7541 : : }
7542 : :
7543 : : /* Remove the GIMPLE_OMP_CONTINUE statement. */
7544 : 1327 : gsi_remove (&gsi, true);
7545 : :
7546 : 1327 : if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
7547 : 81 : collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
7548 : : cont_bb, body_bb);
7549 : : }
7550 : :
7551 : : /* Remove the GIMPLE_OMP_FOR statement. */
7552 : 1343 : gsi = gsi_for_stmt (fd->for_stmt);
7553 : 1343 : gsi_remove (&gsi, true);
7554 : :
7555 : : /* Remove the GIMPLE_OMP_RETURN statement. */
7556 : 1343 : gsi = gsi_last_nondebug_bb (exit_bb);
7557 : 1343 : gsi_remove (&gsi, true);
7558 : :
7559 : 1343 : FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
7560 : 1343 : if (!broken_loop)
7561 : 1327 : remove_edge (BRANCH_EDGE (entry_bb));
7562 : : else
7563 : : {
7564 : 16 : remove_edge_and_dominated_blocks (BRANCH_EDGE (entry_bb));
7565 : 16 : region->outer->cont = NULL;
7566 : : }
7567 : :
7568 : : /* Connect all the blocks. */
7569 : 1343 : if (!broken_loop)
7570 : : {
7571 : 1327 : ep = find_edge (cont_bb, body_bb);
7572 : 1327 : if (gimple_omp_for_combined_p (fd->for_stmt))
7573 : : {
7574 : 617 : remove_edge (ep);
7575 : 617 : ep = NULL;
7576 : : }
7577 : 710 : else if (fd->collapse > 1)
7578 : : {
7579 : 81 : remove_edge (ep);
7580 : 81 : ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
7581 : : }
7582 : : else
7583 : 629 : ep->flags = EDGE_TRUE_VALUE;
7584 : 2573 : find_edge (cont_bb, fin_bb)->flags
7585 : 1408 : = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
7586 : : }
7587 : :
7588 : 1343 : set_immediate_dominator (CDI_DOMINATORS, body_bb,
7589 : : recompute_dominator (CDI_DOMINATORS, body_bb));
7590 : 1343 : if (!broken_loop)
7591 : 1327 : set_immediate_dominator (CDI_DOMINATORS, fin_bb,
7592 : : recompute_dominator (CDI_DOMINATORS, fin_bb));
7593 : :
7594 : 1327 : if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
7595 : : {
7596 : 710 : class loop *loop = alloc_loop ();
7597 : 710 : loop->header = body_bb;
7598 : 710 : if (collapse_bb == NULL)
7599 : 629 : loop->latch = cont_bb;
7600 : 710 : add_loop (loop, body_bb->loop_father);
7601 : : }
7602 : 1343 : }
7603 : :
7604 : : /* A subroutine of expand_omp_for. Generate code for an OpenACC
7605 : : partitioned loop. The lowering here is abstracted, in that the
7606 : : loop parameters are passed through internal functions, which are
7607 : : further lowered by oacc_device_lower, once we get to the target
7608 : : compiler. The loop is of the form:
7609 : :
7610 : : for (V = B; V LTGT E; V += S) {BODY}
7611 : :
7612 : : where LTGT is < or >. We may have a specified chunking size, CHUNKING
7613 : : (constant 0 for no chunking) and we will have a GWV partitioning
7614 : : mask, specifying dimensions over which the loop is to be
7615 : : partitioned (see note below). We generate code that looks like
7616 : : (this ignores tiling):
7617 : :
7618 : : <entry_bb> [incoming FALL->body, BRANCH->exit]
7619 : : typedef signedintify (typeof (V)) T; // underlying signed integral type
7620 : : T range = E - B;
7621 : : T chunk_no = 0;
7622 : : T DIR = LTGT == '<' ? +1 : -1;
7623 : : T chunk_max = GOACC_LOOP_CHUNK (dir, range, S, CHUNK_SIZE, GWV);
7624 : : T step = GOACC_LOOP_STEP (dir, range, S, CHUNK_SIZE, GWV);
7625 : :
7626 : : <head_bb> [created by splitting end of entry_bb]
7627 : : T offset = GOACC_LOOP_OFFSET (dir, range, S, CHUNK_SIZE, GWV, chunk_no);
7628 : : T bound = GOACC_LOOP_BOUND (dir, range, S, CHUNK_SIZE, GWV, offset);
7629 : : if (!(offset LTGT bound)) goto bottom_bb;
7630 : :
7631 : : <body_bb> [incoming]
7632 : : V = B + offset;
7633 : : {BODY}
7634 : :
7635 : : <cont_bb> [incoming, may == body_bb FALL->exit_bb, BRANCH->body_bb]
7636 : : offset += step;
7637 : : if (offset LTGT bound) goto body_bb; [*]
7638 : :
7639 : : <bottom_bb> [created by splitting start of exit_bb] insert BRANCH->head_bb
7640 : : chunk_no++;
7641 : : if (chunk < chunk_max) goto head_bb;
7642 : :
7643 : : <exit_bb> [incoming]
7644 : : V = B + ((range -/+ 1) / S +/- 1) * S [*]
7645 : :
7646 : : [*] Needed if V live at end of loop. */
7647 : :
7648 : : static void
7649 : 11557 : expand_oacc_for (struct omp_region *region, struct omp_for_data *fd)
7650 : : {
7651 : 11557 : bool is_oacc_kernels_parallelized
7652 : 11557 : = (lookup_attribute ("oacc kernels parallelized",
7653 : 11557 : DECL_ATTRIBUTES (current_function_decl)) != NULL);
7654 : 11557 : {
7655 : 11557 : bool is_oacc_kernels
7656 : 11557 : = (lookup_attribute ("oacc kernels",
7657 : 11557 : DECL_ATTRIBUTES (current_function_decl)) != NULL);
7658 : 11557 : if (is_oacc_kernels_parallelized)
7659 : 388 : gcc_checking_assert (is_oacc_kernels);
7660 : : }
7661 : 23114 : gcc_assert (gimple_in_ssa_p (cfun) == is_oacc_kernels_parallelized);
7662 : : /* In the following, some of the 'gimple_in_ssa_p (cfun)' conditionals are
7663 : : for SSA specifics, and some are for 'parloops' OpenACC
7664 : : 'kernels'-parallelized specifics. */
7665 : :
7666 : 11557 : tree v = fd->loop.v;
7667 : 11557 : enum tree_code cond_code = fd->loop.cond_code;
7668 : 11557 : enum tree_code plus_code = PLUS_EXPR;
7669 : :
7670 : 11557 : tree chunk_size = integer_minus_one_node;
7671 : 11557 : tree gwv = integer_zero_node;
7672 : 11557 : tree iter_type = TREE_TYPE (v);
7673 : 11557 : tree diff_type = iter_type;
7674 : 11557 : tree plus_type = iter_type;
7675 : 11557 : struct oacc_collapse *counts = NULL;
7676 : :
7677 : 11557 : gcc_checking_assert (gimple_omp_for_kind (fd->for_stmt)
7678 : : == GF_OMP_FOR_KIND_OACC_LOOP);
7679 : 11557 : gcc_assert (!gimple_omp_for_combined_into_p (fd->for_stmt));
7680 : 11557 : gcc_assert (cond_code == LT_EXPR || cond_code == GT_EXPR);
7681 : :
7682 : 11557 : if (POINTER_TYPE_P (iter_type))
7683 : : {
7684 : 52 : plus_code = POINTER_PLUS_EXPR;
7685 : 52 : plus_type = sizetype;
7686 : : }
7687 : 23816 : for (int ix = fd->collapse; ix--;)
7688 : : {
7689 : 12259 : tree diff_type2 = TREE_TYPE (fd->loops[ix].step);
7690 : 12259 : if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (diff_type2))
7691 : 0 : diff_type = diff_type2;
7692 : : }
7693 : 11557 : if (POINTER_TYPE_P (diff_type) || TYPE_UNSIGNED (diff_type))
7694 : 1002 : diff_type = signed_type_for (diff_type);
7695 : 11557 : if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (integer_type_node))
7696 : 23 : diff_type = integer_type_node;
7697 : :
7698 : 11557 : basic_block entry_bb = region->entry; /* BB ending in OMP_FOR */
7699 : 11557 : basic_block exit_bb = region->exit; /* BB ending in OMP_RETURN */
7700 : 11557 : basic_block cont_bb = region->cont; /* BB ending in OMP_CONTINUE */
7701 : 11557 : basic_block bottom_bb = NULL;
7702 : :
7703 : : /* entry_bb has two successors; the branch edge is to the exit
7704 : : block, fallthrough edge to body. */
7705 : 11557 : gcc_assert (EDGE_COUNT (entry_bb->succs) == 2
7706 : : && BRANCH_EDGE (entry_bb)->dest == exit_bb);
7707 : :
7708 : : /* If cont_bb non-NULL, it has 2 successors. The branch successor is
7709 : : body_bb, or to a block whose only successor is the body_bb. Its
7710 : : fallthrough successor is the final block (same as the branch
7711 : : successor of the entry_bb). */
7712 : 11557 : if (cont_bb)
7713 : : {
7714 : 11518 : basic_block body_bb = FALLTHRU_EDGE (entry_bb)->dest;
7715 : 11518 : basic_block bed = BRANCH_EDGE (cont_bb)->dest;
7716 : :
7717 : 11518 : gcc_assert (FALLTHRU_EDGE (cont_bb)->dest == exit_bb);
7718 : 11518 : gcc_assert (bed == body_bb || single_succ_edge (bed)->dest == body_bb);
7719 : : }
7720 : : else
7721 : 39 : gcc_assert (!gimple_in_ssa_p (cfun));
7722 : :
7723 : : /* The exit block only has entry_bb and cont_bb as predecessors. */
7724 : 23153 : gcc_assert (EDGE_COUNT (exit_bb->preds) == 1 + (cont_bb != NULL));
7725 : :
7726 : 11557 : tree chunk_no;
7727 : 11557 : tree chunk_max = NULL_TREE;
7728 : 11557 : tree bound, offset;
7729 : 11557 : tree step = create_tmp_var (diff_type, ".step");
7730 : 11557 : bool up = cond_code == LT_EXPR;
7731 : 11717 : tree dir = build_int_cst (diff_type, up ? +1 : -1);
7732 : 11557 : bool chunking = !gimple_in_ssa_p (cfun);
7733 : 11557 : bool negating;
7734 : :
7735 : : /* Tiling vars. */
7736 : 11557 : tree tile_size = NULL_TREE;
7737 : 11557 : tree element_s = NULL_TREE;
7738 : 11557 : tree e_bound = NULL_TREE, e_offset = NULL_TREE, e_step = NULL_TREE;
7739 : 11557 : basic_block elem_body_bb = NULL;
7740 : 11557 : basic_block elem_cont_bb = NULL;
7741 : :
7742 : : /* SSA instances. */
7743 : 11557 : tree offset_incr = NULL_TREE;
7744 : 11557 : tree offset_init = NULL_TREE;
7745 : :
7746 : 11557 : gimple_stmt_iterator gsi;
7747 : 11557 : gassign *ass;
7748 : 11557 : gcall *call;
7749 : 11557 : gimple *stmt;
7750 : 11557 : tree expr;
7751 : 11557 : location_t loc;
7752 : 11557 : edge split, be, fte;
7753 : :
7754 : : /* Split the end of entry_bb to create head_bb. */
7755 : 11557 : split = split_block (entry_bb, last_nondebug_stmt (entry_bb));
7756 : 11557 : basic_block head_bb = split->dest;
7757 : 11557 : entry_bb = split->src;
7758 : :
7759 : : /* Chunk setup goes at end of entry_bb, replacing the omp_for. */
7760 : 11557 : gsi = gsi_last_nondebug_bb (entry_bb);
7761 : 11557 : gomp_for *for_stmt = as_a <gomp_for *> (gsi_stmt (gsi));
7762 : 11557 : loc = gimple_location (for_stmt);
7763 : :
7764 : 11557 : if (gimple_in_ssa_p (cfun))
7765 : : {
7766 : 388 : offset_init = gimple_omp_for_index (for_stmt, 0);
7767 : 388 : gcc_assert (integer_zerop (fd->loop.n1));
7768 : : /* The SSA parallelizer does gang parallelism. */
7769 : 388 : gwv = build_int_cst (integer_type_node, GOMP_DIM_MASK (GOMP_DIM_GANG));
7770 : : }
7771 : :
7772 : 11557 : if (fd->collapse > 1 || fd->tiling)
7773 : : {
7774 : 1188 : gcc_assert (!gimple_in_ssa_p (cfun) && up);
7775 : 594 : counts = XALLOCAVEC (struct oacc_collapse, fd->collapse);
7776 : 594 : tree total = expand_oacc_collapse_init (fd, &gsi, counts, diff_type,
7777 : 594 : TREE_TYPE (fd->loop.n2), loc);
7778 : :
7779 : 594 : if (SSA_VAR_P (fd->loop.n2))
7780 : : {
7781 : 107 : total = force_gimple_operand_gsi (&gsi, total, false, NULL_TREE,
7782 : : true, GSI_SAME_STMT);
7783 : 107 : ass = gimple_build_assign (fd->loop.n2, total);
7784 : 107 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7785 : : }
7786 : : }
7787 : :
7788 : 11557 : tree b = fd->loop.n1;
7789 : 11557 : tree e = fd->loop.n2;
7790 : 11557 : tree s = fd->loop.step;
7791 : :
7792 : 11557 : b = force_gimple_operand_gsi (&gsi, b, true, NULL_TREE, true, GSI_SAME_STMT);
7793 : 11557 : e = force_gimple_operand_gsi (&gsi, e, true, NULL_TREE, true, GSI_SAME_STMT);
7794 : :
7795 : : /* Convert the step, avoiding possible unsigned->signed overflow. */
7796 : 11557 : negating = !up && TYPE_UNSIGNED (TREE_TYPE (s));
7797 : 32 : if (negating)
7798 : 32 : s = fold_build1 (NEGATE_EXPR, TREE_TYPE (s), s);
7799 : 11557 : s = fold_convert (diff_type, s);
7800 : 11557 : if (negating)
7801 : 32 : s = fold_build1 (NEGATE_EXPR, diff_type, s);
7802 : 11557 : s = force_gimple_operand_gsi (&gsi, s, true, NULL_TREE, true, GSI_SAME_STMT);
7803 : :
7804 : 11557 : if (!chunking)
7805 : 388 : chunk_size = integer_zero_node;
7806 : 11557 : expr = fold_convert (diff_type, chunk_size);
7807 : 11557 : chunk_size = force_gimple_operand_gsi (&gsi, expr, true,
7808 : : NULL_TREE, true, GSI_SAME_STMT);
7809 : :
7810 : 11557 : if (fd->tiling)
7811 : : {
7812 : : /* Determine the tile size and element step,
7813 : : modify the outer loop step size. */
7814 : 177 : tile_size = create_tmp_var (diff_type, ".tile_size");
7815 : 177 : expr = build_int_cst (diff_type, 1);
7816 : 461 : for (int ix = 0; ix < fd->collapse; ix++)
7817 : 284 : expr = fold_build2 (MULT_EXPR, diff_type, counts[ix].tile, expr);
7818 : 177 : expr = force_gimple_operand_gsi (&gsi, expr, true,
7819 : : NULL_TREE, true, GSI_SAME_STMT);
7820 : 177 : ass = gimple_build_assign (tile_size, expr);
7821 : 177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7822 : :
7823 : 177 : element_s = create_tmp_var (diff_type, ".element_s");
7824 : 177 : ass = gimple_build_assign (element_s, s);
7825 : 177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7826 : :
7827 : 177 : expr = fold_build2 (MULT_EXPR, diff_type, s, tile_size);
7828 : 177 : s = force_gimple_operand_gsi (&gsi, expr, true,
7829 : : NULL_TREE, true, GSI_SAME_STMT);
7830 : : }
7831 : :
7832 : : /* Determine the range, avoiding possible unsigned->signed overflow. */
7833 : 11557 : negating = !up && TYPE_UNSIGNED (iter_type);
7834 : 23066 : expr = fold_build2 (MINUS_EXPR, plus_type,
7835 : : fold_convert (plus_type, negating ? b : e),
7836 : : fold_convert (plus_type, negating ? e : b));
7837 : 11557 : expr = fold_convert (diff_type, expr);
7838 : 11557 : if (negating)
7839 : 48 : expr = fold_build1 (NEGATE_EXPR, diff_type, expr);
7840 : 11557 : tree range = force_gimple_operand_gsi (&gsi, expr, true,
7841 : : NULL_TREE, true, GSI_SAME_STMT);
7842 : :
7843 : 11557 : chunk_no = build_int_cst (diff_type, 0);
7844 : 11557 : if (chunking)
7845 : : {
7846 : 11169 : gcc_assert (!gimple_in_ssa_p (cfun));
7847 : :
7848 : 11169 : expr = chunk_no;
7849 : 11169 : chunk_max = create_tmp_var (diff_type, ".chunk_max");
7850 : 11169 : chunk_no = create_tmp_var (diff_type, ".chunk_no");
7851 : :
7852 : 11169 : ass = gimple_build_assign (chunk_no, expr);
7853 : 11169 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7854 : :
7855 : 11169 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
7856 : 11169 : build_int_cst (integer_type_node,
7857 : 11169 : IFN_GOACC_LOOP_CHUNKS),
7858 : : dir, range, s, chunk_size, gwv);
7859 : 11169 : gimple_call_set_lhs (call, chunk_max);
7860 : 11169 : gimple_set_location (call, loc);
7861 : 11169 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7862 : : }
7863 : : else
7864 : : chunk_size = chunk_no;
7865 : :
7866 : 11557 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
7867 : 11557 : build_int_cst (integer_type_node,
7868 : 11557 : IFN_GOACC_LOOP_STEP),
7869 : : dir, range, s, chunk_size, gwv);
7870 : 11557 : gimple_call_set_lhs (call, step);
7871 : 11557 : gimple_set_location (call, loc);
7872 : 11557 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7873 : :
7874 : : /* Remove the GIMPLE_OMP_FOR. */
7875 : 11557 : gsi_remove (&gsi, true);
7876 : :
7877 : : /* Fixup edges from head_bb. */
7878 : 11557 : be = BRANCH_EDGE (head_bb);
7879 : 11557 : fte = FALLTHRU_EDGE (head_bb);
7880 : 11557 : be->flags |= EDGE_FALSE_VALUE;
7881 : 11557 : fte->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
7882 : :
7883 : 11557 : basic_block body_bb = fte->dest;
7884 : :
7885 : 11557 : if (gimple_in_ssa_p (cfun))
7886 : : {
7887 : 388 : gsi = gsi_last_nondebug_bb (cont_bb);
7888 : 388 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
7889 : :
7890 : 388 : offset = gimple_omp_continue_control_use (cont_stmt);
7891 : 388 : offset_incr = gimple_omp_continue_control_def (cont_stmt);
7892 : : }
7893 : : else
7894 : : {
7895 : 11169 : offset = create_tmp_var (diff_type, ".offset");
7896 : 11169 : offset_init = offset_incr = offset;
7897 : : }
7898 : 11557 : bound = create_tmp_var (TREE_TYPE (offset), ".bound");
7899 : :
7900 : : /* Loop offset & bound go into head_bb. */
7901 : 11557 : gsi = gsi_start_bb (head_bb);
7902 : :
7903 : 11557 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
7904 : 11557 : build_int_cst (integer_type_node,
7905 : 11557 : IFN_GOACC_LOOP_OFFSET),
7906 : : dir, range, s,
7907 : : chunk_size, gwv, chunk_no);
7908 : 11557 : gimple_call_set_lhs (call, offset_init);
7909 : 11557 : gimple_set_location (call, loc);
7910 : 11557 : gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
7911 : :
7912 : 11557 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
7913 : 11557 : build_int_cst (integer_type_node,
7914 : 11557 : IFN_GOACC_LOOP_BOUND),
7915 : : dir, range, s,
7916 : : chunk_size, gwv, offset_init);
7917 : 11557 : gimple_call_set_lhs (call, bound);
7918 : 11557 : gimple_set_location (call, loc);
7919 : 11557 : gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
7920 : :
7921 : 11557 : expr = build2 (cond_code, boolean_type_node, offset_init, bound);
7922 : 11557 : gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
7923 : : GSI_CONTINUE_LINKING);
7924 : :
7925 : : /* V assignment goes into body_bb. */
7926 : 11557 : if (!gimple_in_ssa_p (cfun))
7927 : : {
7928 : 11169 : gsi = gsi_start_bb (body_bb);
7929 : :
7930 : 11169 : expr = build2 (plus_code, iter_type, b,
7931 : : fold_convert (plus_type, offset));
7932 : 11169 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
7933 : : true, GSI_SAME_STMT);
7934 : 11169 : ass = gimple_build_assign (v, expr);
7935 : 11169 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7936 : :
7937 : 11169 : if (fd->collapse > 1 || fd->tiling)
7938 : 594 : expand_oacc_collapse_vars (fd, false, &gsi, counts, v, diff_type);
7939 : :
7940 : 11169 : if (fd->tiling)
7941 : : {
7942 : : /* Determine the range of the element loop -- usually simply
7943 : : the tile_size, but could be smaller if the final
7944 : : iteration of the outer loop is a partial tile. */
7945 : 177 : tree e_range = create_tmp_var (diff_type, ".e_range");
7946 : :
7947 : 177 : expr = build2 (MIN_EXPR, diff_type,
7948 : : build2 (MINUS_EXPR, diff_type, bound, offset),
7949 : : build2 (MULT_EXPR, diff_type, tile_size,
7950 : : element_s));
7951 : 177 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
7952 : : true, GSI_SAME_STMT);
7953 : 177 : ass = gimple_build_assign (e_range, expr);
7954 : 177 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7955 : :
7956 : : /* Determine bound, offset & step of inner loop. */
7957 : 177 : e_bound = create_tmp_var (diff_type, ".e_bound");
7958 : 177 : e_offset = create_tmp_var (diff_type, ".e_offset");
7959 : 177 : e_step = create_tmp_var (diff_type, ".e_step");
7960 : :
7961 : : /* Mark these as element loops. */
7962 : 177 : tree t, e_gwv = integer_minus_one_node;
7963 : 177 : tree chunk = build_int_cst (diff_type, 0); /* Never chunked. */
7964 : :
7965 : 177 : t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_OFFSET);
7966 : 177 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
7967 : : element_s, chunk, e_gwv, chunk);
7968 : 177 : gimple_call_set_lhs (call, e_offset);
7969 : 177 : gimple_set_location (call, loc);
7970 : 177 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7971 : :
7972 : 177 : t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_BOUND);
7973 : 177 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
7974 : : element_s, chunk, e_gwv, e_offset);
7975 : 177 : gimple_call_set_lhs (call, e_bound);
7976 : 177 : gimple_set_location (call, loc);
7977 : 177 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7978 : :
7979 : 177 : t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_STEP);
7980 : 177 : call = gimple_build_call_internal (IFN_GOACC_LOOP, 6, t, dir, e_range,
7981 : : element_s, chunk, e_gwv);
7982 : 177 : gimple_call_set_lhs (call, e_step);
7983 : 177 : gimple_set_location (call, loc);
7984 : 177 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7985 : :
7986 : : /* Add test and split block. */
7987 : 177 : expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
7988 : 177 : stmt = gimple_build_cond_empty (expr);
7989 : 177 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
7990 : 177 : split = split_block (body_bb, stmt);
7991 : 177 : elem_body_bb = split->dest;
7992 : 177 : if (cont_bb == body_bb)
7993 : 146 : cont_bb = elem_body_bb;
7994 : 177 : body_bb = split->src;
7995 : :
7996 : 177 : split->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
7997 : :
7998 : : /* Add a dummy exit for the tiled block when cont_bb is missing. */
7999 : 177 : if (cont_bb == NULL)
8000 : : {
8001 : 5 : edge e = make_edge (body_bb, exit_bb, EDGE_FALSE_VALUE);
8002 : 5 : e->probability = profile_probability::even ();
8003 : 5 : split->probability = profile_probability::even ();
8004 : : }
8005 : :
8006 : : /* Initialize the user's loop vars. */
8007 : 177 : gsi = gsi_start_bb (elem_body_bb);
8008 : 177 : expand_oacc_collapse_vars (fd, true, &gsi, counts, e_offset,
8009 : : diff_type);
8010 : : }
8011 : : }
8012 : :
8013 : : /* Loop increment goes into cont_bb. If this is not a loop, we
8014 : : will have spawned threads as if it was, and each one will
8015 : : execute one iteration. The specification is not explicit about
8016 : : whether such constructs are ill-formed or not, and they can
8017 : : occur, especially when noreturn routines are involved. */
8018 : 11557 : if (cont_bb)
8019 : : {
8020 : 11518 : gsi = gsi_last_nondebug_bb (cont_bb);
8021 : 11518 : gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
8022 : 11518 : loc = gimple_location (cont_stmt);
8023 : :
8024 : 11518 : if (fd->tiling)
8025 : : {
8026 : : /* Insert element loop increment and test. */
8027 : 172 : expr = build2 (PLUS_EXPR, diff_type, e_offset, e_step);
8028 : 172 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8029 : : true, GSI_SAME_STMT);
8030 : 172 : ass = gimple_build_assign (e_offset, expr);
8031 : 172 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8032 : 172 : expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
8033 : :
8034 : 172 : stmt = gimple_build_cond_empty (expr);
8035 : 172 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8036 : 172 : split = split_block (cont_bb, stmt);
8037 : 172 : elem_cont_bb = split->src;
8038 : 172 : cont_bb = split->dest;
8039 : :
8040 : 172 : split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8041 : 172 : split->probability = profile_probability::unlikely ().guessed ();
8042 : 172 : edge latch_edge
8043 : 172 : = make_edge (elem_cont_bb, elem_body_bb, EDGE_TRUE_VALUE);
8044 : 172 : latch_edge->probability = profile_probability::likely ().guessed ();
8045 : :
8046 : 172 : edge skip_edge = make_edge (body_bb, cont_bb, EDGE_FALSE_VALUE);
8047 : 172 : skip_edge->probability = profile_probability::unlikely ().guessed ();
8048 : 172 : edge loop_entry_edge = EDGE_SUCC (body_bb, 1 - skip_edge->dest_idx);
8049 : 172 : loop_entry_edge->probability
8050 : 172 : = profile_probability::likely ().guessed ();
8051 : :
8052 : 172 : gsi = gsi_for_stmt (cont_stmt);
8053 : : }
8054 : :
8055 : : /* Increment offset. */
8056 : 11518 : if (gimple_in_ssa_p (cfun))
8057 : 388 : expr = build2 (plus_code, iter_type, offset,
8058 : : fold_convert (plus_type, step));
8059 : : else
8060 : 11130 : expr = build2 (PLUS_EXPR, diff_type, offset, step);
8061 : 11518 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8062 : : true, GSI_SAME_STMT);
8063 : 11518 : ass = gimple_build_assign (offset_incr, expr);
8064 : 11518 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8065 : 11518 : expr = build2 (cond_code, boolean_type_node, offset_incr, bound);
8066 : 11518 : gsi_insert_before (&gsi, gimple_build_cond_empty (expr), GSI_SAME_STMT);
8067 : :
8068 : : /* Remove the GIMPLE_OMP_CONTINUE. */
8069 : 11518 : gsi_remove (&gsi, true);
8070 : :
8071 : : /* Fixup edges from cont_bb. */
8072 : 11518 : be = BRANCH_EDGE (cont_bb);
8073 : 11518 : fte = FALLTHRU_EDGE (cont_bb);
8074 : 11518 : be->flags |= EDGE_TRUE_VALUE;
8075 : 11518 : fte->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8076 : :
8077 : 11518 : if (chunking)
8078 : : {
8079 : : /* Split the beginning of exit_bb to make bottom_bb. We
8080 : : need to insert a nop at the start, because splitting is
8081 : : after a stmt, not before. */
8082 : 11130 : gsi = gsi_start_bb (exit_bb);
8083 : 11130 : stmt = gimple_build_nop ();
8084 : 11130 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8085 : 11130 : split = split_block (exit_bb, stmt);
8086 : 11130 : bottom_bb = split->src;
8087 : 11130 : exit_bb = split->dest;
8088 : 11130 : gsi = gsi_last_bb (bottom_bb);
8089 : :
8090 : : /* Chunk increment and test goes into bottom_bb. */
8091 : 11130 : expr = build2 (PLUS_EXPR, diff_type, chunk_no,
8092 : 11130 : build_int_cst (diff_type, 1));
8093 : 11130 : ass = gimple_build_assign (chunk_no, expr);
8094 : 11130 : gsi_insert_after (&gsi, ass, GSI_CONTINUE_LINKING);
8095 : :
8096 : : /* Chunk test at end of bottom_bb. */
8097 : 11130 : expr = build2 (LT_EXPR, boolean_type_node, chunk_no, chunk_max);
8098 : 11130 : gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
8099 : : GSI_CONTINUE_LINKING);
8100 : :
8101 : : /* Fixup edges from bottom_bb. */
8102 : 11130 : split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8103 : 11130 : split->probability = profile_probability::unlikely ().guessed ();
8104 : 11130 : edge latch_edge = make_edge (bottom_bb, head_bb, EDGE_TRUE_VALUE);
8105 : 11130 : latch_edge->probability = profile_probability::likely ().guessed ();
8106 : : }
8107 : : }
8108 : :
8109 : 11557 : gsi = gsi_last_nondebug_bb (exit_bb);
8110 : 11557 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
8111 : 11557 : loc = gimple_location (gsi_stmt (gsi));
8112 : :
8113 : 11557 : if (!gimple_in_ssa_p (cfun))
8114 : : {
8115 : : /* Insert the final value of V, in case it is live. This is the
8116 : : value for the only thread that survives past the join. */
8117 : 11169 : expr = fold_build2 (MINUS_EXPR, diff_type, range, dir);
8118 : 11169 : expr = fold_build2 (PLUS_EXPR, diff_type, expr, s);
8119 : 11169 : expr = fold_build2 (TRUNC_DIV_EXPR, diff_type, expr, s);
8120 : 11169 : expr = fold_build2 (MULT_EXPR, diff_type, expr, s);
8121 : 11169 : expr = build2 (plus_code, iter_type, b, fold_convert (plus_type, expr));
8122 : 11169 : expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8123 : : true, GSI_SAME_STMT);
8124 : 11169 : ass = gimple_build_assign (v, expr);
8125 : 11169 : gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8126 : : }
8127 : :
8128 : : /* Remove the OMP_RETURN. */
8129 : 11557 : gsi_remove (&gsi, true);
8130 : :
8131 : 11557 : if (cont_bb)
8132 : : {
8133 : : /* We now have one, two or three nested loops. Update the loop
8134 : : structures. */
8135 : 11518 : class loop *parent = entry_bb->loop_father;
8136 : 11518 : class loop *body = body_bb->loop_father;
8137 : :
8138 : 11518 : if (chunking)
8139 : : {
8140 : 11130 : class loop *chunk_loop = alloc_loop ();
8141 : 11130 : chunk_loop->header = head_bb;
8142 : 11130 : chunk_loop->latch = bottom_bb;
8143 : 11130 : add_loop (chunk_loop, parent);
8144 : 11130 : parent = chunk_loop;
8145 : : }
8146 : 388 : else if (parent != body)
8147 : : {
8148 : 388 : gcc_assert (body->header == body_bb);
8149 : 388 : gcc_assert (body->latch == cont_bb
8150 : : || single_pred (body->latch) == cont_bb);
8151 : : parent = NULL;
8152 : : }
8153 : :
8154 : 11130 : if (parent)
8155 : : {
8156 : 11130 : class loop *body_loop = alloc_loop ();
8157 : 11130 : body_loop->header = body_bb;
8158 : 11130 : body_loop->latch = cont_bb;
8159 : 11130 : add_loop (body_loop, parent);
8160 : :
8161 : 11130 : if (fd->tiling)
8162 : : {
8163 : : /* Insert tiling's element loop. */
8164 : 172 : class loop *inner_loop = alloc_loop ();
8165 : 172 : inner_loop->header = elem_body_bb;
8166 : 172 : inner_loop->latch = elem_cont_bb;
8167 : 172 : add_loop (inner_loop, body_loop);
8168 : : }
8169 : : }
8170 : : }
8171 : 11557 : }
8172 : :
8173 : : /* Expand the OMP loop defined by REGION. */
8174 : :
8175 : : static void
8176 : 47475 : expand_omp_for (struct omp_region *region, gimple *inner_stmt)
8177 : : {
8178 : 47475 : struct omp_for_data fd;
8179 : 47475 : struct omp_for_data_loop *loops;
8180 : :
8181 : 47475 : loops = XALLOCAVEC (struct omp_for_data_loop,
8182 : : gimple_omp_for_collapse
8183 : : (last_nondebug_stmt (region->entry)));
8184 : 47475 : omp_extract_for_data (as_a <gomp_for *> (last_nondebug_stmt (region->entry)),
8185 : : &fd, loops);
8186 : 47475 : region->sched_kind = fd.sched_kind;
8187 : 47475 : region->sched_modifiers = fd.sched_modifiers;
8188 : 47475 : region->has_lastprivate_conditional = fd.lastprivate_conditional != 0;
8189 : 47475 : if (fd.non_rect && !gimple_omp_for_combined_into_p (fd.for_stmt))
8190 : : {
8191 : 2344 : for (int i = fd.first_nonrect; i <= fd.last_nonrect; i++)
8192 : 1644 : if ((loops[i].m1 || loops[i].m2)
8193 : 731 : && (loops[i].m1 == NULL_TREE
8194 : 506 : || TREE_CODE (loops[i].m1) == INTEGER_CST)
8195 : 612 : && (loops[i].m2 == NULL_TREE
8196 : 343 : || TREE_CODE (loops[i].m2) == INTEGER_CST)
8197 : 596 : && TREE_CODE (loops[i].step) == INTEGER_CST
8198 : 586 : && TREE_CODE (loops[i - loops[i].outer].step) == INTEGER_CST)
8199 : : {
8200 : 586 : tree t;
8201 : 586 : tree itype = TREE_TYPE (loops[i].v);
8202 : 586 : if (loops[i].m1 && loops[i].m2)
8203 : 108 : t = fold_build2 (MINUS_EXPR, itype, loops[i].m2, loops[i].m1);
8204 : 478 : else if (loops[i].m1)
8205 : 267 : t = fold_build1 (NEGATE_EXPR, itype, loops[i].m1);
8206 : : else
8207 : : t = loops[i].m2;
8208 : 586 : t = fold_build2 (MULT_EXPR, itype, t,
8209 : : fold_convert (itype,
8210 : : loops[i - loops[i].outer].step));
8211 : 586 : if (TYPE_UNSIGNED (itype) && loops[i].cond_code == GT_EXPR)
8212 : 3 : t = fold_build2 (TRUNC_MOD_EXPR, itype,
8213 : : fold_build1 (NEGATE_EXPR, itype, t),
8214 : : fold_build1 (NEGATE_EXPR, itype,
8215 : : fold_convert (itype,
8216 : : loops[i].step)));
8217 : : else
8218 : 583 : t = fold_build2 (TRUNC_MOD_EXPR, itype, t,
8219 : : fold_convert (itype, loops[i].step));
8220 : 586 : if (integer_nonzerop (t))
8221 : 8 : error_at (gimple_location (fd.for_stmt),
8222 : : "invalid OpenMP non-rectangular loop step; "
8223 : : "%<(%E - %E) * %E%> is not a multiple of loop %d "
8224 : : "step %qE",
8225 : 8 : loops[i].m2 ? loops[i].m2 : integer_zero_node,
8226 : 8 : loops[i].m1 ? loops[i].m1 : integer_zero_node,
8227 : 8 : loops[i - loops[i].outer].step, i + 1,
8228 : : loops[i].step);
8229 : : }
8230 : : }
8231 : :
8232 : 47475 : gcc_assert (EDGE_COUNT (region->entry->succs) == 2);
8233 : 47475 : BRANCH_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
8234 : 47475 : FALLTHRU_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
8235 : 47475 : if (region->cont)
8236 : : {
8237 : 45188 : gcc_assert (EDGE_COUNT (region->cont->succs) == 2);
8238 : 45188 : BRANCH_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
8239 : 45188 : FALLTHRU_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
8240 : : }
8241 : : else
8242 : : /* If there isn't a continue then this is a degerate case where
8243 : : the introduction of abnormal edges during lowering will prevent
8244 : : original loops from being detected. Fix that up. */
8245 : 2287 : loops_state_set (LOOPS_NEED_FIXUP);
8246 : :
8247 : 47475 : if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_SIMD)
8248 : 9361 : expand_omp_simd (region, &fd);
8249 : 38114 : else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_OACC_LOOP)
8250 : : {
8251 : 11557 : gcc_assert (!inner_stmt && !fd.non_rect);
8252 : 11557 : expand_oacc_for (region, &fd);
8253 : : }
8254 : 26557 : else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_TASKLOOP)
8255 : : {
8256 : 2686 : if (gimple_omp_for_combined_into_p (fd.for_stmt))
8257 : 1343 : expand_omp_taskloop_for_inner (region, &fd, inner_stmt);
8258 : : else
8259 : 1343 : expand_omp_taskloop_for_outer (region, &fd, inner_stmt);
8260 : : }
8261 : 23871 : else if (fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC
8262 : 20189 : && !fd.have_ordered)
8263 : : {
8264 : 19742 : if (fd.chunk_size == NULL)
8265 : 13931 : expand_omp_for_static_nochunk (region, &fd, inner_stmt);
8266 : : else
8267 : 5811 : expand_omp_for_static_chunk (region, &fd, inner_stmt);
8268 : : }
8269 : : else
8270 : : {
8271 : 4129 : int fn_index, start_ix, next_ix;
8272 : 4129 : unsigned HOST_WIDE_INT sched = 0;
8273 : 4129 : tree sched_arg = NULL_TREE;
8274 : :
8275 : 4129 : gcc_assert (gimple_omp_for_kind (fd.for_stmt)
8276 : : == GF_OMP_FOR_KIND_FOR && !fd.non_rect);
8277 : 4129 : if (fd.chunk_size == NULL
8278 : 1665 : && fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC)
8279 : 0 : fd.chunk_size = integer_zero_node;
8280 : 4129 : switch (fd.sched_kind)
8281 : : {
8282 : 1665 : case OMP_CLAUSE_SCHEDULE_RUNTIME:
8283 : 1665 : if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_NONMONOTONIC) != 0
8284 : 28 : && fd.lastprivate_conditional == 0)
8285 : : {
8286 : 28 : gcc_assert (!fd.have_ordered);
8287 : : fn_index = 6;
8288 : : sched = 4;
8289 : : }
8290 : 1637 : else if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
8291 : 1605 : && !fd.have_ordered
8292 : 1561 : && fd.lastprivate_conditional == 0)
8293 : : fn_index = 7;
8294 : : else
8295 : : {
8296 : 87 : fn_index = 3;
8297 : 87 : sched = (HOST_WIDE_INT_1U << 31);
8298 : : }
8299 : : break;
8300 : 2017 : case OMP_CLAUSE_SCHEDULE_DYNAMIC:
8301 : 2017 : case OMP_CLAUSE_SCHEDULE_GUIDED:
8302 : 2017 : if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
8303 : 1961 : && !fd.have_ordered
8304 : 1798 : && fd.lastprivate_conditional == 0)
8305 : : {
8306 : 1771 : fn_index = 3 + fd.sched_kind;
8307 : 1771 : sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
8308 : : break;
8309 : : }
8310 : 246 : fn_index = fd.sched_kind;
8311 : 246 : sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
8312 : 246 : sched += (HOST_WIDE_INT_1U << 31);
8313 : 246 : break;
8314 : 447 : case OMP_CLAUSE_SCHEDULE_STATIC:
8315 : 447 : gcc_assert (fd.have_ordered);
8316 : : fn_index = 0;
8317 : : sched = (HOST_WIDE_INT_1U << 31) + 1;
8318 : : break;
8319 : 0 : default:
8320 : 0 : gcc_unreachable ();
8321 : : }
8322 : 4129 : if (!fd.ordered)
8323 : 3798 : fn_index += fd.have_ordered * 8;
8324 : 4129 : if (fd.ordered)
8325 : 331 : start_ix = ((int)BUILT_IN_GOMP_LOOP_DOACROSS_STATIC_START) + fn_index;
8326 : : else
8327 : 3798 : start_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_START) + fn_index;
8328 : 4129 : next_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_NEXT) + fn_index;
8329 : 4129 : if (fd.have_reductemp || fd.have_pointer_condtemp)
8330 : : {
8331 : 169 : if (fd.ordered)
8332 : : start_ix = (int)BUILT_IN_GOMP_LOOP_DOACROSS_START;
8333 : 133 : else if (fd.have_ordered)
8334 : : start_ix = (int)BUILT_IN_GOMP_LOOP_ORDERED_START;
8335 : : else
8336 : 95 : start_ix = (int)BUILT_IN_GOMP_LOOP_START;
8337 : 169 : sched_arg = build_int_cstu (long_integer_type_node, sched);
8338 : 169 : if (!fd.chunk_size)
8339 : 38 : fd.chunk_size = integer_zero_node;
8340 : : }
8341 : 4129 : if (fd.iter_type == long_long_unsigned_type_node)
8342 : : {
8343 : 778 : start_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_START
8344 : : - (int)BUILT_IN_GOMP_LOOP_STATIC_START);
8345 : 778 : next_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_NEXT
8346 : : - (int)BUILT_IN_GOMP_LOOP_STATIC_NEXT);
8347 : : }
8348 : 4129 : expand_omp_for_generic (region, &fd, (enum built_in_function) start_ix,
8349 : : (enum built_in_function) next_ix, sched_arg,
8350 : : inner_stmt);
8351 : : }
8352 : 47475 : }
8353 : :
8354 : : /* Expand code for an OpenMP sections directive. In pseudo code, we generate
8355 : :
8356 : : v = GOMP_sections_start (n);
8357 : : L0:
8358 : : switch (v)
8359 : : {
8360 : : case 0:
8361 : : goto L2;
8362 : : case 1:
8363 : : section 1;
8364 : : goto L1;
8365 : : case 2:
8366 : : ...
8367 : : case n:
8368 : : ...
8369 : : default:
8370 : : abort ();
8371 : : }
8372 : : L1:
8373 : : v = GOMP_sections_next ();
8374 : : goto L0;
8375 : : L2:
8376 : : reduction;
8377 : :
8378 : : If this is a combined parallel sections, replace the call to
8379 : : GOMP_sections_start with call to GOMP_sections_next. */
8380 : :
8381 : : static void
8382 : 378 : expand_omp_sections (struct omp_region *region)
8383 : : {
8384 : 378 : tree t, u, vin = NULL, vmain, vnext, l2;
8385 : 378 : unsigned len;
8386 : 378 : basic_block entry_bb, l0_bb, l1_bb, l2_bb, default_bb;
8387 : 378 : gimple_stmt_iterator si, switch_si;
8388 : 378 : gomp_sections *sections_stmt;
8389 : 378 : gimple *stmt;
8390 : 378 : gomp_continue *cont;
8391 : 378 : edge_iterator ei;
8392 : 378 : edge e;
8393 : 378 : struct omp_region *inner;
8394 : 378 : unsigned i, casei;
8395 : 378 : bool exit_reachable = region->cont != NULL;
8396 : :
8397 : 378 : gcc_assert (region->exit != NULL);
8398 : 378 : entry_bb = region->entry;
8399 : 378 : l0_bb = single_succ (entry_bb);
8400 : 378 : l1_bb = region->cont;
8401 : 378 : l2_bb = region->exit;
8402 : 719 : if (single_pred_p (l2_bb) && single_pred (l2_bb) == l0_bb)
8403 : 314 : l2 = gimple_block_label (l2_bb);
8404 : : else
8405 : : {
8406 : : /* This can happen if there are reductions. */
8407 : 64 : len = EDGE_COUNT (l0_bb->succs);
8408 : 64 : gcc_assert (len > 0);
8409 : 64 : e = EDGE_SUCC (l0_bb, len - 1);
8410 : 64 : si = gsi_last_nondebug_bb (e->dest);
8411 : 64 : l2 = NULL_TREE;
8412 : 64 : if (gsi_end_p (si)
8413 : 64 : || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
8414 : 64 : l2 = gimple_block_label (e->dest);
8415 : : else
8416 : 0 : FOR_EACH_EDGE (e, ei, l0_bb->succs)
8417 : : {
8418 : 0 : si = gsi_last_nondebug_bb (e->dest);
8419 : 0 : if (gsi_end_p (si)
8420 : 0 : || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
8421 : : {
8422 : 0 : l2 = gimple_block_label (e->dest);
8423 : 0 : break;
8424 : : }
8425 : : }
8426 : : }
8427 : 378 : if (exit_reachable)
8428 : 332 : default_bb = create_empty_bb (l1_bb->prev_bb);
8429 : : else
8430 : 46 : default_bb = create_empty_bb (l0_bb);
8431 : :
8432 : : /* We will build a switch() with enough cases for all the
8433 : : GIMPLE_OMP_SECTION regions, a '0' case to handle the end of more work
8434 : : and a default case to abort if something goes wrong. */
8435 : 378 : len = EDGE_COUNT (l0_bb->succs);
8436 : :
8437 : : /* Use vec::quick_push on label_vec throughout, since we know the size
8438 : : in advance. */
8439 : 378 : auto_vec<tree> label_vec (len);
8440 : :
8441 : : /* The call to GOMP_sections_start goes in ENTRY_BB, replacing the
8442 : : GIMPLE_OMP_SECTIONS statement. */
8443 : 378 : si = gsi_last_nondebug_bb (entry_bb);
8444 : 378 : sections_stmt = as_a <gomp_sections *> (gsi_stmt (si));
8445 : 378 : gcc_assert (gimple_code (sections_stmt) == GIMPLE_OMP_SECTIONS);
8446 : 378 : vin = gimple_omp_sections_control (sections_stmt);
8447 : 378 : tree clauses = gimple_omp_sections_clauses (sections_stmt);
8448 : 378 : tree reductmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
8449 : 378 : tree condtmp = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
8450 : 378 : tree cond_var = NULL_TREE;
8451 : 378 : if (reductmp || condtmp)
8452 : : {
8453 : 18 : tree reductions = null_pointer_node, mem = null_pointer_node;
8454 : 18 : tree memv = NULL_TREE, condtemp = NULL_TREE;
8455 : 18 : gimple_stmt_iterator gsi = gsi_none ();
8456 : 18 : gimple *g = NULL;
8457 : 18 : if (reductmp)
8458 : : {
8459 : 8 : reductions = OMP_CLAUSE_DECL (reductmp);
8460 : 8 : gcc_assert (TREE_CODE (reductions) == SSA_NAME);
8461 : 8 : g = SSA_NAME_DEF_STMT (reductions);
8462 : 8 : reductions = gimple_assign_rhs1 (g);
8463 : 8 : OMP_CLAUSE_DECL (reductmp) = reductions;
8464 : 8 : gsi = gsi_for_stmt (g);
8465 : : }
8466 : : else
8467 : 10 : gsi = si;
8468 : 18 : if (condtmp)
8469 : : {
8470 : 12 : condtemp = OMP_CLAUSE_DECL (condtmp);
8471 : 12 : tree c = omp_find_clause (OMP_CLAUSE_CHAIN (condtmp),
8472 : : OMP_CLAUSE__CONDTEMP_);
8473 : 12 : cond_var = OMP_CLAUSE_DECL (c);
8474 : 12 : tree type = TREE_TYPE (condtemp);
8475 : 12 : memv = create_tmp_var (type);
8476 : 12 : TREE_ADDRESSABLE (memv) = 1;
8477 : 12 : unsigned cnt = 0;
8478 : 74 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
8479 : 62 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8480 : 62 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8481 : 24 : ++cnt;
8482 : 12 : unsigned HOST_WIDE_INT sz
8483 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type))) * cnt;
8484 : 12 : expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
8485 : : false);
8486 : 12 : mem = build_fold_addr_expr (memv);
8487 : : }
8488 : 18 : t = build_int_cst (unsigned_type_node, len - 1);
8489 : 18 : u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS2_START);
8490 : 18 : stmt = gimple_build_call (u, 3, t, reductions, mem);
8491 : 18 : gimple_call_set_lhs (stmt, vin);
8492 : 18 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8493 : 18 : if (condtmp)
8494 : : {
8495 : 12 : expand_omp_build_assign (&gsi, condtemp, memv, false);
8496 : 12 : tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
8497 : 12 : vin, build_one_cst (TREE_TYPE (cond_var)));
8498 : 12 : expand_omp_build_assign (&gsi, cond_var, t, false);
8499 : : }
8500 : 18 : if (reductmp)
8501 : : {
8502 : 8 : gsi_remove (&gsi, true);
8503 : 8 : release_ssa_name (gimple_assign_lhs (g));
8504 : : }
8505 : : }
8506 : 360 : else if (!is_combined_parallel (region))
8507 : : {
8508 : : /* If we are not inside a combined parallel+sections region,
8509 : : call GOMP_sections_start. */
8510 : 248 : t = build_int_cst (unsigned_type_node, len - 1);
8511 : 248 : u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_START);
8512 : 248 : stmt = gimple_build_call (u, 1, t);
8513 : : }
8514 : : else
8515 : : {
8516 : : /* Otherwise, call GOMP_sections_next. */
8517 : 112 : u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
8518 : 112 : stmt = gimple_build_call (u, 0);
8519 : : }
8520 : 378 : if (!reductmp && !condtmp)
8521 : : {
8522 : 360 : gimple_call_set_lhs (stmt, vin);
8523 : 360 : gsi_insert_after (&si, stmt, GSI_SAME_STMT);
8524 : : }
8525 : 378 : gsi_remove (&si, true);
8526 : :
8527 : : /* The switch() statement replacing GIMPLE_OMP_SECTIONS_SWITCH goes in
8528 : : L0_BB. */
8529 : 378 : switch_si = gsi_last_nondebug_bb (l0_bb);
8530 : 378 : gcc_assert (gimple_code (gsi_stmt (switch_si)) == GIMPLE_OMP_SECTIONS_SWITCH);
8531 : 378 : if (exit_reachable)
8532 : : {
8533 : 332 : cont = as_a <gomp_continue *> (last_nondebug_stmt (l1_bb));
8534 : 332 : gcc_assert (gimple_code (cont) == GIMPLE_OMP_CONTINUE);
8535 : 332 : vmain = gimple_omp_continue_control_use (cont);
8536 : 332 : vnext = gimple_omp_continue_control_def (cont);
8537 : : }
8538 : : else
8539 : : {
8540 : : vmain = vin;
8541 : : vnext = NULL_TREE;
8542 : : }
8543 : :
8544 : 378 : t = build_case_label (build_int_cst (unsigned_type_node, 0), NULL, l2);
8545 : 378 : label_vec.quick_push (t);
8546 : 378 : i = 1;
8547 : :
8548 : : /* Convert each GIMPLE_OMP_SECTION into a CASE_LABEL_EXPR. */
8549 : 378 : for (inner = region->inner, casei = 1;
8550 : 1260 : inner;
8551 : 882 : inner = inner->next, i++, casei++)
8552 : : {
8553 : 882 : basic_block s_entry_bb, s_exit_bb;
8554 : :
8555 : : /* Skip optional reduction region. */
8556 : 882 : if (inner->type == GIMPLE_OMP_ATOMIC_LOAD)
8557 : : {
8558 : 27 : --i;
8559 : 27 : --casei;
8560 : 27 : continue;
8561 : : }
8562 : :
8563 : 855 : s_entry_bb = inner->entry;
8564 : 855 : s_exit_bb = inner->exit;
8565 : :
8566 : 855 : t = gimple_block_label (s_entry_bb);
8567 : 855 : u = build_int_cst (unsigned_type_node, casei);
8568 : 855 : u = build_case_label (u, NULL, t);
8569 : 855 : label_vec.quick_push (u);
8570 : :
8571 : 855 : si = gsi_last_nondebug_bb (s_entry_bb);
8572 : 855 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SECTION);
8573 : 855 : gcc_assert (i < len || gimple_omp_section_last_p (gsi_stmt (si)));
8574 : 855 : gsi_remove (&si, true);
8575 : 855 : single_succ_edge (s_entry_bb)->flags = EDGE_FALLTHRU;
8576 : :
8577 : 855 : if (s_exit_bb == NULL)
8578 : 122 : continue;
8579 : :
8580 : 733 : si = gsi_last_nondebug_bb (s_exit_bb);
8581 : 733 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
8582 : 733 : gsi_remove (&si, true);
8583 : :
8584 : 733 : single_succ_edge (s_exit_bb)->flags = EDGE_FALLTHRU;
8585 : : }
8586 : :
8587 : : /* Error handling code goes in DEFAULT_BB. */
8588 : 378 : t = gimple_block_label (default_bb);
8589 : 378 : u = build_case_label (NULL, NULL, t);
8590 : 378 : make_edge (l0_bb, default_bb, 0);
8591 : 378 : add_bb_to_loop (default_bb, current_loops->tree_root);
8592 : :
8593 : 378 : stmt = gimple_build_switch (vmain, u, label_vec);
8594 : 378 : gsi_insert_after (&switch_si, stmt, GSI_SAME_STMT);
8595 : 378 : gsi_remove (&switch_si, true);
8596 : :
8597 : 378 : si = gsi_start_bb (default_bb);
8598 : 378 : stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
8599 : 378 : gsi_insert_after (&si, stmt, GSI_CONTINUE_LINKING);
8600 : :
8601 : 378 : if (exit_reachable)
8602 : : {
8603 : 332 : tree bfn_decl;
8604 : :
8605 : : /* Code to get the next section goes in L1_BB. */
8606 : 332 : si = gsi_last_nondebug_bb (l1_bb);
8607 : 332 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CONTINUE);
8608 : :
8609 : 332 : bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
8610 : 332 : stmt = gimple_build_call (bfn_decl, 0);
8611 : 332 : gimple_call_set_lhs (stmt, vnext);
8612 : 332 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
8613 : 332 : if (cond_var)
8614 : : {
8615 : 12 : tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
8616 : 12 : vnext, build_one_cst (TREE_TYPE (cond_var)));
8617 : 12 : expand_omp_build_assign (&si, cond_var, t, false);
8618 : : }
8619 : 332 : gsi_remove (&si, true);
8620 : :
8621 : 332 : single_succ_edge (l1_bb)->flags = EDGE_FALLTHRU;
8622 : : }
8623 : :
8624 : : /* Cleanup function replaces GIMPLE_OMP_RETURN in EXIT_BB. */
8625 : 378 : si = gsi_last_nondebug_bb (l2_bb);
8626 : 378 : if (gimple_omp_return_nowait_p (gsi_stmt (si)))
8627 : 251 : t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_NOWAIT);
8628 : 127 : else if (gimple_omp_return_lhs (gsi_stmt (si)))
8629 : 0 : t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_CANCEL);
8630 : : else
8631 : 127 : t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END);
8632 : 378 : stmt = gimple_build_call (t, 0);
8633 : 378 : if (gimple_omp_return_lhs (gsi_stmt (si)))
8634 : 0 : gimple_call_set_lhs (stmt, gimple_omp_return_lhs (gsi_stmt (si)));
8635 : 378 : gsi_insert_after (&si, stmt, GSI_SAME_STMT);
8636 : 378 : gsi_remove (&si, true);
8637 : :
8638 : 378 : set_immediate_dominator (CDI_DOMINATORS, default_bb, l0_bb);
8639 : 378 : }
8640 : :
8641 : : /* Expand code for an OpenMP single or scope directive. We've already expanded
8642 : : much of the code, here we simply place the GOMP_barrier call. */
8643 : :
8644 : : static void
8645 : 1261 : expand_omp_single (struct omp_region *region)
8646 : : {
8647 : 1261 : basic_block entry_bb, exit_bb;
8648 : 1261 : gimple_stmt_iterator si;
8649 : :
8650 : 1261 : entry_bb = region->entry;
8651 : 1261 : exit_bb = region->exit;
8652 : :
8653 : 1261 : si = gsi_last_nondebug_bb (entry_bb);
8654 : 1261 : enum gimple_code code = gimple_code (gsi_stmt (si));
8655 : 1261 : gcc_assert (code == GIMPLE_OMP_SINGLE || code == GIMPLE_OMP_SCOPE);
8656 : 1261 : gsi_remove (&si, true);
8657 : 1261 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
8658 : :
8659 : 1261 : if (exit_bb == NULL)
8660 : : {
8661 : 8 : gcc_assert (code == GIMPLE_OMP_SCOPE);
8662 : 8 : return;
8663 : : }
8664 : :
8665 : 1253 : si = gsi_last_nondebug_bb (exit_bb);
8666 : 1253 : if (!gimple_omp_return_nowait_p (gsi_stmt (si)))
8667 : : {
8668 : 978 : tree t = gimple_omp_return_lhs (gsi_stmt (si));
8669 : 978 : gsi_insert_after (&si, omp_build_barrier (t), GSI_SAME_STMT);
8670 : : }
8671 : 1253 : gsi_remove (&si, true);
8672 : 1253 : single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
8673 : : }
8674 : :
8675 : : /* Generic expansion for OpenMP synchronization directives: master,
8676 : : ordered and critical. All we need to do here is remove the entry
8677 : : and exit markers for REGION. */
8678 : :
8679 : : static void
8680 : 10760 : expand_omp_synch (struct omp_region *region)
8681 : : {
8682 : 10760 : basic_block entry_bb, exit_bb;
8683 : 10760 : gimple_stmt_iterator si;
8684 : :
8685 : 10760 : entry_bb = region->entry;
8686 : 10760 : exit_bb = region->exit;
8687 : :
8688 : 10760 : si = gsi_last_nondebug_bb (entry_bb);
8689 : 10760 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SINGLE
8690 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASTER
8691 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASKED
8692 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TASKGROUP
8693 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ORDERED
8694 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CRITICAL
8695 : : || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS);
8696 : 10760 : if (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS
8697 : 10760 : && gimple_omp_teams_host (as_a <gomp_teams *> (gsi_stmt (si))))
8698 : : {
8699 : 2535 : expand_omp_taskreg (region);
8700 : 2535 : return;
8701 : : }
8702 : 8225 : gsi_remove (&si, true);
8703 : 8225 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
8704 : :
8705 : 8225 : if (exit_bb)
8706 : : {
8707 : 7667 : si = gsi_last_nondebug_bb (exit_bb);
8708 : 7667 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
8709 : 7667 : gsi_remove (&si, true);
8710 : 7667 : single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
8711 : : }
8712 : : }
8713 : :
8714 : : /* Translate enum omp_memory_order to enum memmodel for the embedded
8715 : : fail clause in there. */
8716 : :
8717 : : static enum memmodel
8718 : 2847 : omp_memory_order_to_fail_memmodel (enum omp_memory_order mo)
8719 : : {
8720 : 2847 : switch (mo & OMP_FAIL_MEMORY_ORDER_MASK)
8721 : : {
8722 : 2576 : case OMP_FAIL_MEMORY_ORDER_UNSPECIFIED:
8723 : 2576 : switch (mo & OMP_MEMORY_ORDER_MASK)
8724 : : {
8725 : : case OMP_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
8726 : : case OMP_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
8727 : : case OMP_MEMORY_ORDER_RELEASE: return MEMMODEL_RELAXED;
8728 : : case OMP_MEMORY_ORDER_ACQ_REL: return MEMMODEL_ACQUIRE;
8729 : : case OMP_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
8730 : 0 : default: break;
8731 : : }
8732 : 0 : gcc_unreachable ();
8733 : : case OMP_FAIL_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
8734 : : case OMP_FAIL_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
8735 : : case OMP_FAIL_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
8736 : 0 : default: gcc_unreachable ();
8737 : : }
8738 : : }
8739 : :
8740 : : /* Translate enum omp_memory_order to enum memmodel. The two enums
8741 : : are using different numbers so that OMP_MEMORY_ORDER_UNSPECIFIED
8742 : : is 0 and omp_memory_order has the fail mode encoded in it too. */
8743 : :
8744 : : static enum memmodel
8745 : 9849 : omp_memory_order_to_memmodel (enum omp_memory_order mo)
8746 : : {
8747 : 9849 : enum memmodel ret, fail_ret;
8748 : 9849 : switch (mo & OMP_MEMORY_ORDER_MASK)
8749 : : {
8750 : : case OMP_MEMORY_ORDER_RELAXED: ret = MEMMODEL_RELAXED; break;
8751 : : case OMP_MEMORY_ORDER_ACQUIRE: ret = MEMMODEL_ACQUIRE; break;
8752 : : case OMP_MEMORY_ORDER_RELEASE: ret = MEMMODEL_RELEASE; break;
8753 : : case OMP_MEMORY_ORDER_ACQ_REL: ret = MEMMODEL_ACQ_REL; break;
8754 : : case OMP_MEMORY_ORDER_SEQ_CST: ret = MEMMODEL_SEQ_CST; break;
8755 : 0 : default: gcc_unreachable ();
8756 : : }
8757 : : /* If we drop the -Winvalid-memory-model warning for C++17 P0418R2,
8758 : : we can just return ret here unconditionally. Otherwise, work around
8759 : : it here and make sure fail memmodel is not stronger. */
8760 : 9849 : if ((mo & OMP_FAIL_MEMORY_ORDER_MASK) == OMP_FAIL_MEMORY_ORDER_UNSPECIFIED)
8761 : : return ret;
8762 : 142 : fail_ret = omp_memory_order_to_fail_memmodel (mo);
8763 : 142 : if (fail_ret > ret)
8764 : 13 : return fail_ret;
8765 : : return ret;
8766 : : }
8767 : :
8768 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8769 : : operation as a normal volatile load. */
8770 : :
8771 : : static bool
8772 : 1183 : expand_omp_atomic_load (basic_block load_bb, tree addr,
8773 : : tree loaded_val, int index)
8774 : : {
8775 : 1183 : enum built_in_function tmpbase;
8776 : 1183 : gimple_stmt_iterator gsi;
8777 : 1183 : basic_block store_bb;
8778 : 1183 : location_t loc;
8779 : 1183 : gimple *stmt;
8780 : 1183 : tree decl, type, itype;
8781 : :
8782 : 1183 : gsi = gsi_last_nondebug_bb (load_bb);
8783 : 1183 : stmt = gsi_stmt (gsi);
8784 : 1183 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
8785 : 1183 : loc = gimple_location (stmt);
8786 : :
8787 : : /* ??? If the target does not implement atomic_load_optab[mode], and mode
8788 : : is smaller than word size, then expand_atomic_load assumes that the load
8789 : : is atomic. We could avoid the builtin entirely in this case. */
8790 : :
8791 : 1183 : tmpbase = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
8792 : 1183 : decl = builtin_decl_explicit (tmpbase);
8793 : 1183 : if (decl == NULL_TREE)
8794 : : return false;
8795 : :
8796 : 1183 : type = TREE_TYPE (loaded_val);
8797 : 1183 : itype = TREE_TYPE (TREE_TYPE (decl));
8798 : :
8799 : 1183 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
8800 : 1183 : tree mo = build_int_cst (integer_type_node,
8801 : 1183 : omp_memory_order_to_memmodel (omo));
8802 : 1183 : gcall *call = gimple_build_call (decl, 2, addr, mo);
8803 : 1183 : gimple_set_location (call, loc);
8804 : 2366 : gimple_set_vuse (call, gimple_vuse (stmt));
8805 : 1183 : gimple *repl;
8806 : 1183 : if (!useless_type_conversion_p (type, itype))
8807 : : {
8808 : 1181 : tree lhs = make_ssa_name (itype);
8809 : 1181 : gimple_call_set_lhs (call, lhs);
8810 : 1181 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8811 : 1181 : repl = gimple_build_assign (loaded_val,
8812 : : build1 (VIEW_CONVERT_EXPR, type, lhs));
8813 : 1181 : gimple_set_location (repl, loc);
8814 : : }
8815 : : else
8816 : : {
8817 : 2 : gimple_call_set_lhs (call, loaded_val);
8818 : : repl = call;
8819 : : }
8820 : 1183 : gsi_replace (&gsi, repl, true);
8821 : :
8822 : 1183 : store_bb = single_succ (load_bb);
8823 : 1183 : gsi = gsi_last_nondebug_bb (store_bb);
8824 : 1183 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
8825 : 1183 : gsi_remove (&gsi, true);
8826 : :
8827 : 1183 : return true;
8828 : : }
8829 : :
8830 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8831 : : operation as a normal volatile store. */
8832 : :
8833 : : static bool
8834 : 861 : expand_omp_atomic_store (basic_block load_bb, tree addr,
8835 : : tree loaded_val, tree stored_val, int index)
8836 : : {
8837 : 861 : enum built_in_function tmpbase;
8838 : 861 : gimple_stmt_iterator gsi;
8839 : 861 : basic_block store_bb = single_succ (load_bb);
8840 : 861 : location_t loc;
8841 : 861 : gimple *stmt;
8842 : 861 : tree decl, type, itype;
8843 : 861 : machine_mode imode;
8844 : 861 : bool exchange;
8845 : :
8846 : 861 : gsi = gsi_last_nondebug_bb (load_bb);
8847 : 861 : stmt = gsi_stmt (gsi);
8848 : 861 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
8849 : :
8850 : : /* If the load value is needed, then this isn't a store but an exchange. */
8851 : 861 : exchange = gimple_omp_atomic_need_value_p (stmt);
8852 : :
8853 : 861 : gsi = gsi_last_nondebug_bb (store_bb);
8854 : 861 : stmt = gsi_stmt (gsi);
8855 : 861 : gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE);
8856 : 861 : loc = gimple_location (stmt);
8857 : :
8858 : : /* ??? If the target does not implement atomic_store_optab[mode], and mode
8859 : : is smaller than word size, then expand_atomic_store assumes that the store
8860 : : is atomic. We could avoid the builtin entirely in this case. */
8861 : :
8862 : 861 : tmpbase = (exchange ? BUILT_IN_ATOMIC_EXCHANGE_N : BUILT_IN_ATOMIC_STORE_N);
8863 : 861 : tmpbase = (enum built_in_function) ((int) tmpbase + index + 1);
8864 : 861 : decl = builtin_decl_explicit (tmpbase);
8865 : 861 : if (decl == NULL_TREE)
8866 : : return false;
8867 : :
8868 : 861 : type = TREE_TYPE (stored_val);
8869 : :
8870 : : /* Dig out the type of the function's second argument. */
8871 : 861 : itype = TREE_TYPE (decl);
8872 : 861 : itype = TYPE_ARG_TYPES (itype);
8873 : 861 : itype = TREE_CHAIN (itype);
8874 : 861 : itype = TREE_VALUE (itype);
8875 : 861 : imode = TYPE_MODE (itype);
8876 : :
8877 : 861 : if (exchange && !can_atomic_exchange_p (imode, true))
8878 : : return false;
8879 : :
8880 : 861 : if (!useless_type_conversion_p (itype, type))
8881 : 861 : stored_val = fold_build1_loc (loc, VIEW_CONVERT_EXPR, itype, stored_val);
8882 : 861 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
8883 : 861 : tree mo = build_int_cst (integer_type_node,
8884 : 861 : omp_memory_order_to_memmodel (omo));
8885 : 861 : stored_val = force_gimple_operand_gsi (&gsi, stored_val, true, NULL_TREE,
8886 : : true, GSI_SAME_STMT);
8887 : 861 : gcall *call = gimple_build_call (decl, 3, addr, stored_val, mo);
8888 : 861 : gimple_set_location (call, loc);
8889 : 1722 : gimple_set_vuse (call, gimple_vuse (stmt));
8890 : 1722 : gimple_set_vdef (call, gimple_vdef (stmt));
8891 : :
8892 : 861 : gimple *repl = call;
8893 : 861 : if (exchange)
8894 : : {
8895 : 88 : if (!useless_type_conversion_p (type, itype))
8896 : : {
8897 : 88 : tree lhs = make_ssa_name (itype);
8898 : 88 : gimple_call_set_lhs (call, lhs);
8899 : 88 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8900 : 88 : repl = gimple_build_assign (loaded_val,
8901 : : build1 (VIEW_CONVERT_EXPR, type, lhs));
8902 : 88 : gimple_set_location (repl, loc);
8903 : : }
8904 : : else
8905 : 0 : gimple_call_set_lhs (call, loaded_val);
8906 : : }
8907 : 861 : gsi_replace (&gsi, repl, true);
8908 : :
8909 : : /* Remove the GIMPLE_OMP_ATOMIC_LOAD that we verified above. */
8910 : 861 : gsi = gsi_last_nondebug_bb (load_bb);
8911 : 861 : gsi_remove (&gsi, true);
8912 : :
8913 : 861 : return true;
8914 : : }
8915 : :
8916 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8917 : : operation as a __atomic_fetch_op builtin. INDEX is log2 of the
8918 : : size of the data type, and thus usable to find the index of the builtin
8919 : : decl. Returns false if the expression is not of the proper form. */
8920 : :
8921 : : static bool
8922 : 5743 : expand_omp_atomic_fetch_op (basic_block load_bb,
8923 : : tree addr, tree loaded_val,
8924 : : tree stored_val, int index)
8925 : : {
8926 : 5743 : enum built_in_function oldbase, newbase, tmpbase;
8927 : 5743 : tree decl, itype, call;
8928 : 5743 : tree lhs, rhs;
8929 : 5743 : basic_block store_bb = single_succ (load_bb);
8930 : 5743 : gimple_stmt_iterator gsi;
8931 : 5743 : gimple *stmt;
8932 : 5743 : location_t loc;
8933 : 5743 : enum tree_code code;
8934 : 5743 : bool need_old, need_new;
8935 : 5743 : machine_mode imode;
8936 : :
8937 : : /* We expect to find the following sequences:
8938 : :
8939 : : load_bb:
8940 : : GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
8941 : :
8942 : : store_bb:
8943 : : val = tmp OP something; (or: something OP tmp)
8944 : : GIMPLE_OMP_STORE (val)
8945 : :
8946 : : ???FIXME: Allow a more flexible sequence.
8947 : : Perhaps use data flow to pick the statements.
8948 : :
8949 : : */
8950 : :
8951 : 5743 : gsi = gsi_after_labels (store_bb);
8952 : 5743 : stmt = gsi_stmt (gsi);
8953 : 5743 : if (is_gimple_debug (stmt))
8954 : : {
8955 : 0 : gsi_next_nondebug (&gsi);
8956 : 0 : if (gsi_end_p (gsi))
8957 : : return false;
8958 : 5743 : stmt = gsi_stmt (gsi);
8959 : : }
8960 : 5743 : loc = gimple_location (stmt);
8961 : 5743 : if (!is_gimple_assign (stmt))
8962 : : return false;
8963 : 5743 : gsi_next_nondebug (&gsi);
8964 : 5743 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_OMP_ATOMIC_STORE)
8965 : : return false;
8966 : 5100 : need_new = gimple_omp_atomic_need_value_p (gsi_stmt (gsi));
8967 : 5100 : need_old = gimple_omp_atomic_need_value_p (last_nondebug_stmt (load_bb));
8968 : 5100 : enum omp_memory_order omo
8969 : 5100 : = gimple_omp_atomic_memory_order (last_nondebug_stmt (load_bb));
8970 : 5100 : enum memmodel mo = omp_memory_order_to_memmodel (omo);
8971 : 5100 : gcc_checking_assert (!need_old || !need_new);
8972 : :
8973 : 5100 : if (!operand_equal_p (gimple_assign_lhs (stmt), stored_val, 0))
8974 : : return false;
8975 : :
8976 : : /* Check for one of the supported fetch-op operations. */
8977 : 5100 : code = gimple_assign_rhs_code (stmt);
8978 : 5100 : switch (code)
8979 : : {
8980 : : case PLUS_EXPR:
8981 : : case POINTER_PLUS_EXPR:
8982 : : oldbase = BUILT_IN_ATOMIC_FETCH_ADD_N;
8983 : : newbase = BUILT_IN_ATOMIC_ADD_FETCH_N;
8984 : : break;
8985 : 67 : case MINUS_EXPR:
8986 : 67 : oldbase = BUILT_IN_ATOMIC_FETCH_SUB_N;
8987 : 67 : newbase = BUILT_IN_ATOMIC_SUB_FETCH_N;
8988 : 67 : break;
8989 : 100 : case BIT_AND_EXPR:
8990 : 100 : oldbase = BUILT_IN_ATOMIC_FETCH_AND_N;
8991 : 100 : newbase = BUILT_IN_ATOMIC_AND_FETCH_N;
8992 : 100 : break;
8993 : 215 : case BIT_IOR_EXPR:
8994 : 215 : oldbase = BUILT_IN_ATOMIC_FETCH_OR_N;
8995 : 215 : newbase = BUILT_IN_ATOMIC_OR_FETCH_N;
8996 : 215 : break;
8997 : 99 : case BIT_XOR_EXPR:
8998 : 99 : oldbase = BUILT_IN_ATOMIC_FETCH_XOR_N;
8999 : 99 : newbase = BUILT_IN_ATOMIC_XOR_FETCH_N;
9000 : 99 : break;
9001 : : default:
9002 : : return false;
9003 : : }
9004 : :
9005 : : /* Make sure the expression is of the proper form. */
9006 : 4489 : if (operand_equal_p (gimple_assign_rhs1 (stmt), loaded_val, 0))
9007 : 4311 : rhs = gimple_assign_rhs2 (stmt);
9008 : 178 : else if (commutative_tree_code (gimple_assign_rhs_code (stmt))
9009 : 178 : && operand_equal_p (gimple_assign_rhs2 (stmt), loaded_val, 0))
9010 : 139 : rhs = gimple_assign_rhs1 (stmt);
9011 : : else
9012 : 39 : return false;
9013 : :
9014 : 8900 : tmpbase = ((enum built_in_function)
9015 : 4450 : ((need_new ? newbase : oldbase) + index + 1));
9016 : 4450 : decl = builtin_decl_explicit (tmpbase);
9017 : 4450 : if (decl == NULL_TREE)
9018 : : return false;
9019 : 4450 : itype = TREE_TYPE (TREE_TYPE (decl));
9020 : 4450 : imode = TYPE_MODE (itype);
9021 : :
9022 : : /* We could test all of the various optabs involved, but the fact of the
9023 : : matter is that (with the exception of i486 vs i586 and xadd) all targets
9024 : : that support any atomic operaton optab also implements compare-and-swap.
9025 : : Let optabs.cc take care of expanding any compare-and-swap loop. */
9026 : 4450 : if (!can_compare_and_swap_p (imode, true) || !can_atomic_load_p (imode))
9027 : 0 : return false;
9028 : :
9029 : 4450 : gsi = gsi_last_nondebug_bb (load_bb);
9030 : 4450 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_LOAD);
9031 : :
9032 : : /* OpenMP does not imply any barrier-like semantics on its atomic ops.
9033 : : It only requires that the operation happen atomically. Thus we can
9034 : : use the RELAXED memory model. */
9035 : 4450 : call = build_call_expr_loc (loc, decl, 3, addr,
9036 : : fold_convert_loc (loc, itype, rhs),
9037 : 4450 : build_int_cst (NULL, mo));
9038 : :
9039 : 4450 : if (need_old || need_new)
9040 : : {
9041 : 500 : lhs = need_old ? loaded_val : stored_val;
9042 : 500 : call = fold_convert_loc (loc, TREE_TYPE (lhs), call);
9043 : 500 : call = build2_loc (loc, MODIFY_EXPR, void_type_node, lhs, call);
9044 : : }
9045 : : else
9046 : 3950 : call = fold_convert_loc (loc, void_type_node, call);
9047 : 4450 : force_gimple_operand_gsi (&gsi, call, true, NULL_TREE, true, GSI_SAME_STMT);
9048 : 4450 : gsi_remove (&gsi, true);
9049 : :
9050 : 4450 : gsi = gsi_last_nondebug_bb (store_bb);
9051 : 4450 : gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
9052 : 4450 : gsi_remove (&gsi, true);
9053 : 4450 : gsi = gsi_last_nondebug_bb (store_bb);
9054 : 4450 : stmt = gsi_stmt (gsi);
9055 : 4450 : gsi_remove (&gsi, true);
9056 : :
9057 : 4450 : if (gimple_in_ssa_p (cfun))
9058 : 29 : release_defs (stmt);
9059 : :
9060 : : return true;
9061 : : }
9062 : :
9063 : : /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
9064 : : compare and exchange as an ATOMIC_COMPARE_EXCHANGE internal function.
9065 : : Returns false if the expression is not of the proper form. */
9066 : :
9067 : : static bool
9068 : 2114 : expand_omp_atomic_cas (basic_block load_bb, tree addr,
9069 : : tree loaded_val, tree stored_val, int index)
9070 : : {
9071 : : /* We expect to find the following sequences:
9072 : :
9073 : : load_bb:
9074 : : GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
9075 : :
9076 : : store_bb:
9077 : : val = tmp == e ? d : tmp;
9078 : : GIMPLE_OMP_ATOMIC_STORE (val)
9079 : :
9080 : : or in store_bb instead:
9081 : : tmp2 = tmp == e;
9082 : : val = tmp2 ? d : tmp;
9083 : : GIMPLE_OMP_ATOMIC_STORE (val)
9084 : :
9085 : : or:
9086 : : tmp3 = VIEW_CONVERT_EXPR<integral_type>(tmp);
9087 : : val = e == tmp3 ? d : tmp;
9088 : : GIMPLE_OMP_ATOMIC_STORE (val)
9089 : :
9090 : : etc. */
9091 : :
9092 : :
9093 : 2114 : basic_block store_bb = single_succ (load_bb);
9094 : 2114 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (store_bb);
9095 : 2114 : gimple *store_stmt = gsi_stmt (gsi);
9096 : 2114 : if (!store_stmt || gimple_code (store_stmt) != GIMPLE_OMP_ATOMIC_STORE)
9097 : : return false;
9098 : 2114 : gsi_prev_nondebug (&gsi);
9099 : 2114 : if (gsi_end_p (gsi))
9100 : : return false;
9101 : 2108 : gimple *condexpr_stmt = gsi_stmt (gsi);
9102 : 2108 : if (!is_gimple_assign (condexpr_stmt)
9103 : 2108 : || gimple_assign_rhs_code (condexpr_stmt) != COND_EXPR)
9104 : : return false;
9105 : 408 : if (!operand_equal_p (gimple_assign_lhs (condexpr_stmt), stored_val, 0))
9106 : : return false;
9107 : 408 : gimple *cond_stmt = NULL;
9108 : 408 : gimple *vce_stmt = NULL;
9109 : 408 : gsi_prev_nondebug (&gsi);
9110 : 408 : if (!gsi_end_p (gsi))
9111 : : {
9112 : 408 : cond_stmt = gsi_stmt (gsi);
9113 : 408 : if (!is_gimple_assign (cond_stmt))
9114 : : return false;
9115 : 408 : if (gimple_assign_rhs_code (cond_stmt) == EQ_EXPR)
9116 : : {
9117 : 323 : gsi_prev_nondebug (&gsi);
9118 : 323 : if (!gsi_end_p (gsi))
9119 : : {
9120 : 89 : vce_stmt = gsi_stmt (gsi);
9121 : 89 : if (!is_gimple_assign (vce_stmt)
9122 : 89 : || gimple_assign_rhs_code (vce_stmt) != VIEW_CONVERT_EXPR)
9123 : : return false;
9124 : : }
9125 : : }
9126 : 85 : else if (gimple_assign_rhs_code (cond_stmt) == VIEW_CONVERT_EXPR)
9127 : : std::swap (vce_stmt, cond_stmt);
9128 : : else
9129 : : return false;
9130 : 69 : if (vce_stmt)
9131 : : {
9132 : 69 : tree vce_rhs = gimple_assign_rhs1 (vce_stmt);
9133 : 69 : if (TREE_CODE (vce_rhs) != VIEW_CONVERT_EXPR
9134 : 69 : || !operand_equal_p (TREE_OPERAND (vce_rhs, 0), loaded_val))
9135 : 12 : return false;
9136 : 114 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vce_rhs))
9137 : 57 : || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (loaded_val))
9138 : 114 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (vce_rhs)),
9139 : 57 : TYPE_SIZE (TREE_TYPE (loaded_val))))
9140 : 0 : return false;
9141 : 57 : gsi_prev_nondebug (&gsi);
9142 : 57 : if (!gsi_end_p (gsi))
9143 : : return false;
9144 : : }
9145 : : }
9146 : 291 : tree cond = gimple_assign_rhs1 (condexpr_stmt);
9147 : 291 : tree cond_op1, cond_op2;
9148 : 291 : if (cond_stmt)
9149 : : {
9150 : : /* We should now always get a separate cond_stmt. */
9151 : 291 : if (!operand_equal_p (cond, gimple_assign_lhs (cond_stmt)))
9152 : : return false;
9153 : 291 : cond_op1 = gimple_assign_rhs1 (cond_stmt);
9154 : 291 : cond_op2 = gimple_assign_rhs2 (cond_stmt);
9155 : : }
9156 : 0 : else if (TREE_CODE (cond) != EQ_EXPR && TREE_CODE (cond) != NE_EXPR)
9157 : : return false;
9158 : : else
9159 : : {
9160 : 0 : cond_op1 = TREE_OPERAND (cond, 0);
9161 : 0 : cond_op2 = TREE_OPERAND (cond, 1);
9162 : : }
9163 : 291 : tree d;
9164 : 291 : if (TREE_CODE (cond) == NE_EXPR)
9165 : : {
9166 : 0 : if (!operand_equal_p (gimple_assign_rhs2 (condexpr_stmt), loaded_val))
9167 : : return false;
9168 : 0 : d = gimple_assign_rhs3 (condexpr_stmt);
9169 : : }
9170 : 291 : else if (!operand_equal_p (gimple_assign_rhs3 (condexpr_stmt), loaded_val))
9171 : : return false;
9172 : : else
9173 : 291 : d = gimple_assign_rhs2 (condexpr_stmt);
9174 : 291 : tree e = vce_stmt ? gimple_assign_lhs (vce_stmt) : loaded_val;
9175 : 291 : if (operand_equal_p (e, cond_op1))
9176 : : e = cond_op2;
9177 : 21 : else if (operand_equal_p (e, cond_op2))
9178 : : e = cond_op1;
9179 : : else
9180 : : return false;
9181 : :
9182 : 291 : location_t loc = gimple_location (store_stmt);
9183 : 291 : gimple *load_stmt = last_nondebug_stmt (load_bb);
9184 : 291 : bool need_new = gimple_omp_atomic_need_value_p (store_stmt);
9185 : 291 : bool need_old = gimple_omp_atomic_need_value_p (load_stmt);
9186 : 291 : bool weak = gimple_omp_atomic_weak_p (load_stmt);
9187 : 291 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (load_stmt);
9188 : 291 : tree mo = build_int_cst (NULL, omp_memory_order_to_memmodel (omo));
9189 : 291 : tree fmo = build_int_cst (NULL, omp_memory_order_to_fail_memmodel (omo));
9190 : 291 : gcc_checking_assert (!need_old || !need_new);
9191 : :
9192 : 291 : enum built_in_function fncode
9193 : : = (enum built_in_function) ((int) BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
9194 : 291 : + index + 1);
9195 : 291 : tree cmpxchg = builtin_decl_explicit (fncode);
9196 : 291 : if (cmpxchg == NULL_TREE)
9197 : : return false;
9198 : 291 : tree itype = TREE_TYPE (TREE_TYPE (cmpxchg));
9199 : :
9200 : 291 : if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
9201 : 291 : || !can_atomic_load_p (TYPE_MODE (itype)))
9202 : 1 : return false;
9203 : :
9204 : 290 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9205 : 290 : if (SCALAR_FLOAT_TYPE_P (type) && !vce_stmt)
9206 : : return false;
9207 : :
9208 : 289 : gsi = gsi_for_stmt (store_stmt);
9209 : 289 : if (!useless_type_conversion_p (itype, TREE_TYPE (e)))
9210 : : {
9211 : 232 : tree ne = create_tmp_reg (itype);
9212 : 232 : gimple *g = gimple_build_assign (ne, NOP_EXPR, e);
9213 : 232 : gimple_set_location (g, loc);
9214 : 232 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9215 : 232 : e = ne;
9216 : : }
9217 : 289 : if (!useless_type_conversion_p (itype, TREE_TYPE (d)))
9218 : : {
9219 : 289 : tree nd = create_tmp_reg (itype);
9220 : 289 : enum tree_code code;
9221 : 289 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (d)))
9222 : : {
9223 : 57 : code = VIEW_CONVERT_EXPR;
9224 : 57 : d = build1 (VIEW_CONVERT_EXPR, itype, d);
9225 : : }
9226 : : else
9227 : : code = NOP_EXPR;
9228 : 289 : gimple *g = gimple_build_assign (nd, code, d);
9229 : 289 : gimple_set_location (g, loc);
9230 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9231 : 289 : d = nd;
9232 : : }
9233 : :
9234 : 289 : tree ctype = build_complex_type (itype);
9235 : 289 : int flag = int_size_in_bytes (itype) + (weak ? 256 : 0);
9236 : 289 : gimple *g
9237 : 289 : = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6, addr, e, d,
9238 : 289 : build_int_cst (integer_type_node, flag),
9239 : : mo, fmo);
9240 : 289 : tree cres = create_tmp_reg (ctype);
9241 : 289 : gimple_call_set_lhs (g, cres);
9242 : 289 : gimple_set_location (g, loc);
9243 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9244 : :
9245 : 289 : if (cond_stmt || need_old || need_new)
9246 : : {
9247 : 289 : tree im = create_tmp_reg (itype);
9248 : 289 : g = gimple_build_assign (im, IMAGPART_EXPR,
9249 : : build1 (IMAGPART_EXPR, itype, cres));
9250 : 289 : gimple_set_location (g, loc);
9251 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9252 : :
9253 : 289 : tree re = NULL_TREE;
9254 : 289 : if (need_old || need_new)
9255 : : {
9256 : 199 : re = create_tmp_reg (itype);
9257 : 199 : g = gimple_build_assign (re, REALPART_EXPR,
9258 : : build1 (REALPART_EXPR, itype, cres));
9259 : 199 : gimple_set_location (g, loc);
9260 : 199 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9261 : : }
9262 : :
9263 : 289 : if (cond_stmt)
9264 : : {
9265 : 289 : g = gimple_build_assign (cond, NOP_EXPR, im);
9266 : 289 : gimple_set_location (g, loc);
9267 : 289 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9268 : : }
9269 : :
9270 : 289 : if (need_new)
9271 : : {
9272 : 44 : g = gimple_build_assign (create_tmp_reg (itype), COND_EXPR,
9273 : : cond_stmt
9274 : 0 : ? cond : build2 (NE_EXPR, boolean_type_node,
9275 : : im, build_zero_cst (itype)),
9276 : : d, re);
9277 : 44 : gimple_set_location (g, loc);
9278 : 44 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9279 : 44 : re = gimple_assign_lhs (g);
9280 : : }
9281 : :
9282 : 289 : if (need_old || need_new)
9283 : : {
9284 : 199 : tree v = need_old ? loaded_val : stored_val;
9285 : 199 : enum tree_code code;
9286 : 199 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (v)))
9287 : : {
9288 : 38 : code = VIEW_CONVERT_EXPR;
9289 : 38 : re = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (v), re);
9290 : : }
9291 : 161 : else if (!useless_type_conversion_p (TREE_TYPE (v), itype))
9292 : : code = NOP_EXPR;
9293 : : else
9294 : 0 : code = TREE_CODE (re);
9295 : 199 : g = gimple_build_assign (v, code, re);
9296 : 199 : gimple_set_location (g, loc);
9297 : 199 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9298 : : }
9299 : : }
9300 : :
9301 : 289 : gsi_remove (&gsi, true);
9302 : 289 : gsi = gsi_for_stmt (load_stmt);
9303 : 289 : gsi_remove (&gsi, true);
9304 : 289 : gsi = gsi_for_stmt (condexpr_stmt);
9305 : 289 : gsi_remove (&gsi, true);
9306 : 289 : if (cond_stmt)
9307 : : {
9308 : 289 : gsi = gsi_for_stmt (cond_stmt);
9309 : 289 : gsi_remove (&gsi, true);
9310 : : }
9311 : 289 : if (vce_stmt)
9312 : : {
9313 : 57 : gsi = gsi_for_stmt (vce_stmt);
9314 : 57 : gsi_remove (&gsi, true);
9315 : : }
9316 : :
9317 : : return true;
9318 : : }
9319 : :
9320 : : /* A subroutine of expand_omp_atomic. Implement the atomic operation as:
9321 : :
9322 : : oldval = *addr;
9323 : : repeat:
9324 : : newval = rhs; // with oldval replacing *addr in rhs
9325 : : oldval = __sync_val_compare_and_swap (addr, oldval, newval);
9326 : : if (oldval != newval)
9327 : : goto repeat;
9328 : :
9329 : : INDEX is log2 of the size of the data type, and thus usable to find the
9330 : : index of the builtin decl. */
9331 : :
9332 : : static bool
9333 : 2493 : expand_omp_atomic_pipeline (basic_block load_bb, basic_block store_bb,
9334 : : tree addr, tree loaded_val, tree stored_val,
9335 : : int index)
9336 : : {
9337 : 2493 : tree loadedi, storedi, initial, new_storedi, old_vali;
9338 : 2493 : tree type, itype, cmpxchg, iaddr, atype;
9339 : 2493 : gimple_stmt_iterator si;
9340 : 2493 : basic_block loop_header = single_succ (load_bb);
9341 : 2493 : gimple *phi, *stmt;
9342 : 2493 : edge e;
9343 : 2493 : enum built_in_function fncode;
9344 : :
9345 : 2493 : fncode = (enum built_in_function)((int)BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
9346 : 2493 : + index + 1);
9347 : 2493 : cmpxchg = builtin_decl_explicit (fncode);
9348 : 2493 : if (cmpxchg == NULL_TREE)
9349 : : return false;
9350 : 2493 : type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9351 : 2493 : atype = type;
9352 : 2493 : itype = TREE_TYPE (TREE_TYPE (cmpxchg));
9353 : :
9354 : 2493 : if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
9355 : 2493 : || !can_atomic_load_p (TYPE_MODE (itype)))
9356 : 79 : return false;
9357 : :
9358 : : /* Load the initial value, replacing the GIMPLE_OMP_ATOMIC_LOAD. */
9359 : 2414 : si = gsi_last_nondebug_bb (load_bb);
9360 : 2414 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
9361 : 2414 : location_t loc = gimple_location (gsi_stmt (si));
9362 : 2414 : enum omp_memory_order omo = gimple_omp_atomic_memory_order (gsi_stmt (si));
9363 : 2414 : tree mo = build_int_cst (NULL, omp_memory_order_to_memmodel (omo));
9364 : 2414 : tree fmo = build_int_cst (NULL, omp_memory_order_to_fail_memmodel (omo));
9365 : :
9366 : : /* For floating-point values, we'll need to view-convert them to integers
9367 : : so that we can perform the atomic compare and swap. Simplify the
9368 : : following code by always setting up the "i"ntegral variables. */
9369 : 2414 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9370 : : {
9371 : 810 : tree iaddr_val;
9372 : :
9373 : 810 : iaddr = create_tmp_reg (build_pointer_type_for_mode (itype, ptr_mode,
9374 : : true));
9375 : 810 : atype = itype;
9376 : 810 : iaddr_val
9377 : 810 : = force_gimple_operand_gsi (&si,
9378 : 810 : fold_convert (TREE_TYPE (iaddr), addr),
9379 : : false, NULL_TREE, true, GSI_SAME_STMT);
9380 : 810 : stmt = gimple_build_assign (iaddr, iaddr_val);
9381 : 810 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9382 : 810 : loadedi = create_tmp_var (itype);
9383 : 810 : if (gimple_in_ssa_p (cfun))
9384 : 25 : loadedi = make_ssa_name (loadedi);
9385 : : }
9386 : : else
9387 : : {
9388 : : iaddr = addr;
9389 : : loadedi = loaded_val;
9390 : : }
9391 : :
9392 : 2414 : fncode = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
9393 : 2414 : tree loaddecl = builtin_decl_explicit (fncode);
9394 : 2414 : if (loaddecl)
9395 : 2414 : initial
9396 : 2414 : = fold_convert (atype,
9397 : : build_call_expr (loaddecl, 2, iaddr,
9398 : : build_int_cst (NULL_TREE,
9399 : : MEMMODEL_RELAXED)));
9400 : : else
9401 : : {
9402 : 0 : tree off
9403 : 0 : = build_int_cst (build_pointer_type_for_mode (atype, ptr_mode,
9404 : 0 : true), 0);
9405 : 0 : initial = build2 (MEM_REF, atype, iaddr, off);
9406 : : }
9407 : :
9408 : 2414 : initial
9409 : 2414 : = force_gimple_operand_gsi (&si, initial, true, NULL_TREE, true,
9410 : : GSI_SAME_STMT);
9411 : :
9412 : : /* Move the value to the LOADEDI temporary. */
9413 : 2414 : if (gimple_in_ssa_p (cfun))
9414 : : {
9415 : 49 : gcc_assert (gimple_seq_empty_p (phi_nodes (loop_header)));
9416 : 49 : phi = create_phi_node (loadedi, loop_header);
9417 : 49 : SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (load_bb)),
9418 : : initial);
9419 : : }
9420 : : else
9421 : 2365 : gsi_insert_before (&si,
9422 : 2365 : gimple_build_assign (loadedi, initial),
9423 : : GSI_SAME_STMT);
9424 : 2414 : if (loadedi != loaded_val)
9425 : : {
9426 : 810 : gimple_stmt_iterator gsi2;
9427 : 810 : tree x;
9428 : :
9429 : 810 : x = build1 (VIEW_CONVERT_EXPR, type, loadedi);
9430 : 810 : gsi2 = gsi_start_bb (loop_header);
9431 : 810 : if (gimple_in_ssa_p (cfun))
9432 : : {
9433 : 25 : gassign *stmt;
9434 : 25 : x = force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
9435 : : true, GSI_SAME_STMT);
9436 : 25 : stmt = gimple_build_assign (loaded_val, x);
9437 : 25 : gsi_insert_before (&gsi2, stmt, GSI_SAME_STMT);
9438 : : }
9439 : : else
9440 : : {
9441 : 785 : x = build2 (MODIFY_EXPR, TREE_TYPE (loaded_val), loaded_val, x);
9442 : 785 : force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
9443 : : true, GSI_SAME_STMT);
9444 : : }
9445 : : }
9446 : 2414 : gsi_remove (&si, true);
9447 : :
9448 : 2414 : si = gsi_last_nondebug_bb (store_bb);
9449 : 2414 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
9450 : :
9451 : 2414 : if (iaddr == addr)
9452 : : storedi = stored_val;
9453 : : else
9454 : 810 : storedi
9455 : 810 : = force_gimple_operand_gsi (&si,
9456 : : build1 (VIEW_CONVERT_EXPR, itype,
9457 : : stored_val), true, NULL_TREE, true,
9458 : : GSI_SAME_STMT);
9459 : :
9460 : : /* Build the compare&swap statement. */
9461 : 2414 : tree ctype = build_complex_type (itype);
9462 : 2414 : int flag = int_size_in_bytes (itype);
9463 : 2414 : new_storedi = build_call_expr_internal_loc (loc, IFN_ATOMIC_COMPARE_EXCHANGE,
9464 : : ctype, 6, iaddr, loadedi,
9465 : : storedi,
9466 : 2414 : build_int_cst (integer_type_node,
9467 : : flag),
9468 : : mo, fmo);
9469 : 2414 : new_storedi = build1 (REALPART_EXPR, itype, new_storedi);
9470 : 2414 : new_storedi = force_gimple_operand_gsi (&si,
9471 : 2414 : fold_convert (TREE_TYPE (loadedi),
9472 : : new_storedi),
9473 : : true, NULL_TREE,
9474 : : true, GSI_SAME_STMT);
9475 : :
9476 : 2414 : if (gimple_in_ssa_p (cfun))
9477 : : old_vali = loadedi;
9478 : : else
9479 : : {
9480 : 2365 : old_vali = create_tmp_var (TREE_TYPE (loadedi));
9481 : 2365 : stmt = gimple_build_assign (old_vali, loadedi);
9482 : 2365 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9483 : :
9484 : 2365 : stmt = gimple_build_assign (loadedi, new_storedi);
9485 : 2365 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9486 : : }
9487 : :
9488 : : /* Note that we always perform the comparison as an integer, even for
9489 : : floating point. This allows the atomic operation to properly
9490 : : succeed even with NaNs and -0.0. */
9491 : 2414 : tree ne = build2 (NE_EXPR, boolean_type_node, new_storedi, old_vali);
9492 : 2414 : stmt = gimple_build_cond_empty (ne);
9493 : 2414 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9494 : :
9495 : : /* Update cfg. */
9496 : 2414 : e = single_succ_edge (store_bb);
9497 : 2414 : e->flags &= ~EDGE_FALLTHRU;
9498 : 2414 : e->flags |= EDGE_FALSE_VALUE;
9499 : : /* Expect no looping. */
9500 : 2414 : e->probability = profile_probability::guessed_always ();
9501 : :
9502 : 2414 : e = make_edge (store_bb, loop_header, EDGE_TRUE_VALUE);
9503 : 2414 : e->probability = profile_probability::guessed_never ();
9504 : :
9505 : : /* Copy the new value to loadedi (we already did that before the condition
9506 : : if we are not in SSA). */
9507 : 2414 : if (gimple_in_ssa_p (cfun))
9508 : : {
9509 : 49 : phi = gimple_seq_first_stmt (phi_nodes (loop_header));
9510 : 49 : SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), new_storedi);
9511 : : }
9512 : :
9513 : : /* Remove GIMPLE_OMP_ATOMIC_STORE. */
9514 : 2414 : stmt = gsi_stmt (si);
9515 : 2414 : gsi_remove (&si, true);
9516 : 2414 : if (gimple_in_ssa_p (cfun))
9517 : 49 : release_defs (stmt);
9518 : :
9519 : 2414 : class loop *loop = alloc_loop ();
9520 : 2414 : loop->header = loop_header;
9521 : 2414 : loop->latch = store_bb;
9522 : 2414 : add_loop (loop, loop_header->loop_father);
9523 : :
9524 : 2414 : return true;
9525 : : }
9526 : :
9527 : : /* A subroutine of expand_omp_atomic. Implement the atomic operation as:
9528 : :
9529 : : GOMP_atomic_start ();
9530 : : *addr = rhs;
9531 : : GOMP_atomic_end ();
9532 : :
9533 : : The result is not globally atomic, but works so long as all parallel
9534 : : references are within #pragma omp atomic directives. According to
9535 : : responses received from omp@openmp.org, appears to be within spec.
9536 : : Which makes sense, since that's how several other compilers handle
9537 : : this situation as well.
9538 : : LOADED_VAL and ADDR are the operands of GIMPLE_OMP_ATOMIC_LOAD we're
9539 : : expanding. STORED_VAL is the operand of the matching
9540 : : GIMPLE_OMP_ATOMIC_STORE.
9541 : :
9542 : : We replace
9543 : : GIMPLE_OMP_ATOMIC_LOAD (loaded_val, addr) with
9544 : : loaded_val = *addr;
9545 : :
9546 : : and replace
9547 : : GIMPLE_OMP_ATOMIC_STORE (stored_val) with
9548 : : *addr = stored_val;
9549 : : */
9550 : :
9551 : : static bool
9552 : 289 : expand_omp_atomic_mutex (basic_block load_bb, basic_block store_bb,
9553 : : tree addr, tree loaded_val, tree stored_val)
9554 : : {
9555 : 289 : gimple_stmt_iterator si;
9556 : 289 : gassign *stmt;
9557 : 289 : tree t;
9558 : :
9559 : 289 : si = gsi_last_nondebug_bb (load_bb);
9560 : 289 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
9561 : :
9562 : 289 : t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
9563 : 289 : t = build_call_expr (t, 0);
9564 : 289 : force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
9565 : :
9566 : 289 : tree mem = build_simple_mem_ref (addr);
9567 : 289 : TREE_TYPE (mem) = TREE_TYPE (loaded_val);
9568 : 289 : TREE_OPERAND (mem, 1)
9569 : 289 : = fold_convert (build_pointer_type_for_mode (TREE_TYPE (mem), ptr_mode,
9570 : : true),
9571 : : TREE_OPERAND (mem, 1));
9572 : 289 : stmt = gimple_build_assign (loaded_val, mem);
9573 : 289 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9574 : 289 : gsi_remove (&si, true);
9575 : :
9576 : 289 : si = gsi_last_nondebug_bb (store_bb);
9577 : 289 : gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
9578 : :
9579 : 289 : stmt = gimple_build_assign (unshare_expr (mem), stored_val);
9580 : 578 : gimple_set_vuse (stmt, gimple_vuse (gsi_stmt (si)));
9581 : 578 : gimple_set_vdef (stmt, gimple_vdef (gsi_stmt (si)));
9582 : 289 : gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9583 : :
9584 : 289 : t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
9585 : 289 : t = build_call_expr (t, 0);
9586 : 289 : force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
9587 : 289 : gsi_remove (&si, true);
9588 : 289 : return true;
9589 : : }
9590 : :
9591 : : /* Expand an GIMPLE_OMP_ATOMIC statement. We try to expand
9592 : : using expand_omp_atomic_fetch_op. If it failed, we try to
9593 : : call expand_omp_atomic_pipeline, and if it fails too, the
9594 : : ultimate fallback is wrapping the operation in a mutex
9595 : : (expand_omp_atomic_mutex). REGION is the atomic region built
9596 : : by build_omp_regions_1(). */
9597 : :
9598 : : static void
9599 : 9486 : expand_omp_atomic (struct omp_region *region)
9600 : : {
9601 : 9486 : basic_block load_bb = region->entry, store_bb = region->exit;
9602 : 9486 : gomp_atomic_load *load
9603 : 9486 : = as_a <gomp_atomic_load *> (last_nondebug_stmt (load_bb));
9604 : 9486 : gomp_atomic_store *store
9605 : 9486 : = as_a <gomp_atomic_store *> (last_nondebug_stmt (store_bb));
9606 : 9486 : tree loaded_val = gimple_omp_atomic_load_lhs (load);
9607 : 9486 : tree addr = gimple_omp_atomic_load_rhs (load);
9608 : 9486 : tree stored_val = gimple_omp_atomic_store_val (store);
9609 : 9486 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9610 : 9486 : HOST_WIDE_INT index;
9611 : :
9612 : : /* Make sure the type is one of the supported sizes. */
9613 : 9486 : index = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9614 : 9486 : index = exact_log2 (index);
9615 : 9452 : if (index >= 0 && index <= 4)
9616 : : {
9617 : 18904 : unsigned int align = TYPE_ALIGN_UNIT (type);
9618 : :
9619 : : /* __sync builtins require strict data alignment. */
9620 : 18904 : if (exact_log2 (align) >= index)
9621 : : {
9622 : : /* Atomic load. */
9623 : 9276 : scalar_mode smode;
9624 : 9276 : if (loaded_val == stored_val
9625 : 1187 : && (is_int_mode (TYPE_MODE (type), &smode)
9626 : 1337 : || is_float_mode (TYPE_MODE (type), &smode))
9627 : 1187 : && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
9628 : 10459 : && expand_omp_atomic_load (load_bb, addr, loaded_val, index))
9629 : 9486 : return;
9630 : :
9631 : : /* Atomic store. */
9632 : 8093 : if ((is_int_mode (TYPE_MODE (type), &smode)
9633 : 9103 : || is_float_mode (TYPE_MODE (type), &smode))
9634 : 8093 : && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
9635 : 8014 : && store_bb == single_succ (load_bb)
9636 : 7399 : && first_stmt (store_bb) == store
9637 : 861 : && expand_omp_atomic_store (load_bb, addr, loaded_val,
9638 : : stored_val, index))
9639 : : return;
9640 : :
9641 : : /* When possible, use specialized atomic update functions. */
9642 : 950 : if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
9643 : 6290 : && store_bb == single_succ (load_bb)
9644 : 12975 : && expand_omp_atomic_fetch_op (load_bb, addr,
9645 : : loaded_val, stored_val, index))
9646 : : return;
9647 : :
9648 : : /* When possible, use ATOMIC_COMPARE_EXCHANGE ifn without a loop. */
9649 : 2782 : if (store_bb == single_succ (load_bb)
9650 : 2163 : && !gimple_in_ssa_p (cfun)
9651 : 4896 : && expand_omp_atomic_cas (load_bb, addr, loaded_val, stored_val,
9652 : : index))
9653 : : return;
9654 : :
9655 : : /* If we don't have specialized __sync builtins, try and implement
9656 : : as a compare and swap loop. */
9657 : 2493 : if (expand_omp_atomic_pipeline (load_bb, store_bb, addr,
9658 : : loaded_val, stored_val, index))
9659 : : return;
9660 : : }
9661 : : }
9662 : :
9663 : : /* The ultimate fallback is wrapping the operation in a mutex. */
9664 : 289 : expand_omp_atomic_mutex (load_bb, store_bb, addr, loaded_val, stored_val);
9665 : : }
9666 : :
9667 : : /* Mark the loops inside the kernels region starting at REGION_ENTRY and ending
9668 : : at REGION_EXIT. */
9669 : :
9670 : : static void
9671 : 1664 : mark_loops_in_oacc_kernels_region (basic_block region_entry,
9672 : : basic_block region_exit)
9673 : : {
9674 : 1664 : class loop *outer = region_entry->loop_father;
9675 : 1664 : gcc_assert (region_exit == NULL || outer == region_exit->loop_father);
9676 : :
9677 : : /* Don't parallelize the kernels region if it contains more than one outer
9678 : : loop. */
9679 : 1664 : unsigned int nr_outer_loops = 0;
9680 : 1664 : class loop *single_outer = NULL;
9681 : 33094 : for (class loop *loop = outer->inner; loop != NULL; loop = loop->next)
9682 : : {
9683 : 31430 : gcc_assert (loop_outer (loop) == outer);
9684 : :
9685 : 31430 : if (!dominated_by_p (CDI_DOMINATORS, loop->header, region_entry))
9686 : 23386 : continue;
9687 : :
9688 : 14600 : if (region_exit != NULL
9689 : 8044 : && dominated_by_p (CDI_DOMINATORS, loop->header, region_exit))
9690 : 6556 : continue;
9691 : :
9692 : 1488 : nr_outer_loops++;
9693 : 1488 : single_outer = loop;
9694 : : }
9695 : 1664 : if (nr_outer_loops != 1)
9696 : : return;
9697 : :
9698 : 1350 : for (class loop *loop = single_outer->inner;
9699 : 2319 : loop != NULL;
9700 : 969 : loop = loop->inner)
9701 : 1057 : if (loop->next)
9702 : : return;
9703 : :
9704 : : /* Mark the loops in the region. */
9705 : 3309 : for (class loop *loop = single_outer; loop != NULL; loop = loop->inner)
9706 : 2047 : loop->in_oacc_kernels_region = true;
9707 : : }
9708 : :
9709 : : /* Build target argument identifier from the DEVICE identifier, value
9710 : : identifier ID and whether the element also has a SUBSEQUENT_PARAM. */
9711 : :
9712 : : static tree
9713 : 20934 : get_target_argument_identifier_1 (int device, bool subseqent_param, int id)
9714 : : {
9715 : 20934 : tree t = build_int_cst (integer_type_node, device);
9716 : 20934 : if (subseqent_param)
9717 : 703 : t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9718 : : build_int_cst (integer_type_node,
9719 : : GOMP_TARGET_ARG_SUBSEQUENT_PARAM));
9720 : 20934 : t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9721 : : build_int_cst (integer_type_node, id));
9722 : 20934 : return t;
9723 : : }
9724 : :
9725 : : /* Like above but return it in type that can be directly stored as an element
9726 : : of the argument array. */
9727 : :
9728 : : static tree
9729 : 703 : get_target_argument_identifier (int device, bool subseqent_param, int id)
9730 : : {
9731 : 703 : tree t = get_target_argument_identifier_1 (device, subseqent_param, id);
9732 : 703 : return fold_convert (ptr_type_node, t);
9733 : : }
9734 : :
9735 : : /* Return a target argument consisting of DEVICE identifier, value identifier
9736 : : ID, and the actual VALUE. */
9737 : :
9738 : : static tree
9739 : 20231 : get_target_argument_value (gimple_stmt_iterator *gsi, int device, int id,
9740 : : tree value)
9741 : : {
9742 : 20231 : tree t = fold_build2 (LSHIFT_EXPR, integer_type_node,
9743 : : fold_convert (integer_type_node, value),
9744 : : build_int_cst (unsigned_type_node,
9745 : : GOMP_TARGET_ARG_VALUE_SHIFT));
9746 : 20231 : t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9747 : : get_target_argument_identifier_1 (device, false, id));
9748 : 20231 : t = fold_convert (ptr_type_node, t);
9749 : 20231 : return force_gimple_operand_gsi (gsi, t, true, NULL, true, GSI_SAME_STMT);
9750 : : }
9751 : :
9752 : : /* If VALUE is an integer constant greater than -2^15 and smaller than 2^15,
9753 : : push one argument to ARGS with both the DEVICE, ID and VALUE embedded in it,
9754 : : otherwise push an identifier (with DEVICE and ID) and the VALUE in two
9755 : : arguments. */
9756 : :
9757 : : static void
9758 : 20934 : push_target_argument_according_to_value (gimple_stmt_iterator *gsi, int device,
9759 : : int id, tree value, vec <tree> *args)
9760 : : {
9761 : 20934 : if (tree_fits_shwi_p (value)
9762 : 20231 : && tree_to_shwi (value) > -(1 << 15)
9763 : 20231 : && tree_to_shwi (value) < (1 << 15))
9764 : 20231 : args->quick_push (get_target_argument_value (gsi, device, id, value));
9765 : : else
9766 : : {
9767 : 703 : args->quick_push (get_target_argument_identifier (device, true, id));
9768 : 703 : value = fold_convert (ptr_type_node, value);
9769 : 703 : value = force_gimple_operand_gsi (gsi, value, true, NULL, true,
9770 : : GSI_SAME_STMT);
9771 : 703 : args->quick_push (value);
9772 : : }
9773 : 20934 : }
9774 : :
9775 : : /* Create an array of arguments that is then passed to GOMP_target. */
9776 : :
9777 : : static tree
9778 : 10467 : get_target_arguments (gimple_stmt_iterator *gsi, gomp_target *tgt_stmt)
9779 : : {
9780 : 10467 : auto_vec <tree, 6> args;
9781 : 10467 : tree clauses = gimple_omp_target_clauses (tgt_stmt);
9782 : 10467 : tree t, c = omp_find_clause (clauses, OMP_CLAUSE_NUM_TEAMS);
9783 : 10467 : if (c)
9784 : 10467 : t = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
9785 : : else
9786 : 0 : t = integer_minus_one_node;
9787 : 10467 : push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
9788 : : GOMP_TARGET_ARG_NUM_TEAMS, t, &args);
9789 : :
9790 : 10467 : c = omp_find_clause (clauses, OMP_CLAUSE_THREAD_LIMIT);
9791 : 10467 : if (c)
9792 : 10467 : t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
9793 : : else
9794 : 0 : t = integer_minus_one_node;
9795 : 10467 : push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
9796 : : GOMP_TARGET_ARG_THREAD_LIMIT, t,
9797 : : &args);
9798 : :
9799 : : /* Produce more, perhaps device specific, arguments here. */
9800 : :
9801 : 20934 : tree argarray = create_tmp_var (build_array_type_nelts (ptr_type_node,
9802 : 10467 : args.length () + 1),
9803 : : ".omp_target_args");
9804 : 32104 : for (unsigned i = 0; i < args.length (); i++)
9805 : : {
9806 : 21637 : tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
9807 : 21637 : build_int_cst (integer_type_node, i),
9808 : : NULL_TREE, NULL_TREE);
9809 : 21637 : gsi_insert_before (gsi, gimple_build_assign (ref, args[i]),
9810 : : GSI_SAME_STMT);
9811 : : }
9812 : 20934 : tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
9813 : 10467 : build_int_cst (integer_type_node, args.length ()),
9814 : : NULL_TREE, NULL_TREE);
9815 : 10467 : gsi_insert_before (gsi, gimple_build_assign (ref, null_pointer_node),
9816 : : GSI_SAME_STMT);
9817 : 10467 : TREE_ADDRESSABLE (argarray) = 1;
9818 : 10467 : return build_fold_addr_expr (argarray);
9819 : 10467 : }
9820 : :
9821 : : /* Expand the GIMPLE_OMP_TARGET starting at REGION. */
9822 : :
9823 : : static void
9824 : 34936 : expand_omp_target (struct omp_region *region)
9825 : : {
9826 : 34936 : basic_block entry_bb, exit_bb, new_bb;
9827 : 34936 : struct function *child_cfun;
9828 : 34936 : tree child_fn, child_fn2, block, t, c;
9829 : 34936 : gimple_stmt_iterator gsi;
9830 : 34936 : gomp_target *entry_stmt;
9831 : 34936 : gimple *stmt;
9832 : 34936 : edge e;
9833 : 34936 : bool offloaded;
9834 : 34936 : int target_kind;
9835 : :
9836 : 34936 : entry_stmt = as_a <gomp_target *> (last_nondebug_stmt (region->entry));
9837 : 34936 : target_kind = gimple_omp_target_kind (entry_stmt);
9838 : 34936 : new_bb = region->entry;
9839 : :
9840 : 34936 : offloaded = is_gimple_omp_offloaded (entry_stmt);
9841 : 34936 : switch (target_kind)
9842 : : {
9843 : 34936 : case GF_OMP_TARGET_KIND_REGION:
9844 : 34936 : case GF_OMP_TARGET_KIND_UPDATE:
9845 : 34936 : case GF_OMP_TARGET_KIND_ENTER_DATA:
9846 : 34936 : case GF_OMP_TARGET_KIND_EXIT_DATA:
9847 : 34936 : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
9848 : 34936 : case GF_OMP_TARGET_KIND_OACC_KERNELS:
9849 : 34936 : case GF_OMP_TARGET_KIND_OACC_SERIAL:
9850 : 34936 : case GF_OMP_TARGET_KIND_OACC_UPDATE:
9851 : 34936 : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
9852 : 34936 : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
9853 : 34936 : case GF_OMP_TARGET_KIND_OACC_DECLARE:
9854 : 34936 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
9855 : 34936 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
9856 : 34936 : case GF_OMP_TARGET_KIND_DATA:
9857 : 34936 : case GF_OMP_TARGET_KIND_OACC_DATA:
9858 : 34936 : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
9859 : 34936 : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
9860 : 34936 : break;
9861 : 0 : default:
9862 : 0 : gcc_unreachable ();
9863 : : }
9864 : :
9865 : 34936 : tree clauses = gimple_omp_target_clauses (entry_stmt);
9866 : :
9867 : 34936 : bool is_ancestor = false;
9868 : 34936 : child_fn = child_fn2 = NULL_TREE;
9869 : 34936 : child_cfun = NULL;
9870 : 34936 : if (offloaded)
9871 : : {
9872 : 19680 : c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE);
9873 : 19680 : if (ENABLE_OFFLOADING && c)
9874 : : is_ancestor = OMP_CLAUSE_DEVICE_ANCESTOR (c);
9875 : 19680 : child_fn = gimple_omp_target_child_fn (entry_stmt);
9876 : 19680 : child_cfun = DECL_STRUCT_FUNCTION (child_fn);
9877 : : }
9878 : :
9879 : : /* Supported by expand_omp_taskreg, but not here. */
9880 : 19680 : if (child_cfun != NULL)
9881 : 19680 : gcc_checking_assert (!child_cfun->cfg);
9882 : 34936 : gcc_checking_assert (!gimple_in_ssa_p (cfun));
9883 : :
9884 : 34936 : entry_bb = region->entry;
9885 : 34936 : exit_bb = region->exit;
9886 : :
9887 : 34936 : if (target_kind == GF_OMP_TARGET_KIND_OACC_KERNELS)
9888 : 1664 : mark_loops_in_oacc_kernels_region (region->entry, region->exit);
9889 : :
9890 : : /* Going on, all OpenACC compute constructs are mapped to
9891 : : 'BUILT_IN_GOACC_PARALLEL', and get their compute regions outlined.
9892 : : To distinguish between them, we attach attributes. */
9893 : 34936 : switch (target_kind)
9894 : : {
9895 : 6646 : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
9896 : 6646 : DECL_ATTRIBUTES (child_fn)
9897 : 6646 : = tree_cons (get_identifier ("oacc parallel"),
9898 : 6646 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9899 : 6646 : break;
9900 : 1664 : case GF_OMP_TARGET_KIND_OACC_KERNELS:
9901 : 1664 : DECL_ATTRIBUTES (child_fn)
9902 : 1664 : = tree_cons (get_identifier ("oacc kernels"),
9903 : 1664 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9904 : 1664 : break;
9905 : 720 : case GF_OMP_TARGET_KIND_OACC_SERIAL:
9906 : 720 : DECL_ATTRIBUTES (child_fn)
9907 : 720 : = tree_cons (get_identifier ("oacc serial"),
9908 : 720 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9909 : 720 : break;
9910 : 62 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
9911 : 62 : DECL_ATTRIBUTES (child_fn)
9912 : 62 : = tree_cons (get_identifier ("oacc parallel_kernels_parallelized"),
9913 : 62 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9914 : 62 : break;
9915 : 121 : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
9916 : 121 : DECL_ATTRIBUTES (child_fn)
9917 : 121 : = tree_cons (get_identifier ("oacc parallel_kernels_gang_single"),
9918 : 121 : NULL_TREE, DECL_ATTRIBUTES (child_fn));
9919 : 121 : break;
9920 : 25723 : default:
9921 : : /* Make sure we don't miss any. */
9922 : 25723 : gcc_checking_assert (!(is_gimple_omp_oacc (entry_stmt)
9923 : : && is_gimple_omp_offloaded (entry_stmt)));
9924 : : break;
9925 : : }
9926 : :
9927 : 34936 : if (offloaded)
9928 : : {
9929 : 19680 : unsigned srcidx, dstidx, num;
9930 : :
9931 : : /* If the offloading region needs data sent from the parent
9932 : : function, then the very first statement (except possible
9933 : : tree profile counter updates) of the offloading body
9934 : : is a copy assignment .OMP_DATA_I = &.OMP_DATA_O. Since
9935 : : &.OMP_DATA_O is passed as an argument to the child function,
9936 : : we need to replace it with the argument as seen by the child
9937 : : function.
9938 : :
9939 : : In most cases, this will end up being the identity assignment
9940 : : .OMP_DATA_I = .OMP_DATA_I. However, if the offloading body had
9941 : : a function call that has been inlined, the original PARM_DECL
9942 : : .OMP_DATA_I may have been converted into a different local
9943 : : variable. In which case, we need to keep the assignment. */
9944 : 19680 : tree data_arg = gimple_omp_target_data_arg (entry_stmt);
9945 : 19680 : if (data_arg)
9946 : : {
9947 : 15120 : basic_block entry_succ_bb = single_succ (entry_bb);
9948 : 15120 : gimple_stmt_iterator gsi;
9949 : 15120 : tree arg;
9950 : 15120 : gimple *tgtcopy_stmt = NULL;
9951 : 15120 : tree sender = TREE_VEC_ELT (data_arg, 0);
9952 : :
9953 : 30240 : for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
9954 : : {
9955 : 15120 : gcc_assert (!gsi_end_p (gsi));
9956 : 15120 : stmt = gsi_stmt (gsi);
9957 : 15120 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
9958 : 0 : continue;
9959 : :
9960 : 15120 : if (gimple_num_ops (stmt) == 2)
9961 : : {
9962 : 15120 : tree arg = gimple_assign_rhs1 (stmt);
9963 : :
9964 : : /* We're ignoring the subcode because we're
9965 : : effectively doing a STRIP_NOPS. */
9966 : :
9967 : 15120 : if ((TREE_CODE (arg) == ADDR_EXPR
9968 : 15120 : && TREE_OPERAND (arg, 0) == sender)
9969 : 15120 : || arg == sender)
9970 : : {
9971 : 15120 : tgtcopy_stmt = stmt;
9972 : 15120 : break;
9973 : : }
9974 : : }
9975 : : }
9976 : :
9977 : 15120 : gcc_assert (tgtcopy_stmt != NULL);
9978 : 15120 : arg = DECL_ARGUMENTS (child_fn);
9979 : :
9980 : 15120 : gcc_assert (gimple_assign_lhs (tgtcopy_stmt) == arg);
9981 : 15120 : gsi_remove (&gsi, true);
9982 : : }
9983 : :
9984 : : /* Declare local variables needed in CHILD_CFUN. */
9985 : 19680 : block = DECL_INITIAL (child_fn);
9986 : 19680 : BLOCK_VARS (block) = vec2chain (child_cfun->local_decls);
9987 : : /* The gimplifier could record temporaries in the offloading block
9988 : : rather than in containing function's local_decls chain,
9989 : : which would mean cgraph missed finalizing them. Do it now. */
9990 : 138969 : for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
9991 : 119289 : if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
9992 : 0 : varpool_node::finalize_decl (t);
9993 : 19680 : DECL_SAVED_TREE (child_fn) = NULL;
9994 : : /* We'll create a CFG for child_fn, so no gimple body is needed. */
9995 : 19680 : gimple_set_body (child_fn, NULL);
9996 : 19680 : TREE_USED (block) = 1;
9997 : :
9998 : : /* Reset DECL_CONTEXT on function arguments. */
9999 : 39360 : for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
10000 : 19680 : DECL_CONTEXT (t) = child_fn;
10001 : :
10002 : : /* Split ENTRY_BB at GIMPLE_*,
10003 : : so that it can be moved to the child function. */
10004 : 19680 : gsi = gsi_last_nondebug_bb (entry_bb);
10005 : 19680 : stmt = gsi_stmt (gsi);
10006 : 19680 : gcc_assert (stmt
10007 : : && gimple_code (stmt) == gimple_code (entry_stmt));
10008 : 19680 : e = split_block (entry_bb, stmt);
10009 : 19680 : gsi_remove (&gsi, true);
10010 : 19680 : entry_bb = e->dest;
10011 : 19680 : single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
10012 : :
10013 : : /* Convert GIMPLE_OMP_RETURN into a RETURN_EXPR. */
10014 : 19680 : if (exit_bb)
10015 : : {
10016 : 19606 : gsi = gsi_last_nondebug_bb (exit_bb);
10017 : 19606 : gcc_assert (!gsi_end_p (gsi)
10018 : : && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
10019 : 19606 : stmt = gimple_build_return (NULL);
10020 : 19606 : gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
10021 : 19606 : gsi_remove (&gsi, true);
10022 : : }
10023 : :
10024 : : /* Move the offloading region into CHILD_CFUN. */
10025 : :
10026 : 19680 : block = gimple_block (entry_stmt);
10027 : :
10028 : 19680 : new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
10029 : 19680 : if (exit_bb)
10030 : 19606 : single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
10031 : : /* When the OMP expansion process cannot guarantee an up-to-date
10032 : : loop tree arrange for the child function to fixup loops. */
10033 : 19680 : if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
10034 : 19680 : child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
10035 : :
10036 : : /* Remove non-local VAR_DECLs from child_cfun->local_decls list. */
10037 : 19680 : num = vec_safe_length (child_cfun->local_decls);
10038 : 583205 : for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
10039 : : {
10040 : 563525 : t = (*child_cfun->local_decls)[srcidx];
10041 : 563525 : if (DECL_CONTEXT (t) == cfun->decl)
10042 : 119289 : continue;
10043 : 444236 : if (srcidx != dstidx)
10044 : 429268 : (*child_cfun->local_decls)[dstidx] = t;
10045 : 444236 : dstidx++;
10046 : : }
10047 : 19680 : if (dstidx != num)
10048 : 16388 : vec_safe_truncate (child_cfun->local_decls, dstidx);
10049 : :
10050 : : /* Inform the callgraph about the new function. */
10051 : 19680 : child_cfun->curr_properties = cfun->curr_properties;
10052 : 19680 : child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
10053 : 19680 : child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
10054 : 19680 : cgraph_node *node = cgraph_node::get_create (child_fn);
10055 : 19680 : node->parallelized_function = 1;
10056 : 39360 : node->has_omp_variant_constructs
10057 : 19680 : |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
10058 : 19680 : cgraph_node::add_new_function (child_fn, true);
10059 : :
10060 : : /* Add the new function to the offload table. */
10061 : 19680 : if (ENABLE_OFFLOADING)
10062 : : {
10063 : : if (in_lto_p)
10064 : : DECL_PRESERVE_P (child_fn) = 1;
10065 : : if (!is_ancestor)
10066 : : vec_safe_push (offload_funcs, child_fn);
10067 : : }
10068 : :
10069 : 19680 : bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
10070 : 19680 : && !DECL_ASSEMBLER_NAME_SET_P (child_fn);
10071 : :
10072 : : /* Fix the callgraph edges for child_cfun. Those for cfun will be
10073 : : fixed in a following pass. */
10074 : 19680 : push_cfun (child_cfun);
10075 : 19680 : if (need_asm)
10076 : 19674 : assign_assembler_name_if_needed (child_fn);
10077 : 19680 : cgraph_edge::rebuild_edges ();
10078 : :
10079 : : /* Some EH regions might become dead, see PR34608. If
10080 : : pass_cleanup_cfg isn't the first pass to happen with the
10081 : : new child, these dead EH edges might cause problems.
10082 : : Clean them up now. */
10083 : 19680 : if (flag_exceptions)
10084 : : {
10085 : 7726 : basic_block bb;
10086 : 7726 : bool changed = false;
10087 : :
10088 : 111336 : FOR_EACH_BB_FN (bb, cfun)
10089 : 103610 : changed |= gimple_purge_dead_eh_edges (bb);
10090 : 7726 : if (changed)
10091 : 0 : cleanup_tree_cfg ();
10092 : : }
10093 : 19680 : if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
10094 : 0 : verify_loop_structure ();
10095 : 19680 : pop_cfun ();
10096 : :
10097 : 19680 : if (dump_file && !gimple_in_ssa_p (cfun))
10098 : : {
10099 : 31 : omp_any_child_fn_dumped = true;
10100 : 31 : dump_function_header (dump_file, child_fn, dump_flags);
10101 : 31 : dump_function_to_file (child_fn, dump_file, dump_flags);
10102 : : }
10103 : :
10104 : 19680 : adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
10105 : :
10106 : : /* Handle the case that an inner ancestor:1 target is called by an outer
10107 : : target region. */
10108 : 19680 : if (is_ancestor)
10109 : : {
10110 : : cgraph_node *fn2_node;
10111 : : child_fn2 = build_decl (DECL_SOURCE_LOCATION (child_fn),
10112 : : FUNCTION_DECL,
10113 : : clone_function_name (child_fn, "nohost"),
10114 : : TREE_TYPE (child_fn));
10115 : : if (in_lto_p)
10116 : : DECL_PRESERVE_P (child_fn2) = 1;
10117 : : TREE_STATIC (child_fn2) = 1;
10118 : : DECL_ARTIFICIAL (child_fn2) = 1;
10119 : : DECL_IGNORED_P (child_fn2) = 0;
10120 : : TREE_PUBLIC (child_fn2) = 0;
10121 : : DECL_UNINLINABLE (child_fn2) = 1;
10122 : : DECL_EXTERNAL (child_fn2) = 0;
10123 : : DECL_CONTEXT (child_fn2) = DECL_CONTEXT (child_fn);
10124 : : DECL_INITIAL (child_fn2) = make_node (BLOCK);
10125 : : BLOCK_SUPERCONTEXT (DECL_INITIAL (child_fn2)) = child_fn2;
10126 : : DECL_ATTRIBUTES (child_fn)
10127 : : = remove_attribute ("omp target entrypoint",
10128 : : DECL_ATTRIBUTES (child_fn));
10129 : : DECL_ATTRIBUTES (child_fn2)
10130 : : = tree_cons (get_identifier ("omp target device_ancestor_nohost"),
10131 : : NULL_TREE, copy_list (DECL_ATTRIBUTES (child_fn)));
10132 : : DECL_ATTRIBUTES (child_fn)
10133 : : = tree_cons (get_identifier ("omp target device_ancestor_host"),
10134 : : NULL_TREE, DECL_ATTRIBUTES (child_fn));
10135 : : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (child_fn2)
10136 : : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
10137 : : DECL_FUNCTION_SPECIFIC_TARGET (child_fn2)
10138 : : = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
10139 : : DECL_FUNCTION_VERSIONED (child_fn2)
10140 : : = DECL_FUNCTION_VERSIONED (current_function_decl);
10141 : :
10142 : : fn2_node = cgraph_node::get_create (child_fn2);
10143 : : fn2_node->offloadable = 1;
10144 : : fn2_node->force_output = 1;
10145 : : node->offloadable = 0;
10146 : :
10147 : : /* Enable pass_omp_device_lower pass. */
10148 : : fn2_node = cgraph_node::get (DECL_CONTEXT (child_fn));
10149 : : fn2_node->has_omp_variant_constructs = 1;
10150 : :
10151 : : t = build_decl (DECL_SOURCE_LOCATION (child_fn),
10152 : : RESULT_DECL, NULL_TREE, void_type_node);
10153 : : DECL_ARTIFICIAL (t) = 1;
10154 : : DECL_IGNORED_P (t) = 1;
10155 : : DECL_CONTEXT (t) = child_fn2;
10156 : : DECL_RESULT (child_fn2) = t;
10157 : : DECL_SAVED_TREE (child_fn2) = build1 (RETURN_EXPR,
10158 : : void_type_node, NULL);
10159 : : tree tmp = DECL_ARGUMENTS (child_fn);
10160 : : t = build_decl (DECL_SOURCE_LOCATION (child_fn), PARM_DECL,
10161 : : DECL_NAME (tmp), TREE_TYPE (tmp));
10162 : : DECL_ARTIFICIAL (t) = 1;
10163 : : DECL_NAMELESS (t) = 1;
10164 : : DECL_ARG_TYPE (t) = ptr_type_node;
10165 : : DECL_CONTEXT (t) = current_function_decl;
10166 : : TREE_USED (t) = 1;
10167 : : TREE_READONLY (t) = 1;
10168 : : DECL_ARGUMENTS (child_fn2) = t;
10169 : : gcc_assert (TREE_CHAIN (tmp) == NULL_TREE);
10170 : :
10171 : : gimplify_function_tree (child_fn2);
10172 : : cgraph_node::add_new_function (child_fn2, true);
10173 : :
10174 : : vec_safe_push (offload_funcs, child_fn2);
10175 : : if (dump_file && !gimple_in_ssa_p (cfun))
10176 : : {
10177 : : dump_function_header (dump_file, child_fn2, dump_flags);
10178 : : dump_function_to_file (child_fn2, dump_file, dump_flags);
10179 : : }
10180 : : }
10181 : : }
10182 : :
10183 : : /* Emit a library call to launch the offloading region, or do data
10184 : : transfers. */
10185 : 34936 : tree t1, t2, t3, t4, depend;
10186 : 34936 : enum built_in_function start_ix;
10187 : 34936 : unsigned int flags_i = 0;
10188 : :
10189 : 34936 : switch (gimple_omp_target_kind (entry_stmt))
10190 : : {
10191 : : case GF_OMP_TARGET_KIND_REGION:
10192 : : start_ix = BUILT_IN_GOMP_TARGET;
10193 : : break;
10194 : : case GF_OMP_TARGET_KIND_DATA:
10195 : : start_ix = BUILT_IN_GOMP_TARGET_DATA;
10196 : : break;
10197 : : case GF_OMP_TARGET_KIND_UPDATE:
10198 : : start_ix = BUILT_IN_GOMP_TARGET_UPDATE;
10199 : : break;
10200 : : case GF_OMP_TARGET_KIND_ENTER_DATA:
10201 : : start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
10202 : : break;
10203 : : case GF_OMP_TARGET_KIND_EXIT_DATA:
10204 : : start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
10205 : : flags_i |= GOMP_TARGET_FLAG_EXIT_DATA;
10206 : : break;
10207 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
10208 : : case GF_OMP_TARGET_KIND_OACC_KERNELS:
10209 : : case GF_OMP_TARGET_KIND_OACC_SERIAL:
10210 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
10211 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
10212 : : start_ix = BUILT_IN_GOACC_PARALLEL;
10213 : : break;
10214 : : case GF_OMP_TARGET_KIND_OACC_DATA:
10215 : : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
10216 : : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
10217 : : start_ix = BUILT_IN_GOACC_DATA_START;
10218 : : break;
10219 : : case GF_OMP_TARGET_KIND_OACC_UPDATE:
10220 : : start_ix = BUILT_IN_GOACC_UPDATE;
10221 : : break;
10222 : : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
10223 : : start_ix = BUILT_IN_GOACC_ENTER_DATA;
10224 : : break;
10225 : : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
10226 : : start_ix = BUILT_IN_GOACC_EXIT_DATA;
10227 : : break;
10228 : : case GF_OMP_TARGET_KIND_OACC_DECLARE:
10229 : : start_ix = BUILT_IN_GOACC_DECLARE;
10230 : : break;
10231 : 0 : default:
10232 : 0 : gcc_unreachable ();
10233 : : }
10234 : :
10235 : 34936 : tree device = NULL_TREE;
10236 : 34936 : location_t device_loc = UNKNOWN_LOCATION;
10237 : 34936 : tree goacc_flags = NULL_TREE;
10238 : 34936 : bool need_device_adjustment = false;
10239 : 34936 : gimple_stmt_iterator adj_gsi;
10240 : 34936 : if (is_gimple_omp_oacc (entry_stmt))
10241 : : {
10242 : : /* By default, no GOACC_FLAGs are set. */
10243 : 14417 : goacc_flags = integer_zero_node;
10244 : : }
10245 : : else
10246 : : {
10247 : 20519 : c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE);
10248 : 20519 : if (c)
10249 : : {
10250 : 871 : device = OMP_CLAUSE_DEVICE_ID (c);
10251 : : /* Ensure 'device' is of the correct type. */
10252 : 871 : device = fold_convert_loc (device_loc, integer_type_node, device);
10253 : 871 : if (TREE_CODE (device) == INTEGER_CST)
10254 : : {
10255 : 50 : if (wi::to_wide (device) == GOMP_DEVICE_ICV)
10256 : 2 : device = build_int_cst (integer_type_node,
10257 : 2 : GOMP_DEVICE_HOST_FALLBACK);
10258 : 48 : else if (wi::to_wide (device) == GOMP_DEVICE_HOST_FALLBACK)
10259 : 0 : device = build_int_cst (integer_type_node,
10260 : 0 : GOMP_DEVICE_HOST_FALLBACK - 1);
10261 : : }
10262 : : else
10263 : : need_device_adjustment = true;
10264 : 871 : device_loc = OMP_CLAUSE_LOCATION (c);
10265 : 871 : if (OMP_CLAUSE_DEVICE_ANCESTOR (c))
10266 : 41 : device = build_int_cst (integer_type_node,
10267 : 41 : GOMP_DEVICE_HOST_FALLBACK);
10268 : : }
10269 : : else
10270 : : {
10271 : : /* By default, the value of DEVICE is GOMP_DEVICE_ICV (let runtime
10272 : : library choose). */
10273 : 19648 : device = build_int_cst (integer_type_node, GOMP_DEVICE_ICV);
10274 : 19648 : device_loc = gimple_location (entry_stmt);
10275 : : }
10276 : :
10277 : 20519 : c = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT);
10278 : : /* FIXME: in_reduction(...) nowait is unimplemented yet, pretend
10279 : : nowait doesn't appear. */
10280 : 20519 : if (c && omp_find_clause (clauses, OMP_CLAUSE_IN_REDUCTION))
10281 : : c = NULL;
10282 : 20255 : if (c)
10283 : 127 : flags_i |= GOMP_TARGET_FLAG_NOWAIT;
10284 : : }
10285 : :
10286 : : /* By default, there is no conditional. */
10287 : 34936 : tree cond = NULL_TREE;
10288 : 34936 : c = omp_find_clause (clauses, OMP_CLAUSE_IF);
10289 : 34936 : if (c)
10290 : 1741 : cond = OMP_CLAUSE_IF_EXPR (c);
10291 : : /* If we found the clause 'if (cond)', build:
10292 : : OpenACC: goacc_flags = (cond ? goacc_flags
10293 : : : goacc_flags | GOACC_FLAG_HOST_FALLBACK)
10294 : : OpenMP: device = (cond ? device : GOMP_DEVICE_HOST_FALLBACK) */
10295 : 1741 : if (cond)
10296 : : {
10297 : 1741 : tree *tp;
10298 : 1741 : if (is_gimple_omp_oacc (entry_stmt))
10299 : : tp = &goacc_flags;
10300 : : else
10301 : 666 : tp = &device;
10302 : :
10303 : 1741 : cond = gimple_boolify (cond);
10304 : :
10305 : 1741 : basic_block cond_bb, then_bb, else_bb;
10306 : 1741 : edge e;
10307 : 1741 : tree tmp_var = create_tmp_var (TREE_TYPE (*tp));
10308 : 1741 : if (offloaded)
10309 : 806 : e = split_block_after_labels (new_bb);
10310 : : else
10311 : : {
10312 : 935 : gsi = gsi_last_nondebug_bb (new_bb);
10313 : 935 : gsi_prev (&gsi);
10314 : 935 : e = split_block (new_bb, gsi_stmt (gsi));
10315 : : }
10316 : 1741 : cond_bb = e->src;
10317 : 1741 : new_bb = e->dest;
10318 : 1741 : remove_edge (e);
10319 : :
10320 : 1741 : then_bb = create_empty_bb (cond_bb);
10321 : 1741 : else_bb = create_empty_bb (then_bb);
10322 : 1741 : set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10323 : 1741 : set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
10324 : :
10325 : 1741 : stmt = gimple_build_cond_empty (cond);
10326 : 1741 : gsi = gsi_last_bb (cond_bb);
10327 : 1741 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10328 : :
10329 : 1741 : gsi = gsi_start_bb (then_bb);
10330 : 1741 : stmt = gimple_build_assign (tmp_var, *tp);
10331 : 1741 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10332 : 1741 : adj_gsi = gsi;
10333 : :
10334 : 1741 : gsi = gsi_start_bb (else_bb);
10335 : 1741 : if (is_gimple_omp_oacc (entry_stmt))
10336 : 1075 : stmt = gimple_build_assign (tmp_var,
10337 : : BIT_IOR_EXPR,
10338 : : *tp,
10339 : 1075 : build_int_cst (integer_type_node,
10340 : 1075 : GOACC_FLAG_HOST_FALLBACK));
10341 : : else
10342 : 666 : stmt = gimple_build_assign (tmp_var,
10343 : 666 : build_int_cst (integer_type_node,
10344 : 666 : GOMP_DEVICE_HOST_FALLBACK));
10345 : 1741 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10346 : :
10347 : 1741 : make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10348 : 1741 : make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
10349 : 1741 : add_bb_to_loop (then_bb, cond_bb->loop_father);
10350 : 1741 : add_bb_to_loop (else_bb, cond_bb->loop_father);
10351 : 1741 : make_edge (then_bb, new_bb, EDGE_FALLTHRU);
10352 : 1741 : make_edge (else_bb, new_bb, EDGE_FALLTHRU);
10353 : :
10354 : 1741 : *tp = tmp_var;
10355 : :
10356 : 1741 : gsi = gsi_last_nondebug_bb (new_bb);
10357 : : }
10358 : : else
10359 : : {
10360 : 33195 : gsi = gsi_last_nondebug_bb (new_bb);
10361 : :
10362 : 33195 : if (device != NULL_TREE)
10363 : 19853 : device = force_gimple_operand_gsi (&gsi, device, true, NULL_TREE,
10364 : : true, GSI_SAME_STMT);
10365 : 33195 : if (need_device_adjustment)
10366 : : {
10367 : 347 : tree tmp_var = create_tmp_var (TREE_TYPE (device));
10368 : 347 : stmt = gimple_build_assign (tmp_var, device);
10369 : 347 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
10370 : 347 : adj_gsi = gsi_for_stmt (stmt);
10371 : 347 : device = tmp_var;
10372 : : }
10373 : : }
10374 : :
10375 : 34936 : if ((c = omp_find_clause (clauses, OMP_CLAUSE_SELF)) != NULL_TREE)
10376 : : {
10377 : 324 : gcc_assert ((is_gimple_omp_oacc (entry_stmt) && offloaded)
10378 : : || (gimple_omp_target_kind (entry_stmt)
10379 : : == GF_OMP_TARGET_KIND_OACC_DATA_KERNELS));
10380 : :
10381 : 87 : edge e;
10382 : 87 : if (offloaded)
10383 : 237 : e = split_block_after_labels (new_bb);
10384 : : else
10385 : : {
10386 : 87 : gsi = gsi_last_nondebug_bb (new_bb);
10387 : 87 : gsi_prev (&gsi);
10388 : 87 : e = split_block (new_bb, gsi_stmt (gsi));
10389 : : }
10390 : 324 : basic_block cond_bb = e->src;
10391 : 324 : new_bb = e->dest;
10392 : 324 : remove_edge (e);
10393 : :
10394 : 324 : basic_block then_bb = create_empty_bb (cond_bb);
10395 : 324 : basic_block else_bb = create_empty_bb (then_bb);
10396 : 324 : set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10397 : 324 : set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
10398 : :
10399 : 324 : tree self_cond = gimple_boolify (OMP_CLAUSE_SELF_EXPR (c));
10400 : 324 : stmt = gimple_build_cond_empty (self_cond);
10401 : 324 : gsi = gsi_last_bb (cond_bb);
10402 : 324 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10403 : :
10404 : 324 : tree tmp_var = create_tmp_var (TREE_TYPE (goacc_flags));
10405 : 324 : stmt = gimple_build_assign (tmp_var, BIT_IOR_EXPR, goacc_flags,
10406 : 324 : build_int_cst (integer_type_node,
10407 : 324 : GOACC_FLAG_LOCAL_DEVICE));
10408 : 324 : gsi = gsi_start_bb (then_bb);
10409 : 324 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10410 : :
10411 : 324 : gsi = gsi_start_bb (else_bb);
10412 : 324 : stmt = gimple_build_assign (tmp_var, goacc_flags);
10413 : 324 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10414 : :
10415 : 324 : make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10416 : 324 : make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
10417 : 324 : add_bb_to_loop (then_bb, cond_bb->loop_father);
10418 : 324 : add_bb_to_loop (else_bb, cond_bb->loop_father);
10419 : 324 : make_edge (then_bb, new_bb, EDGE_FALLTHRU);
10420 : 324 : make_edge (else_bb, new_bb, EDGE_FALLTHRU);
10421 : :
10422 : 324 : goacc_flags = tmp_var;
10423 : 324 : gsi = gsi_last_nondebug_bb (new_bb);
10424 : : }
10425 : :
10426 : 34936 : if (need_device_adjustment)
10427 : : {
10428 : 821 : tree uns = fold_convert (unsigned_type_node, device);
10429 : 821 : uns = force_gimple_operand_gsi (&adj_gsi, uns, true, NULL_TREE,
10430 : : false, GSI_CONTINUE_LINKING);
10431 : 821 : edge e = split_block (gsi_bb (adj_gsi), gsi_stmt (adj_gsi));
10432 : 821 : basic_block cond_bb = e->src;
10433 : 821 : basic_block else_bb = e->dest;
10434 : 821 : if (gsi_bb (adj_gsi) == new_bb)
10435 : : {
10436 : 347 : new_bb = else_bb;
10437 : 347 : gsi = gsi_last_nondebug_bb (new_bb);
10438 : : }
10439 : :
10440 : 821 : basic_block then_bb = create_empty_bb (cond_bb);
10441 : 821 : set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10442 : :
10443 : 821 : cond = build2 (GT_EXPR, boolean_type_node, uns,
10444 : 821 : build_int_cst (unsigned_type_node,
10445 : 821 : GOMP_DEVICE_HOST_FALLBACK - 1));
10446 : 821 : stmt = gimple_build_cond_empty (cond);
10447 : 821 : adj_gsi = gsi_last_bb (cond_bb);
10448 : 821 : gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
10449 : :
10450 : 821 : adj_gsi = gsi_start_bb (then_bb);
10451 : 821 : tree add = build2 (PLUS_EXPR, integer_type_node, device,
10452 : 821 : build_int_cst (integer_type_node, -1));
10453 : 821 : stmt = gimple_build_assign (device, add);
10454 : 821 : gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
10455 : :
10456 : 821 : make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10457 : 821 : e->flags = EDGE_FALSE_VALUE;
10458 : 821 : add_bb_to_loop (then_bb, cond_bb->loop_father);
10459 : 821 : make_edge (then_bb, else_bb, EDGE_FALLTHRU);
10460 : : }
10461 : :
10462 : 34936 : t = gimple_omp_target_data_arg (entry_stmt);
10463 : 34936 : if (t == NULL)
10464 : : {
10465 : 4787 : t1 = size_zero_node;
10466 : 4787 : t2 = build_zero_cst (ptr_type_node);
10467 : 4787 : t3 = t2;
10468 : 4787 : t4 = t2;
10469 : : }
10470 : 30149 : else if (TREE_VEC_LENGTH (t) == 3 || is_gimple_omp_oacc (entry_stmt))
10471 : : {
10472 : 30149 : t1 = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (TREE_VEC_ELT (t, 1))));
10473 : 30149 : t1 = size_binop (PLUS_EXPR, t1, size_int (1));
10474 : 30149 : t2 = build_fold_addr_expr (TREE_VEC_ELT (t, 0));
10475 : 30149 : t3 = build_fold_addr_expr (TREE_VEC_ELT (t, 1));
10476 : 30149 : t4 = build_fold_addr_expr (TREE_VEC_ELT (t, 2));
10477 : : }
10478 : : else
10479 : : {
10480 : 0 : t1 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 3), true, NULL_TREE,
10481 : : true, GSI_SAME_STMT);
10482 : 0 : t2 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 0), true, NULL_TREE,
10483 : : true, GSI_SAME_STMT);
10484 : 0 : t3 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 1), true, NULL_TREE,
10485 : : true, GSI_SAME_STMT);
10486 : 0 : t4 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 2), true, NULL_TREE,
10487 : : true, GSI_SAME_STMT);
10488 : : }
10489 : :
10490 : 34936 : gimple *g;
10491 : 34936 : bool tagging = false;
10492 : : /* The maximum number used by any start_ix, without varargs. */
10493 : 34936 : auto_vec<tree, 11> args;
10494 : 34936 : if (is_gimple_omp_oacc (entry_stmt))
10495 : : {
10496 : 14417 : tree goacc_flags_m = fold_build1 (GOACC_FLAGS_MARSHAL_OP,
10497 : : TREE_TYPE (goacc_flags), goacc_flags);
10498 : 14417 : goacc_flags_m = force_gimple_operand_gsi (&gsi, goacc_flags_m, true,
10499 : : NULL_TREE, true,
10500 : : GSI_SAME_STMT);
10501 : 14417 : args.quick_push (goacc_flags_m);
10502 : : }
10503 : : else
10504 : 20519 : args.quick_push (device);
10505 : 34936 : if (offloaded)
10506 : 19680 : args.quick_push (build_fold_addr_expr (child_fn2 ? child_fn2 : child_fn));
10507 : 34936 : args.quick_push (t1);
10508 : 34936 : args.quick_push (t2);
10509 : 34936 : args.quick_push (t3);
10510 : 34936 : args.quick_push (t4);
10511 : 34936 : switch (start_ix)
10512 : : {
10513 : : case BUILT_IN_GOACC_DATA_START:
10514 : : case BUILT_IN_GOACC_DECLARE:
10515 : : case BUILT_IN_GOMP_TARGET_DATA:
10516 : : break;
10517 : 18906 : case BUILT_IN_GOMP_TARGET:
10518 : 18906 : case BUILT_IN_GOMP_TARGET_UPDATE:
10519 : 18906 : case BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA:
10520 : 18906 : args.quick_push (build_int_cst (unsigned_type_node, flags_i));
10521 : 18906 : c = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
10522 : 18906 : if (c)
10523 : 319 : depend = OMP_CLAUSE_DECL (c);
10524 : : else
10525 : 18587 : depend = build_int_cst (ptr_type_node, 0);
10526 : 18906 : args.quick_push (depend);
10527 : 18906 : if (start_ix == BUILT_IN_GOMP_TARGET)
10528 : 10467 : args.quick_push (get_target_arguments (&gsi, entry_stmt));
10529 : : break;
10530 : 9213 : case BUILT_IN_GOACC_PARALLEL:
10531 : 9213 : if (lookup_attribute ("oacc serial", DECL_ATTRIBUTES (child_fn)) != NULL)
10532 : : {
10533 : : tree dims = NULL_TREE;
10534 : : unsigned int ix;
10535 : :
10536 : : /* For serial constructs we set all dimensions to 1. */
10537 : 2880 : for (ix = GOMP_DIM_MAX; ix--;)
10538 : 2160 : dims = tree_cons (NULL_TREE, integer_one_node, dims);
10539 : 720 : oacc_replace_fn_attrib (child_fn, dims);
10540 : : }
10541 : : else
10542 : 8493 : oacc_set_fn_attrib (child_fn, clauses, &args);
10543 : : tagging = true;
10544 : : /* FALLTHRU */
10545 : 11910 : case BUILT_IN_GOACC_ENTER_DATA:
10546 : 11910 : case BUILT_IN_GOACC_EXIT_DATA:
10547 : 11910 : case BUILT_IN_GOACC_UPDATE:
10548 : 11910 : {
10549 : 11910 : tree t_async = NULL_TREE;
10550 : :
10551 : : /* If present, use the value specified by the respective
10552 : : clause, making sure that is of the correct type. */
10553 : 11910 : c = omp_find_clause (clauses, OMP_CLAUSE_ASYNC);
10554 : 11910 : if (c)
10555 : 1704 : t_async = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
10556 : : integer_type_node,
10557 : 1704 : OMP_CLAUSE_ASYNC_EXPR (c));
10558 : 10206 : else if (!tagging)
10559 : : /* Default values for t_async. */
10560 : 2395 : t_async = fold_convert_loc (gimple_location (entry_stmt),
10561 : : integer_type_node,
10562 : 2395 : build_int_cst (integer_type_node,
10563 : 2395 : GOMP_ASYNC_SYNC));
10564 : 11910 : if (tagging && t_async)
10565 : : {
10566 : 1402 : unsigned HOST_WIDE_INT i_async = GOMP_LAUNCH_OP_MAX;
10567 : :
10568 : 1402 : if (TREE_CODE (t_async) == INTEGER_CST)
10569 : : {
10570 : : /* See if we can pack the async arg in to the tag's
10571 : : operand. */
10572 : 1362 : i_async = TREE_INT_CST_LOW (t_async);
10573 : 1362 : if (i_async < GOMP_LAUNCH_OP_MAX)
10574 : : t_async = NULL_TREE;
10575 : : else
10576 : 856 : i_async = GOMP_LAUNCH_OP_MAX;
10577 : : }
10578 : 1402 : args.safe_push (oacc_launch_pack (GOMP_LAUNCH_ASYNC, NULL_TREE,
10579 : : i_async));
10580 : : }
10581 : 11910 : if (t_async)
10582 : 3553 : args.safe_push (force_gimple_operand_gsi (&gsi, t_async, true,
10583 : : NULL_TREE, true,
10584 : : GSI_SAME_STMT));
10585 : :
10586 : : /* Save the argument index, and ... */
10587 : 11910 : unsigned t_wait_idx = args.length ();
10588 : 11910 : unsigned num_waits = 0;
10589 : 11910 : c = omp_find_clause (clauses, OMP_CLAUSE_WAIT);
10590 : 11910 : if (!tagging || c)
10591 : : /* ... push a placeholder. */
10592 : 2900 : args.safe_push (integer_zero_node);
10593 : :
10594 : 13736 : for (; c; c = OMP_CLAUSE_CHAIN (c))
10595 : 1826 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_WAIT)
10596 : : {
10597 : 406 : tree arg = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
10598 : : integer_type_node,
10599 : 406 : OMP_CLAUSE_WAIT_EXPR (c));
10600 : 406 : arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE, true,
10601 : : GSI_SAME_STMT);
10602 : 406 : args.safe_push (arg);
10603 : 406 : num_waits++;
10604 : : }
10605 : :
10606 : 11910 : if (!tagging || num_waits)
10607 : : {
10608 : 203 : tree len;
10609 : :
10610 : : /* Now that we know the number, update the placeholder. */
10611 : 203 : if (tagging)
10612 : 203 : len = oacc_launch_pack (GOMP_LAUNCH_WAIT, NULL_TREE, num_waits);
10613 : : else
10614 : 2697 : len = build_int_cst (integer_type_node, num_waits);
10615 : 2900 : len = fold_convert_loc (gimple_location (entry_stmt),
10616 : : unsigned_type_node, len);
10617 : 2900 : args[t_wait_idx] = len;
10618 : : }
10619 : : }
10620 : 11910 : break;
10621 : 0 : default:
10622 : 0 : gcc_unreachable ();
10623 : : }
10624 : 22377 : if (tagging)
10625 : : /* Push terminal marker - zero. */
10626 : 9213 : args.safe_push (oacc_launch_pack (0, NULL_TREE, 0));
10627 : :
10628 : 34936 : if (child_fn2)
10629 : : {
10630 : : g = gimple_build_call_internal (IFN_GOMP_TARGET_REV, 1,
10631 : : build_fold_addr_expr (child_fn));
10632 : : gimple_set_location (g, gimple_location (entry_stmt));
10633 : : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
10634 : : }
10635 : :
10636 : 34936 : g = gimple_build_call_vec (builtin_decl_explicit (start_ix), args);
10637 : 34936 : gimple_set_location (g, gimple_location (entry_stmt));
10638 : 34936 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
10639 : 34936 : if (!offloaded)
10640 : : {
10641 : 15256 : g = gsi_stmt (gsi);
10642 : 15256 : gcc_assert (g && gimple_code (g) == GIMPLE_OMP_TARGET);
10643 : 15256 : gsi_remove (&gsi, true);
10644 : : }
10645 : 34936 : }
10646 : :
10647 : : /* Expand the parallel region tree rooted at REGION. Expansion
10648 : : proceeds in depth-first order. Innermost regions are expanded
10649 : : first. This way, parallel regions that require a new function to
10650 : : be created (e.g., GIMPLE_OMP_PARALLEL) can be expanded without having any
10651 : : internal dependencies in their body. */
10652 : :
10653 : : static void
10654 : 80587 : expand_omp (struct omp_region *region)
10655 : : {
10656 : 80587 : omp_any_child_fn_dumped = false;
10657 : 206558 : while (region)
10658 : : {
10659 : 125971 : location_t saved_location;
10660 : 125971 : gimple *inner_stmt = NULL;
10661 : :
10662 : : /* First, determine whether this is a combined parallel+workshare
10663 : : region. */
10664 : 125971 : if (region->type == GIMPLE_OMP_PARALLEL)
10665 : 16258 : determine_parallel_type (region);
10666 : :
10667 : 125971 : if (region->type == GIMPLE_OMP_FOR
10668 : 125971 : && gimple_omp_for_combined_p (last_nondebug_stmt (region->entry)))
10669 : 14601 : inner_stmt = last_nondebug_stmt (region->inner->entry);
10670 : :
10671 : 125971 : if (region->inner)
10672 : 56920 : expand_omp (region->inner);
10673 : :
10674 : 125971 : saved_location = input_location;
10675 : 125971 : if (gimple_has_location (last_nondebug_stmt (region->entry)))
10676 : 125777 : input_location = gimple_location (last_nondebug_stmt (region->entry));
10677 : :
10678 : 125971 : switch (region->type)
10679 : : {
10680 : 20119 : case GIMPLE_OMP_PARALLEL:
10681 : 20119 : case GIMPLE_OMP_TASK:
10682 : 20119 : expand_omp_taskreg (region);
10683 : 20119 : break;
10684 : :
10685 : 47475 : case GIMPLE_OMP_FOR:
10686 : 47475 : expand_omp_for (region, inner_stmt);
10687 : 47475 : break;
10688 : :
10689 : 378 : case GIMPLE_OMP_SECTIONS:
10690 : 378 : expand_omp_sections (region);
10691 : 378 : break;
10692 : :
10693 : : case GIMPLE_OMP_SECTION:
10694 : : /* Individual omp sections are handled together with their
10695 : : parent GIMPLE_OMP_SECTIONS region. */
10696 : : break;
10697 : :
10698 : 0 : case GIMPLE_OMP_STRUCTURED_BLOCK:
10699 : : /* We should have gotten rid of these in gimple lowering. */
10700 : 0 : gcc_unreachable ();
10701 : :
10702 : 1261 : case GIMPLE_OMP_SINGLE:
10703 : 1261 : case GIMPLE_OMP_SCOPE:
10704 : 1261 : expand_omp_single (region);
10705 : 1261 : break;
10706 : :
10707 : 1112 : case GIMPLE_OMP_ORDERED:
10708 : 1112 : {
10709 : 1112 : gomp_ordered *ord_stmt
10710 : 1112 : = as_a <gomp_ordered *> (last_nondebug_stmt (region->entry));
10711 : 1112 : if (gimple_omp_ordered_standalone_p (ord_stmt))
10712 : : {
10713 : : /* We'll expand these when expanding corresponding
10714 : : worksharing region with ordered(n) clause. */
10715 : 701 : gcc_assert (region->outer
10716 : : && region->outer->type == GIMPLE_OMP_FOR);
10717 : 701 : region->ord_stmt = ord_stmt;
10718 : 701 : break;
10719 : : }
10720 : : }
10721 : : /* FALLTHRU */
10722 : 10760 : case GIMPLE_OMP_MASTER:
10723 : 10760 : case GIMPLE_OMP_MASKED:
10724 : 10760 : case GIMPLE_OMP_TASKGROUP:
10725 : 10760 : case GIMPLE_OMP_CRITICAL:
10726 : 10760 : case GIMPLE_OMP_TEAMS:
10727 : 10760 : expand_omp_synch (region);
10728 : 10760 : break;
10729 : :
10730 : 9486 : case GIMPLE_OMP_ATOMIC_LOAD:
10731 : 9486 : expand_omp_atomic (region);
10732 : 9486 : break;
10733 : :
10734 : 34936 : case GIMPLE_OMP_TARGET:
10735 : 34936 : expand_omp_target (region);
10736 : 34936 : break;
10737 : :
10738 : 0 : default:
10739 : 0 : gcc_unreachable ();
10740 : : }
10741 : :
10742 : 125971 : input_location = saved_location;
10743 : 125971 : region = region->next;
10744 : : }
10745 : 80587 : if (omp_any_child_fn_dumped)
10746 : : {
10747 : 45 : if (dump_file)
10748 : 45 : dump_function_header (dump_file, current_function_decl, dump_flags);
10749 : 45 : omp_any_child_fn_dumped = false;
10750 : : }
10751 : 80587 : }
10752 : :
10753 : : /* Helper for build_omp_regions. Scan the dominator tree starting at
10754 : : block BB. PARENT is the region that contains BB. If SINGLE_TREE is
10755 : : true, the function ends once a single tree is built (otherwise, whole
10756 : : forest of OMP constructs may be built). */
10757 : :
10758 : : static void
10759 : 1309417 : build_omp_regions_1 (basic_block bb, struct omp_region *parent,
10760 : : bool single_tree)
10761 : : {
10762 : 1309417 : gimple_stmt_iterator gsi;
10763 : 1309417 : gimple *stmt;
10764 : 1309417 : basic_block son;
10765 : :
10766 : 1309417 : gsi = gsi_last_nondebug_bb (bb);
10767 : 1309417 : if (!gsi_end_p (gsi) && is_gimple_omp (gsi_stmt (gsi)))
10768 : : {
10769 : 284798 : struct omp_region *region;
10770 : 284798 : enum gimple_code code;
10771 : :
10772 : 284798 : stmt = gsi_stmt (gsi);
10773 : 284798 : code = gimple_code (stmt);
10774 : 284798 : if (code == GIMPLE_OMP_RETURN)
10775 : : {
10776 : : /* STMT is the return point out of region PARENT. Mark it
10777 : : as the exit point and make PARENT the immediately
10778 : : enclosing region. */
10779 : 99661 : gcc_assert (parent);
10780 : 99661 : region = parent;
10781 : 99661 : region->exit = bb;
10782 : 99661 : parent = parent->outer;
10783 : : }
10784 : 185137 : else if (code == GIMPLE_OMP_ATOMIC_STORE)
10785 : : {
10786 : : /* GIMPLE_OMP_ATOMIC_STORE is analogous to
10787 : : GIMPLE_OMP_RETURN, but matches with
10788 : : GIMPLE_OMP_ATOMIC_LOAD. */
10789 : 9486 : gcc_assert (parent);
10790 : 9486 : gcc_assert (parent->type == GIMPLE_OMP_ATOMIC_LOAD);
10791 : 9486 : region = parent;
10792 : 9486 : region->exit = bb;
10793 : 9486 : parent = parent->outer;
10794 : : }
10795 : 175651 : else if (code == GIMPLE_OMP_CONTINUE)
10796 : : {
10797 : 49302 : gcc_assert (parent);
10798 : 49302 : parent->cont = bb;
10799 : : }
10800 : 126349 : else if (code == GIMPLE_OMP_SECTIONS_SWITCH)
10801 : : {
10802 : : /* GIMPLE_OMP_SECTIONS_SWITCH is part of
10803 : : GIMPLE_OMP_SECTIONS, and we do nothing for it. */
10804 : : }
10805 : : else
10806 : : {
10807 : 125971 : region = new_omp_region (bb, code, parent);
10808 : : /* Otherwise... */
10809 : 125971 : if (code == GIMPLE_OMP_TARGET)
10810 : : {
10811 : 34936 : switch (gimple_omp_target_kind (stmt))
10812 : : {
10813 : : case GF_OMP_TARGET_KIND_REGION:
10814 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
10815 : : case GF_OMP_TARGET_KIND_OACC_KERNELS:
10816 : : case GF_OMP_TARGET_KIND_OACC_SERIAL:
10817 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
10818 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
10819 : : break;
10820 : : case GF_OMP_TARGET_KIND_UPDATE:
10821 : : case GF_OMP_TARGET_KIND_ENTER_DATA:
10822 : : case GF_OMP_TARGET_KIND_EXIT_DATA:
10823 : : case GF_OMP_TARGET_KIND_DATA:
10824 : : case GF_OMP_TARGET_KIND_OACC_DATA:
10825 : : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
10826 : : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
10827 : : case GF_OMP_TARGET_KIND_OACC_UPDATE:
10828 : : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
10829 : : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
10830 : : case GF_OMP_TARGET_KIND_OACC_DECLARE:
10831 : : /* ..., other than for those stand-alone directives...
10832 : : To be precise, target data isn't stand-alone, but
10833 : : gimplifier put the end API call into try finally block
10834 : : for it, so omp expansion can treat it as such. */
10835 : : region = NULL;
10836 : : break;
10837 : 0 : default:
10838 : 0 : gcc_unreachable ();
10839 : : }
10840 : : }
10841 : 91035 : else if (code == GIMPLE_OMP_ORDERED
10842 : 91035 : && gimple_omp_ordered_standalone_p (stmt))
10843 : : /* #pragma omp ordered depend is also just a stand-alone
10844 : : directive. */
10845 : : region = NULL;
10846 : 90334 : else if (code == GIMPLE_OMP_TASK
10847 : 90334 : && gimple_omp_task_taskwait_p (stmt))
10848 : : /* #pragma omp taskwait depend(...) is a stand-alone directive. */
10849 : : region = NULL;
10850 : 90256 : else if (code == GIMPLE_OMP_TASKGROUP)
10851 : : /* #pragma omp taskgroup isn't a stand-alone directive, but
10852 : : gimplifier put the end API call into try finall block
10853 : : for it, so omp expansion can treat it as such. */
10854 : : region = NULL;
10855 : : /* ..., this directive becomes the parent for a new region. */
10856 : : if (region)
10857 : : parent = region;
10858 : : }
10859 : : }
10860 : :
10861 : 1309417 : if (single_tree && !parent)
10862 : 0 : return;
10863 : :
10864 : 1309417 : for (son = first_dom_son (CDI_DOMINATORS, bb);
10865 : 2554195 : son;
10866 : 1244778 : son = next_dom_son (CDI_DOMINATORS, son))
10867 : 1244778 : build_omp_regions_1 (son, parent, single_tree);
10868 : : }
10869 : :
10870 : : /* Builds the tree of OMP regions rooted at ROOT, storing it to
10871 : : root_omp_region. */
10872 : :
10873 : : static void
10874 : 0 : build_omp_regions_root (basic_block root)
10875 : : {
10876 : 0 : gcc_assert (root_omp_region == NULL);
10877 : 0 : build_omp_regions_1 (root, NULL, true);
10878 : 0 : gcc_assert (root_omp_region != NULL);
10879 : 0 : }
10880 : :
10881 : : /* Expands omp construct (and its subconstructs) starting in HEAD. */
10882 : :
10883 : : void
10884 : 0 : omp_expand_local (basic_block head)
10885 : : {
10886 : 0 : build_omp_regions_root (head);
10887 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
10888 : : {
10889 : 0 : fprintf (dump_file, "\nOMP region tree\n\n");
10890 : 0 : dump_omp_region (dump_file, root_omp_region, 0);
10891 : 0 : fprintf (dump_file, "\n");
10892 : : }
10893 : :
10894 : 0 : remove_exit_barriers (root_omp_region);
10895 : 0 : expand_omp (root_omp_region);
10896 : :
10897 : 0 : omp_free_regions ();
10898 : 0 : }
10899 : :
10900 : : /* Scan the CFG and build a tree of OMP regions. Return the root of
10901 : : the OMP region tree. */
10902 : :
10903 : : static void
10904 : 64639 : build_omp_regions (void)
10905 : : {
10906 : 64639 : gcc_assert (root_omp_region == NULL);
10907 : 64639 : calculate_dominance_info (CDI_DOMINATORS);
10908 : 64639 : build_omp_regions_1 (ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, false);
10909 : 64639 : }
10910 : :
10911 : : /* Main entry point for expanding OMP-GIMPLE into runtime calls. */
10912 : :
10913 : : static unsigned int
10914 : 64639 : execute_expand_omp (void)
10915 : : {
10916 : 64639 : build_omp_regions ();
10917 : :
10918 : 64639 : if (!root_omp_region)
10919 : : return 0;
10920 : :
10921 : 23667 : if (dump_file)
10922 : : {
10923 : 164 : fprintf (dump_file, "\nOMP region tree\n\n");
10924 : 164 : dump_omp_region (dump_file, root_omp_region, 0);
10925 : 164 : fprintf (dump_file, "\n");
10926 : : }
10927 : :
10928 : 23667 : remove_exit_barriers (root_omp_region);
10929 : :
10930 : 23667 : expand_omp (root_omp_region);
10931 : :
10932 : 23667 : omp_free_regions ();
10933 : :
10934 : 23667 : return (TODO_cleanup_cfg
10935 : 46774 : | (gimple_in_ssa_p (cfun) ? TODO_update_ssa_only_virtuals : 0));
10936 : : }
10937 : :
10938 : : /* OMP expansion -- the default pass, run before creation of SSA form. */
10939 : :
10940 : : namespace {
10941 : :
10942 : : const pass_data pass_data_expand_omp =
10943 : : {
10944 : : GIMPLE_PASS, /* type */
10945 : : "ompexp", /* name */
10946 : : OPTGROUP_OMP, /* optinfo_flags */
10947 : : TV_NONE, /* tv_id */
10948 : : PROP_gimple_any, /* properties_required */
10949 : : PROP_gimple_eomp, /* properties_provided */
10950 : : 0, /* properties_destroyed */
10951 : : 0, /* todo_flags_start */
10952 : : 0, /* todo_flags_finish */
10953 : : };
10954 : :
10955 : : class pass_expand_omp : public gimple_opt_pass
10956 : : {
10957 : : public:
10958 : 283157 : pass_expand_omp (gcc::context *ctxt)
10959 : 566314 : : gimple_opt_pass (pass_data_expand_omp, ctxt)
10960 : : {}
10961 : :
10962 : : /* opt_pass methods: */
10963 : 2765159 : unsigned int execute (function *) final override
10964 : : {
10965 : 2758127 : bool gate = ((flag_openacc != 0 || flag_openmp != 0
10966 : 2721814 : || flag_openmp_simd != 0)
10967 : 2803727 : && !seen_error ());
10968 : :
10969 : : /* This pass always runs, to provide PROP_gimple_eomp.
10970 : : But often, there is nothing to do. */
10971 : 2765159 : if (!gate)
10972 : 2723622 : return 0;
10973 : :
10974 : 41537 : return execute_expand_omp ();
10975 : : }
10976 : :
10977 : : }; // class pass_expand_omp
10978 : :
10979 : : } // anon namespace
10980 : :
10981 : : gimple_opt_pass *
10982 : 283157 : make_pass_expand_omp (gcc::context *ctxt)
10983 : : {
10984 : 283157 : return new pass_expand_omp (ctxt);
10985 : : }
10986 : :
10987 : : namespace {
10988 : :
10989 : : const pass_data pass_data_expand_omp_ssa =
10990 : : {
10991 : : GIMPLE_PASS, /* type */
10992 : : "ompexpssa", /* name */
10993 : : OPTGROUP_OMP, /* optinfo_flags */
10994 : : TV_NONE, /* tv_id */
10995 : : PROP_cfg | PROP_ssa, /* properties_required */
10996 : : PROP_gimple_eomp, /* properties_provided */
10997 : : 0, /* properties_destroyed */
10998 : : 0, /* todo_flags_start */
10999 : : TODO_cleanup_cfg | TODO_rebuild_alias, /* todo_flags_finish */
11000 : : };
11001 : :
11002 : : class pass_expand_omp_ssa : public gimple_opt_pass
11003 : : {
11004 : : public:
11005 : 566314 : pass_expand_omp_ssa (gcc::context *ctxt)
11006 : 1132628 : : gimple_opt_pass (pass_data_expand_omp_ssa, ctxt)
11007 : : {}
11008 : :
11009 : : /* opt_pass methods: */
11010 : 231115 : bool gate (function *fun) final override
11011 : : {
11012 : 231115 : return !(fun->curr_properties & PROP_gimple_eomp);
11013 : : }
11014 : 23102 : unsigned int execute (function *) final override
11015 : : {
11016 : 23102 : return execute_expand_omp ();
11017 : : }
11018 : 283157 : opt_pass * clone () final override
11019 : : {
11020 : 283157 : return new pass_expand_omp_ssa (m_ctxt);
11021 : : }
11022 : :
11023 : : }; // class pass_expand_omp_ssa
11024 : :
11025 : : } // anon namespace
11026 : :
11027 : : gimple_opt_pass *
11028 : 283157 : make_pass_expand_omp_ssa (gcc::context *ctxt)
11029 : : {
11030 : 283157 : return new pass_expand_omp_ssa (ctxt);
11031 : : }
11032 : :
11033 : : /* Called from tree-cfg.cc::make_edges to create cfg edges for all relevant
11034 : : GIMPLE_* codes. */
11035 : :
11036 : : bool
11037 : 287156 : omp_make_gimple_edges (basic_block bb, struct omp_region **region,
11038 : : int *region_idx)
11039 : : {
11040 : 287156 : gimple *last = last_nondebug_stmt (bb);
11041 : 287156 : enum gimple_code code = gimple_code (last);
11042 : 287156 : struct omp_region *cur_region = *region;
11043 : 287156 : bool fallthru = false;
11044 : :
11045 : 287156 : switch (code)
11046 : : {
11047 : 75100 : case GIMPLE_OMP_PARALLEL:
11048 : 75100 : case GIMPLE_OMP_FOR:
11049 : 75100 : case GIMPLE_OMP_SINGLE:
11050 : 75100 : case GIMPLE_OMP_TEAMS:
11051 : 75100 : case GIMPLE_OMP_MASTER:
11052 : 75100 : case GIMPLE_OMP_MASKED:
11053 : 75100 : case GIMPLE_OMP_SCOPE:
11054 : 75100 : case GIMPLE_OMP_CRITICAL:
11055 : 75100 : case GIMPLE_OMP_SECTION:
11056 : 75100 : cur_region = new_omp_region (bb, code, cur_region);
11057 : 75100 : fallthru = true;
11058 : 75100 : break;
11059 : :
11060 : 536 : case GIMPLE_OMP_TASKGROUP:
11061 : 536 : cur_region = new_omp_region (bb, code, cur_region);
11062 : 536 : fallthru = true;
11063 : 536 : cur_region = cur_region->outer;
11064 : 536 : break;
11065 : :
11066 : 3861 : case GIMPLE_OMP_TASK:
11067 : 3861 : cur_region = new_omp_region (bb, code, cur_region);
11068 : 3861 : fallthru = true;
11069 : 3861 : if (gimple_omp_task_taskwait_p (last))
11070 : 78 : cur_region = cur_region->outer;
11071 : : break;
11072 : :
11073 : 1116 : case GIMPLE_OMP_ORDERED:
11074 : 1116 : cur_region = new_omp_region (bb, code, cur_region);
11075 : 1116 : fallthru = true;
11076 : 1116 : if (gimple_omp_ordered_standalone_p (last))
11077 : 705 : cur_region = cur_region->outer;
11078 : : break;
11079 : :
11080 : 35308 : case GIMPLE_OMP_TARGET:
11081 : 35308 : cur_region = new_omp_region (bb, code, cur_region);
11082 : 35308 : fallthru = true;
11083 : 35308 : switch (gimple_omp_target_kind (last))
11084 : : {
11085 : : case GF_OMP_TARGET_KIND_REGION:
11086 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
11087 : : case GF_OMP_TARGET_KIND_OACC_KERNELS:
11088 : : case GF_OMP_TARGET_KIND_OACC_SERIAL:
11089 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
11090 : : case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
11091 : : break;
11092 : 15428 : case GF_OMP_TARGET_KIND_UPDATE:
11093 : 15428 : case GF_OMP_TARGET_KIND_ENTER_DATA:
11094 : 15428 : case GF_OMP_TARGET_KIND_EXIT_DATA:
11095 : 15428 : case GF_OMP_TARGET_KIND_DATA:
11096 : 15428 : case GF_OMP_TARGET_KIND_OACC_DATA:
11097 : 15428 : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
11098 : 15428 : case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
11099 : 15428 : case GF_OMP_TARGET_KIND_OACC_UPDATE:
11100 : 15428 : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
11101 : 15428 : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
11102 : 15428 : case GF_OMP_TARGET_KIND_OACC_DECLARE:
11103 : 15428 : cur_region = cur_region->outer;
11104 : 15428 : break;
11105 : 0 : default:
11106 : 0 : gcc_unreachable ();
11107 : : }
11108 : : break;
11109 : :
11110 : 378 : case GIMPLE_OMP_SECTIONS:
11111 : 378 : cur_region = new_omp_region (bb, code, cur_region);
11112 : 378 : fallthru = true;
11113 : 378 : break;
11114 : :
11115 : : case GIMPLE_OMP_SECTIONS_SWITCH:
11116 : : fallthru = false;
11117 : : break;
11118 : :
11119 : 19680 : case GIMPLE_OMP_ATOMIC_LOAD:
11120 : 19680 : case GIMPLE_OMP_ATOMIC_STORE:
11121 : 19680 : fallthru = true;
11122 : 19680 : break;
11123 : :
11124 : 99552 : case GIMPLE_OMP_RETURN:
11125 : : /* In the case of a GIMPLE_OMP_SECTION, the edge will go
11126 : : somewhere other than the next block. This will be
11127 : : created later. */
11128 : 99552 : cur_region->exit = bb;
11129 : 99552 : if (cur_region->type == GIMPLE_OMP_TASK)
11130 : : /* Add an edge corresponding to not scheduling the task
11131 : : immediately. */
11132 : 3783 : make_edge (cur_region->entry, bb, EDGE_ABNORMAL);
11133 : 99552 : fallthru = cur_region->type != GIMPLE_OMP_SECTION;
11134 : 99552 : cur_region = cur_region->outer;
11135 : 99552 : break;
11136 : :
11137 : 51247 : case GIMPLE_OMP_CONTINUE:
11138 : 51247 : cur_region->cont = bb;
11139 : 51247 : switch (cur_region->type)
11140 : : {
11141 : 47086 : case GIMPLE_OMP_FOR:
11142 : : /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
11143 : : succs edges as abnormal to prevent splitting
11144 : : them. */
11145 : 47086 : single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
11146 : : /* Make the loopback edge. */
11147 : 47086 : make_edge (bb, single_succ (cur_region->entry),
11148 : : EDGE_ABNORMAL);
11149 : :
11150 : : /* Create an edge from GIMPLE_OMP_FOR to exit, which
11151 : : corresponds to the case that the body of the loop
11152 : : is not executed at all. */
11153 : 47086 : make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
11154 : 47086 : make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
11155 : 47086 : fallthru = false;
11156 : 47086 : break;
11157 : :
11158 : 378 : case GIMPLE_OMP_SECTIONS:
11159 : : /* Wire up the edges into and out of the nested sections. */
11160 : 378 : {
11161 : 378 : basic_block switch_bb = single_succ (cur_region->entry);
11162 : :
11163 : 378 : struct omp_region *i;
11164 : 1233 : for (i = cur_region->inner; i ; i = i->next)
11165 : : {
11166 : 855 : gcc_assert (i->type == GIMPLE_OMP_SECTION);
11167 : 855 : make_edge (switch_bb, i->entry, 0);
11168 : 855 : make_edge (i->exit, bb, EDGE_FALLTHRU);
11169 : : }
11170 : :
11171 : : /* Make the loopback edge to the block with
11172 : : GIMPLE_OMP_SECTIONS_SWITCH. */
11173 : 378 : make_edge (bb, switch_bb, 0);
11174 : :
11175 : : /* Make the edge from the switch to exit. */
11176 : 378 : make_edge (switch_bb, bb->next_bb, 0);
11177 : 378 : fallthru = false;
11178 : : }
11179 : 378 : break;
11180 : :
11181 : : case GIMPLE_OMP_TASK:
11182 : : fallthru = true;
11183 : : break;
11184 : :
11185 : 0 : default:
11186 : 0 : gcc_unreachable ();
11187 : : }
11188 : : break;
11189 : :
11190 : 0 : default:
11191 : 0 : gcc_unreachable ();
11192 : : }
11193 : :
11194 : 287156 : if (*region != cur_region)
11195 : : {
11196 : 199104 : *region = cur_region;
11197 : 199104 : if (cur_region)
11198 : 159239 : *region_idx = cur_region->entry->index;
11199 : : else
11200 : 39865 : *region_idx = 0;
11201 : : }
11202 : :
11203 : 287156 : return fallthru;
11204 : : }
|