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, 2007 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"
44 #include "splay-tree.h"
47 /* Lowering of OpenMP parallel and workshare constructs proceeds in two
48 phases. The first phase scans the function looking for OMP statements
49 and then for variables that must be replaced to satisfy data sharing
50 clauses. The second phase expands code for the constructs, as well as
51 re-gimplifying things when variables have been replaced with complex
54 Final code generation is done by pass_expand_omp. The flowgraph is
55 scanned for parallel regions which are then moved to a new
56 function, to be invoked by the thread library. */
58 /* Context structure. Used to store information about each parallel
59 directive in the code. */
61 typedef struct omp_context
63 /* This field must be at the beginning, as we do "inheritance": Some
64 callback functions for tree-inline.c (e.g., omp_copy_decl)
65 receive a copy_body_data pointer that is up-casted to an
66 omp_context pointer. */
69 /* The tree of contexts corresponding to the encountered constructs. */
70 struct omp_context
*outer
;
73 /* Map variables to fields in a structure that allows communication
74 between sending and receiving threads. */
80 /* A chain of variables to add to the top-level block surrounding the
81 construct. In the case of a parallel, this is in the child function. */
84 /* What to do with variables with implicitly determined sharing
86 enum omp_clause_default_kind default_kind
;
88 /* Nesting depth of this context. Used to beautify error messages re
89 invalid gotos. The outermost ctx is depth 1, with depth 0 being
90 reserved for the main body of the function. */
93 /* True if this parallel directive is nested within another. */
98 /* A structure describing the main elements of a parallel loop. */
102 tree v
, n1
, n2
, step
, chunk_size
, for_stmt
;
103 enum tree_code cond_code
;
105 bool have_nowait
, have_ordered
;
106 enum omp_clause_schedule_kind sched_kind
;
110 static splay_tree all_contexts
;
111 static int parallel_nesting_level
;
112 struct omp_region
*root_omp_region
;
114 static void scan_omp (tree
*, omp_context
*);
115 static void lower_omp (tree
*, omp_context
*);
116 static tree
lookup_decl_in_outer_ctx (tree
, omp_context
*);
117 static tree
maybe_lookup_decl_in_outer_ctx (tree
, omp_context
*);
119 /* Find an OpenMP clause of type KIND within CLAUSES. */
122 find_omp_clause (tree clauses
, enum tree_code kind
)
124 for (; clauses
; clauses
= OMP_CLAUSE_CHAIN (clauses
))
125 if (OMP_CLAUSE_CODE (clauses
) == kind
)
131 /* Return true if CTX is for an omp parallel. */
134 is_parallel_ctx (omp_context
*ctx
)
136 return TREE_CODE (ctx
->stmt
) == OMP_PARALLEL
;
140 /* Return true if REGION is a combined parallel+workshare region. */
143 is_combined_parallel (struct omp_region
*region
)
145 return region
->is_combined_parallel
;
149 /* Extract the header elements of parallel loop FOR_STMT and store
153 extract_omp_for_data (tree for_stmt
, struct omp_for_data
*fd
)
157 fd
->for_stmt
= for_stmt
;
160 t
= OMP_FOR_INIT (for_stmt
);
161 gcc_assert (TREE_CODE (t
) == GIMPLE_MODIFY_STMT
);
162 fd
->v
= GIMPLE_STMT_OPERAND (t
, 0);
163 gcc_assert (DECL_P (fd
->v
));
164 gcc_assert (TREE_CODE (TREE_TYPE (fd
->v
)) == INTEGER_TYPE
);
165 fd
->n1
= GIMPLE_STMT_OPERAND (t
, 1);
167 t
= OMP_FOR_COND (for_stmt
);
168 fd
->cond_code
= TREE_CODE (t
);
169 gcc_assert (TREE_OPERAND (t
, 0) == fd
->v
);
170 fd
->n2
= TREE_OPERAND (t
, 1);
171 switch (fd
->cond_code
)
177 fd
->n2
= fold_build2 (PLUS_EXPR
, TREE_TYPE (fd
->n2
), fd
->n2
,
178 build_int_cst (TREE_TYPE (fd
->n2
), 1));
179 fd
->cond_code
= LT_EXPR
;
182 fd
->n2
= fold_build2 (MINUS_EXPR
, TREE_TYPE (fd
->n2
), fd
->n2
,
183 build_int_cst (TREE_TYPE (fd
->n2
), 1));
184 fd
->cond_code
= GT_EXPR
;
190 t
= OMP_FOR_INCR (fd
->for_stmt
);
191 gcc_assert (TREE_CODE (t
) == GIMPLE_MODIFY_STMT
);
192 gcc_assert (GIMPLE_STMT_OPERAND (t
, 0) == fd
->v
);
193 t
= GIMPLE_STMT_OPERAND (t
, 1);
194 gcc_assert (TREE_OPERAND (t
, 0) == fd
->v
);
195 switch (TREE_CODE (t
))
198 fd
->step
= TREE_OPERAND (t
, 1);
201 fd
->step
= TREE_OPERAND (t
, 1);
202 fd
->step
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (fd
->step
), fd
->step
);
208 fd
->have_nowait
= fd
->have_ordered
= false;
209 fd
->sched_kind
= OMP_CLAUSE_SCHEDULE_STATIC
;
210 fd
->chunk_size
= NULL_TREE
;
212 for (t
= OMP_FOR_CLAUSES (for_stmt
); t
; t
= OMP_CLAUSE_CHAIN (t
))
213 switch (OMP_CLAUSE_CODE (t
))
215 case OMP_CLAUSE_NOWAIT
:
216 fd
->have_nowait
= true;
218 case OMP_CLAUSE_ORDERED
:
219 fd
->have_ordered
= true;
221 case OMP_CLAUSE_SCHEDULE
:
222 fd
->sched_kind
= OMP_CLAUSE_SCHEDULE_KIND (t
);
223 fd
->chunk_size
= OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (t
);
229 if (fd
->sched_kind
== OMP_CLAUSE_SCHEDULE_RUNTIME
)
230 gcc_assert (fd
->chunk_size
== NULL
);
231 else if (fd
->chunk_size
== NULL
)
233 /* We only need to compute a default chunk size for ordered
234 static loops and dynamic loops. */
235 if (fd
->sched_kind
!= OMP_CLAUSE_SCHEDULE_STATIC
|| fd
->have_ordered
)
236 fd
->chunk_size
= (fd
->sched_kind
== OMP_CLAUSE_SCHEDULE_STATIC
)
237 ? integer_zero_node
: integer_one_node
;
242 /* Given two blocks PAR_ENTRY_BB and WS_ENTRY_BB such that WS_ENTRY_BB
243 is the immediate dominator of PAR_ENTRY_BB, return true if there
244 are no data dependencies that would prevent expanding the parallel
245 directive at PAR_ENTRY_BB as a combined parallel+workshare region.
247 When expanding a combined parallel+workshare region, the call to
248 the child function may need additional arguments in the case of
249 OMP_FOR regions. In some cases, these arguments are computed out
250 of variables passed in from the parent to the child via 'struct
251 .omp_data_s'. For instance:
253 #pragma omp parallel for schedule (guided, i * 4)
258 # BLOCK 2 (PAR_ENTRY_BB)
260 #pragma omp parallel [child fn: bar.omp_fn.0 ( ..., D.1598)
262 # BLOCK 3 (WS_ENTRY_BB)
263 .omp_data_i = &.omp_data_o;
264 D.1667 = .omp_data_i->i;
266 #pragma omp for schedule (guided, D.1598)
268 When we outline the parallel region, the call to the child function
269 'bar.omp_fn.0' will need the value D.1598 in its argument list, but
270 that value is computed *after* the call site. So, in principle we
271 cannot do the transformation.
273 To see whether the code in WS_ENTRY_BB blocks the combined
274 parallel+workshare call, we collect all the variables used in the
275 OMP_FOR header check whether they appear on the LHS of any
276 statement in WS_ENTRY_BB. If so, then we cannot emit the combined
279 FIXME. If we had the SSA form built at this point, we could merely
280 hoist the code in block 3 into block 2 and be done with it. But at
281 this point we don't have dataflow information and though we could
282 hack something up here, it is really not worth the aggravation. */
285 workshare_safe_to_combine_p (basic_block par_entry_bb
, basic_block ws_entry_bb
)
287 struct omp_for_data fd
;
288 tree par_stmt
, ws_stmt
;
290 par_stmt
= last_stmt (par_entry_bb
);
291 ws_stmt
= last_stmt (ws_entry_bb
);
293 if (TREE_CODE (ws_stmt
) == OMP_SECTIONS
)
296 gcc_assert (TREE_CODE (ws_stmt
) == OMP_FOR
);
298 extract_omp_for_data (ws_stmt
, &fd
);
300 /* FIXME. We give up too easily here. If any of these arguments
301 are not constants, they will likely involve variables that have
302 been mapped into fields of .omp_data_s for sharing with the child
303 function. With appropriate data flow, it would be possible to
305 if (!is_gimple_min_invariant (fd
.n1
)
306 || !is_gimple_min_invariant (fd
.n2
)
307 || !is_gimple_min_invariant (fd
.step
)
308 || (fd
.chunk_size
&& !is_gimple_min_invariant (fd
.chunk_size
)))
315 /* Collect additional arguments needed to emit a combined
316 parallel+workshare call. WS_STMT is the workshare directive being
320 get_ws_args_for (tree ws_stmt
)
324 if (TREE_CODE (ws_stmt
) == OMP_FOR
)
326 struct omp_for_data fd
;
329 extract_omp_for_data (ws_stmt
, &fd
);
334 t
= fold_convert (long_integer_type_node
, fd
.chunk_size
);
335 ws_args
= tree_cons (NULL
, t
, ws_args
);
338 t
= fold_convert (long_integer_type_node
, fd
.step
);
339 ws_args
= tree_cons (NULL
, t
, ws_args
);
341 t
= fold_convert (long_integer_type_node
, fd
.n2
);
342 ws_args
= tree_cons (NULL
, t
, ws_args
);
344 t
= fold_convert (long_integer_type_node
, fd
.n1
);
345 ws_args
= tree_cons (NULL
, t
, ws_args
);
349 else if (TREE_CODE (ws_stmt
) == OMP_SECTIONS
)
351 basic_block bb
= bb_for_stmt (ws_stmt
);
352 t
= build_int_cst (unsigned_type_node
, EDGE_COUNT (bb
->succs
));
353 t
= tree_cons (NULL
, t
, NULL
);
361 /* Discover whether REGION is a combined parallel+workshare region. */
364 determine_parallel_type (struct omp_region
*region
)
366 basic_block par_entry_bb
, par_exit_bb
;
367 basic_block ws_entry_bb
, ws_exit_bb
;
369 if (region
== NULL
|| region
->inner
== NULL
370 || region
->exit
== NULL
|| region
->inner
->exit
== NULL
)
373 /* We only support parallel+for and parallel+sections. */
374 if (region
->type
!= OMP_PARALLEL
375 || (region
->inner
->type
!= OMP_FOR
376 && region
->inner
->type
!= OMP_SECTIONS
))
379 /* Check for perfect nesting PAR_ENTRY_BB -> WS_ENTRY_BB and
380 WS_EXIT_BB -> PAR_EXIT_BB. */
381 par_entry_bb
= region
->entry
;
382 par_exit_bb
= region
->exit
;
383 ws_entry_bb
= region
->inner
->entry
;
384 ws_exit_bb
= region
->inner
->exit
;
386 if (single_succ (par_entry_bb
) == ws_entry_bb
387 && single_succ (ws_exit_bb
) == par_exit_bb
388 && workshare_safe_to_combine_p (par_entry_bb
, ws_entry_bb
))
390 tree ws_stmt
= last_stmt (region
->inner
->entry
);
392 if (region
->inner
->type
== OMP_FOR
)
394 /* If this is a combined parallel loop, we need to determine
395 whether or not to use the combined library calls. There
396 are two cases where we do not apply the transformation:
397 static loops and any kind of ordered loop. In the first
398 case, we already open code the loop so there is no need
399 to do anything else. In the latter case, the combined
400 parallel loop call would still need extra synchronization
401 to implement ordered semantics, so there would not be any
402 gain in using the combined call. */
403 tree clauses
= OMP_FOR_CLAUSES (ws_stmt
);
404 tree c
= find_omp_clause (clauses
, OMP_CLAUSE_SCHEDULE
);
406 || OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_STATIC
407 || find_omp_clause (clauses
, OMP_CLAUSE_ORDERED
))
409 region
->is_combined_parallel
= false;
410 region
->inner
->is_combined_parallel
= false;
415 region
->is_combined_parallel
= true;
416 region
->inner
->is_combined_parallel
= true;
417 region
->ws_args
= get_ws_args_for (ws_stmt
);
422 /* Return true if EXPR is variable sized. */
425 is_variable_sized (tree expr
)
427 return !TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
430 /* Return true if DECL is a reference type. */
433 is_reference (tree decl
)
435 return lang_hooks
.decls
.omp_privatize_by_reference (decl
);
438 /* Lookup variables in the decl or field splay trees. The "maybe" form
439 allows for the variable form to not have been entered, otherwise we
440 assert that the variable must have been entered. */
443 lookup_decl (tree var
, omp_context
*ctx
)
446 n
= (tree
*) pointer_map_contains (ctx
->cb
.decl_map
, var
);
451 maybe_lookup_decl (tree var
, omp_context
*ctx
)
454 n
= (tree
*) pointer_map_contains (ctx
->cb
.decl_map
, var
);
455 return n
? *n
: NULL_TREE
;
459 lookup_field (tree var
, omp_context
*ctx
)
462 n
= splay_tree_lookup (ctx
->field_map
, (splay_tree_key
) var
);
463 return (tree
) n
->value
;
467 maybe_lookup_field (tree var
, omp_context
*ctx
)
470 n
= splay_tree_lookup (ctx
->field_map
, (splay_tree_key
) var
);
471 return n
? (tree
) n
->value
: NULL_TREE
;
474 /* Return true if DECL should be copied by pointer. SHARED_P is true
475 if DECL is to be shared. */
478 use_pointer_for_field (tree decl
, bool shared_p
)
480 if (AGGREGATE_TYPE_P (TREE_TYPE (decl
)))
483 /* We can only use copy-in/copy-out semantics for shared variables
484 when we know the value is not accessible from an outer scope. */
487 /* ??? Trivially accessible from anywhere. But why would we even
488 be passing an address in this case? Should we simply assert
489 this to be false, or should we have a cleanup pass that removes
490 these from the list of mappings? */
491 if (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
))
494 /* For variables with DECL_HAS_VALUE_EXPR_P set, we cannot tell
495 without analyzing the expression whether or not its location
496 is accessible to anyone else. In the case of nested parallel
497 regions it certainly may be. */
498 if (TREE_CODE (decl
) != RESULT_DECL
&& DECL_HAS_VALUE_EXPR_P (decl
))
501 /* Do not use copy-in/copy-out for variables that have their
503 if (TREE_ADDRESSABLE (decl
))
510 /* Construct a new automatic decl similar to VAR. */
513 omp_copy_decl_2 (tree var
, tree name
, tree type
, omp_context
*ctx
)
515 tree copy
= build_decl (VAR_DECL
, name
, type
);
517 TREE_ADDRESSABLE (copy
) = TREE_ADDRESSABLE (var
);
518 DECL_GIMPLE_REG_P (copy
) = DECL_GIMPLE_REG_P (var
);
519 DECL_ARTIFICIAL (copy
) = DECL_ARTIFICIAL (var
);
520 DECL_IGNORED_P (copy
) = DECL_IGNORED_P (var
);
521 TREE_USED (copy
) = 1;
522 DECL_CONTEXT (copy
) = current_function_decl
;
523 DECL_SEEN_IN_BIND_EXPR_P (copy
) = 1;
525 TREE_CHAIN (copy
) = ctx
->block_vars
;
526 ctx
->block_vars
= copy
;
532 omp_copy_decl_1 (tree var
, omp_context
*ctx
)
534 return omp_copy_decl_2 (var
, DECL_NAME (var
), TREE_TYPE (var
), ctx
);
537 /* Build tree nodes to access the field for VAR on the receiver side. */
540 build_receiver_ref (tree var
, bool by_ref
, omp_context
*ctx
)
542 tree x
, field
= lookup_field (var
, ctx
);
544 /* If the receiver record type was remapped in the child function,
545 remap the field into the new record type. */
546 x
= maybe_lookup_field (field
, ctx
);
550 x
= build_fold_indirect_ref (ctx
->receiver_decl
);
551 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL
);
553 x
= build_fold_indirect_ref (x
);
558 /* Build tree nodes to access VAR in the scope outer to CTX. In the case
559 of a parallel, this is a component reference; for workshare constructs
560 this is some variable. */
563 build_outer_var_ref (tree var
, omp_context
*ctx
)
567 if (is_global_var (maybe_lookup_decl_in_outer_ctx (var
, ctx
)))
569 else if (is_variable_sized (var
))
571 x
= TREE_OPERAND (DECL_VALUE_EXPR (var
), 0);
572 x
= build_outer_var_ref (x
, ctx
);
573 x
= build_fold_indirect_ref (x
);
575 else if (is_parallel_ctx (ctx
))
577 bool by_ref
= use_pointer_for_field (var
, false);
578 x
= build_receiver_ref (var
, by_ref
, ctx
);
581 x
= lookup_decl (var
, ctx
->outer
);
582 else if (is_reference (var
))
583 /* This can happen with orphaned constructs. If var is reference, it is
584 possible it is shared and as such valid. */
589 if (is_reference (var
))
590 x
= build_fold_indirect_ref (x
);
595 /* Build tree nodes to access the field for VAR on the sender side. */
598 build_sender_ref (tree var
, omp_context
*ctx
)
600 tree field
= lookup_field (var
, ctx
);
601 return build3 (COMPONENT_REF
, TREE_TYPE (field
),
602 ctx
->sender_decl
, field
, NULL
);
605 /* Add a new field for VAR inside the structure CTX->SENDER_DECL. */
608 install_var_field (tree var
, bool by_ref
, omp_context
*ctx
)
612 gcc_assert (!splay_tree_lookup (ctx
->field_map
, (splay_tree_key
) var
));
614 type
= TREE_TYPE (var
);
616 type
= build_pointer_type (type
);
618 field
= build_decl (FIELD_DECL
, DECL_NAME (var
), type
);
620 /* Remember what variable this field was created for. This does have a
621 side effect of making dwarf2out ignore this member, so for helpful
622 debugging we clear it later in delete_omp_context. */
623 DECL_ABSTRACT_ORIGIN (field
) = var
;
625 insert_field_into_struct (ctx
->record_type
, field
);
627 splay_tree_insert (ctx
->field_map
, (splay_tree_key
) var
,
628 (splay_tree_value
) field
);
632 install_var_local (tree var
, omp_context
*ctx
)
634 tree new_var
= omp_copy_decl_1 (var
, ctx
);
635 insert_decl_map (&ctx
->cb
, var
, new_var
);
639 /* Adjust the replacement for DECL in CTX for the new context. This means
640 copying the DECL_VALUE_EXPR, and fixing up the type. */
643 fixup_remapped_decl (tree decl
, omp_context
*ctx
, bool private_debug
)
647 new_decl
= lookup_decl (decl
, ctx
);
649 TREE_TYPE (new_decl
) = remap_type (TREE_TYPE (decl
), &ctx
->cb
);
651 if ((!TREE_CONSTANT (DECL_SIZE (new_decl
)) || private_debug
)
652 && DECL_HAS_VALUE_EXPR_P (decl
))
654 tree ve
= DECL_VALUE_EXPR (decl
);
655 walk_tree (&ve
, copy_body_r
, &ctx
->cb
, NULL
);
656 SET_DECL_VALUE_EXPR (new_decl
, ve
);
657 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
660 if (!TREE_CONSTANT (DECL_SIZE (new_decl
)))
662 size
= remap_decl (DECL_SIZE (decl
), &ctx
->cb
);
663 if (size
== error_mark_node
)
664 size
= TYPE_SIZE (TREE_TYPE (new_decl
));
665 DECL_SIZE (new_decl
) = size
;
667 size
= remap_decl (DECL_SIZE_UNIT (decl
), &ctx
->cb
);
668 if (size
== error_mark_node
)
669 size
= TYPE_SIZE_UNIT (TREE_TYPE (new_decl
));
670 DECL_SIZE_UNIT (new_decl
) = size
;
674 /* The callback for remap_decl. Search all containing contexts for a
675 mapping of the variable; this avoids having to duplicate the splay
676 tree ahead of time. We know a mapping doesn't already exist in the
677 given context. Create new mappings to implement default semantics. */
680 omp_copy_decl (tree var
, copy_body_data
*cb
)
682 omp_context
*ctx
= (omp_context
*) cb
;
685 if (TREE_CODE (var
) == LABEL_DECL
)
687 new_var
= create_artificial_label ();
688 DECL_CONTEXT (new_var
) = current_function_decl
;
689 insert_decl_map (&ctx
->cb
, var
, new_var
);
693 while (!is_parallel_ctx (ctx
))
698 new_var
= maybe_lookup_decl (var
, ctx
);
703 if (is_global_var (var
) || decl_function_context (var
) != ctx
->cb
.src_fn
)
706 return error_mark_node
;
710 /* Return the parallel region associated with STMT. */
712 /* Debugging dumps for parallel regions. */
713 void dump_omp_region (FILE *, struct omp_region
*, int);
714 void debug_omp_region (struct omp_region
*);
715 void debug_all_omp_regions (void);
717 /* Dump the parallel region tree rooted at REGION. */
720 dump_omp_region (FILE *file
, struct omp_region
*region
, int indent
)
722 fprintf (file
, "%*sbb %d: %s\n", indent
, "", region
->entry
->index
,
723 tree_code_name
[region
->type
]);
726 dump_omp_region (file
, region
->inner
, indent
+ 4);
730 fprintf (file
, "%*sbb %d: OMP_CONTINUE\n", indent
, "",
731 region
->cont
->index
);
735 fprintf (file
, "%*sbb %d: OMP_RETURN\n", indent
, "",
736 region
->exit
->index
);
738 fprintf (file
, "%*s[no exit marker]\n", indent
, "");
741 dump_omp_region (file
, region
->next
, indent
);
745 debug_omp_region (struct omp_region
*region
)
747 dump_omp_region (stderr
, region
, 0);
751 debug_all_omp_regions (void)
753 dump_omp_region (stderr
, root_omp_region
, 0);
757 /* Create a new parallel region starting at STMT inside region PARENT. */
760 new_omp_region (basic_block bb
, enum tree_code type
, struct omp_region
*parent
)
762 struct omp_region
*region
= xcalloc (1, sizeof (*region
));
764 region
->outer
= parent
;
770 /* This is a nested region. Add it to the list of inner
771 regions in PARENT. */
772 region
->next
= parent
->inner
;
773 parent
->inner
= region
;
777 /* This is a toplevel region. Add it to the list of toplevel
778 regions in ROOT_OMP_REGION. */
779 region
->next
= root_omp_region
;
780 root_omp_region
= region
;
786 /* Release the memory associated with the region tree rooted at REGION. */
789 free_omp_region_1 (struct omp_region
*region
)
791 struct omp_region
*i
, *n
;
793 for (i
= region
->inner
; i
; i
= n
)
796 free_omp_region_1 (i
);
802 /* Release the memory for the entire omp region tree. */
805 free_omp_regions (void)
807 struct omp_region
*r
, *n
;
808 for (r
= root_omp_region
; r
; r
= n
)
811 free_omp_region_1 (r
);
813 root_omp_region
= NULL
;
817 /* Create a new context, with OUTER_CTX being the surrounding context. */
820 new_omp_context (tree stmt
, omp_context
*outer_ctx
)
822 omp_context
*ctx
= XCNEW (omp_context
);
824 splay_tree_insert (all_contexts
, (splay_tree_key
) stmt
,
825 (splay_tree_value
) ctx
);
830 ctx
->outer
= outer_ctx
;
831 ctx
->cb
= outer_ctx
->cb
;
832 ctx
->cb
.block
= NULL
;
833 ctx
->depth
= outer_ctx
->depth
+ 1;
837 ctx
->cb
.src_fn
= current_function_decl
;
838 ctx
->cb
.dst_fn
= current_function_decl
;
839 ctx
->cb
.src_node
= cgraph_node (current_function_decl
);
840 ctx
->cb
.dst_node
= ctx
->cb
.src_node
;
841 ctx
->cb
.src_cfun
= cfun
;
842 ctx
->cb
.copy_decl
= omp_copy_decl
;
843 ctx
->cb
.eh_region
= -1;
844 ctx
->cb
.transform_call_graph_edges
= CB_CGE_MOVE
;
848 ctx
->cb
.decl_map
= pointer_map_create ();
853 /* Destroy a omp_context data structures. Called through the splay tree
854 value delete callback. */
857 delete_omp_context (splay_tree_value value
)
859 omp_context
*ctx
= (omp_context
*) value
;
861 pointer_map_destroy (ctx
->cb
.decl_map
);
864 splay_tree_delete (ctx
->field_map
);
866 /* We hijacked DECL_ABSTRACT_ORIGIN earlier. We need to clear it before
867 it produces corrupt debug information. */
868 if (ctx
->record_type
)
871 for (t
= TYPE_FIELDS (ctx
->record_type
); t
; t
= TREE_CHAIN (t
))
872 DECL_ABSTRACT_ORIGIN (t
) = NULL
;
878 /* Fix up RECEIVER_DECL with a type that has been remapped to the child
882 fixup_child_record_type (omp_context
*ctx
)
884 tree f
, type
= ctx
->record_type
;
886 /* ??? It isn't sufficient to just call remap_type here, because
887 variably_modified_type_p doesn't work the way we expect for
888 record types. Testing each field for whether it needs remapping
889 and creating a new record by hand works, however. */
890 for (f
= TYPE_FIELDS (type
); f
; f
= TREE_CHAIN (f
))
891 if (variably_modified_type_p (TREE_TYPE (f
), ctx
->cb
.src_fn
))
895 tree name
, new_fields
= NULL
;
897 type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
898 name
= DECL_NAME (TYPE_NAME (ctx
->record_type
));
899 name
= build_decl (TYPE_DECL
, name
, type
);
900 TYPE_NAME (type
) = name
;
902 for (f
= TYPE_FIELDS (ctx
->record_type
); f
; f
= TREE_CHAIN (f
))
904 tree new_f
= copy_node (f
);
905 DECL_CONTEXT (new_f
) = type
;
906 TREE_TYPE (new_f
) = remap_type (TREE_TYPE (f
), &ctx
->cb
);
907 TREE_CHAIN (new_f
) = new_fields
;
910 /* Arrange to be able to look up the receiver field
911 given the sender field. */
912 splay_tree_insert (ctx
->field_map
, (splay_tree_key
) f
,
913 (splay_tree_value
) new_f
);
915 TYPE_FIELDS (type
) = nreverse (new_fields
);
919 TREE_TYPE (ctx
->receiver_decl
) = build_pointer_type (type
);
922 /* Instantiate decls as necessary in CTX to satisfy the data sharing
923 specified by CLAUSES. */
926 scan_sharing_clauses (tree clauses
, omp_context
*ctx
)
929 bool scan_array_reductions
= false;
931 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
935 switch (OMP_CLAUSE_CODE (c
))
937 case OMP_CLAUSE_PRIVATE
:
938 decl
= OMP_CLAUSE_DECL (c
);
939 if (!is_variable_sized (decl
))
940 install_var_local (decl
, ctx
);
943 case OMP_CLAUSE_SHARED
:
944 gcc_assert (is_parallel_ctx (ctx
));
945 decl
= OMP_CLAUSE_DECL (c
);
946 gcc_assert (!is_variable_sized (decl
));
947 by_ref
= use_pointer_for_field (decl
, true);
948 /* Global variables don't need to be copied,
949 the receiver side will use them directly. */
950 if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl
, ctx
)))
952 if (! TREE_READONLY (decl
)
953 || TREE_ADDRESSABLE (decl
)
955 || is_reference (decl
))
957 install_var_field (decl
, by_ref
, ctx
);
958 install_var_local (decl
, ctx
);
961 /* We don't need to copy const scalar vars back. */
962 OMP_CLAUSE_SET_CODE (c
, OMP_CLAUSE_FIRSTPRIVATE
);
965 case OMP_CLAUSE_LASTPRIVATE
:
966 /* Let the corresponding firstprivate clause create
968 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
972 case OMP_CLAUSE_FIRSTPRIVATE
:
973 case OMP_CLAUSE_REDUCTION
:
974 decl
= OMP_CLAUSE_DECL (c
);
976 if (is_variable_sized (decl
))
978 else if (is_parallel_ctx (ctx
)
979 && ! is_global_var (maybe_lookup_decl_in_outer_ctx (decl
,
982 by_ref
= use_pointer_for_field (decl
, false);
983 install_var_field (decl
, by_ref
, ctx
);
985 install_var_local (decl
, ctx
);
988 case OMP_CLAUSE_COPYPRIVATE
:
990 scan_omp (&OMP_CLAUSE_DECL (c
), ctx
->outer
);
993 case OMP_CLAUSE_COPYIN
:
994 decl
= OMP_CLAUSE_DECL (c
);
995 by_ref
= use_pointer_for_field (decl
, false);
996 install_var_field (decl
, by_ref
, ctx
);
999 case OMP_CLAUSE_DEFAULT
:
1000 ctx
->default_kind
= OMP_CLAUSE_DEFAULT_KIND (c
);
1004 case OMP_CLAUSE_NUM_THREADS
:
1005 case OMP_CLAUSE_SCHEDULE
:
1007 scan_omp (&OMP_CLAUSE_OPERAND (c
, 0), ctx
->outer
);
1010 case OMP_CLAUSE_NOWAIT
:
1011 case OMP_CLAUSE_ORDERED
:
1019 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
1021 switch (OMP_CLAUSE_CODE (c
))
1023 case OMP_CLAUSE_LASTPRIVATE
:
1024 /* Let the corresponding firstprivate clause create
1026 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
1030 case OMP_CLAUSE_PRIVATE
:
1031 case OMP_CLAUSE_FIRSTPRIVATE
:
1032 case OMP_CLAUSE_REDUCTION
:
1033 decl
= OMP_CLAUSE_DECL (c
);
1034 if (is_variable_sized (decl
))
1035 install_var_local (decl
, ctx
);
1036 fixup_remapped_decl (decl
, ctx
,
1037 OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
1038 && OMP_CLAUSE_PRIVATE_DEBUG (c
));
1039 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
1040 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
1041 scan_array_reductions
= true;
1044 case OMP_CLAUSE_SHARED
:
1045 decl
= OMP_CLAUSE_DECL (c
);
1046 if (! is_global_var (maybe_lookup_decl_in_outer_ctx (decl
, ctx
)))
1047 fixup_remapped_decl (decl
, ctx
, false);
1050 case OMP_CLAUSE_COPYPRIVATE
:
1051 case OMP_CLAUSE_COPYIN
:
1052 case OMP_CLAUSE_DEFAULT
:
1054 case OMP_CLAUSE_NUM_THREADS
:
1055 case OMP_CLAUSE_SCHEDULE
:
1056 case OMP_CLAUSE_NOWAIT
:
1057 case OMP_CLAUSE_ORDERED
:
1065 if (scan_array_reductions
)
1066 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
1067 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
1068 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
1070 scan_omp (&OMP_CLAUSE_REDUCTION_INIT (c
), ctx
);
1071 scan_omp (&OMP_CLAUSE_REDUCTION_MERGE (c
), ctx
);
1075 /* Create a new name for omp child function. Returns an identifier. */
1077 static GTY(()) unsigned int tmp_ompfn_id_num
;
1080 create_omp_child_function_name (void)
1082 tree name
= DECL_ASSEMBLER_NAME (current_function_decl
);
1083 size_t len
= IDENTIFIER_LENGTH (name
);
1084 char *tmp_name
, *prefix
;
1086 prefix
= alloca (len
+ sizeof ("_omp_fn"));
1087 memcpy (prefix
, IDENTIFIER_POINTER (name
), len
);
1088 strcpy (prefix
+ len
, "_omp_fn");
1089 #ifndef NO_DOT_IN_LABEL
1091 #elif !defined NO_DOLLAR_IN_LABEL
1094 ASM_FORMAT_PRIVATE_NAME (tmp_name
, prefix
, tmp_ompfn_id_num
++);
1095 return get_identifier (tmp_name
);
1098 /* Build a decl for the omp child function. It'll not contain a body
1099 yet, just the bare decl. */
1102 create_omp_child_function (omp_context
*ctx
)
1104 tree decl
, type
, name
, t
;
1106 name
= create_omp_child_function_name ();
1107 type
= build_function_type_list (void_type_node
, ptr_type_node
, NULL_TREE
);
1109 decl
= build_decl (FUNCTION_DECL
, name
, type
);
1110 decl
= lang_hooks
.decls
.pushdecl (decl
);
1112 ctx
->cb
.dst_fn
= decl
;
1114 TREE_STATIC (decl
) = 1;
1115 TREE_USED (decl
) = 1;
1116 DECL_ARTIFICIAL (decl
) = 1;
1117 DECL_IGNORED_P (decl
) = 0;
1118 TREE_PUBLIC (decl
) = 0;
1119 DECL_UNINLINABLE (decl
) = 1;
1120 DECL_EXTERNAL (decl
) = 0;
1121 DECL_CONTEXT (decl
) = NULL_TREE
;
1122 DECL_INITIAL (decl
) = make_node (BLOCK
);
1124 t
= build_decl (RESULT_DECL
, NULL_TREE
, void_type_node
);
1125 DECL_ARTIFICIAL (t
) = 1;
1126 DECL_IGNORED_P (t
) = 1;
1127 DECL_RESULT (decl
) = t
;
1129 t
= build_decl (PARM_DECL
, get_identifier (".omp_data_i"), ptr_type_node
);
1130 DECL_ARTIFICIAL (t
) = 1;
1131 DECL_ARG_TYPE (t
) = ptr_type_node
;
1132 DECL_CONTEXT (t
) = current_function_decl
;
1134 DECL_ARGUMENTS (decl
) = t
;
1135 ctx
->receiver_decl
= t
;
1137 /* Allocate memory for the function structure. The call to
1138 allocate_struct_function clobbers CFUN, so we need to restore
1140 allocate_struct_function (decl
);
1141 DECL_SOURCE_LOCATION (decl
) = EXPR_LOCATION (ctx
->stmt
);
1142 cfun
->function_end_locus
= EXPR_LOCATION (ctx
->stmt
);
1143 cfun
= ctx
->cb
.src_cfun
;
1147 /* Scan an OpenMP parallel directive. */
1150 scan_omp_parallel (tree
*stmt_p
, omp_context
*outer_ctx
)
1155 /* Ignore parallel directives with empty bodies, unless there
1156 are copyin clauses. */
1158 && empty_body_p (OMP_PARALLEL_BODY (*stmt_p
))
1159 && find_omp_clause (OMP_CLAUSES (*stmt_p
), OMP_CLAUSE_COPYIN
) == NULL
)
1161 *stmt_p
= build_empty_stmt ();
1165 ctx
= new_omp_context (*stmt_p
, outer_ctx
);
1166 if (parallel_nesting_level
> 1)
1167 ctx
->is_nested
= true;
1168 ctx
->field_map
= splay_tree_new (splay_tree_compare_pointers
, 0, 0);
1169 ctx
->default_kind
= OMP_CLAUSE_DEFAULT_SHARED
;
1170 ctx
->record_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1171 name
= create_tmp_var_name (".omp_data_s");
1172 name
= build_decl (TYPE_DECL
, name
, ctx
->record_type
);
1173 TYPE_NAME (ctx
->record_type
) = name
;
1174 create_omp_child_function (ctx
);
1175 OMP_PARALLEL_FN (*stmt_p
) = ctx
->cb
.dst_fn
;
1177 scan_sharing_clauses (OMP_PARALLEL_CLAUSES (*stmt_p
), ctx
);
1178 scan_omp (&OMP_PARALLEL_BODY (*stmt_p
), ctx
);
1180 if (TYPE_FIELDS (ctx
->record_type
) == NULL
)
1181 ctx
->record_type
= ctx
->receiver_decl
= NULL
;
1184 layout_type (ctx
->record_type
);
1185 fixup_child_record_type (ctx
);
1190 /* Scan an OpenMP loop directive. */
1193 scan_omp_for (tree
*stmt_p
, omp_context
*outer_ctx
)
1199 ctx
= new_omp_context (stmt
, outer_ctx
);
1201 scan_sharing_clauses (OMP_FOR_CLAUSES (stmt
), ctx
);
1203 scan_omp (&OMP_FOR_PRE_BODY (stmt
), ctx
);
1204 scan_omp (&OMP_FOR_INIT (stmt
), ctx
);
1205 scan_omp (&OMP_FOR_COND (stmt
), ctx
);
1206 scan_omp (&OMP_FOR_INCR (stmt
), ctx
);
1207 scan_omp (&OMP_FOR_BODY (stmt
), ctx
);
1210 /* Scan an OpenMP sections directive. */
1213 scan_omp_sections (tree
*stmt_p
, omp_context
*outer_ctx
)
1219 ctx
= new_omp_context (stmt
, outer_ctx
);
1220 scan_sharing_clauses (OMP_SECTIONS_CLAUSES (stmt
), ctx
);
1221 scan_omp (&OMP_SECTIONS_BODY (stmt
), ctx
);
1224 /* Scan an OpenMP single directive. */
1227 scan_omp_single (tree
*stmt_p
, omp_context
*outer_ctx
)
1229 tree stmt
= *stmt_p
;
1233 ctx
= new_omp_context (stmt
, outer_ctx
);
1234 ctx
->field_map
= splay_tree_new (splay_tree_compare_pointers
, 0, 0);
1235 ctx
->record_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1236 name
= create_tmp_var_name (".omp_copy_s");
1237 name
= build_decl (TYPE_DECL
, name
, ctx
->record_type
);
1238 TYPE_NAME (ctx
->record_type
) = name
;
1240 scan_sharing_clauses (OMP_SINGLE_CLAUSES (stmt
), ctx
);
1241 scan_omp (&OMP_SINGLE_BODY (stmt
), ctx
);
1243 if (TYPE_FIELDS (ctx
->record_type
) == NULL
)
1244 ctx
->record_type
= NULL
;
1246 layout_type (ctx
->record_type
);
1250 /* Check OpenMP nesting restrictions. */
1252 check_omp_nesting_restrictions (tree t
, omp_context
*ctx
)
1254 switch (TREE_CODE (t
))
1259 for (; ctx
!= NULL
; ctx
= ctx
->outer
)
1260 switch (TREE_CODE (ctx
->stmt
))
1267 warning (0, "work-sharing region may not be closely nested inside "
1268 "of work-sharing, critical, ordered or master region");
1277 for (; ctx
!= NULL
; ctx
= ctx
->outer
)
1278 switch (TREE_CODE (ctx
->stmt
))
1283 warning (0, "master region may not be closely nested inside "
1284 "of work-sharing region");
1293 for (; ctx
!= NULL
; ctx
= ctx
->outer
)
1294 switch (TREE_CODE (ctx
->stmt
))
1297 warning (0, "ordered region may not be closely nested inside "
1298 "of critical region");
1301 if (find_omp_clause (OMP_CLAUSES (ctx
->stmt
),
1302 OMP_CLAUSE_ORDERED
) == NULL
)
1303 warning (0, "ordered region must be closely nested inside "
1304 "a loop region with an ordered clause");
1313 for (; ctx
!= NULL
; ctx
= ctx
->outer
)
1314 if (TREE_CODE (ctx
->stmt
) == OMP_CRITICAL
1315 && OMP_CRITICAL_NAME (t
) == OMP_CRITICAL_NAME (ctx
->stmt
))
1317 warning (0, "critical region may not be nested inside a critical "
1318 "region with the same name");
1328 /* Callback for walk_stmts used to scan for OpenMP directives at TP. */
1331 scan_omp_1 (tree
*tp
, int *walk_subtrees
, void *data
)
1333 struct walk_stmt_info
*wi
= data
;
1334 omp_context
*ctx
= wi
->info
;
1337 if (EXPR_HAS_LOCATION (t
))
1338 input_location
= EXPR_LOCATION (t
);
1340 /* Check the OpenMP nesting restrictions. */
1341 if (OMP_DIRECTIVE_P (t
) && ctx
!= NULL
)
1342 check_omp_nesting_restrictions (t
, ctx
);
1345 switch (TREE_CODE (t
))
1348 parallel_nesting_level
++;
1349 scan_omp_parallel (tp
, ctx
);
1350 parallel_nesting_level
--;
1354 scan_omp_for (tp
, ctx
);
1358 scan_omp_sections (tp
, ctx
);
1362 scan_omp_single (tp
, ctx
);
1369 ctx
= new_omp_context (*tp
, ctx
);
1370 scan_omp (&OMP_BODY (*tp
), ctx
);
1378 for (var
= BIND_EXPR_VARS (t
); var
; var
= TREE_CHAIN (var
))
1379 insert_decl_map (&ctx
->cb
, var
, var
);
1388 *tp
= remap_decl (t
, &ctx
->cb
);
1392 if (ctx
&& TYPE_P (t
))
1393 *tp
= remap_type (t
, &ctx
->cb
);
1394 else if (!DECL_P (t
))
1403 /* Scan all the statements starting at STMT_P. CTX contains context
1404 information about the OpenMP directives and clauses found during
1408 scan_omp (tree
*stmt_p
, omp_context
*ctx
)
1410 location_t saved_location
;
1411 struct walk_stmt_info wi
;
1413 memset (&wi
, 0, sizeof (wi
));
1414 wi
.callback
= scan_omp_1
;
1416 wi
.want_bind_expr
= (ctx
!= NULL
);
1417 wi
.want_locations
= true;
1419 saved_location
= input_location
;
1420 walk_stmts (&wi
, stmt_p
);
1421 input_location
= saved_location
;
1424 /* Re-gimplification and code generation routines. */
1426 /* Build a call to GOMP_barrier. */
1429 build_omp_barrier (tree
*stmt_list
)
1431 tree t
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_BARRIER
], 0);
1432 gimplify_and_add (t
, stmt_list
);
1435 /* If a context was created for STMT when it was scanned, return it. */
1437 static omp_context
*
1438 maybe_lookup_ctx (tree stmt
)
1441 n
= splay_tree_lookup (all_contexts
, (splay_tree_key
) stmt
);
1442 return n
? (omp_context
*) n
->value
: NULL
;
1446 /* Find the mapping for DECL in CTX or the immediately enclosing
1447 context that has a mapping for DECL.
1449 If CTX is a nested parallel directive, we may have to use the decl
1450 mappings created in CTX's parent context. Suppose that we have the
1451 following parallel nesting (variable UIDs showed for clarity):
1454 #omp parallel shared(iD.1562) -> outer parallel
1455 iD.1562 = iD.1562 + 1;
1457 #omp parallel shared (iD.1562) -> inner parallel
1458 iD.1562 = iD.1562 - 1;
1460 Each parallel structure will create a distinct .omp_data_s structure
1461 for copying iD.1562 in/out of the directive:
1463 outer parallel .omp_data_s.1.i -> iD.1562
1464 inner parallel .omp_data_s.2.i -> iD.1562
1466 A shared variable mapping will produce a copy-out operation before
1467 the parallel directive and a copy-in operation after it. So, in
1468 this case we would have:
1471 .omp_data_o.1.i = iD.1562;
1472 #omp parallel shared(iD.1562) -> outer parallel
1473 .omp_data_i.1 = &.omp_data_o.1
1474 .omp_data_i.1->i = .omp_data_i.1->i + 1;
1476 .omp_data_o.2.i = iD.1562; -> **
1477 #omp parallel shared(iD.1562) -> inner parallel
1478 .omp_data_i.2 = &.omp_data_o.2
1479 .omp_data_i.2->i = .omp_data_i.2->i - 1;
1482 ** This is a problem. The symbol iD.1562 cannot be referenced
1483 inside the body of the outer parallel region. But since we are
1484 emitting this copy operation while expanding the inner parallel
1485 directive, we need to access the CTX structure of the outer
1486 parallel directive to get the correct mapping:
1488 .omp_data_o.2.i = .omp_data_i.1->i
1490 Since there may be other workshare or parallel directives enclosing
1491 the parallel directive, it may be necessary to walk up the context
1492 parent chain. This is not a problem in general because nested
1493 parallelism happens only rarely. */
1496 lookup_decl_in_outer_ctx (tree decl
, omp_context
*ctx
)
1501 gcc_assert (ctx
->is_nested
);
1503 for (up
= ctx
->outer
, t
= NULL
; up
&& t
== NULL
; up
= up
->outer
)
1504 t
= maybe_lookup_decl (decl
, up
);
1512 /* Similar to lookup_decl_in_outer_ctx, but return DECL if not found
1513 in outer contexts. */
1516 maybe_lookup_decl_in_outer_ctx (tree decl
, omp_context
*ctx
)
1522 for (up
= ctx
->outer
, t
= NULL
; up
&& t
== NULL
; up
= up
->outer
)
1523 t
= maybe_lookup_decl (decl
, up
);
1525 return t
? t
: decl
;
1529 /* Construct the initialization value for reduction CLAUSE. */
1532 omp_reduction_init (tree clause
, tree type
)
1534 switch (OMP_CLAUSE_REDUCTION_CODE (clause
))
1541 case TRUTH_ORIF_EXPR
:
1542 case TRUTH_XOR_EXPR
:
1544 return fold_convert (type
, integer_zero_node
);
1547 case TRUTH_AND_EXPR
:
1548 case TRUTH_ANDIF_EXPR
:
1550 return fold_convert (type
, integer_one_node
);
1553 return fold_convert (type
, integer_minus_one_node
);
1556 if (SCALAR_FLOAT_TYPE_P (type
))
1558 REAL_VALUE_TYPE max
, min
;
1559 if (HONOR_INFINITIES (TYPE_MODE (type
)))
1562 real_arithmetic (&min
, NEGATE_EXPR
, &max
, NULL
);
1565 real_maxval (&min
, 1, TYPE_MODE (type
));
1566 return build_real (type
, min
);
1570 gcc_assert (INTEGRAL_TYPE_P (type
));
1571 return TYPE_MIN_VALUE (type
);
1575 if (SCALAR_FLOAT_TYPE_P (type
))
1577 REAL_VALUE_TYPE max
;
1578 if (HONOR_INFINITIES (TYPE_MODE (type
)))
1581 real_maxval (&max
, 0, TYPE_MODE (type
));
1582 return build_real (type
, max
);
1586 gcc_assert (INTEGRAL_TYPE_P (type
));
1587 return TYPE_MAX_VALUE (type
);
1595 /* Generate code to implement the input clauses, FIRSTPRIVATE and COPYIN,
1596 from the receiver (aka child) side and initializers for REFERENCE_TYPE
1597 private variables. Initialization statements go in ILIST, while calls
1598 to destructors go in DLIST. */
1601 lower_rec_input_clauses (tree clauses
, tree
*ilist
, tree
*dlist
,
1604 tree_stmt_iterator diter
;
1605 tree c
, dtor
, copyin_seq
, x
, ptr
;
1606 bool copyin_by_ref
= false;
1607 bool lastprivate_firstprivate
= false;
1610 *dlist
= alloc_stmt_list ();
1611 diter
= tsi_start (*dlist
);
1614 /* Do all the fixed sized types in the first pass, and the variable sized
1615 types in the second pass. This makes sure that the scalar arguments to
1616 the variable sized types are processed before we use them in the
1617 variable sized operations. */
1618 for (pass
= 0; pass
< 2; ++pass
)
1620 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
1622 enum omp_clause_code c_kind
= OMP_CLAUSE_CODE (c
);
1628 case OMP_CLAUSE_PRIVATE
:
1629 if (OMP_CLAUSE_PRIVATE_DEBUG (c
))
1632 case OMP_CLAUSE_SHARED
:
1633 if (maybe_lookup_decl (OMP_CLAUSE_DECL (c
), ctx
) == NULL
)
1635 gcc_assert (is_global_var (OMP_CLAUSE_DECL (c
)));
1638 case OMP_CLAUSE_FIRSTPRIVATE
:
1639 case OMP_CLAUSE_COPYIN
:
1640 case OMP_CLAUSE_REDUCTION
:
1642 case OMP_CLAUSE_LASTPRIVATE
:
1643 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
1645 lastprivate_firstprivate
= true;
1654 new_var
= var
= OMP_CLAUSE_DECL (c
);
1655 if (c_kind
!= OMP_CLAUSE_COPYIN
)
1656 new_var
= lookup_decl (var
, ctx
);
1658 if (c_kind
== OMP_CLAUSE_SHARED
|| c_kind
== OMP_CLAUSE_COPYIN
)
1663 else if (is_variable_sized (var
))
1665 /* For variable sized types, we need to allocate the
1666 actual storage here. Call alloca and store the
1667 result in the pointer decl that we created elsewhere. */
1671 ptr
= DECL_VALUE_EXPR (new_var
);
1672 gcc_assert (TREE_CODE (ptr
) == INDIRECT_REF
);
1673 ptr
= TREE_OPERAND (ptr
, 0);
1674 gcc_assert (DECL_P (ptr
));
1676 x
= TYPE_SIZE_UNIT (TREE_TYPE (new_var
));
1677 x
= build_call_expr (built_in_decls
[BUILT_IN_ALLOCA
], 1, x
);
1678 x
= fold_convert (TREE_TYPE (ptr
), x
);
1679 x
= build_gimple_modify_stmt (ptr
, x
);
1680 gimplify_and_add (x
, ilist
);
1682 else if (is_reference (var
))
1684 /* For references that are being privatized for Fortran,
1685 allocate new backing storage for the new pointer
1686 variable. This allows us to avoid changing all the
1687 code that expects a pointer to something that expects
1688 a direct variable. Note that this doesn't apply to
1689 C++, since reference types are disallowed in data
1690 sharing clauses there, except for NRV optimized
1695 x
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var
)));
1696 if (TREE_CONSTANT (x
))
1698 const char *name
= NULL
;
1699 if (DECL_NAME (var
))
1700 name
= IDENTIFIER_POINTER (DECL_NAME (new_var
));
1702 x
= create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var
)),
1704 gimple_add_tmp_var (x
);
1705 x
= build_fold_addr_expr_with_type (x
, TREE_TYPE (new_var
));
1709 x
= build_call_expr (built_in_decls
[BUILT_IN_ALLOCA
], 1, x
);
1710 x
= fold_convert (TREE_TYPE (new_var
), x
);
1713 x
= build_gimple_modify_stmt (new_var
, x
);
1714 gimplify_and_add (x
, ilist
);
1716 new_var
= build_fold_indirect_ref (new_var
);
1718 else if (c_kind
== OMP_CLAUSE_REDUCTION
1719 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
1727 switch (OMP_CLAUSE_CODE (c
))
1729 case OMP_CLAUSE_SHARED
:
1730 /* Shared global vars are just accessed directly. */
1731 if (is_global_var (new_var
))
1733 /* Set up the DECL_VALUE_EXPR for shared variables now. This
1734 needs to be delayed until after fixup_child_record_type so
1735 that we get the correct type during the dereference. */
1736 by_ref
= use_pointer_for_field (var
, true);
1737 x
= build_receiver_ref (var
, by_ref
, ctx
);
1738 SET_DECL_VALUE_EXPR (new_var
, x
);
1739 DECL_HAS_VALUE_EXPR_P (new_var
) = 1;
1741 /* ??? If VAR is not passed by reference, and the variable
1742 hasn't been initialized yet, then we'll get a warning for
1743 the store into the omp_data_s structure. Ideally, we'd be
1744 able to notice this and not store anything at all, but
1745 we're generating code too early. Suppress the warning. */
1747 TREE_NO_WARNING (var
) = 1;
1750 case OMP_CLAUSE_LASTPRIVATE
:
1751 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
1755 case OMP_CLAUSE_PRIVATE
:
1756 x
= lang_hooks
.decls
.omp_clause_default_ctor (c
, new_var
);
1758 gimplify_and_add (x
, ilist
);
1762 x
= lang_hooks
.decls
.omp_clause_dtor (c
, new_var
);
1766 gimplify_stmt (&dtor
);
1767 tsi_link_before (&diter
, dtor
, TSI_SAME_STMT
);
1771 case OMP_CLAUSE_FIRSTPRIVATE
:
1772 x
= build_outer_var_ref (var
, ctx
);
1773 x
= lang_hooks
.decls
.omp_clause_copy_ctor (c
, new_var
, x
);
1774 gimplify_and_add (x
, ilist
);
1778 case OMP_CLAUSE_COPYIN
:
1779 by_ref
= use_pointer_for_field (var
, false);
1780 x
= build_receiver_ref (var
, by_ref
, ctx
);
1781 x
= lang_hooks
.decls
.omp_clause_assign_op (c
, new_var
, x
);
1782 append_to_statement_list (x
, ©in_seq
);
1783 copyin_by_ref
|= by_ref
;
1786 case OMP_CLAUSE_REDUCTION
:
1787 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
1789 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c
), ilist
);
1790 OMP_CLAUSE_REDUCTION_INIT (c
) = NULL
;
1794 x
= omp_reduction_init (c
, TREE_TYPE (new_var
));
1795 gcc_assert (TREE_CODE (TREE_TYPE (new_var
)) != ARRAY_TYPE
);
1796 x
= build_gimple_modify_stmt (new_var
, x
);
1797 gimplify_and_add (x
, ilist
);
1807 /* The copyin sequence is not to be executed by the main thread, since
1808 that would result in self-copies. Perhaps not visible to scalars,
1809 but it certainly is to C++ operator=. */
1812 x
= build_call_expr (built_in_decls
[BUILT_IN_OMP_GET_THREAD_NUM
], 0);
1813 x
= build2 (NE_EXPR
, boolean_type_node
, x
,
1814 build_int_cst (TREE_TYPE (x
), 0));
1815 x
= build3 (COND_EXPR
, void_type_node
, x
, copyin_seq
, NULL
);
1816 gimplify_and_add (x
, ilist
);
1819 /* If any copyin variable is passed by reference, we must ensure the
1820 master thread doesn't modify it before it is copied over in all
1821 threads. Similarly for variables in both firstprivate and
1822 lastprivate clauses we need to ensure the lastprivate copying
1823 happens after firstprivate copying in all threads. */
1824 if (copyin_by_ref
|| lastprivate_firstprivate
)
1825 build_omp_barrier (ilist
);
1829 /* Generate code to implement the LASTPRIVATE clauses. This is used for
1830 both parallel and workshare constructs. PREDICATE may be NULL if it's
1834 lower_lastprivate_clauses (tree clauses
, tree predicate
, tree
*stmt_list
,
1837 tree sub_list
, x
, c
;
1839 /* Early exit if there are no lastprivate clauses. */
1840 clauses
= find_omp_clause (clauses
, OMP_CLAUSE_LASTPRIVATE
);
1841 if (clauses
== NULL
)
1843 /* If this was a workshare clause, see if it had been combined
1844 with its parallel. In that case, look for the clauses on the
1845 parallel statement itself. */
1846 if (is_parallel_ctx (ctx
))
1850 if (ctx
== NULL
|| !is_parallel_ctx (ctx
))
1853 clauses
= find_omp_clause (OMP_PARALLEL_CLAUSES (ctx
->stmt
),
1854 OMP_CLAUSE_LASTPRIVATE
);
1855 if (clauses
== NULL
)
1859 sub_list
= alloc_stmt_list ();
1861 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
1865 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_LASTPRIVATE
)
1868 var
= OMP_CLAUSE_DECL (c
);
1869 new_var
= lookup_decl (var
, ctx
);
1871 x
= build_outer_var_ref (var
, ctx
);
1872 if (is_reference (var
))
1873 new_var
= build_fold_indirect_ref (new_var
);
1874 x
= lang_hooks
.decls
.omp_clause_assign_op (c
, x
, new_var
);
1875 append_to_statement_list (x
, &sub_list
);
1879 x
= build3 (COND_EXPR
, void_type_node
, predicate
, sub_list
, NULL
);
1883 gimplify_and_add (x
, stmt_list
);
1887 /* Generate code to implement the REDUCTION clauses. */
1890 lower_reduction_clauses (tree clauses
, tree
*stmt_list
, omp_context
*ctx
)
1892 tree sub_list
= NULL
, x
, c
;
1895 /* First see if there is exactly one reduction clause. Use OMP_ATOMIC
1896 update in that case, otherwise use a lock. */
1897 for (c
= clauses
; c
&& count
< 2; c
= OMP_CLAUSE_CHAIN (c
))
1898 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
1900 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
1902 /* Never use OMP_ATOMIC for array reductions. */
1912 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
1914 tree var
, ref
, new_var
;
1915 enum tree_code code
;
1917 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
1920 var
= OMP_CLAUSE_DECL (c
);
1921 new_var
= lookup_decl (var
, ctx
);
1922 if (is_reference (var
))
1923 new_var
= build_fold_indirect_ref (new_var
);
1924 ref
= build_outer_var_ref (var
, ctx
);
1925 code
= OMP_CLAUSE_REDUCTION_CODE (c
);
1927 /* reduction(-:var) sums up the partial results, so it acts
1928 identically to reduction(+:var). */
1929 if (code
== MINUS_EXPR
)
1934 tree addr
= build_fold_addr_expr (ref
);
1936 addr
= save_expr (addr
);
1937 ref
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (addr
)), addr
);
1938 x
= fold_build2 (code
, TREE_TYPE (ref
), ref
, new_var
);
1939 x
= build2 (OMP_ATOMIC
, void_type_node
, addr
, x
);
1940 gimplify_and_add (x
, stmt_list
);
1944 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
1946 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
1948 if (is_reference (var
))
1949 ref
= build_fold_addr_expr (ref
);
1950 SET_DECL_VALUE_EXPR (placeholder
, ref
);
1951 DECL_HAS_VALUE_EXPR_P (placeholder
) = 1;
1952 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c
), &sub_list
);
1953 OMP_CLAUSE_REDUCTION_MERGE (c
) = NULL
;
1954 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = NULL
;
1958 x
= build2 (code
, TREE_TYPE (ref
), ref
, new_var
);
1959 ref
= build_outer_var_ref (var
, ctx
);
1960 x
= build_gimple_modify_stmt (ref
, x
);
1961 append_to_statement_list (x
, &sub_list
);
1965 x
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_ATOMIC_START
], 0);
1966 gimplify_and_add (x
, stmt_list
);
1968 gimplify_and_add (sub_list
, stmt_list
);
1970 x
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_ATOMIC_END
], 0);
1971 gimplify_and_add (x
, stmt_list
);
1975 /* Generate code to implement the COPYPRIVATE clauses. */
1978 lower_copyprivate_clauses (tree clauses
, tree
*slist
, tree
*rlist
,
1983 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
1988 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_COPYPRIVATE
)
1991 var
= OMP_CLAUSE_DECL (c
);
1992 by_ref
= use_pointer_for_field (var
, false);
1994 ref
= build_sender_ref (var
, ctx
);
1995 x
= (ctx
->is_nested
) ? lookup_decl_in_outer_ctx (var
, ctx
) : var
;
1996 x
= by_ref
? build_fold_addr_expr (x
) : x
;
1997 x
= build_gimple_modify_stmt (ref
, x
);
1998 gimplify_and_add (x
, slist
);
2000 ref
= build_receiver_ref (var
, by_ref
, ctx
);
2001 if (is_reference (var
))
2003 ref
= build_fold_indirect_ref (ref
);
2004 var
= build_fold_indirect_ref (var
);
2006 x
= lang_hooks
.decls
.omp_clause_assign_op (c
, var
, ref
);
2007 gimplify_and_add (x
, rlist
);
2012 /* Generate code to implement the clauses, FIRSTPRIVATE, COPYIN, LASTPRIVATE,
2013 and REDUCTION from the sender (aka parent) side. */
2016 lower_send_clauses (tree clauses
, tree
*ilist
, tree
*olist
, omp_context
*ctx
)
2020 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
2022 tree val
, ref
, x
, var
;
2023 bool by_ref
, do_in
= false, do_out
= false;
2025 switch (OMP_CLAUSE_CODE (c
))
2027 case OMP_CLAUSE_FIRSTPRIVATE
:
2028 case OMP_CLAUSE_COPYIN
:
2029 case OMP_CLAUSE_LASTPRIVATE
:
2030 case OMP_CLAUSE_REDUCTION
:
2036 var
= val
= OMP_CLAUSE_DECL (c
);
2038 var
= lookup_decl_in_outer_ctx (val
, ctx
);
2040 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_COPYIN
2041 && is_global_var (var
))
2043 if (is_variable_sized (val
))
2045 by_ref
= use_pointer_for_field (val
, false);
2047 switch (OMP_CLAUSE_CODE (c
))
2049 case OMP_CLAUSE_FIRSTPRIVATE
:
2050 case OMP_CLAUSE_COPYIN
:
2054 case OMP_CLAUSE_LASTPRIVATE
:
2055 if (by_ref
|| is_reference (val
))
2057 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
2065 case OMP_CLAUSE_REDUCTION
:
2067 do_out
= !(by_ref
|| is_reference (val
));
2076 ref
= build_sender_ref (val
, ctx
);
2077 x
= by_ref
? build_fold_addr_expr (var
) : var
;
2078 x
= build_gimple_modify_stmt (ref
, x
);
2079 gimplify_and_add (x
, ilist
);
2084 ref
= build_sender_ref (val
, ctx
);
2085 x
= build_gimple_modify_stmt (var
, ref
);
2086 gimplify_and_add (x
, olist
);
2091 /* Generate code to implement SHARED from the sender (aka parent) side.
2092 This is trickier, since OMP_PARALLEL_CLAUSES doesn't list things that
2093 got automatically shared. */
2096 lower_send_shared_vars (tree
*ilist
, tree
*olist
, omp_context
*ctx
)
2098 tree var
, ovar
, nvar
, f
, x
;
2100 if (ctx
->record_type
== NULL
)
2103 for (f
= TYPE_FIELDS (ctx
->record_type
); f
; f
= TREE_CHAIN (f
))
2105 ovar
= DECL_ABSTRACT_ORIGIN (f
);
2106 nvar
= maybe_lookup_decl (ovar
, ctx
);
2107 if (!nvar
|| !DECL_HAS_VALUE_EXPR_P (nvar
))
2112 /* If CTX is a nested parallel directive. Find the immediately
2113 enclosing parallel or workshare construct that contains a
2114 mapping for OVAR. */
2116 var
= lookup_decl_in_outer_ctx (ovar
, ctx
);
2118 if (use_pointer_for_field (ovar
, true))
2120 x
= build_sender_ref (ovar
, ctx
);
2121 var
= build_fold_addr_expr (var
);
2122 x
= build_gimple_modify_stmt (x
, var
);
2123 gimplify_and_add (x
, ilist
);
2127 x
= build_sender_ref (ovar
, ctx
);
2128 x
= build_gimple_modify_stmt (x
, var
);
2129 gimplify_and_add (x
, ilist
);
2131 x
= build_sender_ref (ovar
, ctx
);
2132 x
= build_gimple_modify_stmt (var
, x
);
2133 gimplify_and_add (x
, olist
);
2138 /* Build the function calls to GOMP_parallel_start etc to actually
2139 generate the parallel operation. REGION is the parallel region
2140 being expanded. BB is the block where to insert the code. WS_ARGS
2141 will be set if this is a call to a combined parallel+workshare
2142 construct, it contains the list of additional arguments needed by
2143 the workshare construct. */
2146 expand_parallel_call (struct omp_region
*region
, basic_block bb
,
2147 tree entry_stmt
, tree ws_args
)
2149 tree t
, t1
, t2
, val
, cond
, c
, list
, clauses
;
2150 block_stmt_iterator si
;
2153 clauses
= OMP_PARALLEL_CLAUSES (entry_stmt
);
2154 push_gimplify_context ();
2156 /* Determine what flavor of GOMP_parallel_start we will be
2158 start_ix
= BUILT_IN_GOMP_PARALLEL_START
;
2159 if (is_combined_parallel (region
))
2161 switch (region
->inner
->type
)
2164 start_ix
= BUILT_IN_GOMP_PARALLEL_LOOP_STATIC_START
2165 + region
->inner
->sched_kind
;
2168 start_ix
= BUILT_IN_GOMP_PARALLEL_SECTIONS_START
;
2175 /* By default, the value of NUM_THREADS is zero (selected at run time)
2176 and there is no conditional. */
2178 val
= build_int_cst (unsigned_type_node
, 0);
2180 c
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
2182 cond
= OMP_CLAUSE_IF_EXPR (c
);
2184 c
= find_omp_clause (clauses
, OMP_CLAUSE_NUM_THREADS
);
2186 val
= OMP_CLAUSE_NUM_THREADS_EXPR (c
);
2188 /* Ensure 'val' is of the correct type. */
2189 val
= fold_convert (unsigned_type_node
, val
);
2191 /* If we found the clause 'if (cond)', build either
2192 (cond != 0) or (cond ? val : 1u). */
2195 block_stmt_iterator si
;
2197 cond
= gimple_boolify (cond
);
2199 if (integer_zerop (val
))
2200 val
= build2 (EQ_EXPR
, unsigned_type_node
, cond
,
2201 build_int_cst (TREE_TYPE (cond
), 0));
2204 basic_block cond_bb
, then_bb
, else_bb
;
2208 tmp
= create_tmp_var (TREE_TYPE (val
), NULL
);
2209 e
= split_block (bb
, NULL
);
2214 then_bb
= create_empty_bb (cond_bb
);
2215 else_bb
= create_empty_bb (then_bb
);
2217 t
= build3 (COND_EXPR
, void_type_node
,
2218 cond
, NULL_TREE
, NULL_TREE
);
2220 si
= bsi_start (cond_bb
);
2221 bsi_insert_after (&si
, t
, BSI_CONTINUE_LINKING
);
2223 si
= bsi_start (then_bb
);
2224 t
= build_gimple_modify_stmt (tmp
, val
);
2225 bsi_insert_after (&si
, t
, BSI_CONTINUE_LINKING
);
2227 si
= bsi_start (else_bb
);
2228 t
= build_gimple_modify_stmt (tmp
,
2229 build_int_cst (unsigned_type_node
, 1));
2230 bsi_insert_after (&si
, t
, BSI_CONTINUE_LINKING
);
2232 make_edge (cond_bb
, then_bb
, EDGE_TRUE_VALUE
);
2233 make_edge (cond_bb
, else_bb
, EDGE_FALSE_VALUE
);
2234 make_edge (then_bb
, bb
, EDGE_FALLTHRU
);
2235 make_edge (else_bb
, bb
, EDGE_FALLTHRU
);
2241 val
= get_formal_tmp_var (val
, &list
);
2242 si
= bsi_start (bb
);
2243 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
2247 t
= OMP_PARALLEL_DATA_ARG (entry_stmt
);
2249 t1
= null_pointer_node
;
2251 t1
= build_fold_addr_expr (t
);
2252 t2
= build_fold_addr_expr (OMP_PARALLEL_FN (entry_stmt
));
2256 tree args
= tree_cons (NULL
, t2
,
2257 tree_cons (NULL
, t1
,
2258 tree_cons (NULL
, val
, ws_args
)));
2259 t
= build_function_call_expr (built_in_decls
[start_ix
], args
);
2262 t
= build_call_expr (built_in_decls
[start_ix
], 3, t2
, t1
, val
);
2264 gimplify_and_add (t
, &list
);
2266 t
= OMP_PARALLEL_DATA_ARG (entry_stmt
);
2268 t
= null_pointer_node
;
2270 t
= build_fold_addr_expr (t
);
2271 t
= build_call_expr (OMP_PARALLEL_FN (entry_stmt
), 1, t
);
2272 gimplify_and_add (t
, &list
);
2274 t
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_PARALLEL_END
], 0);
2275 gimplify_and_add (t
, &list
);
2278 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
2280 pop_gimplify_context (NULL_TREE
);
2284 /* If exceptions are enabled, wrap *STMT_P in a MUST_NOT_THROW catch
2285 handler. This prevents programs from violating the structured
2286 block semantics with throws. */
2289 maybe_catch_exception (tree
*stmt_p
)
2293 if (!flag_exceptions
)
2296 if (lang_protect_cleanup_actions
)
2297 t
= lang_protect_cleanup_actions ();
2299 t
= build_call_expr (built_in_decls
[BUILT_IN_TRAP
], 0);
2300 f
= build2 (EH_FILTER_EXPR
, void_type_node
, NULL
, NULL
);
2301 EH_FILTER_MUST_NOT_THROW (f
) = 1;
2302 gimplify_and_add (t
, &EH_FILTER_FAILURE (f
));
2304 t
= build2 (TRY_CATCH_EXPR
, void_type_node
, *stmt_p
, NULL
);
2305 append_to_statement_list (f
, &TREE_OPERAND (t
, 1));
2308 append_to_statement_list (t
, stmt_p
);
2311 /* Chain all the DECLs in LIST by their TREE_CHAIN fields. */
2314 list2chain (tree list
)
2318 for (t
= list
; t
; t
= TREE_CHAIN (t
))
2320 tree var
= TREE_VALUE (t
);
2322 TREE_CHAIN (var
) = TREE_VALUE (TREE_CHAIN (t
));
2324 TREE_CHAIN (var
) = NULL_TREE
;
2327 return list
? TREE_VALUE (list
) : NULL_TREE
;
2331 /* Remove barriers in REGION->EXIT's block. Note that this is only
2332 valid for OMP_PARALLEL regions. Since the end of a parallel region
2333 is an implicit barrier, any workshare inside the OMP_PARALLEL that
2334 left a barrier at the end of the OMP_PARALLEL region can now be
2338 remove_exit_barrier (struct omp_region
*region
)
2340 block_stmt_iterator si
;
2341 basic_block exit_bb
;
2346 exit_bb
= region
->exit
;
2348 /* If the parallel region doesn't return, we don't have REGION->EXIT
2353 /* The last insn in the block will be the parallel's OMP_RETURN. The
2354 workshare's OMP_RETURN will be in a preceding block. The kinds of
2355 statements that can appear in between are extremely limited -- no
2356 memory operations at all. Here, we allow nothing at all, so the
2357 only thing we allow to precede this OMP_RETURN is a label. */
2358 si
= bsi_last (exit_bb
);
2359 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_RETURN
);
2361 if (!bsi_end_p (si
) && TREE_CODE (bsi_stmt (si
)) != LABEL_EXPR
)
2364 FOR_EACH_EDGE (e
, ei
, exit_bb
->preds
)
2366 si
= bsi_last (e
->src
);
2370 if (TREE_CODE (t
) == OMP_RETURN
)
2371 OMP_RETURN_NOWAIT (t
) = 1;
2376 remove_exit_barriers (struct omp_region
*region
)
2378 if (region
->type
== OMP_PARALLEL
)
2379 remove_exit_barrier (region
);
2383 region
= region
->inner
;
2384 remove_exit_barriers (region
);
2385 while (region
->next
)
2387 region
= region
->next
;
2388 remove_exit_barriers (region
);
2393 /* Expand the OpenMP parallel directive starting at REGION. */
2396 expand_omp_parallel (struct omp_region
*region
)
2398 basic_block entry_bb
, exit_bb
, new_bb
;
2399 struct function
*child_cfun
, *saved_cfun
;
2400 tree child_fn
, block
, t
, ws_args
;
2401 block_stmt_iterator si
;
2404 bool do_cleanup_cfg
= false;
2406 entry_stmt
= last_stmt (region
->entry
);
2407 child_fn
= OMP_PARALLEL_FN (entry_stmt
);
2408 child_cfun
= DECL_STRUCT_FUNCTION (child_fn
);
2411 entry_bb
= region
->entry
;
2412 exit_bb
= region
->exit
;
2414 if (is_combined_parallel (region
))
2415 ws_args
= region
->ws_args
;
2417 ws_args
= NULL_TREE
;
2419 if (child_cfun
->cfg
)
2421 /* Due to inlining, it may happen that we have already outlined
2422 the region, in which case all we need to do is make the
2423 sub-graph unreachable and emit the parallel call. */
2424 edge entry_succ_e
, exit_succ_e
;
2425 block_stmt_iterator si
;
2427 entry_succ_e
= single_succ_edge (entry_bb
);
2429 si
= bsi_last (entry_bb
);
2430 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_PARALLEL
);
2431 bsi_remove (&si
, true);
2434 remove_edge (entry_succ_e
);
2437 exit_succ_e
= single_succ_edge (exit_bb
);
2438 make_edge (new_bb
, exit_succ_e
->dest
, EDGE_FALLTHRU
);
2440 do_cleanup_cfg
= true;
2444 /* If the parallel region needs data sent from the parent
2445 function, then the very first statement (except possible
2446 tree profile counter updates) of the parallel body
2447 is a copy assignment .OMP_DATA_I = &.OMP_DATA_O. Since
2448 &.OMP_DATA_O is passed as an argument to the child function,
2449 we need to replace it with the argument as seen by the child
2452 In most cases, this will end up being the identity assignment
2453 .OMP_DATA_I = .OMP_DATA_I. However, if the parallel body had
2454 a function call that has been inlined, the original PARM_DECL
2455 .OMP_DATA_I may have been converted into a different local
2456 variable. In which case, we need to keep the assignment. */
2457 if (OMP_PARALLEL_DATA_ARG (entry_stmt
))
2459 basic_block entry_succ_bb
= single_succ (entry_bb
);
2460 block_stmt_iterator si
;
2462 for (si
= bsi_start (entry_succ_bb
); ; bsi_next (&si
))
2466 gcc_assert (!bsi_end_p (si
));
2467 stmt
= bsi_stmt (si
);
2468 if (TREE_CODE (stmt
) != GIMPLE_MODIFY_STMT
)
2471 arg
= GIMPLE_STMT_OPERAND (stmt
, 1);
2473 if (TREE_CODE (arg
) == ADDR_EXPR
2474 && TREE_OPERAND (arg
, 0)
2475 == OMP_PARALLEL_DATA_ARG (entry_stmt
))
2477 if (GIMPLE_STMT_OPERAND (stmt
, 0)
2478 == DECL_ARGUMENTS (child_fn
))
2479 bsi_remove (&si
, true);
2481 GIMPLE_STMT_OPERAND (stmt
, 1) = DECL_ARGUMENTS (child_fn
);
2487 /* Declare local variables needed in CHILD_CFUN. */
2488 block
= DECL_INITIAL (child_fn
);
2489 BLOCK_VARS (block
) = list2chain (child_cfun
->unexpanded_var_list
);
2490 DECL_SAVED_TREE (child_fn
) = bb_stmt_list (single_succ (entry_bb
));
2492 /* Reset DECL_CONTEXT on locals and function arguments. */
2493 for (t
= BLOCK_VARS (block
); t
; t
= TREE_CHAIN (t
))
2494 DECL_CONTEXT (t
) = child_fn
;
2496 for (t
= DECL_ARGUMENTS (child_fn
); t
; t
= TREE_CHAIN (t
))
2497 DECL_CONTEXT (t
) = child_fn
;
2499 /* Split ENTRY_BB at OMP_PARALLEL so that it can be moved to the
2501 si
= bsi_last (entry_bb
);
2503 gcc_assert (t
&& TREE_CODE (t
) == OMP_PARALLEL
);
2504 bsi_remove (&si
, true);
2505 e
= split_block (entry_bb
, t
);
2507 single_succ_edge (entry_bb
)->flags
= EDGE_FALLTHRU
;
2509 /* Move the parallel region into CHILD_CFUN. We need to reset
2510 dominance information because the expansion of the inner
2511 regions has invalidated it. */
2512 free_dominance_info (CDI_DOMINATORS
);
2513 new_bb
= move_sese_region_to_fn (child_cfun
, entry_bb
, exit_bb
);
2515 single_succ_edge (new_bb
)->flags
= EDGE_FALLTHRU
;
2516 DECL_STRUCT_FUNCTION (child_fn
)->curr_properties
2517 = cfun
->curr_properties
;
2518 cgraph_add_new_function (child_fn
, true);
2520 /* Convert OMP_RETURN into a RETURN_EXPR. */
2523 si
= bsi_last (exit_bb
);
2524 gcc_assert (!bsi_end_p (si
)
2525 && TREE_CODE (bsi_stmt (si
)) == OMP_RETURN
);
2526 t
= build1 (RETURN_EXPR
, void_type_node
, NULL
);
2527 bsi_insert_after (&si
, t
, BSI_SAME_STMT
);
2528 bsi_remove (&si
, true);
2532 /* Emit a library call to launch the children threads. */
2533 expand_parallel_call (region
, new_bb
, entry_stmt
, ws_args
);
2537 /* Clean up the unreachable sub-graph we created above. */
2538 free_dominance_info (CDI_DOMINATORS
);
2539 free_dominance_info (CDI_POST_DOMINATORS
);
2540 cleanup_tree_cfg ();
2545 /* A subroutine of expand_omp_for. Generate code for a parallel
2546 loop with any schedule. Given parameters:
2548 for (V = N1; V cond N2; V += STEP) BODY;
2550 where COND is "<" or ">", we generate pseudocode
2552 more = GOMP_loop_foo_start (N1, N2, STEP, CHUNK, &istart0, &iend0);
2553 if (more) goto L0; else goto L3;
2560 if (V cond iend) goto L1; else goto L2;
2562 if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
2565 If this is a combined omp parallel loop, instead of the call to
2566 GOMP_loop_foo_start, we emit 'goto L3'. */
2569 expand_omp_for_generic (struct omp_region
*region
,
2570 struct omp_for_data
*fd
,
2571 enum built_in_function start_fn
,
2572 enum built_in_function next_fn
)
2574 tree type
, istart0
, iend0
, iend
;
2576 basic_block entry_bb
, cont_bb
, exit_bb
, l0_bb
, l1_bb
;
2577 basic_block l2_bb
= NULL
, l3_bb
= NULL
;
2578 block_stmt_iterator si
;
2579 bool in_combined_parallel
= is_combined_parallel (region
);
2581 type
= TREE_TYPE (fd
->v
);
2583 istart0
= create_tmp_var (long_integer_type_node
, ".istart0");
2584 iend0
= create_tmp_var (long_integer_type_node
, ".iend0");
2585 iend
= create_tmp_var (type
, NULL
);
2586 TREE_ADDRESSABLE (istart0
) = 1;
2587 TREE_ADDRESSABLE (iend0
) = 1;
2589 gcc_assert ((region
->cont
!= NULL
) ^ (region
->exit
== NULL
));
2591 entry_bb
= region
->entry
;
2592 l0_bb
= create_empty_bb (entry_bb
);
2593 l1_bb
= single_succ (entry_bb
);
2595 cont_bb
= region
->cont
;
2596 exit_bb
= region
->exit
;
2599 l2_bb
= create_empty_bb (cont_bb
);
2600 l3_bb
= single_succ (cont_bb
);
2603 si
= bsi_last (entry_bb
);
2604 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_FOR
);
2605 if (!in_combined_parallel
)
2607 tree t0
, t1
, t2
, t3
, t4
;
2608 /* If this is not a combined parallel loop, emit a call to
2609 GOMP_loop_foo_start in ENTRY_BB. */
2610 list
= alloc_stmt_list ();
2611 t4
= build_fold_addr_expr (iend0
);
2612 t3
= build_fold_addr_expr (istart0
);
2613 t2
= fold_convert (long_integer_type_node
, fd
->step
);
2614 t1
= fold_convert (long_integer_type_node
, fd
->n2
);
2615 t0
= fold_convert (long_integer_type_node
, fd
->n1
);
2618 t
= fold_convert (long_integer_type_node
, fd
->chunk_size
);
2619 t
= build_call_expr (built_in_decls
[start_fn
], 6,
2620 t0
, t1
, t2
, t
, t3
, t4
);
2623 t
= build_call_expr (built_in_decls
[start_fn
], 5,
2624 t0
, t1
, t2
, t3
, t4
);
2625 t
= get_formal_tmp_var (t
, &list
);
2628 t
= build3 (COND_EXPR
, void_type_node
, t
, NULL_TREE
, NULL_TREE
);
2629 append_to_statement_list (t
, &list
);
2631 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
2633 bsi_remove (&si
, true);
2635 /* Iteration setup for sequential loop goes in L0_BB. */
2636 list
= alloc_stmt_list ();
2637 t
= fold_convert (type
, istart0
);
2638 t
= build_gimple_modify_stmt (fd
->v
, t
);
2639 gimplify_and_add (t
, &list
);
2641 t
= fold_convert (type
, iend0
);
2642 t
= build_gimple_modify_stmt (iend
, t
);
2643 gimplify_and_add (t
, &list
);
2645 si
= bsi_start (l0_bb
);
2646 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
2648 /* Handle the rare case where BODY doesn't ever return. */
2649 if (cont_bb
== NULL
)
2651 remove_edge (single_succ_edge (entry_bb
));
2652 make_edge (entry_bb
, l0_bb
, EDGE_FALLTHRU
);
2653 make_edge (l0_bb
, l1_bb
, EDGE_FALLTHRU
);
2657 /* Code to control the increment and predicate for the sequential
2658 loop goes in the first half of EXIT_BB (we split EXIT_BB so
2659 that we can inherit all the edges going out of the loop
2661 list
= alloc_stmt_list ();
2663 t
= build2 (PLUS_EXPR
, type
, fd
->v
, fd
->step
);
2664 t
= build_gimple_modify_stmt (fd
->v
, t
);
2665 gimplify_and_add (t
, &list
);
2667 t
= build2 (fd
->cond_code
, boolean_type_node
, fd
->v
, iend
);
2668 t
= get_formal_tmp_var (t
, &list
);
2669 t
= build3 (COND_EXPR
, void_type_node
, t
, NULL_TREE
, NULL_TREE
);
2670 append_to_statement_list (t
, &list
);
2672 si
= bsi_last (cont_bb
);
2673 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
2674 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_CONTINUE
);
2675 bsi_remove (&si
, true);
2677 /* Emit code to get the next parallel iteration in L2_BB. */
2678 list
= alloc_stmt_list ();
2680 t
= build_call_expr (built_in_decls
[next_fn
], 2,
2681 build_fold_addr_expr (istart0
),
2682 build_fold_addr_expr (iend0
));
2683 t
= get_formal_tmp_var (t
, &list
);
2684 t
= build3 (COND_EXPR
, void_type_node
, t
, NULL_TREE
, NULL_TREE
);
2685 append_to_statement_list (t
, &list
);
2687 si
= bsi_start (l2_bb
);
2688 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
2690 /* Add the loop cleanup function. */
2691 si
= bsi_last (exit_bb
);
2692 if (OMP_RETURN_NOWAIT (bsi_stmt (si
)))
2693 t
= built_in_decls
[BUILT_IN_GOMP_LOOP_END_NOWAIT
];
2695 t
= built_in_decls
[BUILT_IN_GOMP_LOOP_END
];
2696 t
= build_call_expr (t
, 0);
2697 bsi_insert_after (&si
, t
, BSI_SAME_STMT
);
2698 bsi_remove (&si
, true);
2700 /* Connect the new blocks. */
2701 remove_edge (single_succ_edge (entry_bb
));
2702 if (in_combined_parallel
)
2703 make_edge (entry_bb
, l2_bb
, EDGE_FALLTHRU
);
2706 make_edge (entry_bb
, l0_bb
, EDGE_TRUE_VALUE
);
2707 make_edge (entry_bb
, l3_bb
, EDGE_FALSE_VALUE
);
2710 make_edge (l0_bb
, l1_bb
, EDGE_FALLTHRU
);
2712 remove_edge (single_succ_edge (cont_bb
));
2713 make_edge (cont_bb
, l1_bb
, EDGE_TRUE_VALUE
);
2714 make_edge (cont_bb
, l2_bb
, EDGE_FALSE_VALUE
);
2716 make_edge (l2_bb
, l0_bb
, EDGE_TRUE_VALUE
);
2717 make_edge (l2_bb
, l3_bb
, EDGE_FALSE_VALUE
);
2721 /* A subroutine of expand_omp_for. Generate code for a parallel
2722 loop with static schedule and no specified chunk size. Given
2725 for (V = N1; V cond N2; V += STEP) BODY;
2727 where COND is "<" or ">", we generate pseudocode
2733 n = (adj + N2 - N1) / STEP;
2735 q += (q * nthreads != n);
2737 e0 = min(s0 + q, n);
2738 if (s0 >= e0) goto L2; else goto L0;
2745 if (V cond e) goto L1;
2750 expand_omp_for_static_nochunk (struct omp_region
*region
,
2751 struct omp_for_data
*fd
)
2753 tree n
, q
, s0
, e0
, e
, t
, nthreads
, threadid
;
2755 basic_block entry_bb
, exit_bb
, seq_start_bb
, body_bb
, cont_bb
;
2757 block_stmt_iterator si
;
2759 type
= TREE_TYPE (fd
->v
);
2761 entry_bb
= region
->entry
;
2762 seq_start_bb
= create_empty_bb (entry_bb
);
2763 body_bb
= single_succ (entry_bb
);
2764 cont_bb
= region
->cont
;
2765 fin_bb
= single_succ (cont_bb
);
2766 exit_bb
= region
->exit
;
2768 /* Iteration space partitioning goes in ENTRY_BB. */
2769 list
= alloc_stmt_list ();
2771 t
= build_call_expr (built_in_decls
[BUILT_IN_OMP_GET_NUM_THREADS
], 0);
2772 t
= fold_convert (type
, t
);
2773 nthreads
= get_formal_tmp_var (t
, &list
);
2775 t
= build_call_expr (built_in_decls
[BUILT_IN_OMP_GET_THREAD_NUM
], 0);
2776 t
= fold_convert (type
, t
);
2777 threadid
= get_formal_tmp_var (t
, &list
);
2779 fd
->n1
= fold_convert (type
, fd
->n1
);
2780 if (!is_gimple_val (fd
->n1
))
2781 fd
->n1
= get_formal_tmp_var (fd
->n1
, &list
);
2783 fd
->n2
= fold_convert (type
, fd
->n2
);
2784 if (!is_gimple_val (fd
->n2
))
2785 fd
->n2
= get_formal_tmp_var (fd
->n2
, &list
);
2787 fd
->step
= fold_convert (type
, fd
->step
);
2788 if (!is_gimple_val (fd
->step
))
2789 fd
->step
= get_formal_tmp_var (fd
->step
, &list
);
2791 t
= build_int_cst (type
, (fd
->cond_code
== LT_EXPR
? -1 : 1));
2792 t
= fold_build2 (PLUS_EXPR
, type
, fd
->step
, t
);
2793 t
= fold_build2 (PLUS_EXPR
, type
, t
, fd
->n2
);
2794 t
= fold_build2 (MINUS_EXPR
, type
, t
, fd
->n1
);
2795 t
= fold_build2 (TRUNC_DIV_EXPR
, type
, t
, fd
->step
);
2796 t
= fold_convert (type
, t
);
2797 if (is_gimple_val (t
))
2800 n
= get_formal_tmp_var (t
, &list
);
2802 t
= build2 (TRUNC_DIV_EXPR
, type
, n
, nthreads
);
2803 q
= get_formal_tmp_var (t
, &list
);
2805 t
= build2 (MULT_EXPR
, type
, q
, nthreads
);
2806 t
= build2 (NE_EXPR
, type
, t
, n
);
2807 t
= build2 (PLUS_EXPR
, type
, q
, t
);
2808 q
= get_formal_tmp_var (t
, &list
);
2810 t
= build2 (MULT_EXPR
, type
, q
, threadid
);
2811 s0
= get_formal_tmp_var (t
, &list
);
2813 t
= build2 (PLUS_EXPR
, type
, s0
, q
);
2814 t
= build2 (MIN_EXPR
, type
, t
, n
);
2815 e0
= get_formal_tmp_var (t
, &list
);
2817 t
= build2 (GE_EXPR
, boolean_type_node
, s0
, e0
);
2818 t
= build3 (COND_EXPR
, void_type_node
, t
, NULL_TREE
, NULL_TREE
);
2819 append_to_statement_list (t
, &list
);
2821 si
= bsi_last (entry_bb
);
2822 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_FOR
);
2823 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
2824 bsi_remove (&si
, true);
2826 /* Setup code for sequential iteration goes in SEQ_START_BB. */
2827 list
= alloc_stmt_list ();
2829 t
= fold_convert (type
, s0
);
2830 t
= build2 (MULT_EXPR
, type
, t
, fd
->step
);
2831 t
= build2 (PLUS_EXPR
, type
, t
, fd
->n1
);
2832 t
= build_gimple_modify_stmt (fd
->v
, t
);
2833 gimplify_and_add (t
, &list
);
2835 t
= fold_convert (type
, e0
);
2836 t
= build2 (MULT_EXPR
, type
, t
, fd
->step
);
2837 t
= build2 (PLUS_EXPR
, type
, t
, fd
->n1
);
2838 e
= get_formal_tmp_var (t
, &list
);
2840 si
= bsi_start (seq_start_bb
);
2841 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
2843 /* The code controlling the sequential loop replaces the OMP_CONTINUE. */
2844 list
= alloc_stmt_list ();
2846 t
= build2 (PLUS_EXPR
, type
, fd
->v
, fd
->step
);
2847 t
= build_gimple_modify_stmt (fd
->v
, t
);
2848 gimplify_and_add (t
, &list
);
2850 t
= build2 (fd
->cond_code
, boolean_type_node
, fd
->v
, e
);
2851 t
= get_formal_tmp_var (t
, &list
);
2852 t
= build3 (COND_EXPR
, void_type_node
, t
, NULL_TREE
, NULL_TREE
);
2853 append_to_statement_list (t
, &list
);
2855 si
= bsi_last (cont_bb
);
2856 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_CONTINUE
);
2857 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
2858 bsi_remove (&si
, true);
2860 /* Replace the OMP_RETURN with a barrier, or nothing. */
2861 si
= bsi_last (exit_bb
);
2862 if (!OMP_RETURN_NOWAIT (bsi_stmt (si
)))
2864 list
= alloc_stmt_list ();
2865 build_omp_barrier (&list
);
2866 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
2868 bsi_remove (&si
, true);
2870 /* Connect all the blocks. */
2871 make_edge (seq_start_bb
, body_bb
, EDGE_FALLTHRU
);
2873 remove_edge (single_succ_edge (entry_bb
));
2874 make_edge (entry_bb
, fin_bb
, EDGE_TRUE_VALUE
);
2875 make_edge (entry_bb
, seq_start_bb
, EDGE_FALSE_VALUE
);
2877 make_edge (cont_bb
, body_bb
, EDGE_TRUE_VALUE
);
2878 find_edge (cont_bb
, fin_bb
)->flags
= EDGE_FALSE_VALUE
;
2882 /* A subroutine of expand_omp_for. Generate code for a parallel
2883 loop with static schedule and a specified chunk size. Given
2886 for (V = N1; V cond N2; V += STEP) BODY;
2888 where COND is "<" or ">", we generate pseudocode
2894 n = (adj + N2 - N1) / STEP;
2897 s0 = (trip * nthreads + threadid) * CHUNK;
2898 e0 = min(s0 + CHUNK, n);
2899 if (s0 < n) goto L1; else goto L4;
2906 if (V cond e) goto L2; else goto L3;
2914 expand_omp_for_static_chunk (struct omp_region
*region
, struct omp_for_data
*fd
)
2916 tree n
, s0
, e0
, e
, t
;
2917 tree trip
, nthreads
, threadid
;
2919 basic_block entry_bb
, exit_bb
, body_bb
, seq_start_bb
, iter_part_bb
;
2920 basic_block trip_update_bb
, cont_bb
, fin_bb
;
2922 block_stmt_iterator si
;
2924 type
= TREE_TYPE (fd
->v
);
2926 entry_bb
= region
->entry
;
2927 iter_part_bb
= create_empty_bb (entry_bb
);
2928 seq_start_bb
= create_empty_bb (iter_part_bb
);
2929 body_bb
= single_succ (entry_bb
);
2930 cont_bb
= region
->cont
;
2931 trip_update_bb
= create_empty_bb (cont_bb
);
2932 fin_bb
= single_succ (cont_bb
);
2933 exit_bb
= region
->exit
;
2935 /* Trip and adjustment setup goes in ENTRY_BB. */
2936 list
= alloc_stmt_list ();
2938 t
= build_call_expr (built_in_decls
[BUILT_IN_OMP_GET_NUM_THREADS
], 0);
2939 t
= fold_convert (type
, t
);
2940 nthreads
= get_formal_tmp_var (t
, &list
);
2942 t
= build_call_expr (built_in_decls
[BUILT_IN_OMP_GET_THREAD_NUM
], 0);
2943 t
= fold_convert (type
, t
);
2944 threadid
= get_formal_tmp_var (t
, &list
);
2946 fd
->n1
= fold_convert (type
, fd
->n1
);
2947 if (!is_gimple_val (fd
->n1
))
2948 fd
->n1
= get_formal_tmp_var (fd
->n1
, &list
);
2950 fd
->n2
= fold_convert (type
, fd
->n2
);
2951 if (!is_gimple_val (fd
->n2
))
2952 fd
->n2
= get_formal_tmp_var (fd
->n2
, &list
);
2954 fd
->step
= fold_convert (type
, fd
->step
);
2955 if (!is_gimple_val (fd
->step
))
2956 fd
->step
= get_formal_tmp_var (fd
->step
, &list
);
2958 fd
->chunk_size
= fold_convert (type
, fd
->chunk_size
);
2959 if (!is_gimple_val (fd
->chunk_size
))
2960 fd
->chunk_size
= get_formal_tmp_var (fd
->chunk_size
, &list
);
2962 t
= build_int_cst (type
, (fd
->cond_code
== LT_EXPR
? -1 : 1));
2963 t
= fold_build2 (PLUS_EXPR
, type
, fd
->step
, t
);
2964 t
= fold_build2 (PLUS_EXPR
, type
, t
, fd
->n2
);
2965 t
= fold_build2 (MINUS_EXPR
, type
, t
, fd
->n1
);
2966 t
= fold_build2 (TRUNC_DIV_EXPR
, type
, t
, fd
->step
);
2967 t
= fold_convert (type
, t
);
2968 if (is_gimple_val (t
))
2971 n
= get_formal_tmp_var (t
, &list
);
2973 t
= build_int_cst (type
, 0);
2974 trip
= get_initialized_tmp_var (t
, &list
, NULL
);
2976 si
= bsi_last (entry_bb
);
2977 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_FOR
);
2978 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
2979 bsi_remove (&si
, true);
2981 /* Iteration space partitioning goes in ITER_PART_BB. */
2982 list
= alloc_stmt_list ();
2984 t
= build2 (MULT_EXPR
, type
, trip
, nthreads
);
2985 t
= build2 (PLUS_EXPR
, type
, t
, threadid
);
2986 t
= build2 (MULT_EXPR
, type
, t
, fd
->chunk_size
);
2987 s0
= get_formal_tmp_var (t
, &list
);
2989 t
= build2 (PLUS_EXPR
, type
, s0
, fd
->chunk_size
);
2990 t
= build2 (MIN_EXPR
, type
, t
, n
);
2991 e0
= get_formal_tmp_var (t
, &list
);
2993 t
= build2 (LT_EXPR
, boolean_type_node
, s0
, n
);
2994 t
= build3 (COND_EXPR
, void_type_node
, t
, NULL_TREE
, NULL_TREE
);
2995 append_to_statement_list (t
, &list
);
2997 si
= bsi_start (iter_part_bb
);
2998 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
3000 /* Setup code for sequential iteration goes in SEQ_START_BB. */
3001 list
= alloc_stmt_list ();
3003 t
= fold_convert (type
, s0
);
3004 t
= build2 (MULT_EXPR
, type
, t
, fd
->step
);
3005 t
= build2 (PLUS_EXPR
, type
, t
, fd
->n1
);
3006 t
= build_gimple_modify_stmt (fd
->v
, t
);
3007 gimplify_and_add (t
, &list
);
3009 t
= fold_convert (type
, e0
);
3010 t
= build2 (MULT_EXPR
, type
, t
, fd
->step
);
3011 t
= build2 (PLUS_EXPR
, type
, t
, fd
->n1
);
3012 e
= get_formal_tmp_var (t
, &list
);
3014 si
= bsi_start (seq_start_bb
);
3015 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
3017 /* The code controlling the sequential loop goes in CONT_BB,
3018 replacing the OMP_CONTINUE. */
3019 list
= alloc_stmt_list ();
3021 t
= build2 (PLUS_EXPR
, type
, fd
->v
, fd
->step
);
3022 t
= build_gimple_modify_stmt (fd
->v
, t
);
3023 gimplify_and_add (t
, &list
);
3025 t
= build2 (fd
->cond_code
, boolean_type_node
, fd
->v
, e
);
3026 t
= get_formal_tmp_var (t
, &list
);
3027 t
= build3 (COND_EXPR
, void_type_node
, t
, NULL_TREE
, NULL_TREE
);
3028 append_to_statement_list (t
, &list
);
3030 si
= bsi_last (cont_bb
);
3031 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_CONTINUE
);
3032 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
3033 bsi_remove (&si
, true);
3035 /* Trip update code goes into TRIP_UPDATE_BB. */
3036 list
= alloc_stmt_list ();
3038 t
= build_int_cst (type
, 1);
3039 t
= build2 (PLUS_EXPR
, type
, trip
, t
);
3040 t
= build_gimple_modify_stmt (trip
, t
);
3041 gimplify_and_add (t
, &list
);
3043 si
= bsi_start (trip_update_bb
);
3044 bsi_insert_after (&si
, list
, BSI_CONTINUE_LINKING
);
3046 /* Replace the OMP_RETURN with a barrier, or nothing. */
3047 si
= bsi_last (exit_bb
);
3048 if (!OMP_RETURN_NOWAIT (bsi_stmt (si
)))
3050 list
= alloc_stmt_list ();
3051 build_omp_barrier (&list
);
3052 bsi_insert_after (&si
, list
, BSI_SAME_STMT
);
3054 bsi_remove (&si
, true);
3056 /* Connect the new blocks. */
3057 remove_edge (single_succ_edge (entry_bb
));
3058 make_edge (entry_bb
, iter_part_bb
, EDGE_FALLTHRU
);
3060 make_edge (iter_part_bb
, seq_start_bb
, EDGE_TRUE_VALUE
);
3061 make_edge (iter_part_bb
, fin_bb
, EDGE_FALSE_VALUE
);
3063 make_edge (seq_start_bb
, body_bb
, EDGE_FALLTHRU
);
3065 remove_edge (single_succ_edge (cont_bb
));
3066 make_edge (cont_bb
, body_bb
, EDGE_TRUE_VALUE
);
3067 make_edge (cont_bb
, trip_update_bb
, EDGE_FALSE_VALUE
);
3069 make_edge (trip_update_bb
, iter_part_bb
, EDGE_FALLTHRU
);
3073 /* Expand the OpenMP loop defined by REGION. */
3076 expand_omp_for (struct omp_region
*region
)
3078 struct omp_for_data fd
;
3080 push_gimplify_context ();
3082 extract_omp_for_data (last_stmt (region
->entry
), &fd
);
3083 region
->sched_kind
= fd
.sched_kind
;
3085 if (fd
.sched_kind
== OMP_CLAUSE_SCHEDULE_STATIC
3090 if (fd
.chunk_size
== NULL
)
3091 expand_omp_for_static_nochunk (region
, &fd
);
3093 expand_omp_for_static_chunk (region
, &fd
);
3097 int fn_index
= fd
.sched_kind
+ fd
.have_ordered
* 4;
3098 int start_ix
= BUILT_IN_GOMP_LOOP_STATIC_START
+ fn_index
;
3099 int next_ix
= BUILT_IN_GOMP_LOOP_STATIC_NEXT
+ fn_index
;
3100 expand_omp_for_generic (region
, &fd
, start_ix
, next_ix
);
3103 pop_gimplify_context (NULL
);
3107 /* Expand code for an OpenMP sections directive. In pseudo code, we generate
3109 v = GOMP_sections_start (n);
3126 v = GOMP_sections_next ();
3131 If this is a combined parallel sections, replace the call to
3132 GOMP_sections_start with 'goto L1'. */
3135 expand_omp_sections (struct omp_region
*region
)
3137 tree label_vec
, l0
, l1
, l2
, t
, u
, v
, sections_stmt
;
3139 basic_block entry_bb
, exit_bb
, l0_bb
, l1_bb
, l2_bb
, default_bb
;
3140 block_stmt_iterator si
;
3141 struct omp_region
*inner
;
3144 entry_bb
= region
->entry
;
3145 l0_bb
= create_empty_bb (entry_bb
);
3146 l0
= tree_block_label (l0_bb
);
3148 gcc_assert ((region
->cont
!= NULL
) ^ (region
->exit
== NULL
));
3149 l1_bb
= region
->cont
;
3152 l2_bb
= single_succ (l1_bb
);
3153 default_bb
= create_empty_bb (l1_bb
->prev_bb
);
3155 l1
= tree_block_label (l1_bb
);
3159 l2_bb
= create_empty_bb (l0_bb
);
3164 l2
= tree_block_label (l2_bb
);
3166 exit_bb
= region
->exit
;
3168 v
= create_tmp_var (unsigned_type_node
, ".section");
3170 /* We will build a switch() with enough cases for all the
3171 OMP_SECTION regions, a '0' case to handle the end of more work
3172 and a default case to abort if something goes wrong. */
3173 len
= EDGE_COUNT (entry_bb
->succs
);
3174 label_vec
= make_tree_vec (len
+ 2);
3176 /* The call to GOMP_sections_start goes in ENTRY_BB, replacing the
3177 OMP_SECTIONS statement. */
3178 si
= bsi_last (entry_bb
);
3179 sections_stmt
= bsi_stmt (si
);
3180 gcc_assert (TREE_CODE (sections_stmt
) == OMP_SECTIONS
);
3181 if (!is_combined_parallel (region
))
3183 /* If we are not inside a combined parallel+sections region,
3184 call GOMP_sections_start. */
3185 t
= build_int_cst (unsigned_type_node
, len
);
3186 u
= built_in_decls
[BUILT_IN_GOMP_SECTIONS_START
];
3187 t
= build_call_expr (u
, 1, t
);
3188 t
= build_gimple_modify_stmt (v
, t
);
3189 bsi_insert_after (&si
, t
, BSI_SAME_STMT
);
3191 bsi_remove (&si
, true);
3193 /* The switch() statement replacing OMP_SECTIONS goes in L0_BB. */
3194 si
= bsi_start (l0_bb
);
3196 t
= build3 (SWITCH_EXPR
, void_type_node
, v
, NULL
, label_vec
);
3197 bsi_insert_after (&si
, t
, BSI_CONTINUE_LINKING
);
3199 t
= build3 (CASE_LABEL_EXPR
, void_type_node
,
3200 build_int_cst (unsigned_type_node
, 0), NULL
, l2
);
3201 TREE_VEC_ELT (label_vec
, 0) = t
;
3202 make_edge (l0_bb
, l2_bb
, 0);
3204 /* Convert each OMP_SECTION into a CASE_LABEL_EXPR. */
3205 for (inner
= region
->inner
, i
= 1; inner
; inner
= inner
->next
, ++i
)
3207 basic_block s_entry_bb
, s_exit_bb
;
3209 s_entry_bb
= inner
->entry
;
3210 s_exit_bb
= inner
->exit
;
3212 t
= tree_block_label (s_entry_bb
);
3213 u
= build_int_cst (unsigned_type_node
, i
);
3214 u
= build3 (CASE_LABEL_EXPR
, void_type_node
, u
, NULL
, t
);
3215 TREE_VEC_ELT (label_vec
, i
) = u
;
3217 si
= bsi_last (s_entry_bb
);
3218 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_SECTION
);
3219 gcc_assert (i
< len
|| OMP_SECTION_LAST (bsi_stmt (si
)));
3220 bsi_remove (&si
, true);
3222 e
= single_pred_edge (s_entry_bb
);
3224 redirect_edge_pred (e
, l0_bb
);
3226 single_succ_edge (s_entry_bb
)->flags
= EDGE_FALLTHRU
;
3228 if (s_exit_bb
== NULL
)
3231 si
= bsi_last (s_exit_bb
);
3232 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_RETURN
);
3233 bsi_remove (&si
, true);
3235 single_succ_edge (s_exit_bb
)->flags
= EDGE_FALLTHRU
;
3238 /* Error handling code goes in DEFAULT_BB. */
3239 t
= tree_block_label (default_bb
);
3240 u
= build3 (CASE_LABEL_EXPR
, void_type_node
, NULL
, NULL
, t
);
3241 TREE_VEC_ELT (label_vec
, len
+ 1) = u
;
3242 make_edge (l0_bb
, default_bb
, 0);
3244 si
= bsi_start (default_bb
);
3245 t
= build_call_expr (built_in_decls
[BUILT_IN_TRAP
], 0);
3246 bsi_insert_after (&si
, t
, BSI_CONTINUE_LINKING
);
3248 /* Code to get the next section goes in L1_BB. */
3251 si
= bsi_last (l1_bb
);
3252 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_CONTINUE
);
3254 t
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_SECTIONS_NEXT
], 0);
3255 t
= build_gimple_modify_stmt (v
, t
);
3256 bsi_insert_after (&si
, t
, BSI_SAME_STMT
);
3257 bsi_remove (&si
, true);
3260 /* Cleanup function replaces OMP_RETURN in EXIT_BB. */
3263 si
= bsi_last (exit_bb
);
3264 if (OMP_RETURN_NOWAIT (bsi_stmt (si
)))
3265 t
= built_in_decls
[BUILT_IN_GOMP_SECTIONS_END_NOWAIT
];
3267 t
= built_in_decls
[BUILT_IN_GOMP_SECTIONS_END
];
3268 t
= build_call_expr (t
, 0);
3269 bsi_insert_after (&si
, t
, BSI_SAME_STMT
);
3270 bsi_remove (&si
, true);
3273 /* Connect the new blocks. */
3274 if (is_combined_parallel (region
))
3276 /* If this was a combined parallel+sections region, we did not
3277 emit a GOMP_sections_start in the entry block, so we just
3278 need to jump to L1_BB to get the next section. */
3279 make_edge (entry_bb
, l1_bb
, EDGE_FALLTHRU
);
3282 make_edge (entry_bb
, l0_bb
, EDGE_FALLTHRU
);
3286 e
= single_succ_edge (l1_bb
);
3287 redirect_edge_succ (e
, l0_bb
);
3288 e
->flags
= EDGE_FALLTHRU
;
3293 /* Expand code for an OpenMP single directive. We've already expanded
3294 much of the code, here we simply place the GOMP_barrier call. */
3297 expand_omp_single (struct omp_region
*region
)
3299 basic_block entry_bb
, exit_bb
;
3300 block_stmt_iterator si
;
3301 bool need_barrier
= false;
3303 entry_bb
= region
->entry
;
3304 exit_bb
= region
->exit
;
3306 si
= bsi_last (entry_bb
);
3307 /* The terminal barrier at the end of a GOMP_single_copy sequence cannot
3308 be removed. We need to ensure that the thread that entered the single
3309 does not exit before the data is copied out by the other threads. */
3310 if (find_omp_clause (OMP_SINGLE_CLAUSES (bsi_stmt (si
)),
3311 OMP_CLAUSE_COPYPRIVATE
))
3312 need_barrier
= true;
3313 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_SINGLE
);
3314 bsi_remove (&si
, true);
3315 single_succ_edge (entry_bb
)->flags
= EDGE_FALLTHRU
;
3317 si
= bsi_last (exit_bb
);
3318 if (!OMP_RETURN_NOWAIT (bsi_stmt (si
)) || need_barrier
)
3320 tree t
= alloc_stmt_list ();
3321 build_omp_barrier (&t
);
3322 bsi_insert_after (&si
, t
, BSI_SAME_STMT
);
3324 bsi_remove (&si
, true);
3325 single_succ_edge (exit_bb
)->flags
= EDGE_FALLTHRU
;
3329 /* Generic expansion for OpenMP synchronization directives: master,
3330 ordered and critical. All we need to do here is remove the entry
3331 and exit markers for REGION. */
3334 expand_omp_synch (struct omp_region
*region
)
3336 basic_block entry_bb
, exit_bb
;
3337 block_stmt_iterator si
;
3339 entry_bb
= region
->entry
;
3340 exit_bb
= region
->exit
;
3342 si
= bsi_last (entry_bb
);
3343 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_SINGLE
3344 || TREE_CODE (bsi_stmt (si
)) == OMP_MASTER
3345 || TREE_CODE (bsi_stmt (si
)) == OMP_ORDERED
3346 || TREE_CODE (bsi_stmt (si
)) == OMP_CRITICAL
);
3347 bsi_remove (&si
, true);
3348 single_succ_edge (entry_bb
)->flags
= EDGE_FALLTHRU
;
3352 si
= bsi_last (exit_bb
);
3353 gcc_assert (TREE_CODE (bsi_stmt (si
)) == OMP_RETURN
);
3354 bsi_remove (&si
, true);
3355 single_succ_edge (exit_bb
)->flags
= EDGE_FALLTHRU
;
3360 /* Expand the parallel region tree rooted at REGION. Expansion
3361 proceeds in depth-first order. Innermost regions are expanded
3362 first. This way, parallel regions that require a new function to
3363 be created (e.g., OMP_PARALLEL) can be expanded without having any
3364 internal dependencies in their body. */
3367 expand_omp (struct omp_region
*region
)
3372 expand_omp (region
->inner
);
3374 switch (region
->type
)
3377 expand_omp_parallel (region
);
3381 expand_omp_for (region
);
3385 expand_omp_sections (region
);
3389 /* Individual omp sections are handled together with their
3390 parent OMP_SECTIONS region. */
3394 expand_omp_single (region
);
3400 expand_omp_synch (region
);
3407 region
= region
->next
;
3412 /* Helper for build_omp_regions. Scan the dominator tree starting at
3413 block BB. PARENT is the region that contains BB. */
3416 build_omp_regions_1 (basic_block bb
, struct omp_region
*parent
)
3418 block_stmt_iterator si
;
3423 if (!bsi_end_p (si
) && OMP_DIRECTIVE_P (bsi_stmt (si
)))
3425 struct omp_region
*region
;
3426 enum tree_code code
;
3428 stmt
= bsi_stmt (si
);
3429 code
= TREE_CODE (stmt
);
3431 if (code
== OMP_RETURN
)
3433 /* STMT is the return point out of region PARENT. Mark it
3434 as the exit point and make PARENT the immediately
3435 enclosing region. */
3436 gcc_assert (parent
);
3439 parent
= parent
->outer
;
3441 /* If REGION is a parallel region, determine whether it is
3442 a combined parallel+workshare region. */
3443 if (region
->type
== OMP_PARALLEL
)
3444 determine_parallel_type (region
);
3446 else if (code
== OMP_CONTINUE
)
3448 gcc_assert (parent
);
3453 /* Otherwise, this directive becomes the parent for a new
3455 region
= new_omp_region (bb
, code
, parent
);
3460 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
3462 son
= next_dom_son (CDI_DOMINATORS
, son
))
3463 build_omp_regions_1 (son
, parent
);
3467 /* Scan the CFG and build a tree of OMP regions. Return the root of
3468 the OMP region tree. */
3471 build_omp_regions (void)
3473 gcc_assert (root_omp_region
== NULL
);
3474 calculate_dominance_info (CDI_DOMINATORS
);
3475 build_omp_regions_1 (ENTRY_BLOCK_PTR
, NULL
);
3479 /* Main entry point for expanding OMP-GIMPLE into runtime calls. */
3482 execute_expand_omp (void)
3484 build_omp_regions ();
3486 if (!root_omp_region
)
3491 fprintf (dump_file
, "\nOMP region tree\n\n");
3492 dump_omp_region (dump_file
, root_omp_region
, 0);
3493 fprintf (dump_file
, "\n");
3496 remove_exit_barriers (root_omp_region
);
3498 expand_omp (root_omp_region
);
3500 free_dominance_info (CDI_DOMINATORS
);
3501 free_dominance_info (CDI_POST_DOMINATORS
);
3502 cleanup_tree_cfg ();
3504 free_omp_regions ();
3510 gate_expand_omp (void)
3512 return flag_openmp
!= 0 && errorcount
== 0;
3515 struct tree_opt_pass pass_expand_omp
=
3517 "ompexp", /* name */
3518 gate_expand_omp
, /* gate */
3519 execute_expand_omp
, /* execute */
3522 0, /* static_pass_number */
3524 PROP_gimple_any
, /* properties_required */
3525 PROP_gimple_lomp
, /* properties_provided */
3526 0, /* properties_destroyed */
3527 0, /* todo_flags_start */
3528 TODO_dump_func
, /* todo_flags_finish */
3532 /* Routines to lower OpenMP directives into OMP-GIMPLE. */
3534 /* Lower the OpenMP sections directive in *STMT_P. */
3537 lower_omp_sections (tree
*stmt_p
, omp_context
*ctx
)
3539 tree new_stmt
, stmt
, body
, bind
, block
, ilist
, olist
, new_body
;
3541 tree_stmt_iterator tsi
;
3546 push_gimplify_context ();
3550 lower_rec_input_clauses (OMP_SECTIONS_CLAUSES (stmt
), &ilist
, &dlist
, ctx
);
3552 tsi
= tsi_start (OMP_SECTIONS_BODY (stmt
));
3553 for (len
= 0; !tsi_end_p (tsi
); len
++, tsi_next (&tsi
))
3556 tsi
= tsi_start (OMP_SECTIONS_BODY (stmt
));
3557 body
= alloc_stmt_list ();
3558 for (i
= 0; i
< len
; i
++, tsi_next (&tsi
))
3561 tree sec_start
, sec_end
;
3563 sec_start
= tsi_stmt (tsi
);
3564 sctx
= maybe_lookup_ctx (sec_start
);
3567 append_to_statement_list (sec_start
, &body
);
3569 lower_omp (&OMP_SECTION_BODY (sec_start
), sctx
);
3570 append_to_statement_list (OMP_SECTION_BODY (sec_start
), &body
);
3571 OMP_SECTION_BODY (sec_start
) = NULL
;
3575 tree l
= alloc_stmt_list ();
3576 lower_lastprivate_clauses (OMP_SECTIONS_CLAUSES (stmt
), NULL
,
3578 append_to_statement_list (l
, &body
);
3579 OMP_SECTION_LAST (sec_start
) = 1;
3582 sec_end
= make_node (OMP_RETURN
);
3583 append_to_statement_list (sec_end
, &body
);
3586 block
= make_node (BLOCK
);
3587 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, body
, block
);
3590 lower_reduction_clauses (OMP_SECTIONS_CLAUSES (stmt
), &olist
, ctx
);
3592 pop_gimplify_context (NULL_TREE
);
3593 record_vars_into (ctx
->block_vars
, ctx
->cb
.dst_fn
);
3595 new_stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
3596 TREE_SIDE_EFFECTS (new_stmt
) = 1;
3598 new_body
= alloc_stmt_list ();
3599 append_to_statement_list (ilist
, &new_body
);
3600 append_to_statement_list (stmt
, &new_body
);
3601 append_to_statement_list (bind
, &new_body
);
3603 t
= make_node (OMP_CONTINUE
);
3604 append_to_statement_list (t
, &new_body
);
3606 append_to_statement_list (olist
, &new_body
);
3607 append_to_statement_list (dlist
, &new_body
);
3609 maybe_catch_exception (&new_body
);
3611 t
= make_node (OMP_RETURN
);
3612 OMP_RETURN_NOWAIT (t
) = !!find_omp_clause (OMP_SECTIONS_CLAUSES (stmt
),
3614 append_to_statement_list (t
, &new_body
);
3616 BIND_EXPR_BODY (new_stmt
) = new_body
;
3617 OMP_SECTIONS_BODY (stmt
) = NULL
;
3623 /* A subroutine of lower_omp_single. Expand the simple form of
3624 an OMP_SINGLE, without a copyprivate clause:
3626 if (GOMP_single_start ())
3628 [ GOMP_barrier (); ] -> unless 'nowait' is present.
3630 FIXME. It may be better to delay expanding the logic of this until
3631 pass_expand_omp. The expanded logic may make the job more difficult
3632 to a synchronization analysis pass. */
3635 lower_omp_single_simple (tree single_stmt
, tree
*pre_p
)
3639 t
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_SINGLE_START
], 0);
3640 t
= build3 (COND_EXPR
, void_type_node
, t
,
3641 OMP_SINGLE_BODY (single_stmt
), NULL
);
3642 gimplify_and_add (t
, pre_p
);
3646 /* A subroutine of lower_omp_single. Expand the simple form of
3647 an OMP_SINGLE, with a copyprivate clause:
3649 #pragma omp single copyprivate (a, b, c)
3651 Create a new structure to hold copies of 'a', 'b' and 'c' and emit:
3654 if ((copyout_p = GOMP_single_copy_start ()) == NULL)
3660 GOMP_single_copy_end (©out);
3671 FIXME. It may be better to delay expanding the logic of this until
3672 pass_expand_omp. The expanded logic may make the job more difficult
3673 to a synchronization analysis pass. */
3676 lower_omp_single_copy (tree single_stmt
, tree
*pre_p
, omp_context
*ctx
)
3678 tree ptr_type
, t
, l0
, l1
, l2
, copyin_seq
;
3680 ctx
->sender_decl
= create_tmp_var (ctx
->record_type
, ".omp_copy_o");
3682 ptr_type
= build_pointer_type (ctx
->record_type
);
3683 ctx
->receiver_decl
= create_tmp_var (ptr_type
, ".omp_copy_i");
3685 l0
= create_artificial_label ();
3686 l1
= create_artificial_label ();
3687 l2
= create_artificial_label ();
3689 t
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_SINGLE_COPY_START
], 0);
3690 t
= fold_convert (ptr_type
, t
);
3691 t
= build_gimple_modify_stmt (ctx
->receiver_decl
, t
);
3692 gimplify_and_add (t
, pre_p
);
3694 t
= build2 (EQ_EXPR
, boolean_type_node
, ctx
->receiver_decl
,
3695 build_int_cst (ptr_type
, 0));
3696 t
= build3 (COND_EXPR
, void_type_node
, t
,
3697 build_and_jump (&l0
), build_and_jump (&l1
));
3698 gimplify_and_add (t
, pre_p
);
3700 t
= build1 (LABEL_EXPR
, void_type_node
, l0
);
3701 gimplify_and_add (t
, pre_p
);
3703 append_to_statement_list (OMP_SINGLE_BODY (single_stmt
), pre_p
);
3706 lower_copyprivate_clauses (OMP_SINGLE_CLAUSES (single_stmt
), pre_p
,
3709 t
= build_fold_addr_expr (ctx
->sender_decl
);
3710 t
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_SINGLE_COPY_END
], 1, t
);
3711 gimplify_and_add (t
, pre_p
);
3713 t
= build_and_jump (&l2
);
3714 gimplify_and_add (t
, pre_p
);
3716 t
= build1 (LABEL_EXPR
, void_type_node
, l1
);
3717 gimplify_and_add (t
, pre_p
);
3719 append_to_statement_list (copyin_seq
, pre_p
);
3721 t
= build1 (LABEL_EXPR
, void_type_node
, l2
);
3722 gimplify_and_add (t
, pre_p
);
3726 /* Expand code for an OpenMP single directive. */
3729 lower_omp_single (tree
*stmt_p
, omp_context
*ctx
)
3731 tree t
, bind
, block
, single_stmt
= *stmt_p
, dlist
;
3733 push_gimplify_context ();
3735 block
= make_node (BLOCK
);
3736 *stmt_p
= bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, block
);
3737 TREE_SIDE_EFFECTS (bind
) = 1;
3739 lower_rec_input_clauses (OMP_SINGLE_CLAUSES (single_stmt
),
3740 &BIND_EXPR_BODY (bind
), &dlist
, ctx
);
3741 lower_omp (&OMP_SINGLE_BODY (single_stmt
), ctx
);
3743 append_to_statement_list (single_stmt
, &BIND_EXPR_BODY (bind
));
3745 if (ctx
->record_type
)
3746 lower_omp_single_copy (single_stmt
, &BIND_EXPR_BODY (bind
), ctx
);
3748 lower_omp_single_simple (single_stmt
, &BIND_EXPR_BODY (bind
));
3750 OMP_SINGLE_BODY (single_stmt
) = NULL
;
3752 append_to_statement_list (dlist
, &BIND_EXPR_BODY (bind
));
3754 maybe_catch_exception (&BIND_EXPR_BODY (bind
));
3756 t
= make_node (OMP_RETURN
);
3757 OMP_RETURN_NOWAIT (t
) = !!find_omp_clause (OMP_SINGLE_CLAUSES (single_stmt
),
3759 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
3761 pop_gimplify_context (bind
);
3763 BIND_EXPR_VARS (bind
) = chainon (BIND_EXPR_VARS (bind
), ctx
->block_vars
);
3764 BLOCK_VARS (block
) = BIND_EXPR_VARS (bind
);
3768 /* Expand code for an OpenMP master directive. */
3771 lower_omp_master (tree
*stmt_p
, omp_context
*ctx
)
3773 tree bind
, block
, stmt
= *stmt_p
, lab
= NULL
, x
;
3775 push_gimplify_context ();
3777 block
= make_node (BLOCK
);
3778 *stmt_p
= bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, block
);
3779 TREE_SIDE_EFFECTS (bind
) = 1;
3781 append_to_statement_list (stmt
, &BIND_EXPR_BODY (bind
));
3783 x
= build_call_expr (built_in_decls
[BUILT_IN_OMP_GET_THREAD_NUM
], 0);
3784 x
= build2 (EQ_EXPR
, boolean_type_node
, x
, integer_zero_node
);
3785 x
= build3 (COND_EXPR
, void_type_node
, x
, NULL
, build_and_jump (&lab
));
3786 gimplify_and_add (x
, &BIND_EXPR_BODY (bind
));
3788 lower_omp (&OMP_MASTER_BODY (stmt
), ctx
);
3789 maybe_catch_exception (&OMP_MASTER_BODY (stmt
));
3790 append_to_statement_list (OMP_MASTER_BODY (stmt
), &BIND_EXPR_BODY (bind
));
3791 OMP_MASTER_BODY (stmt
) = NULL
;
3793 x
= build1 (LABEL_EXPR
, void_type_node
, lab
);
3794 gimplify_and_add (x
, &BIND_EXPR_BODY (bind
));
3796 x
= make_node (OMP_RETURN
);
3797 OMP_RETURN_NOWAIT (x
) = 1;
3798 append_to_statement_list (x
, &BIND_EXPR_BODY (bind
));
3800 pop_gimplify_context (bind
);
3802 BIND_EXPR_VARS (bind
) = chainon (BIND_EXPR_VARS (bind
), ctx
->block_vars
);
3803 BLOCK_VARS (block
) = BIND_EXPR_VARS (bind
);
3807 /* Expand code for an OpenMP ordered directive. */
3810 lower_omp_ordered (tree
*stmt_p
, omp_context
*ctx
)
3812 tree bind
, block
, stmt
= *stmt_p
, x
;
3814 push_gimplify_context ();
3816 block
= make_node (BLOCK
);
3817 *stmt_p
= bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, block
);
3818 TREE_SIDE_EFFECTS (bind
) = 1;
3820 append_to_statement_list (stmt
, &BIND_EXPR_BODY (bind
));
3822 x
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_ORDERED_START
], 0);
3823 gimplify_and_add (x
, &BIND_EXPR_BODY (bind
));
3825 lower_omp (&OMP_ORDERED_BODY (stmt
), ctx
);
3826 maybe_catch_exception (&OMP_ORDERED_BODY (stmt
));
3827 append_to_statement_list (OMP_ORDERED_BODY (stmt
), &BIND_EXPR_BODY (bind
));
3828 OMP_ORDERED_BODY (stmt
) = NULL
;
3830 x
= build_call_expr (built_in_decls
[BUILT_IN_GOMP_ORDERED_END
], 0);
3831 gimplify_and_add (x
, &BIND_EXPR_BODY (bind
));
3833 x
= make_node (OMP_RETURN
);
3834 OMP_RETURN_NOWAIT (x
) = 1;
3835 append_to_statement_list (x
, &BIND_EXPR_BODY (bind
));
3837 pop_gimplify_context (bind
);
3839 BIND_EXPR_VARS (bind
) = chainon (BIND_EXPR_VARS (bind
), ctx
->block_vars
);
3840 BLOCK_VARS (block
) = BIND_EXPR_VARS (bind
);
3844 /* Gimplify an OMP_CRITICAL statement. This is a relatively simple
3845 substitution of a couple of function calls. But in the NAMED case,
3846 requires that languages coordinate a symbol name. It is therefore
3847 best put here in common code. */
3849 static GTY((param1_is (tree
), param2_is (tree
)))
3850 splay_tree critical_name_mutexes
;
3853 lower_omp_critical (tree
*stmt_p
, omp_context
*ctx
)
3855 tree bind
, block
, stmt
= *stmt_p
;
3856 tree t
, lock
, unlock
, name
;
3858 name
= OMP_CRITICAL_NAME (stmt
);
3864 if (!critical_name_mutexes
)
3865 critical_name_mutexes
3866 = splay_tree_new_ggc (splay_tree_compare_pointers
);
3868 n
= splay_tree_lookup (critical_name_mutexes
, (splay_tree_key
) name
);
3873 decl
= create_tmp_var_raw (ptr_type_node
, NULL
);
3875 new_str
= ACONCAT ((".gomp_critical_user_",
3876 IDENTIFIER_POINTER (name
), NULL
));
3877 DECL_NAME (decl
) = get_identifier (new_str
);
3878 TREE_PUBLIC (decl
) = 1;
3879 TREE_STATIC (decl
) = 1;
3880 DECL_COMMON (decl
) = 1;
3881 DECL_ARTIFICIAL (decl
) = 1;
3882 DECL_IGNORED_P (decl
) = 1;
3883 varpool_finalize_decl (decl
);
3885 splay_tree_insert (critical_name_mutexes
, (splay_tree_key
) name
,
3886 (splay_tree_value
) decl
);
3889 decl
= (tree
) n
->value
;
3891 lock
= built_in_decls
[BUILT_IN_GOMP_CRITICAL_NAME_START
];
3892 lock
= build_call_expr (lock
, 1, build_fold_addr_expr (decl
));
3894 unlock
= built_in_decls
[BUILT_IN_GOMP_CRITICAL_NAME_END
];
3895 unlock
= build_call_expr (unlock
, 1, build_fold_addr_expr (decl
));
3899 lock
= built_in_decls
[BUILT_IN_GOMP_CRITICAL_START
];
3900 lock
= build_call_expr (lock
, 0);
3902 unlock
= built_in_decls
[BUILT_IN_GOMP_CRITICAL_END
];
3903 unlock
= build_call_expr (unlock
, 0);
3906 push_gimplify_context ();
3908 block
= make_node (BLOCK
);
3909 *stmt_p
= bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, block
);
3910 TREE_SIDE_EFFECTS (bind
) = 1;
3912 append_to_statement_list (stmt
, &BIND_EXPR_BODY (bind
));
3914 gimplify_and_add (lock
, &BIND_EXPR_BODY (bind
));
3916 lower_omp (&OMP_CRITICAL_BODY (stmt
), ctx
);
3917 maybe_catch_exception (&OMP_CRITICAL_BODY (stmt
));
3918 append_to_statement_list (OMP_CRITICAL_BODY (stmt
), &BIND_EXPR_BODY (bind
));
3919 OMP_CRITICAL_BODY (stmt
) = NULL
;
3921 gimplify_and_add (unlock
, &BIND_EXPR_BODY (bind
));
3923 t
= make_node (OMP_RETURN
);
3924 OMP_RETURN_NOWAIT (t
) = 1;
3925 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
3927 pop_gimplify_context (bind
);
3928 BIND_EXPR_VARS (bind
) = chainon (BIND_EXPR_VARS (bind
), ctx
->block_vars
);
3929 BLOCK_VARS (block
) = BIND_EXPR_VARS (bind
);
3933 /* A subroutine of lower_omp_for. Generate code to emit the predicate
3934 for a lastprivate clause. Given a loop control predicate of (V
3935 cond N2), we gate the clause on (!(V cond N2)). The lowered form
3936 is appended to *DLIST, iterator initialization is appended to
3940 lower_omp_for_lastprivate (struct omp_for_data
*fd
, tree
*body_p
,
3941 tree
*dlist
, struct omp_context
*ctx
)
3943 tree clauses
, cond
, stmts
, vinit
, t
;
3944 enum tree_code cond_code
;
3946 cond_code
= fd
->cond_code
;
3947 cond_code
= cond_code
== LT_EXPR
? GE_EXPR
: LE_EXPR
;
3949 /* When possible, use a strict equality expression. This can let VRP
3950 type optimizations deduce the value and remove a copy. */
3951 if (host_integerp (fd
->step
, 0))
3953 HOST_WIDE_INT step
= TREE_INT_CST_LOW (fd
->step
);
3954 if (step
== 1 || step
== -1)
3955 cond_code
= EQ_EXPR
;
3958 cond
= build2 (cond_code
, boolean_type_node
, fd
->v
, fd
->n2
);
3960 clauses
= OMP_FOR_CLAUSES (fd
->for_stmt
);
3962 lower_lastprivate_clauses (clauses
, cond
, &stmts
, ctx
);
3965 append_to_statement_list (stmts
, dlist
);
3967 /* Optimize: v = 0; is usually cheaper than v = some_other_constant. */
3969 if (cond_code
== EQ_EXPR
3970 && host_integerp (fd
->n2
, 0)
3971 && ! integer_zerop (fd
->n2
))
3972 vinit
= build_int_cst (TREE_TYPE (fd
->v
), 0);
3974 /* Initialize the iterator variable, so that threads that don't execute
3975 any iterations don't execute the lastprivate clauses by accident. */
3976 t
= build_gimple_modify_stmt (fd
->v
, vinit
);
3977 gimplify_and_add (t
, body_p
);
3982 /* Lower code for an OpenMP loop directive. */
3985 lower_omp_for (tree
*stmt_p
, omp_context
*ctx
)
3987 tree t
, stmt
, ilist
, dlist
, new_stmt
, *body_p
, *rhs_p
;
3988 struct omp_for_data fd
;
3992 push_gimplify_context ();
3994 lower_omp (&OMP_FOR_PRE_BODY (stmt
), ctx
);
3995 lower_omp (&OMP_FOR_BODY (stmt
), ctx
);
3997 /* Move declaration of temporaries in the loop body before we make
3999 if (TREE_CODE (OMP_FOR_BODY (stmt
)) == BIND_EXPR
)
4000 record_vars_into (BIND_EXPR_VARS (OMP_FOR_BODY (stmt
)), ctx
->cb
.dst_fn
);
4002 new_stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
4003 TREE_SIDE_EFFECTS (new_stmt
) = 1;
4004 body_p
= &BIND_EXPR_BODY (new_stmt
);
4006 /* The pre-body and input clauses go before the lowered OMP_FOR. */
4009 append_to_statement_list (OMP_FOR_PRE_BODY (stmt
), body_p
);
4010 lower_rec_input_clauses (OMP_FOR_CLAUSES (stmt
), body_p
, &dlist
, ctx
);
4012 /* Lower the header expressions. At this point, we can assume that
4013 the header is of the form:
4015 #pragma omp for (V = VAL1; V {<|>|<=|>=} VAL2; V = V [+-] VAL3)
4017 We just need to make sure that VAL1, VAL2 and VAL3 are lowered
4018 using the .omp_data_s mapping, if needed. */
4019 rhs_p
= &GIMPLE_STMT_OPERAND (OMP_FOR_INIT (stmt
), 1);
4020 if (!is_gimple_min_invariant (*rhs_p
))
4021 *rhs_p
= get_formal_tmp_var (*rhs_p
, body_p
);
4023 rhs_p
= &TREE_OPERAND (OMP_FOR_COND (stmt
), 1);
4024 if (!is_gimple_min_invariant (*rhs_p
))
4025 *rhs_p
= get_formal_tmp_var (*rhs_p
, body_p
);
4027 rhs_p
= &TREE_OPERAND (GIMPLE_STMT_OPERAND (OMP_FOR_INCR (stmt
), 1), 1);
4028 if (!is_gimple_min_invariant (*rhs_p
))
4029 *rhs_p
= get_formal_tmp_var (*rhs_p
, body_p
);
4031 /* Once lowered, extract the bounds and clauses. */
4032 extract_omp_for_data (stmt
, &fd
);
4034 lower_omp_for_lastprivate (&fd
, body_p
, &dlist
, ctx
);
4036 append_to_statement_list (stmt
, body_p
);
4038 append_to_statement_list (OMP_FOR_BODY (stmt
), body_p
);
4040 t
= make_node (OMP_CONTINUE
);
4041 append_to_statement_list (t
, body_p
);
4043 /* After the loop, add exit clauses. */
4044 lower_reduction_clauses (OMP_FOR_CLAUSES (stmt
), body_p
, ctx
);
4045 append_to_statement_list (dlist
, body_p
);
4047 maybe_catch_exception (body_p
);
4049 /* Region exit marker goes at the end of the loop body. */
4050 t
= make_node (OMP_RETURN
);
4051 OMP_RETURN_NOWAIT (t
) = fd
.have_nowait
;
4052 append_to_statement_list (t
, body_p
);
4054 pop_gimplify_context (NULL_TREE
);
4055 record_vars_into (ctx
->block_vars
, ctx
->cb
.dst_fn
);
4057 OMP_FOR_BODY (stmt
) = NULL_TREE
;
4058 OMP_FOR_PRE_BODY (stmt
) = NULL_TREE
;
4063 /* Lower the OpenMP parallel directive in *STMT_P. CTX holds context
4064 information for the directive. */
4067 lower_omp_parallel (tree
*stmt_p
, omp_context
*ctx
)
4069 tree clauses
, par_bind
, par_body
, new_body
, bind
;
4070 tree olist
, ilist
, par_olist
, par_ilist
;
4071 tree stmt
, child_fn
, t
;
4075 clauses
= OMP_PARALLEL_CLAUSES (stmt
);
4076 par_bind
= OMP_PARALLEL_BODY (stmt
);
4077 par_body
= BIND_EXPR_BODY (par_bind
);
4078 child_fn
= ctx
->cb
.dst_fn
;
4080 push_gimplify_context ();
4082 par_olist
= NULL_TREE
;
4083 par_ilist
= NULL_TREE
;
4084 lower_rec_input_clauses (clauses
, &par_ilist
, &par_olist
, ctx
);
4085 lower_omp (&par_body
, ctx
);
4086 lower_reduction_clauses (clauses
, &par_olist
, ctx
);
4088 /* Declare all the variables created by mapping and the variables
4089 declared in the scope of the parallel body. */
4090 record_vars_into (ctx
->block_vars
, child_fn
);
4091 record_vars_into (BIND_EXPR_VARS (par_bind
), child_fn
);
4093 if (ctx
->record_type
)
4095 ctx
->sender_decl
= create_tmp_var (ctx
->record_type
, ".omp_data_o");
4096 OMP_PARALLEL_DATA_ARG (stmt
) = ctx
->sender_decl
;
4101 lower_send_clauses (clauses
, &ilist
, &olist
, ctx
);
4102 lower_send_shared_vars (&ilist
, &olist
, ctx
);
4104 /* Once all the expansions are done, sequence all the different
4105 fragments inside OMP_PARALLEL_BODY. */
4106 bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, NULL
, NULL
);
4107 append_to_statement_list (ilist
, &BIND_EXPR_BODY (bind
));
4109 new_body
= alloc_stmt_list ();
4111 if (ctx
->record_type
)
4113 t
= build_fold_addr_expr (ctx
->sender_decl
);
4114 /* fixup_child_record_type might have changed receiver_decl's type. */
4115 t
= fold_convert (TREE_TYPE (ctx
->receiver_decl
), t
);
4116 t
= build_gimple_modify_stmt (ctx
->receiver_decl
, t
);
4117 append_to_statement_list (t
, &new_body
);
4120 append_to_statement_list (par_ilist
, &new_body
);
4121 append_to_statement_list (par_body
, &new_body
);
4122 append_to_statement_list (par_olist
, &new_body
);
4123 maybe_catch_exception (&new_body
);
4124 t
= make_node (OMP_RETURN
);
4125 append_to_statement_list (t
, &new_body
);
4126 OMP_PARALLEL_BODY (stmt
) = new_body
;
4128 append_to_statement_list (stmt
, &BIND_EXPR_BODY (bind
));
4129 append_to_statement_list (olist
, &BIND_EXPR_BODY (bind
));
4133 pop_gimplify_context (NULL_TREE
);
4137 /* Pass *TP back through the gimplifier within the context determined by WI.
4138 This handles replacement of DECL_VALUE_EXPR, as well as adjusting the
4139 flags on ADDR_EXPR. */
4142 lower_regimplify (tree
*tp
, struct walk_stmt_info
*wi
)
4144 enum gimplify_status gs
;
4148 gs
= gimplify_expr (tp
, &pre
, NULL
, is_gimple_lvalue
, fb_lvalue
);
4149 else if (wi
->val_only
)
4150 gs
= gimplify_expr (tp
, &pre
, NULL
, is_gimple_val
, fb_rvalue
);
4152 gs
= gimplify_expr (tp
, &pre
, NULL
, is_gimple_formal_tmp_var
, fb_rvalue
);
4153 gcc_assert (gs
== GS_ALL_DONE
);
4156 tsi_link_before (&wi
->tsi
, pre
, TSI_SAME_STMT
);
4159 /* Copy EXP into a temporary. Insert the initialization statement before TSI. */
4162 init_tmp_var (tree exp
, tree_stmt_iterator
*tsi
)
4166 t
= create_tmp_var (TREE_TYPE (exp
), NULL
);
4167 DECL_GIMPLE_REG_P (t
) = 1;
4168 stmt
= build_gimple_modify_stmt (t
, exp
);
4169 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
4170 tsi_link_before (tsi
, stmt
, TSI_SAME_STMT
);
4175 /* Similarly, but copy from the temporary and insert the statement
4176 after the iterator. */
4179 save_tmp_var (tree exp
, tree_stmt_iterator
*tsi
)
4183 t
= create_tmp_var (TREE_TYPE (exp
), NULL
);
4184 DECL_GIMPLE_REG_P (t
) = 1;
4185 stmt
= build_gimple_modify_stmt (exp
, t
);
4186 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
4187 tsi_link_after (tsi
, stmt
, TSI_SAME_STMT
);
4192 /* Callback for walk_stmts. Lower the OpenMP directive pointed by TP. */
4195 lower_omp_1 (tree
*tp
, int *walk_subtrees
, void *data
)
4197 struct walk_stmt_info
*wi
= data
;
4198 omp_context
*ctx
= wi
->info
;
4201 /* If we have issued syntax errors, avoid doing any heavy lifting.
4202 Just replace the OpenMP directives with a NOP to avoid
4203 confusing RTL expansion. */
4204 if (errorcount
&& OMP_DIRECTIVE_P (*tp
))
4206 *tp
= build_empty_stmt ();
4211 switch (TREE_CODE (*tp
))
4214 ctx
= maybe_lookup_ctx (t
);
4215 lower_omp_parallel (tp
, ctx
);
4219 ctx
= maybe_lookup_ctx (t
);
4221 lower_omp_for (tp
, ctx
);
4225 ctx
= maybe_lookup_ctx (t
);
4227 lower_omp_sections (tp
, ctx
);
4231 ctx
= maybe_lookup_ctx (t
);
4233 lower_omp_single (tp
, ctx
);
4237 ctx
= maybe_lookup_ctx (t
);
4239 lower_omp_master (tp
, ctx
);
4243 ctx
= maybe_lookup_ctx (t
);
4245 lower_omp_ordered (tp
, ctx
);
4249 ctx
= maybe_lookup_ctx (t
);
4251 lower_omp_critical (tp
, ctx
);
4255 if (ctx
&& DECL_HAS_VALUE_EXPR_P (t
))
4257 lower_regimplify (&t
, wi
);
4261 t
= save_tmp_var (t
, &wi
->tsi
);
4263 t
= init_tmp_var (t
, &wi
->tsi
);
4271 lower_regimplify (tp
, wi
);
4275 case ARRAY_RANGE_REF
:
4279 case VIEW_CONVERT_EXPR
:
4281 lower_regimplify (tp
, wi
);
4288 wi
->val_only
= true;
4289 lower_regimplify (&TREE_OPERAND (t
, 0), wi
);
4294 if (!TYPE_P (t
) && !DECL_P (t
))
4303 lower_omp (tree
*stmt_p
, omp_context
*ctx
)
4305 struct walk_stmt_info wi
;
4307 memset (&wi
, 0, sizeof (wi
));
4308 wi
.callback
= lower_omp_1
;
4311 wi
.want_locations
= true;
4313 walk_stmts (&wi
, stmt_p
);
4316 /* Main entry point. */
4319 execute_lower_omp (void)
4321 all_contexts
= splay_tree_new (splay_tree_compare_pointers
, 0,
4322 delete_omp_context
);
4324 scan_omp (&DECL_SAVED_TREE (current_function_decl
), NULL
);
4325 gcc_assert (parallel_nesting_level
== 0);
4327 if (all_contexts
->root
)
4328 lower_omp (&DECL_SAVED_TREE (current_function_decl
), NULL
);
4332 splay_tree_delete (all_contexts
);
4333 all_contexts
= NULL
;
4339 gate_lower_omp (void)
4341 return flag_openmp
!= 0;
4344 struct tree_opt_pass pass_lower_omp
=
4346 "omplower", /* name */
4347 gate_lower_omp
, /* gate */
4348 execute_lower_omp
, /* execute */
4351 0, /* static_pass_number */
4353 PROP_gimple_any
, /* properties_required */
4354 PROP_gimple_lomp
, /* properties_provided */
4355 0, /* properties_destroyed */
4356 0, /* todo_flags_start */
4357 TODO_dump_func
, /* todo_flags_finish */
4361 /* The following is a utility to diagnose OpenMP structured block violations.
4362 It is not part of the "omplower" pass, as that's invoked too late. It
4363 should be invoked by the respective front ends after gimplification. */
4365 static splay_tree all_labels
;
4367 /* Check for mismatched contexts and generate an error if needed. Return
4368 true if an error is detected. */
4371 diagnose_sb_0 (tree
*stmt_p
, tree branch_ctx
, tree label_ctx
)
4375 if ((label_ctx
? TREE_VALUE (label_ctx
) : NULL
) == branch_ctx
)
4378 /* Try to avoid confusing the user by producing and error message
4379 with correct "exit" or "enter" verbage. We prefer "exit"
4380 unless we can show that LABEL_CTX is nested within BRANCH_CTX. */
4381 if (branch_ctx
== NULL
)
4387 if (TREE_VALUE (label_ctx
) == branch_ctx
)
4392 label_ctx
= TREE_CHAIN (label_ctx
);
4397 error ("invalid exit from OpenMP structured block");
4399 error ("invalid entry to OpenMP structured block");
4401 *stmt_p
= build_empty_stmt ();
4405 /* Pass 1: Create a minimal tree of OpenMP structured blocks, and record
4406 where in the tree each label is found. */
4409 diagnose_sb_1 (tree
*tp
, int *walk_subtrees
, void *data
)
4411 struct walk_stmt_info
*wi
= data
;
4412 tree context
= (tree
) wi
->info
;
4417 switch (TREE_CODE (t
))
4422 walk_tree (&OMP_CLAUSES (t
), diagnose_sb_1
, wi
, NULL
);
4428 /* The minimal context here is just a tree of statements. */
4429 inner_context
= tree_cons (NULL
, t
, context
);
4430 wi
->info
= inner_context
;
4431 walk_stmts (wi
, &OMP_BODY (t
));
4436 walk_tree (&OMP_FOR_CLAUSES (t
), diagnose_sb_1
, wi
, NULL
);
4437 inner_context
= tree_cons (NULL
, t
, context
);
4438 wi
->info
= inner_context
;
4439 walk_tree (&OMP_FOR_INIT (t
), diagnose_sb_1
, wi
, NULL
);
4440 walk_tree (&OMP_FOR_COND (t
), diagnose_sb_1
, wi
, NULL
);
4441 walk_tree (&OMP_FOR_INCR (t
), diagnose_sb_1
, wi
, NULL
);
4442 walk_stmts (wi
, &OMP_FOR_PRE_BODY (t
));
4443 walk_stmts (wi
, &OMP_FOR_BODY (t
));
4448 splay_tree_insert (all_labels
, (splay_tree_key
) LABEL_EXPR_LABEL (t
),
4449 (splay_tree_value
) context
);
4459 /* Pass 2: Check each branch and see if its context differs from that of
4460 the destination label's context. */
4463 diagnose_sb_2 (tree
*tp
, int *walk_subtrees
, void *data
)
4465 struct walk_stmt_info
*wi
= data
;
4466 tree context
= (tree
) wi
->info
;
4471 switch (TREE_CODE (t
))
4476 walk_tree (&OMP_CLAUSES (t
), diagnose_sb_2
, wi
, NULL
);
4483 walk_stmts (wi
, &OMP_BODY (t
));
4488 walk_tree (&OMP_FOR_CLAUSES (t
), diagnose_sb_2
, wi
, NULL
);
4490 walk_tree (&OMP_FOR_INIT (t
), diagnose_sb_2
, wi
, NULL
);
4491 walk_tree (&OMP_FOR_COND (t
), diagnose_sb_2
, wi
, NULL
);
4492 walk_tree (&OMP_FOR_INCR (t
), diagnose_sb_2
, wi
, NULL
);
4493 walk_stmts (wi
, &OMP_FOR_PRE_BODY (t
));
4494 walk_stmts (wi
, &OMP_FOR_BODY (t
));
4500 tree lab
= GOTO_DESTINATION (t
);
4501 if (TREE_CODE (lab
) != LABEL_DECL
)
4504 n
= splay_tree_lookup (all_labels
, (splay_tree_key
) lab
);
4505 diagnose_sb_0 (tp
, context
, n
? (tree
) n
->value
: NULL_TREE
);
4511 tree vec
= SWITCH_LABELS (t
);
4512 int i
, len
= TREE_VEC_LENGTH (vec
);
4513 for (i
= 0; i
< len
; ++i
)
4515 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
4516 n
= splay_tree_lookup (all_labels
, (splay_tree_key
) lab
);
4517 if (diagnose_sb_0 (tp
, context
, (tree
) n
->value
))
4524 diagnose_sb_0 (tp
, context
, NULL_TREE
);
4535 diagnose_omp_structured_block_errors (tree fndecl
)
4537 tree save_current
= current_function_decl
;
4538 struct walk_stmt_info wi
;
4540 current_function_decl
= fndecl
;
4542 all_labels
= splay_tree_new (splay_tree_compare_pointers
, 0, 0);
4544 memset (&wi
, 0, sizeof (wi
));
4545 wi
.callback
= diagnose_sb_1
;
4546 walk_stmts (&wi
, &DECL_SAVED_TREE (fndecl
));
4548 memset (&wi
, 0, sizeof (wi
));
4549 wi
.callback
= diagnose_sb_2
;
4550 wi
.want_locations
= true;
4551 wi
.want_return_expr
= true;
4552 walk_stmts (&wi
, &DECL_SAVED_TREE (fndecl
));
4554 splay_tree_delete (all_labels
);
4557 current_function_decl
= save_current
;
4560 #include "gt-omp-low.h"