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"
24 #include "tree-flow.h"
25 #include "tree-dump.h"
27 #include "tree-chrec.h"
28 #include "tree-data-ref.h"
29 #include "tree-scalar-evolution.h"
35 #include "graphite-ppl.h"
36 #include "graphite-poly.h"
37 #include "graphite-sese-to-poly.h"
39 /* Returns the index of the PHI argument defined in the outermost
43 phi_arg_in_outermost_loop (gimple phi
)
45 loop_p loop
= gimple_bb (phi
)->loop_father
;
48 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
49 if (!flow_bb_inside_loop_p (loop
, gimple_phi_arg_edge (phi
, i
)->src
))
51 loop
= gimple_phi_arg_edge (phi
, i
)->src
->loop_father
;
58 /* Removes a simple copy phi node "RES = phi (INIT, RES)" at position
59 PSI by inserting on the loop ENTRY edge assignment "RES = INIT". */
62 remove_simple_copy_phi (gimple_stmt_iterator
*psi
)
64 gimple phi
= gsi_stmt (*psi
);
65 tree res
= gimple_phi_result (phi
);
66 size_t entry
= phi_arg_in_outermost_loop (phi
);
67 tree init
= gimple_phi_arg_def (phi
, entry
);
68 gimple stmt
= gimple_build_assign (res
, init
);
69 edge e
= gimple_phi_arg_edge (phi
, entry
);
71 remove_phi_node (psi
, false);
72 gsi_insert_on_edge_immediate (e
, stmt
);
73 SSA_NAME_DEF_STMT (res
) = stmt
;
76 /* Removes an invariant phi node at position PSI by inserting on the
77 loop ENTRY edge the assignment RES = INIT. */
80 remove_invariant_phi (sese region
, gimple_stmt_iterator
*psi
)
82 gimple phi
= gsi_stmt (*psi
);
83 loop_p loop
= loop_containing_stmt (phi
);
84 tree res
= gimple_phi_result (phi
);
85 tree scev
= scalar_evolution_in_region (region
, loop
, res
);
86 size_t entry
= phi_arg_in_outermost_loop (phi
);
87 edge e
= gimple_phi_arg_edge (phi
, entry
);
91 gimple_stmt_iterator gsi
;
93 if (tree_contains_chrecs (scev
, NULL
))
94 scev
= gimple_phi_arg_def (phi
, entry
);
96 var
= force_gimple_operand (scev
, &stmts
, true, NULL_TREE
);
97 stmt
= gimple_build_assign (res
, var
);
98 remove_phi_node (psi
, false);
101 stmts
= gimple_seq_alloc ();
103 gsi
= gsi_last (stmts
);
104 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
105 gsi_insert_seq_on_edge (e
, stmts
);
106 gsi_commit_edge_inserts ();
107 SSA_NAME_DEF_STMT (res
) = stmt
;
110 /* Returns true when the phi node at PSI is of the form "a = phi (a, x)". */
113 simple_copy_phi_p (gimple phi
)
117 if (gimple_phi_num_args (phi
) != 2)
120 res
= gimple_phi_result (phi
);
121 return (res
== gimple_phi_arg_def (phi
, 0)
122 || res
== gimple_phi_arg_def (phi
, 1));
125 /* Returns true when the phi node at position PSI is a reduction phi
126 node in REGION. Otherwise moves the pointer PSI to the next phi to
130 reduction_phi_p (sese region
, gimple_stmt_iterator
*psi
)
133 gimple phi
= gsi_stmt (*psi
);
134 tree res
= gimple_phi_result (phi
);
136 loop
= loop_containing_stmt (phi
);
138 if (simple_copy_phi_p (phi
))
140 /* PRE introduces phi nodes like these, for an example,
141 see id-5.f in the fortran graphite testsuite:
143 # prephitmp.85_265 = PHI <prephitmp.85_258(33), prephitmp.85_265(18)>
145 remove_simple_copy_phi (psi
);
149 if (scev_analyzable_p (res
, region
))
151 tree scev
= scalar_evolution_in_region (region
, loop
, res
);
153 if (evolution_function_is_invariant_p (scev
, loop
->num
))
154 remove_invariant_phi (region
, psi
);
161 /* All the other cases are considered reductions. */
165 /* Store the GRAPHITE representation of BB. */
168 new_gimple_bb (basic_block bb
, VEC (data_reference_p
, heap
) *drs
)
170 struct gimple_bb
*gbb
;
172 gbb
= XNEW (struct gimple_bb
);
175 GBB_DATA_REFS (gbb
) = drs
;
176 GBB_CONDITIONS (gbb
) = NULL
;
177 GBB_CONDITION_CASES (gbb
) = NULL
;
183 free_data_refs_aux (VEC (data_reference_p
, heap
) *datarefs
)
186 struct data_reference
*dr
;
188 FOR_EACH_VEC_ELT (data_reference_p
, datarefs
, i
, dr
)
191 base_alias_pair
*bap
= (base_alias_pair
*)(dr
->aux
);
194 free (bap
->alias_set
);
203 free_gimple_bb (struct gimple_bb
*gbb
)
205 free_data_refs_aux (GBB_DATA_REFS (gbb
));
206 free_data_refs (GBB_DATA_REFS (gbb
));
208 VEC_free (gimple
, heap
, GBB_CONDITIONS (gbb
));
209 VEC_free (gimple
, heap
, GBB_CONDITION_CASES (gbb
));
210 GBB_BB (gbb
)->aux
= 0;
214 /* Deletes all gimple bbs in SCOP. */
217 remove_gbbs_in_scop (scop_p scop
)
222 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
223 free_gimple_bb (PBB_BLACK_BOX (pbb
));
226 /* Deletes all scops in SCOPS. */
229 free_scops (VEC (scop_p
, heap
) *scops
)
234 FOR_EACH_VEC_ELT (scop_p
, scops
, i
, scop
)
236 remove_gbbs_in_scop (scop
);
237 free_sese (SCOP_REGION (scop
));
241 VEC_free (scop_p
, heap
, scops
);
244 /* Generates a polyhedral black box only if the bb contains interesting
248 try_generate_gimple_bb (scop_p scop
, basic_block bb
)
250 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 5);
251 loop_p nest
= outermost_loop_in_sese (SCOP_REGION (scop
), bb
);
252 gimple_stmt_iterator gsi
;
254 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
256 gimple stmt
= gsi_stmt (gsi
);
257 if (!is_gimple_debug (stmt
))
258 graphite_find_data_references_in_stmt (nest
, stmt
, &drs
);
261 return new_gimple_bb (bb
, drs
);
264 /* Returns true if all predecessors of BB, that are not dominated by BB, are
265 marked in MAP. The predecessors dominated by BB are loop latches and will
266 be handled after BB. */
269 all_non_dominated_preds_marked_p (basic_block bb
, sbitmap map
)
274 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
275 if (!TEST_BIT (map
, e
->src
->index
)
276 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
282 /* Compare the depth of two basic_block's P1 and P2. */
285 compare_bb_depths (const void *p1
, const void *p2
)
287 const_basic_block
const bb1
= *(const_basic_block
const*)p1
;
288 const_basic_block
const bb2
= *(const_basic_block
const*)p2
;
289 int d1
= loop_depth (bb1
->loop_father
);
290 int d2
= loop_depth (bb2
->loop_father
);
301 /* Sort the basic blocks from DOM such that the first are the ones at
302 a deepest loop level. */
305 graphite_sort_dominated_info (VEC (basic_block
, heap
) *dom
)
307 VEC_qsort (basic_block
, dom
, compare_bb_depths
);
310 /* Recursive helper function for build_scops_bbs. */
313 build_scop_bbs_1 (scop_p scop
, sbitmap visited
, basic_block bb
)
315 sese region
= SCOP_REGION (scop
);
316 VEC (basic_block
, heap
) *dom
;
319 if (TEST_BIT (visited
, bb
->index
)
320 || !bb_in_sese_p (bb
, region
))
323 pbb
= new_poly_bb (scop
, try_generate_gimple_bb (scop
, bb
));
324 VEC_safe_push (poly_bb_p
, heap
, SCOP_BBS (scop
), pbb
);
325 SET_BIT (visited
, bb
->index
);
327 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
332 graphite_sort_dominated_info (dom
);
334 while (!VEC_empty (basic_block
, dom
))
339 FOR_EACH_VEC_ELT (basic_block
, dom
, i
, dom_bb
)
340 if (all_non_dominated_preds_marked_p (dom_bb
, visited
))
342 build_scop_bbs_1 (scop
, visited
, dom_bb
);
343 VEC_unordered_remove (basic_block
, dom
, i
);
348 VEC_free (basic_block
, heap
, dom
);
351 /* Gather the basic blocks belonging to the SCOP. */
354 build_scop_bbs (scop_p scop
)
356 sbitmap visited
= sbitmap_alloc (last_basic_block
);
357 sese region
= SCOP_REGION (scop
);
359 sbitmap_zero (visited
);
360 build_scop_bbs_1 (scop
, visited
, SESE_ENTRY_BB (region
));
361 sbitmap_free (visited
);
364 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
365 We generate SCATTERING_DIMENSIONS scattering dimensions.
367 CLooG 0.15.0 and previous versions require, that all
368 scattering functions of one CloogProgram have the same number of
369 scattering dimensions, therefore we allow to specify it. This
370 should be removed in future versions of CLooG.
372 The scattering polyhedron consists of these dimensions: scattering,
373 loop_iterators, parameters.
377 | scattering_dimensions = 5
378 | used_scattering_dimensions = 3
386 | Scattering polyhedron:
388 | scattering: {s1, s2, s3, s4, s5}
389 | loop_iterators: {i}
390 | parameters: {p1, p2}
392 | s1 s2 s3 s4 s5 i p1 p2 1
393 | 1 0 0 0 0 0 0 0 -4 = 0
394 | 0 1 0 0 0 -1 0 0 0 = 0
395 | 0 0 1 0 0 0 0 0 -5 = 0 */
398 build_pbb_scattering_polyhedrons (ppl_Linear_Expression_t static_schedule
,
399 poly_bb_p pbb
, int scattering_dimensions
)
402 scop_p scop
= PBB_SCOP (pbb
);
403 int nb_iterators
= pbb_dim_iter_domain (pbb
);
404 int used_scattering_dimensions
= nb_iterators
* 2 + 1;
405 int nb_params
= scop_nb_params (scop
);
407 ppl_dimension_type dim
= scattering_dimensions
+ nb_iterators
+ nb_params
;
410 gcc_assert (scattering_dimensions
>= used_scattering_dimensions
);
413 ppl_new_Coefficient (&c
);
414 PBB_TRANSFORMED (pbb
) = poly_scattering_new ();
415 ppl_new_C_Polyhedron_from_space_dimension
416 (&PBB_TRANSFORMED_SCATTERING (pbb
), dim
, 0);
418 PBB_NB_SCATTERING_TRANSFORM (pbb
) = scattering_dimensions
;
420 for (i
= 0; i
< scattering_dimensions
; i
++)
422 ppl_Constraint_t cstr
;
423 ppl_Linear_Expression_t expr
;
425 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
427 ppl_assign_Coefficient_from_mpz_t (c
, v
);
428 ppl_Linear_Expression_add_to_coefficient (expr
, i
, c
);
430 /* Textual order inside this loop. */
433 ppl_Linear_Expression_coefficient (static_schedule
, i
/ 2, c
);
434 ppl_Coefficient_to_mpz_t (c
, v
);
436 ppl_assign_Coefficient_from_mpz_t (c
, v
);
437 ppl_Linear_Expression_add_to_inhomogeneous (expr
, c
);
440 /* Iterations of this loop. */
441 else /* if ((i % 2) == 1) */
443 int loop
= (i
- 1) / 2;
446 ppl_assign_Coefficient_from_mpz_t (c
, v
);
447 ppl_Linear_Expression_add_to_coefficient
448 (expr
, scattering_dimensions
+ loop
, c
);
451 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
452 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb
), cstr
);
453 ppl_delete_Linear_Expression (expr
);
454 ppl_delete_Constraint (cstr
);
458 ppl_delete_Coefficient (c
);
460 PBB_ORIGINAL (pbb
) = poly_scattering_copy (PBB_TRANSFORMED (pbb
));
463 /* Build for BB the static schedule.
465 The static schedule is a Dewey numbering of the abstract syntax
466 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
468 The following example informally defines the static schedule:
487 Static schedules for A to F:
500 build_scop_scattering (scop_p scop
)
504 gimple_bb_p previous_gbb
= NULL
;
505 ppl_Linear_Expression_t static_schedule
;
510 ppl_new_Coefficient (&c
);
511 ppl_new_Linear_Expression (&static_schedule
);
513 /* We have to start schedules at 0 on the first component and
514 because we cannot compare_prefix_loops against a previous loop,
515 prefix will be equal to zero, and that index will be
516 incremented before copying. */
518 ppl_assign_Coefficient_from_mpz_t (c
, v
);
519 ppl_Linear_Expression_add_to_coefficient (static_schedule
, 0, c
);
521 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
523 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
524 ppl_Linear_Expression_t common
;
526 int nb_scat_dims
= pbb_dim_iter_domain (pbb
) * 2 + 1;
529 prefix
= nb_common_loops (SCOP_REGION (scop
), previous_gbb
, gbb
);
534 ppl_new_Linear_Expression_with_dimension (&common
, prefix
+ 1);
535 ppl_assign_Linear_Expression_from_Linear_Expression (common
,
539 ppl_assign_Coefficient_from_mpz_t (c
, v
);
540 ppl_Linear_Expression_add_to_coefficient (common
, prefix
, c
);
541 ppl_assign_Linear_Expression_from_Linear_Expression (static_schedule
,
544 build_pbb_scattering_polyhedrons (common
, pbb
, nb_scat_dims
);
546 ppl_delete_Linear_Expression (common
);
550 ppl_delete_Coefficient (c
);
551 ppl_delete_Linear_Expression (static_schedule
);
554 /* Add the value K to the dimension D of the linear expression EXPR. */
557 add_value_to_dim (ppl_dimension_type d
, ppl_Linear_Expression_t expr
,
561 ppl_Coefficient_t coef
;
563 ppl_new_Coefficient (&coef
);
564 ppl_Linear_Expression_coefficient (expr
, d
, coef
);
566 ppl_Coefficient_to_mpz_t (coef
, val
);
568 mpz_add (val
, val
, k
);
570 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
571 ppl_Linear_Expression_add_to_coefficient (expr
, d
, coef
);
573 ppl_delete_Coefficient (coef
);
576 /* In the context of scop S, scan E, the right hand side of a scalar
577 evolution function in loop VAR, and translate it to a linear
581 scan_tree_for_params_right_scev (sese s
, tree e
, int var
,
582 ppl_Linear_Expression_t expr
)
586 loop_p loop
= get_loop (var
);
587 ppl_dimension_type l
= sese_loop_depth (s
, loop
) - 1;
590 /* Scalar evolutions should happen in the sese region. */
591 gcc_assert (sese_loop_depth (s
, loop
) > 0);
593 /* We can not deal with parametric strides like:
599 gcc_assert (TREE_CODE (e
) == INTEGER_CST
);
602 tree_int_to_gmp (e
, val
);
603 add_value_to_dim (l
, expr
, val
);
608 /* Scan the integer constant CST, and add it to the inhomogeneous part of the
609 linear expression EXPR. K is the multiplier of the constant. */
612 scan_tree_for_params_int (tree cst
, ppl_Linear_Expression_t expr
, mpz_t k
)
615 ppl_Coefficient_t coef
;
616 tree type
= TREE_TYPE (cst
);
620 /* Necessary to not get "-1 = 2^n - 1". */
621 mpz_set_double_int (val
, double_int_sext (tree_to_double_int (cst
),
622 TYPE_PRECISION (type
)), false);
624 mpz_mul (val
, val
, k
);
625 ppl_new_Coefficient (&coef
);
626 ppl_assign_Coefficient_from_mpz_t (coef
, val
);
627 ppl_Linear_Expression_add_to_inhomogeneous (expr
, coef
);
629 ppl_delete_Coefficient (coef
);
632 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
633 Otherwise returns -1. */
636 parameter_index_in_region_1 (tree name
, sese region
)
641 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
643 FOR_EACH_VEC_ELT (tree
, SESE_PARAMS (region
), i
, p
)
650 /* When the parameter NAME is in REGION, returns its index in
651 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
652 and returns the index of NAME. */
655 parameter_index_in_region (tree name
, sese region
)
659 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
661 i
= parameter_index_in_region_1 (name
, region
);
665 gcc_assert (SESE_ADD_PARAMS (region
));
667 i
= VEC_length (tree
, SESE_PARAMS (region
));
668 VEC_safe_push (tree
, heap
, SESE_PARAMS (region
), name
);
672 /* In the context of sese S, scan the expression E and translate it to
673 a linear expression C. When parsing a symbolic multiplication, K
674 represents the constant multiplier of an expression containing
678 scan_tree_for_params (sese s
, tree e
, ppl_Linear_Expression_t c
,
681 if (e
== chrec_dont_know
)
684 switch (TREE_CODE (e
))
686 case POLYNOMIAL_CHREC
:
687 scan_tree_for_params_right_scev (s
, CHREC_RIGHT (e
),
688 CHREC_VARIABLE (e
), c
);
689 scan_tree_for_params (s
, CHREC_LEFT (e
), c
, k
);
693 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
698 gcc_assert (host_integerp (TREE_OPERAND (e
, 1), 0));
700 tree_int_to_gmp (TREE_OPERAND (e
, 1), val
);
701 mpz_mul (val
, val
, k
);
702 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, val
);
706 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
713 gcc_assert (host_integerp (TREE_OPERAND (e
, 0), 0));
715 tree_int_to_gmp (TREE_OPERAND (e
, 0), val
);
716 mpz_mul (val
, val
, k
);
717 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, val
);
721 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
726 case POINTER_PLUS_EXPR
:
727 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
728 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), c
, k
);
733 ppl_Linear_Expression_t tmp_expr
= NULL
;
737 ppl_dimension_type dim
;
738 ppl_Linear_Expression_space_dimension (c
, &dim
);
739 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
742 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
743 scan_tree_for_params (s
, TREE_OPERAND (e
, 1), tmp_expr
, k
);
747 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
749 ppl_delete_Linear_Expression (tmp_expr
);
757 ppl_Linear_Expression_t tmp_expr
= NULL
;
761 ppl_dimension_type dim
;
762 ppl_Linear_Expression_space_dimension (c
, &dim
);
763 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
766 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
770 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
772 ppl_delete_Linear_Expression (tmp_expr
);
780 ppl_Linear_Expression_t tmp_expr
= NULL
;
784 ppl_dimension_type dim
;
785 ppl_Linear_Expression_space_dimension (c
, &dim
);
786 ppl_new_Linear_Expression_with_dimension (&tmp_expr
, dim
);
789 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), tmp_expr
, k
);
793 ppl_Coefficient_t coef
;
796 ppl_subtract_Linear_Expression_from_Linear_Expression (c
,
798 ppl_delete_Linear_Expression (tmp_expr
);
799 mpz_init (minus_one
);
800 mpz_set_si (minus_one
, -1);
801 ppl_new_Coefficient_from_mpz_t (&coef
, minus_one
);
802 ppl_Linear_Expression_add_to_inhomogeneous (c
, coef
);
803 mpz_clear (minus_one
);
804 ppl_delete_Coefficient (coef
);
812 ppl_dimension_type p
= parameter_index_in_region (e
, s
);
816 ppl_dimension_type dim
;
817 ppl_Linear_Expression_space_dimension (c
, &dim
);
818 p
+= dim
- sese_nb_params (s
);
819 add_value_to_dim (p
, c
, k
);
826 scan_tree_for_params_int (e
, c
, k
);
830 case NON_LVALUE_EXPR
:
831 scan_tree_for_params (s
, TREE_OPERAND (e
, 0), c
, k
);
840 /* Find parameters with respect to REGION in BB. We are looking in memory
841 access functions, conditions and loop bounds. */
844 find_params_in_bb (sese region
, gimple_bb_p gbb
)
850 loop_p loop
= GBB_BB (gbb
)->loop_father
;
856 /* Find parameters in the access functions of data references. */
857 FOR_EACH_VEC_ELT (data_reference_p
, GBB_DATA_REFS (gbb
), i
, dr
)
858 for (j
= 0; j
< DR_NUM_DIMENSIONS (dr
); j
++)
859 scan_tree_for_params (region
, DR_ACCESS_FN (dr
, j
), NULL
, one
);
861 /* Find parameters in conditional statements. */
862 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
864 tree lhs
= scalar_evolution_in_region (region
, loop
,
865 gimple_cond_lhs (stmt
));
866 tree rhs
= scalar_evolution_in_region (region
, loop
,
867 gimple_cond_rhs (stmt
));
869 scan_tree_for_params (region
, lhs
, NULL
, one
);
870 scan_tree_for_params (region
, rhs
, NULL
, one
);
876 /* Record the parameters used in the SCOP. A variable is a parameter
877 in a scop if it does not vary during the execution of that scop. */
880 find_scop_parameters (scop_p scop
)
884 sese region
= SCOP_REGION (scop
);
891 /* Find the parameters used in the loop bounds. */
892 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
894 tree nb_iters
= number_of_latch_executions (loop
);
896 if (!chrec_contains_symbols (nb_iters
))
899 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
900 scan_tree_for_params (region
, nb_iters
, NULL
, one
);
905 /* Find the parameters used in data accesses. */
906 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
907 find_params_in_bb (region
, PBB_BLACK_BOX (pbb
));
909 scop_set_nb_params (scop
, sese_nb_params (region
));
910 SESE_ADD_PARAMS (region
) = false;
912 ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension
913 (&SCOP_CONTEXT (scop
), scop_nb_params (scop
), 0);
916 /* Insert in the SCOP context constraints from the estimation of the
917 number of iterations. UB_EXPR is a linear expression describing
918 the number of iterations in a loop. This expression is bounded by
919 the estimation NIT. */
922 add_upper_bounds_from_estimated_nit (scop_p scop
, double_int nit
,
923 ppl_dimension_type dim
,
924 ppl_Linear_Expression_t ub_expr
)
927 ppl_Linear_Expression_t nb_iters_le
;
928 ppl_Polyhedron_t pol
;
929 ppl_Coefficient_t coef
;
932 ppl_new_C_Polyhedron_from_space_dimension (&pol
, dim
, 0);
933 ppl_new_Linear_Expression_from_Linear_Expression (&nb_iters_le
,
936 /* Construct the negated number of last iteration in VAL. */
938 mpz_set_double_int (val
, nit
, false);
939 mpz_sub_ui (val
, val
, 1);
942 /* NB_ITERS_LE holds the number of last iteration in
943 parametrical form. Subtract estimated number of last
944 iteration and assert that result is not positive. */
945 ppl_new_Coefficient_from_mpz_t (&coef
, val
);
946 ppl_Linear_Expression_add_to_inhomogeneous (nb_iters_le
, coef
);
947 ppl_delete_Coefficient (coef
);
948 ppl_new_Constraint (&ub
, nb_iters_le
,
949 PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
950 ppl_Polyhedron_add_constraint (pol
, ub
);
952 /* Remove all but last GDIM dimensions from POL to obtain
953 only the constraints on the parameters. */
955 graphite_dim_t gdim
= scop_nb_params (scop
);
956 ppl_dimension_type
*dims
= XNEWVEC (ppl_dimension_type
, dim
- gdim
);
959 for (i
= 0; i
< dim
- gdim
; i
++)
962 ppl_Polyhedron_remove_space_dimensions (pol
, dims
, dim
- gdim
);
966 /* Add the constraints on the parameters to the SCoP context. */
968 ppl_Pointset_Powerset_C_Polyhedron_t constraints_ps
;
970 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
971 (&constraints_ps
, pol
);
972 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
973 (SCOP_CONTEXT (scop
), constraints_ps
);
974 ppl_delete_Pointset_Powerset_C_Polyhedron (constraints_ps
);
977 ppl_delete_Polyhedron (pol
);
978 ppl_delete_Linear_Expression (nb_iters_le
);
979 ppl_delete_Constraint (ub
);
983 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
984 the constraints for the surrounding loops. */
987 build_loop_iteration_domains (scop_p scop
, struct loop
*loop
,
988 ppl_Polyhedron_t outer_ph
, int nb
,
989 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
)
993 tree nb_iters
= number_of_latch_executions (loop
);
994 ppl_dimension_type dim
= nb
+ 1 + scop_nb_params (scop
);
995 sese region
= SCOP_REGION (scop
);
998 ppl_const_Constraint_System_t pcs
;
999 ppl_dimension_type
*map
1000 = (ppl_dimension_type
*) XNEWVEC (ppl_dimension_type
, dim
);
1002 ppl_new_C_Polyhedron_from_space_dimension (&ph
, dim
, 0);
1003 ppl_Polyhedron_get_constraints (outer_ph
, &pcs
);
1004 ppl_Polyhedron_add_constraints (ph
, pcs
);
1006 for (i
= 0; i
< (int) nb
; i
++)
1008 for (i
= (int) nb
; i
< (int) dim
- 1; i
++)
1012 ppl_Polyhedron_map_space_dimensions (ph
, map
, dim
);
1018 ppl_Constraint_t lb
;
1019 ppl_Linear_Expression_t lb_expr
;
1021 ppl_new_Linear_Expression_with_dimension (&lb_expr
, dim
);
1022 ppl_set_coef (lb_expr
, nb
, 1);
1023 ppl_new_Constraint (&lb
, lb_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1024 ppl_delete_Linear_Expression (lb_expr
);
1025 ppl_Polyhedron_add_constraint (ph
, lb
);
1026 ppl_delete_Constraint (lb
);
1029 if (TREE_CODE (nb_iters
) == INTEGER_CST
)
1031 ppl_Constraint_t ub
;
1032 ppl_Linear_Expression_t ub_expr
;
1034 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1036 /* loop_i <= cst_nb_iters */
1037 ppl_set_coef (ub_expr
, nb
, -1);
1038 ppl_set_inhomogeneous_tree (ub_expr
, nb_iters
);
1039 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1040 ppl_Polyhedron_add_constraint (ph
, ub
);
1041 ppl_delete_Linear_Expression (ub_expr
);
1042 ppl_delete_Constraint (ub
);
1044 else if (!chrec_contains_undetermined (nb_iters
))
1047 ppl_Constraint_t ub
;
1048 ppl_Linear_Expression_t ub_expr
;
1052 mpz_set_si (one
, 1);
1053 ppl_new_Linear_Expression_with_dimension (&ub_expr
, dim
);
1054 nb_iters
= scalar_evolution_in_region (region
, loop
, nb_iters
);
1055 scan_tree_for_params (SCOP_REGION (scop
), nb_iters
, ub_expr
, one
);
1058 if (estimated_loop_iterations (loop
, true, &nit
))
1059 add_upper_bounds_from_estimated_nit (scop
, nit
, dim
, ub_expr
);
1061 /* loop_i <= expr_nb_iters */
1062 ppl_set_coef (ub_expr
, nb
, -1);
1063 ppl_new_Constraint (&ub
, ub_expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1064 ppl_Polyhedron_add_constraint (ph
, ub
);
1065 ppl_delete_Linear_Expression (ub_expr
);
1066 ppl_delete_Constraint (ub
);
1071 if (loop
->inner
&& loop_in_sese_p (loop
->inner
, region
))
1072 build_loop_iteration_domains (scop
, loop
->inner
, ph
, nb
+ 1, domains
);
1076 && loop_in_sese_p (loop
->next
, region
))
1077 build_loop_iteration_domains (scop
, loop
->next
, outer_ph
, nb
, domains
);
1079 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1080 (&domains
[loop
->num
], ph
);
1082 ppl_delete_Polyhedron (ph
);
1085 /* Returns a linear expression for tree T evaluated in PBB. */
1087 static ppl_Linear_Expression_t
1088 create_linear_expr_from_tree (poly_bb_p pbb
, tree t
)
1091 ppl_Linear_Expression_t res
;
1092 ppl_dimension_type dim
;
1093 sese region
= SCOP_REGION (PBB_SCOP (pbb
));
1094 loop_p loop
= pbb_loop (pbb
);
1096 dim
= pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
1097 ppl_new_Linear_Expression_with_dimension (&res
, dim
);
1099 t
= scalar_evolution_in_region (region
, loop
, t
);
1100 gcc_assert (!automatically_generated_chrec_p (t
));
1103 mpz_set_si (one
, 1);
1104 scan_tree_for_params (region
, t
, res
, one
);
1110 /* Returns the ppl constraint type from the gimple tree code CODE. */
1112 static enum ppl_enum_Constraint_Type
1113 ppl_constraint_type_from_tree_code (enum tree_code code
)
1117 /* We do not support LT and GT to be able to work with C_Polyhedron.
1118 As we work on integer polyhedron "a < b" can be expressed by
1125 return PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
;
1128 return PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
;
1131 return PPL_CONSTRAINT_TYPE_EQUAL
;
1138 /* Add conditional statement STMT to PS. It is evaluated in PBB and
1139 CODE is used as the comparison operator. This allows us to invert the
1140 condition or to handle inequalities. */
1143 add_condition_to_domain (ppl_Pointset_Powerset_C_Polyhedron_t ps
, gimple stmt
,
1144 poly_bb_p pbb
, enum tree_code code
)
1147 ppl_Coefficient_t c
;
1148 ppl_Linear_Expression_t left
, right
;
1149 ppl_Constraint_t cstr
;
1150 enum ppl_enum_Constraint_Type type
;
1152 left
= create_linear_expr_from_tree (pbb
, gimple_cond_lhs (stmt
));
1153 right
= create_linear_expr_from_tree (pbb
, gimple_cond_rhs (stmt
));
1155 /* If we have < or > expressions convert them to <= or >= by adding 1 to
1156 the left or the right side of the expression. */
1157 if (code
== LT_EXPR
)
1161 ppl_new_Coefficient (&c
);
1162 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1163 ppl_Linear_Expression_add_to_inhomogeneous (left
, c
);
1164 ppl_delete_Coefficient (c
);
1169 else if (code
== GT_EXPR
)
1173 ppl_new_Coefficient (&c
);
1174 ppl_assign_Coefficient_from_mpz_t (c
, v
);
1175 ppl_Linear_Expression_add_to_inhomogeneous (right
, c
);
1176 ppl_delete_Coefficient (c
);
1182 type
= ppl_constraint_type_from_tree_code (code
);
1184 ppl_subtract_Linear_Expression_from_Linear_Expression (left
, right
);
1186 ppl_new_Constraint (&cstr
, left
, type
);
1187 ppl_Pointset_Powerset_C_Polyhedron_add_constraint (ps
, cstr
);
1189 ppl_delete_Constraint (cstr
);
1190 ppl_delete_Linear_Expression (left
);
1191 ppl_delete_Linear_Expression (right
);
1194 /* Add conditional statement STMT to pbb. CODE is used as the comparision
1195 operator. This allows us to invert the condition or to handle
1199 add_condition_to_pbb (poly_bb_p pbb
, gimple stmt
, enum tree_code code
)
1201 if (code
== NE_EXPR
)
1203 ppl_Pointset_Powerset_C_Polyhedron_t left
= PBB_DOMAIN (pbb
);
1204 ppl_Pointset_Powerset_C_Polyhedron_t right
;
1205 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1207 add_condition_to_domain (left
, stmt
, pbb
, LT_EXPR
);
1208 add_condition_to_domain (right
, stmt
, pbb
, GT_EXPR
);
1209 ppl_Pointset_Powerset_C_Polyhedron_upper_bound_assign (left
, right
);
1210 ppl_delete_Pointset_Powerset_C_Polyhedron (right
);
1213 add_condition_to_domain (PBB_DOMAIN (pbb
), stmt
, pbb
, code
);
1216 /* Add conditions to the domain of PBB. */
1219 add_conditions_to_domain (poly_bb_p pbb
)
1223 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
1225 if (VEC_empty (gimple
, GBB_CONDITIONS (gbb
)))
1228 FOR_EACH_VEC_ELT (gimple
, GBB_CONDITIONS (gbb
), i
, stmt
)
1229 switch (gimple_code (stmt
))
1233 enum tree_code code
= gimple_cond_code (stmt
);
1235 /* The conditions for ELSE-branches are inverted. */
1236 if (!VEC_index (gimple
, GBB_CONDITION_CASES (gbb
), i
))
1237 code
= invert_tree_comparison (code
, false);
1239 add_condition_to_pbb (pbb
, stmt
, code
);
1244 /* Switch statements are not supported right now - fall throught. */
1252 /* Traverses all the GBBs of the SCOP and add their constraints to the
1253 iteration domains. */
1256 add_conditions_to_constraints (scop_p scop
)
1261 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1262 add_conditions_to_domain (pbb
);
1265 /* Structure used to pass data to dom_walk. */
1269 VEC (gimple
, heap
) **conditions
, **cases
;
1273 /* Returns a COND_EXPR statement when BB has a single predecessor, the
1274 edge between BB and its predecessor is not a loop exit edge, and
1275 the last statement of the single predecessor is a COND_EXPR. */
1278 single_pred_cond_non_loop_exit (basic_block bb
)
1280 if (single_pred_p (bb
))
1282 edge e
= single_pred_edge (bb
);
1283 basic_block pred
= e
->src
;
1286 if (loop_depth (pred
->loop_father
) > loop_depth (bb
->loop_father
))
1289 stmt
= last_stmt (pred
);
1291 if (stmt
&& gimple_code (stmt
) == GIMPLE_COND
)
1298 /* Call-back for dom_walk executed before visiting the dominated
1302 build_sese_conditions_before (struct dom_walk_data
*dw_data
,
1305 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1306 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1307 VEC (gimple
, heap
) **cases
= data
->cases
;
1311 if (!bb_in_sese_p (bb
, data
->region
))
1314 stmt
= single_pred_cond_non_loop_exit (bb
);
1318 edge e
= single_pred_edge (bb
);
1320 VEC_safe_push (gimple
, heap
, *conditions
, stmt
);
1322 if (e
->flags
& EDGE_TRUE_VALUE
)
1323 VEC_safe_push (gimple
, heap
, *cases
, stmt
);
1325 VEC_safe_push (gimple
, heap
, *cases
, NULL
);
1328 gbb
= gbb_from_bb (bb
);
1332 GBB_CONDITIONS (gbb
) = VEC_copy (gimple
, heap
, *conditions
);
1333 GBB_CONDITION_CASES (gbb
) = VEC_copy (gimple
, heap
, *cases
);
1337 /* Call-back for dom_walk executed after visiting the dominated
1341 build_sese_conditions_after (struct dom_walk_data
*dw_data
,
1344 struct bsc
*data
= (struct bsc
*) dw_data
->global_data
;
1345 VEC (gimple
, heap
) **conditions
= data
->conditions
;
1346 VEC (gimple
, heap
) **cases
= data
->cases
;
1348 if (!bb_in_sese_p (bb
, data
->region
))
1351 if (single_pred_cond_non_loop_exit (bb
))
1353 VEC_pop (gimple
, *conditions
);
1354 VEC_pop (gimple
, *cases
);
1358 /* Record all conditions in REGION. */
1361 build_sese_conditions (sese region
)
1363 struct dom_walk_data walk_data
;
1364 VEC (gimple
, heap
) *conditions
= VEC_alloc (gimple
, heap
, 3);
1365 VEC (gimple
, heap
) *cases
= VEC_alloc (gimple
, heap
, 3);
1368 data
.conditions
= &conditions
;
1369 data
.cases
= &cases
;
1370 data
.region
= region
;
1372 walk_data
.dom_direction
= CDI_DOMINATORS
;
1373 walk_data
.initialize_block_local_data
= NULL
;
1374 walk_data
.before_dom_children
= build_sese_conditions_before
;
1375 walk_data
.after_dom_children
= build_sese_conditions_after
;
1376 walk_data
.global_data
= &data
;
1377 walk_data
.block_local_data_size
= 0;
1379 init_walk_dominator_tree (&walk_data
);
1380 walk_dominator_tree (&walk_data
, SESE_ENTRY_BB (region
));
1381 fini_walk_dominator_tree (&walk_data
);
1383 VEC_free (gimple
, heap
, conditions
);
1384 VEC_free (gimple
, heap
, cases
);
1387 /* Add constraints on the possible values of parameter P from the type
1391 add_param_constraints (scop_p scop
, ppl_Polyhedron_t context
, graphite_dim_t p
)
1393 ppl_Constraint_t cstr
;
1394 ppl_Linear_Expression_t le
;
1395 tree parameter
= VEC_index (tree
, SESE_PARAMS (SCOP_REGION (scop
)), p
);
1396 tree type
= TREE_TYPE (parameter
);
1397 tree lb
= NULL_TREE
;
1398 tree ub
= NULL_TREE
;
1400 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1401 lb
= lower_bound_in_type (type
, type
);
1403 lb
= TYPE_MIN_VALUE (type
);
1405 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1406 ub
= upper_bound_in_type (type
, type
);
1408 ub
= TYPE_MAX_VALUE (type
);
1412 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1413 ppl_set_coef (le
, p
, -1);
1414 ppl_set_inhomogeneous_tree (le
, lb
);
1415 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
);
1416 ppl_Polyhedron_add_constraint (context
, cstr
);
1417 ppl_delete_Linear_Expression (le
);
1418 ppl_delete_Constraint (cstr
);
1423 ppl_new_Linear_Expression_with_dimension (&le
, scop_nb_params (scop
));
1424 ppl_set_coef (le
, p
, -1);
1425 ppl_set_inhomogeneous_tree (le
, ub
);
1426 ppl_new_Constraint (&cstr
, le
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1427 ppl_Polyhedron_add_constraint (context
, cstr
);
1428 ppl_delete_Linear_Expression (le
);
1429 ppl_delete_Constraint (cstr
);
1433 /* Build the context of the SCOP. The context usually contains extra
1434 constraints that are added to the iteration domains that constrain
1438 build_scop_context (scop_p scop
)
1440 ppl_Polyhedron_t context
;
1441 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
1442 graphite_dim_t p
, n
= scop_nb_params (scop
);
1444 ppl_new_C_Polyhedron_from_space_dimension (&context
, n
, 0);
1446 for (p
= 0; p
< n
; p
++)
1447 add_param_constraints (scop
, context
, p
);
1449 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1451 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1452 (SCOP_CONTEXT (scop
), ps
);
1454 ppl_delete_Pointset_Powerset_C_Polyhedron (ps
);
1455 ppl_delete_Polyhedron (context
);
1458 /* Build the iteration domains: the loops belonging to the current
1459 SCOP, and that vary for the execution of the current basic block.
1460 Returns false if there is no loop in SCOP. */
1463 build_scop_iteration_domain (scop_p scop
)
1466 sese region
= SCOP_REGION (scop
);
1468 ppl_Polyhedron_t ph
;
1470 int nb_loops
= number_of_loops ();
1471 ppl_Pointset_Powerset_C_Polyhedron_t
*domains
1472 = XNEWVEC (ppl_Pointset_Powerset_C_Polyhedron_t
, nb_loops
);
1474 for (i
= 0; i
< nb_loops
; i
++)
1477 ppl_new_C_Polyhedron_from_space_dimension (&ph
, scop_nb_params (scop
), 0);
1479 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), i
, loop
)
1480 if (!loop_in_sese_p (loop_outer (loop
), region
))
1481 build_loop_iteration_domains (scop
, loop
, ph
, 0, domains
);
1483 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1484 if (domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
])
1485 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1486 (&PBB_DOMAIN (pbb
), (ppl_const_Pointset_Powerset_C_Polyhedron_t
)
1487 domains
[gbb_loop (PBB_BLACK_BOX (pbb
))->num
]);
1489 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1490 (&PBB_DOMAIN (pbb
), ph
);
1492 for (i
= 0; i
< nb_loops
; i
++)
1494 ppl_delete_Pointset_Powerset_C_Polyhedron (domains
[i
]);
1496 ppl_delete_Polyhedron (ph
);
1500 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1501 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1502 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1506 pdr_add_alias_set (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1507 ppl_dimension_type accessp_nb_dims
,
1508 ppl_dimension_type dom_nb_dims
)
1510 ppl_Linear_Expression_t alias
;
1511 ppl_Constraint_t cstr
;
1512 int alias_set_num
= 0;
1513 base_alias_pair
*bap
= (base_alias_pair
*)(dr
->aux
);
1515 if (bap
&& bap
->alias_set
)
1516 alias_set_num
= *(bap
->alias_set
);
1518 ppl_new_Linear_Expression_with_dimension (&alias
, accessp_nb_dims
);
1520 ppl_set_coef (alias
, dom_nb_dims
, 1);
1521 ppl_set_inhomogeneous (alias
, -alias_set_num
);
1522 ppl_new_Constraint (&cstr
, alias
, PPL_CONSTRAINT_TYPE_EQUAL
);
1523 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1525 ppl_delete_Linear_Expression (alias
);
1526 ppl_delete_Constraint (cstr
);
1529 /* Add to ACCESSES polyhedron equalities defining the access functions
1530 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1531 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1532 PBB is the poly_bb_p that contains the data reference DR. */
1535 pdr_add_memory_accesses (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1536 ppl_dimension_type accessp_nb_dims
,
1537 ppl_dimension_type dom_nb_dims
,
1540 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1542 scop_p scop
= PBB_SCOP (pbb
);
1543 sese region
= SCOP_REGION (scop
);
1547 for (i
= 0; i
< nb_subscripts
; i
++)
1549 ppl_Linear_Expression_t fn
, access
;
1550 ppl_Constraint_t cstr
;
1551 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1552 tree afn
= DR_ACCESS_FN (dr
, nb_subscripts
- 1 - i
);
1554 ppl_new_Linear_Expression_with_dimension (&fn
, dom_nb_dims
);
1555 ppl_new_Linear_Expression_with_dimension (&access
, accessp_nb_dims
);
1558 scan_tree_for_params (region
, afn
, fn
, v
);
1559 ppl_assign_Linear_Expression_from_Linear_Expression (access
, fn
);
1561 ppl_set_coef (access
, subscript
, -1);
1562 ppl_new_Constraint (&cstr
, access
, PPL_CONSTRAINT_TYPE_EQUAL
);
1563 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1565 ppl_delete_Linear_Expression (fn
);
1566 ppl_delete_Linear_Expression (access
);
1567 ppl_delete_Constraint (cstr
);
1573 /* Add constrains representing the size of the accessed data to the
1574 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1575 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1579 pdr_add_data_dimensions (ppl_Polyhedron_t accesses
, data_reference_p dr
,
1580 ppl_dimension_type accessp_nb_dims
,
1581 ppl_dimension_type dom_nb_dims
)
1583 tree ref
= DR_REF (dr
);
1584 int i
, nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1586 for (i
= nb_subscripts
- 1; i
>= 0; i
--, ref
= TREE_OPERAND (ref
, 0))
1588 ppl_Linear_Expression_t expr
;
1589 ppl_Constraint_t cstr
;
1590 ppl_dimension_type subscript
= dom_nb_dims
+ 1 + i
;
1593 if (TREE_CODE (ref
) != ARRAY_REF
)
1596 low
= array_ref_low_bound (ref
);
1598 /* subscript - low >= 0 */
1599 if (host_integerp (low
, 0))
1603 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1604 ppl_set_coef (expr
, subscript
, 1);
1606 minus_low
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (low
), low
);
1607 ppl_set_inhomogeneous_tree (expr
, minus_low
);
1609 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1610 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1611 ppl_delete_Linear_Expression (expr
);
1612 ppl_delete_Constraint (cstr
);
1615 high
= array_ref_up_bound (ref
);
1617 /* high - subscript >= 0 */
1618 if (high
&& host_integerp (high
, 0)
1619 /* 1-element arrays at end of structures may extend over
1620 their declared size. */
1621 && !(array_at_struct_end_p (ref
)
1622 && operand_equal_p (low
, high
, 0)))
1624 ppl_new_Linear_Expression_with_dimension (&expr
, accessp_nb_dims
);
1625 ppl_set_coef (expr
, subscript
, -1);
1627 ppl_set_inhomogeneous_tree (expr
, high
);
1629 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
1630 ppl_Polyhedron_add_constraint (accesses
, cstr
);
1631 ppl_delete_Linear_Expression (expr
);
1632 ppl_delete_Constraint (cstr
);
1637 /* Build data accesses for DR in PBB. */
1640 build_poly_dr (data_reference_p dr
, poly_bb_p pbb
)
1642 ppl_Polyhedron_t accesses
;
1643 ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps
;
1644 ppl_dimension_type dom_nb_dims
;
1645 ppl_dimension_type accessp_nb_dims
;
1646 int dr_base_object_set
;
1648 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb
),
1650 accessp_nb_dims
= dom_nb_dims
+ 1 + DR_NUM_DIMENSIONS (dr
);
1652 ppl_new_C_Polyhedron_from_space_dimension (&accesses
, accessp_nb_dims
, 0);
1654 pdr_add_alias_set (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1655 pdr_add_memory_accesses (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
, pbb
);
1656 pdr_add_data_dimensions (accesses
, dr
, accessp_nb_dims
, dom_nb_dims
);
1658 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps
,
1660 ppl_delete_Polyhedron (accesses
);
1662 gcc_assert (dr
->aux
);
1663 dr_base_object_set
= ((base_alias_pair
*)(dr
->aux
))->base_obj_set
;
1665 new_poly_dr (pbb
, dr_base_object_set
, accesses_ps
,
1666 DR_IS_READ (dr
) ? PDR_READ
: PDR_WRITE
,
1667 dr
, DR_NUM_DIMENSIONS (dr
));
1670 /* Write to FILE the alias graph of data references in DIMACS format. */
1673 write_alias_graph_to_ascii_dimacs (FILE *file
, char *comment
,
1674 VEC (data_reference_p
, heap
) *drs
)
1676 int num_vertex
= VEC_length (data_reference_p
, drs
);
1678 data_reference_p dr1
, dr2
;
1681 if (num_vertex
== 0)
1684 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1685 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1686 if (dr_may_alias_p (dr1
, dr2
))
1689 fprintf (file
, "$\n");
1692 fprintf (file
, "c %s\n", comment
);
1694 fprintf (file
, "p edge %d %d\n", num_vertex
, edge_num
);
1696 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1697 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1698 if (dr_may_alias_p (dr1
, dr2
))
1699 fprintf (file
, "e %d %d\n", i
+ 1, j
+ 1);
1704 /* Write to FILE the alias graph of data references in DOT format. */
1707 write_alias_graph_to_ascii_dot (FILE *file
, char *comment
,
1708 VEC (data_reference_p
, heap
) *drs
)
1710 int num_vertex
= VEC_length (data_reference_p
, drs
);
1711 data_reference_p dr1
, dr2
;
1714 if (num_vertex
== 0)
1717 fprintf (file
, "$\n");
1720 fprintf (file
, "c %s\n", comment
);
1722 /* First print all the vertices. */
1723 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1724 fprintf (file
, "n%d;\n", i
);
1726 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1727 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1728 if (dr_may_alias_p (dr1
, dr2
))
1729 fprintf (file
, "n%d n%d\n", i
, j
);
1734 /* Write to FILE the alias graph of data references in ECC format. */
1737 write_alias_graph_to_ascii_ecc (FILE *file
, char *comment
,
1738 VEC (data_reference_p
, heap
) *drs
)
1740 int num_vertex
= VEC_length (data_reference_p
, drs
);
1741 data_reference_p dr1
, dr2
;
1744 if (num_vertex
== 0)
1747 fprintf (file
, "$\n");
1750 fprintf (file
, "c %s\n", comment
);
1752 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1753 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1754 if (dr_may_alias_p (dr1
, dr2
))
1755 fprintf (file
, "%d %d\n", i
, j
);
1760 /* Check if DR1 and DR2 are in the same object set. */
1763 dr_same_base_object_p (const struct data_reference
*dr1
,
1764 const struct data_reference
*dr2
)
1766 return operand_equal_p (DR_BASE_OBJECT (dr1
), DR_BASE_OBJECT (dr2
), 0);
1769 /* Uses DFS component number as representative of alias-sets. Also tests for
1770 optimality by verifying if every connected component is a clique. Returns
1771 true (1) if the above test is true, and false (0) otherwise. */
1774 build_alias_set_optimal_p (VEC (data_reference_p
, heap
) *drs
)
1776 int num_vertices
= VEC_length (data_reference_p
, drs
);
1777 struct graph
*g
= new_graph (num_vertices
);
1778 data_reference_p dr1
, dr2
;
1780 int num_connected_components
;
1781 int v_indx1
, v_indx2
, num_vertices_in_component
;
1784 struct graph_edge
*e
;
1785 int this_component_is_clique
;
1786 int all_components_are_cliques
= 1;
1788 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1789 for (j
= i
+1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1790 if (dr_may_alias_p (dr1
, dr2
))
1796 all_vertices
= XNEWVEC (int, num_vertices
);
1797 vertices
= XNEWVEC (int, num_vertices
);
1798 for (i
= 0; i
< num_vertices
; i
++)
1799 all_vertices
[i
] = i
;
1801 num_connected_components
= graphds_dfs (g
, all_vertices
, num_vertices
,
1803 for (i
= 0; i
< g
->n_vertices
; i
++)
1805 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1806 base_alias_pair
*bap
;
1808 gcc_assert (dr
->aux
);
1809 bap
= (base_alias_pair
*)(dr
->aux
);
1811 bap
->alias_set
= XNEW (int);
1812 *(bap
->alias_set
) = g
->vertices
[i
].component
+ 1;
1815 /* Verify if the DFS numbering results in optimal solution. */
1816 for (i
= 0; i
< num_connected_components
; i
++)
1818 num_vertices_in_component
= 0;
1819 /* Get all vertices whose DFS component number is the same as i. */
1820 for (j
= 0; j
< num_vertices
; j
++)
1821 if (g
->vertices
[j
].component
== i
)
1822 vertices
[num_vertices_in_component
++] = j
;
1824 /* Now test if the vertices in 'vertices' form a clique, by testing
1825 for edges among each pair. */
1826 this_component_is_clique
= 1;
1827 for (v_indx1
= 0; v_indx1
< num_vertices_in_component
; v_indx1
++)
1829 for (v_indx2
= v_indx1
+1; v_indx2
< num_vertices_in_component
; v_indx2
++)
1831 /* Check if the two vertices are connected by iterating
1832 through all the edges which have one of these are source. */
1833 e
= g
->vertices
[vertices
[v_indx2
]].pred
;
1836 if (e
->src
== vertices
[v_indx1
])
1842 this_component_is_clique
= 0;
1846 if (!this_component_is_clique
)
1847 all_components_are_cliques
= 0;
1851 free (all_vertices
);
1854 return all_components_are_cliques
;
1857 /* Group each data reference in DRS with its base object set num. */
1860 build_base_obj_set_for_drs (VEC (data_reference_p
, heap
) *drs
)
1862 int num_vertex
= VEC_length (data_reference_p
, drs
);
1863 struct graph
*g
= new_graph (num_vertex
);
1864 data_reference_p dr1
, dr2
;
1868 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr1
)
1869 for (j
= i
+ 1; VEC_iterate (data_reference_p
, drs
, j
, dr2
); j
++)
1870 if (dr_same_base_object_p (dr1
, dr2
))
1876 queue
= XNEWVEC (int, num_vertex
);
1877 for (i
= 0; i
< num_vertex
; i
++)
1880 graphds_dfs (g
, queue
, num_vertex
, NULL
, true, NULL
);
1882 for (i
= 0; i
< g
->n_vertices
; i
++)
1884 data_reference_p dr
= VEC_index (data_reference_p
, drs
, i
);
1885 base_alias_pair
*bap
;
1887 gcc_assert (dr
->aux
);
1888 bap
= (base_alias_pair
*)(dr
->aux
);
1890 bap
->base_obj_set
= g
->vertices
[i
].component
+ 1;
1897 /* Build the data references for PBB. */
1900 build_pbb_drs (poly_bb_p pbb
)
1903 data_reference_p dr
;
1904 VEC (data_reference_p
, heap
) *gbb_drs
= GBB_DATA_REFS (PBB_BLACK_BOX (pbb
));
1906 FOR_EACH_VEC_ELT (data_reference_p
, gbb_drs
, j
, dr
)
1907 build_poly_dr (dr
, pbb
);
1910 /* Dump to file the alias graphs for the data references in DRS. */
1913 dump_alias_graphs (VEC (data_reference_p
, heap
) *drs
)
1916 FILE *file_dimacs
, *file_ecc
, *file_dot
;
1918 file_dimacs
= fopen ("/tmp/dr_alias_graph_dimacs", "ab");
1921 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1922 current_function_name ());
1923 write_alias_graph_to_ascii_dimacs (file_dimacs
, comment
, drs
);
1924 fclose (file_dimacs
);
1927 file_ecc
= fopen ("/tmp/dr_alias_graph_ecc", "ab");
1930 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1931 current_function_name ());
1932 write_alias_graph_to_ascii_ecc (file_ecc
, comment
, drs
);
1936 file_dot
= fopen ("/tmp/dr_alias_graph_dot", "ab");
1939 snprintf (comment
, sizeof (comment
), "%s %s", main_input_filename
,
1940 current_function_name ());
1941 write_alias_graph_to_ascii_dot (file_dot
, comment
, drs
);
1946 /* Build data references in SCOP. */
1949 build_scop_drs (scop_p scop
)
1953 data_reference_p dr
;
1954 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 3);
1956 /* Remove all the PBBs that do not have data references: these basic
1957 blocks are not handled in the polyhedral representation. */
1958 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1959 if (VEC_empty (data_reference_p
, GBB_DATA_REFS (PBB_BLACK_BOX (pbb
))))
1961 free_gimple_bb (PBB_BLACK_BOX (pbb
));
1962 VEC_ordered_remove (poly_bb_p
, SCOP_BBS (scop
), i
);
1966 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1967 for (j
= 0; VEC_iterate (data_reference_p
,
1968 GBB_DATA_REFS (PBB_BLACK_BOX (pbb
)), j
, dr
); j
++)
1969 VEC_safe_push (data_reference_p
, heap
, drs
, dr
);
1971 FOR_EACH_VEC_ELT (data_reference_p
, drs
, i
, dr
)
1972 dr
->aux
= XNEW (base_alias_pair
);
1974 if (!build_alias_set_optimal_p (drs
))
1976 /* TODO: Add support when building alias set is not optimal. */
1980 build_base_obj_set_for_drs (drs
);
1982 /* When debugging, enable the following code. This cannot be used
1983 in production compilers. */
1985 dump_alias_graphs (drs
);
1987 VEC_free (data_reference_p
, heap
, drs
);
1989 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1990 build_pbb_drs (pbb
);
1993 /* Return a gsi at the position of the phi node STMT. */
1995 static gimple_stmt_iterator
1996 gsi_for_phi_node (gimple stmt
)
1998 gimple_stmt_iterator psi
;
1999 basic_block bb
= gimple_bb (stmt
);
2001 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2002 if (stmt
== gsi_stmt (psi
))
2009 /* Analyze all the data references of STMTS and add them to the
2010 GBB_DATA_REFS vector of BB. */
2013 analyze_drs_in_stmts (scop_p scop
, basic_block bb
, VEC (gimple
, heap
) *stmts
)
2020 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
2023 nest
= outermost_loop_in_sese (SCOP_REGION (scop
), bb
);
2024 gbb
= gbb_from_bb (bb
);
2026 FOR_EACH_VEC_ELT (gimple
, stmts
, i
, stmt
)
2027 if (!is_gimple_debug (stmt
))
2028 graphite_find_data_references_in_stmt (nest
, stmt
,
2029 &GBB_DATA_REFS (gbb
));
2032 /* Insert STMT at the end of the STMTS sequence and then insert the
2033 statements from STMTS at INSERT_GSI and call analyze_drs_in_stmts
2037 insert_stmts (scop_p scop
, gimple stmt
, gimple_seq stmts
,
2038 gimple_stmt_iterator insert_gsi
)
2040 gimple_stmt_iterator gsi
;
2041 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2044 stmts
= gimple_seq_alloc ();
2046 gsi
= gsi_last (stmts
);
2047 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2048 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2049 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2051 gsi_insert_seq_before (&insert_gsi
, stmts
, GSI_SAME_STMT
);
2052 analyze_drs_in_stmts (scop
, gsi_bb (insert_gsi
), x
);
2053 VEC_free (gimple
, heap
, x
);
2056 /* Insert the assignment "RES := EXPR" just after AFTER_STMT. */
2059 insert_out_of_ssa_copy (scop_p scop
, tree res
, tree expr
, gimple after_stmt
)
2062 gimple_stmt_iterator si
;
2063 gimple_stmt_iterator gsi
;
2064 tree var
= force_gimple_operand (expr
, &stmts
, true, NULL_TREE
);
2065 gimple stmt
= gimple_build_assign (res
, var
);
2066 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2069 stmts
= gimple_seq_alloc ();
2070 si
= gsi_last (stmts
);
2071 gsi_insert_after (&si
, stmt
, GSI_NEW_STMT
);
2072 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2073 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2075 if (gimple_code (after_stmt
) == GIMPLE_PHI
)
2077 gsi
= gsi_after_labels (gimple_bb (after_stmt
));
2078 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
2082 gsi
= gsi_for_stmt (after_stmt
);
2083 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
2086 analyze_drs_in_stmts (scop
, gimple_bb (after_stmt
), x
);
2087 VEC_free (gimple
, heap
, x
);
2090 /* Creates a poly_bb_p for basic_block BB from the existing PBB. */
2093 new_pbb_from_pbb (scop_p scop
, poly_bb_p pbb
, basic_block bb
)
2095 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 3);
2096 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
2097 gimple_bb_p gbb1
= new_gimple_bb (bb
, drs
);
2098 poly_bb_p pbb1
= new_poly_bb (scop
, gbb1
);
2099 int index
, n
= VEC_length (poly_bb_p
, SCOP_BBS (scop
));
2101 /* The INDEX of PBB in SCOP_BBS. */
2102 for (index
= 0; index
< n
; index
++)
2103 if (VEC_index (poly_bb_p
, SCOP_BBS (scop
), index
) == pbb
)
2106 GBB_PBB (gbb1
) = pbb1
;
2107 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
2108 (&PBB_DOMAIN (pbb1
), PBB_DOMAIN (pbb
));
2109 GBB_CONDITIONS (gbb1
) = VEC_copy (gimple
, heap
, GBB_CONDITIONS (gbb
));
2110 GBB_CONDITION_CASES (gbb1
) = VEC_copy (gimple
, heap
, GBB_CONDITION_CASES (gbb
));
2111 VEC_safe_insert (poly_bb_p
, heap
, SCOP_BBS (scop
), index
+ 1, pbb1
);
2114 /* Insert on edge E the assignment "RES := EXPR". */
2117 insert_out_of_ssa_copy_on_edge (scop_p scop
, edge e
, tree res
, tree expr
)
2119 gimple_stmt_iterator gsi
;
2121 tree var
= force_gimple_operand (expr
, &stmts
, true, NULL_TREE
);
2122 gimple stmt
= gimple_build_assign (res
, var
);
2124 VEC (gimple
, heap
) *x
= VEC_alloc (gimple
, heap
, 3);
2127 stmts
= gimple_seq_alloc ();
2129 gsi
= gsi_last (stmts
);
2130 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
2131 for (gsi
= gsi_start (stmts
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2132 VEC_safe_push (gimple
, heap
, x
, gsi_stmt (gsi
));
2134 gsi_insert_seq_on_edge (e
, stmts
);
2135 gsi_commit_edge_inserts ();
2136 bb
= gimple_bb (stmt
);
2138 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
2141 if (!gbb_from_bb (bb
))
2142 new_pbb_from_pbb (scop
, pbb_from_bb (e
->src
), bb
);
2144 analyze_drs_in_stmts (scop
, bb
, x
);
2145 VEC_free (gimple
, heap
, x
);
2148 /* Creates a zero dimension array of the same type as VAR. */
2151 create_zero_dim_array (tree var
, const char *base_name
)
2153 tree index_type
= build_index_type (integer_zero_node
);
2154 tree elt_type
= TREE_TYPE (var
);
2155 tree array_type
= build_array_type (elt_type
, index_type
);
2156 tree base
= create_tmp_var (array_type
, base_name
);
2158 add_referenced_var (base
);
2160 return build4 (ARRAY_REF
, elt_type
, base
, integer_zero_node
, NULL_TREE
,
2164 /* Returns true when PHI is a loop close phi node. */
2167 scalar_close_phi_node_p (gimple phi
)
2169 if (gimple_code (phi
) != GIMPLE_PHI
2170 || !is_gimple_reg (gimple_phi_result (phi
)))
2173 /* Note that loop close phi nodes should have a single argument
2174 because we translated the representation into a canonical form
2175 before Graphite: see canonicalize_loop_closed_ssa_form. */
2176 return (gimple_phi_num_args (phi
) == 1);
2179 /* For a definition DEF in REGION, propagates the expression EXPR in
2180 all the uses of DEF outside REGION. */
2183 propagate_expr_outside_region (tree def
, tree expr
, sese region
)
2185 imm_use_iterator imm_iter
;
2188 bool replaced_once
= false;
2190 gcc_assert (TREE_CODE (def
) == SSA_NAME
);
2192 expr
= force_gimple_operand (unshare_expr (expr
), &stmts
, true,
2195 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2196 if (!is_gimple_debug (use_stmt
)
2197 && !bb_in_sese_p (gimple_bb (use_stmt
), region
))
2200 use_operand_p use_p
;
2202 FOR_EACH_PHI_OR_STMT_USE (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2203 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0)
2204 && (replaced_once
= true))
2205 replace_exp (use_p
, expr
);
2207 update_stmt (use_stmt
);
2212 gsi_insert_seq_on_edge (SESE_ENTRY (region
), stmts
);
2213 gsi_commit_edge_inserts ();
2217 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2218 dimension array for it. */
2221 rewrite_close_phi_out_of_ssa (scop_p scop
, gimple_stmt_iterator
*psi
)
2223 sese region
= SCOP_REGION (scop
);
2224 gimple phi
= gsi_stmt (*psi
);
2225 tree res
= gimple_phi_result (phi
);
2226 tree var
= SSA_NAME_VAR (res
);
2227 basic_block bb
= gimple_bb (phi
);
2228 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
2229 tree arg
= gimple_phi_arg_def (phi
, 0);
2232 /* Note that loop close phi nodes should have a single argument
2233 because we translated the representation into a canonical form
2234 before Graphite: see canonicalize_loop_closed_ssa_form. */
2235 gcc_assert (gimple_phi_num_args (phi
) == 1);
2237 /* The phi node can be a non close phi node, when its argument is
2238 invariant, or a default definition. */
2239 if (is_gimple_min_invariant (arg
)
2240 || SSA_NAME_IS_DEFAULT_DEF (arg
))
2242 propagate_expr_outside_region (res
, arg
, region
);
2247 else if (gimple_bb (SSA_NAME_DEF_STMT (arg
))->loop_father
== bb
->loop_father
)
2249 propagate_expr_outside_region (res
, arg
, region
);
2250 stmt
= gimple_build_assign (res
, arg
);
2251 remove_phi_node (psi
, false);
2252 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
2253 SSA_NAME_DEF_STMT (res
) = stmt
;
2257 /* If res is scev analyzable and is not a scalar value, it is safe
2258 to ignore the close phi node: it will be code generated in the
2259 out of Graphite pass. */
2260 else if (scev_analyzable_p (res
, region
))
2262 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (res
));
2265 if (!loop_in_sese_p (loop
, region
))
2267 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (arg
));
2268 scev
= scalar_evolution_in_region (region
, loop
, arg
);
2269 scev
= compute_overall_effect_of_inner_loop (loop
, scev
);
2272 scev
= scalar_evolution_in_region (region
, loop
, res
);
2274 if (tree_does_not_contain_chrecs (scev
))
2275 propagate_expr_outside_region (res
, scev
, region
);
2282 tree zero_dim_array
= create_zero_dim_array (var
, "Close_Phi");
2284 stmt
= gimple_build_assign (res
, zero_dim_array
);
2286 if (TREE_CODE (arg
) == SSA_NAME
)
2287 insert_out_of_ssa_copy (scop
, zero_dim_array
, arg
,
2288 SSA_NAME_DEF_STMT (arg
));
2290 insert_out_of_ssa_copy_on_edge (scop
, single_pred_edge (bb
),
2291 zero_dim_array
, arg
);
2294 remove_phi_node (psi
, false);
2295 SSA_NAME_DEF_STMT (res
) = stmt
;
2297 insert_stmts (scop
, stmt
, NULL
, gsi_after_labels (bb
));
2300 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2301 dimension array for it. */
2304 rewrite_phi_out_of_ssa (scop_p scop
, gimple_stmt_iterator
*psi
)
2307 gimple phi
= gsi_stmt (*psi
);
2308 basic_block bb
= gimple_bb (phi
);
2309 tree res
= gimple_phi_result (phi
);
2310 tree var
= SSA_NAME_VAR (res
);
2311 tree zero_dim_array
= create_zero_dim_array (var
, "phi_out_of_ssa");
2315 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2317 tree arg
= gimple_phi_arg_def (phi
, i
);
2318 edge e
= gimple_phi_arg_edge (phi
, i
);
2320 /* Avoid the insertion of code in the loop latch to please the
2321 pattern matching of the vectorizer. */
2322 if (TREE_CODE (arg
) == SSA_NAME
2323 && e
->src
== bb
->loop_father
->latch
)
2324 insert_out_of_ssa_copy (scop
, zero_dim_array
, arg
,
2325 SSA_NAME_DEF_STMT (arg
));
2327 insert_out_of_ssa_copy_on_edge (scop
, e
, zero_dim_array
, arg
);
2330 var
= force_gimple_operand (zero_dim_array
, &stmts
, true, NULL_TREE
);
2332 stmt
= gimple_build_assign (res
, var
);
2333 remove_phi_node (psi
, false);
2334 SSA_NAME_DEF_STMT (res
) = stmt
;
2336 insert_stmts (scop
, stmt
, stmts
, gsi_after_labels (bb
));
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 (scop
, &psi
);
2392 else if (reduction_phi_p (region
, &psi
))
2393 rewrite_phi_out_of_ssa (scop
, &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 (scop_p scop
, tree zero_dim_array
,
2407 tree def
, gimple use_stmt
)
2409 tree var
= SSA_NAME_VAR (def
);
2410 gimple name_stmt
= gimple_build_assign (var
, zero_dim_array
);
2411 tree name
= make_ssa_name (var
, name_stmt
);
2413 use_operand_p use_p
;
2415 gcc_assert (gimple_code (use_stmt
) != GIMPLE_PHI
);
2417 gimple_assign_set_lhs (name_stmt
, name
);
2418 insert_stmts (scop
, name_stmt
, NULL
, gsi_for_stmt (use_stmt
));
2420 FOR_EACH_SSA_USE_OPERAND (use_p
, use_stmt
, iter
, SSA_OP_ALL_USES
)
2421 if (operand_equal_p (def
, USE_FROM_PTR (use_p
), 0))
2422 replace_exp (use_p
, name
);
2424 update_stmt (use_stmt
);
2427 /* For every definition DEF in the SCOP that is used outside the scop,
2428 insert a closing-scop definition in the basic block just after this
2432 handle_scalar_deps_crossing_scop_limits (scop_p scop
, tree def
, gimple stmt
)
2434 tree var
= create_tmp_reg (TREE_TYPE (def
), NULL
);
2435 tree new_name
= make_ssa_name (var
, stmt
);
2436 bool needs_copy
= false;
2437 use_operand_p use_p
;
2438 imm_use_iterator imm_iter
;
2440 sese region
= SCOP_REGION (scop
);
2442 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2444 if (!bb_in_sese_p (gimple_bb (use_stmt
), region
))
2446 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
2448 SET_USE (use_p
, new_name
);
2450 update_stmt (use_stmt
);
2455 /* Insert in the empty BB just after the scop a use of DEF such
2456 that the rewrite of cross_bb_scalar_dependences won't insert
2457 arrays everywhere else. */
2460 gimple assign
= gimple_build_assign (new_name
, def
);
2461 gimple_stmt_iterator psi
= gsi_after_labels (SESE_EXIT (region
)->dest
);
2463 add_referenced_var (var
);
2464 SSA_NAME_DEF_STMT (new_name
) = assign
;
2465 update_stmt (assign
);
2466 gsi_insert_before (&psi
, assign
, GSI_SAME_STMT
);
2470 /* Rewrite the scalar dependences crossing the boundary of the BB
2471 containing STMT with an array. Return true when something has been
2475 rewrite_cross_bb_scalar_deps (scop_p scop
, gimple_stmt_iterator
*gsi
)
2477 sese region
= SCOP_REGION (scop
);
2478 gimple stmt
= gsi_stmt (*gsi
);
2479 imm_use_iterator imm_iter
;
2482 tree zero_dim_array
= NULL_TREE
;
2486 switch (gimple_code (stmt
))
2489 def
= gimple_assign_lhs (stmt
);
2493 def
= gimple_call_lhs (stmt
);
2501 || !is_gimple_reg (def
))
2504 if (scev_analyzable_p (def
, region
))
2506 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (def
));
2507 tree scev
= scalar_evolution_in_region (region
, loop
, def
);
2509 if (tree_contains_chrecs (scev
, NULL
))
2512 propagate_expr_outside_region (def
, scev
, region
);
2516 def_bb
= gimple_bb (stmt
);
2518 handle_scalar_deps_crossing_scop_limits (scop
, def
, stmt
);
2520 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2521 if (gimple_code (use_stmt
) == GIMPLE_PHI
2524 gimple_stmt_iterator psi
= gsi_for_stmt (use_stmt
);
2526 if (scalar_close_phi_node_p (gsi_stmt (psi
)))
2527 rewrite_close_phi_out_of_ssa (scop
, &psi
);
2529 rewrite_phi_out_of_ssa (scop
, &psi
);
2532 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
2533 if (gimple_code (use_stmt
) != GIMPLE_PHI
2534 && def_bb
!= gimple_bb (use_stmt
)
2535 && !is_gimple_debug (use_stmt
)
2538 if (!zero_dim_array
)
2540 zero_dim_array
= create_zero_dim_array
2541 (SSA_NAME_VAR (def
), "Cross_BB_scalar_dependence");
2542 insert_out_of_ssa_copy (scop
, zero_dim_array
, def
,
2543 SSA_NAME_DEF_STMT (def
));
2547 rewrite_cross_bb_scalar_dependence (scop
, zero_dim_array
,
2554 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2557 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop
)
2560 gimple_stmt_iterator psi
;
2561 sese region
= SCOP_REGION (scop
);
2562 bool changed
= false;
2564 /* Create an extra empty BB after the scop. */
2565 split_edge (SESE_EXIT (region
));
2568 if (bb_in_sese_p (bb
, region
))
2569 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2570 changed
|= rewrite_cross_bb_scalar_deps (scop
, &psi
);
2575 update_ssa (TODO_update_ssa
);
2576 #ifdef ENABLE_CHECKING
2577 verify_loop_closed_ssa (true);
2582 /* Returns the number of pbbs that are in loops contained in SCOP. */
2585 nb_pbbs_in_loops (scop_p scop
)
2591 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
2592 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb
)), SCOP_REGION (scop
)))
2598 /* Return the number of data references in BB that write in
2602 nb_data_writes_in_bb (basic_block bb
)
2605 gimple_stmt_iterator gsi
;
2607 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2608 if (gimple_vdef (gsi_stmt (gsi
)))
2614 /* Splits at STMT the basic block BB represented as PBB in the
2618 split_pbb (scop_p scop
, poly_bb_p pbb
, basic_block bb
, gimple stmt
)
2620 edge e1
= split_block (bb
, stmt
);
2621 new_pbb_from_pbb (scop
, pbb
, e1
->dest
);
2625 /* Splits STMT out of its current BB. This is done for reduction
2626 statements for which we want to ignore data dependences. */
2629 split_reduction_stmt (scop_p scop
, gimple stmt
)
2631 basic_block bb
= gimple_bb (stmt
);
2632 poly_bb_p pbb
= pbb_from_bb (bb
);
2633 gimple_bb_p gbb
= gbb_from_bb (bb
);
2636 data_reference_p dr
;
2638 /* Do not split basic blocks with no writes to memory: the reduction
2639 will be the only write to memory. */
2640 if (nb_data_writes_in_bb (bb
) == 0)
2643 e1
= split_pbb (scop
, pbb
, bb
, stmt
);
2645 /* Split once more only when the reduction stmt is not the only one
2646 left in the original BB. */
2647 if (!gsi_one_before_end_p (gsi_start_nondebug_bb (bb
)))
2649 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
2651 e1
= split_pbb (scop
, pbb
, bb
, gsi_stmt (gsi
));
2654 /* A part of the data references will end in a different basic block
2655 after the split: move the DRs from the original GBB to the newly
2657 FOR_EACH_VEC_ELT (data_reference_p
, GBB_DATA_REFS (gbb
), i
, dr
)
2659 basic_block bb1
= gimple_bb (DR_STMT (dr
));
2663 gimple_bb_p gbb1
= gbb_from_bb (bb1
);
2664 VEC_safe_push (data_reference_p
, heap
, GBB_DATA_REFS (gbb1
), dr
);
2665 VEC_ordered_remove (data_reference_p
, GBB_DATA_REFS (gbb
), i
);
2673 /* Return true when stmt is a reduction operation. */
2676 is_reduction_operation_p (gimple stmt
)
2678 enum tree_code code
;
2680 gcc_assert (is_gimple_assign (stmt
));
2681 code
= gimple_assign_rhs_code (stmt
);
2683 return flag_associative_math
2684 && commutative_tree_code (code
)
2685 && associative_tree_code (code
);
2688 /* Returns true when PHI contains an argument ARG. */
2691 phi_contains_arg (gimple phi
, tree arg
)
2695 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2696 if (operand_equal_p (arg
, gimple_phi_arg_def (phi
, i
), 0))
2702 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2705 follow_ssa_with_commutative_ops (tree arg
, tree lhs
)
2709 if (TREE_CODE (arg
) != SSA_NAME
)
2712 stmt
= SSA_NAME_DEF_STMT (arg
);
2714 if (gimple_code (stmt
) == GIMPLE_NOP
2715 || gimple_code (stmt
) == GIMPLE_CALL
)
2718 if (gimple_code (stmt
) == GIMPLE_PHI
)
2720 if (phi_contains_arg (stmt
, lhs
))
2725 if (!is_gimple_assign (stmt
))
2728 if (gimple_num_ops (stmt
) == 2)
2729 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2731 if (is_reduction_operation_p (stmt
))
2733 gimple res
= follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt
), lhs
);
2736 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt
), lhs
);
2742 /* Detect commutative and associative scalar reductions starting at
2743 the STMT. Return the phi node of the reduction cycle, or NULL. */
2746 detect_commutative_reduction_arg (tree lhs
, gimple stmt
, tree arg
,
2747 VEC (gimple
, heap
) **in
,
2748 VEC (gimple
, heap
) **out
)
2750 gimple phi
= follow_ssa_with_commutative_ops (arg
, lhs
);
2755 VEC_safe_push (gimple
, heap
, *in
, stmt
);
2756 VEC_safe_push (gimple
, heap
, *out
, stmt
);
2760 /* Detect commutative and associative scalar reductions starting at
2761 STMT. Return the phi node of the reduction cycle, or NULL. */
2764 detect_commutative_reduction_assign (gimple stmt
, VEC (gimple
, heap
) **in
,
2765 VEC (gimple
, heap
) **out
)
2767 tree lhs
= gimple_assign_lhs (stmt
);
2769 if (gimple_num_ops (stmt
) == 2)
2770 return detect_commutative_reduction_arg (lhs
, stmt
,
2771 gimple_assign_rhs1 (stmt
),
2774 if (is_reduction_operation_p (stmt
))
2776 gimple res
= detect_commutative_reduction_arg (lhs
, stmt
,
2777 gimple_assign_rhs1 (stmt
),
2780 : detect_commutative_reduction_arg (lhs
, stmt
,
2781 gimple_assign_rhs2 (stmt
),
2788 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2791 follow_inital_value_to_phi (tree arg
, tree lhs
)
2795 if (!arg
|| TREE_CODE (arg
) != SSA_NAME
)
2798 stmt
= SSA_NAME_DEF_STMT (arg
);
2800 if (gimple_code (stmt
) == GIMPLE_PHI
2801 && phi_contains_arg (stmt
, lhs
))
2808 /* Return the argument of the loop PHI that is the inital value coming
2809 from outside the loop. */
2812 edge_initial_value_for_loop_phi (gimple phi
)
2816 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2818 edge e
= gimple_phi_arg_edge (phi
, i
);
2820 if (loop_depth (e
->src
->loop_father
)
2821 < loop_depth (e
->dest
->loop_father
))
2828 /* Return the argument of the loop PHI that is the inital value coming
2829 from outside the loop. */
2832 initial_value_for_loop_phi (gimple phi
)
2836 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2838 edge e
= gimple_phi_arg_edge (phi
, i
);
2840 if (loop_depth (e
->src
->loop_father
)
2841 < loop_depth (e
->dest
->loop_father
))
2842 return gimple_phi_arg_def (phi
, i
);
2848 /* Detect commutative and associative scalar reductions belonging to
2849 the SCOP starting at the loop closed phi node STMT. Return the phi
2850 node of the reduction cycle, or NULL. */
2853 detect_commutative_reduction (scop_p scop
, gimple stmt
, VEC (gimple
, heap
) **in
,
2854 VEC (gimple
, heap
) **out
)
2856 if (scalar_close_phi_node_p (stmt
))
2858 tree arg
= gimple_phi_arg_def (stmt
, 0);
2859 gimple def
, loop_phi
;
2861 if (TREE_CODE (arg
) != SSA_NAME
)
2864 /* Note that loop close phi nodes should have a single argument
2865 because we translated the representation into a canonical form
2866 before Graphite: see canonicalize_loop_closed_ssa_form. */
2867 gcc_assert (gimple_phi_num_args (stmt
) == 1);
2869 def
= SSA_NAME_DEF_STMT (arg
);
2870 if (!stmt_in_sese_p (def
, SCOP_REGION (scop
)))
2873 loop_phi
= detect_commutative_reduction (scop
, def
, in
, out
);
2877 tree lhs
= gimple_phi_result (stmt
);
2878 tree init
= initial_value_for_loop_phi (loop_phi
);
2879 gimple phi
= follow_inital_value_to_phi (init
, lhs
);
2881 VEC_safe_push (gimple
, heap
, *in
, loop_phi
);
2882 VEC_safe_push (gimple
, heap
, *out
, stmt
);
2889 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
2890 return detect_commutative_reduction_assign (stmt
, in
, out
);
2895 /* Translate the scalar reduction statement STMT to an array RED
2896 knowing that its recursive phi node is LOOP_PHI. */
2899 translate_scalar_reduction_to_array_for_stmt (scop_p scop
, tree red
,
2900 gimple stmt
, gimple loop_phi
)
2902 tree res
= gimple_phi_result (loop_phi
);
2903 gimple assign
= gimple_build_assign (res
, red
);
2904 gimple_stmt_iterator gsi
;
2906 insert_stmts (scop
, assign
, NULL
, gsi_after_labels (gimple_bb (loop_phi
)));
2908 assign
= gimple_build_assign (red
, gimple_assign_lhs (stmt
));
2909 gsi
= gsi_for_stmt (stmt
);
2911 insert_stmts (scop
, assign
, NULL
, gsi
);
2914 /* Removes the PHI node and resets all the debug stmts that are using
2918 remove_phi (gimple phi
)
2920 imm_use_iterator imm_iter
;
2922 use_operand_p use_p
;
2923 gimple_stmt_iterator gsi
;
2924 VEC (gimple
, heap
) *update
= VEC_alloc (gimple
, heap
, 3);
2928 def
= PHI_RESULT (phi
);
2929 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, def
)
2931 stmt
= USE_STMT (use_p
);
2933 if (is_gimple_debug (stmt
))
2935 gimple_debug_bind_reset_value (stmt
);
2936 VEC_safe_push (gimple
, heap
, update
, stmt
);
2940 FOR_EACH_VEC_ELT (gimple
, update
, i
, stmt
)
2943 VEC_free (gimple
, heap
, update
);
2945 gsi
= gsi_for_phi_node (phi
);
2946 remove_phi_node (&gsi
, false);
2949 /* Rewrite out of SSA the reduction described by the loop phi nodes
2950 IN, and the close phi nodes OUT. IN and OUT are structured by loop
2953 IN: stmt, loop_n, ..., loop_0
2954 OUT: stmt, close_n, ..., close_0
2956 the first element is the reduction statement, and the next elements
2957 are the loop and close phi nodes of each of the outer loops. */
2960 translate_scalar_reduction_to_array (scop_p scop
,
2961 VEC (gimple
, heap
) *in
,
2962 VEC (gimple
, heap
) *out
)
2966 tree red
= NULL_TREE
;
2968 FOR_EACH_VEC_ELT (gimple
, in
, i
, loop_phi
)
2970 gimple close_phi
= VEC_index (gimple
, out
, i
);
2974 gimple stmt
= loop_phi
;
2975 basic_block bb
= split_reduction_stmt (scop
, stmt
);
2976 poly_bb_p pbb
= pbb_from_bb (bb
);
2977 PBB_IS_REDUCTION (pbb
) = true;
2978 gcc_assert (close_phi
== loop_phi
);
2980 red
= create_zero_dim_array
2981 (gimple_assign_lhs (stmt
), "Commutative_Associative_Reduction");
2982 translate_scalar_reduction_to_array_for_stmt
2983 (scop
, red
, stmt
, VEC_index (gimple
, in
, 1));
2987 if (i
== VEC_length (gimple
, in
) - 1)
2989 insert_out_of_ssa_copy (scop
, gimple_phi_result (close_phi
), red
,
2991 insert_out_of_ssa_copy_on_edge
2992 (scop
, edge_initial_value_for_loop_phi (loop_phi
),
2993 red
, initial_value_for_loop_phi (loop_phi
));
2996 remove_phi (loop_phi
);
2997 remove_phi (close_phi
);
3001 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. Returns
3002 true when something has been changed. */
3005 rewrite_commutative_reductions_out_of_ssa_close_phi (scop_p scop
,
3009 VEC (gimple
, heap
) *in
= VEC_alloc (gimple
, heap
, 10);
3010 VEC (gimple
, heap
) *out
= VEC_alloc (gimple
, heap
, 10);
3012 detect_commutative_reduction (scop
, close_phi
, &in
, &out
);
3013 res
= VEC_length (gimple
, in
) > 0;
3015 translate_scalar_reduction_to_array (scop
, in
, out
);
3017 VEC_free (gimple
, heap
, in
);
3018 VEC_free (gimple
, heap
, out
);
3022 /* Rewrites all the commutative reductions from LOOP out of SSA.
3023 Returns true when something has been changed. */
3026 rewrite_commutative_reductions_out_of_ssa_loop (scop_p scop
,
3029 gimple_stmt_iterator gsi
;
3030 edge exit
= single_exit (loop
);
3032 bool changed
= false;
3037 for (gsi
= gsi_start_phis (exit
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3038 if ((res
= gimple_phi_result (gsi_stmt (gsi
)))
3039 && is_gimple_reg (res
)
3040 && !scev_analyzable_p (res
, SCOP_REGION (scop
)))
3041 changed
|= rewrite_commutative_reductions_out_of_ssa_close_phi
3042 (scop
, gsi_stmt (gsi
));
3047 /* Rewrites all the commutative reductions from SCOP out of SSA. */
3050 rewrite_commutative_reductions_out_of_ssa (scop_p scop
)
3054 bool changed
= false;
3055 sese region
= SCOP_REGION (scop
);
3057 FOR_EACH_LOOP (li
, loop
, 0)
3058 if (loop_in_sese_p (loop
, region
))
3059 changed
|= rewrite_commutative_reductions_out_of_ssa_loop (scop
, loop
);
3064 gsi_commit_edge_inserts ();
3065 update_ssa (TODO_update_ssa
);
3066 #ifdef ENABLE_CHECKING
3067 verify_loop_closed_ssa (true);
3072 /* Java does not initialize long_long_integer_type_node. */
3073 #define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
3075 /* Can all ivs be represented by a signed integer?
3076 As CLooG might generate negative values in its expressions, signed loop ivs
3077 are required in the backend. */
3080 scop_ivs_can_be_represented (scop_p scop
)
3084 gimple_stmt_iterator psi
;
3086 FOR_EACH_LOOP (li
, loop
, 0)
3088 if (!loop_in_sese_p (loop
, SCOP_REGION (scop
)))
3091 for (psi
= gsi_start_phis (loop
->header
);
3092 !gsi_end_p (psi
); gsi_next (&psi
))
3094 gimple phi
= gsi_stmt (psi
);
3095 tree res
= PHI_RESULT (phi
);
3096 tree type
= TREE_TYPE (res
);
3098 if (TYPE_UNSIGNED (type
)
3099 && TYPE_PRECISION (type
) >= TYPE_PRECISION (my_long_long
))
3109 /* Builds the polyhedral representation for a SESE region. */
3112 build_poly_scop (scop_p scop
)
3114 sese region
= SCOP_REGION (scop
);
3115 graphite_dim_t max_dim
;
3117 build_scop_bbs (scop
);
3119 /* FIXME: This restriction is needed to avoid a problem in CLooG.
3120 Once CLooG is fixed, remove this guard. Anyways, it makes no
3121 sense to optimize a scop containing only PBBs that do not belong
3123 if (nb_pbbs_in_loops (scop
) == 0)
3126 if (!scop_ivs_can_be_represented (scop
))
3129 build_sese_loop_nests (region
);
3130 build_sese_conditions (region
);
3131 find_scop_parameters (scop
);
3133 max_dim
= PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS
);
3134 if (scop_nb_params (scop
) > max_dim
)
3137 build_scop_iteration_domain (scop
);
3138 build_scop_context (scop
);
3139 add_conditions_to_constraints (scop
);
3141 /* Rewrite out of SSA only after having translated the
3142 representation to the polyhedral representation to avoid scev
3143 analysis failures. That means that these functions will insert
3144 new data references that they create in the right place. */
3145 if (flag_associative_math
)
3146 rewrite_commutative_reductions_out_of_ssa (scop
);
3147 rewrite_reductions_out_of_ssa (scop
);
3148 rewrite_cross_bb_scalar_deps_out_of_ssa (scop
);
3150 build_scop_drs (scop
);
3152 build_scop_scattering (scop
);
3154 /* This SCoP has been translated to the polyhedral
3156 POLY_SCOP_P (scop
) = true;