1 /* Conversion of SESE regions to Polyhedra.
2 Copyright (C) 2009 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
32 #include "tree-dump.h"
35 #include "tree-chrec.h"
36 #include "tree-data-ref.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-pass.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
46 #include "cloog/cloog.h"
48 #include "graphite-ppl.h"
50 #include "graphite-poly.h"
51 #include "graphite-scop-detection.h"
52 #include "graphite-clast-to-gimple.h"
53 #include "graphite-sese-to-poly.h"
55 /* Check if VAR is used in a phi node, that is no loop header. */
58 var_used_in_not_loop_header_phi_node (tree var)
60 imm_use_iterator imm_iter;
64 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, var)
66 basic_block bb = gimple_bb (stmt);
68 if (gimple_code (stmt) == GIMPLE_PHI
69 && bb->loop_father->header != bb)
76 /* Returns the index of the phi argument corresponding to the initial
80 loop_entry_phi_arg (gimple phi)
82 loop_p loop = gimple_bb (phi)->loop_father;
85 for (i = 0; i < gimple_phi_num_args (phi); i++)
86 if (!flow_bb_inside_loop_p (loop, gimple_phi_arg_edge (phi, i)->src))
93 /* Removes a simple copy phi node "RES = phi (INIT, RES)" at position
94 PSI by inserting on the loop ENTRY edge assignment "RES = INIT". */
97 remove_simple_copy_phi (gimple_stmt_iterator *psi)
99 gimple phi = gsi_stmt (*psi);
100 tree res = gimple_phi_result (phi);
101 size_t entry = loop_entry_phi_arg (phi);
102 tree init = gimple_phi_arg_def (phi, entry);
103 gimple stmt = gimple_build_assign (res, init);
104 edge e = gimple_phi_arg_edge (phi, entry);
106 remove_phi_node (psi, false);
107 gsi_insert_on_edge_immediate (e, stmt);
108 SSA_NAME_DEF_STMT (res) = stmt;
111 /* Removes an invariant phi node at position PSI by inserting on the
112 loop ENTRY edge the assignment RES = INIT. */
115 remove_invariant_phi (sese region, gimple_stmt_iterator *psi)
117 gimple phi = gsi_stmt (*psi);
118 loop_p loop = loop_containing_stmt (phi);
119 tree res = gimple_phi_result (phi);
120 tree scev = scalar_evolution_in_region (region, loop, res);
121 size_t entry = loop_entry_phi_arg (phi);
122 edge e = gimple_phi_arg_edge (phi, entry);
126 gimple_stmt_iterator gsi;
128 if (tree_contains_chrecs (scev, NULL))
129 scev = gimple_phi_arg_def (phi, entry);
131 var = force_gimple_operand (scev, &stmts, true, NULL_TREE);
132 stmt = gimple_build_assign (res, var);
133 remove_phi_node (psi, false);
136 stmts = gimple_seq_alloc ();
138 gsi = gsi_last (stmts);
139 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
140 gsi_insert_seq_on_edge (e, stmts);
141 gsi_commit_edge_inserts ();
142 SSA_NAME_DEF_STMT (res) = stmt;
145 /* Returns true when the phi node at PSI is of the form "a = phi (a, x)". */
148 simple_copy_phi_p (gimple phi)
152 if (gimple_phi_num_args (phi) != 2)
155 res = gimple_phi_result (phi);
156 return (res == gimple_phi_arg_def (phi, 0)
157 || res == gimple_phi_arg_def (phi, 1));
160 /* Returns true when the phi node at position PSI is a reduction phi
161 node in REGION. Otherwise moves the pointer PSI to the next phi to
165 reduction_phi_p (sese region, gimple_stmt_iterator *psi)
170 gimple phi = gsi_stmt (*psi);
171 tree res = gimple_phi_result (phi);
173 if (!is_gimple_reg (res))
179 loop = loop_containing_stmt (phi);
181 if (simple_copy_phi_p (phi))
183 /* FIXME: PRE introduces phi nodes like these, for an example,
184 see id-5.f in the fortran graphite testsuite:
186 # prephitmp.85_265 = PHI <prephitmp.85_258(33), prephitmp.85_265(18)>
188 remove_simple_copy_phi (psi);
192 /* Main induction variables with constant strides in LOOP are not
194 if (simple_iv (loop, loop, res, &iv, true))
196 if (integer_zerop (iv.step))
197 remove_invariant_phi (region, psi);
204 scev = scalar_evolution_in_region (region, loop, res);
205 if (chrec_contains_undetermined (scev))
208 if (evolution_function_is_invariant_p (scev, loop->num))
210 remove_invariant_phi (region, psi);
214 /* All the other cases are considered reductions. */
218 /* Returns true when BB will be represented in graphite. Return false
219 for the basic blocks that contain code eliminated in the code
220 generation pass: i.e. induction variables and exit conditions. */
223 graphite_stmt_p (sese region, basic_block bb,
224 VEC (data_reference_p, heap) *drs)
226 gimple_stmt_iterator gsi;
227 loop_p loop = bb->loop_father;
229 if (VEC_length (data_reference_p, drs) > 0)
232 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
234 gimple stmt = gsi_stmt (gsi);
236 switch (gimple_code (stmt))
239 /* Control flow expressions can be ignored, as they are
240 represented in the iteration domains and will be
241 regenerated by graphite. */
249 tree var = gimple_assign_lhs (stmt);
251 /* We need these bbs to be able to construct the phi nodes. */
252 if (var_used_in_not_loop_header_phi_node (var))
255 var = scalar_evolution_in_region (region, loop, var);
256 if (chrec_contains_undetermined (var))
270 /* Store the GRAPHITE representation of BB. */
273 new_gimple_bb (basic_block bb, VEC (data_reference_p, heap) *drs)
275 struct gimple_bb *gbb;
277 gbb = XNEW (struct gimple_bb);
280 GBB_DATA_REFS (gbb) = drs;
281 GBB_CONDITIONS (gbb) = NULL;
282 GBB_CONDITION_CASES (gbb) = NULL;
283 GBB_CLOOG_IV_TYPES (gbb) = NULL;
289 free_data_refs_aux (VEC (data_reference_p, heap) *datarefs)
292 struct data_reference *dr;
294 for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
305 free_gimple_bb (struct gimple_bb *gbb)
307 if (GBB_CLOOG_IV_TYPES (gbb))
308 htab_delete (GBB_CLOOG_IV_TYPES (gbb));
310 free_data_refs_aux (GBB_DATA_REFS (gbb));
311 free_data_refs (GBB_DATA_REFS (gbb));
313 VEC_free (gimple, heap, GBB_CONDITIONS (gbb));
314 VEC_free (gimple, heap, GBB_CONDITION_CASES (gbb));
315 GBB_BB (gbb)->aux = 0;
319 /* Deletes all gimple bbs in SCOP. */
322 remove_gbbs_in_scop (scop_p scop)
327 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
328 free_gimple_bb (PBB_BLACK_BOX (pbb));
331 /* Deletes all scops in SCOPS. */
334 free_scops (VEC (scop_p, heap) *scops)
339 for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
341 remove_gbbs_in_scop (scop);
342 free_sese (SCOP_REGION (scop));
346 VEC_free (scop_p, heap, scops);
349 /* Generates a polyhedral black box only if the bb contains interesting
353 try_generate_gimple_bb (scop_p scop, basic_block bb, sbitmap reductions)
355 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
356 loop_p nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
357 gimple_stmt_iterator gsi;
359 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
361 gimple stmt = gsi_stmt (gsi);
362 if (!is_gimple_debug (stmt))
363 graphite_find_data_references_in_stmt (nest, stmt, &drs);
366 if (!graphite_stmt_p (SCOP_REGION (scop), bb, drs))
367 free_data_refs (drs);
369 new_poly_bb (scop, new_gimple_bb (bb, drs), TEST_BIT (reductions,
373 /* Returns true if all predecessors of BB, that are not dominated by BB, are
374 marked in MAP. The predecessors dominated by BB are loop latches and will
375 be handled after BB. */
378 all_non_dominated_preds_marked_p (basic_block bb, sbitmap map)
383 FOR_EACH_EDGE (e, ei, bb->preds)
384 if (!TEST_BIT (map, e->src->index)
385 && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
391 /* Compare the depth of two basic_block's P1 and P2. */
394 compare_bb_depths (const void *p1, const void *p2)
396 const_basic_block const bb1 = *(const_basic_block const*)p1;
397 const_basic_block const bb2 = *(const_basic_block const*)p2;
398 int d1 = loop_depth (bb1->loop_father);
399 int d2 = loop_depth (bb2->loop_father);
410 /* Sort the basic blocks from DOM such that the first are the ones at
411 a deepest loop level. */
414 graphite_sort_dominated_info (VEC (basic_block, heap) *dom)
416 size_t len = VEC_length (basic_block, dom);
418 qsort (VEC_address (basic_block, dom), len, sizeof (basic_block),
422 /* Recursive helper function for build_scops_bbs. */
425 build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb, sbitmap reductions)
427 sese region = SCOP_REGION (scop);
428 VEC (basic_block, heap) *dom;
430 if (TEST_BIT (visited, bb->index)
431 || !bb_in_sese_p (bb, region))
434 try_generate_gimple_bb (scop, bb, reductions);
435 SET_BIT (visited, bb->index);
437 dom = get_dominated_by (CDI_DOMINATORS, bb);
442 graphite_sort_dominated_info (dom);
444 while (!VEC_empty (basic_block, dom))
449 for (i = 0; VEC_iterate (basic_block, dom, i, dom_bb); i++)
450 if (all_non_dominated_preds_marked_p (dom_bb, visited))
452 build_scop_bbs_1 (scop, visited, dom_bb, reductions);
453 VEC_unordered_remove (basic_block, dom, i);
458 VEC_free (basic_block, heap, dom);
461 /* Gather the basic blocks belonging to the SCOP. */
464 build_scop_bbs (scop_p scop, sbitmap reductions)
466 sbitmap visited = sbitmap_alloc (last_basic_block);
467 sese region = SCOP_REGION (scop);
469 sbitmap_zero (visited);
470 build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region), reductions);
471 sbitmap_free (visited);
474 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
475 We generate SCATTERING_DIMENSIONS scattering dimensions.
477 CLooG 0.15.0 and previous versions require, that all
478 scattering functions of one CloogProgram have the same number of
479 scattering dimensions, therefore we allow to specify it. This
480 should be removed in future versions of CLooG.
482 The scattering polyhedron consists of these dimensions: scattering,
483 loop_iterators, parameters.
487 | scattering_dimensions = 5
488 | used_scattering_dimensions = 3
496 | Scattering polyhedron:
498 | scattering: {s1, s2, s3, s4, s5}
499 | loop_iterators: {i}
500 | parameters: {p1, p2}
502 | s1 s2 s3 s4 s5 i p1 p2 1
503 | 1 0 0 0 0 0 0 0 -4 = 0
504 | 0 1 0 0 0 -1 0 0 0 = 0
505 | 0 0 1 0 0 0 0 0 -5 = 0 */
508 build_pbb_scattering_polyhedrons (ppl_Linear_Expression_t static_schedule,
509 poly_bb_p pbb, int scattering_dimensions)
512 scop_p scop = PBB_SCOP (pbb);
513 int nb_iterators = pbb_dim_iter_domain (pbb);
514 int used_scattering_dimensions = nb_iterators * 2 + 1;
515 int nb_params = scop_nb_params (scop);
517 ppl_dimension_type dim = scattering_dimensions + nb_iterators + nb_params;
520 gcc_assert (scattering_dimensions >= used_scattering_dimensions);
523 ppl_new_Coefficient (&c);
524 PBB_TRANSFORMED (pbb) = poly_scattering_new ();
525 ppl_new_C_Polyhedron_from_space_dimension
526 (&PBB_TRANSFORMED_SCATTERING (pbb), dim, 0);
528 PBB_NB_SCATTERING_TRANSFORM (pbb) = scattering_dimensions;
530 for (i = 0; i < scattering_dimensions; i++)
532 ppl_Constraint_t cstr;
533 ppl_Linear_Expression_t expr;
535 ppl_new_Linear_Expression_with_dimension (&expr, dim);
537 ppl_assign_Coefficient_from_mpz_t (c, v);
538 ppl_Linear_Expression_add_to_coefficient (expr, i, c);
540 /* Textual order inside this loop. */
543 ppl_Linear_Expression_coefficient (static_schedule, i / 2, c);
544 ppl_Coefficient_to_mpz_t (c, v);
546 ppl_assign_Coefficient_from_mpz_t (c, v);
547 ppl_Linear_Expression_add_to_inhomogeneous (expr, c);
550 /* Iterations of this loop. */
551 else /* if ((i % 2) == 1) */
553 int loop = (i - 1) / 2;
555 value_set_si (v, -1);
556 ppl_assign_Coefficient_from_mpz_t (c, v);
557 ppl_Linear_Expression_add_to_coefficient
558 (expr, scattering_dimensions + loop, c);
561 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
562 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb), cstr);
563 ppl_delete_Linear_Expression (expr);
564 ppl_delete_Constraint (cstr);
568 ppl_delete_Coefficient (c);
570 PBB_ORIGINAL (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
573 /* Build for BB the static schedule.
575 The static schedule is a Dewey numbering of the abstract syntax
576 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
578 The following example informally defines the static schedule:
597 Static schedules for A to F:
610 build_scop_scattering (scop_p scop)
614 gimple_bb_p previous_gbb = NULL;
615 ppl_Linear_Expression_t static_schedule;
620 ppl_new_Coefficient (&c);
621 ppl_new_Linear_Expression (&static_schedule);
623 /* We have to start schedules at 0 on the first component and
624 because we cannot compare_prefix_loops against a previous loop,
625 prefix will be equal to zero, and that index will be
626 incremented before copying. */
627 value_set_si (v, -1);
628 ppl_assign_Coefficient_from_mpz_t (c, v);
629 ppl_Linear_Expression_add_to_coefficient (static_schedule, 0, c);
631 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
633 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
634 ppl_Linear_Expression_t common;
636 int nb_scat_dims = pbb_dim_iter_domain (pbb) * 2 + 1;
639 prefix = nb_common_loops (SCOP_REGION (scop), previous_gbb, gbb);
644 ppl_new_Linear_Expression_with_dimension (&common, prefix + 1);
645 ppl_assign_Linear_Expression_from_Linear_Expression (common,
649 ppl_assign_Coefficient_from_mpz_t (c, v);
650 ppl_Linear_Expression_add_to_coefficient (common, prefix, c);
651 ppl_assign_Linear_Expression_from_Linear_Expression (static_schedule,
654 build_pbb_scattering_polyhedrons (common, pbb, nb_scat_dims);
656 ppl_delete_Linear_Expression (common);
660 ppl_delete_Coefficient (c);
661 ppl_delete_Linear_Expression (static_schedule);
664 /* Add the value K to the dimension D of the linear expression EXPR. */
667 add_value_to_dim (ppl_dimension_type d, ppl_Linear_Expression_t expr,
671 ppl_Coefficient_t coef;
673 ppl_new_Coefficient (&coef);
674 ppl_Linear_Expression_coefficient (expr, d, coef);
676 ppl_Coefficient_to_mpz_t (coef, val);
678 value_addto (val, val, k);
680 ppl_assign_Coefficient_from_mpz_t (coef, val);
681 ppl_Linear_Expression_add_to_coefficient (expr, d, coef);
683 ppl_delete_Coefficient (coef);
686 /* In the context of scop S, scan E, the right hand side of a scalar
687 evolution function in loop VAR, and translate it to a linear
691 scan_tree_for_params_right_scev (sese s, tree e, int var,
692 ppl_Linear_Expression_t expr)
696 loop_p loop = get_loop (var);
697 ppl_dimension_type l = sese_loop_depth (s, loop) - 1;
700 /* Scalar evolutions should happen in the sese region. */
701 gcc_assert (sese_loop_depth (s, loop) > 0);
703 /* We can not deal with parametric strides like:
709 gcc_assert (TREE_CODE (e) == INTEGER_CST);
712 value_set_si (val, int_cst_value (e));
713 add_value_to_dim (l, expr, val);
718 /* Scan the integer constant CST, and add it to the inhomogeneous part of the
719 linear expression EXPR. K is the multiplier of the constant. */
722 scan_tree_for_params_int (tree cst, ppl_Linear_Expression_t expr, Value k)
725 ppl_Coefficient_t coef;
726 int v = int_cst_value (cst);
729 value_set_si (val, 0);
731 /* Necessary to not get "-1 = 2^n - 1". */
733 value_sub_int (val, val, -v);
735 value_add_int (val, val, v);
737 value_multiply (val, val, k);
738 ppl_new_Coefficient (&coef);
739 ppl_assign_Coefficient_from_mpz_t (coef, val);
740 ppl_Linear_Expression_add_to_inhomogeneous (expr, coef);
742 ppl_delete_Coefficient (coef);
745 /* Saves in NV at index I a new name for variable P. */
748 save_var_name (char **nv, int i, tree p)
750 const char *name = get_name (SSA_NAME_VAR (p));
754 int len = strlen (name) + 16;
755 nv[i] = XNEWVEC (char, len);
756 snprintf (nv[i], len, "%s_%d", name, SSA_NAME_VERSION (p));
760 nv[i] = XNEWVEC (char, 16);
761 snprintf (nv[i], 2 + 16, "T_%d", SSA_NAME_VERSION (p));
765 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
766 Otherwise returns -1. */
769 parameter_index_in_region_1 (tree name, sese region)
774 gcc_assert (TREE_CODE (name) == SSA_NAME);
776 for (i = 0; VEC_iterate (tree, SESE_PARAMS (region), i, p); i++)
783 /* When the parameter NAME is in REGION, returns its index in
784 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
785 and returns the index of NAME. */
788 parameter_index_in_region (tree name, sese region)
792 gcc_assert (TREE_CODE (name) == SSA_NAME);
794 i = parameter_index_in_region_1 (name, region);
798 gcc_assert (SESE_ADD_PARAMS (region));
800 i = VEC_length (tree, SESE_PARAMS (region));
801 save_var_name (SESE_PARAMS_NAMES (region), i, name);
802 save_clast_name_index (SESE_PARAMS_INDEX (region),
803 SESE_PARAMS_NAMES (region)[i], i);
804 VEC_safe_push (tree, heap, SESE_PARAMS (region), name);
808 /* In the context of sese S, scan the expression E and translate it to
809 a linear expression C. When parsing a symbolic multiplication, K
810 represents the constant multiplier of an expression containing
814 scan_tree_for_params (sese s, tree e, ppl_Linear_Expression_t c,
817 if (e == chrec_dont_know)
820 switch (TREE_CODE (e))
822 case POLYNOMIAL_CHREC:
823 scan_tree_for_params_right_scev (s, CHREC_RIGHT (e),
824 CHREC_VARIABLE (e), c);
825 scan_tree_for_params (s, CHREC_LEFT (e), c, k);
829 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
834 gcc_assert (host_integerp (TREE_OPERAND (e, 1), 0));
836 value_set_si (val, int_cst_value (TREE_OPERAND (e, 1)));
837 value_multiply (val, val, k);
838 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, val);
842 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
849 gcc_assert (host_integerp (TREE_OPERAND (e, 0), 0));
851 value_set_si (val, int_cst_value (TREE_OPERAND (e, 0)));
852 value_multiply (val, val, k);
853 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, val);
857 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, k);
862 case POINTER_PLUS_EXPR:
863 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
864 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, k);
869 ppl_Linear_Expression_t tmp_expr = NULL;
873 ppl_dimension_type dim;
874 ppl_Linear_Expression_space_dimension (c, &dim);
875 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
878 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
879 scan_tree_for_params (s, TREE_OPERAND (e, 1), tmp_expr, k);
883 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
885 ppl_delete_Linear_Expression (tmp_expr);
893 ppl_Linear_Expression_t tmp_expr = NULL;
897 ppl_dimension_type dim;
898 ppl_Linear_Expression_space_dimension (c, &dim);
899 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
902 scan_tree_for_params (s, TREE_OPERAND (e, 0), tmp_expr, k);
906 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
908 ppl_delete_Linear_Expression (tmp_expr);
916 ppl_Linear_Expression_t tmp_expr = NULL;
920 ppl_dimension_type dim;
921 ppl_Linear_Expression_space_dimension (c, &dim);
922 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
925 scan_tree_for_params (s, TREE_OPERAND (e, 0), tmp_expr, k);
929 ppl_Coefficient_t coef;
932 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
934 ppl_delete_Linear_Expression (tmp_expr);
935 value_init (minus_one);
936 value_set_si (minus_one, -1);
937 ppl_new_Coefficient_from_mpz_t (&coef, minus_one);
938 ppl_Linear_Expression_add_to_inhomogeneous (c, coef);
939 value_clear (minus_one);
940 ppl_delete_Coefficient (coef);
948 ppl_dimension_type p = parameter_index_in_region (e, s);
952 ppl_dimension_type dim;
953 ppl_Linear_Expression_space_dimension (c, &dim);
954 p += dim - sese_nb_params (s);
955 add_value_to_dim (p, c, k);
962 scan_tree_for_params_int (e, c, k);
966 case NON_LVALUE_EXPR:
967 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
976 /* Find parameters with respect to REGION in BB. We are looking in memory
977 access functions, conditions and loop bounds. */
980 find_params_in_bb (sese region, gimple_bb_p gbb)
986 loop_p loop = GBB_BB (gbb)->loop_father;
990 value_set_si (one, 1);
992 /* Find parameters in the access functions of data references. */
993 for (i = 0; VEC_iterate (data_reference_p, GBB_DATA_REFS (gbb), i, dr); i++)
994 for (j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
995 scan_tree_for_params (region, DR_ACCESS_FN (dr, j), NULL, one);
997 /* Find parameters in conditional statements. */
998 for (i = 0; VEC_iterate (gimple, GBB_CONDITIONS (gbb), i, stmt); i++)
1000 tree lhs = scalar_evolution_in_region (region, loop,
1001 gimple_cond_lhs (stmt));
1002 tree rhs = scalar_evolution_in_region (region, loop,
1003 gimple_cond_rhs (stmt));
1005 scan_tree_for_params (region, lhs, NULL, one);
1006 scan_tree_for_params (region, rhs, NULL, one);
1012 /* Record the parameters used in the SCOP. A variable is a parameter
1013 in a scop if it does not vary during the execution of that scop. */
1016 find_scop_parameters (scop_p scop)
1020 sese region = SCOP_REGION (scop);
1025 value_set_si (one, 1);
1027 /* Find the parameters used in the loop bounds. */
1028 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop); i++)
1030 tree nb_iters = number_of_latch_executions (loop);
1032 if (!chrec_contains_symbols (nb_iters))
1035 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
1036 scan_tree_for_params (region, nb_iters, NULL, one);
1041 /* Find the parameters used in data accesses. */
1042 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1043 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
1045 scop_set_nb_params (scop, sese_nb_params (region));
1046 SESE_ADD_PARAMS (region) = false;
1048 ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension
1049 (&SCOP_CONTEXT (scop), scop_nb_params (scop), 0);
1052 /* Returns a gimple_bb from BB. */
1054 static inline gimple_bb_p
1055 gbb_from_bb (basic_block bb)
1057 return (gimple_bb_p) bb->aux;
1060 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
1061 the constraints for the surrounding loops. */
1064 build_loop_iteration_domains (scop_p scop, struct loop *loop,
1065 ppl_Polyhedron_t outer_ph, int nb)
1068 ppl_Polyhedron_t ph;
1069 tree nb_iters = number_of_latch_executions (loop);
1070 ppl_dimension_type dim = nb + 1 + scop_nb_params (scop);
1071 sese region = SCOP_REGION (scop);
1074 ppl_const_Constraint_System_t pcs;
1075 ppl_dimension_type *map
1076 = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim);
1078 ppl_new_C_Polyhedron_from_space_dimension (&ph, dim, 0);
1079 ppl_Polyhedron_get_constraints (outer_ph, &pcs);
1080 ppl_Polyhedron_add_constraints (ph, pcs);
1082 for (i = 0; i < (int) nb; i++)
1084 for (i = (int) nb; i < (int) dim - 1; i++)
1088 ppl_Polyhedron_map_space_dimensions (ph, map, dim);
1094 ppl_Constraint_t lb;
1095 ppl_Linear_Expression_t lb_expr;
1097 ppl_new_Linear_Expression_with_dimension (&lb_expr, dim);
1098 ppl_set_coef (lb_expr, nb, 1);
1099 ppl_new_Constraint (&lb, lb_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1100 ppl_delete_Linear_Expression (lb_expr);
1101 ppl_Polyhedron_add_constraint (ph, lb);
1102 ppl_delete_Constraint (lb);
1105 if (TREE_CODE (nb_iters) == INTEGER_CST)
1107 ppl_Constraint_t ub;
1108 ppl_Linear_Expression_t ub_expr;
1110 ppl_new_Linear_Expression_with_dimension (&ub_expr, dim);
1112 /* loop_i <= cst_nb_iters */
1113 ppl_set_coef (ub_expr, nb, -1);
1114 ppl_set_inhomogeneous_tree (ub_expr, nb_iters);
1115 ppl_new_Constraint (&ub, ub_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1116 ppl_Polyhedron_add_constraint (ph, ub);
1117 ppl_delete_Linear_Expression (ub_expr);
1118 ppl_delete_Constraint (ub);
1120 else if (!chrec_contains_undetermined (nb_iters))
1123 ppl_Constraint_t ub;
1124 ppl_Linear_Expression_t ub_expr;
1128 value_set_si (one, 1);
1129 ppl_new_Linear_Expression_with_dimension (&ub_expr, dim);
1130 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
1131 scan_tree_for_params (SCOP_REGION (scop), nb_iters, ub_expr, one);
1134 /* N <= estimated_nb_iters
1136 FIXME: This is a workaround that should go away once we will
1137 have the PIP algorithm. */
1138 if (estimated_loop_iterations (loop, true, &nit))
1141 ppl_Linear_Expression_t nb_iters_le;
1142 ppl_Polyhedron_t pol;
1143 graphite_dim_t p, n = scop_nb_params (scop);
1144 ppl_Coefficient_t coef;
1146 ppl_new_C_Polyhedron_from_space_dimension (&pol, dim, 0);
1147 ppl_new_Linear_Expression_from_Linear_Expression (&nb_iters_le,
1151 mpz_set_double_int (val, nit, false);
1152 ppl_new_Coefficient_from_mpz_t (&coef, val);
1153 ppl_Linear_Expression_add_to_inhomogeneous (nb_iters_le, coef);
1154 ppl_delete_Coefficient (coef);
1155 ppl_new_Constraint (&ub, nb_iters_le,
1156 PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL);
1157 ppl_Polyhedron_add_constraint (pol, ub);
1159 for (p = 0; p < n; p++)
1161 ppl_Linear_Expression_t le;
1163 ppl_new_Linear_Expression_with_dimension (&le, dim);
1164 ppl_set_coef (le, nb + 1 + p, -1);
1166 value_set_si (val, -1);
1167 ppl_min_for_le_polyhedron (pol, le, val);
1168 if (!value_mone_p (val))
1170 ppl_Linear_Expression_t parm_bound;
1171 ppl_Constraint_t cstr;
1173 ppl_new_Linear_Expression_with_dimension (&parm_bound, n);
1174 ppl_set_coef (parm_bound, p, -1);
1175 ppl_set_inhomogeneous_gmp (parm_bound, val);
1176 ppl_new_Constraint (&cstr, parm_bound,
1177 PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1178 ppl_Pointset_Powerset_C_Polyhedron_add_constraint
1179 (SCOP_CONTEXT (scop), cstr);
1181 ppl_delete_Constraint (cstr);
1182 ppl_delete_Linear_Expression (parm_bound);
1185 ppl_delete_Linear_Expression (le);
1188 ppl_delete_Polyhedron (pol);
1189 ppl_delete_Linear_Expression (nb_iters_le);
1190 ppl_delete_Constraint (ub);
1194 /* loop_i <= expr_nb_iters */
1195 ppl_set_coef (ub_expr, nb, -1);
1196 ppl_new_Constraint (&ub, ub_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1197 ppl_Polyhedron_add_constraint (ph, ub);
1198 ppl_delete_Linear_Expression (ub_expr);
1199 ppl_delete_Constraint (ub);
1204 if (loop->inner && loop_in_sese_p (loop->inner, region))
1205 build_loop_iteration_domains (scop, loop->inner, ph, nb + 1);
1209 && loop_in_sese_p (loop->next, region))
1210 build_loop_iteration_domains (scop, loop->next, outer_ph, nb);
1212 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1213 ((ppl_Pointset_Powerset_C_Polyhedron_t *) &loop->aux, ph);
1215 ppl_delete_Polyhedron (ph);
1218 /* Returns a linear expression for tree T evaluated in PBB. */
1220 static ppl_Linear_Expression_t
1221 create_linear_expr_from_tree (poly_bb_p pbb, tree t)
1224 ppl_Linear_Expression_t res;
1225 ppl_dimension_type dim;
1226 sese region = SCOP_REGION (PBB_SCOP (pbb));
1227 loop_p loop = pbb_loop (pbb);
1229 dim = pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
1230 ppl_new_Linear_Expression_with_dimension (&res, dim);
1232 t = scalar_evolution_in_region (region, loop, t);
1233 gcc_assert (!automatically_generated_chrec_p (t));
1236 value_set_si (one, 1);
1237 scan_tree_for_params (region, t, res, one);
1243 /* Returns the ppl constraint type from the gimple tree code CODE. */
1245 static enum ppl_enum_Constraint_Type
1246 ppl_constraint_type_from_tree_code (enum tree_code code)
1250 /* We do not support LT and GT to be able to work with C_Polyhedron.
1251 As we work on integer polyhedron "a < b" can be expressed by
1258 return PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL;
1261 return PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL;
1264 return PPL_CONSTRAINT_TYPE_EQUAL;
1271 /* Add conditional statement STMT to PS. It is evaluated in PBB and
1272 CODE is used as the comparison operator. This allows us to invert the
1273 condition or to handle inequalities. */
1276 add_condition_to_domain (ppl_Pointset_Powerset_C_Polyhedron_t ps, gimple stmt,
1277 poly_bb_p pbb, enum tree_code code)
1280 ppl_Coefficient_t c;
1281 ppl_Linear_Expression_t left, right;
1282 ppl_Constraint_t cstr;
1283 enum ppl_enum_Constraint_Type type;
1285 left = create_linear_expr_from_tree (pbb, gimple_cond_lhs (stmt));
1286 right = create_linear_expr_from_tree (pbb, gimple_cond_rhs (stmt));
1288 /* If we have < or > expressions convert them to <= or >= by adding 1 to
1289 the left or the right side of the expression. */
1290 if (code == LT_EXPR)
1293 value_set_si (v, 1);
1294 ppl_new_Coefficient (&c);
1295 ppl_assign_Coefficient_from_mpz_t (c, v);
1296 ppl_Linear_Expression_add_to_inhomogeneous (left, c);
1297 ppl_delete_Coefficient (c);
1302 else if (code == GT_EXPR)
1305 value_set_si (v, 1);
1306 ppl_new_Coefficient (&c);
1307 ppl_assign_Coefficient_from_mpz_t (c, v);
1308 ppl_Linear_Expression_add_to_inhomogeneous (right, c);
1309 ppl_delete_Coefficient (c);
1315 type = ppl_constraint_type_from_tree_code (code);
1317 ppl_subtract_Linear_Expression_from_Linear_Expression (left, right);
1319 ppl_new_Constraint (&cstr, left, type);
1320 ppl_Pointset_Powerset_C_Polyhedron_add_constraint (ps, cstr);
1322 ppl_delete_Constraint (cstr);
1323 ppl_delete_Linear_Expression (left);
1324 ppl_delete_Linear_Expression (right);
1327 /* Add conditional statement STMT to pbb. CODE is used as the comparision
1328 operator. This allows us to invert the condition or to handle
1332 add_condition_to_pbb (poly_bb_p pbb, gimple stmt, enum tree_code code)
1334 if (code == NE_EXPR)
1336 ppl_Pointset_Powerset_C_Polyhedron_t left = PBB_DOMAIN (pbb);
1337 ppl_Pointset_Powerset_C_Polyhedron_t right;
1338 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1340 add_condition_to_domain (left, stmt, pbb, LT_EXPR);
1341 add_condition_to_domain (right, stmt, pbb, GT_EXPR);
1342 ppl_Pointset_Powerset_C_Polyhedron_upper_bound_assign (left,
1344 ppl_delete_Pointset_Powerset_C_Polyhedron (right);
1347 add_condition_to_domain (PBB_DOMAIN (pbb), stmt, pbb, code);
1350 /* Add conditions to the domain of PBB. */
1353 add_conditions_to_domain (poly_bb_p pbb)
1357 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1358 VEC (gimple, heap) *conditions = GBB_CONDITIONS (gbb);
1360 if (VEC_empty (gimple, conditions))
1363 for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
1364 switch (gimple_code (stmt))
1368 enum tree_code code = gimple_cond_code (stmt);
1370 /* The conditions for ELSE-branches are inverted. */
1371 if (VEC_index (gimple, gbb->condition_cases, i) == NULL)
1372 code = invert_tree_comparison (code, false);
1374 add_condition_to_pbb (pbb, stmt, code);
1379 /* Switch statements are not supported right now - fall throught. */
1387 /* Structure used to pass data to dom_walk. */
1391 VEC (gimple, heap) **conditions, **cases;
1395 /* Returns non NULL when BB has a single predecessor and the last
1396 statement of that predecessor is a COND_EXPR. */
1399 single_pred_cond (basic_block bb)
1401 if (single_pred_p (bb))
1403 edge e = single_pred_edge (bb);
1404 basic_block pred = e->src;
1405 gimple stmt = last_stmt (pred);
1407 if (stmt && gimple_code (stmt) == GIMPLE_COND)
1413 /* Call-back for dom_walk executed before visiting the dominated
1417 build_sese_conditions_before (struct dom_walk_data *dw_data,
1420 struct bsc *data = (struct bsc *) dw_data->global_data;
1421 VEC (gimple, heap) **conditions = data->conditions;
1422 VEC (gimple, heap) **cases = data->cases;
1423 gimple_bb_p gbb = gbb_from_bb (bb);
1424 gimple stmt = single_pred_cond (bb);
1426 if (!bb_in_sese_p (bb, data->region))
1431 edge e = single_pred_edge (bb);
1433 VEC_safe_push (gimple, heap, *conditions, stmt);
1435 if (e->flags & EDGE_TRUE_VALUE)
1436 VEC_safe_push (gimple, heap, *cases, stmt);
1438 VEC_safe_push (gimple, heap, *cases, NULL);
1443 GBB_CONDITIONS (gbb) = VEC_copy (gimple, heap, *conditions);
1444 GBB_CONDITION_CASES (gbb) = VEC_copy (gimple, heap, *cases);
1448 /* Call-back for dom_walk executed after visiting the dominated
1452 build_sese_conditions_after (struct dom_walk_data *dw_data,
1455 struct bsc *data = (struct bsc *) dw_data->global_data;
1456 VEC (gimple, heap) **conditions = data->conditions;
1457 VEC (gimple, heap) **cases = data->cases;
1459 if (!bb_in_sese_p (bb, data->region))
1462 if (single_pred_cond (bb))
1464 VEC_pop (gimple, *conditions);
1465 VEC_pop (gimple, *cases);
1469 /* Record all conditions in REGION. */
1472 build_sese_conditions (sese region)
1474 struct dom_walk_data walk_data;
1475 VEC (gimple, heap) *conditions = VEC_alloc (gimple, heap, 3);
1476 VEC (gimple, heap) *cases = VEC_alloc (gimple, heap, 3);
1479 data.conditions = &conditions;
1480 data.cases = &cases;
1481 data.region = region;
1483 walk_data.dom_direction = CDI_DOMINATORS;
1484 walk_data.initialize_block_local_data = NULL;
1485 walk_data.before_dom_children = build_sese_conditions_before;
1486 walk_data.after_dom_children = build_sese_conditions_after;
1487 walk_data.global_data = &data;
1488 walk_data.block_local_data_size = 0;
1490 init_walk_dominator_tree (&walk_data);
1491 walk_dominator_tree (&walk_data, SESE_ENTRY_BB (region));
1492 fini_walk_dominator_tree (&walk_data);
1494 VEC_free (gimple, heap, conditions);
1495 VEC_free (gimple, heap, cases);
1498 /* Traverses all the GBBs of the SCOP and add their constraints to the
1499 iteration domains. */
1502 add_conditions_to_constraints (scop_p scop)
1507 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1508 add_conditions_to_domain (pbb);
1511 /* Add constraints on the possible values of parameter P from the type
1515 add_param_constraints (scop_p scop, ppl_Polyhedron_t context, graphite_dim_t p)
1517 ppl_Constraint_t cstr;
1518 ppl_Linear_Expression_t le;
1519 tree parameter = VEC_index (tree, SESE_PARAMS (SCOP_REGION (scop)), p);
1520 tree type = TREE_TYPE (parameter);
1523 /* Disabled until we fix CPU2006. */
1526 if (!INTEGRAL_TYPE_P (type))
1529 lb = TYPE_MIN_VALUE (type);
1530 ub = TYPE_MAX_VALUE (type);
1534 ppl_new_Linear_Expression_with_dimension (&le, scop_nb_params (scop));
1535 ppl_set_coef (le, p, -1);
1536 ppl_set_inhomogeneous_tree (le, lb);
1537 ppl_new_Constraint (&cstr, le, PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL);
1538 ppl_Polyhedron_add_constraint (context, cstr);
1539 ppl_delete_Linear_Expression (le);
1540 ppl_delete_Constraint (cstr);
1545 ppl_new_Linear_Expression_with_dimension (&le, scop_nb_params (scop));
1546 ppl_set_coef (le, p, -1);
1547 ppl_set_inhomogeneous_tree (le, ub);
1548 ppl_new_Constraint (&cstr, le, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1549 ppl_Polyhedron_add_constraint (context, cstr);
1550 ppl_delete_Linear_Expression (le);
1551 ppl_delete_Constraint (cstr);
1555 /* Build the context of the SCOP. The context usually contains extra
1556 constraints that are added to the iteration domains that constrain
1560 build_scop_context (scop_p scop)
1562 ppl_Polyhedron_t context;
1563 ppl_Pointset_Powerset_C_Polyhedron_t ps;
1564 graphite_dim_t p, n = scop_nb_params (scop);
1566 ppl_new_C_Polyhedron_from_space_dimension (&context, n, 0);
1568 for (p = 0; p < n; p++)
1569 add_param_constraints (scop, context, p);
1571 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1573 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1574 (SCOP_CONTEXT (scop), ps);
1576 ppl_delete_Pointset_Powerset_C_Polyhedron (ps);
1577 ppl_delete_Polyhedron (context);
1580 /* Build the iteration domains: the loops belonging to the current
1581 SCOP, and that vary for the execution of the current basic block.
1582 Returns false if there is no loop in SCOP. */
1585 build_scop_iteration_domain (scop_p scop)
1588 sese region = SCOP_REGION (scop);
1590 ppl_Polyhedron_t ph;
1593 ppl_new_C_Polyhedron_from_space_dimension (&ph, scop_nb_params (scop), 0);
1595 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop); i++)
1596 if (!loop_in_sese_p (loop_outer (loop), region))
1597 build_loop_iteration_domains (scop, loop, ph, 0);
1599 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1600 if (gbb_loop (PBB_BLACK_BOX (pbb))->aux)
1601 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1602 (&PBB_DOMAIN (pbb), (ppl_const_Pointset_Powerset_C_Polyhedron_t)
1603 gbb_loop (PBB_BLACK_BOX (pbb))->aux);
1605 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1606 (&PBB_DOMAIN (pbb), ph);
1608 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop); i++)
1611 ppl_delete_Pointset_Powerset_C_Polyhedron
1612 ((ppl_Pointset_Powerset_C_Polyhedron_t) loop->aux);
1616 ppl_delete_Polyhedron (ph);
1619 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1620 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1621 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1625 pdr_add_alias_set (ppl_Polyhedron_t accesses, data_reference_p dr,
1626 ppl_dimension_type accessp_nb_dims,
1627 ppl_dimension_type dom_nb_dims)
1629 ppl_Linear_Expression_t alias;
1630 ppl_Constraint_t cstr;
1631 int alias_set_num = 0;
1633 if (dr->aux != NULL)
1634 alias_set_num = ((int *)(dr->aux))[ALIAS_SET_INDEX];
1636 ppl_new_Linear_Expression_with_dimension (&alias, accessp_nb_dims);
1638 ppl_set_coef (alias, dom_nb_dims, 1);
1639 ppl_set_inhomogeneous (alias, -alias_set_num);
1640 ppl_new_Constraint (&cstr, alias, PPL_CONSTRAINT_TYPE_EQUAL);
1641 ppl_Polyhedron_add_constraint (accesses, cstr);
1643 ppl_delete_Linear_Expression (alias);
1644 ppl_delete_Constraint (cstr);
1647 /* Add to ACCESSES polyhedron equalities defining the access functions
1648 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1649 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1650 PBB is the poly_bb_p that contains the data reference DR. */
1653 pdr_add_memory_accesses (ppl_Polyhedron_t accesses, data_reference_p dr,
1654 ppl_dimension_type accessp_nb_dims,
1655 ppl_dimension_type dom_nb_dims,
1658 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1660 scop_p scop = PBB_SCOP (pbb);
1661 sese region = SCOP_REGION (scop);
1665 for (i = 0; i < nb_subscripts; i++)
1667 ppl_Linear_Expression_t fn, access;
1668 ppl_Constraint_t cstr;
1669 ppl_dimension_type subscript = dom_nb_dims + 1 + i;
1670 tree afn = DR_ACCESS_FN (dr, nb_subscripts - 1 - i);
1672 ppl_new_Linear_Expression_with_dimension (&fn, dom_nb_dims);
1673 ppl_new_Linear_Expression_with_dimension (&access, accessp_nb_dims);
1675 value_set_si (v, 1);
1676 scan_tree_for_params (region, afn, fn, v);
1677 ppl_assign_Linear_Expression_from_Linear_Expression (access, fn);
1679 ppl_set_coef (access, subscript, -1);
1680 ppl_new_Constraint (&cstr, access, PPL_CONSTRAINT_TYPE_EQUAL);
1681 ppl_Polyhedron_add_constraint (accesses, cstr);
1683 ppl_delete_Linear_Expression (fn);
1684 ppl_delete_Linear_Expression (access);
1685 ppl_delete_Constraint (cstr);
1691 /* Add constrains representing the size of the accessed data to the
1692 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1693 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1697 pdr_add_data_dimensions (ppl_Polyhedron_t accesses, data_reference_p dr,
1698 ppl_dimension_type accessp_nb_dims,
1699 ppl_dimension_type dom_nb_dims)
1701 tree ref = DR_REF (dr);
1702 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1704 for (i = nb_subscripts - 1; i >= 0; i--, ref = TREE_OPERAND (ref, 0))
1706 ppl_Linear_Expression_t expr;
1707 ppl_Constraint_t cstr;
1708 ppl_dimension_type subscript = dom_nb_dims + 1 + i;
1711 if (TREE_CODE (ref) != ARRAY_REF)
1714 low = array_ref_low_bound (ref);
1716 /* subscript - low >= 0 */
1717 if (host_integerp (low, 0))
1719 ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
1720 ppl_set_coef (expr, subscript, 1);
1722 ppl_set_inhomogeneous (expr, -int_cst_value (low));
1724 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1725 ppl_Polyhedron_add_constraint (accesses, cstr);
1726 ppl_delete_Linear_Expression (expr);
1727 ppl_delete_Constraint (cstr);
1730 high = array_ref_up_bound (ref);
1732 /* high - subscript >= 0
1733 XXX: 1-element arrays at end of structures may extend over their
1735 if (high && host_integerp (high, 0))
1737 ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
1738 ppl_set_coef (expr, subscript, -1);
1740 ppl_set_inhomogeneous (expr, int_cst_value (high));
1742 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1743 ppl_Polyhedron_add_constraint (accesses, cstr);
1744 ppl_delete_Linear_Expression (expr);
1745 ppl_delete_Constraint (cstr);
1750 /* Build data accesses for DR in PBB. */
1753 build_poly_dr (data_reference_p dr, poly_bb_p pbb)
1755 ppl_Polyhedron_t accesses;
1756 ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps;
1757 ppl_dimension_type dom_nb_dims;
1758 ppl_dimension_type accessp_nb_dims;
1759 int dr_base_object_set;
1761 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb),
1763 accessp_nb_dims = dom_nb_dims + 1 + DR_NUM_DIMENSIONS (dr);
1765 ppl_new_C_Polyhedron_from_space_dimension (&accesses, accessp_nb_dims, 0);
1767 pdr_add_alias_set (accesses, dr, accessp_nb_dims, dom_nb_dims);
1768 pdr_add_memory_accesses (accesses, dr, accessp_nb_dims, dom_nb_dims, pbb);
1769 pdr_add_data_dimensions (accesses, dr, accessp_nb_dims, dom_nb_dims);
1771 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps,
1773 ppl_delete_Polyhedron (accesses);
1775 dr_base_object_set = ((int *)(dr->aux))[BASE_OBJECT_SET_INDEX];
1777 new_poly_dr (pbb, dr_base_object_set, accesses_ps, DR_IS_READ (dr) ? PDR_READ : PDR_WRITE,
1778 dr, DR_NUM_DIMENSIONS (dr));
1781 /* Write to FILE the alias graph of data references in DIMACS format. */
1784 write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
1785 VEC (data_reference_p, heap) *drs)
1787 int num_vertex = VEC_length (data_reference_p, drs);
1789 data_reference_p dr1, dr2;
1792 if (num_vertex == 0)
1795 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1796 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1797 if (dr_may_alias_p (dr1, dr2))
1800 fprintf (file, "$\n");
1803 fprintf (file, "c %s\n", comment);
1805 fprintf (file, "p edge %d %d\n", num_vertex, edge_num);
1807 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1808 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1809 if (dr_may_alias_p (dr1, dr2))
1810 fprintf (file, "e %d %d\n", i + 1, j + 1);
1815 /* Write to FILE the alias graph of data references in DOT format. */
1818 write_alias_graph_to_ascii_dot (FILE *file, char *comment,
1819 VEC (data_reference_p, heap) *drs)
1821 int num_vertex = VEC_length (data_reference_p, drs);
1822 data_reference_p dr1, dr2;
1825 if (num_vertex == 0)
1828 fprintf (file, "$\n");
1831 fprintf (file, "c %s\n", comment);
1833 /* First print all the vertices. */
1834 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1835 fprintf (file, "n%d;\n", i);
1837 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1838 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1839 if (dr_may_alias_p (dr1, dr2))
1840 fprintf (file, "n%d n%d\n", i, j);
1845 /* Write to FILE the alias graph of data references in ECC format. */
1848 write_alias_graph_to_ascii_ecc (FILE *file, char *comment,
1849 VEC (data_reference_p, heap) *drs)
1851 int num_vertex = VEC_length (data_reference_p, drs);
1852 data_reference_p dr1, dr2;
1855 if (num_vertex == 0)
1858 fprintf (file, "$\n");
1861 fprintf (file, "c %s\n", comment);
1863 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1864 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1865 if (dr_may_alias_p (dr1, dr2))
1866 fprintf (file, "%d %d\n", i, j);
1872 /* Uses DFS component number as representative of alias-sets. Also tests for
1873 optimality by verifying if every connected component is a clique. Returns
1874 true (1) if the above test is true, and false (0) otherwise. */
1877 partition_drs_to_sets (VEC (data_reference_p, heap) *drs, int choice,
1878 bool (* edge_exist_p) (const struct data_reference *,
1879 const struct data_reference *))
1882 int num_vertices = VEC_length (data_reference_p, drs);
1883 struct graph *g = new_graph (num_vertices);
1884 data_reference_p dr1, dr2;
1886 int num_connected_components;
1887 int v_indx1, v_indx2, num_vertices_in_component;
1890 struct graph_edge *e;
1891 int this_component_is_clique, all_components_are_cliques;
1893 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1894 for (j = i+1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1895 if (edge_exist_p (dr1, dr2))
1901 all_vertices = XNEWVEC (int, num_vertices);
1902 vertices = XNEWVEC (int, num_vertices);
1903 for (i = 0; i < num_vertices; i++)
1904 all_vertices[i] = i;
1906 num_connected_components = graphds_dfs (g, all_vertices, num_vertices, NULL, true, NULL);
1908 /* Verify if the DFS numbering results in optimal solution. */
1909 for (i = 0; i < num_connected_components; i++)
1911 num_vertices_in_component = 0;
1912 /* Get all vertices whose DFS component number is the same as i. */
1913 for (j = 0; j < num_vertices; j++)
1914 if (g->vertices[j].component == i)
1915 vertices[num_vertices_in_component++] = j;
1917 /* Now test if the vertices in 'vertices' form a clique, by testing
1918 for edges among each pair. */
1919 this_component_is_clique = 1;
1920 for (v_indx1 = 0; v_indx1 < num_vertices_in_component; v_indx1++)
1922 for (v_indx2 = v_indx1+1; v_indx2 < num_vertices_in_component; v_indx2++)
1924 /* Check if the two vertices are connected by iterating
1925 through all the edges which have one of these are source. */
1926 e = g->vertices[vertices[v_indx2]].pred;
1929 if (e->src == vertices[v_indx1])
1935 this_component_is_clique = 0;
1939 if (!this_component_is_clique)
1940 all_components_are_cliques = 0;
1944 for (i = 0; i < g->n_vertices; i++)
1946 data_reference_p dr = VEC_index (data_reference_p, drs, i);
1948 dr->aux = XNEWVEC (int, 2);
1949 ((int *)(dr->aux))[choice] = g->vertices[i].component + 1;
1952 free (all_vertices);
1955 return all_components_are_cliques;
1959 dr_same_base_object_p (const struct data_reference *dr1,
1960 const struct data_reference *dr2)
1962 return operand_equal_p (DR_BASE_OBJECT (dr1), DR_BASE_OBJECT (dr2), 0);
1965 /* Group each data reference in DRS with it's alias set num. */
1968 build_alias_set_for_drs (VEC (data_reference_p, heap) *drs)
1970 partition_drs_to_sets (drs, ALIAS_SET_INDEX, dr_may_alias_p);
1973 /* Group each data reference in DRS with it's base object set num. */
1976 build_base_obj_set_for_drs (VEC (data_reference_p, heap) *drs)
1978 partition_drs_to_sets (drs, BASE_OBJECT_SET_INDEX, dr_same_base_object_p);
1981 /* Build the data references for PBB. */
1984 build_pbb_drs (poly_bb_p pbb)
1987 data_reference_p dr;
1988 VEC (data_reference_p, heap) *gbb_drs = GBB_DATA_REFS (PBB_BLACK_BOX (pbb));
1990 for (j = 0; VEC_iterate (data_reference_p, gbb_drs, j, dr); j++)
1991 build_poly_dr (dr, pbb);
1994 /* Build data references in SCOP. */
1997 build_scop_drs (scop_p scop)
2001 data_reference_p dr;
2002 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 3);
2004 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
2005 for (j = 0; VEC_iterate (data_reference_p,
2006 GBB_DATA_REFS (PBB_BLACK_BOX (pbb)), j, dr); j++)
2007 VEC_safe_push (data_reference_p, heap, drs, dr);
2009 build_alias_set_for_drs (drs);
2010 build_base_obj_set_for_drs (drs);
2012 /* When debugging, enable the following code. This cannot be used
2013 in production compilers. */
2017 FILE *file_dimacs, *file_ecc, *file_dot;
2019 file_dimacs = fopen ("/tmp/dr_alias_graph_dimacs", "ab");
2020 file_ecc = fopen ("/tmp/dr_alias_graph_ecc", "ab");
2021 file_dot = fopen ("/tmp/dr_alias_graph_dot", "ab");
2024 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
2025 current_function_name ());
2026 write_alias_graph_to_ascii_dimacs (file_dimacs, comment, drs);
2027 fclose (file_dimacs);
2031 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
2032 current_function_name ());
2033 write_alias_graph_to_ascii_ecc (file_ecc, comment, drs);
2038 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
2039 current_function_name ());
2040 write_alias_graph_to_ascii_dot (file_dot, comment, drs);
2046 VEC_free (data_reference_p, heap, drs);
2048 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
2049 build_pbb_drs (pbb);
2052 /* Return a gsi at the position of the phi node STMT. */
2054 static gimple_stmt_iterator
2055 gsi_for_phi_node (gimple stmt)
2057 gimple_stmt_iterator psi;
2058 basic_block bb = gimple_bb (stmt);
2060 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
2061 if (stmt == gsi_stmt (psi))
2068 /* Insert the assignment "RES := VAR" just after the definition of VAR. */
2071 insert_out_of_ssa_copy (tree res, tree var)
2075 gimple_stmt_iterator si;
2076 gimple_stmt_iterator gsi;
2078 var = force_gimple_operand (var, &stmts, true, NULL_TREE);
2079 stmt = gimple_build_assign (res, var);
2081 stmts = gimple_seq_alloc ();
2082 si = gsi_last (stmts);
2083 gsi_insert_after (&si, stmt, GSI_NEW_STMT);
2085 stmt = SSA_NAME_DEF_STMT (var);
2086 if (gimple_code (stmt) == GIMPLE_PHI)
2088 gsi = gsi_after_labels (gimple_bb (stmt));
2089 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2093 gsi = gsi_for_stmt (stmt);
2094 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
2098 /* Insert on edge E the assignment "RES := EXPR". */
2101 insert_out_of_ssa_copy_on_edge (edge e, tree res, tree expr)
2103 gimple_stmt_iterator gsi;
2105 tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
2106 gimple stmt = gimple_build_assign (res, var);
2109 stmts = gimple_seq_alloc ();
2111 gsi = gsi_last (stmts);
2112 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
2113 gsi_insert_seq_on_edge (e, stmts);
2114 gsi_commit_edge_inserts ();
2117 /* Creates a zero dimension array of the same type as VAR. */
2120 create_zero_dim_array (tree var)
2122 tree index_type = build_index_type (integer_zero_node);
2123 tree elt_type = TREE_TYPE (var);
2124 tree array_type = build_array_type (elt_type, index_type);
2125 tree base = create_tmp_var (array_type, "Red");
2127 add_referenced_var (base);
2129 return build4 (ARRAY_REF, elt_type, base, integer_zero_node, NULL_TREE,
2133 /* Returns true when PHI is a loop close phi node. */
2136 scalar_close_phi_node_p (gimple phi)
2138 if (gimple_code (phi) != GIMPLE_PHI
2139 || !is_gimple_reg (gimple_phi_result (phi)))
2142 return (gimple_phi_num_args (phi) == 1);
2145 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2146 dimension array for it. */
2149 rewrite_close_phi_out_of_ssa (gimple_stmt_iterator *psi)
2151 gimple phi = gsi_stmt (*psi);
2152 tree res = gimple_phi_result (phi);
2153 tree var = SSA_NAME_VAR (res);
2154 tree zero_dim_array = create_zero_dim_array (var);
2155 gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
2156 gimple stmt = gimple_build_assign (res, zero_dim_array);
2157 tree arg = gimple_phi_arg_def (phi, 0);
2159 insert_out_of_ssa_copy (zero_dim_array, arg);
2161 remove_phi_node (psi, false);
2162 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2163 SSA_NAME_DEF_STMT (res) = stmt;
2166 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2167 dimension array for it. */
2170 rewrite_phi_out_of_ssa (gimple_stmt_iterator *psi)
2173 gimple phi = gsi_stmt (*psi);
2174 basic_block bb = gimple_bb (phi);
2175 tree res = gimple_phi_result (phi);
2176 tree var = SSA_NAME_VAR (res);
2177 tree zero_dim_array = create_zero_dim_array (var);
2178 gimple_stmt_iterator gsi;
2182 for (i = 0; i < gimple_phi_num_args (phi); i++)
2184 tree arg = gimple_phi_arg_def (phi, i);
2186 /* Try to avoid the insertion on edges as much as possible: this
2187 would avoid the insertion of code on loop latch edges, making
2188 the pattern matching of the vectorizer happy, or it would
2189 avoid the insertion of useless basic blocks. Note that it is
2190 incorrect to insert out of SSA copies close by their
2191 definition when they are more than two loop levels apart:
2192 for example, starting from a double nested loop
2202 the following transform is incorrect
2214 whereas inserting the copy on the incomming edge is correct
2226 if (TREE_CODE (arg) == SSA_NAME
2227 && is_gimple_reg (arg)
2228 && gimple_bb (SSA_NAME_DEF_STMT (arg))
2229 && (flow_bb_inside_loop_p (bb->loop_father,
2230 gimple_bb (SSA_NAME_DEF_STMT (arg)))
2231 || flow_bb_inside_loop_p (loop_outer (bb->loop_father),
2232 gimple_bb (SSA_NAME_DEF_STMT (arg)))))
2233 insert_out_of_ssa_copy (zero_dim_array, arg);
2235 insert_out_of_ssa_copy_on_edge (gimple_phi_arg_edge (phi, i),
2236 zero_dim_array, arg);
2239 var = force_gimple_operand (zero_dim_array, &stmts, true, NULL_TREE);
2242 stmts = gimple_seq_alloc ();
2244 stmt = gimple_build_assign (res, var);
2245 remove_phi_node (psi, false);
2246 SSA_NAME_DEF_STMT (res) = stmt;
2248 gsi = gsi_last (stmts);
2249 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
2251 gsi = gsi_after_labels (bb);
2252 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2255 /* Return true when DEF can be analyzed in REGION by the scalar
2256 evolution analyzer. */
2259 scev_analyzable_p (tree def, sese region)
2261 gimple stmt = SSA_NAME_DEF_STMT (def);
2262 loop_p loop = loop_containing_stmt (stmt);
2263 tree scev = scalar_evolution_in_region (region, loop, def);
2265 return !chrec_contains_undetermined (scev);
2268 /* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
2269 read from ZERO_DIM_ARRAY. */
2272 rewrite_cross_bb_scalar_dependence (tree zero_dim_array, tree def, gimple use_stmt)
2274 tree var = SSA_NAME_VAR (def);
2275 gimple name_stmt = gimple_build_assign (var, zero_dim_array);
2276 tree name = make_ssa_name (var, name_stmt);
2278 use_operand_p use_p;
2279 gimple_stmt_iterator gsi;
2281 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
2283 gimple_assign_set_lhs (name_stmt, name);
2285 gsi = gsi_for_stmt (use_stmt);
2286 gsi_insert_before (&gsi, name_stmt, GSI_NEW_STMT);
2288 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, iter, SSA_OP_ALL_USES)
2289 if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
2290 replace_exp (use_p, name);
2292 update_stmt (use_stmt);
2295 /* Rewrite the scalar dependences crossing the boundary of the BB
2296 containing STMT with an array. */
2299 rewrite_cross_bb_scalar_deps (sese region, gimple_stmt_iterator *gsi)
2301 gimple stmt = gsi_stmt (*gsi);
2302 imm_use_iterator imm_iter;
2305 tree zero_dim_array = NULL_TREE;
2308 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2311 def = gimple_assign_lhs (stmt);
2312 if (!is_gimple_reg (def)
2313 || scev_analyzable_p (def, region))
2316 def_bb = gimple_bb (stmt);
2318 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2319 if (def_bb != gimple_bb (use_stmt)
2320 && gimple_code (use_stmt) != GIMPLE_PHI)
2322 if (!zero_dim_array)
2324 zero_dim_array = create_zero_dim_array (SSA_NAME_VAR (def));
2325 insert_out_of_ssa_copy (zero_dim_array, def);
2329 rewrite_cross_bb_scalar_dependence (zero_dim_array, def, use_stmt);
2333 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2336 rewrite_reductions_out_of_ssa (scop_p scop)
2339 gimple_stmt_iterator psi;
2340 sese region = SCOP_REGION (scop);
2343 if (bb_in_sese_p (bb, region))
2344 for (psi = gsi_start_phis (bb); !gsi_end_p (psi);)
2346 if (scalar_close_phi_node_p (gsi_stmt (psi)))
2347 rewrite_close_phi_out_of_ssa (&psi);
2348 else if (reduction_phi_p (region, &psi))
2349 rewrite_phi_out_of_ssa (&psi);
2352 update_ssa (TODO_update_ssa);
2353 #ifdef ENABLE_CHECKING
2355 verify_loop_closed_ssa ();
2359 if (bb_in_sese_p (bb, region))
2360 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
2361 rewrite_cross_bb_scalar_deps (region, &psi);
2363 update_ssa (TODO_update_ssa);
2364 #ifdef ENABLE_CHECKING
2366 verify_loop_closed_ssa ();
2370 /* Returns the number of pbbs that are in loops contained in SCOP. */
2373 nb_pbbs_in_loops (scop_p scop)
2379 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
2380 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), SCOP_REGION (scop)))
2386 /* Return the number of data references in BB that write in
2390 nb_data_writes_in_bb (basic_block bb)
2393 gimple_stmt_iterator gsi;
2395 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2396 if (gimple_vdef (gsi_stmt (gsi)))
2402 /* Splits STMT out of its current BB. */
2405 split_reduction_stmt (gimple stmt)
2407 gimple_stmt_iterator gsi;
2408 basic_block bb = gimple_bb (stmt);
2411 /* Do not split basic blocks with no writes to memory: the reduction
2412 will be the only write to memory. */
2413 if (nb_data_writes_in_bb (bb) == 0)
2416 split_block (bb, stmt);
2418 gsi = gsi_last_bb (bb);
2420 e = split_block (bb, gsi_stmt (gsi));
2425 /* Return true when stmt is a reduction operation. */
2428 is_reduction_operation_p (gimple stmt)
2430 return flag_associative_math
2431 && commutative_tree_code (gimple_assign_rhs_code (stmt))
2432 && associative_tree_code (gimple_assign_rhs_code (stmt));
2435 /* Returns true when PHI contains an argument ARG. */
2438 phi_contains_arg (gimple phi, tree arg)
2442 for (i = 0; i < gimple_phi_num_args (phi); i++)
2443 if (operand_equal_p (arg, gimple_phi_arg_def (phi, i), 0))
2449 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2452 follow_ssa_with_commutative_ops (tree arg, tree lhs)
2456 if (TREE_CODE (arg) != SSA_NAME)
2459 stmt = SSA_NAME_DEF_STMT (arg);
2461 if (gimple_code (stmt) == GIMPLE_PHI)
2463 if (phi_contains_arg (stmt, lhs))
2468 if (gimple_num_ops (stmt) == 2)
2469 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2471 if (is_reduction_operation_p (stmt))
2473 gimple res = follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2476 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt), lhs);
2482 /* Detect commutative and associative scalar reductions starting at
2486 detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
2487 VEC (gimple, heap) **in,
2488 VEC (gimple, heap) **out)
2490 gimple phi = follow_ssa_with_commutative_ops (arg, lhs);
2494 VEC_safe_push (gimple, heap, *in, stmt);
2495 VEC_safe_push (gimple, heap, *out, stmt);
2502 /* Detect commutative and associative scalar reductions starting at
2506 detect_commutative_reduction_assign (gimple stmt, VEC (gimple, heap) **in,
2507 VEC (gimple, heap) **out)
2509 tree lhs = gimple_assign_lhs (stmt);
2511 if (gimple_num_ops (stmt) == 2)
2512 return detect_commutative_reduction_arg (lhs, stmt,
2513 gimple_assign_rhs1 (stmt),
2516 if (is_reduction_operation_p (stmt))
2518 gimple res = detect_commutative_reduction_arg (lhs, stmt,
2519 gimple_assign_rhs1 (stmt),
2522 : detect_commutative_reduction_arg (lhs, stmt,
2523 gimple_assign_rhs2 (stmt),
2530 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2533 follow_inital_value_to_phi (tree arg, tree lhs)
2537 if (!arg || TREE_CODE (arg) != SSA_NAME)
2540 stmt = SSA_NAME_DEF_STMT (arg);
2542 if (gimple_code (stmt) == GIMPLE_PHI
2543 && phi_contains_arg (stmt, lhs))
2550 /* Return the argument of the loop PHI that is the inital value coming
2551 from outside the loop. */
2554 edge_initial_value_for_loop_phi (gimple phi)
2558 for (i = 0; i < gimple_phi_num_args (phi); i++)
2560 edge e = gimple_phi_arg_edge (phi, i);
2562 if (loop_depth (e->src->loop_father)
2563 < loop_depth (e->dest->loop_father))
2570 /* Return the argument of the loop PHI that is the inital value coming
2571 from outside the loop. */
2574 initial_value_for_loop_phi (gimple phi)
2578 for (i = 0; i < gimple_phi_num_args (phi); i++)
2580 edge e = gimple_phi_arg_edge (phi, i);
2582 if (loop_depth (e->src->loop_father)
2583 < loop_depth (e->dest->loop_father))
2584 return gimple_phi_arg_def (phi, i);
2590 /* Detect commutative and associative scalar reductions starting at
2591 the loop closed phi node CLOSE_PHI. */
2594 detect_commutative_reduction (gimple stmt, VEC (gimple, heap) **in,
2595 VEC (gimple, heap) **out)
2597 if (scalar_close_phi_node_p (stmt))
2599 tree arg = gimple_phi_arg_def (stmt, 0);
2600 gimple def = SSA_NAME_DEF_STMT (arg);
2601 gimple loop_phi = detect_commutative_reduction (def, in, out);
2605 tree lhs = gimple_phi_result (stmt);
2606 tree init = initial_value_for_loop_phi (loop_phi);
2607 gimple phi = follow_inital_value_to_phi (init, lhs);
2609 VEC_safe_push (gimple, heap, *in, loop_phi);
2610 VEC_safe_push (gimple, heap, *out, stmt);
2617 if (gimple_code (stmt) == GIMPLE_ASSIGN)
2618 return detect_commutative_reduction_assign (stmt, in, out);
2623 /* Translate the scalar reduction statement STMT to an array RED
2624 knowing that its recursive phi node is LOOP_PHI. */
2627 translate_scalar_reduction_to_array_for_stmt (tree red, gimple stmt,
2630 basic_block bb = gimple_bb (stmt);
2631 gimple_stmt_iterator insert_gsi = gsi_after_labels (bb);
2632 tree res = gimple_phi_result (loop_phi);
2633 gimple assign = gimple_build_assign (res, red);
2635 gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
2637 assign = gimple_build_assign (red, gimple_assign_lhs (stmt));
2638 insert_gsi = gsi_for_stmt (stmt);
2639 gsi_insert_after (&insert_gsi, assign, GSI_SAME_STMT);
2642 /* Insert the assignment "result (CLOSE_PHI) = RED". */
2645 insert_copyout (tree red, gimple close_phi)
2647 tree res = gimple_phi_result (close_phi);
2648 basic_block bb = gimple_bb (close_phi);
2649 gimple_stmt_iterator insert_gsi = gsi_after_labels (bb);
2650 gimple assign = gimple_build_assign (res, red);
2652 gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
2655 /* Insert the assignment "RED = initial_value (LOOP_PHI)". */
2658 insert_copyin (tree red, gimple loop_phi)
2661 tree init = initial_value_for_loop_phi (loop_phi);
2662 tree expr = build2 (MODIFY_EXPR, TREE_TYPE (init), red, init);
2664 force_gimple_operand (expr, &stmts, true, NULL);
2665 gsi_insert_seq_on_edge (edge_initial_value_for_loop_phi (loop_phi), stmts);
2668 /* Rewrite out of SSA the reduction described by the loop phi nodes
2669 IN, and the close phi nodes OUT. IN and OUT are structured by loop
2672 IN: stmt, loop_n, ..., loop_0
2673 OUT: stmt, close_n, ..., close_0
2675 the first element is the reduction statement, and the next elements
2676 are the loop and close phi nodes of each of the outer loops. */
2679 translate_scalar_reduction_to_array (VEC (gimple, heap) *in,
2680 VEC (gimple, heap) *out,
2686 gimple_stmt_iterator gsi;
2688 for (i = 0; VEC_iterate (gimple, in, i, loop_phi); i++)
2690 gimple close_phi = VEC_index (gimple, out, i);
2694 gimple stmt = loop_phi;
2695 basic_block bb = split_reduction_stmt (stmt);
2697 SET_BIT (reductions, bb->index);
2698 gcc_assert (close_phi == loop_phi);
2700 red = create_zero_dim_array (gimple_assign_lhs (stmt));
2701 translate_scalar_reduction_to_array_for_stmt
2702 (red, stmt, VEC_index (gimple, in, 1));
2706 if (i == VEC_length (gimple, in) - 1)
2708 insert_copyout (red, close_phi);
2709 insert_copyin (red, loop_phi);
2712 gsi = gsi_for_phi_node (loop_phi);
2713 remove_phi_node (&gsi, false);
2715 gsi = gsi_for_phi_node (close_phi);
2716 remove_phi_node (&gsi, false);
2720 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. */
2723 rewrite_commutative_reductions_out_of_ssa_close_phi (gimple close_phi,
2726 VEC (gimple, heap) *in = VEC_alloc (gimple, heap, 10);
2727 VEC (gimple, heap) *out = VEC_alloc (gimple, heap, 10);
2729 detect_commutative_reduction (close_phi, &in, &out);
2730 if (VEC_length (gimple, in) > 0)
2731 translate_scalar_reduction_to_array (in, out, reductions);
2733 VEC_free (gimple, heap, in);
2734 VEC_free (gimple, heap, out);
2737 /* Rewrites all the commutative reductions from LOOP out of SSA. */
2740 rewrite_commutative_reductions_out_of_ssa_loop (loop_p loop,
2743 gimple_stmt_iterator gsi;
2744 edge exit = single_exit (loop);
2749 for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
2750 rewrite_commutative_reductions_out_of_ssa_close_phi (gsi_stmt (gsi),
2754 /* Rewrites all the commutative reductions from SCOP out of SSA. */
2757 rewrite_commutative_reductions_out_of_ssa (sese region, sbitmap reductions)
2762 FOR_EACH_LOOP (li, loop, 0)
2763 if (loop_in_sese_p (loop, region))
2764 rewrite_commutative_reductions_out_of_ssa_loop (loop, reductions);
2766 gsi_commit_edge_inserts ();
2767 update_ssa (TODO_update_ssa);
2768 #ifdef ENABLE_CHECKING
2770 verify_loop_closed_ssa ();
2774 /* Builds the polyhedral representation for a SESE region. */
2777 build_poly_scop (scop_p scop)
2779 sese region = SCOP_REGION (scop);
2780 sbitmap reductions = sbitmap_alloc (last_basic_block * 2);
2782 sbitmap_zero (reductions);
2783 rewrite_commutative_reductions_out_of_ssa (region, reductions);
2784 rewrite_reductions_out_of_ssa (scop);
2785 build_scop_bbs (scop, reductions);
2786 sbitmap_free (reductions);
2788 /* FIXME: This restriction is needed to avoid a problem in CLooG.
2789 Once CLooG is fixed, remove this guard. Anyways, it makes no
2790 sense to optimize a scop containing only PBBs that do not belong
2792 if (nb_pbbs_in_loops (scop) == 0)
2795 build_sese_loop_nests (region);
2796 build_sese_conditions (region);
2797 find_scop_parameters (scop);
2799 build_scop_iteration_domain (scop);
2800 build_scop_context (scop);
2802 add_conditions_to_constraints (scop);
2804 build_scop_scattering (scop);
2805 build_scop_drs (scop);
2810 /* Always return false. Exercise the scop_to_clast function. */
2813 check_poly_representation (scop_p scop ATTRIBUTE_UNUSED)
2815 #ifdef ENABLE_CHECKING
2816 cloog_prog_clast pc = scop_to_clast (scop);
2817 cloog_clast_free (pc.stmt);
2818 cloog_program_free (pc.prog);