graphite-poly.c (pbb_remove_duplicate_pdrs): Initialize PBB_PDR_DUPLICATES_REMOVED.
[gcc.git] / gcc / graphite-poly.c
1 /* Graphite polyhedral representation.
2 Copyright (C) 2009 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4 Tobias Grosser <grosser@fim.uni-passau.de>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <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 "output.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "toplev.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "cfgloop.h"
36 #include "tree-chrec.h"
37 #include "tree-data-ref.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-pass.h"
40 #include "domwalk.h"
41 #include "value-prof.h"
42 #include "pointer-set.h"
43 #include "gimple.h"
44 #include "params.h"
45
46 #ifdef HAVE_cloog
47 #include "cloog/cloog.h"
48 #include "ppl_c.h"
49 #include "sese.h"
50 #include "graphite-ppl.h"
51 #include "graphite.h"
52 #include "graphite-poly.h"
53 #include "graphite-dependences.h"
54
55 /* Return the maximal loop depth in SCOP. */
56
57 int
58 scop_max_loop_depth (scop_p scop)
59 {
60 int i;
61 poly_bb_p pbb;
62 int max_nb_loops = 0;
63
64 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
65 {
66 int nb_loops = pbb_dim_iter_domain (pbb);
67 if (max_nb_loops < nb_loops)
68 max_nb_loops = nb_loops;
69 }
70
71 return max_nb_loops;
72 }
73
74 /* Extend the scattering matrix of PBB to MAX_SCATTERING scattering
75 dimensions. */
76
77 static void
78 extend_scattering (poly_bb_p pbb, int max_scattering)
79 {
80 ppl_dimension_type nb_old_dims, nb_new_dims;
81 int nb_added_dims, i;
82 ppl_Coefficient_t coef;
83 Value one;
84
85 nb_added_dims = max_scattering - pbb_nb_scattering_transform (pbb);
86 value_init (one);
87 value_set_si (one, 1);
88 ppl_new_Coefficient (&coef);
89 ppl_assign_Coefficient_from_mpz_t (coef, one);
90
91 gcc_assert (nb_added_dims >= 0);
92
93 nb_old_dims = pbb_nb_scattering_transform (pbb) + pbb_dim_iter_domain (pbb)
94 + scop_nb_params (PBB_SCOP (pbb));
95 nb_new_dims = nb_old_dims + nb_added_dims;
96
97 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb),
98 pbb_nb_scattering_transform (pbb), nb_added_dims);
99 PBB_NB_SCATTERING_TRANSFORM (pbb) += nb_added_dims;
100
101 /* Add identity matrix for the added dimensions. */
102 for (i = max_scattering - nb_added_dims; i < max_scattering; i++)
103 {
104 ppl_Constraint_t cstr;
105 ppl_Linear_Expression_t expr;
106
107 ppl_new_Linear_Expression_with_dimension (&expr, nb_new_dims);
108 ppl_Linear_Expression_add_to_coefficient (expr, i, coef);
109 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
110 ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb), cstr);
111 ppl_delete_Constraint (cstr);
112 ppl_delete_Linear_Expression (expr);
113 }
114
115 ppl_delete_Coefficient (coef);
116 value_clear (one);
117 }
118
119 /* All scattering matrices in SCOP will have the same number of scattering
120 dimensions. */
121
122 int
123 unify_scattering_dimensions (scop_p scop)
124 {
125 int i;
126 poly_bb_p pbb;
127 graphite_dim_t max_scattering = 0;
128
129 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
130 max_scattering = MAX (pbb_nb_scattering_transform (pbb), max_scattering);
131
132 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
133 extend_scattering (pbb, max_scattering);
134
135 return max_scattering;
136 }
137
138 /* Prints to FILE the scattering function of PBB. */
139
140 void
141 print_scattering_function (FILE *file, poly_bb_p pbb)
142 {
143 graphite_dim_t i;
144
145 if (!PBB_TRANSFORMED (pbb))
146 return;
147
148 fprintf (file, "scattering bb_%d (\n", pbb_index (pbb));
149 fprintf (file, "# eq");
150
151 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
152 fprintf (file, " s%d", (int) i);
153
154 for (i = 0; i < pbb_nb_local_vars (pbb); i++)
155 fprintf (file, " lv%d", (int) i);
156
157 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
158 fprintf (file, " i%d", (int) i);
159
160 for (i = 0; i < pbb_nb_params (pbb); i++)
161 fprintf (file, " p%d", (int) i);
162
163 fprintf (file, " cst\n");
164
165 ppl_print_polyhedron_matrix (file, PBB_TRANSFORMED_SCATTERING (pbb));
166
167 fprintf (file, ")\n");
168 }
169
170 /* Prints to FILE the iteration domain of PBB. */
171
172 void
173 print_iteration_domain (FILE *file, poly_bb_p pbb)
174 {
175 print_pbb_domain (file, pbb);
176 }
177
178 /* Prints to FILE the scattering functions of every PBB of SCOP. */
179
180 void
181 print_scattering_functions (FILE *file, scop_p scop)
182 {
183 int i;
184 poly_bb_p pbb;
185
186 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
187 print_scattering_function (file, pbb);
188 }
189
190 /* Prints to FILE the iteration domains of every PBB of SCOP. */
191
192 void
193 print_iteration_domains (FILE *file, scop_p scop)
194 {
195 int i;
196 poly_bb_p pbb;
197
198 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
199 print_iteration_domain (file, pbb);
200 }
201
202 /* Prints to STDERR the scattering function of PBB. */
203
204 void
205 debug_scattering_function (poly_bb_p pbb)
206 {
207 print_scattering_function (stderr, pbb);
208 }
209
210 /* Prints to STDERR the iteration domain of PBB. */
211
212 void
213 debug_iteration_domain (poly_bb_p pbb)
214 {
215 print_iteration_domain (stderr, pbb);
216 }
217
218 /* Prints to STDERR the scattering functions of every PBB of SCOP. */
219
220 void
221 debug_scattering_functions (scop_p scop)
222 {
223 print_scattering_functions (stderr, scop);
224 }
225
226 /* Prints to STDERR the iteration domains of every PBB of SCOP. */
227
228 void
229 debug_iteration_domains (scop_p scop)
230 {
231 print_iteration_domains (stderr, scop);
232 }
233
234 /* Apply graphite transformations to all the basic blocks of SCOP. */
235
236 bool
237 apply_poly_transforms (scop_p scop)
238 {
239 bool transform_done = false;
240
241 /* Generate code even if we did not apply any real transformation.
242 This also allows to check the performance for the identity
243 transformation: GIMPLE -> GRAPHITE -> GIMPLE
244 Keep in mind that CLooG optimizes in control, so the loop structure
245 may change, even if we only use -fgraphite-identity. */
246 if (flag_graphite_identity)
247 transform_done = true;
248
249 if (flag_loop_parallelize_all)
250 transform_done = true;
251
252 if (flag_loop_block)
253 {
254 transform_done |= scop_do_strip_mine (scop);
255 transform_done |= scop_do_interchange (scop);
256 }
257 else
258 {
259 if (flag_loop_strip_mine)
260 transform_done |= scop_do_strip_mine (scop);
261
262 if (flag_loop_interchange)
263 transform_done |= scop_do_interchange (scop);
264 }
265
266 return transform_done;
267 }
268
269 /* Returns true when it PDR1 is a duplicate of PDR2: same PBB, and
270 their ACCESSES, TYPE, and NB_SUBSCRIPTS are the same. */
271
272 static inline bool
273 can_collapse_pdrs (poly_dr_p pdr1, poly_dr_p pdr2)
274 {
275 bool res;
276 ppl_Pointset_Powerset_C_Polyhedron_t af1, af2, diff;
277
278 if (PDR_PBB (pdr1) != PDR_PBB (pdr2)
279 || PDR_NB_SUBSCRIPTS (pdr1) != PDR_NB_SUBSCRIPTS (pdr2)
280 || PDR_TYPE (pdr1) != PDR_TYPE (pdr2))
281 return false;
282
283 af1 = PDR_ACCESSES (pdr1);
284 af2 = PDR_ACCESSES (pdr2);
285 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
286 (&diff, af1);
287 ppl_Pointset_Powerset_C_Polyhedron_difference_assign (diff, af2);
288
289 res = ppl_Pointset_Powerset_C_Polyhedron_is_empty (diff);
290 ppl_delete_Pointset_Powerset_C_Polyhedron (diff);
291 return res;
292 }
293
294 /* Removes duplicated data references in PBB. */
295
296 void
297 pbb_remove_duplicate_pdrs (poly_bb_p pbb)
298 {
299 int i, j;
300 poly_dr_p pdr1, pdr2;
301 unsigned n = VEC_length (poly_dr_p, PBB_DRS (pbb));
302 VEC (poly_dr_p, heap) *collapsed = VEC_alloc (poly_dr_p, heap, n);
303
304 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr1); i++)
305 for (j = 0; VEC_iterate (poly_dr_p, collapsed, j, pdr2); j++)
306 if (!can_collapse_pdrs (pdr1, pdr2))
307 VEC_quick_push (poly_dr_p, collapsed, pdr1);
308
309 PBB_PDR_DUPLICATES_REMOVED (pbb) = true;
310 }
311
312 /* Create a new polyhedral data reference and add it to PBB. It is
313 defined by its ACCESSES, its TYPE, and the number of subscripts
314 NB_SUBSCRIPTS. */
315
316 void
317 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
318 ppl_Pointset_Powerset_C_Polyhedron_t accesses,
319 enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts)
320 {
321 static int id = 0;
322 poly_dr_p pdr = XNEW (struct poly_dr);
323
324 PDR_ID (pdr) = id++;
325 PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
326 PDR_NB_REFS (pdr) = 1;
327 PDR_PBB (pdr) = pbb;
328 PDR_ACCESSES (pdr) = accesses;
329 PDR_TYPE (pdr) = type;
330 PDR_CDR (pdr) = cdr;
331 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
332 VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
333 }
334
335 /* Free polyhedral data reference PDR. */
336
337 void
338 free_poly_dr (poly_dr_p pdr)
339 {
340 ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr));
341 XDELETE (pdr);
342 }
343
344 /* Create a new polyhedral black box. */
345
346 void
347 new_poly_bb (scop_p scop, void *black_box, bool reduction)
348 {
349 poly_bb_p pbb = XNEW (struct poly_bb);
350
351 PBB_DOMAIN (pbb) = NULL;
352 PBB_SCOP (pbb) = scop;
353 pbb_set_black_box (pbb, black_box);
354 PBB_TRANSFORMED (pbb) = NULL;
355 PBB_SAVED (pbb) = NULL;
356 PBB_ORIGINAL (pbb) = NULL;
357 PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
358 PBB_IS_REDUCTION (pbb) = reduction;
359 PBB_PDR_DUPLICATES_REMOVED (pbb) = false;
360 VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
361 }
362
363 /* Free polyhedral black box. */
364
365 void
366 free_poly_bb (poly_bb_p pbb)
367 {
368 int i;
369 poly_dr_p pdr;
370
371 ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb));
372
373 if (PBB_TRANSFORMED (pbb))
374 poly_scattering_free (PBB_TRANSFORMED (pbb));
375
376 if (PBB_SAVED (pbb))
377 poly_scattering_free (PBB_SAVED (pbb));
378
379 if (PBB_ORIGINAL (pbb))
380 poly_scattering_free (PBB_ORIGINAL (pbb));
381
382 if (PBB_DRS (pbb))
383 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
384 free_poly_dr (pdr);
385
386 VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
387 XDELETE (pbb);
388 }
389
390 static void
391 print_pdr_access_layout (FILE *file, poly_dr_p pdr)
392 {
393 graphite_dim_t i;
394
395 fprintf (file, "# eq");
396
397 for (i = 0; i < pdr_dim_iter_domain (pdr); i++)
398 fprintf (file, " i%d", (int) i);
399
400 for (i = 0; i < pdr_nb_params (pdr); i++)
401 fprintf (file, " p%d", (int) i);
402
403 fprintf (file, " alias");
404
405 for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
406 fprintf (file, " sub%d", (int) i);
407
408 fprintf (file, " cst\n");
409 }
410
411 /* Prints to FILE the polyhedral data reference PDR. */
412
413 void
414 print_pdr (FILE *file, poly_dr_p pdr)
415 {
416 fprintf (file, "pdr_%d (", PDR_ID (pdr));
417
418 switch (PDR_TYPE (pdr))
419 {
420 case PDR_READ:
421 fprintf (file, "read \n");
422 break;
423
424 case PDR_WRITE:
425 fprintf (file, "write \n");
426 break;
427
428 case PDR_MAY_WRITE:
429 fprintf (file, "may_write \n");
430 break;
431
432 default:
433 gcc_unreachable ();
434 }
435
436 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
437
438 fprintf (file, "data accesses (\n");
439 print_pdr_access_layout (file, pdr);
440 ppl_print_powerset_matrix (file, PDR_ACCESSES (pdr));
441 fprintf (file, ")\n");
442
443 fprintf (file, ")\n");
444 }
445
446 /* Prints to STDERR the polyhedral data reference PDR. */
447
448 void
449 debug_pdr (poly_dr_p pdr)
450 {
451 print_pdr (stderr, pdr);
452 }
453
454 /* Creates a new SCOP containing REGION. */
455
456 scop_p
457 new_scop (void *region)
458 {
459 scop_p scop = XNEW (struct scop);
460
461 SCOP_CONTEXT (scop) = NULL;
462 scop_set_region (scop, region);
463 SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
464 SCOP_ORIGINAL_PDDRS (scop) = htab_create (10, hash_poly_ddr_p,
465 eq_poly_ddr_p, free_poly_ddr);
466 return scop;
467 }
468
469 /* Deletes SCOP. */
470
471 void
472 free_scop (scop_p scop)
473 {
474 int i;
475 poly_bb_p pbb;
476
477 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
478 free_poly_bb (pbb);
479
480 VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
481
482 if (SCOP_CONTEXT (scop))
483 ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop));
484
485 htab_delete (SCOP_ORIGINAL_PDDRS (scop));
486 XDELETE (scop);
487 }
488
489 /* Print to FILE the domain of PBB. */
490
491 void
492 print_pbb_domain (FILE *file, poly_bb_p pbb)
493 {
494 graphite_dim_t i;
495 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
496
497 if (!PBB_DOMAIN (pbb))
498 return;
499
500 fprintf (file, "domains bb_%d (\n", GBB_BB (gbb)->index);
501 fprintf (file, "# eq");
502
503 for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
504 fprintf (file, " i%d", (int) i);
505
506 for (i = 0; i < pbb_nb_params (pbb); i++)
507 fprintf (file, " p%d", (int) i);
508
509 fprintf (file, " cst\n");
510
511 if (PBB_DOMAIN (pbb))
512 ppl_print_powerset_matrix (file, PBB_DOMAIN (pbb));
513
514 fprintf (file, ")\n");
515 }
516
517 /* Dump the cases of a graphite basic block GBB on FILE. */
518
519 static void
520 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
521 {
522 int i;
523 gimple stmt;
524 VEC (gimple, heap) *cases;
525
526 if (!gbb)
527 return;
528
529 cases = GBB_CONDITION_CASES (gbb);
530 if (VEC_empty (gimple, cases))
531 return;
532
533 fprintf (file, "cases bb_%d (", GBB_BB (gbb)->index);
534
535 for (i = 0; VEC_iterate (gimple, cases, i, stmt); i++)
536 print_gimple_stmt (file, stmt, 0, 0);
537
538 fprintf (file, ")\n");
539 }
540
541 /* Dump conditions of a graphite basic block GBB on FILE. */
542
543 static void
544 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
545 {
546 int i;
547 gimple stmt;
548 VEC (gimple, heap) *conditions;
549
550 if (!gbb)
551 return;
552
553 conditions = GBB_CONDITIONS (gbb);
554 if (VEC_empty (gimple, conditions))
555 return;
556
557 fprintf (file, "conditions bb_%d (", GBB_BB (gbb)->index);
558
559 for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
560 print_gimple_stmt (file, stmt, 0, 0);
561
562 fprintf (file, ")\n");
563 }
564
565 /* Print to FILE all the data references of PBB. */
566
567 void
568 print_pdrs (FILE *file, poly_bb_p pbb)
569 {
570 int i;
571 poly_dr_p pdr;
572
573 for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
574 print_pdr (file, pdr);
575 }
576
577 /* Print to STDERR all the data references of PBB. */
578
579 void
580 debug_pdrs (poly_bb_p pbb)
581 {
582 print_pdrs (stderr, pbb);
583 }
584
585 /* Print to FILE the domain and scattering function of PBB. */
586
587 void
588 print_pbb (FILE *file, poly_bb_p pbb)
589 {
590 fprintf (file, "pbb_%d (\n", pbb_index (pbb));
591 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
592 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
593 print_pdrs (file, pbb);
594 print_pbb_domain (file, pbb);
595 print_scattering_function (file, pbb);
596 fprintf (file, ")\n");
597 }
598
599 /* Print to FILE the parameters of SCOP. */
600
601 void
602 print_scop_params (FILE *file, scop_p scop)
603 {
604 int i;
605 tree t;
606
607 fprintf (file, "parameters (\n");
608 for (i = 0; VEC_iterate (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t); i++)
609 {
610 fprintf (file, "p_%d -> ", i);
611 print_generic_expr (file, t, 0);
612 fprintf (file, "\n");
613 }
614 fprintf (file, ")\n");
615 }
616
617 /* Print to FILE the context of SCoP. */
618 void
619 print_scop_context (FILE *file, scop_p scop)
620 {
621 graphite_dim_t i;
622
623 fprintf (file, "context (\n");
624 fprintf (file, "# eq");
625
626 for (i = 0; i < scop_nb_params (scop); i++)
627 fprintf (file, " p%d", (int) i);
628
629 fprintf (file, " cst\n");
630
631 if (SCOP_CONTEXT (scop))
632 ppl_print_powerset_matrix (file, SCOP_CONTEXT (scop));
633
634 fprintf (file, ")\n");
635 }
636
637 /* Print to FILE the SCOP. */
638
639 void
640 print_scop (FILE *file, scop_p scop)
641 {
642 int i;
643 poly_bb_p pbb;
644
645 fprintf (file, "scop (\n");
646 print_scop_params (file, scop);
647 print_scop_context (file, scop);
648
649 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
650 print_pbb (file, pbb);
651
652 fprintf (file, "original_lst (\n");
653 print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
654 fprintf (file, ")\n");
655
656 fprintf (file, "transformed_lst (\n");
657 print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
658 fprintf (file, ")\n");
659
660 fprintf (file, ")\n");
661 }
662
663 /* Print to STDERR the domain of PBB. */
664
665 void
666 debug_pbb_domain (poly_bb_p pbb)
667 {
668 print_pbb_domain (stderr, pbb);
669 }
670
671 /* Print to FILE the domain and scattering function of PBB. */
672
673 void
674 debug_pbb (poly_bb_p pbb)
675 {
676 print_pbb (stderr, pbb);
677 }
678
679 /* Print to STDERR the context of SCOP. */
680
681 void
682 debug_scop_context (scop_p scop)
683 {
684 print_scop_context (stderr, scop);
685 }
686
687 /* Print to STDERR the SCOP. */
688
689 void
690 debug_scop (scop_p scop)
691 {
692 print_scop (stderr, scop);
693 }
694
695 /* Print to STDERR the parameters of SCOP. */
696
697 void
698 debug_scop_params (scop_p scop)
699 {
700 print_scop_params (stderr, scop);
701 }
702
703
704 /* The dimension in the transformed scattering polyhedron of PBB
705 containing the scattering iterator for the loop at depth LOOP_DEPTH. */
706
707 ppl_dimension_type
708 psct_scattering_dim_for_loop_depth (poly_bb_p pbb, graphite_dim_t loop_depth)
709 {
710 ppl_const_Constraint_System_t pcs;
711 ppl_Constraint_System_const_iterator_t cit, cend;
712 ppl_const_Constraint_t cstr;
713 ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
714 ppl_dimension_type iter = psct_iterator_dim (pbb, loop_depth);
715 ppl_Linear_Expression_t expr;
716 ppl_Coefficient_t coef;
717 Value val;
718 graphite_dim_t i;
719
720 value_init (val);
721 ppl_new_Coefficient (&coef);
722 ppl_Polyhedron_get_constraints (ph, &pcs);
723 ppl_new_Constraint_System_const_iterator (&cit);
724 ppl_new_Constraint_System_const_iterator (&cend);
725
726 for (ppl_Constraint_System_begin (pcs, cit),
727 ppl_Constraint_System_end (pcs, cend);
728 !ppl_Constraint_System_const_iterator_equal_test (cit, cend);
729 ppl_Constraint_System_const_iterator_increment (cit))
730 {
731 ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
732 ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
733 ppl_Linear_Expression_coefficient (expr, iter, coef);
734 ppl_Coefficient_to_mpz_t (coef, val);
735
736 if (value_zero_p (val))
737 {
738 ppl_delete_Linear_Expression (expr);
739 continue;
740 }
741
742 for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
743 {
744 ppl_dimension_type scatter = psct_scattering_dim (pbb, i);
745
746 ppl_Linear_Expression_coefficient (expr, scatter, coef);
747 ppl_Coefficient_to_mpz_t (coef, val);
748
749 if (value_notzero_p (val))
750 {
751 value_clear (val);
752 ppl_delete_Linear_Expression (expr);
753 ppl_delete_Coefficient (coef);
754 ppl_delete_Constraint_System_const_iterator (cit);
755 ppl_delete_Constraint_System_const_iterator (cend);
756
757 return scatter;
758 }
759 }
760 }
761
762 gcc_unreachable ();
763 }
764
765 /* Returns the number of iterations NITER of the loop around PBB at
766 depth LOOP_DEPTH. */
767
768 void
769 pbb_number_of_iterations (poly_bb_p pbb,
770 graphite_dim_t loop_depth,
771 Value niter)
772 {
773 ppl_Linear_Expression_t le;
774 ppl_dimension_type dim;
775
776 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
777 ppl_new_Linear_Expression_with_dimension (&le, dim);
778 ppl_set_coef (le, pbb_iterator_dim (pbb, loop_depth), 1);
779 value_set_si (niter, -1);
780 ppl_max_for_le_pointset (PBB_DOMAIN (pbb), le, niter);
781 ppl_delete_Linear_Expression (le);
782 }
783
784 /* Returns the number of iterations NITER of the loop around PBB at
785 time(scattering) dimension TIME_DEPTH. */
786
787 void
788 pbb_number_of_iterations_at_time (poly_bb_p pbb,
789 graphite_dim_t time_depth,
790 Value niter)
791 {
792 ppl_Pointset_Powerset_C_Polyhedron_t ext_domain, sctr;
793 ppl_Linear_Expression_t le;
794 ppl_dimension_type dim;
795
796 /* Takes together domain and scattering polyhedrons, and composes
797 them into the bigger polyhedron that has the following format:
798
799 t0..t_{n-1} | l0..l_{nlcl-1} | i0..i_{niter-1} | g0..g_{nparm-1}
800
801 where
802 | t0..t_{n-1} are time dimensions (scattering dimensions)
803 | l0..l_{nclc-1} are local variables in scattering function
804 | i0..i_{niter-1} are original iteration variables
805 | g0..g_{nparam-1} are global parameters. */
806
807 ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&sctr,
808 PBB_TRANSFORMED_SCATTERING (pbb));
809
810 /* Extend the iteration domain with the scattering dimensions:
811 0..0 | 0..0 | i0..i_{niter-1} | g0..g_{nparm-1}. */
812 ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
813 (&ext_domain, PBB_DOMAIN (pbb));
814 ppl_insert_dimensions_pointset (ext_domain, 0,
815 pbb_nb_scattering_transform (pbb)
816 + pbb_nb_local_vars (pbb));
817
818 /* Add to sctr the extended domain. */
819 ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (sctr, ext_domain);
820
821 /* Extract the number of iterations. */
822 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (sctr, &dim);
823 ppl_new_Linear_Expression_with_dimension (&le, dim);
824 ppl_set_coef (le, time_depth, 1);
825 value_set_si (niter, -1);
826 ppl_max_for_le_pointset (sctr, le, niter);
827
828 ppl_delete_Linear_Expression (le);
829 ppl_delete_Pointset_Powerset_C_Polyhedron (sctr);
830 ppl_delete_Pointset_Powerset_C_Polyhedron (ext_domain);
831 }
832
833 /* Translates LOOP to LST. */
834
835 static lst_p
836 loop_to_lst (loop_p loop, VEC (poly_bb_p, heap) *bbs, int *i)
837 {
838 poly_bb_p pbb;
839 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
840
841 for (; VEC_iterate (poly_bb_p, bbs, *i, pbb); (*i)++)
842 {
843 lst_p stmt;
844 basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
845
846 if (bb->loop_father == loop)
847 stmt = new_lst_stmt (pbb);
848 else if (flow_bb_inside_loop_p (loop, bb))
849 {
850 loop_p next = loop->inner;
851
852 while (next && !flow_bb_inside_loop_p (next, bb))
853 next = next->next;
854
855 stmt = loop_to_lst (next, bbs, i);
856 }
857 else
858 {
859 (*i)--;
860 return new_lst_loop (seq);
861 }
862
863 VEC_safe_push (lst_p, heap, seq, stmt);
864 }
865
866 return new_lst_loop (seq);
867 }
868
869 /* Reads the original scattering of the SCOP and returns an LST
870 representing it. */
871
872 void
873 scop_to_lst (scop_p scop)
874 {
875 lst_p res;
876 int i, n = VEC_length (poly_bb_p, SCOP_BBS (scop));
877 VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 5);
878 sese region = SCOP_REGION (scop);
879
880 for (i = 0; i < n; i++)
881 {
882 poly_bb_p pbb = VEC_index (poly_bb_p, SCOP_BBS (scop), i);
883 loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
884
885 if (loop_in_sese_p (loop, region))
886 res = loop_to_lst (loop, SCOP_BBS (scop), &i);
887 else
888 res = new_lst_stmt (pbb);
889
890 VEC_safe_push (lst_p, heap, seq, res);
891 }
892
893 res = new_lst_loop (seq);
894 SCOP_ORIGINAL_SCHEDULE (scop) = res;
895 SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
896 }
897
898 /* Print LST to FILE with INDENT spaces of indentation. */
899
900 void
901 print_lst (FILE *file, lst_p lst, int indent)
902 {
903 if (!lst)
904 return;
905
906 indent_to (file, indent);
907
908 if (LST_LOOP_P (lst))
909 {
910 int i;
911 lst_p l;
912
913 if (LST_LOOP_FATHER (lst))
914 fprintf (file, "%d (loop", lst_dewey_number (lst));
915 else
916 fprintf (file, "(root");
917
918 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
919 print_lst (file, l, indent + 2);
920
921 fprintf (file, ")");
922 }
923 else
924 fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
925 }
926
927 /* Print LST to STDERR. */
928
929 void
930 debug_lst (lst_p lst)
931 {
932 print_lst (stderr, lst, 0);
933 }
934
935 /* Pretty print to FILE the loop statement tree LST in DOT format. */
936
937 static void
938 dot_lst_1 (FILE *file, lst_p lst)
939 {
940 if (!lst)
941 return;
942
943 if (LST_LOOP_P (lst))
944 {
945 int i;
946 lst_p l;
947
948 if (!LST_LOOP_FATHER (lst))
949 fprintf (file, "L -> L_%d_%d\n",
950 lst_depth (lst),
951 lst_dewey_number (lst));
952 else
953 fprintf (file, "L_%d_%d -> L_%d_%d\n",
954 lst_depth (LST_LOOP_FATHER (lst)),
955 lst_dewey_number (LST_LOOP_FATHER (lst)),
956 lst_depth (lst),
957 lst_dewey_number (lst));
958
959 for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
960 dot_lst_1 (file, l);
961 }
962
963 else
964 fprintf (file, "L_%d_%d -> S_%d\n",
965 lst_depth (LST_LOOP_FATHER (lst)),
966 lst_dewey_number (LST_LOOP_FATHER (lst)),
967 pbb_index (LST_PBB (lst)));
968
969 }
970
971 /* Display the LST using dotty. */
972
973 void
974 dot_lst (lst_p lst)
975 {
976 /* When debugging, enable the following code. This cannot be used
977 in production compilers because it calls "system". */
978 #if 0
979 int x;
980 FILE *stream = fopen ("/tmp/lst.dot", "w");
981 gcc_assert (stream);
982
983 fputs ("digraph all {\n", stream);
984 dot_lst_1 (stream, lst);
985 fputs ("}\n\n", stream);
986 fclose (stream);
987
988 x = system ("dotty /tmp/lst.dot");
989 #else
990 fputs ("digraph all {\n", stderr);
991 dot_lst_1 (stderr, lst);
992 fputs ("}\n\n", stderr);
993
994 #endif
995 }
996
997 #endif
998