1 /* Conversion of SESE regions to Polyhedra.
2 Copyright (C) 2009, 2010 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"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
32 #include "tree-dump.h"
35 #include "tree-chrec.h"
36 #include "tree-data-ref.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-pass.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
47 #include "graphite-ppl.h"
49 #include "graphite-poly.h"
50 #include "graphite-scop-detection.h"
51 #include "graphite-sese-to-poly.h"
53 /* Check if VAR is used in a phi node, that is no loop header. */
56 var_used_in_not_loop_header_phi_node (tree var
)
58 imm_use_iterator imm_iter
;
62 FOR_EACH_IMM_USE_STMT (stmt
, imm_iter
, var
)
64 basic_block bb
= gimple_bb (stmt
);
66 if (gimple_code (stmt
) == GIMPLE_PHI
67 && bb
->loop_father
->header
!= bb
)
74 /* Returns the index of the PHI argument defined in the outermost
78 phi_arg_in_outermost_loop (gimple phi
)
80 loop_p loop
= gimple_bb (phi
)->loop_father
;
83 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
84 if (!flow_bb_inside_loop_p (loop
, gimple_phi_arg_edge (phi
, i
)->src
))
86 loop
= gimple_phi_arg_edge (phi
, i
)->src
->loop_father
;
93 /* Removes a simple copy phi node "RES = phi (INIT, RES)" at position
94 PSI by inserting on the loop ENTRY edge assignment "RES = INIT". */
97 remove_simple_copy_phi (gimple_stmt_iterator
*psi
)
99 gimple phi
= gsi_stmt (*psi
);
100 tree res
= gimple_phi_result (phi
);
101 size_t entry
= phi_arg_in_outermost_loop (phi
);
102 tree init
= gimple_phi_arg_def (phi
, entry
);
103 gimple stmt
= gimple_build_assign (res
, init
);
104 edge e
= gimple_phi_arg_edge (phi
, entry
);
106 remove_phi_node (psi
, false);
107 gsi_insert_on_edge_immediate (e
, stmt
);
108 SSA_NAME_DEF_STMT (res
) = stmt
;
111 /* Removes an invariant phi node at position PSI by inserting on the
112 loop ENTRY edge the assignment RES = INIT. */
115 remove_invariant_phi (sese region
, gimple_stmt_iterator
*psi
)
117 gimple phi
= gsi_stmt (*psi
);
118 loop_p loop
= loop_containing_stmt (phi
);
119 tree res
= gimple_phi_result (phi
);
120 tree scev
= scalar_evolution_in_region (region
, loop
, res
);
121 size_t entry
= phi_arg_in_outermost_loop (phi
);
122 edge e
= gimple_phi_arg_edge (phi
, entry
);
126 gimple_stmt_iterator gsi
;
128 if (tree_contains_chrecs (scev
, NULL
))
129 scev
= gimple_phi_arg_def (phi
, entry
);
131 var
= force_gimple_operand (scev
, &stmts
, true, NULL_TREE
);
132 stmt
= gimple_build_assign (res
, var
);
133 remove_phi_node (psi
, false);
136 stmts
= gimple_seq_alloc ();
138 gsi
= gsi_last (stmts
);
139 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
140 gsi_insert_seq_on_edge (e
, stmts
);
141 gsi_commit_edge_inserts ();
142 SSA_NAME_DEF_STMT (res
) = stmt
;
145 /* Returns true when the phi node at PSI is of the form "a = phi (a, x)". */
148 simple_copy_phi_p (gimple phi
)
152 if (gimple_phi_num_args (phi
) != 2)
155 res
= gimple_phi_result (phi
);
156 return (res
== gimple_phi_arg_def (phi
, 0)
157 || res
== gimple_phi_arg_def (phi
, 1));
160 /* Returns true when the phi node at position PSI is a reduction phi
161 node in REGION. Otherwise moves the pointer PSI to the next phi to
165 reduction_phi_p (sese region
, gimple_stmt_iterator
*psi
)
168 gimple phi
= gsi_stmt (*psi
);
169 tree res
= gimple_phi_result (phi
);
171 loop
= loop_containing_stmt (phi
);
173 if (simple_copy_phi_p (phi
))
175 /* PRE introduces phi nodes like these, for an example,
176 see id-5.f in the fortran graphite testsuite:
178 # prephitmp.85_265 = PHI <prephitmp.85_258(33), prephitmp.85_265(18)>
180 remove_simple_copy_phi (psi
);
184 if (scev_analyzable_p (res
, region
))
186 tree scev
= scalar_evolution_in_region (region
, loop
, res
);
188 if (evolution_function_is_invariant_p (scev
, loop
->num
))
189 remove_invariant_phi (region
, psi
);
196 /* All the other cases are considered reductions. */
200 /* Returns true when BB will be represented in graphite. Return false
201 for the basic blocks that contain code eliminated in the code
202 generation pass: i.e. induction variables and exit conditions. */
205 graphite_stmt_p (sese region
, basic_block bb
,
206 VEC (data_reference_p
, heap
) *drs
)
208 gimple_stmt_iterator gsi
;
209 loop_p loop
= bb
->loop_father
;
211 if (VEC_length (data_reference_p
, drs
) > 0)
214 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
216 gimple stmt
= gsi_stmt (gsi
);
218 switch (gimple_code (stmt
))
221 /* Control flow expressions can be ignored, as they are
222 represented in the iteration domains and will be
223 regenerated by graphite. */
231 tree var
= gimple_assign_lhs (stmt
);
233 /* We need these bbs to be able to construct the phi nodes. */
234 if (var_used_in_not_loop_header_phi_node (var
))
237 var
= scalar_evolution_in_region (region
, loop
, var
);
238 if (chrec_contains_undetermined (var
))
252 /* Store the GRAPHITE representation of BB. */
255 new_gimple_bb (basic_block bb
, VEC (data_reference_p
, heap
) *drs
)
257 struct gimple_bb
*gbb
;
259 gbb
= XNEW (struct gimple_bb
);
262 GBB_DATA_REFS (gbb
) = drs
;
263 GBB_CONDITIONS (gbb
) = NULL
;
264 GBB_CONDITION_CASES (gbb
) = NULL
;
270 free_data_refs_aux (VEC (data_reference_p
, heap
) *datarefs
)
273 struct data_reference
*dr
;
275 FOR_EACH_VEC_ELT (data_reference_p
, datarefs
, i
, dr
)
278 base_alias_pair
*bap
= (base_alias_pair
*)(dr
->aux
);
281 free (bap
->alias_set
);
290 free_gimple_bb (struct gimple_bb
*gbb
)
292 free_data_refs_aux (GBB_DATA_REFS (gbb
));
293 free_data_refs (GBB_DATA_REFS (gbb
));
295 VEC_free (gimple
, heap
, GBB_CONDITIONS (gbb
));
296 VEC_free (gimple
, heap
, GBB_CONDITION_CASES (gbb
));
297 GBB_BB (gbb
)->aux
= 0;
301 /* Deletes all gimple bbs in SCOP. */
304 remove_gbbs_in_scop (scop_p scop
)
309 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
310 free_gimple_bb (PBB_BLACK_BOX (pbb
));
313 /* Deletes all scops in SCOPS. */
316 free_scops (VEC (scop_p
, heap
) *scops
)
321 FOR_EACH_VEC_ELT (scop_p
, scops
, i
, scop
)
323 remove_gbbs_in_scop (scop
);
324 free_sese (SCOP_REGION (scop
));
328 VEC_free (scop_p
, heap
, scops
);
331 /* Generates a polyhedral black box only if the bb contains interesting
335 try_generate_gimple_bb (scop_p scop
, basic_block bb
, sbitmap reductions
)
337 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 5);
338 loop_p nest
= outermost_loop_in_sese (SCOP_REGION (scop
), bb
);
339 gimple_stmt_iterator gsi
;
341 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
343 gimple stmt
= gsi_stmt (gsi
);
344 if (!is_gimple_debug (stmt
))
345 graphite_find_data_references_in_stmt (nest
, stmt
, &drs
);
348 if (!graphite_stmt_p (SCOP_REGION (scop
), bb
, drs
))
349 free_data_refs (drs
);
351 new_poly_bb (scop
, new_gimple_bb (bb
, drs
), TEST_BIT (reductions
,
355 /* Returns true if all predecessors of BB, that are not dominated by BB, are
356 marked in MAP. The predecessors dominated by BB are loop latches and will
357 be handled after BB. */
360 all_non_dominated_preds_marked_p (basic_block bb
, sbitmap map
)
365 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
366 if (!TEST_BIT (map
, e
->src
->index
)
367 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
373 /* Compare the depth of two basic_block's P1 and P2. */
376 compare_bb_depths (const void *p1
, const void *p2
)
378 const_basic_block
const bb1
= *(const_basic_block
const*)p1
;
379 const_basic_block
const bb2
= *(const_basic_block
const*)p2
;
380 int d1
= loop_depth (bb1
->loop_father
);
381 int d2
= loop_depth (bb2
->loop_father
);
392 /* Sort the basic blocks from DOM such that the first are the ones at
393 a deepest loop level. */
396 graphite_sort_dominated_info (VEC (basic_block
, heap
) *dom
)
398 VEC_qsort (basic_block
, dom
, compare_bb_depths
);
401 /* Recursive helper function for build_scops_bbs. */
404 build_scop_bbs_1 (scop_p scop
, sbitmap visited
, basic_block bb
, sbitmap reductions
)
406 sese region
= SCOP_REGION (scop
);
407 VEC (basic_block
, heap
) *dom
;
409 if (TEST_BIT (visited
, bb
->index
)
410 || !bb_in_sese_p (bb
, region
))
413 try_generate_gimple_bb (scop
, bb
, reductions
);
414 SET_BIT (visited
, bb
->index
);
416 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
421 graphite_sort_dominated_info (dom
);
423 while (!VEC_empty (basic_block
, dom
))
428 FOR_EACH_VEC_ELT (basic_block
, dom
, i
, dom_bb
)
429 if (all_non_dominated_preds_marked_p (dom_bb
, visited
))
431 build_scop_bbs_1 (scop
, visited
, dom_bb
, reductions
);
432 VEC_unordered_remove (basic_block
, dom
, i
);
437 VEC_free (basic_block
, heap
, dom
);
440 /* Gather the basic blocks belonging to the SCOP. */
443 build_scop_bbs (scop_p scop
, sbitmap reductions
)
445 sbitmap visited
= sbitmap_alloc (last_basic_block
);
446 sese region
= SCOP_REGION (scop
);
448 sbitmap_zero (visited
);
449 build_scop_bbs_1 (scop
, visited
, SESE_ENTRY_BB (region
), reductions
);
450 sbitmap_free (visited
);
453 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
454 We generate SCATTERING_DIMENSIONS scattering dimensions.
456 CLooG 0.15.0 and previous versions require, that all
457 scattering functions of one CloogProgram have the same number of
458 scattering dimensions, therefore we allow to specify it. This
459 should be removed in future versions of CLooG.
461 The scattering polyhedron consists of these dimensions: scattering,
462 loop_iterators, parameters.
466 | scattering_dimensions = 5
467 | used_scattering_dimensions = 3
475 | Scattering polyhedron:
477 | scattering: {s1, s2, s3, s4, s5}
478 | loop_iterators: {i}
479 | parameters: {p1, p2}
481 | s1 s2 s3 s4 s5 i p1 p2 1
482 | 1 0 0 0 0 0 0 0 -4 = 0
483 | 0 1 0 0 0 -1 0 0 0 = 0
484 | 0 0 1 0 0 0 0 0 -5 = 0 */
487 build_pbb_scattering_polyhedrons (ppl_Linear_Expression_t static_schedule
,
488 poly_bb_p pbb
, int scattering_dimensions
)
491 scop_p scop
= PBB_SCOP (pbb
);
492 int nb_iterators
= pbb_dim_iter_domain (pbb
);
493 int used_scattering_dimensions
= nb_iterators
* 2 + 1;
494 int nb_params
= scop_nb_params (scop
);
496 ppl_dimension_type dim
= scattering_dimensions
+ nb_iterators
+ nb_params
;
499 gcc_assert (scattering_dimensions
>= used_scattering_dimensions
);
502 ppl_new_Coefficient (&c
);
503 PBB_TRANSFORMED (pbb
) = poly_scattering_new ();
504 ppl_new_C_Polyhedron_from_space_dimension
505 (&PBB_TRANSFORMED_SCATTERING (pbb
), dim
, 0);
507 PBB_NB_SCATTERING_TRANSFORM (pbb
) = scattering_dimensions
;
509 for (i
= 0; i
< scattering_dimensions
; i
++)
511 ppl_Constraint_t cstr
;
512 ppl_Linear_Expression_t expr
;
514 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
516 ppl_assign_Coefficient_from_mpz_t (c
, v
);
517 ppl_Linear_Expression_add_to_coefficient (expr
, i
, c
);
519 /* Textual order inside this loop. */
522 ppl_Linear_Expression_coefficient (static_schedule
, i
/ 2, c
);
523 ppl_Coefficient_to_mpz_t (c
, v
);
525 ppl_assign_Coefficient_from_mpz_t (c
, v
);
526 ppl_Linear_Expression_add_to_inhomogeneous (expr
, c
);
529 /* Iterations of this loop. */
530 else /* if ((i % 2) == 1) */
532 int loop
= (i
- 1) / 2;
535 ppl_assign_Coefficient_from_mpz_t (c
, v
);
536 ppl_Linear_Expression_add_to_coefficient
537 (expr
, scattering_dimensions
+ loop
, c
);
540 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
541 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb
), cstr
);
542 ppl_delete_Linear_Expression (expr
);
543 ppl_delete_Constraint (cstr
);
547 ppl_delete_Coefficient (c
);
549 PBB_ORIGINAL (pbb
) = poly_scattering_copy (PBB_TRANSFORMED (pbb
));
552 /* Build for BB the static schedule.
554 The static schedule is a Dewey numbering of the abstract syntax
555 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
557 The following example informally defines the static schedule:
576 Static schedules for A to F:
589 build_scop_scattering (scop_p scop
)
593 gimple_bb_p previous_gbb
= NULL
;
594 ppl_Linear_Expression_t static_schedule
;
599 ppl_new_Coefficient (&c
);
600 ppl_new_Linear_Expression (&static_schedule
);
602 /* We have to start schedules at 0 on the first component and
603 because we cannot compare_prefix_loops against a previous loop,
604 prefix will be equal to zero, and that index will be
605 incremented before copying. */
607 ppl_assign_Coefficient_from_mpz_t (c
, v
);
608 ppl_Linear_Expression_add_to_coefficient (static_schedule
, 0, c
);
610 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
612 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
613 ppl_Linear_Expression_t common
;
615 int nb_scat_dims
= pbb_dim_iter_domain (pbb
) * 2 + 1;
618 prefix
= nb_common_loops (SCOP_REGION (scop
), previous_gbb
, gbb
);
623 ppl_new_Linear_Expression_with_dimension (&common
, prefix
+ 1);
624 ppl_assign_Linear_Expression_from_Linear_Expression (common
,
628 ppl_assign_Coefficient_from_mpz_t (c
, v
);
629 ppl_Linear_Expression_add_to_coefficient (common
, prefix
, c
);
630 ppl_assign_Linear_Expression_from_Linear_Expression (static_schedule
,
633 build_pbb_scattering_polyhedrons (common
, pbb
, nb_scat_dims
);
635 ppl_delete_Linear_Expression (common
);
639 ppl_delete_Coefficient (c
);
640 ppl_delete_Linear_Expression (static_schedule
);
643 /* Add the value K to the dimension D of the linear expression EXPR. */
646 add_value_to_dim (ppl_dimension_type d
, ppl_Linear_Expression_t expr
,
650 ppl_Coefficient_t coef
;
652 ppl_new_Coefficient (&coef
);
653 ppl_Linear_Expression_coefficient (expr
, d
, coef
);
655 ppl_Coefficient_to_mpz_t (coef
, val
);
657 mpz_add (val
, val
, k
);
659 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
660 ppl_Linear_Expression_add_to_coefficient (expr
, d
, coef
);
662 ppl_delete_Coefficient (coef
);
665 /* In the context of scop S, scan E, the right hand side of a scalar
666 evolution function in loop VAR, and translate it to a linear
670 scan_tree_for_params_right_scev (sese s
, tree e
, int var
,
671 ppl_Linear_Expression_t expr
)
675 loop_p loop
= get_loop (var
);
676 ppl_dimension_type l
= sese_loop_depth (s
, loop
) - 1;
679 /* Scalar evolutions should happen in the sese region. */
680 gcc_assert (sese_loop_depth (s
, loop
) > 0);
682 /* We can not deal with parametric strides like:
688 gcc_assert (TREE_CODE (e
) == INTEGER_CST
);
691 mpz_set_si (val
, int_cst_value (e
));
692 add_value_to_dim (l
, expr
, val
);
697 /* Scan the integer constant CST, and add it to the inhomogeneous part of the
698 linear expression EXPR. K is the multiplier of the constant. */
701 scan_tree_for_params_int (tree cst
, ppl_Linear_Expression_t expr
, mpz_t k
)
704 ppl_Coefficient_t coef
;
705 int v
= int_cst_value (cst
);
710 /* Necessary to not get "-1 = 2^n - 1". */
712 mpz_sub_ui (val
, val
, -v
);
714 mpz_add_ui (val
, val
, v
);
716 mpz_mul (val
, val
, k
);
717 ppl_new_Coefficient (&coef
);
718 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
719 ppl_Linear_Expression_add_to_inhomogeneous (expr
, coef
);
721 ppl_delete_Coefficient (coef
);
724 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
725 Otherwise returns -1. */
728 parameter_index_in_region_1 (tree name
, sese region
)
733 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
735 FOR_EACH_VEC_ELT (tree
, SESE_PARAMS (region
), i
, p
)
742 /* When the parameter NAME is in REGION, returns its index in
743 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
744 and returns the index of NAME. */
747 parameter_index_in_region (tree name
, sese region
)
751 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
753 i
= parameter_index_in_region_1 (name
, region
);
757 gcc_assert (SESE_ADD_PARAMS (region
));
759 i
= VEC_length (tree
, SESE_PARAMS (region
));
760 VEC_safe_push (tree
, heap
, SESE_PARAMS (region
), name
);
764 /* In the context of sese S, scan the expression E and translate it to
765 a linear expression C. When parsing a symbolic multiplication, K
766 represents the constant multiplier of an expression containing
770 scan_tree_for_params (sese s
, tree e
, ppl_Linear_Expression_t c
,
773 if (e
== chrec_dont_know
)
776 switch (TREE_CODE (e
))
778 case POLYNOMIAL_CHREC
:
779 scan_tree_for_params_right_scev (s
, CHREC_RIGHT (e
),
780 CHREC_VARIABLE (e
), c
);
781 scan_tree_for_params (s
, CHREC_LEFT (e
), c
, k
);
785 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
790 gcc_assert (host_integerp (TREE_OPERAND (e
, 1), 0));
792 mpz_set_si (val
, int_cst_value (TREE_OPERAND (e
, 1)));
793 mpz_mul (val
, val
, k
);
794 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, val
);
798 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
805 gcc_assert (host_integerp (TREE_OPERAND (e
, 0), 0));
807 mpz_set_si (val
, int_cst_value (TREE_OPERAND (e
, 0)));
808 mpz_mul (val
, val
, k
);
809 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, val
);
813 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
818 case POINTER_PLUS_EXPR
:
819 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
820 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
825 ppl_Linear_Expression_t tmp_expr
= NULL
;
829 ppl_dimension_type dim
;
830 ppl_Linear_Expression_space_dimension (c
, &dim
);
831 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
834 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
835 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), tmp_expr
, k
);
839 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
841 ppl_delete_Linear_Expression (tmp_expr
);
849 ppl_Linear_Expression_t tmp_expr
= NULL
;
853 ppl_dimension_type dim
;
854 ppl_Linear_Expression_space_dimension (c
, &dim
);
855 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
858 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
862 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
864 ppl_delete_Linear_Expression (tmp_expr
);
872 ppl_Linear_Expression_t tmp_expr
= NULL
;
876 ppl_dimension_type dim
;
877 ppl_Linear_Expression_space_dimension (c
, &dim
);
878 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
881 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
885 ppl_Coefficient_t coef
;
888 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
890 ppl_delete_Linear_Expression (tmp_expr
);
891 mpz_init (minus_one
);
892 mpz_set_si (minus_one
, -1);
893 ppl_new_Coefficient_from_mpz_t (&coef
, minus_one
);
894 ppl_Linear_Expression_add_to_inhomogeneous (c
, coef
);
895 mpz_clear (minus_one
);
896 ppl_delete_Coefficient (coef
);
904 ppl_dimension_type p
= parameter_index_in_region (e
, s
);
908 ppl_dimension_type dim
;
909 ppl_Linear_Expression_space_dimension (c
, &dim
);
910 p
+= dim
- sese_nb_params (s
);
911 add_value_to_dim (p
, c
, k
);
918 scan_tree_for_params_int (e
, c
, k
);
922 case NON_LVALUE_EXPR
:
923 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
932 /* Find parameters with respect to REGION in BB. We are looking in memory
933 access functions, conditions and loop bounds. */
936 find_params_in_bb (sese region
, gimple_bb_p gbb
)
942 loop_p loop
= GBB_BB (gbb
)->loop_father
;
948 /* Find parameters in the access functions of data references. */
949 FOR_EACH_VEC_ELT (data_reference_p
, GBB_DATA_REFS (gbb
), i
, dr
)
950 for (j
= 0; j
< DR_NUM_DIMENSIONS (dr
); j
++)
951 scan_tree_for_params (region
, DR_ACCESS_FN (dr
, j
), NULL
, one
);
953 /* Find parameters in conditional statements. */
954 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
956 tree lhs
= scalar_evolution_in_region (region
, loop
,
957 gimple_cond_lhs (stmt
));
958 tree rhs
= scalar_evolution_in_region (region
, loop
,
959 gimple_cond_rhs (stmt
));
961 scan_tree_for_params (region
, lhs
, NULL
, one
);
962 scan_tree_for_params (region
, rhs
, NULL
, one
);
968 /* Record the parameters used in the SCOP. A variable is a parameter
969 in a scop if it does not vary during the execution of that scop. */
972 find_scop_parameters (scop_p scop
)
976 sese region
= SCOP_REGION (scop
);
983 /* Find the parameters used in the loop bounds. */
984 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
986 tree nb_iters
= number_of_latch_executions (loop
);
988 if (!chrec_contains_symbols (nb_iters
))
991 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
992 scan_tree_for_params (region
, nb_iters
, NULL
, one
);
997 /* Find the parameters used in data accesses. */
998 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
999 find_params_in_bb (region
, PBB_BLACK_BOX (pbb
));
1001 scop_set_nb_params (scop
, sese_nb_params (region
));
1002 SESE_ADD_PARAMS (region
) = false;
1004 ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension
1005 (&SCOP_CONTEXT (scop
), scop_nb_params (scop
), 0);
1008 /* Returns a gimple_bb from BB. */
1010 static inline gimple_bb_p
1011 gbb_from_bb (basic_block bb
)
1013 return (gimple_bb_p
) bb
->aux
;
1016 /* Insert in the SCOP context constraints from the estimation of the
1017 number of iterations. UB_EXPR is a linear expression describing
1018 the number of iterations in a loop. This expression is bounded by
1019 the estimation NIT. */
1022 add_upper_bounds_from_estimated_nit (scop_p scop
, double_int nit
,
1023 ppl_dimension_type dim
,
1024 ppl_Linear_Expression_t ub_expr
)
1027 ppl_Linear_Expression_t nb_iters_le
;
1028 ppl_Polyhedron_t pol
;
1029 ppl_Coefficient_t coef
;
1030 ppl_Constraint_t ub
;
1032 ppl_new_C_Polyhedron_from_space_dimension (&pol
, dim
, 0);
1033 ppl_new_Linear_Expression_from_Linear_Expression (&nb_iters_le
,
1036 /* Construct the negated number of last iteration in VAL. */
1038 mpz_set_double_int (val
, nit
, false);
1039 mpz_sub_ui (val
, val
, 1);
1042 /* NB_ITERS_LE holds the number of last iteration in
1043 parametrical form. Subtract estimated number of last
1044 iteration and assert that result is not positive. */
1045 ppl_new_Coefficient_from_mpz_t (&coef
, val
);
1046 ppl_Linear_Expression_add_to_inhomogeneous (nb_iters_le
, coef
);
1047 ppl_delete_Coefficient (coef
);
1048 ppl_new_Constraint (&ub
, nb_iters_le
,
1049 PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
1050 ppl_Polyhedron_add_constraint (pol
, ub
);
1052 /* Remove all but last GDIM dimensions from POL to obtain
1053 only the constraints on the parameters. */
1055 graphite_dim_t gdim
= scop_nb_params (scop
);
1056 ppl_dimension_type
*dims
= XNEWVEC (ppl_dimension_type
, dim
- gdim
);
1059 for (i
= 0; i
< dim
- gdim
; i
++)
1062 ppl_Polyhedron_remove_space_dimensions (pol
, dims
, dim
- gdim
);
1066 /* Add the constraints on the parameters to the SCoP context. */
1068 ppl_Pointset_Powerset_C_Polyhedron_t constraints_ps
;
1070 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1071 (&constraints_ps
, pol
);
1072 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1073 (SCOP_CONTEXT (scop
), constraints_ps
);
1074 ppl_delete_Pointset_Powerset_C_Polyhedron (constraints_ps
);
1077 ppl_delete_Polyhedron (pol
);
1078 ppl_delete_Linear_Expression (nb_iters_le
);
1079 ppl_delete_Constraint (ub
);
1083 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
1084 the constraints for the surrounding loops. */
1087 build_loop_iteration_domains (scop_p scop
, struct loop
*loop
,
1088 ppl_Polyhedron_t outer_ph
, int nb
,
1089 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
)
1092 ppl_Polyhedron_t ph
;
1093 tree nb_iters
= number_of_latch_executions (loop
);
1094 ppl_dimension_type dim
= nb
+ 1 + scop_nb_params (scop
);
1095 sese region
= SCOP_REGION (scop
);
1098 ppl_const_Constraint_System_t pcs
;
1099 ppl_dimension_type
*map
1100 = (ppl_dimension_type
*) XNEWVEC (ppl_dimension_type
, dim
);
1102 ppl_new_C_Polyhedron_from_space_dimension (&ph
, dim
, 0);
1103 ppl_Polyhedron_get_constraints (outer_ph
, &pcs
);
1104 ppl_Polyhedron_add_constraints (ph
, pcs
);
1106 for (i
= 0; i
< (int) nb
; i
++)
1108 for (i
= (int) nb
; i
< (int) dim
- 1; i
++)
1112 ppl_Polyhedron_map_space_dimensions (ph
, map
, dim
);
1118 ppl_Constraint_t lb
;
1119 ppl_Linear_Expression_t lb_expr
;
1121 ppl_new_Linear_Expression_with_dimension (&lb_expr
, dim
);
1122 ppl_set_coef (lb_expr
, nb
, 1);
1123 ppl_new_Constraint (&lb
, lb_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1124 ppl_delete_Linear_Expression (lb_expr
);
1125 ppl_Polyhedron_add_constraint (ph
, lb
);
1126 ppl_delete_Constraint (lb
);
1129 if (TREE_CODE (nb_iters
) == INTEGER_CST
)
1131 ppl_Constraint_t ub
;
1132 ppl_Linear_Expression_t ub_expr
;
1134 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1136 /* loop_i <= cst_nb_iters */
1137 ppl_set_coef (ub_expr
, nb
, -1);
1138 ppl_set_inhomogeneous_tree (ub_expr
, nb_iters
);
1139 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1140 ppl_Polyhedron_add_constraint (ph
, ub
);
1141 ppl_delete_Linear_Expression (ub_expr
);
1142 ppl_delete_Constraint (ub
);
1144 else if (!chrec_contains_undetermined (nb_iters
))
1147 ppl_Constraint_t ub
;
1148 ppl_Linear_Expression_t ub_expr
;
1152 mpz_set_si (one
, 1);
1153 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1154 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
1155 scan_tree_for_params (SCOP_REGION (scop
), nb_iters
, ub_expr
, one
);
1158 if (estimated_loop_iterations (loop
, true, &nit
))
1159 add_upper_bounds_from_estimated_nit (scop
, nit
, dim
, ub_expr
);
1161 /* loop_i <= expr_nb_iters */
1162 ppl_set_coef (ub_expr
, nb
, -1);
1163 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1164 ppl_Polyhedron_add_constraint (ph
, ub
);
1165 ppl_delete_Linear_Expression (ub_expr
);
1166 ppl_delete_Constraint (ub
);
1171 if (loop
->inner
&& loop_in_sese_p (loop
->inner
, region
))
1172 build_loop_iteration_domains (scop
, loop
->inner
, ph
, nb
+ 1, domains
);
1176 && loop_in_sese_p (loop
->next
, region
))
1177 build_loop_iteration_domains (scop
, loop
->next
, outer_ph
, nb
, domains
);
1179 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1180 (&domains
[loop
->num
], ph
);
1182 ppl_delete_Polyhedron (ph
);
1185 /* Returns a linear expression for tree T evaluated in PBB. */
1187 static ppl_Linear_Expression_t
1188 create_linear_expr_from_tree (poly_bb_p pbb
, tree t
)
1191 ppl_Linear_Expression_t res
;
1192 ppl_dimension_type dim
;
1193 sese region
= SCOP_REGION (PBB_SCOP (pbb
));
1194 loop_p loop
= pbb_loop (pbb
);
1196 dim
= pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
1197 ppl_new_Linear_Expression_with_dimension (&res
, dim
);
1199 t
= scalar_evolution_in_region (region
, loop
, t
);
1200 gcc_assert (!automatically_generated_chrec_p (t
));
1203 mpz_set_si (one
, 1);
1204 scan_tree_for_params (region
, t
, res
, one
);
1210 /* Returns the ppl constraint type from the gimple tree code CODE. */
1212 static enum ppl_enum_Constraint_Type
1213 ppl_constraint_type_from_tree_code (enum tree_code code
)
1217 /* We do not support LT and GT to be able to work with C_Polyhedron.
1218 As we work on integer polyhedron "a < b" can be expressed by
1225 return PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
;
1228 return PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
;
1231 return PPL_CONSTRAINT_TYPE_EQUAL
;
1238 /* Add conditional statement STMT to PS. It is evaluated in PBB and
1239 CODE is used as the comparison operator. This allows us to invert the
1240 condition or to handle inequalities. */
1243 add_condition_to_domain (ppl_Pointset_Powerset_C_Polyhedron_t ps
, gimple stmt
,
1244 poly_bb_p pbb
, enum tree_code code
)
1247 ppl_Coefficient_t c
;
1248 ppl_Linear_Expression_t left
, right
;
1249 ppl_Constraint_t cstr
;
1250 enum ppl_enum_Constraint_Type type
;
1252 left
= create_linear_expr_from_tree (pbb
, gimple_cond_lhs (stmt
));
1253 right
= create_linear_expr_from_tree (pbb
, gimple_cond_rhs (stmt
));
1255 /* If we have < or > expressions convert them to <= or >= by adding 1 to
1256 the left or the right side of the expression. */
1257 if (code
== LT_EXPR
)
1261 ppl_new_Coefficient (&c
);
1262 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1263 ppl_Linear_Expression_add_to_inhomogeneous (left
, c
);
1264 ppl_delete_Coefficient (c
);
1269 else if (code
== GT_EXPR
)
1273 ppl_new_Coefficient (&c
);
1274 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1275 ppl_Linear_Expression_add_to_inhomogeneous (right
, c
);
1276 ppl_delete_Coefficient (c
);
1282 type
= ppl_constraint_type_from_tree_code (code
);
1284 ppl_subtract_Linear_Expression_from_Linear_Expression (left
, right
);
1286 ppl_new_Constraint (&cstr
, left
, type
);
1287 ppl_Pointset_Powerset_C_Polyhedron_add_constraint (ps
, cstr
);
1289 ppl_delete_Constraint (cstr
);
1290 ppl_delete_Linear_Expression (left
);
1291 ppl_delete_Linear_Expression (right
);
1294 /* Add conditional statement STMT to pbb. CODE is used as the comparision
1295 operator. This allows us to invert the condition or to handle
1299 add_condition_to_pbb (poly_bb_p pbb
, gimple stmt
, enum tree_code code
)
1301 if (code
== NE_EXPR
)
1303 ppl_Pointset_Powerset_C_Polyhedron_t left
= PBB_DOMAIN (pbb
);
1304 ppl_Pointset_Powerset_C_Polyhedron_t right
;
1305 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1307 add_condition_to_domain (left
, stmt
, pbb
, LT_EXPR
);
1308 add_condition_to_domain (right
, stmt
, pbb
, GT_EXPR
);
1309 ppl_Pointset_Powerset_C_Polyhedron_upper_bound_assign (left
, right
);
1310 ppl_delete_Pointset_Powerset_C_Polyhedron (right
);
1313 add_condition_to_domain (PBB_DOMAIN (pbb
), stmt
, pbb
, code
);
1316 /* Add conditions to the domain of PBB. */
1319 add_conditions_to_domain (poly_bb_p pbb
)
1323 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
1325 if (VEC_empty (gimple
, GBB_CONDITIONS (gbb
)))
1328 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
1329 switch (gimple_code (stmt
))
1333 enum tree_code code
= gimple_cond_code (stmt
);
1335 /* The conditions for ELSE-branches are inverted. */
1336 if (!VEC_index (gimple
, GBB_CONDITION_CASES (gbb
), i
))
1337 code
= invert_tree_comparison (code
, false);
1339 add_condition_to_pbb (pbb
, stmt
, code
);
1344 /* Switch statements are not supported right now - fall throught. */
1352 /* Structure used to pass data to dom_walk. */
1356 VEC (gimple
, heap
) **conditions
, **cases
;
1360 /* Returns a COND_EXPR statement when BB has a single predecessor, the
1361 edge between BB and its predecessor is not a loop exit edge, and
1362 the last statement of the single predecessor is a COND_EXPR. */
1365 single_pred_cond_non_loop_exit (basic_block bb
)
1367 if (single_pred_p (bb
))
1369 edge e
= single_pred_edge (bb
);
1370 basic_block pred
= e
->src
;
1373 if (loop_depth (pred
->loop_father
) > loop_depth (bb
->loop_father
))
1376 stmt
= last_stmt (pred
);
1378 if (stmt
&& gimple_code (stmt
) == GIMPLE_COND
)
1385 /* Call-back for dom_walk executed before visiting the dominated
1389 build_sese_conditions_before (struct dom_walk_data
*dw_data
,
1392 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1393 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1394 VEC (gimple
, heap
) **cases
= data
->cases
;
1398 if (!bb_in_sese_p (bb
, data
->region
))
1401 stmt
= single_pred_cond_non_loop_exit (bb
);
1405 edge e
= single_pred_edge (bb
);
1407 VEC_safe_push (gimple
, heap
, *conditions
, stmt
);
1409 if (e
->flags
& EDGE_TRUE_VALUE
)
1410 VEC_safe_push (gimple
, heap
, *cases
, stmt
);
1412 VEC_safe_push (gimple
, heap
, *cases
, NULL
);
1415 gbb
= gbb_from_bb (bb
);
1419 GBB_CONDITIONS (gbb
) = VEC_copy (gimple
, heap
, *conditions
);
1420 GBB_CONDITION_CASES (gbb
) = VEC_copy (gimple
, heap
, *cases
);
1424 /* Call-back for dom_walk executed after visiting the dominated
1428 build_sese_conditions_after (struct dom_walk_data
*dw_data
,
1431 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1432 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1433 VEC (gimple
, heap
) **cases
= data
->cases
;
1435 if (!bb_in_sese_p (bb
, data
->region
))
1438 if (single_pred_cond_non_loop_exit (bb
))
1440 VEC_pop (gimple
, *conditions
);
1441 VEC_pop (gimple
, *cases
);
1445 /* Record all conditions in REGION. */
1448 build_sese_conditions (sese region
)
1450 struct dom_walk_data walk_data
;
1451 VEC (gimple
, heap
) *conditions
= VEC_alloc (gimple
, heap
, 3);
1452 VEC (gimple
, heap
) *cases
= VEC_alloc (gimple
, heap
, 3);
1455 data
.conditions
= &conditions
;
1456 data
.cases
= &cases
;
1457 data
.region
= region
;
1459 walk_data
.dom_direction
= CDI_DOMINATORS
;
1460 walk_data
.initialize_block_local_data
= NULL
;
1461 walk_data
.before_dom_children
= build_sese_conditions_before
;
1462 walk_data
.after_dom_children
= build_sese_conditions_after
;
1463 walk_data
.global_data
= &data
;
1464 walk_data
.block_local_data_size
= 0;
1466 init_walk_dominator_tree (&walk_data
);
1467 walk_dominator_tree (&walk_data
, SESE_ENTRY_BB (region
));
1468 fini_walk_dominator_tree (&walk_data
);
1470 VEC_free (gimple
, heap
, conditions
);
1471 VEC_free (gimple
, heap
, cases
);
1474 /* Traverses all the GBBs of the SCOP and add their constraints to the
1475 iteration domains. */
1478 add_conditions_to_constraints (scop_p scop
)
1483 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1484 add_conditions_to_domain (pbb
);
1487 /* Add constraints on the possible values of parameter P from the type
1491 add_param_constraints (scop_p scop
, ppl_Polyhedron_t context
, graphite_dim_t p
)
1493 ppl_Constraint_t cstr
;
1494 ppl_Linear_Expression_t le
;
1495 tree parameter
= VEC_index (tree
, SESE_PARAMS (SCOP_REGION (scop
)), p
);
1496 tree type
= TREE_TYPE (parameter
);
1497 tree lb
= NULL_TREE
;
1498 tree ub
= NULL_TREE
;
1500 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1501 lb
= lower_bound_in_type (type
, type
);
1503 lb
= TYPE_MIN_VALUE (type
);
1505 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1506 ub
= upper_bound_in_type (type
, type
);
1508 ub
= TYPE_MAX_VALUE (type
);
1512 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1513 ppl_set_coef (le
, p
, -1);
1514 ppl_set_inhomogeneous_tree (le
, lb
);
1515 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
1516 ppl_Polyhedron_add_constraint (context
, cstr
);
1517 ppl_delete_Linear_Expression (le
);
1518 ppl_delete_Constraint (cstr
);
1523 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1524 ppl_set_coef (le
, p
, -1);
1525 ppl_set_inhomogeneous_tree (le
, ub
);
1526 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1527 ppl_Polyhedron_add_constraint (context
, cstr
);
1528 ppl_delete_Linear_Expression (le
);
1529 ppl_delete_Constraint (cstr
);
1533 /* Build the context of the SCOP. The context usually contains extra
1534 constraints that are added to the iteration domains that constrain
1538 build_scop_context (scop_p scop
)
1540 ppl_Polyhedron_t context
;
1541 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
1542 graphite_dim_t p
, n
= scop_nb_params (scop
);
1544 ppl_new_C_Polyhedron_from_space_dimension (&context
, n
, 0);
1546 for (p
= 0; p
< n
; p
++)
1547 add_param_constraints (scop
, context
, p
);
1549 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1551 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1552 (SCOP_CONTEXT (scop
), ps
);
1554 ppl_delete_Pointset_Powerset_C_Polyhedron (ps
);
1555 ppl_delete_Polyhedron (context
);
1558 /* Build the iteration domains: the loops belonging to the current
1559 SCOP, and that vary for the execution of the current basic block.
1560 Returns false if there is no loop in SCOP. */
1563 build_scop_iteration_domain (scop_p scop
)
1566 sese region
= SCOP_REGION (scop
);
1568 ppl_Polyhedron_t ph
;
1570 int nb_loops
= number_of_loops ();
1571 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
1572 = XNEWVEC (ppl_Pointset_Powerset_C_Polyhedron_t
, nb_loops
);
1574 for (i
= 0; i
< nb_loops
; i
++)
1577 ppl_new_C_Polyhedron_from_space_dimension (&ph
, scop_nb_params (scop
), 0);
1579 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
1580 if (!loop_in_sese_p (loop_outer (loop
), region
))
1581 build_loop_iteration_domains (scop
, loop
, ph
, 0, domains
);
1583 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1584 if (domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
])
1585 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1586 (&PBB_DOMAIN (pbb
), (ppl_const_Pointset_Powerset_C_Polyhedron_t
)
1587 domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
]);
1589 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1590 (&PBB_DOMAIN (pbb
), ph
);
1592 for (i
= 0; i
< nb_loops
; i
++)
1594 ppl_delete_Pointset_Powerset_C_Polyhedron (domains
[i
]);
1596 ppl_delete_Polyhedron (ph
);
1600 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1601 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1602 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1606 pdr_add_alias_set (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1607 ppl_dimension_type accessp_nb_dims
,
1608 ppl_dimension_type dom_nb_dims
)
1610 ppl_Linear_Expression_t alias
;
1611 ppl_Constraint_t cstr
;
1612 int alias_set_num
= 0;
1613 base_alias_pair
*bap
= (base_alias_pair
*)(dr
->aux
);
1615 if (bap
&& bap
->alias_set
)
1616 alias_set_num
= *(bap
->alias_set
);
1618 ppl_new_Linear_Expression_with_dimension (&alias
, accessp_nb_dims
);
1620 ppl_set_coef (alias
, dom_nb_dims
, 1);
1621 ppl_set_inhomogeneous (alias
, -alias_set_num
);
1622 ppl_new_Constraint (&cstr
, alias
, PPL_CONSTRAINT_TYPE_EQUAL
);
1623 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1625 ppl_delete_Linear_Expression (alias
);
1626 ppl_delete_Constraint (cstr
);
1629 /* Add to ACCESSES polyhedron equalities defining the access functions
1630 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1631 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1632 PBB is the poly_bb_p that contains the data reference DR. */
1635 pdr_add_memory_accesses (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1636 ppl_dimension_type accessp_nb_dims
,
1637 ppl_dimension_type dom_nb_dims
,
1640 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1642 scop_p scop
= PBB_SCOP (pbb
);
1643 sese region
= SCOP_REGION (scop
);
1647 for (i
= 0; i
< nb_subscripts
; i
++)
1649 ppl_Linear_Expression_t fn
, access
;
1650 ppl_Constraint_t cstr
;
1651 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1652 tree afn
= DR_ACCESS_FN (dr
, nb_subscripts
- 1 - i
);
1654 ppl_new_Linear_Expression_with_dimension (&fn
, dom_nb_dims
);
1655 ppl_new_Linear_Expression_with_dimension (&access
, accessp_nb_dims
);
1658 scan_tree_for_params (region
, afn
, fn
, v
);
1659 ppl_assign_Linear_Expression_from_Linear_Expression (access
, fn
);
1661 ppl_set_coef (access
, subscript
, -1);
1662 ppl_new_Constraint (&cstr
, access
, PPL_CONSTRAINT_TYPE_EQUAL
);
1663 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1665 ppl_delete_Linear_Expression (fn
);
1666 ppl_delete_Linear_Expression (access
);
1667 ppl_delete_Constraint (cstr
);
1673 /* Add constrains representing the size of the accessed data to the
1674 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1675 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1679 pdr_add_data_dimensions (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1680 ppl_dimension_type accessp_nb_dims
,
1681 ppl_dimension_type dom_nb_dims
)
1683 tree ref
= DR_REF (dr
);
1684 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1686 for (i
= nb_subscripts
- 1; i
>= 0; i
--, ref
= TREE_OPERAND (ref
, 0))
1688 ppl_Linear_Expression_t expr
;
1689 ppl_Constraint_t cstr
;
1690 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1693 if (TREE_CODE (ref
) != ARRAY_REF
)
1696 low
= array_ref_low_bound (ref
);
1698 /* subscript - low >= 0 */
1699 if (host_integerp (low
, 0))
1701 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1702 ppl_set_coef (expr
, subscript
, 1);
1704 ppl_set_inhomogeneous (expr
, -int_cst_value (low
));
1706 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1707 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1708 ppl_delete_Linear_Expression (expr
);
1709 ppl_delete_Constraint (cstr
);
1712 high
= array_ref_up_bound (ref
);
1714 /* high - subscript >= 0 */
1715 if (high
&& host_integerp (high
, 0)
1716 /* 1-element arrays at end of structures may extend over
1717 their declared size. */
1718 && !(array_at_struct_end_p (ref
)
1719 && operand_equal_p (low
, high
, 0)))
1721 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1722 ppl_set_coef (expr
, subscript
, -1);
1724 ppl_set_inhomogeneous (expr
, int_cst_value (high
));
1726 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1727 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1728 ppl_delete_Linear_Expression (expr
);
1729 ppl_delete_Constraint (cstr
);
1734 /* Build data accesses for DR in PBB. */
1737 build_poly_dr (data_reference_p dr
, poly_bb_p pbb
)
1739 ppl_Polyhedron_t accesses
;
1740 ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps
;
1741 ppl_dimension_type dom_nb_dims
;
1742 ppl_dimension_type accessp_nb_dims
;
1743 int dr_base_object_set
;
1745 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb
),
1747 accessp_nb_dims
= dom_nb_dims
+ 1 + DR_NUM_DIMENSIONS (dr
);
1749 ppl_new_C_Polyhedron_from_space_dimension (&accesses
, accessp_nb_dims
, 0);
1751 pdr_add_alias_set (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1752 pdr_add_memory_accesses (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
, pbb
);
1753 pdr_add_data_dimensions (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1755 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps
,
1757 ppl_delete_Polyhedron (accesses
);
1759 gcc_assert (dr
->aux
);
1760 dr_base_object_set
= ((base_alias_pair
*)(dr
->aux
))->base_obj_set
;
1762 new_poly_dr (pbb
, dr_base_object_set
, accesses_ps
,
1763 DR_IS_READ (dr
) ? PDR_READ
: PDR_WRITE
,
1764 dr
, DR_NUM_DIMENSIONS (dr
));
1767 /* Write to FILE the alias graph of data references in DIMACS format. */
1770 write_alias_graph_to_ascii_dimacs (FILE *file
, char *comment
,
1771 VEC (data_reference_p
, heap
) *drs
)
1773 int num_vertex
= VEC_length (data_reference_p
, drs
);
1775 data_reference_p dr1
, dr2
;
1778 if (num_vertex
== 0)
1781 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1782 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1783 if (dr_may_alias_p (dr1
, dr2
))
1786 fprintf (file
, "$\n");
1789 fprintf (file
, "c %s\n", comment
);
1791 fprintf (file
, "p edge %d %d\n", num_vertex
, edge_num
);
1793 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1794 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1795 if (dr_may_alias_p (dr1
, dr2
))
1796 fprintf (file
, "e %d %d\n", i
+ 1, j
+ 1);
1801 /* Write to FILE the alias graph of data references in DOT format. */
1804 write_alias_graph_to_ascii_dot (FILE *file
, char *comment
,
1805 VEC (data_reference_p
, heap
) *drs
)
1807 int num_vertex
= VEC_length (data_reference_p
, drs
);
1808 data_reference_p dr1
, dr2
;
1811 if (num_vertex
== 0)
1814 fprintf (file
, "$\n");
1817 fprintf (file
, "c %s\n", comment
);
1819 /* First print all the vertices. */
1820 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1821 fprintf (file
, "n%d;\n", i
);
1823 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1824 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1825 if (dr_may_alias_p (dr1
, dr2
))
1826 fprintf (file
, "n%d n%d\n", i
, j
);
1831 /* Write to FILE the alias graph of data references in ECC format. */
1834 write_alias_graph_to_ascii_ecc (FILE *file
, char *comment
,
1835 VEC (data_reference_p
, heap
) *drs
)
1837 int num_vertex
= VEC_length (data_reference_p
, drs
);
1838 data_reference_p dr1
, dr2
;
1841 if (num_vertex
== 0)
1844 fprintf (file
, "$\n");
1847 fprintf (file
, "c %s\n", comment
);
1849 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1850 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1851 if (dr_may_alias_p (dr1
, dr2
))
1852 fprintf (file
, "%d %d\n", i
, j
);
1857 /* Check if DR1 and DR2 are in the same object set. */
1860 dr_same_base_object_p (const struct data_reference
*dr1
,
1861 const struct data_reference
*dr2
)
1863 return operand_equal_p (DR_BASE_OBJECT (dr1
), DR_BASE_OBJECT (dr2
), 0);
1866 /* Uses DFS component number as representative of alias-sets. Also tests for
1867 optimality by verifying if every connected component is a clique. Returns
1868 true (1) if the above test is true, and false (0) otherwise. */
1871 build_alias_set_optimal_p (VEC (data_reference_p
, heap
) *drs
)
1873 int num_vertices
= VEC_length (data_reference_p
, drs
);
1874 struct graph
*g
= new_graph (num_vertices
);
1875 data_reference_p dr1
, dr2
;
1877 int num_connected_components
;
1878 int v_indx1
, v_indx2
, num_vertices_in_component
;
1881 struct graph_edge
*e
;
1882 int this_component_is_clique
;
1883 int all_components_are_cliques
= 1;
1885 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1886 for (j
= i
+1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1887 if (dr_may_alias_p (dr1
, dr2
))
1893 all_vertices
= XNEWVEC (int, num_vertices
);
1894 vertices
= XNEWVEC (int, num_vertices
);
1895 for (i
= 0; i
< num_vertices
; i
++)
1896 all_vertices
[i
] = i
;
1898 num_connected_components
= graphds_dfs (g
, all_vertices
, num_vertices
,
1900 for (i
= 0; i
< g
->n_vertices
; i
++)
1902 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1903 base_alias_pair
*bap
;
1905 gcc_assert (dr
->aux
);
1906 bap
= (base_alias_pair
*)(dr
->aux
);
1908 bap
->alias_set
= XNEW (int);
1909 *(bap
->alias_set
) = g
->vertices
[i
].component
+ 1;
1912 /* Verify if the DFS numbering results in optimal solution. */
1913 for (i
= 0; i
< num_connected_components
; i
++)
1915 num_vertices_in_component
= 0;
1916 /* Get all vertices whose DFS component number is the same as i. */
1917 for (j
= 0; j
< num_vertices
; j
++)
1918 if (g
->vertices
[j
].component
== i
)
1919 vertices
[num_vertices_in_component
++] = j
;
1921 /* Now test if the vertices in 'vertices' form a clique, by testing
1922 for edges among each pair. */
1923 this_component_is_clique
= 1;
1924 for (v_indx1
= 0; v_indx1
< num_vertices_in_component
; v_indx1
++)
1926 for (v_indx2
= v_indx1
+1; v_indx2
< num_vertices_in_component
; v_indx2
++)
1928 /* Check if the two vertices are connected by iterating
1929 through all the edges which have one of these are source. */
1930 e
= g
->vertices
[vertices
[v_indx2
]].pred
;
1933 if (e
->src
== vertices
[v_indx1
])
1939 this_component_is_clique
= 0;
1943 if (!this_component_is_clique
)
1944 all_components_are_cliques
= 0;
1948 free (all_vertices
);
1951 return all_components_are_cliques
;
1954 /* Group each data reference in DRS with it's base object set num. */
1957 build_base_obj_set_for_drs (VEC (data_reference_p
, heap
) *drs
)
1959 int num_vertex
= VEC_length (data_reference_p
, drs
);
1960 struct graph
*g
= new_graph (num_vertex
);
1961 data_reference_p dr1
, dr2
;
1965 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1966 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1967 if (dr_same_base_object_p (dr1
, dr2
))
1973 queue
= XNEWVEC (int, num_vertex
);
1974 for (i
= 0; i
< num_vertex
; i
++)
1977 graphds_dfs (g
, queue
, num_vertex
, NULL
, true, NULL
);
1979 for (i
= 0; i
< g
->n_vertices
; i
++)
1981 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1982 base_alias_pair
*bap
;
1984 gcc_assert (dr
->aux
);
1985 bap
= (base_alias_pair
*)(dr
->aux
);
1987 bap
->base_obj_set
= g
->vertices
[i
].component
+ 1;
1994 /* Build the data references for PBB. */
1997 build_pbb_drs (poly_bb_p pbb
)
2000 data_reference_p dr
;
2001 VEC (data_reference_p
, heap
) *gbb_drs
= GBB_DATA_REFS (PBB_BLACK_BOX (pbb
));
2003 FOR_EACH_VEC_ELT (data_reference_p
, gbb_drs
, j
, dr
)
2004 build_poly_dr (dr
, pbb
);
2007 /* Dump to file the alias graphs for the data references in DRS. */
2010 dump_alias_graphs (VEC (data_reference_p
, heap
) *drs
)
2013 FILE *file_dimacs
, *file_ecc
, *file_dot
;
2015 file_dimacs
= fopen ("/tmp/dr_alias_graph_dimacs", "ab");
2018 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
2019 current_function_name ());
2020 write_alias_graph_to_ascii_dimacs (file_dimacs
, comment
, drs
);
2021 fclose (file_dimacs
);
2024 file_ecc
= fopen ("/tmp/dr_alias_graph_ecc", "ab");
2027 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
2028 current_function_name ());
2029 write_alias_graph_to_ascii_ecc (file_ecc
, comment
, drs
);
2033 file_dot
= fopen ("/tmp/dr_alias_graph_dot", "ab");
2036 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
2037 current_function_name ());
2038 write_alias_graph_to_ascii_dot (file_dot
, comment
, drs
);
2043 /* Build data references in SCOP. */
2046 build_scop_drs (scop_p scop
)
2050 data_reference_p dr
;
2051 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 3);
2053 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2054 for (j
= 0; VEC_iterate (data_reference_p
,
2055 GBB_DATA_REFS (PBB_BLACK_BOX (pbb
)), j
, dr
); j
++)
2056 VEC_safe_push (data_reference_p
, heap
, drs
, dr
);
2058 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr
)
2059 dr
->aux
= XNEW (base_alias_pair
);
2061 if (!build_alias_set_optimal_p (drs
))
2063 /* TODO: Add support when building alias set is not optimal. */
2067 build_base_obj_set_for_drs (drs
);
2069 /* When debugging, enable the following code. This cannot be used
2070 in production compilers. */
2072 dump_alias_graphs (drs
);
2074 VEC_free (data_reference_p
, heap
, drs
);
2076 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2077 build_pbb_drs (pbb
);
2080 /* Return a gsi at the position of the phi node STMT. */
2082 static gimple_stmt_iterator
2083 gsi_for_phi_node (gimple stmt
)
2085 gimple_stmt_iterator psi
;
2086 basic_block bb
= gimple_bb (stmt
);
2088 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2089 if (stmt
== gsi_stmt (psi
))
2096 /* Insert the assignment "RES := VAR" just after AFTER_STMT. */
2099 insert_out_of_ssa_copy (tree res
, tree var
, gimple after_stmt
)
2103 gimple_stmt_iterator si
;
2104 gimple_stmt_iterator gsi
;
2106 var
= force_gimple_operand (var
, &stmts
, true, NULL_TREE
);
2107 stmt
= gimple_build_assign (res
, var
);
2109 stmts
= gimple_seq_alloc ();
2110 si
= gsi_last (stmts
);
2111 gsi_insert_after (&si
, stmt
, GSI_NEW_STMT
);
2113 if (gimple_code (after_stmt
) == GIMPLE_PHI
)
2115 gsi
= gsi_after_labels (gimple_bb (after_stmt
));
2116 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
2120 gsi
= gsi_for_stmt (after_stmt
);
2121 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
2125 /* Insert on edge E the assignment "RES := EXPR". */
2128 insert_out_of_ssa_copy_on_edge (edge e
, tree res
, tree expr
)
2130 gimple_stmt_iterator gsi
;
2132 tree var
= force_gimple_operand (expr
, &stmts
, true, NULL_TREE
);
2133 gimple stmt
= gimple_build_assign (res
, var
);
2136 stmts
= gimple_seq_alloc ();
2138 gsi
= gsi_last (stmts
);
2139 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2140 gsi_insert_seq_on_edge (e
, stmts
);
2141 gsi_commit_edge_inserts ();
2144 /* Creates a zero dimension array of the same type as VAR. */
2147 create_zero_dim_array (tree var
, const char *base_name
)
2149 tree index_type
= build_index_type (integer_zero_node
);
2150 tree elt_type
= TREE_TYPE (var
);
2151 tree array_type
= build_array_type (elt_type
, index_type
);
2152 tree base
= create_tmp_var (array_type
, base_name
);
2154 add_referenced_var (base
);
2156 return build4 (ARRAY_REF
, elt_type
, base
, integer_zero_node
, NULL_TREE
,
2160 /* Returns true when PHI is a loop close phi node. */
2163 scalar_close_phi_node_p (gimple phi
)
2165 if (gimple_code (phi
) != GIMPLE_PHI
2166 || !is_gimple_reg (gimple_phi_result (phi
)))
2169 /* Note that loop close phi nodes should have a single argument
2170 because we translated the representation into a canonical form
2171 before Graphite: see canonicalize_loop_closed_ssa_form. */
2172 return (gimple_phi_num_args (phi
) == 1);
2175 /* For a definition DEF in REGION, propagates the expression EXPR in
2176 all the uses of DEF outside REGION. */
2179 propagate_expr_outside_region (tree def
, tree expr
, sese region
)
2181 imm_use_iterator imm_iter
;
2184 bool replaced_once
= false;
2186 gcc_assert (TREE_CODE (def
) == SSA_NAME
);
2188 expr
= force_gimple_operand (unshare_expr (expr
), &stmts
, true,
2191 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2192 if (!is_gimple_debug (use_stmt
)
2193 && !bb_in_sese_p (gimple_bb (use_stmt
), region
))
2196 use_operand_p use_p
;
2198 FOR_EACH_PHI_OR_STMT_USE (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2199 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0)
2200 && (replaced_once
= true))
2201 replace_exp (use_p
, expr
);
2203 update_stmt (use_stmt
);
2208 gsi_insert_seq_on_edge (SESE_ENTRY (region
), stmts
);
2209 gsi_commit_edge_inserts ();
2213 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2214 dimension array for it. */
2217 rewrite_close_phi_out_of_ssa (gimple_stmt_iterator
*psi
, sese region
)
2219 gimple phi
= gsi_stmt (*psi
);
2220 tree res
= gimple_phi_result (phi
);
2221 tree var
= SSA_NAME_VAR (res
);
2222 basic_block bb
= gimple_bb (phi
);
2223 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
2224 tree arg
= gimple_phi_arg_def (phi
, 0);
2227 /* Note that loop close phi nodes should have a single argument
2228 because we translated the representation into a canonical form
2229 before Graphite: see canonicalize_loop_closed_ssa_form. */
2230 gcc_assert (gimple_phi_num_args (phi
) == 1);
2232 /* The phi node can be a non close phi node, when its argument is
2233 invariant, or a default definition. */
2234 if (is_gimple_min_invariant (arg
)
2235 || SSA_NAME_IS_DEFAULT_DEF (arg
))
2237 propagate_expr_outside_region (res
, arg
, region
);
2242 else if (gimple_bb (SSA_NAME_DEF_STMT (arg
))->loop_father
== bb
->loop_father
)
2244 propagate_expr_outside_region (res
, arg
, region
);
2245 stmt
= gimple_build_assign (res
, arg
);
2246 remove_phi_node (psi
, false);
2247 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2248 SSA_NAME_DEF_STMT (res
) = stmt
;
2252 /* If res is scev analyzable and is not a scalar value, it is safe
2253 to ignore the close phi node: it will be code generated in the
2254 out of Graphite pass. */
2255 else if (scev_analyzable_p (res
, region
))
2257 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (res
));
2260 if (!loop_in_sese_p (loop
, region
))
2262 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (arg
));
2263 scev
= scalar_evolution_in_region (region
, loop
, arg
);
2264 scev
= compute_overall_effect_of_inner_loop (loop
, scev
);
2267 scev
= scalar_evolution_in_region (region
, loop
, res
);
2269 if (tree_does_not_contain_chrecs (scev
))
2270 propagate_expr_outside_region (res
, scev
, region
);
2277 tree zero_dim_array
= create_zero_dim_array (var
, "Close_Phi");
2279 stmt
= gimple_build_assign (res
, zero_dim_array
);
2281 if (TREE_CODE (arg
) == SSA_NAME
)
2282 insert_out_of_ssa_copy (zero_dim_array
, arg
, SSA_NAME_DEF_STMT (arg
));
2284 insert_out_of_ssa_copy_on_edge (single_pred_edge (bb
),
2285 zero_dim_array
, arg
);
2288 remove_phi_node (psi
, false);
2289 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2290 SSA_NAME_DEF_STMT (res
) = stmt
;
2293 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2294 dimension array for it. */
2297 rewrite_phi_out_of_ssa (gimple_stmt_iterator
*psi
)
2300 gimple phi
= gsi_stmt (*psi
);
2301 basic_block bb
= gimple_bb (phi
);
2302 tree res
= gimple_phi_result (phi
);
2303 tree var
= SSA_NAME_VAR (res
);
2304 tree zero_dim_array
= create_zero_dim_array (var
, "phi_out_of_ssa");
2305 gimple_stmt_iterator gsi
;
2309 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2311 tree arg
= gimple_phi_arg_def (phi
, i
);
2312 edge e
= gimple_phi_arg_edge (phi
, i
);
2314 /* Avoid the insertion of code in the loop latch to please the
2315 pattern matching of the vectorizer. */
2316 if (TREE_CODE (arg
) == SSA_NAME
2317 && e
->src
== bb
->loop_father
->latch
)
2318 insert_out_of_ssa_copy (zero_dim_array
, arg
, SSA_NAME_DEF_STMT (arg
));
2320 insert_out_of_ssa_copy_on_edge (e
, zero_dim_array
, arg
);
2323 var
= force_gimple_operand (zero_dim_array
, &stmts
, true, NULL_TREE
);
2326 stmts
= gimple_seq_alloc ();
2328 stmt
= gimple_build_assign (res
, var
);
2329 remove_phi_node (psi
, false);
2330 SSA_NAME_DEF_STMT (res
) = stmt
;
2332 gsi
= gsi_last (stmts
);
2333 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2335 gsi
= gsi_after_labels (bb
);
2336 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
2339 /* Rewrite the degenerate phi node at position PSI from the degenerate
2340 form "x = phi (y, y, ..., y)" to "x = y". */
2343 rewrite_degenerate_phi (gimple_stmt_iterator
*psi
)
2347 gimple_stmt_iterator gsi
;
2348 gimple phi
= gsi_stmt (*psi
);
2349 tree res
= gimple_phi_result (phi
);
2352 bb
= gimple_bb (phi
);
2353 rhs
= degenerate_phi_result (phi
);
2356 stmt
= gimple_build_assign (res
, rhs
);
2357 remove_phi_node (psi
, false);
2358 SSA_NAME_DEF_STMT (res
) = stmt
;
2360 gsi
= gsi_after_labels (bb
);
2361 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2364 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2367 rewrite_reductions_out_of_ssa (scop_p scop
)
2370 gimple_stmt_iterator psi
;
2371 sese region
= SCOP_REGION (scop
);
2374 if (bb_in_sese_p (bb
, region
))
2375 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);)
2377 gimple phi
= gsi_stmt (psi
);
2379 if (!is_gimple_reg (gimple_phi_result (phi
)))
2385 if (gimple_phi_num_args (phi
) > 1
2386 && degenerate_phi_result (phi
))
2387 rewrite_degenerate_phi (&psi
);
2389 else if (scalar_close_phi_node_p (phi
))
2390 rewrite_close_phi_out_of_ssa (&psi
, region
);
2392 else if (reduction_phi_p (region
, &psi
))
2393 rewrite_phi_out_of_ssa (&psi
);
2396 update_ssa (TODO_update_ssa
);
2397 #ifdef ENABLE_CHECKING
2398 verify_loop_closed_ssa (true);
2402 /* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
2403 read from ZERO_DIM_ARRAY. */
2406 rewrite_cross_bb_scalar_dependence (tree zero_dim_array
, tree def
, gimple use_stmt
)
2408 tree var
= SSA_NAME_VAR (def
);
2409 gimple name_stmt
= gimple_build_assign (var
, zero_dim_array
);
2410 tree name
= make_ssa_name (var
, name_stmt
);
2412 use_operand_p use_p
;
2413 gimple_stmt_iterator gsi
;
2415 gcc_assert (gimple_code (use_stmt
) != GIMPLE_PHI
);
2417 gimple_assign_set_lhs (name_stmt
, name
);
2419 gsi
= gsi_for_stmt (use_stmt
);
2420 gsi_insert_before (&gsi
, name_stmt
, GSI_NEW_STMT
);
2422 FOR_EACH_SSA_USE_OPERAND (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2423 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0))
2424 replace_exp (use_p
, name
);
2426 update_stmt (use_stmt
);
2429 /* Rewrite the scalar dependences crossing the boundary of the BB
2430 containing STMT with an array. Return true when something has been
2434 rewrite_cross_bb_scalar_deps (sese region
, gimple_stmt_iterator
*gsi
)
2436 gimple stmt
= gsi_stmt (*gsi
);
2437 imm_use_iterator imm_iter
;
2440 tree zero_dim_array
= NULL_TREE
;
2444 switch (gimple_code (stmt
))
2447 def
= gimple_assign_lhs (stmt
);
2451 def
= gimple_call_lhs (stmt
);
2459 || !is_gimple_reg (def
))
2462 if (scev_analyzable_p (def
, region
))
2464 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (def
));
2465 tree scev
= scalar_evolution_in_region (region
, loop
, def
);
2467 if (tree_contains_chrecs (scev
, NULL
))
2470 propagate_expr_outside_region (def
, scev
, region
);
2474 def_bb
= gimple_bb (stmt
);
2476 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2477 if (gimple_code (use_stmt
) == GIMPLE_PHI
2480 gimple_stmt_iterator psi
= gsi_for_stmt (use_stmt
);
2482 if (scalar_close_phi_node_p (gsi_stmt (psi
)))
2483 rewrite_close_phi_out_of_ssa (&psi
, region
);
2485 rewrite_phi_out_of_ssa (&psi
);
2488 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2489 if (gimple_code (use_stmt
) != GIMPLE_PHI
2490 && def_bb
!= gimple_bb (use_stmt
)
2491 && !is_gimple_debug (use_stmt
)
2494 if (!zero_dim_array
)
2496 zero_dim_array
= create_zero_dim_array
2497 (SSA_NAME_VAR (def
), "Cross_BB_scalar_dependence");
2498 insert_out_of_ssa_copy (zero_dim_array
, def
,
2499 SSA_NAME_DEF_STMT (def
));
2503 rewrite_cross_bb_scalar_dependence (zero_dim_array
, def
, use_stmt
);
2509 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2512 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop
)
2515 gimple_stmt_iterator psi
;
2516 sese region
= SCOP_REGION (scop
);
2517 bool changed
= false;
2520 if (bb_in_sese_p (bb
, region
))
2521 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2522 changed
|= rewrite_cross_bb_scalar_deps (region
, &psi
);
2527 update_ssa (TODO_update_ssa
);
2528 #ifdef ENABLE_CHECKING
2529 verify_loop_closed_ssa (true);
2534 /* Returns the number of pbbs that are in loops contained in SCOP. */
2537 nb_pbbs_in_loops (scop_p scop
)
2543 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2544 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb
)), SCOP_REGION (scop
)))
2550 /* Return the number of data references in BB that write in
2554 nb_data_writes_in_bb (basic_block bb
)
2557 gimple_stmt_iterator gsi
;
2559 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2560 if (gimple_vdef (gsi_stmt (gsi
)))
2566 /* Splits STMT out of its current BB. */
2569 split_reduction_stmt (gimple stmt
)
2571 gimple_stmt_iterator gsi
;
2572 basic_block bb
= gimple_bb (stmt
);
2575 /* Do not split basic blocks with no writes to memory: the reduction
2576 will be the only write to memory. */
2577 if (nb_data_writes_in_bb (bb
) == 0)
2580 split_block (bb
, stmt
);
2582 if (gsi_one_before_end_p (gsi_start_nondebug_bb (bb
)))
2585 gsi
= gsi_last_bb (bb
);
2587 e
= split_block (bb
, gsi_stmt (gsi
));
2592 /* Return true when stmt is a reduction operation. */
2595 is_reduction_operation_p (gimple stmt
)
2597 enum tree_code code
;
2599 gcc_assert (is_gimple_assign (stmt
));
2600 code
= gimple_assign_rhs_code (stmt
);
2602 return flag_associative_math
2603 && commutative_tree_code (code
)
2604 && associative_tree_code (code
);
2607 /* Returns true when PHI contains an argument ARG. */
2610 phi_contains_arg (gimple phi
, tree arg
)
2614 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2615 if (operand_equal_p (arg
, gimple_phi_arg_def (phi
, i
), 0))
2621 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2624 follow_ssa_with_commutative_ops (tree arg
, tree lhs
)
2628 if (TREE_CODE (arg
) != SSA_NAME
)
2631 stmt
= SSA_NAME_DEF_STMT (arg
);
2633 if (gimple_code (stmt
) == GIMPLE_NOP
2634 || gimple_code (stmt
) == GIMPLE_CALL
)
2637 if (gimple_code (stmt
) == GIMPLE_PHI
)
2639 if (phi_contains_arg (stmt
, lhs
))
2644 if (!is_gimple_assign (stmt
))
2647 if (gimple_num_ops (stmt
) == 2)
2648 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2650 if (is_reduction_operation_p (stmt
))
2652 gimple res
= follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2655 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt
), lhs
);
2661 /* Detect commutative and associative scalar reductions starting at
2662 the STMT. Return the phi node of the reduction cycle, or NULL. */
2665 detect_commutative_reduction_arg (tree lhs
, gimple stmt
, tree arg
,
2666 VEC (gimple
, heap
) **in
,
2667 VEC (gimple
, heap
) **out
)
2669 gimple phi
= follow_ssa_with_commutative_ops (arg
, lhs
);
2674 VEC_safe_push (gimple
, heap
, *in
, stmt
);
2675 VEC_safe_push (gimple
, heap
, *out
, stmt
);
2679 /* Detect commutative and associative scalar reductions starting at
2680 STMT. Return the phi node of the reduction cycle, or NULL. */
2683 detect_commutative_reduction_assign (gimple stmt
, VEC (gimple
, heap
) **in
,
2684 VEC (gimple
, heap
) **out
)
2686 tree lhs
= gimple_assign_lhs (stmt
);
2688 if (gimple_num_ops (stmt
) == 2)
2689 return detect_commutative_reduction_arg (lhs
, stmt
,
2690 gimple_assign_rhs1 (stmt
),
2693 if (is_reduction_operation_p (stmt
))
2695 gimple res
= detect_commutative_reduction_arg (lhs
, stmt
,
2696 gimple_assign_rhs1 (stmt
),
2699 : detect_commutative_reduction_arg (lhs
, stmt
,
2700 gimple_assign_rhs2 (stmt
),
2707 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2710 follow_inital_value_to_phi (tree arg
, tree lhs
)
2714 if (!arg
|| TREE_CODE (arg
) != SSA_NAME
)
2717 stmt
= SSA_NAME_DEF_STMT (arg
);
2719 if (gimple_code (stmt
) == GIMPLE_PHI
2720 && phi_contains_arg (stmt
, lhs
))
2727 /* Return the argument of the loop PHI that is the inital value coming
2728 from outside the loop. */
2731 edge_initial_value_for_loop_phi (gimple phi
)
2735 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2737 edge e
= gimple_phi_arg_edge (phi
, i
);
2739 if (loop_depth (e
->src
->loop_father
)
2740 < loop_depth (e
->dest
->loop_father
))
2747 /* Return the argument of the loop PHI that is the inital value coming
2748 from outside the loop. */
2751 initial_value_for_loop_phi (gimple phi
)
2755 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2757 edge e
= gimple_phi_arg_edge (phi
, i
);
2759 if (loop_depth (e
->src
->loop_father
)
2760 < loop_depth (e
->dest
->loop_father
))
2761 return gimple_phi_arg_def (phi
, i
);
2767 /* Detect commutative and associative scalar reductions starting at
2768 the loop closed phi node STMT. Return the phi node of the
2769 reduction cycle, or NULL. */
2772 detect_commutative_reduction (gimple stmt
, VEC (gimple
, heap
) **in
,
2773 VEC (gimple
, heap
) **out
)
2775 if (scalar_close_phi_node_p (stmt
))
2777 tree arg
= gimple_phi_arg_def (stmt
, 0);
2778 gimple def
, loop_phi
;
2780 if (TREE_CODE (arg
) != SSA_NAME
)
2783 /* Note that loop close phi nodes should have a single argument
2784 because we translated the representation into a canonical form
2785 before Graphite: see canonicalize_loop_closed_ssa_form. */
2786 gcc_assert (gimple_phi_num_args (stmt
) == 1);
2788 def
= SSA_NAME_DEF_STMT (arg
);
2789 loop_phi
= detect_commutative_reduction (def
, in
, out
);
2793 tree lhs
= gimple_phi_result (stmt
);
2794 tree init
= initial_value_for_loop_phi (loop_phi
);
2795 gimple phi
= follow_inital_value_to_phi (init
, lhs
);
2797 VEC_safe_push (gimple
, heap
, *in
, loop_phi
);
2798 VEC_safe_push (gimple
, heap
, *out
, stmt
);
2805 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
2806 return detect_commutative_reduction_assign (stmt
, in
, out
);
2811 /* Translate the scalar reduction statement STMT to an array RED
2812 knowing that its recursive phi node is LOOP_PHI. */
2815 translate_scalar_reduction_to_array_for_stmt (tree red
, gimple stmt
,
2818 gimple_stmt_iterator insert_gsi
= gsi_after_labels (gimple_bb (loop_phi
));
2819 tree res
= gimple_phi_result (loop_phi
);
2820 gimple assign
= gimple_build_assign (res
, red
);
2822 gsi_insert_before (&insert_gsi
, assign
, GSI_SAME_STMT
);
2824 insert_gsi
= gsi_after_labels (gimple_bb (stmt
));
2825 assign
= gimple_build_assign (red
, gimple_assign_lhs (stmt
));
2826 insert_gsi
= gsi_for_stmt (stmt
);
2827 gsi_insert_after (&insert_gsi
, assign
, GSI_SAME_STMT
);
2830 /* Removes the PHI node and resets all the debug stmts that are using
2834 remove_phi (gimple phi
)
2836 imm_use_iterator imm_iter
;
2838 use_operand_p use_p
;
2839 gimple_stmt_iterator gsi
;
2840 VEC (gimple
, heap
) *update
= VEC_alloc (gimple
, heap
, 3);
2844 def
= PHI_RESULT (phi
);
2845 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
2847 stmt
= USE_STMT (use_p
);
2849 if (is_gimple_debug (stmt
))
2851 gimple_debug_bind_reset_value (stmt
);
2852 VEC_safe_push (gimple
, heap
, update
, stmt
);
2856 FOR_EACH_VEC_ELT (gimple
, update
, i
, stmt
)
2859 VEC_free (gimple
, heap
, update
);
2861 gsi
= gsi_for_phi_node (phi
);
2862 remove_phi_node (&gsi
, false);
2865 /* Rewrite out of SSA the reduction described by the loop phi nodes
2866 IN, and the close phi nodes OUT. IN and OUT are structured by loop
2869 IN: stmt, loop_n, ..., loop_0
2870 OUT: stmt, close_n, ..., close_0
2872 the first element is the reduction statement, and the next elements
2873 are the loop and close phi nodes of each of the outer loops. */
2876 translate_scalar_reduction_to_array (VEC (gimple
, heap
) *in
,
2877 VEC (gimple
, heap
) *out
,
2882 tree red
= NULL_TREE
;
2884 FOR_EACH_VEC_ELT (gimple
, in
, i
, loop_phi
)
2886 gimple close_phi
= VEC_index (gimple
, out
, i
);
2890 gimple stmt
= loop_phi
;
2891 basic_block bb
= split_reduction_stmt (stmt
);
2893 SET_BIT (reductions
, bb
->index
);
2894 gcc_assert (close_phi
== loop_phi
);
2896 red
= create_zero_dim_array
2897 (gimple_assign_lhs (stmt
), "Commutative_Associative_Reduction");
2898 translate_scalar_reduction_to_array_for_stmt
2899 (red
, stmt
, VEC_index (gimple
, in
, 1));
2903 if (i
== VEC_length (gimple
, in
) - 1)
2905 insert_out_of_ssa_copy (gimple_phi_result (close_phi
), red
,
2907 insert_out_of_ssa_copy_on_edge
2908 (edge_initial_value_for_loop_phi (loop_phi
),
2909 red
, initial_value_for_loop_phi (loop_phi
));
2912 remove_phi (loop_phi
);
2913 remove_phi (close_phi
);
2917 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. Returns
2918 true when something has been changed. */
2921 rewrite_commutative_reductions_out_of_ssa_close_phi (gimple close_phi
,
2925 VEC (gimple
, heap
) *in
= VEC_alloc (gimple
, heap
, 10);
2926 VEC (gimple
, heap
) *out
= VEC_alloc (gimple
, heap
, 10);
2928 detect_commutative_reduction (close_phi
, &in
, &out
);
2929 res
= VEC_length (gimple
, in
) > 0;
2931 translate_scalar_reduction_to_array (in
, out
, reductions
);
2933 VEC_free (gimple
, heap
, in
);
2934 VEC_free (gimple
, heap
, out
);
2938 /* Rewrites all the commutative reductions from LOOP out of SSA.
2939 Returns true when something has been changed. */
2942 rewrite_commutative_reductions_out_of_ssa_loop (loop_p loop
,
2946 gimple_stmt_iterator gsi
;
2947 edge exit
= single_exit (loop
);
2949 bool changed
= false;
2954 for (gsi
= gsi_start_phis (exit
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2955 if ((res
= gimple_phi_result (gsi_stmt (gsi
)))
2956 && is_gimple_reg (res
)
2957 && !scev_analyzable_p (res
, region
))
2958 changed
|= rewrite_commutative_reductions_out_of_ssa_close_phi
2959 (gsi_stmt (gsi
), reductions
);
2964 /* Rewrites all the commutative reductions from SCOP out of SSA. */
2967 rewrite_commutative_reductions_out_of_ssa (sese region
, sbitmap reductions
)
2971 bool changed
= false;
2973 if (!flag_associative_math
)
2976 FOR_EACH_LOOP (li
, loop
, 0)
2977 if (loop_in_sese_p (loop
, region
))
2978 changed
|= rewrite_commutative_reductions_out_of_ssa_loop (loop
,
2985 gsi_commit_edge_inserts ();
2986 update_ssa (TODO_update_ssa
);
2987 #ifdef ENABLE_CHECKING
2988 verify_loop_closed_ssa (true);
2993 /* Java does not initialize long_long_integer_type_node. */
2994 #define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
2996 /* Can all ivs be represented by a signed integer?
2997 As CLooG might generate negative values in its expressions, signed loop ivs
2998 are required in the backend. */
3001 scop_ivs_can_be_represented (scop_p scop
)
3005 gimple_stmt_iterator psi
;
3007 FOR_EACH_LOOP (li
, loop
, 0)
3009 if (!loop_in_sese_p (loop
, SCOP_REGION (scop
)))
3012 for (psi
= gsi_start_phis (loop
->header
);
3013 !gsi_end_p (psi
); gsi_next (&psi
))
3015 gimple phi
= gsi_stmt (psi
);
3016 tree res
= PHI_RESULT (phi
);
3017 tree type
= TREE_TYPE (res
);
3019 if (TYPE_UNSIGNED (type
)
3020 && TYPE_PRECISION (type
) >= TYPE_PRECISION (my_long_long
))
3030 /* Builds the polyhedral representation for a SESE region. */
3033 build_poly_scop (scop_p scop
)
3035 sese region
= SCOP_REGION (scop
);
3036 graphite_dim_t max_dim
;
3039 /* FIXME: This restriction is needed to avoid a problem in CLooG.
3040 Once CLooG is fixed, remove this guard. Anyways, it makes no
3041 sense to optimize a scop containing only PBBs that do not belong
3043 if (nb_pbbs_in_loops (scop
) == 0)
3046 if (!scop_ivs_can_be_represented (scop
))
3049 build_sese_loop_nests (region
);
3050 build_sese_conditions (region
);
3051 find_scop_parameters (scop
);
3053 max_dim
= PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS
);
3054 if (scop_nb_params (scop
) > max_dim
)
3057 build_scop_iteration_domain (scop
);
3058 build_scop_context (scop
);
3060 add_conditions_to_constraints (scop
);
3062 build_scop_scattering (scop
);
3063 build_scop_drs (scop
);
3065 /* This SCoP has been translated to the polyhedral
3067 POLY_SCOP_P (scop
) = true;