PR target/47111
[official-gcc.git] / gcc / graphite-sese-to-poly.c
blob0cd601adf3c691b4c019b3d24fa7d64ea9c9c012
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 "tree-flow.h"
25 #include "tree-dump.h"
26 #include "cfgloop.h"
27 #include "tree-chrec.h"
28 #include "tree-data-ref.h"
29 #include "tree-scalar-evolution.h"
30 #include "domwalk.h"
31 #include "sese.h"
33 #ifdef HAVE_cloog
34 #include "ppl_c.h"
35 #include "graphite-ppl.h"
36 #include "graphite-poly.h"
37 #include "graphite-sese-to-poly.h"
39 /* Returns the index of the PHI argument defined in the outermost
40 loop. */
42 static size_t
43 phi_arg_in_outermost_loop (gimple phi)
45 loop_p loop = gimple_bb (phi)->loop_father;
46 size_t i, res = 0;
48 for (i = 0; i < gimple_phi_num_args (phi); i++)
49 if (!flow_bb_inside_loop_p (loop, gimple_phi_arg_edge (phi, i)->src))
51 loop = gimple_phi_arg_edge (phi, i)->src->loop_father;
52 res = i;
55 return res;
58 /* Removes a simple copy phi node "RES = phi (INIT, RES)" at position
59 PSI by inserting on the loop ENTRY edge assignment "RES = INIT". */
61 static void
62 remove_simple_copy_phi (gimple_stmt_iterator *psi)
64 gimple phi = gsi_stmt (*psi);
65 tree res = gimple_phi_result (phi);
66 size_t entry = phi_arg_in_outermost_loop (phi);
67 tree init = gimple_phi_arg_def (phi, entry);
68 gimple stmt = gimple_build_assign (res, init);
69 edge e = gimple_phi_arg_edge (phi, entry);
71 remove_phi_node (psi, false);
72 gsi_insert_on_edge_immediate (e, stmt);
73 SSA_NAME_DEF_STMT (res) = stmt;
76 /* Removes an invariant phi node at position PSI by inserting on the
77 loop ENTRY edge the assignment RES = INIT. */
79 static void
80 remove_invariant_phi (sese region, gimple_stmt_iterator *psi)
82 gimple phi = gsi_stmt (*psi);
83 loop_p loop = loop_containing_stmt (phi);
84 tree res = gimple_phi_result (phi);
85 tree scev = scalar_evolution_in_region (region, loop, res);
86 size_t entry = phi_arg_in_outermost_loop (phi);
87 edge e = gimple_phi_arg_edge (phi, entry);
88 tree var;
89 gimple stmt;
90 gimple_seq stmts;
91 gimple_stmt_iterator gsi;
93 if (tree_contains_chrecs (scev, NULL))
94 scev = gimple_phi_arg_def (phi, entry);
96 var = force_gimple_operand (scev, &stmts, true, NULL_TREE);
97 stmt = gimple_build_assign (res, var);
98 remove_phi_node (psi, false);
100 if (!stmts)
101 stmts = gimple_seq_alloc ();
103 gsi = gsi_last (stmts);
104 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
105 gsi_insert_seq_on_edge (e, stmts);
106 gsi_commit_edge_inserts ();
107 SSA_NAME_DEF_STMT (res) = stmt;
110 /* Returns true when the phi node at PSI is of the form "a = phi (a, x)". */
112 static inline bool
113 simple_copy_phi_p (gimple phi)
115 tree res;
117 if (gimple_phi_num_args (phi) != 2)
118 return false;
120 res = gimple_phi_result (phi);
121 return (res == gimple_phi_arg_def (phi, 0)
122 || res == gimple_phi_arg_def (phi, 1));
125 /* Returns true when the phi node at position PSI is a reduction phi
126 node in REGION. Otherwise moves the pointer PSI to the next phi to
127 be considered. */
129 static bool
130 reduction_phi_p (sese region, gimple_stmt_iterator *psi)
132 loop_p loop;
133 gimple phi = gsi_stmt (*psi);
134 tree res = gimple_phi_result (phi);
136 loop = loop_containing_stmt (phi);
138 if (simple_copy_phi_p (phi))
140 /* PRE introduces phi nodes like these, for an example,
141 see id-5.f in the fortran graphite testsuite:
143 # prephitmp.85_265 = PHI <prephitmp.85_258(33), prephitmp.85_265(18)>
145 remove_simple_copy_phi (psi);
146 return false;
149 if (scev_analyzable_p (res, region))
151 tree scev = scalar_evolution_in_region (region, loop, res);
153 if (evolution_function_is_invariant_p (scev, loop->num))
154 remove_invariant_phi (region, psi);
155 else
156 gsi_next (psi);
158 return false;
161 /* All the other cases are considered reductions. */
162 return true;
165 /* Store the GRAPHITE representation of BB. */
167 static gimple_bb_p
168 new_gimple_bb (basic_block bb, VEC (data_reference_p, heap) *drs)
170 struct gimple_bb *gbb;
172 gbb = XNEW (struct gimple_bb);
173 bb->aux = gbb;
174 GBB_BB (gbb) = bb;
175 GBB_DATA_REFS (gbb) = drs;
176 GBB_CONDITIONS (gbb) = NULL;
177 GBB_CONDITION_CASES (gbb) = NULL;
179 return gbb;
182 static void
183 free_data_refs_aux (VEC (data_reference_p, heap) *datarefs)
185 unsigned int i;
186 struct data_reference *dr;
188 FOR_EACH_VEC_ELT (data_reference_p, datarefs, i, dr)
189 if (dr->aux)
191 base_alias_pair *bap = (base_alias_pair *)(dr->aux);
193 if (bap->alias_set)
194 free (bap->alias_set);
196 free (bap);
197 dr->aux = NULL;
200 /* Frees GBB. */
202 static void
203 free_gimple_bb (struct gimple_bb *gbb)
205 free_data_refs_aux (GBB_DATA_REFS (gbb));
206 free_data_refs (GBB_DATA_REFS (gbb));
208 VEC_free (gimple, heap, GBB_CONDITIONS (gbb));
209 VEC_free (gimple, heap, GBB_CONDITION_CASES (gbb));
210 GBB_BB (gbb)->aux = 0;
211 XDELETE (gbb);
214 /* Deletes all gimple bbs in SCOP. */
216 static void
217 remove_gbbs_in_scop (scop_p scop)
219 int i;
220 poly_bb_p pbb;
222 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
223 free_gimple_bb (PBB_BLACK_BOX (pbb));
226 /* Deletes all scops in SCOPS. */
228 void
229 free_scops (VEC (scop_p, heap) *scops)
231 int i;
232 scop_p scop;
234 FOR_EACH_VEC_ELT (scop_p, scops, i, scop)
236 remove_gbbs_in_scop (scop);
237 free_sese (SCOP_REGION (scop));
238 free_scop (scop);
241 VEC_free (scop_p, heap, scops);
244 /* Generates a polyhedral black box only if the bb contains interesting
245 information. */
247 static gimple_bb_p
248 try_generate_gimple_bb (scop_p scop, basic_block bb)
250 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
251 loop_p nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
252 gimple_stmt_iterator gsi;
254 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
256 gimple stmt = gsi_stmt (gsi);
257 if (!is_gimple_debug (stmt))
258 graphite_find_data_references_in_stmt (nest, stmt, &drs);
261 return new_gimple_bb (bb, drs);
264 /* Returns true if all predecessors of BB, that are not dominated by BB, are
265 marked in MAP. The predecessors dominated by BB are loop latches and will
266 be handled after BB. */
268 static bool
269 all_non_dominated_preds_marked_p (basic_block bb, sbitmap map)
271 edge e;
272 edge_iterator ei;
274 FOR_EACH_EDGE (e, ei, bb->preds)
275 if (!TEST_BIT (map, e->src->index)
276 && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
277 return false;
279 return true;
282 /* Compare the depth of two basic_block's P1 and P2. */
284 static int
285 compare_bb_depths (const void *p1, const void *p2)
287 const_basic_block const bb1 = *(const_basic_block const*)p1;
288 const_basic_block const bb2 = *(const_basic_block const*)p2;
289 int d1 = loop_depth (bb1->loop_father);
290 int d2 = loop_depth (bb2->loop_father);
292 if (d1 < d2)
293 return 1;
295 if (d1 > d2)
296 return -1;
298 return 0;
301 /* Sort the basic blocks from DOM such that the first are the ones at
302 a deepest loop level. */
304 static void
305 graphite_sort_dominated_info (VEC (basic_block, heap) *dom)
307 VEC_qsort (basic_block, dom, compare_bb_depths);
310 /* Recursive helper function for build_scops_bbs. */
312 static void
313 build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
315 sese region = SCOP_REGION (scop);
316 VEC (basic_block, heap) *dom;
317 poly_bb_p pbb;
319 if (TEST_BIT (visited, bb->index)
320 || !bb_in_sese_p (bb, region))
321 return;
323 pbb = new_poly_bb (scop, try_generate_gimple_bb (scop, bb));
324 VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
325 SET_BIT (visited, bb->index);
327 dom = get_dominated_by (CDI_DOMINATORS, bb);
329 if (dom == NULL)
330 return;
332 graphite_sort_dominated_info (dom);
334 while (!VEC_empty (basic_block, dom))
336 int i;
337 basic_block dom_bb;
339 FOR_EACH_VEC_ELT (basic_block, dom, i, dom_bb)
340 if (all_non_dominated_preds_marked_p (dom_bb, visited))
342 build_scop_bbs_1 (scop, visited, dom_bb);
343 VEC_unordered_remove (basic_block, dom, i);
344 break;
348 VEC_free (basic_block, heap, dom);
351 /* Gather the basic blocks belonging to the SCOP. */
353 static void
354 build_scop_bbs (scop_p scop)
356 sbitmap visited = sbitmap_alloc (last_basic_block);
357 sese region = SCOP_REGION (scop);
359 sbitmap_zero (visited);
360 build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region));
361 sbitmap_free (visited);
364 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
365 We generate SCATTERING_DIMENSIONS scattering dimensions.
367 CLooG 0.15.0 and previous versions require, that all
368 scattering functions of one CloogProgram have the same number of
369 scattering dimensions, therefore we allow to specify it. This
370 should be removed in future versions of CLooG.
372 The scattering polyhedron consists of these dimensions: scattering,
373 loop_iterators, parameters.
375 Example:
377 | scattering_dimensions = 5
378 | used_scattering_dimensions = 3
379 | nb_iterators = 1
380 | scop_nb_params = 2
382 | Schedule:
384 | 4 5
386 | Scattering polyhedron:
388 | scattering: {s1, s2, s3, s4, s5}
389 | loop_iterators: {i}
390 | parameters: {p1, p2}
392 | s1 s2 s3 s4 s5 i p1 p2 1
393 | 1 0 0 0 0 0 0 0 -4 = 0
394 | 0 1 0 0 0 -1 0 0 0 = 0
395 | 0 0 1 0 0 0 0 0 -5 = 0 */
397 static void
398 build_pbb_scattering_polyhedrons (ppl_Linear_Expression_t static_schedule,
399 poly_bb_p pbb, int scattering_dimensions)
401 int i;
402 scop_p scop = PBB_SCOP (pbb);
403 int nb_iterators = pbb_dim_iter_domain (pbb);
404 int used_scattering_dimensions = nb_iterators * 2 + 1;
405 int nb_params = scop_nb_params (scop);
406 ppl_Coefficient_t c;
407 ppl_dimension_type dim = scattering_dimensions + nb_iterators + nb_params;
408 mpz_t v;
410 gcc_assert (scattering_dimensions >= used_scattering_dimensions);
412 mpz_init (v);
413 ppl_new_Coefficient (&c);
414 PBB_TRANSFORMED (pbb) = poly_scattering_new ();
415 ppl_new_C_Polyhedron_from_space_dimension
416 (&PBB_TRANSFORMED_SCATTERING (pbb), dim, 0);
418 PBB_NB_SCATTERING_TRANSFORM (pbb) = scattering_dimensions;
420 for (i = 0; i < scattering_dimensions; i++)
422 ppl_Constraint_t cstr;
423 ppl_Linear_Expression_t expr;
425 ppl_new_Linear_Expression_with_dimension (&expr, dim);
426 mpz_set_si (v, 1);
427 ppl_assign_Coefficient_from_mpz_t (c, v);
428 ppl_Linear_Expression_add_to_coefficient (expr, i, c);
430 /* Textual order inside this loop. */
431 if ((i % 2) == 0)
433 ppl_Linear_Expression_coefficient (static_schedule, i / 2, c);
434 ppl_Coefficient_to_mpz_t (c, v);
435 mpz_neg (v, v);
436 ppl_assign_Coefficient_from_mpz_t (c, v);
437 ppl_Linear_Expression_add_to_inhomogeneous (expr, c);
440 /* Iterations of this loop. */
441 else /* if ((i % 2) == 1) */
443 int loop = (i - 1) / 2;
445 mpz_set_si (v, -1);
446 ppl_assign_Coefficient_from_mpz_t (c, v);
447 ppl_Linear_Expression_add_to_coefficient
448 (expr, scattering_dimensions + loop, c);
451 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
452 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb), cstr);
453 ppl_delete_Linear_Expression (expr);
454 ppl_delete_Constraint (cstr);
457 mpz_clear (v);
458 ppl_delete_Coefficient (c);
460 PBB_ORIGINAL (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
463 /* Build for BB the static schedule.
465 The static schedule is a Dewey numbering of the abstract syntax
466 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
468 The following example informally defines the static schedule:
471 for (i: ...)
473 for (j: ...)
479 for (k: ...)
487 Static schedules for A to F:
489 DEPTH
490 0 1 2
492 B 1 0 0
493 C 1 0 1
494 D 1 1 0
495 E 1 1 1
499 static void
500 build_scop_scattering (scop_p scop)
502 int i;
503 poly_bb_p pbb;
504 gimple_bb_p previous_gbb = NULL;
505 ppl_Linear_Expression_t static_schedule;
506 ppl_Coefficient_t c;
507 mpz_t v;
509 mpz_init (v);
510 ppl_new_Coefficient (&c);
511 ppl_new_Linear_Expression (&static_schedule);
513 /* We have to start schedules at 0 on the first component and
514 because we cannot compare_prefix_loops against a previous loop,
515 prefix will be equal to zero, and that index will be
516 incremented before copying. */
517 mpz_set_si (v, -1);
518 ppl_assign_Coefficient_from_mpz_t (c, v);
519 ppl_Linear_Expression_add_to_coefficient (static_schedule, 0, c);
521 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
523 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
524 ppl_Linear_Expression_t common;
525 int prefix;
526 int nb_scat_dims = pbb_dim_iter_domain (pbb) * 2 + 1;
528 if (previous_gbb)
529 prefix = nb_common_loops (SCOP_REGION (scop), previous_gbb, gbb);
530 else
531 prefix = 0;
533 previous_gbb = gbb;
534 ppl_new_Linear_Expression_with_dimension (&common, prefix + 1);
535 ppl_assign_Linear_Expression_from_Linear_Expression (common,
536 static_schedule);
538 mpz_set_si (v, 1);
539 ppl_assign_Coefficient_from_mpz_t (c, v);
540 ppl_Linear_Expression_add_to_coefficient (common, prefix, c);
541 ppl_assign_Linear_Expression_from_Linear_Expression (static_schedule,
542 common);
544 build_pbb_scattering_polyhedrons (common, pbb, nb_scat_dims);
546 ppl_delete_Linear_Expression (common);
549 mpz_clear (v);
550 ppl_delete_Coefficient (c);
551 ppl_delete_Linear_Expression (static_schedule);
554 /* Add the value K to the dimension D of the linear expression EXPR. */
556 static void
557 add_value_to_dim (ppl_dimension_type d, ppl_Linear_Expression_t expr,
558 mpz_t k)
560 mpz_t val;
561 ppl_Coefficient_t coef;
563 ppl_new_Coefficient (&coef);
564 ppl_Linear_Expression_coefficient (expr, d, coef);
565 mpz_init (val);
566 ppl_Coefficient_to_mpz_t (coef, val);
568 mpz_add (val, val, k);
570 ppl_assign_Coefficient_from_mpz_t (coef, val);
571 ppl_Linear_Expression_add_to_coefficient (expr, d, coef);
572 mpz_clear (val);
573 ppl_delete_Coefficient (coef);
576 /* In the context of scop S, scan E, the right hand side of a scalar
577 evolution function in loop VAR, and translate it to a linear
578 expression EXPR. */
580 static void
581 scan_tree_for_params_right_scev (sese s, tree e, int var,
582 ppl_Linear_Expression_t expr)
584 if (expr)
586 loop_p loop = get_loop (var);
587 ppl_dimension_type l = sese_loop_depth (s, loop) - 1;
588 mpz_t val;
590 /* Scalar evolutions should happen in the sese region. */
591 gcc_assert (sese_loop_depth (s, loop) > 0);
593 /* We can not deal with parametric strides like:
595 | p = parameter;
597 | for i:
598 | a [i * p] = ... */
599 gcc_assert (TREE_CODE (e) == INTEGER_CST);
601 mpz_init (val);
602 tree_int_to_gmp (e, val);
603 add_value_to_dim (l, expr, val);
604 mpz_clear (val);
608 /* Scan the integer constant CST, and add it to the inhomogeneous part of the
609 linear expression EXPR. K is the multiplier of the constant. */
611 static void
612 scan_tree_for_params_int (tree cst, ppl_Linear_Expression_t expr, mpz_t k)
614 mpz_t val;
615 ppl_Coefficient_t coef;
616 tree type = TREE_TYPE (cst);
618 mpz_init (val);
620 /* Necessary to not get "-1 = 2^n - 1". */
621 mpz_set_double_int (val, double_int_sext (tree_to_double_int (cst),
622 TYPE_PRECISION (type)), false);
624 mpz_mul (val, val, k);
625 ppl_new_Coefficient (&coef);
626 ppl_assign_Coefficient_from_mpz_t (coef, val);
627 ppl_Linear_Expression_add_to_inhomogeneous (expr, coef);
628 mpz_clear (val);
629 ppl_delete_Coefficient (coef);
632 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
633 Otherwise returns -1. */
635 static inline int
636 parameter_index_in_region_1 (tree name, sese region)
638 int i;
639 tree p;
641 gcc_assert (TREE_CODE (name) == SSA_NAME);
643 FOR_EACH_VEC_ELT (tree, SESE_PARAMS (region), i, p)
644 if (p == name)
645 return i;
647 return -1;
650 /* When the parameter NAME is in REGION, returns its index in
651 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
652 and returns the index of NAME. */
654 static int
655 parameter_index_in_region (tree name, sese region)
657 int i;
659 gcc_assert (TREE_CODE (name) == SSA_NAME);
661 i = parameter_index_in_region_1 (name, region);
662 if (i != -1)
663 return i;
665 gcc_assert (SESE_ADD_PARAMS (region));
667 i = VEC_length (tree, SESE_PARAMS (region));
668 VEC_safe_push (tree, heap, SESE_PARAMS (region), name);
669 return i;
672 /* In the context of sese S, scan the expression E and translate it to
673 a linear expression C. When parsing a symbolic multiplication, K
674 represents the constant multiplier of an expression containing
675 parameters. */
677 static void
678 scan_tree_for_params (sese s, tree e, ppl_Linear_Expression_t c,
679 mpz_t k)
681 if (e == chrec_dont_know)
682 return;
684 switch (TREE_CODE (e))
686 case POLYNOMIAL_CHREC:
687 scan_tree_for_params_right_scev (s, CHREC_RIGHT (e),
688 CHREC_VARIABLE (e), c);
689 scan_tree_for_params (s, CHREC_LEFT (e), c, k);
690 break;
692 case MULT_EXPR:
693 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
695 if (c)
697 mpz_t val;
698 gcc_assert (host_integerp (TREE_OPERAND (e, 1), 0));
699 mpz_init (val);
700 tree_int_to_gmp (TREE_OPERAND (e, 1), val);
701 mpz_mul (val, val, k);
702 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, val);
703 mpz_clear (val);
705 else
706 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
708 else
710 if (c)
712 mpz_t val;
713 gcc_assert (host_integerp (TREE_OPERAND (e, 0), 0));
714 mpz_init (val);
715 tree_int_to_gmp (TREE_OPERAND (e, 0), val);
716 mpz_mul (val, val, k);
717 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, val);
718 mpz_clear (val);
720 else
721 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, k);
723 break;
725 case PLUS_EXPR:
726 case POINTER_PLUS_EXPR:
727 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
728 scan_tree_for_params (s, TREE_OPERAND (e, 1), c, k);
729 break;
731 case MINUS_EXPR:
733 ppl_Linear_Expression_t tmp_expr = NULL;
735 if (c)
737 ppl_dimension_type dim;
738 ppl_Linear_Expression_space_dimension (c, &dim);
739 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
742 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
743 scan_tree_for_params (s, TREE_OPERAND (e, 1), tmp_expr, k);
745 if (c)
747 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
748 tmp_expr);
749 ppl_delete_Linear_Expression (tmp_expr);
752 break;
755 case NEGATE_EXPR:
757 ppl_Linear_Expression_t tmp_expr = NULL;
759 if (c)
761 ppl_dimension_type dim;
762 ppl_Linear_Expression_space_dimension (c, &dim);
763 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
766 scan_tree_for_params (s, TREE_OPERAND (e, 0), tmp_expr, k);
768 if (c)
770 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
771 tmp_expr);
772 ppl_delete_Linear_Expression (tmp_expr);
775 break;
778 case BIT_NOT_EXPR:
780 ppl_Linear_Expression_t tmp_expr = NULL;
782 if (c)
784 ppl_dimension_type dim;
785 ppl_Linear_Expression_space_dimension (c, &dim);
786 ppl_new_Linear_Expression_with_dimension (&tmp_expr, dim);
789 scan_tree_for_params (s, TREE_OPERAND (e, 0), tmp_expr, k);
791 if (c)
793 ppl_Coefficient_t coef;
794 mpz_t minus_one;
796 ppl_subtract_Linear_Expression_from_Linear_Expression (c,
797 tmp_expr);
798 ppl_delete_Linear_Expression (tmp_expr);
799 mpz_init (minus_one);
800 mpz_set_si (minus_one, -1);
801 ppl_new_Coefficient_from_mpz_t (&coef, minus_one);
802 ppl_Linear_Expression_add_to_inhomogeneous (c, coef);
803 mpz_clear (minus_one);
804 ppl_delete_Coefficient (coef);
807 break;
810 case SSA_NAME:
812 ppl_dimension_type p = parameter_index_in_region (e, s);
814 if (c)
816 ppl_dimension_type dim;
817 ppl_Linear_Expression_space_dimension (c, &dim);
818 p += dim - sese_nb_params (s);
819 add_value_to_dim (p, c, k);
821 break;
824 case INTEGER_CST:
825 if (c)
826 scan_tree_for_params_int (e, c, k);
827 break;
829 CASE_CONVERT:
830 case NON_LVALUE_EXPR:
831 scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
832 break;
834 default:
835 gcc_unreachable ();
836 break;
840 /* Find parameters with respect to REGION in BB. We are looking in memory
841 access functions, conditions and loop bounds. */
843 static void
844 find_params_in_bb (sese region, gimple_bb_p gbb)
846 int i;
847 unsigned j;
848 data_reference_p dr;
849 gimple stmt;
850 loop_p loop = GBB_BB (gbb)->loop_father;
851 mpz_t one;
853 mpz_init (one);
854 mpz_set_si (one, 1);
856 /* Find parameters in the access functions of data references. */
857 FOR_EACH_VEC_ELT (data_reference_p, GBB_DATA_REFS (gbb), i, dr)
858 for (j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
859 scan_tree_for_params (region, DR_ACCESS_FN (dr, j), NULL, one);
861 /* Find parameters in conditional statements. */
862 FOR_EACH_VEC_ELT (gimple, GBB_CONDITIONS (gbb), i, stmt)
864 tree lhs = scalar_evolution_in_region (region, loop,
865 gimple_cond_lhs (stmt));
866 tree rhs = scalar_evolution_in_region (region, loop,
867 gimple_cond_rhs (stmt));
869 scan_tree_for_params (region, lhs, NULL, one);
870 scan_tree_for_params (region, rhs, NULL, one);
873 mpz_clear (one);
876 /* Record the parameters used in the SCOP. A variable is a parameter
877 in a scop if it does not vary during the execution of that scop. */
879 static void
880 find_scop_parameters (scop_p scop)
882 poly_bb_p pbb;
883 unsigned i;
884 sese region = SCOP_REGION (scop);
885 struct loop *loop;
886 mpz_t one;
888 mpz_init (one);
889 mpz_set_si (one, 1);
891 /* Find the parameters used in the loop bounds. */
892 FOR_EACH_VEC_ELT (loop_p, SESE_LOOP_NEST (region), i, loop)
894 tree nb_iters = number_of_latch_executions (loop);
896 if (!chrec_contains_symbols (nb_iters))
897 continue;
899 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
900 scan_tree_for_params (region, nb_iters, NULL, one);
903 mpz_clear (one);
905 /* Find the parameters used in data accesses. */
906 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
907 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
909 scop_set_nb_params (scop, sese_nb_params (region));
910 SESE_ADD_PARAMS (region) = false;
912 ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension
913 (&SCOP_CONTEXT (scop), scop_nb_params (scop), 0);
916 /* Insert in the SCOP context constraints from the estimation of the
917 number of iterations. UB_EXPR is a linear expression describing
918 the number of iterations in a loop. This expression is bounded by
919 the estimation NIT. */
921 static void
922 add_upper_bounds_from_estimated_nit (scop_p scop, double_int nit,
923 ppl_dimension_type dim,
924 ppl_Linear_Expression_t ub_expr)
926 mpz_t val;
927 ppl_Linear_Expression_t nb_iters_le;
928 ppl_Polyhedron_t pol;
929 ppl_Coefficient_t coef;
930 ppl_Constraint_t ub;
932 ppl_new_C_Polyhedron_from_space_dimension (&pol, dim, 0);
933 ppl_new_Linear_Expression_from_Linear_Expression (&nb_iters_le,
934 ub_expr);
936 /* Construct the negated number of last iteration in VAL. */
937 mpz_init (val);
938 mpz_set_double_int (val, nit, false);
939 mpz_sub_ui (val, val, 1);
940 mpz_neg (val, val);
942 /* NB_ITERS_LE holds the number of last iteration in
943 parametrical form. Subtract estimated number of last
944 iteration and assert that result is not positive. */
945 ppl_new_Coefficient_from_mpz_t (&coef, val);
946 ppl_Linear_Expression_add_to_inhomogeneous (nb_iters_le, coef);
947 ppl_delete_Coefficient (coef);
948 ppl_new_Constraint (&ub, nb_iters_le,
949 PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL);
950 ppl_Polyhedron_add_constraint (pol, ub);
952 /* Remove all but last GDIM dimensions from POL to obtain
953 only the constraints on the parameters. */
955 graphite_dim_t gdim = scop_nb_params (scop);
956 ppl_dimension_type *dims = XNEWVEC (ppl_dimension_type, dim - gdim);
957 graphite_dim_t i;
959 for (i = 0; i < dim - gdim; i++)
960 dims[i] = i;
962 ppl_Polyhedron_remove_space_dimensions (pol, dims, dim - gdim);
963 XDELETEVEC (dims);
966 /* Add the constraints on the parameters to the SCoP context. */
968 ppl_Pointset_Powerset_C_Polyhedron_t constraints_ps;
970 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
971 (&constraints_ps, pol);
972 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
973 (SCOP_CONTEXT (scop), constraints_ps);
974 ppl_delete_Pointset_Powerset_C_Polyhedron (constraints_ps);
977 ppl_delete_Polyhedron (pol);
978 ppl_delete_Linear_Expression (nb_iters_le);
979 ppl_delete_Constraint (ub);
980 mpz_clear (val);
983 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
984 the constraints for the surrounding loops. */
986 static void
987 build_loop_iteration_domains (scop_p scop, struct loop *loop,
988 ppl_Polyhedron_t outer_ph, int nb,
989 ppl_Pointset_Powerset_C_Polyhedron_t *domains)
991 int i;
992 ppl_Polyhedron_t ph;
993 tree nb_iters = number_of_latch_executions (loop);
994 ppl_dimension_type dim = nb + 1 + scop_nb_params (scop);
995 sese region = SCOP_REGION (scop);
998 ppl_const_Constraint_System_t pcs;
999 ppl_dimension_type *map
1000 = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim);
1002 ppl_new_C_Polyhedron_from_space_dimension (&ph, dim, 0);
1003 ppl_Polyhedron_get_constraints (outer_ph, &pcs);
1004 ppl_Polyhedron_add_constraints (ph, pcs);
1006 for (i = 0; i < (int) nb; i++)
1007 map[i] = i;
1008 for (i = (int) nb; i < (int) dim - 1; i++)
1009 map[i] = i + 1;
1010 map[dim - 1] = nb;
1012 ppl_Polyhedron_map_space_dimensions (ph, map, dim);
1013 free (map);
1016 /* 0 <= loop_i */
1018 ppl_Constraint_t lb;
1019 ppl_Linear_Expression_t lb_expr;
1021 ppl_new_Linear_Expression_with_dimension (&lb_expr, dim);
1022 ppl_set_coef (lb_expr, nb, 1);
1023 ppl_new_Constraint (&lb, lb_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1024 ppl_delete_Linear_Expression (lb_expr);
1025 ppl_Polyhedron_add_constraint (ph, lb);
1026 ppl_delete_Constraint (lb);
1029 if (TREE_CODE (nb_iters) == INTEGER_CST)
1031 ppl_Constraint_t ub;
1032 ppl_Linear_Expression_t ub_expr;
1034 ppl_new_Linear_Expression_with_dimension (&ub_expr, dim);
1036 /* loop_i <= cst_nb_iters */
1037 ppl_set_coef (ub_expr, nb, -1);
1038 ppl_set_inhomogeneous_tree (ub_expr, nb_iters);
1039 ppl_new_Constraint (&ub, ub_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1040 ppl_Polyhedron_add_constraint (ph, ub);
1041 ppl_delete_Linear_Expression (ub_expr);
1042 ppl_delete_Constraint (ub);
1044 else if (!chrec_contains_undetermined (nb_iters))
1046 mpz_t one;
1047 ppl_Constraint_t ub;
1048 ppl_Linear_Expression_t ub_expr;
1049 double_int nit;
1051 mpz_init (one);
1052 mpz_set_si (one, 1);
1053 ppl_new_Linear_Expression_with_dimension (&ub_expr, dim);
1054 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
1055 scan_tree_for_params (SCOP_REGION (scop), nb_iters, ub_expr, one);
1056 mpz_clear (one);
1058 if (estimated_loop_iterations (loop, true, &nit))
1059 add_upper_bounds_from_estimated_nit (scop, nit, dim, ub_expr);
1061 /* loop_i <= expr_nb_iters */
1062 ppl_set_coef (ub_expr, nb, -1);
1063 ppl_new_Constraint (&ub, ub_expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1064 ppl_Polyhedron_add_constraint (ph, ub);
1065 ppl_delete_Linear_Expression (ub_expr);
1066 ppl_delete_Constraint (ub);
1068 else
1069 gcc_unreachable ();
1071 if (loop->inner && loop_in_sese_p (loop->inner, region))
1072 build_loop_iteration_domains (scop, loop->inner, ph, nb + 1, domains);
1074 if (nb != 0
1075 && loop->next
1076 && loop_in_sese_p (loop->next, region))
1077 build_loop_iteration_domains (scop, loop->next, outer_ph, nb, domains);
1079 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1080 (&domains[loop->num], ph);
1082 ppl_delete_Polyhedron (ph);
1085 /* Returns a linear expression for tree T evaluated in PBB. */
1087 static ppl_Linear_Expression_t
1088 create_linear_expr_from_tree (poly_bb_p pbb, tree t)
1090 mpz_t one;
1091 ppl_Linear_Expression_t res;
1092 ppl_dimension_type dim;
1093 sese region = SCOP_REGION (PBB_SCOP (pbb));
1094 loop_p loop = pbb_loop (pbb);
1096 dim = pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
1097 ppl_new_Linear_Expression_with_dimension (&res, dim);
1099 t = scalar_evolution_in_region (region, loop, t);
1100 gcc_assert (!automatically_generated_chrec_p (t));
1102 mpz_init (one);
1103 mpz_set_si (one, 1);
1104 scan_tree_for_params (region, t, res, one);
1105 mpz_clear (one);
1107 return res;
1110 /* Returns the ppl constraint type from the gimple tree code CODE. */
1112 static enum ppl_enum_Constraint_Type
1113 ppl_constraint_type_from_tree_code (enum tree_code code)
1115 switch (code)
1117 /* We do not support LT and GT to be able to work with C_Polyhedron.
1118 As we work on integer polyhedron "a < b" can be expressed by
1119 "a + 1 <= b". */
1120 case LT_EXPR:
1121 case GT_EXPR:
1122 gcc_unreachable ();
1124 case LE_EXPR:
1125 return PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL;
1127 case GE_EXPR:
1128 return PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL;
1130 case EQ_EXPR:
1131 return PPL_CONSTRAINT_TYPE_EQUAL;
1133 default:
1134 gcc_unreachable ();
1138 /* Add conditional statement STMT to PS. It is evaluated in PBB and
1139 CODE is used as the comparison operator. This allows us to invert the
1140 condition or to handle inequalities. */
1142 static void
1143 add_condition_to_domain (ppl_Pointset_Powerset_C_Polyhedron_t ps, gimple stmt,
1144 poly_bb_p pbb, enum tree_code code)
1146 mpz_t v;
1147 ppl_Coefficient_t c;
1148 ppl_Linear_Expression_t left, right;
1149 ppl_Constraint_t cstr;
1150 enum ppl_enum_Constraint_Type type;
1152 left = create_linear_expr_from_tree (pbb, gimple_cond_lhs (stmt));
1153 right = create_linear_expr_from_tree (pbb, gimple_cond_rhs (stmt));
1155 /* If we have < or > expressions convert them to <= or >= by adding 1 to
1156 the left or the right side of the expression. */
1157 if (code == LT_EXPR)
1159 mpz_init (v);
1160 mpz_set_si (v, 1);
1161 ppl_new_Coefficient (&c);
1162 ppl_assign_Coefficient_from_mpz_t (c, v);
1163 ppl_Linear_Expression_add_to_inhomogeneous (left, c);
1164 ppl_delete_Coefficient (c);
1165 mpz_clear (v);
1167 code = LE_EXPR;
1169 else if (code == GT_EXPR)
1171 mpz_init (v);
1172 mpz_set_si (v, 1);
1173 ppl_new_Coefficient (&c);
1174 ppl_assign_Coefficient_from_mpz_t (c, v);
1175 ppl_Linear_Expression_add_to_inhomogeneous (right, c);
1176 ppl_delete_Coefficient (c);
1177 mpz_clear (v);
1179 code = GE_EXPR;
1182 type = ppl_constraint_type_from_tree_code (code);
1184 ppl_subtract_Linear_Expression_from_Linear_Expression (left, right);
1186 ppl_new_Constraint (&cstr, left, type);
1187 ppl_Pointset_Powerset_C_Polyhedron_add_constraint (ps, cstr);
1189 ppl_delete_Constraint (cstr);
1190 ppl_delete_Linear_Expression (left);
1191 ppl_delete_Linear_Expression (right);
1194 /* Add conditional statement STMT to pbb. CODE is used as the comparision
1195 operator. This allows us to invert the condition or to handle
1196 inequalities. */
1198 static void
1199 add_condition_to_pbb (poly_bb_p pbb, gimple stmt, enum tree_code code)
1201 if (code == NE_EXPR)
1203 ppl_Pointset_Powerset_C_Polyhedron_t left = PBB_DOMAIN (pbb);
1204 ppl_Pointset_Powerset_C_Polyhedron_t right;
1205 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1206 (&right, left);
1207 add_condition_to_domain (left, stmt, pbb, LT_EXPR);
1208 add_condition_to_domain (right, stmt, pbb, GT_EXPR);
1209 ppl_Pointset_Powerset_C_Polyhedron_upper_bound_assign (left, right);
1210 ppl_delete_Pointset_Powerset_C_Polyhedron (right);
1212 else
1213 add_condition_to_domain (PBB_DOMAIN (pbb), stmt, pbb, code);
1216 /* Add conditions to the domain of PBB. */
1218 static void
1219 add_conditions_to_domain (poly_bb_p pbb)
1221 unsigned int i;
1222 gimple stmt;
1223 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1225 if (VEC_empty (gimple, GBB_CONDITIONS (gbb)))
1226 return;
1228 FOR_EACH_VEC_ELT (gimple, GBB_CONDITIONS (gbb), i, stmt)
1229 switch (gimple_code (stmt))
1231 case GIMPLE_COND:
1233 enum tree_code code = gimple_cond_code (stmt);
1235 /* The conditions for ELSE-branches are inverted. */
1236 if (!VEC_index (gimple, GBB_CONDITION_CASES (gbb), i))
1237 code = invert_tree_comparison (code, false);
1239 add_condition_to_pbb (pbb, stmt, code);
1240 break;
1243 case GIMPLE_SWITCH:
1244 /* Switch statements are not supported right now - fall throught. */
1246 default:
1247 gcc_unreachable ();
1248 break;
1252 /* Traverses all the GBBs of the SCOP and add their constraints to the
1253 iteration domains. */
1255 static void
1256 add_conditions_to_constraints (scop_p scop)
1258 int i;
1259 poly_bb_p pbb;
1261 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1262 add_conditions_to_domain (pbb);
1265 /* Structure used to pass data to dom_walk. */
1267 struct bsc
1269 VEC (gimple, heap) **conditions, **cases;
1270 sese region;
1273 /* Returns a COND_EXPR statement when BB has a single predecessor, the
1274 edge between BB and its predecessor is not a loop exit edge, and
1275 the last statement of the single predecessor is a COND_EXPR. */
1277 static gimple
1278 single_pred_cond_non_loop_exit (basic_block bb)
1280 if (single_pred_p (bb))
1282 edge e = single_pred_edge (bb);
1283 basic_block pred = e->src;
1284 gimple stmt;
1286 if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
1287 return NULL;
1289 stmt = last_stmt (pred);
1291 if (stmt && gimple_code (stmt) == GIMPLE_COND)
1292 return stmt;
1295 return NULL;
1298 /* Call-back for dom_walk executed before visiting the dominated
1299 blocks. */
1301 static void
1302 build_sese_conditions_before (struct dom_walk_data *dw_data,
1303 basic_block bb)
1305 struct bsc *data = (struct bsc *) dw_data->global_data;
1306 VEC (gimple, heap) **conditions = data->conditions;
1307 VEC (gimple, heap) **cases = data->cases;
1308 gimple_bb_p gbb;
1309 gimple stmt;
1311 if (!bb_in_sese_p (bb, data->region))
1312 return;
1314 stmt = single_pred_cond_non_loop_exit (bb);
1316 if (stmt)
1318 edge e = single_pred_edge (bb);
1320 VEC_safe_push (gimple, heap, *conditions, stmt);
1322 if (e->flags & EDGE_TRUE_VALUE)
1323 VEC_safe_push (gimple, heap, *cases, stmt);
1324 else
1325 VEC_safe_push (gimple, heap, *cases, NULL);
1328 gbb = gbb_from_bb (bb);
1330 if (gbb)
1332 GBB_CONDITIONS (gbb) = VEC_copy (gimple, heap, *conditions);
1333 GBB_CONDITION_CASES (gbb) = VEC_copy (gimple, heap, *cases);
1337 /* Call-back for dom_walk executed after visiting the dominated
1338 blocks. */
1340 static void
1341 build_sese_conditions_after (struct dom_walk_data *dw_data,
1342 basic_block bb)
1344 struct bsc *data = (struct bsc *) dw_data->global_data;
1345 VEC (gimple, heap) **conditions = data->conditions;
1346 VEC (gimple, heap) **cases = data->cases;
1348 if (!bb_in_sese_p (bb, data->region))
1349 return;
1351 if (single_pred_cond_non_loop_exit (bb))
1353 VEC_pop (gimple, *conditions);
1354 VEC_pop (gimple, *cases);
1358 /* Record all conditions in REGION. */
1360 static void
1361 build_sese_conditions (sese region)
1363 struct dom_walk_data walk_data;
1364 VEC (gimple, heap) *conditions = VEC_alloc (gimple, heap, 3);
1365 VEC (gimple, heap) *cases = VEC_alloc (gimple, heap, 3);
1366 struct bsc data;
1368 data.conditions = &conditions;
1369 data.cases = &cases;
1370 data.region = region;
1372 walk_data.dom_direction = CDI_DOMINATORS;
1373 walk_data.initialize_block_local_data = NULL;
1374 walk_data.before_dom_children = build_sese_conditions_before;
1375 walk_data.after_dom_children = build_sese_conditions_after;
1376 walk_data.global_data = &data;
1377 walk_data.block_local_data_size = 0;
1379 init_walk_dominator_tree (&walk_data);
1380 walk_dominator_tree (&walk_data, SESE_ENTRY_BB (region));
1381 fini_walk_dominator_tree (&walk_data);
1383 VEC_free (gimple, heap, conditions);
1384 VEC_free (gimple, heap, cases);
1387 /* Add constraints on the possible values of parameter P from the type
1388 of P. */
1390 static void
1391 add_param_constraints (scop_p scop, ppl_Polyhedron_t context, graphite_dim_t p)
1393 ppl_Constraint_t cstr;
1394 ppl_Linear_Expression_t le;
1395 tree parameter = VEC_index (tree, SESE_PARAMS (SCOP_REGION (scop)), p);
1396 tree type = TREE_TYPE (parameter);
1397 tree lb = NULL_TREE;
1398 tree ub = NULL_TREE;
1400 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1401 lb = lower_bound_in_type (type, type);
1402 else
1403 lb = TYPE_MIN_VALUE (type);
1405 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1406 ub = upper_bound_in_type (type, type);
1407 else
1408 ub = TYPE_MAX_VALUE (type);
1410 if (lb)
1412 ppl_new_Linear_Expression_with_dimension (&le, scop_nb_params (scop));
1413 ppl_set_coef (le, p, -1);
1414 ppl_set_inhomogeneous_tree (le, lb);
1415 ppl_new_Constraint (&cstr, le, PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL);
1416 ppl_Polyhedron_add_constraint (context, cstr);
1417 ppl_delete_Linear_Expression (le);
1418 ppl_delete_Constraint (cstr);
1421 if (ub)
1423 ppl_new_Linear_Expression_with_dimension (&le, scop_nb_params (scop));
1424 ppl_set_coef (le, p, -1);
1425 ppl_set_inhomogeneous_tree (le, ub);
1426 ppl_new_Constraint (&cstr, le, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1427 ppl_Polyhedron_add_constraint (context, cstr);
1428 ppl_delete_Linear_Expression (le);
1429 ppl_delete_Constraint (cstr);
1433 /* Build the context of the SCOP. The context usually contains extra
1434 constraints that are added to the iteration domains that constrain
1435 some parameters. */
1437 static void
1438 build_scop_context (scop_p scop)
1440 ppl_Polyhedron_t context;
1441 ppl_Pointset_Powerset_C_Polyhedron_t ps;
1442 graphite_dim_t p, n = scop_nb_params (scop);
1444 ppl_new_C_Polyhedron_from_space_dimension (&context, n, 0);
1446 for (p = 0; p < n; p++)
1447 add_param_constraints (scop, context, p);
1449 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1450 (&ps, context);
1451 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign
1452 (SCOP_CONTEXT (scop), ps);
1454 ppl_delete_Pointset_Powerset_C_Polyhedron (ps);
1455 ppl_delete_Polyhedron (context);
1458 /* Build the iteration domains: the loops belonging to the current
1459 SCOP, and that vary for the execution of the current basic block.
1460 Returns false if there is no loop in SCOP. */
1462 static void
1463 build_scop_iteration_domain (scop_p scop)
1465 struct loop *loop;
1466 sese region = SCOP_REGION (scop);
1467 int i;
1468 ppl_Polyhedron_t ph;
1469 poly_bb_p pbb;
1470 int nb_loops = number_of_loops ();
1471 ppl_Pointset_Powerset_C_Polyhedron_t *domains
1472 = XNEWVEC (ppl_Pointset_Powerset_C_Polyhedron_t, nb_loops);
1474 for (i = 0; i < nb_loops; i++)
1475 domains[i] = NULL;
1477 ppl_new_C_Polyhedron_from_space_dimension (&ph, scop_nb_params (scop), 0);
1479 FOR_EACH_VEC_ELT (loop_p, SESE_LOOP_NEST (region), i, loop)
1480 if (!loop_in_sese_p (loop_outer (loop), region))
1481 build_loop_iteration_domains (scop, loop, ph, 0, domains);
1483 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1484 if (domains[gbb_loop (PBB_BLACK_BOX (pbb))->num])
1485 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
1486 (&PBB_DOMAIN (pbb), (ppl_const_Pointset_Powerset_C_Polyhedron_t)
1487 domains[gbb_loop (PBB_BLACK_BOX (pbb))->num]);
1488 else
1489 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
1490 (&PBB_DOMAIN (pbb), ph);
1492 for (i = 0; i < nb_loops; i++)
1493 if (domains[i])
1494 ppl_delete_Pointset_Powerset_C_Polyhedron (domains[i]);
1496 ppl_delete_Polyhedron (ph);
1497 free (domains);
1500 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1501 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1502 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1503 domain. */
1505 static void
1506 pdr_add_alias_set (ppl_Polyhedron_t accesses, data_reference_p dr,
1507 ppl_dimension_type accessp_nb_dims,
1508 ppl_dimension_type dom_nb_dims)
1510 ppl_Linear_Expression_t alias;
1511 ppl_Constraint_t cstr;
1512 int alias_set_num = 0;
1513 base_alias_pair *bap = (base_alias_pair *)(dr->aux);
1515 if (bap && bap->alias_set)
1516 alias_set_num = *(bap->alias_set);
1518 ppl_new_Linear_Expression_with_dimension (&alias, accessp_nb_dims);
1520 ppl_set_coef (alias, dom_nb_dims, 1);
1521 ppl_set_inhomogeneous (alias, -alias_set_num);
1522 ppl_new_Constraint (&cstr, alias, PPL_CONSTRAINT_TYPE_EQUAL);
1523 ppl_Polyhedron_add_constraint (accesses, cstr);
1525 ppl_delete_Linear_Expression (alias);
1526 ppl_delete_Constraint (cstr);
1529 /* Add to ACCESSES polyhedron equalities defining the access functions
1530 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1531 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1532 PBB is the poly_bb_p that contains the data reference DR. */
1534 static void
1535 pdr_add_memory_accesses (ppl_Polyhedron_t accesses, data_reference_p dr,
1536 ppl_dimension_type accessp_nb_dims,
1537 ppl_dimension_type dom_nb_dims,
1538 poly_bb_p pbb)
1540 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1541 mpz_t v;
1542 scop_p scop = PBB_SCOP (pbb);
1543 sese region = SCOP_REGION (scop);
1545 mpz_init (v);
1547 for (i = 0; i < nb_subscripts; i++)
1549 ppl_Linear_Expression_t fn, access;
1550 ppl_Constraint_t cstr;
1551 ppl_dimension_type subscript = dom_nb_dims + 1 + i;
1552 tree afn = DR_ACCESS_FN (dr, nb_subscripts - 1 - i);
1554 ppl_new_Linear_Expression_with_dimension (&fn, dom_nb_dims);
1555 ppl_new_Linear_Expression_with_dimension (&access, accessp_nb_dims);
1557 mpz_set_si (v, 1);
1558 scan_tree_for_params (region, afn, fn, v);
1559 ppl_assign_Linear_Expression_from_Linear_Expression (access, fn);
1561 ppl_set_coef (access, subscript, -1);
1562 ppl_new_Constraint (&cstr, access, PPL_CONSTRAINT_TYPE_EQUAL);
1563 ppl_Polyhedron_add_constraint (accesses, cstr);
1565 ppl_delete_Linear_Expression (fn);
1566 ppl_delete_Linear_Expression (access);
1567 ppl_delete_Constraint (cstr);
1570 mpz_clear (v);
1573 /* Add constrains representing the size of the accessed data to the
1574 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1575 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1576 domain. */
1578 static void
1579 pdr_add_data_dimensions (ppl_Polyhedron_t accesses, data_reference_p dr,
1580 ppl_dimension_type accessp_nb_dims,
1581 ppl_dimension_type dom_nb_dims)
1583 tree ref = DR_REF (dr);
1584 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1586 for (i = nb_subscripts - 1; i >= 0; i--, ref = TREE_OPERAND (ref, 0))
1588 ppl_Linear_Expression_t expr;
1589 ppl_Constraint_t cstr;
1590 ppl_dimension_type subscript = dom_nb_dims + 1 + i;
1591 tree low, high;
1593 if (TREE_CODE (ref) != ARRAY_REF)
1594 break;
1596 low = array_ref_low_bound (ref);
1598 /* subscript - low >= 0 */
1599 if (host_integerp (low, 0))
1601 tree minus_low;
1603 ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
1604 ppl_set_coef (expr, subscript, 1);
1606 minus_low = fold_build1 (NEGATE_EXPR, TREE_TYPE (low), low);
1607 ppl_set_inhomogeneous_tree (expr, minus_low);
1609 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1610 ppl_Polyhedron_add_constraint (accesses, cstr);
1611 ppl_delete_Linear_Expression (expr);
1612 ppl_delete_Constraint (cstr);
1615 high = array_ref_up_bound (ref);
1617 /* high - subscript >= 0 */
1618 if (high && host_integerp (high, 0)
1619 /* 1-element arrays at end of structures may extend over
1620 their declared size. */
1621 && !(array_at_struct_end_p (ref)
1622 && operand_equal_p (low, high, 0)))
1624 ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
1625 ppl_set_coef (expr, subscript, -1);
1627 ppl_set_inhomogeneous_tree (expr, high);
1629 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
1630 ppl_Polyhedron_add_constraint (accesses, cstr);
1631 ppl_delete_Linear_Expression (expr);
1632 ppl_delete_Constraint (cstr);
1637 /* Build data accesses for DR in PBB. */
1639 static void
1640 build_poly_dr (data_reference_p dr, poly_bb_p pbb)
1642 ppl_Polyhedron_t accesses;
1643 ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps;
1644 ppl_dimension_type dom_nb_dims;
1645 ppl_dimension_type accessp_nb_dims;
1646 int dr_base_object_set;
1648 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb),
1649 &dom_nb_dims);
1650 accessp_nb_dims = dom_nb_dims + 1 + DR_NUM_DIMENSIONS (dr);
1652 ppl_new_C_Polyhedron_from_space_dimension (&accesses, accessp_nb_dims, 0);
1654 pdr_add_alias_set (accesses, dr, accessp_nb_dims, dom_nb_dims);
1655 pdr_add_memory_accesses (accesses, dr, accessp_nb_dims, dom_nb_dims, pbb);
1656 pdr_add_data_dimensions (accesses, dr, accessp_nb_dims, dom_nb_dims);
1658 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps,
1659 accesses);
1660 ppl_delete_Polyhedron (accesses);
1662 gcc_assert (dr->aux);
1663 dr_base_object_set = ((base_alias_pair *)(dr->aux))->base_obj_set;
1665 new_poly_dr (pbb, dr_base_object_set, accesses_ps,
1666 DR_IS_READ (dr) ? PDR_READ : PDR_WRITE,
1667 dr, DR_NUM_DIMENSIONS (dr));
1670 /* Write to FILE the alias graph of data references in DIMACS format. */
1672 static inline bool
1673 write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
1674 VEC (data_reference_p, heap) *drs)
1676 int num_vertex = VEC_length (data_reference_p, drs);
1677 int edge_num = 0;
1678 data_reference_p dr1, dr2;
1679 int i, j;
1681 if (num_vertex == 0)
1682 return true;
1684 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
1685 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1686 if (dr_may_alias_p (dr1, dr2))
1687 edge_num++;
1689 fprintf (file, "$\n");
1691 if (comment)
1692 fprintf (file, "c %s\n", comment);
1694 fprintf (file, "p edge %d %d\n", num_vertex, edge_num);
1696 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
1697 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1698 if (dr_may_alias_p (dr1, dr2))
1699 fprintf (file, "e %d %d\n", i + 1, j + 1);
1701 return true;
1704 /* Write to FILE the alias graph of data references in DOT format. */
1706 static inline bool
1707 write_alias_graph_to_ascii_dot (FILE *file, char *comment,
1708 VEC (data_reference_p, heap) *drs)
1710 int num_vertex = VEC_length (data_reference_p, drs);
1711 data_reference_p dr1, dr2;
1712 int i, j;
1714 if (num_vertex == 0)
1715 return true;
1717 fprintf (file, "$\n");
1719 if (comment)
1720 fprintf (file, "c %s\n", comment);
1722 /* First print all the vertices. */
1723 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
1724 fprintf (file, "n%d;\n", i);
1726 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
1727 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1728 if (dr_may_alias_p (dr1, dr2))
1729 fprintf (file, "n%d n%d\n", i, j);
1731 return true;
1734 /* Write to FILE the alias graph of data references in ECC format. */
1736 static inline bool
1737 write_alias_graph_to_ascii_ecc (FILE *file, char *comment,
1738 VEC (data_reference_p, heap) *drs)
1740 int num_vertex = VEC_length (data_reference_p, drs);
1741 data_reference_p dr1, dr2;
1742 int i, j;
1744 if (num_vertex == 0)
1745 return true;
1747 fprintf (file, "$\n");
1749 if (comment)
1750 fprintf (file, "c %s\n", comment);
1752 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
1753 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1754 if (dr_may_alias_p (dr1, dr2))
1755 fprintf (file, "%d %d\n", i, j);
1757 return true;
1760 /* Check if DR1 and DR2 are in the same object set. */
1762 static bool
1763 dr_same_base_object_p (const struct data_reference *dr1,
1764 const struct data_reference *dr2)
1766 return operand_equal_p (DR_BASE_OBJECT (dr1), DR_BASE_OBJECT (dr2), 0);
1769 /* Uses DFS component number as representative of alias-sets. Also tests for
1770 optimality by verifying if every connected component is a clique. Returns
1771 true (1) if the above test is true, and false (0) otherwise. */
1773 static int
1774 build_alias_set_optimal_p (VEC (data_reference_p, heap) *drs)
1776 int num_vertices = VEC_length (data_reference_p, drs);
1777 struct graph *g = new_graph (num_vertices);
1778 data_reference_p dr1, dr2;
1779 int i, j;
1780 int num_connected_components;
1781 int v_indx1, v_indx2, num_vertices_in_component;
1782 int *all_vertices;
1783 int *vertices;
1784 struct graph_edge *e;
1785 int this_component_is_clique;
1786 int all_components_are_cliques = 1;
1788 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
1789 for (j = i+1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1790 if (dr_may_alias_p (dr1, dr2))
1792 add_edge (g, i, j);
1793 add_edge (g, j, i);
1796 all_vertices = XNEWVEC (int, num_vertices);
1797 vertices = XNEWVEC (int, num_vertices);
1798 for (i = 0; i < num_vertices; i++)
1799 all_vertices[i] = i;
1801 num_connected_components = graphds_dfs (g, all_vertices, num_vertices,
1802 NULL, true, NULL);
1803 for (i = 0; i < g->n_vertices; i++)
1805 data_reference_p dr = VEC_index (data_reference_p, drs, i);
1806 base_alias_pair *bap;
1808 gcc_assert (dr->aux);
1809 bap = (base_alias_pair *)(dr->aux);
1811 bap->alias_set = XNEW (int);
1812 *(bap->alias_set) = g->vertices[i].component + 1;
1815 /* Verify if the DFS numbering results in optimal solution. */
1816 for (i = 0; i < num_connected_components; i++)
1818 num_vertices_in_component = 0;
1819 /* Get all vertices whose DFS component number is the same as i. */
1820 for (j = 0; j < num_vertices; j++)
1821 if (g->vertices[j].component == i)
1822 vertices[num_vertices_in_component++] = j;
1824 /* Now test if the vertices in 'vertices' form a clique, by testing
1825 for edges among each pair. */
1826 this_component_is_clique = 1;
1827 for (v_indx1 = 0; v_indx1 < num_vertices_in_component; v_indx1++)
1829 for (v_indx2 = v_indx1+1; v_indx2 < num_vertices_in_component; v_indx2++)
1831 /* Check if the two vertices are connected by iterating
1832 through all the edges which have one of these are source. */
1833 e = g->vertices[vertices[v_indx2]].pred;
1834 while (e)
1836 if (e->src == vertices[v_indx1])
1837 break;
1838 e = e->pred_next;
1840 if (!e)
1842 this_component_is_clique = 0;
1843 break;
1846 if (!this_component_is_clique)
1847 all_components_are_cliques = 0;
1851 free (all_vertices);
1852 free (vertices);
1853 free_graph (g);
1854 return all_components_are_cliques;
1857 /* Group each data reference in DRS with its base object set num. */
1859 static void
1860 build_base_obj_set_for_drs (VEC (data_reference_p, heap) *drs)
1862 int num_vertex = VEC_length (data_reference_p, drs);
1863 struct graph *g = new_graph (num_vertex);
1864 data_reference_p dr1, dr2;
1865 int i, j;
1866 int *queue;
1868 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
1869 for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
1870 if (dr_same_base_object_p (dr1, dr2))
1872 add_edge (g, i, j);
1873 add_edge (g, j, i);
1876 queue = XNEWVEC (int, num_vertex);
1877 for (i = 0; i < num_vertex; i++)
1878 queue[i] = i;
1880 graphds_dfs (g, queue, num_vertex, NULL, true, NULL);
1882 for (i = 0; i < g->n_vertices; i++)
1884 data_reference_p dr = VEC_index (data_reference_p, drs, i);
1885 base_alias_pair *bap;
1887 gcc_assert (dr->aux);
1888 bap = (base_alias_pair *)(dr->aux);
1890 bap->base_obj_set = g->vertices[i].component + 1;
1893 free (queue);
1894 free_graph (g);
1897 /* Build the data references for PBB. */
1899 static void
1900 build_pbb_drs (poly_bb_p pbb)
1902 int j;
1903 data_reference_p dr;
1904 VEC (data_reference_p, heap) *gbb_drs = GBB_DATA_REFS (PBB_BLACK_BOX (pbb));
1906 FOR_EACH_VEC_ELT (data_reference_p, gbb_drs, j, dr)
1907 build_poly_dr (dr, pbb);
1910 /* Dump to file the alias graphs for the data references in DRS. */
1912 static void
1913 dump_alias_graphs (VEC (data_reference_p, heap) *drs)
1915 char comment[100];
1916 FILE *file_dimacs, *file_ecc, *file_dot;
1918 file_dimacs = fopen ("/tmp/dr_alias_graph_dimacs", "ab");
1919 if (file_dimacs)
1921 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
1922 current_function_name ());
1923 write_alias_graph_to_ascii_dimacs (file_dimacs, comment, drs);
1924 fclose (file_dimacs);
1927 file_ecc = fopen ("/tmp/dr_alias_graph_ecc", "ab");
1928 if (file_ecc)
1930 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
1931 current_function_name ());
1932 write_alias_graph_to_ascii_ecc (file_ecc, comment, drs);
1933 fclose (file_ecc);
1936 file_dot = fopen ("/tmp/dr_alias_graph_dot", "ab");
1937 if (file_dot)
1939 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
1940 current_function_name ());
1941 write_alias_graph_to_ascii_dot (file_dot, comment, drs);
1942 fclose (file_dot);
1946 /* Build data references in SCOP. */
1948 static void
1949 build_scop_drs (scop_p scop)
1951 int i, j;
1952 poly_bb_p pbb;
1953 data_reference_p dr;
1954 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 3);
1956 /* Remove all the PBBs that do not have data references: these basic
1957 blocks are not handled in the polyhedral representation. */
1958 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1959 if (VEC_empty (data_reference_p, GBB_DATA_REFS (PBB_BLACK_BOX (pbb))))
1961 free_gimple_bb (PBB_BLACK_BOX (pbb));
1962 VEC_ordered_remove (poly_bb_p, SCOP_BBS (scop), i);
1963 i--;
1966 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1967 for (j = 0; VEC_iterate (data_reference_p,
1968 GBB_DATA_REFS (PBB_BLACK_BOX (pbb)), j, dr); j++)
1969 VEC_safe_push (data_reference_p, heap, drs, dr);
1971 FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr)
1972 dr->aux = XNEW (base_alias_pair);
1974 if (!build_alias_set_optimal_p (drs))
1976 /* TODO: Add support when building alias set is not optimal. */
1980 build_base_obj_set_for_drs (drs);
1982 /* When debugging, enable the following code. This cannot be used
1983 in production compilers. */
1984 if (0)
1985 dump_alias_graphs (drs);
1987 VEC_free (data_reference_p, heap, drs);
1989 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1990 build_pbb_drs (pbb);
1993 /* Return a gsi at the position of the phi node STMT. */
1995 static gimple_stmt_iterator
1996 gsi_for_phi_node (gimple stmt)
1998 gimple_stmt_iterator psi;
1999 basic_block bb = gimple_bb (stmt);
2001 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
2002 if (stmt == gsi_stmt (psi))
2003 return psi;
2005 gcc_unreachable ();
2006 return psi;
2009 /* Analyze all the data references of STMTS and add them to the
2010 GBB_DATA_REFS vector of BB. */
2012 static void
2013 analyze_drs_in_stmts (scop_p scop, basic_block bb, VEC (gimple, heap) *stmts)
2015 loop_p nest;
2016 gimple_bb_p gbb;
2017 gimple stmt;
2018 int i;
2020 if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
2021 return;
2023 nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
2024 gbb = gbb_from_bb (bb);
2026 FOR_EACH_VEC_ELT (gimple, stmts, i, stmt)
2027 if (!is_gimple_debug (stmt))
2028 graphite_find_data_references_in_stmt (nest, stmt,
2029 &GBB_DATA_REFS (gbb));
2032 /* Insert STMT at the end of the STMTS sequence and then insert the
2033 statements from STMTS at INSERT_GSI and call analyze_drs_in_stmts
2034 on STMTS. */
2036 static void
2037 insert_stmts (scop_p scop, gimple stmt, gimple_seq stmts,
2038 gimple_stmt_iterator insert_gsi)
2040 gimple_stmt_iterator gsi;
2041 VEC (gimple, heap) *x = VEC_alloc (gimple, heap, 3);
2043 if (!stmts)
2044 stmts = gimple_seq_alloc ();
2046 gsi = gsi_last (stmts);
2047 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
2048 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2049 VEC_safe_push (gimple, heap, x, gsi_stmt (gsi));
2051 gsi_insert_seq_before (&insert_gsi, stmts, GSI_SAME_STMT);
2052 analyze_drs_in_stmts (scop, gsi_bb (insert_gsi), x);
2053 VEC_free (gimple, heap, x);
2056 /* Insert the assignment "RES := EXPR" just after AFTER_STMT. */
2058 static void
2059 insert_out_of_ssa_copy (scop_p scop, tree res, tree expr, gimple after_stmt)
2061 gimple_seq stmts;
2062 gimple_stmt_iterator si;
2063 gimple_stmt_iterator gsi;
2064 tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
2065 gimple stmt = gimple_build_assign (res, var);
2066 VEC (gimple, heap) *x = VEC_alloc (gimple, heap, 3);
2068 if (!stmts)
2069 stmts = gimple_seq_alloc ();
2070 si = gsi_last (stmts);
2071 gsi_insert_after (&si, stmt, GSI_NEW_STMT);
2072 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2073 VEC_safe_push (gimple, heap, x, gsi_stmt (gsi));
2075 if (gimple_code (after_stmt) == GIMPLE_PHI)
2077 gsi = gsi_after_labels (gimple_bb (after_stmt));
2078 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2080 else
2082 gsi = gsi_for_stmt (after_stmt);
2083 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
2086 analyze_drs_in_stmts (scop, gimple_bb (after_stmt), x);
2087 VEC_free (gimple, heap, x);
2090 /* Creates a poly_bb_p for basic_block BB from the existing PBB. */
2092 static void
2093 new_pbb_from_pbb (scop_p scop, poly_bb_p pbb, basic_block bb)
2095 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 3);
2096 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
2097 gimple_bb_p gbb1 = new_gimple_bb (bb, drs);
2098 poly_bb_p pbb1 = new_poly_bb (scop, gbb1);
2099 int index, n = VEC_length (poly_bb_p, SCOP_BBS (scop));
2101 /* The INDEX of PBB in SCOP_BBS. */
2102 for (index = 0; index < n; index++)
2103 if (VEC_index (poly_bb_p, SCOP_BBS (scop), index) == pbb)
2104 break;
2106 GBB_PBB (gbb1) = pbb1;
2107 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
2108 (&PBB_DOMAIN (pbb1), PBB_DOMAIN (pbb));
2109 GBB_CONDITIONS (gbb1) = VEC_copy (gimple, heap, GBB_CONDITIONS (gbb));
2110 GBB_CONDITION_CASES (gbb1) = VEC_copy (gimple, heap, GBB_CONDITION_CASES (gbb));
2111 VEC_safe_insert (poly_bb_p, heap, SCOP_BBS (scop), index + 1, pbb1);
2114 /* Insert on edge E the assignment "RES := EXPR". */
2116 static void
2117 insert_out_of_ssa_copy_on_edge (scop_p scop, edge e, tree res, tree expr)
2119 gimple_stmt_iterator gsi;
2120 gimple_seq stmts;
2121 tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
2122 gimple stmt = gimple_build_assign (res, var);
2123 basic_block bb;
2124 VEC (gimple, heap) *x = VEC_alloc (gimple, heap, 3);
2126 if (!stmts)
2127 stmts = gimple_seq_alloc ();
2129 gsi = gsi_last (stmts);
2130 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
2131 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2132 VEC_safe_push (gimple, heap, x, gsi_stmt (gsi));
2134 gsi_insert_seq_on_edge (e, stmts);
2135 gsi_commit_edge_inserts ();
2136 bb = gimple_bb (stmt);
2138 if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
2139 return;
2141 if (!gbb_from_bb (bb))
2142 new_pbb_from_pbb (scop, pbb_from_bb (e->src), bb);
2144 analyze_drs_in_stmts (scop, bb, x);
2145 VEC_free (gimple, heap, x);
2148 /* Creates a zero dimension array of the same type as VAR. */
2150 static tree
2151 create_zero_dim_array (tree var, const char *base_name)
2153 tree index_type = build_index_type (integer_zero_node);
2154 tree elt_type = TREE_TYPE (var);
2155 tree array_type = build_array_type (elt_type, index_type);
2156 tree base = create_tmp_var (array_type, base_name);
2158 add_referenced_var (base);
2160 return build4 (ARRAY_REF, elt_type, base, integer_zero_node, NULL_TREE,
2161 NULL_TREE);
2164 /* Returns true when PHI is a loop close phi node. */
2166 static bool
2167 scalar_close_phi_node_p (gimple phi)
2169 if (gimple_code (phi) != GIMPLE_PHI
2170 || !is_gimple_reg (gimple_phi_result (phi)))
2171 return false;
2173 /* Note that loop close phi nodes should have a single argument
2174 because we translated the representation into a canonical form
2175 before Graphite: see canonicalize_loop_closed_ssa_form. */
2176 return (gimple_phi_num_args (phi) == 1);
2179 /* For a definition DEF in REGION, propagates the expression EXPR in
2180 all the uses of DEF outside REGION. */
2182 static void
2183 propagate_expr_outside_region (tree def, tree expr, sese region)
2185 imm_use_iterator imm_iter;
2186 gimple use_stmt;
2187 gimple_seq stmts;
2188 bool replaced_once = false;
2190 gcc_assert (TREE_CODE (def) == SSA_NAME);
2192 expr = force_gimple_operand (unshare_expr (expr), &stmts, true,
2193 NULL_TREE);
2195 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2196 if (!is_gimple_debug (use_stmt)
2197 && !bb_in_sese_p (gimple_bb (use_stmt), region))
2199 ssa_op_iter iter;
2200 use_operand_p use_p;
2202 FOR_EACH_PHI_OR_STMT_USE (use_p, use_stmt, iter, SSA_OP_ALL_USES)
2203 if (operand_equal_p (def, USE_FROM_PTR (use_p), 0)
2204 && (replaced_once = true))
2205 replace_exp (use_p, expr);
2207 update_stmt (use_stmt);
2210 if (replaced_once)
2212 gsi_insert_seq_on_edge (SESE_ENTRY (region), stmts);
2213 gsi_commit_edge_inserts ();
2217 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2218 dimension array for it. */
2220 static void
2221 rewrite_close_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
2223 sese region = SCOP_REGION (scop);
2224 gimple phi = gsi_stmt (*psi);
2225 tree res = gimple_phi_result (phi);
2226 tree var = SSA_NAME_VAR (res);
2227 basic_block bb = gimple_bb (phi);
2228 gimple_stmt_iterator gsi = gsi_after_labels (bb);
2229 tree arg = gimple_phi_arg_def (phi, 0);
2230 gimple stmt;
2232 /* Note that loop close phi nodes should have a single argument
2233 because we translated the representation into a canonical form
2234 before Graphite: see canonicalize_loop_closed_ssa_form. */
2235 gcc_assert (gimple_phi_num_args (phi) == 1);
2237 /* The phi node can be a non close phi node, when its argument is
2238 invariant, or a default definition. */
2239 if (is_gimple_min_invariant (arg)
2240 || SSA_NAME_IS_DEFAULT_DEF (arg))
2242 propagate_expr_outside_region (res, arg, region);
2243 gsi_next (psi);
2244 return;
2247 else if (gimple_bb (SSA_NAME_DEF_STMT (arg))->loop_father == bb->loop_father)
2249 propagate_expr_outside_region (res, arg, region);
2250 stmt = gimple_build_assign (res, arg);
2251 remove_phi_node (psi, false);
2252 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2253 SSA_NAME_DEF_STMT (res) = stmt;
2254 return;
2257 /* If res is scev analyzable and is not a scalar value, it is safe
2258 to ignore the close phi node: it will be code generated in the
2259 out of Graphite pass. */
2260 else if (scev_analyzable_p (res, region))
2262 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (res));
2263 tree scev;
2265 if (!loop_in_sese_p (loop, region))
2267 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (arg));
2268 scev = scalar_evolution_in_region (region, loop, arg);
2269 scev = compute_overall_effect_of_inner_loop (loop, scev);
2271 else
2272 scev = scalar_evolution_in_region (region, loop, res);
2274 if (tree_does_not_contain_chrecs (scev))
2275 propagate_expr_outside_region (res, scev, region);
2277 gsi_next (psi);
2278 return;
2280 else
2282 tree zero_dim_array = create_zero_dim_array (var, "Close_Phi");
2284 stmt = gimple_build_assign (res, zero_dim_array);
2286 if (TREE_CODE (arg) == SSA_NAME)
2287 insert_out_of_ssa_copy (scop, zero_dim_array, arg,
2288 SSA_NAME_DEF_STMT (arg));
2289 else
2290 insert_out_of_ssa_copy_on_edge (scop, single_pred_edge (bb),
2291 zero_dim_array, arg);
2294 remove_phi_node (psi, false);
2295 SSA_NAME_DEF_STMT (res) = stmt;
2297 insert_stmts (scop, stmt, NULL, gsi_after_labels (bb));
2300 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2301 dimension array for it. */
2303 static void
2304 rewrite_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
2306 size_t i;
2307 gimple phi = gsi_stmt (*psi);
2308 basic_block bb = gimple_bb (phi);
2309 tree res = gimple_phi_result (phi);
2310 tree var = SSA_NAME_VAR (res);
2311 tree zero_dim_array = create_zero_dim_array (var, "phi_out_of_ssa");
2312 gimple stmt;
2313 gimple_seq stmts;
2315 for (i = 0; i < gimple_phi_num_args (phi); i++)
2317 tree arg = gimple_phi_arg_def (phi, i);
2318 edge e = gimple_phi_arg_edge (phi, i);
2320 /* Avoid the insertion of code in the loop latch to please the
2321 pattern matching of the vectorizer. */
2322 if (TREE_CODE (arg) == SSA_NAME
2323 && e->src == bb->loop_father->latch)
2324 insert_out_of_ssa_copy (scop, zero_dim_array, arg,
2325 SSA_NAME_DEF_STMT (arg));
2326 else
2327 insert_out_of_ssa_copy_on_edge (scop, e, zero_dim_array, arg);
2330 var = force_gimple_operand (zero_dim_array, &stmts, true, NULL_TREE);
2332 stmt = gimple_build_assign (res, var);
2333 remove_phi_node (psi, false);
2334 SSA_NAME_DEF_STMT (res) = stmt;
2336 insert_stmts (scop, stmt, stmts, gsi_after_labels (bb));
2339 /* Rewrite the degenerate phi node at position PSI from the degenerate
2340 form "x = phi (y, y, ..., y)" to "x = y". */
2342 static void
2343 rewrite_degenerate_phi (gimple_stmt_iterator *psi)
2345 tree rhs;
2346 gimple stmt;
2347 gimple_stmt_iterator gsi;
2348 gimple phi = gsi_stmt (*psi);
2349 tree res = gimple_phi_result (phi);
2350 basic_block bb;
2352 bb = gimple_bb (phi);
2353 rhs = degenerate_phi_result (phi);
2354 gcc_assert (rhs);
2356 stmt = gimple_build_assign (res, rhs);
2357 remove_phi_node (psi, false);
2358 SSA_NAME_DEF_STMT (res) = stmt;
2360 gsi = gsi_after_labels (bb);
2361 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2364 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2366 static void
2367 rewrite_reductions_out_of_ssa (scop_p scop)
2369 basic_block bb;
2370 gimple_stmt_iterator psi;
2371 sese region = SCOP_REGION (scop);
2373 FOR_EACH_BB (bb)
2374 if (bb_in_sese_p (bb, region))
2375 for (psi = gsi_start_phis (bb); !gsi_end_p (psi);)
2377 gimple phi = gsi_stmt (psi);
2379 if (!is_gimple_reg (gimple_phi_result (phi)))
2381 gsi_next (&psi);
2382 continue;
2385 if (gimple_phi_num_args (phi) > 1
2386 && degenerate_phi_result (phi))
2387 rewrite_degenerate_phi (&psi);
2389 else if (scalar_close_phi_node_p (phi))
2390 rewrite_close_phi_out_of_ssa (scop, &psi);
2392 else if (reduction_phi_p (region, &psi))
2393 rewrite_phi_out_of_ssa (scop, &psi);
2396 update_ssa (TODO_update_ssa);
2397 #ifdef ENABLE_CHECKING
2398 verify_loop_closed_ssa (true);
2399 #endif
2402 /* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
2403 read from ZERO_DIM_ARRAY. */
2405 static void
2406 rewrite_cross_bb_scalar_dependence (scop_p scop, tree zero_dim_array,
2407 tree def, gimple use_stmt)
2409 tree var = SSA_NAME_VAR (def);
2410 gimple name_stmt = gimple_build_assign (var, zero_dim_array);
2411 tree name = make_ssa_name (var, name_stmt);
2412 ssa_op_iter iter;
2413 use_operand_p use_p;
2415 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
2417 gimple_assign_set_lhs (name_stmt, name);
2418 insert_stmts (scop, name_stmt, NULL, gsi_for_stmt (use_stmt));
2420 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, iter, SSA_OP_ALL_USES)
2421 if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
2422 replace_exp (use_p, name);
2424 update_stmt (use_stmt);
2427 /* For every definition DEF in the SCOP that is used outside the scop,
2428 insert a closing-scop definition in the basic block just after this
2429 SCOP. */
2431 static void
2432 handle_scalar_deps_crossing_scop_limits (scop_p scop, tree def, gimple stmt)
2434 tree var = create_tmp_reg (TREE_TYPE (def), NULL);
2435 tree new_name = make_ssa_name (var, stmt);
2436 bool needs_copy = false;
2437 use_operand_p use_p;
2438 imm_use_iterator imm_iter;
2439 gimple use_stmt;
2440 sese region = SCOP_REGION (scop);
2442 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2444 if (!bb_in_sese_p (gimple_bb (use_stmt), region))
2446 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
2448 SET_USE (use_p, new_name);
2450 update_stmt (use_stmt);
2451 needs_copy = true;
2455 /* Insert in the empty BB just after the scop a use of DEF such
2456 that the rewrite of cross_bb_scalar_dependences won't insert
2457 arrays everywhere else. */
2458 if (needs_copy)
2460 gimple assign = gimple_build_assign (new_name, def);
2461 gimple_stmt_iterator psi = gsi_after_labels (SESE_EXIT (region)->dest);
2463 add_referenced_var (var);
2464 SSA_NAME_DEF_STMT (new_name) = assign;
2465 update_stmt (assign);
2466 gsi_insert_before (&psi, assign, GSI_SAME_STMT);
2470 /* Rewrite the scalar dependences crossing the boundary of the BB
2471 containing STMT with an array. Return true when something has been
2472 changed. */
2474 static bool
2475 rewrite_cross_bb_scalar_deps (scop_p scop, gimple_stmt_iterator *gsi)
2477 sese region = SCOP_REGION (scop);
2478 gimple stmt = gsi_stmt (*gsi);
2479 imm_use_iterator imm_iter;
2480 tree def;
2481 basic_block def_bb;
2482 tree zero_dim_array = NULL_TREE;
2483 gimple use_stmt;
2484 bool res = false;
2486 switch (gimple_code (stmt))
2488 case GIMPLE_ASSIGN:
2489 def = gimple_assign_lhs (stmt);
2490 break;
2492 case GIMPLE_CALL:
2493 def = gimple_call_lhs (stmt);
2494 break;
2496 default:
2497 return false;
2500 if (!def
2501 || !is_gimple_reg (def))
2502 return false;
2504 if (scev_analyzable_p (def, region))
2506 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (def));
2507 tree scev = scalar_evolution_in_region (region, loop, def);
2509 if (tree_contains_chrecs (scev, NULL))
2510 return false;
2512 propagate_expr_outside_region (def, scev, region);
2513 return true;
2516 def_bb = gimple_bb (stmt);
2518 handle_scalar_deps_crossing_scop_limits (scop, def, stmt);
2520 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2521 if (gimple_code (use_stmt) == GIMPLE_PHI
2522 && (res = true))
2524 gimple_stmt_iterator psi = gsi_for_stmt (use_stmt);
2526 if (scalar_close_phi_node_p (gsi_stmt (psi)))
2527 rewrite_close_phi_out_of_ssa (scop, &psi);
2528 else
2529 rewrite_phi_out_of_ssa (scop, &psi);
2532 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2533 if (gimple_code (use_stmt) != GIMPLE_PHI
2534 && def_bb != gimple_bb (use_stmt)
2535 && !is_gimple_debug (use_stmt)
2536 && (res = true))
2538 if (!zero_dim_array)
2540 zero_dim_array = create_zero_dim_array
2541 (SSA_NAME_VAR (def), "Cross_BB_scalar_dependence");
2542 insert_out_of_ssa_copy (scop, zero_dim_array, def,
2543 SSA_NAME_DEF_STMT (def));
2544 gsi_next (gsi);
2547 rewrite_cross_bb_scalar_dependence (scop, zero_dim_array,
2548 def, use_stmt);
2551 return res;
2554 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2556 static void
2557 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop)
2559 basic_block bb;
2560 gimple_stmt_iterator psi;
2561 sese region = SCOP_REGION (scop);
2562 bool changed = false;
2564 /* Create an extra empty BB after the scop. */
2565 split_edge (SESE_EXIT (region));
2567 FOR_EACH_BB (bb)
2568 if (bb_in_sese_p (bb, region))
2569 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
2570 changed |= rewrite_cross_bb_scalar_deps (scop, &psi);
2572 if (changed)
2574 scev_reset_htab ();
2575 update_ssa (TODO_update_ssa);
2576 #ifdef ENABLE_CHECKING
2577 verify_loop_closed_ssa (true);
2578 #endif
2582 /* Returns the number of pbbs that are in loops contained in SCOP. */
2584 static int
2585 nb_pbbs_in_loops (scop_p scop)
2587 int i;
2588 poly_bb_p pbb;
2589 int res = 0;
2591 FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
2592 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), SCOP_REGION (scop)))
2593 res++;
2595 return res;
2598 /* Return the number of data references in BB that write in
2599 memory. */
2601 static int
2602 nb_data_writes_in_bb (basic_block bb)
2604 int res = 0;
2605 gimple_stmt_iterator gsi;
2607 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2608 if (gimple_vdef (gsi_stmt (gsi)))
2609 res++;
2611 return res;
2614 /* Splits at STMT the basic block BB represented as PBB in the
2615 polyhedral form. */
2617 static edge
2618 split_pbb (scop_p scop, poly_bb_p pbb, basic_block bb, gimple stmt)
2620 edge e1 = split_block (bb, stmt);
2621 new_pbb_from_pbb (scop, pbb, e1->dest);
2622 return e1;
2625 /* Splits STMT out of its current BB. This is done for reduction
2626 statements for which we want to ignore data dependences. */
2628 static basic_block
2629 split_reduction_stmt (scop_p scop, gimple stmt)
2631 basic_block bb = gimple_bb (stmt);
2632 poly_bb_p pbb = pbb_from_bb (bb);
2633 gimple_bb_p gbb = gbb_from_bb (bb);
2634 edge e1;
2635 int i;
2636 data_reference_p dr;
2638 /* Do not split basic blocks with no writes to memory: the reduction
2639 will be the only write to memory. */
2640 if (nb_data_writes_in_bb (bb) == 0)
2641 return bb;
2643 e1 = split_pbb (scop, pbb, bb, stmt);
2645 /* Split once more only when the reduction stmt is not the only one
2646 left in the original BB. */
2647 if (!gsi_one_before_end_p (gsi_start_nondebug_bb (bb)))
2649 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2650 gsi_prev (&gsi);
2651 e1 = split_pbb (scop, pbb, bb, gsi_stmt (gsi));
2654 /* A part of the data references will end in a different basic block
2655 after the split: move the DRs from the original GBB to the newly
2656 created GBB1. */
2657 FOR_EACH_VEC_ELT (data_reference_p, GBB_DATA_REFS (gbb), i, dr)
2659 basic_block bb1 = gimple_bb (DR_STMT (dr));
2661 if (bb1 != bb)
2663 gimple_bb_p gbb1 = gbb_from_bb (bb1);
2664 VEC_safe_push (data_reference_p, heap, GBB_DATA_REFS (gbb1), dr);
2665 VEC_ordered_remove (data_reference_p, GBB_DATA_REFS (gbb), i);
2666 i--;
2670 return e1->dest;
2673 /* Return true when stmt is a reduction operation. */
2675 static inline bool
2676 is_reduction_operation_p (gimple stmt)
2678 enum tree_code code;
2680 gcc_assert (is_gimple_assign (stmt));
2681 code = gimple_assign_rhs_code (stmt);
2683 return flag_associative_math
2684 && commutative_tree_code (code)
2685 && associative_tree_code (code);
2688 /* Returns true when PHI contains an argument ARG. */
2690 static bool
2691 phi_contains_arg (gimple phi, tree arg)
2693 size_t i;
2695 for (i = 0; i < gimple_phi_num_args (phi); i++)
2696 if (operand_equal_p (arg, gimple_phi_arg_def (phi, i), 0))
2697 return true;
2699 return false;
2702 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2704 static gimple
2705 follow_ssa_with_commutative_ops (tree arg, tree lhs)
2707 gimple stmt;
2709 if (TREE_CODE (arg) != SSA_NAME)
2710 return NULL;
2712 stmt = SSA_NAME_DEF_STMT (arg);
2714 if (gimple_code (stmt) == GIMPLE_NOP
2715 || gimple_code (stmt) == GIMPLE_CALL)
2716 return NULL;
2718 if (gimple_code (stmt) == GIMPLE_PHI)
2720 if (phi_contains_arg (stmt, lhs))
2721 return stmt;
2722 return NULL;
2725 if (!is_gimple_assign (stmt))
2726 return NULL;
2728 if (gimple_num_ops (stmt) == 2)
2729 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2731 if (is_reduction_operation_p (stmt))
2733 gimple res = follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2735 return res ? res :
2736 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt), lhs);
2739 return NULL;
2742 /* Detect commutative and associative scalar reductions starting at
2743 the STMT. Return the phi node of the reduction cycle, or NULL. */
2745 static gimple
2746 detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
2747 VEC (gimple, heap) **in,
2748 VEC (gimple, heap) **out)
2750 gimple phi = follow_ssa_with_commutative_ops (arg, lhs);
2752 if (!phi)
2753 return NULL;
2755 VEC_safe_push (gimple, heap, *in, stmt);
2756 VEC_safe_push (gimple, heap, *out, stmt);
2757 return phi;
2760 /* Detect commutative and associative scalar reductions starting at
2761 STMT. Return the phi node of the reduction cycle, or NULL. */
2763 static gimple
2764 detect_commutative_reduction_assign (gimple stmt, VEC (gimple, heap) **in,
2765 VEC (gimple, heap) **out)
2767 tree lhs = gimple_assign_lhs (stmt);
2769 if (gimple_num_ops (stmt) == 2)
2770 return detect_commutative_reduction_arg (lhs, stmt,
2771 gimple_assign_rhs1 (stmt),
2772 in, out);
2774 if (is_reduction_operation_p (stmt))
2776 gimple res = detect_commutative_reduction_arg (lhs, stmt,
2777 gimple_assign_rhs1 (stmt),
2778 in, out);
2779 return res ? res
2780 : detect_commutative_reduction_arg (lhs, stmt,
2781 gimple_assign_rhs2 (stmt),
2782 in, out);
2785 return NULL;
2788 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2790 static gimple
2791 follow_inital_value_to_phi (tree arg, tree lhs)
2793 gimple stmt;
2795 if (!arg || TREE_CODE (arg) != SSA_NAME)
2796 return NULL;
2798 stmt = SSA_NAME_DEF_STMT (arg);
2800 if (gimple_code (stmt) == GIMPLE_PHI
2801 && phi_contains_arg (stmt, lhs))
2802 return stmt;
2804 return NULL;
2808 /* Return the argument of the loop PHI that is the inital value coming
2809 from outside the loop. */
2811 static edge
2812 edge_initial_value_for_loop_phi (gimple phi)
2814 size_t i;
2816 for (i = 0; i < gimple_phi_num_args (phi); i++)
2818 edge e = gimple_phi_arg_edge (phi, i);
2820 if (loop_depth (e->src->loop_father)
2821 < loop_depth (e->dest->loop_father))
2822 return e;
2825 return NULL;
2828 /* Return the argument of the loop PHI that is the inital value coming
2829 from outside the loop. */
2831 static tree
2832 initial_value_for_loop_phi (gimple phi)
2834 size_t i;
2836 for (i = 0; i < gimple_phi_num_args (phi); i++)
2838 edge e = gimple_phi_arg_edge (phi, i);
2840 if (loop_depth (e->src->loop_father)
2841 < loop_depth (e->dest->loop_father))
2842 return gimple_phi_arg_def (phi, i);
2845 return NULL_TREE;
2848 /* Detect commutative and associative scalar reductions belonging to
2849 the SCOP starting at the loop closed phi node STMT. Return the phi
2850 node of the reduction cycle, or NULL. */
2852 static gimple
2853 detect_commutative_reduction (scop_p scop, gimple stmt, VEC (gimple, heap) **in,
2854 VEC (gimple, heap) **out)
2856 if (scalar_close_phi_node_p (stmt))
2858 tree arg = gimple_phi_arg_def (stmt, 0);
2859 gimple def, loop_phi;
2861 if (TREE_CODE (arg) != SSA_NAME)
2862 return NULL;
2864 /* Note that loop close phi nodes should have a single argument
2865 because we translated the representation into a canonical form
2866 before Graphite: see canonicalize_loop_closed_ssa_form. */
2867 gcc_assert (gimple_phi_num_args (stmt) == 1);
2869 def = SSA_NAME_DEF_STMT (arg);
2870 if (!stmt_in_sese_p (def, SCOP_REGION (scop)))
2871 return NULL;
2873 loop_phi = detect_commutative_reduction (scop, def, in, out);
2875 if (loop_phi)
2877 tree lhs = gimple_phi_result (stmt);
2878 tree init = initial_value_for_loop_phi (loop_phi);
2879 gimple phi = follow_inital_value_to_phi (init, lhs);
2881 VEC_safe_push (gimple, heap, *in, loop_phi);
2882 VEC_safe_push (gimple, heap, *out, stmt);
2883 return phi;
2885 else
2886 return NULL;
2889 if (gimple_code (stmt) == GIMPLE_ASSIGN)
2890 return detect_commutative_reduction_assign (stmt, in, out);
2892 return NULL;
2895 /* Translate the scalar reduction statement STMT to an array RED
2896 knowing that its recursive phi node is LOOP_PHI. */
2898 static void
2899 translate_scalar_reduction_to_array_for_stmt (scop_p scop, tree red,
2900 gimple stmt, gimple loop_phi)
2902 tree res = gimple_phi_result (loop_phi);
2903 gimple assign = gimple_build_assign (res, red);
2904 gimple_stmt_iterator gsi;
2906 insert_stmts (scop, assign, NULL, gsi_after_labels (gimple_bb (loop_phi)));
2908 assign = gimple_build_assign (red, gimple_assign_lhs (stmt));
2909 gsi = gsi_for_stmt (stmt);
2910 gsi_next (&gsi);
2911 insert_stmts (scop, assign, NULL, gsi);
2914 /* Removes the PHI node and resets all the debug stmts that are using
2915 the PHI_RESULT. */
2917 static void
2918 remove_phi (gimple phi)
2920 imm_use_iterator imm_iter;
2921 tree def;
2922 use_operand_p use_p;
2923 gimple_stmt_iterator gsi;
2924 VEC (gimple, heap) *update = VEC_alloc (gimple, heap, 3);
2925 unsigned int i;
2926 gimple stmt;
2928 def = PHI_RESULT (phi);
2929 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
2931 stmt = USE_STMT (use_p);
2933 if (is_gimple_debug (stmt))
2935 gimple_debug_bind_reset_value (stmt);
2936 VEC_safe_push (gimple, heap, update, stmt);
2940 FOR_EACH_VEC_ELT (gimple, update, i, stmt)
2941 update_stmt (stmt);
2943 VEC_free (gimple, heap, update);
2945 gsi = gsi_for_phi_node (phi);
2946 remove_phi_node (&gsi, false);
2949 /* Rewrite out of SSA the reduction described by the loop phi nodes
2950 IN, and the close phi nodes OUT. IN and OUT are structured by loop
2951 levels like this:
2953 IN: stmt, loop_n, ..., loop_0
2954 OUT: stmt, close_n, ..., close_0
2956 the first element is the reduction statement, and the next elements
2957 are the loop and close phi nodes of each of the outer loops. */
2959 static void
2960 translate_scalar_reduction_to_array (scop_p scop,
2961 VEC (gimple, heap) *in,
2962 VEC (gimple, heap) *out)
2964 unsigned int i;
2965 gimple loop_phi;
2966 tree red = NULL_TREE;
2968 FOR_EACH_VEC_ELT (gimple, in, i, loop_phi)
2970 gimple close_phi = VEC_index (gimple, out, i);
2972 if (i == 0)
2974 gimple stmt = loop_phi;
2975 basic_block bb = split_reduction_stmt (scop, stmt);
2976 poly_bb_p pbb = pbb_from_bb (bb);
2977 PBB_IS_REDUCTION (pbb) = true;
2978 gcc_assert (close_phi == loop_phi);
2980 red = create_zero_dim_array
2981 (gimple_assign_lhs (stmt), "Commutative_Associative_Reduction");
2982 translate_scalar_reduction_to_array_for_stmt
2983 (scop, red, stmt, VEC_index (gimple, in, 1));
2984 continue;
2987 if (i == VEC_length (gimple, in) - 1)
2989 insert_out_of_ssa_copy (scop, gimple_phi_result (close_phi), red,
2990 close_phi);
2991 insert_out_of_ssa_copy_on_edge
2992 (scop, edge_initial_value_for_loop_phi (loop_phi),
2993 red, initial_value_for_loop_phi (loop_phi));
2996 remove_phi (loop_phi);
2997 remove_phi (close_phi);
3001 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. Returns
3002 true when something has been changed. */
3004 static bool
3005 rewrite_commutative_reductions_out_of_ssa_close_phi (scop_p scop,
3006 gimple close_phi)
3008 bool res;
3009 VEC (gimple, heap) *in = VEC_alloc (gimple, heap, 10);
3010 VEC (gimple, heap) *out = VEC_alloc (gimple, heap, 10);
3012 detect_commutative_reduction (scop, close_phi, &in, &out);
3013 res = VEC_length (gimple, in) > 0;
3014 if (res)
3015 translate_scalar_reduction_to_array (scop, in, out);
3017 VEC_free (gimple, heap, in);
3018 VEC_free (gimple, heap, out);
3019 return res;
3022 /* Rewrites all the commutative reductions from LOOP out of SSA.
3023 Returns true when something has been changed. */
3025 static bool
3026 rewrite_commutative_reductions_out_of_ssa_loop (scop_p scop,
3027 loop_p loop)
3029 gimple_stmt_iterator gsi;
3030 edge exit = single_exit (loop);
3031 tree res;
3032 bool changed = false;
3034 if (!exit)
3035 return false;
3037 for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
3038 if ((res = gimple_phi_result (gsi_stmt (gsi)))
3039 && is_gimple_reg (res)
3040 && !scev_analyzable_p (res, SCOP_REGION (scop)))
3041 changed |= rewrite_commutative_reductions_out_of_ssa_close_phi
3042 (scop, gsi_stmt (gsi));
3044 return changed;
3047 /* Rewrites all the commutative reductions from SCOP out of SSA. */
3049 static void
3050 rewrite_commutative_reductions_out_of_ssa (scop_p scop)
3052 loop_iterator li;
3053 loop_p loop;
3054 bool changed = false;
3055 sese region = SCOP_REGION (scop);
3057 FOR_EACH_LOOP (li, loop, 0)
3058 if (loop_in_sese_p (loop, region))
3059 changed |= rewrite_commutative_reductions_out_of_ssa_loop (scop, loop);
3061 if (changed)
3063 scev_reset_htab ();
3064 gsi_commit_edge_inserts ();
3065 update_ssa (TODO_update_ssa);
3066 #ifdef ENABLE_CHECKING
3067 verify_loop_closed_ssa (true);
3068 #endif
3072 /* Java does not initialize long_long_integer_type_node. */
3073 #define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
3075 /* Can all ivs be represented by a signed integer?
3076 As CLooG might generate negative values in its expressions, signed loop ivs
3077 are required in the backend. */
3079 static bool
3080 scop_ivs_can_be_represented (scop_p scop)
3082 loop_iterator li;
3083 loop_p loop;
3084 gimple_stmt_iterator psi;
3086 FOR_EACH_LOOP (li, loop, 0)
3088 if (!loop_in_sese_p (loop, SCOP_REGION (scop)))
3089 continue;
3091 for (psi = gsi_start_phis (loop->header);
3092 !gsi_end_p (psi); gsi_next (&psi))
3094 gimple phi = gsi_stmt (psi);
3095 tree res = PHI_RESULT (phi);
3096 tree type = TREE_TYPE (res);
3098 if (TYPE_UNSIGNED (type)
3099 && TYPE_PRECISION (type) >= TYPE_PRECISION (my_long_long))
3100 return false;
3104 return true;
3107 #undef my_long_long
3109 /* Builds the polyhedral representation for a SESE region. */
3111 void
3112 build_poly_scop (scop_p scop)
3114 sese region = SCOP_REGION (scop);
3115 graphite_dim_t max_dim;
3117 build_scop_bbs (scop);
3119 /* FIXME: This restriction is needed to avoid a problem in CLooG.
3120 Once CLooG is fixed, remove this guard. Anyways, it makes no
3121 sense to optimize a scop containing only PBBs that do not belong
3122 to any loops. */
3123 if (nb_pbbs_in_loops (scop) == 0)
3124 return;
3126 if (!scop_ivs_can_be_represented (scop))
3127 return;
3129 build_sese_loop_nests (region);
3130 build_sese_conditions (region);
3131 find_scop_parameters (scop);
3133 max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
3134 if (scop_nb_params (scop) > max_dim)
3135 return;
3137 build_scop_iteration_domain (scop);
3138 build_scop_context (scop);
3139 add_conditions_to_constraints (scop);
3141 /* Rewrite out of SSA only after having translated the
3142 representation to the polyhedral representation to avoid scev
3143 analysis failures. That means that these functions will insert
3144 new data references that they create in the right place. */
3145 if (flag_associative_math)
3146 rewrite_commutative_reductions_out_of_ssa (scop);
3147 rewrite_reductions_out_of_ssa (scop);
3148 rewrite_cross_bb_scalar_deps_out_of_ssa (scop);
3150 build_scop_drs (scop);
3151 scop_to_lst (scop);
3152 build_scop_scattering (scop);
3154 /* This SCoP has been translated to the polyhedral
3155 representation. */
3156 POLY_SCOP_P (scop) = true;
3158 #endif