1 /* Loop autoparallelization.
2 Copyright (C) 2006-2018 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
4 Zdenek Dvorak <dvorakz@suse.cz> and Razya Ladelsky <razya@il.ibm.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "tree-pass.h"
32 #include "gimple-pretty-print.h"
33 #include "fold-const.h"
35 #include "gimple-iterator.h"
36 #include "gimplify-me.h"
37 #include "gimple-walk.h"
38 #include "stor-layout.h"
39 #include "tree-nested.h"
41 #include "tree-ssa-loop-ivopts.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "tree-into-ssa.h"
47 #include "tree-scalar-evolution.h"
48 #include "langhooks.h"
49 #include "tree-vectorizer.h"
50 #include "tree-hasher.h"
51 #include "tree-parloops.h"
52 #include "omp-general.h"
56 #include "params-enum.h"
57 #include "tree-ssa-alias.h"
59 #include "gomp-constants.h"
61 #include "stringpool.h"
64 /* This pass tries to distribute iterations of loops into several threads.
65 The implementation is straightforward -- for each loop we test whether its
66 iterations are independent, and if it is the case (and some additional
67 conditions regarding profitability and correctness are satisfied), we
68 add GIMPLE_OMP_PARALLEL and GIMPLE_OMP_FOR codes and let omp expansion
71 The most of the complexity is in bringing the code into shape expected
73 -- for GIMPLE_OMP_FOR, ensuring that the loop has only one induction
74 variable and that the exit test is at the start of the loop body
75 -- for GIMPLE_OMP_PARALLEL, replacing the references to local addressable
76 variables by accesses through pointers, and breaking up ssa chains
77 by storing the values incoming to the parallelized loop to a structure
78 passed to the new function as an argument (something similar is done
79 in omp gimplification, unfortunately only a small part of the code
83 -- if there are several parallelizable loops in a function, it may be
84 possible to generate the threads just once (using synchronization to
85 ensure that cross-loop dependences are obeyed).
86 -- handling of common reduction patterns for outer loops.
88 More info can also be found at http://gcc.gnu.org/wiki/AutoParInGCC */
91 currently we use vect_force_simple_reduction() to detect reduction patterns.
92 The code transformation will be introduced by an example.
99 for (i = 0; i < N; i++)
109 # sum_29 = PHI <sum_11(5), 1(3)>
110 # i_28 = PHI <i_12(5), 0(3)>
113 sum_11 = D.1795_8 + sum_29;
121 # sum_21 = PHI <sum_11(4)>
122 printf (&"%d"[0], sum_21);
125 after reduction transformation (only relevant parts):
133 # Storing the initial value given by the user. #
135 .paral_data_store.32.sum.27 = 1;
137 #pragma omp parallel num_threads(4)
139 #pragma omp for schedule(static)
141 # The neutral element corresponding to the particular
142 reduction's operation, e.g. 0 for PLUS_EXPR,
143 1 for MULT_EXPR, etc. replaces the user's initial value. #
145 # sum.27_29 = PHI <sum.27_11, 0>
147 sum.27_11 = D.1827_8 + sum.27_29;
151 # Adding this reduction phi is done at create_phi_for_local_result() #
152 # sum.27_56 = PHI <sum.27_11, 0>
155 # Creating the atomic operation is done at
156 create_call_for_reduction_1() #
158 #pragma omp atomic_load
159 D.1839_59 = *&.paral_data_load.33_51->reduction.23;
160 D.1840_60 = sum.27_56 + D.1839_59;
161 #pragma omp atomic_store (D.1840_60);
165 # collecting the result after the join of the threads is done at
166 create_loads_for_reductions().
167 The value computed by the threads is loaded from the
171 .paral_data_load.33_52 = &.paral_data_store.32;
172 sum_37 = .paral_data_load.33_52->sum.27;
173 sum_43 = D.1795_41 + sum_37;
176 # sum_21 = PHI <sum_43, sum_26>
177 printf (&"%d"[0], sum_21);
185 /* Minimal number of iterations of a loop that should be executed in each
187 #define MIN_PER_THREAD PARAM_VALUE (PARAM_PARLOOPS_MIN_PER_THREAD)
189 /* Element of the hashtable, representing a
190 reduction in the current loop. */
191 struct reduction_info
193 gimple
*reduc_stmt
; /* reduction statement. */
194 gimple
*reduc_phi
; /* The phi node defining the reduction. */
195 enum tree_code reduction_code
;/* code for the reduction operation. */
196 unsigned reduc_version
; /* SSA_NAME_VERSION of original reduc_phi
198 gphi
*keep_res
; /* The PHI_RESULT of this phi is the resulting value
199 of the reduction variable when existing the loop. */
200 tree initial_value
; /* The initial value of the reduction var before entering the loop. */
201 tree field
; /* the name of the field in the parloop data structure intended for reduction. */
202 tree reduc_addr
; /* The address of the reduction variable for
203 openacc reductions. */
204 tree init
; /* reduction initialization value. */
205 gphi
*new_phi
; /* (helper field) Newly created phi node whose result
206 will be passed to the atomic operation. Represents
207 the local result each thread computed for the reduction
211 /* Reduction info hashtable helpers. */
213 struct reduction_hasher
: free_ptr_hash
<reduction_info
>
215 static inline hashval_t
hash (const reduction_info
*);
216 static inline bool equal (const reduction_info
*, const reduction_info
*);
219 /* Equality and hash functions for hashtab code. */
222 reduction_hasher::equal (const reduction_info
*a
, const reduction_info
*b
)
224 return (a
->reduc_phi
== b
->reduc_phi
);
228 reduction_hasher::hash (const reduction_info
*a
)
230 return a
->reduc_version
;
233 typedef hash_table
<reduction_hasher
> reduction_info_table_type
;
236 static struct reduction_info
*
237 reduction_phi (reduction_info_table_type
*reduction_list
, gimple
*phi
)
239 struct reduction_info tmpred
, *red
;
241 if (reduction_list
->elements () == 0 || phi
== NULL
)
244 if (gimple_uid (phi
) == (unsigned int)-1
245 || gimple_uid (phi
) == 0)
248 tmpred
.reduc_phi
= phi
;
249 tmpred
.reduc_version
= gimple_uid (phi
);
250 red
= reduction_list
->find (&tmpred
);
251 gcc_assert (red
== NULL
|| red
->reduc_phi
== phi
);
256 /* Element of hashtable of names to copy. */
258 struct name_to_copy_elt
260 unsigned version
; /* The version of the name to copy. */
261 tree new_name
; /* The new name used in the copy. */
262 tree field
; /* The field of the structure used to pass the
266 /* Name copies hashtable helpers. */
268 struct name_to_copy_hasher
: free_ptr_hash
<name_to_copy_elt
>
270 static inline hashval_t
hash (const name_to_copy_elt
*);
271 static inline bool equal (const name_to_copy_elt
*, const name_to_copy_elt
*);
274 /* Equality and hash functions for hashtab code. */
277 name_to_copy_hasher::equal (const name_to_copy_elt
*a
, const name_to_copy_elt
*b
)
279 return a
->version
== b
->version
;
283 name_to_copy_hasher::hash (const name_to_copy_elt
*a
)
285 return (hashval_t
) a
->version
;
288 typedef hash_table
<name_to_copy_hasher
> name_to_copy_table_type
;
290 /* A transformation matrix, which is a self-contained ROWSIZE x COLSIZE
291 matrix. Rather than use floats, we simply keep a single DENOMINATOR that
292 represents the denominator for every element in the matrix. */
293 typedef struct lambda_trans_matrix_s
295 lambda_matrix matrix
;
299 } *lambda_trans_matrix
;
300 #define LTM_MATRIX(T) ((T)->matrix)
301 #define LTM_ROWSIZE(T) ((T)->rowsize)
302 #define LTM_COLSIZE(T) ((T)->colsize)
303 #define LTM_DENOMINATOR(T) ((T)->denominator)
305 /* Allocate a new transformation matrix. */
307 static lambda_trans_matrix
308 lambda_trans_matrix_new (int colsize
, int rowsize
,
309 struct obstack
* lambda_obstack
)
311 lambda_trans_matrix ret
;
313 ret
= (lambda_trans_matrix
)
314 obstack_alloc (lambda_obstack
, sizeof (struct lambda_trans_matrix_s
));
315 LTM_MATRIX (ret
) = lambda_matrix_new (rowsize
, colsize
, lambda_obstack
);
316 LTM_ROWSIZE (ret
) = rowsize
;
317 LTM_COLSIZE (ret
) = colsize
;
318 LTM_DENOMINATOR (ret
) = 1;
322 /* Multiply a vector VEC by a matrix MAT.
323 MAT is an M*N matrix, and VEC is a vector with length N. The result
324 is stored in DEST which must be a vector of length M. */
327 lambda_matrix_vector_mult (lambda_matrix matrix
, int m
, int n
,
328 lambda_vector vec
, lambda_vector dest
)
332 lambda_vector_clear (dest
, m
);
333 for (i
= 0; i
< m
; i
++)
334 for (j
= 0; j
< n
; j
++)
335 dest
[i
] += matrix
[i
][j
] * vec
[j
];
338 /* Return true if TRANS is a legal transformation matrix that respects
339 the dependence vectors in DISTS and DIRS. The conservative answer
342 "Wolfe proves that a unimodular transformation represented by the
343 matrix T is legal when applied to a loop nest with a set of
344 lexicographically non-negative distance vectors RDG if and only if
345 for each vector d in RDG, (T.d >= 0) is lexicographically positive.
346 i.e.: if and only if it transforms the lexicographically positive
347 distance vectors to lexicographically positive vectors. Note that
348 a unimodular matrix must transform the zero vector (and only it) to
349 the zero vector." S.Muchnick. */
352 lambda_transform_legal_p (lambda_trans_matrix trans
,
354 vec
<ddr_p
> dependence_relations
)
357 lambda_vector distres
;
358 struct data_dependence_relation
*ddr
;
360 gcc_assert (LTM_COLSIZE (trans
) == nb_loops
361 && LTM_ROWSIZE (trans
) == nb_loops
);
363 /* When there are no dependences, the transformation is correct. */
364 if (dependence_relations
.length () == 0)
367 ddr
= dependence_relations
[0];
371 /* When there is an unknown relation in the dependence_relations, we
372 know that it is no worth looking at this loop nest: give up. */
373 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
376 distres
= lambda_vector_new (nb_loops
);
378 /* For each distance vector in the dependence graph. */
379 FOR_EACH_VEC_ELT (dependence_relations
, i
, ddr
)
381 /* Don't care about relations for which we know that there is no
382 dependence, nor about read-read (aka. output-dependences):
383 these data accesses can happen in any order. */
384 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
385 || (DR_IS_READ (DDR_A (ddr
)) && DR_IS_READ (DDR_B (ddr
))))
388 /* Conservatively answer: "this transformation is not valid". */
389 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
392 /* If the dependence could not be captured by a distance vector,
393 conservatively answer that the transform is not valid. */
394 if (DDR_NUM_DIST_VECTS (ddr
) == 0)
397 /* Compute trans.dist_vect */
398 for (j
= 0; j
< DDR_NUM_DIST_VECTS (ddr
); j
++)
400 lambda_matrix_vector_mult (LTM_MATRIX (trans
), nb_loops
, nb_loops
,
401 DDR_DIST_VECT (ddr
, j
), distres
);
403 if (!lambda_vector_lexico_pos (distres
, nb_loops
))
410 /* Data dependency analysis. Returns true if the iterations of LOOP
411 are independent on each other (that is, if we can execute them
415 loop_parallel_p (struct loop
*loop
, struct obstack
* parloop_obstack
)
417 vec
<ddr_p
> dependence_relations
;
418 vec
<data_reference_p
> datarefs
;
419 lambda_trans_matrix trans
;
422 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
424 fprintf (dump_file
, "Considering loop %d\n", loop
->num
);
426 fprintf (dump_file
, "loop is innermost\n");
428 fprintf (dump_file
, "loop NOT innermost\n");
431 /* Check for problems with dependences. If the loop can be reversed,
432 the iterations are independent. */
433 auto_vec
<loop_p
, 3> loop_nest
;
434 datarefs
.create (10);
435 dependence_relations
.create (100);
436 if (! compute_data_dependences_for_loop (loop
, true, &loop_nest
, &datarefs
,
437 &dependence_relations
))
439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
440 fprintf (dump_file
, " FAILED: cannot analyze data dependencies\n");
444 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
445 dump_data_dependence_relations (dump_file
, dependence_relations
);
447 trans
= lambda_trans_matrix_new (1, 1, parloop_obstack
);
448 LTM_MATRIX (trans
)[0][0] = -1;
450 if (lambda_transform_legal_p (trans
, 1, dependence_relations
))
453 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
454 fprintf (dump_file
, " SUCCESS: may be parallelized\n");
456 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
458 " FAILED: data dependencies exist across iterations\n");
461 free_dependence_relations (dependence_relations
);
462 free_data_refs (datarefs
);
467 /* Return true when LOOP contains basic blocks marked with the
468 BB_IRREDUCIBLE_LOOP flag. */
471 loop_has_blocks_with_irreducible_flag (struct loop
*loop
)
474 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
477 for (i
= 0; i
< loop
->num_nodes
; i
++)
478 if (bbs
[i
]->flags
& BB_IRREDUCIBLE_LOOP
)
487 /* Assigns the address of OBJ in TYPE to an ssa name, and returns this name.
488 The assignment statement is placed on edge ENTRY. DECL_ADDRESS maps decls
489 to their addresses that can be reused. The address of OBJ is known to
490 be invariant in the whole function. Other needed statements are placed
494 take_address_of (tree obj
, tree type
, edge entry
,
495 int_tree_htab_type
*decl_address
, gimple_stmt_iterator
*gsi
)
498 tree
*var_p
, name
, addr
;
502 /* Since the address of OBJ is invariant, the trees may be shared.
503 Avoid rewriting unrelated parts of the code. */
504 obj
= unshare_expr (obj
);
506 handled_component_p (*var_p
);
507 var_p
= &TREE_OPERAND (*var_p
, 0))
510 /* Canonicalize the access to base on a MEM_REF. */
512 *var_p
= build_simple_mem_ref (build_fold_addr_expr (*var_p
));
514 /* Assign a canonical SSA name to the address of the base decl used
515 in the address and share it for all accesses and addresses based
517 uid
= DECL_UID (TREE_OPERAND (TREE_OPERAND (*var_p
, 0), 0));
520 int_tree_map
*slot
= decl_address
->find_slot (elt
, INSERT
);
525 addr
= TREE_OPERAND (*var_p
, 0);
527 = get_name (TREE_OPERAND (TREE_OPERAND (*var_p
, 0), 0));
529 name
= make_temp_ssa_name (TREE_TYPE (addr
), NULL
, obj_name
);
531 name
= make_ssa_name (TREE_TYPE (addr
));
532 stmt
= gimple_build_assign (name
, addr
);
533 gsi_insert_on_edge_immediate (entry
, stmt
);
541 /* Express the address in terms of the canonical SSA name. */
542 TREE_OPERAND (*var_p
, 0) = name
;
544 return build_fold_addr_expr_with_type (obj
, type
);
546 name
= force_gimple_operand (build_addr (obj
),
547 &stmts
, true, NULL_TREE
);
548 if (!gimple_seq_empty_p (stmts
))
549 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
551 if (!useless_type_conversion_p (type
, TREE_TYPE (name
)))
553 name
= force_gimple_operand (fold_convert (type
, name
), &stmts
, true,
555 if (!gimple_seq_empty_p (stmts
))
556 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
563 reduc_stmt_res (gimple
*stmt
)
565 return (gimple_code (stmt
) == GIMPLE_PHI
566 ? gimple_phi_result (stmt
)
567 : gimple_assign_lhs (stmt
));
570 /* Callback for htab_traverse. Create the initialization statement
571 for reduction described in SLOT, and place it at the preheader of
572 the loop described in DATA. */
575 initialize_reductions (reduction_info
**slot
, struct loop
*loop
)
581 struct reduction_info
*const reduc
= *slot
;
583 /* Create initialization in preheader:
584 reduction_variable = initialization value of reduction. */
586 /* In the phi node at the header, replace the argument coming
587 from the preheader with the reduction initialization value. */
589 /* Initialize the reduction. */
590 type
= TREE_TYPE (PHI_RESULT (reduc
->reduc_phi
));
591 init
= omp_reduction_init_op (gimple_location (reduc
->reduc_stmt
),
592 reduc
->reduction_code
, type
);
595 /* Replace the argument representing the initialization value
596 with the initialization value for the reduction (neutral
597 element for the particular operation, e.g. 0 for PLUS_EXPR,
598 1 for MULT_EXPR, etc).
599 Keep the old value in a new variable "reduction_initial",
600 that will be taken in consideration after the parallel
601 computing is done. */
603 e
= loop_preheader_edge (loop
);
604 arg
= PHI_ARG_DEF_FROM_EDGE (reduc
->reduc_phi
, e
);
605 /* Create new variable to hold the initial value. */
607 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE
608 (reduc
->reduc_phi
, loop_preheader_edge (loop
)), init
);
609 reduc
->initial_value
= arg
;
615 struct walk_stmt_info info
;
617 int_tree_htab_type
*decl_address
;
618 gimple_stmt_iterator
*gsi
;
623 /* Eliminates references to local variables in *TP out of the single
624 entry single exit region starting at DTA->ENTRY.
625 DECL_ADDRESS contains addresses of the references that had their
626 address taken already. If the expression is changed, CHANGED is
627 set to true. Callback for walk_tree. */
630 eliminate_local_variables_1 (tree
*tp
, int *walk_subtrees
, void *data
)
632 struct elv_data
*const dta
= (struct elv_data
*) data
;
633 tree t
= *tp
, var
, addr
, addr_type
, type
, obj
;
639 if (!SSA_VAR_P (t
) || DECL_EXTERNAL (t
))
642 type
= TREE_TYPE (t
);
643 addr_type
= build_pointer_type (type
);
644 addr
= take_address_of (t
, addr_type
, dta
->entry
, dta
->decl_address
,
646 if (dta
->gsi
== NULL
&& addr
== NULL_TREE
)
652 *tp
= build_simple_mem_ref (addr
);
658 if (TREE_CODE (t
) == ADDR_EXPR
)
660 /* ADDR_EXPR may appear in two contexts:
661 -- as a gimple operand, when the address taken is a function invariant
662 -- as gimple rhs, when the resulting address in not a function
664 We do not need to do anything special in the latter case (the base of
665 the memory reference whose address is taken may be replaced in the
666 DECL_P case). The former case is more complicated, as we need to
667 ensure that the new address is still a gimple operand. Thus, it
668 is not sufficient to replace just the base of the memory reference --
669 we need to move the whole computation of the address out of the
671 if (!is_gimple_val (t
))
675 obj
= TREE_OPERAND (t
, 0);
676 var
= get_base_address (obj
);
677 if (!var
|| !SSA_VAR_P (var
) || DECL_EXTERNAL (var
))
680 addr_type
= TREE_TYPE (t
);
681 addr
= take_address_of (obj
, addr_type
, dta
->entry
, dta
->decl_address
,
683 if (dta
->gsi
== NULL
&& addr
== NULL_TREE
)
700 /* Moves the references to local variables in STMT at *GSI out of the single
701 entry single exit region starting at ENTRY. DECL_ADDRESS contains
702 addresses of the references that had their address taken
706 eliminate_local_variables_stmt (edge entry
, gimple_stmt_iterator
*gsi
,
707 int_tree_htab_type
*decl_address
)
710 gimple
*stmt
= gsi_stmt (*gsi
);
712 memset (&dta
.info
, '\0', sizeof (dta
.info
));
714 dta
.decl_address
= decl_address
;
718 if (gimple_debug_bind_p (stmt
))
721 walk_tree (gimple_debug_bind_get_value_ptr (stmt
),
722 eliminate_local_variables_1
, &dta
.info
, NULL
);
725 gimple_debug_bind_reset_value (stmt
);
729 else if (gimple_clobber_p (stmt
))
731 unlink_stmt_vdef (stmt
);
732 stmt
= gimple_build_nop ();
733 gsi_replace (gsi
, stmt
, false);
739 walk_gimple_op (stmt
, eliminate_local_variables_1
, &dta
.info
);
746 /* Eliminates the references to local variables from the single entry
747 single exit region between the ENTRY and EXIT edges.
750 1) Taking address of a local variable -- these are moved out of the
751 region (and temporary variable is created to hold the address if
754 2) Dereferencing a local variable -- these are replaced with indirect
758 eliminate_local_variables (edge entry
, edge exit
)
761 auto_vec
<basic_block
, 3> body
;
763 gimple_stmt_iterator gsi
;
764 bool has_debug_stmt
= false;
765 int_tree_htab_type
decl_address (10);
766 basic_block entry_bb
= entry
->src
;
767 basic_block exit_bb
= exit
->dest
;
769 gather_blocks_in_sese_region (entry_bb
, exit_bb
, &body
);
771 FOR_EACH_VEC_ELT (body
, i
, bb
)
772 if (bb
!= entry_bb
&& bb
!= exit_bb
)
774 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
775 if (is_gimple_debug (gsi_stmt (gsi
)))
777 if (gimple_debug_bind_p (gsi_stmt (gsi
)))
778 has_debug_stmt
= true;
781 eliminate_local_variables_stmt (entry
, &gsi
, &decl_address
);
785 FOR_EACH_VEC_ELT (body
, i
, bb
)
786 if (bb
!= entry_bb
&& bb
!= exit_bb
)
787 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
788 if (gimple_debug_bind_p (gsi_stmt (gsi
)))
789 eliminate_local_variables_stmt (entry
, &gsi
, &decl_address
);
792 /* Returns true if expression EXPR is not defined between ENTRY and
793 EXIT, i.e. if all its operands are defined outside of the region. */
796 expr_invariant_in_region_p (edge entry
, edge exit
, tree expr
)
798 basic_block entry_bb
= entry
->src
;
799 basic_block exit_bb
= exit
->dest
;
802 if (is_gimple_min_invariant (expr
))
805 if (TREE_CODE (expr
) == SSA_NAME
)
807 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (expr
));
809 && dominated_by_p (CDI_DOMINATORS
, def_bb
, entry_bb
)
810 && !dominated_by_p (CDI_DOMINATORS
, def_bb
, exit_bb
))
819 /* If COPY_NAME_P is true, creates and returns a duplicate of NAME.
820 The copies are stored to NAME_COPIES, if NAME was already duplicated,
821 its duplicate stored in NAME_COPIES is returned.
823 Regardless of COPY_NAME_P, the decl used as a base of the ssa name is also
824 duplicated, storing the copies in DECL_COPIES. */
827 separate_decls_in_region_name (tree name
, name_to_copy_table_type
*name_copies
,
828 int_tree_htab_type
*decl_copies
,
831 tree copy
, var
, var_copy
;
832 unsigned idx
, uid
, nuid
;
833 struct int_tree_map ielt
;
834 struct name_to_copy_elt elt
, *nelt
;
835 name_to_copy_elt
**slot
;
838 if (TREE_CODE (name
) != SSA_NAME
)
841 idx
= SSA_NAME_VERSION (name
);
843 slot
= name_copies
->find_slot_with_hash (&elt
, idx
,
844 copy_name_p
? INSERT
: NO_INSERT
);
846 return (*slot
)->new_name
;
850 copy
= duplicate_ssa_name (name
, NULL
);
851 nelt
= XNEW (struct name_to_copy_elt
);
853 nelt
->new_name
= copy
;
854 nelt
->field
= NULL_TREE
;
863 var
= SSA_NAME_VAR (name
);
867 uid
= DECL_UID (var
);
869 dslot
= decl_copies
->find_slot_with_hash (ielt
, uid
, INSERT
);
872 var_copy
= create_tmp_var (TREE_TYPE (var
), get_name (var
));
873 DECL_GIMPLE_REG_P (var_copy
) = DECL_GIMPLE_REG_P (var
);
875 dslot
->to
= var_copy
;
877 /* Ensure that when we meet this decl next time, we won't duplicate
879 nuid
= DECL_UID (var_copy
);
881 dslot
= decl_copies
->find_slot_with_hash (ielt
, nuid
, INSERT
);
882 gcc_assert (!dslot
->to
);
884 dslot
->to
= var_copy
;
887 var_copy
= dslot
->to
;
889 replace_ssa_name_symbol (copy
, var_copy
);
893 /* Finds the ssa names used in STMT that are defined outside the
894 region between ENTRY and EXIT and replaces such ssa names with
895 their duplicates. The duplicates are stored to NAME_COPIES. Base
896 decls of all ssa names used in STMT (including those defined in
897 LOOP) are replaced with the new temporary variables; the
898 replacement decls are stored in DECL_COPIES. */
901 separate_decls_in_region_stmt (edge entry
, edge exit
, gimple
*stmt
,
902 name_to_copy_table_type
*name_copies
,
903 int_tree_htab_type
*decl_copies
)
911 FOR_EACH_PHI_OR_STMT_DEF (def
, stmt
, oi
, SSA_OP_DEF
)
913 name
= DEF_FROM_PTR (def
);
914 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
915 copy
= separate_decls_in_region_name (name
, name_copies
, decl_copies
,
917 gcc_assert (copy
== name
);
920 FOR_EACH_PHI_OR_STMT_USE (use
, stmt
, oi
, SSA_OP_USE
)
922 name
= USE_FROM_PTR (use
);
923 if (TREE_CODE (name
) != SSA_NAME
)
926 copy_name_p
= expr_invariant_in_region_p (entry
, exit
, name
);
927 copy
= separate_decls_in_region_name (name
, name_copies
, decl_copies
,
933 /* Finds the ssa names used in STMT that are defined outside the
934 region between ENTRY and EXIT and replaces such ssa names with
935 their duplicates. The duplicates are stored to NAME_COPIES. Base
936 decls of all ssa names used in STMT (including those defined in
937 LOOP) are replaced with the new temporary variables; the
938 replacement decls are stored in DECL_COPIES. */
941 separate_decls_in_region_debug (gimple
*stmt
,
942 name_to_copy_table_type
*name_copies
,
943 int_tree_htab_type
*decl_copies
)
948 struct int_tree_map ielt
;
949 struct name_to_copy_elt elt
;
950 name_to_copy_elt
**slot
;
953 if (gimple_debug_bind_p (stmt
))
954 var
= gimple_debug_bind_get_var (stmt
);
955 else if (gimple_debug_source_bind_p (stmt
))
956 var
= gimple_debug_source_bind_get_var (stmt
);
959 if (TREE_CODE (var
) == DEBUG_EXPR_DECL
|| TREE_CODE (var
) == LABEL_DECL
)
961 gcc_assert (DECL_P (var
) && SSA_VAR_P (var
));
962 ielt
.uid
= DECL_UID (var
);
963 dslot
= decl_copies
->find_slot_with_hash (ielt
, ielt
.uid
, NO_INSERT
);
966 if (gimple_debug_bind_p (stmt
))
967 gimple_debug_bind_set_var (stmt
, dslot
->to
);
968 else if (gimple_debug_source_bind_p (stmt
))
969 gimple_debug_source_bind_set_var (stmt
, dslot
->to
);
971 FOR_EACH_PHI_OR_STMT_USE (use
, stmt
, oi
, SSA_OP_USE
)
973 name
= USE_FROM_PTR (use
);
974 if (TREE_CODE (name
) != SSA_NAME
)
977 elt
.version
= SSA_NAME_VERSION (name
);
978 slot
= name_copies
->find_slot_with_hash (&elt
, elt
.version
, NO_INSERT
);
981 gimple_debug_bind_reset_value (stmt
);
986 SET_USE (use
, (*slot
)->new_name
);
992 /* Callback for htab_traverse. Adds a field corresponding to the reduction
993 specified in SLOT. The type is passed in DATA. */
996 add_field_for_reduction (reduction_info
**slot
, tree type
)
999 struct reduction_info
*const red
= *slot
;
1000 tree var
= reduc_stmt_res (red
->reduc_stmt
);
1001 tree field
= build_decl (gimple_location (red
->reduc_stmt
), FIELD_DECL
,
1002 SSA_NAME_IDENTIFIER (var
), TREE_TYPE (var
));
1004 insert_field_into_struct (type
, field
);
1011 /* Callback for htab_traverse. Adds a field corresponding to a ssa name
1012 described in SLOT. The type is passed in DATA. */
1015 add_field_for_name (name_to_copy_elt
**slot
, tree type
)
1017 struct name_to_copy_elt
*const elt
= *slot
;
1018 tree name
= ssa_name (elt
->version
);
1019 tree field
= build_decl (UNKNOWN_LOCATION
,
1020 FIELD_DECL
, SSA_NAME_IDENTIFIER (name
),
1023 insert_field_into_struct (type
, field
);
1029 /* Callback for htab_traverse. A local result is the intermediate result
1030 computed by a single
1031 thread, or the initial value in case no iteration was executed.
1032 This function creates a phi node reflecting these values.
1033 The phi's result will be stored in NEW_PHI field of the
1034 reduction's data structure. */
1037 create_phi_for_local_result (reduction_info
**slot
, struct loop
*loop
)
1039 struct reduction_info
*const reduc
= *slot
;
1042 basic_block store_bb
, continue_bb
;
1044 source_location locus
;
1046 /* STORE_BB is the block where the phi
1047 should be stored. It is the destination of the loop exit.
1048 (Find the fallthru edge from GIMPLE_OMP_CONTINUE). */
1049 continue_bb
= single_pred (loop
->latch
);
1050 store_bb
= FALLTHRU_EDGE (continue_bb
)->dest
;
1052 /* STORE_BB has two predecessors. One coming from the loop
1053 (the reduction's result is computed at the loop),
1054 and another coming from a block preceding the loop,
1056 are executed (the initial value should be taken). */
1057 if (EDGE_PRED (store_bb
, 0) == FALLTHRU_EDGE (continue_bb
))
1058 e
= EDGE_PRED (store_bb
, 1);
1060 e
= EDGE_PRED (store_bb
, 0);
1061 tree lhs
= reduc_stmt_res (reduc
->reduc_stmt
);
1062 local_res
= copy_ssa_name (lhs
);
1063 locus
= gimple_location (reduc
->reduc_stmt
);
1064 new_phi
= create_phi_node (local_res
, store_bb
);
1065 add_phi_arg (new_phi
, reduc
->init
, e
, locus
);
1066 add_phi_arg (new_phi
, lhs
, FALLTHRU_EDGE (continue_bb
), locus
);
1067 reduc
->new_phi
= new_phi
;
1077 basic_block store_bb
;
1078 basic_block load_bb
;
1081 /* Callback for htab_traverse. Create an atomic instruction for the
1082 reduction described in SLOT.
1083 DATA annotates the place in memory the atomic operation relates to,
1084 and the basic block it needs to be generated in. */
1087 create_call_for_reduction_1 (reduction_info
**slot
, struct clsn_data
*clsn_data
)
1089 struct reduction_info
*const reduc
= *slot
;
1090 gimple_stmt_iterator gsi
;
1091 tree type
= TREE_TYPE (PHI_RESULT (reduc
->reduc_phi
));
1096 tree t
, addr
, ref
, x
;
1097 tree tmp_load
, name
;
1100 if (reduc
->reduc_addr
== NULL_TREE
)
1102 load_struct
= build_simple_mem_ref (clsn_data
->load
);
1103 t
= build3 (COMPONENT_REF
, type
, load_struct
, reduc
->field
, NULL_TREE
);
1105 addr
= build_addr (t
);
1109 /* Set the address for the atomic store. */
1110 addr
= reduc
->reduc_addr
;
1112 /* Remove the non-atomic store '*addr = sum'. */
1113 tree res
= PHI_RESULT (reduc
->keep_res
);
1114 use_operand_p use_p
;
1116 bool single_use_p
= single_imm_use (res
, &use_p
, &stmt
);
1117 gcc_assert (single_use_p
);
1118 replace_uses_by (gimple_vdef (stmt
),
1119 gimple_vuse (stmt
));
1120 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
1121 gsi_remove (&gsi
, true);
1124 /* Create phi node. */
1125 bb
= clsn_data
->load_bb
;
1127 gsi
= gsi_last_bb (bb
);
1128 e
= split_block (bb
, gsi_stmt (gsi
));
1131 tmp_load
= create_tmp_var (TREE_TYPE (TREE_TYPE (addr
)));
1132 tmp_load
= make_ssa_name (tmp_load
);
1133 load
= gimple_build_omp_atomic_load (tmp_load
, addr
);
1134 SSA_NAME_DEF_STMT (tmp_load
) = load
;
1135 gsi
= gsi_start_bb (new_bb
);
1136 gsi_insert_after (&gsi
, load
, GSI_NEW_STMT
);
1138 e
= split_block (new_bb
, load
);
1140 gsi
= gsi_start_bb (new_bb
);
1142 x
= fold_build2 (reduc
->reduction_code
,
1143 TREE_TYPE (PHI_RESULT (reduc
->new_phi
)), ref
,
1144 PHI_RESULT (reduc
->new_phi
));
1146 name
= force_gimple_operand_gsi (&gsi
, x
, true, NULL_TREE
, true,
1147 GSI_CONTINUE_LINKING
);
1149 gsi_insert_after (&gsi
, gimple_build_omp_atomic_store (name
), GSI_NEW_STMT
);
1153 /* Create the atomic operation at the join point of the threads.
1154 REDUCTION_LIST describes the reductions in the LOOP.
1155 LD_ST_DATA describes the shared data structure where
1156 shared data is stored in and loaded from. */
1158 create_call_for_reduction (struct loop
*loop
,
1159 reduction_info_table_type
*reduction_list
,
1160 struct clsn_data
*ld_st_data
)
1162 reduction_list
->traverse
<struct loop
*, create_phi_for_local_result
> (loop
);
1163 /* Find the fallthru edge from GIMPLE_OMP_CONTINUE. */
1164 basic_block continue_bb
= single_pred (loop
->latch
);
1165 ld_st_data
->load_bb
= FALLTHRU_EDGE (continue_bb
)->dest
;
1167 ->traverse
<struct clsn_data
*, create_call_for_reduction_1
> (ld_st_data
);
1170 /* Callback for htab_traverse. Loads the final reduction value at the
1171 join point of all threads, and inserts it in the right place. */
1174 create_loads_for_reductions (reduction_info
**slot
, struct clsn_data
*clsn_data
)
1176 struct reduction_info
*const red
= *slot
;
1178 gimple_stmt_iterator gsi
;
1179 tree type
= TREE_TYPE (reduc_stmt_res (red
->reduc_stmt
));
1184 /* If there's no exit phi, the result of the reduction is unused. */
1185 if (red
->keep_res
== NULL
)
1188 gsi
= gsi_after_labels (clsn_data
->load_bb
);
1189 load_struct
= build_simple_mem_ref (clsn_data
->load
);
1190 load_struct
= build3 (COMPONENT_REF
, type
, load_struct
, red
->field
,
1194 name
= PHI_RESULT (red
->keep_res
);
1195 stmt
= gimple_build_assign (name
, x
);
1197 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
1199 for (gsi
= gsi_start_phis (gimple_bb (red
->keep_res
));
1200 !gsi_end_p (gsi
); gsi_next (&gsi
))
1201 if (gsi_stmt (gsi
) == red
->keep_res
)
1203 remove_phi_node (&gsi
, false);
1209 /* Load the reduction result that was stored in LD_ST_DATA.
1210 REDUCTION_LIST describes the list of reductions that the
1211 loads should be generated for. */
1213 create_final_loads_for_reduction (reduction_info_table_type
*reduction_list
,
1214 struct clsn_data
*ld_st_data
)
1216 gimple_stmt_iterator gsi
;
1220 gsi
= gsi_after_labels (ld_st_data
->load_bb
);
1221 t
= build_fold_addr_expr (ld_st_data
->store
);
1222 stmt
= gimple_build_assign (ld_st_data
->load
, t
);
1224 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
1227 ->traverse
<struct clsn_data
*, create_loads_for_reductions
> (ld_st_data
);
1231 /* Callback for htab_traverse. Store the neutral value for the
1232 particular reduction's operation, e.g. 0 for PLUS_EXPR,
1233 1 for MULT_EXPR, etc. into the reduction field.
1234 The reduction is specified in SLOT. The store information is
1238 create_stores_for_reduction (reduction_info
**slot
, struct clsn_data
*clsn_data
)
1240 struct reduction_info
*const red
= *slot
;
1243 gimple_stmt_iterator gsi
;
1244 tree type
= TREE_TYPE (reduc_stmt_res (red
->reduc_stmt
));
1246 gsi
= gsi_last_bb (clsn_data
->store_bb
);
1247 t
= build3 (COMPONENT_REF
, type
, clsn_data
->store
, red
->field
, NULL_TREE
);
1248 stmt
= gimple_build_assign (t
, red
->initial_value
);
1249 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
1254 /* Callback for htab_traverse. Creates loads to a field of LOAD in LOAD_BB and
1255 store to a field of STORE in STORE_BB for the ssa name and its duplicate
1256 specified in SLOT. */
1259 create_loads_and_stores_for_name (name_to_copy_elt
**slot
,
1260 struct clsn_data
*clsn_data
)
1262 struct name_to_copy_elt
*const elt
= *slot
;
1265 gimple_stmt_iterator gsi
;
1266 tree type
= TREE_TYPE (elt
->new_name
);
1269 gsi
= gsi_last_bb (clsn_data
->store_bb
);
1270 t
= build3 (COMPONENT_REF
, type
, clsn_data
->store
, elt
->field
, NULL_TREE
);
1271 stmt
= gimple_build_assign (t
, ssa_name (elt
->version
));
1272 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
1274 gsi
= gsi_last_bb (clsn_data
->load_bb
);
1275 load_struct
= build_simple_mem_ref (clsn_data
->load
);
1276 t
= build3 (COMPONENT_REF
, type
, load_struct
, elt
->field
, NULL_TREE
);
1277 stmt
= gimple_build_assign (elt
->new_name
, t
);
1278 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
1283 /* Moves all the variables used in LOOP and defined outside of it (including
1284 the initial values of loop phi nodes, and *PER_THREAD if it is a ssa
1285 name) to a structure created for this purpose. The code
1293 is transformed this way:
1308 `old' is stored to *ARG_STRUCT and `new' is stored to NEW_ARG_STRUCT. The
1309 pointer `new' is intentionally not initialized (the loop will be split to a
1310 separate function later, and `new' will be initialized from its arguments).
1311 LD_ST_DATA holds information about the shared data structure used to pass
1312 information among the threads. It is initialized here, and
1313 gen_parallel_loop will pass it to create_call_for_reduction that
1314 needs this information. REDUCTION_LIST describes the reductions
1318 separate_decls_in_region (edge entry
, edge exit
,
1319 reduction_info_table_type
*reduction_list
,
1320 tree
*arg_struct
, tree
*new_arg_struct
,
1321 struct clsn_data
*ld_st_data
)
1324 basic_block bb1
= split_edge (entry
);
1325 basic_block bb0
= single_pred (bb1
);
1326 name_to_copy_table_type
name_copies (10);
1327 int_tree_htab_type
decl_copies (10);
1329 tree type
, type_name
, nvar
;
1330 gimple_stmt_iterator gsi
;
1331 struct clsn_data clsn_data
;
1332 auto_vec
<basic_block
, 3> body
;
1334 basic_block entry_bb
= bb1
;
1335 basic_block exit_bb
= exit
->dest
;
1336 bool has_debug_stmt
= false;
1338 entry
= single_succ_edge (entry_bb
);
1339 gather_blocks_in_sese_region (entry_bb
, exit_bb
, &body
);
1341 FOR_EACH_VEC_ELT (body
, i
, bb
)
1343 if (bb
!= entry_bb
&& bb
!= exit_bb
)
1345 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1346 separate_decls_in_region_stmt (entry
, exit
, gsi_stmt (gsi
),
1347 &name_copies
, &decl_copies
);
1349 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1351 gimple
*stmt
= gsi_stmt (gsi
);
1353 if (is_gimple_debug (stmt
))
1354 has_debug_stmt
= true;
1356 separate_decls_in_region_stmt (entry
, exit
, stmt
,
1357 &name_copies
, &decl_copies
);
1362 /* Now process debug bind stmts. We must not create decls while
1363 processing debug stmts, so we defer their processing so as to
1364 make sure we will have debug info for as many variables as
1365 possible (all of those that were dealt with in the loop above),
1366 and discard those for which we know there's nothing we can
1369 FOR_EACH_VEC_ELT (body
, i
, bb
)
1370 if (bb
!= entry_bb
&& bb
!= exit_bb
)
1372 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
1374 gimple
*stmt
= gsi_stmt (gsi
);
1376 if (is_gimple_debug (stmt
))
1378 if (separate_decls_in_region_debug (stmt
, &name_copies
,
1381 gsi_remove (&gsi
, true);
1390 if (name_copies
.elements () == 0 && reduction_list
->elements () == 0)
1392 /* It may happen that there is nothing to copy (if there are only
1393 loop carried and external variables in the loop). */
1395 *new_arg_struct
= NULL
;
1399 /* Create the type for the structure to store the ssa names to. */
1400 type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1401 type_name
= build_decl (UNKNOWN_LOCATION
,
1402 TYPE_DECL
, create_tmp_var_name (".paral_data"),
1404 TYPE_NAME (type
) = type_name
;
1406 name_copies
.traverse
<tree
, add_field_for_name
> (type
);
1407 if (reduction_list
&& reduction_list
->elements () > 0)
1409 /* Create the fields for reductions. */
1410 reduction_list
->traverse
<tree
, add_field_for_reduction
> (type
);
1414 /* Create the loads and stores. */
1415 *arg_struct
= create_tmp_var (type
, ".paral_data_store");
1416 nvar
= create_tmp_var (build_pointer_type (type
), ".paral_data_load");
1417 *new_arg_struct
= make_ssa_name (nvar
);
1419 ld_st_data
->store
= *arg_struct
;
1420 ld_st_data
->load
= *new_arg_struct
;
1421 ld_st_data
->store_bb
= bb0
;
1422 ld_st_data
->load_bb
= bb1
;
1425 .traverse
<struct clsn_data
*, create_loads_and_stores_for_name
>
1428 /* Load the calculation from memory (after the join of the threads). */
1430 if (reduction_list
&& reduction_list
->elements () > 0)
1433 ->traverse
<struct clsn_data
*, create_stores_for_reduction
>
1435 clsn_data
.load
= make_ssa_name (nvar
);
1436 clsn_data
.load_bb
= exit
->dest
;
1437 clsn_data
.store
= ld_st_data
->store
;
1438 create_final_loads_for_reduction (reduction_list
, &clsn_data
);
1443 /* Returns true if FN was created to run in parallel. */
1446 parallelized_function_p (tree fndecl
)
1448 cgraph_node
*node
= cgraph_node::get (fndecl
);
1449 gcc_assert (node
!= NULL
);
1450 return node
->parallelized_function
;
1453 /* Creates and returns an empty function that will receive the body of
1454 a parallelized loop. */
1457 create_loop_fn (location_t loc
)
1461 tree decl
, type
, name
, t
;
1462 struct function
*act_cfun
= cfun
;
1463 static unsigned loopfn_num
;
1465 loc
= LOCATION_LOCUS (loc
);
1466 snprintf (buf
, 100, "%s.$loopfn", current_function_name ());
1467 ASM_FORMAT_PRIVATE_NAME (tname
, buf
, loopfn_num
++);
1468 clean_symbol_name (tname
);
1469 name
= get_identifier (tname
);
1470 type
= build_function_type_list (void_type_node
, ptr_type_node
, NULL_TREE
);
1472 decl
= build_decl (loc
, FUNCTION_DECL
, name
, type
);
1473 TREE_STATIC (decl
) = 1;
1474 TREE_USED (decl
) = 1;
1475 DECL_ARTIFICIAL (decl
) = 1;
1476 DECL_IGNORED_P (decl
) = 0;
1477 TREE_PUBLIC (decl
) = 0;
1478 DECL_UNINLINABLE (decl
) = 1;
1479 DECL_EXTERNAL (decl
) = 0;
1480 DECL_CONTEXT (decl
) = NULL_TREE
;
1481 DECL_INITIAL (decl
) = make_node (BLOCK
);
1482 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl
)) = decl
;
1484 t
= build_decl (loc
, RESULT_DECL
, NULL_TREE
, void_type_node
);
1485 DECL_ARTIFICIAL (t
) = 1;
1486 DECL_IGNORED_P (t
) = 1;
1487 DECL_RESULT (decl
) = t
;
1489 t
= build_decl (loc
, PARM_DECL
, get_identifier (".paral_data_param"),
1491 DECL_ARTIFICIAL (t
) = 1;
1492 DECL_ARG_TYPE (t
) = ptr_type_node
;
1493 DECL_CONTEXT (t
) = decl
;
1495 DECL_ARGUMENTS (decl
) = t
;
1497 allocate_struct_function (decl
, false);
1499 /* The call to allocate_struct_function clobbers CFUN, so we need to restore
1501 set_cfun (act_cfun
);
1506 /* Replace uses of NAME by VAL in block BB. */
1509 replace_uses_in_bb_by (tree name
, tree val
, basic_block bb
)
1512 imm_use_iterator imm_iter
;
1514 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, name
)
1516 if (gimple_bb (use_stmt
) != bb
)
1519 use_operand_p use_p
;
1520 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
1521 SET_USE (use_p
, val
);
1525 /* Do transformation from:
1532 ivtmp_a = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1533 sum_a = PHI <sum_init (preheader), sum_b (latch)>
1537 sum_b = sum_a + sum_update
1545 ivtmp_b = ivtmp_a + 1;
1549 sum_z = PHI <sum_b (cond[1]), ...>
1551 [1] Where <bb cond> is single_pred (bb latch); In the simplest case,
1561 ivtmp_a = PHI <ivtmp_c (latch)>
1562 sum_a = PHI <sum_c (latch)>
1566 sum_b = sum_a + sum_update
1571 ivtmp_c = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1572 sum_c = PHI <sum_init (preheader), sum_b (latch)>
1573 if (ivtmp_c < n + 1)
1579 ivtmp_b = ivtmp_a + 1;
1583 sum_y = PHI <sum_c (newheader)>
1586 sum_z = PHI <sum_y (newexit), ...>
1589 In unified diff format:
1594 + goto <bb newheader>
1597 - ivtmp_a = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1598 - sum_a = PHI <sum_init (preheader), sum_b (latch)>
1599 + ivtmp_a = PHI <ivtmp_c (latch)>
1600 + sum_a = PHI <sum_c (latch)>
1604 sum_b = sum_a + sum_update
1611 + ivtmp_c = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1612 + sum_c = PHI <sum_init (preheader), sum_b (latch)>
1613 + if (ivtmp_c < n + 1)
1619 ivtmp_b = ivtmp_a + 1;
1621 + goto <bb newheader>
1624 + sum_y = PHI <sum_c (newheader)>
1627 - sum_z = PHI <sum_b (cond[1]), ...>
1628 + sum_z = PHI <sum_y (newexit), ...>
1630 Note: the example does not show any virtual phis, but these are handled more
1631 or less as reductions.
1634 Moves the exit condition of LOOP to the beginning of its header.
1635 REDUCTION_LIST describes the reductions in LOOP. BOUND is the new loop
1639 transform_to_exit_first_loop_alt (struct loop
*loop
,
1640 reduction_info_table_type
*reduction_list
,
1643 basic_block header
= loop
->header
;
1644 basic_block latch
= loop
->latch
;
1645 edge exit
= single_dom_exit (loop
);
1646 basic_block exit_block
= exit
->dest
;
1647 gcond
*cond_stmt
= as_a
<gcond
*> (last_stmt (exit
->src
));
1648 tree control
= gimple_cond_lhs (cond_stmt
);
1651 /* Rewriting virtuals into loop-closed ssa normal form makes this
1652 transformation simpler. It also ensures that the virtuals are in
1653 loop-closed ssa normal from after the transformation, which is required by
1654 create_parallel_loop. */
1655 rewrite_virtuals_into_loop_closed_ssa (loop
);
1657 /* Create the new_header block. */
1658 basic_block new_header
= split_block_before_cond_jump (exit
->src
);
1659 edge edge_at_split
= single_pred_edge (new_header
);
1661 /* Redirect entry edge to new_header. */
1662 edge entry
= loop_preheader_edge (loop
);
1663 e
= redirect_edge_and_branch (entry
, new_header
);
1664 gcc_assert (e
== entry
);
1666 /* Redirect post_inc_edge to new_header. */
1667 edge post_inc_edge
= single_succ_edge (latch
);
1668 e
= redirect_edge_and_branch (post_inc_edge
, new_header
);
1669 gcc_assert (e
== post_inc_edge
);
1671 /* Redirect post_cond_edge to header. */
1672 edge post_cond_edge
= single_pred_edge (latch
);
1673 e
= redirect_edge_and_branch (post_cond_edge
, header
);
1674 gcc_assert (e
== post_cond_edge
);
1676 /* Redirect edge_at_split to latch. */
1677 e
= redirect_edge_and_branch (edge_at_split
, latch
);
1678 gcc_assert (e
== edge_at_split
);
1680 /* Set the new loop bound. */
1681 gimple_cond_set_rhs (cond_stmt
, bound
);
1682 update_stmt (cond_stmt
);
1684 /* Repair the ssa. */
1685 vec
<edge_var_map
> *v
= redirect_edge_var_map_vector (post_inc_edge
);
1689 for (gsi
= gsi_start_phis (header
), i
= 0;
1690 !gsi_end_p (gsi
) && v
->iterate (i
, &vm
);
1691 gsi_next (&gsi
), i
++)
1693 gphi
*phi
= gsi
.phi ();
1694 tree res_a
= PHI_RESULT (phi
);
1696 /* Create new phi. */
1697 tree res_c
= copy_ssa_name (res_a
, phi
);
1698 gphi
*nphi
= create_phi_node (res_c
, new_header
);
1700 /* Replace ivtmp_a with ivtmp_c in condition 'if (ivtmp_a < n)'. */
1701 replace_uses_in_bb_by (res_a
, res_c
, new_header
);
1703 /* Replace ivtmp/sum_b with ivtmp/sum_c in header phi. */
1704 add_phi_arg (phi
, res_c
, post_cond_edge
, UNKNOWN_LOCATION
);
1706 /* Replace sum_b with sum_c in exit phi. */
1707 tree res_b
= redirect_edge_var_map_def (vm
);
1708 replace_uses_in_bb_by (res_b
, res_c
, exit_block
);
1710 struct reduction_info
*red
= reduction_phi (reduction_list
, phi
);
1711 gcc_assert (virtual_operand_p (res_a
)
1717 /* Register the new reduction phi. */
1718 red
->reduc_phi
= nphi
;
1719 gimple_set_uid (red
->reduc_phi
, red
->reduc_version
);
1722 gcc_assert (gsi_end_p (gsi
) && !v
->iterate (i
, &vm
));
1724 /* Set the preheader argument of the new phis to ivtmp/sum_init. */
1725 flush_pending_stmts (entry
);
1727 /* Set the latch arguments of the new phis to ivtmp/sum_b. */
1728 flush_pending_stmts (post_inc_edge
);
1731 basic_block new_exit_block
= NULL
;
1732 if (!single_pred_p (exit
->dest
))
1734 /* Create a new empty exit block, inbetween the new loop header and the
1735 old exit block. The function separate_decls_in_region needs this block
1736 to insert code that is active on loop exit, but not any other path. */
1737 new_exit_block
= split_edge (exit
);
1740 /* Insert and register the reduction exit phis. */
1741 for (gphi_iterator gsi
= gsi_start_phis (exit_block
);
1745 gphi
*phi
= gsi
.phi ();
1747 tree res_z
= PHI_RESULT (phi
);
1750 if (new_exit_block
!= NULL
)
1752 /* Now that we have a new exit block, duplicate the phi of the old
1753 exit block in the new exit block to preserve loop-closed ssa. */
1754 edge succ_new_exit_block
= single_succ_edge (new_exit_block
);
1755 edge pred_new_exit_block
= single_pred_edge (new_exit_block
);
1756 tree res_y
= copy_ssa_name (res_z
, phi
);
1757 nphi
= create_phi_node (res_y
, new_exit_block
);
1758 res_c
= PHI_ARG_DEF_FROM_EDGE (phi
, succ_new_exit_block
);
1759 add_phi_arg (nphi
, res_c
, pred_new_exit_block
, UNKNOWN_LOCATION
);
1760 add_phi_arg (phi
, res_y
, succ_new_exit_block
, UNKNOWN_LOCATION
);
1763 res_c
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
1765 if (virtual_operand_p (res_z
))
1768 gimple
*reduc_phi
= SSA_NAME_DEF_STMT (res_c
);
1769 struct reduction_info
*red
= reduction_phi (reduction_list
, reduc_phi
);
1771 red
->keep_res
= (nphi
!= NULL
1776 /* We're going to cancel the loop at the end of gen_parallel_loop, but until
1777 then we're still using some fields, so only bother about fields that are
1778 still used: header and latch.
1779 The loop has a new header bb, so we update it. The latch bb stays the
1781 loop
->header
= new_header
;
1783 /* Recalculate dominance info. */
1784 free_dominance_info (CDI_DOMINATORS
);
1785 calculate_dominance_info (CDI_DOMINATORS
);
1787 checking_verify_ssa (true, true);
1790 /* Tries to moves the exit condition of LOOP to the beginning of its header
1791 without duplication of the loop body. NIT is the number of iterations of the
1792 loop. REDUCTION_LIST describes the reductions in LOOP. Return true if
1793 transformation is successful. */
1796 try_transform_to_exit_first_loop_alt (struct loop
*loop
,
1797 reduction_info_table_type
*reduction_list
,
1800 /* Check whether the latch contains a single statement. */
1801 if (!gimple_seq_nondebug_singleton_p (bb_seq (loop
->latch
)))
1804 /* Check whether the latch contains no phis. */
1805 if (phi_nodes (loop
->latch
) != NULL
)
1808 /* Check whether the latch contains the loop iv increment. */
1809 edge back
= single_succ_edge (loop
->latch
);
1810 edge exit
= single_dom_exit (loop
);
1811 gcond
*cond_stmt
= as_a
<gcond
*> (last_stmt (exit
->src
));
1812 tree control
= gimple_cond_lhs (cond_stmt
);
1813 gphi
*phi
= as_a
<gphi
*> (SSA_NAME_DEF_STMT (control
));
1814 tree inc_res
= gimple_phi_arg_def (phi
, back
->dest_idx
);
1815 if (gimple_bb (SSA_NAME_DEF_STMT (inc_res
)) != loop
->latch
)
1818 /* Check whether there's no code between the loop condition and the latch. */
1819 if (!single_pred_p (loop
->latch
)
1820 || single_pred (loop
->latch
) != exit
->src
)
1823 tree alt_bound
= NULL_TREE
;
1824 tree nit_type
= TREE_TYPE (nit
);
1826 /* Figure out whether nit + 1 overflows. */
1827 if (TREE_CODE (nit
) == INTEGER_CST
)
1829 if (!tree_int_cst_equal (nit
, TYPE_MAX_VALUE (nit_type
)))
1831 alt_bound
= fold_build2_loc (UNKNOWN_LOCATION
, PLUS_EXPR
, nit_type
,
1832 nit
, build_one_cst (nit_type
));
1834 gcc_assert (TREE_CODE (alt_bound
) == INTEGER_CST
);
1835 transform_to_exit_first_loop_alt (loop
, reduction_list
, alt_bound
);
1840 /* Todo: Figure out if we can trigger this, if it's worth to handle
1841 optimally, and if we can handle it optimally. */
1846 gcc_assert (TREE_CODE (nit
) == SSA_NAME
);
1848 /* Variable nit is the loop bound as returned by canonicalize_loop_ivs, for an
1849 iv with base 0 and step 1 that is incremented in the latch, like this:
1852 # iv_1 = PHI <0 (preheader), iv_2 (latch)>
1863 The range of iv_1 is [0, nit]. The latch edge is taken for
1864 iv_1 == [0, nit - 1] and the exit edge is taken for iv_1 == nit. So the
1865 number of latch executions is equal to nit.
1867 The function max_loop_iterations gives us the maximum number of latch
1868 executions, so it gives us the maximum value of nit. */
1870 if (!max_loop_iterations (loop
, &nit_max
))
1873 /* Check if nit + 1 overflows. */
1874 widest_int type_max
= wi::to_widest (TYPE_MAX_VALUE (nit_type
));
1875 if (nit_max
>= type_max
)
1878 gimple
*def
= SSA_NAME_DEF_STMT (nit
);
1880 /* Try to find nit + 1, in the form of n in an assignment nit = n - 1. */
1882 && is_gimple_assign (def
)
1883 && gimple_assign_rhs_code (def
) == PLUS_EXPR
)
1885 tree op1
= gimple_assign_rhs1 (def
);
1886 tree op2
= gimple_assign_rhs2 (def
);
1887 if (integer_minus_onep (op1
))
1889 else if (integer_minus_onep (op2
))
1893 /* If not found, insert nit + 1. */
1894 if (alt_bound
== NULL_TREE
)
1896 alt_bound
= fold_build2 (PLUS_EXPR
, nit_type
, nit
,
1897 build_int_cst_type (nit_type
, 1));
1899 gimple_stmt_iterator gsi
= gsi_last_bb (loop_preheader_edge (loop
)->src
);
1902 = force_gimple_operand_gsi (&gsi
, alt_bound
, true, NULL_TREE
, false,
1903 GSI_CONTINUE_LINKING
);
1906 transform_to_exit_first_loop_alt (loop
, reduction_list
, alt_bound
);
1910 /* Moves the exit condition of LOOP to the beginning of its header. NIT is the
1911 number of iterations of the loop. REDUCTION_LIST describes the reductions in
1915 transform_to_exit_first_loop (struct loop
*loop
,
1916 reduction_info_table_type
*reduction_list
,
1919 basic_block
*bbs
, *nbbs
, ex_bb
, orig_header
;
1922 edge exit
= single_dom_exit (loop
), hpred
;
1923 tree control
, control_name
, res
, t
;
1926 gcond
*cond_stmt
, *cond_nit
;
1929 split_block_after_labels (loop
->header
);
1930 orig_header
= single_succ (loop
->header
);
1931 hpred
= single_succ_edge (loop
->header
);
1933 cond_stmt
= as_a
<gcond
*> (last_stmt (exit
->src
));
1934 control
= gimple_cond_lhs (cond_stmt
);
1935 gcc_assert (gimple_cond_rhs (cond_stmt
) == nit
);
1937 /* Make sure that we have phi nodes on exit for all loop header phis
1938 (create_parallel_loop requires that). */
1939 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
1944 res
= PHI_RESULT (phi
);
1945 t
= copy_ssa_name (res
, phi
);
1946 SET_PHI_RESULT (phi
, t
);
1947 nphi
= create_phi_node (res
, orig_header
);
1948 add_phi_arg (nphi
, t
, hpred
, UNKNOWN_LOCATION
);
1952 gimple_cond_set_lhs (cond_stmt
, t
);
1953 update_stmt (cond_stmt
);
1958 bbs
= get_loop_body_in_dom_order (loop
);
1960 for (n
= 0; bbs
[n
] != exit
->src
; n
++)
1962 nbbs
= XNEWVEC (basic_block
, n
);
1963 ok
= gimple_duplicate_sese_tail (single_succ_edge (loop
->header
), exit
,
1970 /* Other than reductions, the only gimple reg that should be copied
1971 out of the loop is the control variable. */
1972 exit
= single_dom_exit (loop
);
1973 control_name
= NULL_TREE
;
1974 for (gphi_iterator gsi
= gsi_start_phis (ex_bb
);
1978 res
= PHI_RESULT (phi
);
1979 if (virtual_operand_p (res
))
1985 /* Check if it is a part of reduction. If it is,
1986 keep the phi at the reduction's keep_res field. The
1987 PHI_RESULT of this phi is the resulting value of the reduction
1988 variable when exiting the loop. */
1990 if (reduction_list
->elements () > 0)
1992 struct reduction_info
*red
;
1994 tree val
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
1995 red
= reduction_phi (reduction_list
, SSA_NAME_DEF_STMT (val
));
1998 red
->keep_res
= phi
;
2003 gcc_assert (control_name
== NULL_TREE
2004 && SSA_NAME_VAR (res
) == SSA_NAME_VAR (control
));
2006 remove_phi_node (&gsi
, false);
2008 gcc_assert (control_name
!= NULL_TREE
);
2010 /* Initialize the control variable to number of iterations
2011 according to the rhs of the exit condition. */
2012 gimple_stmt_iterator gsi
= gsi_after_labels (ex_bb
);
2013 cond_nit
= as_a
<gcond
*> (last_stmt (exit
->src
));
2014 nit_1
= gimple_cond_rhs (cond_nit
);
2015 nit_1
= force_gimple_operand_gsi (&gsi
,
2016 fold_convert (TREE_TYPE (control_name
), nit_1
),
2017 false, NULL_TREE
, false, GSI_SAME_STMT
);
2018 stmt
= gimple_build_assign (control_name
, nit_1
);
2019 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2022 /* Create the parallel constructs for LOOP as described in gen_parallel_loop.
2023 LOOP_FN and DATA are the arguments of GIMPLE_OMP_PARALLEL.
2024 NEW_DATA is the variable that should be initialized from the argument
2025 of LOOP_FN. N_THREADS is the requested number of threads, which can be 0 if
2026 that number is to be determined later. */
2029 create_parallel_loop (struct loop
*loop
, tree loop_fn
, tree data
,
2030 tree new_data
, unsigned n_threads
, location_t loc
,
2031 bool oacc_kernels_p
)
2033 gimple_stmt_iterator gsi
;
2034 basic_block for_bb
, ex_bb
, continue_bb
;
2036 gomp_parallel
*omp_par_stmt
;
2037 gimple
*omp_return_stmt1
, *omp_return_stmt2
;
2041 gomp_continue
*omp_cont_stmt
;
2042 tree cvar
, cvar_init
, initvar
, cvar_next
, cvar_base
, type
;
2043 edge exit
, nexit
, guard
, end
, e
;
2047 gcc_checking_assert (lookup_attribute ("oacc kernels",
2048 DECL_ATTRIBUTES (cfun
->decl
)));
2049 /* Indicate to later processing that this is a parallelized OpenACC
2050 kernels construct. */
2051 DECL_ATTRIBUTES (cfun
->decl
)
2052 = tree_cons (get_identifier ("oacc kernels parallelized"),
2053 NULL_TREE
, DECL_ATTRIBUTES (cfun
->decl
));
2057 /* Prepare the GIMPLE_OMP_PARALLEL statement. */
2059 basic_block bb
= loop_preheader_edge (loop
)->src
;
2060 basic_block paral_bb
= single_pred (bb
);
2061 gsi
= gsi_last_bb (paral_bb
);
2063 gcc_checking_assert (n_threads
!= 0);
2064 t
= build_omp_clause (loc
, OMP_CLAUSE_NUM_THREADS
);
2065 OMP_CLAUSE_NUM_THREADS_EXPR (t
)
2066 = build_int_cst (integer_type_node
, n_threads
);
2067 omp_par_stmt
= gimple_build_omp_parallel (NULL
, t
, loop_fn
, data
);
2068 gimple_set_location (omp_par_stmt
, loc
);
2070 gsi_insert_after (&gsi
, omp_par_stmt
, GSI_NEW_STMT
);
2072 /* Initialize NEW_DATA. */
2075 gassign
*assign_stmt
;
2077 gsi
= gsi_after_labels (bb
);
2079 param
= make_ssa_name (DECL_ARGUMENTS (loop_fn
));
2080 assign_stmt
= gimple_build_assign (param
, build_fold_addr_expr (data
));
2081 gsi_insert_before (&gsi
, assign_stmt
, GSI_SAME_STMT
);
2083 assign_stmt
= gimple_build_assign (new_data
,
2084 fold_convert (TREE_TYPE (new_data
), param
));
2085 gsi_insert_before (&gsi
, assign_stmt
, GSI_SAME_STMT
);
2088 /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_PARALLEL. */
2089 bb
= split_loop_exit_edge (single_dom_exit (loop
));
2090 gsi
= gsi_last_bb (bb
);
2091 omp_return_stmt1
= gimple_build_omp_return (false);
2092 gimple_set_location (omp_return_stmt1
, loc
);
2093 gsi_insert_after (&gsi
, omp_return_stmt1
, GSI_NEW_STMT
);
2096 /* Extract data for GIMPLE_OMP_FOR. */
2097 gcc_assert (loop
->header
== single_dom_exit (loop
)->src
);
2098 cond_stmt
= as_a
<gcond
*> (last_stmt (loop
->header
));
2100 cvar
= gimple_cond_lhs (cond_stmt
);
2101 cvar_base
= SSA_NAME_VAR (cvar
);
2102 phi
= SSA_NAME_DEF_STMT (cvar
);
2103 cvar_init
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
2104 initvar
= copy_ssa_name (cvar
);
2105 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, loop_preheader_edge (loop
)),
2107 cvar_next
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_latch_edge (loop
));
2109 gsi
= gsi_last_nondebug_bb (loop
->latch
);
2110 gcc_assert (gsi_stmt (gsi
) == SSA_NAME_DEF_STMT (cvar_next
));
2111 gsi_remove (&gsi
, true);
2114 for_bb
= split_edge (loop_preheader_edge (loop
));
2115 ex_bb
= split_loop_exit_edge (single_dom_exit (loop
));
2116 extract_true_false_edges_from_block (loop
->header
, &nexit
, &exit
);
2117 gcc_assert (exit
== single_dom_exit (loop
));
2119 guard
= make_edge (for_bb
, ex_bb
, 0);
2120 /* FIXME: What is the probability? */
2121 guard
->probability
= profile_probability::guessed_never ();
2122 /* Split the latch edge, so LOOPS_HAVE_SIMPLE_LATCHES is still valid. */
2123 loop
->latch
= split_edge (single_succ_edge (loop
->latch
));
2124 single_pred_edge (loop
->latch
)->flags
= 0;
2125 end
= make_single_succ_edge (single_pred (loop
->latch
), ex_bb
, EDGE_FALLTHRU
);
2126 rescan_loop_exit (end
, true, false);
2128 for (gphi_iterator gpi
= gsi_start_phis (ex_bb
);
2129 !gsi_end_p (gpi
); gsi_next (&gpi
))
2131 source_location locus
;
2132 gphi
*phi
= gpi
.phi ();
2133 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
2134 gimple
*def_stmt
= SSA_NAME_DEF_STMT (def
);
2136 /* If the exit phi is not connected to a header phi in the same loop, this
2137 value is not modified in the loop, and we're done with this phi. */
2138 if (!(gimple_code (def_stmt
) == GIMPLE_PHI
2139 && gimple_bb (def_stmt
) == loop
->header
))
2141 locus
= gimple_phi_arg_location_from_edge (phi
, exit
);
2142 add_phi_arg (phi
, def
, guard
, locus
);
2143 add_phi_arg (phi
, def
, end
, locus
);
2147 gphi
*stmt
= as_a
<gphi
*> (def_stmt
);
2148 def
= PHI_ARG_DEF_FROM_EDGE (stmt
, loop_preheader_edge (loop
));
2149 locus
= gimple_phi_arg_location_from_edge (stmt
,
2150 loop_preheader_edge (loop
));
2151 add_phi_arg (phi
, def
, guard
, locus
);
2153 def
= PHI_ARG_DEF_FROM_EDGE (stmt
, loop_latch_edge (loop
));
2154 locus
= gimple_phi_arg_location_from_edge (stmt
, loop_latch_edge (loop
));
2155 add_phi_arg (phi
, def
, end
, locus
);
2157 e
= redirect_edge_and_branch (exit
, nexit
->dest
);
2158 PENDING_STMT (e
) = NULL
;
2160 /* Emit GIMPLE_OMP_FOR. */
2162 /* Parallelized OpenACC kernels constructs use gang parallelism. See also
2163 omp-offload.c:execute_oacc_device_lower. */
2164 t
= build_omp_clause (loc
, OMP_CLAUSE_GANG
);
2167 t
= build_omp_clause (loc
, OMP_CLAUSE_SCHEDULE
);
2168 int chunk_size
= PARAM_VALUE (PARAM_PARLOOPS_CHUNK_SIZE
);
2169 enum PARAM_PARLOOPS_SCHEDULE_KIND schedule_type \
2170 = (enum PARAM_PARLOOPS_SCHEDULE_KIND
) PARAM_VALUE (PARAM_PARLOOPS_SCHEDULE
);
2171 switch (schedule_type
)
2173 case PARAM_PARLOOPS_SCHEDULE_KIND_static
:
2174 OMP_CLAUSE_SCHEDULE_KIND (t
) = OMP_CLAUSE_SCHEDULE_STATIC
;
2176 case PARAM_PARLOOPS_SCHEDULE_KIND_dynamic
:
2177 OMP_CLAUSE_SCHEDULE_KIND (t
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
2179 case PARAM_PARLOOPS_SCHEDULE_KIND_guided
:
2180 OMP_CLAUSE_SCHEDULE_KIND (t
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
2182 case PARAM_PARLOOPS_SCHEDULE_KIND_auto
:
2183 OMP_CLAUSE_SCHEDULE_KIND (t
) = OMP_CLAUSE_SCHEDULE_AUTO
;
2186 case PARAM_PARLOOPS_SCHEDULE_KIND_runtime
:
2187 OMP_CLAUSE_SCHEDULE_KIND (t
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
2193 if (chunk_size
!= 0)
2194 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (t
)
2195 = build_int_cst (integer_type_node
, chunk_size
);
2198 for_stmt
= gimple_build_omp_for (NULL
,
2200 ? GF_OMP_FOR_KIND_OACC_LOOP
2201 : GF_OMP_FOR_KIND_FOR
),
2204 gimple_cond_set_lhs (cond_stmt
, cvar_base
);
2205 type
= TREE_TYPE (cvar
);
2206 gimple_set_location (for_stmt
, loc
);
2207 gimple_omp_for_set_index (for_stmt
, 0, initvar
);
2208 gimple_omp_for_set_initial (for_stmt
, 0, cvar_init
);
2209 gimple_omp_for_set_final (for_stmt
, 0, gimple_cond_rhs (cond_stmt
));
2210 gimple_omp_for_set_cond (for_stmt
, 0, gimple_cond_code (cond_stmt
));
2211 gimple_omp_for_set_incr (for_stmt
, 0, build2 (PLUS_EXPR
, type
,
2213 build_int_cst (type
, 1)));
2215 gsi
= gsi_last_bb (for_bb
);
2216 gsi_insert_after (&gsi
, for_stmt
, GSI_NEW_STMT
);
2217 SSA_NAME_DEF_STMT (initvar
) = for_stmt
;
2219 /* Emit GIMPLE_OMP_CONTINUE. */
2220 continue_bb
= single_pred (loop
->latch
);
2221 gsi
= gsi_last_bb (continue_bb
);
2222 omp_cont_stmt
= gimple_build_omp_continue (cvar_next
, cvar
);
2223 gimple_set_location (omp_cont_stmt
, loc
);
2224 gsi_insert_after (&gsi
, omp_cont_stmt
, GSI_NEW_STMT
);
2225 SSA_NAME_DEF_STMT (cvar_next
) = omp_cont_stmt
;
2227 /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_FOR. */
2228 gsi
= gsi_last_bb (ex_bb
);
2229 omp_return_stmt2
= gimple_build_omp_return (true);
2230 gimple_set_location (omp_return_stmt2
, loc
);
2231 gsi_insert_after (&gsi
, omp_return_stmt2
, GSI_NEW_STMT
);
2233 /* After the above dom info is hosed. Re-compute it. */
2234 free_dominance_info (CDI_DOMINATORS
);
2235 calculate_dominance_info (CDI_DOMINATORS
);
2238 /* Generates code to execute the iterations of LOOP in N_THREADS
2239 threads in parallel, which can be 0 if that number is to be determined
2242 NITER describes number of iterations of LOOP.
2243 REDUCTION_LIST describes the reductions existent in the LOOP. */
2246 gen_parallel_loop (struct loop
*loop
,
2247 reduction_info_table_type
*reduction_list
,
2248 unsigned n_threads
, struct tree_niter_desc
*niter
,
2249 bool oacc_kernels_p
)
2251 tree many_iterations_cond
, type
, nit
;
2252 tree arg_struct
, new_arg_struct
;
2255 struct clsn_data clsn_data
;
2258 unsigned int m_p_thread
=2;
2262 ---------------------------------------------------------------------
2265 IV = phi (INIT, IV + STEP)
2271 ---------------------------------------------------------------------
2273 with # of iterations NITER (possibly with MAY_BE_ZERO assumption),
2274 we generate the following code:
2276 ---------------------------------------------------------------------
2279 || NITER < MIN_PER_THREAD * N_THREADS)
2283 store all local loop-invariant variables used in body of the loop to DATA.
2284 GIMPLE_OMP_PARALLEL (OMP_CLAUSE_NUM_THREADS (N_THREADS), LOOPFN, DATA);
2285 load the variables from DATA.
2286 GIMPLE_OMP_FOR (IV = INIT; COND; IV += STEP) (OMP_CLAUSE_SCHEDULE (static))
2289 GIMPLE_OMP_CONTINUE;
2290 GIMPLE_OMP_RETURN -- GIMPLE_OMP_FOR
2291 GIMPLE_OMP_RETURN -- GIMPLE_OMP_PARALLEL
2297 IV = phi (INIT, IV + STEP)
2308 /* Create two versions of the loop -- in the old one, we know that the
2309 number of iterations is large enough, and we will transform it into the
2310 loop that will be split to loop_fn, the new one will be used for the
2311 remaining iterations. */
2313 /* We should compute a better number-of-iterations value for outer loops.
2316 for (i = 0; i < n; ++i)
2317 for (j = 0; j < m; ++j)
2320 we should compute nit = n * m, not nit = n.
2321 Also may_be_zero handling would need to be adjusted. */
2323 type
= TREE_TYPE (niter
->niter
);
2324 nit
= force_gimple_operand (unshare_expr (niter
->niter
), &stmts
, true,
2327 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
2329 if (!oacc_kernels_p
)
2334 m_p_thread
=MIN_PER_THREAD
;
2336 gcc_checking_assert (n_threads
!= 0);
2337 many_iterations_cond
=
2338 fold_build2 (GE_EXPR
, boolean_type_node
,
2339 nit
, build_int_cst (type
, m_p_thread
* n_threads
- 1));
2341 many_iterations_cond
2342 = fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
2343 invert_truthvalue (unshare_expr (niter
->may_be_zero
)),
2344 many_iterations_cond
);
2345 many_iterations_cond
2346 = force_gimple_operand (many_iterations_cond
, &stmts
, false, NULL_TREE
);
2348 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
2349 if (!is_gimple_condexpr (many_iterations_cond
))
2351 many_iterations_cond
2352 = force_gimple_operand (many_iterations_cond
, &stmts
,
2355 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
),
2359 initialize_original_copy_tables ();
2361 /* We assume that the loop usually iterates a lot. */
2362 loop_version (loop
, many_iterations_cond
, NULL
,
2363 profile_probability::likely (),
2364 profile_probability::unlikely (),
2365 profile_probability::likely (),
2366 profile_probability::unlikely (), true);
2367 update_ssa (TODO_update_ssa
);
2368 free_original_copy_tables ();
2371 /* Base all the induction variables in LOOP on a single control one. */
2372 canonicalize_loop_ivs (loop
, &nit
, true);
2374 /* Ensure that the exit condition is the first statement in the loop.
2375 The common case is that latch of the loop is empty (apart from the
2376 increment) and immediately follows the loop exit test. Attempt to move the
2377 entry of the loop directly before the exit check and increase the number of
2378 iterations of the loop by one. */
2379 if (try_transform_to_exit_first_loop_alt (loop
, reduction_list
, nit
))
2382 && (dump_flags
& TDF_DETAILS
))
2384 "alternative exit-first loop transform succeeded"
2385 " for loop %d\n", loop
->num
);
2392 /* Fall back on the method that handles more cases, but duplicates the
2393 loop body: move the exit condition of LOOP to the beginning of its
2394 header, and duplicate the part of the last iteration that gets disabled
2395 to the exit of the loop. */
2396 transform_to_exit_first_loop (loop
, reduction_list
, nit
);
2399 /* Generate initializations for reductions. */
2400 if (reduction_list
->elements () > 0)
2401 reduction_list
->traverse
<struct loop
*, initialize_reductions
> (loop
);
2403 /* Eliminate the references to local variables from the loop. */
2404 gcc_assert (single_exit (loop
));
2405 entry
= loop_preheader_edge (loop
);
2406 exit
= single_dom_exit (loop
);
2408 /* This rewrites the body in terms of new variables. This has already
2409 been done for oacc_kernels_p in pass_lower_omp/lower_omp (). */
2410 if (!oacc_kernels_p
)
2412 eliminate_local_variables (entry
, exit
);
2413 /* In the old loop, move all variables non-local to the loop to a
2414 structure and back, and create separate decls for the variables used in
2416 separate_decls_in_region (entry
, exit
, reduction_list
, &arg_struct
,
2417 &new_arg_struct
, &clsn_data
);
2421 arg_struct
= NULL_TREE
;
2422 new_arg_struct
= NULL_TREE
;
2423 clsn_data
.load
= NULL_TREE
;
2424 clsn_data
.load_bb
= exit
->dest
;
2425 clsn_data
.store
= NULL_TREE
;
2426 clsn_data
.store_bb
= NULL
;
2429 /* Create the parallel constructs. */
2430 loc
= UNKNOWN_LOCATION
;
2431 cond_stmt
= last_stmt (loop
->header
);
2433 loc
= gimple_location (cond_stmt
);
2434 create_parallel_loop (loop
, create_loop_fn (loc
), arg_struct
, new_arg_struct
,
2435 n_threads
, loc
, oacc_kernels_p
);
2436 if (reduction_list
->elements () > 0)
2437 create_call_for_reduction (loop
, reduction_list
, &clsn_data
);
2441 /* Free loop bound estimations that could contain references to
2442 removed statements. */
2443 free_numbers_of_iterations_estimates (cfun
);
2446 /* Returns true when LOOP contains vector phi nodes. */
2449 loop_has_vector_phi_nodes (struct loop
*loop ATTRIBUTE_UNUSED
)
2452 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
2456 for (i
= 0; i
< loop
->num_nodes
; i
++)
2457 for (gsi
= gsi_start_phis (bbs
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
2458 if (TREE_CODE (TREE_TYPE (PHI_RESULT (gsi
.phi ()))) == VECTOR_TYPE
)
2467 /* Create a reduction_info struct, initialize it with REDUC_STMT
2468 and PHI, insert it to the REDUCTION_LIST. */
2471 build_new_reduction (reduction_info_table_type
*reduction_list
,
2472 gimple
*reduc_stmt
, gphi
*phi
)
2474 reduction_info
**slot
;
2475 struct reduction_info
*new_reduction
;
2476 enum tree_code reduction_code
;
2478 gcc_assert (reduc_stmt
);
2480 if (gimple_code (reduc_stmt
) == GIMPLE_PHI
)
2482 tree op1
= PHI_ARG_DEF (reduc_stmt
, 0);
2483 gimple
*def1
= SSA_NAME_DEF_STMT (op1
);
2484 reduction_code
= gimple_assign_rhs_code (def1
);
2487 reduction_code
= gimple_assign_rhs_code (reduc_stmt
);
2488 /* Check for OpenMP supported reduction. */
2489 switch (reduction_code
)
2499 case TRUTH_XOR_EXPR
:
2500 case TRUTH_AND_EXPR
:
2506 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2509 "Detected reduction. reduction stmt is:\n");
2510 print_gimple_stmt (dump_file
, reduc_stmt
, 0);
2511 fprintf (dump_file
, "\n");
2514 new_reduction
= XCNEW (struct reduction_info
);
2516 new_reduction
->reduc_stmt
= reduc_stmt
;
2517 new_reduction
->reduc_phi
= phi
;
2518 new_reduction
->reduc_version
= SSA_NAME_VERSION (gimple_phi_result (phi
));
2519 new_reduction
->reduction_code
= reduction_code
;
2520 slot
= reduction_list
->find_slot (new_reduction
, INSERT
);
2521 *slot
= new_reduction
;
2524 /* Callback for htab_traverse. Sets gimple_uid of reduc_phi stmts. */
2527 set_reduc_phi_uids (reduction_info
**slot
, void *data ATTRIBUTE_UNUSED
)
2529 struct reduction_info
*const red
= *slot
;
2530 gimple_set_uid (red
->reduc_phi
, red
->reduc_version
);
2534 /* Detect all reductions in the LOOP, insert them into REDUCTION_LIST. */
2537 gather_scalar_reductions (loop_p loop
, reduction_info_table_type
*reduction_list
)
2540 loop_vec_info simple_loop_info
;
2541 auto_vec
<gphi
*, 4> double_reduc_phis
;
2542 auto_vec
<gimple
*, 4> double_reduc_stmts
;
2544 if (!stmt_vec_info_vec
.exists ())
2545 init_stmt_vec_info_vec ();
2547 simple_loop_info
= vect_analyze_loop_form (loop
);
2548 if (simple_loop_info
== NULL
)
2551 for (gsi
= gsi_start_phis (loop
->header
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2553 gphi
*phi
= gsi
.phi ();
2555 tree res
= PHI_RESULT (phi
);
2558 if (virtual_operand_p (res
))
2561 if (simple_iv (loop
, loop
, res
, &iv
, true))
2565 = vect_force_simple_reduction (simple_loop_info
, phi
,
2566 &double_reduc
, true);
2572 if (loop
->inner
->inner
!= NULL
)
2575 double_reduc_phis
.safe_push (phi
);
2576 double_reduc_stmts
.safe_push (reduc_stmt
);
2580 build_new_reduction (reduction_list
, reduc_stmt
, phi
);
2582 delete simple_loop_info
;
2584 if (!double_reduc_phis
.is_empty ())
2586 simple_loop_info
= vect_analyze_loop_form (loop
->inner
);
2587 if (simple_loop_info
)
2592 FOR_EACH_VEC_ELT (double_reduc_phis
, i
, phi
)
2595 tree res
= PHI_RESULT (phi
);
2598 use_operand_p use_p
;
2600 bool single_use_p
= single_imm_use (res
, &use_p
, &inner_stmt
);
2601 gcc_assert (single_use_p
);
2602 if (gimple_code (inner_stmt
) != GIMPLE_PHI
)
2604 gphi
*inner_phi
= as_a
<gphi
*> (inner_stmt
);
2605 if (simple_iv (loop
->inner
, loop
->inner
, PHI_RESULT (inner_phi
),
2609 gimple
*inner_reduc_stmt
2610 = vect_force_simple_reduction (simple_loop_info
, inner_phi
,
2611 &double_reduc
, true);
2612 gcc_assert (!double_reduc
);
2613 if (inner_reduc_stmt
== NULL
)
2616 build_new_reduction (reduction_list
, double_reduc_stmts
[i
], phi
);
2618 delete simple_loop_info
;
2623 /* Release the claim on gimple_uid. */
2624 free_stmt_vec_info_vec ();
2626 if (reduction_list
->elements () == 0)
2629 /* As gimple_uid is used by the vectorizer in between vect_analyze_loop_form
2630 and free_stmt_vec_info_vec, we can set gimple_uid of reduc_phi stmts only
2633 FOR_EACH_BB_FN (bb
, cfun
)
2634 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2635 gimple_set_uid (gsi_stmt (gsi
), (unsigned int)-1);
2636 reduction_list
->traverse
<void *, set_reduc_phi_uids
> (NULL
);
2639 /* Try to initialize NITER for code generation part. */
2642 try_get_loop_niter (loop_p loop
, struct tree_niter_desc
*niter
)
2644 edge exit
= single_dom_exit (loop
);
2648 /* We need to know # of iterations, and there should be no uses of values
2649 defined inside loop outside of it, unless the values are invariants of
2651 if (!number_of_iterations_exit (loop
, exit
, niter
, false))
2653 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2654 fprintf (dump_file
, " FAILED: number of iterations not known\n");
2661 /* Return the default def of the first function argument. */
2664 get_omp_data_i_param (void)
2666 tree decl
= DECL_ARGUMENTS (cfun
->decl
);
2667 gcc_assert (DECL_CHAIN (decl
) == NULL_TREE
);
2668 return ssa_default_def (cfun
, decl
);
2671 /* For PHI in loop header of LOOP, look for pattern:
2674 .omp_data_i = &.omp_data_arr;
2675 addr = .omp_data_i->sum;
2679 sum_b = PHI <sum_a (preheader), sum_c (latch)>
2681 and return addr. Otherwise, return NULL_TREE. */
2684 find_reduc_addr (struct loop
*loop
, gphi
*phi
)
2686 edge e
= loop_preheader_edge (loop
);
2687 tree arg
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
2688 gimple
*stmt
= SSA_NAME_DEF_STMT (arg
);
2689 if (!gimple_assign_single_p (stmt
))
2691 tree memref
= gimple_assign_rhs1 (stmt
);
2692 if (TREE_CODE (memref
) != MEM_REF
)
2694 tree addr
= TREE_OPERAND (memref
, 0);
2696 gimple
*stmt2
= SSA_NAME_DEF_STMT (addr
);
2697 if (!gimple_assign_single_p (stmt2
))
2699 tree compref
= gimple_assign_rhs1 (stmt2
);
2700 if (TREE_CODE (compref
) != COMPONENT_REF
)
2702 tree addr2
= TREE_OPERAND (compref
, 0);
2703 if (TREE_CODE (addr2
) != MEM_REF
)
2705 addr2
= TREE_OPERAND (addr2
, 0);
2706 if (TREE_CODE (addr2
) != SSA_NAME
2707 || addr2
!= get_omp_data_i_param ())
2713 /* Try to initialize REDUCTION_LIST for code generation part.
2714 REDUCTION_LIST describes the reductions. */
2717 try_create_reduction_list (loop_p loop
,
2718 reduction_info_table_type
*reduction_list
,
2719 bool oacc_kernels_p
)
2721 edge exit
= single_dom_exit (loop
);
2726 /* Try to get rid of exit phis. */
2727 final_value_replacement_loop (loop
);
2729 gather_scalar_reductions (loop
, reduction_list
);
2732 for (gsi
= gsi_start_phis (exit
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2734 gphi
*phi
= gsi
.phi ();
2735 struct reduction_info
*red
;
2736 imm_use_iterator imm_iter
;
2737 use_operand_p use_p
;
2739 tree val
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
2741 if (!virtual_operand_p (val
))
2743 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2745 fprintf (dump_file
, "phi is ");
2746 print_gimple_stmt (dump_file
, phi
, 0);
2747 fprintf (dump_file
, "arg of phi to exit: value ");
2748 print_generic_expr (dump_file
, val
);
2749 fprintf (dump_file
, " used outside loop\n");
2751 " checking if it is part of reduction pattern:\n");
2753 if (reduction_list
->elements () == 0)
2755 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2757 " FAILED: it is not a part of reduction.\n");
2761 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, val
)
2763 if (!gimple_debug_bind_p (USE_STMT (use_p
))
2764 && flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
))))
2766 reduc_phi
= USE_STMT (use_p
);
2770 red
= reduction_phi (reduction_list
, reduc_phi
);
2773 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2775 " FAILED: it is not a part of reduction.\n");
2778 if (red
->keep_res
!= NULL
)
2780 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2782 " FAILED: reduction has multiple exit phis.\n");
2785 red
->keep_res
= phi
;
2786 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2788 fprintf (dump_file
, "reduction phi is ");
2789 print_gimple_stmt (dump_file
, red
->reduc_phi
, 0);
2790 fprintf (dump_file
, "reduction stmt is ");
2791 print_gimple_stmt (dump_file
, red
->reduc_stmt
, 0);
2796 /* The iterations of the loop may communicate only through bivs whose
2797 iteration space can be distributed efficiently. */
2798 for (gsi
= gsi_start_phis (loop
->header
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2800 gphi
*phi
= gsi
.phi ();
2801 tree def
= PHI_RESULT (phi
);
2804 if (!virtual_operand_p (def
) && !simple_iv (loop
, loop
, def
, &iv
, true))
2806 struct reduction_info
*red
;
2808 red
= reduction_phi (reduction_list
, phi
);
2811 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2813 " FAILED: scalar dependency between iterations\n");
2821 for (gsi
= gsi_start_phis (loop
->header
); !gsi_end_p (gsi
);
2824 gphi
*phi
= gsi
.phi ();
2825 tree def
= PHI_RESULT (phi
);
2828 if (!virtual_operand_p (def
)
2829 && !simple_iv (loop
, loop
, def
, &iv
, true))
2831 tree addr
= find_reduc_addr (loop
, phi
);
2832 if (addr
== NULL_TREE
)
2834 struct reduction_info
*red
= reduction_phi (reduction_list
, phi
);
2835 red
->reduc_addr
= addr
;
2843 /* Return true if LOOP contains phis with ADDR_EXPR in args. */
2846 loop_has_phi_with_address_arg (struct loop
*loop
)
2848 basic_block
*bbs
= get_loop_body (loop
);
2853 for (i
= 0; i
< loop
->num_nodes
; i
++)
2854 for (gsi
= gsi_start_phis (bbs
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
2856 gphi
*phi
= gsi
.phi ();
2857 for (j
= 0; j
< gimple_phi_num_args (phi
); j
++)
2859 tree arg
= gimple_phi_arg_def (phi
, j
);
2860 if (TREE_CODE (arg
) == ADDR_EXPR
)
2862 /* This should be handled by eliminate_local_variables, but that
2863 function currently ignores phis. */
2875 /* Return true if memory ref REF (corresponding to the stmt at GSI in
2876 REGIONS_BB[I]) conflicts with the statements in REGIONS_BB[I] after gsi,
2877 or the statements in REGIONS_BB[I + n]. REF_IS_STORE indicates if REF is a
2878 store. Ignore conflicts with SKIP_STMT. */
2881 ref_conflicts_with_region (gimple_stmt_iterator gsi
, ao_ref
*ref
,
2882 bool ref_is_store
, vec
<basic_block
> region_bbs
,
2883 unsigned int i
, gimple
*skip_stmt
)
2885 basic_block bb
= region_bbs
[i
];
2890 for (; !gsi_end_p (gsi
);
2893 gimple
*stmt
= gsi_stmt (gsi
);
2894 if (stmt
== skip_stmt
)
2898 fprintf (dump_file
, "skipping reduction store: ");
2899 print_gimple_stmt (dump_file
, stmt
, 0);
2904 if (!gimple_vdef (stmt
)
2905 && !gimple_vuse (stmt
))
2908 if (gimple_code (stmt
) == GIMPLE_RETURN
)
2913 if (ref_maybe_used_by_stmt_p (stmt
, ref
))
2917 fprintf (dump_file
, "Stmt ");
2918 print_gimple_stmt (dump_file
, stmt
, 0);
2925 if (stmt_may_clobber_ref_p_1 (stmt
, ref
))
2929 fprintf (dump_file
, "Stmt ");
2930 print_gimple_stmt (dump_file
, stmt
, 0);
2937 if (i
== region_bbs
.length ())
2940 gsi
= gsi_start_bb (bb
);
2946 /* Return true if the bbs in REGION_BBS but not in in_loop_bbs can be executed
2947 in parallel with REGION_BBS containing the loop. Return the stores of
2948 reduction results in REDUCTION_STORES. */
2951 oacc_entry_exit_ok_1 (bitmap in_loop_bbs
, vec
<basic_block
> region_bbs
,
2952 reduction_info_table_type
*reduction_list
,
2953 bitmap reduction_stores
)
2955 tree omp_data_i
= get_omp_data_i_param ();
2959 FOR_EACH_VEC_ELT (region_bbs
, i
, bb
)
2961 if (bitmap_bit_p (in_loop_bbs
, bb
->index
))
2964 gimple_stmt_iterator gsi
;
2965 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
2968 gimple
*stmt
= gsi_stmt (gsi
);
2969 gimple
*skip_stmt
= NULL
;
2971 if (is_gimple_debug (stmt
)
2972 || gimple_code (stmt
) == GIMPLE_COND
)
2976 bool ref_is_store
= false;
2977 if (gimple_assign_load_p (stmt
))
2979 tree rhs
= gimple_assign_rhs1 (stmt
);
2980 tree base
= get_base_address (rhs
);
2981 if (TREE_CODE (base
) == MEM_REF
2982 && operand_equal_p (TREE_OPERAND (base
, 0), omp_data_i
, 0))
2985 tree lhs
= gimple_assign_lhs (stmt
);
2986 if (TREE_CODE (lhs
) == SSA_NAME
2987 && has_single_use (lhs
))
2989 use_operand_p use_p
;
2991 single_imm_use (lhs
, &use_p
, &use_stmt
);
2992 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
2994 struct reduction_info
*red
;
2995 red
= reduction_phi (reduction_list
, use_stmt
);
2996 tree val
= PHI_RESULT (red
->keep_res
);
2997 if (has_single_use (val
))
2999 single_imm_use (val
, &use_p
, &use_stmt
);
3000 if (gimple_store_p (use_stmt
))
3003 = SSA_NAME_VERSION (gimple_vdef (use_stmt
));
3004 bitmap_set_bit (reduction_stores
, id
);
3005 skip_stmt
= use_stmt
;
3008 fprintf (dump_file
, "found reduction load: ");
3009 print_gimple_stmt (dump_file
, stmt
, 0);
3016 ao_ref_init (&ref
, rhs
);
3018 else if (gimple_store_p (stmt
))
3020 ao_ref_init (&ref
, gimple_assign_lhs (stmt
));
3021 ref_is_store
= true;
3023 else if (gimple_code (stmt
) == GIMPLE_OMP_RETURN
)
3025 else if (!gimple_has_side_effects (stmt
)
3026 && !gimple_could_trap_p (stmt
)
3027 && !stmt_could_throw_p (stmt
)
3028 && !gimple_vdef (stmt
)
3029 && !gimple_vuse (stmt
))
3031 else if (gimple_call_internal_p (stmt
, IFN_GOACC_DIM_POS
))
3033 else if (gimple_code (stmt
) == GIMPLE_RETURN
)
3039 fprintf (dump_file
, "Unhandled stmt in entry/exit: ");
3040 print_gimple_stmt (dump_file
, stmt
, 0);
3045 if (ref_conflicts_with_region (gsi
, &ref
, ref_is_store
, region_bbs
,
3050 fprintf (dump_file
, "conflicts with entry/exit stmt: ");
3051 print_gimple_stmt (dump_file
, stmt
, 0);
3061 /* Find stores inside REGION_BBS and outside IN_LOOP_BBS, and guard them with
3062 gang_pos == 0, except when the stores are REDUCTION_STORES. Return true
3063 if any changes were made. */
3066 oacc_entry_exit_single_gang (bitmap in_loop_bbs
, vec
<basic_block
> region_bbs
,
3067 bitmap reduction_stores
)
3069 tree gang_pos
= NULL_TREE
;
3070 bool changed
= false;
3074 FOR_EACH_VEC_ELT (region_bbs
, i
, bb
)
3076 if (bitmap_bit_p (in_loop_bbs
, bb
->index
))
3079 gimple_stmt_iterator gsi
;
3080 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
3082 gimple
*stmt
= gsi_stmt (gsi
);
3084 if (!gimple_store_p (stmt
))
3086 /* Update gsi to point to next stmt. */
3091 if (bitmap_bit_p (reduction_stores
,
3092 SSA_NAME_VERSION (gimple_vdef (stmt
))))
3097 "skipped reduction store for single-gang"
3099 print_gimple_stmt (dump_file
, stmt
, 0);
3102 /* Update gsi to point to next stmt. */
3109 if (gang_pos
== NULL_TREE
)
3111 tree arg
= build_int_cst (integer_type_node
, GOMP_DIM_GANG
);
3113 = gimple_build_call_internal (IFN_GOACC_DIM_POS
, 1, arg
);
3114 gang_pos
= make_ssa_name (integer_type_node
);
3115 gimple_call_set_lhs (gang_single
, gang_pos
);
3116 gimple_stmt_iterator start
3117 = gsi_start_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
3118 tree vuse
= ssa_default_def (cfun
, gimple_vop (cfun
));
3119 gimple_set_vuse (gang_single
, vuse
);
3120 gsi_insert_before (&start
, gang_single
, GSI_SAME_STMT
);
3126 "found store that needs single-gang neutering: ");
3127 print_gimple_stmt (dump_file
, stmt
, 0);
3131 /* Split block before store. */
3132 gimple_stmt_iterator gsi2
= gsi
;
3135 if (gsi_end_p (gsi2
))
3137 e
= split_block_after_labels (bb
);
3138 gsi2
= gsi_last_bb (bb
);
3141 e
= split_block (bb
, gsi_stmt (gsi2
));
3142 basic_block bb2
= e
->dest
;
3144 /* Split block after store. */
3145 gimple_stmt_iterator gsi3
= gsi_start_bb (bb2
);
3146 edge e2
= split_block (bb2
, gsi_stmt (gsi3
));
3147 basic_block bb3
= e2
->dest
;
3150 = gimple_build_cond (EQ_EXPR
, gang_pos
, integer_zero_node
,
3151 NULL_TREE
, NULL_TREE
);
3152 gsi_insert_after (&gsi2
, cond
, GSI_NEW_STMT
);
3154 edge e3
= make_edge (bb
, bb3
, EDGE_FALSE_VALUE
);
3155 /* FIXME: What is the probability? */
3156 e3
->probability
= profile_probability::guessed_never ();
3157 e
->flags
= EDGE_TRUE_VALUE
;
3159 tree vdef
= gimple_vdef (stmt
);
3160 tree vuse
= gimple_vuse (stmt
);
3162 tree phi_res
= copy_ssa_name (vdef
);
3163 gphi
*new_phi
= create_phi_node (phi_res
, bb3
);
3164 replace_uses_by (vdef
, phi_res
);
3165 add_phi_arg (new_phi
, vuse
, e3
, UNKNOWN_LOCATION
);
3166 add_phi_arg (new_phi
, vdef
, e2
, UNKNOWN_LOCATION
);
3168 /* Update gsi to point to next stmt. */
3170 gsi
= gsi_start_bb (bb
);
3178 /* Return true if the statements before and after the LOOP can be executed in
3179 parallel with the function containing the loop. Resolve conflicting stores
3180 outside LOOP by guarding them such that only a single gang executes them. */
3183 oacc_entry_exit_ok (struct loop
*loop
,
3184 reduction_info_table_type
*reduction_list
)
3186 basic_block
*loop_bbs
= get_loop_body_in_dom_order (loop
);
3187 vec
<basic_block
> region_bbs
3188 = get_all_dominated_blocks (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3190 bitmap in_loop_bbs
= BITMAP_ALLOC (NULL
);
3191 bitmap_clear (in_loop_bbs
);
3192 for (unsigned int i
= 0; i
< loop
->num_nodes
; i
++)
3193 bitmap_set_bit (in_loop_bbs
, loop_bbs
[i
]->index
);
3195 bitmap reduction_stores
= BITMAP_ALLOC (NULL
);
3196 bool res
= oacc_entry_exit_ok_1 (in_loop_bbs
, region_bbs
, reduction_list
,
3201 bool changed
= oacc_entry_exit_single_gang (in_loop_bbs
, region_bbs
,
3205 free_dominance_info (CDI_DOMINATORS
);
3206 calculate_dominance_info (CDI_DOMINATORS
);
3210 region_bbs
.release ();
3213 BITMAP_FREE (in_loop_bbs
);
3214 BITMAP_FREE (reduction_stores
);
3219 /* Detect parallel loops and generate parallel code using libgomp
3220 primitives. Returns true if some loop was parallelized, false
3224 parallelize_loops (bool oacc_kernels_p
)
3227 bool changed
= false;
3229 struct loop
*skip_loop
= NULL
;
3230 struct tree_niter_desc niter_desc
;
3231 struct obstack parloop_obstack
;
3232 HOST_WIDE_INT estimated
;
3233 source_location loop_loc
;
3235 /* Do not parallelize loops in the functions created by parallelization. */
3237 && parallelized_function_p (cfun
->decl
))
3240 /* Do not parallelize loops in offloaded functions. */
3242 && oacc_get_fn_attrib (cfun
->decl
) != NULL
)
3245 if (cfun
->has_nonlocal_label
)
3248 /* For OpenACC kernels, n_threads will be determined later; otherwise, it's
3249 the argument to -ftree-parallelize-loops. */
3253 n_threads
= flag_tree_parallelize_loops
;
3255 gcc_obstack_init (&parloop_obstack
);
3256 reduction_info_table_type
reduction_list (10);
3258 calculate_dominance_info (CDI_DOMINATORS
);
3260 FOR_EACH_LOOP (loop
, 0)
3262 if (loop
== skip_loop
)
3264 if (!loop
->in_oacc_kernels_region
3265 && dump_file
&& (dump_flags
& TDF_DETAILS
))
3267 "Skipping loop %d as inner loop of parallelized loop\n",
3270 skip_loop
= loop
->inner
;
3276 reduction_list
.empty ();
3280 if (!loop
->in_oacc_kernels_region
)
3283 /* Don't try to parallelize inner loops in an oacc kernels region. */
3285 skip_loop
= loop
->inner
;
3287 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3289 "Trying loop %d with header bb %d in oacc kernels"
3290 " region\n", loop
->num
, loop
->header
->index
);
3293 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3295 fprintf (dump_file
, "Trying loop %d as candidate\n",loop
->num
);
3297 fprintf (dump_file
, "loop %d is not innermost\n",loop
->num
);
3299 fprintf (dump_file
, "loop %d is innermost\n",loop
->num
);
3302 if (!single_dom_exit (loop
))
3305 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3306 fprintf (dump_file
, "loop is !single_dom_exit\n");
3311 if (/* And of course, the loop must be parallelizable. */
3312 !can_duplicate_loop_p (loop
)
3313 || loop_has_blocks_with_irreducible_flag (loop
)
3314 || (loop_preheader_edge (loop
)->src
->flags
& BB_IRREDUCIBLE_LOOP
)
3315 /* FIXME: the check for vector phi nodes could be removed. */
3316 || loop_has_vector_phi_nodes (loop
))
3319 estimated
= estimated_loop_iterations_int (loop
);
3320 if (estimated
== -1)
3321 estimated
= get_likely_max_loop_iterations_int (loop
);
3322 /* FIXME: Bypass this check as graphite doesn't update the
3323 count and frequency correctly now. */
3324 if (!flag_loop_parallelize_all
3326 && ((estimated
!= -1
3328 < ((HOST_WIDE_INT
) n_threads
3329 * (loop
->inner
? 2 : MIN_PER_THREAD
) - 1)))
3330 /* Do not bother with loops in cold areas. */
3331 || optimize_loop_nest_for_size_p (loop
)))
3334 if (!try_get_loop_niter (loop
, &niter_desc
))
3337 if (!try_create_reduction_list (loop
, &reduction_list
, oacc_kernels_p
))
3340 if (loop_has_phi_with_address_arg (loop
))
3343 if (!loop
->can_be_parallel
3344 && !loop_parallel_p (loop
, &parloop_obstack
))
3348 && !oacc_entry_exit_ok (loop
, &reduction_list
))
3351 fprintf (dump_file
, "entry/exit not ok: FAILED\n");
3356 skip_loop
= loop
->inner
;
3358 loop_loc
= find_loop_location (loop
);
3360 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loop_loc
,
3361 "parallelizing outer loop %d\n", loop
->num
);
3363 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loop_loc
,
3364 "parallelizing inner loop %d\n", loop
->num
);
3366 gen_parallel_loop (loop
, &reduction_list
,
3367 n_threads
, &niter_desc
, oacc_kernels_p
);
3370 obstack_free (&parloop_obstack
, NULL
);
3372 /* Parallelization will cause new function calls to be inserted through
3373 which local variables will escape. Reset the points-to solution
3376 pt_solution_reset (&cfun
->gimple_df
->escaped
);
3381 /* Parallelization. */
3385 const pass_data pass_data_parallelize_loops
=
3387 GIMPLE_PASS
, /* type */
3388 "parloops", /* name */
3389 OPTGROUP_LOOP
, /* optinfo_flags */
3390 TV_TREE_PARALLELIZE_LOOPS
, /* tv_id */
3391 ( PROP_cfg
| PROP_ssa
), /* properties_required */
3392 0, /* properties_provided */
3393 0, /* properties_destroyed */
3394 0, /* todo_flags_start */
3395 0, /* todo_flags_finish */
3398 class pass_parallelize_loops
: public gimple_opt_pass
3401 pass_parallelize_loops (gcc::context
*ctxt
)
3402 : gimple_opt_pass (pass_data_parallelize_loops
, ctxt
),
3403 oacc_kernels_p (false)
3406 /* opt_pass methods: */
3407 virtual bool gate (function
*)
3410 return flag_openacc
;
3412 return flag_tree_parallelize_loops
> 1;
3414 virtual unsigned int execute (function
*);
3415 opt_pass
* clone () { return new pass_parallelize_loops (m_ctxt
); }
3416 void set_pass_param (unsigned int n
, bool param
)
3418 gcc_assert (n
== 0);
3419 oacc_kernels_p
= param
;
3423 bool oacc_kernels_p
;
3424 }; // class pass_parallelize_loops
3427 pass_parallelize_loops::execute (function
*fun
)
3429 tree nthreads
= builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS
);
3430 if (nthreads
== NULL_TREE
)
3433 bool in_loop_pipeline
= scev_initialized_p ();
3434 if (!in_loop_pipeline
)
3435 loop_optimizer_init (LOOPS_NORMAL
3436 | LOOPS_HAVE_RECORDED_EXITS
);
3438 if (number_of_loops (fun
) <= 1)
3441 if (!in_loop_pipeline
)
3443 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
3447 unsigned int todo
= 0;
3448 if (parallelize_loops (oacc_kernels_p
))
3450 fun
->curr_properties
&= ~(PROP_gimple_eomp
);
3452 checking_verify_loop_structure ();
3454 todo
|= TODO_update_ssa
;
3457 if (!in_loop_pipeline
)
3460 loop_optimizer_finalize ();
3469 make_pass_parallelize_loops (gcc::context
*ctxt
)
3471 return new pass_parallelize_loops (ctxt
);