1 /* Conversion of SESE regions to Polyhedra.
2 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
24 #include "tree-flow.h"
25 #include "tree-dump.h"
27 #include "tree-chrec.h"
28 #include "tree-data-ref.h"
29 #include "tree-scalar-evolution.h"
35 #include "graphite-ppl.h"
36 #include "graphite-poly.h"
37 #include "graphite-sese-to-poly.h"
39 /* Returns the index of the PHI argument defined in the outermost
43 phi_arg_in_outermost_loop (gimple phi
)
45 loop_p loop
= gimple_bb (phi
)->loop_father
;
48 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
49 if (!flow_bb_inside_loop_p (loop
, gimple_phi_arg_edge (phi
, i
)->src
))
51 loop
= gimple_phi_arg_edge (phi
, i
)->src
->loop_father
;
58 /* Removes a simple copy phi node "RES = phi (INIT, RES)" at position
59 PSI by inserting on the loop ENTRY edge assignment "RES = INIT". */
62 remove_simple_copy_phi (gimple_stmt_iterator
*psi
)
64 gimple phi
= gsi_stmt (*psi
);
65 tree res
= gimple_phi_result (phi
);
66 size_t entry
= phi_arg_in_outermost_loop (phi
);
67 tree init
= gimple_phi_arg_def (phi
, entry
);
68 gimple stmt
= gimple_build_assign (res
, init
);
69 edge e
= gimple_phi_arg_edge (phi
, entry
);
71 remove_phi_node (psi
, false);
72 gsi_insert_on_edge_immediate (e
, stmt
);
73 SSA_NAME_DEF_STMT (res
) = stmt
;
76 /* Removes an invariant phi node at position PSI by inserting on the
77 loop ENTRY edge the assignment RES = INIT. */
80 remove_invariant_phi (sese region
, gimple_stmt_iterator
*psi
)
82 gimple phi
= gsi_stmt (*psi
);
83 loop_p loop
= loop_containing_stmt (phi
);
84 tree res
= gimple_phi_result (phi
);
85 tree scev
= scalar_evolution_in_region (region
, loop
, res
);
86 size_t entry
= phi_arg_in_outermost_loop (phi
);
87 edge e
= gimple_phi_arg_edge (phi
, entry
);
91 gimple_stmt_iterator gsi
;
93 if (tree_contains_chrecs (scev
, NULL
))
94 scev
= gimple_phi_arg_def (phi
, entry
);
96 var
= force_gimple_operand (scev
, &stmts
, true, NULL_TREE
);
97 stmt
= gimple_build_assign (res
, var
);
98 remove_phi_node (psi
, false);
101 stmts
= gimple_seq_alloc ();
103 gsi
= gsi_last (stmts
);
104 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
105 gsi_insert_seq_on_edge (e
, stmts
);
106 gsi_commit_edge_inserts ();
107 SSA_NAME_DEF_STMT (res
) = stmt
;
110 /* Returns true when the phi node at PSI is of the form "a = phi (a, x)". */
113 simple_copy_phi_p (gimple phi
)
117 if (gimple_phi_num_args (phi
) != 2)
120 res
= gimple_phi_result (phi
);
121 return (res
== gimple_phi_arg_def (phi
, 0)
122 || res
== gimple_phi_arg_def (phi
, 1));
125 /* Returns true when the phi node at position PSI is a reduction phi
126 node in REGION. Otherwise moves the pointer PSI to the next phi to
130 reduction_phi_p (sese region
, gimple_stmt_iterator
*psi
)
133 gimple phi
= gsi_stmt (*psi
);
134 tree res
= gimple_phi_result (phi
);
136 loop
= loop_containing_stmt (phi
);
138 if (simple_copy_phi_p (phi
))
140 /* PRE introduces phi nodes like these, for an example,
141 see id-5.f in the fortran graphite testsuite:
143 # prephitmp.85_265 = PHI <prephitmp.85_258(33), prephitmp.85_265(18)>
145 remove_simple_copy_phi (psi
);
149 if (scev_analyzable_p (res
, region
))
151 tree scev
= scalar_evolution_in_region (region
, loop
, res
);
153 if (evolution_function_is_invariant_p (scev
, loop
->num
))
154 remove_invariant_phi (region
, psi
);
161 /* All the other cases are considered reductions. */
165 /* Store the GRAPHITE representation of BB. */
168 new_gimple_bb (basic_block bb
, VEC (data_reference_p
, heap
) *drs
)
170 struct gimple_bb
*gbb
;
172 gbb
= XNEW (struct gimple_bb
);
175 GBB_DATA_REFS (gbb
) = drs
;
176 GBB_CONDITIONS (gbb
) = NULL
;
177 GBB_CONDITION_CASES (gbb
) = NULL
;
183 free_data_refs_aux (VEC (data_reference_p
, heap
) *datarefs
)
186 struct data_reference
*dr
;
188 FOR_EACH_VEC_ELT (data_reference_p
, datarefs
, i
, dr
)
191 base_alias_pair
*bap
= (base_alias_pair
*)(dr
->aux
);
194 free (bap
->alias_set
);
203 free_gimple_bb (struct gimple_bb
*gbb
)
205 free_data_refs_aux (GBB_DATA_REFS (gbb
));
206 free_data_refs (GBB_DATA_REFS (gbb
));
208 VEC_free (gimple
, heap
, GBB_CONDITIONS (gbb
));
209 VEC_free (gimple
, heap
, GBB_CONDITION_CASES (gbb
));
210 GBB_BB (gbb
)->aux
= 0;
214 /* Deletes all gimple bbs in SCOP. */
217 remove_gbbs_in_scop (scop_p scop
)
222 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
223 free_gimple_bb (PBB_BLACK_BOX (pbb
));
226 /* Deletes all scops in SCOPS. */
229 free_scops (VEC (scop_p
, heap
) *scops
)
234 FOR_EACH_VEC_ELT (scop_p
, scops
, i
, scop
)
236 remove_gbbs_in_scop (scop
);
237 free_sese (SCOP_REGION (scop
));
241 VEC_free (scop_p
, heap
, scops
);
244 /* Same as outermost_loop_in_sese, returns the outermost loop
245 containing BB in REGION, but makes sure that the returned loop
246 belongs to the REGION, and so this returns the first loop in the
247 REGION when the loop containing BB does not belong to REGION. */
250 outermost_loop_in_sese_1 (sese region
, basic_block bb
)
252 loop_p nest
= outermost_loop_in_sese (region
, bb
);
254 if (loop_in_sese_p (nest
, region
))
257 /* When the basic block BB does not belong to a loop in the region,
258 return the first loop in the region. */
261 if (loop_in_sese_p (nest
, region
))
270 /* Generates a polyhedral black box only if the bb contains interesting
274 try_generate_gimple_bb (scop_p scop
, basic_block bb
)
276 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 5);
277 sese region
= SCOP_REGION (scop
);
278 loop_p nest
= outermost_loop_in_sese_1 (region
, bb
);
279 gimple_stmt_iterator gsi
;
281 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
283 gimple stmt
= gsi_stmt (gsi
);
286 if (is_gimple_debug (stmt
))
289 loop
= loop_containing_stmt (stmt
);
290 if (!loop_in_sese_p (loop
, region
))
293 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
296 return new_gimple_bb (bb
, drs
);
299 /* Returns true if all predecessors of BB, that are not dominated by BB, are
300 marked in MAP. The predecessors dominated by BB are loop latches and will
301 be handled after BB. */
304 all_non_dominated_preds_marked_p (basic_block bb
, sbitmap map
)
309 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
310 if (!TEST_BIT (map
, e
->src
->index
)
311 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
317 /* Compare the depth of two basic_block's P1 and P2. */
320 compare_bb_depths (const void *p1
, const void *p2
)
322 const_basic_block
const bb1
= *(const_basic_block
const*)p1
;
323 const_basic_block
const bb2
= *(const_basic_block
const*)p2
;
324 int d1
= loop_depth (bb1
->loop_father
);
325 int d2
= loop_depth (bb2
->loop_father
);
336 /* Sort the basic blocks from DOM such that the first are the ones at
337 a deepest loop level. */
340 graphite_sort_dominated_info (VEC (basic_block
, heap
) *dom
)
342 VEC_qsort (basic_block
, dom
, compare_bb_depths
);
345 /* Recursive helper function for build_scops_bbs. */
348 build_scop_bbs_1 (scop_p scop
, sbitmap visited
, basic_block bb
)
350 sese region
= SCOP_REGION (scop
);
351 VEC (basic_block
, heap
) *dom
;
354 if (TEST_BIT (visited
, bb
->index
)
355 || !bb_in_sese_p (bb
, region
))
358 pbb
= new_poly_bb (scop
, try_generate_gimple_bb (scop
, bb
));
359 VEC_safe_push (poly_bb_p
, heap
, SCOP_BBS (scop
), pbb
);
360 SET_BIT (visited
, bb
->index
);
362 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
367 graphite_sort_dominated_info (dom
);
369 while (!VEC_empty (basic_block
, dom
))
374 FOR_EACH_VEC_ELT (basic_block
, dom
, i
, dom_bb
)
375 if (all_non_dominated_preds_marked_p (dom_bb
, visited
))
377 build_scop_bbs_1 (scop
, visited
, dom_bb
);
378 VEC_unordered_remove (basic_block
, dom
, i
);
383 VEC_free (basic_block
, heap
, dom
);
386 /* Gather the basic blocks belonging to the SCOP. */
389 build_scop_bbs (scop_p scop
)
391 sbitmap visited
= sbitmap_alloc (last_basic_block
);
392 sese region
= SCOP_REGION (scop
);
394 sbitmap_zero (visited
);
395 build_scop_bbs_1 (scop
, visited
, SESE_ENTRY_BB (region
));
396 sbitmap_free (visited
);
399 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
400 We generate SCATTERING_DIMENSIONS scattering dimensions.
402 CLooG 0.15.0 and previous versions require, that all
403 scattering functions of one CloogProgram have the same number of
404 scattering dimensions, therefore we allow to specify it. This
405 should be removed in future versions of CLooG.
407 The scattering polyhedron consists of these dimensions: scattering,
408 loop_iterators, parameters.
412 | scattering_dimensions = 5
413 | used_scattering_dimensions = 3
421 | Scattering polyhedron:
423 | scattering: {s1, s2, s3, s4, s5}
424 | loop_iterators: {i}
425 | parameters: {p1, p2}
427 | s1 s2 s3 s4 s5 i p1 p2 1
428 | 1 0 0 0 0 0 0 0 -4 = 0
429 | 0 1 0 0 0 -1 0 0 0 = 0
430 | 0 0 1 0 0 0 0 0 -5 = 0 */
433 build_pbb_scattering_polyhedrons (ppl_Linear_Expression_t static_schedule
,
434 poly_bb_p pbb
, int scattering_dimensions
)
437 scop_p scop
= PBB_SCOP (pbb
);
438 int nb_iterators
= pbb_dim_iter_domain (pbb
);
439 int used_scattering_dimensions
= nb_iterators
* 2 + 1;
440 int nb_params
= scop_nb_params (scop
);
442 ppl_dimension_type dim
= scattering_dimensions
+ nb_iterators
+ nb_params
;
445 gcc_assert (scattering_dimensions
>= used_scattering_dimensions
);
448 ppl_new_Coefficient (&c
);
449 PBB_TRANSFORMED (pbb
) = poly_scattering_new ();
450 ppl_new_C_Polyhedron_from_space_dimension
451 (&PBB_TRANSFORMED_SCATTERING (pbb
), dim
, 0);
453 PBB_NB_SCATTERING_TRANSFORM (pbb
) = scattering_dimensions
;
455 for (i
= 0; i
< scattering_dimensions
; i
++)
457 ppl_Constraint_t cstr
;
458 ppl_Linear_Expression_t expr
;
460 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
462 ppl_assign_Coefficient_from_mpz_t (c
, v
);
463 ppl_Linear_Expression_add_to_coefficient (expr
, i
, c
);
465 /* Textual order inside this loop. */
468 ppl_Linear_Expression_coefficient (static_schedule
, i
/ 2, c
);
469 ppl_Coefficient_to_mpz_t (c
, v
);
471 ppl_assign_Coefficient_from_mpz_t (c
, v
);
472 ppl_Linear_Expression_add_to_inhomogeneous (expr
, c
);
475 /* Iterations of this loop. */
476 else /* if ((i % 2) == 1) */
478 int loop
= (i
- 1) / 2;
481 ppl_assign_Coefficient_from_mpz_t (c
, v
);
482 ppl_Linear_Expression_add_to_coefficient
483 (expr
, scattering_dimensions
+ loop
, c
);
486 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
487 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb
), cstr
);
488 ppl_delete_Linear_Expression (expr
);
489 ppl_delete_Constraint (cstr
);
493 ppl_delete_Coefficient (c
);
495 PBB_ORIGINAL (pbb
) = poly_scattering_copy (PBB_TRANSFORMED (pbb
));
498 /* Build for BB the static schedule.
500 The static schedule is a Dewey numbering of the abstract syntax
501 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
503 The following example informally defines the static schedule:
522 Static schedules for A to F:
535 build_scop_scattering (scop_p scop
)
539 gimple_bb_p previous_gbb
= NULL
;
540 ppl_Linear_Expression_t static_schedule
;
545 ppl_new_Coefficient (&c
);
546 ppl_new_Linear_Expression (&static_schedule
);
548 /* We have to start schedules at 0 on the first component and
549 because we cannot compare_prefix_loops against a previous loop,
550 prefix will be equal to zero, and that index will be
551 incremented before copying. */
553 ppl_assign_Coefficient_from_mpz_t (c
, v
);
554 ppl_Linear_Expression_add_to_coefficient (static_schedule
, 0, c
);
556 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
558 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
559 ppl_Linear_Expression_t common
;
561 int nb_scat_dims
= pbb_dim_iter_domain (pbb
) * 2 + 1;
564 prefix
= nb_common_loops (SCOP_REGION (scop
), previous_gbb
, gbb
);
569 ppl_new_Linear_Expression_with_dimension (&common
, prefix
+ 1);
570 ppl_assign_Linear_Expression_from_Linear_Expression (common
,
574 ppl_assign_Coefficient_from_mpz_t (c
, v
);
575 ppl_Linear_Expression_add_to_coefficient (common
, prefix
, c
);
576 ppl_assign_Linear_Expression_from_Linear_Expression (static_schedule
,
579 build_pbb_scattering_polyhedrons (common
, pbb
, nb_scat_dims
);
581 ppl_delete_Linear_Expression (common
);
585 ppl_delete_Coefficient (c
);
586 ppl_delete_Linear_Expression (static_schedule
);
589 /* Add the value K to the dimension D of the linear expression EXPR. */
592 add_value_to_dim (ppl_dimension_type d
, ppl_Linear_Expression_t expr
,
596 ppl_Coefficient_t coef
;
598 ppl_new_Coefficient (&coef
);
599 ppl_Linear_Expression_coefficient (expr
, d
, coef
);
601 ppl_Coefficient_to_mpz_t (coef
, val
);
603 mpz_add (val
, val
, k
);
605 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
606 ppl_Linear_Expression_add_to_coefficient (expr
, d
, coef
);
608 ppl_delete_Coefficient (coef
);
611 /* In the context of scop S, scan E, the right hand side of a scalar
612 evolution function in loop VAR, and translate it to a linear
616 scan_tree_for_params_right_scev (sese s
, tree e
, int var
,
617 ppl_Linear_Expression_t expr
)
621 loop_p loop
= get_loop (var
);
622 ppl_dimension_type l
= sese_loop_depth (s
, loop
) - 1;
625 /* Scalar evolutions should happen in the sese region. */
626 gcc_assert (sese_loop_depth (s
, loop
) > 0);
628 /* We can not deal with parametric strides like:
634 gcc_assert (TREE_CODE (e
) == INTEGER_CST
);
637 tree_int_to_gmp (e
, val
);
638 add_value_to_dim (l
, expr
, val
);
643 /* Scan the integer constant CST, and add it to the inhomogeneous part of the
644 linear expression EXPR. K is the multiplier of the constant. */
647 scan_tree_for_params_int (tree cst
, ppl_Linear_Expression_t expr
, mpz_t k
)
650 ppl_Coefficient_t coef
;
651 tree type
= TREE_TYPE (cst
);
655 /* Necessary to not get "-1 = 2^n - 1". */
656 mpz_set_double_int (val
, double_int_sext (tree_to_double_int (cst
),
657 TYPE_PRECISION (type
)), false);
659 mpz_mul (val
, val
, k
);
660 ppl_new_Coefficient (&coef
);
661 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
662 ppl_Linear_Expression_add_to_inhomogeneous (expr
, coef
);
664 ppl_delete_Coefficient (coef
);
667 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
668 Otherwise returns -1. */
671 parameter_index_in_region_1 (tree name
, sese region
)
676 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
678 FOR_EACH_VEC_ELT (tree
, SESE_PARAMS (region
), i
, p
)
685 /* When the parameter NAME is in REGION, returns its index in
686 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
687 and returns the index of NAME. */
690 parameter_index_in_region (tree name
, sese region
)
694 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
696 i
= parameter_index_in_region_1 (name
, region
);
700 gcc_assert (SESE_ADD_PARAMS (region
));
702 i
= VEC_length (tree
, SESE_PARAMS (region
));
703 VEC_safe_push (tree
, heap
, SESE_PARAMS (region
), name
);
707 /* In the context of sese S, scan the expression E and translate it to
708 a linear expression C. When parsing a symbolic multiplication, K
709 represents the constant multiplier of an expression containing
713 scan_tree_for_params (sese s
, tree e
, ppl_Linear_Expression_t c
,
716 if (e
== chrec_dont_know
)
719 switch (TREE_CODE (e
))
721 case POLYNOMIAL_CHREC
:
722 scan_tree_for_params_right_scev (s
, CHREC_RIGHT (e
),
723 CHREC_VARIABLE (e
), c
);
724 scan_tree_for_params (s
, CHREC_LEFT (e
), c
, k
);
728 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
733 gcc_assert (host_integerp (TREE_OPERAND (e
, 1), 0));
735 tree_int_to_gmp (TREE_OPERAND (e
, 1), val
);
736 mpz_mul (val
, val
, k
);
737 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, val
);
741 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
748 gcc_assert (host_integerp (TREE_OPERAND (e
, 0), 0));
750 tree_int_to_gmp (TREE_OPERAND (e
, 0), val
);
751 mpz_mul (val
, val
, k
);
752 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, val
);
756 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
761 case POINTER_PLUS_EXPR
:
762 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
763 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
768 ppl_Linear_Expression_t tmp_expr
= NULL
;
772 ppl_dimension_type dim
;
773 ppl_Linear_Expression_space_dimension (c
, &dim
);
774 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
777 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
778 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), tmp_expr
, k
);
782 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
784 ppl_delete_Linear_Expression (tmp_expr
);
792 ppl_Linear_Expression_t tmp_expr
= NULL
;
796 ppl_dimension_type dim
;
797 ppl_Linear_Expression_space_dimension (c
, &dim
);
798 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
801 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
805 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
807 ppl_delete_Linear_Expression (tmp_expr
);
815 ppl_Linear_Expression_t tmp_expr
= NULL
;
819 ppl_dimension_type dim
;
820 ppl_Linear_Expression_space_dimension (c
, &dim
);
821 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
824 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
828 ppl_Coefficient_t coef
;
831 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
833 ppl_delete_Linear_Expression (tmp_expr
);
834 mpz_init (minus_one
);
835 mpz_set_si (minus_one
, -1);
836 ppl_new_Coefficient_from_mpz_t (&coef
, minus_one
);
837 ppl_Linear_Expression_add_to_inhomogeneous (c
, coef
);
838 mpz_clear (minus_one
);
839 ppl_delete_Coefficient (coef
);
847 ppl_dimension_type p
= parameter_index_in_region (e
, s
);
851 ppl_dimension_type dim
;
852 ppl_Linear_Expression_space_dimension (c
, &dim
);
853 p
+= dim
- sese_nb_params (s
);
854 add_value_to_dim (p
, c
, k
);
861 scan_tree_for_params_int (e
, c
, k
);
865 case NON_LVALUE_EXPR
:
866 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
878 /* Find parameters with respect to REGION in BB. We are looking in memory
879 access functions, conditions and loop bounds. */
882 find_params_in_bb (sese region
, gimple_bb_p gbb
)
888 loop_p loop
= GBB_BB (gbb
)->loop_father
;
894 /* Find parameters in the access functions of data references. */
895 FOR_EACH_VEC_ELT (data_reference_p
, GBB_DATA_REFS (gbb
), i
, dr
)
896 for (j
= 0; j
< DR_NUM_DIMENSIONS (dr
); j
++)
897 scan_tree_for_params (region
, DR_ACCESS_FN (dr
, j
), NULL
, one
);
899 /* Find parameters in conditional statements. */
900 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
902 tree lhs
= scalar_evolution_in_region (region
, loop
,
903 gimple_cond_lhs (stmt
));
904 tree rhs
= scalar_evolution_in_region (region
, loop
,
905 gimple_cond_rhs (stmt
));
907 scan_tree_for_params (region
, lhs
, NULL
, one
);
908 scan_tree_for_params (region
, rhs
, NULL
, one
);
914 /* Record the parameters used in the SCOP. A variable is a parameter
915 in a scop if it does not vary during the execution of that scop. */
918 find_scop_parameters (scop_p scop
)
922 sese region
= SCOP_REGION (scop
);
929 /* Find the parameters used in the loop bounds. */
930 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
932 tree nb_iters
= number_of_latch_executions (loop
);
934 if (!chrec_contains_symbols (nb_iters
))
937 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
938 scan_tree_for_params (region
, nb_iters
, NULL
, one
);
943 /* Find the parameters used in data accesses. */
944 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
945 find_params_in_bb (region
, PBB_BLACK_BOX (pbb
));
947 scop_set_nb_params (scop
, sese_nb_params (region
));
948 SESE_ADD_PARAMS (region
) = false;
950 ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension
951 (&SCOP_CONTEXT (scop
), scop_nb_params (scop
), 0);
954 /* Insert in the SCOP context constraints from the estimation of the
955 number of iterations. UB_EXPR is a linear expression describing
956 the number of iterations in a loop. This expression is bounded by
957 the estimation NIT. */
960 add_upper_bounds_from_estimated_nit (scop_p scop
, double_int nit
,
961 ppl_dimension_type dim
,
962 ppl_Linear_Expression_t ub_expr
)
965 ppl_Linear_Expression_t nb_iters_le
;
966 ppl_Polyhedron_t pol
;
967 ppl_Coefficient_t coef
;
970 ppl_new_C_Polyhedron_from_space_dimension (&pol
, dim
, 0);
971 ppl_new_Linear_Expression_from_Linear_Expression (&nb_iters_le
,
974 /* Construct the negated number of last iteration in VAL. */
976 mpz_set_double_int (val
, nit
, false);
977 mpz_sub_ui (val
, val
, 1);
980 /* NB_ITERS_LE holds the number of last iteration in
981 parametrical form. Subtract estimated number of last
982 iteration and assert that result is not positive. */
983 ppl_new_Coefficient_from_mpz_t (&coef
, val
);
984 ppl_Linear_Expression_add_to_inhomogeneous (nb_iters_le
, coef
);
985 ppl_delete_Coefficient (coef
);
986 ppl_new_Constraint (&ub
, nb_iters_le
,
987 PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
988 ppl_Polyhedron_add_constraint (pol
, ub
);
990 /* Remove all but last GDIM dimensions from POL to obtain
991 only the constraints on the parameters. */
993 graphite_dim_t gdim
= scop_nb_params (scop
);
994 ppl_dimension_type
*dims
= XNEWVEC (ppl_dimension_type
, dim
- gdim
);
997 for (i
= 0; i
< dim
- gdim
; i
++)
1000 ppl_Polyhedron_remove_space_dimensions (pol
, dims
, dim
- gdim
);
1004 /* Add the constraints on the parameters to the SCoP context. */
1006 ppl_Pointset_Powerset_C_Polyhedron_t constraints_ps
;
1008 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1009 (&constraints_ps
, pol
);
1010 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1011 (SCOP_CONTEXT (scop
), constraints_ps
);
1012 ppl_delete_Pointset_Powerset_C_Polyhedron (constraints_ps
);
1015 ppl_delete_Polyhedron (pol
);
1016 ppl_delete_Linear_Expression (nb_iters_le
);
1017 ppl_delete_Constraint (ub
);
1021 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
1022 the constraints for the surrounding loops. */
1025 build_loop_iteration_domains (scop_p scop
, struct loop
*loop
,
1026 ppl_Polyhedron_t outer_ph
, int nb
,
1027 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
)
1030 ppl_Polyhedron_t ph
;
1031 tree nb_iters
= number_of_latch_executions (loop
);
1032 ppl_dimension_type dim
= nb
+ 1 + scop_nb_params (scop
);
1033 sese region
= SCOP_REGION (scop
);
1036 ppl_const_Constraint_System_t pcs
;
1037 ppl_dimension_type
*map
1038 = (ppl_dimension_type
*) XNEWVEC (ppl_dimension_type
, dim
);
1040 ppl_new_C_Polyhedron_from_space_dimension (&ph
, dim
, 0);
1041 ppl_Polyhedron_get_constraints (outer_ph
, &pcs
);
1042 ppl_Polyhedron_add_constraints (ph
, pcs
);
1044 for (i
= 0; i
< (int) nb
; i
++)
1046 for (i
= (int) nb
; i
< (int) dim
- 1; i
++)
1050 ppl_Polyhedron_map_space_dimensions (ph
, map
, dim
);
1056 ppl_Constraint_t lb
;
1057 ppl_Linear_Expression_t lb_expr
;
1059 ppl_new_Linear_Expression_with_dimension (&lb_expr
, dim
);
1060 ppl_set_coef (lb_expr
, nb
, 1);
1061 ppl_new_Constraint (&lb
, lb_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1062 ppl_delete_Linear_Expression (lb_expr
);
1063 ppl_Polyhedron_add_constraint (ph
, lb
);
1064 ppl_delete_Constraint (lb
);
1067 if (TREE_CODE (nb_iters
) == INTEGER_CST
)
1069 ppl_Constraint_t ub
;
1070 ppl_Linear_Expression_t ub_expr
;
1072 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1074 /* loop_i <= cst_nb_iters */
1075 ppl_set_coef (ub_expr
, nb
, -1);
1076 ppl_set_inhomogeneous_tree (ub_expr
, nb_iters
);
1077 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1078 ppl_Polyhedron_add_constraint (ph
, ub
);
1079 ppl_delete_Linear_Expression (ub_expr
);
1080 ppl_delete_Constraint (ub
);
1082 else if (!chrec_contains_undetermined (nb_iters
))
1085 ppl_Constraint_t ub
;
1086 ppl_Linear_Expression_t ub_expr
;
1090 mpz_set_si (one
, 1);
1091 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1092 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
1093 scan_tree_for_params (SCOP_REGION (scop
), nb_iters
, ub_expr
, one
);
1096 if (estimated_loop_iterations (loop
, true, &nit
))
1097 add_upper_bounds_from_estimated_nit (scop
, nit
, dim
, ub_expr
);
1099 /* loop_i <= expr_nb_iters */
1100 ppl_set_coef (ub_expr
, nb
, -1);
1101 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1102 ppl_Polyhedron_add_constraint (ph
, ub
);
1103 ppl_delete_Linear_Expression (ub_expr
);
1104 ppl_delete_Constraint (ub
);
1109 if (loop
->inner
&& loop_in_sese_p (loop
->inner
, region
))
1110 build_loop_iteration_domains (scop
, loop
->inner
, ph
, nb
+ 1, domains
);
1114 && loop_in_sese_p (loop
->next
, region
))
1115 build_loop_iteration_domains (scop
, loop
->next
, outer_ph
, nb
, domains
);
1117 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1118 (&domains
[loop
->num
], ph
);
1120 ppl_delete_Polyhedron (ph
);
1123 /* Returns a linear expression for tree T evaluated in PBB. */
1125 static ppl_Linear_Expression_t
1126 create_linear_expr_from_tree (poly_bb_p pbb
, tree t
)
1129 ppl_Linear_Expression_t res
;
1130 ppl_dimension_type dim
;
1131 sese region
= SCOP_REGION (PBB_SCOP (pbb
));
1132 loop_p loop
= pbb_loop (pbb
);
1134 dim
= pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
1135 ppl_new_Linear_Expression_with_dimension (&res
, dim
);
1137 t
= scalar_evolution_in_region (region
, loop
, t
);
1138 gcc_assert (!automatically_generated_chrec_p (t
));
1141 mpz_set_si (one
, 1);
1142 scan_tree_for_params (region
, t
, res
, one
);
1148 /* Returns the ppl constraint type from the gimple tree code CODE. */
1150 static enum ppl_enum_Constraint_Type
1151 ppl_constraint_type_from_tree_code (enum tree_code code
)
1155 /* We do not support LT and GT to be able to work with C_Polyhedron.
1156 As we work on integer polyhedron "a < b" can be expressed by
1163 return PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
;
1166 return PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
;
1169 return PPL_CONSTRAINT_TYPE_EQUAL
;
1176 /* Add conditional statement STMT to PS. It is evaluated in PBB and
1177 CODE is used as the comparison operator. This allows us to invert the
1178 condition or to handle inequalities. */
1181 add_condition_to_domain (ppl_Pointset_Powerset_C_Polyhedron_t ps
, gimple stmt
,
1182 poly_bb_p pbb
, enum tree_code code
)
1185 ppl_Coefficient_t c
;
1186 ppl_Linear_Expression_t left
, right
;
1187 ppl_Constraint_t cstr
;
1188 enum ppl_enum_Constraint_Type type
;
1190 left
= create_linear_expr_from_tree (pbb
, gimple_cond_lhs (stmt
));
1191 right
= create_linear_expr_from_tree (pbb
, gimple_cond_rhs (stmt
));
1193 /* If we have < or > expressions convert them to <= or >= by adding 1 to
1194 the left or the right side of the expression. */
1195 if (code
== LT_EXPR
)
1199 ppl_new_Coefficient (&c
);
1200 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1201 ppl_Linear_Expression_add_to_inhomogeneous (left
, c
);
1202 ppl_delete_Coefficient (c
);
1207 else if (code
== GT_EXPR
)
1211 ppl_new_Coefficient (&c
);
1212 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1213 ppl_Linear_Expression_add_to_inhomogeneous (right
, c
);
1214 ppl_delete_Coefficient (c
);
1220 type
= ppl_constraint_type_from_tree_code (code
);
1222 ppl_subtract_Linear_Expression_from_Linear_Expression (left
, right
);
1224 ppl_new_Constraint (&cstr
, left
, type
);
1225 ppl_Pointset_Powerset_C_Polyhedron_add_constraint (ps
, cstr
);
1227 ppl_delete_Constraint (cstr
);
1228 ppl_delete_Linear_Expression (left
);
1229 ppl_delete_Linear_Expression (right
);
1232 /* Add conditional statement STMT to pbb. CODE is used as the comparision
1233 operator. This allows us to invert the condition or to handle
1237 add_condition_to_pbb (poly_bb_p pbb
, gimple stmt
, enum tree_code code
)
1239 if (code
== NE_EXPR
)
1241 ppl_Pointset_Powerset_C_Polyhedron_t left
= PBB_DOMAIN (pbb
);
1242 ppl_Pointset_Powerset_C_Polyhedron_t right
;
1243 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1245 add_condition_to_domain (left
, stmt
, pbb
, LT_EXPR
);
1246 add_condition_to_domain (right
, stmt
, pbb
, GT_EXPR
);
1247 ppl_Pointset_Powerset_C_Polyhedron_upper_bound_assign (left
, right
);
1248 ppl_delete_Pointset_Powerset_C_Polyhedron (right
);
1251 add_condition_to_domain (PBB_DOMAIN (pbb
), stmt
, pbb
, code
);
1254 /* Add conditions to the domain of PBB. */
1257 add_conditions_to_domain (poly_bb_p pbb
)
1261 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
1263 if (VEC_empty (gimple
, GBB_CONDITIONS (gbb
)))
1266 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
1267 switch (gimple_code (stmt
))
1271 enum tree_code code
= gimple_cond_code (stmt
);
1273 /* The conditions for ELSE-branches are inverted. */
1274 if (!VEC_index (gimple
, GBB_CONDITION_CASES (gbb
), i
))
1275 code
= invert_tree_comparison (code
, false);
1277 add_condition_to_pbb (pbb
, stmt
, code
);
1282 /* Switch statements are not supported right now - fall throught. */
1290 /* Traverses all the GBBs of the SCOP and add their constraints to the
1291 iteration domains. */
1294 add_conditions_to_constraints (scop_p scop
)
1299 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1300 add_conditions_to_domain (pbb
);
1303 /* Structure used to pass data to dom_walk. */
1307 VEC (gimple
, heap
) **conditions
, **cases
;
1311 /* Returns a COND_EXPR statement when BB has a single predecessor, the
1312 edge between BB and its predecessor is not a loop exit edge, and
1313 the last statement of the single predecessor is a COND_EXPR. */
1316 single_pred_cond_non_loop_exit (basic_block bb
)
1318 if (single_pred_p (bb
))
1320 edge e
= single_pred_edge (bb
);
1321 basic_block pred
= e
->src
;
1324 if (loop_depth (pred
->loop_father
) > loop_depth (bb
->loop_father
))
1327 stmt
= last_stmt (pred
);
1329 if (stmt
&& gimple_code (stmt
) == GIMPLE_COND
)
1336 /* Call-back for dom_walk executed before visiting the dominated
1340 build_sese_conditions_before (struct dom_walk_data
*dw_data
,
1343 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1344 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1345 VEC (gimple
, heap
) **cases
= data
->cases
;
1349 if (!bb_in_sese_p (bb
, data
->region
))
1352 stmt
= single_pred_cond_non_loop_exit (bb
);
1356 edge e
= single_pred_edge (bb
);
1358 VEC_safe_push (gimple
, heap
, *conditions
, stmt
);
1360 if (e
->flags
& EDGE_TRUE_VALUE
)
1361 VEC_safe_push (gimple
, heap
, *cases
, stmt
);
1363 VEC_safe_push (gimple
, heap
, *cases
, NULL
);
1366 gbb
= gbb_from_bb (bb
);
1370 GBB_CONDITIONS (gbb
) = VEC_copy (gimple
, heap
, *conditions
);
1371 GBB_CONDITION_CASES (gbb
) = VEC_copy (gimple
, heap
, *cases
);
1375 /* Call-back for dom_walk executed after visiting the dominated
1379 build_sese_conditions_after (struct dom_walk_data
*dw_data
,
1382 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1383 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1384 VEC (gimple
, heap
) **cases
= data
->cases
;
1386 if (!bb_in_sese_p (bb
, data
->region
))
1389 if (single_pred_cond_non_loop_exit (bb
))
1391 VEC_pop (gimple
, *conditions
);
1392 VEC_pop (gimple
, *cases
);
1396 /* Record all conditions in REGION. */
1399 build_sese_conditions (sese region
)
1401 struct dom_walk_data walk_data
;
1402 VEC (gimple
, heap
) *conditions
= VEC_alloc (gimple
, heap
, 3);
1403 VEC (gimple
, heap
) *cases
= VEC_alloc (gimple
, heap
, 3);
1406 data
.conditions
= &conditions
;
1407 data
.cases
= &cases
;
1408 data
.region
= region
;
1410 walk_data
.dom_direction
= CDI_DOMINATORS
;
1411 walk_data
.initialize_block_local_data
= NULL
;
1412 walk_data
.before_dom_children
= build_sese_conditions_before
;
1413 walk_data
.after_dom_children
= build_sese_conditions_after
;
1414 walk_data
.global_data
= &data
;
1415 walk_data
.block_local_data_size
= 0;
1417 init_walk_dominator_tree (&walk_data
);
1418 walk_dominator_tree (&walk_data
, SESE_ENTRY_BB (region
));
1419 fini_walk_dominator_tree (&walk_data
);
1421 VEC_free (gimple
, heap
, conditions
);
1422 VEC_free (gimple
, heap
, cases
);
1425 /* Add constraints on the possible values of parameter P from the type
1429 add_param_constraints (scop_p scop
, ppl_Polyhedron_t context
, graphite_dim_t p
)
1431 ppl_Constraint_t cstr
;
1432 ppl_Linear_Expression_t le
;
1433 tree parameter
= VEC_index (tree
, SESE_PARAMS (SCOP_REGION (scop
)), p
);
1434 tree type
= TREE_TYPE (parameter
);
1435 tree lb
= NULL_TREE
;
1436 tree ub
= NULL_TREE
;
1438 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1439 lb
= lower_bound_in_type (type
, type
);
1441 lb
= TYPE_MIN_VALUE (type
);
1443 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1444 ub
= upper_bound_in_type (type
, type
);
1446 ub
= TYPE_MAX_VALUE (type
);
1450 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1451 ppl_set_coef (le
, p
, -1);
1452 ppl_set_inhomogeneous_tree (le
, lb
);
1453 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
1454 ppl_Polyhedron_add_constraint (context
, cstr
);
1455 ppl_delete_Linear_Expression (le
);
1456 ppl_delete_Constraint (cstr
);
1461 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1462 ppl_set_coef (le
, p
, -1);
1463 ppl_set_inhomogeneous_tree (le
, ub
);
1464 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1465 ppl_Polyhedron_add_constraint (context
, cstr
);
1466 ppl_delete_Linear_Expression (le
);
1467 ppl_delete_Constraint (cstr
);
1471 /* Build the context of the SCOP. The context usually contains extra
1472 constraints that are added to the iteration domains that constrain
1476 build_scop_context (scop_p scop
)
1478 ppl_Polyhedron_t context
;
1479 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
1480 graphite_dim_t p
, n
= scop_nb_params (scop
);
1482 ppl_new_C_Polyhedron_from_space_dimension (&context
, n
, 0);
1484 for (p
= 0; p
< n
; p
++)
1485 add_param_constraints (scop
, context
, p
);
1487 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1489 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1490 (SCOP_CONTEXT (scop
), ps
);
1492 ppl_delete_Pointset_Powerset_C_Polyhedron (ps
);
1493 ppl_delete_Polyhedron (context
);
1496 /* Build the iteration domains: the loops belonging to the current
1497 SCOP, and that vary for the execution of the current basic block.
1498 Returns false if there is no loop in SCOP. */
1501 build_scop_iteration_domain (scop_p scop
)
1504 sese region
= SCOP_REGION (scop
);
1506 ppl_Polyhedron_t ph
;
1508 int nb_loops
= number_of_loops ();
1509 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
1510 = XNEWVEC (ppl_Pointset_Powerset_C_Polyhedron_t
, nb_loops
);
1512 for (i
= 0; i
< nb_loops
; i
++)
1515 ppl_new_C_Polyhedron_from_space_dimension (&ph
, scop_nb_params (scop
), 0);
1517 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
1518 if (!loop_in_sese_p (loop_outer (loop
), region
))
1519 build_loop_iteration_domains (scop
, loop
, ph
, 0, domains
);
1521 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1522 if (domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
])
1523 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1524 (&PBB_DOMAIN (pbb
), (ppl_const_Pointset_Powerset_C_Polyhedron_t
)
1525 domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
]);
1527 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1528 (&PBB_DOMAIN (pbb
), ph
);
1530 for (i
= 0; i
< nb_loops
; i
++)
1532 ppl_delete_Pointset_Powerset_C_Polyhedron (domains
[i
]);
1534 ppl_delete_Polyhedron (ph
);
1538 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1539 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1540 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1544 pdr_add_alias_set (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1545 ppl_dimension_type accessp_nb_dims
,
1546 ppl_dimension_type dom_nb_dims
)
1548 ppl_Linear_Expression_t alias
;
1549 ppl_Constraint_t cstr
;
1550 int alias_set_num
= 0;
1551 base_alias_pair
*bap
= (base_alias_pair
*)(dr
->aux
);
1553 if (bap
&& bap
->alias_set
)
1554 alias_set_num
= *(bap
->alias_set
);
1556 ppl_new_Linear_Expression_with_dimension (&alias
, accessp_nb_dims
);
1558 ppl_set_coef (alias
, dom_nb_dims
, 1);
1559 ppl_set_inhomogeneous (alias
, -alias_set_num
);
1560 ppl_new_Constraint (&cstr
, alias
, PPL_CONSTRAINT_TYPE_EQUAL
);
1561 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1563 ppl_delete_Linear_Expression (alias
);
1564 ppl_delete_Constraint (cstr
);
1567 /* Add to ACCESSES polyhedron equalities defining the access functions
1568 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1569 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1570 PBB is the poly_bb_p that contains the data reference DR. */
1573 pdr_add_memory_accesses (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1574 ppl_dimension_type accessp_nb_dims
,
1575 ppl_dimension_type dom_nb_dims
,
1578 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1580 scop_p scop
= PBB_SCOP (pbb
);
1581 sese region
= SCOP_REGION (scop
);
1585 for (i
= 0; i
< nb_subscripts
; i
++)
1587 ppl_Linear_Expression_t fn
, access
;
1588 ppl_Constraint_t cstr
;
1589 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1590 tree afn
= DR_ACCESS_FN (dr
, nb_subscripts
- 1 - i
);
1592 ppl_new_Linear_Expression_with_dimension (&fn
, dom_nb_dims
);
1593 ppl_new_Linear_Expression_with_dimension (&access
, accessp_nb_dims
);
1596 scan_tree_for_params (region
, afn
, fn
, v
);
1597 ppl_assign_Linear_Expression_from_Linear_Expression (access
, fn
);
1599 ppl_set_coef (access
, subscript
, -1);
1600 ppl_new_Constraint (&cstr
, access
, PPL_CONSTRAINT_TYPE_EQUAL
);
1601 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1603 ppl_delete_Linear_Expression (fn
);
1604 ppl_delete_Linear_Expression (access
);
1605 ppl_delete_Constraint (cstr
);
1611 /* Add constrains representing the size of the accessed data to the
1612 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1613 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1617 pdr_add_data_dimensions (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1618 ppl_dimension_type accessp_nb_dims
,
1619 ppl_dimension_type dom_nb_dims
)
1621 tree ref
= DR_REF (dr
);
1622 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1624 for (i
= nb_subscripts
- 1; i
>= 0; i
--, ref
= TREE_OPERAND (ref
, 0))
1626 ppl_Linear_Expression_t expr
;
1627 ppl_Constraint_t cstr
;
1628 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1631 if (TREE_CODE (ref
) != ARRAY_REF
)
1634 low
= array_ref_low_bound (ref
);
1636 /* subscript - low >= 0 */
1637 if (host_integerp (low
, 0))
1641 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1642 ppl_set_coef (expr
, subscript
, 1);
1644 minus_low
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (low
), low
);
1645 ppl_set_inhomogeneous_tree (expr
, minus_low
);
1647 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1648 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1649 ppl_delete_Linear_Expression (expr
);
1650 ppl_delete_Constraint (cstr
);
1653 high
= array_ref_up_bound (ref
);
1655 /* high - subscript >= 0 */
1656 if (high
&& host_integerp (high
, 0)
1657 /* 1-element arrays at end of structures may extend over
1658 their declared size. */
1659 && !(array_at_struct_end_p (ref
)
1660 && operand_equal_p (low
, high
, 0)))
1662 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1663 ppl_set_coef (expr
, subscript
, -1);
1665 ppl_set_inhomogeneous_tree (expr
, high
);
1667 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1668 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1669 ppl_delete_Linear_Expression (expr
);
1670 ppl_delete_Constraint (cstr
);
1675 /* Build data accesses for DR in PBB. */
1678 build_poly_dr (data_reference_p dr
, poly_bb_p pbb
)
1680 ppl_Polyhedron_t accesses
;
1681 ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps
;
1682 ppl_dimension_type dom_nb_dims
;
1683 ppl_dimension_type accessp_nb_dims
;
1684 int dr_base_object_set
;
1686 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb
),
1688 accessp_nb_dims
= dom_nb_dims
+ 1 + DR_NUM_DIMENSIONS (dr
);
1690 ppl_new_C_Polyhedron_from_space_dimension (&accesses
, accessp_nb_dims
, 0);
1692 pdr_add_alias_set (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1693 pdr_add_memory_accesses (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
, pbb
);
1694 pdr_add_data_dimensions (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1696 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps
,
1698 ppl_delete_Polyhedron (accesses
);
1700 gcc_assert (dr
->aux
);
1701 dr_base_object_set
= ((base_alias_pair
*)(dr
->aux
))->base_obj_set
;
1703 new_poly_dr (pbb
, dr_base_object_set
, accesses_ps
,
1704 DR_IS_READ (dr
) ? PDR_READ
: PDR_WRITE
,
1705 dr
, DR_NUM_DIMENSIONS (dr
));
1708 /* Write to FILE the alias graph of data references in DIMACS format. */
1711 write_alias_graph_to_ascii_dimacs (FILE *file
, char *comment
,
1712 VEC (data_reference_p
, heap
) *drs
)
1714 int num_vertex
= VEC_length (data_reference_p
, drs
);
1716 data_reference_p dr1
, dr2
;
1719 if (num_vertex
== 0)
1722 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1723 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1724 if (dr_may_alias_p (dr1
, dr2
))
1727 fprintf (file
, "$\n");
1730 fprintf (file
, "c %s\n", comment
);
1732 fprintf (file
, "p edge %d %d\n", num_vertex
, edge_num
);
1734 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1735 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1736 if (dr_may_alias_p (dr1
, dr2
))
1737 fprintf (file
, "e %d %d\n", i
+ 1, j
+ 1);
1742 /* Write to FILE the alias graph of data references in DOT format. */
1745 write_alias_graph_to_ascii_dot (FILE *file
, char *comment
,
1746 VEC (data_reference_p
, heap
) *drs
)
1748 int num_vertex
= VEC_length (data_reference_p
, drs
);
1749 data_reference_p dr1
, dr2
;
1752 if (num_vertex
== 0)
1755 fprintf (file
, "$\n");
1758 fprintf (file
, "c %s\n", comment
);
1760 /* First print all the vertices. */
1761 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1762 fprintf (file
, "n%d;\n", i
);
1764 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1765 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1766 if (dr_may_alias_p (dr1
, dr2
))
1767 fprintf (file
, "n%d n%d\n", i
, j
);
1772 /* Write to FILE the alias graph of data references in ECC format. */
1775 write_alias_graph_to_ascii_ecc (FILE *file
, char *comment
,
1776 VEC (data_reference_p
, heap
) *drs
)
1778 int num_vertex
= VEC_length (data_reference_p
, drs
);
1779 data_reference_p dr1
, dr2
;
1782 if (num_vertex
== 0)
1785 fprintf (file
, "$\n");
1788 fprintf (file
, "c %s\n", comment
);
1790 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1791 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1792 if (dr_may_alias_p (dr1
, dr2
))
1793 fprintf (file
, "%d %d\n", i
, j
);
1798 /* Check if DR1 and DR2 are in the same object set. */
1801 dr_same_base_object_p (const struct data_reference
*dr1
,
1802 const struct data_reference
*dr2
)
1804 return operand_equal_p (DR_BASE_OBJECT (dr1
), DR_BASE_OBJECT (dr2
), 0);
1807 /* Uses DFS component number as representative of alias-sets. Also tests for
1808 optimality by verifying if every connected component is a clique. Returns
1809 true (1) if the above test is true, and false (0) otherwise. */
1812 build_alias_set_optimal_p (VEC (data_reference_p
, heap
) *drs
)
1814 int num_vertices
= VEC_length (data_reference_p
, drs
);
1815 struct graph
*g
= new_graph (num_vertices
);
1816 data_reference_p dr1
, dr2
;
1818 int num_connected_components
;
1819 int v_indx1
, v_indx2
, num_vertices_in_component
;
1822 struct graph_edge
*e
;
1823 int this_component_is_clique
;
1824 int all_components_are_cliques
= 1;
1826 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1827 for (j
= i
+1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1828 if (dr_may_alias_p (dr1
, dr2
))
1834 all_vertices
= XNEWVEC (int, num_vertices
);
1835 vertices
= XNEWVEC (int, num_vertices
);
1836 for (i
= 0; i
< num_vertices
; i
++)
1837 all_vertices
[i
] = i
;
1839 num_connected_components
= graphds_dfs (g
, all_vertices
, num_vertices
,
1841 for (i
= 0; i
< g
->n_vertices
; i
++)
1843 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1844 base_alias_pair
*bap
;
1846 gcc_assert (dr
->aux
);
1847 bap
= (base_alias_pair
*)(dr
->aux
);
1849 bap
->alias_set
= XNEW (int);
1850 *(bap
->alias_set
) = g
->vertices
[i
].component
+ 1;
1853 /* Verify if the DFS numbering results in optimal solution. */
1854 for (i
= 0; i
< num_connected_components
; i
++)
1856 num_vertices_in_component
= 0;
1857 /* Get all vertices whose DFS component number is the same as i. */
1858 for (j
= 0; j
< num_vertices
; j
++)
1859 if (g
->vertices
[j
].component
== i
)
1860 vertices
[num_vertices_in_component
++] = j
;
1862 /* Now test if the vertices in 'vertices' form a clique, by testing
1863 for edges among each pair. */
1864 this_component_is_clique
= 1;
1865 for (v_indx1
= 0; v_indx1
< num_vertices_in_component
; v_indx1
++)
1867 for (v_indx2
= v_indx1
+1; v_indx2
< num_vertices_in_component
; v_indx2
++)
1869 /* Check if the two vertices are connected by iterating
1870 through all the edges which have one of these are source. */
1871 e
= g
->vertices
[vertices
[v_indx2
]].pred
;
1874 if (e
->src
== vertices
[v_indx1
])
1880 this_component_is_clique
= 0;
1884 if (!this_component_is_clique
)
1885 all_components_are_cliques
= 0;
1889 free (all_vertices
);
1892 return all_components_are_cliques
;
1895 /* Group each data reference in DRS with its base object set num. */
1898 build_base_obj_set_for_drs (VEC (data_reference_p
, heap
) *drs
)
1900 int num_vertex
= VEC_length (data_reference_p
, drs
);
1901 struct graph
*g
= new_graph (num_vertex
);
1902 data_reference_p dr1
, dr2
;
1906 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1907 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1908 if (dr_same_base_object_p (dr1
, dr2
))
1914 queue
= XNEWVEC (int, num_vertex
);
1915 for (i
= 0; i
< num_vertex
; i
++)
1918 graphds_dfs (g
, queue
, num_vertex
, NULL
, true, NULL
);
1920 for (i
= 0; i
< g
->n_vertices
; i
++)
1922 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1923 base_alias_pair
*bap
;
1925 gcc_assert (dr
->aux
);
1926 bap
= (base_alias_pair
*)(dr
->aux
);
1928 bap
->base_obj_set
= g
->vertices
[i
].component
+ 1;
1935 /* Build the data references for PBB. */
1938 build_pbb_drs (poly_bb_p pbb
)
1941 data_reference_p dr
;
1942 VEC (data_reference_p
, heap
) *gbb_drs
= GBB_DATA_REFS (PBB_BLACK_BOX (pbb
));
1944 FOR_EACH_VEC_ELT (data_reference_p
, gbb_drs
, j
, dr
)
1945 build_poly_dr (dr
, pbb
);
1948 /* Dump to file the alias graphs for the data references in DRS. */
1951 dump_alias_graphs (VEC (data_reference_p
, heap
) *drs
)
1954 FILE *file_dimacs
, *file_ecc
, *file_dot
;
1956 file_dimacs
= fopen ("/tmp/dr_alias_graph_dimacs", "ab");
1959 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1960 current_function_name ());
1961 write_alias_graph_to_ascii_dimacs (file_dimacs
, comment
, drs
);
1962 fclose (file_dimacs
);
1965 file_ecc
= fopen ("/tmp/dr_alias_graph_ecc", "ab");
1968 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1969 current_function_name ());
1970 write_alias_graph_to_ascii_ecc (file_ecc
, comment
, drs
);
1974 file_dot
= fopen ("/tmp/dr_alias_graph_dot", "ab");
1977 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1978 current_function_name ());
1979 write_alias_graph_to_ascii_dot (file_dot
, comment
, drs
);
1984 /* Build data references in SCOP. */
1987 build_scop_drs (scop_p scop
)
1991 data_reference_p dr
;
1992 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 3);
1994 /* Remove all the PBBs that do not have data references: these basic
1995 blocks are not handled in the polyhedral representation. */
1996 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1997 if (VEC_empty (data_reference_p
, GBB_DATA_REFS (PBB_BLACK_BOX (pbb
))))
1999 free_gimple_bb (PBB_BLACK_BOX (pbb
));
2000 VEC_ordered_remove (poly_bb_p
, SCOP_BBS (scop
), i
);
2004 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2005 for (j
= 0; VEC_iterate (data_reference_p
,
2006 GBB_DATA_REFS (PBB_BLACK_BOX (pbb
)), j
, dr
); j
++)
2007 VEC_safe_push (data_reference_p
, heap
, drs
, dr
);
2009 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr
)
2010 dr
->aux
= XNEW (base_alias_pair
);
2012 if (!build_alias_set_optimal_p (drs
))
2014 /* TODO: Add support when building alias set is not optimal. */
2018 build_base_obj_set_for_drs (drs
);
2020 /* When debugging, enable the following code. This cannot be used
2021 in production compilers. */
2023 dump_alias_graphs (drs
);
2025 VEC_free (data_reference_p
, heap
, drs
);
2027 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2028 build_pbb_drs (pbb
);
2031 /* Return a gsi at the position of the phi node STMT. */
2033 static gimple_stmt_iterator
2034 gsi_for_phi_node (gimple stmt
)
2036 gimple_stmt_iterator psi
;
2037 basic_block bb
= gimple_bb (stmt
);
2039 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2040 if (stmt
== gsi_stmt (psi
))
2047 /* Analyze all the data references of STMTS and add them to the
2048 GBB_DATA_REFS vector of BB. */
2051 analyze_drs_in_stmts (scop_p scop
, basic_block bb
, VEC (gimple
, heap
) *stmts
)
2057 sese region
= SCOP_REGION (scop
);
2059 if (!bb_in_sese_p (bb
, region
))
2062 nest
= outermost_loop_in_sese_1 (region
, bb
);
2063 gbb
= gbb_from_bb (bb
);
2065 FOR_EACH_VEC_ELT (gimple
, stmts
, i
, stmt
)
2069 if (is_gimple_debug (stmt
))
2072 loop
= loop_containing_stmt (stmt
);
2073 if (!loop_in_sese_p (loop
, region
))
2076 graphite_find_data_references_in_stmt (nest
, loop
, stmt
,
2077 &GBB_DATA_REFS (gbb
));
2081 /* Insert STMT at the end of the STMTS sequence and then insert the
2082 statements from STMTS at INSERT_GSI and call analyze_drs_in_stmts
2086 insert_stmts (scop_p scop
, gimple stmt
, gimple_seq stmts
,
2087 gimple_stmt_iterator insert_gsi
)
2089 gimple_stmt_iterator gsi
;
2090 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2093 stmts
= gimple_seq_alloc ();
2095 gsi
= gsi_last (stmts
);
2096 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2097 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2098 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2100 gsi_insert_seq_before (&insert_gsi
, stmts
, GSI_SAME_STMT
);
2101 analyze_drs_in_stmts (scop
, gsi_bb (insert_gsi
), x
);
2102 VEC_free (gimple
, heap
, x
);
2105 /* Insert the assignment "RES := EXPR" just after AFTER_STMT. */
2108 insert_out_of_ssa_copy (scop_p scop
, tree res
, tree expr
, gimple after_stmt
)
2111 gimple_stmt_iterator si
;
2112 gimple_stmt_iterator gsi
;
2113 tree var
= force_gimple_operand (expr
, &stmts
, true, NULL_TREE
);
2114 gimple stmt
= gimple_build_assign (res
, var
);
2115 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2118 stmts
= gimple_seq_alloc ();
2119 si
= gsi_last (stmts
);
2120 gsi_insert_after (&si
, stmt
, GSI_NEW_STMT
);
2121 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2122 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2124 if (gimple_code (after_stmt
) == GIMPLE_PHI
)
2126 gsi
= gsi_after_labels (gimple_bb (after_stmt
));
2127 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
2131 gsi
= gsi_for_stmt (after_stmt
);
2132 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
2135 analyze_drs_in_stmts (scop
, gimple_bb (after_stmt
), x
);
2136 VEC_free (gimple
, heap
, x
);
2139 /* Creates a poly_bb_p for basic_block BB from the existing PBB. */
2142 new_pbb_from_pbb (scop_p scop
, poly_bb_p pbb
, basic_block bb
)
2144 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 3);
2145 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
2146 gimple_bb_p gbb1
= new_gimple_bb (bb
, drs
);
2147 poly_bb_p pbb1
= new_poly_bb (scop
, gbb1
);
2148 int index
, n
= VEC_length (poly_bb_p
, SCOP_BBS (scop
));
2150 /* The INDEX of PBB in SCOP_BBS. */
2151 for (index
= 0; index
< n
; index
++)
2152 if (VEC_index (poly_bb_p
, SCOP_BBS (scop
), index
) == pbb
)
2155 if (PBB_DOMAIN (pbb
))
2156 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
2157 (&PBB_DOMAIN (pbb1
), PBB_DOMAIN (pbb
));
2159 GBB_PBB (gbb1
) = pbb1
;
2160 GBB_CONDITIONS (gbb1
) = VEC_copy (gimple
, heap
, GBB_CONDITIONS (gbb
));
2161 GBB_CONDITION_CASES (gbb1
) = VEC_copy (gimple
, heap
, GBB_CONDITION_CASES (gbb
));
2162 VEC_safe_insert (poly_bb_p
, heap
, SCOP_BBS (scop
), index
+ 1, pbb1
);
2165 /* Insert on edge E the assignment "RES := EXPR". */
2168 insert_out_of_ssa_copy_on_edge (scop_p scop
, edge e
, tree res
, tree expr
)
2170 gimple_stmt_iterator gsi
;
2172 tree var
= force_gimple_operand (expr
, &stmts
, true, NULL_TREE
);
2173 gimple stmt
= gimple_build_assign (res
, var
);
2175 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2178 stmts
= gimple_seq_alloc ();
2180 gsi
= gsi_last (stmts
);
2181 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2182 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2183 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2185 gsi_insert_seq_on_edge (e
, stmts
);
2186 gsi_commit_edge_inserts ();
2187 bb
= gimple_bb (stmt
);
2189 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
2192 if (!gbb_from_bb (bb
))
2193 new_pbb_from_pbb (scop
, pbb_from_bb (e
->src
), bb
);
2195 analyze_drs_in_stmts (scop
, bb
, x
);
2196 VEC_free (gimple
, heap
, x
);
2199 /* Creates a zero dimension array of the same type as VAR. */
2202 create_zero_dim_array (tree var
, const char *base_name
)
2204 tree index_type
= build_index_type (integer_zero_node
);
2205 tree elt_type
= TREE_TYPE (var
);
2206 tree array_type
= build_array_type (elt_type
, index_type
);
2207 tree base
= create_tmp_var (array_type
, base_name
);
2209 add_referenced_var (base
);
2211 return build4 (ARRAY_REF
, elt_type
, base
, integer_zero_node
, NULL_TREE
,
2215 /* Returns true when PHI is a loop close phi node. */
2218 scalar_close_phi_node_p (gimple phi
)
2220 if (gimple_code (phi
) != GIMPLE_PHI
2221 || !is_gimple_reg (gimple_phi_result (phi
)))
2224 /* Note that loop close phi nodes should have a single argument
2225 because we translated the representation into a canonical form
2226 before Graphite: see canonicalize_loop_closed_ssa_form. */
2227 return (gimple_phi_num_args (phi
) == 1);
2230 /* For a definition DEF in REGION, propagates the expression EXPR in
2231 all the uses of DEF outside REGION. */
2234 propagate_expr_outside_region (tree def
, tree expr
, sese region
)
2236 imm_use_iterator imm_iter
;
2239 bool replaced_once
= false;
2241 gcc_assert (TREE_CODE (def
) == SSA_NAME
);
2243 expr
= force_gimple_operand (unshare_expr (expr
), &stmts
, true,
2246 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2247 if (!is_gimple_debug (use_stmt
)
2248 && !bb_in_sese_p (gimple_bb (use_stmt
), region
))
2251 use_operand_p use_p
;
2253 FOR_EACH_PHI_OR_STMT_USE (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2254 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0)
2255 && (replaced_once
= true))
2256 replace_exp (use_p
, expr
);
2258 update_stmt (use_stmt
);
2263 gsi_insert_seq_on_edge (SESE_ENTRY (region
), stmts
);
2264 gsi_commit_edge_inserts ();
2268 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2269 dimension array for it. */
2272 rewrite_close_phi_out_of_ssa (scop_p scop
, gimple_stmt_iterator
*psi
)
2274 sese region
= SCOP_REGION (scop
);
2275 gimple phi
= gsi_stmt (*psi
);
2276 tree res
= gimple_phi_result (phi
);
2277 tree var
= SSA_NAME_VAR (res
);
2278 basic_block bb
= gimple_bb (phi
);
2279 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
2280 tree arg
= gimple_phi_arg_def (phi
, 0);
2283 /* Note that loop close phi nodes should have a single argument
2284 because we translated the representation into a canonical form
2285 before Graphite: see canonicalize_loop_closed_ssa_form. */
2286 gcc_assert (gimple_phi_num_args (phi
) == 1);
2288 /* The phi node can be a non close phi node, when its argument is
2289 invariant, or a default definition. */
2290 if (is_gimple_min_invariant (arg
)
2291 || SSA_NAME_IS_DEFAULT_DEF (arg
))
2293 propagate_expr_outside_region (res
, arg
, region
);
2298 else if (gimple_bb (SSA_NAME_DEF_STMT (arg
))->loop_father
== bb
->loop_father
)
2300 propagate_expr_outside_region (res
, arg
, region
);
2301 stmt
= gimple_build_assign (res
, arg
);
2302 remove_phi_node (psi
, false);
2303 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2304 SSA_NAME_DEF_STMT (res
) = stmt
;
2308 /* If res is scev analyzable and is not a scalar value, it is safe
2309 to ignore the close phi node: it will be code generated in the
2310 out of Graphite pass. */
2311 else if (scev_analyzable_p (res
, region
))
2313 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (res
));
2316 if (!loop_in_sese_p (loop
, region
))
2318 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (arg
));
2319 scev
= scalar_evolution_in_region (region
, loop
, arg
);
2320 scev
= compute_overall_effect_of_inner_loop (loop
, scev
);
2323 scev
= scalar_evolution_in_region (region
, loop
, res
);
2325 if (tree_does_not_contain_chrecs (scev
))
2326 propagate_expr_outside_region (res
, scev
, region
);
2333 tree zero_dim_array
= create_zero_dim_array (var
, "Close_Phi");
2335 stmt
= gimple_build_assign (res
, zero_dim_array
);
2337 if (TREE_CODE (arg
) == SSA_NAME
)
2338 insert_out_of_ssa_copy (scop
, zero_dim_array
, arg
,
2339 SSA_NAME_DEF_STMT (arg
));
2341 insert_out_of_ssa_copy_on_edge (scop
, single_pred_edge (bb
),
2342 zero_dim_array
, arg
);
2345 remove_phi_node (psi
, false);
2346 SSA_NAME_DEF_STMT (res
) = stmt
;
2348 insert_stmts (scop
, stmt
, NULL
, gsi_after_labels (bb
));
2351 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2352 dimension array for it. */
2355 rewrite_phi_out_of_ssa (scop_p scop
, gimple_stmt_iterator
*psi
)
2358 gimple phi
= gsi_stmt (*psi
);
2359 basic_block bb
= gimple_bb (phi
);
2360 tree res
= gimple_phi_result (phi
);
2361 tree var
= SSA_NAME_VAR (res
);
2362 tree zero_dim_array
= create_zero_dim_array (var
, "phi_out_of_ssa");
2366 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2368 tree arg
= gimple_phi_arg_def (phi
, i
);
2369 edge e
= gimple_phi_arg_edge (phi
, i
);
2371 /* Avoid the insertion of code in the loop latch to please the
2372 pattern matching of the vectorizer. */
2373 if (TREE_CODE (arg
) == SSA_NAME
2374 && e
->src
== bb
->loop_father
->latch
)
2375 insert_out_of_ssa_copy (scop
, zero_dim_array
, arg
,
2376 SSA_NAME_DEF_STMT (arg
));
2378 insert_out_of_ssa_copy_on_edge (scop
, e
, zero_dim_array
, arg
);
2381 var
= force_gimple_operand (zero_dim_array
, &stmts
, true, NULL_TREE
);
2383 stmt
= gimple_build_assign (res
, var
);
2384 remove_phi_node (psi
, false);
2385 SSA_NAME_DEF_STMT (res
) = stmt
;
2387 insert_stmts (scop
, stmt
, stmts
, gsi_after_labels (bb
));
2390 /* Rewrite the degenerate phi node at position PSI from the degenerate
2391 form "x = phi (y, y, ..., y)" to "x = y". */
2394 rewrite_degenerate_phi (gimple_stmt_iterator
*psi
)
2398 gimple_stmt_iterator gsi
;
2399 gimple phi
= gsi_stmt (*psi
);
2400 tree res
= gimple_phi_result (phi
);
2403 bb
= gimple_bb (phi
);
2404 rhs
= degenerate_phi_result (phi
);
2407 stmt
= gimple_build_assign (res
, rhs
);
2408 remove_phi_node (psi
, false);
2409 SSA_NAME_DEF_STMT (res
) = stmt
;
2411 gsi
= gsi_after_labels (bb
);
2412 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2415 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2418 rewrite_reductions_out_of_ssa (scop_p scop
)
2421 gimple_stmt_iterator psi
;
2422 sese region
= SCOP_REGION (scop
);
2425 if (bb_in_sese_p (bb
, region
))
2426 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);)
2428 gimple phi
= gsi_stmt (psi
);
2430 if (!is_gimple_reg (gimple_phi_result (phi
)))
2436 if (gimple_phi_num_args (phi
) > 1
2437 && degenerate_phi_result (phi
))
2438 rewrite_degenerate_phi (&psi
);
2440 else if (scalar_close_phi_node_p (phi
))
2441 rewrite_close_phi_out_of_ssa (scop
, &psi
);
2443 else if (reduction_phi_p (region
, &psi
))
2444 rewrite_phi_out_of_ssa (scop
, &psi
);
2447 update_ssa (TODO_update_ssa
);
2448 #ifdef ENABLE_CHECKING
2449 verify_loop_closed_ssa (true);
2453 /* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
2454 read from ZERO_DIM_ARRAY. */
2457 rewrite_cross_bb_scalar_dependence (scop_p scop
, tree zero_dim_array
,
2458 tree def
, gimple use_stmt
)
2460 tree var
= SSA_NAME_VAR (def
);
2461 gimple name_stmt
= gimple_build_assign (var
, zero_dim_array
);
2462 tree name
= make_ssa_name (var
, name_stmt
);
2464 use_operand_p use_p
;
2466 gcc_assert (gimple_code (use_stmt
) != GIMPLE_PHI
);
2468 gimple_assign_set_lhs (name_stmt
, name
);
2469 insert_stmts (scop
, name_stmt
, NULL
, gsi_for_stmt (use_stmt
));
2471 FOR_EACH_SSA_USE_OPERAND (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2472 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0))
2473 replace_exp (use_p
, name
);
2475 update_stmt (use_stmt
);
2478 /* For every definition DEF in the SCOP that is used outside the scop,
2479 insert a closing-scop definition in the basic block just after this
2483 handle_scalar_deps_crossing_scop_limits (scop_p scop
, tree def
, gimple stmt
)
2485 tree var
= create_tmp_reg (TREE_TYPE (def
), NULL
);
2486 tree new_name
= make_ssa_name (var
, stmt
);
2487 bool needs_copy
= false;
2488 use_operand_p use_p
;
2489 imm_use_iterator imm_iter
;
2491 sese region
= SCOP_REGION (scop
);
2493 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2495 if (!bb_in_sese_p (gimple_bb (use_stmt
), region
))
2497 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
2499 SET_USE (use_p
, new_name
);
2501 update_stmt (use_stmt
);
2506 /* Insert in the empty BB just after the scop a use of DEF such
2507 that the rewrite of cross_bb_scalar_dependences won't insert
2508 arrays everywhere else. */
2511 gimple assign
= gimple_build_assign (new_name
, def
);
2512 gimple_stmt_iterator psi
= gsi_after_labels (SESE_EXIT (region
)->dest
);
2514 add_referenced_var (var
);
2515 SSA_NAME_DEF_STMT (new_name
) = assign
;
2516 update_stmt (assign
);
2517 gsi_insert_before (&psi
, assign
, GSI_SAME_STMT
);
2521 /* Rewrite the scalar dependences crossing the boundary of the BB
2522 containing STMT with an array. Return true when something has been
2526 rewrite_cross_bb_scalar_deps (scop_p scop
, gimple_stmt_iterator
*gsi
)
2528 sese region
= SCOP_REGION (scop
);
2529 gimple stmt
= gsi_stmt (*gsi
);
2530 imm_use_iterator imm_iter
;
2533 tree zero_dim_array
= NULL_TREE
;
2537 switch (gimple_code (stmt
))
2540 def
= gimple_assign_lhs (stmt
);
2544 def
= gimple_call_lhs (stmt
);
2552 || !is_gimple_reg (def
))
2555 if (scev_analyzable_p (def
, region
))
2557 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (def
));
2558 tree scev
= scalar_evolution_in_region (region
, loop
, def
);
2560 if (tree_contains_chrecs (scev
, NULL
))
2563 propagate_expr_outside_region (def
, scev
, region
);
2567 def_bb
= gimple_bb (stmt
);
2569 handle_scalar_deps_crossing_scop_limits (scop
, def
, stmt
);
2571 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2572 if (gimple_code (use_stmt
) == GIMPLE_PHI
2575 gimple_stmt_iterator psi
= gsi_for_stmt (use_stmt
);
2577 if (scalar_close_phi_node_p (gsi_stmt (psi
)))
2578 rewrite_close_phi_out_of_ssa (scop
, &psi
);
2580 rewrite_phi_out_of_ssa (scop
, &psi
);
2583 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2584 if (gimple_code (use_stmt
) != GIMPLE_PHI
2585 && def_bb
!= gimple_bb (use_stmt
)
2586 && !is_gimple_debug (use_stmt
)
2589 if (!zero_dim_array
)
2591 zero_dim_array
= create_zero_dim_array
2592 (SSA_NAME_VAR (def
), "Cross_BB_scalar_dependence");
2593 insert_out_of_ssa_copy (scop
, zero_dim_array
, def
,
2594 SSA_NAME_DEF_STMT (def
));
2598 rewrite_cross_bb_scalar_dependence (scop
, zero_dim_array
,
2605 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2608 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop
)
2611 gimple_stmt_iterator psi
;
2612 sese region
= SCOP_REGION (scop
);
2613 bool changed
= false;
2615 /* Create an extra empty BB after the scop. */
2616 split_edge (SESE_EXIT (region
));
2619 if (bb_in_sese_p (bb
, region
))
2620 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2621 changed
|= rewrite_cross_bb_scalar_deps (scop
, &psi
);
2626 update_ssa (TODO_update_ssa
);
2627 #ifdef ENABLE_CHECKING
2628 verify_loop_closed_ssa (true);
2633 /* Returns the number of pbbs that are in loops contained in SCOP. */
2636 nb_pbbs_in_loops (scop_p scop
)
2642 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2643 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb
)), SCOP_REGION (scop
)))
2649 /* Return the number of data references in BB that write in
2653 nb_data_writes_in_bb (basic_block bb
)
2656 gimple_stmt_iterator gsi
;
2658 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2659 if (gimple_vdef (gsi_stmt (gsi
)))
2665 /* Splits at STMT the basic block BB represented as PBB in the
2669 split_pbb (scop_p scop
, poly_bb_p pbb
, basic_block bb
, gimple stmt
)
2671 edge e1
= split_block (bb
, stmt
);
2672 new_pbb_from_pbb (scop
, pbb
, e1
->dest
);
2676 /* Splits STMT out of its current BB. This is done for reduction
2677 statements for which we want to ignore data dependences. */
2680 split_reduction_stmt (scop_p scop
, gimple stmt
)
2682 basic_block bb
= gimple_bb (stmt
);
2683 poly_bb_p pbb
= pbb_from_bb (bb
);
2684 gimple_bb_p gbb
= gbb_from_bb (bb
);
2687 data_reference_p dr
;
2689 /* Do not split basic blocks with no writes to memory: the reduction
2690 will be the only write to memory. */
2691 if (nb_data_writes_in_bb (bb
) == 0
2692 /* Or if we have already marked BB as a reduction. */
2693 || PBB_IS_REDUCTION (pbb_from_bb (bb
)))
2696 e1
= split_pbb (scop
, pbb
, bb
, stmt
);
2698 /* Split once more only when the reduction stmt is not the only one
2699 left in the original BB. */
2700 if (!gsi_one_before_end_p (gsi_start_nondebug_bb (bb
)))
2702 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
2704 e1
= split_pbb (scop
, pbb
, bb
, gsi_stmt (gsi
));
2707 /* A part of the data references will end in a different basic block
2708 after the split: move the DRs from the original GBB to the newly
2710 FOR_EACH_VEC_ELT (data_reference_p
, GBB_DATA_REFS (gbb
), i
, dr
)
2712 basic_block bb1
= gimple_bb (DR_STMT (dr
));
2716 gimple_bb_p gbb1
= gbb_from_bb (bb1
);
2717 VEC_safe_push (data_reference_p
, heap
, GBB_DATA_REFS (gbb1
), dr
);
2718 VEC_ordered_remove (data_reference_p
, GBB_DATA_REFS (gbb
), i
);
2726 /* Return true when stmt is a reduction operation. */
2729 is_reduction_operation_p (gimple stmt
)
2731 enum tree_code code
;
2733 gcc_assert (is_gimple_assign (stmt
));
2734 code
= gimple_assign_rhs_code (stmt
);
2736 return flag_associative_math
2737 && commutative_tree_code (code
)
2738 && associative_tree_code (code
);
2741 /* Returns true when PHI contains an argument ARG. */
2744 phi_contains_arg (gimple phi
, tree arg
)
2748 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2749 if (operand_equal_p (arg
, gimple_phi_arg_def (phi
, i
), 0))
2755 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2758 follow_ssa_with_commutative_ops (tree arg
, tree lhs
)
2762 if (TREE_CODE (arg
) != SSA_NAME
)
2765 stmt
= SSA_NAME_DEF_STMT (arg
);
2767 if (gimple_code (stmt
) == GIMPLE_NOP
2768 || gimple_code (stmt
) == GIMPLE_CALL
)
2771 if (gimple_code (stmt
) == GIMPLE_PHI
)
2773 if (phi_contains_arg (stmt
, lhs
))
2778 if (!is_gimple_assign (stmt
))
2781 if (gimple_num_ops (stmt
) == 2)
2782 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2784 if (is_reduction_operation_p (stmt
))
2786 gimple res
= follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2789 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt
), lhs
);
2795 /* Detect commutative and associative scalar reductions starting at
2796 the STMT. Return the phi node of the reduction cycle, or NULL. */
2799 detect_commutative_reduction_arg (tree lhs
, gimple stmt
, tree arg
,
2800 VEC (gimple
, heap
) **in
,
2801 VEC (gimple
, heap
) **out
)
2803 gimple phi
= follow_ssa_with_commutative_ops (arg
, lhs
);
2808 VEC_safe_push (gimple
, heap
, *in
, stmt
);
2809 VEC_safe_push (gimple
, heap
, *out
, stmt
);
2813 /* Detect commutative and associative scalar reductions starting at
2814 STMT. Return the phi node of the reduction cycle, or NULL. */
2817 detect_commutative_reduction_assign (gimple stmt
, VEC (gimple
, heap
) **in
,
2818 VEC (gimple
, heap
) **out
)
2820 tree lhs
= gimple_assign_lhs (stmt
);
2822 if (gimple_num_ops (stmt
) == 2)
2823 return detect_commutative_reduction_arg (lhs
, stmt
,
2824 gimple_assign_rhs1 (stmt
),
2827 if (is_reduction_operation_p (stmt
))
2829 gimple res
= detect_commutative_reduction_arg (lhs
, stmt
,
2830 gimple_assign_rhs1 (stmt
),
2833 : detect_commutative_reduction_arg (lhs
, stmt
,
2834 gimple_assign_rhs2 (stmt
),
2841 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2844 follow_inital_value_to_phi (tree arg
, tree lhs
)
2848 if (!arg
|| TREE_CODE (arg
) != SSA_NAME
)
2851 stmt
= SSA_NAME_DEF_STMT (arg
);
2853 if (gimple_code (stmt
) == GIMPLE_PHI
2854 && phi_contains_arg (stmt
, lhs
))
2861 /* Return the argument of the loop PHI that is the inital value coming
2862 from outside the loop. */
2865 edge_initial_value_for_loop_phi (gimple phi
)
2869 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2871 edge e
= gimple_phi_arg_edge (phi
, i
);
2873 if (loop_depth (e
->src
->loop_father
)
2874 < loop_depth (e
->dest
->loop_father
))
2881 /* Return the argument of the loop PHI that is the inital value coming
2882 from outside the loop. */
2885 initial_value_for_loop_phi (gimple phi
)
2889 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2891 edge e
= gimple_phi_arg_edge (phi
, i
);
2893 if (loop_depth (e
->src
->loop_father
)
2894 < loop_depth (e
->dest
->loop_father
))
2895 return gimple_phi_arg_def (phi
, i
);
2901 /* Returns true when DEF is used outside the reduction cycle of
2905 used_outside_reduction (tree def
, gimple loop_phi
)
2907 use_operand_p use_p
;
2908 imm_use_iterator imm_iter
;
2909 loop_p loop
= loop_containing_stmt (loop_phi
);
2911 /* In LOOP, DEF should be used only in LOOP_PHI. */
2912 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
2914 gimple stmt
= USE_STMT (use_p
);
2916 if (stmt
!= loop_phi
2917 && !is_gimple_debug (stmt
)
2918 && flow_bb_inside_loop_p (loop
, gimple_bb (stmt
)))
2925 /* Detect commutative and associative scalar reductions belonging to
2926 the SCOP starting at the loop closed phi node STMT. Return the phi
2927 node of the reduction cycle, or NULL. */
2930 detect_commutative_reduction (scop_p scop
, gimple stmt
, VEC (gimple
, heap
) **in
,
2931 VEC (gimple
, heap
) **out
)
2933 if (scalar_close_phi_node_p (stmt
))
2935 gimple def
, loop_phi
, phi
, close_phi
= stmt
;
2936 tree init
, lhs
, arg
= gimple_phi_arg_def (close_phi
, 0);
2938 if (TREE_CODE (arg
) != SSA_NAME
)
2941 /* Note that loop close phi nodes should have a single argument
2942 because we translated the representation into a canonical form
2943 before Graphite: see canonicalize_loop_closed_ssa_form. */
2944 gcc_assert (gimple_phi_num_args (close_phi
) == 1);
2946 def
= SSA_NAME_DEF_STMT (arg
);
2947 if (!stmt_in_sese_p (def
, SCOP_REGION (scop
))
2948 || !(loop_phi
= detect_commutative_reduction (scop
, def
, in
, out
)))
2951 lhs
= gimple_phi_result (close_phi
);
2952 init
= initial_value_for_loop_phi (loop_phi
);
2953 phi
= follow_inital_value_to_phi (init
, lhs
);
2955 if (phi
&& (used_outside_reduction (lhs
, phi
)
2956 || !has_single_use (gimple_phi_result (phi
))))
2959 VEC_safe_push (gimple
, heap
, *in
, loop_phi
);
2960 VEC_safe_push (gimple
, heap
, *out
, close_phi
);
2964 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
2965 return detect_commutative_reduction_assign (stmt
, in
, out
);
2970 /* Translate the scalar reduction statement STMT to an array RED
2971 knowing that its recursive phi node is LOOP_PHI. */
2974 translate_scalar_reduction_to_array_for_stmt (scop_p scop
, tree red
,
2975 gimple stmt
, gimple loop_phi
)
2977 tree res
= gimple_phi_result (loop_phi
);
2978 gimple assign
= gimple_build_assign (res
, unshare_expr (red
));
2979 gimple_stmt_iterator gsi
;
2981 insert_stmts (scop
, assign
, NULL
, gsi_after_labels (gimple_bb (loop_phi
)));
2983 assign
= gimple_build_assign (unshare_expr (red
), gimple_assign_lhs (stmt
));
2984 gsi
= gsi_for_stmt (stmt
);
2986 insert_stmts (scop
, assign
, NULL
, gsi
);
2989 /* Removes the PHI node and resets all the debug stmts that are using
2993 remove_phi (gimple phi
)
2995 imm_use_iterator imm_iter
;
2997 use_operand_p use_p
;
2998 gimple_stmt_iterator gsi
;
2999 VEC (gimple
, heap
) *update
= VEC_alloc (gimple
, heap
, 3);
3003 def
= PHI_RESULT (phi
);
3004 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
3006 stmt
= USE_STMT (use_p
);
3008 if (is_gimple_debug (stmt
))
3010 gimple_debug_bind_reset_value (stmt
);
3011 VEC_safe_push (gimple
, heap
, update
, stmt
);
3015 FOR_EACH_VEC_ELT (gimple
, update
, i
, stmt
)
3018 VEC_free (gimple
, heap
, update
);
3020 gsi
= gsi_for_phi_node (phi
);
3021 remove_phi_node (&gsi
, false);
3024 /* Helper function for for_each_index. For each INDEX of the data
3025 reference REF, returns true when its indices are valid in the loop
3026 nest LOOP passed in as DATA. */
3029 dr_indices_valid_in_loop (tree ref ATTRIBUTE_UNUSED
, tree
*index
, void *data
)
3032 basic_block header
, def_bb
;
3035 if (TREE_CODE (*index
) != SSA_NAME
)
3038 loop
= *((loop_p
*) data
);
3039 header
= loop
->header
;
3040 stmt
= SSA_NAME_DEF_STMT (*index
);
3045 def_bb
= gimple_bb (stmt
);
3050 return dominated_by_p (CDI_DOMINATORS
, header
, def_bb
);
3053 /* When the result of a CLOSE_PHI is written to a memory location,
3054 return a pointer to that memory reference, otherwise return
3058 close_phi_written_to_memory (gimple close_phi
)
3060 imm_use_iterator imm_iter
;
3061 use_operand_p use_p
;
3063 tree res
, def
= gimple_phi_result (close_phi
);
3065 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
3066 if ((stmt
= USE_STMT (use_p
))
3067 && gimple_code (stmt
) == GIMPLE_ASSIGN
3068 && (res
= gimple_assign_lhs (stmt
)))
3070 switch (TREE_CODE (res
))
3080 tree arg
= gimple_phi_arg_def (close_phi
, 0);
3081 loop_p nest
= loop_containing_stmt (SSA_NAME_DEF_STMT (arg
));
3083 /* FIXME: this restriction is for id-{24,25}.f and
3084 could be handled by duplicating the computation of
3085 array indices before the loop of the close_phi. */
3086 if (for_each_index (&res
, dr_indices_valid_in_loop
, &nest
))
3098 /* Rewrite out of SSA the reduction described by the loop phi nodes
3099 IN, and the close phi nodes OUT. IN and OUT are structured by loop
3102 IN: stmt, loop_n, ..., loop_0
3103 OUT: stmt, close_n, ..., close_0
3105 the first element is the reduction statement, and the next elements
3106 are the loop and close phi nodes of each of the outer loops. */
3109 translate_scalar_reduction_to_array (scop_p scop
,
3110 VEC (gimple
, heap
) *in
,
3111 VEC (gimple
, heap
) *out
)
3114 unsigned int i
= VEC_length (gimple
, out
) - 1;
3115 tree red
= close_phi_written_to_memory (VEC_index (gimple
, out
, i
));
3117 FOR_EACH_VEC_ELT (gimple
, in
, i
, loop_phi
)
3119 gimple close_phi
= VEC_index (gimple
, out
, i
);
3123 gimple stmt
= loop_phi
;
3124 basic_block bb
= split_reduction_stmt (scop
, stmt
);
3125 poly_bb_p pbb
= pbb_from_bb (bb
);
3126 PBB_IS_REDUCTION (pbb
) = true;
3127 gcc_assert (close_phi
== loop_phi
);
3130 red
= create_zero_dim_array
3131 (gimple_assign_lhs (stmt
), "Commutative_Associative_Reduction");
3133 translate_scalar_reduction_to_array_for_stmt
3134 (scop
, red
, stmt
, VEC_index (gimple
, in
, 1));
3138 if (i
== VEC_length (gimple
, in
) - 1)
3140 insert_out_of_ssa_copy (scop
, gimple_phi_result (close_phi
),
3141 unshare_expr (red
), close_phi
);
3142 insert_out_of_ssa_copy_on_edge
3143 (scop
, edge_initial_value_for_loop_phi (loop_phi
),
3144 unshare_expr (red
), initial_value_for_loop_phi (loop_phi
));
3147 remove_phi (loop_phi
);
3148 remove_phi (close_phi
);
3152 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. Returns
3153 true when something has been changed. */
3156 rewrite_commutative_reductions_out_of_ssa_close_phi (scop_p scop
,
3160 VEC (gimple
, heap
) *in
= VEC_alloc (gimple
, heap
, 10);
3161 VEC (gimple
, heap
) *out
= VEC_alloc (gimple
, heap
, 10);
3163 detect_commutative_reduction (scop
, close_phi
, &in
, &out
);
3164 res
= VEC_length (gimple
, in
) > 1;
3166 translate_scalar_reduction_to_array (scop
, in
, out
);
3168 VEC_free (gimple
, heap
, in
);
3169 VEC_free (gimple
, heap
, out
);
3173 /* Rewrites all the commutative reductions from LOOP out of SSA.
3174 Returns true when something has been changed. */
3177 rewrite_commutative_reductions_out_of_ssa_loop (scop_p scop
,
3180 gimple_stmt_iterator gsi
;
3181 edge exit
= single_exit (loop
);
3183 bool changed
= false;
3188 for (gsi
= gsi_start_phis (exit
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3189 if ((res
= gimple_phi_result (gsi_stmt (gsi
)))
3190 && is_gimple_reg (res
)
3191 && !scev_analyzable_p (res
, SCOP_REGION (scop
)))
3192 changed
|= rewrite_commutative_reductions_out_of_ssa_close_phi
3193 (scop
, gsi_stmt (gsi
));
3198 /* Rewrites all the commutative reductions from SCOP out of SSA. */
3201 rewrite_commutative_reductions_out_of_ssa (scop_p scop
)
3205 bool changed
= false;
3206 sese region
= SCOP_REGION (scop
);
3208 FOR_EACH_LOOP (li
, loop
, 0)
3209 if (loop_in_sese_p (loop
, region
))
3210 changed
|= rewrite_commutative_reductions_out_of_ssa_loop (scop
, loop
);
3215 gsi_commit_edge_inserts ();
3216 update_ssa (TODO_update_ssa
);
3217 #ifdef ENABLE_CHECKING
3218 verify_loop_closed_ssa (true);
3223 /* Java does not initialize long_long_integer_type_node. */
3224 #define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
3226 /* Can all ivs be represented by a signed integer?
3227 As CLooG might generate negative values in its expressions, signed loop ivs
3228 are required in the backend. */
3231 scop_ivs_can_be_represented (scop_p scop
)
3235 gimple_stmt_iterator psi
;
3237 FOR_EACH_LOOP (li
, loop
, 0)
3239 if (!loop_in_sese_p (loop
, SCOP_REGION (scop
)))
3242 for (psi
= gsi_start_phis (loop
->header
);
3243 !gsi_end_p (psi
); gsi_next (&psi
))
3245 gimple phi
= gsi_stmt (psi
);
3246 tree res
= PHI_RESULT (phi
);
3247 tree type
= TREE_TYPE (res
);
3249 if (TYPE_UNSIGNED (type
)
3250 && TYPE_PRECISION (type
) >= TYPE_PRECISION (my_long_long
))
3260 /* Builds the polyhedral representation for a SESE region. */
3263 build_poly_scop (scop_p scop
)
3265 sese region
= SCOP_REGION (scop
);
3266 graphite_dim_t max_dim
;
3268 build_scop_bbs (scop
);
3270 /* FIXME: This restriction is needed to avoid a problem in CLooG.
3271 Once CLooG is fixed, remove this guard. Anyways, it makes no
3272 sense to optimize a scop containing only PBBs that do not belong
3274 if (nb_pbbs_in_loops (scop
) == 0)
3277 if (!scop_ivs_can_be_represented (scop
))
3280 if (flag_associative_math
)
3281 rewrite_commutative_reductions_out_of_ssa (scop
);
3283 build_sese_loop_nests (region
);
3284 build_sese_conditions (region
);
3285 find_scop_parameters (scop
);
3287 max_dim
= PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS
);
3288 if (scop_nb_params (scop
) > max_dim
)
3291 build_scop_iteration_domain (scop
);
3292 build_scop_context (scop
);
3293 add_conditions_to_constraints (scop
);
3295 /* Rewrite out of SSA only after having translated the
3296 representation to the polyhedral representation to avoid scev
3297 analysis failures. That means that these functions will insert
3298 new data references that they create in the right place. */
3299 rewrite_reductions_out_of_ssa (scop
);
3300 rewrite_cross_bb_scalar_deps_out_of_ssa (scop
);
3302 build_scop_drs (scop
);
3304 build_scop_scattering (scop
);
3306 /* This SCoP has been translated to the polyhedral
3308 POLY_SCOP_P (scop
) = true;