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
);
193 free (bap
->alias_set
);
202 free_gimple_bb (struct gimple_bb
*gbb
)
204 free_data_refs_aux (GBB_DATA_REFS (gbb
));
205 free_data_refs (GBB_DATA_REFS (gbb
));
207 VEC_free (gimple
, heap
, GBB_CONDITIONS (gbb
));
208 VEC_free (gimple
, heap
, GBB_CONDITION_CASES (gbb
));
209 GBB_BB (gbb
)->aux
= 0;
213 /* Deletes all gimple bbs in SCOP. */
216 remove_gbbs_in_scop (scop_p scop
)
221 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
222 free_gimple_bb (PBB_BLACK_BOX (pbb
));
225 /* Deletes all scops in SCOPS. */
228 free_scops (VEC (scop_p
, heap
) *scops
)
233 FOR_EACH_VEC_ELT (scop_p
, scops
, i
, scop
)
235 remove_gbbs_in_scop (scop
);
236 free_sese (SCOP_REGION (scop
));
240 VEC_free (scop_p
, heap
, scops
);
243 /* Same as outermost_loop_in_sese, returns the outermost loop
244 containing BB in REGION, but makes sure that the returned loop
245 belongs to the REGION, and so this returns the first loop in the
246 REGION when the loop containing BB does not belong to REGION. */
249 outermost_loop_in_sese_1 (sese region
, basic_block bb
)
251 loop_p nest
= outermost_loop_in_sese (region
, bb
);
253 if (loop_in_sese_p (nest
, region
))
256 /* When the basic block BB does not belong to a loop in the region,
257 return the first loop in the region. */
260 if (loop_in_sese_p (nest
, region
))
269 /* Generates a polyhedral black box only if the bb contains interesting
273 try_generate_gimple_bb (scop_p scop
, basic_block bb
)
275 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 5);
276 sese region
= SCOP_REGION (scop
);
277 loop_p nest
= outermost_loop_in_sese_1 (region
, bb
);
278 gimple_stmt_iterator gsi
;
280 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
282 gimple stmt
= gsi_stmt (gsi
);
285 if (is_gimple_debug (stmt
))
288 loop
= loop_containing_stmt (stmt
);
289 if (!loop_in_sese_p (loop
, region
))
292 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
295 return new_gimple_bb (bb
, drs
);
298 /* Returns true if all predecessors of BB, that are not dominated by BB, are
299 marked in MAP. The predecessors dominated by BB are loop latches and will
300 be handled after BB. */
303 all_non_dominated_preds_marked_p (basic_block bb
, sbitmap map
)
308 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
309 if (!TEST_BIT (map
, e
->src
->index
)
310 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
316 /* Compare the depth of two basic_block's P1 and P2. */
319 compare_bb_depths (const void *p1
, const void *p2
)
321 const_basic_block
const bb1
= *(const_basic_block
const*)p1
;
322 const_basic_block
const bb2
= *(const_basic_block
const*)p2
;
323 int d1
= loop_depth (bb1
->loop_father
);
324 int d2
= loop_depth (bb2
->loop_father
);
335 /* Sort the basic blocks from DOM such that the first are the ones at
336 a deepest loop level. */
339 graphite_sort_dominated_info (VEC (basic_block
, heap
) *dom
)
341 VEC_qsort (basic_block
, dom
, compare_bb_depths
);
344 /* Recursive helper function for build_scops_bbs. */
347 build_scop_bbs_1 (scop_p scop
, sbitmap visited
, basic_block bb
)
349 sese region
= SCOP_REGION (scop
);
350 VEC (basic_block
, heap
) *dom
;
353 if (TEST_BIT (visited
, bb
->index
)
354 || !bb_in_sese_p (bb
, region
))
357 pbb
= new_poly_bb (scop
, try_generate_gimple_bb (scop
, bb
));
358 VEC_safe_push (poly_bb_p
, heap
, SCOP_BBS (scop
), pbb
);
359 SET_BIT (visited
, bb
->index
);
361 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
366 graphite_sort_dominated_info (dom
);
368 while (!VEC_empty (basic_block
, dom
))
373 FOR_EACH_VEC_ELT (basic_block
, dom
, i
, dom_bb
)
374 if (all_non_dominated_preds_marked_p (dom_bb
, visited
))
376 build_scop_bbs_1 (scop
, visited
, dom_bb
);
377 VEC_unordered_remove (basic_block
, dom
, i
);
382 VEC_free (basic_block
, heap
, dom
);
385 /* Gather the basic blocks belonging to the SCOP. */
388 build_scop_bbs (scop_p scop
)
390 sbitmap visited
= sbitmap_alloc (last_basic_block
);
391 sese region
= SCOP_REGION (scop
);
393 sbitmap_zero (visited
);
394 build_scop_bbs_1 (scop
, visited
, SESE_ENTRY_BB (region
));
395 sbitmap_free (visited
);
398 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
399 We generate SCATTERING_DIMENSIONS scattering dimensions.
401 CLooG 0.15.0 and previous versions require, that all
402 scattering functions of one CloogProgram have the same number of
403 scattering dimensions, therefore we allow to specify it. This
404 should be removed in future versions of CLooG.
406 The scattering polyhedron consists of these dimensions: scattering,
407 loop_iterators, parameters.
411 | scattering_dimensions = 5
412 | used_scattering_dimensions = 3
420 | Scattering polyhedron:
422 | scattering: {s1, s2, s3, s4, s5}
423 | loop_iterators: {i}
424 | parameters: {p1, p2}
426 | s1 s2 s3 s4 s5 i p1 p2 1
427 | 1 0 0 0 0 0 0 0 -4 = 0
428 | 0 1 0 0 0 -1 0 0 0 = 0
429 | 0 0 1 0 0 0 0 0 -5 = 0 */
432 build_pbb_scattering_polyhedrons (ppl_Linear_Expression_t static_schedule
,
433 poly_bb_p pbb
, int scattering_dimensions
)
436 scop_p scop
= PBB_SCOP (pbb
);
437 int nb_iterators
= pbb_dim_iter_domain (pbb
);
438 int used_scattering_dimensions
= nb_iterators
* 2 + 1;
439 int nb_params
= scop_nb_params (scop
);
441 ppl_dimension_type dim
= scattering_dimensions
+ nb_iterators
+ nb_params
;
444 gcc_assert (scattering_dimensions
>= used_scattering_dimensions
);
447 ppl_new_Coefficient (&c
);
448 PBB_TRANSFORMED (pbb
) = poly_scattering_new ();
449 ppl_new_C_Polyhedron_from_space_dimension
450 (&PBB_TRANSFORMED_SCATTERING (pbb
), dim
, 0);
452 PBB_NB_SCATTERING_TRANSFORM (pbb
) = scattering_dimensions
;
454 for (i
= 0; i
< scattering_dimensions
; i
++)
456 ppl_Constraint_t cstr
;
457 ppl_Linear_Expression_t expr
;
459 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
461 ppl_assign_Coefficient_from_mpz_t (c
, v
);
462 ppl_Linear_Expression_add_to_coefficient (expr
, i
, c
);
464 /* Textual order inside this loop. */
467 ppl_Linear_Expression_coefficient (static_schedule
, i
/ 2, c
);
468 ppl_Coefficient_to_mpz_t (c
, v
);
470 ppl_assign_Coefficient_from_mpz_t (c
, v
);
471 ppl_Linear_Expression_add_to_inhomogeneous (expr
, c
);
474 /* Iterations of this loop. */
475 else /* if ((i % 2) == 1) */
477 int loop
= (i
- 1) / 2;
480 ppl_assign_Coefficient_from_mpz_t (c
, v
);
481 ppl_Linear_Expression_add_to_coefficient
482 (expr
, scattering_dimensions
+ loop
, c
);
485 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
486 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb
), cstr
);
487 ppl_delete_Linear_Expression (expr
);
488 ppl_delete_Constraint (cstr
);
492 ppl_delete_Coefficient (c
);
494 PBB_ORIGINAL (pbb
) = poly_scattering_copy (PBB_TRANSFORMED (pbb
));
497 /* Build for BB the static schedule.
499 The static schedule is a Dewey numbering of the abstract syntax
500 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
502 The following example informally defines the static schedule:
521 Static schedules for A to F:
534 build_scop_scattering (scop_p scop
)
538 gimple_bb_p previous_gbb
= NULL
;
539 ppl_Linear_Expression_t static_schedule
;
544 ppl_new_Coefficient (&c
);
545 ppl_new_Linear_Expression (&static_schedule
);
547 /* We have to start schedules at 0 on the first component and
548 because we cannot compare_prefix_loops against a previous loop,
549 prefix will be equal to zero, and that index will be
550 incremented before copying. */
552 ppl_assign_Coefficient_from_mpz_t (c
, v
);
553 ppl_Linear_Expression_add_to_coefficient (static_schedule
, 0, c
);
555 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
557 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
558 ppl_Linear_Expression_t common
;
560 int nb_scat_dims
= pbb_dim_iter_domain (pbb
) * 2 + 1;
563 prefix
= nb_common_loops (SCOP_REGION (scop
), previous_gbb
, gbb
);
568 ppl_new_Linear_Expression_with_dimension (&common
, prefix
+ 1);
569 ppl_assign_Linear_Expression_from_Linear_Expression (common
,
573 ppl_assign_Coefficient_from_mpz_t (c
, v
);
574 ppl_Linear_Expression_add_to_coefficient (common
, prefix
, c
);
575 ppl_assign_Linear_Expression_from_Linear_Expression (static_schedule
,
578 build_pbb_scattering_polyhedrons (common
, pbb
, nb_scat_dims
);
580 ppl_delete_Linear_Expression (common
);
584 ppl_delete_Coefficient (c
);
585 ppl_delete_Linear_Expression (static_schedule
);
588 /* Add the value K to the dimension D of the linear expression EXPR. */
591 add_value_to_dim (ppl_dimension_type d
, ppl_Linear_Expression_t expr
,
595 ppl_Coefficient_t coef
;
597 ppl_new_Coefficient (&coef
);
598 ppl_Linear_Expression_coefficient (expr
, d
, coef
);
600 ppl_Coefficient_to_mpz_t (coef
, val
);
602 mpz_add (val
, val
, k
);
604 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
605 ppl_Linear_Expression_add_to_coefficient (expr
, d
, coef
);
607 ppl_delete_Coefficient (coef
);
610 /* In the context of scop S, scan E, the right hand side of a scalar
611 evolution function in loop VAR, and translate it to a linear
615 scan_tree_for_params_right_scev (sese s
, tree e
, int var
,
616 ppl_Linear_Expression_t expr
)
620 loop_p loop
= get_loop (var
);
621 ppl_dimension_type l
= sese_loop_depth (s
, loop
) - 1;
624 /* Scalar evolutions should happen in the sese region. */
625 gcc_assert (sese_loop_depth (s
, loop
) > 0);
627 /* We can not deal with parametric strides like:
633 gcc_assert (TREE_CODE (e
) == INTEGER_CST
);
636 tree_int_to_gmp (e
, val
);
637 add_value_to_dim (l
, expr
, val
);
642 /* Scan the integer constant CST, and add it to the inhomogeneous part of the
643 linear expression EXPR. K is the multiplier of the constant. */
646 scan_tree_for_params_int (tree cst
, ppl_Linear_Expression_t expr
, mpz_t k
)
649 ppl_Coefficient_t coef
;
650 tree type
= TREE_TYPE (cst
);
654 /* Necessary to not get "-1 = 2^n - 1". */
655 mpz_set_double_int (val
, double_int_sext (tree_to_double_int (cst
),
656 TYPE_PRECISION (type
)), false);
658 mpz_mul (val
, val
, k
);
659 ppl_new_Coefficient (&coef
);
660 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
661 ppl_Linear_Expression_add_to_inhomogeneous (expr
, coef
);
663 ppl_delete_Coefficient (coef
);
666 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
667 Otherwise returns -1. */
670 parameter_index_in_region_1 (tree name
, sese region
)
675 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
677 FOR_EACH_VEC_ELT (tree
, SESE_PARAMS (region
), i
, p
)
684 /* When the parameter NAME is in REGION, returns its index in
685 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
686 and returns the index of NAME. */
689 parameter_index_in_region (tree name
, sese region
)
693 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
695 i
= parameter_index_in_region_1 (name
, region
);
699 gcc_assert (SESE_ADD_PARAMS (region
));
701 i
= VEC_length (tree
, SESE_PARAMS (region
));
702 VEC_safe_push (tree
, heap
, SESE_PARAMS (region
), name
);
706 /* In the context of sese S, scan the expression E and translate it to
707 a linear expression C. When parsing a symbolic multiplication, K
708 represents the constant multiplier of an expression containing
712 scan_tree_for_params (sese s
, tree e
, ppl_Linear_Expression_t c
,
715 if (e
== chrec_dont_know
)
718 switch (TREE_CODE (e
))
720 case POLYNOMIAL_CHREC
:
721 scan_tree_for_params_right_scev (s
, CHREC_RIGHT (e
),
722 CHREC_VARIABLE (e
), c
);
723 scan_tree_for_params (s
, CHREC_LEFT (e
), c
, k
);
727 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
732 gcc_assert (host_integerp (TREE_OPERAND (e
, 1), 0));
734 tree_int_to_gmp (TREE_OPERAND (e
, 1), val
);
735 mpz_mul (val
, val
, k
);
736 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, val
);
740 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
747 gcc_assert (host_integerp (TREE_OPERAND (e
, 0), 0));
749 tree_int_to_gmp (TREE_OPERAND (e
, 0), val
);
750 mpz_mul (val
, val
, k
);
751 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, val
);
755 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
760 case POINTER_PLUS_EXPR
:
761 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
762 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
767 ppl_Linear_Expression_t tmp_expr
= NULL
;
771 ppl_dimension_type dim
;
772 ppl_Linear_Expression_space_dimension (c
, &dim
);
773 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
776 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
777 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), tmp_expr
, k
);
781 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
783 ppl_delete_Linear_Expression (tmp_expr
);
791 ppl_Linear_Expression_t tmp_expr
= NULL
;
795 ppl_dimension_type dim
;
796 ppl_Linear_Expression_space_dimension (c
, &dim
);
797 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
800 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
804 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
806 ppl_delete_Linear_Expression (tmp_expr
);
814 ppl_Linear_Expression_t tmp_expr
= NULL
;
818 ppl_dimension_type dim
;
819 ppl_Linear_Expression_space_dimension (c
, &dim
);
820 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
823 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
827 ppl_Coefficient_t coef
;
830 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
832 ppl_delete_Linear_Expression (tmp_expr
);
833 mpz_init (minus_one
);
834 mpz_set_si (minus_one
, -1);
835 ppl_new_Coefficient_from_mpz_t (&coef
, minus_one
);
836 ppl_Linear_Expression_add_to_inhomogeneous (c
, coef
);
837 mpz_clear (minus_one
);
838 ppl_delete_Coefficient (coef
);
846 ppl_dimension_type p
= parameter_index_in_region (e
, s
);
850 ppl_dimension_type dim
;
851 ppl_Linear_Expression_space_dimension (c
, &dim
);
852 p
+= dim
- sese_nb_params (s
);
853 add_value_to_dim (p
, c
, k
);
860 scan_tree_for_params_int (e
, c
, k
);
864 case NON_LVALUE_EXPR
:
865 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
877 /* Find parameters with respect to REGION in BB. We are looking in memory
878 access functions, conditions and loop bounds. */
881 find_params_in_bb (sese region
, gimple_bb_p gbb
)
887 loop_p loop
= GBB_BB (gbb
)->loop_father
;
893 /* Find parameters in the access functions of data references. */
894 FOR_EACH_VEC_ELT (data_reference_p
, GBB_DATA_REFS (gbb
), i
, dr
)
895 for (j
= 0; j
< DR_NUM_DIMENSIONS (dr
); j
++)
896 scan_tree_for_params (region
, DR_ACCESS_FN (dr
, j
), NULL
, one
);
898 /* Find parameters in conditional statements. */
899 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
901 tree lhs
= scalar_evolution_in_region (region
, loop
,
902 gimple_cond_lhs (stmt
));
903 tree rhs
= scalar_evolution_in_region (region
, loop
,
904 gimple_cond_rhs (stmt
));
906 scan_tree_for_params (region
, lhs
, NULL
, one
);
907 scan_tree_for_params (region
, rhs
, NULL
, one
);
913 /* Record the parameters used in the SCOP. A variable is a parameter
914 in a scop if it does not vary during the execution of that scop. */
917 find_scop_parameters (scop_p scop
)
921 sese region
= SCOP_REGION (scop
);
928 /* Find the parameters used in the loop bounds. */
929 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
931 tree nb_iters
= number_of_latch_executions (loop
);
933 if (!chrec_contains_symbols (nb_iters
))
936 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
937 scan_tree_for_params (region
, nb_iters
, NULL
, one
);
942 /* Find the parameters used in data accesses. */
943 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
944 find_params_in_bb (region
, PBB_BLACK_BOX (pbb
));
946 scop_set_nb_params (scop
, sese_nb_params (region
));
947 SESE_ADD_PARAMS (region
) = false;
949 ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension
950 (&SCOP_CONTEXT (scop
), scop_nb_params (scop
), 0);
953 /* Insert in the SCOP context constraints from the estimation of the
954 number of iterations. UB_EXPR is a linear expression describing
955 the number of iterations in a loop. This expression is bounded by
956 the estimation NIT. */
959 add_upper_bounds_from_estimated_nit (scop_p scop
, double_int nit
,
960 ppl_dimension_type dim
,
961 ppl_Linear_Expression_t ub_expr
)
964 ppl_Linear_Expression_t nb_iters_le
;
965 ppl_Polyhedron_t pol
;
966 ppl_Coefficient_t coef
;
969 ppl_new_C_Polyhedron_from_space_dimension (&pol
, dim
, 0);
970 ppl_new_Linear_Expression_from_Linear_Expression (&nb_iters_le
,
973 /* Construct the negated number of last iteration in VAL. */
975 mpz_set_double_int (val
, nit
, false);
976 mpz_sub_ui (val
, val
, 1);
979 /* NB_ITERS_LE holds the number of last iteration in
980 parametrical form. Subtract estimated number of last
981 iteration and assert that result is not positive. */
982 ppl_new_Coefficient_from_mpz_t (&coef
, val
);
983 ppl_Linear_Expression_add_to_inhomogeneous (nb_iters_le
, coef
);
984 ppl_delete_Coefficient (coef
);
985 ppl_new_Constraint (&ub
, nb_iters_le
,
986 PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
987 ppl_Polyhedron_add_constraint (pol
, ub
);
989 /* Remove all but last GDIM dimensions from POL to obtain
990 only the constraints on the parameters. */
992 graphite_dim_t gdim
= scop_nb_params (scop
);
993 ppl_dimension_type
*dims
= XNEWVEC (ppl_dimension_type
, dim
- gdim
);
996 for (i
= 0; i
< dim
- gdim
; i
++)
999 ppl_Polyhedron_remove_space_dimensions (pol
, dims
, dim
- gdim
);
1003 /* Add the constraints on the parameters to the SCoP context. */
1005 ppl_Pointset_Powerset_C_Polyhedron_t constraints_ps
;
1007 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1008 (&constraints_ps
, pol
);
1009 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1010 (SCOP_CONTEXT (scop
), constraints_ps
);
1011 ppl_delete_Pointset_Powerset_C_Polyhedron (constraints_ps
);
1014 ppl_delete_Polyhedron (pol
);
1015 ppl_delete_Linear_Expression (nb_iters_le
);
1016 ppl_delete_Constraint (ub
);
1020 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
1021 the constraints for the surrounding loops. */
1024 build_loop_iteration_domains (scop_p scop
, struct loop
*loop
,
1025 ppl_Polyhedron_t outer_ph
, int nb
,
1026 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
)
1029 ppl_Polyhedron_t ph
;
1030 tree nb_iters
= number_of_latch_executions (loop
);
1031 ppl_dimension_type dim
= nb
+ 1 + scop_nb_params (scop
);
1032 sese region
= SCOP_REGION (scop
);
1035 ppl_const_Constraint_System_t pcs
;
1036 ppl_dimension_type
*map
1037 = (ppl_dimension_type
*) XNEWVEC (ppl_dimension_type
, dim
);
1039 ppl_new_C_Polyhedron_from_space_dimension (&ph
, dim
, 0);
1040 ppl_Polyhedron_get_constraints (outer_ph
, &pcs
);
1041 ppl_Polyhedron_add_constraints (ph
, pcs
);
1043 for (i
= 0; i
< (int) nb
; i
++)
1045 for (i
= (int) nb
; i
< (int) dim
- 1; i
++)
1049 ppl_Polyhedron_map_space_dimensions (ph
, map
, dim
);
1055 ppl_Constraint_t lb
;
1056 ppl_Linear_Expression_t lb_expr
;
1058 ppl_new_Linear_Expression_with_dimension (&lb_expr
, dim
);
1059 ppl_set_coef (lb_expr
, nb
, 1);
1060 ppl_new_Constraint (&lb
, lb_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1061 ppl_delete_Linear_Expression (lb_expr
);
1062 ppl_Polyhedron_add_constraint (ph
, lb
);
1063 ppl_delete_Constraint (lb
);
1066 if (TREE_CODE (nb_iters
) == INTEGER_CST
)
1068 ppl_Constraint_t ub
;
1069 ppl_Linear_Expression_t ub_expr
;
1071 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1073 /* loop_i <= cst_nb_iters */
1074 ppl_set_coef (ub_expr
, nb
, -1);
1075 ppl_set_inhomogeneous_tree (ub_expr
, nb_iters
);
1076 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1077 ppl_Polyhedron_add_constraint (ph
, ub
);
1078 ppl_delete_Linear_Expression (ub_expr
);
1079 ppl_delete_Constraint (ub
);
1081 else if (!chrec_contains_undetermined (nb_iters
))
1084 ppl_Constraint_t ub
;
1085 ppl_Linear_Expression_t ub_expr
;
1089 mpz_set_si (one
, 1);
1090 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1091 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
1092 scan_tree_for_params (SCOP_REGION (scop
), nb_iters
, ub_expr
, one
);
1095 if (estimated_loop_iterations (loop
, true, &nit
))
1096 add_upper_bounds_from_estimated_nit (scop
, nit
, dim
, ub_expr
);
1098 /* loop_i <= expr_nb_iters */
1099 ppl_set_coef (ub_expr
, nb
, -1);
1100 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1101 ppl_Polyhedron_add_constraint (ph
, ub
);
1102 ppl_delete_Linear_Expression (ub_expr
);
1103 ppl_delete_Constraint (ub
);
1108 if (loop
->inner
&& loop_in_sese_p (loop
->inner
, region
))
1109 build_loop_iteration_domains (scop
, loop
->inner
, ph
, nb
+ 1, domains
);
1113 && loop_in_sese_p (loop
->next
, region
))
1114 build_loop_iteration_domains (scop
, loop
->next
, outer_ph
, nb
, domains
);
1116 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1117 (&domains
[loop
->num
], ph
);
1119 ppl_delete_Polyhedron (ph
);
1122 /* Returns a linear expression for tree T evaluated in PBB. */
1124 static ppl_Linear_Expression_t
1125 create_linear_expr_from_tree (poly_bb_p pbb
, tree t
)
1128 ppl_Linear_Expression_t res
;
1129 ppl_dimension_type dim
;
1130 sese region
= SCOP_REGION (PBB_SCOP (pbb
));
1131 loop_p loop
= pbb_loop (pbb
);
1133 dim
= pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
1134 ppl_new_Linear_Expression_with_dimension (&res
, dim
);
1136 t
= scalar_evolution_in_region (region
, loop
, t
);
1137 gcc_assert (!automatically_generated_chrec_p (t
));
1140 mpz_set_si (one
, 1);
1141 scan_tree_for_params (region
, t
, res
, one
);
1147 /* Returns the ppl constraint type from the gimple tree code CODE. */
1149 static enum ppl_enum_Constraint_Type
1150 ppl_constraint_type_from_tree_code (enum tree_code code
)
1154 /* We do not support LT and GT to be able to work with C_Polyhedron.
1155 As we work on integer polyhedron "a < b" can be expressed by
1162 return PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
;
1165 return PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
;
1168 return PPL_CONSTRAINT_TYPE_EQUAL
;
1175 /* Add conditional statement STMT to PS. It is evaluated in PBB and
1176 CODE is used as the comparison operator. This allows us to invert the
1177 condition or to handle inequalities. */
1180 add_condition_to_domain (ppl_Pointset_Powerset_C_Polyhedron_t ps
, gimple stmt
,
1181 poly_bb_p pbb
, enum tree_code code
)
1184 ppl_Coefficient_t c
;
1185 ppl_Linear_Expression_t left
, right
;
1186 ppl_Constraint_t cstr
;
1187 enum ppl_enum_Constraint_Type type
;
1189 left
= create_linear_expr_from_tree (pbb
, gimple_cond_lhs (stmt
));
1190 right
= create_linear_expr_from_tree (pbb
, gimple_cond_rhs (stmt
));
1192 /* If we have < or > expressions convert them to <= or >= by adding 1 to
1193 the left or the right side of the expression. */
1194 if (code
== LT_EXPR
)
1198 ppl_new_Coefficient (&c
);
1199 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1200 ppl_Linear_Expression_add_to_inhomogeneous (left
, c
);
1201 ppl_delete_Coefficient (c
);
1206 else if (code
== GT_EXPR
)
1210 ppl_new_Coefficient (&c
);
1211 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1212 ppl_Linear_Expression_add_to_inhomogeneous (right
, c
);
1213 ppl_delete_Coefficient (c
);
1219 type
= ppl_constraint_type_from_tree_code (code
);
1221 ppl_subtract_Linear_Expression_from_Linear_Expression (left
, right
);
1223 ppl_new_Constraint (&cstr
, left
, type
);
1224 ppl_Pointset_Powerset_C_Polyhedron_add_constraint (ps
, cstr
);
1226 ppl_delete_Constraint (cstr
);
1227 ppl_delete_Linear_Expression (left
);
1228 ppl_delete_Linear_Expression (right
);
1231 /* Add conditional statement STMT to pbb. CODE is used as the comparision
1232 operator. This allows us to invert the condition or to handle
1236 add_condition_to_pbb (poly_bb_p pbb
, gimple stmt
, enum tree_code code
)
1238 if (code
== NE_EXPR
)
1240 ppl_Pointset_Powerset_C_Polyhedron_t left
= PBB_DOMAIN (pbb
);
1241 ppl_Pointset_Powerset_C_Polyhedron_t right
;
1242 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1244 add_condition_to_domain (left
, stmt
, pbb
, LT_EXPR
);
1245 add_condition_to_domain (right
, stmt
, pbb
, GT_EXPR
);
1246 ppl_Pointset_Powerset_C_Polyhedron_upper_bound_assign (left
, right
);
1247 ppl_delete_Pointset_Powerset_C_Polyhedron (right
);
1250 add_condition_to_domain (PBB_DOMAIN (pbb
), stmt
, pbb
, code
);
1253 /* Add conditions to the domain of PBB. */
1256 add_conditions_to_domain (poly_bb_p pbb
)
1260 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
1262 if (VEC_empty (gimple
, GBB_CONDITIONS (gbb
)))
1265 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
1266 switch (gimple_code (stmt
))
1270 enum tree_code code
= gimple_cond_code (stmt
);
1272 /* The conditions for ELSE-branches are inverted. */
1273 if (!VEC_index (gimple
, GBB_CONDITION_CASES (gbb
), i
))
1274 code
= invert_tree_comparison (code
, false);
1276 add_condition_to_pbb (pbb
, stmt
, code
);
1281 /* Switch statements are not supported right now - fall throught. */
1289 /* Traverses all the GBBs of the SCOP and add their constraints to the
1290 iteration domains. */
1293 add_conditions_to_constraints (scop_p scop
)
1298 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1299 add_conditions_to_domain (pbb
);
1302 /* Structure used to pass data to dom_walk. */
1306 VEC (gimple
, heap
) **conditions
, **cases
;
1310 /* Returns a COND_EXPR statement when BB has a single predecessor, the
1311 edge between BB and its predecessor is not a loop exit edge, and
1312 the last statement of the single predecessor is a COND_EXPR. */
1315 single_pred_cond_non_loop_exit (basic_block bb
)
1317 if (single_pred_p (bb
))
1319 edge e
= single_pred_edge (bb
);
1320 basic_block pred
= e
->src
;
1323 if (loop_depth (pred
->loop_father
) > loop_depth (bb
->loop_father
))
1326 stmt
= last_stmt (pred
);
1328 if (stmt
&& gimple_code (stmt
) == GIMPLE_COND
)
1335 /* Call-back for dom_walk executed before visiting the dominated
1339 build_sese_conditions_before (struct dom_walk_data
*dw_data
,
1342 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1343 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1344 VEC (gimple
, heap
) **cases
= data
->cases
;
1348 if (!bb_in_sese_p (bb
, data
->region
))
1351 stmt
= single_pred_cond_non_loop_exit (bb
);
1355 edge e
= single_pred_edge (bb
);
1357 VEC_safe_push (gimple
, heap
, *conditions
, stmt
);
1359 if (e
->flags
& EDGE_TRUE_VALUE
)
1360 VEC_safe_push (gimple
, heap
, *cases
, stmt
);
1362 VEC_safe_push (gimple
, heap
, *cases
, NULL
);
1365 gbb
= gbb_from_bb (bb
);
1369 GBB_CONDITIONS (gbb
) = VEC_copy (gimple
, heap
, *conditions
);
1370 GBB_CONDITION_CASES (gbb
) = VEC_copy (gimple
, heap
, *cases
);
1374 /* Call-back for dom_walk executed after visiting the dominated
1378 build_sese_conditions_after (struct dom_walk_data
*dw_data
,
1381 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1382 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1383 VEC (gimple
, heap
) **cases
= data
->cases
;
1385 if (!bb_in_sese_p (bb
, data
->region
))
1388 if (single_pred_cond_non_loop_exit (bb
))
1390 VEC_pop (gimple
, *conditions
);
1391 VEC_pop (gimple
, *cases
);
1395 /* Record all conditions in REGION. */
1398 build_sese_conditions (sese region
)
1400 struct dom_walk_data walk_data
;
1401 VEC (gimple
, heap
) *conditions
= VEC_alloc (gimple
, heap
, 3);
1402 VEC (gimple
, heap
) *cases
= VEC_alloc (gimple
, heap
, 3);
1405 data
.conditions
= &conditions
;
1406 data
.cases
= &cases
;
1407 data
.region
= region
;
1409 walk_data
.dom_direction
= CDI_DOMINATORS
;
1410 walk_data
.initialize_block_local_data
= NULL
;
1411 walk_data
.before_dom_children
= build_sese_conditions_before
;
1412 walk_data
.after_dom_children
= build_sese_conditions_after
;
1413 walk_data
.global_data
= &data
;
1414 walk_data
.block_local_data_size
= 0;
1416 init_walk_dominator_tree (&walk_data
);
1417 walk_dominator_tree (&walk_data
, SESE_ENTRY_BB (region
));
1418 fini_walk_dominator_tree (&walk_data
);
1420 VEC_free (gimple
, heap
, conditions
);
1421 VEC_free (gimple
, heap
, cases
);
1424 /* Add constraints on the possible values of parameter P from the type
1428 add_param_constraints (scop_p scop
, ppl_Polyhedron_t context
, graphite_dim_t p
)
1430 ppl_Constraint_t cstr
;
1431 ppl_Linear_Expression_t le
;
1432 tree parameter
= VEC_index (tree
, SESE_PARAMS (SCOP_REGION (scop
)), p
);
1433 tree type
= TREE_TYPE (parameter
);
1434 tree lb
= NULL_TREE
;
1435 tree ub
= NULL_TREE
;
1437 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1438 lb
= lower_bound_in_type (type
, type
);
1440 lb
= TYPE_MIN_VALUE (type
);
1442 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1443 ub
= upper_bound_in_type (type
, type
);
1445 ub
= TYPE_MAX_VALUE (type
);
1449 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1450 ppl_set_coef (le
, p
, -1);
1451 ppl_set_inhomogeneous_tree (le
, lb
);
1452 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
1453 ppl_Polyhedron_add_constraint (context
, cstr
);
1454 ppl_delete_Linear_Expression (le
);
1455 ppl_delete_Constraint (cstr
);
1460 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1461 ppl_set_coef (le
, p
, -1);
1462 ppl_set_inhomogeneous_tree (le
, ub
);
1463 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1464 ppl_Polyhedron_add_constraint (context
, cstr
);
1465 ppl_delete_Linear_Expression (le
);
1466 ppl_delete_Constraint (cstr
);
1470 /* Build the context of the SCOP. The context usually contains extra
1471 constraints that are added to the iteration domains that constrain
1475 build_scop_context (scop_p scop
)
1477 ppl_Polyhedron_t context
;
1478 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
1479 graphite_dim_t p
, n
= scop_nb_params (scop
);
1481 ppl_new_C_Polyhedron_from_space_dimension (&context
, n
, 0);
1483 for (p
= 0; p
< n
; p
++)
1484 add_param_constraints (scop
, context
, p
);
1486 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1488 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1489 (SCOP_CONTEXT (scop
), ps
);
1491 ppl_delete_Pointset_Powerset_C_Polyhedron (ps
);
1492 ppl_delete_Polyhedron (context
);
1495 /* Build the iteration domains: the loops belonging to the current
1496 SCOP, and that vary for the execution of the current basic block.
1497 Returns false if there is no loop in SCOP. */
1500 build_scop_iteration_domain (scop_p scop
)
1503 sese region
= SCOP_REGION (scop
);
1505 ppl_Polyhedron_t ph
;
1507 int nb_loops
= number_of_loops ();
1508 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
1509 = XNEWVEC (ppl_Pointset_Powerset_C_Polyhedron_t
, nb_loops
);
1511 for (i
= 0; i
< nb_loops
; i
++)
1514 ppl_new_C_Polyhedron_from_space_dimension (&ph
, scop_nb_params (scop
), 0);
1516 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
1517 if (!loop_in_sese_p (loop_outer (loop
), region
))
1518 build_loop_iteration_domains (scop
, loop
, ph
, 0, domains
);
1520 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1521 if (domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
])
1522 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1523 (&PBB_DOMAIN (pbb
), (ppl_const_Pointset_Powerset_C_Polyhedron_t
)
1524 domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
]);
1526 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1527 (&PBB_DOMAIN (pbb
), ph
);
1529 for (i
= 0; i
< nb_loops
; i
++)
1531 ppl_delete_Pointset_Powerset_C_Polyhedron (domains
[i
]);
1533 ppl_delete_Polyhedron (ph
);
1537 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1538 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1539 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1543 pdr_add_alias_set (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1544 ppl_dimension_type accessp_nb_dims
,
1545 ppl_dimension_type dom_nb_dims
)
1547 ppl_Linear_Expression_t alias
;
1548 ppl_Constraint_t cstr
;
1549 int alias_set_num
= 0;
1550 base_alias_pair
*bap
= (base_alias_pair
*)(dr
->aux
);
1552 if (bap
&& bap
->alias_set
)
1553 alias_set_num
= *(bap
->alias_set
);
1555 ppl_new_Linear_Expression_with_dimension (&alias
, accessp_nb_dims
);
1557 ppl_set_coef (alias
, dom_nb_dims
, 1);
1558 ppl_set_inhomogeneous (alias
, -alias_set_num
);
1559 ppl_new_Constraint (&cstr
, alias
, PPL_CONSTRAINT_TYPE_EQUAL
);
1560 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1562 ppl_delete_Linear_Expression (alias
);
1563 ppl_delete_Constraint (cstr
);
1566 /* Add to ACCESSES polyhedron equalities defining the access functions
1567 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1568 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1569 PBB is the poly_bb_p that contains the data reference DR. */
1572 pdr_add_memory_accesses (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1573 ppl_dimension_type accessp_nb_dims
,
1574 ppl_dimension_type dom_nb_dims
,
1577 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1579 scop_p scop
= PBB_SCOP (pbb
);
1580 sese region
= SCOP_REGION (scop
);
1584 for (i
= 0; i
< nb_subscripts
; i
++)
1586 ppl_Linear_Expression_t fn
, access
;
1587 ppl_Constraint_t cstr
;
1588 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1589 tree afn
= DR_ACCESS_FN (dr
, nb_subscripts
- 1 - i
);
1591 ppl_new_Linear_Expression_with_dimension (&fn
, dom_nb_dims
);
1592 ppl_new_Linear_Expression_with_dimension (&access
, accessp_nb_dims
);
1595 scan_tree_for_params (region
, afn
, fn
, v
);
1596 ppl_assign_Linear_Expression_from_Linear_Expression (access
, fn
);
1598 ppl_set_coef (access
, subscript
, -1);
1599 ppl_new_Constraint (&cstr
, access
, PPL_CONSTRAINT_TYPE_EQUAL
);
1600 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1602 ppl_delete_Linear_Expression (fn
);
1603 ppl_delete_Linear_Expression (access
);
1604 ppl_delete_Constraint (cstr
);
1610 /* Add constrains representing the size of the accessed data to the
1611 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1612 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1616 pdr_add_data_dimensions (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1617 ppl_dimension_type accessp_nb_dims
,
1618 ppl_dimension_type dom_nb_dims
)
1620 tree ref
= DR_REF (dr
);
1621 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1623 for (i
= nb_subscripts
- 1; i
>= 0; i
--, ref
= TREE_OPERAND (ref
, 0))
1625 ppl_Linear_Expression_t expr
;
1626 ppl_Constraint_t cstr
;
1627 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1630 if (TREE_CODE (ref
) != ARRAY_REF
)
1633 low
= array_ref_low_bound (ref
);
1635 /* subscript - low >= 0 */
1636 if (host_integerp (low
, 0))
1640 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1641 ppl_set_coef (expr
, subscript
, 1);
1643 minus_low
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (low
), low
);
1644 ppl_set_inhomogeneous_tree (expr
, minus_low
);
1646 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1647 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1648 ppl_delete_Linear_Expression (expr
);
1649 ppl_delete_Constraint (cstr
);
1652 high
= array_ref_up_bound (ref
);
1654 /* high - subscript >= 0 */
1655 if (high
&& host_integerp (high
, 0)
1656 /* 1-element arrays at end of structures may extend over
1657 their declared size. */
1658 && !(array_at_struct_end_p (ref
)
1659 && operand_equal_p (low
, high
, 0)))
1661 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1662 ppl_set_coef (expr
, subscript
, -1);
1664 ppl_set_inhomogeneous_tree (expr
, high
);
1666 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1667 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1668 ppl_delete_Linear_Expression (expr
);
1669 ppl_delete_Constraint (cstr
);
1674 /* Build data accesses for DR in PBB. */
1677 build_poly_dr (data_reference_p dr
, poly_bb_p pbb
)
1679 ppl_Polyhedron_t accesses
;
1680 ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps
;
1681 ppl_dimension_type dom_nb_dims
;
1682 ppl_dimension_type accessp_nb_dims
;
1683 int dr_base_object_set
;
1685 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb
),
1687 accessp_nb_dims
= dom_nb_dims
+ 1 + DR_NUM_DIMENSIONS (dr
);
1689 ppl_new_C_Polyhedron_from_space_dimension (&accesses
, accessp_nb_dims
, 0);
1691 pdr_add_alias_set (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1692 pdr_add_memory_accesses (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
, pbb
);
1693 pdr_add_data_dimensions (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1695 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps
,
1697 ppl_delete_Polyhedron (accesses
);
1699 gcc_assert (dr
->aux
);
1700 dr_base_object_set
= ((base_alias_pair
*)(dr
->aux
))->base_obj_set
;
1702 new_poly_dr (pbb
, dr_base_object_set
, accesses_ps
,
1703 DR_IS_READ (dr
) ? PDR_READ
: PDR_WRITE
,
1704 dr
, DR_NUM_DIMENSIONS (dr
));
1707 /* Write to FILE the alias graph of data references in DIMACS format. */
1710 write_alias_graph_to_ascii_dimacs (FILE *file
, char *comment
,
1711 VEC (data_reference_p
, heap
) *drs
)
1713 int num_vertex
= VEC_length (data_reference_p
, drs
);
1715 data_reference_p dr1
, dr2
;
1718 if (num_vertex
== 0)
1721 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1722 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1723 if (dr_may_alias_p (dr1
, dr2
))
1726 fprintf (file
, "$\n");
1729 fprintf (file
, "c %s\n", comment
);
1731 fprintf (file
, "p edge %d %d\n", num_vertex
, edge_num
);
1733 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1734 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1735 if (dr_may_alias_p (dr1
, dr2
))
1736 fprintf (file
, "e %d %d\n", i
+ 1, j
+ 1);
1741 /* Write to FILE the alias graph of data references in DOT format. */
1744 write_alias_graph_to_ascii_dot (FILE *file
, char *comment
,
1745 VEC (data_reference_p
, heap
) *drs
)
1747 int num_vertex
= VEC_length (data_reference_p
, drs
);
1748 data_reference_p dr1
, dr2
;
1751 if (num_vertex
== 0)
1754 fprintf (file
, "$\n");
1757 fprintf (file
, "c %s\n", comment
);
1759 /* First print all the vertices. */
1760 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1761 fprintf (file
, "n%d;\n", i
);
1763 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1764 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1765 if (dr_may_alias_p (dr1
, dr2
))
1766 fprintf (file
, "n%d n%d\n", i
, j
);
1771 /* Write to FILE the alias graph of data references in ECC format. */
1774 write_alias_graph_to_ascii_ecc (FILE *file
, char *comment
,
1775 VEC (data_reference_p
, heap
) *drs
)
1777 int num_vertex
= VEC_length (data_reference_p
, drs
);
1778 data_reference_p dr1
, dr2
;
1781 if (num_vertex
== 0)
1784 fprintf (file
, "$\n");
1787 fprintf (file
, "c %s\n", comment
);
1789 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1790 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1791 if (dr_may_alias_p (dr1
, dr2
))
1792 fprintf (file
, "%d %d\n", i
, j
);
1797 /* Check if DR1 and DR2 are in the same object set. */
1800 dr_same_base_object_p (const struct data_reference
*dr1
,
1801 const struct data_reference
*dr2
)
1803 return operand_equal_p (DR_BASE_OBJECT (dr1
), DR_BASE_OBJECT (dr2
), 0);
1806 /* Uses DFS component number as representative of alias-sets. Also tests for
1807 optimality by verifying if every connected component is a clique. Returns
1808 true (1) if the above test is true, and false (0) otherwise. */
1811 build_alias_set_optimal_p (VEC (data_reference_p
, heap
) *drs
)
1813 int num_vertices
= VEC_length (data_reference_p
, drs
);
1814 struct graph
*g
= new_graph (num_vertices
);
1815 data_reference_p dr1
, dr2
;
1817 int num_connected_components
;
1818 int v_indx1
, v_indx2
, num_vertices_in_component
;
1821 struct graph_edge
*e
;
1822 int this_component_is_clique
;
1823 int all_components_are_cliques
= 1;
1825 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1826 for (j
= i
+1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1827 if (dr_may_alias_p (dr1
, dr2
))
1833 all_vertices
= XNEWVEC (int, num_vertices
);
1834 vertices
= XNEWVEC (int, num_vertices
);
1835 for (i
= 0; i
< num_vertices
; i
++)
1836 all_vertices
[i
] = i
;
1838 num_connected_components
= graphds_dfs (g
, all_vertices
, num_vertices
,
1840 for (i
= 0; i
< g
->n_vertices
; i
++)
1842 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1843 base_alias_pair
*bap
;
1845 gcc_assert (dr
->aux
);
1846 bap
= (base_alias_pair
*)(dr
->aux
);
1848 bap
->alias_set
= XNEW (int);
1849 *(bap
->alias_set
) = g
->vertices
[i
].component
+ 1;
1852 /* Verify if the DFS numbering results in optimal solution. */
1853 for (i
= 0; i
< num_connected_components
; i
++)
1855 num_vertices_in_component
= 0;
1856 /* Get all vertices whose DFS component number is the same as i. */
1857 for (j
= 0; j
< num_vertices
; j
++)
1858 if (g
->vertices
[j
].component
== i
)
1859 vertices
[num_vertices_in_component
++] = j
;
1861 /* Now test if the vertices in 'vertices' form a clique, by testing
1862 for edges among each pair. */
1863 this_component_is_clique
= 1;
1864 for (v_indx1
= 0; v_indx1
< num_vertices_in_component
; v_indx1
++)
1866 for (v_indx2
= v_indx1
+1; v_indx2
< num_vertices_in_component
; v_indx2
++)
1868 /* Check if the two vertices are connected by iterating
1869 through all the edges which have one of these are source. */
1870 e
= g
->vertices
[vertices
[v_indx2
]].pred
;
1873 if (e
->src
== vertices
[v_indx1
])
1879 this_component_is_clique
= 0;
1883 if (!this_component_is_clique
)
1884 all_components_are_cliques
= 0;
1888 free (all_vertices
);
1891 return all_components_are_cliques
;
1894 /* Group each data reference in DRS with its base object set num. */
1897 build_base_obj_set_for_drs (VEC (data_reference_p
, heap
) *drs
)
1899 int num_vertex
= VEC_length (data_reference_p
, drs
);
1900 struct graph
*g
= new_graph (num_vertex
);
1901 data_reference_p dr1
, dr2
;
1905 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1906 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1907 if (dr_same_base_object_p (dr1
, dr2
))
1913 queue
= XNEWVEC (int, num_vertex
);
1914 for (i
= 0; i
< num_vertex
; i
++)
1917 graphds_dfs (g
, queue
, num_vertex
, NULL
, true, NULL
);
1919 for (i
= 0; i
< g
->n_vertices
; i
++)
1921 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1922 base_alias_pair
*bap
;
1924 gcc_assert (dr
->aux
);
1925 bap
= (base_alias_pair
*)(dr
->aux
);
1927 bap
->base_obj_set
= g
->vertices
[i
].component
+ 1;
1934 /* Build the data references for PBB. */
1937 build_pbb_drs (poly_bb_p pbb
)
1940 data_reference_p dr
;
1941 VEC (data_reference_p
, heap
) *gbb_drs
= GBB_DATA_REFS (PBB_BLACK_BOX (pbb
));
1943 FOR_EACH_VEC_ELT (data_reference_p
, gbb_drs
, j
, dr
)
1944 build_poly_dr (dr
, pbb
);
1947 /* Dump to file the alias graphs for the data references in DRS. */
1950 dump_alias_graphs (VEC (data_reference_p
, heap
) *drs
)
1953 FILE *file_dimacs
, *file_ecc
, *file_dot
;
1955 file_dimacs
= fopen ("/tmp/dr_alias_graph_dimacs", "ab");
1958 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1959 current_function_name ());
1960 write_alias_graph_to_ascii_dimacs (file_dimacs
, comment
, drs
);
1961 fclose (file_dimacs
);
1964 file_ecc
= fopen ("/tmp/dr_alias_graph_ecc", "ab");
1967 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1968 current_function_name ());
1969 write_alias_graph_to_ascii_ecc (file_ecc
, comment
, drs
);
1973 file_dot
= fopen ("/tmp/dr_alias_graph_dot", "ab");
1976 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1977 current_function_name ());
1978 write_alias_graph_to_ascii_dot (file_dot
, comment
, drs
);
1983 /* Build data references in SCOP. */
1986 build_scop_drs (scop_p scop
)
1990 data_reference_p dr
;
1991 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 3);
1993 /* Remove all the PBBs that do not have data references: these basic
1994 blocks are not handled in the polyhedral representation. */
1995 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1996 if (VEC_empty (data_reference_p
, GBB_DATA_REFS (PBB_BLACK_BOX (pbb
))))
1998 free_gimple_bb (PBB_BLACK_BOX (pbb
));
1999 VEC_ordered_remove (poly_bb_p
, SCOP_BBS (scop
), i
);
2003 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2004 for (j
= 0; VEC_iterate (data_reference_p
,
2005 GBB_DATA_REFS (PBB_BLACK_BOX (pbb
)), j
, dr
); j
++)
2006 VEC_safe_push (data_reference_p
, heap
, drs
, dr
);
2008 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr
)
2009 dr
->aux
= XNEW (base_alias_pair
);
2011 if (!build_alias_set_optimal_p (drs
))
2013 /* TODO: Add support when building alias set is not optimal. */
2017 build_base_obj_set_for_drs (drs
);
2019 /* When debugging, enable the following code. This cannot be used
2020 in production compilers. */
2022 dump_alias_graphs (drs
);
2024 VEC_free (data_reference_p
, heap
, drs
);
2026 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2027 build_pbb_drs (pbb
);
2030 /* Return a gsi at the position of the phi node STMT. */
2032 static gimple_stmt_iterator
2033 gsi_for_phi_node (gimple stmt
)
2035 gimple_stmt_iterator psi
;
2036 basic_block bb
= gimple_bb (stmt
);
2038 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2039 if (stmt
== gsi_stmt (psi
))
2046 /* Analyze all the data references of STMTS and add them to the
2047 GBB_DATA_REFS vector of BB. */
2050 analyze_drs_in_stmts (scop_p scop
, basic_block bb
, VEC (gimple
, heap
) *stmts
)
2056 sese region
= SCOP_REGION (scop
);
2058 if (!bb_in_sese_p (bb
, region
))
2061 nest
= outermost_loop_in_sese_1 (region
, bb
);
2062 gbb
= gbb_from_bb (bb
);
2064 FOR_EACH_VEC_ELT (gimple
, stmts
, i
, stmt
)
2068 if (is_gimple_debug (stmt
))
2071 loop
= loop_containing_stmt (stmt
);
2072 if (!loop_in_sese_p (loop
, region
))
2075 graphite_find_data_references_in_stmt (nest
, loop
, stmt
,
2076 &GBB_DATA_REFS (gbb
));
2080 /* Insert STMT at the end of the STMTS sequence and then insert the
2081 statements from STMTS at INSERT_GSI and call analyze_drs_in_stmts
2085 insert_stmts (scop_p scop
, gimple stmt
, gimple_seq stmts
,
2086 gimple_stmt_iterator insert_gsi
)
2088 gimple_stmt_iterator gsi
;
2089 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2092 stmts
= gimple_seq_alloc ();
2094 gsi
= gsi_last (stmts
);
2095 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2096 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2097 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2099 gsi_insert_seq_before (&insert_gsi
, stmts
, GSI_SAME_STMT
);
2100 analyze_drs_in_stmts (scop
, gsi_bb (insert_gsi
), x
);
2101 VEC_free (gimple
, heap
, x
);
2104 /* Insert the assignment "RES := EXPR" just after AFTER_STMT. */
2107 insert_out_of_ssa_copy (scop_p scop
, tree res
, tree expr
, gimple after_stmt
)
2110 gimple_stmt_iterator si
;
2111 gimple_stmt_iterator gsi
;
2112 tree var
= force_gimple_operand (expr
, &stmts
, true, NULL_TREE
);
2113 gimple stmt
= gimple_build_assign (res
, var
);
2114 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2117 stmts
= gimple_seq_alloc ();
2118 si
= gsi_last (stmts
);
2119 gsi_insert_after (&si
, stmt
, GSI_NEW_STMT
);
2120 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2121 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2123 if (gimple_code (after_stmt
) == GIMPLE_PHI
)
2125 gsi
= gsi_after_labels (gimple_bb (after_stmt
));
2126 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
2130 gsi
= gsi_for_stmt (after_stmt
);
2131 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
2134 analyze_drs_in_stmts (scop
, gimple_bb (after_stmt
), x
);
2135 VEC_free (gimple
, heap
, x
);
2138 /* Creates a poly_bb_p for basic_block BB from the existing PBB. */
2141 new_pbb_from_pbb (scop_p scop
, poly_bb_p pbb
, basic_block bb
)
2143 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 3);
2144 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
2145 gimple_bb_p gbb1
= new_gimple_bb (bb
, drs
);
2146 poly_bb_p pbb1
= new_poly_bb (scop
, gbb1
);
2147 int index
, n
= VEC_length (poly_bb_p
, SCOP_BBS (scop
));
2149 /* The INDEX of PBB in SCOP_BBS. */
2150 for (index
= 0; index
< n
; index
++)
2151 if (VEC_index (poly_bb_p
, SCOP_BBS (scop
), index
) == pbb
)
2154 if (PBB_DOMAIN (pbb
))
2155 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
2156 (&PBB_DOMAIN (pbb1
), PBB_DOMAIN (pbb
));
2158 GBB_PBB (gbb1
) = pbb1
;
2159 GBB_CONDITIONS (gbb1
) = VEC_copy (gimple
, heap
, GBB_CONDITIONS (gbb
));
2160 GBB_CONDITION_CASES (gbb1
) = VEC_copy (gimple
, heap
, GBB_CONDITION_CASES (gbb
));
2161 VEC_safe_insert (poly_bb_p
, heap
, SCOP_BBS (scop
), index
+ 1, pbb1
);
2164 /* Insert on edge E the assignment "RES := EXPR". */
2167 insert_out_of_ssa_copy_on_edge (scop_p scop
, edge e
, tree res
, tree expr
)
2169 gimple_stmt_iterator gsi
;
2171 tree var
= force_gimple_operand (expr
, &stmts
, true, NULL_TREE
);
2172 gimple stmt
= gimple_build_assign (res
, var
);
2174 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2177 stmts
= gimple_seq_alloc ();
2179 gsi
= gsi_last (stmts
);
2180 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2181 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2182 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2184 gsi_insert_seq_on_edge (e
, stmts
);
2185 gsi_commit_edge_inserts ();
2186 bb
= gimple_bb (stmt
);
2188 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
2191 if (!gbb_from_bb (bb
))
2192 new_pbb_from_pbb (scop
, pbb_from_bb (e
->src
), bb
);
2194 analyze_drs_in_stmts (scop
, bb
, x
);
2195 VEC_free (gimple
, heap
, x
);
2198 /* Creates a zero dimension array of the same type as VAR. */
2201 create_zero_dim_array (tree var
, const char *base_name
)
2203 tree index_type
= build_index_type (integer_zero_node
);
2204 tree elt_type
= TREE_TYPE (var
);
2205 tree array_type
= build_array_type (elt_type
, index_type
);
2206 tree base
= create_tmp_var (array_type
, base_name
);
2208 add_referenced_var (base
);
2210 return build4 (ARRAY_REF
, elt_type
, base
, integer_zero_node
, NULL_TREE
,
2214 /* Returns true when PHI is a loop close phi node. */
2217 scalar_close_phi_node_p (gimple phi
)
2219 if (gimple_code (phi
) != GIMPLE_PHI
2220 || !is_gimple_reg (gimple_phi_result (phi
)))
2223 /* Note that loop close phi nodes should have a single argument
2224 because we translated the representation into a canonical form
2225 before Graphite: see canonicalize_loop_closed_ssa_form. */
2226 return (gimple_phi_num_args (phi
) == 1);
2229 /* For a definition DEF in REGION, propagates the expression EXPR in
2230 all the uses of DEF outside REGION. */
2233 propagate_expr_outside_region (tree def
, tree expr
, sese region
)
2235 imm_use_iterator imm_iter
;
2238 bool replaced_once
= false;
2240 gcc_assert (TREE_CODE (def
) == SSA_NAME
);
2242 expr
= force_gimple_operand (unshare_expr (expr
), &stmts
, true,
2245 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2246 if (!is_gimple_debug (use_stmt
)
2247 && !bb_in_sese_p (gimple_bb (use_stmt
), region
))
2250 use_operand_p use_p
;
2252 FOR_EACH_PHI_OR_STMT_USE (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2253 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0)
2254 && (replaced_once
= true))
2255 replace_exp (use_p
, expr
);
2257 update_stmt (use_stmt
);
2262 gsi_insert_seq_on_edge (SESE_ENTRY (region
), stmts
);
2263 gsi_commit_edge_inserts ();
2267 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2268 dimension array for it. */
2271 rewrite_close_phi_out_of_ssa (scop_p scop
, gimple_stmt_iterator
*psi
)
2273 sese region
= SCOP_REGION (scop
);
2274 gimple phi
= gsi_stmt (*psi
);
2275 tree res
= gimple_phi_result (phi
);
2276 tree var
= SSA_NAME_VAR (res
);
2277 basic_block bb
= gimple_bb (phi
);
2278 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
2279 tree arg
= gimple_phi_arg_def (phi
, 0);
2282 /* Note that loop close phi nodes should have a single argument
2283 because we translated the representation into a canonical form
2284 before Graphite: see canonicalize_loop_closed_ssa_form. */
2285 gcc_assert (gimple_phi_num_args (phi
) == 1);
2287 /* The phi node can be a non close phi node, when its argument is
2288 invariant, or a default definition. */
2289 if (is_gimple_min_invariant (arg
)
2290 || SSA_NAME_IS_DEFAULT_DEF (arg
))
2292 propagate_expr_outside_region (res
, arg
, region
);
2297 else if (gimple_bb (SSA_NAME_DEF_STMT (arg
))->loop_father
== bb
->loop_father
)
2299 propagate_expr_outside_region (res
, arg
, region
);
2300 stmt
= gimple_build_assign (res
, arg
);
2301 remove_phi_node (psi
, false);
2302 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2303 SSA_NAME_DEF_STMT (res
) = stmt
;
2307 /* If res is scev analyzable and is not a scalar value, it is safe
2308 to ignore the close phi node: it will be code generated in the
2309 out of Graphite pass. */
2310 else if (scev_analyzable_p (res
, region
))
2312 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (res
));
2315 if (!loop_in_sese_p (loop
, region
))
2317 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (arg
));
2318 scev
= scalar_evolution_in_region (region
, loop
, arg
);
2319 scev
= compute_overall_effect_of_inner_loop (loop
, scev
);
2322 scev
= scalar_evolution_in_region (region
, loop
, res
);
2324 if (tree_does_not_contain_chrecs (scev
))
2325 propagate_expr_outside_region (res
, scev
, region
);
2332 tree zero_dim_array
= create_zero_dim_array (var
, "Close_Phi");
2334 stmt
= gimple_build_assign (res
, zero_dim_array
);
2336 if (TREE_CODE (arg
) == SSA_NAME
)
2337 insert_out_of_ssa_copy (scop
, zero_dim_array
, arg
,
2338 SSA_NAME_DEF_STMT (arg
));
2340 insert_out_of_ssa_copy_on_edge (scop
, single_pred_edge (bb
),
2341 zero_dim_array
, arg
);
2344 remove_phi_node (psi
, false);
2345 SSA_NAME_DEF_STMT (res
) = stmt
;
2347 insert_stmts (scop
, stmt
, NULL
, gsi_after_labels (bb
));
2350 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2351 dimension array for it. */
2354 rewrite_phi_out_of_ssa (scop_p scop
, gimple_stmt_iterator
*psi
)
2357 gimple phi
= gsi_stmt (*psi
);
2358 basic_block bb
= gimple_bb (phi
);
2359 tree res
= gimple_phi_result (phi
);
2360 tree var
= SSA_NAME_VAR (res
);
2361 tree zero_dim_array
= create_zero_dim_array (var
, "phi_out_of_ssa");
2365 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2367 tree arg
= gimple_phi_arg_def (phi
, i
);
2368 edge e
= gimple_phi_arg_edge (phi
, i
);
2370 /* Avoid the insertion of code in the loop latch to please the
2371 pattern matching of the vectorizer. */
2372 if (TREE_CODE (arg
) == SSA_NAME
2373 && e
->src
== bb
->loop_father
->latch
)
2374 insert_out_of_ssa_copy (scop
, zero_dim_array
, arg
,
2375 SSA_NAME_DEF_STMT (arg
));
2377 insert_out_of_ssa_copy_on_edge (scop
, e
, zero_dim_array
, arg
);
2380 var
= force_gimple_operand (zero_dim_array
, &stmts
, true, NULL_TREE
);
2382 stmt
= gimple_build_assign (res
, var
);
2383 remove_phi_node (psi
, false);
2384 SSA_NAME_DEF_STMT (res
) = stmt
;
2386 insert_stmts (scop
, stmt
, stmts
, gsi_after_labels (bb
));
2389 /* Rewrite the degenerate phi node at position PSI from the degenerate
2390 form "x = phi (y, y, ..., y)" to "x = y". */
2393 rewrite_degenerate_phi (gimple_stmt_iterator
*psi
)
2397 gimple_stmt_iterator gsi
;
2398 gimple phi
= gsi_stmt (*psi
);
2399 tree res
= gimple_phi_result (phi
);
2402 bb
= gimple_bb (phi
);
2403 rhs
= degenerate_phi_result (phi
);
2406 stmt
= gimple_build_assign (res
, rhs
);
2407 remove_phi_node (psi
, false);
2408 SSA_NAME_DEF_STMT (res
) = stmt
;
2410 gsi
= gsi_after_labels (bb
);
2411 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2414 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2417 rewrite_reductions_out_of_ssa (scop_p scop
)
2420 gimple_stmt_iterator psi
;
2421 sese region
= SCOP_REGION (scop
);
2424 if (bb_in_sese_p (bb
, region
))
2425 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);)
2427 gimple phi
= gsi_stmt (psi
);
2429 if (!is_gimple_reg (gimple_phi_result (phi
)))
2435 if (gimple_phi_num_args (phi
) > 1
2436 && degenerate_phi_result (phi
))
2437 rewrite_degenerate_phi (&psi
);
2439 else if (scalar_close_phi_node_p (phi
))
2440 rewrite_close_phi_out_of_ssa (scop
, &psi
);
2442 else if (reduction_phi_p (region
, &psi
))
2443 rewrite_phi_out_of_ssa (scop
, &psi
);
2446 update_ssa (TODO_update_ssa
);
2447 #ifdef ENABLE_CHECKING
2448 verify_loop_closed_ssa (true);
2452 /* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
2453 read from ZERO_DIM_ARRAY. */
2456 rewrite_cross_bb_scalar_dependence (scop_p scop
, tree zero_dim_array
,
2457 tree def
, gimple use_stmt
)
2459 tree var
= SSA_NAME_VAR (def
);
2460 gimple name_stmt
= gimple_build_assign (var
, zero_dim_array
);
2461 tree name
= make_ssa_name (var
, name_stmt
);
2463 use_operand_p use_p
;
2465 gcc_assert (gimple_code (use_stmt
) != GIMPLE_PHI
);
2467 gimple_assign_set_lhs (name_stmt
, name
);
2468 insert_stmts (scop
, name_stmt
, NULL
, gsi_for_stmt (use_stmt
));
2470 FOR_EACH_SSA_USE_OPERAND (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2471 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0))
2472 replace_exp (use_p
, name
);
2474 update_stmt (use_stmt
);
2477 /* For every definition DEF in the SCOP that is used outside the scop,
2478 insert a closing-scop definition in the basic block just after this
2482 handle_scalar_deps_crossing_scop_limits (scop_p scop
, tree def
, gimple stmt
)
2484 tree var
= create_tmp_reg (TREE_TYPE (def
), NULL
);
2485 tree new_name
= make_ssa_name (var
, stmt
);
2486 bool needs_copy
= false;
2487 use_operand_p use_p
;
2488 imm_use_iterator imm_iter
;
2490 sese region
= SCOP_REGION (scop
);
2492 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2494 if (!bb_in_sese_p (gimple_bb (use_stmt
), region
))
2496 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
2498 SET_USE (use_p
, new_name
);
2500 update_stmt (use_stmt
);
2505 /* Insert in the empty BB just after the scop a use of DEF such
2506 that the rewrite of cross_bb_scalar_dependences won't insert
2507 arrays everywhere else. */
2510 gimple assign
= gimple_build_assign (new_name
, def
);
2511 gimple_stmt_iterator psi
= gsi_after_labels (SESE_EXIT (region
)->dest
);
2513 add_referenced_var (var
);
2514 SSA_NAME_DEF_STMT (new_name
) = assign
;
2515 update_stmt (assign
);
2516 gsi_insert_before (&psi
, assign
, GSI_SAME_STMT
);
2520 /* Rewrite the scalar dependences crossing the boundary of the BB
2521 containing STMT with an array. Return true when something has been
2525 rewrite_cross_bb_scalar_deps (scop_p scop
, gimple_stmt_iterator
*gsi
)
2527 sese region
= SCOP_REGION (scop
);
2528 gimple stmt
= gsi_stmt (*gsi
);
2529 imm_use_iterator imm_iter
;
2532 tree zero_dim_array
= NULL_TREE
;
2536 switch (gimple_code (stmt
))
2539 def
= gimple_assign_lhs (stmt
);
2543 def
= gimple_call_lhs (stmt
);
2551 || !is_gimple_reg (def
))
2554 if (scev_analyzable_p (def
, region
))
2556 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (def
));
2557 tree scev
= scalar_evolution_in_region (region
, loop
, def
);
2559 if (tree_contains_chrecs (scev
, NULL
))
2562 propagate_expr_outside_region (def
, scev
, region
);
2566 def_bb
= gimple_bb (stmt
);
2568 handle_scalar_deps_crossing_scop_limits (scop
, def
, stmt
);
2570 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2571 if (gimple_code (use_stmt
) == GIMPLE_PHI
2574 gimple_stmt_iterator psi
= gsi_for_stmt (use_stmt
);
2576 if (scalar_close_phi_node_p (gsi_stmt (psi
)))
2577 rewrite_close_phi_out_of_ssa (scop
, &psi
);
2579 rewrite_phi_out_of_ssa (scop
, &psi
);
2582 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2583 if (gimple_code (use_stmt
) != GIMPLE_PHI
2584 && def_bb
!= gimple_bb (use_stmt
)
2585 && !is_gimple_debug (use_stmt
)
2588 if (!zero_dim_array
)
2590 zero_dim_array
= create_zero_dim_array
2591 (SSA_NAME_VAR (def
), "Cross_BB_scalar_dependence");
2592 insert_out_of_ssa_copy (scop
, zero_dim_array
, def
,
2593 SSA_NAME_DEF_STMT (def
));
2597 rewrite_cross_bb_scalar_dependence (scop
, zero_dim_array
,
2604 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2607 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop
)
2610 gimple_stmt_iterator psi
;
2611 sese region
= SCOP_REGION (scop
);
2612 bool changed
= false;
2614 /* Create an extra empty BB after the scop. */
2615 split_edge (SESE_EXIT (region
));
2618 if (bb_in_sese_p (bb
, region
))
2619 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2620 changed
|= rewrite_cross_bb_scalar_deps (scop
, &psi
);
2625 update_ssa (TODO_update_ssa
);
2626 #ifdef ENABLE_CHECKING
2627 verify_loop_closed_ssa (true);
2632 /* Returns the number of pbbs that are in loops contained in SCOP. */
2635 nb_pbbs_in_loops (scop_p scop
)
2641 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2642 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb
)), SCOP_REGION (scop
)))
2648 /* Return the number of data references in BB that write in
2652 nb_data_writes_in_bb (basic_block bb
)
2655 gimple_stmt_iterator gsi
;
2657 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2658 if (gimple_vdef (gsi_stmt (gsi
)))
2664 /* Splits at STMT the basic block BB represented as PBB in the
2668 split_pbb (scop_p scop
, poly_bb_p pbb
, basic_block bb
, gimple stmt
)
2670 edge e1
= split_block (bb
, stmt
);
2671 new_pbb_from_pbb (scop
, pbb
, e1
->dest
);
2675 /* Splits STMT out of its current BB. This is done for reduction
2676 statements for which we want to ignore data dependences. */
2679 split_reduction_stmt (scop_p scop
, gimple stmt
)
2681 basic_block bb
= gimple_bb (stmt
);
2682 poly_bb_p pbb
= pbb_from_bb (bb
);
2683 gimple_bb_p gbb
= gbb_from_bb (bb
);
2686 data_reference_p dr
;
2688 /* Do not split basic blocks with no writes to memory: the reduction
2689 will be the only write to memory. */
2690 if (nb_data_writes_in_bb (bb
) == 0
2691 /* Or if we have already marked BB as a reduction. */
2692 || PBB_IS_REDUCTION (pbb_from_bb (bb
)))
2695 e1
= split_pbb (scop
, pbb
, bb
, stmt
);
2697 /* Split once more only when the reduction stmt is not the only one
2698 left in the original BB. */
2699 if (!gsi_one_before_end_p (gsi_start_nondebug_bb (bb
)))
2701 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
2703 e1
= split_pbb (scop
, pbb
, bb
, gsi_stmt (gsi
));
2706 /* A part of the data references will end in a different basic block
2707 after the split: move the DRs from the original GBB to the newly
2709 FOR_EACH_VEC_ELT (data_reference_p
, GBB_DATA_REFS (gbb
), i
, dr
)
2711 basic_block bb1
= gimple_bb (DR_STMT (dr
));
2715 gimple_bb_p gbb1
= gbb_from_bb (bb1
);
2716 VEC_safe_push (data_reference_p
, heap
, GBB_DATA_REFS (gbb1
), dr
);
2717 VEC_ordered_remove (data_reference_p
, GBB_DATA_REFS (gbb
), i
);
2725 /* Return true when stmt is a reduction operation. */
2728 is_reduction_operation_p (gimple stmt
)
2730 enum tree_code code
;
2732 gcc_assert (is_gimple_assign (stmt
));
2733 code
= gimple_assign_rhs_code (stmt
);
2735 return flag_associative_math
2736 && commutative_tree_code (code
)
2737 && associative_tree_code (code
);
2740 /* Returns true when PHI contains an argument ARG. */
2743 phi_contains_arg (gimple phi
, tree arg
)
2747 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2748 if (operand_equal_p (arg
, gimple_phi_arg_def (phi
, i
), 0))
2754 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2757 follow_ssa_with_commutative_ops (tree arg
, tree lhs
)
2761 if (TREE_CODE (arg
) != SSA_NAME
)
2764 stmt
= SSA_NAME_DEF_STMT (arg
);
2766 if (gimple_code (stmt
) == GIMPLE_NOP
2767 || gimple_code (stmt
) == GIMPLE_CALL
)
2770 if (gimple_code (stmt
) == GIMPLE_PHI
)
2772 if (phi_contains_arg (stmt
, lhs
))
2777 if (!is_gimple_assign (stmt
))
2780 if (gimple_num_ops (stmt
) == 2)
2781 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2783 if (is_reduction_operation_p (stmt
))
2785 gimple res
= follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2788 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt
), lhs
);
2794 /* Detect commutative and associative scalar reductions starting at
2795 the STMT. Return the phi node of the reduction cycle, or NULL. */
2798 detect_commutative_reduction_arg (tree lhs
, gimple stmt
, tree arg
,
2799 VEC (gimple
, heap
) **in
,
2800 VEC (gimple
, heap
) **out
)
2802 gimple phi
= follow_ssa_with_commutative_ops (arg
, lhs
);
2807 VEC_safe_push (gimple
, heap
, *in
, stmt
);
2808 VEC_safe_push (gimple
, heap
, *out
, stmt
);
2812 /* Detect commutative and associative scalar reductions starting at
2813 STMT. Return the phi node of the reduction cycle, or NULL. */
2816 detect_commutative_reduction_assign (gimple stmt
, VEC (gimple
, heap
) **in
,
2817 VEC (gimple
, heap
) **out
)
2819 tree lhs
= gimple_assign_lhs (stmt
);
2821 if (gimple_num_ops (stmt
) == 2)
2822 return detect_commutative_reduction_arg (lhs
, stmt
,
2823 gimple_assign_rhs1 (stmt
),
2826 if (is_reduction_operation_p (stmt
))
2828 gimple res
= detect_commutative_reduction_arg (lhs
, stmt
,
2829 gimple_assign_rhs1 (stmt
),
2832 : detect_commutative_reduction_arg (lhs
, stmt
,
2833 gimple_assign_rhs2 (stmt
),
2840 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2843 follow_inital_value_to_phi (tree arg
, tree lhs
)
2847 if (!arg
|| TREE_CODE (arg
) != SSA_NAME
)
2850 stmt
= SSA_NAME_DEF_STMT (arg
);
2852 if (gimple_code (stmt
) == GIMPLE_PHI
2853 && phi_contains_arg (stmt
, lhs
))
2860 /* Return the argument of the loop PHI that is the inital value coming
2861 from outside the loop. */
2864 edge_initial_value_for_loop_phi (gimple phi
)
2868 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2870 edge e
= gimple_phi_arg_edge (phi
, i
);
2872 if (loop_depth (e
->src
->loop_father
)
2873 < loop_depth (e
->dest
->loop_father
))
2880 /* Return the argument of the loop PHI that is the inital value coming
2881 from outside the loop. */
2884 initial_value_for_loop_phi (gimple phi
)
2888 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2890 edge e
= gimple_phi_arg_edge (phi
, i
);
2892 if (loop_depth (e
->src
->loop_father
)
2893 < loop_depth (e
->dest
->loop_father
))
2894 return gimple_phi_arg_def (phi
, i
);
2900 /* Returns true when DEF is used outside the reduction cycle of
2904 used_outside_reduction (tree def
, gimple loop_phi
)
2906 use_operand_p use_p
;
2907 imm_use_iterator imm_iter
;
2908 loop_p loop
= loop_containing_stmt (loop_phi
);
2910 /* In LOOP, DEF should be used only in LOOP_PHI. */
2911 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
2913 gimple stmt
= USE_STMT (use_p
);
2915 if (stmt
!= loop_phi
2916 && !is_gimple_debug (stmt
)
2917 && flow_bb_inside_loop_p (loop
, gimple_bb (stmt
)))
2924 /* Detect commutative and associative scalar reductions belonging to
2925 the SCOP starting at the loop closed phi node STMT. Return the phi
2926 node of the reduction cycle, or NULL. */
2929 detect_commutative_reduction (scop_p scop
, gimple stmt
, VEC (gimple
, heap
) **in
,
2930 VEC (gimple
, heap
) **out
)
2932 if (scalar_close_phi_node_p (stmt
))
2934 gimple def
, loop_phi
, phi
, close_phi
= stmt
;
2935 tree init
, lhs
, arg
= gimple_phi_arg_def (close_phi
, 0);
2937 if (TREE_CODE (arg
) != SSA_NAME
)
2940 /* Note that loop close phi nodes should have a single argument
2941 because we translated the representation into a canonical form
2942 before Graphite: see canonicalize_loop_closed_ssa_form. */
2943 gcc_assert (gimple_phi_num_args (close_phi
) == 1);
2945 def
= SSA_NAME_DEF_STMT (arg
);
2946 if (!stmt_in_sese_p (def
, SCOP_REGION (scop
))
2947 || !(loop_phi
= detect_commutative_reduction (scop
, def
, in
, out
)))
2950 lhs
= gimple_phi_result (close_phi
);
2951 init
= initial_value_for_loop_phi (loop_phi
);
2952 phi
= follow_inital_value_to_phi (init
, lhs
);
2954 if (phi
&& (used_outside_reduction (lhs
, phi
)
2955 || !has_single_use (gimple_phi_result (phi
))))
2958 VEC_safe_push (gimple
, heap
, *in
, loop_phi
);
2959 VEC_safe_push (gimple
, heap
, *out
, close_phi
);
2963 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
2964 return detect_commutative_reduction_assign (stmt
, in
, out
);
2969 /* Translate the scalar reduction statement STMT to an array RED
2970 knowing that its recursive phi node is LOOP_PHI. */
2973 translate_scalar_reduction_to_array_for_stmt (scop_p scop
, tree red
,
2974 gimple stmt
, gimple loop_phi
)
2976 tree res
= gimple_phi_result (loop_phi
);
2977 gimple assign
= gimple_build_assign (res
, unshare_expr (red
));
2978 gimple_stmt_iterator gsi
;
2980 insert_stmts (scop
, assign
, NULL
, gsi_after_labels (gimple_bb (loop_phi
)));
2982 assign
= gimple_build_assign (unshare_expr (red
), gimple_assign_lhs (stmt
));
2983 gsi
= gsi_for_stmt (stmt
);
2985 insert_stmts (scop
, assign
, NULL
, gsi
);
2988 /* Removes the PHI node and resets all the debug stmts that are using
2992 remove_phi (gimple phi
)
2994 imm_use_iterator imm_iter
;
2996 use_operand_p use_p
;
2997 gimple_stmt_iterator gsi
;
2998 VEC (gimple
, heap
) *update
= VEC_alloc (gimple
, heap
, 3);
3002 def
= PHI_RESULT (phi
);
3003 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
3005 stmt
= USE_STMT (use_p
);
3007 if (is_gimple_debug (stmt
))
3009 gimple_debug_bind_reset_value (stmt
);
3010 VEC_safe_push (gimple
, heap
, update
, stmt
);
3014 FOR_EACH_VEC_ELT (gimple
, update
, i
, stmt
)
3017 VEC_free (gimple
, heap
, update
);
3019 gsi
= gsi_for_phi_node (phi
);
3020 remove_phi_node (&gsi
, false);
3023 /* Helper function for for_each_index. For each INDEX of the data
3024 reference REF, returns true when its indices are valid in the loop
3025 nest LOOP passed in as DATA. */
3028 dr_indices_valid_in_loop (tree ref ATTRIBUTE_UNUSED
, tree
*index
, void *data
)
3031 basic_block header
, def_bb
;
3034 if (TREE_CODE (*index
) != SSA_NAME
)
3037 loop
= *((loop_p
*) data
);
3038 header
= loop
->header
;
3039 stmt
= SSA_NAME_DEF_STMT (*index
);
3044 def_bb
= gimple_bb (stmt
);
3049 return dominated_by_p (CDI_DOMINATORS
, header
, def_bb
);
3052 /* When the result of a CLOSE_PHI is written to a memory location,
3053 return a pointer to that memory reference, otherwise return
3057 close_phi_written_to_memory (gimple close_phi
)
3059 imm_use_iterator imm_iter
;
3060 use_operand_p use_p
;
3062 tree res
, def
= gimple_phi_result (close_phi
);
3064 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
3065 if ((stmt
= USE_STMT (use_p
))
3066 && gimple_code (stmt
) == GIMPLE_ASSIGN
3067 && (res
= gimple_assign_lhs (stmt
)))
3069 switch (TREE_CODE (res
))
3079 tree arg
= gimple_phi_arg_def (close_phi
, 0);
3080 loop_p nest
= loop_containing_stmt (SSA_NAME_DEF_STMT (arg
));
3082 /* FIXME: this restriction is for id-{24,25}.f and
3083 could be handled by duplicating the computation of
3084 array indices before the loop of the close_phi. */
3085 if (for_each_index (&res
, dr_indices_valid_in_loop
, &nest
))
3097 /* Rewrite out of SSA the reduction described by the loop phi nodes
3098 IN, and the close phi nodes OUT. IN and OUT are structured by loop
3101 IN: stmt, loop_n, ..., loop_0
3102 OUT: stmt, close_n, ..., close_0
3104 the first element is the reduction statement, and the next elements
3105 are the loop and close phi nodes of each of the outer loops. */
3108 translate_scalar_reduction_to_array (scop_p scop
,
3109 VEC (gimple
, heap
) *in
,
3110 VEC (gimple
, heap
) *out
)
3113 unsigned int i
= VEC_length (gimple
, out
) - 1;
3114 tree red
= close_phi_written_to_memory (VEC_index (gimple
, out
, i
));
3116 FOR_EACH_VEC_ELT (gimple
, in
, i
, loop_phi
)
3118 gimple close_phi
= VEC_index (gimple
, out
, i
);
3122 gimple stmt
= loop_phi
;
3123 basic_block bb
= split_reduction_stmt (scop
, stmt
);
3124 poly_bb_p pbb
= pbb_from_bb (bb
);
3125 PBB_IS_REDUCTION (pbb
) = true;
3126 gcc_assert (close_phi
== loop_phi
);
3129 red
= create_zero_dim_array
3130 (gimple_assign_lhs (stmt
), "Commutative_Associative_Reduction");
3132 translate_scalar_reduction_to_array_for_stmt
3133 (scop
, red
, stmt
, VEC_index (gimple
, in
, 1));
3137 if (i
== VEC_length (gimple
, in
) - 1)
3139 insert_out_of_ssa_copy (scop
, gimple_phi_result (close_phi
),
3140 unshare_expr (red
), close_phi
);
3141 insert_out_of_ssa_copy_on_edge
3142 (scop
, edge_initial_value_for_loop_phi (loop_phi
),
3143 unshare_expr (red
), initial_value_for_loop_phi (loop_phi
));
3146 remove_phi (loop_phi
);
3147 remove_phi (close_phi
);
3151 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. Returns
3152 true when something has been changed. */
3155 rewrite_commutative_reductions_out_of_ssa_close_phi (scop_p scop
,
3159 VEC (gimple
, heap
) *in
= VEC_alloc (gimple
, heap
, 10);
3160 VEC (gimple
, heap
) *out
= VEC_alloc (gimple
, heap
, 10);
3162 detect_commutative_reduction (scop
, close_phi
, &in
, &out
);
3163 res
= VEC_length (gimple
, in
) > 1;
3165 translate_scalar_reduction_to_array (scop
, in
, out
);
3167 VEC_free (gimple
, heap
, in
);
3168 VEC_free (gimple
, heap
, out
);
3172 /* Rewrites all the commutative reductions from LOOP out of SSA.
3173 Returns true when something has been changed. */
3176 rewrite_commutative_reductions_out_of_ssa_loop (scop_p scop
,
3179 gimple_stmt_iterator gsi
;
3180 edge exit
= single_exit (loop
);
3182 bool changed
= false;
3187 for (gsi
= gsi_start_phis (exit
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3188 if ((res
= gimple_phi_result (gsi_stmt (gsi
)))
3189 && is_gimple_reg (res
)
3190 && !scev_analyzable_p (res
, SCOP_REGION (scop
)))
3191 changed
|= rewrite_commutative_reductions_out_of_ssa_close_phi
3192 (scop
, gsi_stmt (gsi
));
3197 /* Rewrites all the commutative reductions from SCOP out of SSA. */
3200 rewrite_commutative_reductions_out_of_ssa (scop_p scop
)
3204 bool changed
= false;
3205 sese region
= SCOP_REGION (scop
);
3207 FOR_EACH_LOOP (li
, loop
, 0)
3208 if (loop_in_sese_p (loop
, region
))
3209 changed
|= rewrite_commutative_reductions_out_of_ssa_loop (scop
, loop
);
3214 gsi_commit_edge_inserts ();
3215 update_ssa (TODO_update_ssa
);
3216 #ifdef ENABLE_CHECKING
3217 verify_loop_closed_ssa (true);
3222 /* Java does not initialize long_long_integer_type_node. */
3223 #define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
3225 /* Can all ivs be represented by a signed integer?
3226 As CLooG might generate negative values in its expressions, signed loop ivs
3227 are required in the backend. */
3230 scop_ivs_can_be_represented (scop_p scop
)
3234 gimple_stmt_iterator psi
;
3236 FOR_EACH_LOOP (li
, loop
, 0)
3238 if (!loop_in_sese_p (loop
, SCOP_REGION (scop
)))
3241 for (psi
= gsi_start_phis (loop
->header
);
3242 !gsi_end_p (psi
); gsi_next (&psi
))
3244 gimple phi
= gsi_stmt (psi
);
3245 tree res
= PHI_RESULT (phi
);
3246 tree type
= TREE_TYPE (res
);
3248 if (TYPE_UNSIGNED (type
)
3249 && TYPE_PRECISION (type
) >= TYPE_PRECISION (my_long_long
))
3259 /* Builds the polyhedral representation for a SESE region. */
3262 build_poly_scop (scop_p scop
)
3264 sese region
= SCOP_REGION (scop
);
3265 graphite_dim_t max_dim
;
3267 build_scop_bbs (scop
);
3269 /* FIXME: This restriction is needed to avoid a problem in CLooG.
3270 Once CLooG is fixed, remove this guard. Anyways, it makes no
3271 sense to optimize a scop containing only PBBs that do not belong
3273 if (nb_pbbs_in_loops (scop
) == 0)
3276 if (!scop_ivs_can_be_represented (scop
))
3279 if (flag_associative_math
)
3280 rewrite_commutative_reductions_out_of_ssa (scop
);
3282 build_sese_loop_nests (region
);
3283 build_sese_conditions (region
);
3284 find_scop_parameters (scop
);
3286 max_dim
= PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS
);
3287 if (scop_nb_params (scop
) > max_dim
)
3290 build_scop_iteration_domain (scop
);
3291 build_scop_context (scop
);
3292 add_conditions_to_constraints (scop
);
3294 /* Rewrite out of SSA only after having translated the
3295 representation to the polyhedral representation to avoid scev
3296 analysis failures. That means that these functions will insert
3297 new data references that they create in the right place. */
3298 rewrite_reductions_out_of_ssa (scop
);
3299 rewrite_cross_bb_scalar_deps_out_of_ssa (scop
);
3301 build_scop_drs (scop
);
3303 build_scop_scattering (scop
);
3305 /* This SCoP has been translated to the polyhedral
3307 POLY_SCOP_P (scop
) = true;