Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / graphite-sese-to-poly.c
blob0f44a92d4ef6e505f59079e6c30fcfc64a7165a6
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
31 #include "toplev.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "cfgloop.h"
35 #include "tree-chrec.h"
36 #include "tree-data-ref.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-pass.h"
39 #include "domwalk.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
42 #include "gimple.h"
43 #include "sese.h"
45 #ifdef HAVE_cloog
46 #include "ppl_c.h"
47 #include "graphite-ppl.h"
48 #include "graphite.h"
49 #include "graphite-poly.h"
50 #include "graphite-scop-detection.h"
51 #include "graphite-sese-to-poly.h"
53 /* Check if VAR is used in a phi node, that is no loop header. */
55 static bool
56 var_used_in_not_loop_header_phi_node (tree var)
58 imm_use_iterator imm_iter;
59 gimple stmt;
60 bool result = false;
62 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, var)
64 basic_block bb = gimple_bb (stmt);
66 if (gimple_code (stmt) == GIMPLE_PHI
67 && bb->loop_father->header != bb)
68 result = true;
71 return result;
74 /* Returns the index of the PHI argument defined in the outermost
75 loop. */
77 static size_t
78 phi_arg_in_outermost_loop (gimple phi)
80 loop_p loop = gimple_bb (phi)->loop_father;
81 size_t i, res = 0;
83 for (i = 0; i < gimple_phi_num_args (phi); i++)
84 if (!flow_bb_inside_loop_p (loop, gimple_phi_arg_edge (phi, i)->src))
86 loop = gimple_phi_arg_edge (phi, i)->src->loop_father;
87 res = i;
90 return res;
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". */
96 static void
97 remove_simple_copy_phi (gimple_stmt_iterator *psi)
99 gimple phi = gsi_stmt (*psi);
100 tree res = gimple_phi_result (phi);
101 size_t entry = phi_arg_in_outermost_loop (phi);
102 tree init = gimple_phi_arg_def (phi, entry);
103 gimple stmt = gimple_build_assign (res, init);
104 edge e = gimple_phi_arg_edge (phi, entry);
106 remove_phi_node (psi, false);
107 gsi_insert_on_edge_immediate (e, stmt);
108 SSA_NAME_DEF_STMT (res) = stmt;
111 /* Removes an invariant phi node at position PSI by inserting on the
112 loop ENTRY edge the assignment RES = INIT. */
114 static void
115 remove_invariant_phi (sese region, gimple_stmt_iterator *psi)
117 gimple phi = gsi_stmt (*psi);
118 loop_p loop = loop_containing_stmt (phi);
119 tree res = gimple_phi_result (phi);
120 tree scev = scalar_evolution_in_region (region, loop, res);
121 size_t entry = phi_arg_in_outermost_loop (phi);
122 edge e = gimple_phi_arg_edge (phi, entry);
123 tree var;
124 gimple stmt;
125 gimple_seq stmts;
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);
135 if (!stmts)
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)". */
147 static inline bool
148 simple_copy_phi_p (gimple phi)
150 tree res;
152 if (gimple_phi_num_args (phi) != 2)
153 return false;
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
162 be considered. */
164 static bool
165 reduction_phi_p (sese region, gimple_stmt_iterator *psi)
167 loop_p loop;
168 gimple phi = gsi_stmt (*psi);
169 tree res = gimple_phi_result (phi);
171 if (!is_gimple_reg (res))
173 gsi_next (psi);
174 return false;
177 loop = loop_containing_stmt (phi);
179 if (simple_copy_phi_p (phi))
181 /* PRE introduces phi nodes like these, for an example,
182 see id-5.f in the fortran graphite testsuite:
184 # prephitmp.85_265 = PHI <prephitmp.85_258(33), prephitmp.85_265(18)>
186 remove_simple_copy_phi (psi);
187 return false;
190 if (scev_analyzable_p (res, region))
192 tree scev = scalar_evolution_in_region (region, loop, res);
194 if (evolution_function_is_invariant_p (scev, loop->num))
195 remove_invariant_phi (region, psi);
196 else
197 gsi_next (psi);
199 return false;
202 /* All the other cases are considered reductions. */
203 return true;
206 /* Returns true when BB will be represented in graphite. Return false
207 for the basic blocks that contain code eliminated in the code
208 generation pass: i.e. induction variables and exit conditions. */
210 static bool
211 graphite_stmt_p (sese region, basic_block bb,
212 VEC (data_reference_p, heap) *drs)
214 gimple_stmt_iterator gsi;
215 loop_p loop = bb->loop_father;
217 if (VEC_length (data_reference_p, drs) > 0)
218 return true;
220 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
222 gimple stmt = gsi_stmt (gsi);
224 switch (gimple_code (stmt))
226 case GIMPLE_DEBUG:
227 /* Control flow expressions can be ignored, as they are
228 represented in the iteration domains and will be
229 regenerated by graphite. */
230 case GIMPLE_COND:
231 case GIMPLE_GOTO:
232 case GIMPLE_SWITCH:
233 break;
235 case GIMPLE_ASSIGN:
237 tree var = gimple_assign_lhs (stmt);
239 /* We need these bbs to be able to construct the phi nodes. */
240 if (var_used_in_not_loop_header_phi_node (var))
241 return true;
243 var = scalar_evolution_in_region (region, loop, var);
244 if (chrec_contains_undetermined (var))
245 return true;
247 break;
250 default:
251 return true;
255 return false;
258 /* Store the GRAPHITE representation of BB. */
260 static gimple_bb_p
261 new_gimple_bb (basic_block bb, VEC (data_reference_p, heap) *drs)
263 struct gimple_bb *gbb;
265 gbb = XNEW (struct gimple_bb);
266 bb->aux = gbb;
267 GBB_BB (gbb) = bb;
268 GBB_DATA_REFS (gbb) = drs;
269 GBB_CONDITIONS (gbb) = NULL;
270 GBB_CONDITION_CASES (gbb) = NULL;
272 return gbb;
275 static void
276 free_data_refs_aux (VEC (data_reference_p, heap) *datarefs)
278 unsigned int i;
279 struct data_reference *dr;
281 for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
282 if (dr->aux)
284 base_alias_pair *bap = (base_alias_pair *)(dr->aux);
286 if (bap->alias_set)
287 free (bap->alias_set);
289 free (bap);
290 dr->aux = NULL;
293 /* Frees GBB. */
295 static void
296 free_gimple_bb (struct gimple_bb *gbb)
298 free_data_refs_aux (GBB_DATA_REFS (gbb));
299 free_data_refs (GBB_DATA_REFS (gbb));
301 VEC_free (gimple, heap, GBB_CONDITIONS (gbb));
302 VEC_free (gimple, heap, GBB_CONDITION_CASES (gbb));
303 GBB_BB (gbb)->aux = 0;
304 XDELETE (gbb);
307 /* Deletes all gimple bbs in SCOP. */
309 static void
310 remove_gbbs_in_scop (scop_p scop)
312 int i;
313 poly_bb_p pbb;
315 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
316 free_gimple_bb (PBB_BLACK_BOX (pbb));
319 /* Deletes all scops in SCOPS. */
321 void
322 free_scops (VEC (scop_p, heap) *scops)
324 int i;
325 scop_p scop;
327 for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
329 remove_gbbs_in_scop (scop);
330 free_sese (SCOP_REGION (scop));
331 free_scop (scop);
334 VEC_free (scop_p, heap, scops);
337 /* Generates a polyhedral black box only if the bb contains interesting
338 information. */
340 static void
341 try_generate_gimple_bb (scop_p scop, basic_block bb, sbitmap reductions)
343 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
344 loop_p nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
345 gimple_stmt_iterator gsi;
347 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
349 gimple stmt = gsi_stmt (gsi);
350 if (!is_gimple_debug (stmt))
351 graphite_find_data_references_in_stmt (nest, stmt, &drs);
354 if (!graphite_stmt_p (SCOP_REGION (scop), bb, drs))
355 free_data_refs (drs);
356 else
357 new_poly_bb (scop, new_gimple_bb (bb, drs), TEST_BIT (reductions,
358 bb->index));
361 /* Returns true if all predecessors of BB, that are not dominated by BB, are
362 marked in MAP. The predecessors dominated by BB are loop latches and will
363 be handled after BB. */
365 static bool
366 all_non_dominated_preds_marked_p (basic_block bb, sbitmap map)
368 edge e;
369 edge_iterator ei;
371 FOR_EACH_EDGE (e, ei, bb->preds)
372 if (!TEST_BIT (map, e->src->index)
373 && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
374 return false;
376 return true;
379 /* Compare the depth of two basic_block's P1 and P2. */
381 static int
382 compare_bb_depths (const void *p1, const void *p2)
384 const_basic_block const bb1 = *(const_basic_block const*)p1;
385 const_basic_block const bb2 = *(const_basic_block const*)p2;
386 int d1 = loop_depth (bb1->loop_father);
387 int d2 = loop_depth (bb2->loop_father);
389 if (d1 < d2)
390 return 1;
392 if (d1 > d2)
393 return -1;
395 return 0;
398 /* Sort the basic blocks from DOM such that the first are the ones at
399 a deepest loop level. */
401 static void
402 graphite_sort_dominated_info (VEC (basic_block, heap) *dom)
404 size_t len = VEC_length (basic_block, dom);
406 qsort (VEC_address (basic_block, dom), len, sizeof (basic_block),
407 compare_bb_depths);
410 /* Recursive helper function for build_scops_bbs. */
412 static void
413 build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb, sbitmap reductions)
415 sese region = SCOP_REGION (scop);
416 VEC (basic_block, heap) *dom;
418 if (TEST_BIT (visited, bb->index)
419 || !bb_in_sese_p (bb, region))
420 return;
422 try_generate_gimple_bb (scop, bb, reductions);
423 SET_BIT (visited, bb->index);
425 dom = get_dominated_by (CDI_DOMINATORS, bb);
427 if (dom == NULL)
428 return;
430 graphite_sort_dominated_info (dom);
432 while (!VEC_empty (basic_block, dom))
434 int i;
435 basic_block dom_bb;
437 for (i = 0; VEC_iterate (basic_block, dom, i, dom_bb); i++)
438 if (all_non_dominated_preds_marked_p (dom_bb, visited))
440 build_scop_bbs_1 (scop, visited, dom_bb, reductions);
441 VEC_unordered_remove (basic_block, dom, i);
442 break;
446 VEC_free (basic_block, heap, dom);
449 /* Gather the basic blocks belonging to the SCOP. */
451 void
452 build_scop_bbs (scop_p scop, sbitmap reductions)
454 sbitmap visited = sbitmap_alloc (last_basic_block);
455 sese region = SCOP_REGION (scop);
457 sbitmap_zero (visited);
458 build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region), reductions);
459 sbitmap_free (visited);
462 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
463 We generate SCATTERING_DIMENSIONS scattering dimensions.
465 CLooG 0.15.0 and previous versions require, that all
466 scattering functions of one CloogProgram have the same number of
467 scattering dimensions, therefore we allow to specify it. This
468 should be removed in future versions of CLooG.
470 The scattering polyhedron consists of these dimensions: scattering,
471 loop_iterators, parameters.
473 Example:
475 | scattering_dimensions = 5
476 | used_scattering_dimensions = 3
477 | nb_iterators = 1
478 | scop_nb_params = 2
480 | Schedule:
482 | 4 5
484 | Scattering polyhedron:
486 | scattering: {s1, s2, s3, s4, s5}
487 | loop_iterators: {i}
488 | parameters: {p1, p2}
490 | s1 s2 s3 s4 s5 i p1 p2 1
491 | 1 0 0 0 0 0 0 0 -4 = 0
492 | 0 1 0 0 0 -1 0 0 0 = 0
493 | 0 0 1 0 0 0 0 0 -5 = 0 */
495 static void
496 build_pbb_scattering_polyhedrons (ppl_Linear_Expression_t static_schedule,
497 poly_bb_p pbb, int scattering_dimensions)
499 int i;
500 scop_p scop = PBB_SCOP (pbb);
501 int nb_iterators = pbb_dim_iter_domain (pbb);
502 int used_scattering_dimensions = nb_iterators * 2 + 1;
503 int nb_params = scop_nb_params (scop);
504 ppl_Coefficient_t c;
505 ppl_dimension_type dim = scattering_dimensions + nb_iterators + nb_params;
506 mpz_t v;
508 gcc_assert (scattering_dimensions >= used_scattering_dimensions);
510 mpz_init (v);
511 ppl_new_Coefficient (&c);
512 PBB_TRANSFORMED (pbb) = poly_scattering_new ();
513 ppl_new_C_Polyhedron_from_space_dimension
514 (&PBB_TRANSFORMED_SCATTERING (pbb), dim, 0);
516 PBB_NB_SCATTERING_TRANSFORM (pbb) = scattering_dimensions;
518 for (i = 0; i < scattering_dimensions; i++)
520 ppl_Constraint_t cstr;
521 ppl_Linear_Expression_t expr;
523 ppl_new_Linear_Expression_with_dimension (&expr, dim);
524 mpz_set_si (v, 1);
525 ppl_assign_Coefficient_from_mpz_t (c, v);
526 ppl_Linear_Expression_add_to_coefficient (expr, i, c);
528 /* Textual order inside this loop. */
529 if ((i % 2) == 0)
531 ppl_Linear_Expression_coefficient (static_schedule, i / 2, c);
532 ppl_Coefficient_to_mpz_t (c, v);
533 mpz_neg (v, v);
534 ppl_assign_Coefficient_from_mpz_t (c, v);
535 ppl_Linear_Expression_add_to_inhomogeneous (expr, c);
538 /* Iterations of this loop. */
539 else /* if ((i % 2) == 1) */
541 int loop = (i - 1) / 2;
543 mpz_set_si (v, -1);
544 ppl_assign_Coefficient_from_mpz_t (c, v);
545 ppl_Linear_Expression_add_to_coefficient
546 (expr, scattering_dimensions + loop, c);
549 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
550 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb), cstr);
551 ppl_delete_Linear_Expression (expr);
552 ppl_delete_Constraint (cstr);
555 mpz_clear (v);
556 ppl_delete_Coefficient (c);
558 PBB_ORIGINAL (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
561 /* Build for BB the static schedule.
563 The static schedule is a Dewey numbering of the abstract syntax
564 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
566 The following example informally defines the static schedule:
569 for (i: ...)
571 for (j: ...)
577 for (k: ...)
585 Static schedules for A to F:
587 DEPTH
588 0 1 2
590 B 1 0 0
591 C 1 0 1
592 D 1 1 0
593 E 1 1 1
597 static void
598 build_scop_scattering (scop_p scop)
600 int i;
601 poly_bb_p pbb;
602 gimple_bb_p previous_gbb = NULL;
603 ppl_Linear_Expression_t static_schedule;
604 ppl_Coefficient_t c;
605 mpz_t v;
607 mpz_init (v);
608 ppl_new_Coefficient (&c);
609 ppl_new_Linear_Expression (&static_schedule);
611 /* We have to start schedules at 0 on the first component and
612 because we cannot compare_prefix_loops against a previous loop,
613 prefix will be equal to zero, and that index will be
614 incremented before copying. */
615 mpz_set_si (v, -1);
616 ppl_assign_Coefficient_from_mpz_t (c, v);
617 ppl_Linear_Expression_add_to_coefficient (static_schedule, 0, c);
619 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
621 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
622 ppl_Linear_Expression_t common;
623 int prefix;
624 int nb_scat_dims = pbb_dim_iter_domain (pbb) * 2 + 1;
626 if (previous_gbb)
627 prefix = nb_common_loops (SCOP_REGION (scop), previous_gbb, gbb);
628 else
629 prefix = 0;
631 previous_gbb = gbb;
632 ppl_new_Linear_Expression_with_dimension (&common, prefix + 1);
633 ppl_assign_Linear_Expression_from_Linear_Expression (common,
634 static_schedule);
636 mpz_set_si (v, 1);
637 ppl_assign_Coefficient_from_mpz_t (c, v);
638 ppl_Linear_Expression_add_to_coefficient (common, prefix, c);
639 ppl_assign_Linear_Expression_from_Linear_Expression (static_schedule,
640 common);
642 build_pbb_scattering_polyhedrons (common, pbb, nb_scat_dims);
644 ppl_delete_Linear_Expression (common);
647 mpz_clear (v);
648 ppl_delete_Coefficient (c);
649 ppl_delete_Linear_Expression (static_schedule);
652 /* Add the value K to the dimension D of the linear expression EXPR. */
654 static void
655 add_value_to_dim (ppl_dimension_type d, ppl_Linear_Expression_t expr,
656 mpz_t k)
658 mpz_t val;
659 ppl_Coefficient_t coef;
661 ppl_new_Coefficient (&coef);
662 ppl_Linear_Expression_coefficient (expr, d, coef);
663 mpz_init (val);
664 ppl_Coefficient_to_mpz_t (coef, val);
666 mpz_add (val, val, k);
668 ppl_assign_Coefficient_from_mpz_t (coef, val);
669 ppl_Linear_Expression_add_to_coefficient (expr, d, coef);
670 mpz_clear (val);
671 ppl_delete_Coefficient (coef);
674 /* In the context of scop S, scan E, the right hand side of a scalar
675 evolution function in loop VAR, and translate it to a linear
676 expression EXPR. */
678 static void
679 scan_tree_for_params_right_scev (sese s, tree e, int var,
680 ppl_Linear_Expression_t expr)
682 if (expr)
684 loop_p loop = get_loop (var);
685 ppl_dimension_type l = sese_loop_depth (s, loop) - 1;
686 mpz_t val;
688 /* Scalar evolutions should happen in the sese region. */
689 gcc_assert (sese_loop_depth (s, loop) > 0);
691 /* We can not deal with parametric strides like:
693 | p = parameter;
695 | for i:
696 | a [i * p] = ... */
697 gcc_assert (TREE_CODE (e) == INTEGER_CST);
699 mpz_init (val);
700 mpz_set_si (val, int_cst_value (e));
701 add_value_to_dim (l, expr, val);
702 mpz_clear (val);
706 /* Scan the integer constant CST, and add it to the inhomogeneous part of the
707 linear expression EXPR. K is the multiplier of the constant. */
709 static void
710 scan_tree_for_params_int (tree cst, ppl_Linear_Expression_t expr, mpz_t k)
712 mpz_t val;
713 ppl_Coefficient_t coef;
714 int v = int_cst_value (cst);
716 mpz_init (val);
717 mpz_set_si (val, 0);
719 /* Necessary to not get "-1 = 2^n - 1". */
720 if (v < 0)
721 mpz_sub_ui (val, val, -v);
722 else
723 mpz_add_ui (val, val, v);
725 mpz_mul (val, val, k);
726 ppl_new_Coefficient (&coef);
727 ppl_assign_Coefficient_from_mpz_t (coef, val);
728 ppl_Linear_Expression_add_to_inhomogeneous (expr, coef);
729 mpz_clear (val);
730 ppl_delete_Coefficient (coef);
733 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
734 Otherwise returns -1. */
736 static inline int
737 parameter_index_in_region_1 (tree name, sese region)
739 int i;
740 tree p;
742 gcc_assert (TREE_CODE (name) == SSA_NAME);
744 for (i = 0; VEC_iterate (tree, SESE_PARAMS (region), i, p); i++)
745 if (p == name)
746 return i;
748 return -1;
751 /* When the parameter NAME is in REGION, returns its index in
752 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
753 and returns the index of NAME. */
755 static int
756 parameter_index_in_region (tree name, sese region)
758 int i;
760 gcc_assert (TREE_CODE (name) == SSA_NAME);
762 i = parameter_index_in_region_1 (name, region);
763 if (i != -1)
764 return i;
766 gcc_assert (SESE_ADD_PARAMS (region));
768 i = VEC_length (tree, SESE_PARAMS (region));
769 VEC_safe_push (tree, heap, SESE_PARAMS (region), name);
770 return i;
773 /* In the context of sese S, scan the expression E and translate it to
774 a linear expression C. When parsing a symbolic multiplication, K
775 represents the constant multiplier of an expression containing
776 parameters. */
778 static void
779 scan_tree_for_params (sese s, tree e, ppl_Linear_Expression_t c,
780 mpz_t k)
782 if (e == chrec_dont_know)
783 return;
785 switch (TREE_CODE (e))
787 case POLYNOMIAL_CHREC:
788 scan_tree_for_params_right_scev (s, CHREC_RIGHT (e),
789 CHREC_VARIABLE (e), c);
790 scan_tree_for_params (s, CHREC_LEFT (e), c, k);
791 break;
793 case MULT_EXPR:
794 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
796 if (c)
798 mpz_t val;
799 gcc_assert (host_integerp (TREE_OPERAND (e, 1), 0));
800 mpz_init (val);
801 mpz_set_si (val, int_cst_value (TREE_OPERAND (e, 1)));
802 mpz_mul (val, val, k);
803 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, val);
804 mpz_clear (val);
806 else
807 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
809 else
811 if (c)
813 mpz_t val;
814 gcc_assert (host_integerp (TREE_OPERAND (e, 0), 0));
815 mpz_init (val);
816 mpz_set_si (val, int_cst_value (TREE_OPERAND (e, 0)));
817 mpz_mul (val, val, k);
818 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, val);
819 mpz_clear (val);
821 else
822 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, k);
824 break;
826 case PLUS_EXPR:
827 case POINTER_PLUS_EXPR:
828 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
829 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, k);
830 break;
832 case MINUS_EXPR:
834 ppl_Linear_Expression_t tmp_expr = NULL;
836 if (c)
838 ppl_dimension_type dim;
839 ppl_Linear_Expression_space_dimension (c, &dim);
840 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
843 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
844 scan_tree_for_params (s, TREE_OPERAND (e, 1), tmp_expr, k);
846 if (c)
848 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
849 tmp_expr);
850 ppl_delete_Linear_Expression (tmp_expr);
853 break;
856 case NEGATE_EXPR:
858 ppl_Linear_Expression_t tmp_expr = NULL;
860 if (c)
862 ppl_dimension_type dim;
863 ppl_Linear_Expression_space_dimension (c, &dim);
864 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
867 scan_tree_for_params (s, TREE_OPERAND (e, 0), tmp_expr, k);
869 if (c)
871 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
872 tmp_expr);
873 ppl_delete_Linear_Expression (tmp_expr);
876 break;
879 case BIT_NOT_EXPR:
881 ppl_Linear_Expression_t tmp_expr = NULL;
883 if (c)
885 ppl_dimension_type dim;
886 ppl_Linear_Expression_space_dimension (c, &dim);
887 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
890 scan_tree_for_params (s, TREE_OPERAND (e, 0), tmp_expr, k);
892 if (c)
894 ppl_Coefficient_t coef;
895 mpz_t minus_one;
897 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
898 tmp_expr);
899 ppl_delete_Linear_Expression (tmp_expr);
900 mpz_init (minus_one);
901 mpz_set_si (minus_one, -1);
902 ppl_new_Coefficient_from_mpz_t (&coef, minus_one);
903 ppl_Linear_Expression_add_to_inhomogeneous (c, coef);
904 mpz_clear (minus_one);
905 ppl_delete_Coefficient (coef);
908 break;
911 case SSA_NAME:
913 ppl_dimension_type p = parameter_index_in_region (e, s);
915 if (c)
917 ppl_dimension_type dim;
918 ppl_Linear_Expression_space_dimension (c, &dim);
919 p += dim - sese_nb_params (s);
920 add_value_to_dim (p, c, k);
922 break;
925 case INTEGER_CST:
926 if (c)
927 scan_tree_for_params_int (e, c, k);
928 break;
930 CASE_CONVERT:
931 case NON_LVALUE_EXPR:
932 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
933 break;
935 default:
936 gcc_unreachable ();
937 break;
941 /* Find parameters with respect to REGION in BB. We are looking in memory
942 access functions, conditions and loop bounds. */
944 static void
945 find_params_in_bb (sese region, gimple_bb_p gbb)
947 int i;
948 unsigned j;
949 data_reference_p dr;
950 gimple stmt;
951 loop_p loop = GBB_BB (gbb)->loop_father;
952 mpz_t one;
954 mpz_init (one);
955 mpz_set_si (one, 1);
957 /* Find parameters in the access functions of data references. */
958 for (i = 0; VEC_iterate (data_reference_p, GBB_DATA_REFS (gbb), i, dr); i++)
959 for (j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
960 scan_tree_for_params (region, DR_ACCESS_FN (dr, j), NULL, one);
962 /* Find parameters in conditional statements. */
963 for (i = 0; VEC_iterate (gimple, GBB_CONDITIONS (gbb), i, stmt); i++)
965 tree lhs = scalar_evolution_in_region (region, loop,
966 gimple_cond_lhs (stmt));
967 tree rhs = scalar_evolution_in_region (region, loop,
968 gimple_cond_rhs (stmt));
970 scan_tree_for_params (region, lhs, NULL, one);
971 scan_tree_for_params (region, rhs, NULL, one);
974 mpz_clear (one);
977 /* Record the parameters used in the SCOP. A variable is a parameter
978 in a scop if it does not vary during the execution of that scop. */
980 static void
981 find_scop_parameters (scop_p scop)
983 poly_bb_p pbb;
984 unsigned i;
985 sese region = SCOP_REGION (scop);
986 struct loop *loop;
987 mpz_t one;
989 mpz_init (one);
990 mpz_set_si (one, 1);
992 /* Find the parameters used in the loop bounds. */
993 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop); i++)
995 tree nb_iters = number_of_latch_executions (loop);
997 if (!chrec_contains_symbols (nb_iters))
998 continue;
1000 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
1001 scan_tree_for_params (region, nb_iters, NULL, one);
1004 mpz_clear (one);
1006 /* Find the parameters used in data accesses. */
1007 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1008 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
1010 scop_set_nb_params (scop, sese_nb_params (region));
1011 SESE_ADD_PARAMS (region) = false;
1013 ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension
1014 (&SCOP_CONTEXT (scop), scop_nb_params (scop), 0);
1017 /* Returns a gimple_bb from BB. */
1019 static inline gimple_bb_p
1020 gbb_from_bb (basic_block bb)
1022 return (gimple_bb_p) bb->aux;
1025 /* Insert in the SCOP context constraints from the estimation of the
1026 number of iterations. UB_EXPR is a linear expression describing
1027 the number of iterations in a loop. This expression is bounded by
1028 the estimation NIT. */
1030 static void
1031 add_upper_bounds_from_estimated_nit (scop_p scop, double_int nit,
1032 ppl_dimension_type dim,
1033 ppl_Linear_Expression_t ub_expr)
1035 mpz_t val;
1036 ppl_Linear_Expression_t nb_iters_le;
1037 ppl_Polyhedron_t pol;
1038 ppl_Coefficient_t coef;
1039 ppl_Constraint_t ub;
1041 ppl_new_Linear_Expression_with_dimension (&ub_expr, dim);
1042 ppl_new_C_Polyhedron_from_space_dimension (&pol, dim, 0);
1043 ppl_new_Linear_Expression_from_Linear_Expression (&nb_iters_le,
1044 ub_expr);
1046 /* Construct the negated number of last iteration in VAL. */
1047 mpz_init (val);
1048 mpz_set_double_int (val, nit, false);
1049 mpz_sub_ui (val, val, 1);
1050 mpz_neg (val, val);
1052 /* NB_ITERS_LE holds the number of last iteration in
1053 parametrical form. Subtract estimated number of last
1054 iteration and assert that result is not positive. */
1055 ppl_new_Coefficient_from_mpz_t (&coef, val);
1056 ppl_Linear_Expression_add_to_inhomogeneous (nb_iters_le, coef);
1057 ppl_delete_Coefficient (coef);
1058 ppl_new_Constraint (&ub, nb_iters_le,
1059 PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL);
1060 ppl_Polyhedron_add_constraint (pol, ub);
1062 /* Remove all but last GDIM dimensions from POL to obtain
1063 only the constraints on the parameters. */
1065 graphite_dim_t gdim = scop_nb_params (scop);
1066 ppl_dimension_type *dims = XNEWVEC (ppl_dimension_type, dim - gdim);
1067 graphite_dim_t i;
1069 for (i = 0; i < dim - gdim; i++)
1070 dims[i] = i;
1072 ppl_Polyhedron_remove_space_dimensions (pol, dims, dim - gdim);
1073 XDELETEVEC (dims);
1076 /* Add the constraints on the parameters to the SCoP context. */
1078 ppl_Pointset_Powerset_C_Polyhedron_t constraints_ps;
1080 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1081 (&constraints_ps, pol);
1082 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1083 (SCOP_CONTEXT (scop), constraints_ps);
1084 ppl_delete_Pointset_Powerset_C_Polyhedron (constraints_ps);
1087 ppl_delete_Polyhedron (pol);
1088 ppl_delete_Linear_Expression (nb_iters_le);
1089 ppl_delete_Constraint (ub);
1090 mpz_clear (val);
1093 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
1094 the constraints for the surrounding loops. */
1096 static void
1097 build_loop_iteration_domains (scop_p scop, struct loop *loop,
1098 ppl_Polyhedron_t outer_ph, int nb,
1099 ppl_Pointset_Powerset_C_Polyhedron_t *domains)
1101 int i;
1102 ppl_Polyhedron_t ph;
1103 tree nb_iters = number_of_latch_executions (loop);
1104 ppl_dimension_type dim = nb + 1 + scop_nb_params (scop);
1105 sese region = SCOP_REGION (scop);
1108 ppl_const_Constraint_System_t pcs;
1109 ppl_dimension_type *map
1110 = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim);
1112 ppl_new_C_Polyhedron_from_space_dimension (&ph, dim, 0);
1113 ppl_Polyhedron_get_constraints (outer_ph, &pcs);
1114 ppl_Polyhedron_add_constraints (ph, pcs);
1116 for (i = 0; i < (int) nb; i++)
1117 map[i] = i;
1118 for (i = (int) nb; i < (int) dim - 1; i++)
1119 map[i] = i + 1;
1120 map[dim - 1] = nb;
1122 ppl_Polyhedron_map_space_dimensions (ph, map, dim);
1123 free (map);
1126 /* 0 <= loop_i */
1128 ppl_Constraint_t lb;
1129 ppl_Linear_Expression_t lb_expr;
1131 ppl_new_Linear_Expression_with_dimension (&lb_expr, dim);
1132 ppl_set_coef (lb_expr, nb, 1);
1133 ppl_new_Constraint (&lb, lb_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1134 ppl_delete_Linear_Expression (lb_expr);
1135 ppl_Polyhedron_add_constraint (ph, lb);
1136 ppl_delete_Constraint (lb);
1139 if (TREE_CODE (nb_iters) == INTEGER_CST)
1141 ppl_Constraint_t ub;
1142 ppl_Linear_Expression_t ub_expr;
1144 ppl_new_Linear_Expression_with_dimension (&ub_expr, dim);
1146 /* loop_i <= cst_nb_iters */
1147 ppl_set_coef (ub_expr, nb, -1);
1148 ppl_set_inhomogeneous_tree (ub_expr, nb_iters);
1149 ppl_new_Constraint (&ub, ub_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1150 ppl_Polyhedron_add_constraint (ph, ub);
1151 ppl_delete_Linear_Expression (ub_expr);
1152 ppl_delete_Constraint (ub);
1154 else if (!chrec_contains_undetermined (nb_iters))
1156 mpz_t one;
1157 ppl_Constraint_t ub;
1158 ppl_Linear_Expression_t ub_expr;
1159 double_int nit;
1161 mpz_init (one);
1162 mpz_set_si (one, 1);
1163 ppl_new_Linear_Expression_with_dimension (&ub_expr, dim);
1164 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
1165 scan_tree_for_params (SCOP_REGION (scop), nb_iters, ub_expr, one);
1166 mpz_clear (one);
1168 if (estimated_loop_iterations (loop, true, &nit))
1169 add_upper_bounds_from_estimated_nit (scop, nit, dim, ub_expr);
1171 /* loop_i <= expr_nb_iters */
1172 ppl_set_coef (ub_expr, nb, -1);
1173 ppl_new_Constraint (&ub, ub_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1174 ppl_Polyhedron_add_constraint (ph, ub);
1175 ppl_delete_Linear_Expression (ub_expr);
1176 ppl_delete_Constraint (ub);
1178 else
1179 gcc_unreachable ();
1181 if (loop->inner && loop_in_sese_p (loop->inner, region))
1182 build_loop_iteration_domains (scop, loop->inner, ph, nb + 1, domains);
1184 if (nb != 0
1185 && loop->next
1186 && loop_in_sese_p (loop->next, region))
1187 build_loop_iteration_domains (scop, loop->next, outer_ph, nb, domains);
1189 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1190 (&domains[loop->num], ph);
1192 ppl_delete_Polyhedron (ph);
1195 /* Returns a linear expression for tree T evaluated in PBB. */
1197 static ppl_Linear_Expression_t
1198 create_linear_expr_from_tree (poly_bb_p pbb, tree t)
1200 mpz_t one;
1201 ppl_Linear_Expression_t res;
1202 ppl_dimension_type dim;
1203 sese region = SCOP_REGION (PBB_SCOP (pbb));
1204 loop_p loop = pbb_loop (pbb);
1206 dim = pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
1207 ppl_new_Linear_Expression_with_dimension (&res, dim);
1209 t = scalar_evolution_in_region (region, loop, t);
1210 gcc_assert (!automatically_generated_chrec_p (t));
1212 mpz_init (one);
1213 mpz_set_si (one, 1);
1214 scan_tree_for_params (region, t, res, one);
1215 mpz_clear (one);
1217 return res;
1220 /* Returns the ppl constraint type from the gimple tree code CODE. */
1222 static enum ppl_enum_Constraint_Type
1223 ppl_constraint_type_from_tree_code (enum tree_code code)
1225 switch (code)
1227 /* We do not support LT and GT to be able to work with C_Polyhedron.
1228 As we work on integer polyhedron "a < b" can be expressed by
1229 "a + 1 <= b". */
1230 case LT_EXPR:
1231 case GT_EXPR:
1232 gcc_unreachable ();
1234 case LE_EXPR:
1235 return PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL;
1237 case GE_EXPR:
1238 return PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL;
1240 case EQ_EXPR:
1241 return PPL_CONSTRAINT_TYPE_EQUAL;
1243 default:
1244 gcc_unreachable ();
1248 /* Add conditional statement STMT to PS. It is evaluated in PBB and
1249 CODE is used as the comparison operator. This allows us to invert the
1250 condition or to handle inequalities. */
1252 static void
1253 add_condition_to_domain (ppl_Pointset_Powerset_C_Polyhedron_t ps, gimple stmt,
1254 poly_bb_p pbb, enum tree_code code)
1256 mpz_t v;
1257 ppl_Coefficient_t c;
1258 ppl_Linear_Expression_t left, right;
1259 ppl_Constraint_t cstr;
1260 enum ppl_enum_Constraint_Type type;
1262 left = create_linear_expr_from_tree (pbb, gimple_cond_lhs (stmt));
1263 right = create_linear_expr_from_tree (pbb, gimple_cond_rhs (stmt));
1265 /* If we have < or > expressions convert them to <= or >= by adding 1 to
1266 the left or the right side of the expression. */
1267 if (code == LT_EXPR)
1269 mpz_init (v);
1270 mpz_set_si (v, 1);
1271 ppl_new_Coefficient (&c);
1272 ppl_assign_Coefficient_from_mpz_t (c, v);
1273 ppl_Linear_Expression_add_to_inhomogeneous (left, c);
1274 ppl_delete_Coefficient (c);
1275 mpz_clear (v);
1277 code = LE_EXPR;
1279 else if (code == GT_EXPR)
1281 mpz_init (v);
1282 mpz_set_si (v, 1);
1283 ppl_new_Coefficient (&c);
1284 ppl_assign_Coefficient_from_mpz_t (c, v);
1285 ppl_Linear_Expression_add_to_inhomogeneous (right, c);
1286 ppl_delete_Coefficient (c);
1287 mpz_clear (v);
1289 code = GE_EXPR;
1292 type = ppl_constraint_type_from_tree_code (code);
1294 ppl_subtract_Linear_Expression_from_Linear_Expression (left, right);
1296 ppl_new_Constraint (&cstr, left, type);
1297 ppl_Pointset_Powerset_C_Polyhedron_add_constraint (ps, cstr);
1299 ppl_delete_Constraint (cstr);
1300 ppl_delete_Linear_Expression (left);
1301 ppl_delete_Linear_Expression (right);
1304 /* Add conditional statement STMT to pbb. CODE is used as the comparision
1305 operator. This allows us to invert the condition or to handle
1306 inequalities. */
1308 static void
1309 add_condition_to_pbb (poly_bb_p pbb, gimple stmt, enum tree_code code)
1311 if (code == NE_EXPR)
1313 ppl_Pointset_Powerset_C_Polyhedron_t left = PBB_DOMAIN (pbb);
1314 ppl_Pointset_Powerset_C_Polyhedron_t right;
1315 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1316 (&right, left);
1317 add_condition_to_domain (left, stmt, pbb, LT_EXPR);
1318 add_condition_to_domain (right, stmt, pbb, GT_EXPR);
1319 ppl_Pointset_Powerset_C_Polyhedron_upper_bound_assign (left, right);
1320 ppl_delete_Pointset_Powerset_C_Polyhedron (right);
1322 else
1323 add_condition_to_domain (PBB_DOMAIN (pbb), stmt, pbb, code);
1326 /* Add conditions to the domain of PBB. */
1328 static void
1329 add_conditions_to_domain (poly_bb_p pbb)
1331 unsigned int i;
1332 gimple stmt;
1333 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1335 if (VEC_empty (gimple, GBB_CONDITIONS (gbb)))
1336 return;
1338 for (i = 0; VEC_iterate (gimple, GBB_CONDITIONS (gbb), i, stmt); i++)
1339 switch (gimple_code (stmt))
1341 case GIMPLE_COND:
1343 enum tree_code code = gimple_cond_code (stmt);
1345 /* The conditions for ELSE-branches are inverted. */
1346 if (!VEC_index (gimple, GBB_CONDITION_CASES (gbb), i))
1347 code = invert_tree_comparison (code, false);
1349 add_condition_to_pbb (pbb, stmt, code);
1350 break;
1353 case GIMPLE_SWITCH:
1354 /* Switch statements are not supported right now - fall throught. */
1356 default:
1357 gcc_unreachable ();
1358 break;
1362 /* Structure used to pass data to dom_walk. */
1364 struct bsc
1366 VEC (gimple, heap) **conditions, **cases;
1367 sese region;
1370 /* Returns a COND_EXPR statement when BB has a single predecessor, the
1371 edge between BB and its predecessor is not a loop exit edge, and
1372 the last statement of the single predecessor is a COND_EXPR. */
1374 static gimple
1375 single_pred_cond_non_loop_exit (basic_block bb)
1377 if (single_pred_p (bb))
1379 edge e = single_pred_edge (bb);
1380 basic_block pred = e->src;
1381 gimple stmt;
1383 if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
1384 return NULL;
1386 stmt = last_stmt (pred);
1388 if (stmt && gimple_code (stmt) == GIMPLE_COND)
1389 return stmt;
1392 return NULL;
1395 /* Call-back for dom_walk executed before visiting the dominated
1396 blocks. */
1398 static void
1399 build_sese_conditions_before (struct dom_walk_data *dw_data,
1400 basic_block bb)
1402 struct bsc *data = (struct bsc *) dw_data->global_data;
1403 VEC (gimple, heap) **conditions = data->conditions;
1404 VEC (gimple, heap) **cases = data->cases;
1405 gimple_bb_p gbb;
1406 gimple stmt;
1408 if (!bb_in_sese_p (bb, data->region))
1409 return;
1411 stmt = single_pred_cond_non_loop_exit (bb);
1413 if (stmt)
1415 edge e = single_pred_edge (bb);
1417 VEC_safe_push (gimple, heap, *conditions, stmt);
1419 if (e->flags & EDGE_TRUE_VALUE)
1420 VEC_safe_push (gimple, heap, *cases, stmt);
1421 else
1422 VEC_safe_push (gimple, heap, *cases, NULL);
1425 gbb = gbb_from_bb (bb);
1427 if (gbb)
1429 GBB_CONDITIONS (gbb) = VEC_copy (gimple, heap, *conditions);
1430 GBB_CONDITION_CASES (gbb) = VEC_copy (gimple, heap, *cases);
1434 /* Call-back for dom_walk executed after visiting the dominated
1435 blocks. */
1437 static void
1438 build_sese_conditions_after (struct dom_walk_data *dw_data,
1439 basic_block bb)
1441 struct bsc *data = (struct bsc *) dw_data->global_data;
1442 VEC (gimple, heap) **conditions = data->conditions;
1443 VEC (gimple, heap) **cases = data->cases;
1445 if (!bb_in_sese_p (bb, data->region))
1446 return;
1448 if (single_pred_cond_non_loop_exit (bb))
1450 VEC_pop (gimple, *conditions);
1451 VEC_pop (gimple, *cases);
1455 /* Record all conditions in REGION. */
1457 static void
1458 build_sese_conditions (sese region)
1460 struct dom_walk_data walk_data;
1461 VEC (gimple, heap) *conditions = VEC_alloc (gimple, heap, 3);
1462 VEC (gimple, heap) *cases = VEC_alloc (gimple, heap, 3);
1463 struct bsc data;
1465 data.conditions = &conditions;
1466 data.cases = &cases;
1467 data.region = region;
1469 walk_data.dom_direction = CDI_DOMINATORS;
1470 walk_data.initialize_block_local_data = NULL;
1471 walk_data.before_dom_children = build_sese_conditions_before;
1472 walk_data.after_dom_children = build_sese_conditions_after;
1473 walk_data.global_data = &data;
1474 walk_data.block_local_data_size = 0;
1476 init_walk_dominator_tree (&walk_data);
1477 walk_dominator_tree (&walk_data, SESE_ENTRY_BB (region));
1478 fini_walk_dominator_tree (&walk_data);
1480 VEC_free (gimple, heap, conditions);
1481 VEC_free (gimple, heap, cases);
1484 /* Traverses all the GBBs of the SCOP and add their constraints to the
1485 iteration domains. */
1487 static void
1488 add_conditions_to_constraints (scop_p scop)
1490 int i;
1491 poly_bb_p pbb;
1493 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1494 add_conditions_to_domain (pbb);
1497 /* Add constraints on the possible values of parameter P from the type
1498 of P. */
1500 static void
1501 add_param_constraints (scop_p scop, ppl_Polyhedron_t context, graphite_dim_t p)
1503 ppl_Constraint_t cstr;
1504 ppl_Linear_Expression_t le;
1505 tree parameter = VEC_index (tree, SESE_PARAMS (SCOP_REGION (scop)), p);
1506 tree type = TREE_TYPE (parameter);
1507 tree lb = NULL_TREE;
1508 tree ub = NULL_TREE;
1510 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1511 lb = lower_bound_in_type (type, type);
1512 else
1513 lb = TYPE_MIN_VALUE (type);
1515 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1516 ub = upper_bound_in_type (type, type);
1517 else
1518 ub = TYPE_MAX_VALUE (type);
1520 if (lb)
1522 ppl_new_Linear_Expression_with_dimension (&le, scop_nb_params (scop));
1523 ppl_set_coef (le, p, -1);
1524 ppl_set_inhomogeneous_tree (le, lb);
1525 ppl_new_Constraint (&cstr, le, PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL);
1526 ppl_Polyhedron_add_constraint (context, cstr);
1527 ppl_delete_Linear_Expression (le);
1528 ppl_delete_Constraint (cstr);
1531 if (ub)
1533 ppl_new_Linear_Expression_with_dimension (&le, scop_nb_params (scop));
1534 ppl_set_coef (le, p, -1);
1535 ppl_set_inhomogeneous_tree (le, ub);
1536 ppl_new_Constraint (&cstr, le, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1537 ppl_Polyhedron_add_constraint (context, cstr);
1538 ppl_delete_Linear_Expression (le);
1539 ppl_delete_Constraint (cstr);
1543 /* Build the context of the SCOP. The context usually contains extra
1544 constraints that are added to the iteration domains that constrain
1545 some parameters. */
1547 static void
1548 build_scop_context (scop_p scop)
1550 ppl_Polyhedron_t context;
1551 ppl_Pointset_Powerset_C_Polyhedron_t ps;
1552 graphite_dim_t p, n = scop_nb_params (scop);
1554 ppl_new_C_Polyhedron_from_space_dimension (&context, n, 0);
1556 for (p = 0; p < n; p++)
1557 add_param_constraints (scop, context, p);
1559 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1560 (&ps, context);
1561 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1562 (SCOP_CONTEXT (scop), ps);
1564 ppl_delete_Pointset_Powerset_C_Polyhedron (ps);
1565 ppl_delete_Polyhedron (context);
1568 /* Build the iteration domains: the loops belonging to the current
1569 SCOP, and that vary for the execution of the current basic block.
1570 Returns false if there is no loop in SCOP. */
1572 static void
1573 build_scop_iteration_domain (scop_p scop)
1575 struct loop *loop;
1576 sese region = SCOP_REGION (scop);
1577 int i;
1578 ppl_Polyhedron_t ph;
1579 poly_bb_p pbb;
1580 int nb_loops = number_of_loops ();
1581 ppl_Pointset_Powerset_C_Polyhedron_t *domains
1582 = XNEWVEC (ppl_Pointset_Powerset_C_Polyhedron_t, nb_loops);
1584 for (i = 0; i < nb_loops; i++)
1585 domains[i] = NULL;
1587 ppl_new_C_Polyhedron_from_space_dimension (&ph, scop_nb_params (scop), 0);
1589 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop); i++)
1590 if (!loop_in_sese_p (loop_outer (loop), region))
1591 build_loop_iteration_domains (scop, loop, ph, 0, domains);
1593 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1594 if (domains[gbb_loop (PBB_BLACK_BOX (pbb))->num])
1595 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1596 (&PBB_DOMAIN (pbb), (ppl_const_Pointset_Powerset_C_Polyhedron_t)
1597 domains[gbb_loop (PBB_BLACK_BOX (pbb))->num]);
1598 else
1599 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1600 (&PBB_DOMAIN (pbb), ph);
1602 for (i = 0; i < nb_loops; i++)
1603 if (domains[i])
1604 ppl_delete_Pointset_Powerset_C_Polyhedron (domains[i]);
1606 ppl_delete_Polyhedron (ph);
1607 free (domains);
1610 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1611 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1612 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1613 domain. */
1615 static void
1616 pdr_add_alias_set (ppl_Polyhedron_t accesses, data_reference_p dr,
1617 ppl_dimension_type accessp_nb_dims,
1618 ppl_dimension_type dom_nb_dims)
1620 ppl_Linear_Expression_t alias;
1621 ppl_Constraint_t cstr;
1622 int alias_set_num = 0;
1623 base_alias_pair *bap = (base_alias_pair *)(dr->aux);
1625 if (bap && bap->alias_set)
1626 alias_set_num = *(bap->alias_set);
1628 ppl_new_Linear_Expression_with_dimension (&alias, accessp_nb_dims);
1630 ppl_set_coef (alias, dom_nb_dims, 1);
1631 ppl_set_inhomogeneous (alias, -alias_set_num);
1632 ppl_new_Constraint (&cstr, alias, PPL_CONSTRAINT_TYPE_EQUAL);
1633 ppl_Polyhedron_add_constraint (accesses, cstr);
1635 ppl_delete_Linear_Expression (alias);
1636 ppl_delete_Constraint (cstr);
1639 /* Add to ACCESSES polyhedron equalities defining the access functions
1640 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1641 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1642 PBB is the poly_bb_p that contains the data reference DR. */
1644 static void
1645 pdr_add_memory_accesses (ppl_Polyhedron_t accesses, data_reference_p dr,
1646 ppl_dimension_type accessp_nb_dims,
1647 ppl_dimension_type dom_nb_dims,
1648 poly_bb_p pbb)
1650 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1651 mpz_t v;
1652 scop_p scop = PBB_SCOP (pbb);
1653 sese region = SCOP_REGION (scop);
1655 mpz_init (v);
1657 for (i = 0; i < nb_subscripts; i++)
1659 ppl_Linear_Expression_t fn, access;
1660 ppl_Constraint_t cstr;
1661 ppl_dimension_type subscript = dom_nb_dims + 1 + i;
1662 tree afn = DR_ACCESS_FN (dr, nb_subscripts - 1 - i);
1664 ppl_new_Linear_Expression_with_dimension (&fn, dom_nb_dims);
1665 ppl_new_Linear_Expression_with_dimension (&access, accessp_nb_dims);
1667 mpz_set_si (v, 1);
1668 scan_tree_for_params (region, afn, fn, v);
1669 ppl_assign_Linear_Expression_from_Linear_Expression (access, fn);
1671 ppl_set_coef (access, subscript, -1);
1672 ppl_new_Constraint (&cstr, access, PPL_CONSTRAINT_TYPE_EQUAL);
1673 ppl_Polyhedron_add_constraint (accesses, cstr);
1675 ppl_delete_Linear_Expression (fn);
1676 ppl_delete_Linear_Expression (access);
1677 ppl_delete_Constraint (cstr);
1680 mpz_clear (v);
1683 /* Add constrains representing the size of the accessed data to the
1684 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1685 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1686 domain. */
1688 static void
1689 pdr_add_data_dimensions (ppl_Polyhedron_t accesses, data_reference_p dr,
1690 ppl_dimension_type accessp_nb_dims,
1691 ppl_dimension_type dom_nb_dims)
1693 tree ref = DR_REF (dr);
1694 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1696 for (i = nb_subscripts - 1; i >= 0; i--, ref = TREE_OPERAND (ref, 0))
1698 ppl_Linear_Expression_t expr;
1699 ppl_Constraint_t cstr;
1700 ppl_dimension_type subscript = dom_nb_dims + 1 + i;
1701 tree low, high;
1703 if (TREE_CODE (ref) != ARRAY_REF)
1704 break;
1706 low = array_ref_low_bound (ref);
1708 /* subscript - low >= 0 */
1709 if (host_integerp (low, 0))
1711 ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
1712 ppl_set_coef (expr, subscript, 1);
1714 ppl_set_inhomogeneous (expr, -int_cst_value (low));
1716 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1717 ppl_Polyhedron_add_constraint (accesses, cstr);
1718 ppl_delete_Linear_Expression (expr);
1719 ppl_delete_Constraint (cstr);
1722 high = array_ref_up_bound (ref);
1724 /* high - subscript >= 0 */
1725 if (high && host_integerp (high, 0)
1726 /* 1-element arrays at end of structures may extend over
1727 their declared size. */
1728 && !(array_at_struct_end_p (ref)
1729 && operand_equal_p (low, high, 0)))
1731 ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
1732 ppl_set_coef (expr, subscript, -1);
1734 ppl_set_inhomogeneous (expr, int_cst_value (high));
1736 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1737 ppl_Polyhedron_add_constraint (accesses, cstr);
1738 ppl_delete_Linear_Expression (expr);
1739 ppl_delete_Constraint (cstr);
1744 /* Build data accesses for DR in PBB. */
1746 static void
1747 build_poly_dr (data_reference_p dr, poly_bb_p pbb)
1749 ppl_Polyhedron_t accesses;
1750 ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps;
1751 ppl_dimension_type dom_nb_dims;
1752 ppl_dimension_type accessp_nb_dims;
1753 int dr_base_object_set;
1755 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb),
1756 &dom_nb_dims);
1757 accessp_nb_dims = dom_nb_dims + 1 + DR_NUM_DIMENSIONS (dr);
1759 ppl_new_C_Polyhedron_from_space_dimension (&accesses, accessp_nb_dims, 0);
1761 pdr_add_alias_set (accesses, dr, accessp_nb_dims, dom_nb_dims);
1762 pdr_add_memory_accesses (accesses, dr, accessp_nb_dims, dom_nb_dims, pbb);
1763 pdr_add_data_dimensions (accesses, dr, accessp_nb_dims, dom_nb_dims);
1765 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps,
1766 accesses);
1767 ppl_delete_Polyhedron (accesses);
1769 if (dr->aux)
1770 dr_base_object_set = ((base_alias_pair *)(dr->aux))->base_obj_set;
1772 new_poly_dr (pbb, dr_base_object_set, accesses_ps, DR_IS_READ (dr) ? PDR_READ : PDR_WRITE,
1773 dr, DR_NUM_DIMENSIONS (dr));
1776 /* Write to FILE the alias graph of data references in DIMACS format. */
1778 static inline bool
1779 write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
1780 VEC (data_reference_p, heap) *drs)
1782 int num_vertex = VEC_length (data_reference_p, drs);
1783 int edge_num = 0;
1784 data_reference_p dr1, dr2;
1785 int i, j;
1787 if (num_vertex == 0)
1788 return true;
1790 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1791 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1792 if (dr_may_alias_p (dr1, dr2))
1793 edge_num++;
1795 fprintf (file, "$\n");
1797 if (comment)
1798 fprintf (file, "c %s\n", comment);
1800 fprintf (file, "p edge %d %d\n", num_vertex, edge_num);
1802 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1803 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1804 if (dr_may_alias_p (dr1, dr2))
1805 fprintf (file, "e %d %d\n", i + 1, j + 1);
1807 return true;
1810 /* Write to FILE the alias graph of data references in DOT format. */
1812 static inline bool
1813 write_alias_graph_to_ascii_dot (FILE *file, char *comment,
1814 VEC (data_reference_p, heap) *drs)
1816 int num_vertex = VEC_length (data_reference_p, drs);
1817 data_reference_p dr1, dr2;
1818 int i, j;
1820 if (num_vertex == 0)
1821 return true;
1823 fprintf (file, "$\n");
1825 if (comment)
1826 fprintf (file, "c %s\n", comment);
1828 /* First print all the vertices. */
1829 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1830 fprintf (file, "n%d;\n", i);
1832 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1833 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1834 if (dr_may_alias_p (dr1, dr2))
1835 fprintf (file, "n%d n%d\n", i, j);
1837 return true;
1840 /* Write to FILE the alias graph of data references in ECC format. */
1842 static inline bool
1843 write_alias_graph_to_ascii_ecc (FILE *file, char *comment,
1844 VEC (data_reference_p, heap) *drs)
1846 int num_vertex = VEC_length (data_reference_p, drs);
1847 data_reference_p dr1, dr2;
1848 int i, j;
1850 if (num_vertex == 0)
1851 return true;
1853 fprintf (file, "$\n");
1855 if (comment)
1856 fprintf (file, "c %s\n", comment);
1858 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1859 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1860 if (dr_may_alias_p (dr1, dr2))
1861 fprintf (file, "%d %d\n", i, j);
1863 return true;
1866 /* Check if DR1 and DR2 are in the same object set. */
1868 static bool
1869 dr_same_base_object_p (const struct data_reference *dr1,
1870 const struct data_reference *dr2)
1872 return operand_equal_p (DR_BASE_OBJECT (dr1), DR_BASE_OBJECT (dr2), 0);
1875 /* Uses DFS component number as representative of alias-sets. Also tests for
1876 optimality by verifying if every connected component is a clique. Returns
1877 true (1) if the above test is true, and false (0) otherwise. */
1879 static int
1880 build_alias_set_optimal_p (VEC (data_reference_p, heap) *drs)
1882 int num_vertices = VEC_length (data_reference_p, drs);
1883 struct graph *g = new_graph (num_vertices);
1884 data_reference_p dr1, dr2;
1885 int i, j;
1886 int num_connected_components;
1887 int v_indx1, v_indx2, num_vertices_in_component;
1888 int *all_vertices;
1889 int *vertices;
1890 struct graph_edge *e;
1891 int this_component_is_clique;
1892 int all_components_are_cliques = 1;
1894 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1895 for (j = i+1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1896 if (dr_may_alias_p (dr1, dr2))
1898 add_edge (g, i, j);
1899 add_edge (g, j, i);
1902 all_vertices = XNEWVEC (int, num_vertices);
1903 vertices = XNEWVEC (int, num_vertices);
1904 for (i = 0; i < num_vertices; i++)
1905 all_vertices[i] = i;
1907 num_connected_components = graphds_dfs (g, all_vertices, num_vertices,
1908 NULL, true, NULL);
1909 for (i = 0; i < g->n_vertices; i++)
1911 data_reference_p dr = VEC_index (data_reference_p, drs, i);
1912 base_alias_pair *bap;
1914 if (dr->aux)
1915 bap = (base_alias_pair *)(dr->aux);
1917 bap->alias_set = XNEW (int);
1918 *(bap->alias_set) = g->vertices[i].component + 1;
1921 /* Verify if the DFS numbering results in optimal solution. */
1922 for (i = 0; i < num_connected_components; i++)
1924 num_vertices_in_component = 0;
1925 /* Get all vertices whose DFS component number is the same as i. */
1926 for (j = 0; j < num_vertices; j++)
1927 if (g->vertices[j].component == i)
1928 vertices[num_vertices_in_component++] = j;
1930 /* Now test if the vertices in 'vertices' form a clique, by testing
1931 for edges among each pair. */
1932 this_component_is_clique = 1;
1933 for (v_indx1 = 0; v_indx1 < num_vertices_in_component; v_indx1++)
1935 for (v_indx2 = v_indx1+1; v_indx2 < num_vertices_in_component; v_indx2++)
1937 /* Check if the two vertices are connected by iterating
1938 through all the edges which have one of these are source. */
1939 e = g->vertices[vertices[v_indx2]].pred;
1940 while (e)
1942 if (e->src == vertices[v_indx1])
1943 break;
1944 e = e->pred_next;
1946 if (!e)
1948 this_component_is_clique = 0;
1949 break;
1952 if (!this_component_is_clique)
1953 all_components_are_cliques = 0;
1957 free (all_vertices);
1958 free (vertices);
1959 free_graph (g);
1960 return all_components_are_cliques;
1963 /* Group each data reference in DRS with it's base object set num. */
1965 static void
1966 build_base_obj_set_for_drs (VEC (data_reference_p, heap) *drs)
1968 int num_vertex = VEC_length (data_reference_p, drs);
1969 struct graph *g = new_graph (num_vertex);
1970 data_reference_p dr1, dr2;
1971 int i, j;
1972 int *queue;
1974 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
1975 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1976 if (dr_same_base_object_p (dr1, dr2))
1978 add_edge (g, i, j);
1979 add_edge (g, j, i);
1982 queue = XNEWVEC (int, num_vertex);
1983 for (i = 0; i < num_vertex; i++)
1984 queue[i] = i;
1986 graphds_dfs (g, queue, num_vertex, NULL, true, NULL);
1988 for (i = 0; i < g->n_vertices; i++)
1990 data_reference_p dr = VEC_index (data_reference_p, drs, i);
1991 base_alias_pair *bap;
1993 if (dr->aux)
1994 bap = (base_alias_pair *)(dr->aux);
1996 bap->base_obj_set = g->vertices[i].component + 1;
1999 free (queue);
2000 free_graph (g);
2003 /* Build the data references for PBB. */
2005 static void
2006 build_pbb_drs (poly_bb_p pbb)
2008 int j;
2009 data_reference_p dr;
2010 VEC (data_reference_p, heap) *gbb_drs = GBB_DATA_REFS (PBB_BLACK_BOX (pbb));
2012 for (j = 0; VEC_iterate (data_reference_p, gbb_drs, j, dr); j++)
2013 build_poly_dr (dr, pbb);
2016 /* Dump to file the alias graphs for the data references in DRS. */
2018 static void
2019 dump_alias_graphs (VEC (data_reference_p, heap) *drs)
2021 char comment[100];
2022 FILE *file_dimacs, *file_ecc, *file_dot;
2024 file_dimacs = fopen ("/tmp/dr_alias_graph_dimacs", "ab");
2025 if (file_dimacs)
2027 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
2028 current_function_name ());
2029 write_alias_graph_to_ascii_dimacs (file_dimacs, comment, drs);
2030 fclose (file_dimacs);
2033 file_ecc = fopen ("/tmp/dr_alias_graph_ecc", "ab");
2034 if (file_ecc)
2036 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
2037 current_function_name ());
2038 write_alias_graph_to_ascii_ecc (file_ecc, comment, drs);
2039 fclose (file_ecc);
2042 file_dot = fopen ("/tmp/dr_alias_graph_dot", "ab");
2043 if (file_dot)
2045 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
2046 current_function_name ());
2047 write_alias_graph_to_ascii_dot (file_dot, comment, drs);
2048 fclose (file_dot);
2052 /* Build data references in SCOP. */
2054 static void
2055 build_scop_drs (scop_p scop)
2057 int i, j;
2058 poly_bb_p pbb;
2059 data_reference_p dr;
2060 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 3);
2062 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
2063 for (j = 0; VEC_iterate (data_reference_p,
2064 GBB_DATA_REFS (PBB_BLACK_BOX (pbb)), j, dr); j++)
2065 VEC_safe_push (data_reference_p, heap, drs, dr);
2067 for (i = 0; VEC_iterate (data_reference_p, drs, i, dr); i++)
2068 dr->aux = XNEW (base_alias_pair);
2070 if (!build_alias_set_optimal_p (drs))
2072 /* TODO: Add support when building alias set is not optimal. */
2076 build_base_obj_set_for_drs (drs);
2078 /* When debugging, enable the following code. This cannot be used
2079 in production compilers. */
2080 if (0)
2081 dump_alias_graphs (drs);
2083 VEC_free (data_reference_p, heap, drs);
2085 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
2086 build_pbb_drs (pbb);
2089 /* Return a gsi at the position of the phi node STMT. */
2091 static gimple_stmt_iterator
2092 gsi_for_phi_node (gimple stmt)
2094 gimple_stmt_iterator psi;
2095 basic_block bb = gimple_bb (stmt);
2097 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
2098 if (stmt == gsi_stmt (psi))
2099 return psi;
2101 gcc_unreachable ();
2102 return psi;
2105 /* Insert the assignment "RES := VAR" just after AFTER_STMT. */
2107 static void
2108 insert_out_of_ssa_copy (tree res, tree var, gimple after_stmt)
2110 gimple stmt;
2111 gimple_seq stmts;
2112 gimple_stmt_iterator si;
2113 gimple_stmt_iterator gsi;
2115 var = force_gimple_operand (var, &stmts, true, NULL_TREE);
2116 stmt = gimple_build_assign (res, var);
2117 if (!stmts)
2118 stmts = gimple_seq_alloc ();
2119 si = gsi_last (stmts);
2120 gsi_insert_after (&si, stmt, GSI_NEW_STMT);
2122 if (gimple_code (after_stmt) == GIMPLE_PHI)
2124 gsi = gsi_after_labels (gimple_bb (after_stmt));
2125 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2127 else
2129 gsi = gsi_for_stmt (after_stmt);
2130 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
2134 /* Insert on edge E the assignment "RES := EXPR". */
2136 static void
2137 insert_out_of_ssa_copy_on_edge (edge e, tree res, tree expr)
2139 gimple_stmt_iterator gsi;
2140 gimple_seq stmts;
2141 tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
2142 gimple stmt = gimple_build_assign (res, var);
2144 if (!stmts)
2145 stmts = gimple_seq_alloc ();
2147 gsi = gsi_last (stmts);
2148 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
2149 gsi_insert_seq_on_edge (e, stmts);
2150 gsi_commit_edge_inserts ();
2153 /* Creates a zero dimension array of the same type as VAR. */
2155 static tree
2156 create_zero_dim_array (tree var, const char *base_name)
2158 tree index_type = build_index_type (integer_zero_node);
2159 tree elt_type = TREE_TYPE (var);
2160 tree array_type = build_array_type (elt_type, index_type);
2161 tree base = create_tmp_var (array_type, base_name);
2163 add_referenced_var (base);
2165 return build4 (ARRAY_REF, elt_type, base, integer_zero_node, NULL_TREE,
2166 NULL_TREE);
2169 /* Returns true when PHI is a loop close phi node. */
2171 static bool
2172 scalar_close_phi_node_p (gimple phi)
2174 if (gimple_code (phi) != GIMPLE_PHI
2175 || !is_gimple_reg (gimple_phi_result (phi)))
2176 return false;
2178 /* Note that loop close phi nodes should have a single argument
2179 because we translated the representation into a canonical form
2180 before Graphite: see canonicalize_loop_closed_ssa_form. */
2181 return (gimple_phi_num_args (phi) == 1);
2184 /* For a definition DEF in REGION, propagates the expression EXPR in
2185 all the uses of DEF outside REGION. */
2187 static void
2188 propagate_expr_outside_region (tree def, tree expr, sese region)
2190 imm_use_iterator imm_iter;
2191 gimple use_stmt;
2192 gimple_seq stmts;
2193 bool replaced_once = false;
2195 gcc_assert (TREE_CODE (def) == SSA_NAME);
2197 expr = force_gimple_operand (unshare_expr (expr), &stmts, true,
2198 NULL_TREE);
2200 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2201 if (!is_gimple_debug (use_stmt)
2202 && !bb_in_sese_p (gimple_bb (use_stmt), region))
2204 ssa_op_iter iter;
2205 use_operand_p use_p;
2207 FOR_EACH_PHI_OR_STMT_USE (use_p, use_stmt, iter, SSA_OP_ALL_USES)
2208 if (operand_equal_p (def, USE_FROM_PTR (use_p), 0)
2209 && (replaced_once = true))
2210 replace_exp (use_p, expr);
2212 update_stmt (use_stmt);
2215 if (replaced_once)
2217 gsi_insert_seq_on_edge (SESE_ENTRY (region), stmts);
2218 gsi_commit_edge_inserts ();
2222 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2223 dimension array for it. */
2225 static void
2226 rewrite_close_phi_out_of_ssa (gimple_stmt_iterator *psi, sese region)
2228 gimple phi = gsi_stmt (*psi);
2229 tree res = gimple_phi_result (phi);
2230 tree var = SSA_NAME_VAR (res);
2231 basic_block bb = gimple_bb (phi);
2232 gimple_stmt_iterator gsi = gsi_after_labels (bb);
2233 tree arg = gimple_phi_arg_def (phi, 0);
2234 gimple stmt;
2236 /* Note that loop close phi nodes should have a single argument
2237 because we translated the representation into a canonical form
2238 before Graphite: see canonicalize_loop_closed_ssa_form. */
2239 gcc_assert (gimple_phi_num_args (phi) == 1);
2241 /* The phi node can be a non close phi node, when its argument is
2242 invariant, or a default definition. */
2243 if (is_gimple_min_invariant (arg)
2244 || SSA_NAME_IS_DEFAULT_DEF (arg))
2246 propagate_expr_outside_region (res, arg, region);
2247 gsi_next (psi);
2248 return;
2251 else if (gimple_bb (SSA_NAME_DEF_STMT (arg))->loop_father == bb->loop_father)
2253 propagate_expr_outside_region (res, arg, region);
2254 stmt = gimple_build_assign (res, arg);
2255 remove_phi_node (psi, false);
2256 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2257 SSA_NAME_DEF_STMT (res) = stmt;
2258 return;
2261 /* If res is scev analyzable and is not a scalar value, it is safe
2262 to ignore the close phi node: it will be code generated in the
2263 out of Graphite pass. */
2264 else if (scev_analyzable_p (res, region))
2266 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (res));
2267 tree scev;
2269 if (!loop_in_sese_p (loop, region))
2271 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (arg));
2272 scev = scalar_evolution_in_region (region, loop, arg);
2273 scev = compute_overall_effect_of_inner_loop (loop, scev);
2275 else
2276 scev = scalar_evolution_in_region (region, loop, res);
2278 if (tree_does_not_contain_chrecs (scev))
2279 propagate_expr_outside_region (res, scev, region);
2281 gsi_next (psi);
2282 return;
2284 else
2286 tree zero_dim_array = create_zero_dim_array (var, "Close_Phi");
2288 stmt = gimple_build_assign (res, zero_dim_array);
2290 if (TREE_CODE (arg) == SSA_NAME)
2291 insert_out_of_ssa_copy (zero_dim_array, arg, SSA_NAME_DEF_STMT (arg));
2292 else
2293 insert_out_of_ssa_copy_on_edge (single_pred_edge (bb),
2294 zero_dim_array, arg);
2297 remove_phi_node (psi, false);
2298 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2299 SSA_NAME_DEF_STMT (res) = stmt;
2302 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2303 dimension array for it. */
2305 static void
2306 rewrite_phi_out_of_ssa (gimple_stmt_iterator *psi)
2308 size_t i;
2309 gimple phi = gsi_stmt (*psi);
2310 basic_block bb = gimple_bb (phi);
2311 tree res = gimple_phi_result (phi);
2312 tree var = SSA_NAME_VAR (res);
2313 tree zero_dim_array = create_zero_dim_array (var, "phi_out_of_ssa");
2314 gimple_stmt_iterator gsi;
2315 gimple stmt;
2316 gimple_seq stmts;
2318 for (i = 0; i < gimple_phi_num_args (phi); i++)
2320 tree arg = gimple_phi_arg_def (phi, i);
2321 edge e = gimple_phi_arg_edge (phi, i);
2323 /* Avoid the insertion of code in the loop latch to please the
2324 pattern matching of the vectorizer. */
2325 if (TREE_CODE (arg) == SSA_NAME
2326 && e->src == bb->loop_father->latch)
2327 insert_out_of_ssa_copy (zero_dim_array, arg, SSA_NAME_DEF_STMT (arg));
2328 else
2329 insert_out_of_ssa_copy_on_edge (e, zero_dim_array, arg);
2332 var = force_gimple_operand (zero_dim_array, &stmts, true, NULL_TREE);
2334 if (!stmts)
2335 stmts = gimple_seq_alloc ();
2337 stmt = gimple_build_assign (res, var);
2338 remove_phi_node (psi, false);
2339 SSA_NAME_DEF_STMT (res) = stmt;
2341 gsi = gsi_last (stmts);
2342 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
2344 gsi = gsi_after_labels (bb);
2345 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2348 /* Rewrite the degenerate phi node at position PSI from the degenerate
2349 form "x = phi (y, y, ..., y)" to "x = y". */
2351 static void
2352 rewrite_degenerate_phi (gimple_stmt_iterator *psi)
2354 tree rhs;
2355 gimple stmt;
2356 gimple_stmt_iterator gsi;
2357 gimple phi = gsi_stmt (*psi);
2358 tree res = gimple_phi_result (phi);
2359 basic_block bb;
2361 if (!is_gimple_reg (res))
2363 gsi_next (psi);
2364 return;
2367 bb = gimple_bb (phi);
2368 rhs = degenerate_phi_result (phi);
2369 gcc_assert (rhs);
2371 stmt = gimple_build_assign (res, rhs);
2372 remove_phi_node (psi, false);
2373 SSA_NAME_DEF_STMT (res) = stmt;
2375 gsi = gsi_after_labels (bb);
2376 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2379 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2381 void
2382 rewrite_reductions_out_of_ssa (scop_p scop)
2384 basic_block bb;
2385 gimple_stmt_iterator psi;
2386 sese region = SCOP_REGION (scop);
2388 FOR_EACH_BB (bb)
2389 if (bb_in_sese_p (bb, region))
2390 for (psi = gsi_start_phis (bb); !gsi_end_p (psi);)
2392 gimple phi = gsi_stmt (psi);
2394 if (gimple_phi_num_args (phi) > 1
2395 && degenerate_phi_result (phi))
2396 rewrite_degenerate_phi (&psi);
2398 else if (scalar_close_phi_node_p (phi))
2399 rewrite_close_phi_out_of_ssa (&psi, region);
2401 else if (reduction_phi_p (region, &psi))
2402 rewrite_phi_out_of_ssa (&psi);
2405 update_ssa (TODO_update_ssa);
2406 #ifdef ENABLE_CHECKING
2407 verify_loop_closed_ssa (true);
2408 #endif
2411 /* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
2412 read from ZERO_DIM_ARRAY. */
2414 static void
2415 rewrite_cross_bb_scalar_dependence (tree zero_dim_array, tree def, gimple use_stmt)
2417 tree var = SSA_NAME_VAR (def);
2418 gimple name_stmt = gimple_build_assign (var, zero_dim_array);
2419 tree name = make_ssa_name (var, name_stmt);
2420 ssa_op_iter iter;
2421 use_operand_p use_p;
2422 gimple_stmt_iterator gsi;
2424 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
2426 gimple_assign_set_lhs (name_stmt, name);
2428 gsi = gsi_for_stmt (use_stmt);
2429 gsi_insert_before (&gsi, name_stmt, GSI_NEW_STMT);
2431 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, iter, SSA_OP_ALL_USES)
2432 if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
2433 replace_exp (use_p, name);
2435 update_stmt (use_stmt);
2438 /* Rewrite the scalar dependences crossing the boundary of the BB
2439 containing STMT with an array. Return true when something has been
2440 changed. */
2442 static bool
2443 rewrite_cross_bb_scalar_deps (sese region, gimple_stmt_iterator *gsi)
2445 gimple stmt = gsi_stmt (*gsi);
2446 imm_use_iterator imm_iter;
2447 tree def;
2448 basic_block def_bb;
2449 tree zero_dim_array = NULL_TREE;
2450 gimple use_stmt;
2451 bool res = false;
2453 switch (gimple_code (stmt))
2455 case GIMPLE_ASSIGN:
2456 def = gimple_assign_lhs (stmt);
2457 break;
2459 case GIMPLE_CALL:
2460 def = gimple_call_lhs (stmt);
2461 break;
2463 default:
2464 return false;
2467 if (!def
2468 || !is_gimple_reg (def))
2469 return false;
2471 if (scev_analyzable_p (def, region))
2473 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (def));
2474 tree scev = scalar_evolution_in_region (region, loop, def);
2476 if (tree_contains_chrecs (scev, NULL))
2477 return false;
2479 propagate_expr_outside_region (def, scev, region);
2480 return true;
2483 def_bb = gimple_bb (stmt);
2485 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2486 if (gimple_code (use_stmt) == GIMPLE_PHI
2487 && (res = true))
2489 gimple_stmt_iterator psi = gsi_for_stmt (use_stmt);
2491 if (scalar_close_phi_node_p (gsi_stmt (psi)))
2492 rewrite_close_phi_out_of_ssa (&psi, region);
2493 else
2494 rewrite_phi_out_of_ssa (&psi);
2497 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2498 if (gimple_code (use_stmt) != GIMPLE_PHI
2499 && def_bb != gimple_bb (use_stmt)
2500 && !is_gimple_debug (use_stmt)
2501 && (res = true))
2503 if (!zero_dim_array)
2505 zero_dim_array = create_zero_dim_array
2506 (SSA_NAME_VAR (def), "Cross_BB_scalar_dependence");
2507 insert_out_of_ssa_copy (zero_dim_array, def,
2508 SSA_NAME_DEF_STMT (def));
2509 gsi_next (gsi);
2512 rewrite_cross_bb_scalar_dependence (zero_dim_array, def, use_stmt);
2515 return res;
2518 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2520 void
2521 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop)
2523 basic_block bb;
2524 gimple_stmt_iterator psi;
2525 sese region = SCOP_REGION (scop);
2526 bool changed = false;
2528 FOR_EACH_BB (bb)
2529 if (bb_in_sese_p (bb, region))
2530 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
2531 changed |= rewrite_cross_bb_scalar_deps (region, &psi);
2533 if (changed)
2535 scev_reset_htab ();
2536 update_ssa (TODO_update_ssa);
2537 #ifdef ENABLE_CHECKING
2538 verify_loop_closed_ssa (true);
2539 #endif
2543 /* Returns the number of pbbs that are in loops contained in SCOP. */
2545 static int
2546 nb_pbbs_in_loops (scop_p scop)
2548 int i;
2549 poly_bb_p pbb;
2550 int res = 0;
2552 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
2553 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), SCOP_REGION (scop)))
2554 res++;
2556 return res;
2559 /* Return the number of data references in BB that write in
2560 memory. */
2562 static int
2563 nb_data_writes_in_bb (basic_block bb)
2565 int res = 0;
2566 gimple_stmt_iterator gsi;
2568 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2569 if (gimple_vdef (gsi_stmt (gsi)))
2570 res++;
2572 return res;
2575 /* Splits STMT out of its current BB. */
2577 static basic_block
2578 split_reduction_stmt (gimple stmt)
2580 gimple_stmt_iterator gsi;
2581 basic_block bb = gimple_bb (stmt);
2582 edge e;
2584 /* Do not split basic blocks with no writes to memory: the reduction
2585 will be the only write to memory. */
2586 if (nb_data_writes_in_bb (bb) == 0)
2587 return bb;
2589 split_block (bb, stmt);
2591 if (gsi_one_before_end_p (gsi_start_nondebug_bb (bb)))
2592 return bb;
2594 gsi = gsi_last_bb (bb);
2595 gsi_prev (&gsi);
2596 e = split_block (bb, gsi_stmt (gsi));
2598 return e->dest;
2601 /* Return true when stmt is a reduction operation. */
2603 static inline bool
2604 is_reduction_operation_p (gimple stmt)
2606 enum tree_code code;
2608 gcc_assert (is_gimple_assign (stmt));
2609 code = gimple_assign_rhs_code (stmt);
2611 return flag_associative_math
2612 && commutative_tree_code (code)
2613 && associative_tree_code (code);
2616 /* Returns true when PHI contains an argument ARG. */
2618 static bool
2619 phi_contains_arg (gimple phi, tree arg)
2621 size_t i;
2623 for (i = 0; i < gimple_phi_num_args (phi); i++)
2624 if (operand_equal_p (arg, gimple_phi_arg_def (phi, i), 0))
2625 return true;
2627 return false;
2630 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2632 static gimple
2633 follow_ssa_with_commutative_ops (tree arg, tree lhs)
2635 gimple stmt;
2637 if (TREE_CODE (arg) != SSA_NAME)
2638 return NULL;
2640 stmt = SSA_NAME_DEF_STMT (arg);
2642 if (gimple_code (stmt) == GIMPLE_NOP
2643 || gimple_code (stmt) == GIMPLE_CALL)
2644 return NULL;
2646 if (gimple_code (stmt) == GIMPLE_PHI)
2648 if (phi_contains_arg (stmt, lhs))
2649 return stmt;
2650 return NULL;
2653 if (!is_gimple_assign (stmt))
2654 return NULL;
2656 if (gimple_num_ops (stmt) == 2)
2657 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2659 if (is_reduction_operation_p (stmt))
2661 gimple res = follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2663 return res ? res :
2664 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt), lhs);
2667 return NULL;
2670 /* Detect commutative and associative scalar reductions starting at
2671 the STMT. Return the phi node of the reduction cycle, or NULL. */
2673 static gimple
2674 detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
2675 VEC (gimple, heap) **in,
2676 VEC (gimple, heap) **out)
2678 gimple phi = follow_ssa_with_commutative_ops (arg, lhs);
2680 if (!phi)
2681 return NULL;
2683 VEC_safe_push (gimple, heap, *in, stmt);
2684 VEC_safe_push (gimple, heap, *out, stmt);
2685 return phi;
2688 /* Detect commutative and associative scalar reductions starting at
2689 STMT. Return the phi node of the reduction cycle, or NULL. */
2691 static gimple
2692 detect_commutative_reduction_assign (gimple stmt, VEC (gimple, heap) **in,
2693 VEC (gimple, heap) **out)
2695 tree lhs = gimple_assign_lhs (stmt);
2697 if (gimple_num_ops (stmt) == 2)
2698 return detect_commutative_reduction_arg (lhs, stmt,
2699 gimple_assign_rhs1 (stmt),
2700 in, out);
2702 if (is_reduction_operation_p (stmt))
2704 gimple res = detect_commutative_reduction_arg (lhs, stmt,
2705 gimple_assign_rhs1 (stmt),
2706 in, out);
2707 return res ? res
2708 : detect_commutative_reduction_arg (lhs, stmt,
2709 gimple_assign_rhs2 (stmt),
2710 in, out);
2713 return NULL;
2716 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2718 static gimple
2719 follow_inital_value_to_phi (tree arg, tree lhs)
2721 gimple stmt;
2723 if (!arg || TREE_CODE (arg) != SSA_NAME)
2724 return NULL;
2726 stmt = SSA_NAME_DEF_STMT (arg);
2728 if (gimple_code (stmt) == GIMPLE_PHI
2729 && phi_contains_arg (stmt, lhs))
2730 return stmt;
2732 return NULL;
2736 /* Return the argument of the loop PHI that is the inital value coming
2737 from outside the loop. */
2739 static edge
2740 edge_initial_value_for_loop_phi (gimple phi)
2742 size_t i;
2744 for (i = 0; i < gimple_phi_num_args (phi); i++)
2746 edge e = gimple_phi_arg_edge (phi, i);
2748 if (loop_depth (e->src->loop_father)
2749 < loop_depth (e->dest->loop_father))
2750 return e;
2753 return NULL;
2756 /* Return the argument of the loop PHI that is the inital value coming
2757 from outside the loop. */
2759 static tree
2760 initial_value_for_loop_phi (gimple phi)
2762 size_t i;
2764 for (i = 0; i < gimple_phi_num_args (phi); i++)
2766 edge e = gimple_phi_arg_edge (phi, i);
2768 if (loop_depth (e->src->loop_father)
2769 < loop_depth (e->dest->loop_father))
2770 return gimple_phi_arg_def (phi, i);
2773 return NULL_TREE;
2776 /* Detect commutative and associative scalar reductions starting at
2777 the loop closed phi node STMT. Return the phi node of the
2778 reduction cycle, or NULL. */
2780 static gimple
2781 detect_commutative_reduction (gimple stmt, VEC (gimple, heap) **in,
2782 VEC (gimple, heap) **out)
2784 if (scalar_close_phi_node_p (stmt))
2786 tree arg = gimple_phi_arg_def (stmt, 0);
2787 gimple def, loop_phi;
2789 if (TREE_CODE (arg) != SSA_NAME)
2790 return NULL;
2792 /* Note that loop close phi nodes should have a single argument
2793 because we translated the representation into a canonical form
2794 before Graphite: see canonicalize_loop_closed_ssa_form. */
2795 gcc_assert (gimple_phi_num_args (stmt) == 1);
2797 def = SSA_NAME_DEF_STMT (arg);
2798 loop_phi = detect_commutative_reduction (def, in, out);
2800 if (loop_phi)
2802 tree lhs = gimple_phi_result (stmt);
2803 tree init = initial_value_for_loop_phi (loop_phi);
2804 gimple phi = follow_inital_value_to_phi (init, lhs);
2806 VEC_safe_push (gimple, heap, *in, loop_phi);
2807 VEC_safe_push (gimple, heap, *out, stmt);
2808 return phi;
2810 else
2811 return NULL;
2814 if (gimple_code (stmt) == GIMPLE_ASSIGN)
2815 return detect_commutative_reduction_assign (stmt, in, out);
2817 return NULL;
2820 /* Translate the scalar reduction statement STMT to an array RED
2821 knowing that its recursive phi node is LOOP_PHI. */
2823 static void
2824 translate_scalar_reduction_to_array_for_stmt (tree red, gimple stmt,
2825 gimple loop_phi)
2827 gimple_stmt_iterator insert_gsi = gsi_after_labels (gimple_bb (loop_phi));
2828 tree res = gimple_phi_result (loop_phi);
2829 gimple assign = gimple_build_assign (res, red);
2831 gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
2833 insert_gsi = gsi_after_labels (gimple_bb (stmt));
2834 assign = gimple_build_assign (red, gimple_assign_lhs (stmt));
2835 insert_gsi = gsi_for_stmt (stmt);
2836 gsi_insert_after (&insert_gsi, assign, GSI_SAME_STMT);
2839 /* Removes the PHI node and resets all the debug stmts that are using
2840 the PHI_RESULT. */
2842 static void
2843 remove_phi (gimple phi)
2845 imm_use_iterator imm_iter;
2846 tree def;
2847 use_operand_p use_p;
2848 gimple_stmt_iterator gsi;
2849 VEC (gimple, heap) *update = VEC_alloc (gimple, heap, 3);
2850 unsigned int i;
2851 gimple stmt;
2853 def = PHI_RESULT (phi);
2854 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
2856 stmt = USE_STMT (use_p);
2858 if (is_gimple_debug (stmt))
2860 gimple_debug_bind_reset_value (stmt);
2861 VEC_safe_push (gimple, heap, update, stmt);
2865 for (i = 0; VEC_iterate (gimple, update, i, stmt); i++)
2866 update_stmt (stmt);
2868 VEC_free (gimple, heap, update);
2870 gsi = gsi_for_phi_node (phi);
2871 remove_phi_node (&gsi, false);
2874 /* Rewrite out of SSA the reduction described by the loop phi nodes
2875 IN, and the close phi nodes OUT. IN and OUT are structured by loop
2876 levels like this:
2878 IN: stmt, loop_n, ..., loop_0
2879 OUT: stmt, close_n, ..., close_0
2881 the first element is the reduction statement, and the next elements
2882 are the loop and close phi nodes of each of the outer loops. */
2884 static void
2885 translate_scalar_reduction_to_array (VEC (gimple, heap) *in,
2886 VEC (gimple, heap) *out,
2887 sbitmap reductions)
2889 unsigned int i;
2890 gimple loop_phi;
2891 tree red = NULL_TREE;
2893 for (i = 0; VEC_iterate (gimple, in, i, loop_phi); i++)
2895 gimple close_phi = VEC_index (gimple, out, i);
2897 if (i == 0)
2899 gimple stmt = loop_phi;
2900 basic_block bb = split_reduction_stmt (stmt);
2902 SET_BIT (reductions, bb->index);
2903 gcc_assert (close_phi == loop_phi);
2905 red = create_zero_dim_array
2906 (gimple_assign_lhs (stmt), "Commutative_Associative_Reduction");
2907 translate_scalar_reduction_to_array_for_stmt
2908 (red, stmt, VEC_index (gimple, in, 1));
2909 continue;
2912 if (i == VEC_length (gimple, in) - 1)
2914 insert_out_of_ssa_copy (gimple_phi_result (close_phi), red,
2915 close_phi);
2916 insert_out_of_ssa_copy_on_edge
2917 (edge_initial_value_for_loop_phi (loop_phi),
2918 red, initial_value_for_loop_phi (loop_phi));
2921 remove_phi (loop_phi);
2922 remove_phi (close_phi);
2926 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. Returns
2927 true when something has been changed. */
2929 static bool
2930 rewrite_commutative_reductions_out_of_ssa_close_phi (gimple close_phi,
2931 sbitmap reductions)
2933 bool res;
2934 VEC (gimple, heap) *in = VEC_alloc (gimple, heap, 10);
2935 VEC (gimple, heap) *out = VEC_alloc (gimple, heap, 10);
2937 detect_commutative_reduction (close_phi, &in, &out);
2938 res = VEC_length (gimple, in) > 0;
2939 if (res)
2940 translate_scalar_reduction_to_array (in, out, reductions);
2942 VEC_free (gimple, heap, in);
2943 VEC_free (gimple, heap, out);
2944 return res;
2947 /* Rewrites all the commutative reductions from LOOP out of SSA.
2948 Returns true when something has been changed. */
2950 static bool
2951 rewrite_commutative_reductions_out_of_ssa_loop (loop_p loop,
2952 sbitmap reductions,
2953 sese region)
2955 gimple_stmt_iterator gsi;
2956 edge exit = single_exit (loop);
2957 tree res;
2958 bool changed = false;
2960 if (!exit)
2961 return false;
2963 for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
2964 if ((res = gimple_phi_result (gsi_stmt (gsi)))
2965 && is_gimple_reg (res)
2966 && !scev_analyzable_p (res, region))
2967 changed |= rewrite_commutative_reductions_out_of_ssa_close_phi
2968 (gsi_stmt (gsi), reductions);
2970 return changed;
2973 /* Rewrites all the commutative reductions from SCOP out of SSA. */
2975 void
2976 rewrite_commutative_reductions_out_of_ssa (sese region, sbitmap reductions)
2978 loop_iterator li;
2979 loop_p loop;
2980 bool changed = false;
2982 if (!flag_associative_math)
2983 return;
2985 FOR_EACH_LOOP (li, loop, 0)
2986 if (loop_in_sese_p (loop, region))
2987 changed |= rewrite_commutative_reductions_out_of_ssa_loop (loop,
2988 reductions,
2989 region);
2991 if (changed)
2993 scev_reset_htab ();
2994 gsi_commit_edge_inserts ();
2995 update_ssa (TODO_update_ssa);
2996 #ifdef ENABLE_CHECKING
2997 verify_loop_closed_ssa (true);
2998 #endif
3002 /* Java does not initialize long_long_integer_type_node. */
3003 #define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
3005 /* Can all ivs be represented by a signed integer?
3006 As CLooG might generate negative values in its expressions, signed loop ivs
3007 are required in the backend. */
3009 static bool
3010 scop_ivs_can_be_represented (scop_p scop)
3012 loop_iterator li;
3013 loop_p loop;
3014 gimple_stmt_iterator psi;
3016 FOR_EACH_LOOP (li, loop, 0)
3018 if (!loop_in_sese_p (loop, SCOP_REGION (scop)))
3019 continue;
3021 for (psi = gsi_start_phis (loop->header);
3022 !gsi_end_p (psi); gsi_next (&psi))
3024 gimple phi = gsi_stmt (psi);
3025 tree res = PHI_RESULT (phi);
3026 tree type = TREE_TYPE (res);
3028 if (TYPE_UNSIGNED (type)
3029 && TYPE_PRECISION (type) >= TYPE_PRECISION (my_long_long))
3030 return false;
3034 return true;
3037 #undef my_long_long
3039 /* Builds the polyhedral representation for a SESE region. */
3041 void
3042 build_poly_scop (scop_p scop)
3044 sese region = SCOP_REGION (scop);
3045 graphite_dim_t max_dim;
3048 /* FIXME: This restriction is needed to avoid a problem in CLooG.
3049 Once CLooG is fixed, remove this guard. Anyways, it makes no
3050 sense to optimize a scop containing only PBBs that do not belong
3051 to any loops. */
3052 if (nb_pbbs_in_loops (scop) == 0)
3053 return;
3055 if (!scop_ivs_can_be_represented (scop))
3056 return;
3058 build_sese_loop_nests (region);
3059 build_sese_conditions (region);
3060 find_scop_parameters (scop);
3062 max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
3063 if (scop_nb_params (scop) > max_dim)
3064 return;
3066 build_scop_iteration_domain (scop);
3067 build_scop_context (scop);
3069 add_conditions_to_constraints (scop);
3070 scop_to_lst (scop);
3071 build_scop_scattering (scop);
3072 build_scop_drs (scop);
3074 /* This SCoP has been translated to the polyhedral
3075 representation. */
3076 POLY_SCOP_P (scop) = true;
3078 #endif