1 /* Lowering pass for OpenMP directives. Converts OpenMP directives
2 into explicit calls to the runtime library (libgomp) and data
3 marshalling to implement data sharing and copying clauses.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "coretypes.h"
31 #include "tree-gimple.h"
32 #include "tree-inline.h"
33 #include "langhooks.h"
34 #include "diagnostic.h"
35 #include "tree-flow.h"
41 #include "tree-pass.h"
46 /* Lowering of OpenMP parallel and workshare constructs proceeds in two
47 phases. The first phase scans the function looking for OMP statements
48 and then for variables that must be replaced to satisfy data sharing
49 clauses. The second phase expands code for the constructs, as well as
50 re-gimplifying things when variables have been replaced with complex
53 Final code generation is done by pass_expand_omp. The flowgraph is
54 scanned for parallel regions which are then moved to a new
55 function, to be invoked by the thread library. */
57 /* Context structure. Used to store information about each parallel
58 directive in the code. */
60 typedef struct omp_context
62 /* This field must be at the beginning, as we do "inheritance": Some
63 callback functions for tree-inline.c (e.g., omp_copy_decl)
64 receive a copy_body_data pointer that is up-casted to an
65 omp_context pointer. */
68 /* The tree of contexts corresponding to the encountered constructs. */
69 struct omp_context *outer;
72 /* Map variables to fields in a structure that allows communication
73 between sending and receiving threads. */
79 /* A chain of variables to add to the top-level block surrounding the
80 construct. In the case of a parallel, this is in the child function. */
83 /* What to do with variables with implicitly determined sharing
85 enum omp_clause_default_kind default_kind;
87 /* Nesting depth of this context. Used to beautify error messages re
88 invalid gotos. The outermost ctx is depth 1, with depth 0 being
89 reserved for the main body of the function. */
92 /* True if this parallel directive is nested within another. */
97 /* A structure describing the main elements of a parallel loop. */
101 tree v, n1, n2, step, chunk_size, for_stmt;
102 enum tree_code cond_code;
104 bool have_nowait, have_ordered;
105 enum omp_clause_schedule_kind sched_kind;
109 static splay_tree all_contexts;
110 static int parallel_nesting_level;
111 struct omp_region *root_omp_region;
113 static void scan_omp (tree *, omp_context *);
114 static void lower_omp (tree *, omp_context *);
115 static tree lookup_decl_in_outer_ctx (tree, omp_context *);
116 static tree maybe_lookup_decl_in_outer_ctx (tree, omp_context *);
118 /* Find an OpenMP clause of type KIND within CLAUSES. */
121 find_omp_clause (tree clauses, enum tree_code kind)
123 for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
124 if (OMP_CLAUSE_CODE (clauses) == kind)
130 /* Return true if CTX is for an omp parallel. */
133 is_parallel_ctx (omp_context *ctx)
135 return TREE_CODE (ctx->stmt) == OMP_PARALLEL;
139 /* Return true if REGION is a combined parallel+workshare region. */
142 is_combined_parallel (struct omp_region *region)
144 return region->is_combined_parallel;
148 /* Extract the header elements of parallel loop FOR_STMT and store
152 extract_omp_for_data (tree for_stmt, struct omp_for_data *fd)
156 fd->for_stmt = for_stmt;
159 t = OMP_FOR_INIT (for_stmt);
160 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
161 fd->v = TREE_OPERAND (t, 0);
162 gcc_assert (DECL_P (fd->v));
163 gcc_assert (TREE_CODE (TREE_TYPE (fd->v)) == INTEGER_TYPE);
164 fd->n1 = TREE_OPERAND (t, 1);
166 t = OMP_FOR_COND (for_stmt);
167 fd->cond_code = TREE_CODE (t);
168 gcc_assert (TREE_OPERAND (t, 0) == fd->v);
169 fd->n2 = TREE_OPERAND (t, 1);
170 switch (fd->cond_code)
176 fd->n2 = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->n2), fd->n2,
177 build_int_cst (TREE_TYPE (fd->n2), 1));
178 fd->cond_code = LT_EXPR;
181 fd->n2 = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->n2), fd->n2,
182 build_int_cst (TREE_TYPE (fd->n2), 1));
183 fd->cond_code = GT_EXPR;
189 t = OMP_FOR_INCR (fd->for_stmt);
190 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
191 gcc_assert (TREE_OPERAND (t, 0) == fd->v);
192 t = TREE_OPERAND (t, 1);
193 gcc_assert (TREE_OPERAND (t, 0) == fd->v);
194 switch (TREE_CODE (t))
197 fd->step = TREE_OPERAND (t, 1);
200 fd->step = TREE_OPERAND (t, 1);
201 fd->step = fold_build1 (NEGATE_EXPR, TREE_TYPE (fd->step), fd->step);
207 fd->have_nowait = fd->have_ordered = false;
208 fd->sched_kind = OMP_CLAUSE_SCHEDULE_STATIC;
209 fd->chunk_size = NULL_TREE;
211 for (t = OMP_FOR_CLAUSES (for_stmt); t ; t = OMP_CLAUSE_CHAIN (t))
212 switch (OMP_CLAUSE_CODE (t))
214 case OMP_CLAUSE_NOWAIT:
215 fd->have_nowait = true;
217 case OMP_CLAUSE_ORDERED:
218 fd->have_ordered = true;
220 case OMP_CLAUSE_SCHEDULE:
221 fd->sched_kind = OMP_CLAUSE_SCHEDULE_KIND (t);
222 fd->chunk_size = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (t);
228 if (fd->sched_kind == OMP_CLAUSE_SCHEDULE_RUNTIME)
229 gcc_assert (fd->chunk_size == NULL);
230 else if (fd->chunk_size == NULL)
232 /* We only need to compute a default chunk size for ordered
233 static loops and dynamic loops. */
234 if (fd->sched_kind != OMP_CLAUSE_SCHEDULE_STATIC || fd->have_ordered)
235 fd->chunk_size = (fd->sched_kind == OMP_CLAUSE_SCHEDULE_STATIC)
236 ? integer_zero_node : integer_one_node;
241 /* Given two blocks PAR_ENTRY_BB and WS_ENTRY_BB such that WS_ENTRY_BB
242 is the immediate dominator of PAR_ENTRY_BB, return true if there
243 are no data dependencies that would prevent expanding the parallel
244 directive at PAR_ENTRY_BB as a combined parallel+workshare region.
246 When expanding a combined parallel+workshare region, the call to
247 the child function may need additional arguments in the case of
248 OMP_FOR regions. In some cases, these arguments are computed out
249 of variables passed in from the parent to the child via 'struct
250 .omp_data_s'. For instance:
252 #pragma omp parallel for schedule (guided, i * 4)
257 # BLOCK 2 (PAR_ENTRY_BB)
259 #pragma omp parallel [child fn: bar.omp_fn.0 ( ..., D.1598)
261 # BLOCK 3 (WS_ENTRY_BB)
262 .omp_data_i = &.omp_data_o;
263 D.1667 = .omp_data_i->i;
265 #pragma omp for schedule (guided, D.1598)
267 When we outline the parallel region, the call to the child function
268 'bar.omp_fn.0' will need the value D.1598 in its argument list, but
269 that value is computed *after* the call site. So, in principle we
270 cannot do the transformation.
272 To see whether the code in WS_ENTRY_BB blocks the combined
273 parallel+workshare call, we collect all the variables used in the
274 OMP_FOR header check whether they appear on the LHS of any
275 statement in WS_ENTRY_BB. If so, then we cannot emit the combined
278 FIXME. If we had the SSA form built at this point, we could merely
279 hoist the code in block 3 into block 2 and be done with it. But at
280 this point we don't have dataflow information and though we could
281 hack something up here, it is really not worth the aggravation. */
284 workshare_safe_to_combine_p (basic_block par_entry_bb, basic_block ws_entry_bb)
286 struct omp_for_data fd;
287 tree par_stmt, ws_stmt;
289 par_stmt = last_stmt (par_entry_bb);
290 ws_stmt = last_stmt (ws_entry_bb);
292 if (TREE_CODE (ws_stmt) == OMP_SECTIONS)
295 gcc_assert (TREE_CODE (ws_stmt) == OMP_FOR);
297 extract_omp_for_data (ws_stmt, &fd);
299 /* FIXME. We give up too easily here. If any of these arguments
300 are not constants, they will likely involve variables that have
301 been mapped into fields of .omp_data_s for sharing with the child
302 function. With appropriate data flow, it would be possible to
304 if (!is_gimple_min_invariant (fd.n1)
305 || !is_gimple_min_invariant (fd.n2)
306 || !is_gimple_min_invariant (fd.step)
307 || (fd.chunk_size && !is_gimple_min_invariant (fd.chunk_size)))
314 /* Collect additional arguments needed to emit a combined
315 parallel+workshare call. WS_STMT is the workshare directive being
319 get_ws_args_for (tree ws_stmt)
323 if (TREE_CODE (ws_stmt) == OMP_FOR)
325 struct omp_for_data fd;
328 extract_omp_for_data (ws_stmt, &fd);
333 t = fold_convert (long_integer_type_node, fd.chunk_size);
334 ws_args = tree_cons (NULL, t, ws_args);
337 t = fold_convert (long_integer_type_node, fd.step);
338 ws_args = tree_cons (NULL, t, ws_args);
340 t = fold_convert (long_integer_type_node, fd.n2);
341 ws_args = tree_cons (NULL, t, ws_args);
343 t = fold_convert (long_integer_type_node, fd.n1);
344 ws_args = tree_cons (NULL, t, ws_args);
348 else if (TREE_CODE (ws_stmt) == OMP_SECTIONS)
350 basic_block bb = bb_for_stmt (ws_stmt);
351 t = build_int_cst (unsigned_type_node, EDGE_COUNT (bb->succs));
352 t = tree_cons (NULL, t, NULL);
360 /* Discover whether REGION is a combined parallel+workshare region. */
363 determine_parallel_type (struct omp_region *region)
365 basic_block par_entry_bb, par_exit_bb;
366 basic_block ws_entry_bb, ws_exit_bb;
368 if (region == NULL || region->inner == NULL)
371 /* We only support parallel+for and parallel+sections. */
372 if (region->type != OMP_PARALLEL
373 || (region->inner->type != OMP_FOR
374 && region->inner->type != OMP_SECTIONS))
377 /* Check for perfect nesting PAR_ENTRY_BB -> WS_ENTRY_BB and
378 WS_EXIT_BB -> PAR_EXIT_BB. */
379 par_entry_bb = region->entry;
380 par_exit_bb = region->exit;
381 ws_entry_bb = region->inner->entry;
382 ws_exit_bb = region->inner->exit;
384 if (single_succ (par_entry_bb) == ws_entry_bb
385 && single_succ (ws_exit_bb) == par_exit_bb
386 && workshare_safe_to_combine_p (par_entry_bb, ws_entry_bb))
388 tree ws_stmt = last_stmt (region->inner->entry);
390 if (region->inner->type == OMP_FOR)
392 /* If this is a combined parallel loop, we need to determine
393 whether or not to use the combined library calls. There
394 are two cases where we do not apply the transformation:
395 static loops and any kind of ordered loop. In the first
396 case, we already open code the loop so there is no need
397 to do anything else. In the latter case, the combined
398 parallel loop call would still need extra synchronization
399 to implement ordered semantics, so there would not be any
400 gain in using the combined call. */
401 tree clauses = OMP_FOR_CLAUSES (ws_stmt);
402 tree c = find_omp_clause (clauses, OMP_CLAUSE_SCHEDULE);
404 || OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_STATIC
405 || find_omp_clause (clauses, OMP_CLAUSE_ORDERED))
407 region->is_combined_parallel = false;
408 region->inner->is_combined_parallel = false;
413 region->is_combined_parallel = true;
414 region->inner->is_combined_parallel = true;
415 region->ws_args = get_ws_args_for (ws_stmt);
420 /* Return true if EXPR is variable sized. */
423 is_variable_sized (tree expr)
425 return !TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
428 /* Return true if DECL is a reference type. */
431 is_reference (tree decl)
433 return lang_hooks.decls.omp_privatize_by_reference (decl);
436 /* Lookup variables in the decl or field splay trees. The "maybe" form
437 allows for the variable form to not have been entered, otherwise we
438 assert that the variable must have been entered. */
441 lookup_decl (tree var, omp_context *ctx)
444 n = splay_tree_lookup (ctx->cb.decl_map, (splay_tree_key) var);
445 return (tree) n->value;
449 maybe_lookup_decl (tree var, omp_context *ctx)
452 n = splay_tree_lookup (ctx->cb.decl_map, (splay_tree_key) var);
453 return n ? (tree) n->value : NULL_TREE;
457 lookup_field (tree var, omp_context *ctx)
460 n = splay_tree_lookup (ctx->field_map, (splay_tree_key) var);
461 return (tree) n->value;
465 maybe_lookup_field (tree var, omp_context *ctx)
468 n = splay_tree_lookup (ctx->field_map, (splay_tree_key) var);
469 return n ? (tree) n->value : NULL_TREE;
472 /* Return true if DECL should be copied by pointer. SHARED_P is true
473 if DECL is to be shared. */
476 use_pointer_for_field (tree decl, bool shared_p)
478 if (AGGREGATE_TYPE_P (TREE_TYPE (decl)))
481 /* We can only use copy-in/copy-out semantics for shared varibles
482 when we know the value is not accessible from an outer scope. */
485 /* ??? Trivially accessible from anywhere. But why would we even
486 be passing an address in this case? Should we simply assert
487 this to be false, or should we have a cleanup pass that removes
488 these from the list of mappings? */
489 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
492 /* For variables with DECL_HAS_VALUE_EXPR_P set, we cannot tell
493 without analyzing the expression whether or not its location
494 is accessible to anyone else. In the case of nested parallel
495 regions it certainly may be. */
496 if (TREE_CODE (decl) != RESULT_DECL && DECL_HAS_VALUE_EXPR_P (decl))
499 /* Do not use copy-in/copy-out for variables that have their
501 if (TREE_ADDRESSABLE (decl))
508 /* Construct a new automatic decl similar to VAR. */
511 omp_copy_decl_2 (tree var, tree name, tree type, omp_context *ctx)
513 tree copy = build_decl (VAR_DECL, name, type);
515 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
516 DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (var);
517 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
518 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
519 TREE_USED (copy) = 1;
520 DECL_CONTEXT (copy) = current_function_decl;
521 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
523 TREE_CHAIN (copy) = ctx->block_vars;
524 ctx->block_vars = copy;
530 omp_copy_decl_1 (tree var, omp_context *ctx)
532 return omp_copy_decl_2 (var, DECL_NAME (var), TREE_TYPE (var), ctx);
535 /* Build tree nodes to access the field for VAR on the receiver side. */
538 build_receiver_ref (tree var, bool by_ref, omp_context *ctx)
540 tree x, field = lookup_field (var, ctx);
542 /* If the receiver record type was remapped in the child function,
543 remap the field into the new record type. */
544 x = maybe_lookup_field (field, ctx);
548 x = build_fold_indirect_ref (ctx->receiver_decl);
549 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL);
551 x = build_fold_indirect_ref (x);
556 /* Build tree nodes to access VAR in the scope outer to CTX. In the case
557 of a parallel, this is a component reference; for workshare constructs
558 this is some variable. */
561 build_outer_var_ref (tree var, omp_context *ctx)
565 if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, ctx)))
567 else if (is_variable_sized (var))
569 x = TREE_OPERAND (DECL_VALUE_EXPR (var), 0);
570 x = build_outer_var_ref (x, ctx);
571 x = build_fold_indirect_ref (x);
573 else if (is_parallel_ctx (ctx))
575 bool by_ref = use_pointer_for_field (var, false);
576 x = build_receiver_ref (var, by_ref, ctx);
579 x = lookup_decl (var, ctx->outer);
583 if (is_reference (var))
584 x = build_fold_indirect_ref (x);
589 /* Build tree nodes to access the field for VAR on the sender side. */
592 build_sender_ref (tree var, omp_context *ctx)
594 tree field = lookup_field (var, ctx);
595 return build3 (COMPONENT_REF, TREE_TYPE (field),
596 ctx->sender_decl, field, NULL);
599 /* Add a new field for VAR inside the structure CTX->SENDER_DECL. */
602 install_var_field (tree var, bool by_ref, omp_context *ctx)
606 gcc_assert (!splay_tree_lookup (ctx->field_map, (splay_tree_key) var));
608 type = TREE_TYPE (var);
610 type = build_pointer_type (type);
612 field = build_decl (FIELD_DECL, DECL_NAME (var), type);
614 /* Remember what variable this field was created for. This does have a
615 side effect of making dwarf2out ignore this member, so for helpful
616 debugging we clear it later in delete_omp_context. */
617 DECL_ABSTRACT_ORIGIN (field) = var;
619 insert_field_into_struct (ctx->record_type, field);
621 splay_tree_insert (ctx->field_map, (splay_tree_key) var,
622 (splay_tree_value) field);
626 install_var_local (tree var, omp_context *ctx)
628 tree new_var = omp_copy_decl_1 (var, ctx);
629 insert_decl_map (&ctx->cb, var, new_var);
633 /* Adjust the replacement for DECL in CTX for the new context. This means
634 copying the DECL_VALUE_EXPR, and fixing up the type. */
637 fixup_remapped_decl (tree decl, omp_context *ctx, bool private_debug)
641 new_decl = lookup_decl (decl, ctx);
643 TREE_TYPE (new_decl) = remap_type (TREE_TYPE (decl), &ctx->cb);
645 if ((!TREE_CONSTANT (DECL_SIZE (new_decl)) || private_debug)
646 && DECL_HAS_VALUE_EXPR_P (decl))
648 tree ve = DECL_VALUE_EXPR (decl);
649 walk_tree (&ve, copy_body_r, &ctx->cb, NULL);
650 SET_DECL_VALUE_EXPR (new_decl, ve);
651 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
654 if (!TREE_CONSTANT (DECL_SIZE (new_decl)))
656 size = remap_decl (DECL_SIZE (decl), &ctx->cb);
657 if (size == error_mark_node)
658 size = TYPE_SIZE (TREE_TYPE (new_decl));
659 DECL_SIZE (new_decl) = size;
661 size = remap_decl (DECL_SIZE_UNIT (decl), &ctx->cb);
662 if (size == error_mark_node)
663 size = TYPE_SIZE_UNIT (TREE_TYPE (new_decl));
664 DECL_SIZE_UNIT (new_decl) = size;
668 /* The callback for remap_decl. Search all containing contexts for a
669 mapping of the variable; this avoids having to duplicate the splay
670 tree ahead of time. We know a mapping doesn't already exist in the
671 given context. Create new mappings to implement default semantics. */
674 omp_copy_decl (tree var, copy_body_data *cb)
676 omp_context *ctx = (omp_context *) cb;
679 if (TREE_CODE (var) == LABEL_DECL)
681 new_var = create_artificial_label ();
682 DECL_CONTEXT (new_var) = current_function_decl;
683 insert_decl_map (&ctx->cb, var, new_var);
687 while (!is_parallel_ctx (ctx))
692 new_var = maybe_lookup_decl (var, ctx);
697 if (is_global_var (var) || decl_function_context (var) != ctx->cb.src_fn)
700 return error_mark_node;
704 /* Return the parallel region associated with STMT. */
706 /* Debugging dumps for parallel regions. */
707 void dump_omp_region (FILE *, struct omp_region *, int);
708 void debug_omp_region (struct omp_region *);
709 void debug_all_omp_regions (void);
711 /* Dump the parallel region tree rooted at REGION. */
714 dump_omp_region (FILE *file, struct omp_region *region, int indent)
716 fprintf (file, "%*sbb %d: %s\n", indent, "", region->entry->index,
717 tree_code_name[region->type]);
720 dump_omp_region (file, region->inner, indent + 4);
724 fprintf (file, "%*sbb %d: OMP_CONTINUE\n", indent, "",
725 region->cont->index);
729 fprintf (file, "%*sbb %d: OMP_RETURN\n", indent, "",
730 region->exit->index);
732 fprintf (file, "%*s[no exit marker]\n", indent, "");
735 dump_omp_region (file, region->next, indent);
739 debug_omp_region (struct omp_region *region)
741 dump_omp_region (stderr, region, 0);
745 debug_all_omp_regions (void)
747 dump_omp_region (stderr, root_omp_region, 0);
751 /* Create a new parallel region starting at STMT inside region PARENT. */
754 new_omp_region (basic_block bb, enum tree_code type, struct omp_region *parent)
756 struct omp_region *region = xcalloc (1, sizeof (*region));
758 region->outer = parent;
764 /* This is a nested region. Add it to the list of inner
765 regions in PARENT. */
766 region->next = parent->inner;
767 parent->inner = region;
771 /* This is a toplevel region. Add it to the list of toplevel
772 regions in ROOT_OMP_REGION. */
773 region->next = root_omp_region;
774 root_omp_region = region;
780 /* Release the memory associated with the region tree rooted at REGION. */
783 free_omp_region_1 (struct omp_region *region)
785 struct omp_region *i, *n;
787 for (i = region->inner; i ; i = n)
790 free_omp_region_1 (i);
796 /* Release the memory for the entire omp region tree. */
799 free_omp_regions (void)
801 struct omp_region *r, *n;
802 for (r = root_omp_region; r ; r = n)
805 free_omp_region_1 (r);
807 root_omp_region = NULL;
811 /* Create a new context, with OUTER_CTX being the surrounding context. */
814 new_omp_context (tree stmt, omp_context *outer_ctx)
816 omp_context *ctx = XCNEW (omp_context);
818 splay_tree_insert (all_contexts, (splay_tree_key) stmt,
819 (splay_tree_value) ctx);
824 ctx->outer = outer_ctx;
825 ctx->cb = outer_ctx->cb;
826 ctx->cb.block = NULL;
827 ctx->depth = outer_ctx->depth + 1;
831 ctx->cb.src_fn = current_function_decl;
832 ctx->cb.dst_fn = current_function_decl;
833 ctx->cb.src_node = cgraph_node (current_function_decl);
834 ctx->cb.dst_node = ctx->cb.src_node;
835 ctx->cb.src_cfun = cfun;
836 ctx->cb.copy_decl = omp_copy_decl;
837 ctx->cb.eh_region = -1;
838 ctx->cb.transform_call_graph_edges = CB_CGE_MOVE;
842 ctx->cb.decl_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
847 /* Destroy a omp_context data structures. Called through the splay tree
848 value delete callback. */
851 delete_omp_context (splay_tree_value value)
853 omp_context *ctx = (omp_context *) value;
855 splay_tree_delete (ctx->cb.decl_map);
858 splay_tree_delete (ctx->field_map);
860 /* We hijacked DECL_ABSTRACT_ORIGIN earlier. We need to clear it before
861 it produces corrupt debug information. */
862 if (ctx->record_type)
865 for (t = TYPE_FIELDS (ctx->record_type); t ; t = TREE_CHAIN (t))
866 DECL_ABSTRACT_ORIGIN (t) = NULL;
872 /* Fix up RECEIVER_DECL with a type that has been remapped to the child
876 fixup_child_record_type (omp_context *ctx)
878 tree f, type = ctx->record_type;
880 /* ??? It isn't sufficient to just call remap_type here, because
881 variably_modified_type_p doesn't work the way we expect for
882 record types. Testing each field for whether it needs remapping
883 and creating a new record by hand works, however. */
884 for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
885 if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
889 tree name, new_fields = NULL;
891 type = lang_hooks.types.make_type (RECORD_TYPE);
892 name = DECL_NAME (TYPE_NAME (ctx->record_type));
893 name = build_decl (TYPE_DECL, name, type);
894 TYPE_NAME (type) = name;
896 for (f = TYPE_FIELDS (ctx->record_type); f ; f = TREE_CHAIN (f))
898 tree new_f = copy_node (f);
899 DECL_CONTEXT (new_f) = type;
900 TREE_TYPE (new_f) = remap_type (TREE_TYPE (f), &ctx->cb);
901 TREE_CHAIN (new_f) = new_fields;
904 /* Arrange to be able to look up the receiver field
905 given the sender field. */
906 splay_tree_insert (ctx->field_map, (splay_tree_key) f,
907 (splay_tree_value) new_f);
909 TYPE_FIELDS (type) = nreverse (new_fields);
913 TREE_TYPE (ctx->receiver_decl) = build_pointer_type (type);
916 /* Instantiate decls as necessary in CTX to satisfy the data sharing
917 specified by CLAUSES. */
920 scan_sharing_clauses (tree clauses, omp_context *ctx)
923 bool scan_array_reductions = false;
925 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
929 switch (OMP_CLAUSE_CODE (c))
931 case OMP_CLAUSE_PRIVATE:
932 decl = OMP_CLAUSE_DECL (c);
933 if (!is_variable_sized (decl))
934 install_var_local (decl, ctx);
937 case OMP_CLAUSE_SHARED:
938 gcc_assert (is_parallel_ctx (ctx));
939 decl = OMP_CLAUSE_DECL (c);
940 gcc_assert (!is_variable_sized (decl));
941 by_ref = use_pointer_for_field (decl, true);
942 /* Global variables don't need to be copied,
943 the receiver side will use them directly. */
944 if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
946 if (! TREE_READONLY (decl)
947 || TREE_ADDRESSABLE (decl)
949 || is_reference (decl))
951 install_var_field (decl, by_ref, ctx);
952 install_var_local (decl, ctx);
955 /* We don't need to copy const scalar vars back. */
956 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_FIRSTPRIVATE);
959 case OMP_CLAUSE_LASTPRIVATE:
960 /* Let the corresponding firstprivate clause create
962 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
966 case OMP_CLAUSE_FIRSTPRIVATE:
967 case OMP_CLAUSE_REDUCTION:
968 decl = OMP_CLAUSE_DECL (c);
970 if (is_variable_sized (decl))
972 else if (is_parallel_ctx (ctx)
973 && ! is_global_var (maybe_lookup_decl_in_outer_ctx (decl,
976 by_ref = use_pointer_for_field (decl, false);
977 install_var_field (decl, by_ref, ctx);
979 install_var_local (decl, ctx);
982 case OMP_CLAUSE_COPYPRIVATE:
984 scan_omp (&OMP_CLAUSE_DECL (c), ctx->outer);
987 case OMP_CLAUSE_COPYIN:
988 decl = OMP_CLAUSE_DECL (c);
989 by_ref = use_pointer_for_field (decl, false);
990 install_var_field (decl, by_ref, ctx);
993 case OMP_CLAUSE_DEFAULT:
994 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
998 case OMP_CLAUSE_NUM_THREADS:
999 case OMP_CLAUSE_SCHEDULE:
1001 scan_omp (&OMP_CLAUSE_OPERAND (c, 0), ctx->outer);
1004 case OMP_CLAUSE_NOWAIT:
1005 case OMP_CLAUSE_ORDERED:
1013 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1015 switch (OMP_CLAUSE_CODE (c))
1017 case OMP_CLAUSE_LASTPRIVATE:
1018 /* Let the corresponding firstprivate clause create
1020 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1024 case OMP_CLAUSE_PRIVATE:
1025 case OMP_CLAUSE_FIRSTPRIVATE:
1026 case OMP_CLAUSE_REDUCTION:
1027 decl = OMP_CLAUSE_DECL (c);
1028 if (is_variable_sized (decl))
1029 install_var_local (decl, ctx);
1030 fixup_remapped_decl (decl, ctx,
1031 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
1032 && OMP_CLAUSE_PRIVATE_DEBUG (c));
1033 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1034 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1035 scan_array_reductions = true;
1038 case OMP_CLAUSE_SHARED:
1039 decl = OMP_CLAUSE_DECL (c);
1040 if (! is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
1041 fixup_remapped_decl (decl, ctx, false);
1044 case OMP_CLAUSE_COPYPRIVATE:
1045 case OMP_CLAUSE_COPYIN:
1046 case OMP_CLAUSE_DEFAULT:
1048 case OMP_CLAUSE_NUM_THREADS:
1049 case OMP_CLAUSE_SCHEDULE:
1050 case OMP_CLAUSE_NOWAIT:
1051 case OMP_CLAUSE_ORDERED:
1059 if (scan_array_reductions)
1060 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1061 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1062 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1064 scan_omp (&OMP_CLAUSE_REDUCTION_INIT (c), ctx);
1065 scan_omp (&OMP_CLAUSE_REDUCTION_MERGE (c), ctx);
1069 /* Create a new name for omp child function. Returns an identifier. */
1071 static GTY(()) unsigned int tmp_ompfn_id_num;
1074 create_omp_child_function_name (void)
1076 tree name = DECL_ASSEMBLER_NAME (current_function_decl);
1077 size_t len = IDENTIFIER_LENGTH (name);
1078 char *tmp_name, *prefix;
1080 prefix = alloca (len + sizeof ("_omp_fn"));
1081 memcpy (prefix, IDENTIFIER_POINTER (name), len);
1082 strcpy (prefix + len, "_omp_fn");
1083 #ifndef NO_DOT_IN_LABEL
1085 #elif !defined NO_DOLLAR_IN_LABEL
1088 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, tmp_ompfn_id_num++);
1089 return get_identifier (tmp_name);
1092 /* Build a decl for the omp child function. It'll not contain a body
1093 yet, just the bare decl. */
1096 create_omp_child_function (omp_context *ctx)
1098 tree decl, type, name, t;
1100 name = create_omp_child_function_name ();
1101 type = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
1103 decl = build_decl (FUNCTION_DECL, name, type);
1104 decl = lang_hooks.decls.pushdecl (decl);
1106 ctx->cb.dst_fn = decl;
1108 TREE_STATIC (decl) = 1;
1109 TREE_USED (decl) = 1;
1110 DECL_ARTIFICIAL (decl) = 1;
1111 DECL_IGNORED_P (decl) = 0;
1112 TREE_PUBLIC (decl) = 0;
1113 DECL_UNINLINABLE (decl) = 1;
1114 DECL_EXTERNAL (decl) = 0;
1115 DECL_CONTEXT (decl) = NULL_TREE;
1116 DECL_INITIAL (decl) = make_node (BLOCK);
1118 t = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1119 DECL_ARTIFICIAL (t) = 1;
1120 DECL_IGNORED_P (t) = 1;
1121 DECL_RESULT (decl) = t;
1123 t = build_decl (PARM_DECL, get_identifier (".omp_data_i"), ptr_type_node);
1124 DECL_ARTIFICIAL (t) = 1;
1125 DECL_ARG_TYPE (t) = ptr_type_node;
1126 DECL_CONTEXT (t) = current_function_decl;
1128 DECL_ARGUMENTS (decl) = t;
1129 ctx->receiver_decl = t;
1131 /* Allocate memory for the function structure. The call to
1132 allocate_struct_function clobbers CFUN, so we need to restore
1134 allocate_struct_function (decl);
1135 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (ctx->stmt);
1136 cfun->function_end_locus = EXPR_LOCATION (ctx->stmt);
1137 cfun = ctx->cb.src_cfun;
1141 /* Scan an OpenMP parallel directive. */
1144 scan_omp_parallel (tree *stmt_p, omp_context *outer_ctx)
1149 /* Ignore parallel directives with empty bodies, unless there
1150 are copyin clauses. */
1152 && empty_body_p (OMP_PARALLEL_BODY (*stmt_p))
1153 && find_omp_clause (OMP_CLAUSES (*stmt_p), OMP_CLAUSE_COPYIN) == NULL)
1155 *stmt_p = build_empty_stmt ();
1159 ctx = new_omp_context (*stmt_p, outer_ctx);
1160 if (parallel_nesting_level > 1)
1161 ctx->is_nested = true;
1162 ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1163 ctx->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
1164 ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
1165 name = create_tmp_var_name (".omp_data_s");
1166 name = build_decl (TYPE_DECL, name, ctx->record_type);
1167 TYPE_NAME (ctx->record_type) = name;
1168 create_omp_child_function (ctx);
1169 OMP_PARALLEL_FN (*stmt_p) = ctx->cb.dst_fn;
1171 scan_sharing_clauses (OMP_PARALLEL_CLAUSES (*stmt_p), ctx);
1172 scan_omp (&OMP_PARALLEL_BODY (*stmt_p), ctx);
1174 if (TYPE_FIELDS (ctx->record_type) == NULL)
1175 ctx->record_type = ctx->receiver_decl = NULL;
1178 layout_type (ctx->record_type);
1179 fixup_child_record_type (ctx);
1184 /* Scan an OpenMP loop directive. */
1187 scan_omp_for (tree *stmt_p, omp_context *outer_ctx)
1193 ctx = new_omp_context (stmt, outer_ctx);
1195 scan_sharing_clauses (OMP_FOR_CLAUSES (stmt), ctx);
1197 scan_omp (&OMP_FOR_PRE_BODY (stmt), ctx);
1198 scan_omp (&OMP_FOR_INIT (stmt), ctx);
1199 scan_omp (&OMP_FOR_COND (stmt), ctx);
1200 scan_omp (&OMP_FOR_INCR (stmt), ctx);
1201 scan_omp (&OMP_FOR_BODY (stmt), ctx);
1204 /* Scan an OpenMP sections directive. */
1207 scan_omp_sections (tree *stmt_p, omp_context *outer_ctx)
1213 ctx = new_omp_context (stmt, outer_ctx);
1214 scan_sharing_clauses (OMP_SECTIONS_CLAUSES (stmt), ctx);
1215 scan_omp (&OMP_SECTIONS_BODY (stmt), ctx);
1218 /* Scan an OpenMP single directive. */
1221 scan_omp_single (tree *stmt_p, omp_context *outer_ctx)
1223 tree stmt = *stmt_p;
1227 ctx = new_omp_context (stmt, outer_ctx);
1228 ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1229 ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
1230 name = create_tmp_var_name (".omp_copy_s");
1231 name = build_decl (TYPE_DECL, name, ctx->record_type);
1232 TYPE_NAME (ctx->record_type) = name;
1234 scan_sharing_clauses (OMP_SINGLE_CLAUSES (stmt), ctx);
1235 scan_omp (&OMP_SINGLE_BODY (stmt), ctx);
1237 if (TYPE_FIELDS (ctx->record_type) == NULL)
1238 ctx->record_type = NULL;
1240 layout_type (ctx->record_type);
1244 /* Callback for walk_stmts used to scan for OpenMP directives at TP. */
1247 scan_omp_1 (tree *tp, int *walk_subtrees, void *data)
1249 struct walk_stmt_info *wi = data;
1250 omp_context *ctx = wi->info;
1253 if (EXPR_HAS_LOCATION (t))
1254 input_location = EXPR_LOCATION (t);
1257 switch (TREE_CODE (t))
1260 parallel_nesting_level++;
1261 scan_omp_parallel (tp, ctx);
1262 parallel_nesting_level--;
1266 scan_omp_for (tp, ctx);
1270 scan_omp_sections (tp, ctx);
1274 scan_omp_single (tp, ctx);
1281 ctx = new_omp_context (*tp, ctx);
1282 scan_omp (&OMP_BODY (*tp), ctx);
1290 for (var = BIND_EXPR_VARS (t); var ; var = TREE_CHAIN (var))
1291 insert_decl_map (&ctx->cb, var, var);
1300 *tp = remap_decl (t, &ctx->cb);
1304 if (ctx && TYPE_P (t))
1305 *tp = remap_type (t, &ctx->cb);
1306 else if (!DECL_P (t))
1315 /* Scan all the statements starting at STMT_P. CTX contains context
1316 information about the OpenMP directives and clauses found during
1320 scan_omp (tree *stmt_p, omp_context *ctx)
1322 location_t saved_location;
1323 struct walk_stmt_info wi;
1325 memset (&wi, 0, sizeof (wi));
1326 wi.callback = scan_omp_1;
1328 wi.want_bind_expr = (ctx != NULL);
1329 wi.want_locations = true;
1331 saved_location = input_location;
1332 walk_stmts (&wi, stmt_p);
1333 input_location = saved_location;
1336 /* Re-gimplification and code generation routines. */
1338 /* Build a call to GOMP_barrier. */
1341 build_omp_barrier (tree *stmt_list)
1345 t = built_in_decls[BUILT_IN_GOMP_BARRIER];
1346 t = build_function_call_expr (t, NULL);
1347 gimplify_and_add (t, stmt_list);
1350 /* If a context was created for STMT when it was scanned, return it. */
1352 static omp_context *
1353 maybe_lookup_ctx (tree stmt)
1356 n = splay_tree_lookup (all_contexts, (splay_tree_key) stmt);
1357 return n ? (omp_context *) n->value : NULL;
1361 /* Find the mapping for DECL in CTX or the immediately enclosing
1362 context that has a mapping for DECL.
1364 If CTX is a nested parallel directive, we may have to use the decl
1365 mappings created in CTX's parent context. Suppose that we have the
1366 following parallel nesting (variable UIDs showed for clarity):
1369 #omp parallel shared(iD.1562) -> outer parallel
1370 iD.1562 = iD.1562 + 1;
1372 #omp parallel shared (iD.1562) -> inner parallel
1373 iD.1562 = iD.1562 - 1;
1375 Each parallel structure will create a distinct .omp_data_s structure
1376 for copying iD.1562 in/out of the directive:
1378 outer parallel .omp_data_s.1.i -> iD.1562
1379 inner parallel .omp_data_s.2.i -> iD.1562
1381 A shared variable mapping will produce a copy-out operation before
1382 the parallel directive and a copy-in operation after it. So, in
1383 this case we would have:
1386 .omp_data_o.1.i = iD.1562;
1387 #omp parallel shared(iD.1562) -> outer parallel
1388 .omp_data_i.1 = &.omp_data_o.1
1389 .omp_data_i.1->i = .omp_data_i.1->i + 1;
1391 .omp_data_o.2.i = iD.1562; -> **
1392 #omp parallel shared(iD.1562) -> inner parallel
1393 .omp_data_i.2 = &.omp_data_o.2
1394 .omp_data_i.2->i = .omp_data_i.2->i - 1;
1397 ** This is a problem. The symbol iD.1562 cannot be referenced
1398 inside the body of the outer parallel region. But since we are
1399 emitting this copy operation while expanding the inner parallel
1400 directive, we need to access the CTX structure of the outer
1401 parallel directive to get the correct mapping:
1403 .omp_data_o.2.i = .omp_data_i.1->i
1405 Since there may be other workshare or parallel directives enclosing
1406 the parallel directive, it may be necessary to walk up the context
1407 parent chain. This is not a problem in general because nested
1408 parallelism happens only rarely. */
1411 lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
1416 gcc_assert (ctx->is_nested);
1418 for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
1419 t = maybe_lookup_decl (decl, up);
1427 /* Similar to lookup_decl_in_outer_ctx, but return DECL if not found
1428 in outer contexts. */
1431 maybe_lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
1437 for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
1438 t = maybe_lookup_decl (decl, up);
1440 return t ? t : decl;
1444 /* Construct the initialization value for reduction CLAUSE. */
1447 omp_reduction_init (tree clause, tree type)
1449 switch (OMP_CLAUSE_REDUCTION_CODE (clause))
1456 case TRUTH_ORIF_EXPR:
1457 case TRUTH_XOR_EXPR:
1459 return fold_convert (type, integer_zero_node);
1462 case TRUTH_AND_EXPR:
1463 case TRUTH_ANDIF_EXPR:
1465 return fold_convert (type, integer_one_node);
1468 return fold_convert (type, integer_minus_one_node);
1471 if (SCALAR_FLOAT_TYPE_P (type))
1473 REAL_VALUE_TYPE max, min;
1474 if (HONOR_INFINITIES (TYPE_MODE (type)))
1477 real_arithmetic (&min, NEGATE_EXPR, &max, NULL);
1480 real_maxval (&min, 1, TYPE_MODE (type));
1481 return build_real (type, min);
1485 gcc_assert (INTEGRAL_TYPE_P (type));
1486 return TYPE_MIN_VALUE (type);
1490 if (SCALAR_FLOAT_TYPE_P (type))
1492 REAL_VALUE_TYPE max;
1493 if (HONOR_INFINITIES (TYPE_MODE (type)))
1496 real_maxval (&max, 0, TYPE_MODE (type));
1497 return build_real (type, max);
1501 gcc_assert (INTEGRAL_TYPE_P (type));
1502 return TYPE_MAX_VALUE (type);
1510 /* Generate code to implement the input clauses, FIRSTPRIVATE and COPYIN,
1511 from the receiver (aka child) side and initializers for REFERENCE_TYPE
1512 private variables. Initialization statements go in ILIST, while calls
1513 to destructors go in DLIST. */
1516 lower_rec_input_clauses (tree clauses, tree *ilist, tree *dlist,
1519 tree_stmt_iterator diter;
1520 tree c, dtor, copyin_seq, x, args, ptr;
1521 bool copyin_by_ref = false;
1522 bool lastprivate_firstprivate = false;
1525 *dlist = alloc_stmt_list ();
1526 diter = tsi_start (*dlist);
1529 /* Do all the fixed sized types in the first pass, and the variable sized
1530 types in the second pass. This makes sure that the scalar arguments to
1531 the variable sized types are processed before we use them in the
1532 variable sized operations. */
1533 for (pass = 0; pass < 2; ++pass)
1535 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
1537 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
1543 case OMP_CLAUSE_PRIVATE:
1544 if (OMP_CLAUSE_PRIVATE_DEBUG (c))
1547 case OMP_CLAUSE_SHARED:
1548 if (maybe_lookup_decl (OMP_CLAUSE_DECL (c), ctx) == NULL)
1550 gcc_assert (is_global_var (OMP_CLAUSE_DECL (c)));
1553 case OMP_CLAUSE_FIRSTPRIVATE:
1554 case OMP_CLAUSE_COPYIN:
1555 case OMP_CLAUSE_REDUCTION:
1557 case OMP_CLAUSE_LASTPRIVATE:
1558 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1560 lastprivate_firstprivate = true;
1569 new_var = var = OMP_CLAUSE_DECL (c);
1570 if (c_kind != OMP_CLAUSE_COPYIN)
1571 new_var = lookup_decl (var, ctx);
1573 if (c_kind == OMP_CLAUSE_SHARED || c_kind == OMP_CLAUSE_COPYIN)
1578 else if (is_variable_sized (var))
1580 /* For variable sized types, we need to allocate the
1581 actual storage here. Call alloca and store the
1582 result in the pointer decl that we created elsewhere. */
1586 ptr = DECL_VALUE_EXPR (new_var);
1587 gcc_assert (TREE_CODE (ptr) == INDIRECT_REF);
1588 ptr = TREE_OPERAND (ptr, 0);
1589 gcc_assert (DECL_P (ptr));
1591 x = TYPE_SIZE_UNIT (TREE_TYPE (new_var));
1592 args = tree_cons (NULL, x, NULL);
1593 x = built_in_decls[BUILT_IN_ALLOCA];
1594 x = build_function_call_expr (x, args);
1595 x = fold_convert (TREE_TYPE (ptr), x);
1596 x = build2 (MODIFY_EXPR, void_type_node, ptr, x);
1597 gimplify_and_add (x, ilist);
1599 else if (is_reference (var))
1601 /* For references that are being privatized for Fortran,
1602 allocate new backing storage for the new pointer
1603 variable. This allows us to avoid changing all the
1604 code that expects a pointer to something that expects
1605 a direct variable. Note that this doesn't apply to
1606 C++, since reference types are disallowed in data
1607 sharing clauses there, except for NRV optimized
1612 x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
1613 if (TREE_CONSTANT (x))
1615 const char *name = NULL;
1616 if (DECL_NAME (var))
1617 name = IDENTIFIER_POINTER (DECL_NAME (new_var));
1619 x = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var)),
1621 gimple_add_tmp_var (x);
1622 x = build_fold_addr_expr_with_type (x, TREE_TYPE (new_var));
1626 args = tree_cons (NULL, x, NULL);
1627 x = built_in_decls[BUILT_IN_ALLOCA];
1628 x = build_function_call_expr (x, args);
1629 x = fold_convert (TREE_TYPE (new_var), x);
1632 x = build2 (MODIFY_EXPR, void_type_node, new_var, x);
1633 gimplify_and_add (x, ilist);
1635 new_var = build_fold_indirect_ref (new_var);
1637 else if (c_kind == OMP_CLAUSE_REDUCTION
1638 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1646 switch (OMP_CLAUSE_CODE (c))
1648 case OMP_CLAUSE_SHARED:
1649 /* Shared global vars are just accessed directly. */
1650 if (is_global_var (new_var))
1652 /* Set up the DECL_VALUE_EXPR for shared variables now. This
1653 needs to be delayed until after fixup_child_record_type so
1654 that we get the correct type during the dereference. */
1655 by_ref = use_pointer_for_field (var, true);
1656 x = build_receiver_ref (var, by_ref, ctx);
1657 SET_DECL_VALUE_EXPR (new_var, x);
1658 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
1660 /* ??? If VAR is not passed by reference, and the variable
1661 hasn't been initialized yet, then we'll get a warning for
1662 the store into the omp_data_s structure. Ideally, we'd be
1663 able to notice this and not store anything at all, but
1664 we're generating code too early. Suppress the warning. */
1666 TREE_NO_WARNING (var) = 1;
1669 case OMP_CLAUSE_LASTPRIVATE:
1670 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1674 case OMP_CLAUSE_PRIVATE:
1675 x = lang_hooks.decls.omp_clause_default_ctor (c, new_var);
1677 gimplify_and_add (x, ilist);
1681 x = lang_hooks.decls.omp_clause_dtor (c, new_var);
1685 gimplify_stmt (&dtor);
1686 tsi_link_before (&diter, dtor, TSI_SAME_STMT);
1690 case OMP_CLAUSE_FIRSTPRIVATE:
1691 x = build_outer_var_ref (var, ctx);
1692 x = lang_hooks.decls.omp_clause_copy_ctor (c, new_var, x);
1693 gimplify_and_add (x, ilist);
1697 case OMP_CLAUSE_COPYIN:
1698 by_ref = use_pointer_for_field (var, false);
1699 x = build_receiver_ref (var, by_ref, ctx);
1700 x = lang_hooks.decls.omp_clause_assign_op (c, new_var, x);
1701 append_to_statement_list (x, ©in_seq);
1702 copyin_by_ref |= by_ref;
1705 case OMP_CLAUSE_REDUCTION:
1706 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1708 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c), ilist);
1709 OMP_CLAUSE_REDUCTION_INIT (c) = NULL;
1713 x = omp_reduction_init (c, TREE_TYPE (new_var));
1714 gcc_assert (TREE_CODE (TREE_TYPE (new_var)) != ARRAY_TYPE);
1715 x = build2 (MODIFY_EXPR, void_type_node, new_var, x);
1716 gimplify_and_add (x, ilist);
1726 /* The copyin sequence is not to be executed by the main thread, since
1727 that would result in self-copies. Perhaps not visible to scalars,
1728 but it certainly is to C++ operator=. */
1731 x = built_in_decls[BUILT_IN_OMP_GET_THREAD_NUM];
1732 x = build_function_call_expr (x, NULL);
1733 x = build2 (NE_EXPR, boolean_type_node, x,
1734 build_int_cst (TREE_TYPE (x), 0));
1735 x = build3 (COND_EXPR, void_type_node, x, copyin_seq, NULL);
1736 gimplify_and_add (x, ilist);
1739 /* If any copyin variable is passed by reference, we must ensure the
1740 master thread doesn't modify it before it is copied over in all
1741 threads. Similarly for variables in both firstprivate and
1742 lastprivate clauses we need to ensure the lastprivate copying
1743 happens after firstprivate copying in all threads. */
1744 if (copyin_by_ref || lastprivate_firstprivate)
1745 build_omp_barrier (ilist);
1749 /* Generate code to implement the LASTPRIVATE clauses. This is used for
1750 both parallel and workshare constructs. PREDICATE may be NULL if it's
1754 lower_lastprivate_clauses (tree clauses, tree predicate, tree *stmt_list,
1757 tree sub_list, x, c;
1759 /* Early exit if there are no lastprivate clauses. */
1760 clauses = find_omp_clause (clauses, OMP_CLAUSE_LASTPRIVATE);
1761 if (clauses == NULL)
1763 /* If this was a workshare clause, see if it had been combined
1764 with its parallel. In that case, look for the clauses on the
1765 parallel statement itself. */
1766 if (is_parallel_ctx (ctx))
1770 if (ctx == NULL || !is_parallel_ctx (ctx))
1773 clauses = find_omp_clause (OMP_PARALLEL_CLAUSES (ctx->stmt),
1774 OMP_CLAUSE_LASTPRIVATE);
1775 if (clauses == NULL)
1779 sub_list = alloc_stmt_list ();
1781 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
1785 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LASTPRIVATE)
1788 var = OMP_CLAUSE_DECL (c);
1789 new_var = lookup_decl (var, ctx);
1791 x = build_outer_var_ref (var, ctx);
1792 if (is_reference (var))
1793 new_var = build_fold_indirect_ref (new_var);
1794 x = lang_hooks.decls.omp_clause_assign_op (c, x, new_var);
1795 append_to_statement_list (x, &sub_list);
1799 x = build3 (COND_EXPR, void_type_node, predicate, sub_list, NULL);
1803 gimplify_and_add (x, stmt_list);
1807 /* Generate code to implement the REDUCTION clauses. */
1810 lower_reduction_clauses (tree clauses, tree *stmt_list, omp_context *ctx)
1812 tree sub_list = NULL, x, c;
1815 /* First see if there is exactly one reduction clause. Use OMP_ATOMIC
1816 update in that case, otherwise use a lock. */
1817 for (c = clauses; c && count < 2; c = OMP_CLAUSE_CHAIN (c))
1818 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
1820 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1822 /* Never use OMP_ATOMIC for array reductions. */
1832 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
1834 tree var, ref, new_var;
1835 enum tree_code code;
1837 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
1840 var = OMP_CLAUSE_DECL (c);
1841 new_var = lookup_decl (var, ctx);
1842 if (is_reference (var))
1843 new_var = build_fold_indirect_ref (new_var);
1844 ref = build_outer_var_ref (var, ctx);
1845 code = OMP_CLAUSE_REDUCTION_CODE (c);
1847 /* reduction(-:var) sums up the partial results, so it acts
1848 identically to reduction(+:var). */
1849 if (code == MINUS_EXPR)
1854 tree addr = build_fold_addr_expr (ref);
1856 addr = save_expr (addr);
1857 ref = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (addr)), addr);
1858 x = fold_build2 (code, TREE_TYPE (ref), ref, new_var);
1859 x = build2 (OMP_ATOMIC, void_type_node, addr, x);
1860 gimplify_and_add (x, stmt_list);
1864 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1866 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
1868 if (is_reference (var))
1869 ref = build_fold_addr_expr (ref);
1870 SET_DECL_VALUE_EXPR (placeholder, ref);
1871 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
1872 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c), &sub_list);
1873 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL;
1874 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
1878 x = build2 (code, TREE_TYPE (ref), ref, new_var);
1879 ref = build_outer_var_ref (var, ctx);
1880 x = build2 (MODIFY_EXPR, void_type_node, ref, x);
1881 append_to_statement_list (x, &sub_list);
1885 x = built_in_decls[BUILT_IN_GOMP_ATOMIC_START];
1886 x = build_function_call_expr (x, NULL);
1887 gimplify_and_add (x, stmt_list);
1889 gimplify_and_add (sub_list, stmt_list);
1891 x = built_in_decls[BUILT_IN_GOMP_ATOMIC_END];
1892 x = build_function_call_expr (x, NULL);
1893 gimplify_and_add (x, stmt_list);
1897 /* Generate code to implement the COPYPRIVATE clauses. */
1900 lower_copyprivate_clauses (tree clauses, tree *slist, tree *rlist,
1905 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
1910 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYPRIVATE)
1913 var = OMP_CLAUSE_DECL (c);
1914 by_ref = use_pointer_for_field (var, false);
1916 ref = build_sender_ref (var, ctx);
1917 x = (ctx->is_nested) ? lookup_decl_in_outer_ctx (var, ctx) : var;
1918 x = by_ref ? build_fold_addr_expr (x) : x;
1919 x = build2 (MODIFY_EXPR, void_type_node, ref, x);
1920 gimplify_and_add (x, slist);
1922 ref = build_receiver_ref (var, by_ref, ctx);
1923 if (is_reference (var))
1925 ref = build_fold_indirect_ref (ref);
1926 var = build_fold_indirect_ref (var);
1928 x = lang_hooks.decls.omp_clause_assign_op (c, var, ref);
1929 gimplify_and_add (x, rlist);
1934 /* Generate code to implement the clauses, FIRSTPRIVATE, COPYIN, LASTPRIVATE,
1935 and REDUCTION from the sender (aka parent) side. */
1938 lower_send_clauses (tree clauses, tree *ilist, tree *olist, omp_context *ctx)
1942 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
1944 tree val, ref, x, var;
1945 bool by_ref, do_in = false, do_out = false;
1947 switch (OMP_CLAUSE_CODE (c))
1949 case OMP_CLAUSE_FIRSTPRIVATE:
1950 case OMP_CLAUSE_COPYIN:
1951 case OMP_CLAUSE_LASTPRIVATE:
1952 case OMP_CLAUSE_REDUCTION:
1958 var = val = OMP_CLAUSE_DECL (c);
1960 var = lookup_decl_in_outer_ctx (val, ctx);
1962 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYIN
1963 && is_global_var (var))
1965 if (is_variable_sized (val))
1967 by_ref = use_pointer_for_field (val, false);
1969 switch (OMP_CLAUSE_CODE (c))
1971 case OMP_CLAUSE_FIRSTPRIVATE:
1972 case OMP_CLAUSE_COPYIN:
1976 case OMP_CLAUSE_LASTPRIVATE:
1977 if (by_ref || is_reference (val))
1979 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1987 case OMP_CLAUSE_REDUCTION:
1989 do_out = !(by_ref || is_reference (val));
1998 ref = build_sender_ref (val, ctx);
1999 x = by_ref ? build_fold_addr_expr (var) : var;
2000 x = build2 (MODIFY_EXPR, void_type_node, ref, x);
2001 gimplify_and_add (x, ilist);
2006 ref = build_sender_ref (val, ctx);
2007 x = build2 (MODIFY_EXPR, void_type_node, var, ref);
2008 gimplify_and_add (x, olist);
2013 /* Generate code to implement SHARED from the sender (aka parent) side.
2014 This is trickier, since OMP_PARALLEL_CLAUSES doesn't list things that
2015 got automatically shared. */
2018 lower_send_shared_vars (tree *ilist, tree *olist, omp_context *ctx)
2020 tree var, ovar, nvar, f, x;
2022 if (ctx->record_type == NULL)
2025 for (f = TYPE_FIELDS (ctx->record_type); f ; f = TREE_CHAIN (f))
2027 ovar = DECL_ABSTRACT_ORIGIN (f);
2028 nvar = maybe_lookup_decl (ovar, ctx);
2029 if (!nvar || !DECL_HAS_VALUE_EXPR_P (nvar))
2034 /* If CTX is a nested parallel directive. Find the immediately
2035 enclosing parallel or workshare construct that contains a
2036 mapping for OVAR. */
2038 var = lookup_decl_in_outer_ctx (ovar, ctx);
2040 if (use_pointer_for_field (ovar, true))
2042 x = build_sender_ref (ovar, ctx);
2043 var = build_fold_addr_expr (var);
2044 x = build2 (MODIFY_EXPR, void_type_node, x, var);
2045 gimplify_and_add (x, ilist);
2049 x = build_sender_ref (ovar, ctx);
2050 x = build2 (MODIFY_EXPR, void_type_node, x, var);
2051 gimplify_and_add (x, ilist);
2053 x = build_sender_ref (ovar, ctx);
2054 x = build2 (MODIFY_EXPR, void_type_node, var, x);
2055 gimplify_and_add (x, olist);
2060 /* Build the function calls to GOMP_parallel_start etc to actually
2061 generate the parallel operation. REGION is the parallel region
2062 being expanded. BB is the block where to insert the code. WS_ARGS
2063 will be set if this is a call to a combined parallel+workshare
2064 construct, it contains the list of additional arguments needed by
2065 the workshare construct. */
2068 expand_parallel_call (struct omp_region *region, basic_block bb,
2069 tree entry_stmt, tree ws_args)
2071 tree t, args, val, cond, c, list, clauses;
2072 block_stmt_iterator si;
2075 clauses = OMP_PARALLEL_CLAUSES (entry_stmt);
2076 push_gimplify_context ();
2078 /* Determine what flavor of GOMP_parallel_start we will be
2080 start_ix = BUILT_IN_GOMP_PARALLEL_START;
2081 if (is_combined_parallel (region))
2083 switch (region->inner->type)
2086 start_ix = BUILT_IN_GOMP_PARALLEL_LOOP_STATIC_START
2087 + region->inner->sched_kind;
2090 start_ix = BUILT_IN_GOMP_PARALLEL_SECTIONS_START;
2097 /* By default, the value of NUM_THREADS is zero (selected at run time)
2098 and there is no conditional. */
2100 val = build_int_cst (unsigned_type_node, 0);
2102 c = find_omp_clause (clauses, OMP_CLAUSE_IF);
2104 cond = OMP_CLAUSE_IF_EXPR (c);
2106 c = find_omp_clause (clauses, OMP_CLAUSE_NUM_THREADS);
2108 val = OMP_CLAUSE_NUM_THREADS_EXPR (c);
2110 /* Ensure 'val' is of the correct type. */
2111 val = fold_convert (unsigned_type_node, val);
2113 /* If we found the clause 'if (cond)', build either
2114 (cond != 0) or (cond ? val : 1u). */
2117 block_stmt_iterator si;
2119 cond = gimple_boolify (cond);
2121 if (integer_zerop (val))
2122 val = build2 (EQ_EXPR, unsigned_type_node, cond,
2123 build_int_cst (TREE_TYPE (cond), 0));
2126 basic_block cond_bb, then_bb, else_bb;
2128 tree t, then_lab, else_lab, tmp;
2130 tmp = create_tmp_var (TREE_TYPE (val), NULL);
2131 e = split_block (bb, NULL);
2136 then_bb = create_empty_bb (cond_bb);
2137 else_bb = create_empty_bb (then_bb);
2138 then_lab = create_artificial_label ();
2139 else_lab = create_artificial_label ();
2141 t = build3 (COND_EXPR, void_type_node,
2143 build_and_jump (&then_lab),
2144 build_and_jump (&else_lab));
2146 si = bsi_start (cond_bb);
2147 bsi_insert_after (&si, t, BSI_CONTINUE_LINKING);
2149 si = bsi_start (then_bb);
2150 t = build1 (LABEL_EXPR, void_type_node, then_lab);
2151 bsi_insert_after (&si, t, BSI_CONTINUE_LINKING);
2152 t = build2 (MODIFY_EXPR, void_type_node, tmp, val);
2153 bsi_insert_after (&si, t, BSI_CONTINUE_LINKING);
2155 si = bsi_start (else_bb);
2156 t = build1 (LABEL_EXPR, void_type_node, else_lab);
2157 bsi_insert_after (&si, t, BSI_CONTINUE_LINKING);
2158 t = build2 (MODIFY_EXPR, void_type_node, tmp,
2159 build_int_cst (unsigned_type_node, 1));
2160 bsi_insert_after (&si, t, BSI_CONTINUE_LINKING);
2162 make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
2163 make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
2164 make_edge (then_bb, bb, EDGE_FALLTHRU);
2165 make_edge (else_bb, bb, EDGE_FALLTHRU);
2171 val = get_formal_tmp_var (val, &list);
2172 si = bsi_start (bb);
2173 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2177 args = tree_cons (NULL, val, NULL);
2178 t = OMP_PARALLEL_DATA_ARG (entry_stmt);
2180 t = null_pointer_node;
2182 t = build_fold_addr_expr (t);
2183 args = tree_cons (NULL, t, args);
2184 t = build_fold_addr_expr (OMP_PARALLEL_FN (entry_stmt));
2185 args = tree_cons (NULL, t, args);
2188 args = chainon (args, ws_args);
2190 t = built_in_decls[start_ix];
2191 t = build_function_call_expr (t, args);
2192 gimplify_and_add (t, &list);
2194 t = OMP_PARALLEL_DATA_ARG (entry_stmt);
2196 t = null_pointer_node;
2198 t = build_fold_addr_expr (t);
2199 args = tree_cons (NULL, t, NULL);
2200 t = build_function_call_expr (OMP_PARALLEL_FN (entry_stmt), args);
2201 gimplify_and_add (t, &list);
2203 t = built_in_decls[BUILT_IN_GOMP_PARALLEL_END];
2204 t = build_function_call_expr (t, NULL);
2205 gimplify_and_add (t, &list);
2208 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2210 pop_gimplify_context (NULL_TREE);
2214 /* If exceptions are enabled, wrap *STMT_P in a MUST_NOT_THROW catch
2215 handler. This prevents programs from violating the structured
2216 block semantics with throws. */
2219 maybe_catch_exception (tree *stmt_p)
2223 if (!flag_exceptions)
2226 if (lang_protect_cleanup_actions)
2227 t = lang_protect_cleanup_actions ();
2230 t = built_in_decls[BUILT_IN_TRAP];
2231 t = build_function_call_expr (t, NULL);
2233 f = build2 (EH_FILTER_EXPR, void_type_node, NULL, NULL);
2234 EH_FILTER_MUST_NOT_THROW (f) = 1;
2235 gimplify_and_add (t, &EH_FILTER_FAILURE (f));
2237 t = build2 (TRY_CATCH_EXPR, void_type_node, *stmt_p, NULL);
2238 append_to_statement_list (f, &TREE_OPERAND (t, 1));
2241 append_to_statement_list (t, stmt_p);
2244 /* Chain all the DECLs in LIST by their TREE_CHAIN fields. */
2247 list2chain (tree list)
2251 for (t = list; t; t = TREE_CHAIN (t))
2253 tree var = TREE_VALUE (t);
2255 TREE_CHAIN (var) = TREE_VALUE (TREE_CHAIN (t));
2257 TREE_CHAIN (var) = NULL_TREE;
2260 return list ? TREE_VALUE (list) : NULL_TREE;
2264 /* Remove barriers in REGION->EXIT's block. Note that this is only
2265 valid for OMP_PARALLEL regions. Since the end of a parallel region
2266 is an implicit barrier, any workshare inside the OMP_PARALLEL that
2267 left a barrier at the end of the OMP_PARALLEL region can now be
2271 remove_exit_barrier (struct omp_region *region)
2273 block_stmt_iterator si;
2274 basic_block exit_bb;
2279 exit_bb = region->exit;
2281 /* If the parallel region doesn't return, we don't have REGION->EXIT
2286 /* The last insn in the block will be the parallel's OMP_RETURN. The
2287 workshare's OMP_RETURN will be in a preceding block. The kinds of
2288 statements that can appear in between are extremely limited -- no
2289 memory operations at all. Here, we allow nothing at all, so the
2290 only thing we allow to precede this OMP_RETURN is a label. */
2291 si = bsi_last (exit_bb);
2292 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_RETURN);
2294 if (!bsi_end_p (si) && TREE_CODE (bsi_stmt (si)) != LABEL_EXPR)
2297 FOR_EACH_EDGE (e, ei, exit_bb->preds)
2299 si = bsi_last (e->src);
2303 if (TREE_CODE (t) == OMP_RETURN)
2304 OMP_RETURN_NOWAIT (t) = 1;
2309 remove_exit_barriers (struct omp_region *region)
2311 if (region->type == OMP_PARALLEL)
2312 remove_exit_barrier (region);
2316 region = region->inner;
2317 remove_exit_barriers (region);
2318 while (region->next)
2320 region = region->next;
2321 remove_exit_barriers (region);
2326 /* Expand the OpenMP parallel directive starting at REGION. */
2329 expand_omp_parallel (struct omp_region *region)
2331 basic_block entry_bb, exit_bb, new_bb;
2332 struct function *child_cfun, *saved_cfun;
2333 tree child_fn, block, t, ws_args;
2334 block_stmt_iterator si;
2338 entry_stmt = last_stmt (region->entry);
2339 child_fn = OMP_PARALLEL_FN (entry_stmt);
2340 child_cfun = DECL_STRUCT_FUNCTION (child_fn);
2343 entry_bb = region->entry;
2344 exit_bb = region->exit;
2346 if (is_combined_parallel (region))
2347 ws_args = region->ws_args;
2349 ws_args = NULL_TREE;
2351 if (child_cfun->cfg)
2353 /* Due to inlining, it may happen that we have already outlined
2354 the region, in which case all we need to do is make the
2355 sub-graph unreachable and emit the parallel call. */
2356 edge entry_succ_e, exit_succ_e;
2357 block_stmt_iterator si;
2359 entry_succ_e = single_succ_edge (entry_bb);
2360 exit_succ_e = single_succ_edge (exit_bb);
2362 si = bsi_last (entry_bb);
2363 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_PARALLEL);
2364 bsi_remove (&si, true);
2367 remove_edge (entry_succ_e);
2368 make_edge (new_bb, exit_succ_e->dest, EDGE_FALLTHRU);
2372 /* If the parallel region needs data sent from the parent
2373 function, then the very first statement of the parallel body
2374 is a copy assignment .OMP_DATA_I = &.OMP_DATA_O. Since
2375 &.OMP_DATA_O is passed as an argument to the child function,
2376 we need to replace it with the argument as seen by the child
2379 In most cases, this will end up being the identity assignment
2380 .OMP_DATA_I = .OMP_DATA_I. However, if the parallel body had
2381 a function call that has been inlined, the original PARM_DECL
2382 .OMP_DATA_I may have been converted into a different local
2383 variable. In which case, we need to keep the assignment. */
2384 if (OMP_PARALLEL_DATA_ARG (entry_stmt))
2386 basic_block entry_succ_bb = single_succ (entry_bb);
2387 block_stmt_iterator si = bsi_start (entry_succ_bb);
2390 gcc_assert (!bsi_end_p (si));
2392 stmt = bsi_stmt (si);
2393 gcc_assert (TREE_CODE (stmt) == MODIFY_EXPR
2394 && TREE_CODE (TREE_OPERAND (stmt, 1)) == ADDR_EXPR
2395 && TREE_OPERAND (TREE_OPERAND (stmt, 1), 0)
2396 == OMP_PARALLEL_DATA_ARG (entry_stmt));
2398 if (TREE_OPERAND (stmt, 0) == DECL_ARGUMENTS (child_fn))
2399 bsi_remove (&si, true);
2401 TREE_OPERAND (stmt, 1) = DECL_ARGUMENTS (child_fn);
2404 /* Declare local variables needed in CHILD_CFUN. */
2405 block = DECL_INITIAL (child_fn);
2406 BLOCK_VARS (block) = list2chain (child_cfun->unexpanded_var_list);
2407 DECL_SAVED_TREE (child_fn) = single_succ (entry_bb)->stmt_list;
2409 /* Reset DECL_CONTEXT on locals and function arguments. */
2410 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
2411 DECL_CONTEXT (t) = child_fn;
2413 for (t = DECL_ARGUMENTS (child_fn); t; t = TREE_CHAIN (t))
2414 DECL_CONTEXT (t) = child_fn;
2416 /* Split ENTRY_BB at OMP_PARALLEL so that it can be moved to the
2418 si = bsi_last (entry_bb);
2420 gcc_assert (t && TREE_CODE (t) == OMP_PARALLEL);
2421 bsi_remove (&si, true);
2422 e = split_block (entry_bb, t);
2424 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
2426 /* Move the parallel region into CHILD_CFUN. We need to reset
2427 dominance information because the expansion of the inner
2428 regions has invalidated it. */
2429 free_dominance_info (CDI_DOMINATORS);
2430 new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb);
2432 single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
2433 cgraph_add_new_function (child_fn);
2435 /* Convert OMP_RETURN into a RETURN_EXPR. */
2438 si = bsi_last (exit_bb);
2439 gcc_assert (!bsi_end_p (si)
2440 && TREE_CODE (bsi_stmt (si)) == OMP_RETURN);
2441 t = build1 (RETURN_EXPR, void_type_node, NULL);
2442 bsi_insert_after (&si, t, TSI_SAME_STMT);
2443 bsi_remove (&si, true);
2447 /* Emit a library call to launch the children threads. */
2448 expand_parallel_call (region, new_bb, entry_stmt, ws_args);
2452 /* A subroutine of expand_omp_for. Generate code for a parallel
2453 loop with any schedule. Given parameters:
2455 for (V = N1; V cond N2; V += STEP) BODY;
2457 where COND is "<" or ">", we generate pseudocode
2459 more = GOMP_loop_foo_start (N1, N2, STEP, CHUNK, &istart0, &iend0);
2460 if (more) goto L0; else goto L3;
2467 if (V cond iend) goto L1; else goto L2;
2469 if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
2472 If this is a combined omp parallel loop, instead of the call to
2473 GOMP_loop_foo_start, we emit 'goto L3'. */
2476 expand_omp_for_generic (struct omp_region *region,
2477 struct omp_for_data *fd,
2478 enum built_in_function start_fn,
2479 enum built_in_function next_fn)
2481 tree l0, l1, l2, l3;
2482 tree type, istart0, iend0, iend;
2484 basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, l2_bb, l3_bb;
2485 block_stmt_iterator si;
2486 bool in_combined_parallel = is_combined_parallel (region);
2488 type = TREE_TYPE (fd->v);
2490 istart0 = create_tmp_var (long_integer_type_node, ".istart0");
2491 iend0 = create_tmp_var (long_integer_type_node, ".iend0");
2492 iend = create_tmp_var (type, NULL);
2493 TREE_ADDRESSABLE (istart0) = 1;
2494 TREE_ADDRESSABLE (iend0) = 1;
2496 entry_bb = region->entry;
2497 l0_bb = create_empty_bb (entry_bb);
2498 l1_bb = single_succ (entry_bb);
2499 cont_bb = region->cont;
2500 l2_bb = create_empty_bb (cont_bb);
2501 l3_bb = single_succ (cont_bb);
2502 exit_bb = region->exit;
2504 l0 = tree_block_label (l0_bb);
2505 l1 = tree_block_label (l1_bb);
2506 l2 = tree_block_label (l2_bb);
2507 l3 = tree_block_label (l3_bb);
2509 si = bsi_last (entry_bb);
2510 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_FOR);
2511 if (!in_combined_parallel)
2513 /* If this is not a combined parallel loop, emit a call to
2514 GOMP_loop_foo_start in ENTRY_BB. */
2515 list = alloc_stmt_list ();
2516 t = build_fold_addr_expr (iend0);
2517 args = tree_cons (NULL, t, NULL);
2518 t = build_fold_addr_expr (istart0);
2519 args = tree_cons (NULL, t, args);
2522 t = fold_convert (long_integer_type_node, fd->chunk_size);
2523 args = tree_cons (NULL, t, args);
2525 t = fold_convert (long_integer_type_node, fd->step);
2526 args = tree_cons (NULL, t, args);
2527 t = fold_convert (long_integer_type_node, fd->n2);
2528 args = tree_cons (NULL, t, args);
2529 t = fold_convert (long_integer_type_node, fd->n1);
2530 args = tree_cons (NULL, t, args);
2531 t = build_function_call_expr (built_in_decls[start_fn], args);
2532 t = get_formal_tmp_var (t, &list);
2533 t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&l0),
2534 build_and_jump (&l3));
2535 append_to_statement_list (t, &list);
2536 bsi_insert_after (&si, list, BSI_SAME_STMT);
2538 bsi_remove (&si, true);
2540 /* Iteration setup for sequential loop goes in L0_BB. */
2541 list = alloc_stmt_list ();
2542 t = fold_convert (type, istart0);
2543 t = build2 (MODIFY_EXPR, void_type_node, fd->v, t);
2544 gimplify_and_add (t, &list);
2546 t = fold_convert (type, iend0);
2547 t = build2 (MODIFY_EXPR, void_type_node, iend, t);
2548 gimplify_and_add (t, &list);
2550 si = bsi_start (l0_bb);
2551 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2553 /* Code to control the increment and predicate for the sequential
2554 loop goes in the first half of EXIT_BB (we split EXIT_BB so
2555 that we can inherit all the edges going out of the loop
2557 list = alloc_stmt_list ();
2559 t = build2 (PLUS_EXPR, type, fd->v, fd->step);
2560 t = build2 (MODIFY_EXPR, void_type_node, fd->v, t);
2561 gimplify_and_add (t, &list);
2563 t = build2 (fd->cond_code, boolean_type_node, fd->v, iend);
2564 t = get_formal_tmp_var (t, &list);
2565 t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&l1),
2566 build_and_jump (&l2));
2567 append_to_statement_list (t, &list);
2569 si = bsi_last (cont_bb);
2570 bsi_insert_after (&si, list, BSI_SAME_STMT);
2571 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_CONTINUE);
2572 bsi_remove (&si, true);
2574 /* Emit code to get the next parallel iteration in L2_BB. */
2575 list = alloc_stmt_list ();
2577 t = build_fold_addr_expr (iend0);
2578 args = tree_cons (NULL, t, NULL);
2579 t = build_fold_addr_expr (istart0);
2580 args = tree_cons (NULL, t, args);
2581 t = build_function_call_expr (built_in_decls[next_fn], args);
2582 t = get_formal_tmp_var (t, &list);
2583 t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&l0),
2584 build_and_jump (&l3));
2585 append_to_statement_list (t, &list);
2587 si = bsi_start (l2_bb);
2588 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2590 /* Add the loop cleanup function. */
2591 si = bsi_last (exit_bb);
2592 if (OMP_RETURN_NOWAIT (bsi_stmt (si)))
2593 t = built_in_decls[BUILT_IN_GOMP_LOOP_END_NOWAIT];
2595 t = built_in_decls[BUILT_IN_GOMP_LOOP_END];
2596 t = build_function_call_expr (t, NULL);
2597 bsi_insert_after (&si, t, BSI_SAME_STMT);
2598 bsi_remove (&si, true);
2600 /* Connect the new blocks. */
2601 remove_edge (single_succ_edge (entry_bb));
2602 if (in_combined_parallel)
2603 make_edge (entry_bb, l2_bb, EDGE_FALLTHRU);
2606 make_edge (entry_bb, l0_bb, EDGE_TRUE_VALUE);
2607 make_edge (entry_bb, l3_bb, EDGE_FALSE_VALUE);
2610 make_edge (l0_bb, l1_bb, EDGE_FALLTHRU);
2612 remove_edge (single_succ_edge (cont_bb));
2613 make_edge (cont_bb, l1_bb, EDGE_TRUE_VALUE);
2614 make_edge (cont_bb, l2_bb, EDGE_FALSE_VALUE);
2616 make_edge (l2_bb, l0_bb, EDGE_TRUE_VALUE);
2617 make_edge (l2_bb, l3_bb, EDGE_FALSE_VALUE);
2621 /* A subroutine of expand_omp_for. Generate code for a parallel
2622 loop with static schedule and no specified chunk size. Given
2625 for (V = N1; V cond N2; V += STEP) BODY;
2627 where COND is "<" or ">", we generate pseudocode
2633 n = (adj + N2 - N1) / STEP;
2635 q += (q * nthreads != n);
2637 e0 = min(s0 + q, n);
2638 if (s0 >= e0) goto L2; else goto L0;
2645 if (V cond e) goto L1;
2650 expand_omp_for_static_nochunk (struct omp_region *region,
2651 struct omp_for_data *fd)
2653 tree l0, l1, l2, n, q, s0, e0, e, t, nthreads, threadid;
2654 tree type, utype, list;
2655 basic_block entry_bb, exit_bb, seq_start_bb, body_bb, cont_bb;
2657 block_stmt_iterator si;
2659 type = TREE_TYPE (fd->v);
2660 utype = lang_hooks.types.unsigned_type (type);
2662 entry_bb = region->entry;
2663 seq_start_bb = create_empty_bb (entry_bb);
2664 body_bb = single_succ (entry_bb);
2665 cont_bb = region->cont;
2666 fin_bb = single_succ (cont_bb);
2667 exit_bb = region->exit;
2669 l0 = tree_block_label (seq_start_bb);
2670 l1 = tree_block_label (body_bb);
2671 l2 = tree_block_label (fin_bb);
2673 /* Iteration space partitioning goes in ENTRY_BB. */
2674 list = alloc_stmt_list ();
2676 t = built_in_decls[BUILT_IN_OMP_GET_NUM_THREADS];
2677 t = build_function_call_expr (t, NULL);
2678 t = fold_convert (utype, t);
2679 nthreads = get_formal_tmp_var (t, &list);
2681 t = built_in_decls[BUILT_IN_OMP_GET_THREAD_NUM];
2682 t = build_function_call_expr (t, NULL);
2683 t = fold_convert (utype, t);
2684 threadid = get_formal_tmp_var (t, &list);
2686 fd->n1 = fold_convert (type, fd->n1);
2687 if (!is_gimple_val (fd->n1))
2688 fd->n1 = get_formal_tmp_var (fd->n1, &list);
2690 fd->n2 = fold_convert (type, fd->n2);
2691 if (!is_gimple_val (fd->n2))
2692 fd->n2 = get_formal_tmp_var (fd->n2, &list);
2694 fd->step = fold_convert (type, fd->step);
2695 if (!is_gimple_val (fd->step))
2696 fd->step = get_formal_tmp_var (fd->step, &list);
2698 t = build_int_cst (type, (fd->cond_code == LT_EXPR ? -1 : 1));
2699 t = fold_build2 (PLUS_EXPR, type, fd->step, t);
2700 t = fold_build2 (PLUS_EXPR, type, t, fd->n2);
2701 t = fold_build2 (MINUS_EXPR, type, t, fd->n1);
2702 t = fold_build2 (TRUNC_DIV_EXPR, type, t, fd->step);
2703 t = fold_convert (utype, t);
2704 if (is_gimple_val (t))
2707 n = get_formal_tmp_var (t, &list);
2709 t = build2 (TRUNC_DIV_EXPR, utype, n, nthreads);
2710 q = get_formal_tmp_var (t, &list);
2712 t = build2 (MULT_EXPR, utype, q, nthreads);
2713 t = build2 (NE_EXPR, utype, t, n);
2714 t = build2 (PLUS_EXPR, utype, q, t);
2715 q = get_formal_tmp_var (t, &list);
2717 t = build2 (MULT_EXPR, utype, q, threadid);
2718 s0 = get_formal_tmp_var (t, &list);
2720 t = build2 (PLUS_EXPR, utype, s0, q);
2721 t = build2 (MIN_EXPR, utype, t, n);
2722 e0 = get_formal_tmp_var (t, &list);
2724 t = build2 (GE_EXPR, boolean_type_node, s0, e0);
2725 t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&l2),
2726 build_and_jump (&l0));
2727 append_to_statement_list (t, &list);
2729 si = bsi_last (entry_bb);
2730 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_FOR);
2731 bsi_insert_after (&si, list, BSI_SAME_STMT);
2732 bsi_remove (&si, true);
2734 /* Setup code for sequential iteration goes in SEQ_START_BB. */
2735 list = alloc_stmt_list ();
2737 t = fold_convert (type, s0);
2738 t = build2 (MULT_EXPR, type, t, fd->step);
2739 t = build2 (PLUS_EXPR, type, t, fd->n1);
2740 t = build2 (MODIFY_EXPR, void_type_node, fd->v, t);
2741 gimplify_and_add (t, &list);
2743 t = fold_convert (type, e0);
2744 t = build2 (MULT_EXPR, type, t, fd->step);
2745 t = build2 (PLUS_EXPR, type, t, fd->n1);
2746 e = get_formal_tmp_var (t, &list);
2748 si = bsi_start (seq_start_bb);
2749 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2751 /* The code controlling the sequential loop replaces the OMP_CONTINUE. */
2752 list = alloc_stmt_list ();
2754 t = build2 (PLUS_EXPR, type, fd->v, fd->step);
2755 t = build2 (MODIFY_EXPR, void_type_node, fd->v, t);
2756 gimplify_and_add (t, &list);
2758 t = build2 (fd->cond_code, boolean_type_node, fd->v, e);
2759 t = get_formal_tmp_var (t, &list);
2760 t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&l1),
2761 build_and_jump (&l2));
2762 append_to_statement_list (t, &list);
2764 si = bsi_last (cont_bb);
2765 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_CONTINUE);
2766 bsi_insert_after (&si, list, BSI_SAME_STMT);
2767 bsi_remove (&si, true);
2769 /* Replace the OMP_RETURN with a barrier, or nothing. */
2770 si = bsi_last (exit_bb);
2771 if (!OMP_RETURN_NOWAIT (bsi_stmt (si)))
2773 list = alloc_stmt_list ();
2774 build_omp_barrier (&list);
2775 bsi_insert_after (&si, list, BSI_SAME_STMT);
2777 bsi_remove (&si, true);
2779 /* Connect all the blocks. */
2780 make_edge (seq_start_bb, body_bb, EDGE_FALLTHRU);
2782 remove_edge (single_succ_edge (entry_bb));
2783 make_edge (entry_bb, fin_bb, EDGE_TRUE_VALUE);
2784 make_edge (entry_bb, seq_start_bb, EDGE_FALSE_VALUE);
2786 make_edge (cont_bb, body_bb, EDGE_TRUE_VALUE);
2787 find_edge (cont_bb, fin_bb)->flags = EDGE_FALSE_VALUE;
2791 /* A subroutine of expand_omp_for. Generate code for a parallel
2792 loop with static schedule and a specified chunk size. Given
2795 for (V = N1; V cond N2; V += STEP) BODY;
2797 where COND is "<" or ">", we generate pseudocode
2803 n = (adj + N2 - N1) / STEP;
2806 s0 = (trip * nthreads + threadid) * CHUNK;
2807 e0 = min(s0 + CHUNK, n);
2808 if (s0 < n) goto L1; else goto L4;
2815 if (V cond e) goto L2; else goto L3;
2823 expand_omp_for_static_chunk (struct omp_region *region, struct omp_for_data *fd)
2825 tree l0, l1, l2, l3, l4, n, s0, e0, e, t;
2826 tree trip, nthreads, threadid;
2828 basic_block entry_bb, exit_bb, body_bb, seq_start_bb, iter_part_bb;
2829 basic_block trip_update_bb, cont_bb, fin_bb;
2831 block_stmt_iterator si;
2833 type = TREE_TYPE (fd->v);
2834 utype = lang_hooks.types.unsigned_type (type);
2836 entry_bb = region->entry;
2837 iter_part_bb = create_empty_bb (entry_bb);
2838 seq_start_bb = create_empty_bb (iter_part_bb);
2839 body_bb = single_succ (entry_bb);
2840 cont_bb = region->cont;
2841 trip_update_bb = create_empty_bb (cont_bb);
2842 fin_bb = single_succ (cont_bb);
2843 exit_bb = region->exit;
2845 l0 = tree_block_label (iter_part_bb);
2846 l1 = tree_block_label (seq_start_bb);
2847 l2 = tree_block_label (body_bb);
2848 l3 = tree_block_label (trip_update_bb);
2849 l4 = tree_block_label (fin_bb);
2851 /* Trip and adjustment setup goes in ENTRY_BB. */
2852 list = alloc_stmt_list ();
2854 t = built_in_decls[BUILT_IN_OMP_GET_NUM_THREADS];
2855 t = build_function_call_expr (t, NULL);
2856 t = fold_convert (utype, t);
2857 nthreads = get_formal_tmp_var (t, &list);
2859 t = built_in_decls[BUILT_IN_OMP_GET_THREAD_NUM];
2860 t = build_function_call_expr (t, NULL);
2861 t = fold_convert (utype, t);
2862 threadid = get_formal_tmp_var (t, &list);
2864 fd->n1 = fold_convert (type, fd->n1);
2865 if (!is_gimple_val (fd->n1))
2866 fd->n1 = get_formal_tmp_var (fd->n1, &list);
2868 fd->n2 = fold_convert (type, fd->n2);
2869 if (!is_gimple_val (fd->n2))
2870 fd->n2 = get_formal_tmp_var (fd->n2, &list);
2872 fd->step = fold_convert (type, fd->step);
2873 if (!is_gimple_val (fd->step))
2874 fd->step = get_formal_tmp_var (fd->step, &list);
2876 fd->chunk_size = fold_convert (utype, fd->chunk_size);
2877 if (!is_gimple_val (fd->chunk_size))
2878 fd->chunk_size = get_formal_tmp_var (fd->chunk_size, &list);
2880 t = build_int_cst (type, (fd->cond_code == LT_EXPR ? -1 : 1));
2881 t = fold_build2 (PLUS_EXPR, type, fd->step, t);
2882 t = fold_build2 (PLUS_EXPR, type, t, fd->n2);
2883 t = fold_build2 (MINUS_EXPR, type, t, fd->n1);
2884 t = fold_build2 (TRUNC_DIV_EXPR, type, t, fd->step);
2885 t = fold_convert (utype, t);
2886 if (is_gimple_val (t))
2889 n = get_formal_tmp_var (t, &list);
2891 t = build_int_cst (utype, 0);
2892 trip = get_initialized_tmp_var (t, &list, NULL);
2894 si = bsi_last (entry_bb);
2895 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_FOR);
2896 bsi_insert_after (&si, list, BSI_SAME_STMT);
2897 bsi_remove (&si, true);
2899 /* Iteration space partitioning goes in ITER_PART_BB. */
2900 list = alloc_stmt_list ();
2902 t = build2 (MULT_EXPR, utype, trip, nthreads);
2903 t = build2 (PLUS_EXPR, utype, t, threadid);
2904 t = build2 (MULT_EXPR, utype, t, fd->chunk_size);
2905 s0 = get_formal_tmp_var (t, &list);
2907 t = build2 (PLUS_EXPR, utype, s0, fd->chunk_size);
2908 t = build2 (MIN_EXPR, utype, t, n);
2909 e0 = get_formal_tmp_var (t, &list);
2911 t = build2 (LT_EXPR, boolean_type_node, s0, n);
2912 t = build3 (COND_EXPR, void_type_node, t,
2913 build_and_jump (&l1), build_and_jump (&l4));
2914 append_to_statement_list (t, &list);
2916 si = bsi_start (iter_part_bb);
2917 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2919 /* Setup code for sequential iteration goes in SEQ_START_BB. */
2920 list = alloc_stmt_list ();
2922 t = fold_convert (type, s0);
2923 t = build2 (MULT_EXPR, type, t, fd->step);
2924 t = build2 (PLUS_EXPR, type, t, fd->n1);
2925 t = build2 (MODIFY_EXPR, void_type_node, fd->v, t);
2926 gimplify_and_add (t, &list);
2928 t = fold_convert (type, e0);
2929 t = build2 (MULT_EXPR, type, t, fd->step);
2930 t = build2 (PLUS_EXPR, type, t, fd->n1);
2931 e = get_formal_tmp_var (t, &list);
2933 si = bsi_start (seq_start_bb);
2934 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2936 /* The code controlling the sequential loop goes in CONT_BB,
2937 replacing the OMP_CONTINUE. */
2938 list = alloc_stmt_list ();
2940 t = build2 (PLUS_EXPR, type, fd->v, fd->step);
2941 t = build2 (MODIFY_EXPR, void_type_node, fd->v, t);
2942 gimplify_and_add (t, &list);
2944 t = build2 (fd->cond_code, boolean_type_node, fd->v, e);
2945 t = get_formal_tmp_var (t, &list);
2946 t = build3 (COND_EXPR, void_type_node, t,
2947 build_and_jump (&l2), build_and_jump (&l3));
2948 append_to_statement_list (t, &list);
2950 si = bsi_last (cont_bb);
2951 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_CONTINUE);
2952 bsi_insert_after (&si, list, BSI_SAME_STMT);
2953 bsi_remove (&si, true);
2955 /* Trip update code goes into TRIP_UPDATE_BB. */
2956 list = alloc_stmt_list ();
2958 t = build_int_cst (utype, 1);
2959 t = build2 (PLUS_EXPR, utype, trip, t);
2960 t = build2 (MODIFY_EXPR, void_type_node, trip, t);
2961 gimplify_and_add (t, &list);
2963 si = bsi_start (trip_update_bb);
2964 bsi_insert_after (&si, list, BSI_CONTINUE_LINKING);
2966 /* Replace the OMP_RETURN with a barrier, or nothing. */
2967 si = bsi_last (exit_bb);
2968 if (!OMP_RETURN_NOWAIT (bsi_stmt (si)))
2970 list = alloc_stmt_list ();
2971 build_omp_barrier (&list);
2972 bsi_insert_after (&si, list, BSI_SAME_STMT);
2974 bsi_remove (&si, true);
2976 /* Connect the new blocks. */
2977 remove_edge (single_succ_edge (entry_bb));
2978 make_edge (entry_bb, iter_part_bb, EDGE_FALLTHRU);
2980 make_edge (iter_part_bb, seq_start_bb, EDGE_TRUE_VALUE);
2981 make_edge (iter_part_bb, fin_bb, EDGE_FALSE_VALUE);
2983 make_edge (seq_start_bb, body_bb, EDGE_FALLTHRU);
2985 remove_edge (single_succ_edge (cont_bb));
2986 make_edge (cont_bb, body_bb, EDGE_TRUE_VALUE);
2987 make_edge (cont_bb, trip_update_bb, EDGE_FALSE_VALUE);
2989 make_edge (trip_update_bb, iter_part_bb, EDGE_FALLTHRU);
2993 /* Expand the OpenMP loop defined by REGION. */
2996 expand_omp_for (struct omp_region *region)
2998 struct omp_for_data fd;
3000 push_gimplify_context ();
3002 extract_omp_for_data (last_stmt (region->entry), &fd);
3003 region->sched_kind = fd.sched_kind;
3005 if (fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC && !fd.have_ordered)
3007 if (fd.chunk_size == NULL)
3008 expand_omp_for_static_nochunk (region, &fd);
3010 expand_omp_for_static_chunk (region, &fd);
3014 int fn_index = fd.sched_kind + fd.have_ordered * 4;
3015 int start_ix = BUILT_IN_GOMP_LOOP_STATIC_START + fn_index;
3016 int next_ix = BUILT_IN_GOMP_LOOP_STATIC_NEXT + fn_index;
3017 expand_omp_for_generic (region, &fd, start_ix, next_ix);
3020 pop_gimplify_context (NULL);
3024 /* Expand code for an OpenMP sections directive. In pseudo code, we generate
3026 v = GOMP_sections_start (n);
3043 v = GOMP_sections_next ();
3048 If this is a combined parallel sections, replace the call to
3049 GOMP_sections_start with 'goto L1'. */
3052 expand_omp_sections (struct omp_region *region)
3054 tree label_vec, l0, l1, l2, t, u, v, sections_stmt;
3056 basic_block entry_bb, exit_bb, l0_bb, l1_bb, l2_bb, default_bb;
3057 block_stmt_iterator si;
3058 struct omp_region *inner;
3061 entry_bb = region->entry;
3062 l0_bb = create_empty_bb (entry_bb);
3063 l1_bb = region->cont;
3064 l2_bb = single_succ (l1_bb);
3065 default_bb = create_empty_bb (l1_bb->prev_bb);
3066 exit_bb = region->exit;
3068 l0 = tree_block_label (l0_bb);
3069 l1 = tree_block_label (l1_bb);
3070 l2 = tree_block_label (l2_bb);
3072 v = create_tmp_var (unsigned_type_node, ".section");
3074 /* We will build a switch() with enough cases for all the
3075 OMP_SECTION regions, a '0' case to handle the end of more work
3076 and a default case to abort if something goes wrong. */
3077 len = EDGE_COUNT (entry_bb->succs);
3078 label_vec = make_tree_vec (len + 2);
3080 /* The call to GOMP_sections_start goes in ENTRY_BB, replacing the
3081 OMP_SECTIONS statement. */
3082 si = bsi_last (entry_bb);
3083 sections_stmt = bsi_stmt (si);
3084 gcc_assert (TREE_CODE (sections_stmt) == OMP_SECTIONS);
3085 if (!is_combined_parallel (region))
3087 /* If we are not inside a combined parallel+sections region,
3088 call GOMP_sections_start. */
3089 t = build_int_cst (unsigned_type_node, len);
3090 t = tree_cons (NULL, t, NULL);
3091 u = built_in_decls[BUILT_IN_GOMP_SECTIONS_START];
3092 t = build_function_call_expr (u, t);
3093 t = build2 (MODIFY_EXPR, void_type_node, v, t);
3094 bsi_insert_after (&si, t, BSI_SAME_STMT);
3096 bsi_remove (&si, true);
3098 /* The switch() statement replacing OMP_SECTIONS goes in L0_BB. */
3099 si = bsi_start (l0_bb);
3101 t = build3 (SWITCH_EXPR, void_type_node, v, NULL, label_vec);
3102 bsi_insert_after (&si, t, BSI_CONTINUE_LINKING);
3104 t = build3 (CASE_LABEL_EXPR, void_type_node,
3105 build_int_cst (unsigned_type_node, 0), NULL, l2);
3106 TREE_VEC_ELT (label_vec, 0) = t;
3107 make_edge (l0_bb, l2_bb, 0);
3109 /* Convert each OMP_SECTION into a CASE_LABEL_EXPR. */
3110 for (inner = region->inner, i = 1; inner; inner = inner->next, ++i)
3112 basic_block s_entry_bb, s_exit_bb;
3114 s_entry_bb = inner->entry;
3115 s_exit_bb = inner->exit;
3117 t = tree_block_label (s_entry_bb);
3118 u = build_int_cst (unsigned_type_node, i);
3119 u = build3 (CASE_LABEL_EXPR, void_type_node, u, NULL, t);
3120 TREE_VEC_ELT (label_vec, i) = u;
3122 si = bsi_last (s_entry_bb);
3123 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_SECTION);
3124 gcc_assert (i < len || OMP_SECTION_LAST (bsi_stmt (si)));
3125 bsi_remove (&si, true);
3127 si = bsi_last (s_exit_bb);
3128 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_RETURN);
3129 bsi_remove (&si, true);
3131 e = single_pred_edge (s_entry_bb);
3133 redirect_edge_pred (e, l0_bb);
3135 single_succ_edge (s_entry_bb)->flags = EDGE_FALLTHRU;
3136 single_succ_edge (s_exit_bb)->flags = EDGE_FALLTHRU;
3139 /* Error handling code goes in DEFAULT_BB. */
3140 t = tree_block_label (default_bb);
3141 u = build3 (CASE_LABEL_EXPR, void_type_node, NULL, NULL, t);
3142 TREE_VEC_ELT (label_vec, len + 1) = u;
3143 make_edge (l0_bb, default_bb, 0);
3145 si = bsi_start (default_bb);
3146 t = built_in_decls[BUILT_IN_TRAP];
3147 t = build_function_call_expr (t, NULL);
3148 bsi_insert_after (&si, t, BSI_CONTINUE_LINKING);
3150 /* Code to get the next section goes in L1_BB. */
3151 si = bsi_last (l1_bb);
3152 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_CONTINUE);
3154 t = built_in_decls[BUILT_IN_GOMP_SECTIONS_NEXT];
3155 t = build_function_call_expr (t, NULL);
3156 t = build2 (MODIFY_EXPR, void_type_node, v, t);
3157 bsi_insert_after (&si, t, BSI_SAME_STMT);
3158 bsi_remove (&si, true);
3160 /* Cleanup function replaces OMP_RETURN in EXIT_BB. */
3161 si = bsi_last (exit_bb);
3162 if (OMP_RETURN_NOWAIT (bsi_stmt (si)))
3163 t = built_in_decls[BUILT_IN_GOMP_SECTIONS_END_NOWAIT];
3165 t = built_in_decls[BUILT_IN_GOMP_SECTIONS_END];
3166 t = build_function_call_expr (t, NULL);
3167 bsi_insert_after (&si, t, BSI_SAME_STMT);
3168 bsi_remove (&si, true);
3170 /* Connect the new blocks. */
3171 if (is_combined_parallel (region))
3173 /* If this was a combined parallel+sections region, we did not
3174 emit a GOMP_sections_start in the entry block, so we just
3175 need to jump to L1_BB to get the next section. */
3176 make_edge (entry_bb, l1_bb, EDGE_FALLTHRU);
3179 make_edge (entry_bb, l0_bb, EDGE_FALLTHRU);
3181 e = single_succ_edge (l1_bb);
3182 redirect_edge_succ (e, l0_bb);
3183 e->flags = EDGE_FALLTHRU;
3187 /* Expand code for an OpenMP single directive. We've already expanded
3188 much of the code, here we simply place the GOMP_barrier call. */
3191 expand_omp_single (struct omp_region *region)
3193 basic_block entry_bb, exit_bb;
3194 block_stmt_iterator si;
3195 bool need_barrier = false;
3197 entry_bb = region->entry;
3198 exit_bb = region->exit;
3200 si = bsi_last (entry_bb);
3201 /* The terminal barrier at the end of a GOMP_single_copy sequence cannot
3202 be removed. We need to ensure that the thread that entered the single
3203 does not exit before the data is copied out by the other threads. */
3204 if (find_omp_clause (OMP_SINGLE_CLAUSES (bsi_stmt (si)),
3205 OMP_CLAUSE_COPYPRIVATE))
3206 need_barrier = true;
3207 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_SINGLE);
3208 bsi_remove (&si, true);
3209 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
3211 si = bsi_last (exit_bb);
3212 if (!OMP_RETURN_NOWAIT (bsi_stmt (si)) || need_barrier)
3214 tree t = alloc_stmt_list ();
3215 build_omp_barrier (&t);
3216 bsi_insert_after (&si, t, BSI_SAME_STMT);
3218 bsi_remove (&si, true);
3219 single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
3223 /* Generic expansion for OpenMP synchronization directives: master,
3224 ordered and critical. All we need to do here is remove the entry
3225 and exit markers for REGION. */
3228 expand_omp_synch (struct omp_region *region)
3230 basic_block entry_bb, exit_bb;
3231 block_stmt_iterator si;
3233 entry_bb = region->entry;
3234 exit_bb = region->exit;
3236 si = bsi_last (entry_bb);
3237 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_SINGLE
3238 || TREE_CODE (bsi_stmt (si)) == OMP_MASTER
3239 || TREE_CODE (bsi_stmt (si)) == OMP_ORDERED
3240 || TREE_CODE (bsi_stmt (si)) == OMP_CRITICAL);
3241 bsi_remove (&si, true);
3242 single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
3244 si = bsi_last (exit_bb);
3245 gcc_assert (TREE_CODE (bsi_stmt (si)) == OMP_RETURN);
3246 bsi_remove (&si, true);
3247 single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
3251 /* Expand the parallel region tree rooted at REGION. Expansion
3252 proceeds in depth-first order. Innermost regions are expanded
3253 first. This way, parallel regions that require a new function to
3254 be created (e.g., OMP_PARALLEL) can be expanded without having any
3255 internal dependencies in their body. */
3258 expand_omp (struct omp_region *region)
3263 expand_omp (region->inner);
3265 switch (region->type)
3268 expand_omp_parallel (region);
3272 expand_omp_for (region);
3276 expand_omp_sections (region);
3280 /* Individual omp sections are handled together with their
3281 parent OMP_SECTIONS region. */
3285 expand_omp_single (region);
3291 expand_omp_synch (region);
3298 region = region->next;
3303 /* Helper for build_omp_regions. Scan the dominator tree starting at
3304 block BB. PARENT is the region that contains BB. */
3307 build_omp_regions_1 (basic_block bb, struct omp_region *parent)
3309 block_stmt_iterator si;
3314 if (!bsi_end_p (si) && OMP_DIRECTIVE_P (bsi_stmt (si)))
3316 struct omp_region *region;
3317 enum tree_code code;
3319 stmt = bsi_stmt (si);
3320 code = TREE_CODE (stmt);
3322 if (code == OMP_RETURN)
3324 /* STMT is the return point out of region PARENT. Mark it
3325 as the exit point and make PARENT the immediately
3326 enclosing region. */
3327 gcc_assert (parent);
3330 parent = parent->outer;
3332 /* If REGION is a parallel region, determine whether it is
3333 a combined parallel+workshare region. */
3334 if (region->type == OMP_PARALLEL)
3335 determine_parallel_type (region);
3337 else if (code == OMP_CONTINUE)
3339 gcc_assert (parent);
3344 /* Otherwise, this directive becomes the parent for a new
3346 region = new_omp_region (bb, code, parent);
3351 for (son = first_dom_son (CDI_DOMINATORS, bb);
3353 son = next_dom_son (CDI_DOMINATORS, son))
3354 build_omp_regions_1 (son, parent);
3358 /* Scan the CFG and build a tree of OMP regions. Return the root of
3359 the OMP region tree. */
3362 build_omp_regions (void)
3364 gcc_assert (root_omp_region == NULL);
3365 calculate_dominance_info (CDI_DOMINATORS);
3366 build_omp_regions_1 (ENTRY_BLOCK_PTR, NULL);
3370 /* Main entry point for expanding OMP-GIMPLE into runtime calls. */
3373 execute_expand_omp (void)
3375 build_omp_regions ();
3377 if (!root_omp_region)
3382 fprintf (dump_file, "\nOMP region tree\n\n");
3383 dump_omp_region (dump_file, root_omp_region, 0);
3384 fprintf (dump_file, "\n");
3387 remove_exit_barriers (root_omp_region);
3389 expand_omp (root_omp_region);
3391 free_dominance_info (CDI_DOMINATORS);
3392 free_dominance_info (CDI_POST_DOMINATORS);
3393 cleanup_tree_cfg ();
3395 free_omp_regions ();
3401 gate_expand_omp (void)
3403 return flag_openmp != 0 && errorcount == 0;
3406 struct tree_opt_pass pass_expand_omp =
3408 "ompexp", /* name */
3409 gate_expand_omp, /* gate */
3410 execute_expand_omp, /* execute */
3413 0, /* static_pass_number */
3415 PROP_gimple_any, /* properties_required */
3416 PROP_gimple_lomp, /* properties_provided */
3417 0, /* properties_destroyed */
3418 0, /* todo_flags_start */
3419 TODO_dump_func, /* todo_flags_finish */
3423 /* Routines to lower OpenMP directives into OMP-GIMPLE. */
3425 /* Lower the OpenMP sections directive in *STMT_P. */
3428 lower_omp_sections (tree *stmt_p, omp_context *ctx)
3430 tree new_stmt, stmt, body, bind, block, ilist, olist, new_body;
3432 tree_stmt_iterator tsi;
3437 push_gimplify_context ();
3441 lower_rec_input_clauses (OMP_SECTIONS_CLAUSES (stmt), &ilist, &dlist, ctx);
3443 tsi = tsi_start (OMP_SECTIONS_BODY (stmt));
3444 for (len = 0; !tsi_end_p (tsi); len++, tsi_next (&tsi))
3447 tsi = tsi_start (OMP_SECTIONS_BODY (stmt));
3448 body = alloc_stmt_list ();
3449 for (i = 0; i < len; i++, tsi_next (&tsi))
3452 tree sec_start, sec_end;
3454 sec_start = tsi_stmt (tsi);
3455 sctx = maybe_lookup_ctx (sec_start);
3458 append_to_statement_list (sec_start, &body);
3460 lower_omp (&OMP_SECTION_BODY (sec_start), sctx);
3461 append_to_statement_list (OMP_SECTION_BODY (sec_start), &body);
3462 OMP_SECTION_BODY (sec_start) = NULL;
3466 tree l = alloc_stmt_list ();
3467 lower_lastprivate_clauses (OMP_SECTIONS_CLAUSES (stmt), NULL,
3469 append_to_statement_list (l, &body);
3470 OMP_SECTION_LAST (sec_start) = 1;
3473 sec_end = make_node (OMP_RETURN);
3474 append_to_statement_list (sec_end, &body);
3477 block = make_node (BLOCK);
3478 bind = build3 (BIND_EXPR, void_type_node, NULL, body, block);
3481 lower_reduction_clauses (OMP_SECTIONS_CLAUSES (stmt), &olist, ctx);
3483 pop_gimplify_context (NULL_TREE);
3484 record_vars_into (ctx->block_vars, ctx->cb.dst_fn);
3486 new_stmt = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3487 TREE_SIDE_EFFECTS (new_stmt) = 1;
3489 new_body = alloc_stmt_list ();
3490 append_to_statement_list (ilist, &new_body);
3491 append_to_statement_list (stmt, &new_body);
3492 append_to_statement_list (bind, &new_body);
3494 t = make_node (OMP_CONTINUE);
3495 append_to_statement_list (t, &new_body);
3497 append_to_statement_list (olist, &new_body);
3498 append_to_statement_list (dlist, &new_body);
3500 maybe_catch_exception (&new_body);
3502 t = make_node (OMP_RETURN);
3503 OMP_RETURN_NOWAIT (t) = !!find_omp_clause (OMP_SECTIONS_CLAUSES (stmt),
3505 append_to_statement_list (t, &new_body);
3507 BIND_EXPR_BODY (new_stmt) = new_body;
3508 OMP_SECTIONS_BODY (stmt) = NULL;
3514 /* A subroutine of lower_omp_single. Expand the simple form of
3515 an OMP_SINGLE, without a copyprivate clause:
3517 if (GOMP_single_start ())
3519 [ GOMP_barrier (); ] -> unless 'nowait' is present.
3521 FIXME. It may be better to delay expanding the logic of this until
3522 pass_expand_omp. The expanded logic may make the job more difficult
3523 to a synchronization analysis pass. */
3526 lower_omp_single_simple (tree single_stmt, tree *pre_p)
3530 t = built_in_decls[BUILT_IN_GOMP_SINGLE_START];
3531 t = build_function_call_expr (t, NULL);
3532 t = build3 (COND_EXPR, void_type_node, t,
3533 OMP_SINGLE_BODY (single_stmt), NULL);
3534 gimplify_and_add (t, pre_p);
3538 /* A subroutine of lower_omp_single. Expand the simple form of
3539 an OMP_SINGLE, with a copyprivate clause:
3541 #pragma omp single copyprivate (a, b, c)
3543 Create a new structure to hold copies of 'a', 'b' and 'c' and emit:
3546 if ((copyout_p = GOMP_single_copy_start ()) == NULL)
3552 GOMP_single_copy_end (©out);
3563 FIXME. It may be better to delay expanding the logic of this until
3564 pass_expand_omp. The expanded logic may make the job more difficult
3565 to a synchronization analysis pass. */
3568 lower_omp_single_copy (tree single_stmt, tree *pre_p, omp_context *ctx)
3570 tree ptr_type, t, args, l0, l1, l2, copyin_seq;
3572 ctx->sender_decl = create_tmp_var (ctx->record_type, ".omp_copy_o");
3574 ptr_type = build_pointer_type (ctx->record_type);
3575 ctx->receiver_decl = create_tmp_var (ptr_type, ".omp_copy_i");
3577 l0 = create_artificial_label ();
3578 l1 = create_artificial_label ();
3579 l2 = create_artificial_label ();
3581 t = built_in_decls[BUILT_IN_GOMP_SINGLE_COPY_START];
3582 t = build_function_call_expr (t, NULL);
3583 t = fold_convert (ptr_type, t);
3584 t = build2 (MODIFY_EXPR, void_type_node, ctx->receiver_decl, t);
3585 gimplify_and_add (t, pre_p);
3587 t = build2 (EQ_EXPR, boolean_type_node, ctx->receiver_decl,
3588 build_int_cst (ptr_type, 0));
3589 t = build3 (COND_EXPR, void_type_node, t,
3590 build_and_jump (&l0), build_and_jump (&l1));
3591 gimplify_and_add (t, pre_p);
3593 t = build1 (LABEL_EXPR, void_type_node, l0);
3594 gimplify_and_add (t, pre_p);
3596 append_to_statement_list (OMP_SINGLE_BODY (single_stmt), pre_p);
3599 lower_copyprivate_clauses (OMP_SINGLE_CLAUSES (single_stmt), pre_p,
3602 t = build_fold_addr_expr (ctx->sender_decl);
3603 args = tree_cons (NULL, t, NULL);
3604 t = built_in_decls[BUILT_IN_GOMP_SINGLE_COPY_END];
3605 t = build_function_call_expr (t, args);
3606 gimplify_and_add (t, pre_p);
3608 t = build_and_jump (&l2);
3609 gimplify_and_add (t, pre_p);
3611 t = build1 (LABEL_EXPR, void_type_node, l1);
3612 gimplify_and_add (t, pre_p);
3614 append_to_statement_list (copyin_seq, pre_p);
3616 t = build1 (LABEL_EXPR, void_type_node, l2);
3617 gimplify_and_add (t, pre_p);
3621 /* Expand code for an OpenMP single directive. */
3624 lower_omp_single (tree *stmt_p, omp_context *ctx)
3626 tree t, bind, block, single_stmt = *stmt_p, dlist;
3628 push_gimplify_context ();
3630 block = make_node (BLOCK);
3631 *stmt_p = bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, block);
3632 TREE_SIDE_EFFECTS (bind) = 1;
3634 lower_rec_input_clauses (OMP_SINGLE_CLAUSES (single_stmt),
3635 &BIND_EXPR_BODY (bind), &dlist, ctx);
3636 lower_omp (&OMP_SINGLE_BODY (single_stmt), ctx);
3638 append_to_statement_list (single_stmt, &BIND_EXPR_BODY (bind));
3640 if (ctx->record_type)
3641 lower_omp_single_copy (single_stmt, &BIND_EXPR_BODY (bind), ctx);
3643 lower_omp_single_simple (single_stmt, &BIND_EXPR_BODY (bind));
3645 OMP_SINGLE_BODY (single_stmt) = NULL;
3647 append_to_statement_list (dlist, &BIND_EXPR_BODY (bind));
3649 maybe_catch_exception (&BIND_EXPR_BODY (bind));
3651 t = make_node (OMP_RETURN);
3652 OMP_RETURN_NOWAIT (t) = !!find_omp_clause (OMP_SINGLE_CLAUSES (single_stmt),
3654 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
3656 pop_gimplify_context (bind);
3658 BIND_EXPR_VARS (bind) = chainon (BIND_EXPR_VARS (bind), ctx->block_vars);
3659 BLOCK_VARS (block) = BIND_EXPR_VARS (bind);
3663 /* Expand code for an OpenMP master directive. */
3666 lower_omp_master (tree *stmt_p, omp_context *ctx)
3668 tree bind, block, stmt = *stmt_p, lab = NULL, x;
3670 push_gimplify_context ();
3672 block = make_node (BLOCK);
3673 *stmt_p = bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, block);
3674 TREE_SIDE_EFFECTS (bind) = 1;
3676 append_to_statement_list (stmt, &BIND_EXPR_BODY (bind));
3678 x = built_in_decls[BUILT_IN_OMP_GET_THREAD_NUM];
3679 x = build_function_call_expr (x, NULL);
3680 x = build2 (EQ_EXPR, boolean_type_node, x, integer_zero_node);
3681 x = build3 (COND_EXPR, void_type_node, x, NULL, build_and_jump (&lab));
3682 gimplify_and_add (x, &BIND_EXPR_BODY (bind));
3684 lower_omp (&OMP_MASTER_BODY (stmt), ctx);
3685 maybe_catch_exception (&OMP_MASTER_BODY (stmt));
3686 append_to_statement_list (OMP_MASTER_BODY (stmt), &BIND_EXPR_BODY (bind));
3687 OMP_MASTER_BODY (stmt) = NULL;
3689 x = build1 (LABEL_EXPR, void_type_node, lab);
3690 gimplify_and_add (x, &BIND_EXPR_BODY (bind));
3692 x = make_node (OMP_RETURN);
3693 OMP_RETURN_NOWAIT (x) = 1;
3694 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3696 pop_gimplify_context (bind);
3698 BIND_EXPR_VARS (bind) = chainon (BIND_EXPR_VARS (bind), ctx->block_vars);
3699 BLOCK_VARS (block) = BIND_EXPR_VARS (bind);
3703 /* Expand code for an OpenMP ordered directive. */
3706 lower_omp_ordered (tree *stmt_p, omp_context *ctx)
3708 tree bind, block, stmt = *stmt_p, x;
3710 push_gimplify_context ();
3712 block = make_node (BLOCK);
3713 *stmt_p = bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, block);
3714 TREE_SIDE_EFFECTS (bind) = 1;
3716 append_to_statement_list (stmt, &BIND_EXPR_BODY (bind));
3718 x = built_in_decls[BUILT_IN_GOMP_ORDERED_START];
3719 x = build_function_call_expr (x, NULL);
3720 gimplify_and_add (x, &BIND_EXPR_BODY (bind));
3722 lower_omp (&OMP_ORDERED_BODY (stmt), ctx);
3723 maybe_catch_exception (&OMP_ORDERED_BODY (stmt));
3724 append_to_statement_list (OMP_ORDERED_BODY (stmt), &BIND_EXPR_BODY (bind));
3725 OMP_ORDERED_BODY (stmt) = NULL;
3727 x = built_in_decls[BUILT_IN_GOMP_ORDERED_END];
3728 x = build_function_call_expr (x, NULL);
3729 gimplify_and_add (x, &BIND_EXPR_BODY (bind));
3731 x = make_node (OMP_RETURN);
3732 OMP_RETURN_NOWAIT (x) = 1;
3733 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3735 pop_gimplify_context (bind);
3737 BIND_EXPR_VARS (bind) = chainon (BIND_EXPR_VARS (bind), ctx->block_vars);
3738 BLOCK_VARS (block) = BIND_EXPR_VARS (bind);
3742 /* Gimplify an OMP_CRITICAL statement. This is a relatively simple
3743 substitution of a couple of function calls. But in the NAMED case,
3744 requires that languages coordinate a symbol name. It is therefore
3745 best put here in common code. */
3747 static GTY((param1_is (tree), param2_is (tree)))
3748 splay_tree critical_name_mutexes;
3751 lower_omp_critical (tree *stmt_p, omp_context *ctx)
3753 tree bind, block, stmt = *stmt_p;
3754 tree t, lock, unlock, name;
3756 name = OMP_CRITICAL_NAME (stmt);
3762 if (!critical_name_mutexes)
3763 critical_name_mutexes
3764 = splay_tree_new_ggc (splay_tree_compare_pointers);
3766 n = splay_tree_lookup (critical_name_mutexes, (splay_tree_key) name);
3771 decl = create_tmp_var_raw (ptr_type_node, NULL);
3773 new_str = ACONCAT ((".gomp_critical_user_",
3774 IDENTIFIER_POINTER (name), NULL));
3775 DECL_NAME (decl) = get_identifier (new_str);
3776 TREE_PUBLIC (decl) = 1;
3777 TREE_STATIC (decl) = 1;
3778 DECL_COMMON (decl) = 1;
3779 DECL_ARTIFICIAL (decl) = 1;
3780 DECL_IGNORED_P (decl) = 1;
3781 cgraph_varpool_finalize_decl (decl);
3783 splay_tree_insert (critical_name_mutexes, (splay_tree_key) name,
3784 (splay_tree_value) decl);
3787 decl = (tree) n->value;
3789 args = tree_cons (NULL, build_fold_addr_expr (decl), NULL);
3790 lock = built_in_decls[BUILT_IN_GOMP_CRITICAL_NAME_START];
3791 lock = build_function_call_expr (lock, args);
3793 args = tree_cons (NULL, build_fold_addr_expr (decl), NULL);
3794 unlock = built_in_decls[BUILT_IN_GOMP_CRITICAL_NAME_END];
3795 unlock = build_function_call_expr (unlock, args);
3799 lock = built_in_decls[BUILT_IN_GOMP_CRITICAL_START];
3800 lock = build_function_call_expr (lock, NULL);
3802 unlock = built_in_decls[BUILT_IN_GOMP_CRITICAL_END];
3803 unlock = build_function_call_expr (unlock, NULL);
3806 push_gimplify_context ();
3808 block = make_node (BLOCK);
3809 *stmt_p = bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, block);
3810 TREE_SIDE_EFFECTS (bind) = 1;
3812 append_to_statement_list (stmt, &BIND_EXPR_BODY (bind));
3814 gimplify_and_add (lock, &BIND_EXPR_BODY (bind));
3816 lower_omp (&OMP_CRITICAL_BODY (stmt), ctx);
3817 maybe_catch_exception (&OMP_CRITICAL_BODY (stmt));
3818 append_to_statement_list (OMP_CRITICAL_BODY (stmt), &BIND_EXPR_BODY (bind));
3819 OMP_CRITICAL_BODY (stmt) = NULL;
3821 gimplify_and_add (unlock, &BIND_EXPR_BODY (bind));
3823 t = make_node (OMP_RETURN);
3824 OMP_RETURN_NOWAIT (t) = 1;
3825 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
3827 pop_gimplify_context (bind);
3828 BIND_EXPR_VARS (bind) = chainon (BIND_EXPR_VARS (bind), ctx->block_vars);
3829 BLOCK_VARS (block) = BIND_EXPR_VARS (bind);
3833 /* A subroutine of lower_omp_for. Generate code to emit the predicate
3834 for a lastprivate clause. Given a loop control predicate of (V
3835 cond N2), we gate the clause on (!(V cond N2)). The lowered form
3836 is appended to *BODY_P. */
3839 lower_omp_for_lastprivate (struct omp_for_data *fd, tree *body_p,
3840 struct omp_context *ctx)
3843 enum tree_code cond_code;
3845 cond_code = fd->cond_code;
3846 cond_code = cond_code == LT_EXPR ? GE_EXPR : LE_EXPR;
3848 /* When possible, use a strict equality expression. This can let VRP
3849 type optimizations deduce the value and remove a copy. */
3850 if (host_integerp (fd->step, 0))
3852 HOST_WIDE_INT step = TREE_INT_CST_LOW (fd->step);
3853 if (step == 1 || step == -1)
3854 cond_code = EQ_EXPR;
3857 cond = build2 (cond_code, boolean_type_node, fd->v, fd->n2);
3859 clauses = OMP_FOR_CLAUSES (fd->for_stmt);
3860 lower_lastprivate_clauses (clauses, cond, body_p, ctx);
3864 /* Lower code for an OpenMP loop directive. */
3867 lower_omp_for (tree *stmt_p, omp_context *ctx)
3869 tree t, stmt, ilist, dlist, new_stmt, *body_p, *rhs_p;
3870 struct omp_for_data fd;
3874 push_gimplify_context ();
3876 lower_omp (&OMP_FOR_PRE_BODY (stmt), ctx);
3877 lower_omp (&OMP_FOR_BODY (stmt), ctx);
3879 /* Move declaration of temporaries in the loop body before we make
3881 if (TREE_CODE (OMP_FOR_BODY (stmt)) == BIND_EXPR)
3882 record_vars_into (BIND_EXPR_VARS (OMP_FOR_BODY (stmt)), ctx->cb.dst_fn);
3884 new_stmt = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3885 TREE_SIDE_EFFECTS (new_stmt) = 1;
3886 body_p = &BIND_EXPR_BODY (new_stmt);
3888 /* The pre-body and input clauses go before the lowered OMP_FOR. */
3891 append_to_statement_list (OMP_FOR_PRE_BODY (stmt), body_p);
3892 lower_rec_input_clauses (OMP_FOR_CLAUSES (stmt), body_p, &dlist, ctx);
3894 /* Lower the header expressions. At this point, we can assume that
3895 the header is of the form:
3897 #pragma omp for (V = VAL1; V {<|>|<=|>=} VAL2; V = V [+-] VAL3)
3899 We just need to make sure that VAL1, VAL2 and VAL3 are lowered
3900 using the .omp_data_s mapping, if needed. */
3901 rhs_p = &TREE_OPERAND (OMP_FOR_INIT (stmt), 1);
3902 if (!is_gimple_min_invariant (*rhs_p))
3903 *rhs_p = get_formal_tmp_var (*rhs_p, body_p);
3905 rhs_p = &TREE_OPERAND (OMP_FOR_COND (stmt), 1);
3906 if (!is_gimple_min_invariant (*rhs_p))
3907 *rhs_p = get_formal_tmp_var (*rhs_p, body_p);
3909 rhs_p = &TREE_OPERAND (TREE_OPERAND (OMP_FOR_INCR (stmt), 1), 1);
3910 if (!is_gimple_min_invariant (*rhs_p))
3911 *rhs_p = get_formal_tmp_var (*rhs_p, body_p);
3913 /* Once lowered, extract the bounds and clauses. */
3914 extract_omp_for_data (stmt, &fd);
3916 append_to_statement_list (stmt, body_p);
3918 append_to_statement_list (OMP_FOR_BODY (stmt), body_p);
3920 t = make_node (OMP_CONTINUE);
3921 append_to_statement_list (t, body_p);
3923 /* After the loop, add exit clauses. */
3924 lower_omp_for_lastprivate (&fd, &dlist, ctx);
3925 lower_reduction_clauses (OMP_FOR_CLAUSES (stmt), body_p, ctx);
3926 append_to_statement_list (dlist, body_p);
3928 maybe_catch_exception (body_p);
3930 /* Region exit marker goes at the end of the loop body. */
3931 t = make_node (OMP_RETURN);
3932 OMP_RETURN_NOWAIT (t) = fd.have_nowait;
3933 append_to_statement_list (t, body_p);
3935 pop_gimplify_context (NULL_TREE);
3936 record_vars_into (ctx->block_vars, ctx->cb.dst_fn);
3938 OMP_FOR_BODY (stmt) = NULL_TREE;
3939 OMP_FOR_PRE_BODY (stmt) = NULL_TREE;
3944 /* Lower the OpenMP parallel directive in *STMT_P. CTX holds context
3945 information for the directive. */
3948 lower_omp_parallel (tree *stmt_p, omp_context *ctx)
3950 tree clauses, par_bind, par_body, new_body, bind;
3951 tree olist, ilist, par_olist, par_ilist;
3952 tree stmt, child_fn, t;
3956 clauses = OMP_PARALLEL_CLAUSES (stmt);
3957 par_bind = OMP_PARALLEL_BODY (stmt);
3958 par_body = BIND_EXPR_BODY (par_bind);
3959 child_fn = ctx->cb.dst_fn;
3961 push_gimplify_context ();
3963 par_olist = NULL_TREE;
3964 par_ilist = NULL_TREE;
3965 lower_rec_input_clauses (clauses, &par_ilist, &par_olist, ctx);
3966 lower_omp (&par_body, ctx);
3967 lower_reduction_clauses (clauses, &par_olist, ctx);
3969 /* Declare all the variables created by mapping and the variables
3970 declared in the scope of the parallel body. */
3971 record_vars_into (ctx->block_vars, child_fn);
3972 record_vars_into (BIND_EXPR_VARS (par_bind), child_fn);
3974 if (ctx->record_type)
3976 ctx->sender_decl = create_tmp_var (ctx->record_type, ".omp_data_o");
3977 OMP_PARALLEL_DATA_ARG (stmt) = ctx->sender_decl;
3982 lower_send_clauses (clauses, &ilist, &olist, ctx);
3983 lower_send_shared_vars (&ilist, &olist, ctx);
3985 /* Once all the expansions are done, sequence all the different
3986 fragments inside OMP_PARALLEL_BODY. */
3987 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3988 append_to_statement_list (ilist, &BIND_EXPR_BODY (bind));
3990 new_body = alloc_stmt_list ();
3992 if (ctx->record_type)
3994 t = build_fold_addr_expr (ctx->sender_decl);
3995 t = build2 (MODIFY_EXPR, void_type_node, ctx->receiver_decl, t);
3996 append_to_statement_list (t, &new_body);
3999 append_to_statement_list (par_ilist, &new_body);
4000 append_to_statement_list (par_body, &new_body);
4001 append_to_statement_list (par_olist, &new_body);
4002 maybe_catch_exception (&new_body);
4003 t = make_node (OMP_RETURN);
4004 append_to_statement_list (t, &new_body);
4005 OMP_PARALLEL_BODY (stmt) = new_body;
4007 append_to_statement_list (stmt, &BIND_EXPR_BODY (bind));
4008 append_to_statement_list (olist, &BIND_EXPR_BODY (bind));
4012 pop_gimplify_context (NULL_TREE);
4016 /* Pass *TP back through the gimplifier within the context determined by WI.
4017 This handles replacement of DECL_VALUE_EXPR, as well as adjusting the
4018 flags on ADDR_EXPR. */
4021 lower_regimplify (tree *tp, struct walk_stmt_info *wi)
4023 enum gimplify_status gs;
4027 gs = gimplify_expr (tp, &pre, NULL, is_gimple_lvalue, fb_lvalue);
4028 else if (wi->val_only)
4029 gs = gimplify_expr (tp, &pre, NULL, is_gimple_val, fb_rvalue);
4031 gs = gimplify_expr (tp, &pre, NULL, is_gimple_formal_tmp_var, fb_rvalue);
4032 gcc_assert (gs == GS_ALL_DONE);
4035 tsi_link_before (&wi->tsi, pre, TSI_SAME_STMT);
4039 /* Callback for walk_stmts. Lower the OpenMP directive pointed by TP. */
4042 lower_omp_1 (tree *tp, int *walk_subtrees, void *data)
4044 struct walk_stmt_info *wi = data;
4045 omp_context *ctx = wi->info;
4048 /* If we have issued syntax errors, avoid doing any heavy lifting.
4049 Just replace the OpenMP directives with a NOP to avoid
4050 confusing RTL expansion. */
4051 if (errorcount && OMP_DIRECTIVE_P (*tp))
4053 *tp = build_empty_stmt ();
4058 switch (TREE_CODE (*tp))
4061 ctx = maybe_lookup_ctx (t);
4062 lower_omp_parallel (tp, ctx);
4066 ctx = maybe_lookup_ctx (t);
4068 lower_omp_for (tp, ctx);
4072 ctx = maybe_lookup_ctx (t);
4074 lower_omp_sections (tp, ctx);
4078 ctx = maybe_lookup_ctx (t);
4080 lower_omp_single (tp, ctx);
4084 ctx = maybe_lookup_ctx (t);
4086 lower_omp_master (tp, ctx);
4090 ctx = maybe_lookup_ctx (t);
4092 lower_omp_ordered (tp, ctx);
4096 ctx = maybe_lookup_ctx (t);
4098 lower_omp_critical (tp, ctx);
4102 if (ctx && DECL_HAS_VALUE_EXPR_P (t))
4103 lower_regimplify (tp, wi);
4108 lower_regimplify (tp, wi);
4112 case ARRAY_RANGE_REF:
4116 case VIEW_CONVERT_EXPR:
4118 lower_regimplify (tp, wi);
4125 wi->val_only = true;
4126 lower_regimplify (&TREE_OPERAND (t, 0), wi);
4131 if (!TYPE_P (t) && !DECL_P (t))
4140 lower_omp (tree *stmt_p, omp_context *ctx)
4142 struct walk_stmt_info wi;
4144 memset (&wi, 0, sizeof (wi));
4145 wi.callback = lower_omp_1;
4148 wi.want_locations = true;
4150 walk_stmts (&wi, stmt_p);
4153 /* Main entry point. */
4156 execute_lower_omp (void)
4158 all_contexts = splay_tree_new (splay_tree_compare_pointers, 0,
4159 delete_omp_context);
4161 scan_omp (&DECL_SAVED_TREE (current_function_decl), NULL);
4162 gcc_assert (parallel_nesting_level == 0);
4164 if (all_contexts->root)
4165 lower_omp (&DECL_SAVED_TREE (current_function_decl), NULL);
4169 splay_tree_delete (all_contexts);
4170 all_contexts = NULL;
4176 gate_lower_omp (void)
4178 return flag_openmp != 0;
4181 struct tree_opt_pass pass_lower_omp =
4183 "omplower", /* name */
4184 gate_lower_omp, /* gate */
4185 execute_lower_omp, /* execute */
4188 0, /* static_pass_number */
4190 PROP_gimple_any, /* properties_required */
4191 PROP_gimple_lomp, /* properties_provided */
4192 0, /* properties_destroyed */
4193 0, /* todo_flags_start */
4194 TODO_dump_func, /* todo_flags_finish */
4198 /* The following is a utility to diagnose OpenMP structured block violations.
4199 It is not part of the "omplower" pass, as that's invoked too late. It
4200 should be invoked by the respective front ends after gimplification. */
4202 static splay_tree all_labels;
4204 /* Check for mismatched contexts and generate an error if needed. Return
4205 true if an error is detected. */
4208 diagnose_sb_0 (tree *stmt_p, tree branch_ctx, tree label_ctx)
4212 if ((label_ctx ? TREE_VALUE (label_ctx) : NULL) == branch_ctx)
4215 /* Try to avoid confusing the user by producing and error message
4216 with correct "exit" or "enter" verbage. We prefer "exit"
4217 unless we can show that LABEL_CTX is nested within BRANCH_CTX. */
4218 if (branch_ctx == NULL)
4224 if (TREE_VALUE (label_ctx) == branch_ctx)
4229 label_ctx = TREE_CHAIN (label_ctx);
4234 error ("invalid exit from OpenMP structured block");
4236 error ("invalid entry to OpenMP structured block");
4238 *stmt_p = build_empty_stmt ();
4242 /* Pass 1: Create a minimal tree of OpenMP structured blocks, and record
4243 where in the tree each label is found. */
4246 diagnose_sb_1 (tree *tp, int *walk_subtrees, void *data)
4248 struct walk_stmt_info *wi = data;
4249 tree context = (tree) wi->info;
4254 switch (TREE_CODE (t))
4259 walk_tree (&OMP_CLAUSES (t), diagnose_sb_1, wi, NULL);
4265 /* The minimal context here is just a tree of statements. */
4266 inner_context = tree_cons (NULL, t, context);
4267 wi->info = inner_context;
4268 walk_stmts (wi, &OMP_BODY (t));
4273 walk_tree (&OMP_FOR_CLAUSES (t), diagnose_sb_1, wi, NULL);
4274 inner_context = tree_cons (NULL, t, context);
4275 wi->info = inner_context;
4276 walk_tree (&OMP_FOR_INIT (t), diagnose_sb_1, wi, NULL);
4277 walk_tree (&OMP_FOR_COND (t), diagnose_sb_1, wi, NULL);
4278 walk_tree (&OMP_FOR_INCR (t), diagnose_sb_1, wi, NULL);
4279 walk_stmts (wi, &OMP_FOR_PRE_BODY (t));
4280 walk_stmts (wi, &OMP_FOR_BODY (t));
4285 splay_tree_insert (all_labels, (splay_tree_key) LABEL_EXPR_LABEL (t),
4286 (splay_tree_value) context);
4296 /* Pass 2: Check each branch and see if its context differs from that of
4297 the destination label's context. */
4300 diagnose_sb_2 (tree *tp, int *walk_subtrees, void *data)
4302 struct walk_stmt_info *wi = data;
4303 tree context = (tree) wi->info;
4308 switch (TREE_CODE (t))
4313 walk_tree (&OMP_CLAUSES (t), diagnose_sb_2, wi, NULL);
4320 walk_stmts (wi, &OMP_BODY (t));
4325 walk_tree (&OMP_FOR_CLAUSES (t), diagnose_sb_2, wi, NULL);
4327 walk_tree (&OMP_FOR_INIT (t), diagnose_sb_2, wi, NULL);
4328 walk_tree (&OMP_FOR_COND (t), diagnose_sb_2, wi, NULL);
4329 walk_tree (&OMP_FOR_INCR (t), diagnose_sb_2, wi, NULL);
4330 walk_stmts (wi, &OMP_FOR_PRE_BODY (t));
4331 walk_stmts (wi, &OMP_FOR_BODY (t));
4337 tree lab = GOTO_DESTINATION (t);
4338 if (TREE_CODE (lab) != LABEL_DECL)
4341 n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
4342 diagnose_sb_0 (tp, context, n ? (tree) n->value : NULL_TREE);
4348 tree vec = SWITCH_LABELS (t);
4349 int i, len = TREE_VEC_LENGTH (vec);
4350 for (i = 0; i < len; ++i)
4352 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
4353 n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
4354 if (diagnose_sb_0 (tp, context, (tree) n->value))
4361 diagnose_sb_0 (tp, context, NULL_TREE);
4372 diagnose_omp_structured_block_errors (tree fndecl)
4374 tree save_current = current_function_decl;
4375 struct walk_stmt_info wi;
4377 current_function_decl = fndecl;
4379 all_labels = splay_tree_new (splay_tree_compare_pointers, 0, 0);
4381 memset (&wi, 0, sizeof (wi));
4382 wi.callback = diagnose_sb_1;
4383 walk_stmts (&wi, &DECL_SAVED_TREE (fndecl));
4385 memset (&wi, 0, sizeof (wi));
4386 wi.callback = diagnose_sb_2;
4387 wi.want_locations = true;
4388 wi.want_return_expr = true;
4389 walk_stmts (&wi, &DECL_SAVED_TREE (fndecl));
4391 splay_tree_delete (all_labels);
4394 current_function_decl = save_current;
4397 #include "gt-omp-low.h"