Debian Codegraph @

gcc/omp-expand.cc

raw
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 is_combined_parallel (struct omp_region *region)
125 {
126 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 is_in_offload_region (struct omp_region *region)
133 {
134 gimple *entry_stmt = last_nondebug_stmt (region->entry);
135 if (is_gimple_omp (entry_stmt)
136 && is_gimple_omp_offloaded (entry_stmt))
137 return true;
138 else if (region->outer)
139 return is_in_offload_region (region->outer);
140 else
141 return (lookup_attribute ("omp declare target",
142 DECL_ATTRIBUTES (current_function_decl))
143 != 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 workshare_safe_to_combine_p (basic_block ws_entry_bb)
190 {
191 struct omp_for_data fd;
192 gimple *ws_stmt = last_nondebug_stmt (ws_entry_bb);
193
194 if (gimple_code (ws_stmt) == GIMPLE_OMP_SECTIONS)
195 return true;
196
197 gcc_assert (gimple_code (ws_stmt) == GIMPLE_OMP_FOR);
198 if (gimple_omp_for_kind (ws_stmt) != GF_OMP_FOR_KIND_FOR)
199 return false;
200
201 omp_extract_for_data (as_a <gomp_for *> (ws_stmt), &fd, NULL);
202
203 if (fd.collapse > 1 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
204 return false;
205 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 if (!is_gimple_min_invariant (fd.loop.n1)
214 || !is_gimple_min_invariant (fd.loop.n2)
215 || !is_gimple_min_invariant (fd.loop.step)
216 || (fd.chunk_size && !is_gimple_min_invariant (fd.chunk_size)))
217 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 omp_adjust_chunk_size (tree chunk_size, bool simd_schedule, bool offload)
227 {
228 if (!simd_schedule || integer_zerop (chunk_size))
229 return chunk_size;
230
231 tree vf;
232 tree type = TREE_TYPE (chunk_size);
233
234 if (offload)
235 {
236 cfun->curr_properties &= ~PROP_gimple_lomp_dev;
237 vf = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_GOMP_MAX_VF,
238 unsigned_type_node, 0);
239 vf = fold_convert (type, vf);
240 }
241 else
242 {
243 poly_uint64 vf_num = omp_max_vf (false);
244 if (known_eq (vf_num, 1U))
245 return chunk_size;
246 vf = build_int_cst (type, vf_num);
247 }
248
249 tree vf_minus_one = fold_build2 (MINUS_EXPR, type, vf,
250 build_int_cst (type, 1));
251 tree negative_vf = fold_build1 (NEGATE_EXPR, type, vf);
252 chunk_size = fold_build2 (PLUS_EXPR, type, chunk_size, vf_minus_one);
253 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 get_ws_args_for (gimple *par_stmt, gimple *ws_stmt, bool offload)
262 {
263 tree t;
264 location_t loc = gimple_location (ws_stmt);
265 vec<tree, va_gc> *ws_args;
266
267 if (gomp_for *for_stmt = dyn_cast <gomp_for *> (ws_stmt))
268 {
269 struct omp_for_data fd;
270 tree n1, n2;
271
272 omp_extract_for_data (for_stmt, &fd, NULL);
273 n1 = fd.loop.n1;
274 n2 = fd.loop.n2;
275
276 if (gimple_omp_for_combined_into_p (for_stmt))
277 {
278 tree innerc
279 = omp_find_clause (gimple_omp_parallel_clauses (par_stmt),
280 OMP_CLAUSE__LOOPTEMP_);
281 gcc_assert (innerc);
282 n1 = OMP_CLAUSE_DECL (innerc);
283 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
284 OMP_CLAUSE__LOOPTEMP_);
285 gcc_assert (innerc);
286 n2 = OMP_CLAUSE_DECL (innerc);
287 }
288
289 vec_alloc (ws_args, 3 + (fd.chunk_size != 0));
290
291 t = fold_convert_loc (loc, long_integer_type_node, n1);
292 ws_args->quick_push (t);
293
294 t = fold_convert_loc (loc, long_integer_type_node, n2);
295 ws_args->quick_push (t);
296
297 t = fold_convert_loc (loc, long_integer_type_node, fd.loop.step);
298 ws_args->quick_push (t);
299
300 if (fd.chunk_size)
301 {
302 t = fold_convert_loc (loc, long_integer_type_node, fd.chunk_size);
303 t = omp_adjust_chunk_size (t, fd.simd_schedule, offload);
304 ws_args->quick_push (t);
305 }
306
307 return ws_args;
308 }
309 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 basic_block bb = single_succ (gimple_bb (ws_stmt));
315 t = build_int_cst (unsigned_type_node, EDGE_COUNT (bb->succs) - 1);
316 vec_alloc (ws_args, 1);
317 ws_args->quick_push (t);
318 return ws_args;
319 }
320
321 gcc_unreachable ();
322 }
323
324 /* Discover whether REGION is a combined parallel+workshare region. */
325
326 static void
327 determine_parallel_type (struct omp_region *region)
328 {
329 basic_block par_entry_bb, par_exit_bb;
330 basic_block ws_entry_bb, ws_exit_bb;
331
332 if (region == NULL || region->inner == NULL
333 || region->exit == NULL || region->inner->exit == NULL
334 || region->inner->cont == NULL)
335 return;
336
337 /* We only support parallel+for and parallel+sections. */
338 if (region->type != GIMPLE_OMP_PARALLEL
339 || (region->inner->type != GIMPLE_OMP_FOR
340 && 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 par_entry_bb = region->entry;
346 par_exit_bb = region->exit;
347 ws_entry_bb = region->inner->entry;
348 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 tree pclauses
354 = gimple_omp_parallel_clauses (last_nondebug_stmt (par_entry_bb));
355 if (omp_find_clause (pclauses, OMP_CLAUSE__REDUCTEMP_))
356 return;
357
358 if (single_succ (par_entry_bb) == ws_entry_bb
359 && single_succ (ws_exit_bb) == par_exit_bb
360 && workshare_safe_to_combine_p (ws_entry_bb)
361 && (gimple_omp_parallel_combined_p (last_nondebug_stmt (par_entry_bb))
362 || (last_and_only_stmt (ws_entry_bb)
363 && last_and_only_stmt (par_exit_bb))))
364 {
365 gimple *par_stmt = last_nondebug_stmt (par_entry_bb);
366 gimple *ws_stmt = last_nondebug_stmt (ws_entry_bb);
367
368 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 tree clauses = gimple_omp_for_clauses (ws_stmt);
380 tree c = omp_find_clause (clauses, OMP_CLAUSE_SCHEDULE);
381 if (c == NULL
382 || ((OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
383 == OMP_CLAUSE_SCHEDULE_STATIC)
384 || omp_find_clause (clauses, OMP_CLAUSE_ORDERED)
385 || omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_)
386 || ((c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_))
387 && POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
388 return;
389 }
390 else if (region->inner->type == GIMPLE_OMP_SECTIONS
391 && (omp_find_clause (gimple_omp_sections_clauses (ws_stmt),
392 OMP_CLAUSE__REDUCTEMP_)
393 || omp_find_clause (gimple_omp_sections_clauses (ws_stmt),
394 OMP_CLAUSE__CONDTEMP_)))
395 return;
396
397 region->is_combined_parallel = true;
398 region->inner->is_combined_parallel = true;
399 region->ws_args = get_ws_args_for (par_stmt, ws_stmt,
400 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 dump_omp_region (FILE *file, struct omp_region *region, int indent)
413 {
414 fprintf (file, "%*sbb %d: %s\n", indent, "", region->entry->index,
415 gimple_code_name[region->type]);
416
417 if (region->inner)
418 dump_omp_region (file, region->inner, indent + 4);
419
420 if (region->cont)
421 {
422 fprintf (file, "%*sbb %d: GIMPLE_OMP_CONTINUE\n", indent, "",
423 region->cont->index);
424 }
425
426 if (region->exit)
427 fprintf (file, "%*sbb %d: GIMPLE_OMP_RETURN\n", indent, "",
428 region->exit->index);
429 else
430 fprintf (file, "%*s[no exit marker]\n", indent, "");
431
432 if (region->next)
433 dump_omp_region (file, region->next, indent);
434 }
435
436 DEBUG_FUNCTION void
437 debug_omp_region (struct omp_region *region)
438 {
439 dump_omp_region (stderr, region, 0);
440 }
441
442 DEBUG_FUNCTION void
443 debug_all_omp_regions (void)
444 {
445 dump_omp_region (stderr, root_omp_region, 0);
446 }
447
448 /* Create a new parallel region starting at STMT inside region PARENT. */
449
450 static struct omp_region *
451 new_omp_region (basic_block bb, enum gimple_code type,
452 struct omp_region *parent)
453 {
454 struct omp_region *region = XCNEW (struct omp_region);
455
456 region->outer = parent;
457 region->entry = bb;
458 region->type = type;
459
460 if (parent)
461 {
462 /* This is a nested region. Add it to the list of inner
463 regions in PARENT. */
464 region->next = parent->inner;
465 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 region->next = root_omp_region;
472 root_omp_region = region;
473 }
474
475 return region;
476 }
477
478 /* Release the memory associated with the region tree rooted at REGION. */
479
480 static void
481 free_omp_region_1 (struct omp_region *region)
482 {
483 struct omp_region *i, *n;
484
485 for (i = region->inner; i ; i = n)
486 {
487 n = i->next;
488 free_omp_region_1 (i);
489 }
490
491 free (region);
492 }
493
494 /* Release the memory for the entire omp region tree. */
495
496 void
497 omp_free_regions (void)
498 {
499 struct omp_region *r, *n;
500 for (r = root_omp_region; r ; r = n)
501 {
502 n = r->next;
503 free_omp_region_1 (r);
504 }
505 root_omp_region = NULL;
506 }
507
508 /* A convenience function to build an empty GIMPLE_COND with just the
509 condition. */
510
511 static gcond *
512 gimple_build_cond_empty (tree cond)
513 {
514 enum tree_code pred_code;
515 tree lhs, rhs;
516
517 gimple_cond_get_ops_from_tree (cond, &pred_code, &lhs, &rhs);
518 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 adjust_context_and_scope (struct omp_region *region, tree entry_block,
531 tree child_fndecl)
532 {
533 tree parent_fndecl = NULL_TREE;
534 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 for (region = region->outer;
541 region && parent_fndecl == NULL_TREE; region = region->outer)
542 switch (region->type)
543 {
544 case GIMPLE_OMP_PARALLEL:
545 case GIMPLE_OMP_TASK:
546 case GIMPLE_OMP_TEAMS:
547 entry_stmt = last_nondebug_stmt (region->entry);
548 parent_fndecl = gimple_omp_taskreg_child_fn (entry_stmt);
549 break;
550 case GIMPLE_OMP_TARGET:
551 entry_stmt = last_nondebug_stmt (region->entry);
552 parent_fndecl
553 = gimple_omp_target_child_fn (as_a <gomp_target *> (entry_stmt));
554 break;
555 default:
556 break;
557 }
558
559 if (parent_fndecl == NULL_TREE)
560 parent_fndecl = current_function_decl;
561 DECL_CONTEXT (child_fndecl) = parent_fndecl;
562
563 if (entry_block != NULL_TREE && TREE_CODE (entry_block) == BLOCK)
564 {
565 tree b = BLOCK_SUPERCONTEXT (entry_block);
566 if (TREE_CODE (b) == BLOCK)
567 {
568 DECL_CHAIN (child_fndecl) = BLOCK_VARS (b);
569 BLOCK_VARS (b) = child_fndecl;
570 }
571 }
572 }
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 expand_parallel_call (struct omp_region *region, basic_block bb,
583 gomp_parallel *entry_stmt,
584 vec<tree, va_gc> *ws_args)
585 {
586 tree t, t1, t2, val, cond, c, clauses, flags;
587 gimple_stmt_iterator gsi;
588 gimple *stmt;
589 enum built_in_function start_ix;
590 int start_ix2;
591 location_t clause_loc;
592 vec<tree, va_gc> *args;
593
594 clauses = gimple_omp_parallel_clauses (entry_stmt);
595
596 /* Determine what flavor of GOMP_parallel we will be
597 emitting. */
598 start_ix = BUILT_IN_GOMP_PARALLEL;
599 tree rtmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
600 if (rtmp)
601 start_ix = BUILT_IN_GOMP_PARALLEL_REDUCTIONS;
602 else if (is_combined_parallel (region))
603 {
604 switch (region->inner->type)
605 {
606 case GIMPLE_OMP_FOR:
607 gcc_assert (region->inner->sched_kind != OMP_CLAUSE_SCHEDULE_AUTO);
608 switch (region->inner->sched_kind)
609 {
610 case OMP_CLAUSE_SCHEDULE_RUNTIME:
611 /* For lastprivate(conditional:), our implementation
612 requires monotonic behavior. */
613 if (region->inner->has_lastprivate_conditional != 0)
614 start_ix2 = 3;
615 else if ((region->inner->sched_modifiers
616 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC) != 0)
617 start_ix2 = 6;
618 else if ((region->inner->sched_modifiers
619 & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0)
620 start_ix2 = 7;
621 else
622 start_ix2 = 3;
623 break;
624 case OMP_CLAUSE_SCHEDULE_DYNAMIC:
625 case OMP_CLAUSE_SCHEDULE_GUIDED:
626 if ((region->inner->sched_modifiers
627 & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
628 && !region->inner->has_lastprivate_conditional)
629 {
630 start_ix2 = 3 + region->inner->sched_kind;
631 break;
632 }
633 /* FALLTHRU */
634 default:
635 start_ix2 = region->inner->sched_kind;
636 break;
637 }
638 start_ix2 += (int) BUILT_IN_GOMP_PARALLEL_LOOP_STATIC;
639 start_ix = (enum built_in_function) start_ix2;
640 break;
641 case GIMPLE_OMP_SECTIONS:
642 start_ix = BUILT_IN_GOMP_PARALLEL_SECTIONS;
643 break;
644 default:
645 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 cond = NULL_TREE;
652 val = build_int_cst (unsigned_type_node, 0);
653 flags = build_int_cst (unsigned_type_node, 0);
654
655 c = omp_find_clause (clauses, OMP_CLAUSE_IF);
656 if (c)
657 cond = OMP_CLAUSE_IF_EXPR (c);
658
659 c = omp_find_clause (clauses, OMP_CLAUSE_NUM_THREADS);
660 if (c)
661 {
662 val = OMP_CLAUSE_NUM_THREADS_EXPR (c);
663 clause_loc = OMP_CLAUSE_LOCATION (c);
664 }
665 else
666 clause_loc = gimple_location (entry_stmt);
667
668 c = omp_find_clause (clauses, OMP_CLAUSE_PROC_BIND);
669 if (c)
670 flags = build_int_cst (unsigned_type_node, OMP_CLAUSE_PROC_BIND_KIND (c));
671
672 /* Ensure 'val' is of the correct type. */
673 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 if (cond)
678 {
679 cond = gimple_boolify (cond);
680
681 if (integer_zerop (val))
682 val = fold_build2_loc (clause_loc,
683 EQ_EXPR, unsigned_type_node, cond,
684 build_int_cst (TREE_TYPE (cond), 0));
685 else
686 {
687 basic_block cond_bb, then_bb, else_bb;
688 edge e, e_then, e_else;
689 tree tmp_then, tmp_else, tmp_join, tmp_var;
690
691 tmp_var = create_tmp_var (TREE_TYPE (val));
692 if (gimple_in_ssa_p (cfun))
693 {
694 tmp_then = make_ssa_name (tmp_var);
695 tmp_else = make_ssa_name (tmp_var);
696 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 e = split_block_after_labels (bb);
706 cond_bb = e->src;
707 bb = e->dest;
708 remove_edge (e);
709
710 then_bb = create_empty_bb (cond_bb);
711 else_bb = create_empty_bb (then_bb);
712 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
713 set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
714
715 stmt = gimple_build_cond_empty (cond);
716 gsi = gsi_start_bb (cond_bb);
717 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
718
719 gsi = gsi_start_bb (then_bb);
720 expand_omp_build_assign (&gsi, tmp_then, val, true);
721
722 gsi = gsi_start_bb (else_bb);
723 expand_omp_build_assign (&gsi, tmp_else,
724 build_int_cst (unsigned_type_node, 1),
725 true);
726
727 make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
728 make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
729 add_bb_to_loop (then_bb, cond_bb->loop_father);
730 add_bb_to_loop (else_bb, cond_bb->loop_father);
731 e_then = make_edge (then_bb, bb, EDGE_FALLTHRU);
732 e_else = make_edge (else_bb, bb, EDGE_FALLTHRU);
733
734 if (gimple_in_ssa_p (cfun))
735 {
736 gphi *phi = create_phi_node (tmp_join, bb);
737 add_phi_arg (phi, tmp_then, e_then, UNKNOWN_LOCATION);
738 add_phi_arg (phi, tmp_else, e_else, UNKNOWN_LOCATION);
739 }
740
741 val = tmp_join;
742 }
743
744 gsi = gsi_start_bb (bb);
745 val = force_gimple_operand_gsi (&gsi, val, true, NULL_TREE,
746 false, GSI_CONTINUE_LINKING);
747 }
748
749 gsi = gsi_last_nondebug_bb (bb);
750 t = gimple_omp_parallel_data_arg (entry_stmt);
751 if (t == NULL)
752 t1 = null_pointer_node;
753 else
754 t1 = build_fold_addr_expr (t);
755 tree child_fndecl = gimple_omp_parallel_child_fn (entry_stmt);
756 t2 = build_fold_addr_expr (child_fndecl);
757
758 vec_alloc (args, 4 + vec_safe_length (ws_args));
759 args->quick_push (t2);
760 args->quick_push (t1);
761 args->quick_push (val);
762 if (ws_args)
763 args->splice (*ws_args);
764 args->quick_push (flags);
765
766 t = build_call_expr_loc_vec (UNKNOWN_LOCATION,
767 builtin_decl_explicit (start_ix), args);
768
769 if (rtmp)
770 {
771 tree type = TREE_TYPE (OMP_CLAUSE_DECL (rtmp));
772 t = build2 (MODIFY_EXPR, type, OMP_CLAUSE_DECL (rtmp),
773 fold_convert (type,
774 fold_convert (pointer_sized_int_node, t)));
775 }
776 force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
777 false, GSI_CONTINUE_LINKING);
778 }
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 expand_task_call (struct omp_region *region, basic_block bb,
785 gomp_task *entry_stmt)
786 {
787 tree t1, t2, t3;
788 gimple_stmt_iterator gsi;
789 location_t loc = gimple_location (entry_stmt);
790
791 tree clauses = gimple_omp_task_clauses (entry_stmt);
792
793 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
794 tree untied = omp_find_clause (clauses, OMP_CLAUSE_UNTIED);
795 tree mergeable = omp_find_clause (clauses, OMP_CLAUSE_MERGEABLE);
796 tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
797 tree finalc = omp_find_clause (clauses, OMP_CLAUSE_FINAL);
798 tree priority = omp_find_clause (clauses, OMP_CLAUSE_PRIORITY);
799 tree detach = omp_find_clause (clauses, OMP_CLAUSE_DETACH);
800
801 unsigned int iflags
802 = (untied ? GOMP_TASK_FLAG_UNTIED : 0)
803 | (mergeable ? GOMP_TASK_FLAG_MERGEABLE : 0)
804 | (depend ? GOMP_TASK_FLAG_DEPEND : 0);
805
806 bool taskloop_p = gimple_omp_task_taskloop_p (entry_stmt);
807 tree startvar = NULL_TREE, endvar = NULL_TREE, step = NULL_TREE;
808 tree num_tasks = NULL_TREE;
809 bool ull = false;
810 if (taskloop_p)
811 {
812 gimple *g = last_nondebug_stmt (region->outer->entry);
813 gcc_assert (gimple_code (g) == GIMPLE_OMP_FOR
814 && gimple_omp_for_kind (g) == GF_OMP_FOR_KIND_TASKLOOP);
815 struct omp_for_data fd;
816 omp_extract_for_data (as_a <gomp_for *> (g), &fd, NULL);
817 startvar = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
818 endvar = omp_find_clause (OMP_CLAUSE_CHAIN (startvar),
819 OMP_CLAUSE__LOOPTEMP_);
820 startvar = OMP_CLAUSE_DECL (startvar);
821 endvar = OMP_CLAUSE_DECL (endvar);
822 step = fold_convert_loc (loc, fd.iter_type, fd.loop.step);
823 if (fd.loop.cond_code == LT_EXPR)
824 iflags |= GOMP_TASK_FLAG_UP;
825 tree tclauses = gimple_omp_for_clauses (g);
826 num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_NUM_TASKS);
827 if (num_tasks)
828 {
829 if (OMP_CLAUSE_NUM_TASKS_STRICT (num_tasks))
830 iflags |= GOMP_TASK_FLAG_STRICT;
831 num_tasks = OMP_CLAUSE_NUM_TASKS_EXPR (num_tasks);
832 }
833 else
834 {
835 num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_GRAINSIZE);
836 if (num_tasks)
837 {
838 iflags |= GOMP_TASK_FLAG_GRAINSIZE;
839 if (OMP_CLAUSE_GRAINSIZE_STRICT (num_tasks))
840 iflags |= GOMP_TASK_FLAG_STRICT;
841 num_tasks = OMP_CLAUSE_GRAINSIZE_EXPR (num_tasks);
842 }
843 else
844 num_tasks = integer_zero_node;
845 }
846 num_tasks = fold_convert_loc (loc, long_integer_type_node, num_tasks);
847 if (ifc == NULL_TREE)
848 iflags |= GOMP_TASK_FLAG_IF;
849 if (omp_find_clause (tclauses, OMP_CLAUSE_NOGROUP))
850 iflags |= GOMP_TASK_FLAG_NOGROUP;
851 ull = fd.iter_type == long_long_unsigned_type_node;
852 if (omp_find_clause (clauses, OMP_CLAUSE_REDUCTION))
853 iflags |= GOMP_TASK_FLAG_REDUCTION;
854 }
855 else
856 {
857 if (priority)
858 iflags |= GOMP_TASK_FLAG_PRIORITY;
859 if (detach)
860 iflags |= GOMP_TASK_FLAG_DETACH;
861 }
862
863 tree flags = build_int_cst (unsigned_type_node, iflags);
864
865 tree cond = boolean_true_node;
866 if (ifc)
867 {
868 if (taskloop_p)
869 {
870 tree t = gimple_boolify (OMP_CLAUSE_IF_EXPR (ifc));
871 t = fold_build3_loc (loc, COND_EXPR, unsigned_type_node, t,
872 build_int_cst (unsigned_type_node,
873 GOMP_TASK_FLAG_IF),
874 build_int_cst (unsigned_type_node, 0));
875 flags = fold_build2_loc (loc, PLUS_EXPR, unsigned_type_node,
876 flags, t);
877 }
878 else
879 cond = gimple_boolify (OMP_CLAUSE_IF_EXPR (ifc));
880 }
881
882 if (finalc)
883 {
884 tree t = gimple_boolify (OMP_CLAUSE_FINAL_EXPR (finalc));
885 t = fold_build3_loc (loc, COND_EXPR, unsigned_type_node, t,
886 build_int_cst (unsigned_type_node,
887 GOMP_TASK_FLAG_FINAL),
888 build_int_cst (unsigned_type_node, 0));
889 flags = fold_build2_loc (loc, PLUS_EXPR, unsigned_type_node, flags, t);
890 }
891 if (depend)
892 depend = OMP_CLAUSE_DECL (depend);
893 else
894 depend = build_int_cst (ptr_type_node, 0);
895 if (priority)
896 priority = fold_convert (integer_type_node,
897 OMP_CLAUSE_PRIORITY_EXPR (priority));
898 else
899 priority = integer_zero_node;
900
901 gsi = gsi_last_nondebug_bb (bb);
902
903 detach = (detach
904 ? build_fold_addr_expr (OMP_CLAUSE_DECL (detach))
905 : null_pointer_node);
906
907 tree t = gimple_omp_task_data_arg (entry_stmt);
908 if (t == NULL)
909 t2 = null_pointer_node;
910 else
911 t2 = build_fold_addr_expr_loc (loc, t);
912 t1 = build_fold_addr_expr_loc (loc, gimple_omp_task_child_fn (entry_stmt));
913 t = gimple_omp_task_copy_fn (entry_stmt);
914 if (t == NULL)
915 t3 = null_pointer_node;
916 else
917 t3 = build_fold_addr_expr_loc (loc, t);
918
919 if (taskloop_p)
920 t = build_call_expr (ull
921 ? builtin_decl_explicit (BUILT_IN_GOMP_TASKLOOP_ULL)
922 : 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 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 force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
935 false, GSI_CONTINUE_LINKING);
936 }
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 expand_taskwait_call (basic_block bb, gomp_task *entry_stmt)
944 {
945 tree clauses = gimple_omp_task_clauses (entry_stmt);
946 tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
947 if (depend == NULL_TREE)
948 return;
949
950 depend = OMP_CLAUSE_DECL (depend);
951
952 bool nowait = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT) != NULL_TREE;
953 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
954 enum built_in_function f = (nowait
955 ? BUILT_IN_GOMP_TASKWAIT_DEPEND_NOWAIT
956 : BUILT_IN_GOMP_TASKWAIT_DEPEND);
957 tree t = build_call_expr (builtin_decl_explicit (f), 1, depend);
958
959 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 expand_teams_call (basic_block bb, gomp_teams *entry_stmt)
969 {
970 tree clauses = gimple_omp_teams_clauses (entry_stmt);
971 tree num_teams = omp_find_clause (clauses, OMP_CLAUSE_NUM_TEAMS);
972 if (num_teams == NULL_TREE)
973 num_teams = build_int_cst (unsigned_type_node, 0);
974 else
975 {
976 num_teams = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (num_teams);
977 num_teams = fold_convert (unsigned_type_node, num_teams);
978 }
979 tree thread_limit = omp_find_clause (clauses, OMP_CLAUSE_THREAD_LIMIT);
980 if (thread_limit == NULL_TREE)
981 thread_limit = build_int_cst (unsigned_type_node, 0);
982 else
983 {
984 thread_limit = OMP_CLAUSE_THREAD_LIMIT_EXPR (thread_limit);
985 thread_limit = fold_convert (unsigned_type_node, thread_limit);
986 }
987
988 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
989 tree t = gimple_omp_teams_data_arg (entry_stmt), t1;
990 if (t == NULL)
991 t1 = null_pointer_node;
992 else
993 t1 = build_fold_addr_expr (t);
994 tree child_fndecl = gimple_omp_teams_child_fn (entry_stmt);
995 tree t2 = build_fold_addr_expr (child_fndecl);
996
997 vec<tree, va_gc> *args;
998 vec_alloc (args, 5);
999 args->quick_push (t2);
1000 args->quick_push (t1);
1001 args->quick_push (num_teams);
1002 args->quick_push (thread_limit);
1003 /* For future extensibility. */
1004 args->quick_push (build_zero_cst (unsigned_type_node));
1005
1006 t = build_call_expr_loc_vec (UNKNOWN_LOCATION,
1007 builtin_decl_explicit (BUILT_IN_GOMP_TEAMS_REG),
1008 args);
1009
1010 force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
1011 false, GSI_CONTINUE_LINKING);
1012 }
1013
1014 /* Chain all the DECLs in LIST by their TREE_CHAIN fields. */
1015
1016 static tree
1017 vec2chain (vec<tree, va_gc> *v)
1018 {
1019 tree chain = NULL_TREE, t;
1020 unsigned ix;
1021
1022 FOR_EACH_VEC_SAFE_ELT_REVERSE (v, ix, t)
1023 {
1024 DECL_CHAIN (t) = chain;
1025 chain = t;
1026 }
1027
1028 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 remove_exit_barrier (struct omp_region *region)
1039 {
1040 gimple_stmt_iterator gsi;
1041 basic_block exit_bb;
1042 edge_iterator ei;
1043 edge e;
1044 gimple *stmt;
1045 int any_addressable_vars = -1;
1046
1047 exit_bb = region->exit;
1048
1049 /* If the parallel region doesn't return, we don't have REGION->EXIT
1050 block at all. */
1051 if (! exit_bb)
1052 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 gsi = gsi_last_nondebug_bb (exit_bb);
1060 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
1061 gsi_prev_nondebug (&gsi);
1062 if (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1063 return;
1064
1065 FOR_EACH_EDGE (e, ei, exit_bb->preds)
1066 {
1067 gsi = gsi_last_nondebug_bb (e->src);
1068 if (gsi_end_p (gsi))
1069 continue;
1070 stmt = gsi_stmt (gsi);
1071 if (gimple_code (stmt) == GIMPLE_OMP_RETURN
1072 && !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 if (any_addressable_vars < 0)
1083 {
1084 gomp_parallel *parallel_stmt
1085 = as_a <gomp_parallel *> (last_nondebug_stmt (region->entry));
1086 tree child_fun = gimple_omp_parallel_child_fn (parallel_stmt);
1087 tree local_decls, block, decl;
1088 unsigned ix;
1089
1090 any_addressable_vars = 0;
1091 FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (child_fun), ix, decl)
1092 if (TREE_ADDRESSABLE (decl))
1093 {
1094 any_addressable_vars = 1;
1095 break;
1096 }
1097 for (block = gimple_block (stmt);
1098 !any_addressable_vars
1099 && block
1100 && TREE_CODE (block) == BLOCK;
1101 block = BLOCK_SUPERCONTEXT (block))
1102 {
1103 for (local_decls = BLOCK_VARS (block);
1104 local_decls;
1105 local_decls = DECL_CHAIN (local_decls))
1106 if (TREE_ADDRESSABLE (local_decls))
1107 {
1108 any_addressable_vars = 1;
1109 break;
1110 }
1111 if (block == gimple_block (parallel_stmt))
1112 break;
1113 }
1114 }
1115 if (!any_addressable_vars)
1116 gimple_omp_return_set_nowait (stmt);
1117 }
1118 }
1119 }
1120
1121 static void
1122 remove_exit_barriers (struct omp_region *region)
1123 {
1124 if (region->type == GIMPLE_OMP_PARALLEL)
1125 remove_exit_barrier (region);
1126
1127 if (region->inner)
1128 {
1129 region = region->inner;
1130 remove_exit_barriers (region);
1131 while (region->next)
1132 {
1133 region = region->next;
1134 remove_exit_barriers (region);
1135 }
1136 }
1137 }
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 optimize_omp_library_calls (gimple *entry_stmt)
1149 {
1150 basic_block bb;
1151 gimple_stmt_iterator gsi;
1152 tree thr_num_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
1153 tree thr_num_id = DECL_ASSEMBLER_NAME (thr_num_tree);
1154 tree num_thr_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
1155 tree num_thr_id = DECL_ASSEMBLER_NAME (num_thr_tree);
1156 bool untied_task = (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
1157 && omp_find_clause (gimple_omp_task_clauses (entry_stmt),
1158 OMP_CLAUSE_UNTIED) != NULL);
1159
1160 FOR_EACH_BB_FN (bb, cfun)
1161 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1162 {
1163 gimple *call = gsi_stmt (gsi);
1164 tree decl;
1165
1166 if (is_gimple_call (call)
1167 && (decl = gimple_call_fndecl (call))
1168 && DECL_EXTERNAL (decl)
1169 && TREE_PUBLIC (decl)
1170 && DECL_INITIAL (decl) == NULL)
1171 {
1172 tree built_in;
1173
1174 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 if (untied_task)
1179 continue;
1180 built_in = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
1181 }
1182 else if (DECL_NAME (decl) == num_thr_id)
1183 built_in = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
1184 else
1185 continue;
1186
1187 if (DECL_ASSEMBLER_NAME (decl) != DECL_ASSEMBLER_NAME (built_in)
1188 || gimple_call_num_args (call) != 0)
1189 continue;
1190
1191 if (flag_exceptions && !TREE_NOTHROW (decl))
1192 continue;
1193
1194 if (TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE
1195 || !types_compatible_p (TREE_TYPE (TREE_TYPE (decl)),
1196 TREE_TYPE (TREE_TYPE (built_in))))
1197 continue;
1198
1199 gimple_call_set_fndecl (call, built_in);
1200 }
1201 }
1202 }
1203
1204 /* Callback for expand_omp_build_assign. Return non-NULL if *tp needs to be
1205 regimplified. */
1206
1207 static tree
1208 expand_omp_regimplify_p (tree *tp, int *walk_subtrees, void *)
1209 {
1210 tree t = *tp;
1211
1212 /* Any variable with DECL_VALUE_EXPR needs to be regimplified. */
1213 if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
1214 return t;
1215
1216 if (TREE_CODE (t) == ADDR_EXPR)
1217 recompute_tree_invariant_for_addr_expr (t);
1218
1219 *walk_subtrees = !TYPE_P (t) && !DECL_P (t);
1220 return NULL_TREE;
1221 }
1222
1223 /* Prepend or append TO = FROM assignment before or after *GSI_P. */
1224
1225 static void
1226 expand_omp_build_assign (gimple_stmt_iterator *gsi_p, tree to, tree from,
1227 bool after)
1228 {
1229 bool simple_p = DECL_P (to) && TREE_ADDRESSABLE (to);
1230 from = force_gimple_operand_gsi (gsi_p, from, simple_p, NULL_TREE,
1231 !after, after ? GSI_CONTINUE_LINKING
1232 : GSI_SAME_STMT);
1233 gimple *stmt = gimple_build_assign (to, from);
1234 if (after)
1235 gsi_insert_after (gsi_p, stmt, GSI_CONTINUE_LINKING);
1236 else
1237 gsi_insert_before (gsi_p, stmt, GSI_SAME_STMT);
1238 if (walk_tree (&from, expand_omp_regimplify_p, NULL, NULL)
1239 || walk_tree (&to, expand_omp_regimplify_p, NULL, NULL))
1240 {
1241 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1242 gimple_regimplify_operands (stmt, &gsi);
1243 }
1244 }
1245
1246 /* Prepend or append LHS CODE RHS condition before or after *GSI_P. */
1247
1248 static gcond *
1249 expand_omp_build_cond (gimple_stmt_iterator *gsi_p, enum tree_code code,
1250 tree lhs, tree rhs, bool after = false)
1251 {
1252 gcond *cond_stmt = gimple_build_cond (code, lhs, rhs, NULL_TREE, NULL_TREE);
1253 if (after)
1254 gsi_insert_after (gsi_p, cond_stmt, GSI_CONTINUE_LINKING);
1255 else
1256 gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT);
1257 if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
1258 NULL, NULL)
1259 || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p,
1260 NULL, NULL))
1261 {
1262 gimple_stmt_iterator gsi = gsi_for_stmt (cond_stmt);
1263 gimple_regimplify_operands (cond_stmt, &gsi);
1264 }
1265 return cond_stmt;
1266 }
1267
1268 /* Expand the OpenMP parallel or task directive starting at REGION. */
1269
1270 static void
1271 expand_omp_taskreg (struct omp_region *region)
1272 {
1273 basic_block entry_bb, exit_bb, new_bb;
1274 struct function *child_cfun;
1275 tree child_fn, block, t;
1276 gimple_stmt_iterator gsi;
1277 gimple *entry_stmt, *stmt;
1278 edge e;
1279 vec<tree, va_gc> *ws_args;
1280
1281 entry_stmt = last_nondebug_stmt (region->entry);
1282 if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
1283 && gimple_omp_task_taskwait_p (entry_stmt))
1284 {
1285 new_bb = region->entry;
1286 gsi = gsi_last_nondebug_bb (region->entry);
1287 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TASK);
1288 gsi_remove (&gsi, true);
1289 expand_taskwait_call (new_bb, as_a <gomp_task *> (entry_stmt));
1290 return;
1291 }
1292
1293 child_fn = gimple_omp_taskreg_child_fn (entry_stmt);
1294 child_cfun = DECL_STRUCT_FUNCTION (child_fn);
1295
1296 entry_bb = region->entry;
1297 if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK)
1298 exit_bb = region->cont;
1299 else
1300 exit_bb = region->exit;
1301
1302 if (is_combined_parallel (region))
1303 ws_args = region->ws_args;
1304 else
1305 ws_args = NULL;
1306
1307 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 edge entry_succ_e, exit_succ_e;
1313
1314 entry_succ_e = single_succ_edge (entry_bb);
1315
1316 gsi = gsi_last_nondebug_bb (entry_bb);
1317 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 gsi_remove (&gsi, true);
1321
1322 new_bb = entry_bb;
1323 if (exit_bb)
1324 {
1325 exit_succ_e = single_succ_edge (exit_bb);
1326 make_edge (new_bb, exit_succ_e->dest, EDGE_FALLTHRU);
1327 }
1328 remove_edge_and_dominated_blocks (entry_succ_e);
1329 }
1330 else
1331 {
1332 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 if (gimple_omp_taskreg_data_arg (entry_stmt))
1348 {
1349 basic_block entry_succ_bb
1350 = single_succ_p (entry_bb) ? single_succ (entry_bb)
1351 : FALLTHRU_EDGE (entry_bb)->dest;
1352 tree arg;
1353 gimple *parcopy_stmt = NULL;
1354
1355 for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
1356 {
1357 gimple *stmt;
1358
1359 gcc_assert (!gsi_end_p (gsi));
1360 stmt = gsi_stmt (gsi);
1361 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1362 continue;
1363
1364 if (gimple_num_ops (stmt) == 2)
1365 {
1366 tree arg = gimple_assign_rhs1 (stmt);
1367
1368 /* We're ignore the subcode because we're
1369 effectively doing a STRIP_NOPS. */
1370
1371 if (TREE_CODE (arg) == ADDR_EXPR
1372 && (TREE_OPERAND (arg, 0)
1373 == gimple_omp_taskreg_data_arg (entry_stmt)))
1374 {
1375 parcopy_stmt = stmt;
1376 break;
1377 }
1378 }
1379 }
1380
1381 gcc_assert (parcopy_stmt != NULL);
1382 arg = DECL_ARGUMENTS (child_fn);
1383
1384 if (!gimple_in_ssa_p (cfun))
1385 {
1386 if (gimple_assign_lhs (parcopy_stmt) == arg)
1387 gsi_remove (&gsi, true);
1388 else
1389 {
1390 /* ?? Is setting the subcode really necessary ?? */
1391 gimple_omp_set_subcode (parcopy_stmt, TREE_CODE (arg));
1392 gimple_assign_set_rhs1 (parcopy_stmt, arg);
1393 }
1394 }
1395 else
1396 {
1397 tree lhs = gimple_assign_lhs (parcopy_stmt);
1398 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 gimple_assign_set_rhs1 (parcopy_stmt, arg);
1406 update_stmt (parcopy_stmt);
1407 }
1408 }
1409
1410 /* Declare local variables needed in CHILD_CFUN. */
1411 block = DECL_INITIAL (child_fn);
1412 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 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
1417 if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
1418 varpool_node::finalize_decl (t);
1419 DECL_SAVED_TREE (child_fn) = NULL;
1420 /* We'll create a CFG for child_fn, so no gimple body is needed. */
1421 gimple_set_body (child_fn, NULL);
1422 TREE_USED (block) = 1;
1423
1424 /* Reset DECL_CONTEXT on function arguments. */
1425 for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
1426 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 gsi = gsi_last_nondebug_bb (entry_bb);
1431 stmt = gsi_stmt (gsi);
1432 gcc_assert (stmt && (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
1433 || gimple_code (stmt) == GIMPLE_OMP_TASK
1434 || gimple_code (stmt) == GIMPLE_OMP_TEAMS));
1435 e = split_block (entry_bb, stmt);
1436 gsi_remove (&gsi, true);
1437 entry_bb = e->dest;
1438 edge e2 = NULL;
1439 if (gimple_code (entry_stmt) != GIMPLE_OMP_TASK)
1440 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
1441 else
1442 {
1443 e2 = make_edge (e->src, BRANCH_EDGE (entry_bb)->dest, EDGE_ABNORMAL);
1444 gcc_assert (e2->dest == region->exit);
1445 remove_edge (BRANCH_EDGE (entry_bb));
1446 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e->src);
1447 gsi = gsi_last_nondebug_bb (region->exit);
1448 gcc_assert (!gsi_end_p (gsi)
1449 && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
1450 gsi_remove (&gsi, true);
1451 }
1452
1453 /* Convert GIMPLE_OMP_{RETURN,CONTINUE} into a RETURN_EXPR. */
1454 if (exit_bb)
1455 {
1456 gsi = gsi_last_nondebug_bb (exit_bb);
1457 gcc_assert (!gsi_end_p (gsi)
1458 && (gimple_code (gsi_stmt (gsi))
1459 == (e2 ? GIMPLE_OMP_CONTINUE : GIMPLE_OMP_RETURN)));
1460 stmt = gimple_build_return (NULL);
1461 gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
1462 gsi_remove (&gsi, true);
1463 }
1464
1465 /* Move the parallel region into CHILD_CFUN. */
1466
1467 if (gimple_in_ssa_p (cfun))
1468 {
1469 init_tree_ssa (child_cfun);
1470 init_ssa_operands (child_cfun);
1471 child_cfun->gimple_df->in_ssa_p = true;
1472 block = NULL_TREE;
1473 }
1474 else
1475 block = gimple_block (entry_stmt);
1476
1477 new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
1478 if (exit_bb)
1479 single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
1480 if (e2)
1481 {
1482 basic_block dest_bb = e2->dest;
1483 if (!exit_bb)
1484 make_edge (new_bb, dest_bb, EDGE_FALLTHRU);
1485 remove_edge (e2);
1486 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 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1491 child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
1492
1493 /* Remove non-local VAR_DECLs from child_cfun->local_decls list. */
1494 num = vec_safe_length (child_cfun->local_decls);
1495 for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
1496 {
1497 t = (*child_cfun->local_decls)[srcidx];
1498 if (DECL_CONTEXT (t) == cfun->decl)
1499 continue;
1500 if (srcidx != dstidx)
1501 (*child_cfun->local_decls)[dstidx] = t;
1502 dstidx++;
1503 }
1504 if (dstidx != num)
1505 vec_safe_truncate (child_cfun->local_decls, dstidx);
1506
1507 /* Inform the callgraph about the new function. */
1508 child_cfun->curr_properties = cfun->curr_properties;
1509 child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
1510 child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
1511 cgraph_node *node = cgraph_node::get_create (child_fn);
1512 node->parallelized_function = 1;
1513 node->has_omp_variant_constructs
1514 |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
1515 cgraph_node::add_new_function (child_fn, true);
1516
1517 bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
1518 && !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 push_cfun (child_cfun);
1523 if (need_asm)
1524 assign_assembler_name_if_needed (child_fn);
1525
1526 if (optimize)
1527 optimize_omp_library_calls (entry_stmt);
1528 update_max_bb_count ();
1529 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 if (flag_exceptions)
1536 {
1537 basic_block bb;
1538 bool changed = false;
1539
1540 FOR_EACH_BB_FN (bb, cfun)
1541 changed |= gimple_purge_dead_eh_edges (bb);
1542 if (changed)
1543 cleanup_tree_cfg ();
1544 }
1545 if (gimple_in_ssa_p (cfun))
1546 update_ssa (TODO_update_ssa);
1547 if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1548 verify_loop_structure ();
1549 pop_cfun ();
1550
1551 if (dump_file && !gimple_in_ssa_p (cfun))
1552 {
1553 omp_any_child_fn_dumped = true;
1554 dump_function_header (dump_file, child_fn, dump_flags);
1555 dump_function_to_file (child_fn, dump_file, dump_flags);
1556 }
1557 }
1558
1559 adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
1560
1561 if (gimple_code (entry_stmt) == GIMPLE_OMP_PARALLEL)
1562 expand_parallel_call (region, new_bb,
1563 as_a <gomp_parallel *> (entry_stmt), ws_args);
1564 else if (gimple_code (entry_stmt) == GIMPLE_OMP_TEAMS)
1565 expand_teams_call (new_bb, as_a <gomp_teams *> (entry_stmt));
1566 else
1567 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 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 tree tiling = fd->tiling;
1592 tree total = build_int_cst (bound_type, 1);
1593 int ix;
1594
1595 gcc_assert (integer_onep (fd->loop.step));
1596 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 for (ix = fd->collapse; ix--;)
1602 {
1603 const omp_for_data_loop *loop = &fd->loops[ix];
1604
1605 tree iter_type = TREE_TYPE (loop->v);
1606 tree plus_type = iter_type;
1607
1608 gcc_assert (loop->cond_code == LT_EXPR || loop->cond_code == GT_EXPR);
1609
1610 if (POINTER_TYPE_P (iter_type))
1611 plus_type = sizetype;
1612
1613 if (tiling)
1614 {
1615 tree num = build_int_cst (integer_type_node, fd->collapse);
1616 tree loop_no = build_int_cst (integer_type_node, ix);
1617 tree tile = TREE_VALUE (tiling);
1618 gcall *call
1619 = 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 counts[ix].outer = create_tmp_var (iter_type, ".outer");
1624 counts[ix].tile = create_tmp_var (diff_type, ".tile");
1625 gimple_call_set_lhs (call, counts[ix].tile);
1626 gimple_set_location (call, loc);
1627 gsi_insert_before (gsi, call, GSI_SAME_STMT);
1628
1629 tiling = TREE_CHAIN (tiling);
1630 }
1631 else
1632 {
1633 counts[ix].tile = NULL;
1634 counts[ix].outer = loop->v;
1635 }
1636
1637 tree b = loop->n1;
1638 tree e = loop->n2;
1639 tree s = loop->step;
1640 bool up = loop->cond_code == LT_EXPR;
1641 tree dir = build_int_cst (diff_type, up ? +1 : -1);
1642 bool negating;
1643 tree expr;
1644
1645 b = force_gimple_operand_gsi (gsi, b, true, NULL_TREE,
1646 true, GSI_SAME_STMT);
1647 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 negating = !up && TYPE_UNSIGNED (TREE_TYPE (s));
1652 if (negating)
1653 s = fold_build1 (NEGATE_EXPR, TREE_TYPE (s), s);
1654 s = fold_convert (diff_type, s);
1655 if (negating)
1656 s = fold_build1 (NEGATE_EXPR, diff_type, s);
1657 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 negating = !up && TYPE_UNSIGNED (iter_type);
1662 expr = fold_build2 (MINUS_EXPR, plus_type,
1663 fold_convert (plus_type, negating ? b : e),
1664 fold_convert (plus_type, negating ? e : b));
1665 expr = fold_convert (diff_type, expr);
1666 if (negating)
1667 expr = fold_build1 (NEGATE_EXPR, diff_type, expr);
1668 tree range = force_gimple_operand_gsi
1669 (gsi, expr, true, NULL_TREE, true, GSI_SAME_STMT);
1670
1671 /* Determine number of iterations. */
1672 expr = fold_build2 (MINUS_EXPR, diff_type, range, dir);
1673 expr = fold_build2 (PLUS_EXPR, diff_type, expr, s);
1674 expr = fold_build2 (TRUNC_DIV_EXPR, diff_type, expr, s);
1675
1676 tree iters = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE,
1677 true, GSI_SAME_STMT);
1678
1679 counts[ix].base = b;
1680 counts[ix].iters = iters;
1681 counts[ix].step = s;
1682
1683 total = fold_build2 (MULT_EXPR, bound_type, total,
1684 fold_convert (bound_type, iters));
1685 }
1686
1687 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 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 tree ivar_type = TREE_TYPE (ivar);
1703
1704 /* The most rapidly changing iteration variable is the innermost
1705 one. */
1706 for (int ix = fd->collapse; ix--;)
1707 {
1708 const omp_for_data_loop *loop = &fd->loops[ix];
1709 const oacc_collapse *collapse = &counts[ix];
1710 tree v = inner ? loop->v : collapse->outer;
1711 tree iter_type = TREE_TYPE (v);
1712 tree plus_type = iter_type;
1713 enum tree_code plus_code = PLUS_EXPR;
1714 tree expr;
1715
1716 if (POINTER_TYPE_P (iter_type))
1717 {
1718 plus_code = POINTER_PLUS_EXPR;
1719 plus_type = sizetype;
1720 }
1721
1722 expr = ivar;
1723 if (ix)
1724 {
1725 tree mod = fold_convert (ivar_type, collapse->iters);
1726 ivar = fold_build2 (TRUNC_DIV_EXPR, ivar_type, expr, mod);
1727 expr = fold_build2 (TRUNC_MOD_EXPR, ivar_type, expr, mod);
1728 ivar = force_gimple_operand_gsi (gsi, ivar, true, NULL_TREE,
1729 true, GSI_SAME_STMT);
1730 }
1731
1732 expr = fold_build2 (MULT_EXPR, diff_type, fold_convert (diff_type, expr),
1733 fold_convert (diff_type, collapse->step));
1734 expr = fold_build2 (plus_code, iter_type,
1735 inner ? collapse->outer : collapse->base,
1736 fold_convert (plus_type, expr));
1737 expr = force_gimple_operand_gsi (gsi, expr, false, NULL_TREE,
1738 true, GSI_SAME_STMT);
1739 gassign *ass = gimple_build_assign (v, expr);
1740 gsi_insert_before (gsi, ass, GSI_SAME_STMT);
1741 }
1742 }
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 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 tree t, type = TREE_TYPE (fd->loop.v);
1837 edge e, ne;
1838 int i;
1839
1840 /* Collapsed loops need work for expansion into SSA form. */
1841 gcc_assert (!gimple_in_ssa_p (cfun));
1842
1843 if (gimple_omp_for_combined_into_p (fd->for_stmt)
1844 && TREE_CODE (fd->loop.n2) != INTEGER_CST)
1845 {
1846 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 tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
1851 OMP_CLAUSE__LOOPTEMP_);
1852 gcc_assert (innerc);
1853 for (i = 0; i < fd->collapse; i++)
1854 {
1855 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
1856 OMP_CLAUSE__LOOPTEMP_);
1857 gcc_assert (innerc);
1858 if (i)
1859 counts[i] = OMP_CLAUSE_DECL (innerc);
1860 else
1861 counts[0] = NULL_TREE;
1862 }
1863 if (fd->non_rect
1864 && fd->last_nonrect == fd->first_nonrect + 1
1865 && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
1866 {
1867 tree c[4];
1868 for (i = 0; i < 4; i++)
1869 {
1870 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
1871 OMP_CLAUSE__LOOPTEMP_);
1872 gcc_assert (innerc);
1873 c[i] = OMP_CLAUSE_DECL (innerc);
1874 }
1875 counts[0] = c[0];
1876 fd->first_inner_iterations = c[1];
1877 fd->factor = c[2];
1878 fd->adjn1 = c[3];
1879 }
1880 return;
1881 }
1882
1883 for (i = fd->collapse; i < fd->ordered; i++)
1884 {
1885 tree itype = TREE_TYPE (fd->loops[i].v);
1886 counts[i] = NULL_TREE;
1887 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 if (t && integer_zerop (t))
1891 {
1892 for (i = fd->collapse; i < fd->ordered; i++)
1893 counts[i] = build_int_cst (type, 0);
1894 break;
1895 }
1896 }
1897 bool rect_count_seen = false;
1898 bool init_n2 = SSA_VAR_P (fd->loop.n2) && zero_iter1_bb;
1899 for (i = 0; i < (fd->ordered ? fd->ordered : fd->collapse); i++)
1900 {
1901 tree itype = TREE_TYPE (fd->loops[i].v);
1902
1903 if (i >= fd->collapse && counts[i])
1904 continue;
1905 if (fd->non_rect)
1906 {
1907 /* Skip loops that use outer iterators in their expressions
1908 during this phase. */
1909 if (fd->loops[i].m1 || fd->loops[i].m2)
1910 {
1911 counts[i] = build_zero_cst (type);
1912 continue;
1913 }
1914 }
1915 if ((SSA_VAR_P (fd->loop.n2) || i >= fd->collapse)
1916 && ((t = fold_binary (fd->loops[i].cond_code, boolean_type_node,
1917 fold_convert (itype, fd->loops[i].n1),
1918 fold_convert (itype, fd->loops[i].n2)))
1919 == NULL_TREE || !integer_onep (t)))
1920 {
1921 gcond *cond_stmt;
1922 tree n1, n2;
1923 if (init_n2 && i < fd->collapse && !rect_count_seen)
1924 {
1925 /* When called with non-NULL zero_iter1_bb, we won't clear
1926 fd->loop.n2 in the if (zero_iter_bb == NULL) code below
1927 and if it is prior to storing fd->loop.n2 where
1928 rect_count_seen is set, it could be used uninitialized.
1929 As zero_iter1_bb in that case can be reached also if there
1930 are non-zero iterations, the clearing can't be emitted
1931 to the zero_iter1_bb, but needs to be done before the
1932 condition. */
1933 gassign *assign_stmt
1934 = gimple_build_assign (fd->loop.n2, build_zero_cst (type));
1935 gsi_insert_before (gsi, assign_stmt, GSI_SAME_STMT);
1936 init_n2 = false;
1937 }
1938 n1 = fold_convert (itype, unshare_expr (fd->loops[i].n1));
1939 n1 = force_gimple_operand_gsi (gsi, n1, true, NULL_TREE,
1940 true, GSI_SAME_STMT);
1941 n2 = fold_convert (itype, unshare_expr (fd->loops[i].n2));
1942 n2 = force_gimple_operand_gsi (gsi, n2, true, NULL_TREE,
1943 true, GSI_SAME_STMT);
1944 cond_stmt = expand_omp_build_cond (gsi, fd->loops[i].cond_code,
1945 n1, n2);
1946 e = split_block (entry_bb, cond_stmt);
1947 basic_block &zero_iter_bb
1948 = i < fd->collapse ? zero_iter1_bb : zero_iter2_bb;
1949 int &first_zero_iter
1950 = i < fd->collapse ? first_zero_iter1 : first_zero_iter2;
1951 if (zero_iter_bb == NULL)
1952 {
1953 gassign *assign_stmt;
1954 first_zero_iter = i;
1955 zero_iter_bb = create_empty_bb (entry_bb);
1956 add_bb_to_loop (zero_iter_bb, entry_bb->loop_father);
1957 *gsi = gsi_after_labels (zero_iter_bb);
1958 if (i < fd->collapse)
1959 assign_stmt = gimple_build_assign (fd->loop.n2,
1960 build_zero_cst (type));
1961 else
1962 {
1963 counts[i] = create_tmp_reg (type, ".count");
1964 assign_stmt
1965 = gimple_build_assign (counts[i], build_zero_cst (type));
1966 }
1967 gsi_insert_before (gsi, assign_stmt, GSI_SAME_STMT);
1968 set_immediate_dominator (CDI_DOMINATORS, zero_iter_bb,
1969 entry_bb);
1970 }
1971 ne = make_edge (entry_bb, zero_iter_bb, EDGE_FALSE_VALUE);
1972 ne->probability = profile_probability::very_unlikely ();
1973 e->flags = EDGE_TRUE_VALUE;
1974 e->probability = ne->probability.invert ();
1975 if (l2_dom_bb == NULL)
1976 l2_dom_bb = entry_bb;
1977 entry_bb = e->dest;
1978 *gsi = gsi_last_nondebug_bb (entry_bb);
1979 }
1980
1981 if (POINTER_TYPE_P (itype))
1982 itype = signed_type_for (itype);
1983 t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
1984 ? -1 : 1));
1985 t = fold_build2 (PLUS_EXPR, itype,
1986 fold_convert (itype, fd->loops[i].step), t);
1987 t = fold_build2 (PLUS_EXPR, itype, t,
1988 fold_convert (itype, fd->loops[i].n2));
1989 t = fold_build2 (MINUS_EXPR, itype, t,
1990 fold_convert (itype, fd->loops[i].n1));
1991 /* ?? We could probably use CEIL_DIV_EXPR instead of
1992 TRUNC_DIV_EXPR and adjusting by hand. Unless we can't
1993 generate the same code in the end because generically we
1994 don't know that the values involved must be negative for
1995 GT?? */
1996 if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
1997 t = fold_build2 (TRUNC_DIV_EXPR, itype,
1998 fold_build1 (NEGATE_EXPR, itype, t),
1999 fold_build1 (NEGATE_EXPR, itype,
2000 fold_convert (itype,
2001 fd->loops[i].step)));
2002 else
2003 t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
2004 fold_convert (itype, fd->loops[i].step));
2005 t = fold_convert (type, t);
2006 if (TREE_CODE (t) == INTEGER_CST)
2007 counts[i] = t;
2008 else
2009 {
2010 if (i < fd->collapse || i != first_zero_iter2)
2011 counts[i] = create_tmp_reg (type, ".count");
2012 expand_omp_build_assign (gsi, counts[i], t);
2013 }
2014 if (SSA_VAR_P (fd->loop.n2) && i < fd->collapse)
2015 {
2016 if (fd->non_rect && i >= fd->first_nonrect && i <= fd->last_nonrect)
2017 continue;
2018 if (!rect_count_seen)
2019 {
2020 t = counts[i];
2021 rect_count_seen = true;
2022 }
2023 else
2024 t = fold_build2 (MULT_EXPR, type, fd->loop.n2, counts[i]);
2025 expand_omp_build_assign (gsi, fd->loop.n2, t);
2026 }
2027 }
2028 if (fd->non_rect && SSA_VAR_P (fd->loop.n2))
2029 {
2030 gcc_assert (fd->last_nonrect != -1);
2031
2032 counts[fd->last_nonrect] = create_tmp_reg (type, ".count");
2033 expand_omp_build_assign (gsi, counts[fd->last_nonrect],
2034 build_zero_cst (type));
2035 for (i = fd->first_nonrect + 1; i < fd->last_nonrect; i++)
2036 if (fd->loops[i].m1
2037 || fd->loops[i].m2
2038 || fd->loops[i].non_rect_referenced)
2039 break;
2040 if (i == fd->last_nonrect
2041 && fd->loops[i].outer == fd->last_nonrect - fd->first_nonrect
2042 && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
2043 && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[i].v)))
2044 {
2045 int o = fd->first_nonrect;
2046 tree itype = TREE_TYPE (fd->loops[o].v);
2047 tree n1o = create_tmp_reg (itype, ".n1o");
2048 t = fold_convert (itype, unshare_expr (fd->loops[o].n1));
2049 expand_omp_build_assign (gsi, n1o, t);
2050 tree n2o = create_tmp_reg (itype, ".n2o");
2051 t = fold_convert (itype, unshare_expr (fd->loops[o].n2));
2052 expand_omp_build_assign (gsi, n2o, t);
2053 if (fd->loops[i].m1 && fd->loops[i].m2)
2054 t = fold_build2 (MINUS_EXPR, itype, unshare_expr (fd->loops[i].m2),
2055 unshare_expr (fd->loops[i].m1));
2056 else if (fd->loops[i].m1)
2057 t = fold_build1 (NEGATE_EXPR, itype,
2058 unshare_expr (fd->loops[i].m1));
2059 else
2060 t = unshare_expr (fd->loops[i].m2);
2061 tree m2minusm1
2062 = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
2063 true, GSI_SAME_STMT);
2064
2065 gimple_stmt_iterator gsi2 = *gsi;
2066 gsi_prev (&gsi2);
2067 e = split_block (entry_bb, gsi_stmt (gsi2));
2068 e = split_block (e->dest, (gimple *) NULL);
2069 basic_block bb1 = e->src;
2070 entry_bb = e->dest;
2071 *gsi = gsi_after_labels (entry_bb);
2072
2073 gsi2 = gsi_after_labels (bb1);
2074 tree ostep = fold_convert (itype, fd->loops[o].step);
2075 t = build_int_cst (itype, (fd->loops[o].cond_code
2076 == LT_EXPR ? -1 : 1));
2077 t = fold_build2 (PLUS_EXPR, itype, ostep, t);
2078 t = fold_build2 (PLUS_EXPR, itype, t, n2o);
2079 t = fold_build2 (MINUS_EXPR, itype, t, n1o);
2080 if (TYPE_UNSIGNED (itype)
2081 && fd->loops[o].cond_code == GT_EXPR)
2082 t = fold_build2 (TRUNC_DIV_EXPR, itype,
2083 fold_build1 (NEGATE_EXPR, itype, t),
2084 fold_build1 (NEGATE_EXPR, itype, ostep));
2085 else
2086 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, ostep);
2087 tree outer_niters
2088 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2089 true, GSI_SAME_STMT);
2090 t = fold_build2 (MINUS_EXPR, itype, outer_niters,
2091 build_one_cst (itype));
2092 t = fold_build2 (MULT_EXPR, itype, t, ostep);
2093 t = fold_build2 (PLUS_EXPR, itype, n1o, t);
2094 tree last = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2095 true, GSI_SAME_STMT);
2096 tree n1, n2, n1e, n2e;
2097 t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2098 if (fd->loops[i].m1)
2099 {
2100 n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2101 n1 = fold_build2 (MULT_EXPR, itype, n1o, n1);
2102 n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2103 }
2104 else
2105 n1 = t;
2106 n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2107 true, GSI_SAME_STMT);
2108 t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2109 if (fd->loops[i].m2)
2110 {
2111 n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2112 n2 = fold_build2 (MULT_EXPR, itype, n1o, n2);
2113 n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2114 }
2115 else
2116 n2 = t;
2117 n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2118 true, GSI_SAME_STMT);
2119 t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2120 if (fd->loops[i].m1)
2121 {
2122 n1e = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2123 n1e = fold_build2 (MULT_EXPR, itype, last, n1e);
2124 n1e = fold_build2 (PLUS_EXPR, itype, n1e, t);
2125 }
2126 else
2127 n1e = t;
2128 n1e = force_gimple_operand_gsi (&gsi2, n1e, true, NULL_TREE,
2129 true, GSI_SAME_STMT);
2130 t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2131 if (fd->loops[i].m2)
2132 {
2133 n2e = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2134 n2e = fold_build2 (MULT_EXPR, itype, last, n2e);
2135 n2e = fold_build2 (PLUS_EXPR, itype, n2e, t);
2136 }
2137 else
2138 n2e = t;
2139 n2e = force_gimple_operand_gsi (&gsi2, n2e, true, NULL_TREE,
2140 true, GSI_SAME_STMT);
2141 gcond *cond_stmt
2142 = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2143 n1, n2);
2144 e = split_block (bb1, cond_stmt);
2145 e->flags = EDGE_TRUE_VALUE;
2146 e->probability = profile_probability::likely ().guessed ();
2147 basic_block bb2 = e->dest;
2148 gsi2 = gsi_after_labels (bb2);
2149
2150 cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2151 n1e, n2e);
2152 e = split_block (bb2, cond_stmt);
2153 e->flags = EDGE_TRUE_VALUE;
2154 e->probability = profile_probability::likely ().guessed ();
2155 gsi2 = gsi_after_labels (e->dest);
2156
2157 tree step = fold_convert (itype, fd->loops[i].step);
2158 t = build_int_cst (itype, (fd->loops[i].cond_code
2159 == LT_EXPR ? -1 : 1));
2160 t = fold_build2 (PLUS_EXPR, itype, step, t);
2161 t = fold_build2 (PLUS_EXPR, itype, t, n2);
2162 t = fold_build2 (MINUS_EXPR, itype, t, n1);
2163 if (TYPE_UNSIGNED (itype)
2164 && fd->loops[i].cond_code == GT_EXPR)
2165 t = fold_build2 (TRUNC_DIV_EXPR, itype,
2166 fold_build1 (NEGATE_EXPR, itype, t),
2167 fold_build1 (NEGATE_EXPR, itype, step));
2168 else
2169 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2170 tree first_inner_iterations
2171 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2172 true, GSI_SAME_STMT);
2173 t = fold_build2 (MULT_EXPR, itype, m2minusm1, ostep);
2174 if (TYPE_UNSIGNED (itype)
2175 && fd->loops[i].cond_code == GT_EXPR)
2176 t = fold_build2 (TRUNC_DIV_EXPR, itype,
2177 fold_build1 (NEGATE_EXPR, itype, t),
2178 fold_build1 (NEGATE_EXPR, itype, step));
2179 else
2180 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2181 tree factor
2182 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2183 true, GSI_SAME_STMT);
2184 t = fold_build2 (MINUS_EXPR, itype, outer_niters,
2185 build_one_cst (itype));
2186 t = fold_build2 (MULT_EXPR, itype, t, outer_niters);
2187 t = fold_build2 (RSHIFT_EXPR, itype, t, integer_one_node);
2188 t = fold_build2 (MULT_EXPR, itype, factor, t);
2189 t = fold_build2 (PLUS_EXPR, itype,
2190 fold_build2 (MULT_EXPR, itype, outer_niters,
2191 first_inner_iterations), t);
2192 expand_omp_build_assign (&gsi2, counts[fd->last_nonrect],
2193 fold_convert (type, t));
2194
2195 basic_block bb3 = create_empty_bb (bb1);
2196 add_bb_to_loop (bb3, bb1->loop_father);
2197
2198 e = make_edge (bb1, bb3, EDGE_FALSE_VALUE);
2199 e->probability = profile_probability::unlikely ().guessed ();
2200
2201 gsi2 = gsi_after_labels (bb3);
2202 cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2203 n1e, n2e);
2204 e = split_block (bb3, cond_stmt);
2205 e->flags = EDGE_TRUE_VALUE;
2206 e->probability = profile_probability::likely ().guessed ();
2207 basic_block bb4 = e->dest;
2208
2209 ne = make_edge (bb3, entry_bb, EDGE_FALSE_VALUE);
2210 ne->probability = e->probability.invert ();
2211
2212 basic_block bb5 = create_empty_bb (bb2);
2213 add_bb_to_loop (bb5, bb2->loop_father);
2214
2215 ne = make_edge (bb2, bb5, EDGE_FALSE_VALUE);
2216 ne->probability = profile_probability::unlikely ().guessed ();
2217
2218 for (int j = 0; j < 2; j++)
2219 {
2220 gsi2 = gsi_after_labels (j ? bb5 : bb4);
2221 t = fold_build2 (MINUS_EXPR, itype,
2222 unshare_expr (fd->loops[i].n1),
2223 unshare_expr (fd->loops[i].n2));
2224 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, m2minusm1);
2225 tree tem
2226 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2227 true, GSI_SAME_STMT);
2228 t = fold_build2 (MINUS_EXPR, itype, tem, n1o);
2229 t = fold_build2 (TRUNC_MOD_EXPR, itype, t, ostep);
2230 t = fold_build2 (MINUS_EXPR, itype, tem, t);
2231 tem = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2232 true, GSI_SAME_STMT);
2233 t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2234 if (fd->loops[i].m1)
2235 {
2236 n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2237 n1 = fold_build2 (MULT_EXPR, itype, tem, n1);
2238 n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2239 }
2240 else
2241 n1 = t;
2242 n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2243 true, GSI_SAME_STMT);
2244 t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2245 if (fd->loops[i].m2)
2246 {
2247 n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2248 n2 = fold_build2 (MULT_EXPR, itype, tem, n2);
2249 n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2250 }
2251 else
2252 n2 = t;
2253 n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2254 true, GSI_SAME_STMT);
2255 expand_omp_build_assign (&gsi2, j ? n2o : n1o, tem);
2256
2257 cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2258 n1, n2);
2259 e = split_block (gsi_bb (gsi2), cond_stmt);
2260 e->flags = j ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE;
2261 e->probability = profile_probability::unlikely ().guessed ();
2262 ne = make_edge (e->src, bb1,
2263 j ? EDGE_FALSE_VALUE : EDGE_TRUE_VALUE);
2264 ne->probability = e->probability.invert ();
2265 gsi2 = gsi_after_labels (e->dest);
2266
2267 t = fold_build2 (PLUS_EXPR, itype, tem, ostep);
2268 expand_omp_build_assign (&gsi2, j ? n2o : n1o, t);
2269
2270 make_edge (e->dest, bb1, EDGE_FALLTHRU);
2271 }
2272
2273 set_immediate_dominator (CDI_DOMINATORS, bb3, bb1);
2274 set_immediate_dominator (CDI_DOMINATORS, bb5, bb2);
2275 set_immediate_dominator (CDI_DOMINATORS, entry_bb, bb1);
2276
2277 if (fd->first_nonrect + 1 == fd->last_nonrect)
2278 {
2279 fd->first_inner_iterations = first_inner_iterations;
2280 fd->factor = factor;
2281 fd->adjn1 = n1o;
2282 }
2283 }
2284 else
2285 {
2286 /* Fallback implementation. Evaluate the loops with m1/m2
2287 non-NULL as well as their outer loops at runtime using temporaries
2288 instead of the original iteration variables, and in the
2289 body just bump the counter. */
2290 gimple_stmt_iterator gsi2 = *gsi;
2291 gsi_prev (&gsi2);
2292 e = split_block (entry_bb, gsi_stmt (gsi2));
2293 e = split_block (e->dest, (gimple *) NULL);
2294 basic_block cur_bb = e->src;
2295 basic_block next_bb = e->dest;
2296 entry_bb = e->dest;
2297 *gsi = gsi_after_labels (entry_bb);
2298
2299 tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
2300 memset (vs, 0, fd->last_nonrect * sizeof (tree));
2301
2302 for (i = 0; i <= fd->last_nonrect; i++)
2303 {
2304 if (fd->loops[i].m1 == NULL_TREE
2305 && fd->loops[i].m2 == NULL_TREE
2306 && !fd->loops[i].non_rect_referenced)
2307 continue;
2308
2309 tree itype = TREE_TYPE (fd->loops[i].v);
2310
2311 gsi2 = gsi_after_labels (cur_bb);
2312 tree n1, n2;
2313 t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
2314 if (fd->loops[i].m1 == NULL_TREE)
2315 n1 = t;
2316 else if (POINTER_TYPE_P (itype))
2317 {
2318 gcc_assert (integer_onep (fd->loops[i].m1));
2319 t = unshare_expr (fd->loops[i].n1);
2320 n1 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
2321 }
2322 else
2323 {
2324 n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
2325 n1 = fold_build2 (MULT_EXPR, itype,
2326 vs[i - fd->loops[i].outer], n1);
2327 n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2328 }
2329 n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2330 true, GSI_SAME_STMT);
2331 if (i < fd->last_nonrect)
2332 {
2333 vs[i] = create_tmp_reg (itype, ".it");
2334 expand_omp_build_assign (&gsi2, vs[i], n1);
2335 }
2336 t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
2337 if (fd->loops[i].m2 == NULL_TREE)
2338 n2 = t;
2339 else if (POINTER_TYPE_P (itype))
2340 {
2341 gcc_assert (integer_onep (fd->loops[i].m2));
2342 t = unshare_expr (fd->loops[i].n2);
2343 n2 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
2344 }
2345 else
2346 {
2347 n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
2348 n2 = fold_build2 (MULT_EXPR, itype,
2349 vs[i - fd->loops[i].outer], n2);
2350 n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2351 }
2352 n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2353 true, GSI_SAME_STMT);
2354 if (POINTER_TYPE_P (itype))
2355 itype = signed_type_for (itype);
2356 if (i == fd->last_nonrect)
2357 {
2358 gcond *cond_stmt
2359 = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2360 n1, n2);
2361 e = split_block (cur_bb, cond_stmt);
2362 e->flags = EDGE_TRUE_VALUE;
2363 ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
2364 e->probability = profile_probability::likely ().guessed ();
2365 ne->probability = e->probability.invert ();
2366 gsi2 = gsi_after_labels (e->dest);
2367
2368 t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
2369 ? -1 : 1));
2370 t = fold_build2 (PLUS_EXPR, itype,
2371 fold_convert (itype, fd->loops[i].step), t);
2372 t = fold_build2 (PLUS_EXPR, itype, t,
2373 fold_convert (itype, n2));
2374 t = fold_build2 (MINUS_EXPR, itype, t,
2375 fold_convert (itype, n1));
2376 tree step = fold_convert (itype, fd->loops[i].step);
2377 if (TYPE_UNSIGNED (itype)
2378 && fd->loops[i].cond_code == GT_EXPR)
2379 t = fold_build2 (TRUNC_DIV_EXPR, itype,
2380 fold_build1 (NEGATE_EXPR, itype, t),
2381 fold_build1 (NEGATE_EXPR, itype, step));
2382 else
2383 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2384 t = fold_convert (type, t);
2385 t = fold_build2 (PLUS_EXPR, type,
2386 counts[fd->last_nonrect], t);
2387 t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2388 true, GSI_SAME_STMT);
2389 expand_omp_build_assign (&gsi2, counts[fd->last_nonrect], t);
2390 e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
2391 set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
2392 break;
2393 }
2394 e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
2395
2396 basic_block new_cur_bb = create_empty_bb (cur_bb);
2397 add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
2398
2399 gsi2 = gsi_after_labels (e->dest);
2400 tree step = fold_convert (itype,
2401 unshare_expr (fd->loops[i].step));
2402 if (POINTER_TYPE_P (TREE_TYPE (vs[i])))
2403 t = fold_build_pointer_plus (vs[i], step);
2404 else
2405 t = fold_build2 (PLUS_EXPR, itype, vs[i], step);
2406 t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2407 true, GSI_SAME_STMT);
2408 expand_omp_build_assign (&gsi2, vs[i], t);
2409
2410 ne = split_block (e->dest, last_nondebug_stmt (e->dest));
2411 gsi2 = gsi_after_labels (ne->dest);
2412
2413 expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, vs[i], n2);
2414 edge e3, e4;
2415 if (next_bb == entry_bb)
2416 {
2417 e3 = find_edge (ne->dest, next_bb);
2418 e3->flags = EDGE_FALSE_VALUE;
2419 }
2420 else
2421 e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
2422 e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
2423 e4->probability = profile_probability::likely ().guessed ();
2424 e3->probability = e4->probability.invert ();
2425 basic_block esrc = e->src;
2426 make_edge (e->src, ne->dest, EDGE_FALLTHRU);
2427 cur_bb = new_cur_bb;
2428 basic_block latch_bb = next_bb;
2429 next_bb = e->dest;
2430 remove_edge (e);
2431 set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
2432 set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
2433 set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
2434 }
2435 }
2436 t = NULL_TREE;
2437 for (i = fd->first_nonrect; i < fd->last_nonrect; i++)
2438 if (!fd->loops[i].non_rect_referenced
2439 && fd->loops[i].m1 == NULL_TREE
2440 && fd->loops[i].m2 == NULL_TREE)
2441 {
2442 if (t == NULL_TREE)
2443 t = counts[i];
2444 else
2445 t = fold_build2 (MULT_EXPR, type, t, counts[i]);
2446 }
2447 if (t)
2448 {
2449 t = fold_build2 (MULT_EXPR, type, counts[fd->last_nonrect], t);
2450 expand_omp_build_assign (gsi, counts[fd->last_nonrect], t);
2451 }
2452 if (!rect_count_seen)
2453 t = counts[fd->last_nonrect];
2454 else
2455 t = fold_build2 (MULT_EXPR, type, fd->loop.n2,
2456 counts[fd->last_nonrect]);
2457 expand_omp_build_assign (gsi, fd->loop.n2, t);
2458 }
2459 else if (fd->non_rect)
2460 {
2461 tree t = fd->loop.n2;
2462 gcc_assert (TREE_CODE (t) == INTEGER_CST);
2463 int non_rect_referenced = 0, non_rect = 0;
2464 for (i = 0; i < fd->collapse; i++)
2465 {
2466 if ((i < fd->first_nonrect || i > fd->last_nonrect)
2467 && !integer_zerop (counts[i]))
2468 t = fold_build2 (TRUNC_DIV_EXPR, type, t, counts[i]);
2469 if (fd->loops[i].non_rect_referenced)
2470 non_rect_referenced++;
2471 if (fd->loops[i].m1 || fd->loops[i].m2)
2472 non_rect++;
2473 }
2474 gcc_assert (non_rect == 1 && non_rect_referenced == 1);
2475 counts[fd->last_nonrect] = t;
2476 }
2477 }
2478
2479 /* Helper function for expand_omp_{for_*,simd}. Generate code like:
2480 T = V;
2481 V3 = N31 + (T % count3) * STEP3;
2482 T = T / count3;
2483 V2 = N21 + (T % count2) * STEP2;
2484 T = T / count2;
2485 V1 = N11 + T * STEP1;
2486 if this loop doesn't have an inner loop construct combined with it.
2487 If it does have an inner loop construct combined with it and the
2488 iteration count isn't known constant, store values from counts array
2489 into its _looptemp_ temporaries instead.
2490 For non-rectangular loops (between fd->first_nonrect and fd->last_nonrect
2491 inclusive), use the count of all those loops together, and either
2492 find quadratic etc. equation roots, or as a fallback, do:
2493 COUNT = 0;
2494 for (tmpi = N11; tmpi COND1 N12; tmpi += STEP1)
2495 for (tmpj = M21 * tmpi + N21;
2496 tmpj COND2 M22 * tmpi + N22; tmpj += STEP2)
2497 {
2498 int tmpk1 = M31 * tmpj + N31;
2499 int tmpk2 = M32 * tmpj + N32;
2500 if (tmpk1 COND3 tmpk2)
2501 {
2502 if (COND3 is <)
2503 adj = STEP3 - 1;
2504 else
2505 adj = STEP3 + 1;
2506 int temp = (adj + tmpk2 - tmpk1) / STEP3;
2507 if (COUNT + temp > T)
2508 {
2509 V1 = tmpi;
2510 V2 = tmpj;
2511 V3 = tmpk1 + (T - COUNT) * STEP3;
2512 goto done;
2513 }
2514 else
2515 COUNT += temp;
2516 }
2517 }
2518 done:;
2519 but for optional innermost or outermost rectangular loops that aren't
2520 referenced by other loop expressions keep doing the division/modulo. */
2521
2522 static void
2523 expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
2524 tree *counts, tree *nonrect_bounds,
2525 gimple *inner_stmt, tree startvar)
2526 {
2527 int i;
2528 if (gimple_omp_for_combined_p (fd->for_stmt))
2529 {
2530 /* If fd->loop.n2 is constant, then no propagation of the counts
2531 is needed, they are constant. */
2532 if (TREE_CODE (fd->loop.n2) == INTEGER_CST)
2533 return;
2534
2535 tree clauses = gimple_code (inner_stmt) != GIMPLE_OMP_FOR
2536 ? gimple_omp_taskreg_clauses (inner_stmt)
2537 : gimple_omp_for_clauses (inner_stmt);
2538 /* First two _looptemp_ clauses are for istart/iend, counts[0]
2539 isn't supposed to be handled, as the inner loop doesn't
2540 use it. */
2541 tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
2542 gcc_assert (innerc);
2543 int count = 0;
2544 if (fd->non_rect
2545 && fd->last_nonrect == fd->first_nonrect + 1
2546 && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
2547 count = 4;
2548 for (i = 0; i < fd->collapse + count; i++)
2549 {
2550 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
2551 OMP_CLAUSE__LOOPTEMP_);
2552 gcc_assert (innerc);
2553 if (i)
2554 {
2555 tree tem = OMP_CLAUSE_DECL (innerc);
2556 tree t;
2557 if (i < fd->collapse)
2558 t = counts[i];
2559 else
2560 switch (i - fd->collapse)
2561 {
2562 case 0: t = counts[0]; break;
2563 case 1: t = fd->first_inner_iterations; break;
2564 case 2: t = fd->factor; break;
2565 case 3: t = fd->adjn1; break;
2566 default: gcc_unreachable ();
2567 }
2568 t = fold_convert (TREE_TYPE (tem), t);
2569 t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
2570 false, GSI_CONTINUE_LINKING);
2571 gassign *stmt = gimple_build_assign (tem, t);
2572 gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
2573 }
2574 }
2575 return;
2576 }
2577
2578 tree type = TREE_TYPE (fd->loop.v);
2579 tree tem = create_tmp_reg (type, ".tem");
2580 gassign *stmt = gimple_build_assign (tem, startvar);
2581 gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
2582
2583 for (i = fd->collapse - 1; i >= 0; i--)
2584 {
2585 tree vtype = TREE_TYPE (fd->loops[i].v), itype, t;
2586 itype = vtype;
2587 if (POINTER_TYPE_P (vtype))
2588 itype = signed_type_for (vtype);
2589 if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
2590 t = fold_build2 (TRUNC_MOD_EXPR, type, tem, counts[i]);
2591 else
2592 t = tem;
2593 if (i == fd->last_nonrect)
2594 {
2595 t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
2596 false, GSI_CONTINUE_LINKING);
2597 tree stopval = t;
2598 tree idx = create_tmp_reg (type, ".count");
2599 expand_omp_build_assign (gsi, idx,
2600 build_zero_cst (type), true);
2601 basic_block bb_triang = NULL, bb_triang_dom = NULL;
2602 if (fd->first_nonrect + 1 == fd->last_nonrect
2603 && (TREE_CODE (fd->loop.n2) == INTEGER_CST
2604 || fd->first_inner_iterations)
2605 && (optab_handler (sqrt_optab, TYPE_MODE (double_type_node))
2606 != CODE_FOR_nothing)
2607 && !integer_zerop (fd->loop.n2))
2608 {
2609 tree outer_n1 = fd->adjn1 ? fd->adjn1 : fd->loops[i - 1].n1;
2610 tree itype = TREE_TYPE (fd->loops[i].v);
2611 tree first_inner_iterations = fd->first_inner_iterations;
2612 tree factor = fd->factor;
2613 gcond *cond_stmt
2614 = expand_omp_build_cond (gsi, NE_EXPR, factor,
2615 build_zero_cst (TREE_TYPE (factor)),
2616 true);
2617 edge e = split_block (gsi_bb (*gsi), cond_stmt);
2618 basic_block bb0 = e->src;
2619 e->flags = EDGE_TRUE_VALUE;
2620 e->probability = profile_probability::likely ();
2621 bb_triang_dom = bb0;
2622 *gsi = gsi_after_labels (e->dest);
2623 tree slltype = long_long_integer_type_node;
2624 tree ulltype = long_long_unsigned_type_node;
2625 tree stopvalull = fold_convert (ulltype, stopval);
2626 stopvalull
2627 = force_gimple_operand_gsi (gsi, stopvalull, true, NULL_TREE,
2628 false, GSI_CONTINUE_LINKING);
2629 first_inner_iterations
2630 = fold_convert (slltype, first_inner_iterations);
2631 first_inner_iterations
2632 = force_gimple_operand_gsi (gsi, first_inner_iterations, true,
2633 NULL_TREE, false,
2634 GSI_CONTINUE_LINKING);
2635 factor = fold_convert (slltype, factor);
2636 factor
2637 = force_gimple_operand_gsi (gsi, factor, true, NULL_TREE,
2638 false, GSI_CONTINUE_LINKING);
2639 tree first_inner_iterationsd
2640 = fold_build1 (FLOAT_EXPR, double_type_node,
2641 first_inner_iterations);
2642 first_inner_iterationsd
2643 = force_gimple_operand_gsi (gsi, first_inner_iterationsd, true,
2644 NULL_TREE, false,
2645 GSI_CONTINUE_LINKING);
2646 tree factord = fold_build1 (FLOAT_EXPR, double_type_node,
2647 factor);
2648 factord = force_gimple_operand_gsi (gsi, factord, true,
2649 NULL_TREE, false,
2650 GSI_CONTINUE_LINKING);
2651 tree stopvald = fold_build1 (FLOAT_EXPR, double_type_node,
2652 stopvalull);
2653 stopvald = force_gimple_operand_gsi (gsi, stopvald, true,
2654 NULL_TREE, false,
2655 GSI_CONTINUE_LINKING);
2656 /* Temporarily disable flag_rounding_math, values will be
2657 decimal numbers divided by 2 and worst case imprecisions
2658 due to too large values ought to be caught later by the
2659 checks for fallback. */
2660 int save_flag_rounding_math = flag_rounding_math;
2661 flag_rounding_math = 0;
2662 t = fold_build2 (RDIV_EXPR, double_type_node, factord,
2663 build_real (double_type_node, dconst2));
2664 tree t3 = fold_build2 (MINUS_EXPR, double_type_node,
2665 first_inner_iterationsd, t);
2666 t3 = force_gimple_operand_gsi (gsi, t3, true, NULL_TREE, false,
2667 GSI_CONTINUE_LINKING);
2668 t = fold_build2 (MULT_EXPR, double_type_node, factord,
2669 build_real (double_type_node, dconst2));
2670 t = fold_build2 (MULT_EXPR, double_type_node, t, stopvald);
2671 t = fold_build2 (PLUS_EXPR, double_type_node, t,
2672 fold_build2 (MULT_EXPR, double_type_node,
2673 t3, t3));
2674 flag_rounding_math = save_flag_rounding_math;
2675 t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2676 GSI_CONTINUE_LINKING);
2677 if (flag_exceptions
2678 && cfun->can_throw_non_call_exceptions
2679 && operation_could_trap_p (LT_EXPR, true, false, NULL_TREE))
2680 {
2681 tree tem = fold_build2 (LT_EXPR, boolean_type_node, t,
2682 build_zero_cst (double_type_node));
2683 tem = force_gimple_operand_gsi (gsi, tem, true, NULL_TREE,
2684 false, GSI_CONTINUE_LINKING);
2685 cond_stmt = gimple_build_cond (NE_EXPR, tem,
2686 boolean_false_node,
2687 NULL_TREE, NULL_TREE);
2688 }
2689 else
2690 cond_stmt
2691 = gimple_build_cond (LT_EXPR, t,
2692 build_zero_cst (double_type_node),
2693 NULL_TREE, NULL_TREE);
2694 gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2695 e = split_block (gsi_bb (*gsi), cond_stmt);
2696 basic_block bb1 = e->src;
2697 e->flags = EDGE_FALSE_VALUE;
2698 e->probability = profile_probability::very_likely ();
2699 *gsi = gsi_after_labels (e->dest);
2700 gcall *call = gimple_build_call_internal (IFN_SQRT, 1, t);
2701 tree sqrtr = create_tmp_var (double_type_node);
2702 gimple_call_set_lhs (call, sqrtr);
2703 gsi_insert_after (gsi, call, GSI_CONTINUE_LINKING);
2704 t = fold_build2 (MINUS_EXPR, double_type_node, sqrtr, t3);
2705 t = fold_build2 (RDIV_EXPR, double_type_node, t, factord);
2706 t = fold_build1 (FIX_TRUNC_EXPR, ulltype, t);
2707 tree c = create_tmp_var (ulltype);
2708 tree d = create_tmp_var (ulltype);
2709 expand_omp_build_assign (gsi, c, t, true);
2710 t = fold_build2 (MINUS_EXPR, ulltype, c,
2711 build_one_cst (ulltype));
2712 t = fold_build2 (MULT_EXPR, ulltype, c, t);
2713 t = fold_build2 (RSHIFT_EXPR, ulltype, t, integer_one_node);
2714 t = fold_build2 (MULT_EXPR, ulltype,
2715 fold_convert (ulltype, fd->factor), t);
2716 tree t2
2717 = fold_build2 (MULT_EXPR, ulltype, c,
2718 fold_convert (ulltype,
2719 fd->first_inner_iterations));
2720 t = fold_build2 (PLUS_EXPR, ulltype, t, t2);
2721 expand_omp_build_assign (gsi, d, t, true);
2722 t = fold_build2 (MULT_EXPR, ulltype,
2723 fold_convert (ulltype, fd->factor), c);
2724 t = fold_build2 (PLUS_EXPR, ulltype,
2725 t, fold_convert (ulltype,
2726 fd->first_inner_iterations));
2727 t2 = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2728 GSI_CONTINUE_LINKING);
2729 cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, d,
2730 NULL_TREE, NULL_TREE);
2731 gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2732 e = split_block (gsi_bb (*gsi), cond_stmt);
2733 basic_block bb2 = e->src;
2734 e->flags = EDGE_TRUE_VALUE;
2735 e->probability = profile_probability::very_likely ();
2736 *gsi = gsi_after_labels (e->dest);
2737 t = fold_build2 (PLUS_EXPR, ulltype, d, t2);
2738 t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2739 GSI_CONTINUE_LINKING);
2740 cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, t,
2741 NULL_TREE, NULL_TREE);
2742 gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
2743 e = split_block (gsi_bb (*gsi), cond_stmt);
2744 basic_block bb3 = e->src;
2745 e->flags = EDGE_FALSE_VALUE;
2746 e->probability = profile_probability::very_likely ();
2747 *gsi = gsi_after_labels (e->dest);
2748 t = fold_convert (itype, c);
2749 t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i - 1].step);
2750 t = fold_build2 (PLUS_EXPR, itype, outer_n1, t);
2751 t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
2752 GSI_CONTINUE_LINKING);
2753 expand_omp_build_assign (gsi, fd->loops[i - 1].v, t, true);
2754 t2 = fold_build2 (MINUS_EXPR, ulltype, stopvalull, d);
2755 t2 = fold_convert (itype, t2);
2756 t2 = fold_build2 (MULT_EXPR, itype, t2, fd->loops[i].step);
2757 t2 = fold_build2 (PLUS_EXPR, itype, t2, fd->loops[i].n1);
2758 if (fd->loops[i].m1)
2759 {
2760 t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i].m1);
2761 t2 = fold_build2 (PLUS_EXPR, itype, t2, t);
2762 }
2763 expand_omp_build_assign (gsi, fd->loops[i].v, t2, true);
2764 e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
2765 bb_triang = e->src;
2766 *gsi = gsi_after_labels (e->dest);
2767 remove_edge (e);
2768 e = make_edge (bb1, gsi_bb (*gsi), EDGE_TRUE_VALUE);
2769 e->probability = profile_probability::very_unlikely ();
2770 e = make_edge (bb2, gsi_bb (*gsi), EDGE_FALSE_VALUE);
2771 e->probability = profile_probability::very_unlikely ();
2772 e = make_edge (bb3, gsi_bb (*gsi), EDGE_TRUE_VALUE);
2773 e->probability = profile_probability::very_unlikely ();
2774
2775 basic_block bb4 = create_empty_bb (bb0);
2776 add_bb_to_loop (bb4, bb0->loop_father);
2777 e = make_edge (bb0, bb4, EDGE_FALSE_VALUE);
2778 e->probability = profile_probability::unlikely ();
2779 make_edge (bb4, gsi_bb (*gsi), EDGE_FALLTHRU);
2780 set_immediate_dominator (CDI_DOMINATORS, bb4, bb0);
2781 set_immediate_dominator (CDI_DOMINATORS, gsi_bb (*gsi), bb0);
2782 gimple_stmt_iterator gsi2 = gsi_after_labels (bb4);
2783 t2 = fold_build2 (TRUNC_DIV_EXPR, type,
2784 counts[i], counts[i - 1]);
2785 t2 = force_gimple_operand_gsi (&gsi2, t2, true, NULL_TREE, false,
2786 GSI_CONTINUE_LINKING);
2787 t = fold_build2 (TRUNC_MOD_EXPR, type, stopval, t2);
2788 t2 = fold_build2 (TRUNC_DIV_EXPR, type, stopval, t2);
2789 t = fold_convert (itype, t);
2790 t2 = fold_convert (itype, t2);
2791 t = fold_build2 (MULT_EXPR, itype, t,
2792 fold_convert (itype, fd->loops[i].step));
2793 t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
2794 t2 = fold_build2 (MULT_EXPR, itype, t2,
2795 fold_convert (itype, fd->loops[i - 1].step));
2796 t2 = fold_build2 (PLUS_EXPR, itype, fd->loops[i - 1].n1, t2);
2797 t2 = force_gimple_operand_gsi (&gsi2, t2, false, NULL_TREE,
2798 false, GSI_CONTINUE_LINKING);
2799 stmt = gimple_build_assign (fd->loops[i - 1].v, t2);
2800 gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
2801 if (fd->loops[i].m1)
2802 {
2803 t2 = fold_build2 (MULT_EXPR, itype, fd->loops[i].m1,
2804 fd->loops[i - 1].v);
2805 t = fold_build2 (PLUS_EXPR, itype, t, t2);
2806 }
2807 t = force_gimple_operand_gsi (&gsi2, t, false, NULL_TREE,
2808 false, GSI_CONTINUE_LINKING);
2809 stmt = gimple_build_assign (fd->loops[i].v, t);
2810 gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
2811 }
2812 /* Fallback implementation. Evaluate the loops in between
2813 (inclusive) fd->first_nonrect and fd->last_nonrect at
2814 runtime unsing temporaries instead of the original iteration
2815 variables, in the body just bump the counter and compare
2816 with the desired value. */
2817 gimple_stmt_iterator gsi2 = *gsi;
2818 basic_block entry_bb = gsi_bb (gsi2);
2819 edge e = split_block (entry_bb, gsi_stmt (gsi2));
2820 e = split_block (e->dest, (gimple *) NULL);
2821 basic_block dom_bb = NULL;
2822 basic_block cur_bb = e->src;
2823 basic_block next_bb = e->dest;
2824 entry_bb = e->dest;
2825 *gsi = gsi_after_labels (entry_bb);
2826
2827 tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
2828 tree n1 = NULL_TREE, n2 = NULL_TREE;
2829 memset (vs, 0, fd->last_nonrect * sizeof (tree));
2830
2831 for (int j = fd->first_nonrect; j <= fd->last_nonrect; j++)
2832 {
2833 tree itype = TREE_TYPE (fd->loops[j].v);
2834 bool rect_p = (fd->loops[j].m1 == NULL_TREE
2835 && fd->loops[j].m2 == NULL_TREE
2836 && !fd->loops[j].non_rect_referenced);
2837 gsi2 = gsi_after_labels (cur_bb);
2838 t = fold_convert (itype, unshare_expr (fd->loops[j].n1));
2839 if (fd->loops[j].m1 == NULL_TREE)
2840 n1 = rect_p ? build_zero_cst (type) : t;
2841 else if (POINTER_TYPE_P (itype))
2842 {
2843 gcc_assert (integer_onep (fd->loops[j].m1));
2844 t = unshare_expr (fd->loops[j].n1);
2845 n1 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
2846 }
2847 else
2848 {
2849 n1 = fold_convert (itype, unshare_expr (fd->loops[j].m1));
2850 n1 = fold_build2 (MULT_EXPR, itype,
2851 vs[j - fd->loops[j].outer], n1);
2852 n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
2853 }
2854 n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
2855 true, GSI_SAME_STMT);
2856 if (j < fd->last_nonrect)
2857 {
2858 vs[j] = create_tmp_reg (rect_p ? type : itype, ".it");
2859 expand_omp_build_assign (&gsi2, vs[j], n1);
2860 }
2861 t = fold_convert (itype, unshare_expr (fd->loops[j].n2));
2862 if (fd->loops[j].m2 == NULL_TREE)
2863 n2 = rect_p ? counts[j] : t;
2864 else if (POINTER_TYPE_P (itype))
2865 {
2866 gcc_assert (integer_onep (fd->loops[j].m2));
2867 t = unshare_expr (fd->loops[j].n2);
2868 n2 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
2869 }
2870 else
2871 {
2872 n2 = fold_convert (itype, unshare_expr (fd->loops[j].m2));
2873 n2 = fold_build2 (MULT_EXPR, itype,
2874 vs[j - fd->loops[j].outer], n2);
2875 n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
2876 }
2877 n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
2878 true, GSI_SAME_STMT);
2879 if (POINTER_TYPE_P (itype))
2880 itype = signed_type_for (itype);
2881 if (j == fd->last_nonrect)
2882 {
2883 gcond *cond_stmt
2884 = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
2885 n1, n2);
2886 e = split_block (cur_bb, cond_stmt);
2887 e->flags = EDGE_TRUE_VALUE;
2888 edge ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
2889 e->probability = profile_probability::likely ().guessed ();
2890 ne->probability = e->probability.invert ();
2891 gsi2 = gsi_after_labels (e->dest);
2892
2893 t = build_int_cst (itype, (fd->loops[j].cond_code == LT_EXPR
2894 ? -1 : 1));
2895 t = fold_build2 (PLUS_EXPR, itype,
2896 fold_convert (itype, fd->loops[j].step), t);
2897 t = fold_build2 (PLUS_EXPR, itype, t,
2898 fold_convert (itype, n2));
2899 t = fold_build2 (MINUS_EXPR, itype, t,
2900 fold_convert (itype, n1));
2901 tree step = fold_convert (itype, fd->loops[j].step);
2902 if (TYPE_UNSIGNED (itype)
2903 && fd->loops[j].cond_code == GT_EXPR)
2904 t = fold_build2 (TRUNC_DIV_EXPR, itype,
2905 fold_build1 (NEGATE_EXPR, itype, t),
2906 fold_build1 (NEGATE_EXPR, itype, step));
2907 else
2908 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
2909 t = fold_convert (type, t);
2910 t = fold_build2 (PLUS_EXPR, type, idx, t);
2911 t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2912 true, GSI_SAME_STMT);
2913 e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
2914 set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
2915 cond_stmt
2916 = gimple_build_cond (LE_EXPR, t, stopval, NULL_TREE,
2917 NULL_TREE);
2918 gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
2919 e = split_block (gsi_bb (gsi2), cond_stmt);
2920 e->flags = EDGE_TRUE_VALUE;
2921 e->probability = profile_probability::likely ().guessed ();
2922 ne = make_edge (e->src, entry_bb, EDGE_FALSE_VALUE);
2923 ne->probability = e->probability.invert ();
2924 gsi2 = gsi_after_labels (e->dest);
2925 expand_omp_build_assign (&gsi2, idx, t);
2926 set_immediate_dominator (CDI_DOMINATORS, entry_bb, dom_bb);
2927 break;
2928 }
2929 e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
2930
2931 basic_block new_cur_bb = create_empty_bb (cur_bb);
2932 add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
2933
2934 gsi2 = gsi_after_labels (e->dest);
2935 if (rect_p)
2936 t = fold_build2 (PLUS_EXPR, type, vs[j],
2937 build_one_cst (type));
2938 else
2939 {
2940 tree step
2941 = fold_convert (itype, unshare_expr (fd->loops[j].step));
2942 if (POINTER_TYPE_P (vtype))
2943 t = fold_build_pointer_plus (vs[j], step);
2944 else
2945 t = fold_build2 (PLUS_EXPR, itype, vs[j], step);
2946 }
2947 t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
2948 true, GSI_SAME_STMT);
2949 expand_omp_build_assign (&gsi2, vs[j], t);
2950
2951 edge ne = split_block (e->dest, last_nondebug_stmt (e->dest));
2952 gsi2 = gsi_after_labels (ne->dest);
2953
2954 gcond *cond_stmt;
2955 if (next_bb == entry_bb)
2956 /* No need to actually check the outermost condition. */
2957 cond_stmt
2958 = gimple_build_cond (EQ_EXPR, boolean_true_node,
2959 boolean_true_node,
2960 NULL_TREE, NULL_TREE);
2961 else
2962 cond_stmt
2963 = gimple_build_cond (rect_p ? LT_EXPR
2964 : fd->loops[j].cond_code,
2965 vs[j], n2, NULL_TREE, NULL_TREE);
2966 gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
2967 edge e3, e4;
2968 if (next_bb == entry_bb)
2969 {
2970 e3 = find_edge (ne->dest, next_bb);
2971 e3->flags = EDGE_FALSE_VALUE;
2972 dom_bb = ne->dest;
2973 }
2974 else
2975 e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
2976 e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
2977 e4->probability = profile_probability::likely ().guessed ();
2978 e3->probability = e4->probability.invert ();
2979 basic_block esrc = e->src;
2980 make_edge (e->src, ne->dest, EDGE_FALLTHRU);
2981 cur_bb = new_cur_bb;
2982 basic_block latch_bb = next_bb;
2983 next_bb = e->dest;
2984 remove_edge (e);
2985 set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
2986 set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
2987 set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
2988 }
2989 for (int j = fd->last_nonrect; j >= fd->first_nonrect; j--)
2990 {
2991 tree vtype = TREE_TYPE (fd->loops[j].v);
2992 tree itype = vtype;
2993 if (POINTER_TYPE_P (itype))
2994 itype = signed_type_for (itype);
2995 bool rect_p = (fd->loops[j].m1 == NULL_TREE
2996 && fd->loops[j].m2 == NULL_TREE
2997 && !fd->loops[j].non_rect_referenced);
2998 if (j == fd->last_nonrect)
2999 {
3000 t = fold_build2 (MINUS_EXPR, type, stopval, idx);
3001 t = fold_convert (itype, t);
3002 tree t2
3003 = fold_convert (itype, unshare_expr (fd->loops[j].step));
3004 t = fold_build2 (MULT_EXPR, itype, t, t2);
3005 if (POINTER_TYPE_P (vtype))
3006 t = fold_build_pointer_plus (n1, t);
3007 else
3008 t = fold_build2 (PLUS_EXPR, itype, n1, t);
3009 }
3010 else if (rect_p)
3011 {
3012 t = fold_convert (itype, vs[j]);
3013 t = fold_build2 (MULT_EXPR, itype, t,
3014 fold_convert (itype, fd->loops[j].step));
3015 if (POINTER_TYPE_P (vtype))
3016 t = fold_build_pointer_plus (fd->loops[j].n1, t);
3017 else
3018 t = fold_build2 (PLUS_EXPR, itype, fd->loops[j].n1, t);
3019 }
3020 else
3021 t = vs[j];
3022 t = force_gimple_operand_gsi (gsi, t, false,
3023 NULL_TREE, true,
3024 GSI_SAME_STMT);
3025 stmt = gimple_build_assign (fd->loops[j].v, t);
3026 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3027 }
3028 if (gsi_end_p (*gsi))
3029 *gsi = gsi_last_bb (gsi_bb (*gsi));
3030 else
3031 gsi_prev (gsi);
3032 if (bb_triang)
3033 {
3034 e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
3035 make_edge (bb_triang, e->dest, EDGE_FALLTHRU);
3036 *gsi = gsi_after_labels (e->dest);
3037 if (!gsi_end_p (*gsi))
3038 gsi_insert_before (gsi, gimple_build_nop (), GSI_NEW_STMT);
3039 set_immediate_dominator (CDI_DOMINATORS, e->dest, bb_triang_dom);
3040 }
3041 }
3042 else
3043 {
3044 t = fold_convert (itype, t);
3045 t = fold_build2 (MULT_EXPR, itype, t,
3046 fold_convert (itype, fd->loops[i].step));
3047 if (POINTER_TYPE_P (vtype))
3048 t = fold_build_pointer_plus (fd->loops[i].n1, t);
3049 else
3050 t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
3051 t = force_gimple_operand_gsi (gsi, t,
3052 DECL_P (fd->loops[i].v)
3053 && TREE_ADDRESSABLE (fd->loops[i].v),
3054 NULL_TREE, false,
3055 GSI_CONTINUE_LINKING);
3056 stmt = gimple_build_assign (fd->loops[i].v, t);
3057 gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3058 }
3059 if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
3060 {
3061 t = fold_build2 (TRUNC_DIV_EXPR, type, tem, counts[i]);
3062 t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
3063 false, GSI_CONTINUE_LINKING);
3064 stmt = gimple_build_assign (tem, t);
3065 gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3066 }
3067 if (i == fd->last_nonrect)
3068 i = fd->first_nonrect;
3069 }
3070 if (fd->non_rect)
3071 for (i = 0; i <= fd->last_nonrect; i++)
3072 if (fd->loops[i].m2)
3073 {
3074 tree itype = TREE_TYPE (fd->loops[i].v);
3075
3076 tree t;
3077 if (POINTER_TYPE_P (itype))
3078 {
3079 gcc_assert (integer_onep (fd->loops[i].m2));
3080 t = fold_build_pointer_plus (fd->loops[i - fd->loops[i].outer].v,
3081 unshare_expr (fd->loops[i].n2));
3082 }
3083 else
3084 {
3085 t = fold_convert (itype, unshare_expr (fd->loops[i].m2));
3086 t = fold_build2 (MULT_EXPR, itype,
3087 fd->loops[i - fd->loops[i].outer].v, t);
3088 t = fold_build2 (PLUS_EXPR, itype, t,
3089 fold_convert (itype,
3090 unshare_expr (fd->loops[i].n2)));
3091 }
3092 nonrect_bounds[i] = create_tmp_reg (itype, ".bound");
3093 t = force_gimple_operand_gsi (gsi, t, false,
3094 NULL_TREE, false,
3095 GSI_CONTINUE_LINKING);
3096 stmt = gimple_build_assign (nonrect_bounds[i], t);
3097 gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
3098 }
3099 }
3100
3101 /* Helper function for expand_omp_for_*. Generate code like:
3102 L10:
3103 V3 += STEP3;
3104 if (V3 cond3 N32) goto BODY_BB; else goto L11;
3105 L11:
3106 V3 = N31;
3107 V2 += STEP2;
3108 if (V2 cond2 N22) goto BODY_BB; else goto L12;
3109 L12:
3110 V2 = N21;
3111 V1 += STEP1;
3112 goto BODY_BB;
3113 For non-rectangular loops, use temporaries stored in nonrect_bounds
3114 for the upper bounds if M?2 multiplier is present. Given e.g.
3115 for (V1 = N11; V1 cond1 N12; V1 += STEP1)
3116 for (V2 = N21; V2 cond2 N22; V2 += STEP2)
3117 for (V3 = N31; V3 cond3 N32; V3 += STEP3)
3118 for (V4 = N41 + M41 * V2; V4 cond4 N42 + M42 * V2; V4 += STEP4)
3119 do:
3120 L10:
3121 V4 += STEP4;
3122 if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L11;
3123 L11:
3124 V4 = N41 + M41 * V2; // This can be left out if the loop
3125 // refers to the immediate parent loop
3126 V3 += STEP3;
3127 if (V3 cond3 N32) goto BODY_BB; else goto L12;
3128 L12:
3129 V3 = N31;
3130 V2 += STEP2;
3131 if (V2 cond2 N22) goto L120; else goto L13;
3132 L120:
3133 V4 = N41 + M41 * V2;
3134 NONRECT_BOUND4 = N42 + M42 * V2;
3135 if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L12;
3136 L13:
3137 V2 = N21;
3138 V1 += STEP1;
3139 goto L120; */
3140
3141 static basic_block
3142 extract_omp_for_update_vars (struct omp_for_data *fd, tree *nonrect_bounds,
3143 basic_block cont_bb, basic_block body_bb)
3144 {
3145 basic_block last_bb, bb, collapse_bb = NULL;
3146 int i;
3147 gimple_stmt_iterator gsi;
3148 edge e;
3149 tree t;
3150 gimple *stmt;
3151
3152 last_bb = cont_bb;
3153 for (i = fd->collapse - 1; i >= 0; i--)
3154 {
3155 tree vtype = TREE_TYPE (fd->loops[i].v);
3156
3157 bb = create_empty_bb (last_bb);
3158 add_bb_to_loop (bb, last_bb->loop_father);
3159 gsi = gsi_start_bb (bb);
3160
3161 if (i < fd->collapse - 1)
3162 {
3163 e = make_edge (last_bb, bb, EDGE_FALSE_VALUE);
3164 e->probability = profile_probability::guessed_always () / 8;
3165
3166 struct omp_for_data_loop *l = &fd->loops[i + 1];
3167 if (l->m1 == NULL_TREE || l->outer != 1)
3168 {
3169 t = l->n1;
3170 if (l->m1)
3171 {
3172 if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3173 t = fold_build_pointer_plus (fd->loops[i + 1 - l->outer].v,
3174 t);
3175 else
3176 {
3177 tree t2
3178 = fold_build2 (MULT_EXPR, TREE_TYPE (t),
3179 fd->loops[i + 1 - l->outer].v, l->m1);
3180 t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t2, t);
3181 }
3182 }
3183 t = force_gimple_operand_gsi (&gsi, t,
3184 DECL_P (l->v)
3185 && TREE_ADDRESSABLE (l->v),
3186 NULL_TREE, false,
3187 GSI_CONTINUE_LINKING);
3188 stmt = gimple_build_assign (l->v, t);
3189 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3190 }
3191 }
3192 else
3193 collapse_bb = bb;
3194
3195 set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
3196
3197 if (POINTER_TYPE_P (vtype))
3198 t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
3199 else
3200 t = fold_build2 (PLUS_EXPR, vtype, fd->loops[i].v, fd->loops[i].step);
3201 t = force_gimple_operand_gsi (&gsi, t,
3202 DECL_P (fd->loops[i].v)
3203 && TREE_ADDRESSABLE (fd->loops[i].v),
3204 NULL_TREE, false, GSI_CONTINUE_LINKING);
3205 stmt = gimple_build_assign (fd->loops[i].v, t);
3206 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3207
3208 if (fd->loops[i].non_rect_referenced)
3209 {
3210 basic_block update_bb = NULL, prev_bb = NULL;
3211 for (int j = i + 1; j <= fd->last_nonrect; j++)
3212 if (j - fd->loops[j].outer == i)
3213 {
3214 tree n1, n2;
3215 struct omp_for_data_loop *l = &fd->loops[j];
3216 basic_block this_bb = create_empty_bb (last_bb);
3217 add_bb_to_loop (this_bb, last_bb->loop_father);
3218 gimple_stmt_iterator gsi2 = gsi_start_bb (this_bb);
3219 if (prev_bb)
3220 {
3221 e = make_edge (prev_bb, this_bb, EDGE_TRUE_VALUE);
3222 e->probability
3223 = profile_probability::guessed_always ().apply_scale (7,
3224 8);
3225 set_immediate_dominator (CDI_DOMINATORS, this_bb, prev_bb);
3226 }
3227 if (l->m1)
3228 {
3229 if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3230 t = fold_build_pointer_plus (fd->loops[i].v, l->n1);
3231 else
3232 {
3233 t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m1), l->m1,
3234 fd->loops[i].v);
3235 t = fold_build2 (PLUS_EXPR, TREE_TYPE (l->v),
3236 t, l->n1);
3237 }
3238 n1 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3239 false,
3240 GSI_CONTINUE_LINKING);
3241 stmt = gimple_build_assign (l->v, n1);
3242 gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
3243 n1 = l->v;
3244 }
3245 else
3246 n1 = force_gimple_operand_gsi (&gsi2, l->n1, true,
3247 NULL_TREE, false,
3248 GSI_CONTINUE_LINKING);
3249 if (l->m2)
3250 {
3251 if (POINTER_TYPE_P (TREE_TYPE (l->v)))
3252 t = fold_build_pointer_plus (fd->loops[i].v, l->n2);
3253 else
3254 {
3255 t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m2), l->m2,
3256 fd->loops[i].v);
3257 t = fold_build2 (PLUS_EXPR,
3258 TREE_TYPE (nonrect_bounds[j]),
3259 t, unshare_expr (l->n2));
3260 }
3261 n2 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3262 false,
3263 GSI_CONTINUE_LINKING);
3264 stmt = gimple_build_assign (nonrect_bounds[j], n2);
3265 gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
3266 n2 = nonrect_bounds[j];
3267 }
3268 else
3269 n2 = force_gimple_operand_gsi (&gsi2, unshare_expr (l->n2),
3270 true, NULL_TREE, false,
3271 GSI_CONTINUE_LINKING);
3272 gcond *cond_stmt
3273 = gimple_build_cond (l->cond_code, n1, n2,
3274 NULL_TREE, NULL_TREE);
3275 gsi_insert_after (&gsi2, cond_stmt, GSI_CONTINUE_LINKING);
3276 if (update_bb == NULL)
3277 update_bb = this_bb;
3278 e = make_edge (this_bb, bb, EDGE_FALSE_VALUE);
3279 e->probability = profile_probability::guessed_always () / 8;
3280 if (prev_bb == NULL)
3281 set_immediate_dominator (CDI_DOMINATORS, this_bb, bb);
3282 prev_bb = this_bb;
3283 }
3284 e = make_edge (prev_bb, body_bb, EDGE_TRUE_VALUE);
3285 e->probability
3286 = profile_probability::guessed_always ().apply_scale (7, 8);
3287 body_bb = update_bb;
3288 }
3289
3290 if (i > 0)
3291 {
3292 if (fd->loops[i].m2)
3293 t = nonrect_bounds[i];
3294 else
3295 t = unshare_expr (fd->loops[i].n2);
3296 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
3297 false, GSI_CONTINUE_LINKING);
3298 tree v = fd->loops[i].v;
3299 if (DECL_P (v) && TREE_ADDRESSABLE (v))
3300 v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
3301 false, GSI_CONTINUE_LINKING);
3302 t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
3303 stmt = gimple_build_cond_empty (t);
3304 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
3305 if (walk_tree (gimple_cond_lhs_ptr (as_a <gcond *> (stmt)),
3306 expand_omp_regimplify_p, NULL, NULL)
3307 || walk_tree (gimple_cond_rhs_ptr (as_a <gcond *> (stmt)),
3308 expand_omp_regimplify_p, NULL, NULL))
3309 gimple_regimplify_operands (stmt, &gsi);
3310 e = make_edge (bb, body_bb, EDGE_TRUE_VALUE);
3311 e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
3312 }
3313 else
3314 make_edge (bb, body_bb, EDGE_FALLTHRU);
3315 set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
3316 last_bb = bb;
3317 }
3318
3319 return collapse_bb;
3320 }
3321
3322 /* Expand #pragma omp ordered depend(source). */
3323
3324 static void
3325 expand_omp_ordered_source (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
3326 tree *counts, location_t loc)
3327 {
3328 enum built_in_function source_ix
3329 = fd->iter_type == long_integer_type_node
3330 ? BUILT_IN_GOMP_DOACROSS_POST : BUILT_IN_GOMP_DOACROSS_ULL_POST;
3331 gimple *g
3332 = gimple_build_call (builtin_decl_explicit (source_ix), 1,
3333 build_fold_addr_expr (counts[fd->ordered]));
3334 gimple_set_location (g, loc);
3335 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3336 }
3337
3338 /* Expand a single depend from #pragma omp ordered depend(sink:...). */
3339
3340 static void
3341 expand_omp_ordered_sink (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
3342 tree *counts, tree c, location_t loc,
3343 basic_block cont_bb)
3344 {
3345 auto_vec<tree, 10> args;
3346 enum built_in_function sink_ix
3347 = fd->iter_type == long_integer_type_node
3348 ? BUILT_IN_GOMP_DOACROSS_WAIT : BUILT_IN_GOMP_DOACROSS_ULL_WAIT;
3349 tree t, off, coff = NULL_TREE, deps = OMP_CLAUSE_DECL (c), cond = NULL_TREE;
3350 int i;
3351 gimple_stmt_iterator gsi2 = *gsi;
3352 bool warned_step = false;
3353
3354 if (deps == NULL)
3355 {
3356 /* Handle doacross(sink: omp_cur_iteration - 1). */
3357 gsi_prev (&gsi2);
3358 edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
3359 edge e2 = split_block_after_labels (e1->dest);
3360 gsi2 = gsi_after_labels (e1->dest);
3361 *gsi = gsi_last_bb (e1->src);
3362 gimple_stmt_iterator gsi3 = *gsi;
3363
3364 if (counts[fd->collapse - 1])
3365 {
3366 gcc_assert (fd->collapse == 1);
3367 t = counts[fd->collapse - 1];
3368 }
3369 else if (fd->collapse > 1)
3370 t = fd->loop.v;
3371 else
3372 {
3373 t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
3374 fd->loops[0].v, fd->loops[0].n1);
3375 t = fold_convert (fd->iter_type, t);
3376 }
3377
3378 t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
3379 false, GSI_CONTINUE_LINKING);
3380 gsi_insert_after (gsi, gimple_build_cond (NE_EXPR, t,
3381 build_zero_cst (TREE_TYPE (t)),
3382 NULL_TREE, NULL_TREE),
3383 GSI_NEW_STMT);
3384
3385 t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
3386 build_minus_one_cst (TREE_TYPE (t)));
3387 t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3388 true, GSI_SAME_STMT);
3389 args.safe_push (t);
3390 for (i = fd->collapse; i < fd->ordered; i++)
3391 {
3392 t = counts[fd->ordered + 2 + (i - fd->collapse)];
3393 t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
3394 build_minus_one_cst (TREE_TYPE (t)));
3395 t = fold_convert (fd->iter_type, t);
3396 t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3397 true, GSI_SAME_STMT);
3398 args.safe_push (t);
3399 }
3400
3401 gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix),
3402 args);
3403 gimple_set_location (g, loc);
3404 gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
3405
3406 edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
3407 e3->probability = profile_probability::guessed_always () / 8;
3408 e1->probability = e3->probability.invert ();
3409 e1->flags = EDGE_TRUE_VALUE;
3410 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
3411
3412 if (fd->ordered > fd->collapse && cont_bb)
3413 {
3414 if (counts[fd->ordered + 1] == NULL_TREE)
3415 counts[fd->ordered + 1]
3416 = create_tmp_var (boolean_type_node, ".first");
3417
3418 edge e4;
3419 if (gsi_end_p (gsi3))
3420 e4 = split_block_after_labels (e1->src);
3421 else
3422 {
3423 gsi_prev (&gsi3);
3424 e4 = split_block (gsi_bb (gsi3), gsi_stmt (gsi3));
3425 }
3426 gsi3 = gsi_last_bb (e4->src);
3427
3428 gsi_insert_after (&gsi3,
3429 gimple_build_cond (NE_EXPR,
3430 counts[fd->ordered + 1],
3431 boolean_false_node,
3432 NULL_TREE, NULL_TREE),
3433 GSI_NEW_STMT);
3434
3435 edge e5 = make_edge (e4->src, e2->dest, EDGE_FALSE_VALUE);
3436 e4->probability = profile_probability::guessed_always () / 8;
3437 e5->probability = e4->probability.invert ();
3438 e4->flags = EDGE_TRUE_VALUE;
3439 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e4->src);
3440 }
3441
3442 *gsi = gsi_after_labels (e2->dest);
3443 return;
3444 }
3445 for (i = 0; i < fd->ordered; i++)
3446 {
3447 tree step = NULL_TREE;
3448 off = TREE_PURPOSE (deps);
3449 if (TREE_CODE (off) == TRUNC_DIV_EXPR)
3450 {
3451 step = TREE_OPERAND (off, 1);
3452 off = TREE_OPERAND (off, 0);
3453 }
3454 if (!integer_zerop (off))
3455 {
3456 gcc_assert (fd->loops[i].cond_code == LT_EXPR
3457 || fd->loops[i].cond_code == GT_EXPR);
3458 bool forward = fd->loops[i].cond_code == LT_EXPR;
3459 if (step)
3460 {
3461 /* Non-simple Fortran DO loops. If step is variable,
3462 we don't know at compile even the direction, so can't
3463 warn. */
3464 if (TREE_CODE (step) != INTEGER_CST)
3465 break;
3466 forward = tree_int_cst_sgn (step) != -1;
3467 }
3468 if (forward ^ OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3469 warning_at (loc, OPT_Wopenmp,
3470 "%qs clause with %<sink%> modifier "
3471 "waiting for lexically later iteration",
3472 OMP_CLAUSE_DOACROSS_DEPEND (c)
3473 ? "depend" : "doacross");
3474 break;
3475 }
3476 deps = TREE_CHAIN (deps);
3477 }
3478 /* If all offsets corresponding to the collapsed loops are zero,
3479 this depend clause can be ignored. FIXME: but there is still a
3480 flush needed. We need to emit one __sync_synchronize () for it
3481 though (perhaps conditionally)? Solve this together with the
3482 conservative dependence folding optimization.
3483 if (i >= fd->collapse)
3484 return; */
3485
3486 deps = OMP_CLAUSE_DECL (c);
3487 gsi_prev (&gsi2);
3488 edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
3489 edge e2 = split_block_after_labels (e1->dest);
3490
3491 gsi2 = gsi_after_labels (e1->dest);
3492 *gsi = gsi_last_bb (e1->src);
3493 for (i = 0; i < fd->ordered; i++)
3494 {
3495 tree itype = TREE_TYPE (fd->loops[i].v);
3496 tree step = NULL_TREE;
3497 tree orig_off = NULL_TREE;
3498 if (POINTER_TYPE_P (itype))
3499 itype = sizetype;
3500 if (i)
3501 deps = TREE_CHAIN (deps);
3502 off = TREE_PURPOSE (deps);
3503 if (TREE_CODE (off) == TRUNC_DIV_EXPR)
3504 {
3505 step = TREE_OPERAND (off, 1);
3506 off = TREE_OPERAND (off, 0);
3507 gcc_assert (fd->loops[i].cond_code == LT_EXPR
3508 && integer_onep (fd->loops[i].step)
3509 && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)));
3510 }
3511 tree s = fold_convert_loc (loc, itype, step ? step : fd->loops[i].step);
3512 if (step)
3513 {
3514 off = fold_convert_loc (loc, itype, off);
3515 orig_off = off;
3516 off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
3517 }
3518
3519 if (integer_zerop (off))
3520 t = boolean_true_node;
3521 else
3522 {
3523 tree a;
3524 tree co = fold_convert_loc (loc, itype, off);
3525 if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
3526 {
3527 if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3528 co = fold_build1_loc (loc, NEGATE_EXPR, itype, co);
3529 a = fold_build2_loc (loc, POINTER_PLUS_EXPR,
3530 TREE_TYPE (fd->loops[i].v), fd->loops[i].v,
3531 co);
3532 }
3533 else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3534 a = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3535 fd->loops[i].v, co);
3536 else
3537 a = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
3538 fd->loops[i].v, co);
3539 if (step)
3540 {
3541 tree t1, t2;
3542 if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3543 t1 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3544 fd->loops[i].n1);
3545 else
3546 t1 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3547 fd->loops[i].n2);
3548 if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3549 t2 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3550 fd->loops[i].n2);
3551 else
3552 t2 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3553 fd->loops[i].n1);
3554 t = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
3555 step, build_int_cst (TREE_TYPE (step), 0));
3556 if (TREE_CODE (step) != INTEGER_CST)
3557 {
3558 t1 = unshare_expr (t1);
3559 t1 = force_gimple_operand_gsi (gsi, t1, true, NULL_TREE,
3560 false, GSI_CONTINUE_LINKING);
3561 t2 = unshare_expr (t2);
3562 t2 = force_gimple_operand_gsi (gsi, t2, true, NULL_TREE,
3563 false, GSI_CONTINUE_LINKING);
3564 }
3565 t = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
3566 t, t2, t1);
3567 }
3568 else if (fd->loops[i].cond_code == LT_EXPR)
3569 {
3570 if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3571 t = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
3572 fd->loops[i].n1);
3573 else
3574 t = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
3575 fd->loops[i].n2);
3576 }
3577 else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3578 t = fold_build2_loc (loc, GT_EXPR, boolean_type_node, a,
3579 fd->loops[i].n2);
3580 else
3581 t = fold_build2_loc (loc, LE_EXPR, boolean_type_node, a,
3582 fd->loops[i].n1);
3583 }
3584 if (cond)
3585 cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node, cond, t);
3586 else
3587 cond = t;
3588
3589 off = fold_convert_loc (loc, itype, off);
3590
3591 if (step
3592 || (fd->loops[i].cond_code == LT_EXPR
3593 ? !integer_onep (fd->loops[i].step)
3594 : !integer_minus_onep (fd->loops[i].step)))
3595 {
3596 if (step == NULL_TREE
3597 && TYPE_UNSIGNED (itype)
3598 && fd->loops[i].cond_code == GT_EXPR)
3599 t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype, off,
3600 fold_build1_loc (loc, NEGATE_EXPR, itype,
3601 s));
3602 else
3603 t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype,
3604 orig_off ? orig_off : off, s);
3605 t = fold_build2_loc (loc, EQ_EXPR, boolean_type_node, t,
3606 build_int_cst (itype, 0));
3607 if (integer_zerop (t) && !warned_step)
3608 {
3609 warning_at (loc, OPT_Wopenmp,
3610 "%qs clause with %<sink%> modifier refers to "
3611 "iteration never in the iteration space",
3612 OMP_CLAUSE_DOACROSS_DEPEND (c)
3613 ? "depend" : "doacross");
3614 warned_step = true;
3615 }
3616 cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node,
3617 cond, t);
3618 }
3619
3620 if (i <= fd->collapse - 1 && fd->collapse > 1)
3621 t = fd->loop.v;
3622 else if (counts[i])
3623 t = counts[i];
3624 else
3625 {
3626 t = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3627 fd->loops[i].v, fd->loops[i].n1);
3628 t = fold_convert_loc (loc, fd->iter_type, t);
3629 }
3630 if (step)
3631 /* We have divided off by step already earlier. */;
3632 else if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
3633 off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off,
3634 fold_build1_loc (loc, NEGATE_EXPR, itype,
3635 s));
3636 else
3637 off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
3638 if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
3639 off = fold_build1_loc (loc, NEGATE_EXPR, itype, off);
3640 off = fold_convert_loc (loc, fd->iter_type, off);
3641 if (i <= fd->collapse - 1 && fd->collapse > 1)
3642 {
3643 if (i)
3644 off = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, coff,
3645 off);
3646 if (i < fd->collapse - 1)
3647 {
3648 coff = fold_build2_loc (loc, MULT_EXPR, fd->iter_type, off,
3649 counts[i]);
3650 continue;
3651 }
3652 }
3653 off = unshare_expr (off);
3654 t = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, t, off);
3655 t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
3656 true, GSI_SAME_STMT);
3657 args.safe_push (t);
3658 }
3659 gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix), args);
3660 gimple_set_location (g, loc);
3661 gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
3662
3663 cond = unshare_expr (cond);
3664 cond = force_gimple_operand_gsi (gsi, cond, true, NULL_TREE, false,
3665 GSI_CONTINUE_LINKING);
3666 gsi_insert_after (gsi, gimple_build_cond_empty (cond), GSI_NEW_STMT);
3667 edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
3668 e3->probability = profile_probability::guessed_always () / 8;
3669 e1->probability = e3->probability.invert ();
3670 e1->flags = EDGE_TRUE_VALUE;
3671 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
3672
3673 *gsi = gsi_after_labels (e2->dest);
3674 }
3675
3676 /* Expand all #pragma omp ordered depend(source) and
3677 #pragma omp ordered depend(sink:...) constructs in the current
3678 #pragma omp for ordered(n) region. */
3679
3680 static void
3681 expand_omp_ordered_source_sink (struct omp_region *region,
3682 struct omp_for_data *fd, tree *counts,
3683 basic_block cont_bb)
3684 {
3685 struct omp_region *inner;
3686 int i;
3687 for (i = fd->collapse - 1; i < fd->ordered; i++)
3688 if (i == fd->collapse - 1 && fd->collapse > 1)
3689 counts[i] = NULL_TREE;
3690 else if (i >= fd->collapse && !cont_bb)
3691 counts[i] = build_zero_cst (fd->iter_type);
3692 else if (!POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
3693 && integer_onep (fd->loops[i].step))
3694 counts[i] = NULL_TREE;
3695 else
3696 counts[i] = create_tmp_var (fd->iter_type, ".orditer");
3697 tree atype
3698 = build_array_type_nelts (fd->iter_type, fd->ordered - fd->collapse + 1);
3699 counts[fd->ordered] = create_tmp_var (atype, ".orditera");
3700 TREE_ADDRESSABLE (counts[fd->ordered]) = 1;
3701 counts[fd->ordered + 1] = NULL_TREE;
3702
3703 for (inner = region->inner; inner; inner = inner->next)
3704 if (inner->type == GIMPLE_OMP_ORDERED)
3705 {
3706 gomp_ordered *ord_stmt = inner->ord_stmt;
3707 gimple_stmt_iterator gsi = gsi_for_stmt (ord_stmt);
3708 location_t loc = gimple_location (ord_stmt);
3709 tree c;
3710 for (c = gimple_omp_ordered_clauses (ord_stmt);
3711 c; c = OMP_CLAUSE_CHAIN (c))
3712 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
3713 break;
3714 if (c)
3715 expand_omp_ordered_source (&gsi, fd, counts, loc);
3716 for (c = gimple_omp_ordered_clauses (ord_stmt);
3717 c; c = OMP_CLAUSE_CHAIN (c))
3718 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
3719 expand_omp_ordered_sink (&gsi, fd, counts, c, loc, cont_bb);
3720 gsi_remove (&gsi, true);
3721 }
3722 }
3723
3724 /* Wrap the body into fd->ordered - fd->collapse loops that aren't
3725 collapsed. */
3726
3727 static basic_block
3728 expand_omp_for_ordered_loops (struct omp_for_data *fd, tree *counts,
3729 basic_block cont_bb, basic_block body_bb,
3730 basic_block l0_bb, bool ordered_lastprivate)
3731 {
3732 if (fd->ordered == fd->collapse)
3733 return cont_bb;
3734
3735 if (!cont_bb)
3736 {
3737 gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3738 for (int i = fd->collapse; i < fd->ordered; i++)
3739 {
3740 tree type = TREE_TYPE (fd->loops[i].v);
3741 tree n1 = fold_convert (type, fd->loops[i].n1);
3742 expand_omp_build_assign (&gsi, fd->loops[i].v, n1);
3743 tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3744 size_int (i - fd->collapse + 1),
3745 NULL_TREE, NULL_TREE);
3746 expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
3747 }
3748 return NULL;
3749 }
3750
3751 for (int i = fd->ordered - 1; i >= fd->collapse; i--)
3752 {
3753 tree t, type = TREE_TYPE (fd->loops[i].v);
3754 gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3755 if (counts[fd->ordered + 1] && i == fd->collapse)
3756 expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
3757 boolean_true_node);
3758 expand_omp_build_assign (&gsi, fd->loops[i].v,
3759 fold_convert (type, fd->loops[i].n1));
3760 if (counts[i])
3761 expand_omp_build_assign (&gsi, counts[i],
3762 build_zero_cst (fd->iter_type));
3763 tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3764 size_int (i - fd->collapse + 1),
3765 NULL_TREE, NULL_TREE);
3766 expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
3767 if (!gsi_end_p (gsi))
3768 gsi_prev (&gsi);
3769 else
3770 gsi = gsi_last_bb (body_bb);
3771 edge e1 = split_block (body_bb, gsi_stmt (gsi));
3772 basic_block new_body = e1->dest;
3773 if (body_bb == cont_bb)
3774 cont_bb = new_body;
3775 edge e2 = NULL;
3776 basic_block new_header;
3777 if (EDGE_COUNT (cont_bb->preds) > 0)
3778 {
3779 gsi = gsi_last_bb (cont_bb);
3780 if (POINTER_TYPE_P (type))
3781 t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
3782 else
3783 t = fold_build2 (PLUS_EXPR, type, fd->loops[i].v,
3784 fold_convert (type, fd->loops[i].step));
3785 expand_omp_build_assign (&gsi, fd->loops[i].v, t);
3786 if (counts[i])
3787 {
3788 t = fold_build2 (PLUS_EXPR, fd->iter_type, counts[i],
3789 build_int_cst (fd->iter_type, 1));
3790 expand_omp_build_assign (&gsi, counts[i], t);
3791 t = counts[i];
3792 }
3793 else
3794 {
3795 t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
3796 fd->loops[i].v, fd->loops[i].n1);
3797 t = fold_convert (fd->iter_type, t);
3798 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
3799 true, GSI_SAME_STMT);
3800 }
3801 aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
3802 size_int (i - fd->collapse + 1),
3803 NULL_TREE, NULL_TREE);
3804 expand_omp_build_assign (&gsi, aref, t);
3805 if (counts[fd->ordered + 1] && i == fd->ordered - 1)
3806 expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
3807 boolean_false_node);
3808 gsi_prev (&gsi);
3809 e2 = split_block (cont_bb, gsi_stmt (gsi));
3810 new_header = e2->dest;
3811 }
3812 else
3813 new_header = cont_bb;
3814 gsi = gsi_after_labels (new_header);
3815 tree v = force_gimple_operand_gsi (&gsi, fd->loops[i].v, true, NULL_TREE,
3816 true, GSI_SAME_STMT);
3817 tree n2
3818 = force_gimple_operand_gsi (&gsi, fold_convert (type, fd->loops[i].n2),
3819 true, NULL_TREE, true, GSI_SAME_STMT);
3820 t = build2 (fd->loops[i].cond_code, boolean_type_node, v, n2);
3821 gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_NEW_STMT);
3822 edge e3 = split_block (new_header, gsi_stmt (gsi));
3823 cont_bb = e3->dest;
3824 remove_edge (e1);
3825 make_edge (body_bb, new_header, EDGE_FALLTHRU);
3826 e3->flags = EDGE_FALSE_VALUE;
3827 e3->probability = profile_probability::guessed_always () / 8;
3828 e1 = make_edge (new_header, new_body, EDGE_TRUE_VALUE);
3829 e1->probability = e3->probability.invert ();
3830
3831 set_immediate_dominator (CDI_DOMINATORS, new_header, body_bb);
3832 set_immediate_dominator (CDI_DOMINATORS, new_body, new_header);
3833
3834 if (e2)
3835 {
3836 class loop *loop = alloc_loop ();
3837 loop->header = new_header;
3838 loop->latch = e2->src;
3839 add_loop (loop, l0_bb->loop_father);
3840 }
3841 }
3842
3843 /* If there are any lastprivate clauses and it is possible some loops
3844 might have zero iterations, ensure all the decls are initialized,
3845 otherwise we could crash evaluating C++ class iterators with lastprivate
3846 clauses. */
3847 bool need_inits = false;
3848 for (int i = fd->collapse; ordered_lastprivate && i < fd->ordered; i++)
3849 if (need_inits)
3850 {
3851 tree type = TREE_TYPE (fd->loops[i].v);
3852 gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
3853 expand_omp_build_assign (&gsi, fd->loops[i].v,
3854 fold_convert (type, fd->loops[i].n1));
3855 }
3856 else
3857 {
3858 tree type = TREE_TYPE (fd->loops[i].v);
3859 tree this_cond = fold_build2 (fd->loops[i].cond_code,
3860 boolean_type_node,
3861 fold_convert (type, fd->loops[i].n1),
3862 fold_convert (type, fd->loops[i].n2));
3863 if (!integer_onep (this_cond))
3864 need_inits = true;
3865 }
3866
3867 return cont_bb;
3868 }
3869
3870 /* A subroutine of expand_omp_for. Generate code for a parallel
3871 loop with any schedule. Given parameters:
3872
3873 for (V = N1; V cond N2; V += STEP) BODY;
3874
3875 where COND is "<" or ">", we generate pseudocode
3876
3877 more = GOMP_loop_foo_start (N1, N2, STEP, CHUNK, &istart0, &iend0);
3878 if (more) goto L0; else goto L3;
3879 L0:
3880 V = istart0;
3881 iend = iend0;
3882 L1:
3883 BODY;
3884 V += STEP;
3885 if (V cond iend) goto L1; else goto L2;
3886 L2:
3887 if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
3888 L3:
3889
3890 If this is a combined omp parallel loop, instead of the call to
3891 GOMP_loop_foo_start, we call GOMP_loop_foo_next.
3892 If this is gimple_omp_for_combined_p loop, then instead of assigning
3893 V and iend in L0 we assign the first two _looptemp_ clause decls of the
3894 inner GIMPLE_OMP_FOR and V += STEP; and
3895 if (V cond iend) goto L1; else goto L2; are removed.
3896
3897 For collapsed loops, given parameters:
3898 collapse(3)
3899 for (V1 = N11; V1 cond1 N12; V1 += STEP1)
3900 for (V2 = N21; V2 cond2 N22; V2 += STEP2)
3901 for (V3 = N31; V3 cond3 N32; V3 += STEP3)
3902 BODY;
3903
3904 we generate pseudocode
3905
3906 if (__builtin_expect (N32 cond3 N31, 0)) goto Z0;
3907 if (cond3 is <)
3908 adj = STEP3 - 1;
3909 else
3910 adj = STEP3 + 1;
3911 count3 = (adj + N32 - N31) / STEP3;
3912 if (__builtin_expect (N22 cond2 N21, 0)) goto Z0;
3913 if (cond2 is <)
3914 adj = STEP2 - 1;
3915 else
3916 adj = STEP2 + 1;
3917 count2 = (adj + N22 - N21) / STEP2;
3918 if (__builtin_expect (N12 cond1 N11, 0)) goto Z0;
3919 if (cond1 is <)
3920 adj = STEP1 - 1;
3921 else
3922 adj = STEP1 + 1;
3923 count1 = (adj + N12 - N11) / STEP1;
3924 count = count1 * count2 * count3;
3925 goto Z1;
3926 Z0:
3927 count = 0;
3928 Z1:
3929 more = GOMP_loop_foo_start (0, count, 1, CHUNK, &istart0, &iend0);
3930 if (more) goto L0; else goto L3;
3931 L0:
3932 V = istart0;
3933 T = V;
3934 V3 = N31 + (T % count3) * STEP3;
3935 T = T / count3;
3936 V2 = N21 + (T % count2) * STEP2;
3937 T = T / count2;
3938 V1 = N11 + T * STEP1;
3939 iend = iend0;
3940 L1:
3941 BODY;
3942 V += 1;
3943 if (V < iend) goto L10; else goto L2;
3944 L10:
3945 V3 += STEP3;
3946 if (V3 cond3 N32) goto L1; else goto L11;
3947 L11:
3948 V3 = N31;
3949 V2 += STEP2;
3950 if (V2 cond2 N22) goto L1; else goto L12;
3951 L12:
3952 V2 = N21;
3953 V1 += STEP1;
3954 goto L1;
3955 L2:
3956 if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
3957 L3:
3958
3959 */
3960
3961 static void
3962 expand_omp_for_generic (struct omp_region *region,
3963 struct omp_for_data *fd,
3964 enum built_in_function start_fn,
3965 enum built_in_function next_fn,
3966 tree sched_arg,
3967 gimple *inner_stmt)
3968 {
3969 tree type, istart0, iend0, iend;
3970 tree t, vmain, vback, bias = NULL_TREE;
3971 basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, collapse_bb;
3972 basic_block l2_bb = NULL, l3_bb = NULL;
3973 gimple_stmt_iterator gsi;
3974 gassign *assign_stmt;
3975 bool in_combined_parallel = is_combined_parallel (region);
3976 bool broken_loop = region->cont == NULL;
3977 edge e, ne;
3978 tree *counts = NULL;
3979 int i;
3980 bool ordered_lastprivate = false;
3981 bool offload = is_in_offload_region (region);
3982
3983 gcc_assert (!broken_loop || !in_combined_parallel);
3984 gcc_assert (fd->iter_type == long_integer_type_node
3985 || !in_combined_parallel);
3986
3987 entry_bb = region->entry;
3988 cont_bb = region->cont;
3989 collapse_bb = NULL;
3990 gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
3991 gcc_assert (broken_loop
3992 || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
3993 l0_bb = split_edge (FALLTHRU_EDGE (entry_bb));
3994 l1_bb = single_succ (l0_bb);
3995 if (!broken_loop)
3996 {
3997 l2_bb = create_empty_bb (cont_bb);
3998 gcc_assert (BRANCH_EDGE (cont_bb)->dest == l1_bb
3999 || (single_succ_edge (BRANCH_EDGE (cont_bb)->dest)->dest
4000 == l1_bb));
4001 gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
4002 }
4003 else
4004 l2_bb = NULL;
4005 l3_bb = BRANCH_EDGE (entry_bb)->dest;
4006 exit_bb = region->exit;
4007
4008 gsi = gsi_last_nondebug_bb (entry_bb);
4009
4010 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
4011 if (fd->ordered
4012 && omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4013 OMP_CLAUSE_LASTPRIVATE))
4014 ordered_lastprivate = false;
4015 tree reductions = NULL_TREE;
4016 tree mem = NULL_TREE, cond_var = NULL_TREE, condtemp = NULL_TREE;
4017 tree memv = NULL_TREE;
4018 if (fd->lastprivate_conditional)
4019 {
4020 tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4021 OMP_CLAUSE__CONDTEMP_);
4022 if (fd->have_pointer_condtemp)
4023 condtemp = OMP_CLAUSE_DECL (c);
4024 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
4025 cond_var = OMP_CLAUSE_DECL (c);
4026 }
4027 if (sched_arg)
4028 {
4029 if (fd->have_reductemp)
4030 {
4031 tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4032 OMP_CLAUSE__REDUCTEMP_);
4033 reductions = OMP_CLAUSE_DECL (c);
4034 gcc_assert (TREE_CODE (reductions) == SSA_NAME);
4035 gimple *g = SSA_NAME_DEF_STMT (reductions);
4036 reductions = gimple_assign_rhs1 (g);
4037 OMP_CLAUSE_DECL (c) = reductions;
4038 entry_bb = gimple_bb (g);
4039 edge e = split_block (entry_bb, g);
4040 if (region->entry == entry_bb)
4041 region->entry = e->dest;
4042 gsi = gsi_last_bb (entry_bb);
4043 }
4044 else
4045 reductions = null_pointer_node;
4046 if (fd->have_pointer_condtemp)
4047 {
4048 tree type = TREE_TYPE (condtemp);
4049 memv = create_tmp_var (type);
4050 TREE_ADDRESSABLE (memv) = 1;
4051 unsigned HOST_WIDE_INT sz
4052 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
4053 sz *= fd->lastprivate_conditional;
4054 expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
4055 false);
4056 mem = build_fold_addr_expr (memv);
4057 }
4058 else
4059 mem = null_pointer_node;
4060 }
4061 if (fd->collapse > 1 || fd->ordered)
4062 {
4063 int first_zero_iter1 = -1, first_zero_iter2 = -1;
4064 basic_block zero_iter1_bb = NULL, zero_iter2_bb = NULL, l2_dom_bb = NULL;
4065
4066 counts = XALLOCAVEC (tree, fd->ordered
4067 ? fd->ordered + 2
4068 + (fd->ordered - fd->collapse)
4069 : fd->collapse);
4070 expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
4071 zero_iter1_bb, first_zero_iter1,
4072 zero_iter2_bb, first_zero_iter2, l2_dom_bb);
4073
4074 if (zero_iter1_bb)
4075 {
4076 /* Some counts[i] vars might be uninitialized if
4077 some loop has zero iterations. But the body shouldn't
4078 be executed in that case, so just avoid uninit warnings. */
4079 for (i = first_zero_iter1;
4080 i < (fd->ordered ? fd->ordered : fd->collapse); i++)
4081 if (SSA_VAR_P (counts[i]))
4082 suppress_warning (counts[i], OPT_Wuninitialized);
4083 gsi_prev (&gsi);
4084 e = split_block (entry_bb, gsi_stmt (gsi));
4085 entry_bb = e->dest;
4086 make_edge (zero_iter1_bb, entry_bb, EDGE_FALLTHRU);
4087 gsi = gsi_last_nondebug_bb (entry_bb);
4088 set_immediate_dominator (CDI_DOMINATORS, entry_bb,
4089 get_immediate_dominator (CDI_DOMINATORS,
4090 zero_iter1_bb));
4091 }
4092 if (zero_iter2_bb)
4093 {
4094 /* Some counts[i] vars might be uninitialized if
4095 some loop has zero iterations. But the body shouldn't
4096 be executed in that case, so just avoid uninit warnings. */
4097 for (i = first_zero_iter2; i < fd->ordered; i++)
4098 if (SSA_VAR_P (counts[i]))
4099 suppress_warning (counts[i], OPT_Wuninitialized);
4100 if (zero_iter1_bb)
4101 make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
4102 else
4103 {
4104 gsi_prev (&gsi);
4105 e = split_block (entry_bb, gsi_stmt (gsi));
4106 entry_bb = e->dest;
4107 make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
4108 gsi = gsi_last_nondebug_bb (entry_bb);
4109 set_immediate_dominator (CDI_DOMINATORS, entry_bb,
4110 get_immediate_dominator
4111 (CDI_DOMINATORS, zero_iter2_bb));
4112 }
4113 }
4114 if (fd->collapse == 1)
4115 {
4116 counts[0] = fd->loop.n2;
4117 fd->loop = fd->loops[0];
4118 }
4119 }
4120
4121 type = TREE_TYPE (fd->loop.v);
4122 istart0 = create_tmp_var (fd->iter_type, ".istart0");
4123 iend0 = create_tmp_var (fd->iter_type, ".iend0");
4124 TREE_ADDRESSABLE (istart0) = 1;
4125 TREE_ADDRESSABLE (iend0) = 1;
4126
4127 /* See if we need to bias by LLONG_MIN. */
4128 if (fd->iter_type == long_long_unsigned_type_node
4129 && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
4130 && !TYPE_UNSIGNED (type)
4131 && fd->ordered == 0)
4132 {
4133 tree n1, n2;
4134
4135 if (fd->loop.cond_code == LT_EXPR)
4136 {
4137 n1 = fd->loop.n1;
4138 n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
4139 }
4140 else
4141 {
4142 n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
4143 n2 = fd->loop.n1;
4144 }
4145 if (TREE_CODE (n1) != INTEGER_CST
4146 || TREE_CODE (n2) != INTEGER_CST
4147 || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
4148 bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
4149 }
4150
4151 gimple_stmt_iterator gsif = gsi;
4152 gsi_prev (&gsif);
4153
4154 tree arr = NULL_TREE;
4155 if (in_combined_parallel)
4156 {
4157 gcc_assert (fd->ordered == 0);
4158 /* In a combined parallel loop, emit a call to
4159 GOMP_loop_foo_next. */
4160 t = build_call_expr (builtin_decl_explicit (next_fn), 2,
4161 build_fold_addr_expr (istart0),
4162 build_fold_addr_expr (iend0));
4163 }
4164 else
4165 {
4166 tree t0, t1, t2, t3, t4;
4167 /* If this is not a combined parallel loop, emit a call to
4168 GOMP_loop_foo_start in ENTRY_BB. */
4169 t4 = build_fold_addr_expr (iend0);
4170 t3 = build_fold_addr_expr (istart0);
4171 if (fd->ordered)
4172 {
4173 t0 = build_int_cst (unsigned_type_node,
4174 fd->ordered - fd->collapse + 1);
4175 arr = create_tmp_var (build_array_type_nelts (fd->iter_type,
4176 fd->ordered
4177 - fd->collapse + 1),
4178 ".omp_counts");
4179 DECL_NAMELESS (arr) = 1;
4180 TREE_ADDRESSABLE (arr) = 1;
4181 TREE_STATIC (arr) = 1;
4182 vec<constructor_elt, va_gc> *v;
4183 vec_alloc (v, fd->ordered - fd->collapse + 1);
4184 int idx;
4185
4186 for (idx = 0; idx < fd->ordered - fd->collapse + 1; idx++)
4187 {
4188 tree c;
4189 if (idx == 0 && fd->collapse > 1)
4190 c = fd->loop.n2;
4191 else
4192 c = counts[idx + fd->collapse - 1];
4193 tree purpose = size_int (idx);
4194 CONSTRUCTOR_APPEND_ELT (v, purpose, c);
4195 if (TREE_CODE (c) != INTEGER_CST)
4196 TREE_STATIC (arr) = 0;
4197 }
4198
4199 DECL_INITIAL (arr) = build_constructor (TREE_TYPE (arr), v);
4200 if (!TREE_STATIC (arr))
4201 force_gimple_operand_gsi (&gsi, build1 (DECL_EXPR,
4202 void_type_node, arr),
4203 true, NULL_TREE, true, GSI_SAME_STMT);
4204 t1 = build_fold_addr_expr (arr);
4205 t2 = NULL_TREE;
4206 }
4207 else
4208 {
4209 t2 = fold_convert (fd->iter_type, fd->loop.step);
4210 t1 = fd->loop.n2;
4211 t0 = fd->loop.n1;
4212 if (gimple_omp_for_combined_into_p (fd->for_stmt))
4213 {
4214 tree innerc
4215 = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
4216 OMP_CLAUSE__LOOPTEMP_);
4217 gcc_assert (innerc);
4218 t0 = OMP_CLAUSE_DECL (innerc);
4219 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4220 OMP_CLAUSE__LOOPTEMP_);
4221 gcc_assert (innerc);
4222 t1 = OMP_CLAUSE_DECL (innerc);
4223 }
4224 if (POINTER_TYPE_P (TREE_TYPE (t0))
4225 && TYPE_PRECISION (TREE_TYPE (t0))
4226 != TYPE_PRECISION (fd->iter_type))
4227 {
4228 /* Avoid casting pointers to integer of a different size. */
4229 tree itype = signed_type_for (type);
4230 t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
4231 t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
4232 }
4233 else
4234 {
4235 t1 = fold_convert (fd->iter_type, t1);
4236 t0 = fold_convert (fd->iter_type, t0);
4237 }
4238 if (bias)
4239 {
4240 t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
4241 t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
4242 }
4243 }
4244 if (fd->iter_type == long_integer_type_node || fd->ordered)
4245 {
4246 if (fd->chunk_size)
4247 {
4248 t = fold_convert (fd->iter_type, fd->chunk_size);
4249 t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
4250 if (sched_arg)
4251 {
4252 if (fd->ordered)
4253 t = build_call_expr (builtin_decl_explicit (start_fn),
4254 8, t0, t1, sched_arg, t, t3, t4,
4255 reductions, mem);
4256 else
4257 t = build_call_expr (builtin_decl_explicit (start_fn),
4258 9, t0, t1, t2, sched_arg, t, t3, t4,
4259 reductions, mem);
4260 }
4261 else if (fd->ordered)
4262 t = build_call_expr (builtin_decl_explicit (start_fn),
4263 5, t0, t1, t, t3, t4);
4264 else
4265 t = build_call_expr (builtin_decl_explicit (start_fn),
4266 6, t0, t1, t2, t, t3, t4);
4267 }
4268 else if (fd->ordered)
4269 t = build_call_expr (builtin_decl_explicit (start_fn),
4270 4, t0, t1, t3, t4);
4271 else
4272 t = build_call_expr (builtin_decl_explicit (start_fn),
4273 5, t0, t1, t2, t3, t4);
4274 }
4275 else
4276 {
4277 tree t5;
4278 tree c_bool_type;
4279 tree bfn_decl;
4280
4281 /* The GOMP_loop_ull_*start functions have additional boolean
4282 argument, true for < loops and false for > loops.
4283 In Fortran, the C bool type can be different from
4284 boolean_type_node. */
4285 bfn_decl = builtin_decl_explicit (start_fn);
4286 c_bool_type = TREE_TYPE (TREE_TYPE (bfn_decl));
4287 t5 = build_int_cst (c_bool_type,
4288 fd->loop.cond_code == LT_EXPR ? 1 : 0);
4289 if (fd->chunk_size)
4290 {
4291 tree bfn_decl = builtin_decl_explicit (start_fn);
4292 t = fold_convert (fd->iter_type, fd->chunk_size);
4293 t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
4294 if (sched_arg)
4295 t = build_call_expr (bfn_decl, 10, t5, t0, t1, t2, sched_arg,
4296 t, t3, t4, reductions, mem);
4297 else
4298 t = build_call_expr (bfn_decl, 7, t5, t0, t1, t2, t, t3, t4);
4299 }
4300 else
4301 t = build_call_expr (builtin_decl_explicit (start_fn),
4302 6, t5, t0, t1, t2, t3, t4);
4303 }
4304 }
4305 if (TREE_TYPE (t) != boolean_type_node)
4306 t = fold_build2 (NE_EXPR, boolean_type_node,
4307 t, build_int_cst (TREE_TYPE (t), 0));
4308 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4309 true, GSI_SAME_STMT);
4310 if (arr && !TREE_STATIC (arr))
4311 {
4312 tree clobber = build_clobber (TREE_TYPE (arr));
4313 gsi_insert_before (&gsi, gimple_build_assign (arr, clobber),
4314 GSI_SAME_STMT);
4315 }
4316 if (fd->have_pointer_condtemp)
4317 expand_omp_build_assign (&gsi, condtemp, memv, false);
4318 if (fd->have_reductemp)
4319 {
4320 gimple *g = gsi_stmt (gsi);
4321 gsi_remove (&gsi, true);
4322 release_ssa_name (gimple_assign_lhs (g));
4323
4324 entry_bb = region->entry;
4325 gsi = gsi_last_nondebug_bb (entry_bb);
4326
4327 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
4328 }
4329 gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
4330
4331 /* Remove the GIMPLE_OMP_FOR statement. */
4332 gsi_remove (&gsi, true);
4333
4334 if (gsi_end_p (gsif))
4335 gsif = gsi_after_labels (gsi_bb (gsif));
4336 gsi_next (&gsif);
4337
4338 /* Iteration setup for sequential loop goes in L0_BB. */
4339 tree startvar = fd->loop.v;
4340 tree endvar = NULL_TREE;
4341
4342 if (gimple_omp_for_combined_p (fd->for_stmt))
4343 {
4344 gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_FOR
4345 && gimple_omp_for_kind (inner_stmt)
4346 == GF_OMP_FOR_KIND_SIMD);
4347 tree innerc = omp_find_clause (gimple_omp_for_clauses (inner_stmt),
4348 OMP_CLAUSE__LOOPTEMP_);
4349 gcc_assert (innerc);
4350 startvar = OMP_CLAUSE_DECL (innerc);
4351 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4352 OMP_CLAUSE__LOOPTEMP_);
4353 gcc_assert (innerc);
4354 endvar = OMP_CLAUSE_DECL (innerc);
4355 }
4356
4357 gsi = gsi_start_bb (l0_bb);
4358 t = istart0;
4359 if (fd->ordered && fd->collapse == 1)
4360 t = fold_build2 (MULT_EXPR, fd->iter_type, t,
4361 fold_convert (fd->iter_type, fd->loop.step));
4362 else if (bias)
4363 t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
4364 if (fd->ordered && fd->collapse == 1)
4365 {
4366 if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4367 t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
4368 fd->loop.n1, fold_convert (sizetype, t));
4369 else
4370 {
4371 t = fold_convert (TREE_TYPE (startvar), t);
4372 t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
4373 fd->loop.n1, t);
4374 }
4375 }
4376 else
4377 {
4378 if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4379 t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
4380 t = fold_convert (TREE_TYPE (startvar), t);
4381 }
4382 t = force_gimple_operand_gsi (&gsi, t,
4383 DECL_P (startvar)
4384 && TREE_ADDRESSABLE (startvar),
4385 NULL_TREE, false, GSI_CONTINUE_LINKING);
4386 assign_stmt = gimple_build_assign (startvar, t);
4387 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4388 if (cond_var)
4389 {
4390 tree itype = TREE_TYPE (cond_var);
4391 /* For lastprivate(conditional:) itervar, we need some iteration
4392 counter that starts at unsigned non-zero and increases.
4393 Prefer as few IVs as possible, so if we can use startvar
4394 itself, use that, or startvar + constant (those would be
4395 incremented with step), and as last resort use the s0 + 1
4396 incremented by 1. */
4397 if ((fd->ordered && fd->collapse == 1)
4398 || bias
4399 || POINTER_TYPE_P (type)
4400 || TREE_CODE (fd->loop.n1) != INTEGER_CST
4401 || fd->loop.cond_code != LT_EXPR)
4402 t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, istart0),
4403 build_int_cst (itype, 1));
4404 else if (tree_int_cst_sgn (fd->loop.n1) == 1)
4405 t = fold_convert (itype, t);
4406 else
4407 {
4408 tree c = fold_convert (itype, fd->loop.n1);
4409 c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
4410 t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
4411 }
4412 t = force_gimple_operand_gsi (&gsi, t, false,
4413 NULL_TREE, false, GSI_CONTINUE_LINKING);
4414 assign_stmt = gimple_build_assign (cond_var, t);
4415 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4416 }
4417
4418 t = iend0;
4419 if (fd->ordered && fd->collapse == 1)
4420 t = fold_build2 (MULT_EXPR, fd->iter_type, t,
4421 fold_convert (fd->iter_type, fd->loop.step));
4422 else if (bias)
4423 t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
4424 if (fd->ordered && fd->collapse == 1)
4425 {
4426 if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4427 t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
4428 fd->loop.n1, fold_convert (sizetype, t));
4429 else
4430 {
4431 t = fold_convert (TREE_TYPE (startvar), t);
4432 t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
4433 fd->loop.n1, t);
4434 }
4435 }
4436 else
4437 {
4438 if (POINTER_TYPE_P (TREE_TYPE (startvar)))
4439 t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
4440 t = fold_convert (TREE_TYPE (startvar), t);
4441 }
4442 iend = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4443 false, GSI_CONTINUE_LINKING);
4444 if (endvar)
4445 {
4446 assign_stmt = gimple_build_assign (endvar, iend);
4447 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4448 if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (iend)))
4449 assign_stmt = gimple_build_assign (fd->loop.v, iend);
4450 else
4451 assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, iend);
4452 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
4453 }
4454 /* Handle linear clause adjustments. */
4455 tree itercnt = NULL_TREE;
4456 if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
4457 for (tree c = gimple_omp_for_clauses (fd->for_stmt);
4458 c; c = OMP_CLAUSE_CHAIN (c))
4459 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
4460 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
4461 {
4462 tree d = OMP_CLAUSE_DECL (c);
4463 tree t = d, a, dest;
4464 if (omp_privatize_by_reference (t))
4465 t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
4466 tree type = TREE_TYPE (t);
4467 if (POINTER_TYPE_P (type))
4468 type = sizetype;
4469 dest = unshare_expr (t);
4470 tree v = create_tmp_var (TREE_TYPE (t), NULL);
4471 expand_omp_build_assign (&gsif, v, t);
4472 if (itercnt == NULL_TREE)
4473 {
4474 itercnt = startvar;
4475 tree n1 = fd->loop.n1;
4476 if (POINTER_TYPE_P (TREE_TYPE (itercnt)))
4477 {
4478 itercnt
4479 = fold_convert (signed_type_for (TREE_TYPE (itercnt)),
4480 itercnt);
4481 n1 = fold_convert (TREE_TYPE (itercnt), n1);
4482 }
4483 itercnt = fold_build2 (MINUS_EXPR, TREE_TYPE (itercnt),
4484 itercnt, n1);
4485 itercnt = fold_build2 (EXACT_DIV_EXPR, TREE_TYPE (itercnt),
4486 itercnt, fd->loop.step);
4487 itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
4488 NULL_TREE, false,
4489 GSI_CONTINUE_LINKING);
4490 }
4491 a = fold_build2 (MULT_EXPR, type,
4492 fold_convert (type, itercnt),
4493 fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
4494 t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
4495 : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
4496 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4497 false, GSI_CONTINUE_LINKING);
4498 expand_omp_build_assign (&gsi, dest, t, true);
4499 }
4500 if (fd->collapse > 1)
4501 expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
4502
4503 if (fd->ordered)
4504 {
4505 /* Until now, counts array contained number of iterations or
4506 variable containing it for ith loop. From now on, we usually need
4507 those counts only for collapsed loops, and only for the 2nd
4508 till the last collapsed one. Move those one element earlier,
4509 we'll use counts[fd->collapse - 1] for the first source/sink
4510 iteration counter and so on and counts[fd->ordered]
4511 as the array holding the current counter values for
4512 depend(source). For doacross(sink:omp_cur_iteration - 1) we need
4513 the counts from fd->collapse to fd->ordered - 1; make a copy of
4514 those to counts[fd->ordered + 2] and onwards.
4515 counts[fd->ordered + 1] can be a flag whether it is the first
4516 iteration with a new collapsed counter (used only if
4517 fd->ordered > fd->collapse). */
4518 if (fd->ordered > fd->collapse)
4519 memcpy (counts + fd->ordered + 2, counts + fd->collapse,
4520 (fd->ordered - fd->collapse) * sizeof (counts[0]));
4521 if (fd->collapse > 1)
4522 memmove (counts, counts + 1, (fd->collapse - 1) * sizeof (counts[0]));
4523 if (broken_loop)
4524 {
4525 int i;
4526 for (i = fd->collapse; i < fd->ordered; i++)
4527 {
4528 tree type = TREE_TYPE (fd->loops[i].v);
4529 tree this_cond
4530 = fold_build2 (fd->loops[i].cond_code, boolean_type_node,
4531 fold_convert (type, fd->loops[i].n1),
4532 fold_convert (type, fd->loops[i].n2));
4533 if (!integer_onep (this_cond))
4534 break;
4535 }
4536 if (i < fd->ordered)
4537 {
4538 if (entry_bb->loop_father != l0_bb->loop_father)
4539 {
4540 remove_bb_from_loops (l0_bb);
4541 add_bb_to_loop (l0_bb, entry_bb->loop_father);
4542 gcc_assert (single_succ (l0_bb) == l1_bb);
4543 }
4544 cont_bb
4545 = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
4546 add_bb_to_loop (cont_bb, l0_bb->loop_father);
4547 gimple_stmt_iterator gsi = gsi_after_labels (cont_bb);
4548 gimple *g = gimple_build_omp_continue (fd->loop.v, fd->loop.v);
4549 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4550 make_edge (cont_bb, l3_bb, EDGE_FALLTHRU);
4551 make_edge (cont_bb, l1_bb, 0);
4552 l2_bb = create_empty_bb (cont_bb);
4553 broken_loop = false;
4554 }
4555 }
4556 expand_omp_ordered_source_sink (region, fd, counts, cont_bb);
4557 cont_bb = expand_omp_for_ordered_loops (fd, counts, cont_bb, l1_bb,
4558 l0_bb, ordered_lastprivate);
4559 if (counts[fd->collapse - 1])
4560 {
4561 gcc_assert (fd->collapse == 1);
4562 gsi = gsi_last_bb (l0_bb);
4563 expand_omp_build_assign (&gsi, counts[fd->collapse - 1],
4564 istart0, true);
4565 if (cont_bb)
4566 {
4567 gsi = gsi_last_bb (cont_bb);
4568 t = fold_build2 (PLUS_EXPR, fd->iter_type,
4569 counts[fd->collapse - 1],
4570 build_int_cst (fd->iter_type, 1));
4571 expand_omp_build_assign (&gsi, counts[fd->collapse - 1], t);
4572 tree aref = build4 (ARRAY_REF, fd->iter_type,
4573 counts[fd->ordered], size_zero_node,
4574 NULL_TREE, NULL_TREE);
4575 expand_omp_build_assign (&gsi, aref, counts[fd->collapse - 1]);
4576 }
4577 t = counts[fd->collapse - 1];
4578 }
4579 else if (fd->collapse > 1)
4580 t = fd->loop.v;
4581 else
4582 {
4583 t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
4584 fd->loops[0].v, fd->loops[0].n1);
4585 t = fold_convert (fd->iter_type, t);
4586 }
4587 gsi = gsi_last_bb (l0_bb);
4588 tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
4589 size_zero_node, NULL_TREE, NULL_TREE);
4590 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4591 false, GSI_CONTINUE_LINKING);
4592 expand_omp_build_assign (&gsi, aref, t, true);
4593 }
4594
4595 if (!broken_loop)
4596 {
4597 /* Code to control the increment and predicate for the sequential
4598 loop goes in the CONT_BB. */
4599 gsi = gsi_last_nondebug_bb (cont_bb);
4600 gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
4601 gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
4602 vmain = gimple_omp_continue_control_use (cont_stmt);
4603 vback = gimple_omp_continue_control_def (cont_stmt);
4604
4605 if (cond_var)
4606 {
4607 tree itype = TREE_TYPE (cond_var);
4608 tree t2;
4609 if ((fd->ordered && fd->collapse == 1)
4610 || bias
4611 || POINTER_TYPE_P (type)
4612 || TREE_CODE (fd->loop.n1) != INTEGER_CST
4613 || fd->loop.cond_code != LT_EXPR)
4614 t2 = build_int_cst (itype, 1);
4615 else
4616 t2 = fold_convert (itype, fd->loop.step);
4617 t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
4618 t2 = force_gimple_operand_gsi (&gsi, t2, false,
4619 NULL_TREE, true, GSI_SAME_STMT);
4620 assign_stmt = gimple_build_assign (cond_var, t2);
4621 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
4622 }
4623
4624 if (!gimple_omp_for_combined_p (fd->for_stmt))
4625 {
4626 if (POINTER_TYPE_P (type))
4627 t = fold_build_pointer_plus (vmain, fd->loop.step);
4628 else
4629 t = fold_build2 (PLUS_EXPR, type, vmain, fd->loop.step);
4630 t = force_gimple_operand_gsi (&gsi, t,
4631 DECL_P (vback)
4632 && TREE_ADDRESSABLE (vback),
4633 NULL_TREE, true, GSI_SAME_STMT);
4634 assign_stmt = gimple_build_assign (vback, t);
4635 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
4636
4637 if (fd->ordered && counts[fd->collapse - 1] == NULL_TREE)
4638 {
4639 tree tem;
4640 if (fd->collapse > 1)
4641 tem = fd->loop.v;
4642 else
4643 {
4644 tem = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
4645 fd->loops[0].v, fd->loops[0].n1);
4646 tem = fold_convert (fd->iter_type, tem);
4647 }
4648 tree aref = build4 (ARRAY_REF, fd->iter_type,
4649 counts[fd->ordered], size_zero_node,
4650 NULL_TREE, NULL_TREE);
4651 tem = force_gimple_operand_gsi (&gsi, tem, true, NULL_TREE,
4652 true, GSI_SAME_STMT);
4653 expand_omp_build_assign (&gsi, aref, tem);
4654 }
4655
4656 t = build2 (fd->loop.cond_code, boolean_type_node,
4657 DECL_P (vback) && TREE_ADDRESSABLE (vback) ? t : vback,
4658 iend);
4659 gcond *cond_stmt = gimple_build_cond_empty (t);
4660 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
4661 }
4662
4663 /* Remove GIMPLE_OMP_CONTINUE. */
4664 gsi_remove (&gsi, true);
4665
4666 if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
4667 collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, l1_bb);
4668
4669 /* Emit code to get the next parallel iteration in L2_BB. */
4670 gsi = gsi_start_bb (l2_bb);
4671
4672 t = build_call_expr (builtin_decl_explicit (next_fn), 2,
4673 build_fold_addr_expr (istart0),
4674 build_fold_addr_expr (iend0));
4675 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
4676 false, GSI_CONTINUE_LINKING);
4677 if (TREE_TYPE (t) != boolean_type_node)
4678 t = fold_build2 (NE_EXPR, boolean_type_node,
4679 t, build_int_cst (TREE_TYPE (t), 0));
4680 gcond *cond_stmt = gimple_build_cond_empty (t);
4681 gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
4682 }
4683
4684 /* Add the loop cleanup function. */
4685 gsi = gsi_last_nondebug_bb (exit_bb);
4686 if (gimple_omp_return_nowait_p (gsi_stmt (gsi)))
4687 t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
4688 else if (gimple_omp_return_lhs (gsi_stmt (gsi)))
4689 t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
4690 else
4691 t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
4692 gcall *call_stmt = gimple_build_call (t, 0);
4693 if (fd->ordered)
4694 {
4695 tree arr = counts[fd->ordered];
4696 tree clobber = build_clobber (TREE_TYPE (arr));
4697 gsi_insert_after (&gsi, gimple_build_assign (arr, clobber),
4698 GSI_SAME_STMT);
4699 }
4700 if (gimple_omp_return_lhs (gsi_stmt (gsi)))
4701 {
4702 gimple_call_set_lhs (call_stmt, gimple_omp_return_lhs (gsi_stmt (gsi)));
4703 if (fd->have_reductemp)
4704 {
4705 gimple *g = gimple_build_assign (reductions, NOP_EXPR,
4706 gimple_call_lhs (call_stmt));
4707 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
4708 }
4709 }
4710 gsi_insert_after (&gsi, call_stmt, GSI_SAME_STMT);
4711 gsi_remove (&gsi, true);
4712
4713 /* Connect the new blocks. */
4714 find_edge (entry_bb, l0_bb)->flags = EDGE_TRUE_VALUE;
4715 find_edge (entry_bb, l3_bb)->flags = EDGE_FALSE_VALUE;
4716
4717 if (!broken_loop)
4718 {
4719 gimple_seq phis;
4720
4721 e = find_edge (cont_bb, l3_bb);
4722 ne = make_edge (l2_bb, l3_bb, EDGE_FALSE_VALUE);
4723
4724 phis = phi_nodes (l3_bb);
4725 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
4726 {
4727 gimple *phi = gsi_stmt (gsi);
4728 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, ne),
4729 PHI_ARG_DEF_FROM_EDGE (phi, e));
4730 }
4731 remove_edge (e);
4732
4733 make_edge (cont_bb, l2_bb, EDGE_FALSE_VALUE);
4734 e = find_edge (cont_bb, l1_bb);
4735 if (e == NULL)
4736 {
4737 e = BRANCH_EDGE (cont_bb);
4738 gcc_assert (single_succ (e->dest) == l1_bb);
4739 }
4740 if (gimple_omp_for_combined_p (fd->for_stmt))
4741 {
4742 remove_edge (e);
4743 e = NULL;
4744 }
4745 else if (fd->collapse > 1)
4746 {
4747 remove_edge (e);
4748 e = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
4749 }
4750 else
4751 e->flags = EDGE_TRUE_VALUE;
4752 if (e)
4753 {
4754 e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
4755 find_edge (cont_bb, l2_bb)->probability = e->probability.invert ();
4756 }
4757 else
4758 {
4759 e = find_edge (cont_bb, l2_bb);
4760 e->flags = EDGE_FALLTHRU;
4761 }
4762 make_edge (l2_bb, l0_bb, EDGE_TRUE_VALUE);
4763
4764 if (gimple_in_ssa_p (cfun))
4765 {
4766 /* Add phis to the outer loop that connect to the phis in the inner,
4767 original loop, and move the loop entry value of the inner phi to
4768 the loop entry value of the outer phi. */
4769 gphi_iterator psi;
4770 for (psi = gsi_start_phis (l3_bb); !gsi_end_p (psi); gsi_next (&psi))
4771 {
4772 location_t locus;
4773 gphi *nphi;
4774 gphi *exit_phi = psi.phi ();
4775
4776 if (virtual_operand_p (gimple_phi_result (exit_phi)))
4777 continue;
4778
4779 edge l2_to_l3 = find_edge (l2_bb, l3_bb);
4780 tree exit_res = PHI_ARG_DEF_FROM_EDGE (exit_phi, l2_to_l3);
4781
4782 basic_block latch = BRANCH_EDGE (cont_bb)->dest;
4783 edge latch_to_l1 = find_edge (latch, l1_bb);
4784 gphi *inner_phi
4785 = find_phi_with_arg_on_edge (exit_res, latch_to_l1);
4786
4787 tree t = gimple_phi_result (exit_phi);
4788 tree new_res = copy_ssa_name (t, NULL);
4789 nphi = create_phi_node (new_res, l0_bb);
4790
4791 edge l0_to_l1 = find_edge (l0_bb, l1_bb);
4792 t = PHI_ARG_DEF_FROM_EDGE (inner_phi, l0_to_l1);
4793 locus = gimple_phi_arg_location_from_edge (inner_phi, l0_to_l1);
4794 edge entry_to_l0 = find_edge (entry_bb, l0_bb);
4795 add_phi_arg (nphi, t, entry_to_l0, locus);
4796
4797 edge l2_to_l0 = find_edge (l2_bb, l0_bb);
4798 add_phi_arg (nphi, exit_res, l2_to_l0, UNKNOWN_LOCATION);
4799
4800 add_phi_arg (inner_phi, new_res, l0_to_l1, UNKNOWN_LOCATION);
4801 }
4802 }
4803
4804 set_immediate_dominator (CDI_DOMINATORS, l2_bb,
4805 recompute_dominator (CDI_DOMINATORS, l2_bb));
4806 set_immediate_dominator (CDI_DOMINATORS, l3_bb,
4807 recompute_dominator (CDI_DOMINATORS, l3_bb));
4808 set_immediate_dominator (CDI_DOMINATORS, l0_bb,
4809 recompute_dominator (CDI_DOMINATORS, l0_bb));
4810 set_immediate_dominator (CDI_DOMINATORS, l1_bb,
4811 recompute_dominator (CDI_DOMINATORS, l1_bb));
4812
4813 /* We enter expand_omp_for_generic with a loop. This original loop may
4814 have its own loop struct, or it may be part of an outer loop struct
4815 (which may be the fake loop). */
4816 class loop *outer_loop = entry_bb->loop_father;
4817 bool orig_loop_has_loop_struct = l1_bb->loop_father != outer_loop;
4818
4819 add_bb_to_loop (l2_bb, outer_loop);
4820
4821 /* We've added a new loop around the original loop. Allocate the
4822 corresponding loop struct. */
4823 class loop *new_loop = alloc_loop ();
4824 new_loop->header = l0_bb;
4825 new_loop->latch = l2_bb;
4826 add_loop (new_loop, outer_loop);
4827
4828 /* Allocate a loop structure for the original loop unless we already
4829 had one. */
4830 if (!orig_loop_has_loop_struct
4831 && !gimple_omp_for_combined_p (fd->for_stmt))
4832 {
4833 class loop *orig_loop = alloc_loop ();
4834 orig_loop->header = l1_bb;
4835 /* The loop may have multiple latches. */
4836 add_loop (orig_loop, new_loop);
4837 }
4838 }
4839 }
4840
4841 /* Helper function for expand_omp_for_static_nochunk. If PTR is NULL,
4842 compute needed allocation size. If !ALLOC of team allocations,
4843 if ALLOC of thread allocation. SZ is the initial needed size for
4844 other purposes, ALLOC_ALIGN guaranteed alignment of allocation in bytes,
4845 CNT number of elements of each array, for !ALLOC this is
4846 omp_get_num_threads (), for ALLOC number of iterations handled by the
4847 current thread. If PTR is non-NULL, it is the start of the allocation
4848 and this routine shall assign to OMP_CLAUSE_DECL (c) of those _scantemp_
4849 clauses pointers to the corresponding arrays. */
4850
4851 static tree
4852 expand_omp_scantemp_alloc (tree clauses, tree ptr, unsigned HOST_WIDE_INT sz,
4853 unsigned HOST_WIDE_INT alloc_align, tree cnt,
4854 gimple_stmt_iterator *gsi, bool alloc)
4855 {
4856 tree eltsz = NULL_TREE;
4857 unsigned HOST_WIDE_INT preval = 0;
4858 if (ptr && sz)
4859 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
4860 ptr, size_int (sz));
4861 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
4862 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
4863 && !OMP_CLAUSE__SCANTEMP__CONTROL (c)
4864 && (!OMP_CLAUSE__SCANTEMP__ALLOC (c)) != alloc)
4865 {
4866 tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
4867 unsigned HOST_WIDE_INT al = TYPE_ALIGN_UNIT (pointee_type);
4868 if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
4869 {
4870 unsigned HOST_WIDE_INT szl
4871 = tree_to_uhwi (TYPE_SIZE_UNIT (pointee_type));
4872 szl = least_bit_hwi (szl);
4873 if (szl)
4874 al = MIN (al, szl);
4875 }
4876 if (ptr == NULL_TREE)
4877 {
4878 if (eltsz == NULL_TREE)
4879 eltsz = TYPE_SIZE_UNIT (pointee_type);
4880 else
4881 eltsz = size_binop (PLUS_EXPR, eltsz,
4882 TYPE_SIZE_UNIT (pointee_type));
4883 }
4884 if (preval == 0 && al <= alloc_align)
4885 {
4886 unsigned HOST_WIDE_INT diff = ROUND_UP (sz, al) - sz;
4887 sz += diff;
4888 if (diff && ptr)
4889 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
4890 ptr, size_int (diff));
4891 }
4892 else if (al > preval)
4893 {
4894 if (ptr)
4895 {
4896 ptr = fold_convert (pointer_sized_int_node, ptr);
4897 ptr = fold_build2 (PLUS_EXPR, pointer_sized_int_node, ptr,
4898 build_int_cst (pointer_sized_int_node,
4899 al - 1));
4900 ptr = fold_build2 (BIT_AND_EXPR, pointer_sized_int_node, ptr,
4901 build_int_cst (pointer_sized_int_node,
4902 -(HOST_WIDE_INT) al));
4903 ptr = fold_convert (ptr_type_node, ptr);
4904 }
4905 else
4906 sz += al - 1;
4907 }
4908 if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
4909 preval = al;
4910 else
4911 preval = 1;
4912 if (ptr)
4913 {
4914 expand_omp_build_assign (gsi, OMP_CLAUSE_DECL (c), ptr, false);
4915 ptr = OMP_CLAUSE_DECL (c);
4916 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
4917 size_binop (MULT_EXPR, cnt,
4918 TYPE_SIZE_UNIT (pointee_type)));
4919 }
4920 }
4921
4922 if (ptr == NULL_TREE)
4923 {
4924 eltsz = size_binop (MULT_EXPR, eltsz, cnt);
4925 if (sz)
4926 eltsz = size_binop (PLUS_EXPR, eltsz, size_int (sz));
4927 return eltsz;
4928 }
4929 else
4930 return ptr;
4931 }
4932
4933 /* Return the last _looptemp_ clause if one has been created for
4934 lastprivate on distribute parallel for{, simd} or taskloop.
4935 FD is the loop data and INNERC should be the second _looptemp_
4936 clause (the one holding the end of the range).
4937 This is followed by collapse - 1 _looptemp_ clauses for the
4938 counts[1] and up, and for triangular loops followed by 4
4939 further _looptemp_ clauses (one for counts[0], one first_inner_iterations,
4940 one factor and one adjn1). After this there is optionally one
4941 _looptemp_ clause that this function returns. */
4942
4943 static tree
4944 find_lastprivate_looptemp (struct omp_for_data *fd, tree innerc)
4945 {
4946 gcc_assert (innerc);
4947 int count = fd->collapse - 1;
4948 if (fd->non_rect
4949 && fd->last_nonrect == fd->first_nonrect + 1
4950 && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
4951 count += 4;
4952 for (int i = 0; i < count; i++)
4953 {
4954 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4955 OMP_CLAUSE__LOOPTEMP_);
4956 gcc_assert (innerc);
4957 }
4958 return omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
4959 OMP_CLAUSE__LOOPTEMP_);
4960 }
4961
4962 /* A subroutine of expand_omp_for. Generate code for a parallel
4963 loop with static schedule and no specified chunk size. Given
4964 parameters:
4965
4966 for (V = N1; V cond N2; V += STEP) BODY;
4967
4968 where COND is "<" or ">", we generate pseudocode
4969
4970 if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
4971 if (cond is <)
4972 adj = STEP - 1;
4973 else
4974 adj = STEP + 1;
4975 if ((__typeof (V)) -1 > 0 && cond is >)
4976 n = -(adj + N2 - N1) / -STEP;
4977 else
4978 n = (adj + N2 - N1) / STEP;
4979 q = n / nthreads;
4980 tt = n % nthreads;
4981 if (threadid < tt) goto L3; else goto L4;
4982 L3:
4983 tt = 0;
4984 q = q + 1;
4985 L4:
4986 s0 = q * threadid + tt;
4987 e0 = s0 + q;
4988 V = s0 * STEP + N1;
4989 if (s0 >= e0) goto L2; else goto L0;
4990 L0:
4991 e = e0 * STEP + N1;
4992 L1:
4993 BODY;
4994 V += STEP;
4995 if (V cond e) goto L1;
4996 L2:
4997 */
4998
4999 static void
5000 expand_omp_for_static_nochunk (struct omp_region *region,
5001 struct omp_for_data *fd,
5002 gimple *inner_stmt)
5003 {
5004 tree n, q, s0, e0, e, t, tt, nthreads = NULL_TREE, threadid;
5005 tree type, itype, vmain, vback;
5006 basic_block entry_bb, second_bb, third_bb, exit_bb, seq_start_bb;
5007 basic_block body_bb, cont_bb, collapse_bb = NULL;
5008 basic_block fin_bb, fourth_bb = NULL, fifth_bb = NULL, sixth_bb = NULL;
5009 basic_block exit1_bb = NULL, exit2_bb = NULL, exit3_bb = NULL;
5010 gimple_stmt_iterator gsi, gsip;
5011 edge ep;
5012 bool broken_loop = region->cont == NULL;
5013 tree *counts = NULL;
5014 tree n1, n2, step;
5015 tree reductions = NULL_TREE;
5016 tree cond_var = NULL_TREE, condtemp = NULL_TREE;
5017
5018 itype = type = TREE_TYPE (fd->loop.v);
5019 if (POINTER_TYPE_P (type))
5020 itype = signed_type_for (type);
5021
5022 entry_bb = region->entry;
5023 cont_bb = region->cont;
5024 gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
5025 fin_bb = BRANCH_EDGE (entry_bb)->dest;
5026 gcc_assert (broken_loop
5027 || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
5028 seq_start_bb = split_edge (FALLTHRU_EDGE (entry_bb));
5029 body_bb = single_succ (seq_start_bb);
5030 if (!broken_loop)
5031 {
5032 gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
5033 || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
5034 gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
5035 }
5036 exit_bb = region->exit;
5037
5038 /* Iteration space partitioning goes in ENTRY_BB. */
5039 gsi = gsi_last_nondebug_bb (entry_bb);
5040 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5041 gsip = gsi;
5042 gsi_prev (&gsip);
5043
5044 if (fd->collapse > 1)
5045 {
5046 int first_zero_iter = -1, dummy = -1;
5047 basic_block l2_dom_bb = NULL, dummy_bb = NULL;
5048
5049 counts = XALLOCAVEC (tree, fd->collapse);
5050 expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
5051 fin_bb, first_zero_iter,
5052 dummy_bb, dummy, l2_dom_bb);
5053 t = NULL_TREE;
5054 }
5055 else if (gimple_omp_for_combined_into_p (fd->for_stmt))
5056 t = integer_one_node;
5057 else
5058 t = fold_binary (fd->loop.cond_code, boolean_type_node,
5059 fold_convert (type, fd->loop.n1),
5060 fold_convert (type, fd->loop.n2));
5061 if (fd->collapse == 1
5062 && TYPE_UNSIGNED (type)
5063 && (t == NULL_TREE || !integer_onep (t)))
5064 {
5065 n1 = fold_convert (type, unshare_expr (fd->loop.n1));
5066 n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
5067 true, GSI_SAME_STMT);
5068 n2 = fold_convert (type, unshare_expr (fd->loop.n2));
5069 n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
5070 true, GSI_SAME_STMT);
5071 gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
5072 n1, n2);
5073 ep = split_block (entry_bb, cond_stmt);
5074 ep->flags = EDGE_TRUE_VALUE;
5075 entry_bb = ep->dest;
5076 ep->probability = profile_probability::very_likely ();
5077 ep = make_edge (ep->src, fin_bb, EDGE_FALSE_VALUE);
5078 ep->probability = profile_probability::very_unlikely ();
5079 if (gimple_in_ssa_p (cfun))
5080 {
5081 int dest_idx = find_edge (entry_bb, fin_bb)->dest_idx;
5082 for (gphi_iterator gpi = gsi_start_phis (fin_bb);
5083 !gsi_end_p (gpi); gsi_next (&gpi))
5084 {
5085 gphi *phi = gpi.phi ();
5086 add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
5087 ep, UNKNOWN_LOCATION);
5088 }
5089 }
5090 gsi = gsi_last_bb (entry_bb);
5091 }
5092
5093 if (fd->lastprivate_conditional)
5094 {
5095 tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5096 tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
5097 if (fd->have_pointer_condtemp)
5098 condtemp = OMP_CLAUSE_DECL (c);
5099 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
5100 cond_var = OMP_CLAUSE_DECL (c);
5101 }
5102 if (fd->have_reductemp
5103 /* For scan, we don't want to reinitialize condtemp before the
5104 second loop. */
5105 || (fd->have_pointer_condtemp && !fd->have_scantemp)
5106 || fd->have_nonctrl_scantemp)
5107 {
5108 tree t1 = build_int_cst (long_integer_type_node, 0);
5109 tree t2 = build_int_cst (long_integer_type_node, 1);
5110 tree t3 = build_int_cstu (long_integer_type_node,
5111 (HOST_WIDE_INT_1U << 31) + 1);
5112 tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5113 gimple_stmt_iterator gsi2 = gsi_none ();
5114 gimple *g = NULL;
5115 tree mem = null_pointer_node, memv = NULL_TREE;
5116 unsigned HOST_WIDE_INT condtemp_sz = 0;
5117 unsigned HOST_WIDE_INT alloc_align = 0;
5118 if (fd->have_reductemp)
5119 {
5120 gcc_assert (!fd->have_nonctrl_scantemp);
5121 tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
5122 reductions = OMP_CLAUSE_DECL (c);
5123 gcc_assert (TREE_CODE (reductions) == SSA_NAME);
5124 g = SSA_NAME_DEF_STMT (reductions);
5125 reductions = gimple_assign_rhs1 (g);
5126 OMP_CLAUSE_DECL (c) = reductions;
5127 gsi2 = gsi_for_stmt (g);
5128 }
5129 else
5130 {
5131 if (gsi_end_p (gsip))
5132 gsi2 = gsi_after_labels (region->entry);
5133 else
5134 gsi2 = gsip;
5135 reductions = null_pointer_node;
5136 }
5137 if (fd->have_pointer_condtemp || fd->have_nonctrl_scantemp)
5138 {
5139 tree type;
5140 if (fd->have_pointer_condtemp)
5141 type = TREE_TYPE (condtemp);
5142 else
5143 type = ptr_type_node;
5144 memv = create_tmp_var (type);
5145 TREE_ADDRESSABLE (memv) = 1;
5146 unsigned HOST_WIDE_INT sz = 0;
5147 tree size = NULL_TREE;
5148 if (fd->have_pointer_condtemp)
5149 {
5150 sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
5151 sz *= fd->lastprivate_conditional;
5152 condtemp_sz = sz;
5153 }
5154 if (fd->have_nonctrl_scantemp)
5155 {
5156 nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5157 gimple *g = gimple_build_call (nthreads, 0);
5158 nthreads = create_tmp_var (integer_type_node);
5159 gimple_call_set_lhs (g, nthreads);
5160 gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
5161 nthreads = fold_convert (sizetype, nthreads);
5162 alloc_align = TYPE_ALIGN_UNIT (long_long_integer_type_node);
5163 size = expand_omp_scantemp_alloc (clauses, NULL_TREE, sz,
5164 alloc_align, nthreads, NULL,
5165 false);
5166 size = fold_convert (type, size);
5167 }
5168 else
5169 size = build_int_cst (type, sz);
5170 expand_omp_build_assign (&gsi2, memv, size, false);
5171 mem = build_fold_addr_expr (memv);
5172 }
5173 tree t
5174 = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
5175 9, t1, t2, t2, t3, t1, null_pointer_node,
5176 null_pointer_node, reductions, mem);
5177 force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
5178 true, GSI_SAME_STMT);
5179 if (fd->have_pointer_condtemp)
5180 expand_omp_build_assign (&gsi2, condtemp, memv, false);
5181 if (fd->have_nonctrl_scantemp)
5182 {
5183 tree ptr = fd->have_pointer_condtemp ? condtemp : memv;
5184 expand_omp_scantemp_alloc (clauses, ptr, condtemp_sz,
5185 alloc_align, nthreads, &gsi2, false);
5186 }
5187 if (fd->have_reductemp)
5188 {
5189 gsi_remove (&gsi2, true);
5190 release_ssa_name (gimple_assign_lhs (g));
5191 }
5192 }
5193 switch (gimple_omp_for_kind (fd->for_stmt))
5194 {
5195 case GF_OMP_FOR_KIND_FOR:
5196 nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5197 threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
5198 break;
5199 case GF_OMP_FOR_KIND_DISTRIBUTE:
5200 nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_TEAMS);
5201 threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_TEAM_NUM);
5202 break;
5203 default:
5204 gcc_unreachable ();
5205 }
5206 nthreads = build_call_expr (nthreads, 0);
5207 nthreads = fold_convert (itype, nthreads);
5208 nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE,
5209 true, GSI_SAME_STMT);
5210 threadid = build_call_expr (threadid, 0);
5211 threadid = fold_convert (itype, threadid);
5212 threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE,
5213 true, GSI_SAME_STMT);
5214
5215 n1 = fd->loop.n1;
5216 n2 = fd->loop.n2;
5217 step = fd->loop.step;
5218 if (gimple_omp_for_combined_into_p (fd->for_stmt))
5219 {
5220 tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
5221 OMP_CLAUSE__LOOPTEMP_);
5222 gcc_assert (innerc);
5223 n1 = OMP_CLAUSE_DECL (innerc);
5224 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5225 OMP_CLAUSE__LOOPTEMP_);
5226 gcc_assert (innerc);
5227 n2 = OMP_CLAUSE_DECL (innerc);
5228 }
5229 n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
5230 true, NULL_TREE, true, GSI_SAME_STMT);
5231 n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
5232 true, NULL_TREE, true, GSI_SAME_STMT);
5233 step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
5234 true, NULL_TREE, true, GSI_SAME_STMT);
5235
5236 t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
5237 t = fold_build2 (PLUS_EXPR, itype, step, t);
5238 t = fold_build2 (PLUS_EXPR, itype, t, n2);
5239 t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
5240 if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
5241 t = fold_build2 (TRUNC_DIV_EXPR, itype,
5242 fold_build1 (NEGATE_EXPR, itype, t),
5243 fold_build1 (NEGATE_EXPR, itype, step));
5244 else
5245 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
5246 t = fold_convert (itype, t);
5247 n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5248
5249 q = create_tmp_reg (itype, "q");
5250 t = fold_build2 (TRUNC_DIV_EXPR, itype, n, nthreads);
5251 t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
5252 gsi_insert_before (&gsi, gimple_build_assign (q, t), GSI_SAME_STMT);
5253
5254 tt = create_tmp_reg (itype, "tt");
5255 t = fold_build2 (TRUNC_MOD_EXPR, itype, n, nthreads);
5256 t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
5257 gsi_insert_before (&gsi, gimple_build_assign (tt, t), GSI_SAME_STMT);
5258
5259 t = build2 (LT_EXPR, boolean_type_node, threadid, tt);
5260 gcond *cond_stmt = gimple_build_cond_empty (t);
5261 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
5262
5263 second_bb = split_block (entry_bb, cond_stmt)->dest;
5264 gsi = gsi_last_nondebug_bb (second_bb);
5265 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5266
5267 gsi_insert_before (&gsi, gimple_build_assign (tt, build_int_cst (itype, 0)),
5268 GSI_SAME_STMT);
5269 gassign *assign_stmt
5270 = gimple_build_assign (q, PLUS_EXPR, q, build_int_cst (itype, 1));
5271 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5272
5273 third_bb = split_block (second_bb, assign_stmt)->dest;
5274 gsi = gsi_last_nondebug_bb (third_bb);
5275 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5276
5277 if (fd->have_nonctrl_scantemp)
5278 {
5279 tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5280 tree controlp = NULL_TREE, controlb = NULL_TREE;
5281 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5282 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5283 && OMP_CLAUSE__SCANTEMP__CONTROL (c))
5284 {
5285 if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
5286 controlb = OMP_CLAUSE_DECL (c);
5287 else
5288 controlp = OMP_CLAUSE_DECL (c);
5289 if (controlb && controlp)
5290 break;
5291 }
5292 gcc_assert (controlp && controlb);
5293 tree cnt = create_tmp_var (sizetype);
5294 gimple *g = gimple_build_assign (cnt, NOP_EXPR, q);
5295 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5296 unsigned HOST_WIDE_INT alloc_align = TYPE_ALIGN_UNIT (ptr_type_node);
5297 tree sz = expand_omp_scantemp_alloc (clauses, NULL_TREE, 0,
5298 alloc_align, cnt, NULL, true);
5299 tree size = create_tmp_var (sizetype);
5300 expand_omp_build_assign (&gsi, size, sz, false);
5301 tree cmp = fold_build2 (GT_EXPR, boolean_type_node,
5302 size, size_int (16384));
5303 expand_omp_build_assign (&gsi, controlb, cmp);
5304 g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
5305 NULL_TREE, NULL_TREE);
5306 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5307 fourth_bb = split_block (third_bb, g)->dest;
5308 gsi = gsi_last_nondebug_bb (fourth_bb);
5309 /* FIXME: Once we have allocators, this should use allocator. */
5310 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_MALLOC), 1, size);
5311 gimple_call_set_lhs (g, controlp);
5312 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5313 expand_omp_scantemp_alloc (clauses, controlp, 0, alloc_align, cnt,
5314 &gsi, true);
5315 gsi_prev (&gsi);
5316 g = gsi_stmt (gsi);
5317 fifth_bb = split_block (fourth_bb, g)->dest;
5318 gsi = gsi_last_nondebug_bb (fifth_bb);
5319
5320 g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
5321 gimple_call_set_lhs (g, controlp);
5322 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5323 tree alloca_decl = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5324 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5325 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5326 && OMP_CLAUSE__SCANTEMP__ALLOC (c))
5327 {
5328 tree tmp = create_tmp_var (sizetype);
5329 tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
5330 g = gimple_build_assign (tmp, MULT_EXPR, cnt,
5331 TYPE_SIZE_UNIT (pointee_type));
5332 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5333 g = gimple_build_call (alloca_decl, 2, tmp,
5334 size_int (TYPE_ALIGN (pointee_type)));
5335 gimple_call_set_lhs (g, OMP_CLAUSE_DECL (c));
5336 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5337 }
5338
5339 sixth_bb = split_block (fifth_bb, g)->dest;
5340 gsi = gsi_last_nondebug_bb (sixth_bb);
5341 }
5342
5343 t = build2 (MULT_EXPR, itype, q, threadid);
5344 t = build2 (PLUS_EXPR, itype, t, tt);
5345 s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5346
5347 t = fold_build2 (PLUS_EXPR, itype, s0, q);
5348 e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
5349
5350 t = build2 (GE_EXPR, boolean_type_node, s0, e0);
5351 gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
5352
5353 /* Remove the GIMPLE_OMP_FOR statement. */
5354 gsi_remove (&gsi, true);
5355
5356 /* Setup code for sequential iteration goes in SEQ_START_BB. */
5357 gsi = gsi_start_bb (seq_start_bb);
5358
5359 tree startvar = fd->loop.v;
5360 tree endvar = NULL_TREE;
5361
5362 if (gimple_omp_for_combined_p (fd->for_stmt))
5363 {
5364 tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
5365 ? gimple_omp_parallel_clauses (inner_stmt)
5366 : gimple_omp_for_clauses (inner_stmt);
5367 tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
5368 gcc_assert (innerc);
5369 startvar = OMP_CLAUSE_DECL (innerc);
5370 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5371 OMP_CLAUSE__LOOPTEMP_);
5372 gcc_assert (innerc);
5373 endvar = OMP_CLAUSE_DECL (innerc);
5374 if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
5375 && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
5376 {
5377 innerc = find_lastprivate_looptemp (fd, innerc);
5378 if (innerc)
5379 {
5380 /* If needed (distribute parallel for with lastprivate),
5381 propagate down the total number of iterations. */
5382 tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
5383 fd->loop.n2);
5384 t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
5385 GSI_CONTINUE_LINKING);
5386 assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
5387 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5388 }
5389 }
5390 }
5391 t = fold_convert (itype, s0);
5392 t = fold_build2 (MULT_EXPR, itype, t, step);
5393 if (POINTER_TYPE_P (type))
5394 {
5395 t = fold_build_pointer_plus (n1, t);
5396 if (!POINTER_TYPE_P (TREE_TYPE (startvar))
5397 && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
5398 t = fold_convert (signed_type_for (type), t);
5399 }
5400 else
5401 t = fold_build2 (PLUS_EXPR, type, t, n1);
5402 t = fold_convert (TREE_TYPE (startvar), t);
5403 t = force_gimple_operand_gsi (&gsi, t,
5404 DECL_P (startvar)
5405 && TREE_ADDRESSABLE (startvar),
5406 NULL_TREE, false, GSI_CONTINUE_LINKING);
5407 assign_stmt = gimple_build_assign (startvar, t);
5408 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5409 if (cond_var)
5410 {
5411 tree itype = TREE_TYPE (cond_var);
5412 /* For lastprivate(conditional:) itervar, we need some iteration
5413 counter that starts at unsigned non-zero and increases.
5414 Prefer as few IVs as possible, so if we can use startvar
5415 itself, use that, or startvar + constant (those would be
5416 incremented with step), and as last resort use the s0 + 1
5417 incremented by 1. */
5418 if (POINTER_TYPE_P (type)
5419 || TREE_CODE (n1) != INTEGER_CST
5420 || fd->loop.cond_code != LT_EXPR)
5421 t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
5422 build_int_cst (itype, 1));
5423 else if (tree_int_cst_sgn (n1) == 1)
5424 t = fold_convert (itype, t);
5425 else
5426 {
5427 tree c = fold_convert (itype, n1);
5428 c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
5429 t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
5430 }
5431 t = force_gimple_operand_gsi (&gsi, t, false,
5432 NULL_TREE, false, GSI_CONTINUE_LINKING);
5433 assign_stmt = gimple_build_assign (cond_var, t);
5434 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5435 }
5436
5437 t = fold_convert (itype, e0);
5438 t = fold_build2 (MULT_EXPR, itype, t, step);
5439 if (POINTER_TYPE_P (type))
5440 {
5441 t = fold_build_pointer_plus (n1, t);
5442 if (!POINTER_TYPE_P (TREE_TYPE (startvar))
5443 && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
5444 t = fold_convert (signed_type_for (type), t);
5445 }
5446 else
5447 t = fold_build2 (PLUS_EXPR, type, t, n1);
5448 t = fold_convert (TREE_TYPE (startvar), t);
5449 e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
5450 false, GSI_CONTINUE_LINKING);
5451 if (endvar)
5452 {
5453 assign_stmt = gimple_build_assign (endvar, e);
5454 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5455 if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
5456 assign_stmt = gimple_build_assign (fd->loop.v, e);
5457 else
5458 assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
5459 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
5460 }
5461 /* Handle linear clause adjustments. */
5462 tree itercnt = NULL_TREE;
5463 tree *nonrect_bounds = NULL;
5464 if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
5465 for (tree c = gimple_omp_for_clauses (fd->for_stmt);
5466 c; c = OMP_CLAUSE_CHAIN (c))
5467 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5468 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
5469 {
5470 tree d = OMP_CLAUSE_DECL (c);
5471 tree t = d, a, dest;
5472 if (omp_privatize_by_reference (t))
5473 t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
5474 if (itercnt == NULL_TREE)
5475 {
5476 if (gimple_omp_for_combined_into_p (fd->for_stmt))
5477 {
5478 itercnt = fold_build2 (MINUS_EXPR, itype,
5479 fold_convert (itype, n1),
5480 fold_convert (itype, fd->loop.n1));
5481 itercnt = fold_build2 (EXACT_DIV_EXPR, itype, itercnt, step);
5482 itercnt = fold_build2 (PLUS_EXPR, itype, itercnt, s0);
5483 itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
5484 NULL_TREE, false,
5485 GSI_CONTINUE_LINKING);
5486 }
5487 else
5488 itercnt = s0;
5489 }
5490 tree type = TREE_TYPE (t);
5491 if (POINTER_TYPE_P (type))
5492 type = sizetype;
5493 a = fold_build2 (MULT_EXPR, type,
5494 fold_convert (type, itercnt),
5495 fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
5496 dest = unshare_expr (t);
5497 t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
5498 : POINTER_PLUS_EXPR, TREE_TYPE (t), t, a);
5499 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
5500 false, GSI_CONTINUE_LINKING);
5501 expand_omp_build_assign (&gsi, dest, t, true);
5502 }
5503 if (fd->collapse > 1)
5504 {
5505 if (fd->non_rect)
5506 {
5507 nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
5508 memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
5509 }
5510 expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
5511 startvar);
5512 }
5513
5514 if (!broken_loop)
5515 {
5516 /* The code controlling the sequential loop replaces the
5517 GIMPLE_OMP_CONTINUE. */
5518 gsi = gsi_last_nondebug_bb (cont_bb);
5519 gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
5520 gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
5521 vmain = gimple_omp_continue_control_use (cont_stmt);
5522 vback = gimple_omp_continue_control_def (cont_stmt);
5523
5524 if (cond_var)
5525 {
5526 tree itype = TREE_TYPE (cond_var);
5527 tree t2;
5528 if (POINTER_TYPE_P (type)
5529 || TREE_CODE (n1) != INTEGER_CST
5530 || fd->loop.cond_code != LT_EXPR)
5531 t2 = build_int_cst (itype, 1);
5532 else
5533 t2 = fold_convert (itype, step);
5534 t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
5535 t2 = force_gimple_operand_gsi (&gsi, t2, false,
5536 NULL_TREE, true, GSI_SAME_STMT);
5537 assign_stmt = gimple_build_assign (cond_var, t2);
5538 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5539 }
5540
5541 if (!gimple_omp_for_combined_p (fd->for_stmt))
5542 {
5543 if (POINTER_TYPE_P (type))
5544 t = fold_build_pointer_plus (vmain, step);
5545 else
5546 t = fold_build2 (PLUS_EXPR, type, vmain, step);
5547 t = force_gimple_operand_gsi (&gsi, t,
5548 DECL_P (vback)
5549 && TREE_ADDRESSABLE (vback),
5550 NULL_TREE, true, GSI_SAME_STMT);
5551 assign_stmt = gimple_build_assign (vback, t);
5552 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5553
5554 t = build2 (fd->loop.cond_code, boolean_type_node,
5555 DECL_P (vback) && TREE_ADDRESSABLE (vback)
5556 ? t : vback, e);
5557 gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
5558 }
5559
5560 /* Remove the GIMPLE_OMP_CONTINUE statement. */
5561 gsi_remove (&gsi, true);
5562
5563 if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
5564 collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
5565 cont_bb, body_bb);
5566 }
5567
5568 /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing. */
5569 gsi = gsi_last_nondebug_bb (exit_bb);
5570 if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
5571 {
5572 t = gimple_omp_return_lhs (gsi_stmt (gsi));
5573 if (fd->have_reductemp
5574 || ((fd->have_pointer_condtemp || fd->have_scantemp)
5575 && !fd->have_nonctrl_scantemp))
5576 {
5577 tree fn;
5578 if (t)
5579 fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
5580 else
5581 fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
5582 gcall *g = gimple_build_call (fn, 0);
5583 if (t)
5584 {
5585 gimple_call_set_lhs (g, t);
5586 if (fd->have_reductemp)
5587 gsi_insert_after (&gsi, gimple_build_assign (reductions,
5588 NOP_EXPR, t),
5589 GSI_SAME_STMT);
5590 }
5591 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
5592 }
5593 else
5594 gsi_insert_after (&gsi, omp_build_barrier (t), GSI_SAME_STMT);
5595 }
5596 else if ((fd->have_pointer_condtemp || fd->have_scantemp)
5597 && !fd->have_nonctrl_scantemp)
5598 {
5599 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
5600 gcall *g = gimple_build_call (fn, 0);
5601 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
5602 }
5603 if (fd->have_scantemp && !fd->have_nonctrl_scantemp)
5604 {
5605 tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5606 tree controlp = NULL_TREE, controlb = NULL_TREE;
5607 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5608 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
5609 && OMP_CLAUSE__SCANTEMP__CONTROL (c))
5610 {
5611 if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
5612 controlb = OMP_CLAUSE_DECL (c);
5613 else
5614 controlp = OMP_CLAUSE_DECL (c);
5615 if (controlb && controlp)
5616 break;
5617 }
5618 gcc_assert (controlp && controlb);
5619 gimple *g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
5620 NULL_TREE, NULL_TREE);
5621 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5622 exit1_bb = split_block (exit_bb, g)->dest;
5623 gsi = gsi_after_labels (exit1_bb);
5624 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_FREE), 1,
5625 controlp);
5626 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5627 exit2_bb = split_block (exit1_bb, g)->dest;
5628 gsi = gsi_after_labels (exit2_bb);
5629 g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE), 1,
5630 controlp);
5631 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5632 exit3_bb = split_block (exit2_bb, g)->dest;
5633 gsi = gsi_after_labels (exit3_bb);
5634 }
5635 gsi_remove (&gsi, true);
5636
5637 /* Connect all the blocks. */
5638 ep = make_edge (entry_bb, third_bb, EDGE_FALSE_VALUE);
5639 ep->probability = profile_probability::guessed_always ().apply_scale (3, 4);
5640 ep = find_edge (entry_bb, second_bb);
5641 ep->flags = EDGE_TRUE_VALUE;
5642 ep->probability = profile_probability::guessed_always () / 4;
5643 if (fourth_bb)
5644 {
5645 ep = make_edge (third_bb, fifth_bb, EDGE_FALSE_VALUE);
5646 ep->probability = profile_probability::guessed_always () / 2;
5647 ep = find_edge (third_bb, fourth_bb);
5648 ep->flags = EDGE_TRUE_VALUE;
5649 ep->probability = profile_probability::guessed_always () / 2;
5650 ep = find_edge (fourth_bb, fifth_bb);
5651 redirect_edge_and_branch (ep, sixth_bb);
5652 }
5653 else
5654 sixth_bb = third_bb;
5655 find_edge (sixth_bb, seq_start_bb)->flags = EDGE_FALSE_VALUE;
5656 find_edge (sixth_bb, fin_bb)->flags = EDGE_TRUE_VALUE;
5657 if (exit1_bb)
5658 {
5659 ep = make_edge (exit_bb, exit2_bb, EDGE_FALSE_VALUE);
5660 ep->probability = profile_probability::guessed_always () / 2;
5661 ep = find_edge (exit_bb, exit1_bb);
5662 ep->flags = EDGE_TRUE_VALUE;
5663 ep->probability = profile_probability::guessed_always () / 2;
5664 ep = find_edge (exit1_bb, exit2_bb);
5665 redirect_edge_and_branch (ep, exit3_bb);
5666 }
5667
5668 if (!broken_loop)
5669 {
5670 ep = find_edge (cont_bb, body_bb);
5671 if (ep == NULL)
5672 {
5673 ep = BRANCH_EDGE (cont_bb);
5674 gcc_assert (single_succ (ep->dest) == body_bb);
5675 }
5676 if (gimple_omp_for_combined_p (fd->for_stmt))
5677 {
5678 remove_edge (ep);
5679 ep = NULL;
5680 }
5681 else if (fd->collapse > 1)
5682 {
5683 remove_edge (ep);
5684 ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
5685 }
5686 else
5687 ep->flags = EDGE_TRUE_VALUE;
5688 find_edge (cont_bb, fin_bb)->flags
5689 = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
5690 }
5691
5692 set_immediate_dominator (CDI_DOMINATORS, second_bb, entry_bb);
5693 set_immediate_dominator (CDI_DOMINATORS, third_bb, entry_bb);
5694 if (fourth_bb)
5695 {
5696 set_immediate_dominator (CDI_DOMINATORS, fifth_bb, third_bb);
5697 set_immediate_dominator (CDI_DOMINATORS, sixth_bb, third_bb);
5698 }
5699 set_immediate_dominator (CDI_DOMINATORS, seq_start_bb, sixth_bb);
5700
5701 set_immediate_dominator (CDI_DOMINATORS, body_bb,
5702 recompute_dominator (CDI_DOMINATORS, body_bb));
5703 set_immediate_dominator (CDI_DOMINATORS, fin_bb,
5704 recompute_dominator (CDI_DOMINATORS, fin_bb));
5705 if (exit1_bb)
5706 {
5707 set_immediate_dominator (CDI_DOMINATORS, exit2_bb, exit_bb);
5708 set_immediate_dominator (CDI_DOMINATORS, exit3_bb, exit_bb);
5709 }
5710
5711 class loop *loop = body_bb->loop_father;
5712 if (loop != entry_bb->loop_father)
5713 {
5714 gcc_assert (broken_loop || loop->header == body_bb);
5715 gcc_assert (broken_loop
5716 || loop->latch == region->cont
5717 || single_pred (loop->latch) == region->cont);
5718 return;
5719 }
5720
5721 if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
5722 {
5723 loop = alloc_loop ();
5724 loop->header = body_bb;
5725 if (collapse_bb == NULL)
5726 loop->latch = cont_bb;
5727 add_loop (loop, body_bb->loop_father);
5728 }
5729 }
5730
5731 /* Return phi in E->DEST with ARG on edge E. */
5732
5733 static gphi *
5734 find_phi_with_arg_on_edge (tree arg, edge e)
5735 {
5736 basic_block bb = e->dest;
5737
5738 for (gphi_iterator gpi = gsi_start_phis (bb);
5739 !gsi_end_p (gpi);
5740 gsi_next (&gpi))
5741 {
5742 gphi *phi = gpi.phi ();
5743 if (PHI_ARG_DEF_FROM_EDGE (phi, e) == arg)
5744 return phi;
5745 }
5746
5747 return NULL;
5748 }
5749
5750 /* A subroutine of expand_omp_for. Generate code for a parallel
5751 loop with static schedule and a specified chunk size. Given
5752 parameters:
5753
5754 for (V = N1; V cond N2; V += STEP) BODY;
5755
5756 where COND is "<" or ">", we generate pseudocode
5757
5758 if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
5759 if (cond is <)
5760 adj = STEP - 1;
5761 else
5762 adj = STEP + 1;
5763 if ((__typeof (V)) -1 > 0 && cond is >)
5764 n = -(adj + N2 - N1) / -STEP;
5765 else
5766 n = (adj + N2 - N1) / STEP;
5767 trip = 0;
5768 V = threadid * CHUNK * STEP + N1; -- this extra definition of V is
5769 here so that V is defined
5770 if the loop is not entered
5771 L0:
5772 s0 = (trip * nthreads + threadid) * CHUNK;
5773 e0 = min (s0 + CHUNK, n);
5774 if (s0 < n) goto L1; else goto L4;
5775 L1:
5776 V = s0 * STEP + N1;
5777 e = e0 * STEP + N1;
5778 L2:
5779 BODY;
5780 V += STEP;
5781 if (V cond e) goto L2; else goto L3;
5782 L3:
5783 trip += 1;
5784 goto L0;
5785 L4:
5786 */
5787
5788 static void
5789 expand_omp_for_static_chunk (struct omp_region *region,
5790 struct omp_for_data *fd, gimple *inner_stmt)
5791 {
5792 tree n, s0, e0, e, t;
5793 tree trip_var, trip_init, trip_main, trip_back, nthreads, threadid;
5794 tree type, itype, vmain, vback, vextra;
5795 basic_block entry_bb, exit_bb, body_bb, seq_start_bb, iter_part_bb;
5796 basic_block trip_update_bb = NULL, cont_bb, collapse_bb = NULL, fin_bb;
5797 gimple_stmt_iterator gsi, gsip;
5798 edge se;
5799 bool broken_loop = region->cont == NULL;
5800 tree *counts = NULL;
5801 tree n1, n2, step;
5802 tree reductions = NULL_TREE;
5803 tree cond_var = NULL_TREE, condtemp = NULL_TREE;
5804
5805 itype = type = TREE_TYPE (fd->loop.v);
5806 if (POINTER_TYPE_P (type))
5807 itype = signed_type_for (type);
5808
5809 entry_bb = region->entry;
5810 se = split_block (entry_bb, last_nondebug_stmt (entry_bb));
5811 entry_bb = se->src;
5812 iter_part_bb = se->dest;
5813 cont_bb = region->cont;
5814 gcc_assert (EDGE_COUNT (iter_part_bb->succs) == 2);
5815 fin_bb = BRANCH_EDGE (iter_part_bb)->dest;
5816 gcc_assert (broken_loop
5817 || fin_bb == FALLTHRU_EDGE (cont_bb)->dest);
5818 seq_start_bb = split_edge (FALLTHRU_EDGE (iter_part_bb));
5819 body_bb = single_succ (seq_start_bb);
5820 if (!broken_loop)
5821 {
5822 gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
5823 || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
5824 gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
5825 trip_update_bb = split_edge (FALLTHRU_EDGE (cont_bb));
5826 }
5827 exit_bb = region->exit;
5828
5829 /* Trip and adjustment setup goes in ENTRY_BB. */
5830 gsi = gsi_last_nondebug_bb (entry_bb);
5831 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
5832 gsip = gsi;
5833 gsi_prev (&gsip);
5834
5835 if (fd->collapse > 1)
5836 {
5837 int first_zero_iter = -1, dummy = -1;
5838 basic_block l2_dom_bb = NULL, dummy_bb = NULL;
5839
5840 counts = XALLOCAVEC (tree, fd->collapse);
5841 expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
5842 fin_bb, first_zero_iter,
5843 dummy_bb, dummy, l2_dom_bb);
5844 t = NULL_TREE;
5845 }
5846 else if (gimple_omp_for_combined_into_p (fd->for_stmt))
5847 t = integer_one_node;
5848 else
5849 t = fold_binary (fd->loop.cond_code, boolean_type_node,
5850 fold_convert (type, fd->loop.n1),
5851 fold_convert (type, fd->loop.n2));
5852 if (fd->collapse == 1
5853 && TYPE_UNSIGNED (type)
5854 && (t == NULL_TREE || !integer_onep (t)))
5855 {
5856 n1 = fold_convert (type, unshare_expr (fd->loop.n1));
5857 n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
5858 true, GSI_SAME_STMT);
5859 n2 = fold_convert (type, unshare_expr (fd->loop.n2));
5860 n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
5861 true, GSI_SAME_STMT);
5862 gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
5863 n1, n2);
5864 se = split_block (entry_bb, cond_stmt);
5865 se->flags = EDGE_TRUE_VALUE;
5866 entry_bb = se->dest;
5867 se->probability = profile_probability::very_likely ();
5868 se = make_edge (se->src, fin_bb, EDGE_FALSE_VALUE);
5869 se->probability = profile_probability::very_unlikely ();
5870 if (gimple_in_ssa_p (cfun))
5871 {
5872 int dest_idx = find_edge (iter_part_bb, fin_bb)->dest_idx;
5873 for (gphi_iterator gpi = gsi_start_phis (fin_bb);
5874 !gsi_end_p (gpi); gsi_next (&gpi))
5875 {
5876 gphi *phi = gpi.phi ();
5877 add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
5878 se, UNKNOWN_LOCATION);
5879 }
5880 }
5881 gsi = gsi_last_bb (entry_bb);
5882 }
5883
5884 if (fd->lastprivate_conditional)
5885 {
5886 tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5887 tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
5888 if (fd->have_pointer_condtemp)
5889 condtemp = OMP_CLAUSE_DECL (c);
5890 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
5891 cond_var = OMP_CLAUSE_DECL (c);
5892 }
5893 if (fd->have_reductemp || fd->have_pointer_condtemp)
5894 {
5895 tree t1 = build_int_cst (long_integer_type_node, 0);
5896 tree t2 = build_int_cst (long_integer_type_node, 1);
5897 tree t3 = build_int_cstu (long_integer_type_node,
5898 (HOST_WIDE_INT_1U << 31) + 1);
5899 tree clauses = gimple_omp_for_clauses (fd->for_stmt);
5900 gimple_stmt_iterator gsi2 = gsi_none ();
5901 gimple *g = NULL;
5902 tree mem = null_pointer_node, memv = NULL_TREE;
5903 if (fd->have_reductemp)
5904 {
5905 tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
5906 reductions = OMP_CLAUSE_DECL (c);
5907 gcc_assert (TREE_CODE (reductions) == SSA_NAME);
5908 g = SSA_NAME_DEF_STMT (reductions);
5909 reductions = gimple_assign_rhs1 (g);
5910 OMP_CLAUSE_DECL (c) = reductions;
5911 gsi2 = gsi_for_stmt (g);
5912 }
5913 else
5914 {
5915 if (gsi_end_p (gsip))
5916 gsi2 = gsi_after_labels (region->entry);
5917 else
5918 gsi2 = gsip;
5919 reductions = null_pointer_node;
5920 }
5921 if (fd->have_pointer_condtemp)
5922 {
5923 tree type = TREE_TYPE (condtemp);
5924 memv = create_tmp_var (type);
5925 TREE_ADDRESSABLE (memv) = 1;
5926 unsigned HOST_WIDE_INT sz
5927 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
5928 sz *= fd->lastprivate_conditional;
5929 expand_omp_build_assign (&gsi2, memv, build_int_cst (type, sz),
5930 false);
5931 mem = build_fold_addr_expr (memv);
5932 }
5933 tree t
5934 = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
5935 9, t1, t2, t2, t3, t1, null_pointer_node,
5936 null_pointer_node, reductions, mem);
5937 force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
5938 true, GSI_SAME_STMT);
5939 if (fd->have_pointer_condtemp)
5940 expand_omp_build_assign (&gsi2, condtemp, memv, false);
5941 if (fd->have_reductemp)
5942 {
5943 gsi_remove (&gsi2, true);
5944 release_ssa_name (gimple_assign_lhs (g));
5945 }
5946 }
5947 switch (gimple_omp_for_kind (fd->for_stmt))
5948 {
5949 case GF_OMP_FOR_KIND_FOR:
5950 nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
5951 threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
5952 break;
5953 case GF_OMP_FOR_KIND_DISTRIBUTE:
5954 nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_TEAMS);
5955 threadid = builtin_decl_explicit (BUILT_IN_OMP_GET_TEAM_NUM);
5956 break;
5957 default:
5958 gcc_unreachable ();
5959 }
5960 nthreads = build_call_expr (nthreads, 0);
5961 nthreads = fold_convert (itype, nthreads);
5962 nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE,
5963 true, GSI_SAME_STMT);
5964 threadid = build_call_expr (threadid, 0);
5965 threadid = fold_convert (itype, threadid);
5966 threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE,
5967 true, GSI_SAME_STMT);
5968
5969 n1 = fd->loop.n1;
5970 n2 = fd->loop.n2;
5971 step = fd->loop.step;
5972 if (gimple_omp_for_combined_into_p (fd->for_stmt))
5973 {
5974 tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
5975 OMP_CLAUSE__LOOPTEMP_);
5976 gcc_assert (innerc);
5977 n1 = OMP_CLAUSE_DECL (innerc);
5978 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
5979 OMP_CLAUSE__LOOPTEMP_);
5980 gcc_assert (innerc);
5981 n2 = OMP_CLAUSE_DECL (innerc);
5982 }
5983 n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
5984 true, NULL_TREE, true, GSI_SAME_STMT);
5985 n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
5986 true, NULL_TREE, true, GSI_SAME_STMT);
5987 step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
5988 true, NULL_TREE, true, GSI_SAME_STMT);
5989 tree chunk_size = fold_convert (itype, fd->chunk_size);
5990 chunk_size = omp_adjust_chunk_size (chunk_size, fd->simd_schedule,
5991 is_in_offload_region (region));
5992 chunk_size
5993 = force_gimple_operand_gsi (&gsi, chunk_size, true, NULL_TREE, true,
5994 GSI_SAME_STMT);
5995
5996 t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
5997 t = fold_build2 (PLUS_EXPR, itype, step, t);
5998 t = fold_build2 (PLUS_EXPR, itype, t, n2);
5999 t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
6000 if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
6001 t = fold_build2 (TRUNC_DIV_EXPR, itype,
6002 fold_build1 (NEGATE_EXPR, itype, t),
6003 fold_build1 (NEGATE_EXPR, itype, step));
6004 else
6005 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
6006 t = fold_convert (itype, t);
6007 n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6008 true, GSI_SAME_STMT);
6009
6010 trip_var = create_tmp_reg (itype, ".trip");
6011 if (gimple_in_ssa_p (cfun))
6012 {
6013 trip_init = make_ssa_name (trip_var);
6014 trip_main = make_ssa_name (trip_var);
6015 trip_back = make_ssa_name (trip_var);
6016 }
6017 else
6018 {
6019 trip_init = trip_var;
6020 trip_main = trip_var;
6021 trip_back = trip_var;
6022 }
6023
6024 gassign *assign_stmt
6025 = gimple_build_assign (trip_init, build_int_cst (itype, 0));
6026 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6027
6028 t = fold_build2 (MULT_EXPR, itype, threadid, chunk_size);
6029 t = fold_build2 (MULT_EXPR, itype, t, step);
6030 if (POINTER_TYPE_P (type))
6031 t = fold_build_pointer_plus (n1, t);
6032 else
6033 t = fold_build2 (PLUS_EXPR, type, t, n1);
6034 vextra = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6035 true, GSI_SAME_STMT);
6036
6037 /* Remove the GIMPLE_OMP_FOR. */
6038 gsi_remove (&gsi, true);
6039
6040 gimple_stmt_iterator gsif = gsi;
6041
6042 /* Iteration space partitioning goes in ITER_PART_BB. */
6043 gsi = gsi_last_bb (iter_part_bb);
6044
6045 t = fold_build2 (MULT_EXPR, itype, trip_main, nthreads);
6046 t = fold_build2 (PLUS_EXPR, itype, t, threadid);
6047 t = fold_build2 (MULT_EXPR, itype, t, chunk_size);
6048 s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6049 false, GSI_CONTINUE_LINKING);
6050
6051 t = fold_build2 (PLUS_EXPR, itype, s0, chunk_size);
6052 t = fold_build2 (MIN_EXPR, itype, t, n);
6053 e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6054 false, GSI_CONTINUE_LINKING);
6055
6056 t = build2 (LT_EXPR, boolean_type_node, s0, n);
6057 gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_CONTINUE_LINKING);
6058
6059 /* Setup code for sequential iteration goes in SEQ_START_BB. */
6060 gsi = gsi_start_bb (seq_start_bb);
6061
6062 tree startvar = fd->loop.v;
6063 tree endvar = NULL_TREE;
6064
6065 if (gimple_omp_for_combined_p (fd->for_stmt))
6066 {
6067 tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
6068 ? gimple_omp_parallel_clauses (inner_stmt)
6069 : gimple_omp_for_clauses (inner_stmt);
6070 tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
6071 gcc_assert (innerc);
6072 startvar = OMP_CLAUSE_DECL (innerc);
6073 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
6074 OMP_CLAUSE__LOOPTEMP_);
6075 gcc_assert (innerc);
6076 endvar = OMP_CLAUSE_DECL (innerc);
6077 if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
6078 && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
6079 {
6080 innerc = find_lastprivate_looptemp (fd, innerc);
6081 if (innerc)
6082 {
6083 /* If needed (distribute parallel for with lastprivate),
6084 propagate down the total number of iterations. */
6085 tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
6086 fd->loop.n2);
6087 t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
6088 GSI_CONTINUE_LINKING);
6089 assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
6090 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6091 }
6092 }
6093 }
6094
6095 t = fold_convert (itype, s0);
6096 t = fold_build2 (MULT_EXPR, itype, t, step);
6097 if (POINTER_TYPE_P (type))
6098 {
6099 t = fold_build_pointer_plus (n1, t);
6100 if (!POINTER_TYPE_P (TREE_TYPE (startvar))
6101 && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
6102 t = fold_convert (signed_type_for (type), t);
6103 }
6104 else
6105 t = fold_build2 (PLUS_EXPR, type, t, n1);
6106 t = fold_convert (TREE_TYPE (startvar), t);
6107 t = force_gimple_operand_gsi (&gsi, t,
6108 DECL_P (startvar)
6109 && TREE_ADDRESSABLE (startvar),
6110 NULL_TREE, false, GSI_CONTINUE_LINKING);
6111 assign_stmt = gimple_build_assign (startvar, t);
6112 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6113 if (cond_var)
6114 {
6115 tree itype = TREE_TYPE (cond_var);
6116 /* For lastprivate(conditional:) itervar, we need some iteration
6117 counter that starts at unsigned non-zero and increases.
6118 Prefer as few IVs as possible, so if we can use startvar
6119 itself, use that, or startvar + constant (those would be
6120 incremented with step), and as last resort use the s0 + 1
6121 incremented by 1. */
6122 if (POINTER_TYPE_P (type)
6123 || TREE_CODE (n1) != INTEGER_CST
6124 || fd->loop.cond_code != LT_EXPR)
6125 t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
6126 build_int_cst (itype, 1));
6127 else if (tree_int_cst_sgn (n1) == 1)
6128 t = fold_convert (itype, t);
6129 else
6130 {
6131 tree c = fold_convert (itype, n1);
6132 c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
6133 t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
6134 }
6135 t = force_gimple_operand_gsi (&gsi, t, false,
6136 NULL_TREE, false, GSI_CONTINUE_LINKING);
6137 assign_stmt = gimple_build_assign (cond_var, t);
6138 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6139 }
6140
6141 t = fold_convert (itype, e0);
6142 t = fold_build2 (MULT_EXPR, itype, t, step);
6143 if (POINTER_TYPE_P (type))
6144 {
6145 t = fold_build_pointer_plus (n1, t);
6146 if (!POINTER_TYPE_P (TREE_TYPE (startvar))
6147 && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
6148 t = fold_convert (signed_type_for (type), t);
6149 }
6150 else
6151 t = fold_build2 (PLUS_EXPR, type, t, n1);
6152 t = fold_convert (TREE_TYPE (startvar), t);
6153 e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6154 false, GSI_CONTINUE_LINKING);
6155 if (endvar)
6156 {
6157 assign_stmt = gimple_build_assign (endvar, e);
6158 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6159 if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
6160 assign_stmt = gimple_build_assign (fd->loop.v, e);
6161 else
6162 assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
6163 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6164 }
6165 /* Handle linear clause adjustments. */
6166 tree itercnt = NULL_TREE, itercntbias = NULL_TREE;
6167 if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
6168 for (tree c = gimple_omp_for_clauses (fd->for_stmt);
6169 c; c = OMP_CLAUSE_CHAIN (c))
6170 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6171 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6172 {
6173 tree d = OMP_CLAUSE_DECL (c);
6174 tree t = d, a, dest;
6175 if (omp_privatize_by_reference (t))
6176 t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
6177 tree type = TREE_TYPE (t);
6178 if (POINTER_TYPE_P (type))
6179 type = sizetype;
6180 dest = unshare_expr (t);
6181 tree v = create_tmp_var (TREE_TYPE (t), NULL);
6182 expand_omp_build_assign (&gsif, v, t);
6183 if (itercnt == NULL_TREE)
6184 {
6185 if (gimple_omp_for_combined_into_p (fd->for_stmt))
6186 {
6187 itercntbias
6188 = fold_build2 (MINUS_EXPR, itype, fold_convert (itype, n1),
6189 fold_convert (itype, fd->loop.n1));
6190 itercntbias = fold_build2 (EXACT_DIV_EXPR, itype,
6191 itercntbias, step);
6192 itercntbias
6193 = force_gimple_operand_gsi (&gsif, itercntbias, true,
6194 NULL_TREE, true,
6195 GSI_SAME_STMT);
6196 itercnt = fold_build2 (PLUS_EXPR, itype, itercntbias, s0);
6197 itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
6198 NULL_TREE, false,
6199 GSI_CONTINUE_LINKING);
6200 }
6201 else
6202 itercnt = s0;
6203 }
6204 a = fold_build2 (MULT_EXPR, type,
6205 fold_convert (type, itercnt),
6206 fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
6207 t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
6208 : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
6209 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6210 false, GSI_CONTINUE_LINKING);
6211 expand_omp_build_assign (&gsi, dest, t, true);
6212 }
6213 if (fd->collapse > 1)
6214 expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
6215
6216 if (!broken_loop)
6217 {
6218 /* The code controlling the sequential loop goes in CONT_BB,
6219 replacing the GIMPLE_OMP_CONTINUE. */
6220 gsi = gsi_last_nondebug_bb (cont_bb);
6221 gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
6222 vmain = gimple_omp_continue_control_use (cont_stmt);
6223 vback = gimple_omp_continue_control_def (cont_stmt);
6224
6225 if (cond_var)
6226 {
6227 tree itype = TREE_TYPE (cond_var);
6228 tree t2;
6229 if (POINTER_TYPE_P (type)
6230 || TREE_CODE (n1) != INTEGER_CST
6231 || fd->loop.cond_code != LT_EXPR)
6232 t2 = build_int_cst (itype, 1);
6233 else
6234 t2 = fold_convert (itype, step);
6235 t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
6236 t2 = force_gimple_operand_gsi (&gsi, t2, false,
6237 NULL_TREE, true, GSI_SAME_STMT);
6238 assign_stmt = gimple_build_assign (cond_var, t2);
6239 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6240 }
6241
6242 if (!gimple_omp_for_combined_p (fd->for_stmt))
6243 {
6244 if (POINTER_TYPE_P (type))
6245 t = fold_build_pointer_plus (vmain, step);
6246 else
6247 t = fold_build2 (PLUS_EXPR, type, vmain, step);
6248 if (DECL_P (vback) && TREE_ADDRESSABLE (vback))
6249 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6250 true, GSI_SAME_STMT);
6251 assign_stmt = gimple_build_assign (vback, t);
6252 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
6253
6254 if (tree_int_cst_equal (fd->chunk_size, integer_one_node))
6255 t = build2 (EQ_EXPR, boolean_type_node,
6256 build_int_cst (itype, 0),
6257 build_int_cst (itype, 1));
6258 else
6259 t = build2 (fd->loop.cond_code, boolean_type_node,
6260 DECL_P (vback) && TREE_ADDRESSABLE (vback)
6261 ? t : vback, e);
6262 gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
6263 }
6264
6265 /* Remove GIMPLE_OMP_CONTINUE. */
6266 gsi_remove (&gsi, true);
6267
6268 if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
6269 collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, body_bb);
6270
6271 /* Trip update code goes into TRIP_UPDATE_BB. */
6272 gsi = gsi_start_bb (trip_update_bb);
6273
6274 t = build_int_cst (itype, 1);
6275 t = build2 (PLUS_EXPR, itype, trip_main, t);
6276 assign_stmt = gimple_build_assign (trip_back, t);
6277 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
6278 }
6279
6280 /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing. */
6281 gsi = gsi_last_nondebug_bb (exit_bb);
6282 if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
6283 {
6284 t = gimple_omp_return_lhs (gsi_stmt (gsi));
6285 if (fd->have_reductemp || fd->have_pointer_condtemp)
6286 {
6287 tree fn;
6288 if (t)
6289 fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
6290 else
6291 fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
6292 gcall *g = gimple_build_call (fn, 0);
6293 if (t)
6294 {
6295 gimple_call_set_lhs (g, t);
6296 if (fd->have_reductemp)
6297 gsi_insert_after (&gsi, gimple_build_assign (reductions,
6298 NOP_EXPR, t),
6299 GSI_SAME_STMT);
6300 }
6301 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
6302 }
6303 else
6304 gsi_insert_after (&gsi, omp_build_barrier (t), GSI_SAME_STMT);
6305 }
6306 else if (fd->have_pointer_condtemp)
6307 {
6308 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
6309 gcall *g = gimple_build_call (fn, 0);
6310 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
6311 }
6312 gsi_remove (&gsi, true);
6313
6314 /* Connect the new blocks. */
6315 find_edge (iter_part_bb, seq_start_bb)->flags = EDGE_TRUE_VALUE;
6316 find_edge (iter_part_bb, fin_bb)->flags = EDGE_FALSE_VALUE;
6317
6318 if (!broken_loop)
6319 {
6320 se = find_edge (cont_bb, body_bb);
6321 if (se == NULL)
6322 {
6323 se = BRANCH_EDGE (cont_bb);
6324 gcc_assert (single_succ (se->dest) == body_bb);
6325 }
6326 if (gimple_omp_for_combined_p (fd->for_stmt))
6327 {
6328 remove_edge (se);
6329 se = NULL;
6330 }
6331 else if (fd->collapse > 1)
6332 {
6333 remove_edge (se);
6334 se = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
6335 }
6336 else
6337 se->flags = EDGE_TRUE_VALUE;
6338 find_edge (cont_bb, trip_update_bb)->flags
6339 = se ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
6340
6341 redirect_edge_and_branch (single_succ_edge (trip_update_bb),
6342 iter_part_bb);
6343 }
6344
6345 if (gimple_in_ssa_p (cfun))
6346 {
6347 gphi_iterator psi;
6348 gphi *phi;
6349 edge re, ene;
6350 edge_var_map *vm;
6351 size_t i;
6352
6353 gcc_assert (fd->collapse == 1 && !broken_loop);
6354
6355 /* When we redirect the edge from trip_update_bb to iter_part_bb, we
6356 remove arguments of the phi nodes in fin_bb. We need to create
6357 appropriate phi nodes in iter_part_bb instead. */
6358 se = find_edge (iter_part_bb, fin_bb);
6359 re = single_succ_edge (trip_update_bb);
6360 vec<edge_var_map> *head = redirect_edge_var_map_vector (re);
6361 ene = single_succ_edge (entry_bb);
6362
6363 psi = gsi_start_phis (fin_bb);
6364 for (i = 0; !gsi_end_p (psi) && head->iterate (i, &vm);
6365 gsi_next (&psi), ++i)
6366 {
6367 gphi *nphi;
6368 location_t locus;
6369
6370 phi = psi.phi ();
6371 if (operand_equal_p (gimple_phi_arg_def (phi, 0),
6372 redirect_edge_var_map_def (vm), 0))
6373 continue;
6374
6375 t = gimple_phi_result (phi);
6376 gcc_assert (t == redirect_edge_var_map_result (vm));
6377
6378 if (!single_pred_p (fin_bb))
6379 t = copy_ssa_name (t, phi);
6380
6381 nphi = create_phi_node (t, iter_part_bb);
6382
6383 t = PHI_ARG_DEF_FROM_EDGE (phi, se);
6384 locus = gimple_phi_arg_location_from_edge (phi, se);
6385
6386 /* A special case -- fd->loop.v is not yet computed in
6387 iter_part_bb, we need to use vextra instead. */
6388 if (t == fd->loop.v)
6389 t = vextra;
6390 add_phi_arg (nphi, t, ene, locus);
6391 locus = redirect_edge_var_map_location (vm);
6392 tree back_arg = redirect_edge_var_map_def (vm);
6393 add_phi_arg (nphi, back_arg, re, locus);
6394 edge ce = find_edge (cont_bb, body_bb);
6395 if (ce == NULL)
6396 {
6397 ce = BRANCH_EDGE (cont_bb);
6398 gcc_assert (single_succ (ce->dest) == body_bb);
6399 ce = single_succ_edge (ce->dest);
6400 }
6401 gphi *inner_loop_phi = find_phi_with_arg_on_edge (back_arg, ce);
6402 gcc_assert (inner_loop_phi != NULL);
6403 add_phi_arg (inner_loop_phi, gimple_phi_result (nphi),
6404 find_edge (seq_start_bb, body_bb), locus);
6405
6406 if (!single_pred_p (fin_bb))
6407 add_phi_arg (phi, gimple_phi_result (nphi), se, locus);
6408 }
6409 gcc_assert (gsi_end_p (psi) && (head == NULL || i == head->length ()));
6410 redirect_edge_var_map_clear (re);
6411 if (single_pred_p (fin_bb))
6412 while (1)
6413 {
6414 psi = gsi_start_phis (fin_bb);
6415 if (gsi_end_p (psi))
6416 break;
6417 remove_phi_node (&psi, false);
6418 }
6419
6420 /* Make phi node for trip. */
6421 phi = create_phi_node (trip_main, iter_part_bb);
6422 add_phi_arg (phi, trip_back, single_succ_edge (trip_update_bb),
6423 UNKNOWN_LOCATION);
6424 add_phi_arg (phi, trip_init, single_succ_edge (entry_bb),
6425 UNKNOWN_LOCATION);
6426 }
6427
6428 if (!broken_loop)
6429 set_immediate_dominator (CDI_DOMINATORS, trip_update_bb, cont_bb);
6430 set_immediate_dominator (CDI_DOMINATORS, iter_part_bb,
6431 recompute_dominator (CDI_DOMINATORS, iter_part_bb));
6432 set_immediate_dominator (CDI_DOMINATORS, fin_bb,
6433 recompute_dominator (CDI_DOMINATORS, fin_bb));
6434 set_immediate_dominator (CDI_DOMINATORS, seq_start_bb,
6435 recompute_dominator (CDI_DOMINATORS, seq_start_bb));
6436 set_immediate_dominator (CDI_DOMINATORS, body_bb,
6437 recompute_dominator (CDI_DOMINATORS, body_bb));
6438
6439 if (!broken_loop)
6440 {
6441 class loop *loop = body_bb->loop_father;
6442 class loop *trip_loop = alloc_loop ();
6443 trip_loop->header = iter_part_bb;
6444 trip_loop->latch = trip_update_bb;
6445 add_loop (trip_loop, iter_part_bb->loop_father);
6446
6447 if (loop != entry_bb->loop_father)
6448 {
6449 gcc_assert (loop->header == body_bb);
6450 gcc_assert (loop->latch == region->cont
6451 || single_pred (loop->latch) == region->cont);
6452 trip_loop->inner = loop;
6453 return;
6454 }
6455
6456 if (!gimple_omp_for_combined_p (fd->for_stmt))
6457 {
6458 loop = alloc_loop ();
6459 loop->header = body_bb;
6460 if (collapse_bb == NULL)
6461 loop->latch = cont_bb;
6462 add_loop (loop, trip_loop);
6463 }
6464 }
6465 }
6466
6467 /* A subroutine of expand_omp_for. Generate code for a simd non-worksharing
6468 loop. Given parameters:
6469
6470 for (V = N1; V cond N2; V += STEP) BODY;
6471
6472 where COND is "<" or ">", we generate pseudocode
6473
6474 V = N1;
6475 goto L1;
6476 L0:
6477 BODY;
6478 V += STEP;
6479 L1:
6480 if (V cond N2) goto L0; else goto L2;
6481 L2:
6482
6483 For collapsed loops, emit the outer loops as scalar
6484 and only try to vectorize the innermost loop. */
6485
6486 static void
6487 expand_omp_simd (struct omp_region *region, struct omp_for_data *fd)
6488 {
6489 tree type, t;
6490 basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, l2_bb, l2_dom_bb;
6491 gimple_stmt_iterator gsi;
6492 gimple *stmt;
6493 gcond *cond_stmt;
6494 bool broken_loop = region->cont == NULL;
6495 edge e, ne;
6496 tree *counts = NULL;
6497 int i;
6498 int safelen_int = INT_MAX;
6499 bool dont_vectorize = false;
6500 tree safelen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6501 OMP_CLAUSE_SAFELEN);
6502 tree simduid = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6503 OMP_CLAUSE__SIMDUID_);
6504 tree ifc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6505 OMP_CLAUSE_IF);
6506 tree simdlen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6507 OMP_CLAUSE_SIMDLEN);
6508 tree condtemp = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6509 OMP_CLAUSE__CONDTEMP_);
6510 tree n1, n2;
6511 tree cond_var = condtemp ? OMP_CLAUSE_DECL (condtemp) : NULL_TREE;
6512
6513 if (safelen)
6514 {
6515 poly_uint64 val;
6516 safelen = OMP_CLAUSE_SAFELEN_EXPR (safelen);
6517 if (!poly_int_tree_p (safelen, &val))
6518 safelen_int = 0;
6519 else
6520 safelen_int = MIN (constant_lower_bound (val), INT_MAX);
6521 if (safelen_int == 1)
6522 safelen_int = 0;
6523 }
6524 if ((ifc && integer_zerop (OMP_CLAUSE_IF_EXPR (ifc)))
6525 || (simdlen && integer_onep (OMP_CLAUSE_SIMDLEN_EXPR (simdlen))))
6526 {
6527 safelen_int = 0;
6528 dont_vectorize = true;
6529 }
6530 type = TREE_TYPE (fd->loop.v);
6531 entry_bb = region->entry;
6532 cont_bb = region->cont;
6533 gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
6534 gcc_assert (broken_loop
6535 || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
6536 l0_bb = FALLTHRU_EDGE (entry_bb)->dest;
6537 if (!broken_loop)
6538 {
6539 gcc_assert (BRANCH_EDGE (cont_bb)->dest == l0_bb);
6540 gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
6541 l1_bb = split_block (cont_bb, last_nondebug_stmt (cont_bb))->dest;
6542 l2_bb = BRANCH_EDGE (entry_bb)->dest;
6543 }
6544 else
6545 {
6546 BRANCH_EDGE (entry_bb)->flags &= ~EDGE_ABNORMAL;
6547 l1_bb = split_edge (BRANCH_EDGE (entry_bb));
6548 l2_bb = single_succ (l1_bb);
6549 }
6550 exit_bb = region->exit;
6551 l2_dom_bb = NULL;
6552
6553 gsi = gsi_last_nondebug_bb (entry_bb);
6554
6555 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
6556 /* Not needed in SSA form right now. */
6557 gcc_assert (!gimple_in_ssa_p (cfun));
6558 if (fd->collapse > 1
6559 && (gimple_omp_for_combined_into_p (fd->for_stmt)
6560 || broken_loop))
6561 {
6562 int first_zero_iter = -1, dummy = -1;
6563 basic_block zero_iter_bb = l2_bb, dummy_bb = NULL;
6564
6565 counts = XALLOCAVEC (tree, fd->collapse);
6566 expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
6567 zero_iter_bb, first_zero_iter,
6568 dummy_bb, dummy, l2_dom_bb);
6569 }
6570 if (l2_dom_bb == NULL)
6571 l2_dom_bb = l1_bb;
6572
6573 n1 = fd->loop.n1;
6574 n2 = fd->loop.n2;
6575 if (gimple_omp_for_combined_into_p (fd->for_stmt))
6576 {
6577 tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6578 OMP_CLAUSE__LOOPTEMP_);
6579 gcc_assert (innerc);
6580 n1 = OMP_CLAUSE_DECL (innerc);
6581 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
6582 OMP_CLAUSE__LOOPTEMP_);
6583 gcc_assert (innerc);
6584 n2 = OMP_CLAUSE_DECL (innerc);
6585 }
6586 tree step = fd->loop.step;
6587 tree orig_step = step; /* May be different from step if is_simt. */
6588
6589 bool is_simt = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
6590 OMP_CLAUSE__SIMT_);
6591 if (is_simt)
6592 {
6593 cfun->curr_properties &= ~PROP_gimple_lomp_dev;
6594 is_simt = safelen_int > 1;
6595 }
6596 tree simt_lane = NULL_TREE, simt_maxlane = NULL_TREE;
6597 if (is_simt)
6598 {
6599 simt_lane = create_tmp_var (unsigned_type_node);
6600 gimple *g = gimple_build_call_internal (IFN_GOMP_SIMT_LANE, 0);
6601 gimple_call_set_lhs (g, simt_lane);
6602 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6603 tree offset = fold_build2 (MULT_EXPR, TREE_TYPE (step), step,
6604 fold_convert (TREE_TYPE (step), simt_lane));
6605 n1 = fold_convert (type, n1);
6606 if (POINTER_TYPE_P (type))
6607 n1 = fold_build_pointer_plus (n1, offset);
6608 else
6609 n1 = fold_build2 (PLUS_EXPR, type, n1, fold_convert (type, offset));
6610
6611 /* Collapsed loops not handled for SIMT yet: limit to one lane only. */
6612 if (fd->collapse > 1)
6613 simt_maxlane = build_one_cst (unsigned_type_node);
6614 else if (safelen_int < omp_max_simt_vf ())
6615 simt_maxlane = build_int_cst (unsigned_type_node, safelen_int);
6616 tree vf
6617 = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_GOMP_SIMT_VF,
6618 unsigned_type_node, 0);
6619 if (simt_maxlane)
6620 vf = fold_build2 (MIN_EXPR, unsigned_type_node, vf, simt_maxlane);
6621 vf = fold_convert (TREE_TYPE (step), vf);
6622 step = fold_build2 (MULT_EXPR, TREE_TYPE (step), step, vf);
6623 }
6624
6625 tree n2var = NULL_TREE;
6626 tree n2v = NULL_TREE;
6627 tree *nonrect_bounds = NULL;
6628 tree min_arg1 = NULL_TREE, min_arg2 = NULL_TREE;
6629 if (fd->collapse > 1)
6630 {
6631 if (broken_loop || gimple_omp_for_combined_into_p (fd->for_stmt))
6632 {
6633 if (fd->non_rect)
6634 {
6635 nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
6636 memset (nonrect_bounds, 0,
6637 sizeof (tree) * (fd->last_nonrect + 1));
6638 }
6639 expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
6640 gcc_assert (entry_bb == gsi_bb (gsi));
6641 gcc_assert (fd->for_stmt == gsi_stmt (gsi));
6642 gsi_prev (&gsi);
6643 entry_bb = split_block (entry_bb, gsi_stmt (gsi))->dest;
6644 expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds,
6645 NULL, n1);
6646 gsi = gsi_for_stmt (fd->for_stmt);
6647 }
6648 if (broken_loop)
6649 ;
6650 else if (gimple_omp_for_combined_into_p (fd->for_stmt))
6651 {
6652 /* Compute in n2var the limit for the first innermost loop,
6653 i.e. fd->loop.v + MIN (n2 - fd->loop.v, cnt)
6654 where cnt is how many iterations would the loop have if
6655 all further iterations were assigned to the current task. */
6656 n2var = create_tmp_var (type);
6657 i = fd->collapse - 1;
6658 tree itype = TREE_TYPE (fd->loops[i].v);
6659 if (POINTER_TYPE_P (itype))
6660 itype = signed_type_for (itype);
6661 t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
6662 ? -1 : 1));
6663 t = fold_build2 (PLUS_EXPR, itype,
6664 fold_convert (itype, fd->loops[i].step), t);
6665 t = fold_build2 (PLUS_EXPR, itype, t,
6666 fold_convert (itype, fd->loops[i].n2));
6667 if (fd->loops[i].m2)
6668 {
6669 tree t2 = fold_convert (itype,
6670 fd->loops[i - fd->loops[i].outer].v);
6671 tree t3 = fold_convert (itype, fd->loops[i].m2);
6672 t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
6673 t = fold_build2 (PLUS_EXPR, itype, t, t2);
6674 }
6675 t = fold_build2 (MINUS_EXPR, itype, t,
6676 fold_convert (itype, fd->loops[i].v));
6677 if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
6678 t = fold_build2 (TRUNC_DIV_EXPR, itype,
6679 fold_build1 (NEGATE_EXPR, itype, t),
6680 fold_build1 (NEGATE_EXPR, itype,
6681 fold_convert (itype,
6682 fd->loops[i].step)));
6683 else
6684 t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
6685 fold_convert (itype, fd->loops[i].step));
6686 t = fold_convert (type, t);
6687 tree t2 = fold_build2 (MINUS_EXPR, type, n2, n1);
6688 min_arg1 = create_tmp_var (type);
6689 expand_omp_build_assign (&gsi, min_arg1, t2);
6690 min_arg2 = create_tmp_var (type);
6691 expand_omp_build_assign (&gsi, min_arg2, t);
6692 }
6693 else
6694 {
6695 if (TREE_CODE (n2) == INTEGER_CST)
6696 {
6697 /* Indicate for lastprivate handling that at least one iteration
6698 has been performed, without wasting runtime. */
6699 if (integer_nonzerop (n2))
6700 expand_omp_build_assign (&gsi, fd->loop.v,
6701 fold_convert (type, n2));
6702 else
6703 /* Indicate that no iteration has been performed. */
6704 expand_omp_build_assign (&gsi, fd->loop.v,
6705 build_one_cst (type));
6706 }
6707 else
6708 {
6709 expand_omp_build_assign (&gsi, fd->loop.v,
6710 build_zero_cst (type));
6711 expand_omp_build_assign (&gsi, n2, build_one_cst (type));
6712 }
6713 for (i = 0; i < fd->collapse; i++)
6714 {
6715 t = fold_convert (TREE_TYPE (fd->loops[i].v), fd->loops[i].n1);
6716 if (fd->loops[i].m1)
6717 {
6718 tree t2
6719 = fold_convert (TREE_TYPE (t),
6720 fd->loops[i - fd->loops[i].outer].v);
6721 tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i].m1);
6722 t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
6723 t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
6724 }
6725 expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6726 /* For normal non-combined collapsed loops just initialize
6727 the outermost iterator in the entry_bb. */
6728 if (!broken_loop)
6729 break;
6730 }
6731 }
6732 }
6733 else
6734 expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
6735 tree altv = NULL_TREE, altn2 = NULL_TREE;
6736 if (fd->collapse == 1
6737 && !broken_loop
6738 && TREE_CODE (orig_step) != INTEGER_CST)
6739 {
6740 /* The vectorizer currently punts on loops with non-constant steps
6741 for the main IV (can't compute number of iterations and gives up
6742 because of that). As for OpenMP loops it is always possible to
6743 compute the number of iterations upfront, use an alternate IV
6744 as the loop iterator:
6745 altn2 = n1 < n2 ? (n2 - n1 + step - 1) / step : 0;
6746 for (i = n1, altv = 0; altv < altn2; altv++, i += step) */
6747 altv = create_tmp_var (unsigned_type_for (TREE_TYPE (fd->loops[0].v)));
6748 expand_omp_build_assign (&gsi, altv, build_zero_cst (TREE_TYPE (altv)));
6749 tree itype = TREE_TYPE (fd->loop.v);
6750 if (POINTER_TYPE_P (itype))
6751 itype = signed_type_for (itype);
6752 t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
6753 t = fold_build2 (PLUS_EXPR, itype,
6754 fold_convert (itype, step), t);
6755 t = fold_build2 (PLUS_EXPR, itype, t, fold_convert (itype, n2));
6756 t = fold_build2 (MINUS_EXPR, itype, t,
6757 fold_convert (itype, fd->loop.v));
6758 if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
6759 t = fold_build2 (TRUNC_DIV_EXPR, itype,
6760 fold_build1 (NEGATE_EXPR, itype, t),
6761 fold_build1 (NEGATE_EXPR, itype,
6762 fold_convert (itype, step)));
6763 else
6764 t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
6765 fold_convert (itype, step));
6766 t = fold_convert (TREE_TYPE (altv), t);
6767 altn2 = create_tmp_var (TREE_TYPE (altv));
6768 expand_omp_build_assign (&gsi, altn2, t);
6769 tree t2 = fold_convert (TREE_TYPE (fd->loop.v), n2);
6770 t2 = fold_build2 (fd->loop.cond_code, boolean_type_node, fd->loop.v, t2);
6771 t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
6772 true, GSI_SAME_STMT);
6773 gassign *g = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
6774 build_zero_cst (TREE_TYPE (altv)));
6775 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6776 }
6777 else if (fd->collapse > 1
6778 && !broken_loop
6779 && !gimple_omp_for_combined_into_p (fd->for_stmt)
6780 && TREE_CODE (fd->loops[fd->collapse - 1].step) != INTEGER_CST)
6781 {
6782 altv = create_tmp_var (unsigned_type_for (TREE_TYPE (fd->loops[0].v)));
6783 altn2 = create_tmp_var (TREE_TYPE (altv));
6784 }
6785 if (cond_var)
6786 {
6787 if (POINTER_TYPE_P (type)
6788 || TREE_CODE (n1) != INTEGER_CST
6789 || fd->loop.cond_code != LT_EXPR
6790 || tree_int_cst_sgn (n1) != 1)
6791 expand_omp_build_assign (&gsi, cond_var,
6792 build_one_cst (TREE_TYPE (cond_var)));
6793 else
6794 expand_omp_build_assign (&gsi, cond_var,
6795 fold_convert (TREE_TYPE (cond_var), n1));
6796 }
6797
6798 /* Remove the GIMPLE_OMP_FOR statement. */
6799 gsi_remove (&gsi, true);
6800
6801 if (!broken_loop)
6802 {
6803 /* Code to control the increment goes in the CONT_BB. */
6804 gsi = gsi_last_nondebug_bb (cont_bb);
6805 stmt = gsi_stmt (gsi);
6806 gcc_assert (gimple_code (stmt) == GIMPLE_OMP_CONTINUE);
6807
6808 if (fd->collapse == 1
6809 || gimple_omp_for_combined_into_p (fd->for_stmt))
6810 {
6811 if (POINTER_TYPE_P (type))
6812 t = fold_build_pointer_plus (fd->loop.v, step);
6813 else
6814 t = fold_build2 (PLUS_EXPR, type, fd->loop.v, step);
6815 expand_omp_build_assign (&gsi, fd->loop.v, t);
6816 }
6817 else if (TREE_CODE (n2) != INTEGER_CST)
6818 expand_omp_build_assign (&gsi, fd->loop.v, build_one_cst (type));
6819 if (altv)
6820 {
6821 t = fold_build2 (PLUS_EXPR, TREE_TYPE (altv), altv,
6822 build_one_cst (TREE_TYPE (altv)));
6823 expand_omp_build_assign (&gsi, altv, t);
6824 }
6825
6826 if (fd->collapse > 1)
6827 {
6828 i = fd->collapse - 1;
6829 if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
6830 t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
6831 else
6832 {
6833 t = fold_convert (TREE_TYPE (fd->loops[i].v),
6834 fd->loops[i].step);
6835 t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
6836 fd->loops[i].v, t);
6837 }
6838 expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6839 }
6840 if (cond_var)
6841 {
6842 if (POINTER_TYPE_P (type)
6843 || TREE_CODE (n1) != INTEGER_CST
6844 || fd->loop.cond_code != LT_EXPR
6845 || tree_int_cst_sgn (n1) != 1)
6846 t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
6847 build_one_cst (TREE_TYPE (cond_var)));
6848 else
6849 t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
6850 fold_convert (TREE_TYPE (cond_var), step));
6851 expand_omp_build_assign (&gsi, cond_var, t);
6852 }
6853
6854 /* Remove GIMPLE_OMP_CONTINUE. */
6855 gsi_remove (&gsi, true);
6856 }
6857
6858 /* Emit the condition in L1_BB. */
6859 gsi = gsi_start_bb (l1_bb);
6860
6861 if (altv)
6862 t = build2 (LT_EXPR, boolean_type_node, altv, altn2);
6863 else if (fd->collapse > 1
6864 && !gimple_omp_for_combined_into_p (fd->for_stmt)
6865 && !broken_loop)
6866 {
6867 i = fd->collapse - 1;
6868 tree itype = TREE_TYPE (fd->loops[i].v);
6869 if (fd->loops[i].m2)
6870 t = n2v = create_tmp_var (itype);
6871 else
6872 t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
6873 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6874 false, GSI_CONTINUE_LINKING);
6875 tree v = fd->loops[i].v;
6876 if (DECL_P (v) && TREE_ADDRESSABLE (v))
6877 v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
6878 false, GSI_CONTINUE_LINKING);
6879 t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
6880 }
6881 else
6882 {
6883 if (fd->collapse > 1 && !broken_loop)
6884 t = n2var;
6885 else
6886 t = fold_convert (type, unshare_expr (n2));
6887 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6888 false, GSI_CONTINUE_LINKING);
6889 tree v = fd->loop.v;
6890 if (DECL_P (v) && TREE_ADDRESSABLE (v))
6891 v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
6892 false, GSI_CONTINUE_LINKING);
6893 t = build2 (fd->loop.cond_code, boolean_type_node, v, t);
6894 }
6895 cond_stmt = gimple_build_cond_empty (t);
6896 gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
6897 if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
6898 NULL, NULL)
6899 || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p,
6900 NULL, NULL))
6901 {
6902 gsi = gsi_for_stmt (cond_stmt);
6903 gimple_regimplify_operands (cond_stmt, &gsi);
6904 }
6905
6906 /* Add 'V -= STEP * (SIMT_VF - 1)' after the loop. */
6907 if (is_simt)
6908 {
6909 gsi = gsi_start_bb (l2_bb);
6910 step = fold_build2 (MINUS_EXPR, TREE_TYPE (step), orig_step, step);
6911 if (POINTER_TYPE_P (type))
6912 t = fold_build_pointer_plus (fd->loop.v, step);
6913 else
6914 t = fold_build2 (PLUS_EXPR, type, fd->loop.v, step);
6915 expand_omp_build_assign (&gsi, fd->loop.v, t);
6916 }
6917
6918 /* Remove GIMPLE_OMP_RETURN. */
6919 gsi = gsi_last_nondebug_bb (exit_bb);
6920 gsi_remove (&gsi, true);
6921
6922 /* Connect the new blocks. */
6923 remove_edge (FALLTHRU_EDGE (entry_bb));
6924
6925 if (!broken_loop)
6926 {
6927 remove_edge (BRANCH_EDGE (entry_bb));
6928 make_edge (entry_bb, l1_bb, EDGE_FALLTHRU);
6929
6930 e = BRANCH_EDGE (l1_bb);
6931 ne = FALLTHRU_EDGE (l1_bb);
6932 e->flags = EDGE_TRUE_VALUE;
6933 }
6934 else
6935 {
6936 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
6937
6938 ne = single_succ_edge (l1_bb);
6939 e = make_edge (l1_bb, l0_bb, EDGE_TRUE_VALUE);
6940
6941 }
6942 ne->flags = EDGE_FALSE_VALUE;
6943 e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
6944 ne->probability = e->probability.invert ();
6945
6946 set_immediate_dominator (CDI_DOMINATORS, l1_bb, entry_bb);
6947 set_immediate_dominator (CDI_DOMINATORS, l0_bb, l1_bb);
6948
6949 if (simt_maxlane)
6950 {
6951 cond_stmt = gimple_build_cond (LT_EXPR, simt_lane, simt_maxlane,
6952 NULL_TREE, NULL_TREE);
6953 gsi = gsi_last_bb (entry_bb);
6954 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
6955 make_edge (entry_bb, l2_bb, EDGE_FALSE_VALUE);
6956 FALLTHRU_EDGE (entry_bb)->flags = EDGE_TRUE_VALUE;
6957 FALLTHRU_EDGE (entry_bb)->probability
6958 = profile_probability::guessed_always ().apply_scale (7, 8);
6959 BRANCH_EDGE (entry_bb)->probability
6960 = FALLTHRU_EDGE (entry_bb)->probability.invert ();
6961 l2_dom_bb = entry_bb;
6962 }
6963 set_immediate_dominator (CDI_DOMINATORS, l2_bb, l2_dom_bb);
6964
6965 if (!broken_loop && fd->collapse > 1)
6966 {
6967 basic_block last_bb = l1_bb;
6968 basic_block init_bb = NULL;
6969 for (i = fd->collapse - 2; i >= 0; i--)
6970 {
6971 tree nextn2v = NULL_TREE;
6972 if (EDGE_SUCC (last_bb, 0)->flags & EDGE_FALSE_VALUE)
6973 e = EDGE_SUCC (last_bb, 0);
6974 else
6975 e = EDGE_SUCC (last_bb, 1);
6976 basic_block bb = split_edge (e);
6977 if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
6978 t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
6979 else
6980 {
6981 t = fold_convert (TREE_TYPE (fd->loops[i].v),
6982 fd->loops[i].step);
6983 t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
6984 fd->loops[i].v, t);
6985 }
6986 gsi = gsi_after_labels (bb);
6987 expand_omp_build_assign (&gsi, fd->loops[i].v, t);
6988
6989 bb = split_block (bb, last_nondebug_stmt (bb))->dest;
6990 gsi = gsi_start_bb (bb);
6991 tree itype = TREE_TYPE (fd->loops[i].v);
6992 if (fd->loops[i].m2)
6993 t = nextn2v = create_tmp_var (itype);
6994 else
6995 t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
6996 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
6997 false, GSI_CONTINUE_LINKING);
6998 tree v = fd->loops[i].v;
6999 if (DECL_P (v) && TREE_ADDRESSABLE (v))
7000 v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
7001 false, GSI_CONTINUE_LINKING);
7002 t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
7003 cond_stmt = gimple_build_cond_empty (t);
7004 gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
7005 if (walk_tree (gimple_cond_lhs_ptr (cond_stmt),
7006 expand_omp_regimplify_p, NULL, NULL)
7007 || walk_tree (gimple_cond_rhs_ptr (cond_stmt),
7008 expand_omp_regimplify_p, NULL, NULL))
7009 {
7010 gsi = gsi_for_stmt (cond_stmt);
7011 gimple_regimplify_operands (cond_stmt, &gsi);
7012 }
7013 ne = single_succ_edge (bb);
7014 ne->flags = EDGE_FALSE_VALUE;
7015
7016 init_bb = create_empty_bb (bb);
7017 set_immediate_dominator (CDI_DOMINATORS, init_bb, bb);
7018 add_bb_to_loop (init_bb, bb->loop_father);
7019 e = make_edge (bb, init_bb, EDGE_TRUE_VALUE);
7020 e->probability
7021 = profile_probability::guessed_always ().apply_scale (7, 8);
7022 ne->probability = e->probability.invert ();
7023
7024 gsi = gsi_after_labels (init_bb);
7025 if (fd->loops[i + 1].m1)
7026 {
7027 tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7028 fd->loops[i + 1
7029 - fd->loops[i + 1].outer].v);
7030 if (POINTER_TYPE_P (TREE_TYPE (t2)))
7031 t = fold_build_pointer_plus (t2, fd->loops[i + 1].n1);
7032 else
7033 {
7034 t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7035 fd->loops[i + 1].n1);
7036 tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m1);
7037 t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
7038 t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
7039 }
7040 }
7041 else
7042 t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7043 fd->loops[i + 1].n1);
7044 expand_omp_build_assign (&gsi, fd->loops[i + 1].v, t);
7045 if (fd->loops[i + 1].m2)
7046 {
7047 if (i + 2 == fd->collapse && (n2var || altv))
7048 {
7049 gcc_assert (n2v == NULL_TREE);
7050 n2v = create_tmp_var (TREE_TYPE (fd->loops[i + 1].v));
7051 }
7052 tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7053 fd->loops[i + 1
7054 - fd->loops[i + 1].outer].v);
7055 if (POINTER_TYPE_P (TREE_TYPE (t2)))
7056 t = fold_build_pointer_plus (t2, fd->loops[i + 1].n2);
7057 else
7058 {
7059 t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7060 fd->loops[i + 1].n2);
7061 tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m2);
7062 t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
7063 t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
7064 }
7065 expand_omp_build_assign (&gsi, n2v, t);
7066 }
7067 if (i + 2 == fd->collapse && n2var)
7068 {
7069 /* For composite simd, n2 is the first iteration the current
7070 task shouldn't already handle, so we effectively want to use
7071 for (V3 = N31; V < N2 && V3 < N32; V++, V3 += STEP3)
7072 as the vectorized loop. Except the vectorizer will not
7073 vectorize that, so instead compute N2VAR as
7074 N2VAR = V + MIN (N2 - V, COUNTS3) and use
7075 for (V3 = N31; V < N2VAR; V++, V3 += STEP3)
7076 as the loop to vectorize. */
7077 tree t2 = fold_build2 (MINUS_EXPR, type, n2, fd->loop.v);
7078 if (fd->loops[i + 1].m1 || fd->loops[i + 1].m2)
7079 {
7080 tree itype = TREE_TYPE (fd->loops[i].v);
7081 if (POINTER_TYPE_P (itype))
7082 itype = signed_type_for (itype);
7083 t = build_int_cst (itype, (fd->loops[i + 1].cond_code
7084 == LT_EXPR ? -1 : 1));
7085 t = fold_build2 (PLUS_EXPR, itype,
7086 fold_convert (itype,
7087 fd->loops[i + 1].step), t);
7088 if (fd->loops[i + 1].m2 == NULL_TREE)
7089 t = fold_build2 (PLUS_EXPR, itype, t,
7090 fold_convert (itype,
7091 fd->loops[i + 1].n2));
7092 else if (POINTER_TYPE_P (TREE_TYPE (n2v)))
7093 {
7094 t = fold_build_pointer_plus (n2v, t);
7095 t = fold_convert (itype, t);
7096 }
7097 else
7098 t = fold_build2 (PLUS_EXPR, itype, t, n2v);
7099 t = fold_build2 (MINUS_EXPR, itype, t,
7100 fold_convert (itype, fd->loops[i + 1].v));
7101 tree step = fold_convert (itype, fd->loops[i + 1].step);
7102 if (TYPE_UNSIGNED (itype)
7103 && fd->loops[i + 1].cond_code == GT_EXPR)
7104 t = fold_build2 (TRUNC_DIV_EXPR, itype,
7105 fold_build1 (NEGATE_EXPR, itype, t),
7106 fold_build1 (NEGATE_EXPR, itype, step));
7107 else
7108 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
7109 t = fold_convert (type, t);
7110 }
7111 else
7112 t = counts[i + 1];
7113 expand_omp_build_assign (&gsi, min_arg1, t2);
7114 expand_omp_build_assign (&gsi, min_arg2, t);
7115 e = split_block (init_bb, last_nondebug_stmt (init_bb));
7116 gsi = gsi_after_labels (e->dest);
7117 init_bb = e->dest;
7118 remove_edge (FALLTHRU_EDGE (entry_bb));
7119 make_edge (entry_bb, init_bb, EDGE_FALLTHRU);
7120 set_immediate_dominator (CDI_DOMINATORS, init_bb, entry_bb);
7121 set_immediate_dominator (CDI_DOMINATORS, l1_bb, init_bb);
7122 t = fold_build2 (MIN_EXPR, type, min_arg1, min_arg2);
7123 t = fold_build2 (PLUS_EXPR, type, fd->loop.v, t);
7124 expand_omp_build_assign (&gsi, n2var, t);
7125 }
7126 if (i + 2 == fd->collapse && altv)
7127 {
7128 /* The vectorizer currently punts on loops with non-constant
7129 steps for the main IV (can't compute number of iterations
7130 and gives up because of that). As for OpenMP loops it is
7131 always possible to compute the number of iterations upfront,
7132 use an alternate IV as the loop iterator. */
7133 expand_omp_build_assign (&gsi, altv,
7134 build_zero_cst (TREE_TYPE (altv)));
7135 tree itype = TREE_TYPE (fd->loops[i + 1].v);
7136 if (POINTER_TYPE_P (itype))
7137 itype = signed_type_for (itype);
7138 t = build_int_cst (itype, (fd->loops[i + 1].cond_code == LT_EXPR
7139 ? -1 : 1));
7140 t = fold_build2 (PLUS_EXPR, itype,
7141 fold_convert (itype, fd->loops[i + 1].step), t);
7142 t = fold_build2 (PLUS_EXPR, itype, t,
7143 fold_convert (itype,
7144 fd->loops[i + 1].m2
7145 ? n2v : fd->loops[i + 1].n2));
7146 t = fold_build2 (MINUS_EXPR, itype, t,
7147 fold_convert (itype, fd->loops[i + 1].v));
7148 tree step = fold_convert (itype, fd->loops[i + 1].step);
7149 if (TYPE_UNSIGNED (itype)
7150 && fd->loops[i + 1].cond_code == GT_EXPR)
7151 t = fold_build2 (TRUNC_DIV_EXPR, itype,
7152 fold_build1 (NEGATE_EXPR, itype, t),
7153 fold_build1 (NEGATE_EXPR, itype, step));
7154 else
7155 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
7156 t = fold_convert (TREE_TYPE (altv), t);
7157 expand_omp_build_assign (&gsi, altn2, t);
7158 tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
7159 fd->loops[i + 1].m2
7160 ? n2v : fd->loops[i + 1].n2);
7161 t2 = fold_build2 (fd->loops[i + 1].cond_code, boolean_type_node,
7162 fd->loops[i + 1].v, t2);
7163 t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
7164 true, GSI_SAME_STMT);
7165 gassign *g
7166 = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
7167 build_zero_cst (TREE_TYPE (altv)));
7168 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
7169 }
7170 n2v = nextn2v;
7171
7172 make_edge (init_bb, last_bb, EDGE_FALLTHRU);
7173 if (!gimple_omp_for_combined_into_p (fd->for_stmt))
7174 {
7175 e = find_edge (entry_bb, last_bb);
7176 redirect_edge_succ (e, bb);
7177 set_immediate_dominator (CDI_DOMINATORS, bb, entry_bb);
7178 set_immediate_dominator (CDI_DOMINATORS, last_bb, init_bb);
7179 }
7180
7181 last_bb = bb;
7182 }
7183 }
7184 if (!broken_loop)
7185 {
7186 class loop *loop = alloc_loop ();
7187 loop->header = l1_bb;
7188 loop->latch = cont_bb;
7189 add_loop (loop, l1_bb->loop_father);
7190 loop->safelen = safelen_int;
7191 if (simduid)
7192 {
7193 loop->simduid = OMP_CLAUSE__SIMDUID__DECL (simduid);
7194 cfun->has_simduid_loops = true;
7195 }
7196 /* If not -fno-tree-loop-vectorize, hint that we want to vectorize
7197 the loop. */
7198 if ((flag_tree_loop_vectorize
7199 || !OPTION_SET_P (flag_tree_loop_vectorize))
7200 && flag_tree_loop_optimize
7201 && loop->safelen > 1)
7202 {
7203 loop->force_vectorize = true;
7204 if (simdlen && tree_fits_uhwi_p (OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
7205 {
7206 unsigned HOST_WIDE_INT v
7207 = tree_to_uhwi (OMP_CLAUSE_SIMDLEN_EXPR (simdlen));
7208 if (v < INT_MAX && v <= (unsigned HOST_WIDE_INT) loop->safelen)
7209 loop->simdlen = v;
7210 }
7211 cfun->has_force_vectorize_loops = true;
7212 }
7213 else if (dont_vectorize)
7214 loop->dont_vectorize = true;
7215 }
7216 else if (simduid)
7217 cfun->has_simduid_loops = true;
7218 }
7219
7220 /* Taskloop construct is represented after gimplification with
7221 two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
7222 in between them. This routine expands the outer GIMPLE_OMP_FOR,
7223 which should just compute all the needed loop temporaries
7224 for GIMPLE_OMP_TASK. */
7225
7226 static void
7227 expand_omp_taskloop_for_outer (struct omp_region *region,
7228 struct omp_for_data *fd,
7229 gimple *inner_stmt)
7230 {
7231 tree type, bias = NULL_TREE;
7232 basic_block entry_bb, cont_bb, exit_bb;
7233 gimple_stmt_iterator gsi;
7234 gassign *assign_stmt;
7235 tree *counts = NULL;
7236 int i;
7237
7238 gcc_assert (inner_stmt);
7239 gcc_assert (region->cont);
7240 gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_TASK
7241 && gimple_omp_task_taskloop_p (inner_stmt));
7242 type = TREE_TYPE (fd->loop.v);
7243
7244 /* See if we need to bias by LLONG_MIN. */
7245 if (fd->iter_type == long_long_unsigned_type_node
7246 && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
7247 && !TYPE_UNSIGNED (type))
7248 {
7249 tree n1, n2;
7250
7251 if (fd->loop.cond_code == LT_EXPR)
7252 {
7253 n1 = fd->loop.n1;
7254 n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
7255 }
7256 else
7257 {
7258 n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
7259 n2 = fd->loop.n1;
7260 }
7261 if (TREE_CODE (n1) != INTEGER_CST
7262 || TREE_CODE (n2) != INTEGER_CST
7263 || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
7264 bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
7265 }
7266
7267 entry_bb = region->entry;
7268 cont_bb = region->cont;
7269 gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
7270 gcc_assert (BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
7271 exit_bb = region->exit;
7272
7273 gsi = gsi_last_nondebug_bb (entry_bb);
7274 gimple *for_stmt = gsi_stmt (gsi);
7275 gcc_assert (gimple_code (for_stmt) == GIMPLE_OMP_FOR);
7276 if (fd->collapse > 1)
7277 {
7278 int first_zero_iter = -1, dummy = -1;
7279 basic_block zero_iter_bb = NULL, dummy_bb = NULL, l2_dom_bb = NULL;
7280
7281 counts = XALLOCAVEC (tree, fd->collapse);
7282 expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
7283 zero_iter_bb, first_zero_iter,
7284 dummy_bb, dummy, l2_dom_bb);
7285
7286 if (zero_iter_bb)
7287 {
7288 /* Some counts[i] vars might be uninitialized if
7289 some loop has zero iterations. But the body shouldn't
7290 be executed in that case, so just avoid uninit warnings. */
7291 for (i = first_zero_iter; i < fd->collapse; i++)
7292 if (SSA_VAR_P (counts[i]))
7293 suppress_warning (counts[i], OPT_Wuninitialized);
7294 gsi_prev (&gsi);
7295 edge e = split_block (entry_bb, gsi_stmt (gsi));
7296 entry_bb = e->dest;
7297 make_edge (zero_iter_bb, entry_bb, EDGE_FALLTHRU);
7298 gsi = gsi_last_bb (entry_bb);
7299 set_immediate_dominator (CDI_DOMINATORS, entry_bb,
7300 get_immediate_dominator (CDI_DOMINATORS,
7301 zero_iter_bb));
7302 }
7303 }
7304
7305 tree t0, t1;
7306 t1 = fd->loop.n2;
7307 t0 = fd->loop.n1;
7308 if (POINTER_TYPE_P (TREE_TYPE (t0))
7309 && TYPE_PRECISION (TREE_TYPE (t0))
7310 != TYPE_PRECISION (fd->iter_type))
7311 {
7312 /* Avoid casting pointers to integer of a different size. */
7313 tree itype = signed_type_for (type);
7314 t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
7315 t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
7316 }
7317 else
7318 {
7319 t1 = fold_convert (fd->iter_type, t1);
7320 t0 = fold_convert (fd->iter_type, t0);
7321 }
7322 if (bias)
7323 {
7324 t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
7325 t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
7326 }
7327
7328 tree innerc = omp_find_clause (gimple_omp_task_clauses (inner_stmt),
7329 OMP_CLAUSE__LOOPTEMP_);
7330 gcc_assert (innerc);
7331 tree startvar = OMP_CLAUSE_DECL (innerc);
7332 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
7333 gcc_assert (innerc);
7334 tree endvar = OMP_CLAUSE_DECL (innerc);
7335 if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST)
7336 {
7337 innerc = find_lastprivate_looptemp (fd, innerc);
7338 if (innerc)
7339 {
7340 /* If needed (inner taskloop has lastprivate clause), propagate
7341 down the total number of iterations. */
7342 tree t = force_gimple_operand_gsi (&gsi, fd->loop.n2, false,
7343 NULL_TREE, false,
7344 GSI_CONTINUE_LINKING);
7345 assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
7346 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7347 }
7348 }
7349
7350 t0 = force_gimple_operand_gsi (&gsi, t0, false, NULL_TREE, false,
7351 GSI_CONTINUE_LINKING);
7352 assign_stmt = gimple_build_assign (startvar, t0);
7353 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7354
7355 t1 = force_gimple_operand_gsi (&gsi, t1, false, NULL_TREE, false,
7356 GSI_CONTINUE_LINKING);
7357 assign_stmt = gimple_build_assign (endvar, t1);
7358 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7359 if (fd->collapse > 1)
7360 expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
7361
7362 /* Remove the GIMPLE_OMP_FOR statement. */
7363 gsi = gsi_for_stmt (for_stmt);
7364 gsi_remove (&gsi, true);
7365
7366 gsi = gsi_last_nondebug_bb (cont_bb);
7367 gsi_remove (&gsi, true);
7368
7369 gsi = gsi_last_nondebug_bb (exit_bb);
7370 gsi_remove (&gsi, true);
7371
7372 FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
7373 remove_edge (BRANCH_EDGE (entry_bb));
7374 FALLTHRU_EDGE (cont_bb)->probability = profile_probability::always ();
7375 remove_edge (BRANCH_EDGE (cont_bb));
7376 set_immediate_dominator (CDI_DOMINATORS, exit_bb, cont_bb);
7377 set_immediate_dominator (CDI_DOMINATORS, region->entry,
7378 recompute_dominator (CDI_DOMINATORS, region->entry));
7379 }
7380
7381 /* Taskloop construct is represented after gimplification with
7382 two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
7383 in between them. This routine expands the inner GIMPLE_OMP_FOR.
7384 GOMP_taskloop{,_ull} function arranges for each task to be given just
7385 a single range of iterations. */
7386
7387 static void
7388 expand_omp_taskloop_for_inner (struct omp_region *region,
7389 struct omp_for_data *fd,
7390 gimple *inner_stmt)
7391 {
7392 tree e, t, type, itype, vmain, vback, bias = NULL_TREE;
7393 basic_block entry_bb, exit_bb, body_bb, cont_bb, collapse_bb = NULL;
7394 basic_block fin_bb;
7395 gimple_stmt_iterator gsi;
7396 edge ep;
7397 bool broken_loop = region->cont == NULL;
7398 tree *counts = NULL;
7399 tree n1, n2, step;
7400
7401 itype = type = TREE_TYPE (fd->loop.v);
7402 if (POINTER_TYPE_P (type))
7403 itype = signed_type_for (type);
7404
7405 /* See if we need to bias by LLONG_MIN. */
7406 if (fd->iter_type == long_long_unsigned_type_node
7407 && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
7408 && !TYPE_UNSIGNED (type))
7409 {
7410 tree n1, n2;
7411
7412 if (fd->loop.cond_code == LT_EXPR)
7413 {
7414 n1 = fd->loop.n1;
7415 n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
7416 }
7417 else
7418 {
7419 n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
7420 n2 = fd->loop.n1;
7421 }
7422 if (TREE_CODE (n1) != INTEGER_CST
7423 || TREE_CODE (n2) != INTEGER_CST
7424 || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
7425 bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
7426 }
7427
7428 entry_bb = region->entry;
7429 cont_bb = region->cont;
7430 gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
7431 fin_bb = BRANCH_EDGE (entry_bb)->dest;
7432 gcc_assert (broken_loop
7433 || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
7434 body_bb = FALLTHRU_EDGE (entry_bb)->dest;
7435 if (!broken_loop)
7436 {
7437 gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb);
7438 gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
7439 }
7440 exit_bb = region->exit;
7441
7442 /* Iteration space partitioning goes in ENTRY_BB. */
7443 gsi = gsi_last_nondebug_bb (entry_bb);
7444 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
7445
7446 if (fd->collapse > 1)
7447 {
7448 int first_zero_iter = -1, dummy = -1;
7449 basic_block l2_dom_bb = NULL, dummy_bb = NULL;
7450
7451 counts = XALLOCAVEC (tree, fd->collapse);
7452 expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
7453 fin_bb, first_zero_iter,
7454 dummy_bb, dummy, l2_dom_bb);
7455 t = NULL_TREE;
7456 }
7457 else
7458 t = integer_one_node;
7459
7460 step = fd->loop.step;
7461 tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
7462 OMP_CLAUSE__LOOPTEMP_);
7463 gcc_assert (innerc);
7464 n1 = OMP_CLAUSE_DECL (innerc);
7465 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
7466 gcc_assert (innerc);
7467 n2 = OMP_CLAUSE_DECL (innerc);
7468 if (bias)
7469 {
7470 n1 = fold_build2 (PLUS_EXPR, fd->iter_type, n1, bias);
7471 n2 = fold_build2 (PLUS_EXPR, fd->iter_type, n2, bias);
7472 }
7473 n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
7474 true, NULL_TREE, true, GSI_SAME_STMT);
7475 n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
7476 true, NULL_TREE, true, GSI_SAME_STMT);
7477 step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
7478 true, NULL_TREE, true, GSI_SAME_STMT);
7479
7480 tree startvar = fd->loop.v;
7481 tree endvar = NULL_TREE;
7482
7483 if (gimple_omp_for_combined_p (fd->for_stmt))
7484 {
7485 tree clauses = gimple_omp_for_clauses (inner_stmt);
7486 tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
7487 gcc_assert (innerc);
7488 startvar = OMP_CLAUSE_DECL (innerc);
7489 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
7490 OMP_CLAUSE__LOOPTEMP_);
7491 gcc_assert (innerc);
7492 endvar = OMP_CLAUSE_DECL (innerc);
7493 }
7494 t = fold_convert (TREE_TYPE (startvar), n1);
7495 t = force_gimple_operand_gsi (&gsi, t,
7496 DECL_P (startvar)
7497 && TREE_ADDRESSABLE (startvar),
7498 NULL_TREE, false, GSI_CONTINUE_LINKING);
7499 gimple *assign_stmt = gimple_build_assign (startvar, t);
7500 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7501
7502 t = fold_convert (TREE_TYPE (startvar), n2);
7503 e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
7504 false, GSI_CONTINUE_LINKING);
7505 if (endvar)
7506 {
7507 assign_stmt = gimple_build_assign (endvar, e);
7508 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7509 if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
7510 assign_stmt = gimple_build_assign (fd->loop.v, e);
7511 else
7512 assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
7513 gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
7514 }
7515
7516 tree *nonrect_bounds = NULL;
7517 if (fd->collapse > 1)
7518 {
7519 if (fd->non_rect)
7520 {
7521 nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
7522 memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
7523 }
7524 gcc_assert (gsi_bb (gsi) == entry_bb);
7525 expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
7526 startvar);
7527 entry_bb = gsi_bb (gsi);
7528 }
7529
7530 if (!broken_loop)
7531 {
7532 /* The code controlling the sequential loop replaces the
7533 GIMPLE_OMP_CONTINUE. */
7534 gsi = gsi_last_nondebug_bb (cont_bb);
7535 gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
7536 gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
7537 vmain = gimple_omp_continue_control_use (cont_stmt);
7538 vback = gimple_omp_continue_control_def (cont_stmt);
7539
7540 if (!gimple_omp_for_combined_p (fd->for_stmt))
7541 {
7542 if (POINTER_TYPE_P (type))
7543 t = fold_build_pointer_plus (vmain, step);
7544 else
7545 t = fold_build2 (PLUS_EXPR, type, vmain, step);
7546 t = force_gimple_operand_gsi (&gsi, t,
7547 DECL_P (vback)
7548 && TREE_ADDRESSABLE (vback),
7549 NULL_TREE, true, GSI_SAME_STMT);
7550 assign_stmt = gimple_build_assign (vback, t);
7551 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
7552
7553 t = build2 (fd->loop.cond_code, boolean_type_node,
7554 DECL_P (vback) && TREE_ADDRESSABLE (vback)
7555 ? t : vback, e);
7556 gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
7557 }
7558
7559 /* Remove the GIMPLE_OMP_CONTINUE statement. */
7560 gsi_remove (&gsi, true);
7561
7562 if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
7563 collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
7564 cont_bb, body_bb);
7565 }
7566
7567 /* Remove the GIMPLE_OMP_FOR statement. */
7568 gsi = gsi_for_stmt (fd->for_stmt);
7569 gsi_remove (&gsi, true);
7570
7571 /* Remove the GIMPLE_OMP_RETURN statement. */
7572 gsi = gsi_last_nondebug_bb (exit_bb);
7573 gsi_remove (&gsi, true);
7574
7575 FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
7576 if (!broken_loop)
7577 remove_edge (BRANCH_EDGE (entry_bb));
7578 else
7579 {
7580 remove_edge_and_dominated_blocks (BRANCH_EDGE (entry_bb));
7581 region->outer->cont = NULL;
7582 }
7583
7584 /* Connect all the blocks. */
7585 if (!broken_loop)
7586 {
7587 ep = find_edge (cont_bb, body_bb);
7588 if (gimple_omp_for_combined_p (fd->for_stmt))
7589 {
7590 remove_edge (ep);
7591 ep = NULL;
7592 }
7593 else if (fd->collapse > 1)
7594 {
7595 remove_edge (ep);
7596 ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
7597 }
7598 else
7599 ep->flags = EDGE_TRUE_VALUE;
7600 find_edge (cont_bb, fin_bb)->flags
7601 = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
7602 }
7603
7604 set_immediate_dominator (CDI_DOMINATORS, body_bb,
7605 recompute_dominator (CDI_DOMINATORS, body_bb));
7606 if (!broken_loop)
7607 set_immediate_dominator (CDI_DOMINATORS, fin_bb,
7608 recompute_dominator (CDI_DOMINATORS, fin_bb));
7609
7610 if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
7611 {
7612 class loop *loop = alloc_loop ();
7613 loop->header = body_bb;
7614 if (collapse_bb == NULL)
7615 loop->latch = cont_bb;
7616 add_loop (loop, body_bb->loop_father);
7617 }
7618 }
7619
7620 /* A subroutine of expand_omp_for. Generate code for an OpenACC
7621 partitioned loop. The lowering here is abstracted, in that the
7622 loop parameters are passed through internal functions, which are
7623 further lowered by oacc_device_lower, once we get to the target
7624 compiler. The loop is of the form:
7625
7626 for (V = B; V LTGT E; V += S) {BODY}
7627
7628 where LTGT is < or >. We may have a specified chunking size, CHUNKING
7629 (constant 0 for no chunking) and we will have a GWV partitioning
7630 mask, specifying dimensions over which the loop is to be
7631 partitioned (see note below). We generate code that looks like
7632 (this ignores tiling):
7633
7634 <entry_bb> [incoming FALL->body, BRANCH->exit]
7635 typedef signedintify (typeof (V)) T; // underlying signed integral type
7636 T range = E - B;
7637 T chunk_no = 0;
7638 T DIR = LTGT == '<' ? +1 : -1;
7639 T chunk_max = GOACC_LOOP_CHUNK (dir, range, S, CHUNK_SIZE, GWV);
7640 T step = GOACC_LOOP_STEP (dir, range, S, CHUNK_SIZE, GWV);
7641
7642 <head_bb> [created by splitting end of entry_bb]
7643 T offset = GOACC_LOOP_OFFSET (dir, range, S, CHUNK_SIZE, GWV, chunk_no);
7644 T bound = GOACC_LOOP_BOUND (dir, range, S, CHUNK_SIZE, GWV, offset);
7645 if (!(offset LTGT bound)) goto bottom_bb;
7646
7647 <body_bb> [incoming]
7648 V = B + offset;
7649 {BODY}
7650
7651 <cont_bb> [incoming, may == body_bb FALL->exit_bb, BRANCH->body_bb]
7652 offset += step;
7653 if (offset LTGT bound) goto body_bb; [*]
7654
7655 <bottom_bb> [created by splitting start of exit_bb] insert BRANCH->head_bb
7656 chunk_no++;
7657 if (chunk < chunk_max) goto head_bb;
7658
7659 <exit_bb> [incoming]
7660 V = B + ((range -/+ 1) / S +/- 1) * S [*]
7661
7662 [*] Needed if V live at end of loop. */
7663
7664 static void
7665 expand_oacc_for (struct omp_region *region, struct omp_for_data *fd)
7666 {
7667 bool is_oacc_kernels_parallelized
7668 = (lookup_attribute ("oacc kernels parallelized",
7669 DECL_ATTRIBUTES (current_function_decl)) != NULL);
7670 {
7671 bool is_oacc_kernels
7672 = (lookup_attribute ("oacc kernels",
7673 DECL_ATTRIBUTES (current_function_decl)) != NULL);
7674 if (is_oacc_kernels_parallelized)
7675 gcc_checking_assert (is_oacc_kernels);
7676 }
7677 gcc_assert (gimple_in_ssa_p (cfun) == is_oacc_kernels_parallelized);
7678 /* In the following, some of the 'gimple_in_ssa_p (cfun)' conditionals are
7679 for SSA specifics, and some are for 'parloops' OpenACC
7680 'kernels'-parallelized specifics. */
7681
7682 tree v = fd->loop.v;
7683 enum tree_code cond_code = fd->loop.cond_code;
7684 enum tree_code plus_code = PLUS_EXPR;
7685
7686 tree chunk_size = integer_minus_one_node;
7687 tree gwv = integer_zero_node;
7688 tree iter_type = TREE_TYPE (v);
7689 tree diff_type = iter_type;
7690 tree plus_type = iter_type;
7691 struct oacc_collapse *counts = NULL;
7692
7693 gcc_checking_assert (gimple_omp_for_kind (fd->for_stmt)
7694 == GF_OMP_FOR_KIND_OACC_LOOP);
7695 gcc_assert (!gimple_omp_for_combined_into_p (fd->for_stmt));
7696 gcc_assert (cond_code == LT_EXPR || cond_code == GT_EXPR);
7697
7698 if (POINTER_TYPE_P (iter_type))
7699 {
7700 plus_code = POINTER_PLUS_EXPR;
7701 plus_type = sizetype;
7702 }
7703 for (int ix = fd->collapse; ix--;)
7704 {
7705 tree diff_type2 = TREE_TYPE (fd->loops[ix].step);
7706 if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (diff_type2))
7707 diff_type = diff_type2;
7708 }
7709 if (POINTER_TYPE_P (diff_type) || TYPE_UNSIGNED (diff_type))
7710 diff_type = signed_type_for (diff_type);
7711 if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (integer_type_node))
7712 diff_type = integer_type_node;
7713
7714 basic_block entry_bb = region->entry; /* BB ending in OMP_FOR */
7715 basic_block exit_bb = region->exit; /* BB ending in OMP_RETURN */
7716 basic_block cont_bb = region->cont; /* BB ending in OMP_CONTINUE */
7717 basic_block bottom_bb = NULL;
7718
7719 /* entry_bb has two successors; the branch edge is to the exit
7720 block, fallthrough edge to body. */
7721 gcc_assert (EDGE_COUNT (entry_bb->succs) == 2
7722 && BRANCH_EDGE (entry_bb)->dest == exit_bb);
7723
7724 /* If cont_bb non-NULL, it has 2 successors. The branch successor is
7725 body_bb, or to a block whose only successor is the body_bb. Its
7726 fallthrough successor is the final block (same as the branch
7727 successor of the entry_bb). */
7728 if (cont_bb)
7729 {
7730 basic_block body_bb = FALLTHRU_EDGE (entry_bb)->dest;
7731 basic_block bed = BRANCH_EDGE (cont_bb)->dest;
7732
7733 gcc_assert (FALLTHRU_EDGE (cont_bb)->dest == exit_bb);
7734 gcc_assert (bed == body_bb || single_succ_edge (bed)->dest == body_bb);
7735 }
7736 else
7737 gcc_assert (!gimple_in_ssa_p (cfun));
7738
7739 /* The exit block only has entry_bb and cont_bb as predecessors. */
7740 gcc_assert (EDGE_COUNT (exit_bb->preds) == 1 + (cont_bb != NULL));
7741
7742 tree chunk_no;
7743 tree chunk_max = NULL_TREE;
7744 tree bound, offset;
7745 tree step = create_tmp_var (diff_type, ".step");
7746 bool up = cond_code == LT_EXPR;
7747 tree dir = build_int_cst (diff_type, up ? +1 : -1);
7748 bool chunking = !gimple_in_ssa_p (cfun);
7749 bool negating;
7750
7751 /* Tiling vars. */
7752 tree tile_size = NULL_TREE;
7753 tree element_s = NULL_TREE;
7754 tree e_bound = NULL_TREE, e_offset = NULL_TREE, e_step = NULL_TREE;
7755 basic_block elem_body_bb = NULL;
7756 basic_block elem_cont_bb = NULL;
7757
7758 /* SSA instances. */
7759 tree offset_incr = NULL_TREE;
7760 tree offset_init = NULL_TREE;
7761
7762 gimple_stmt_iterator gsi;
7763 gassign *ass;
7764 gcall *call;
7765 gimple *stmt;
7766 tree expr;
7767 location_t loc;
7768 edge split, be, fte;
7769
7770 /* Split the end of entry_bb to create head_bb. */
7771 split = split_block (entry_bb, last_nondebug_stmt (entry_bb));
7772 basic_block head_bb = split->dest;
7773 entry_bb = split->src;
7774
7775 /* Chunk setup goes at end of entry_bb, replacing the omp_for. */
7776 gsi = gsi_last_nondebug_bb (entry_bb);
7777 gomp_for *for_stmt = as_a <gomp_for *> (gsi_stmt (gsi));
7778 loc = gimple_location (for_stmt);
7779
7780 if (gimple_in_ssa_p (cfun))
7781 {
7782 offset_init = gimple_omp_for_index (for_stmt, 0);
7783 gcc_assert (integer_zerop (fd->loop.n1));
7784 /* The SSA parallelizer does gang parallelism. */
7785 gwv = build_int_cst (integer_type_node, GOMP_DIM_MASK (GOMP_DIM_GANG));
7786 }
7787
7788 if (fd->collapse > 1 || fd->tiling)
7789 {
7790 gcc_assert (!gimple_in_ssa_p (cfun) && up);
7791 counts = XALLOCAVEC (struct oacc_collapse, fd->collapse);
7792 tree total = expand_oacc_collapse_init (fd, &gsi, counts, diff_type,
7793 TREE_TYPE (fd->loop.n2), loc);
7794
7795 if (SSA_VAR_P (fd->loop.n2))
7796 {
7797 total = force_gimple_operand_gsi (&gsi, total, false, NULL_TREE,
7798 true, GSI_SAME_STMT);
7799 ass = gimple_build_assign (fd->loop.n2, total);
7800 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7801 }
7802 }
7803
7804 tree b = fd->loop.n1;
7805 tree e = fd->loop.n2;
7806 tree s = fd->loop.step;
7807
7808 b = force_gimple_operand_gsi (&gsi, b, true, NULL_TREE, true, GSI_SAME_STMT);
7809 e = force_gimple_operand_gsi (&gsi, e, true, NULL_TREE, true, GSI_SAME_STMT);
7810
7811 /* Convert the step, avoiding possible unsigned->signed overflow. */
7812 negating = !up && TYPE_UNSIGNED (TREE_TYPE (s));
7813 if (negating)
7814 s = fold_build1 (NEGATE_EXPR, TREE_TYPE (s), s);
7815 s = fold_convert (diff_type, s);
7816 if (negating)
7817 s = fold_build1 (NEGATE_EXPR, diff_type, s);
7818 s = force_gimple_operand_gsi (&gsi, s, true, NULL_TREE, true, GSI_SAME_STMT);
7819
7820 if (!chunking)
7821 chunk_size = integer_zero_node;
7822 expr = fold_convert (diff_type, chunk_size);
7823 chunk_size = force_gimple_operand_gsi (&gsi, expr, true,
7824 NULL_TREE, true, GSI_SAME_STMT);
7825
7826 if (fd->tiling)
7827 {
7828 /* Determine the tile size and element step,
7829 modify the outer loop step size. */
7830 tile_size = create_tmp_var (diff_type, ".tile_size");
7831 expr = build_int_cst (diff_type, 1);
7832 for (int ix = 0; ix < fd->collapse; ix++)
7833 expr = fold_build2 (MULT_EXPR, diff_type, counts[ix].tile, expr);
7834 expr = force_gimple_operand_gsi (&gsi, expr, true,
7835 NULL_TREE, true, GSI_SAME_STMT);
7836 ass = gimple_build_assign (tile_size, expr);
7837 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7838
7839 element_s = create_tmp_var (diff_type, ".element_s");
7840 ass = gimple_build_assign (element_s, s);
7841 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7842
7843 expr = fold_build2 (MULT_EXPR, diff_type, s, tile_size);
7844 s = force_gimple_operand_gsi (&gsi, expr, true,
7845 NULL_TREE, true, GSI_SAME_STMT);
7846 }
7847
7848 /* Determine the range, avoiding possible unsigned->signed overflow. */
7849 negating = !up && TYPE_UNSIGNED (iter_type);
7850 expr = fold_build2 (MINUS_EXPR, plus_type,
7851 fold_convert (plus_type, negating ? b : e),
7852 fold_convert (plus_type, negating ? e : b));
7853 expr = fold_convert (diff_type, expr);
7854 if (negating)
7855 expr = fold_build1 (NEGATE_EXPR, diff_type, expr);
7856 tree range = force_gimple_operand_gsi (&gsi, expr, true,
7857 NULL_TREE, true, GSI_SAME_STMT);
7858
7859 chunk_no = build_int_cst (diff_type, 0);
7860 if (chunking)
7861 {
7862 gcc_assert (!gimple_in_ssa_p (cfun));
7863
7864 expr = chunk_no;
7865 chunk_max = create_tmp_var (diff_type, ".chunk_max");
7866 chunk_no = create_tmp_var (diff_type, ".chunk_no");
7867
7868 ass = gimple_build_assign (chunk_no, expr);
7869 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7870
7871 call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
7872 build_int_cst (integer_type_node,
7873 IFN_GOACC_LOOP_CHUNKS),
7874 dir, range, s, chunk_size, gwv);
7875 gimple_call_set_lhs (call, chunk_max);
7876 gimple_set_location (call, loc);
7877 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7878 }
7879 else
7880 chunk_size = chunk_no;
7881
7882 call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
7883 build_int_cst (integer_type_node,
7884 IFN_GOACC_LOOP_STEP),
7885 dir, range, s, chunk_size, gwv);
7886 gimple_call_set_lhs (call, step);
7887 gimple_set_location (call, loc);
7888 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7889
7890 /* Remove the GIMPLE_OMP_FOR. */
7891 gsi_remove (&gsi, true);
7892
7893 /* Fixup edges from head_bb. */
7894 be = BRANCH_EDGE (head_bb);
7895 fte = FALLTHRU_EDGE (head_bb);
7896 be->flags |= EDGE_FALSE_VALUE;
7897 fte->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
7898
7899 basic_block body_bb = fte->dest;
7900
7901 if (gimple_in_ssa_p (cfun))
7902 {
7903 gsi = gsi_last_nondebug_bb (cont_bb);
7904 gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
7905
7906 offset = gimple_omp_continue_control_use (cont_stmt);
7907 offset_incr = gimple_omp_continue_control_def (cont_stmt);
7908 }
7909 else
7910 {
7911 offset = create_tmp_var (diff_type, ".offset");
7912 offset_init = offset_incr = offset;
7913 }
7914 bound = create_tmp_var (TREE_TYPE (offset), ".bound");
7915
7916 /* Loop offset & bound go into head_bb. */
7917 gsi = gsi_start_bb (head_bb);
7918
7919 call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
7920 build_int_cst (integer_type_node,
7921 IFN_GOACC_LOOP_OFFSET),
7922 dir, range, s,
7923 chunk_size, gwv, chunk_no);
7924 gimple_call_set_lhs (call, offset_init);
7925 gimple_set_location (call, loc);
7926 gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
7927
7928 call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
7929 build_int_cst (integer_type_node,
7930 IFN_GOACC_LOOP_BOUND),
7931 dir, range, s,
7932 chunk_size, gwv, offset_init);
7933 gimple_call_set_lhs (call, bound);
7934 gimple_set_location (call, loc);
7935 gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
7936
7937 expr = build2 (cond_code, boolean_type_node, offset_init, bound);
7938 gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
7939 GSI_CONTINUE_LINKING);
7940
7941 /* V assignment goes into body_bb. */
7942 if (!gimple_in_ssa_p (cfun))
7943 {
7944 gsi = gsi_start_bb (body_bb);
7945
7946 expr = build2 (plus_code, iter_type, b,
7947 fold_convert (plus_type, offset));
7948 expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
7949 true, GSI_SAME_STMT);
7950 ass = gimple_build_assign (v, expr);
7951 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7952
7953 if (fd->collapse > 1 || fd->tiling)
7954 expand_oacc_collapse_vars (fd, false, &gsi, counts, v, diff_type);
7955
7956 if (fd->tiling)
7957 {
7958 /* Determine the range of the element loop -- usually simply
7959 the tile_size, but could be smaller if the final
7960 iteration of the outer loop is a partial tile. */
7961 tree e_range = create_tmp_var (diff_type, ".e_range");
7962
7963 expr = build2 (MIN_EXPR, diff_type,
7964 build2 (MINUS_EXPR, diff_type, bound, offset),
7965 build2 (MULT_EXPR, diff_type, tile_size,
7966 element_s));
7967 expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
7968 true, GSI_SAME_STMT);
7969 ass = gimple_build_assign (e_range, expr);
7970 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
7971
7972 /* Determine bound, offset & step of inner loop. */
7973 e_bound = create_tmp_var (diff_type, ".e_bound");
7974 e_offset = create_tmp_var (diff_type, ".e_offset");
7975 e_step = create_tmp_var (diff_type, ".e_step");
7976
7977 /* Mark these as element loops. */
7978 tree t, e_gwv = integer_minus_one_node;
7979 tree chunk = build_int_cst (diff_type, 0); /* Never chunked. */
7980
7981 t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_OFFSET);
7982 call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
7983 element_s, chunk, e_gwv, chunk);
7984 gimple_call_set_lhs (call, e_offset);
7985 gimple_set_location (call, loc);
7986 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7987
7988 t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_BOUND);
7989 call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
7990 element_s, chunk, e_gwv, e_offset);
7991 gimple_call_set_lhs (call, e_bound);
7992 gimple_set_location (call, loc);
7993 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
7994
7995 t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_STEP);
7996 call = gimple_build_call_internal (IFN_GOACC_LOOP, 6, t, dir, e_range,
7997 element_s, chunk, e_gwv);
7998 gimple_call_set_lhs (call, e_step);
7999 gimple_set_location (call, loc);
8000 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8001
8002 /* Add test and split block. */
8003 expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
8004 stmt = gimple_build_cond_empty (expr);
8005 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8006 split = split_block (body_bb, stmt);
8007 elem_body_bb = split->dest;
8008 if (cont_bb == body_bb)
8009 cont_bb = elem_body_bb;
8010 body_bb = split->src;
8011
8012 split->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
8013
8014 /* Add a dummy exit for the tiled block when cont_bb is missing. */
8015 if (cont_bb == NULL)
8016 {
8017 edge e = make_edge (body_bb, exit_bb, EDGE_FALSE_VALUE);
8018 e->probability = profile_probability::even ();
8019 split->probability = profile_probability::even ();
8020 }
8021
8022 /* Initialize the user's loop vars. */
8023 gsi = gsi_start_bb (elem_body_bb);
8024 expand_oacc_collapse_vars (fd, true, &gsi, counts, e_offset,
8025 diff_type);
8026 }
8027 }
8028
8029 /* Loop increment goes into cont_bb. If this is not a loop, we
8030 will have spawned threads as if it was, and each one will
8031 execute one iteration. The specification is not explicit about
8032 whether such constructs are ill-formed or not, and they can
8033 occur, especially when noreturn routines are involved. */
8034 if (cont_bb)
8035 {
8036 gsi = gsi_last_nondebug_bb (cont_bb);
8037 gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
8038 loc = gimple_location (cont_stmt);
8039
8040 if (fd->tiling)
8041 {
8042 /* Insert element loop increment and test. */
8043 expr = build2 (PLUS_EXPR, diff_type, e_offset, e_step);
8044 expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8045 true, GSI_SAME_STMT);
8046 ass = gimple_build_assign (e_offset, expr);
8047 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8048 expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
8049
8050 stmt = gimple_build_cond_empty (expr);
8051 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8052 split = split_block (cont_bb, stmt);
8053 elem_cont_bb = split->src;
8054 cont_bb = split->dest;
8055
8056 split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8057 split->probability = profile_probability::unlikely ().guessed ();
8058 edge latch_edge
8059 = make_edge (elem_cont_bb, elem_body_bb, EDGE_TRUE_VALUE);
8060 latch_edge->probability = profile_probability::likely ().guessed ();
8061
8062 edge skip_edge = make_edge (body_bb, cont_bb, EDGE_FALSE_VALUE);
8063 skip_edge->probability = profile_probability::unlikely ().guessed ();
8064 edge loop_entry_edge = EDGE_SUCC (body_bb, 1 - skip_edge->dest_idx);
8065 loop_entry_edge->probability
8066 = profile_probability::likely ().guessed ();
8067
8068 gsi = gsi_for_stmt (cont_stmt);
8069 }
8070
8071 /* Increment offset. */
8072 if (gimple_in_ssa_p (cfun))
8073 expr = build2 (plus_code, iter_type, offset,
8074 fold_convert (plus_type, step));
8075 else
8076 expr = build2 (PLUS_EXPR, diff_type, offset, step);
8077 expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8078 true, GSI_SAME_STMT);
8079 ass = gimple_build_assign (offset_incr, expr);
8080 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8081 expr = build2 (cond_code, boolean_type_node, offset_incr, bound);
8082 gsi_insert_before (&gsi, gimple_build_cond_empty (expr), GSI_SAME_STMT);
8083
8084 /* Remove the GIMPLE_OMP_CONTINUE. */
8085 gsi_remove (&gsi, true);
8086
8087 /* Fixup edges from cont_bb. */
8088 be = BRANCH_EDGE (cont_bb);
8089 fte = FALLTHRU_EDGE (cont_bb);
8090 be->flags |= EDGE_TRUE_VALUE;
8091 fte->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8092
8093 if (chunking)
8094 {
8095 /* Split the beginning of exit_bb to make bottom_bb. We
8096 need to insert a nop at the start, because splitting is
8097 after a stmt, not before. */
8098 gsi = gsi_start_bb (exit_bb);
8099 stmt = gimple_build_nop ();
8100 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8101 split = split_block (exit_bb, stmt);
8102 bottom_bb = split->src;
8103 exit_bb = split->dest;
8104 gsi = gsi_last_bb (bottom_bb);
8105
8106 /* Chunk increment and test goes into bottom_bb. */
8107 expr = build2 (PLUS_EXPR, diff_type, chunk_no,
8108 build_int_cst (diff_type, 1));
8109 ass = gimple_build_assign (chunk_no, expr);
8110 gsi_insert_after (&gsi, ass, GSI_CONTINUE_LINKING);
8111
8112 /* Chunk test at end of bottom_bb. */
8113 expr = build2 (LT_EXPR, boolean_type_node, chunk_no, chunk_max);
8114 gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
8115 GSI_CONTINUE_LINKING);
8116
8117 /* Fixup edges from bottom_bb. */
8118 split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
8119 split->probability = profile_probability::unlikely ().guessed ();
8120 edge latch_edge = make_edge (bottom_bb, head_bb, EDGE_TRUE_VALUE);
8121 latch_edge->probability = profile_probability::likely ().guessed ();
8122 }
8123 }
8124
8125 gsi = gsi_last_nondebug_bb (exit_bb);
8126 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
8127 loc = gimple_location (gsi_stmt (gsi));
8128
8129 if (!gimple_in_ssa_p (cfun))
8130 {
8131 /* Insert the final value of V, in case it is live. This is the
8132 value for the only thread that survives past the join. */
8133 expr = fold_build2 (MINUS_EXPR, diff_type, range, dir);
8134 expr = fold_build2 (PLUS_EXPR, diff_type, expr, s);
8135 expr = fold_build2 (TRUNC_DIV_EXPR, diff_type, expr, s);
8136 expr = fold_build2 (MULT_EXPR, diff_type, expr, s);
8137 expr = build2 (plus_code, iter_type, b, fold_convert (plus_type, expr));
8138 expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
8139 true, GSI_SAME_STMT);
8140 ass = gimple_build_assign (v, expr);
8141 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
8142 }
8143
8144 /* Remove the OMP_RETURN. */
8145 gsi_remove (&gsi, true);
8146
8147 if (cont_bb)
8148 {
8149 /* We now have one, two or three nested loops. Update the loop
8150 structures. */
8151 class loop *parent = entry_bb->loop_father;
8152 class loop *body = body_bb->loop_father;
8153
8154 if (chunking)
8155 {
8156 class loop *chunk_loop = alloc_loop ();
8157 chunk_loop->header = head_bb;
8158 chunk_loop->latch = bottom_bb;
8159 add_loop (chunk_loop, parent);
8160 parent = chunk_loop;
8161 }
8162 else if (parent != body)
8163 {
8164 gcc_assert (body->header == body_bb);
8165 gcc_assert (body->latch == cont_bb
8166 || single_pred (body->latch) == cont_bb);
8167 parent = NULL;
8168 }
8169
8170 if (parent)
8171 {
8172 class loop *body_loop = alloc_loop ();
8173 body_loop->header = body_bb;
8174 body_loop->latch = cont_bb;
8175 add_loop (body_loop, parent);
8176
8177 if (fd->tiling)
8178 {
8179 /* Insert tiling's element loop. */
8180 class loop *inner_loop = alloc_loop ();
8181 inner_loop->header = elem_body_bb;
8182 inner_loop->latch = elem_cont_bb;
8183 add_loop (inner_loop, body_loop);
8184 }
8185 }
8186 }
8187 }
8188
8189 /* Expand the OMP loop defined by REGION. */
8190
8191 static void
8192 expand_omp_for (struct omp_region *region, gimple *inner_stmt)
8193 {
8194 struct omp_for_data fd;
8195 struct omp_for_data_loop *loops;
8196
8197 loops = XALLOCAVEC (struct omp_for_data_loop,
8198 gimple_omp_for_collapse
8199 (last_nondebug_stmt (region->entry)));
8200 omp_extract_for_data (as_a <gomp_for *> (last_nondebug_stmt (region->entry)),
8201 &fd, loops);
8202 region->sched_kind = fd.sched_kind;
8203 region->sched_modifiers = fd.sched_modifiers;
8204 region->has_lastprivate_conditional = fd.lastprivate_conditional != 0;
8205 if (fd.non_rect && !gimple_omp_for_combined_into_p (fd.for_stmt))
8206 {
8207 for (int i = fd.first_nonrect; i <= fd.last_nonrect; i++)
8208 if ((loops[i].m1 || loops[i].m2)
8209 && (loops[i].m1 == NULL_TREE
8210 || TREE_CODE (loops[i].m1) == INTEGER_CST)
8211 && (loops[i].m2 == NULL_TREE
8212 || TREE_CODE (loops[i].m2) == INTEGER_CST)
8213 && TREE_CODE (loops[i].step) == INTEGER_CST
8214 && TREE_CODE (loops[i - loops[i].outer].step) == INTEGER_CST)
8215 {
8216 tree t;
8217 tree itype = TREE_TYPE (loops[i].v);
8218 if (loops[i].m1 && loops[i].m2)
8219 t = fold_build2 (MINUS_EXPR, itype, loops[i].m2, loops[i].m1);
8220 else if (loops[i].m1)
8221 t = fold_build1 (NEGATE_EXPR, itype, loops[i].m1);
8222 else
8223 t = loops[i].m2;
8224 t = fold_build2 (MULT_EXPR, itype, t,
8225 fold_convert (itype,
8226 loops[i - loops[i].outer].step));
8227 if (TYPE_UNSIGNED (itype) && loops[i].cond_code == GT_EXPR)
8228 t = fold_build2 (TRUNC_MOD_EXPR, itype,
8229 fold_build1 (NEGATE_EXPR, itype, t),
8230 fold_build1 (NEGATE_EXPR, itype,
8231 fold_convert (itype,
8232 loops[i].step)));
8233 else
8234 t = fold_build2 (TRUNC_MOD_EXPR, itype, t,
8235 fold_convert (itype, loops[i].step));
8236 if (integer_nonzerop (t))
8237 error_at (gimple_location (fd.for_stmt),
8238 "invalid OpenMP non-rectangular loop step; "
8239 "%<(%E - %E) * %E%> is not a multiple of loop %d "
8240 "step %qE",
8241 loops[i].m2 ? loops[i].m2 : integer_zero_node,
8242 loops[i].m1 ? loops[i].m1 : integer_zero_node,
8243 loops[i - loops[i].outer].step, i + 1,
8244 loops[i].step);
8245 }
8246 }
8247
8248 gcc_assert (EDGE_COUNT (region->entry->succs) == 2);
8249 BRANCH_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
8250 FALLTHRU_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
8251 if (region->cont)
8252 {
8253 gcc_assert (EDGE_COUNT (region->cont->succs) == 2);
8254 BRANCH_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
8255 FALLTHRU_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
8256 }
8257 else
8258 /* If there isn't a continue then this is a degerate case where
8259 the introduction of abnormal edges during lowering will prevent
8260 original loops from being detected. Fix that up. */
8261 loops_state_set (LOOPS_NEED_FIXUP);
8262
8263 if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_SIMD)
8264 expand_omp_simd (region, &fd);
8265 else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_OACC_LOOP)
8266 {
8267 gcc_assert (!inner_stmt && !fd.non_rect);
8268 expand_oacc_for (region, &fd);
8269 }
8270 else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_TASKLOOP)
8271 {
8272 if (gimple_omp_for_combined_into_p (fd.for_stmt))
8273 expand_omp_taskloop_for_inner (region, &fd, inner_stmt);
8274 else
8275 expand_omp_taskloop_for_outer (region, &fd, inner_stmt);
8276 }
8277 else if (fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC
8278 && !fd.have_ordered)
8279 {
8280 if (fd.chunk_size == NULL)
8281 expand_omp_for_static_nochunk (region, &fd, inner_stmt);
8282 else
8283 expand_omp_for_static_chunk (region, &fd, inner_stmt);
8284 }
8285 else
8286 {
8287 int fn_index, start_ix, next_ix;
8288 unsigned HOST_WIDE_INT sched = 0;
8289 tree sched_arg = NULL_TREE;
8290
8291 gcc_assert (gimple_omp_for_kind (fd.for_stmt)
8292 == GF_OMP_FOR_KIND_FOR && !fd.non_rect);
8293 if (fd.chunk_size == NULL
8294 && fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC)
8295 fd.chunk_size = integer_zero_node;
8296 switch (fd.sched_kind)
8297 {
8298 case OMP_CLAUSE_SCHEDULE_RUNTIME:
8299 if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_NONMONOTONIC) != 0
8300 && fd.lastprivate_conditional == 0)
8301 {
8302 gcc_assert (!fd.have_ordered);
8303 fn_index = 6;
8304 sched = 4;
8305 }
8306 else if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
8307 && !fd.have_ordered
8308 && fd.lastprivate_conditional == 0)
8309 fn_index = 7;
8310 else
8311 {
8312 fn_index = 3;
8313 sched = (HOST_WIDE_INT_1U << 31);
8314 }
8315 break;
8316 case OMP_CLAUSE_SCHEDULE_DYNAMIC:
8317 case OMP_CLAUSE_SCHEDULE_GUIDED:
8318 if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
8319 && !fd.have_ordered
8320 && fd.lastprivate_conditional == 0)
8321 {
8322 fn_index = 3 + fd.sched_kind;
8323 sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
8324 break;
8325 }
8326 fn_index = fd.sched_kind;
8327 sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
8328 sched += (HOST_WIDE_INT_1U << 31);
8329 break;
8330 case OMP_CLAUSE_SCHEDULE_STATIC:
8331 gcc_assert (fd.have_ordered);
8332 fn_index = 0;
8333 sched = (HOST_WIDE_INT_1U << 31) + 1;
8334 break;
8335 default:
8336 gcc_unreachable ();
8337 }
8338 if (!fd.ordered)
8339 fn_index += fd.have_ordered * 8;
8340 if (fd.ordered)
8341 start_ix = ((int)BUILT_IN_GOMP_LOOP_DOACROSS_STATIC_START) + fn_index;
8342 else
8343 start_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_START) + fn_index;
8344 next_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_NEXT) + fn_index;
8345 if (fd.have_reductemp || fd.have_pointer_condtemp)
8346 {
8347 if (fd.ordered)
8348 start_ix = (int)BUILT_IN_GOMP_LOOP_DOACROSS_START;
8349 else if (fd.have_ordered)
8350 start_ix = (int)BUILT_IN_GOMP_LOOP_ORDERED_START;
8351 else
8352 start_ix = (int)BUILT_IN_GOMP_LOOP_START;
8353 sched_arg = build_int_cstu (long_integer_type_node, sched);
8354 if (!fd.chunk_size)
8355 fd.chunk_size = integer_zero_node;
8356 }
8357 if (fd.iter_type == long_long_unsigned_type_node)
8358 {
8359 start_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_START
8360 - (int)BUILT_IN_GOMP_LOOP_STATIC_START);
8361 next_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_NEXT
8362 - (int)BUILT_IN_GOMP_LOOP_STATIC_NEXT);
8363 }
8364 expand_omp_for_generic (region, &fd, (enum built_in_function) start_ix,
8365 (enum built_in_function) next_ix, sched_arg,
8366 inner_stmt);
8367 }
8368 }
8369
8370 /* Expand code for an OpenMP sections directive. In pseudo code, we generate
8371
8372 v = GOMP_sections_start (n);
8373 L0:
8374 switch (v)
8375 {
8376 case 0:
8377 goto L2;
8378 case 1:
8379 section 1;
8380 goto L1;
8381 case 2:
8382 ...
8383 case n:
8384 ...
8385 default:
8386 abort ();
8387 }
8388 L1:
8389 v = GOMP_sections_next ();
8390 goto L0;
8391 L2:
8392 reduction;
8393
8394 If this is a combined parallel sections, replace the call to
8395 GOMP_sections_start with call to GOMP_sections_next. */
8396
8397 static void
8398 expand_omp_sections (struct omp_region *region)
8399 {
8400 tree t, u, vin = NULL, vmain, vnext, l2;
8401 unsigned len;
8402 basic_block entry_bb, l0_bb, l1_bb, l2_bb, default_bb;
8403 gimple_stmt_iterator si, switch_si;
8404 gomp_sections *sections_stmt;
8405 gimple *stmt;
8406 gomp_continue *cont;
8407 edge_iterator ei;
8408 edge e;
8409 struct omp_region *inner;
8410 unsigned i, casei;
8411 bool exit_reachable = region->cont != NULL;
8412
8413 gcc_assert (region->exit != NULL);
8414 entry_bb = region->entry;
8415 l0_bb = single_succ (entry_bb);
8416 l1_bb = region->cont;
8417 l2_bb = region->exit;
8418 if (single_pred_p (l2_bb) && single_pred (l2_bb) == l0_bb)
8419 l2 = gimple_block_label (l2_bb);
8420 else
8421 {
8422 /* This can happen if there are reductions. */
8423 len = EDGE_COUNT (l0_bb->succs);
8424 gcc_assert (len > 0);
8425 e = EDGE_SUCC (l0_bb, len - 1);
8426 si = gsi_last_nondebug_bb (e->dest);
8427 l2 = NULL_TREE;
8428 if (gsi_end_p (si)
8429 || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
8430 l2 = gimple_block_label (e->dest);
8431 else
8432 FOR_EACH_EDGE (e, ei, l0_bb->succs)
8433 {
8434 si = gsi_last_nondebug_bb (e->dest);
8435 if (gsi_end_p (si)
8436 || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
8437 {
8438 l2 = gimple_block_label (e->dest);
8439 break;
8440 }
8441 }
8442 }
8443 if (exit_reachable)
8444 default_bb = create_empty_bb (l1_bb->prev_bb);
8445 else
8446 default_bb = create_empty_bb (l0_bb);
8447
8448 /* We will build a switch() with enough cases for all the
8449 GIMPLE_OMP_SECTION regions, a '0' case to handle the end of more work
8450 and a default case to abort if something goes wrong. */
8451 len = EDGE_COUNT (l0_bb->succs);
8452
8453 /* Use vec::quick_push on label_vec throughout, since we know the size
8454 in advance. */
8455 auto_vec<tree> label_vec (len);
8456
8457 /* The call to GOMP_sections_start goes in ENTRY_BB, replacing the
8458 GIMPLE_OMP_SECTIONS statement. */
8459 si = gsi_last_nondebug_bb (entry_bb);
8460 sections_stmt = as_a <gomp_sections *> (gsi_stmt (si));
8461 gcc_assert (gimple_code (sections_stmt) == GIMPLE_OMP_SECTIONS);
8462 vin = gimple_omp_sections_control (sections_stmt);
8463 tree clauses = gimple_omp_sections_clauses (sections_stmt);
8464 tree reductmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
8465 tree condtmp = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
8466 tree cond_var = NULL_TREE;
8467 if (reductmp || condtmp)
8468 {
8469 tree reductions = null_pointer_node, mem = null_pointer_node;
8470 tree memv = NULL_TREE, condtemp = NULL_TREE;
8471 gimple_stmt_iterator gsi = gsi_none ();
8472 gimple *g = NULL;
8473 if (reductmp)
8474 {
8475 reductions = OMP_CLAUSE_DECL (reductmp);
8476 gcc_assert (TREE_CODE (reductions) == SSA_NAME);
8477 g = SSA_NAME_DEF_STMT (reductions);
8478 reductions = gimple_assign_rhs1 (g);
8479 OMP_CLAUSE_DECL (reductmp) = reductions;
8480 gsi = gsi_for_stmt (g);
8481 }
8482 else
8483 gsi = si;
8484 if (condtmp)
8485 {
8486 condtemp = OMP_CLAUSE_DECL (condtmp);
8487 tree c = omp_find_clause (OMP_CLAUSE_CHAIN (condtmp),
8488 OMP_CLAUSE__CONDTEMP_);
8489 cond_var = OMP_CLAUSE_DECL (c);
8490 tree type = TREE_TYPE (condtemp);
8491 memv = create_tmp_var (type);
8492 TREE_ADDRESSABLE (memv) = 1;
8493 unsigned cnt = 0;
8494 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
8495 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8496 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8497 ++cnt;
8498 unsigned HOST_WIDE_INT sz
8499 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type))) * cnt;
8500 expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
8501 false);
8502 mem = build_fold_addr_expr (memv);
8503 }
8504 t = build_int_cst (unsigned_type_node, len - 1);
8505 u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS2_START);
8506 stmt = gimple_build_call (u, 3, t, reductions, mem);
8507 gimple_call_set_lhs (stmt, vin);
8508 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
8509 if (condtmp)
8510 {
8511 expand_omp_build_assign (&gsi, condtemp, memv, false);
8512 tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
8513 vin, build_one_cst (TREE_TYPE (cond_var)));
8514 expand_omp_build_assign (&gsi, cond_var, t, false);
8515 }
8516 if (reductmp)
8517 {
8518 gsi_remove (&gsi, true);
8519 release_ssa_name (gimple_assign_lhs (g));
8520 }
8521 }
8522 else if (!is_combined_parallel (region))
8523 {
8524 /* If we are not inside a combined parallel+sections region,
8525 call GOMP_sections_start. */
8526 t = build_int_cst (unsigned_type_node, len - 1);
8527 u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_START);
8528 stmt = gimple_build_call (u, 1, t);
8529 }
8530 else
8531 {
8532 /* Otherwise, call GOMP_sections_next. */
8533 u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
8534 stmt = gimple_build_call (u, 0);
8535 }
8536 if (!reductmp && !condtmp)
8537 {
8538 gimple_call_set_lhs (stmt, vin);
8539 gsi_insert_after (&si, stmt, GSI_SAME_STMT);
8540 }
8541 gsi_remove (&si, true);
8542
8543 /* The switch() statement replacing GIMPLE_OMP_SECTIONS_SWITCH goes in
8544 L0_BB. */
8545 switch_si = gsi_last_nondebug_bb (l0_bb);
8546 gcc_assert (gimple_code (gsi_stmt (switch_si)) == GIMPLE_OMP_SECTIONS_SWITCH);
8547 if (exit_reachable)
8548 {
8549 cont = as_a <gomp_continue *> (last_nondebug_stmt (l1_bb));
8550 gcc_assert (gimple_code (cont) == GIMPLE_OMP_CONTINUE);
8551 vmain = gimple_omp_continue_control_use (cont);
8552 vnext = gimple_omp_continue_control_def (cont);
8553 }
8554 else
8555 {
8556 vmain = vin;
8557 vnext = NULL_TREE;
8558 }
8559
8560 t = build_case_label (build_int_cst (unsigned_type_node, 0), NULL, l2);
8561 label_vec.quick_push (t);
8562 i = 1;
8563
8564 /* Convert each GIMPLE_OMP_SECTION into a CASE_LABEL_EXPR. */
8565 for (inner = region->inner, casei = 1;
8566 inner;
8567 inner = inner->next, i++, casei++)
8568 {
8569 basic_block s_entry_bb, s_exit_bb;
8570
8571 /* Skip optional reduction region. */
8572 if (inner->type == GIMPLE_OMP_ATOMIC_LOAD)
8573 {
8574 --i;
8575 --casei;
8576 continue;
8577 }
8578
8579 s_entry_bb = inner->entry;
8580 s_exit_bb = inner->exit;
8581
8582 t = gimple_block_label (s_entry_bb);
8583 u = build_int_cst (unsigned_type_node, casei);
8584 u = build_case_label (u, NULL, t);
8585 label_vec.quick_push (u);
8586
8587 si = gsi_last_nondebug_bb (s_entry_bb);
8588 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SECTION);
8589 gcc_assert (i < len || gimple_omp_section_last_p (gsi_stmt (si)));
8590 gsi_remove (&si, true);
8591 single_succ_edge (s_entry_bb)->flags = EDGE_FALLTHRU;
8592
8593 if (s_exit_bb == NULL)
8594 continue;
8595
8596 si = gsi_last_nondebug_bb (s_exit_bb);
8597 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
8598 gsi_remove (&si, true);
8599
8600 single_succ_edge (s_exit_bb)->flags = EDGE_FALLTHRU;
8601 }
8602
8603 /* Error handling code goes in DEFAULT_BB. */
8604 t = gimple_block_label (default_bb);
8605 u = build_case_label (NULL, NULL, t);
8606 make_edge (l0_bb, default_bb, 0);
8607 add_bb_to_loop (default_bb, current_loops->tree_root);
8608
8609 stmt = gimple_build_switch (vmain, u, label_vec);
8610 gsi_insert_after (&switch_si, stmt, GSI_SAME_STMT);
8611 gsi_remove (&switch_si, true);
8612
8613 si = gsi_start_bb (default_bb);
8614 stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
8615 gsi_insert_after (&si, stmt, GSI_CONTINUE_LINKING);
8616
8617 if (exit_reachable)
8618 {
8619 tree bfn_decl;
8620
8621 /* Code to get the next section goes in L1_BB. */
8622 si = gsi_last_nondebug_bb (l1_bb);
8623 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CONTINUE);
8624
8625 bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
8626 stmt = gimple_build_call (bfn_decl, 0);
8627 gimple_call_set_lhs (stmt, vnext);
8628 gsi_insert_before (&si, stmt, GSI_SAME_STMT);
8629 if (cond_var)
8630 {
8631 tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
8632 vnext, build_one_cst (TREE_TYPE (cond_var)));
8633 expand_omp_build_assign (&si, cond_var, t, false);
8634 }
8635 gsi_remove (&si, true);
8636
8637 single_succ_edge (l1_bb)->flags = EDGE_FALLTHRU;
8638 }
8639
8640 /* Cleanup function replaces GIMPLE_OMP_RETURN in EXIT_BB. */
8641 si = gsi_last_nondebug_bb (l2_bb);
8642 if (gimple_omp_return_nowait_p (gsi_stmt (si)))
8643 t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_NOWAIT);
8644 else if (gimple_omp_return_lhs (gsi_stmt (si)))
8645 t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_CANCEL);
8646 else
8647 t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END);
8648 stmt = gimple_build_call (t, 0);
8649 if (gimple_omp_return_lhs (gsi_stmt (si)))
8650 gimple_call_set_lhs (stmt, gimple_omp_return_lhs (gsi_stmt (si)));
8651 gsi_insert_after (&si, stmt, GSI_SAME_STMT);
8652 gsi_remove (&si, true);
8653
8654 set_immediate_dominator (CDI_DOMINATORS, default_bb, l0_bb);
8655 }
8656
8657 /* Expand code for an OpenMP single or scope directive. We've already expanded
8658 much of the code, here we simply place the GOMP_barrier call. */
8659
8660 static void
8661 expand_omp_single (struct omp_region *region)
8662 {
8663 basic_block entry_bb, exit_bb;
8664 gimple_stmt_iterator si;
8665
8666 entry_bb = region->entry;
8667 exit_bb = region->exit;
8668
8669 si = gsi_last_nondebug_bb (entry_bb);
8670 enum gimple_code code = gimple_code (gsi_stmt (si));
8671 gcc_assert (code == GIMPLE_OMP_SINGLE || code == GIMPLE_OMP_SCOPE);
8672 gsi_remove (&si, true);
8673 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
8674
8675 if (exit_bb == NULL)
8676 {
8677 gcc_assert (code == GIMPLE_OMP_SCOPE);
8678 return;
8679 }
8680
8681 si = gsi_last_nondebug_bb (exit_bb);
8682 if (!gimple_omp_return_nowait_p (gsi_stmt (si)))
8683 {
8684 tree t = gimple_omp_return_lhs (gsi_stmt (si));
8685 gsi_insert_after (&si, omp_build_barrier (t), GSI_SAME_STMT);
8686 }
8687 gsi_remove (&si, true);
8688 single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
8689 }
8690
8691 /* Generic expansion for OpenMP synchronization directives: master,
8692 ordered and critical. All we need to do here is remove the entry
8693 and exit markers for REGION. */
8694
8695 static void
8696 expand_omp_synch (struct omp_region *region)
8697 {
8698 basic_block entry_bb, exit_bb;
8699 gimple_stmt_iterator si;
8700
8701 entry_bb = region->entry;
8702 exit_bb = region->exit;
8703
8704 si = gsi_last_nondebug_bb (entry_bb);
8705 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SINGLE
8706 || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASTER
8707 || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASKED
8708 || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TASKGROUP
8709 || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ORDERED
8710 || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CRITICAL
8711 || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS);
8712 if (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS
8713 && gimple_omp_teams_host (as_a <gomp_teams *> (gsi_stmt (si))))
8714 {
8715 expand_omp_taskreg (region);
8716 return;
8717 }
8718 gsi_remove (&si, true);
8719 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
8720
8721 if (exit_bb)
8722 {
8723 si = gsi_last_nondebug_bb (exit_bb);
8724 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
8725 gsi_remove (&si, true);
8726 single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
8727 }
8728 }
8729
8730 /* Translate enum omp_memory_order to enum memmodel for the embedded
8731 fail clause in there. */
8732
8733 static enum memmodel
8734 omp_memory_order_to_fail_memmodel (enum omp_memory_order mo)
8735 {
8736 switch (mo & OMP_FAIL_MEMORY_ORDER_MASK)
8737 {
8738 case OMP_FAIL_MEMORY_ORDER_UNSPECIFIED:
8739 switch (mo & OMP_MEMORY_ORDER_MASK)
8740 {
8741 case OMP_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
8742 case OMP_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
8743 case OMP_MEMORY_ORDER_RELEASE: return MEMMODEL_RELAXED;
8744 case OMP_MEMORY_ORDER_ACQ_REL: return MEMMODEL_ACQUIRE;
8745 case OMP_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
8746 default: break;
8747 }
8748 gcc_unreachable ();
8749 case OMP_FAIL_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
8750 case OMP_FAIL_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
8751 case OMP_FAIL_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
8752 default: gcc_unreachable ();
8753 }
8754 }
8755
8756 /* Translate enum omp_memory_order to enum memmodel. The two enums
8757 are using different numbers so that OMP_MEMORY_ORDER_UNSPECIFIED
8758 is 0 and omp_memory_order has the fail mode encoded in it too. */
8759
8760 static enum memmodel
8761 omp_memory_order_to_memmodel (enum omp_memory_order mo)
8762 {
8763 enum memmodel ret, fail_ret;
8764 switch (mo & OMP_MEMORY_ORDER_MASK)
8765 {
8766 case OMP_MEMORY_ORDER_RELAXED: ret = MEMMODEL_RELAXED; break;
8767 case OMP_MEMORY_ORDER_ACQUIRE: ret = MEMMODEL_ACQUIRE; break;
8768 case OMP_MEMORY_ORDER_RELEASE: ret = MEMMODEL_RELEASE; break;
8769 case OMP_MEMORY_ORDER_ACQ_REL: ret = MEMMODEL_ACQ_REL; break;
8770 case OMP_MEMORY_ORDER_SEQ_CST: ret = MEMMODEL_SEQ_CST; break;
8771 default: gcc_unreachable ();
8772 }
8773 /* If we drop the -Winvalid-memory-model warning for C++17 P0418R2,
8774 we can just return ret here unconditionally. Otherwise, work around
8775 it here and make sure fail memmodel is not stronger. */
8776 if ((mo & OMP_FAIL_MEMORY_ORDER_MASK) == OMP_FAIL_MEMORY_ORDER_UNSPECIFIED)
8777 return ret;
8778 fail_ret = omp_memory_order_to_fail_memmodel (mo);
8779 if (fail_ret > ret)
8780 return fail_ret;
8781 return ret;
8782 }
8783
8784 /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8785 operation as a normal volatile load. */
8786
8787 static bool
8788 expand_omp_atomic_load (basic_block load_bb, tree addr,
8789 tree loaded_val, int index)
8790 {
8791 enum built_in_function tmpbase;
8792 gimple_stmt_iterator gsi;
8793 basic_block store_bb;
8794 location_t loc;
8795 gimple *stmt;
8796 tree decl, type, itype;
8797
8798 gsi = gsi_last_nondebug_bb (load_bb);
8799 stmt = gsi_stmt (gsi);
8800 gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
8801 loc = gimple_location (stmt);
8802
8803 /* ??? If the target does not implement atomic_load_optab[mode], and mode
8804 is smaller than word size, then expand_atomic_load assumes that the load
8805 is atomic. We could avoid the builtin entirely in this case. */
8806
8807 tmpbase = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
8808 decl = builtin_decl_explicit (tmpbase);
8809 if (decl == NULL_TREE)
8810 return false;
8811
8812 type = TREE_TYPE (loaded_val);
8813 itype = TREE_TYPE (TREE_TYPE (decl));
8814
8815 enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
8816 tree mo = build_int_cst (integer_type_node,
8817 omp_memory_order_to_memmodel (omo));
8818 gcall *call = gimple_build_call (decl, 2, addr, mo);
8819 gimple_set_location (call, loc);
8820 gimple_set_vuse (call, gimple_vuse (stmt));
8821 gimple *repl;
8822 if (!useless_type_conversion_p (type, itype))
8823 {
8824 tree lhs = make_ssa_name (itype);
8825 gimple_call_set_lhs (call, lhs);
8826 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8827 repl = gimple_build_assign (loaded_val,
8828 build1 (VIEW_CONVERT_EXPR, type, lhs));
8829 gimple_set_location (repl, loc);
8830 }
8831 else
8832 {
8833 gimple_call_set_lhs (call, loaded_val);
8834 repl = call;
8835 }
8836 gsi_replace (&gsi, repl, true);
8837
8838 store_bb = single_succ (load_bb);
8839 gsi = gsi_last_nondebug_bb (store_bb);
8840 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
8841 gsi_remove (&gsi, true);
8842
8843 return true;
8844 }
8845
8846 /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8847 operation as a normal volatile store. */
8848
8849 static bool
8850 expand_omp_atomic_store (basic_block load_bb, tree addr,
8851 tree loaded_val, tree stored_val, int index)
8852 {
8853 enum built_in_function tmpbase;
8854 gimple_stmt_iterator gsi;
8855 basic_block store_bb = single_succ (load_bb);
8856 location_t loc;
8857 gimple *stmt;
8858 tree decl, type, itype;
8859 machine_mode imode;
8860 bool exchange;
8861
8862 gsi = gsi_last_nondebug_bb (load_bb);
8863 stmt = gsi_stmt (gsi);
8864 gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
8865
8866 /* If the load value is needed, then this isn't a store but an exchange. */
8867 exchange = gimple_omp_atomic_need_value_p (stmt);
8868
8869 gsi = gsi_last_nondebug_bb (store_bb);
8870 stmt = gsi_stmt (gsi);
8871 gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE);
8872 loc = gimple_location (stmt);
8873
8874 /* ??? If the target does not implement atomic_store_optab[mode], and mode
8875 is smaller than word size, then expand_atomic_store assumes that the store
8876 is atomic. We could avoid the builtin entirely in this case. */
8877
8878 tmpbase = (exchange ? BUILT_IN_ATOMIC_EXCHANGE_N : BUILT_IN_ATOMIC_STORE_N);
8879 tmpbase = (enum built_in_function) ((int) tmpbase + index + 1);
8880 decl = builtin_decl_explicit (tmpbase);
8881 if (decl == NULL_TREE)
8882 return false;
8883
8884 type = TREE_TYPE (stored_val);
8885
8886 /* Dig out the type of the function's second argument. */
8887 itype = TREE_TYPE (decl);
8888 itype = TYPE_ARG_TYPES (itype);
8889 itype = TREE_CHAIN (itype);
8890 itype = TREE_VALUE (itype);
8891 imode = TYPE_MODE (itype);
8892
8893 if (exchange && !can_atomic_exchange_p (imode, true))
8894 return false;
8895
8896 if (!useless_type_conversion_p (itype, type))
8897 stored_val = fold_build1_loc (loc, VIEW_CONVERT_EXPR, itype, stored_val);
8898 enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
8899 tree mo = build_int_cst (integer_type_node,
8900 omp_memory_order_to_memmodel (omo));
8901 stored_val = force_gimple_operand_gsi (&gsi, stored_val, true, NULL_TREE,
8902 true, GSI_SAME_STMT);
8903 gcall *call = gimple_build_call (decl, 3, addr, stored_val, mo);
8904 gimple_set_location (call, loc);
8905 gimple_set_vuse (call, gimple_vuse (stmt));
8906 gimple_set_vdef (call, gimple_vdef (stmt));
8907
8908 gimple *repl = call;
8909 if (exchange)
8910 {
8911 if (!useless_type_conversion_p (type, itype))
8912 {
8913 tree lhs = make_ssa_name (itype);
8914 gimple_call_set_lhs (call, lhs);
8915 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
8916 repl = gimple_build_assign (loaded_val,
8917 build1 (VIEW_CONVERT_EXPR, type, lhs));
8918 gimple_set_location (repl, loc);
8919 }
8920 else
8921 gimple_call_set_lhs (call, loaded_val);
8922 }
8923 gsi_replace (&gsi, repl, true);
8924
8925 /* Remove the GIMPLE_OMP_ATOMIC_LOAD that we verified above. */
8926 gsi = gsi_last_nondebug_bb (load_bb);
8927 gsi_remove (&gsi, true);
8928
8929 return true;
8930 }
8931
8932 /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
8933 operation as a __atomic_fetch_op builtin. INDEX is log2 of the
8934 size of the data type, and thus usable to find the index of the builtin
8935 decl. Returns false if the expression is not of the proper form. */
8936
8937 static bool
8938 expand_omp_atomic_fetch_op (basic_block load_bb,
8939 tree addr, tree loaded_val,
8940 tree stored_val, int index)
8941 {
8942 enum built_in_function oldbase, newbase, tmpbase;
8943 tree decl, itype, call;
8944 tree lhs, rhs;
8945 basic_block store_bb = single_succ (load_bb);
8946 gimple_stmt_iterator gsi;
8947 gimple *stmt;
8948 location_t loc;
8949 enum tree_code code;
8950 bool need_old, need_new;
8951 machine_mode imode;
8952
8953 /* We expect to find the following sequences:
8954
8955 load_bb:
8956 GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
8957
8958 store_bb:
8959 val = tmp OP something; (or: something OP tmp)
8960 GIMPLE_OMP_STORE (val)
8961
8962 ???FIXME: Allow a more flexible sequence.
8963 Perhaps use data flow to pick the statements.
8964
8965 */
8966
8967 gsi = gsi_after_labels (store_bb);
8968 stmt = gsi_stmt (gsi);
8969 if (is_gimple_debug (stmt))
8970 {
8971 gsi_next_nondebug (&gsi);
8972 if (gsi_end_p (gsi))
8973 return false;
8974 stmt = gsi_stmt (gsi);
8975 }
8976 loc = gimple_location (stmt);
8977 if (!is_gimple_assign (stmt))
8978 return false;
8979 gsi_next_nondebug (&gsi);
8980 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_OMP_ATOMIC_STORE)
8981 return false;
8982 need_new = gimple_omp_atomic_need_value_p (gsi_stmt (gsi));
8983 need_old = gimple_omp_atomic_need_value_p (last_nondebug_stmt (load_bb));
8984 enum omp_memory_order omo
8985 = gimple_omp_atomic_memory_order (last_nondebug_stmt (load_bb));
8986 enum memmodel mo = omp_memory_order_to_memmodel (omo);
8987 gcc_checking_assert (!need_old || !need_new);
8988
8989 if (!operand_equal_p (gimple_assign_lhs (stmt), stored_val, 0))
8990 return false;
8991
8992 /* Check for one of the supported fetch-op operations. */
8993 code = gimple_assign_rhs_code (stmt);
8994 switch (code)
8995 {
8996 case PLUS_EXPR:
8997 case POINTER_PLUS_EXPR:
8998 oldbase = BUILT_IN_ATOMIC_FETCH_ADD_N;
8999 newbase = BUILT_IN_ATOMIC_ADD_FETCH_N;
9000 break;
9001 case MINUS_EXPR:
9002 oldbase = BUILT_IN_ATOMIC_FETCH_SUB_N;
9003 newbase = BUILT_IN_ATOMIC_SUB_FETCH_N;
9004 break;
9005 case BIT_AND_EXPR:
9006 oldbase = BUILT_IN_ATOMIC_FETCH_AND_N;
9007 newbase = BUILT_IN_ATOMIC_AND_FETCH_N;
9008 break;
9009 case BIT_IOR_EXPR:
9010 oldbase = BUILT_IN_ATOMIC_FETCH_OR_N;
9011 newbase = BUILT_IN_ATOMIC_OR_FETCH_N;
9012 break;
9013 case BIT_XOR_EXPR:
9014 oldbase = BUILT_IN_ATOMIC_FETCH_XOR_N;
9015 newbase = BUILT_IN_ATOMIC_XOR_FETCH_N;
9016 break;
9017 default:
9018 return false;
9019 }
9020
9021 /* Make sure the expression is of the proper form. */
9022 if (operand_equal_p (gimple_assign_rhs1 (stmt), loaded_val, 0))
9023 rhs = gimple_assign_rhs2 (stmt);
9024 else if (commutative_tree_code (gimple_assign_rhs_code (stmt))
9025 && operand_equal_p (gimple_assign_rhs2 (stmt), loaded_val, 0))
9026 rhs = gimple_assign_rhs1 (stmt);
9027 else
9028 return false;
9029
9030 tmpbase = ((enum built_in_function)
9031 ((need_new ? newbase : oldbase) + index + 1));
9032 decl = builtin_decl_explicit (tmpbase);
9033 if (decl == NULL_TREE)
9034 return false;
9035 itype = TREE_TYPE (TREE_TYPE (decl));
9036 imode = TYPE_MODE (itype);
9037
9038 /* We could test all of the various optabs involved, but the fact of the
9039 matter is that (with the exception of i486 vs i586 and xadd) all targets
9040 that support any atomic operaton optab also implements compare-and-swap.
9041 Let optabs.cc take care of expanding any compare-and-swap loop. */
9042 if (!can_compare_and_swap_p (imode, true) || !can_atomic_load_p (imode))
9043 return false;
9044
9045 gsi = gsi_last_nondebug_bb (load_bb);
9046 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_LOAD);
9047
9048 /* OpenMP does not imply any barrier-like semantics on its atomic ops.
9049 It only requires that the operation happen atomically. Thus we can
9050 use the RELAXED memory model. */
9051 call = build_call_expr_loc (loc, decl, 3, addr,
9052 fold_convert_loc (loc, itype, rhs),
9053 build_int_cst (NULL, mo));
9054
9055 if (need_old || need_new)
9056 {
9057 lhs = need_old ? loaded_val : stored_val;
9058 call = fold_convert_loc (loc, TREE_TYPE (lhs), call);
9059 call = build2_loc (loc, MODIFY_EXPR, void_type_node, lhs, call);
9060 }
9061 else
9062 call = fold_convert_loc (loc, void_type_node, call);
9063 force_gimple_operand_gsi (&gsi, call, true, NULL_TREE, true, GSI_SAME_STMT);
9064 gsi_remove (&gsi, true);
9065
9066 gsi = gsi_last_nondebug_bb (store_bb);
9067 gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
9068 gsi_remove (&gsi, true);
9069 gsi = gsi_last_nondebug_bb (store_bb);
9070 stmt = gsi_stmt (gsi);
9071 gsi_remove (&gsi, true);
9072
9073 if (gimple_in_ssa_p (cfun))
9074 release_defs (stmt);
9075
9076 return true;
9077 }
9078
9079 /* A subroutine of expand_omp_atomic. Attempt to implement the atomic
9080 compare and exchange as an ATOMIC_COMPARE_EXCHANGE internal function.
9081 Returns false if the expression is not of the proper form. */
9082
9083 static bool
9084 expand_omp_atomic_cas (basic_block load_bb, tree addr,
9085 tree loaded_val, tree stored_val, int index)
9086 {
9087 /* We expect to find the following sequences:
9088
9089 load_bb:
9090 GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
9091
9092 store_bb:
9093 val = tmp == e ? d : tmp;
9094 GIMPLE_OMP_ATOMIC_STORE (val)
9095
9096 or in store_bb instead:
9097 tmp2 = tmp == e;
9098 val = tmp2 ? d : tmp;
9099 GIMPLE_OMP_ATOMIC_STORE (val)
9100
9101 or:
9102 tmp3 = VIEW_CONVERT_EXPR<integral_type>(tmp);
9103 val = e == tmp3 ? d : tmp;
9104 GIMPLE_OMP_ATOMIC_STORE (val)
9105
9106 etc. */
9107
9108
9109 basic_block store_bb = single_succ (load_bb);
9110 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (store_bb);
9111 gimple *store_stmt = gsi_stmt (gsi);
9112 if (!store_stmt || gimple_code (store_stmt) != GIMPLE_OMP_ATOMIC_STORE)
9113 return false;
9114 gsi_prev_nondebug (&gsi);
9115 if (gsi_end_p (gsi))
9116 return false;
9117 gimple *condexpr_stmt = gsi_stmt (gsi);
9118 if (!is_gimple_assign (condexpr_stmt)
9119 || gimple_assign_rhs_code (condexpr_stmt) != COND_EXPR)
9120 return false;
9121 if (!operand_equal_p (gimple_assign_lhs (condexpr_stmt), stored_val, 0))
9122 return false;
9123 gimple *cond_stmt = NULL;
9124 gimple *vce_stmt = NULL;
9125 gsi_prev_nondebug (&gsi);
9126 if (!gsi_end_p (gsi))
9127 {
9128 cond_stmt = gsi_stmt (gsi);
9129 if (!is_gimple_assign (cond_stmt))
9130 return false;
9131 if (gimple_assign_rhs_code (cond_stmt) == EQ_EXPR)
9132 {
9133 gsi_prev_nondebug (&gsi);
9134 if (!gsi_end_p (gsi))
9135 {
9136 vce_stmt = gsi_stmt (gsi);
9137 if (!is_gimple_assign (vce_stmt)
9138 || gimple_assign_rhs_code (vce_stmt) != VIEW_CONVERT_EXPR)
9139 return false;
9140 }
9141 }
9142 else if (gimple_assign_rhs_code (cond_stmt) == VIEW_CONVERT_EXPR)
9143 std::swap (vce_stmt, cond_stmt);
9144 else
9145 return false;
9146 if (vce_stmt)
9147 {
9148 tree vce_rhs = gimple_assign_rhs1 (vce_stmt);
9149 if (TREE_CODE (vce_rhs) != VIEW_CONVERT_EXPR
9150 || !operand_equal_p (TREE_OPERAND (vce_rhs, 0), loaded_val))
9151 return false;
9152 if (!INTEGRAL_TYPE_P (TREE_TYPE (vce_rhs))
9153 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (loaded_val))
9154 || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (vce_rhs)),
9155 TYPE_SIZE (TREE_TYPE (loaded_val))))
9156 return false;
9157 gsi_prev_nondebug (&gsi);
9158 if (!gsi_end_p (gsi))
9159 return false;
9160 }
9161 }
9162 tree cond = gimple_assign_rhs1 (condexpr_stmt);
9163 tree cond_op1, cond_op2;
9164 if (cond_stmt)
9165 {
9166 /* We should now always get a separate cond_stmt. */
9167 if (!operand_equal_p (cond, gimple_assign_lhs (cond_stmt)))
9168 return false;
9169 cond_op1 = gimple_assign_rhs1 (cond_stmt);
9170 cond_op2 = gimple_assign_rhs2 (cond_stmt);
9171 }
9172 else if (TREE_CODE (cond) != EQ_EXPR && TREE_CODE (cond) != NE_EXPR)
9173 return false;
9174 else
9175 {
9176 cond_op1 = TREE_OPERAND (cond, 0);
9177 cond_op2 = TREE_OPERAND (cond, 1);
9178 }
9179 tree d;
9180 if (TREE_CODE (cond) == NE_EXPR)
9181 {
9182 if (!operand_equal_p (gimple_assign_rhs2 (condexpr_stmt), loaded_val))
9183 return false;
9184 d = gimple_assign_rhs3 (condexpr_stmt);
9185 }
9186 else if (!operand_equal_p (gimple_assign_rhs3 (condexpr_stmt), loaded_val))
9187 return false;
9188 else
9189 d = gimple_assign_rhs2 (condexpr_stmt);
9190 tree e = vce_stmt ? gimple_assign_lhs (vce_stmt) : loaded_val;
9191 if (operand_equal_p (e, cond_op1))
9192 e = cond_op2;
9193 else if (operand_equal_p (e, cond_op2))
9194 e = cond_op1;
9195 else
9196 return false;
9197
9198 location_t loc = gimple_location (store_stmt);
9199 gimple *load_stmt = last_nondebug_stmt (load_bb);
9200 bool need_new = gimple_omp_atomic_need_value_p (store_stmt);
9201 bool need_old = gimple_omp_atomic_need_value_p (load_stmt);
9202 bool weak = gimple_omp_atomic_weak_p (load_stmt);
9203 enum omp_memory_order omo = gimple_omp_atomic_memory_order (load_stmt);
9204 tree mo = build_int_cst (NULL, omp_memory_order_to_memmodel (omo));
9205 tree fmo = build_int_cst (NULL, omp_memory_order_to_fail_memmodel (omo));
9206 gcc_checking_assert (!need_old || !need_new);
9207
9208 enum built_in_function fncode
9209 = (enum built_in_function) ((int) BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
9210 + index + 1);
9211 tree cmpxchg = builtin_decl_explicit (fncode);
9212 if (cmpxchg == NULL_TREE)
9213 return false;
9214 tree itype = TREE_TYPE (TREE_TYPE (cmpxchg));
9215
9216 if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
9217 || !can_atomic_load_p (TYPE_MODE (itype)))
9218 return false;
9219
9220 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9221 if (SCALAR_FLOAT_TYPE_P (type) && !vce_stmt)
9222 return false;
9223
9224 gsi = gsi_for_stmt (store_stmt);
9225 if (!useless_type_conversion_p (itype, TREE_TYPE (e)))
9226 {
9227 tree ne = create_tmp_reg (itype);
9228 gimple *g = gimple_build_assign (ne, NOP_EXPR, e);
9229 gimple_set_location (g, loc);
9230 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9231 e = ne;
9232 }
9233 if (!useless_type_conversion_p (itype, TREE_TYPE (d)))
9234 {
9235 tree nd = create_tmp_reg (itype);
9236 enum tree_code code;
9237 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (d)))
9238 {
9239 code = VIEW_CONVERT_EXPR;
9240 d = build1 (VIEW_CONVERT_EXPR, itype, d);
9241 }
9242 else
9243 code = NOP_EXPR;
9244 gimple *g = gimple_build_assign (nd, code, d);
9245 gimple_set_location (g, loc);
9246 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9247 d = nd;
9248 }
9249
9250 tree ctype = build_complex_type (itype);
9251 int flag = int_size_in_bytes (itype) + (weak ? 256 : 0);
9252 gimple *g
9253 = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6, addr, e, d,
9254 build_int_cst (integer_type_node, flag),
9255 mo, fmo);
9256 tree cres = create_tmp_reg (ctype);
9257 gimple_call_set_lhs (g, cres);
9258 gimple_set_location (g, loc);
9259 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9260
9261 if (cond_stmt || need_old || need_new)
9262 {
9263 tree im = create_tmp_reg (itype);
9264 g = gimple_build_assign (im, IMAGPART_EXPR,
9265 build1 (IMAGPART_EXPR, itype, cres));
9266 gimple_set_location (g, loc);
9267 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9268
9269 tree re = NULL_TREE;
9270 if (need_old || need_new)
9271 {
9272 re = create_tmp_reg (itype);
9273 g = gimple_build_assign (re, REALPART_EXPR,
9274 build1 (REALPART_EXPR, itype, cres));
9275 gimple_set_location (g, loc);
9276 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9277 }
9278
9279 if (cond_stmt)
9280 {
9281 g = gimple_build_assign (cond, NOP_EXPR, im);
9282 gimple_set_location (g, loc);
9283 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9284 }
9285
9286 if (need_new)
9287 {
9288 g = gimple_build_assign (create_tmp_reg (itype), COND_EXPR,
9289 cond_stmt
9290 ? cond : build2 (NE_EXPR, boolean_type_node,
9291 im, build_zero_cst (itype)),
9292 d, re);
9293 gimple_set_location (g, loc);
9294 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9295 re = gimple_assign_lhs (g);
9296 }
9297
9298 if (need_old || need_new)
9299 {
9300 tree v = need_old ? loaded_val : stored_val;
9301 enum tree_code code;
9302 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (v)))
9303 {
9304 code = VIEW_CONVERT_EXPR;
9305 re = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (v), re);
9306 }
9307 else if (!useless_type_conversion_p (TREE_TYPE (v), itype))
9308 code = NOP_EXPR;
9309 else
9310 code = TREE_CODE (re);
9311 g = gimple_build_assign (v, code, re);
9312 gimple_set_location (g, loc);
9313 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
9314 }
9315 }
9316
9317 gsi_remove (&gsi, true);
9318 gsi = gsi_for_stmt (load_stmt);
9319 gsi_remove (&gsi, true);
9320 gsi = gsi_for_stmt (condexpr_stmt);
9321 gsi_remove (&gsi, true);
9322 if (cond_stmt)
9323 {
9324 gsi = gsi_for_stmt (cond_stmt);
9325 gsi_remove (&gsi, true);
9326 }
9327 if (vce_stmt)
9328 {
9329 gsi = gsi_for_stmt (vce_stmt);
9330 gsi_remove (&gsi, true);
9331 }
9332
9333 return true;
9334 }
9335
9336 /* A subroutine of expand_omp_atomic. Implement the atomic operation as:
9337
9338 oldval = *addr;
9339 repeat:
9340 newval = rhs; // with oldval replacing *addr in rhs
9341 oldval = __sync_val_compare_and_swap (addr, oldval, newval);
9342 if (oldval != newval)
9343 goto repeat;
9344
9345 INDEX is log2 of the size of the data type, and thus usable to find the
9346 index of the builtin decl. */
9347
9348 static bool
9349 expand_omp_atomic_pipeline (basic_block load_bb, basic_block store_bb,
9350 tree addr, tree loaded_val, tree stored_val,
9351 int index)
9352 {
9353 tree loadedi, storedi, initial, new_storedi, old_vali;
9354 tree type, itype, cmpxchg, iaddr, atype;
9355 gimple_stmt_iterator si;
9356 basic_block loop_header = single_succ (load_bb);
9357 gimple *phi, *stmt;
9358 edge e;
9359 enum built_in_function fncode;
9360
9361 fncode = (enum built_in_function)((int)BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
9362 + index + 1);
9363 cmpxchg = builtin_decl_explicit (fncode);
9364 if (cmpxchg == NULL_TREE)
9365 return false;
9366 type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9367 atype = type;
9368 itype = TREE_TYPE (TREE_TYPE (cmpxchg));
9369
9370 if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
9371 || !can_atomic_load_p (TYPE_MODE (itype)))
9372 return false;
9373
9374 /* Load the initial value, replacing the GIMPLE_OMP_ATOMIC_LOAD. */
9375 si = gsi_last_nondebug_bb (load_bb);
9376 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
9377 location_t loc = gimple_location (gsi_stmt (si));
9378 enum omp_memory_order omo = gimple_omp_atomic_memory_order (gsi_stmt (si));
9379 tree mo = build_int_cst (NULL, omp_memory_order_to_memmodel (omo));
9380 tree fmo = build_int_cst (NULL, omp_memory_order_to_fail_memmodel (omo));
9381
9382 /* For floating-point values, we'll need to view-convert them to integers
9383 so that we can perform the atomic compare and swap. Simplify the
9384 following code by always setting up the "i"ntegral variables. */
9385 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9386 {
9387 tree iaddr_val;
9388
9389 iaddr = create_tmp_reg (build_pointer_type_for_mode (itype, ptr_mode,
9390 true));
9391 atype = itype;
9392 iaddr_val
9393 = force_gimple_operand_gsi (&si,
9394 fold_convert (TREE_TYPE (iaddr), addr),
9395 false, NULL_TREE, true, GSI_SAME_STMT);
9396 stmt = gimple_build_assign (iaddr, iaddr_val);
9397 gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9398 loadedi = create_tmp_var (itype);
9399 if (gimple_in_ssa_p (cfun))
9400 loadedi = make_ssa_name (loadedi);
9401 }
9402 else
9403 {
9404 iaddr = addr;
9405 loadedi = loaded_val;
9406 }
9407
9408 fncode = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
9409 tree loaddecl = builtin_decl_explicit (fncode);
9410 if (loaddecl)
9411 initial
9412 = fold_convert (atype,
9413 build_call_expr (loaddecl, 2, iaddr,
9414 build_int_cst (NULL_TREE,
9415 MEMMODEL_RELAXED)));
9416 else
9417 {
9418 tree off
9419 = build_int_cst (build_pointer_type_for_mode (atype, ptr_mode,
9420 true), 0);
9421 initial = build2 (MEM_REF, atype, iaddr, off);
9422 }
9423
9424 initial
9425 = force_gimple_operand_gsi (&si, initial, true, NULL_TREE, true,
9426 GSI_SAME_STMT);
9427
9428 /* Move the value to the LOADEDI temporary. */
9429 if (gimple_in_ssa_p (cfun))
9430 {
9431 gcc_assert (gimple_seq_empty_p (phi_nodes (loop_header)));
9432 phi = create_phi_node (loadedi, loop_header);
9433 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (load_bb)),
9434 initial);
9435 }
9436 else
9437 gsi_insert_before (&si,
9438 gimple_build_assign (loadedi, initial),
9439 GSI_SAME_STMT);
9440 if (loadedi != loaded_val)
9441 {
9442 gimple_stmt_iterator gsi2;
9443 tree x;
9444
9445 x = build1 (VIEW_CONVERT_EXPR, type, loadedi);
9446 gsi2 = gsi_start_bb (loop_header);
9447 if (gimple_in_ssa_p (cfun))
9448 {
9449 gassign *stmt;
9450 x = force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
9451 true, GSI_SAME_STMT);
9452 stmt = gimple_build_assign (loaded_val, x);
9453 gsi_insert_before (&gsi2, stmt, GSI_SAME_STMT);
9454 }
9455 else
9456 {
9457 x = build2 (MODIFY_EXPR, TREE_TYPE (loaded_val), loaded_val, x);
9458 force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
9459 true, GSI_SAME_STMT);
9460 }
9461 }
9462 gsi_remove (&si, true);
9463
9464 si = gsi_last_nondebug_bb (store_bb);
9465 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
9466
9467 if (iaddr == addr)
9468 storedi = stored_val;
9469 else
9470 storedi
9471 = force_gimple_operand_gsi (&si,
9472 build1 (VIEW_CONVERT_EXPR, itype,
9473 stored_val), true, NULL_TREE, true,
9474 GSI_SAME_STMT);
9475
9476 /* Build the compare&swap statement. */
9477 tree ctype = build_complex_type (itype);
9478 int flag = int_size_in_bytes (itype);
9479 new_storedi = build_call_expr_internal_loc (loc, IFN_ATOMIC_COMPARE_EXCHANGE,
9480 ctype, 6, iaddr, loadedi,
9481 storedi,
9482 build_int_cst (integer_type_node,
9483 flag),
9484 mo, fmo);
9485 new_storedi = build1 (REALPART_EXPR, itype, new_storedi);
9486 new_storedi = force_gimple_operand_gsi (&si,
9487 fold_convert (TREE_TYPE (loadedi),
9488 new_storedi),
9489 true, NULL_TREE,
9490 true, GSI_SAME_STMT);
9491
9492 if (gimple_in_ssa_p (cfun))
9493 old_vali = loadedi;
9494 else
9495 {
9496 old_vali = create_tmp_var (TREE_TYPE (loadedi));
9497 stmt = gimple_build_assign (old_vali, loadedi);
9498 gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9499
9500 stmt = gimple_build_assign (loadedi, new_storedi);
9501 gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9502 }
9503
9504 /* Note that we always perform the comparison as an integer, even for
9505 floating point. This allows the atomic operation to properly
9506 succeed even with NaNs and -0.0. */
9507 tree ne = build2 (NE_EXPR, boolean_type_node, new_storedi, old_vali);
9508 stmt = gimple_build_cond_empty (ne);
9509 gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9510
9511 /* Update cfg. */
9512 e = single_succ_edge (store_bb);
9513 e->flags &= ~EDGE_FALLTHRU;
9514 e->flags |= EDGE_FALSE_VALUE;
9515 /* Expect no looping. */
9516 e->probability = profile_probability::guessed_always ();
9517
9518 e = make_edge (store_bb, loop_header, EDGE_TRUE_VALUE);
9519 e->probability = profile_probability::guessed_never ();
9520
9521 /* Copy the new value to loadedi (we already did that before the condition
9522 if we are not in SSA). */
9523 if (gimple_in_ssa_p (cfun))
9524 {
9525 phi = gimple_seq_first_stmt (phi_nodes (loop_header));
9526 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), new_storedi);
9527 }
9528
9529 /* Remove GIMPLE_OMP_ATOMIC_STORE. */
9530 stmt = gsi_stmt (si);
9531 gsi_remove (&si, true);
9532 if (gimple_in_ssa_p (cfun))
9533 release_defs (stmt);
9534
9535 class loop *loop = alloc_loop ();
9536 loop->header = loop_header;
9537 loop->latch = store_bb;
9538 add_loop (loop, loop_header->loop_father);
9539
9540 return true;
9541 }
9542
9543 /* A subroutine of expand_omp_atomic. Implement the atomic operation as:
9544
9545 GOMP_atomic_start ();
9546 *addr = rhs;
9547 GOMP_atomic_end ();
9548
9549 The result is not globally atomic, but works so long as all parallel
9550 references are within #pragma omp atomic directives. According to
9551 responses received from [email protected], appears to be within spec.
9552 Which makes sense, since that's how several other compilers handle
9553 this situation as well.
9554 LOADED_VAL and ADDR are the operands of GIMPLE_OMP_ATOMIC_LOAD we're
9555 expanding. STORED_VAL is the operand of the matching
9556 GIMPLE_OMP_ATOMIC_STORE.
9557
9558 We replace
9559 GIMPLE_OMP_ATOMIC_LOAD (loaded_val, addr) with
9560 loaded_val = *addr;
9561
9562 and replace
9563 GIMPLE_OMP_ATOMIC_STORE (stored_val) with
9564 *addr = stored_val;
9565 */
9566
9567 static bool
9568 expand_omp_atomic_mutex (basic_block load_bb, basic_block store_bb,
9569 tree addr, tree loaded_val, tree stored_val)
9570 {
9571 gimple_stmt_iterator si;
9572 gassign *stmt;
9573 tree t;
9574
9575 si = gsi_last_nondebug_bb (load_bb);
9576 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
9577
9578 t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
9579 t = build_call_expr (t, 0);
9580 force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
9581
9582 tree mem = build_simple_mem_ref (addr);
9583 TREE_TYPE (mem) = TREE_TYPE (loaded_val);
9584 TREE_OPERAND (mem, 1)
9585 = fold_convert (build_pointer_type_for_mode (TREE_TYPE (mem), ptr_mode,
9586 true),
9587 TREE_OPERAND (mem, 1));
9588 stmt = gimple_build_assign (loaded_val, mem);
9589 gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9590 gsi_remove (&si, true);
9591
9592 si = gsi_last_nondebug_bb (store_bb);
9593 gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
9594
9595 stmt = gimple_build_assign (unshare_expr (mem), stored_val);
9596 gimple_set_vuse (stmt, gimple_vuse (gsi_stmt (si)));
9597 gimple_set_vdef (stmt, gimple_vdef (gsi_stmt (si)));
9598 gsi_insert_before (&si, stmt, GSI_SAME_STMT);
9599
9600 t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
9601 t = build_call_expr (t, 0);
9602 force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
9603 gsi_remove (&si, true);
9604 return true;
9605 }
9606
9607 /* Expand an GIMPLE_OMP_ATOMIC statement. We try to expand
9608 using expand_omp_atomic_fetch_op. If it failed, we try to
9609 call expand_omp_atomic_pipeline, and if it fails too, the
9610 ultimate fallback is wrapping the operation in a mutex
9611 (expand_omp_atomic_mutex). REGION is the atomic region built
9612 by build_omp_regions_1(). */
9613
9614 static void
9615 expand_omp_atomic (struct omp_region *region)
9616 {
9617 basic_block load_bb = region->entry, store_bb = region->exit;
9618 gomp_atomic_load *load
9619 = as_a <gomp_atomic_load *> (last_nondebug_stmt (load_bb));
9620 gomp_atomic_store *store
9621 = as_a <gomp_atomic_store *> (last_nondebug_stmt (store_bb));
9622 tree loaded_val = gimple_omp_atomic_load_lhs (load);
9623 tree addr = gimple_omp_atomic_load_rhs (load);
9624 tree stored_val = gimple_omp_atomic_store_val (store);
9625 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
9626 HOST_WIDE_INT index;
9627
9628 /* Make sure the type is one of the supported sizes. */
9629 index = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9630 index = exact_log2 (index);
9631 if (index >= 0 && index <= 4)
9632 {
9633 unsigned int align = TYPE_ALIGN_UNIT (type);
9634
9635 /* __sync builtins require strict data alignment. */
9636 if (exact_log2 (align) >= index)
9637 {
9638 /* Atomic load. */
9639 scalar_mode smode;
9640 if (loaded_val == stored_val
9641 && (is_int_mode (TYPE_MODE (type), &smode)
9642 || is_float_mode (TYPE_MODE (type), &smode))
9643 && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
9644 && expand_omp_atomic_load (load_bb, addr, loaded_val, index))
9645 return;
9646
9647 /* Atomic store. */
9648 if ((is_int_mode (TYPE_MODE (type), &smode)
9649 || is_float_mode (TYPE_MODE (type), &smode))
9650 && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
9651 && store_bb == single_succ (load_bb)
9652 && first_stmt (store_bb) == store
9653 && expand_omp_atomic_store (load_bb, addr, loaded_val,
9654 stored_val, index))
9655 return;
9656
9657 /* When possible, use specialized atomic update functions. */
9658 if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
9659 && store_bb == single_succ (load_bb)
9660 && expand_omp_atomic_fetch_op (load_bb, addr,
9661 loaded_val, stored_val, index))
9662 return;
9663
9664 /* When possible, use ATOMIC_COMPARE_EXCHANGE ifn without a loop. */
9665 if (store_bb == single_succ (load_bb)
9666 && !gimple_in_ssa_p (cfun)
9667 && expand_omp_atomic_cas (load_bb, addr, loaded_val, stored_val,
9668 index))
9669 return;
9670
9671 /* If we don't have specialized __sync builtins, try and implement
9672 as a compare and swap loop. */
9673 if (expand_omp_atomic_pipeline (load_bb, store_bb, addr,
9674 loaded_val, stored_val, index))
9675 return;
9676 }
9677 }
9678
9679 /* The ultimate fallback is wrapping the operation in a mutex. */
9680 expand_omp_atomic_mutex (load_bb, store_bb, addr, loaded_val, stored_val);
9681 }
9682
9683 /* Mark the loops inside the kernels region starting at REGION_ENTRY and ending
9684 at REGION_EXIT. */
9685
9686 static void
9687 mark_loops_in_oacc_kernels_region (basic_block region_entry,
9688 basic_block region_exit)
9689 {
9690 class loop *outer = region_entry->loop_father;
9691 gcc_assert (region_exit == NULL || outer == region_exit->loop_father);
9692
9693 /* Don't parallelize the kernels region if it contains more than one outer
9694 loop. */
9695 unsigned int nr_outer_loops = 0;
9696 class loop *single_outer = NULL;
9697 for (class loop *loop = outer->inner; loop != NULL; loop = loop->next)
9698 {
9699 gcc_assert (loop_outer (loop) == outer);
9700
9701 if (!dominated_by_p (CDI_DOMINATORS, loop->header, region_entry))
9702 continue;
9703
9704 if (region_exit != NULL
9705 && dominated_by_p (CDI_DOMINATORS, loop->header, region_exit))
9706 continue;
9707
9708 nr_outer_loops++;
9709 single_outer = loop;
9710 }
9711 if (nr_outer_loops != 1)
9712 return;
9713
9714 for (class loop *loop = single_outer->inner;
9715 loop != NULL;
9716 loop = loop->inner)
9717 if (loop->next)
9718 return;
9719
9720 /* Mark the loops in the region. */
9721 for (class loop *loop = single_outer; loop != NULL; loop = loop->inner)
9722 loop->in_oacc_kernels_region = true;
9723 }
9724
9725 /* Build target argument identifier from the DEVICE identifier, value
9726 identifier ID and whether the element also has a SUBSEQUENT_PARAM. */
9727
9728 static tree
9729 get_target_argument_identifier_1 (int device, bool subseqent_param, int id)
9730 {
9731 tree t = build_int_cst (integer_type_node, device);
9732 if (subseqent_param)
9733 t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9734 build_int_cst (integer_type_node,
9735 GOMP_TARGET_ARG_SUBSEQUENT_PARAM));
9736 t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9737 build_int_cst (integer_type_node, id));
9738 return t;
9739 }
9740
9741 /* Like above but return it in type that can be directly stored as an element
9742 of the argument array. */
9743
9744 static tree
9745 get_target_argument_identifier (int device, bool subseqent_param, int id)
9746 {
9747 tree t = get_target_argument_identifier_1 (device, subseqent_param, id);
9748 return fold_convert (ptr_type_node, t);
9749 }
9750
9751 /* Return a target argument consisting of DEVICE identifier, value identifier
9752 ID, and the actual VALUE. */
9753
9754 static tree
9755 get_target_argument_value (gimple_stmt_iterator *gsi, int device, int id,
9756 tree value)
9757 {
9758 tree t = fold_build2 (LSHIFT_EXPR, integer_type_node,
9759 fold_convert (integer_type_node, value),
9760 build_int_cst (unsigned_type_node,
9761 GOMP_TARGET_ARG_VALUE_SHIFT));
9762 t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
9763 get_target_argument_identifier_1 (device, false, id));
9764 t = fold_convert (ptr_type_node, t);
9765 return force_gimple_operand_gsi (gsi, t, true, NULL, true, GSI_SAME_STMT);
9766 }
9767
9768 /* If VALUE is an integer constant greater than -2^15 and smaller than 2^15,
9769 push one argument to ARGS with both the DEVICE, ID and VALUE embedded in it,
9770 otherwise push an identifier (with DEVICE and ID) and the VALUE in two
9771 arguments. */
9772
9773 static void
9774 push_target_argument_according_to_value (gimple_stmt_iterator *gsi, int device,
9775 int id, tree value, vec <tree> *args)
9776 {
9777 if (tree_fits_shwi_p (value)
9778 && tree_to_shwi (value) > -(1 << 15)
9779 && tree_to_shwi (value) < (1 << 15))
9780 args->quick_push (get_target_argument_value (gsi, device, id, value));
9781 else
9782 {
9783 args->quick_push (get_target_argument_identifier (device, true, id));
9784 value = fold_convert (ptr_type_node, value);
9785 value = force_gimple_operand_gsi (gsi, value, true, NULL, true,
9786 GSI_SAME_STMT);
9787 args->quick_push (value);
9788 }
9789 }
9790
9791 /* Create an array of arguments that is then passed to GOMP_target. */
9792
9793 static tree
9794 get_target_arguments (gimple_stmt_iterator *gsi, gomp_target *tgt_stmt)
9795 {
9796 auto_vec <tree, 6> args;
9797 tree clauses = gimple_omp_target_clauses (tgt_stmt);
9798 tree t, c = omp_find_clause (clauses, OMP_CLAUSE_NUM_TEAMS);
9799 if (c)
9800 t = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
9801 else
9802 t = integer_minus_one_node;
9803 push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
9804 GOMP_TARGET_ARG_NUM_TEAMS, t, &args);
9805
9806 c = omp_find_clause (clauses, OMP_CLAUSE_THREAD_LIMIT);
9807 if (c)
9808 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
9809 else
9810 t = integer_minus_one_node;
9811 push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
9812 GOMP_TARGET_ARG_THREAD_LIMIT, t,
9813 &args);
9814
9815 /* Produce more, perhaps device specific, arguments here. */
9816
9817 tree argarray = create_tmp_var (build_array_type_nelts (ptr_type_node,
9818 args.length () + 1),
9819 ".omp_target_args");
9820 for (unsigned i = 0; i < args.length (); i++)
9821 {
9822 tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
9823 build_int_cst (integer_type_node, i),
9824 NULL_TREE, NULL_TREE);
9825 gsi_insert_before (gsi, gimple_build_assign (ref, args[i]),
9826 GSI_SAME_STMT);
9827 }
9828 tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
9829 build_int_cst (integer_type_node, args.length ()),
9830 NULL_TREE, NULL_TREE);
9831 gsi_insert_before (gsi, gimple_build_assign (ref, null_pointer_node),
9832 GSI_SAME_STMT);
9833 TREE_ADDRESSABLE (argarray) = 1;
9834 return build_fold_addr_expr (argarray);
9835 }
9836
9837 /* Expand the GIMPLE_OMP_TARGET starting at REGION. */
9838
9839 static void
9840 expand_omp_target (struct omp_region *region)
9841 {
9842 basic_block entry_bb, exit_bb, new_bb;
9843 struct function *child_cfun;
9844 tree child_fn, child_fn2, block, t, c;
9845 gimple_stmt_iterator gsi;
9846 gomp_target *entry_stmt;
9847 gimple *stmt;
9848 edge e;
9849 bool offloaded;
9850 int target_kind;
9851
9852 entry_stmt = as_a <gomp_target *> (last_nondebug_stmt (region->entry));
9853 target_kind = gimple_omp_target_kind (entry_stmt);
9854 new_bb = region->entry;
9855
9856 offloaded = is_gimple_omp_offloaded (entry_stmt);
9857 switch (target_kind)
9858 {
9859 case GF_OMP_TARGET_KIND_REGION:
9860 case GF_OMP_TARGET_KIND_UPDATE:
9861 case GF_OMP_TARGET_KIND_ENTER_DATA:
9862 case GF_OMP_TARGET_KIND_EXIT_DATA:
9863 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
9864 case GF_OMP_TARGET_KIND_OACC_KERNELS:
9865 case GF_OMP_TARGET_KIND_OACC_SERIAL:
9866 case GF_OMP_TARGET_KIND_OACC_UPDATE:
9867 case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
9868 case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
9869 case GF_OMP_TARGET_KIND_OACC_DECLARE:
9870 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
9871 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
9872 case GF_OMP_TARGET_KIND_DATA:
9873 case GF_OMP_TARGET_KIND_OACC_DATA:
9874 case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
9875 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
9876 break;
9877 default:
9878 gcc_unreachable ();
9879 }
9880
9881 tree clauses = gimple_omp_target_clauses (entry_stmt);
9882
9883 bool is_ancestor = false;
9884 child_fn = child_fn2 = NULL_TREE;
9885 child_cfun = NULL;
9886 if (offloaded)
9887 {
9888 c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE);
9889 if (ENABLE_OFFLOADING && c)
9890 is_ancestor = OMP_CLAUSE_DEVICE_ANCESTOR (c);
9891 child_fn = gimple_omp_target_child_fn (entry_stmt);
9892 child_cfun = DECL_STRUCT_FUNCTION (child_fn);
9893 }
9894
9895 /* Supported by expand_omp_taskreg, but not here. */
9896 if (child_cfun != NULL)
9897 gcc_checking_assert (!child_cfun->cfg);
9898 gcc_checking_assert (!gimple_in_ssa_p (cfun));
9899
9900 entry_bb = region->entry;
9901 exit_bb = region->exit;
9902
9903 if (target_kind == GF_OMP_TARGET_KIND_OACC_KERNELS)
9904 mark_loops_in_oacc_kernels_region (region->entry, region->exit);
9905
9906 /* Going on, all OpenACC compute constructs are mapped to
9907 'BUILT_IN_GOACC_PARALLEL', and get their compute regions outlined.
9908 To distinguish between them, we attach attributes. */
9909 switch (target_kind)
9910 {
9911 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
9912 DECL_ATTRIBUTES (child_fn)
9913 = tree_cons (get_identifier ("oacc parallel"),
9914 NULL_TREE, DECL_ATTRIBUTES (child_fn));
9915 break;
9916 case GF_OMP_TARGET_KIND_OACC_KERNELS:
9917 DECL_ATTRIBUTES (child_fn)
9918 = tree_cons (get_identifier ("oacc kernels"),
9919 NULL_TREE, DECL_ATTRIBUTES (child_fn));
9920 break;
9921 case GF_OMP_TARGET_KIND_OACC_SERIAL:
9922 DECL_ATTRIBUTES (child_fn)
9923 = tree_cons (get_identifier ("oacc serial"),
9924 NULL_TREE, DECL_ATTRIBUTES (child_fn));
9925 break;
9926 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
9927 DECL_ATTRIBUTES (child_fn)
9928 = tree_cons (get_identifier ("oacc parallel_kernels_parallelized"),
9929 NULL_TREE, DECL_ATTRIBUTES (child_fn));
9930 break;
9931 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
9932 DECL_ATTRIBUTES (child_fn)
9933 = tree_cons (get_identifier ("oacc parallel_kernels_gang_single"),
9934 NULL_TREE, DECL_ATTRIBUTES (child_fn));
9935 break;
9936 default:
9937 /* Make sure we don't miss any. */
9938 gcc_checking_assert (!(is_gimple_omp_oacc (entry_stmt)
9939 && is_gimple_omp_offloaded (entry_stmt)));
9940 break;
9941 }
9942
9943 if (offloaded)
9944 {
9945 unsigned srcidx, dstidx, num;
9946
9947 /* If the offloading region needs data sent from the parent
9948 function, then the very first statement (except possible
9949 tree profile counter updates) of the offloading body
9950 is a copy assignment .OMP_DATA_I = &.OMP_DATA_O. Since
9951 &.OMP_DATA_O is passed as an argument to the child function,
9952 we need to replace it with the argument as seen by the child
9953 function.
9954
9955 In most cases, this will end up being the identity assignment
9956 .OMP_DATA_I = .OMP_DATA_I. However, if the offloading body had
9957 a function call that has been inlined, the original PARM_DECL
9958 .OMP_DATA_I may have been converted into a different local
9959 variable. In which case, we need to keep the assignment. */
9960 tree data_arg = gimple_omp_target_data_arg (entry_stmt);
9961 if (data_arg)
9962 {
9963 basic_block entry_succ_bb = single_succ (entry_bb);
9964 gimple_stmt_iterator gsi;
9965 tree arg;
9966 gimple *tgtcopy_stmt = NULL;
9967 tree sender = TREE_VEC_ELT (data_arg, 0);
9968
9969 for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
9970 {
9971 gcc_assert (!gsi_end_p (gsi));
9972 stmt = gsi_stmt (gsi);
9973 if (gimple_code (stmt) != GIMPLE_ASSIGN)
9974 continue;
9975
9976 if (gimple_num_ops (stmt) == 2)
9977 {
9978 tree arg = gimple_assign_rhs1 (stmt);
9979
9980 /* We're ignoring the subcode because we're
9981 effectively doing a STRIP_NOPS. */
9982
9983 if ((TREE_CODE (arg) == ADDR_EXPR
9984 && TREE_OPERAND (arg, 0) == sender)
9985 || arg == sender)
9986 {
9987 tgtcopy_stmt = stmt;
9988 break;
9989 }
9990 }
9991 }
9992
9993 gcc_assert (tgtcopy_stmt != NULL);
9994 arg = DECL_ARGUMENTS (child_fn);
9995
9996 gcc_assert (gimple_assign_lhs (tgtcopy_stmt) == arg);
9997 gsi_remove (&gsi, true);
9998 }
9999
10000 /* Declare local variables needed in CHILD_CFUN. */
10001 block = DECL_INITIAL (child_fn);
10002 BLOCK_VARS (block) = vec2chain (child_cfun->local_decls);
10003 /* The gimplifier could record temporaries in the offloading block
10004 rather than in containing function's local_decls chain,
10005 which would mean cgraph missed finalizing them. Do it now. */
10006 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
10007 if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
10008 varpool_node::finalize_decl (t);
10009 DECL_SAVED_TREE (child_fn) = NULL;
10010 /* We'll create a CFG for child_fn, so no gimple body is needed. */
10011 gimple_set_body (child_fn, NULL);
10012 TREE_USED (block) = 1;
10013
10014 /* Reset DECL_CONTEXT on function arguments. */
10015 for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
10016 DECL_CONTEXT (t) = child_fn;
10017
10018 /* Split ENTRY_BB at GIMPLE_*,
10019 so that it can be moved to the child function. */
10020 gsi = gsi_last_nondebug_bb (entry_bb);
10021 stmt = gsi_stmt (gsi);
10022 gcc_assert (stmt
10023 && gimple_code (stmt) == gimple_code (entry_stmt));
10024 e = split_block (entry_bb, stmt);
10025 gsi_remove (&gsi, true);
10026 entry_bb = e->dest;
10027 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
10028
10029 /* Convert GIMPLE_OMP_RETURN into a RETURN_EXPR. */
10030 if (exit_bb)
10031 {
10032 gsi = gsi_last_nondebug_bb (exit_bb);
10033 gcc_assert (!gsi_end_p (gsi)
10034 && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
10035 stmt = gimple_build_return (NULL);
10036 gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
10037 gsi_remove (&gsi, true);
10038 }
10039
10040 /* Move the offloading region into CHILD_CFUN. */
10041
10042 block = gimple_block (entry_stmt);
10043
10044 new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
10045 if (exit_bb)
10046 single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
10047 /* When the OMP expansion process cannot guarantee an up-to-date
10048 loop tree arrange for the child function to fixup loops. */
10049 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
10050 child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
10051
10052 /* Remove non-local VAR_DECLs from child_cfun->local_decls list. */
10053 num = vec_safe_length (child_cfun->local_decls);
10054 for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
10055 {
10056 t = (*child_cfun->local_decls)[srcidx];
10057 if (DECL_CONTEXT (t) == cfun->decl)
10058 continue;
10059 if (srcidx != dstidx)
10060 (*child_cfun->local_decls)[dstidx] = t;
10061 dstidx++;
10062 }
10063 if (dstidx != num)
10064 vec_safe_truncate (child_cfun->local_decls, dstidx);
10065
10066 /* Inform the callgraph about the new function. */
10067 child_cfun->curr_properties = cfun->curr_properties;
10068 child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
10069 child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
10070 cgraph_node *node = cgraph_node::get_create (child_fn);
10071 node->parallelized_function = 1;
10072 node->has_omp_variant_constructs
10073 |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
10074 cgraph_node::add_new_function (child_fn, true);
10075
10076 /* Add the new function to the offload table. */
10077 if (ENABLE_OFFLOADING)
10078 {
10079 if (in_lto_p)
10080 DECL_PRESERVE_P (child_fn) = 1;
10081 if (!is_ancestor)
10082 vec_safe_push (offload_funcs, child_fn);
10083 }
10084
10085 bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
10086 && !DECL_ASSEMBLER_NAME_SET_P (child_fn);
10087
10088 /* Fix the callgraph edges for child_cfun. Those for cfun will be
10089 fixed in a following pass. */
10090 push_cfun (child_cfun);
10091 if (need_asm)
10092 assign_assembler_name_if_needed (child_fn);
10093 cgraph_edge::rebuild_edges ();
10094
10095 /* Some EH regions might become dead, see PR34608. If
10096 pass_cleanup_cfg isn't the first pass to happen with the
10097 new child, these dead EH edges might cause problems.
10098 Clean them up now. */
10099 if (flag_exceptions)
10100 {
10101 basic_block bb;
10102 bool changed = false;
10103
10104 FOR_EACH_BB_FN (bb, cfun)
10105 changed |= gimple_purge_dead_eh_edges (bb);
10106 if (changed)
10107 cleanup_tree_cfg ();
10108 }
10109 if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
10110 verify_loop_structure ();
10111 pop_cfun ();
10112
10113 if (dump_file && !gimple_in_ssa_p (cfun))
10114 {
10115 omp_any_child_fn_dumped = true;
10116 dump_function_header (dump_file, child_fn, dump_flags);
10117 dump_function_to_file (child_fn, dump_file, dump_flags);
10118 }
10119
10120 adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
10121
10122 /* Handle the case that an inner ancestor:1 target is called by an outer
10123 target region. */
10124 if (is_ancestor)
10125 {
10126 cgraph_node *fn2_node;
10127 child_fn2 = build_decl (DECL_SOURCE_LOCATION (child_fn),
10128 FUNCTION_DECL,
10129 clone_function_name (child_fn, "nohost"),
10130 TREE_TYPE (child_fn));
10131 if (in_lto_p)
10132 DECL_PRESERVE_P (child_fn2) = 1;
10133 TREE_STATIC (child_fn2) = 1;
10134 DECL_ARTIFICIAL (child_fn2) = 1;
10135 DECL_IGNORED_P (child_fn2) = 0;
10136 TREE_PUBLIC (child_fn2) = 0;
10137 DECL_UNINLINABLE (child_fn2) = 1;
10138 DECL_EXTERNAL (child_fn2) = 0;
10139 DECL_CONTEXT (child_fn2) = DECL_CONTEXT (child_fn);
10140 DECL_INITIAL (child_fn2) = make_node (BLOCK);
10141 BLOCK_SUPERCONTEXT (DECL_INITIAL (child_fn2)) = child_fn2;
10142 DECL_ATTRIBUTES (child_fn)
10143 = remove_attribute ("omp target entrypoint",
10144 DECL_ATTRIBUTES (child_fn));
10145 DECL_ATTRIBUTES (child_fn2)
10146 = tree_cons (get_identifier ("omp target device_ancestor_nohost"),
10147 NULL_TREE, copy_list (DECL_ATTRIBUTES (child_fn)));
10148 DECL_ATTRIBUTES (child_fn)
10149 = tree_cons (get_identifier ("omp target device_ancestor_host"),
10150 NULL_TREE, DECL_ATTRIBUTES (child_fn));
10151 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (child_fn2)
10152 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
10153 DECL_FUNCTION_SPECIFIC_TARGET (child_fn2)
10154 = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
10155 DECL_FUNCTION_VERSIONED (child_fn2)
10156 = DECL_FUNCTION_VERSIONED (current_function_decl);
10157
10158 fn2_node = cgraph_node::get_create (child_fn2);
10159 fn2_node->offloadable = 1;
10160 fn2_node->force_output = 1;
10161 node->offloadable = 0;
10162
10163 /* Enable pass_omp_device_lower pass. */
10164 fn2_node = cgraph_node::get (DECL_CONTEXT (child_fn));
10165 fn2_node->has_omp_variant_constructs = 1;
10166
10167 t = build_decl (DECL_SOURCE_LOCATION (child_fn),
10168 RESULT_DECL, NULL_TREE, void_type_node);
10169 DECL_ARTIFICIAL (t) = 1;
10170 DECL_IGNORED_P (t) = 1;
10171 DECL_CONTEXT (t) = child_fn2;
10172 DECL_RESULT (child_fn2) = t;
10173 DECL_SAVED_TREE (child_fn2) = build1 (RETURN_EXPR,
10174 void_type_node, NULL);
10175 tree tmp = DECL_ARGUMENTS (child_fn);
10176 t = build_decl (DECL_SOURCE_LOCATION (child_fn), PARM_DECL,
10177 DECL_NAME (tmp), TREE_TYPE (tmp));
10178 DECL_ARTIFICIAL (t) = 1;
10179 DECL_NAMELESS (t) = 1;
10180 DECL_ARG_TYPE (t) = ptr_type_node;
10181 DECL_CONTEXT (t) = current_function_decl;
10182 TREE_USED (t) = 1;
10183 TREE_READONLY (t) = 1;
10184 DECL_ARGUMENTS (child_fn2) = t;
10185 gcc_assert (TREE_CHAIN (tmp) == NULL_TREE);
10186
10187 gimplify_function_tree (child_fn2);
10188 cgraph_node::add_new_function (child_fn2, true);
10189
10190 vec_safe_push (offload_funcs, child_fn2);
10191 if (dump_file && !gimple_in_ssa_p (cfun))
10192 {
10193 dump_function_header (dump_file, child_fn2, dump_flags);
10194 dump_function_to_file (child_fn2, dump_file, dump_flags);
10195 }
10196 }
10197 }
10198
10199 /* Emit a library call to launch the offloading region, or do data
10200 transfers. */
10201 tree t1, t2, t3, t4, depend;
10202 enum built_in_function start_ix;
10203 unsigned int flags_i = 0;
10204
10205 switch (gimple_omp_target_kind (entry_stmt))
10206 {
10207 case GF_OMP_TARGET_KIND_REGION:
10208 start_ix = BUILT_IN_GOMP_TARGET;
10209 break;
10210 case GF_OMP_TARGET_KIND_DATA:
10211 start_ix = BUILT_IN_GOMP_TARGET_DATA;
10212 break;
10213 case GF_OMP_TARGET_KIND_UPDATE:
10214 start_ix = BUILT_IN_GOMP_TARGET_UPDATE;
10215 break;
10216 case GF_OMP_TARGET_KIND_ENTER_DATA:
10217 start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
10218 break;
10219 case GF_OMP_TARGET_KIND_EXIT_DATA:
10220 start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
10221 flags_i |= GOMP_TARGET_FLAG_EXIT_DATA;
10222 break;
10223 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
10224 case GF_OMP_TARGET_KIND_OACC_KERNELS:
10225 case GF_OMP_TARGET_KIND_OACC_SERIAL:
10226 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
10227 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
10228 start_ix = BUILT_IN_GOACC_PARALLEL;
10229 break;
10230 case GF_OMP_TARGET_KIND_OACC_DATA:
10231 case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
10232 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
10233 start_ix = BUILT_IN_GOACC_DATA_START;
10234 break;
10235 case GF_OMP_TARGET_KIND_OACC_UPDATE:
10236 start_ix = BUILT_IN_GOACC_UPDATE;
10237 break;
10238 case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
10239 start_ix = BUILT_IN_GOACC_ENTER_DATA;
10240 break;
10241 case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
10242 start_ix = BUILT_IN_GOACC_EXIT_DATA;
10243 break;
10244 case GF_OMP_TARGET_KIND_OACC_DECLARE:
10245 start_ix = BUILT_IN_GOACC_DECLARE;
10246 break;
10247 default:
10248 gcc_unreachable ();
10249 }
10250
10251 tree device = NULL_TREE;
10252 location_t device_loc = UNKNOWN_LOCATION;
10253 tree goacc_flags = NULL_TREE;
10254 bool need_device_adjustment = false;
10255 gimple_stmt_iterator adj_gsi;
10256 if (is_gimple_omp_oacc (entry_stmt))
10257 {
10258 /* By default, no GOACC_FLAGs are set. */
10259 goacc_flags = integer_zero_node;
10260 }
10261 else
10262 {
10263 c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE);
10264 if (c)
10265 {
10266 device = OMP_CLAUSE_DEVICE_ID (c);
10267 /* Ensure 'device' is of the correct type. */
10268 device = fold_convert_loc (device_loc, integer_type_node, device);
10269 if (TREE_CODE (device) == INTEGER_CST)
10270 {
10271 if (wi::to_wide (device) == GOMP_DEVICE_ICV)
10272 device = build_int_cst (integer_type_node,
10273 GOMP_DEVICE_HOST_FALLBACK);
10274 else if (wi::to_wide (device) == GOMP_DEVICE_HOST_FALLBACK)
10275 device = build_int_cst (integer_type_node,
10276 GOMP_DEVICE_HOST_FALLBACK - 1);
10277 }
10278 else
10279 need_device_adjustment = true;
10280 device_loc = OMP_CLAUSE_LOCATION (c);
10281 if (OMP_CLAUSE_DEVICE_ANCESTOR (c))
10282 device = build_int_cst (integer_type_node,
10283 GOMP_DEVICE_HOST_FALLBACK);
10284 }
10285 else
10286 {
10287 /* By default, the value of DEVICE is GOMP_DEVICE_ICV (let runtime
10288 library choose). */
10289 device = build_int_cst (integer_type_node, GOMP_DEVICE_ICV);
10290 device_loc = gimple_location (entry_stmt);
10291 }
10292
10293 c = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT);
10294 /* FIXME: in_reduction(...) nowait is unimplemented yet, pretend
10295 nowait doesn't appear. */
10296 if (c && omp_find_clause (clauses, OMP_CLAUSE_IN_REDUCTION))
10297 c = NULL;
10298 if (c)
10299 flags_i |= GOMP_TARGET_FLAG_NOWAIT;
10300 }
10301
10302 /* By default, there is no conditional. */
10303 tree cond = NULL_TREE;
10304 c = omp_find_clause (clauses, OMP_CLAUSE_IF);
10305 if (c)
10306 cond = OMP_CLAUSE_IF_EXPR (c);
10307 /* If we found the clause 'if (cond)', build:
10308 OpenACC: goacc_flags = (cond ? goacc_flags
10309 : goacc_flags | GOACC_FLAG_HOST_FALLBACK)
10310 OpenMP: device = (cond ? device : GOMP_DEVICE_HOST_FALLBACK) */
10311 if (cond)
10312 {
10313 tree *tp;
10314 if (is_gimple_omp_oacc (entry_stmt))
10315 tp = &goacc_flags;
10316 else
10317 tp = &device;
10318
10319 cond = gimple_boolify (cond);
10320
10321 basic_block cond_bb, then_bb, else_bb;
10322 edge e;
10323 tree tmp_var = create_tmp_var (TREE_TYPE (*tp));
10324 if (offloaded)
10325 e = split_block_after_labels (new_bb);
10326 else
10327 {
10328 gsi = gsi_last_nondebug_bb (new_bb);
10329 gsi_prev (&gsi);
10330 e = split_block (new_bb, gsi_stmt (gsi));
10331 }
10332 cond_bb = e->src;
10333 new_bb = e->dest;
10334 remove_edge (e);
10335
10336 then_bb = create_empty_bb (cond_bb);
10337 else_bb = create_empty_bb (then_bb);
10338 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10339 set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
10340
10341 stmt = gimple_build_cond_empty (cond);
10342 gsi = gsi_last_bb (cond_bb);
10343 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10344
10345 gsi = gsi_start_bb (then_bb);
10346 stmt = gimple_build_assign (tmp_var, *tp);
10347 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10348 adj_gsi = gsi;
10349
10350 gsi = gsi_start_bb (else_bb);
10351 if (is_gimple_omp_oacc (entry_stmt))
10352 stmt = gimple_build_assign (tmp_var,
10353 BIT_IOR_EXPR,
10354 *tp,
10355 build_int_cst (integer_type_node,
10356 GOACC_FLAG_HOST_FALLBACK));
10357 else
10358 stmt = gimple_build_assign (tmp_var,
10359 build_int_cst (integer_type_node,
10360 GOMP_DEVICE_HOST_FALLBACK));
10361 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10362
10363 make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10364 make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
10365 add_bb_to_loop (then_bb, cond_bb->loop_father);
10366 add_bb_to_loop (else_bb, cond_bb->loop_father);
10367 make_edge (then_bb, new_bb, EDGE_FALLTHRU);
10368 make_edge (else_bb, new_bb, EDGE_FALLTHRU);
10369
10370 *tp = tmp_var;
10371
10372 gsi = gsi_last_nondebug_bb (new_bb);
10373 }
10374 else
10375 {
10376 gsi = gsi_last_nondebug_bb (new_bb);
10377
10378 if (device != NULL_TREE)
10379 device = force_gimple_operand_gsi (&gsi, device, true, NULL_TREE,
10380 true, GSI_SAME_STMT);
10381 if (need_device_adjustment)
10382 {
10383 tree tmp_var = create_tmp_var (TREE_TYPE (device));
10384 stmt = gimple_build_assign (tmp_var, device);
10385 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
10386 adj_gsi = gsi_for_stmt (stmt);
10387 device = tmp_var;
10388 }
10389 }
10390
10391 if ((c = omp_find_clause (clauses, OMP_CLAUSE_SELF)) != NULL_TREE)
10392 {
10393 gcc_assert ((is_gimple_omp_oacc (entry_stmt) && offloaded)
10394 || (gimple_omp_target_kind (entry_stmt)
10395 == GF_OMP_TARGET_KIND_OACC_DATA_KERNELS));
10396
10397 edge e;
10398 if (offloaded)
10399 e = split_block_after_labels (new_bb);
10400 else
10401 {
10402 gsi = gsi_last_nondebug_bb (new_bb);
10403 gsi_prev (&gsi);
10404 e = split_block (new_bb, gsi_stmt (gsi));
10405 }
10406 basic_block cond_bb = e->src;
10407 new_bb = e->dest;
10408 remove_edge (e);
10409
10410 basic_block then_bb = create_empty_bb (cond_bb);
10411 basic_block else_bb = create_empty_bb (then_bb);
10412 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10413 set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
10414
10415 tree self_cond = gimple_boolify (OMP_CLAUSE_SELF_EXPR (c));
10416 stmt = gimple_build_cond_empty (self_cond);
10417 gsi = gsi_last_bb (cond_bb);
10418 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10419
10420 tree tmp_var = create_tmp_var (TREE_TYPE (goacc_flags));
10421 stmt = gimple_build_assign (tmp_var, BIT_IOR_EXPR, goacc_flags,
10422 build_int_cst (integer_type_node,
10423 GOACC_FLAG_LOCAL_DEVICE));
10424 gsi = gsi_start_bb (then_bb);
10425 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10426
10427 gsi = gsi_start_bb (else_bb);
10428 stmt = gimple_build_assign (tmp_var, goacc_flags);
10429 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
10430
10431 make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10432 make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
10433 add_bb_to_loop (then_bb, cond_bb->loop_father);
10434 add_bb_to_loop (else_bb, cond_bb->loop_father);
10435 make_edge (then_bb, new_bb, EDGE_FALLTHRU);
10436 make_edge (else_bb, new_bb, EDGE_FALLTHRU);
10437
10438 goacc_flags = tmp_var;
10439 gsi = gsi_last_nondebug_bb (new_bb);
10440 }
10441
10442 if (need_device_adjustment)
10443 {
10444 tree uns = fold_convert (unsigned_type_node, device);
10445 uns = force_gimple_operand_gsi (&adj_gsi, uns, true, NULL_TREE,
10446 false, GSI_CONTINUE_LINKING);
10447 edge e = split_block (gsi_bb (adj_gsi), gsi_stmt (adj_gsi));
10448 basic_block cond_bb = e->src;
10449 basic_block else_bb = e->dest;
10450 if (gsi_bb (adj_gsi) == new_bb)
10451 {
10452 new_bb = else_bb;
10453 gsi = gsi_last_nondebug_bb (new_bb);
10454 }
10455
10456 basic_block then_bb = create_empty_bb (cond_bb);
10457 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
10458
10459 cond = build2 (GT_EXPR, boolean_type_node, uns,
10460 build_int_cst (unsigned_type_node,
10461 GOMP_DEVICE_HOST_FALLBACK - 1));
10462 stmt = gimple_build_cond_empty (cond);
10463 adj_gsi = gsi_last_bb (cond_bb);
10464 gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
10465
10466 adj_gsi = gsi_start_bb (then_bb);
10467 tree add = build2 (PLUS_EXPR, integer_type_node, device,
10468 build_int_cst (integer_type_node, -1));
10469 stmt = gimple_build_assign (device, add);
10470 gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
10471
10472 make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
10473 e->flags = EDGE_FALSE_VALUE;
10474 add_bb_to_loop (then_bb, cond_bb->loop_father);
10475 make_edge (then_bb, else_bb, EDGE_FALLTHRU);
10476 }
10477
10478 t = gimple_omp_target_data_arg (entry_stmt);
10479 if (t == NULL)
10480 {
10481 t1 = size_zero_node;
10482 t2 = build_zero_cst (ptr_type_node);
10483 t3 = t2;
10484 t4 = t2;
10485 }
10486 else if (TREE_VEC_LENGTH (t) == 3 || is_gimple_omp_oacc (entry_stmt))
10487 {
10488 t1 = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (TREE_VEC_ELT (t, 1))));
10489 t1 = size_binop (PLUS_EXPR, t1, size_int (1));
10490 t2 = build_fold_addr_expr (TREE_VEC_ELT (t, 0));
10491 t3 = build_fold_addr_expr (TREE_VEC_ELT (t, 1));
10492 t4 = build_fold_addr_expr (TREE_VEC_ELT (t, 2));
10493 }
10494 else
10495 {
10496 t1 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 3), true, NULL_TREE,
10497 true, GSI_SAME_STMT);
10498 t2 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 0), true, NULL_TREE,
10499 true, GSI_SAME_STMT);
10500 t3 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 1), true, NULL_TREE,
10501 true, GSI_SAME_STMT);
10502 t4 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 2), true, NULL_TREE,
10503 true, GSI_SAME_STMT);
10504 }
10505
10506 gimple *g;
10507 bool tagging = false;
10508 /* The maximum number used by any start_ix, without varargs. */
10509 auto_vec<tree, 11> args;
10510 if (is_gimple_omp_oacc (entry_stmt))
10511 {
10512 tree goacc_flags_m = fold_build1 (GOACC_FLAGS_MARSHAL_OP,
10513 TREE_TYPE (goacc_flags), goacc_flags);
10514 goacc_flags_m = force_gimple_operand_gsi (&gsi, goacc_flags_m, true,
10515 NULL_TREE, true,
10516 GSI_SAME_STMT);
10517 args.quick_push (goacc_flags_m);
10518 }
10519 else
10520 args.quick_push (device);
10521 if (offloaded)
10522 args.quick_push (build_fold_addr_expr (child_fn2 ? child_fn2 : child_fn));
10523 args.quick_push (t1);
10524 args.quick_push (t2);
10525 args.quick_push (t3);
10526 args.quick_push (t4);
10527 switch (start_ix)
10528 {
10529 case BUILT_IN_GOACC_DATA_START:
10530 case BUILT_IN_GOACC_DECLARE:
10531 case BUILT_IN_GOMP_TARGET_DATA:
10532 break;
10533 case BUILT_IN_GOMP_TARGET:
10534 case BUILT_IN_GOMP_TARGET_UPDATE:
10535 case BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA:
10536 args.quick_push (build_int_cst (unsigned_type_node, flags_i));
10537 c = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
10538 if (c)
10539 depend = OMP_CLAUSE_DECL (c);
10540 else
10541 depend = build_int_cst (ptr_type_node, 0);
10542 args.quick_push (depend);
10543 if (start_ix == BUILT_IN_GOMP_TARGET)
10544 args.quick_push (get_target_arguments (&gsi, entry_stmt));
10545 break;
10546 case BUILT_IN_GOACC_PARALLEL:
10547 if (lookup_attribute ("oacc serial", DECL_ATTRIBUTES (child_fn)) != NULL)
10548 {
10549 tree dims = NULL_TREE;
10550 unsigned int ix;
10551
10552 /* For serial constructs we set all dimensions to 1. */
10553 for (ix = GOMP_DIM_MAX; ix--;)
10554 dims = tree_cons (NULL_TREE, integer_one_node, dims);
10555 oacc_replace_fn_attrib (child_fn, dims);
10556 }
10557 else
10558 oacc_set_fn_attrib (child_fn, clauses, &args);
10559 tagging = true;
10560 /* FALLTHRU */
10561 case BUILT_IN_GOACC_ENTER_DATA:
10562 case BUILT_IN_GOACC_EXIT_DATA:
10563 case BUILT_IN_GOACC_UPDATE:
10564 {
10565 tree t_async = NULL_TREE;
10566
10567 /* If present, use the value specified by the respective
10568 clause, making sure that is of the correct type. */
10569 c = omp_find_clause (clauses, OMP_CLAUSE_ASYNC);
10570 if (c)
10571 t_async = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
10572 integer_type_node,
10573 OMP_CLAUSE_ASYNC_EXPR (c));
10574 else if (!tagging)
10575 /* Default values for t_async. */
10576 t_async = fold_convert_loc (gimple_location (entry_stmt),
10577 integer_type_node,
10578 build_int_cst (integer_type_node,
10579 GOMP_ASYNC_SYNC));
10580 if (tagging && t_async)
10581 {
10582 unsigned HOST_WIDE_INT i_async = GOMP_LAUNCH_OP_MAX;
10583
10584 if (TREE_CODE (t_async) == INTEGER_CST)
10585 {
10586 /* See if we can pack the async arg in to the tag's
10587 operand. */
10588 i_async = TREE_INT_CST_LOW (t_async);
10589 if (i_async < GOMP_LAUNCH_OP_MAX)
10590 t_async = NULL_TREE;
10591 else
10592 i_async = GOMP_LAUNCH_OP_MAX;
10593 }
10594 args.safe_push (oacc_launch_pack (GOMP_LAUNCH_ASYNC, NULL_TREE,
10595 i_async));
10596 }
10597 if (t_async)
10598 args.safe_push (force_gimple_operand_gsi (&gsi, t_async, true,
10599 NULL_TREE, true,
10600 GSI_SAME_STMT));
10601
10602 /* Save the argument index, and ... */
10603 unsigned t_wait_idx = args.length ();
10604 unsigned num_waits = 0;
10605 c = omp_find_clause (clauses, OMP_CLAUSE_WAIT);
10606 if (!tagging || c)
10607 /* ... push a placeholder. */
10608 args.safe_push (integer_zero_node);
10609
10610 for (; c; c = OMP_CLAUSE_CHAIN (c))
10611 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_WAIT)
10612 {
10613 tree arg = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
10614 integer_type_node,
10615 OMP_CLAUSE_WAIT_EXPR (c));
10616 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE, true,
10617 GSI_SAME_STMT);
10618 args.safe_push (arg);
10619 num_waits++;
10620 }
10621
10622 if (!tagging || num_waits)
10623 {
10624 tree len;
10625
10626 /* Now that we know the number, update the placeholder. */
10627 if (tagging)
10628 len = oacc_launch_pack (GOMP_LAUNCH_WAIT, NULL_TREE, num_waits);
10629 else
10630 len = build_int_cst (integer_type_node, num_waits);
10631 len = fold_convert_loc (gimple_location (entry_stmt),
10632 unsigned_type_node, len);
10633 args[t_wait_idx] = len;
10634 }
10635 }
10636 break;
10637 default:
10638 gcc_unreachable ();
10639 }
10640 if (tagging)
10641 /* Push terminal marker - zero. */
10642 args.safe_push (oacc_launch_pack (0, NULL_TREE, 0));
10643
10644 if (child_fn2)
10645 {
10646 g = gimple_build_call_internal (IFN_GOMP_TARGET_REV, 1,
10647 build_fold_addr_expr (child_fn));
10648 gimple_set_location (g, gimple_location (entry_stmt));
10649 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
10650 }
10651
10652 g = gimple_build_call_vec (builtin_decl_explicit (start_ix), args);
10653 gimple_set_location (g, gimple_location (entry_stmt));
10654 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
10655 if (!offloaded)
10656 {
10657 g = gsi_stmt (gsi);
10658 gcc_assert (g && gimple_code (g) == GIMPLE_OMP_TARGET);
10659 gsi_remove (&gsi, true);
10660 }
10661 }
10662
10663 /* Expand the parallel region tree rooted at REGION. Expansion
10664 proceeds in depth-first order. Innermost regions are expanded
10665 first. This way, parallel regions that require a new function to
10666 be created (e.g., GIMPLE_OMP_PARALLEL) can be expanded without having any
10667 internal dependencies in their body. */
10668
10669 static void
10670 expand_omp (struct omp_region *region)
10671 {
10672 omp_any_child_fn_dumped = false;
10673 while (region)
10674 {
10675 location_t saved_location;
10676 gimple *inner_stmt = NULL;
10677
10678 /* First, determine whether this is a combined parallel+workshare
10679 region. */
10680 if (region->type == GIMPLE_OMP_PARALLEL)
10681 determine_parallel_type (region);
10682
10683 if (region->type == GIMPLE_OMP_FOR
10684 && gimple_omp_for_combined_p (last_nondebug_stmt (region->entry)))
10685 inner_stmt = last_nondebug_stmt (region->inner->entry);
10686
10687 if (region->inner)
10688 expand_omp (region->inner);
10689
10690 saved_location = input_location;
10691 if (gimple_has_location (last_nondebug_stmt (region->entry)))
10692 input_location = gimple_location (last_nondebug_stmt (region->entry));
10693
10694 switch (region->type)
10695 {
10696 case GIMPLE_OMP_PARALLEL:
10697 case GIMPLE_OMP_TASK:
10698 expand_omp_taskreg (region);
10699 break;
10700
10701 case GIMPLE_OMP_FOR:
10702 expand_omp_for (region, inner_stmt);
10703 break;
10704
10705 case GIMPLE_OMP_SECTIONS:
10706 expand_omp_sections (region);
10707 break;
10708
10709 case GIMPLE_OMP_SECTION:
10710 /* Individual omp sections are handled together with their
10711 parent GIMPLE_OMP_SECTIONS region. */
10712 break;
10713
10714 case GIMPLE_OMP_STRUCTURED_BLOCK:
10715 /* We should have gotten rid of these in gimple lowering. */
10716 gcc_unreachable ();
10717
10718 case GIMPLE_OMP_SINGLE:
10719 case GIMPLE_OMP_SCOPE:
10720 expand_omp_single (region);
10721 break;
10722
10723 case GIMPLE_OMP_ORDERED:
10724 {
10725 gomp_ordered *ord_stmt
10726 = as_a <gomp_ordered *> (last_nondebug_stmt (region->entry));
10727 if (gimple_omp_ordered_standalone_p (ord_stmt))
10728 {
10729 /* We'll expand these when expanding corresponding
10730 worksharing region with ordered(n) clause. */
10731 gcc_assert (region->outer
10732 && region->outer->type == GIMPLE_OMP_FOR);
10733 region->ord_stmt = ord_stmt;
10734 break;
10735 }
10736 }
10737 /* FALLTHRU */
10738 case GIMPLE_OMP_MASTER:
10739 case GIMPLE_OMP_MASKED:
10740 case GIMPLE_OMP_TASKGROUP:
10741 case GIMPLE_OMP_CRITICAL:
10742 case GIMPLE_OMP_TEAMS:
10743 expand_omp_synch (region);
10744 break;
10745
10746 case GIMPLE_OMP_ATOMIC_LOAD:
10747 expand_omp_atomic (region);
10748 break;
10749
10750 case GIMPLE_OMP_TARGET:
10751 expand_omp_target (region);
10752 break;
10753
10754 default:
10755 gcc_unreachable ();
10756 }
10757
10758 input_location = saved_location;
10759 region = region->next;
10760 }
10761 if (omp_any_child_fn_dumped)
10762 {
10763 if (dump_file)
10764 dump_function_header (dump_file, current_function_decl, dump_flags);
10765 omp_any_child_fn_dumped = false;
10766 }
10767 }
10768
10769 /* Helper for build_omp_regions. Scan the dominator tree starting at
10770 block BB. PARENT is the region that contains BB. If SINGLE_TREE is
10771 true, the function ends once a single tree is built (otherwise, whole
10772 forest of OMP constructs may be built). */
10773
10774 static void
10775 build_omp_regions_1 (basic_block bb, struct omp_region *parent,
10776 bool single_tree)
10777 {
10778 gimple_stmt_iterator gsi;
10779 gimple *stmt;
10780 basic_block son;
10781
10782 gsi = gsi_last_nondebug_bb (bb);
10783 if (!gsi_end_p (gsi) && is_gimple_omp (gsi_stmt (gsi)))
10784 {
10785 struct omp_region *region;
10786 enum gimple_code code;
10787
10788 stmt = gsi_stmt (gsi);
10789 code = gimple_code (stmt);
10790 if (code == GIMPLE_OMP_RETURN)
10791 {
10792 /* STMT is the return point out of region PARENT. Mark it
10793 as the exit point and make PARENT the immediately
10794 enclosing region. */
10795 gcc_assert (parent);
10796 region = parent;
10797 region->exit = bb;
10798 parent = parent->outer;
10799 }
10800 else if (code == GIMPLE_OMP_ATOMIC_STORE)
10801 {
10802 /* GIMPLE_OMP_ATOMIC_STORE is analogous to
10803 GIMPLE_OMP_RETURN, but matches with
10804 GIMPLE_OMP_ATOMIC_LOAD. */
10805 gcc_assert (parent);
10806 gcc_assert (parent->type == GIMPLE_OMP_ATOMIC_LOAD);
10807 region = parent;
10808 region->exit = bb;
10809 parent = parent->outer;
10810 }
10811 else if (code == GIMPLE_OMP_CONTINUE)
10812 {
10813 gcc_assert (parent);
10814 parent->cont = bb;
10815 }
10816 else if (code == GIMPLE_OMP_SECTIONS_SWITCH)
10817 {
10818 /* GIMPLE_OMP_SECTIONS_SWITCH is part of
10819 GIMPLE_OMP_SECTIONS, and we do nothing for it. */
10820 }
10821 else
10822 {
10823 region = new_omp_region (bb, code, parent);
10824 /* Otherwise... */
10825 if (code == GIMPLE_OMP_TARGET)
10826 {
10827 switch (gimple_omp_target_kind (stmt))
10828 {
10829 case GF_OMP_TARGET_KIND_REGION:
10830 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
10831 case GF_OMP_TARGET_KIND_OACC_KERNELS:
10832 case GF_OMP_TARGET_KIND_OACC_SERIAL:
10833 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
10834 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
10835 break;
10836 case GF_OMP_TARGET_KIND_UPDATE:
10837 case GF_OMP_TARGET_KIND_ENTER_DATA:
10838 case GF_OMP_TARGET_KIND_EXIT_DATA:
10839 case GF_OMP_TARGET_KIND_DATA:
10840 case GF_OMP_TARGET_KIND_OACC_DATA:
10841 case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
10842 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
10843 case GF_OMP_TARGET_KIND_OACC_UPDATE:
10844 case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
10845 case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
10846 case GF_OMP_TARGET_KIND_OACC_DECLARE:
10847 /* ..., other than for those stand-alone directives...
10848 To be precise, target data isn't stand-alone, but
10849 gimplifier put the end API call into try finally block
10850 for it, so omp expansion can treat it as such. */
10851 region = NULL;
10852 break;
10853 default:
10854 gcc_unreachable ();
10855 }
10856 }
10857 else if (code == GIMPLE_OMP_ORDERED
10858 && gimple_omp_ordered_standalone_p (stmt))
10859 /* #pragma omp ordered depend is also just a stand-alone
10860 directive. */
10861 region = NULL;
10862 else if (code == GIMPLE_OMP_TASK
10863 && gimple_omp_task_taskwait_p (stmt))
10864 /* #pragma omp taskwait depend(...) is a stand-alone directive. */
10865 region = NULL;
10866 else if (code == GIMPLE_OMP_TASKGROUP)
10867 /* #pragma omp taskgroup isn't a stand-alone directive, but
10868 gimplifier put the end API call into try finall block
10869 for it, so omp expansion can treat it as such. */
10870 region = NULL;
10871 /* ..., this directive becomes the parent for a new region. */
10872 if (region)
10873 parent = region;
10874 }
10875 }
10876
10877 if (single_tree && !parent)
10878 return;
10879
10880 for (son = first_dom_son (CDI_DOMINATORS, bb);
10881 son;
10882 son = next_dom_son (CDI_DOMINATORS, son))
10883 build_omp_regions_1 (son, parent, single_tree);
10884 }
10885
10886 /* Builds the tree of OMP regions rooted at ROOT, storing it to
10887 root_omp_region. */
10888
10889 static void
10890 build_omp_regions_root (basic_block root)
10891 {
10892 gcc_assert (root_omp_region == NULL);
10893 build_omp_regions_1 (root, NULL, true);
10894 gcc_assert (root_omp_region != NULL);
10895 }
10896
10897 /* Expands omp construct (and its subconstructs) starting in HEAD. */
10898
10899 void
10900 omp_expand_local (basic_block head)
10901 {
10902 build_omp_regions_root (head);
10903 if (dump_file && (dump_flags & TDF_DETAILS))
10904 {
10905 fprintf (dump_file, "\nOMP region tree\n\n");
10906 dump_omp_region (dump_file, root_omp_region, 0);
10907 fprintf (dump_file, "\n");
10908 }
10909
10910 remove_exit_barriers (root_omp_region);
10911 expand_omp (root_omp_region);
10912
10913 omp_free_regions ();
10914 }
10915
10916 /* Scan the CFG and build a tree of OMP regions. Return the root of
10917 the OMP region tree. */
10918
10919 static void
10920 build_omp_regions (void)
10921 {
10922 gcc_assert (root_omp_region == NULL);
10923 calculate_dominance_info (CDI_DOMINATORS);
10924 build_omp_regions_1 (ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, false);
10925 }
10926
10927 /* Main entry point for expanding OMP-GIMPLE into runtime calls. */
10928
10929 static unsigned int
10930 execute_expand_omp (void)
10931 {
10932 build_omp_regions ();
10933
10934 if (!root_omp_region)
10935 return 0;
10936
10937 if (dump_file)
10938 {
10939 fprintf (dump_file, "\nOMP region tree\n\n");
10940 dump_omp_region (dump_file, root_omp_region, 0);
10941 fprintf (dump_file, "\n");
10942 }
10943
10944 remove_exit_barriers (root_omp_region);
10945
10946 expand_omp (root_omp_region);
10947
10948 omp_free_regions ();
10949
10950 return (TODO_cleanup_cfg
10951 | (gimple_in_ssa_p (cfun) ? TODO_update_ssa_only_virtuals : 0));
10952 }
10953
10954 /* OMP expansion -- the default pass, run before creation of SSA form. */
10955
10956 namespace {
10957
10958 const pass_data pass_data_expand_omp =
10959 {
10960 GIMPLE_PASS, /* type */
10961 "ompexp", /* name */
10962 OPTGROUP_OMP, /* optinfo_flags */
10963 TV_NONE, /* tv_id */
10964 PROP_gimple_any, /* properties_required */
10965 PROP_gimple_eomp, /* properties_provided */
10966 0, /* properties_destroyed */
10967 0, /* todo_flags_start */
10968 0, /* todo_flags_finish */
10969 };
10970
10971 class pass_expand_omp : public gimple_opt_pass
10972 {
10973 public:
10974 pass_expand_omp (gcc::context *ctxt)
10975 : gimple_opt_pass (pass_data_expand_omp, ctxt)
10976 {}
10977
10978 /* opt_pass methods: */
10979 unsigned int execute (function *) final override
10980 {
10981 bool gate = ((flag_openacc != 0 || flag_openmp != 0
10982 || flag_openmp_simd != 0)
10983 && !seen_error ());
10984
10985 /* This pass always runs, to provide PROP_gimple_eomp.
10986 But often, there is nothing to do. */
10987 if (!gate)
10988 return 0;
10989
10990 return execute_expand_omp ();
10991 }
10992
10993 }; // class pass_expand_omp
10994
10995 } // anon namespace
10996
10997 gimple_opt_pass *
10998 make_pass_expand_omp (gcc::context *ctxt)
10999 {
11000 return new pass_expand_omp (ctxt);
11001 }
11002
11003 namespace {
11004
11005 const pass_data pass_data_expand_omp_ssa =
11006 {
11007 GIMPLE_PASS, /* type */
11008 "ompexpssa", /* name */
11009 OPTGROUP_OMP, /* optinfo_flags */
11010 TV_NONE, /* tv_id */
11011 PROP_cfg | PROP_ssa, /* properties_required */
11012 PROP_gimple_eomp, /* properties_provided */
11013 0, /* properties_destroyed */
11014 0, /* todo_flags_start */
11015 TODO_cleanup_cfg | TODO_rebuild_alias, /* todo_flags_finish */
11016 };
11017
11018 class pass_expand_omp_ssa : public gimple_opt_pass
11019 {
11020 public:
11021 pass_expand_omp_ssa (gcc::context *ctxt)
11022 : gimple_opt_pass (pass_data_expand_omp_ssa, ctxt)
11023 {}
11024
11025 /* opt_pass methods: */
11026 bool gate (function *fun) final override
11027 {
11028 return !(fun->curr_properties & PROP_gimple_eomp);
11029 }
11030 unsigned int execute (function *) final override
11031 {
11032 return execute_expand_omp ();
11033 }
11034 opt_pass * clone () final override
11035 {
11036 return new pass_expand_omp_ssa (m_ctxt);
11037 }
11038
11039 }; // class pass_expand_omp_ssa
11040
11041 } // anon namespace
11042
11043 gimple_opt_pass *
11044 make_pass_expand_omp_ssa (gcc::context *ctxt)
11045 {
11046 return new pass_expand_omp_ssa (ctxt);
11047 }
11048
11049 /* Called from tree-cfg.cc::make_edges to create cfg edges for all relevant
11050 GIMPLE_* codes. */
11051
11052 bool
11053 omp_make_gimple_edges (basic_block bb, struct omp_region **region,
11054 int *region_idx)
11055 {
11056 gimple *last = last_nondebug_stmt (bb);
11057 enum gimple_code code = gimple_code (last);
11058 struct omp_region *cur_region = *region;
11059 bool fallthru = false;
11060
11061 switch (code)
11062 {
11063 case GIMPLE_OMP_PARALLEL:
11064 case GIMPLE_OMP_FOR:
11065 case GIMPLE_OMP_SINGLE:
11066 case GIMPLE_OMP_TEAMS:
11067 case GIMPLE_OMP_MASTER:
11068 case GIMPLE_OMP_MASKED:
11069 case GIMPLE_OMP_SCOPE:
11070 case GIMPLE_OMP_CRITICAL:
11071 case GIMPLE_OMP_SECTION:
11072 cur_region = new_omp_region (bb, code, cur_region);
11073 fallthru = true;
11074 break;
11075
11076 case GIMPLE_OMP_TASKGROUP:
11077 cur_region = new_omp_region (bb, code, cur_region);
11078 fallthru = true;
11079 cur_region = cur_region->outer;
11080 break;
11081
11082 case GIMPLE_OMP_TASK:
11083 cur_region = new_omp_region (bb, code, cur_region);
11084 fallthru = true;
11085 if (gimple_omp_task_taskwait_p (last))
11086 cur_region = cur_region->outer;
11087 break;
11088
11089 case GIMPLE_OMP_ORDERED:
11090 cur_region = new_omp_region (bb, code, cur_region);
11091 fallthru = true;
11092 if (gimple_omp_ordered_standalone_p (last))
11093 cur_region = cur_region->outer;
11094 break;
11095
11096 case GIMPLE_OMP_TARGET:
11097 cur_region = new_omp_region (bb, code, cur_region);
11098 fallthru = true;
11099 switch (gimple_omp_target_kind (last))
11100 {
11101 case GF_OMP_TARGET_KIND_REGION:
11102 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
11103 case GF_OMP_TARGET_KIND_OACC_KERNELS:
11104 case GF_OMP_TARGET_KIND_OACC_SERIAL:
11105 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
11106 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
11107 break;
11108 case GF_OMP_TARGET_KIND_UPDATE:
11109 case GF_OMP_TARGET_KIND_ENTER_DATA:
11110 case GF_OMP_TARGET_KIND_EXIT_DATA:
11111 case GF_OMP_TARGET_KIND_DATA:
11112 case GF_OMP_TARGET_KIND_OACC_DATA:
11113 case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
11114 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
11115 case GF_OMP_TARGET_KIND_OACC_UPDATE:
11116 case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
11117 case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
11118 case GF_OMP_TARGET_KIND_OACC_DECLARE:
11119 cur_region = cur_region->outer;
11120 break;
11121 default:
11122 gcc_unreachable ();
11123 }
11124 break;
11125
11126 case GIMPLE_OMP_SECTIONS:
11127 cur_region = new_omp_region (bb, code, cur_region);
11128 fallthru = true;
11129 break;
11130
11131 case GIMPLE_OMP_SECTIONS_SWITCH:
11132 fallthru = false;
11133 break;
11134
11135 case GIMPLE_OMP_ATOMIC_LOAD:
11136 case GIMPLE_OMP_ATOMIC_STORE:
11137 fallthru = true;
11138 break;
11139
11140 case GIMPLE_OMP_RETURN:
11141 /* In the case of a GIMPLE_OMP_SECTION, the edge will go
11142 somewhere other than the next block. This will be
11143 created later. */
11144 cur_region->exit = bb;
11145 if (cur_region->type == GIMPLE_OMP_TASK)
11146 /* Add an edge corresponding to not scheduling the task
11147 immediately. */
11148 make_edge (cur_region->entry, bb, EDGE_ABNORMAL);
11149 fallthru = cur_region->type != GIMPLE_OMP_SECTION;
11150 cur_region = cur_region->outer;
11151 break;
11152
11153 case GIMPLE_OMP_CONTINUE:
11154 cur_region->cont = bb;
11155 switch (cur_region->type)
11156 {
11157 case GIMPLE_OMP_FOR:
11158 /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
11159 succs edges as abnormal to prevent splitting
11160 them. */
11161 single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
11162 /* Make the loopback edge. */
11163 make_edge (bb, single_succ (cur_region->entry),
11164 EDGE_ABNORMAL);
11165
11166 /* Create an edge from GIMPLE_OMP_FOR to exit, which
11167 corresponds to the case that the body of the loop
11168 is not executed at all. */
11169 make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
11170 make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
11171 fallthru = false;
11172 break;
11173
11174 case GIMPLE_OMP_SECTIONS:
11175 /* Wire up the edges into and out of the nested sections. */
11176 {
11177 basic_block switch_bb = single_succ (cur_region->entry);
11178
11179 struct omp_region *i;
11180 for (i = cur_region->inner; i ; i = i->next)
11181 {
11182 gcc_assert (i->type == GIMPLE_OMP_SECTION);
11183 make_edge (switch_bb, i->entry, 0);
11184 make_edge (i->exit, bb, EDGE_FALLTHRU);
11185 }
11186
11187 /* Make the loopback edge to the block with
11188 GIMPLE_OMP_SECTIONS_SWITCH. */
11189 make_edge (bb, switch_bb, 0);
11190
11191 /* Make the edge from the switch to exit. */
11192 make_edge (switch_bb, bb->next_bb, 0);
11193 fallthru = false;
11194 }
11195 break;
11196
11197 case GIMPLE_OMP_TASK:
11198 fallthru = true;
11199 break;
11200
11201 default:
11202 gcc_unreachable ();
11203 }
11204 break;
11205
11206 default:
11207 gcc_unreachable ();
11208 }
11209
11210 if (*region != cur_region)
11211 {
11212 *region = cur_region;
11213 if (cur_region)
11214 *region_idx = cur_region->entry->index;
11215 else
11216 *region_idx = 0;
11217 }
11218
11219 return fallthru;
11220 }