2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Georges-Andre Silber <Georges-Andre.Silber@ensmp.fr>
5 and Sebastian Pop <sebastian.pop@amd.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
14 GCC is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* This pass performs loop distribution: for example, the loop
40 This pass uses an RDG, Reduced Dependence Graph built on top of the
41 data dependence relations. The RDG is then topologically sorted to
42 obtain a map of information producers/consumers based on which it
43 generates the new loops. */
47 #include "coretypes.h"
48 #include "tree-flow.h"
50 #include "tree-chrec.h"
51 #include "tree-data-ref.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-pass.h"
55 enum partition_kind
{ PKIND_NORMAL
, PKIND_MEMSET
, PKIND_MEMCPY
};
57 typedef struct partition_s
61 enum partition_kind kind
;
62 /* data-references a kind != PKIND_NORMAL partition is about. */
63 data_reference_p main_dr
;
64 data_reference_p secondary_dr
;
67 DEF_VEC_P (partition_t
);
68 DEF_VEC_ALLOC_P (partition_t
, heap
);
70 /* Allocate and initialize a partition from BITMAP. */
73 partition_alloc (bitmap stmts
)
75 partition_t partition
= XCNEW (struct partition_s
);
76 partition
->stmts
= stmts
? stmts
: BITMAP_ALLOC (NULL
);
77 partition
->has_writes
= false;
78 partition
->kind
= PKIND_NORMAL
;
85 partition_free (partition_t partition
)
87 BITMAP_FREE (partition
->stmts
);
91 /* Returns true if the partition can be generated as a builtin. */
94 partition_builtin_p (partition_t partition
)
96 return partition
->kind
!= PKIND_NORMAL
;
99 /* Returns true if the partition has an writes. */
102 partition_has_writes (partition_t partition
)
104 return partition
->has_writes
;
107 /* If bit I is not set, it means that this node represents an
108 operation that has already been performed, and that should not be
109 performed again. This is the subgraph of remaining important
110 computations that is passed to the DFS algorithm for avoiding to
111 include several times the same stores in different loops. */
112 static bitmap remaining_stmts
;
114 /* A node of the RDG is marked in this bitmap when it has as a
115 predecessor a node that writes to memory. */
116 static bitmap upstream_mem_writes
;
118 /* Returns true when DEF is an SSA_NAME defined in LOOP and used after
122 ssa_name_has_uses_outside_loop_p (tree def
, loop_p loop
)
124 imm_use_iterator imm_iter
;
127 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
128 if (loop
!= loop_containing_stmt (USE_STMT (use_p
)))
134 /* Returns true when STMT defines a scalar variable used after the
138 stmt_has_scalar_dependences_outside_loop (loop_p loop
, gimple stmt
)
143 if (gimple_code (stmt
) == GIMPLE_PHI
)
144 return ssa_name_has_uses_outside_loop_p (gimple_phi_result (stmt
), loop
);
146 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, op_iter
, SSA_OP_DEF
)
147 if (ssa_name_has_uses_outside_loop_p (DEF_FROM_PTR (def_p
), loop
))
153 /* Update the PHI nodes of NEW_LOOP. NEW_LOOP is a duplicate of
157 update_phis_for_loop_copy (struct loop
*orig_loop
, struct loop
*new_loop
)
160 gimple_stmt_iterator si_new
, si_orig
;
161 edge orig_loop_latch
= loop_latch_edge (orig_loop
);
162 edge orig_entry_e
= loop_preheader_edge (orig_loop
);
163 edge new_loop_entry_e
= loop_preheader_edge (new_loop
);
165 /* Scan the phis in the headers of the old and new loops
166 (they are organized in exactly the same order). */
167 for (si_new
= gsi_start_phis (new_loop
->header
),
168 si_orig
= gsi_start_phis (orig_loop
->header
);
169 !gsi_end_p (si_new
) && !gsi_end_p (si_orig
);
170 gsi_next (&si_new
), gsi_next (&si_orig
))
173 source_location locus
;
174 gimple phi_new
= gsi_stmt (si_new
);
175 gimple phi_orig
= gsi_stmt (si_orig
);
177 /* Add the first phi argument for the phi in NEW_LOOP (the one
178 associated with the entry of NEW_LOOP) */
179 def
= PHI_ARG_DEF_FROM_EDGE (phi_orig
, orig_entry_e
);
180 locus
= gimple_phi_arg_location_from_edge (phi_orig
, orig_entry_e
);
181 add_phi_arg (phi_new
, def
, new_loop_entry_e
, locus
);
183 /* Add the second phi argument for the phi in NEW_LOOP (the one
184 associated with the latch of NEW_LOOP) */
185 def
= PHI_ARG_DEF_FROM_EDGE (phi_orig
, orig_loop_latch
);
186 locus
= gimple_phi_arg_location_from_edge (phi_orig
, orig_loop_latch
);
188 if (TREE_CODE (def
) == SSA_NAME
)
190 new_ssa_name
= get_current_def (def
);
193 /* This only happens if there are no definitions inside the
194 loop. Use the the invariant in the new loop as is. */
198 /* Could be an integer. */
201 add_phi_arg (phi_new
, new_ssa_name
, loop_latch_edge (new_loop
), locus
);
205 /* Return a copy of LOOP placed before LOOP. */
208 copy_loop_before (struct loop
*loop
)
211 edge preheader
= loop_preheader_edge (loop
);
213 initialize_original_copy_tables ();
214 res
= slpeel_tree_duplicate_loop_to_edge_cfg (loop
, preheader
);
215 gcc_assert (res
!= NULL
);
216 free_original_copy_tables ();
218 update_phis_for_loop_copy (loop
, res
);
219 rename_variables_in_loop (res
);
224 /* Creates an empty basic block after LOOP. */
227 create_bb_after_loop (struct loop
*loop
)
229 edge exit
= single_exit (loop
);
237 /* Generate code for PARTITION from the code in LOOP. The loop is
238 copied when COPY_P is true. All the statements not flagged in the
239 PARTITION bitmap are removed from the loop or from its copy. The
240 statements are indexed in sequence inside a basic block, and the
241 basic blocks of a loop are taken in dom order. */
244 generate_loops_for_partition (struct loop
*loop
, partition_t partition
,
248 gimple_stmt_iterator bsi
;
253 loop
= copy_loop_before (loop
);
254 gcc_assert (loop
!= NULL
);
255 create_preheader (loop
, CP_SIMPLE_PREHEADERS
);
256 create_bb_after_loop (loop
);
259 /* Remove stmts not in the PARTITION bitmap. The order in which we
260 visit the phi nodes and the statements is exactly as in
262 bbs
= get_loop_body_in_dom_order (loop
);
264 if (MAY_HAVE_DEBUG_STMTS
)
265 for (x
= 0, i
= 0; i
< loop
->num_nodes
; i
++)
267 basic_block bb
= bbs
[i
];
269 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
270 if (!bitmap_bit_p (partition
->stmts
, x
++))
271 reset_debug_uses (gsi_stmt (bsi
));
273 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
275 gimple stmt
= gsi_stmt (bsi
);
276 if (gimple_code (stmt
) != GIMPLE_LABEL
277 && !is_gimple_debug (stmt
)
278 && !bitmap_bit_p (partition
->stmts
, x
++))
279 reset_debug_uses (stmt
);
283 for (x
= 0, i
= 0; i
< loop
->num_nodes
; i
++)
285 basic_block bb
= bbs
[i
];
287 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
);)
288 if (!bitmap_bit_p (partition
->stmts
, x
++))
290 gimple phi
= gsi_stmt (bsi
);
291 if (virtual_operand_p (gimple_phi_result (phi
)))
292 mark_virtual_phi_result_for_renaming (phi
);
293 remove_phi_node (&bsi
, true);
298 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);)
300 gimple stmt
= gsi_stmt (bsi
);
301 if (gimple_code (stmt
) != GIMPLE_LABEL
302 && !is_gimple_debug (stmt
)
303 && !bitmap_bit_p (partition
->stmts
, x
++))
305 unlink_stmt_vdef (stmt
);
306 gsi_remove (&bsi
, true);
317 /* Build the size argument for a memory operation call. */
320 build_size_arg_loc (location_t loc
, data_reference_p dr
, tree nb_iter
)
323 size
= fold_build2_loc (loc
, MULT_EXPR
, sizetype
,
324 fold_convert_loc (loc
, sizetype
, nb_iter
),
325 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
))));
326 return fold_convert_loc (loc
, size_type_node
, size
);
329 /* Build an address argument for a memory operation call. */
332 build_addr_arg_loc (location_t loc
, data_reference_p dr
, tree nb_bytes
)
336 addr_base
= size_binop_loc (loc
, PLUS_EXPR
, DR_OFFSET (dr
), DR_INIT (dr
));
337 addr_base
= fold_convert_loc (loc
, sizetype
, addr_base
);
339 /* Test for a negative stride, iterating over every element. */
340 if (tree_int_cst_sgn (DR_STEP (dr
)) == -1)
342 addr_base
= size_binop_loc (loc
, MINUS_EXPR
, addr_base
,
343 fold_convert_loc (loc
, sizetype
, nb_bytes
));
344 addr_base
= size_binop_loc (loc
, PLUS_EXPR
, addr_base
,
345 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
))));
348 return fold_build_pointer_plus_loc (loc
, DR_BASE_ADDRESS (dr
), addr_base
);
351 /* Generate a call to memset for PARTITION in LOOP. */
354 generate_memset_builtin (struct loop
*loop
, partition_t partition
)
356 gimple_stmt_iterator gsi
;
357 gimple stmt
, fn_call
;
358 tree nb_iter
, mem
, fn
, nb_bytes
;
362 stmt
= DR_STMT (partition
->main_dr
);
363 loc
= gimple_location (stmt
);
364 if (gimple_bb (stmt
) == loop
->latch
)
365 nb_iter
= number_of_latch_executions (loop
);
367 nb_iter
= number_of_exit_cond_executions (loop
);
369 /* The new statements will be placed before LOOP. */
370 gsi
= gsi_last_bb (loop_preheader_edge (loop
)->src
);
372 nb_bytes
= build_size_arg_loc (loc
, partition
->main_dr
, nb_iter
);
373 nb_bytes
= force_gimple_operand_gsi (&gsi
, nb_bytes
, true, NULL_TREE
,
374 false, GSI_CONTINUE_LINKING
);
375 mem
= build_addr_arg_loc (loc
, partition
->main_dr
, nb_bytes
);
376 mem
= force_gimple_operand_gsi (&gsi
, mem
, true, NULL_TREE
,
377 false, GSI_CONTINUE_LINKING
);
379 /* This exactly matches the pattern recognition in classify_partition. */
380 val
= gimple_assign_rhs1 (stmt
);
381 if (integer_zerop (val
)
383 || TREE_CODE (val
) == CONSTRUCTOR
)
384 val
= integer_zero_node
;
385 else if (integer_all_onesp (val
))
386 val
= build_int_cst (integer_type_node
, -1);
389 if (TREE_CODE (val
) == INTEGER_CST
)
390 val
= fold_convert (integer_type_node
, val
);
391 else if (!useless_type_conversion_p (integer_type_node
, TREE_TYPE (val
)))
394 tree tem
= make_ssa_name (integer_type_node
, NULL
);
395 cstmt
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, val
, NULL_TREE
);
396 gsi_insert_after (&gsi
, cstmt
, GSI_CONTINUE_LINKING
);
401 fn
= build_fold_addr_expr (builtin_decl_implicit (BUILT_IN_MEMSET
));
402 fn_call
= gimple_build_call (fn
, 3, mem
, val
, nb_bytes
);
403 gsi_insert_after (&gsi
, fn_call
, GSI_CONTINUE_LINKING
);
405 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
407 fprintf (dump_file
, "generated memset");
408 if (integer_zerop (val
))
409 fprintf (dump_file
, " zero\n");
410 else if (integer_all_onesp (val
))
411 fprintf (dump_file
, " minus one\n");
413 fprintf (dump_file
, "\n");
417 /* Generate a call to memcpy for PARTITION in LOOP. */
420 generate_memcpy_builtin (struct loop
*loop
, partition_t partition
)
422 gimple_stmt_iterator gsi
;
423 gimple stmt
, fn_call
;
424 tree nb_iter
, dest
, src
, fn
, nb_bytes
;
426 enum built_in_function kind
;
428 stmt
= DR_STMT (partition
->main_dr
);
429 loc
= gimple_location (stmt
);
430 if (gimple_bb (stmt
) == loop
->latch
)
431 nb_iter
= number_of_latch_executions (loop
);
433 nb_iter
= number_of_exit_cond_executions (loop
);
435 /* The new statements will be placed before LOOP. */
436 gsi
= gsi_last_bb (loop_preheader_edge (loop
)->src
);
438 nb_bytes
= build_size_arg_loc (loc
, partition
->main_dr
, nb_iter
);
439 nb_bytes
= force_gimple_operand_gsi (&gsi
, nb_bytes
, true, NULL_TREE
,
440 false, GSI_CONTINUE_LINKING
);
441 dest
= build_addr_arg_loc (loc
, partition
->main_dr
, nb_bytes
);
442 src
= build_addr_arg_loc (loc
, partition
->secondary_dr
, nb_bytes
);
443 if (ptr_derefs_may_alias_p (dest
, src
))
444 kind
= BUILT_IN_MEMMOVE
;
446 kind
= BUILT_IN_MEMCPY
;
448 dest
= force_gimple_operand_gsi (&gsi
, dest
, true, NULL_TREE
,
449 false, GSI_CONTINUE_LINKING
);
450 src
= force_gimple_operand_gsi (&gsi
, src
, true, NULL_TREE
,
451 false, GSI_CONTINUE_LINKING
);
452 fn
= build_fold_addr_expr (builtin_decl_implicit (kind
));
453 fn_call
= gimple_build_call (fn
, 3, dest
, src
, nb_bytes
);
454 gsi_insert_after (&gsi
, fn_call
, GSI_CONTINUE_LINKING
);
456 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
458 if (kind
== BUILT_IN_MEMCPY
)
459 fprintf (dump_file
, "generated memcpy\n");
461 fprintf (dump_file
, "generated memmove\n");
465 /* Remove and destroy the loop LOOP. */
468 destroy_loop (struct loop
*loop
)
470 unsigned nbbs
= loop
->num_nodes
;
471 edge exit
= single_exit (loop
);
472 basic_block src
= loop_preheader_edge (loop
)->src
, dest
= exit
->dest
;
476 bbs
= get_loop_body_in_dom_order (loop
);
478 redirect_edge_pred (exit
, src
);
479 exit
->flags
&= ~(EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
480 exit
->flags
|= EDGE_FALLTHRU
;
481 cancel_loop_tree (loop
);
482 rescan_loop_exit (exit
, false, true);
484 for (i
= 0; i
< nbbs
; i
++)
486 /* We have made sure to not leave any dangling uses of SSA
487 names defined in the loop. With the exception of virtuals.
488 Make sure we replace all uses of virtual defs that will remain
489 outside of the loop with the bare symbol as delete_basic_block
490 will release them. */
491 gimple_stmt_iterator gsi
;
492 for (gsi
= gsi_start_phis (bbs
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
494 gimple phi
= gsi_stmt (gsi
);
495 if (virtual_operand_p (gimple_phi_result (phi
)))
496 mark_virtual_phi_result_for_renaming (phi
);
498 for (gsi
= gsi_start_bb (bbs
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
500 gimple stmt
= gsi_stmt (gsi
);
501 tree vdef
= gimple_vdef (stmt
);
502 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
503 mark_virtual_operand_for_renaming (vdef
);
505 delete_basic_block (bbs
[i
]);
509 set_immediate_dominator (CDI_DOMINATORS
, dest
,
510 recompute_dominator (CDI_DOMINATORS
, dest
));
513 /* Generates code for PARTITION. */
516 generate_code_for_partition (struct loop
*loop
,
517 partition_t partition
, bool copy_p
)
519 switch (partition
->kind
)
522 generate_memset_builtin (loop
, partition
);
523 /* If this is the last partition for which we generate code, we have
524 to destroy the loop. */
530 generate_memcpy_builtin (loop
, partition
);
531 /* If this is the last partition for which we generate code, we have
532 to destroy the loop. */
538 generate_loops_for_partition (loop
, partition
, copy_p
);
547 /* Returns true if the node V of RDG cannot be recomputed. */
550 rdg_cannot_recompute_vertex_p (struct graph
*rdg
, int v
)
552 if (RDG_MEM_WRITE_STMT (rdg
, v
))
558 /* Returns true when the vertex V has already been generated in the
559 current partition (V is in PROCESSED), or when V belongs to another
560 partition and cannot be recomputed (V is not in REMAINING_STMTS). */
563 already_processed_vertex_p (bitmap processed
, int v
)
565 return (bitmap_bit_p (processed
, v
)
566 || !bitmap_bit_p (remaining_stmts
, v
));
569 /* Returns NULL when there is no anti-dependence among the successors
570 of vertex V, otherwise returns the edge with the anti-dep. */
572 static struct graph_edge
*
573 has_anti_dependence (struct vertex
*v
)
575 struct graph_edge
*e
;
578 for (e
= v
->succ
; e
; e
= e
->succ_next
)
579 if (RDGE_TYPE (e
) == anti_dd
)
585 /* Returns true when V has an anti-dependence edge among its successors. */
588 predecessor_has_mem_write (struct graph
*rdg
, struct vertex
*v
)
590 struct graph_edge
*e
;
593 for (e
= v
->pred
; e
; e
= e
->pred_next
)
594 if (bitmap_bit_p (upstream_mem_writes
, e
->src
)
595 /* Don't consider flow channels: a write to memory followed
596 by a read from memory. These channels allow the split of
597 the RDG in different partitions. */
598 && !RDG_MEM_WRITE_STMT (rdg
, e
->src
))
604 /* Initializes the upstream_mem_writes bitmap following the
605 information from RDG. */
608 mark_nodes_having_upstream_mem_writes (struct graph
*rdg
)
611 bitmap seen
= BITMAP_ALLOC (NULL
);
613 for (v
= rdg
->n_vertices
- 1; v
>= 0; v
--)
614 if (!bitmap_bit_p (seen
, v
))
617 VEC (int, heap
) *nodes
= VEC_alloc (int, heap
, 3);
619 graphds_dfs (rdg
, &v
, 1, &nodes
, false, NULL
);
621 FOR_EACH_VEC_ELT (int, nodes
, i
, x
)
623 if (!bitmap_set_bit (seen
, x
))
626 if (RDG_MEM_WRITE_STMT (rdg
, x
)
627 || predecessor_has_mem_write (rdg
, &(rdg
->vertices
[x
]))
628 /* In anti dependences the read should occur before
629 the write, this is why both the read and the write
630 should be placed in the same partition. */
631 || has_anti_dependence (&(rdg
->vertices
[x
])))
633 bitmap_set_bit (upstream_mem_writes
, x
);
637 VEC_free (int, heap
, nodes
);
641 /* Returns true when vertex u has a memory write node as a predecessor
645 has_upstream_mem_writes (int u
)
647 return bitmap_bit_p (upstream_mem_writes
, u
);
650 static void rdg_flag_vertex_and_dependent (struct graph
*, int, partition_t
,
653 /* Flag the uses of U stopping following the information from
654 upstream_mem_writes. */
657 rdg_flag_uses (struct graph
*rdg
, int u
, partition_t partition
, bitmap loops
,
661 struct vertex
*x
= &(rdg
->vertices
[u
]);
662 gimple stmt
= RDGV_STMT (x
);
663 struct graph_edge
*anti_dep
= has_anti_dependence (x
);
665 /* Keep in the same partition the destination of an antidependence,
666 because this is a store to the exact same location. Putting this
667 in another partition is bad for cache locality. */
670 int v
= anti_dep
->dest
;
672 if (!already_processed_vertex_p (processed
, v
))
673 rdg_flag_vertex_and_dependent (rdg
, v
, partition
, loops
,
677 if (gimple_code (stmt
) != GIMPLE_PHI
)
679 if ((use_p
= gimple_vuse_op (stmt
)) != NULL_USE_OPERAND_P
)
681 tree use
= USE_FROM_PTR (use_p
);
683 if (TREE_CODE (use
) == SSA_NAME
)
685 gimple def_stmt
= SSA_NAME_DEF_STMT (use
);
686 int v
= rdg_vertex_for_stmt (rdg
, def_stmt
);
689 && !already_processed_vertex_p (processed
, v
))
690 rdg_flag_vertex_and_dependent (rdg
, v
, partition
, loops
,
696 if (is_gimple_assign (stmt
) && has_upstream_mem_writes (u
))
698 tree op0
= gimple_assign_lhs (stmt
);
700 /* Scalar channels don't have enough space for transmitting data
701 between tasks, unless we add more storage by privatizing. */
702 if (is_gimple_reg (op0
))
705 imm_use_iterator iter
;
707 FOR_EACH_IMM_USE_FAST (use_p
, iter
, op0
)
709 int v
= rdg_vertex_for_stmt (rdg
, USE_STMT (use_p
));
711 if (!already_processed_vertex_p (processed
, v
))
712 rdg_flag_vertex_and_dependent (rdg
, v
, partition
, loops
,
719 /* Flag V from RDG as part of PARTITION, and also flag its loop number
723 rdg_flag_vertex (struct graph
*rdg
, int v
, partition_t partition
, bitmap loops
)
727 if (!bitmap_set_bit (partition
->stmts
, v
))
730 loop
= loop_containing_stmt (RDG_STMT (rdg
, v
));
731 bitmap_set_bit (loops
, loop
->num
);
733 if (rdg_cannot_recompute_vertex_p (rdg
, v
))
735 partition
->has_writes
= true;
736 bitmap_clear_bit (remaining_stmts
, v
);
740 /* Flag in the bitmap PARTITION the vertex V and all its predecessors.
741 Also flag their loop number in LOOPS. */
744 rdg_flag_vertex_and_dependent (struct graph
*rdg
, int v
, partition_t partition
,
745 bitmap loops
, bitmap processed
)
748 VEC (int, heap
) *nodes
= VEC_alloc (int, heap
, 3);
751 bitmap_set_bit (processed
, v
);
752 rdg_flag_uses (rdg
, v
, partition
, loops
, processed
);
753 graphds_dfs (rdg
, &v
, 1, &nodes
, false, remaining_stmts
);
754 rdg_flag_vertex (rdg
, v
, partition
, loops
);
756 FOR_EACH_VEC_ELT (int, nodes
, i
, x
)
757 if (!already_processed_vertex_p (processed
, x
))
758 rdg_flag_vertex_and_dependent (rdg
, x
, partition
, loops
, processed
);
760 VEC_free (int, heap
, nodes
);
763 /* Initialize CONDS with all the condition statements from the basic
767 collect_condition_stmts (struct loop
*loop
, VEC (gimple
, heap
) **conds
)
771 VEC (edge
, heap
) *exits
= get_loop_exit_edges (loop
);
773 FOR_EACH_VEC_ELT (edge
, exits
, i
, e
)
775 gimple cond
= last_stmt (e
->src
);
778 VEC_safe_push (gimple
, heap
, *conds
, cond
);
781 VEC_free (edge
, heap
, exits
);
784 /* Add to PARTITION all the exit condition statements for LOOPS
785 together with all their dependent statements determined from
789 rdg_flag_loop_exits (struct graph
*rdg
, bitmap loops
, partition_t partition
,
794 VEC (gimple
, heap
) *conds
= VEC_alloc (gimple
, heap
, 3);
796 EXECUTE_IF_SET_IN_BITMAP (loops
, 0, i
, bi
)
797 collect_condition_stmts (get_loop (i
), &conds
);
799 while (!VEC_empty (gimple
, conds
))
801 gimple cond
= VEC_pop (gimple
, conds
);
802 int v
= rdg_vertex_for_stmt (rdg
, cond
);
803 bitmap new_loops
= BITMAP_ALLOC (NULL
);
805 if (!already_processed_vertex_p (processed
, v
))
806 rdg_flag_vertex_and_dependent (rdg
, v
, partition
, new_loops
, processed
);
808 EXECUTE_IF_SET_IN_BITMAP (new_loops
, 0, i
, bi
)
809 if (bitmap_set_bit (loops
, i
))
810 collect_condition_stmts (get_loop (i
), &conds
);
812 BITMAP_FREE (new_loops
);
815 VEC_free (gimple
, heap
, conds
);
818 /* Returns a bitmap in which all the statements needed for computing
819 the strongly connected component C of the RDG are flagged, also
820 including the loop exit conditions. */
823 build_rdg_partition_for_component (struct graph
*rdg
, rdgc c
)
826 partition_t partition
= partition_alloc (NULL
);
827 bitmap loops
= BITMAP_ALLOC (NULL
);
828 bitmap processed
= BITMAP_ALLOC (NULL
);
830 FOR_EACH_VEC_ELT (int, c
->vertices
, i
, v
)
831 if (!already_processed_vertex_p (processed
, v
))
832 rdg_flag_vertex_and_dependent (rdg
, v
, partition
, loops
, processed
);
834 rdg_flag_loop_exits (rdg
, loops
, partition
, processed
);
836 BITMAP_FREE (processed
);
841 /* Free memory for COMPONENTS. */
844 free_rdg_components (VEC (rdgc
, heap
) *components
)
849 FOR_EACH_VEC_ELT (rdgc
, components
, i
, x
)
851 VEC_free (int, heap
, x
->vertices
);
855 VEC_free (rdgc
, heap
, components
);
858 /* Build the COMPONENTS vector with the strongly connected components
859 of RDG in which the STARTING_VERTICES occur. */
862 rdg_build_components (struct graph
*rdg
, VEC (int, heap
) *starting_vertices
,
863 VEC (rdgc
, heap
) **components
)
866 bitmap saved_components
= BITMAP_ALLOC (NULL
);
867 int n_components
= graphds_scc (rdg
, NULL
);
868 VEC (int, heap
) **all_components
= XNEWVEC (VEC (int, heap
) *, n_components
);
870 for (i
= 0; i
< n_components
; i
++)
871 all_components
[i
] = VEC_alloc (int, heap
, 3);
873 for (i
= 0; i
< rdg
->n_vertices
; i
++)
874 VEC_safe_push (int, heap
, all_components
[rdg
->vertices
[i
].component
], i
);
876 FOR_EACH_VEC_ELT (int, starting_vertices
, i
, v
)
878 int c
= rdg
->vertices
[v
].component
;
880 if (bitmap_set_bit (saved_components
, c
))
882 rdgc x
= XCNEW (struct rdg_component
);
884 x
->vertices
= all_components
[c
];
886 VEC_safe_push (rdgc
, heap
, *components
, x
);
890 for (i
= 0; i
< n_components
; i
++)
891 if (!bitmap_bit_p (saved_components
, i
))
892 VEC_free (int, heap
, all_components
[i
]);
894 free (all_components
);
895 BITMAP_FREE (saved_components
);
898 /* Classifies the builtin kind we can generate for PARTITION of RDG and LOOP.
899 For the moment we detect only the memset zero pattern. */
902 classify_partition (loop_p loop
, struct graph
*rdg
, partition_t partition
)
907 data_reference_p single_load
, single_store
;
909 partition
->kind
= PKIND_NORMAL
;
910 partition
->main_dr
= NULL
;
911 partition
->secondary_dr
= NULL
;
913 if (!flag_tree_loop_distribute_patterns
)
916 /* Perform general partition disqualification for builtins. */
917 nb_iter
= number_of_exit_cond_executions (loop
);
918 if (!nb_iter
|| nb_iter
== chrec_dont_know
)
921 EXECUTE_IF_SET_IN_BITMAP (partition
->stmts
, 0, i
, bi
)
923 gimple stmt
= RDG_STMT (rdg
, i
);
925 if (gimple_has_volatile_ops (stmt
))
928 /* If the stmt has uses outside of the loop fail.
929 ??? If the stmt is generated in another partition that
930 is not created as builtin we can ignore this. */
931 if (stmt_has_scalar_dependences_outside_loop (loop
, stmt
))
933 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
934 fprintf (dump_file
, "not generating builtin, partition has "
935 "scalar uses outside of the loop\n");
940 /* Detect memset and memcpy. */
943 EXECUTE_IF_SET_IN_BITMAP (partition
->stmts
, 0, i
, bi
)
945 gimple stmt
= RDG_STMT (rdg
, i
);
949 if (gimple_code (stmt
) == GIMPLE_PHI
)
952 /* Any scalar stmts are ok. */
953 if (!gimple_vuse (stmt
))
956 /* Otherwise just regular loads/stores. */
957 if (!gimple_assign_single_p (stmt
))
960 /* But exactly one store and/or load. */
962 VEC_iterate (data_reference_p
, RDG_DATAREFS (rdg
, i
), j
, dr
); ++j
)
966 if (single_load
!= NULL
)
972 if (single_store
!= NULL
)
979 if (single_store
&& !single_load
)
981 gimple stmt
= DR_STMT (single_store
);
982 tree rhs
= gimple_assign_rhs1 (stmt
);
983 if (!(integer_zerop (rhs
)
984 || integer_all_onesp (rhs
)
986 || (TREE_CODE (rhs
) == CONSTRUCTOR
987 && !TREE_CLOBBER_P (rhs
))
988 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
989 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)))
990 == TYPE_MODE (unsigned_char_type_node
)))))
992 if (TREE_CODE (rhs
) == SSA_NAME
993 && !SSA_NAME_IS_DEFAULT_DEF (rhs
)
994 && flow_bb_inside_loop_p (loop
, gimple_bb (SSA_NAME_DEF_STMT (rhs
))))
996 if (!adjacent_dr_p (single_store
))
998 partition
->kind
= PKIND_MEMSET
;
999 partition
->main_dr
= single_store
;
1001 else if (single_store
&& single_load
)
1003 gimple store
= DR_STMT (single_store
);
1004 gimple load
= DR_STMT (single_load
);
1005 /* Direct aggregate copy or via an SSA name temporary. */
1007 && gimple_assign_lhs (load
) != gimple_assign_rhs1 (store
))
1009 if (!adjacent_dr_p (single_store
)
1010 || !adjacent_dr_p (single_load
)
1011 || !operand_equal_p (DR_STEP (single_store
),
1012 DR_STEP (single_load
), 0))
1014 partition
->kind
= PKIND_MEMCPY
;
1015 partition
->main_dr
= single_store
;
1016 partition
->secondary_dr
= single_load
;
1020 /* For a data reference REF, return the declaration of its base
1021 address or NULL_TREE if the base is not determined. */
1024 ref_base_address (data_reference_p dr
)
1026 tree base_address
= DR_BASE_ADDRESS (dr
);
1028 && TREE_CODE (base_address
) == ADDR_EXPR
)
1029 return TREE_OPERAND (base_address
, 0);
1031 return base_address
;
1034 /* Returns true when PARTITION1 and PARTITION2 have similar memory
1038 similar_memory_accesses (struct graph
*rdg
, partition_t partition1
,
1039 partition_t partition2
)
1041 unsigned i
, j
, k
, l
;
1042 bitmap_iterator bi
, bj
;
1043 data_reference_p ref1
, ref2
;
1045 /* First check whether in the intersection of the two partitions are
1046 any loads or stores. Common loads are the situation that happens
1048 EXECUTE_IF_AND_IN_BITMAP (partition1
->stmts
, partition2
->stmts
, 0, i
, bi
)
1049 if (RDG_MEM_WRITE_STMT (rdg
, i
)
1050 || RDG_MEM_READS_STMT (rdg
, i
))
1053 /* Then check all data-references against each other. */
1054 EXECUTE_IF_SET_IN_BITMAP (partition1
->stmts
, 0, i
, bi
)
1055 if (RDG_MEM_WRITE_STMT (rdg
, i
)
1056 || RDG_MEM_READS_STMT (rdg
, i
))
1057 EXECUTE_IF_SET_IN_BITMAP (partition2
->stmts
, 0, j
, bj
)
1058 if (RDG_MEM_WRITE_STMT (rdg
, j
)
1059 || RDG_MEM_READS_STMT (rdg
, j
))
1061 FOR_EACH_VEC_ELT (data_reference_p
, RDG_DATAREFS (rdg
, i
), k
, ref1
)
1063 tree base1
= ref_base_address (ref1
);
1065 FOR_EACH_VEC_ELT (data_reference_p
,
1066 RDG_DATAREFS (rdg
, j
), l
, ref2
)
1067 if (base1
== ref_base_address (ref2
))
1075 /* Aggregate several components into a useful partition that is
1076 registered in the PARTITIONS vector. Partitions will be
1077 distributed in different loops. */
1080 rdg_build_partitions (struct graph
*rdg
, VEC (rdgc
, heap
) *components
,
1081 VEC (int, heap
) **other_stores
,
1082 VEC (partition_t
, heap
) **partitions
, bitmap processed
)
1086 partition_t partition
= partition_alloc (NULL
);
1088 FOR_EACH_VEC_ELT (rdgc
, components
, i
, x
)
1091 int v
= VEC_index (int, x
->vertices
, 0);
1093 if (bitmap_bit_p (processed
, v
))
1096 np
= build_rdg_partition_for_component (rdg
, x
);
1097 bitmap_ior_into (partition
->stmts
, np
->stmts
);
1098 partition
->has_writes
= partition_has_writes (np
);
1099 bitmap_ior_into (processed
, np
->stmts
);
1100 partition_free (np
);
1102 if (partition_has_writes (partition
))
1104 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1106 fprintf (dump_file
, "ldist useful partition:\n");
1107 dump_bitmap (dump_file
, partition
->stmts
);
1110 VEC_safe_push (partition_t
, heap
, *partitions
, partition
);
1111 partition
= partition_alloc (NULL
);
1115 /* Add the nodes from the RDG that were not marked as processed, and
1116 that are used outside the current loop. These are scalar
1117 computations that are not yet part of previous partitions. */
1118 for (i
= 0; i
< rdg
->n_vertices
; i
++)
1119 if (!bitmap_bit_p (processed
, i
)
1120 && rdg_defs_used_in_other_loops_p (rdg
, i
))
1121 VEC_safe_push (int, heap
, *other_stores
, i
);
1123 /* If there are still statements left in the OTHER_STORES array,
1124 create other components and partitions with these stores and
1125 their dependences. */
1126 if (VEC_length (int, *other_stores
) > 0)
1128 VEC (rdgc
, heap
) *comps
= VEC_alloc (rdgc
, heap
, 3);
1129 VEC (int, heap
) *foo
= VEC_alloc (int, heap
, 3);
1131 rdg_build_components (rdg
, *other_stores
, &comps
);
1132 rdg_build_partitions (rdg
, comps
, &foo
, partitions
, processed
);
1134 VEC_free (int, heap
, foo
);
1135 free_rdg_components (comps
);
1138 /* If there is something left in the last partition, save it. */
1139 if (bitmap_count_bits (partition
->stmts
) > 0)
1140 VEC_safe_push (partition_t
, heap
, *partitions
, partition
);
1142 partition_free (partition
);
1145 /* Dump to FILE the PARTITIONS. */
1148 dump_rdg_partitions (FILE *file
, VEC (partition_t
, heap
) *partitions
)
1151 partition_t partition
;
1153 FOR_EACH_VEC_ELT (partition_t
, partitions
, i
, partition
)
1154 debug_bitmap_file (file
, partition
->stmts
);
1157 /* Debug PARTITIONS. */
1158 extern void debug_rdg_partitions (VEC (partition_t
, heap
) *);
1161 debug_rdg_partitions (VEC (partition_t
, heap
) *partitions
)
1163 dump_rdg_partitions (stderr
, partitions
);
1166 /* Returns the number of read and write operations in the RDG. */
1169 number_of_rw_in_rdg (struct graph
*rdg
)
1173 for (i
= 0; i
< rdg
->n_vertices
; i
++)
1175 if (RDG_MEM_WRITE_STMT (rdg
, i
))
1178 if (RDG_MEM_READS_STMT (rdg
, i
))
1185 /* Returns the number of read and write operations in a PARTITION of
1189 number_of_rw_in_partition (struct graph
*rdg
, partition_t partition
)
1195 EXECUTE_IF_SET_IN_BITMAP (partition
->stmts
, 0, i
, ii
)
1197 if (RDG_MEM_WRITE_STMT (rdg
, i
))
1200 if (RDG_MEM_READS_STMT (rdg
, i
))
1207 /* Returns true when one of the PARTITIONS contains all the read or
1208 write operations of RDG. */
1211 partition_contains_all_rw (struct graph
*rdg
, VEC (partition_t
, heap
) *partitions
)
1214 partition_t partition
;
1215 int nrw
= number_of_rw_in_rdg (rdg
);
1217 FOR_EACH_VEC_ELT (partition_t
, partitions
, i
, partition
)
1218 if (nrw
== number_of_rw_in_partition (rdg
, partition
))
1224 /* Generate code from STARTING_VERTICES in RDG. Returns the number of
1225 distributed loops. */
1228 ldist_gen (struct loop
*loop
, struct graph
*rdg
,
1229 VEC (int, heap
) *starting_vertices
)
1232 VEC (rdgc
, heap
) *components
= VEC_alloc (rdgc
, heap
, 3);
1233 VEC (partition_t
, heap
) *partitions
= VEC_alloc (partition_t
, heap
, 3);
1234 VEC (int, heap
) *other_stores
= VEC_alloc (int, heap
, 3);
1235 partition_t partition
;
1236 bitmap processed
= BITMAP_ALLOC (NULL
);
1239 remaining_stmts
= BITMAP_ALLOC (NULL
);
1240 upstream_mem_writes
= BITMAP_ALLOC (NULL
);
1242 for (i
= 0; i
< rdg
->n_vertices
; i
++)
1244 bitmap_set_bit (remaining_stmts
, i
);
1246 /* Save in OTHER_STORES all the memory writes that are not in
1247 STARTING_VERTICES. */
1248 if (RDG_MEM_WRITE_STMT (rdg
, i
))
1254 FOR_EACH_VEC_ELT (int, starting_vertices
, j
, v
)
1262 VEC_safe_push (int, heap
, other_stores
, i
);
1266 mark_nodes_having_upstream_mem_writes (rdg
);
1267 rdg_build_components (rdg
, starting_vertices
, &components
);
1268 rdg_build_partitions (rdg
, components
, &other_stores
, &partitions
,
1270 BITMAP_FREE (processed
);
1272 any_builtin
= false;
1273 FOR_EACH_VEC_ELT (partition_t
, partitions
, i
, partition
)
1275 classify_partition (loop
, rdg
, partition
);
1276 any_builtin
|= partition_builtin_p (partition
);
1279 /* If we are only distributing patterns fuse all partitions that
1280 were not properly classified as builtins. Else fuse partitions
1281 with similar memory accesses. */
1282 if (!flag_tree_loop_distribution
)
1285 /* If we did not detect any builtin simply bail out. */
1291 /* Only fuse adjacent non-builtin partitions, see PR53616.
1292 ??? Use dependence information to improve partition ordering. */
1296 for (; VEC_iterate (partition_t
, partitions
, i
, into
); ++i
)
1297 if (!partition_builtin_p (into
))
1299 for (++i
; VEC_iterate (partition_t
, partitions
, i
, partition
); ++i
)
1300 if (!partition_builtin_p (partition
))
1302 bitmap_ior_into (into
->stmts
, partition
->stmts
);
1303 VEC_ordered_remove (partition_t
, partitions
, i
);
1309 while ((unsigned) i
< VEC_length (partition_t
, partitions
));
1315 for (i
= 0; VEC_iterate (partition_t
, partitions
, i
, into
); ++i
)
1317 if (partition_builtin_p (into
))
1320 VEC_iterate (partition_t
, partitions
, j
, partition
); ++j
)
1322 if (!partition_builtin_p (partition
)
1323 /* ??? The following is horribly inefficient,
1324 we are re-computing and analyzing data-references
1325 of the stmts in the partitions all the time. */
1326 && similar_memory_accesses (rdg
, into
, partition
))
1328 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1330 fprintf (dump_file
, "fusing partitions\n");
1331 dump_bitmap (dump_file
, into
->stmts
);
1332 dump_bitmap (dump_file
, partition
->stmts
);
1333 fprintf (dump_file
, "because they have similar "
1334 "memory accesses\n");
1336 bitmap_ior_into (into
->stmts
, partition
->stmts
);
1337 VEC_ordered_remove (partition_t
, partitions
, j
);
1344 nbp
= VEC_length (partition_t
, partitions
);
1347 && !partition_builtin_p (VEC_index (partition_t
, partitions
, 0)))
1349 && partition_contains_all_rw (rdg
, partitions
)))
1355 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1356 dump_rdg_partitions (dump_file
, partitions
);
1358 FOR_EACH_VEC_ELT (partition_t
, partitions
, i
, partition
)
1359 generate_code_for_partition (loop
, partition
, i
< nbp
- 1);
1363 BITMAP_FREE (remaining_stmts
);
1364 BITMAP_FREE (upstream_mem_writes
);
1366 FOR_EACH_VEC_ELT (partition_t
, partitions
, i
, partition
)
1367 partition_free (partition
);
1369 VEC_free (int, heap
, other_stores
);
1370 VEC_free (partition_t
, heap
, partitions
);
1371 free_rdg_components (components
);
1375 /* Distributes the code from LOOP in such a way that producer
1376 statements are placed before consumer statements. When STMTS is
1377 NULL, performs the maximal distribution, if STMTS is not NULL,
1378 tries to separate only these statements from the LOOP's body.
1379 Returns the number of distributed loops. */
1382 distribute_loop (struct loop
*loop
, VEC (gimple
, heap
) *stmts
)
1388 VEC (int, heap
) *vertices
;
1389 VEC (ddr_p
, heap
) *dependence_relations
;
1390 VEC (data_reference_p
, heap
) *datarefs
;
1391 VEC (loop_p
, heap
) *loop_nest
;
1393 datarefs
= VEC_alloc (data_reference_p
, heap
, 10);
1394 dependence_relations
= VEC_alloc (ddr_p
, heap
, 100);
1395 loop_nest
= VEC_alloc (loop_p
, heap
, 3);
1396 rdg
= build_rdg (loop
, &loop_nest
, &dependence_relations
, &datarefs
);
1400 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1402 "FIXME: Loop %d not distributed: failed to build the RDG.\n",
1405 free_dependence_relations (dependence_relations
);
1406 free_data_refs (datarefs
);
1407 VEC_free (loop_p
, heap
, loop_nest
);
1411 vertices
= VEC_alloc (int, heap
, 3);
1413 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1414 dump_rdg (dump_file
, rdg
);
1416 FOR_EACH_VEC_ELT (gimple
, stmts
, i
, s
)
1418 int v
= rdg_vertex_for_stmt (rdg
, s
);
1422 VEC_safe_push (int, heap
, vertices
, v
);
1424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1426 "ldist asked to generate code for vertex %d\n", v
);
1430 res
= ldist_gen (loop
, rdg
, vertices
);
1431 VEC_free (int, heap
, vertices
);
1433 free_dependence_relations (dependence_relations
);
1434 free_data_refs (datarefs
);
1435 VEC_free (loop_p
, heap
, loop_nest
);
1439 /* Distribute all loops in the current function. */
1442 tree_loop_distribution (void)
1446 bool changed
= false;
1451 gimple_stmt_iterator gsi
;
1452 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1453 gimple_set_uid (gsi_stmt (gsi
), -1);
1454 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1455 gimple_set_uid (gsi_stmt (gsi
), -1);
1458 /* We can at the moment only distribute non-nested loops, thus restrict
1459 walking to innermost loops. */
1460 FOR_EACH_LOOP (li
, loop
, LI_ONLY_INNERMOST
)
1462 VEC (gimple
, heap
) *work_list
= NULL
;
1464 int num
= loop
->num
;
1465 int nb_generated_loops
= 0;
1468 /* If the loop doesn't have a single exit we will fail anyway,
1469 so do that early. */
1470 if (!single_exit (loop
))
1473 /* Only distribute loops with a header and latch for now. */
1474 if (loop
->num_nodes
> 2)
1477 /* Initialize the worklist with stmts we seed the partitions with. */
1478 bbs
= get_loop_body_in_dom_order (loop
);
1479 for (i
= 0; i
< loop
->num_nodes
; ++i
)
1481 gimple_stmt_iterator gsi
;
1482 for (gsi
= gsi_start_bb (bbs
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
1484 gimple stmt
= gsi_stmt (gsi
);
1485 /* Only distribute stores for now.
1486 ??? We should also try to distribute scalar reductions,
1487 thus SSA defs that have scalar uses outside of the loop. */
1488 if (!gimple_assign_single_p (stmt
)
1489 || is_gimple_reg (gimple_assign_lhs (stmt
)))
1492 VEC_safe_push (gimple
, heap
, work_list
, stmt
);
1497 if (VEC_length (gimple
, work_list
) > 0)
1498 nb_generated_loops
= distribute_loop (loop
, work_list
);
1500 if (nb_generated_loops
> 0)
1503 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1505 if (nb_generated_loops
> 1)
1506 fprintf (dump_file
, "Loop %d distributed: split to %d loops.\n",
1507 num
, nb_generated_loops
);
1509 fprintf (dump_file
, "Loop %d is the same.\n", num
);
1512 VEC_free (gimple
, heap
, work_list
);
1517 mark_virtual_operands_for_renaming (cfun
);
1518 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
1521 #ifdef ENABLE_CHECKING
1522 verify_loop_structure ();
1529 gate_tree_loop_distribution (void)
1531 return flag_tree_loop_distribution
1532 || flag_tree_loop_distribute_patterns
;
1535 struct gimple_opt_pass pass_loop_distribution
=
1540 gate_tree_loop_distribution
, /* gate */
1541 tree_loop_distribution
, /* execute */
1544 0, /* static_pass_number */
1545 TV_TREE_LOOP_DISTRIBUTION
, /* tv_id */
1546 PROP_cfg
| PROP_ssa
, /* properties_required */
1547 0, /* properties_provided */
1548 0, /* properties_destroyed */
1549 0, /* todo_flags_start */
1551 | TODO_verify_ssa
/* todo_flags_finish */