Add -march=interaptiv.
[platform/upstream/gcc.git] / gcc / graphite-poly.c
1 /* Graphite polyhedral representation.
2    Copyright (C) 2009-2015 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
22 #include "config.h"
23
24 #ifdef HAVE_isl
25 /* Workaround for GMP 5.1.3 bug, see PR56019.  */
26 #include <stddef.h>
27
28 #include <isl/constraint.h>
29 #include <isl/set.h>
30 #include <isl/map.h>
31 #include <isl/union_map.h>
32 #include <isl/constraint.h>
33 #include <isl/ilp.h>
34 #include <isl/aff.h>
35 #include <isl/val.h>
36
37 /* Since ISL-0.13, the extern is in val_gmp.h.  */
38 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
39 extern "C" {
40 #endif
41 #include <isl/val_gmp.h>
42 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
43 }
44 #endif
45
46 #include "system.h"
47 #include "coretypes.h"
48 #include "backend.h"
49 #include "diagnostic-core.h"
50 #include "cfghooks.h"
51 #include "tree.h"
52 #include "gimple.h"
53 #include "fold-const.h"
54 #include "gimple-iterator.h"
55 #include "tree-ssa-loop.h"
56 #include "gimple-pretty-print.h"
57 #include "cfgloop.h"
58 #include "tree-data-ref.h"
59 #include "graphite-poly.h"
60
61 #define OPENSCOP_MAX_STRING 256
62
63
64 /* Print to STDERR the GMP value VAL.  */
65
66 DEBUG_FUNCTION void
67 debug_gmp_value (mpz_t val)
68 {
69   gmp_fprintf (stderr, "%Zd", val);
70 }
71
72 /* Return the maximal loop depth in SCOP.  */
73
74 int
75 scop_max_loop_depth (scop_p scop)
76 {
77   int i;
78   poly_bb_p pbb;
79   int max_nb_loops = 0;
80
81   FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
82     {
83       int nb_loops = pbb_dim_iter_domain (pbb);
84       if (max_nb_loops < nb_loops)
85         max_nb_loops = nb_loops;
86     }
87
88   return max_nb_loops;
89 }
90
91 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
92    level.  */
93
94 static void
95 print_scattering_function_1 (FILE *file, poly_bb_p pbb, int verbosity)
96 {
97   graphite_dim_t i;
98
99   if (verbosity > 0)
100     {
101       fprintf (file, "# scattering bb_%d (\n", pbb_index (pbb));
102       fprintf (file, "#eq");
103
104       for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
105         fprintf (file, "     s%d", (int) i);
106
107       for (i = 0; i < pbb_nb_local_vars (pbb); i++)
108         fprintf (file, "    lv%d", (int) i);
109
110       for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
111         fprintf (file, "     i%d", (int) i);
112
113       for (i = 0; i < pbb_nb_params (pbb); i++)
114         fprintf (file, "     p%d", (int) i);
115
116       fprintf (file, "    cst\n");
117     }
118
119   fprintf (file, "isl\n");
120   print_isl_map (file, pbb->transformed ? pbb->transformed : pbb->schedule);
121
122   if (verbosity > 0)
123     fprintf (file, "#)\n");
124 }
125
126 /* Prints to FILE the scattering function of PBB, at some VERBOSITY
127    level.  */
128
129 void
130 print_scattering_function (FILE *file, poly_bb_p pbb, int verbosity)
131 {
132   if (!PBB_TRANSFORMED (pbb))
133     return;
134
135   if (pbb->schedule || pbb->transformed)
136     {
137       if (verbosity > 0)
138         fprintf (file, "# Scattering function is provided\n");
139
140       fprintf (file, "1\n");
141     }
142   else
143     {
144       if (verbosity > 0)
145         fprintf (file, "# Scattering function is not provided\n");
146
147       fprintf (file, "0\n");
148       return;
149     }
150
151   print_scattering_function_1 (file, pbb, verbosity);
152
153   if (verbosity > 0)
154     fprintf (file, "# Scattering names are not provided\n");
155
156   fprintf (file, "0\n");
157
158 }
159
160 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
161    level.  */
162
163 void
164 print_iteration_domain (FILE *file, poly_bb_p pbb, int verbosity)
165 {
166   print_pbb_domain (file, pbb, verbosity);
167 }
168
169 /* Prints to FILE the scattering functions of every PBB of SCOP.  */
170
171 void
172 print_scattering_functions (FILE *file, scop_p scop, int verbosity)
173 {
174   int i;
175   poly_bb_p pbb;
176
177   FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
178     print_scattering_function (file, pbb, verbosity);
179 }
180
181 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
182    VERBOSITY level.  */
183
184 void
185 print_iteration_domains (FILE *file, scop_p scop, int verbosity)
186 {
187   int i;
188   poly_bb_p pbb;
189
190   FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
191     print_iteration_domain (file, pbb, verbosity);
192 }
193
194 /* Prints to STDERR the scattering function of PBB, at some VERBOSITY
195    level.  */
196
197 DEBUG_FUNCTION void
198 debug_scattering_function (poly_bb_p pbb, int verbosity)
199 {
200   print_scattering_function (stderr, pbb, verbosity);
201 }
202
203 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
204    level.  */
205
206 DEBUG_FUNCTION void
207 debug_iteration_domain (poly_bb_p pbb, int verbosity)
208 {
209   print_iteration_domain (stderr, pbb, verbosity);
210 }
211
212 /* Prints to STDERR the scattering functions of every PBB of SCOP, at
213    some VERBOSITY level.  */
214
215 DEBUG_FUNCTION void
216 debug_scattering_functions (scop_p scop, int verbosity)
217 {
218   print_scattering_functions (stderr, scop, verbosity);
219 }
220
221 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
222    some VERBOSITY level.  */
223
224 DEBUG_FUNCTION void
225 debug_iteration_domains (scop_p scop, int verbosity)
226 {
227   print_iteration_domains (stderr, scop, verbosity);
228 }
229
230 /* Apply graphite transformations to all the basic blocks of SCOP.  */
231
232 bool
233 apply_poly_transforms (scop_p scop)
234 {
235   bool transform_done = false;
236
237   /* Generate code even if we did not apply any real transformation.
238      This also allows to check the performance for the identity
239      transformation: GIMPLE -> GRAPHITE -> GIMPLE
240      Keep in mind that CLooG optimizes in control, so the loop structure
241      may change, even if we only use -fgraphite-identity.  */
242   if (flag_graphite_identity)
243     transform_done = true;
244
245   if (flag_loop_parallelize_all)
246     transform_done = true;
247
248   if (flag_loop_block)
249     transform_done |= scop_do_block (scop);
250   else
251     {
252       if (flag_loop_strip_mine)
253         transform_done |= scop_do_strip_mine (scop, 0);
254
255       if (flag_loop_interchange)
256         transform_done |= scop_do_interchange (scop);
257     }
258
259   /* This pass needs to be run at the final stage, as it does not
260      update the lst.  */
261   if (flag_loop_optimize_isl || flag_loop_unroll_jam)
262     transform_done |= optimize_isl (scop);
263
264   return transform_done;
265 }
266
267 /* Create a new polyhedral data reference and add it to PBB.  It is
268    defined by its ACCESSES, its TYPE, and the number of subscripts
269    NB_SUBSCRIPTS.  */
270
271 void
272 new_poly_dr (poly_bb_p pbb, int dr_base_object_set,
273              enum poly_dr_type type, void *cdr, graphite_dim_t nb_subscripts,
274              isl_map *acc, isl_set *subscript_sizes)
275 {
276   static int id = 0;
277   poly_dr_p pdr = XNEW (struct poly_dr);
278
279   PDR_ID (pdr) = id++;
280   PDR_BASE_OBJECT_SET (pdr) = dr_base_object_set;
281   PDR_NB_REFS (pdr) = 1;
282   PDR_PBB (pdr) = pbb;
283   pdr->accesses = acc;
284   pdr->subscript_sizes = subscript_sizes;
285   PDR_TYPE (pdr) = type;
286   PDR_CDR (pdr) = cdr;
287   PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
288   PBB_DRS (pbb).safe_push (pdr);
289 }
290
291 /* Free polyhedral data reference PDR.  */
292
293 void
294 free_poly_dr (poly_dr_p pdr)
295 {
296   isl_map_free (pdr->accesses);
297   isl_set_free (pdr->subscript_sizes);
298   XDELETE (pdr);
299 }
300
301 /* Create a new polyhedral black box.  */
302
303 poly_bb_p
304 new_poly_bb (scop_p scop, void *black_box)
305 {
306   poly_bb_p pbb = XNEW (struct poly_bb);
307
308   pbb->domain = NULL;
309   pbb->schedule = NULL;
310   pbb->transformed = NULL;
311   pbb->saved = NULL;
312   pbb->map_sepclass = NULL;
313   PBB_SCOP (pbb) = scop;
314   pbb_set_black_box (pbb, black_box);
315   PBB_TRANSFORMED (pbb) = NULL;
316   PBB_SAVED (pbb) = NULL;
317   PBB_ORIGINAL (pbb) = NULL;
318   PBB_DRS (pbb).create (3);
319   PBB_IS_REDUCTION (pbb) = false;
320   GBB_PBB ((gimple_bb_p) black_box) = pbb;
321
322   return pbb;
323 }
324
325 /* Free polyhedral black box.  */
326
327 void
328 free_poly_bb (poly_bb_p pbb)
329 {
330   int i;
331   poly_dr_p pdr;
332
333   isl_set_free (pbb->domain);
334   isl_map_free (pbb->schedule);
335   isl_map_free (pbb->transformed);
336   isl_map_free (pbb->saved);
337
338   if (PBB_DRS (pbb).exists ())
339     FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
340       free_poly_dr (pdr);
341
342   PBB_DRS (pbb).release ();
343   XDELETE (pbb);
344 }
345
346 static void
347 print_pdr_access_layout (FILE *file, poly_bb_p pbb, poly_dr_p pdr)
348 {
349   graphite_dim_t i;
350
351   fprintf (file, "#  eq");
352
353   fprintf (file, "   alias");
354
355   for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
356     fprintf (file, "   sub%d", (int) i);
357
358   for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
359     fprintf (file, "     i%d", (int) i);
360
361   for (i = 0; i < pbb_nb_params (pbb); i++)
362     fprintf (file, "     p%d", (int) i);
363
364   fprintf (file, "    cst\n");
365 }
366
367 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
368    level.  */
369
370 void
371 print_pdr (FILE *file, poly_dr_p pdr, int verbosity)
372 {
373   if (verbosity > 1)
374     {
375       fprintf (file, "# pdr_%d (", PDR_ID (pdr));
376
377       switch (PDR_TYPE (pdr))
378         {
379         case PDR_READ:
380           fprintf (file, "read \n");
381           break;
382
383         case PDR_WRITE:
384           fprintf (file, "write \n");
385           break;
386
387         case PDR_MAY_WRITE:
388           fprintf (file, "may_write \n");
389           break;
390
391         default:
392           gcc_unreachable ();
393         }
394
395       dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
396     }
397
398   if (verbosity > 0)
399     {
400       fprintf (file, "# data accesses (\n");
401       print_pdr_access_layout (file, PDR_PBB (pdr), pdr);
402     }
403
404   /* XXX isl dump accesses/subscripts */
405
406   if (verbosity > 0)
407     fprintf (file, "#)\n");
408
409   if (verbosity > 1)
410     fprintf (file, "#)\n");
411 }
412
413 /* Prints to STDERR the polyhedral data reference PDR, at some
414    VERBOSITY level.  */
415
416 DEBUG_FUNCTION void
417 debug_pdr (poly_dr_p pdr, int verbosity)
418 {
419   print_pdr (stderr, pdr, verbosity);
420 }
421
422 /* Creates a new SCOP containing REGION.  */
423
424 scop_p
425 new_scop (void *region)
426 {
427   scop_p scop = XNEW (struct scop);
428
429   scop->context = NULL;
430   scop->must_raw = NULL;
431   scop->may_raw = NULL;
432   scop->must_raw_no_source = NULL;
433   scop->may_raw_no_source = NULL;
434   scop->must_war = NULL;
435   scop->may_war = NULL;
436   scop->must_war_no_source = NULL;
437   scop->may_war_no_source = NULL;
438   scop->must_waw = NULL;
439   scop->may_waw = NULL;
440   scop->must_waw_no_source = NULL;
441   scop->may_waw_no_source = NULL;
442   scop_set_region (scop, region);
443   SCOP_BBS (scop).create (3);
444   SCOP_ORIGINAL_SCHEDULE (scop) = NULL;
445   SCOP_TRANSFORMED_SCHEDULE (scop) = NULL;
446   SCOP_SAVED_SCHEDULE (scop) = NULL;
447   POLY_SCOP_P (scop) = false;
448
449   return scop;
450 }
451
452 /* Deletes SCOP.  */
453
454 void
455 free_scop (scop_p scop)
456 {
457   int i;
458   poly_bb_p pbb;
459
460   FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
461     free_poly_bb (pbb);
462
463   SCOP_BBS (scop).release ();
464
465   isl_set_free (scop->context);
466   isl_union_map_free (scop->must_raw);
467   isl_union_map_free (scop->may_raw);
468   isl_union_map_free (scop->must_raw_no_source);
469   isl_union_map_free (scop->may_raw_no_source);
470   isl_union_map_free (scop->must_war);
471   isl_union_map_free (scop->may_war);
472   isl_union_map_free (scop->must_war_no_source);
473   isl_union_map_free (scop->may_war_no_source);
474   isl_union_map_free (scop->must_waw);
475   isl_union_map_free (scop->may_waw);
476   isl_union_map_free (scop->must_waw_no_source);
477   isl_union_map_free (scop->may_waw_no_source);
478   free_lst (SCOP_ORIGINAL_SCHEDULE (scop));
479   free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
480   free_lst (SCOP_SAVED_SCHEDULE (scop));
481   XDELETE (scop);
482 }
483
484 /* Print to FILE the domain of PBB in OpenScop format, at some VERBOSITY
485    level.  */
486
487 static void
488 openscop_print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity)
489 {
490   graphite_dim_t i;
491   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
492
493   if (!pbb->domain)
494     return;
495
496   if (verbosity > 0)
497     {
498       fprintf (file, "\n# Iteration domain of bb_%d (\n", GBB_BB (gbb)->index);
499       fprintf (file, "#eq");
500
501       for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
502         fprintf (file, "     i%d", (int) i);
503
504       for (i = 0; i < pbb_nb_params (pbb); i++)
505         fprintf (file, "     p%d", (int) i);
506
507       fprintf (file, "    cst\n");
508     }
509
510   fprintf (file, "XXX isl\n");
511
512   if (verbosity > 0)
513     fprintf (file, "#)\n");
514 }
515
516 /* Print to FILE the domain of PBB, at some VERBOSITY level.  */
517
518 void
519 print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity ATTRIBUTE_UNUSED)
520 {
521   print_isl_set (file, pbb->domain);
522 }
523
524 /* Dump the cases of a graphite basic block GBB on FILE.  */
525
526 static void
527 dump_gbb_cases (FILE *file, gimple_bb_p gbb)
528 {
529   int i;
530   gimple stmt;
531   vec<gimple> cases;
532
533   if (!gbb)
534     return;
535
536   cases = GBB_CONDITION_CASES (gbb);
537   if (cases.is_empty ())
538     return;
539
540   fprintf (file, "# cases bb_%d (\n", GBB_BB (gbb)->index);
541
542   FOR_EACH_VEC_ELT (cases, i, stmt)
543     {
544       fprintf (file, "# ");
545       print_gimple_stmt (file, stmt, 0, 0);
546     }
547
548   fprintf (file, "#)\n");
549 }
550
551 /* Dump conditions of a graphite basic block GBB on FILE.  */
552
553 static void
554 dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
555 {
556   int i;
557   gimple stmt;
558   vec<gimple> conditions;
559
560   if (!gbb)
561     return;
562
563   conditions = GBB_CONDITIONS (gbb);
564   if (conditions.is_empty ())
565     return;
566
567   fprintf (file, "# conditions bb_%d (\n", GBB_BB (gbb)->index);
568
569   FOR_EACH_VEC_ELT (conditions, i, stmt)
570     {
571       fprintf (file, "# ");
572       print_gimple_stmt (file, stmt, 0, 0);
573     }
574
575   fprintf (file, "#)\n");
576 }
577
578 /* Print to FILE all the data references of PBB, at some VERBOSITY
579    level.  */
580
581 void
582 print_pdrs (FILE *file, poly_bb_p pbb, int verbosity)
583 {
584   int i;
585   poly_dr_p pdr;
586   int nb_reads = 0;
587   int nb_writes = 0;
588
589   if (PBB_DRS (pbb).length () == 0)
590     {
591       if (verbosity > 0)
592         fprintf (file, "# Access informations are not provided\n");\
593       fprintf (file, "0\n");
594       return;
595     }
596
597   if (verbosity > 1)
598     fprintf (file, "# Data references (\n");
599
600   if (verbosity > 0)
601     fprintf (file, "# Access informations are provided\n");
602   fprintf (file, "1\n");
603
604   FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
605     if (PDR_TYPE (pdr) == PDR_READ)
606       nb_reads++;
607     else
608       nb_writes++;
609
610   if (verbosity > 1)
611     fprintf (file, "# Read data references (\n");
612
613   if (verbosity > 0)
614     fprintf (file, "# Read access informations\n");
615   fprintf (file, "%d\n", nb_reads);
616
617   FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
618     if (PDR_TYPE (pdr) == PDR_READ)
619       print_pdr (file, pdr, verbosity);
620
621   if (verbosity > 1)
622     fprintf (file, "#)\n");
623
624   if (verbosity > 1)
625     fprintf (file, "# Write data references (\n");
626
627   if (verbosity > 0)
628     fprintf (file, "# Write access informations\n");
629   fprintf (file, "%d\n", nb_writes);
630
631   FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
632     if (PDR_TYPE (pdr) != PDR_READ)
633       print_pdr (file, pdr, verbosity);
634
635   if (verbosity > 1)
636     fprintf (file, "#)\n");
637
638   if (verbosity > 1)
639     fprintf (file, "#)\n");
640 }
641
642 /* Print to STDERR all the data references of PBB.  */
643
644 DEBUG_FUNCTION void
645 debug_pdrs (poly_bb_p pbb, int verbosity)
646 {
647   print_pdrs (stderr, pbb, verbosity);
648 }
649
650 /* Print to FILE the body of PBB, at some VERBOSITY level.
651    If statement_body_provided is false statement body is not printed.  */
652
653 static void
654 print_pbb_body (FILE *file, poly_bb_p pbb, int verbosity,
655                 bool statement_body_provided)
656 {
657   if (verbosity > 1)
658     fprintf (file, "# Body (\n");
659
660   if (!statement_body_provided)
661     {
662       if (verbosity > 0)
663         fprintf (file, "# Statement body is not provided\n");
664
665       fprintf (file, "0\n");
666
667       if (verbosity > 1)
668         fprintf (file, "#)\n");
669       return;
670     }
671
672   if (verbosity > 0)
673     fprintf (file, "# Statement body is provided\n");
674   fprintf (file, "1\n");
675
676   if (verbosity > 0)
677     fprintf (file, "# Original iterator names\n# Iterator names are not provided yet.\n");
678
679   if (verbosity > 0)
680     fprintf (file, "# Statement body\n");
681
682   fprintf (file, "{\n");
683   dump_bb (file, pbb_bb (pbb), 0, 0);
684   fprintf (file, "}\n");
685
686   if (verbosity > 1)
687     fprintf (file, "#)\n");
688 }
689
690 /* Print to FILE the domain and scattering function of PBB, at some
691    VERBOSITY level.  */
692
693 void
694 print_pbb (FILE *file, poly_bb_p pbb, int verbosity)
695 {
696   if (verbosity > 1)
697     {
698       fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
699       dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
700       dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
701     }
702
703   openscop_print_pbb_domain (file, pbb, verbosity);
704   print_scattering_function (file, pbb, verbosity);
705   print_pdrs (file, pbb, verbosity);
706   print_pbb_body (file, pbb, verbosity, false);
707
708   if (verbosity > 1)
709     fprintf (file, "#)\n");
710 }
711
712 /* Print to FILE the parameters of SCOP, at some VERBOSITY level.  */
713
714 void
715 print_scop_params (FILE *file, scop_p scop, int verbosity)
716 {
717   int i;
718   tree t;
719
720   if (verbosity > 1)
721     fprintf (file, "# parameters (\n");
722
723   if (SESE_PARAMS (SCOP_REGION (scop)).length ())
724     {
725       if (verbosity > 0)
726         fprintf (file, "# Parameter names are provided\n");
727
728       fprintf (file, "1\n");
729
730       if (verbosity > 0)
731         fprintf (file, "# Parameter names\n");
732     }
733   else
734     {
735       if (verbosity > 0)
736         fprintf (file, "# Parameter names are not provided\n");
737       fprintf (file, "0\n");
738     }
739
740   FOR_EACH_VEC_ELT (SESE_PARAMS (SCOP_REGION (scop)), i, t)
741     {
742       print_generic_expr (file, t, 0);
743       fprintf (file, " ");
744     }
745
746   fprintf (file, "\n");
747
748   if (verbosity > 1)
749     fprintf (file, "#)\n");
750 }
751
752 /* Print to FILE the context of SCoP in OpenScop format, at some VERBOSITY
753    level.  */
754
755 static void
756 openscop_print_scop_context (FILE *file, scop_p scop, int verbosity)
757 {
758   graphite_dim_t i;
759
760   if (verbosity > 0)
761     {
762       fprintf (file, "# Context (\n");
763       fprintf (file, "#eq");
764
765       for (i = 0; i < scop_nb_params (scop); i++)
766         fprintf (file, "     p%d", (int) i);
767
768       fprintf (file, "    cst\n");
769     }
770
771   if (scop->context)
772     /* XXX isl print context */
773     fprintf (file, "XXX isl\n");
774   else
775     fprintf (file, "0 %d 0 0 0 %d\n", (int) scop_nb_params (scop) + 2,
776              (int) scop_nb_params (scop));
777
778   if (verbosity > 0)
779     fprintf (file, "# )\n");
780 }
781
782 /* Print to FILE the context of SCoP, at some VERBOSITY level.  */
783
784 void
785 print_scop_context (FILE *file, scop_p scop, int verbosity)
786 {
787   graphite_dim_t i;
788
789   if (verbosity > 0)
790     {
791       fprintf (file, "# Context (\n");
792       fprintf (file, "#eq");
793
794       for (i = 0; i < scop_nb_params (scop); i++)
795         fprintf (file, "     p%d", (int) i);
796
797       fprintf (file, "    cst\n");
798     }
799
800   if (scop->context)
801     print_isl_set (file, scop->context);
802   else
803     fprintf (file, "no isl context %d\n", (int) scop_nb_params (scop) + 2);
804
805   if (verbosity > 0)
806     fprintf (file, "# )\n");
807 }
808
809 /* Print to FILE the SCOP, at some VERBOSITY level.  */
810
811 void
812 print_scop (FILE *file, scop_p scop, int verbosity)
813 {
814   int i;
815   poly_bb_p pbb;
816
817   fprintf (file, "SCoP 1\n#(\n");
818   fprintf (file, "# Language\nGimple\n");
819   openscop_print_scop_context (file, scop, verbosity);
820   print_scop_params (file, scop, verbosity);
821
822   if (verbosity > 0)
823     fprintf (file, "# Number of statements\n");
824
825   fprintf (file, "%d\n", SCOP_BBS (scop).length ());
826
827   FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
828     print_pbb (file, pbb, verbosity);
829
830   if (verbosity > 1)
831     {
832       fprintf (file, "# original_lst (\n");
833       print_lst (file, SCOP_ORIGINAL_SCHEDULE (scop), 0);
834       fprintf (file, "\n#)\n");
835
836       fprintf (file, "# transformed_lst (\n");
837       print_lst (file, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
838       fprintf (file, "\n#)\n");
839     }
840
841   fprintf (file, "#)\n");
842 }
843
844 /* Print to STDERR the domain of PBB, at some VERBOSITY level.  */
845
846 DEBUG_FUNCTION void
847 debug_pbb_domain (poly_bb_p pbb, int verbosity)
848 {
849   print_pbb_domain (stderr, pbb, verbosity);
850 }
851
852 /* Print to FILE the domain and scattering function of PBB, at some
853    VERBOSITY level.  */
854
855 DEBUG_FUNCTION void
856 debug_pbb (poly_bb_p pbb, int verbosity)
857 {
858   print_pbb (stderr, pbb, verbosity);
859 }
860
861 /* Print to STDERR the context of SCOP, at some VERBOSITY level.  */
862
863 DEBUG_FUNCTION void
864 debug_scop_context (scop_p scop, int verbosity)
865 {
866   print_scop_context (stderr, scop, verbosity);
867 }
868
869 /* Print to STDERR the SCOP, at some VERBOSITY level.  */
870
871 DEBUG_FUNCTION void
872 debug_scop (scop_p scop, int verbosity)
873 {
874   print_scop (stderr, scop, verbosity);
875 }
876
877 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
878    level.  */
879
880 DEBUG_FUNCTION void
881 debug_scop_params (scop_p scop, int verbosity)
882 {
883   print_scop_params (stderr, scop, verbosity);
884 }
885
886 extern isl_ctx *the_isl_ctx;
887 void
888 print_isl_set (FILE *f, isl_set *set)
889 {
890   isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
891   p = isl_printer_print_set (p, set);
892   isl_printer_free (p);
893 }
894
895 DEBUG_FUNCTION void
896 debug_isl_set (isl_set *set)
897 {
898   print_isl_set (stderr, set);
899 }
900
901 void
902 print_isl_map (FILE *f, isl_map *map)
903 {
904   isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
905   p = isl_printer_print_map (p, map);
906   isl_printer_free (p);
907 }
908
909 DEBUG_FUNCTION void
910 debug_isl_map (isl_map *map)
911 {
912   print_isl_map (stderr, map);
913 }
914
915 void
916 print_isl_aff (FILE *f, isl_aff *aff)
917 {
918   isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
919   p = isl_printer_print_aff (p, aff);
920   isl_printer_free (p);
921 }
922
923 DEBUG_FUNCTION void
924 debug_isl_aff (isl_aff *aff)
925 {
926   print_isl_aff (stderr, aff);
927 }
928
929 void
930 print_isl_constraint (FILE *f, isl_constraint *c)
931 {
932   isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
933   p = isl_printer_print_constraint (p, c);
934   isl_printer_free (p);
935 }
936
937 DEBUG_FUNCTION void
938 debug_isl_constraint (isl_constraint *c)
939 {
940   print_isl_constraint (stderr, c);
941 }
942
943 /* Returns the number of iterations RES of the loop around PBB at
944    time(scattering) dimension TIME_DEPTH.  */
945
946 void
947 pbb_number_of_iterations_at_time (poly_bb_p pbb,
948                                   graphite_dim_t time_depth,
949                                   mpz_t res)
950 {
951   isl_set *transdomain;
952   isl_space *dc;
953   isl_aff *aff;
954   isl_val *isllb, *islub;
955
956   /* Map the iteration domain through the current scatter, and work
957      on the resulting set.  */
958   transdomain = isl_set_apply (isl_set_copy (pbb->domain),
959                                isl_map_copy (pbb->transformed));
960
961   /* Select the time_depth' dimension via an affine expression.  */
962   dc = isl_set_get_space (transdomain);
963   aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
964   aff = isl_aff_set_coefficient_si (aff, isl_dim_in, time_depth, 1);
965
966   /* And find the min/max for that function.  */
967   /* XXX isl check results?  */
968   isllb = isl_set_min_val (transdomain, aff);
969   islub = isl_set_max_val (transdomain, aff);
970
971   islub = isl_val_sub (islub, isllb);
972   islub = isl_val_add_ui (islub, 1);
973   isl_val_get_num_gmp (islub, res);
974
975   isl_val_free (islub);
976   isl_aff_free (aff);
977   isl_set_free (transdomain);
978 }
979
980 /* Translates LOOP to LST.  */
981
982 static lst_p
983 loop_to_lst (loop_p loop, vec<poly_bb_p> bbs, int *i)
984 {
985   poly_bb_p pbb;
986   vec<lst_p> seq;
987   seq.create (5);
988
989   for (; bbs.iterate (*i, &pbb); (*i)++)
990     {
991       lst_p stmt;
992       basic_block bb = GBB_BB (PBB_BLACK_BOX (pbb));
993
994       if (bb->loop_father == loop)
995         stmt = new_lst_stmt (pbb);
996       else if (flow_bb_inside_loop_p (loop, bb))
997         {
998           loop_p next = loop->inner;
999
1000           while (next && !flow_bb_inside_loop_p (next, bb))
1001             next = next->next;
1002
1003           stmt = loop_to_lst (next, bbs, i);
1004         }
1005       else
1006         {
1007           (*i)--;
1008           return new_lst_loop (seq);
1009         }
1010
1011       seq.safe_push (stmt);
1012     }
1013
1014   return new_lst_loop (seq);
1015 }
1016
1017 /* Reads the original scattering of the SCOP and returns an LST
1018    representing it.  */
1019
1020 void
1021 scop_to_lst (scop_p scop)
1022 {
1023   lst_p res;
1024   int i, n = SCOP_BBS (scop).length ();
1025   vec<lst_p> seq;
1026   seq.create (5);
1027   sese region = SCOP_REGION (scop);
1028
1029   for (i = 0; i < n; i++)
1030     {
1031       poly_bb_p pbb = SCOP_BBS (scop)[i];
1032       loop_p loop = outermost_loop_in_sese (region, GBB_BB (PBB_BLACK_BOX (pbb)));
1033
1034       if (loop_in_sese_p (loop, region))
1035         res = loop_to_lst (loop, SCOP_BBS (scop), &i);
1036       else
1037         res = new_lst_stmt (pbb);
1038
1039       seq.safe_push (res);
1040     }
1041
1042   res = new_lst_loop (seq);
1043   SCOP_ORIGINAL_SCHEDULE (scop) = res;
1044   SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (res);
1045 }
1046
1047 /* Print to FILE on a new line COLUMN white spaces.  */
1048
1049 static void
1050 lst_indent_to (FILE *file, int column)
1051 {
1052   int i;
1053
1054   if (column > 0)
1055     fprintf (file, "\n#");
1056
1057   for (i = 0; i < column; i++)
1058     fprintf (file, " ");
1059 }
1060
1061 /* Print LST to FILE with INDENT spaces of indentation.  */
1062
1063 void
1064 print_lst (FILE *file, lst_p lst, int indent)
1065 {
1066   if (!lst)
1067     return;
1068
1069   lst_indent_to (file, indent);
1070
1071   if (LST_LOOP_P (lst))
1072     {
1073       int i;
1074       lst_p l;
1075
1076       if (LST_LOOP_FATHER (lst))
1077         fprintf (file, "%d (loop", lst_dewey_number (lst));
1078       else
1079         fprintf (file, "#(root");
1080
1081       FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1082         print_lst (file, l, indent + 2);
1083
1084       fprintf (file, ")");
1085     }
1086   else
1087     fprintf (file, "%d stmt_%d", lst_dewey_number (lst), pbb_index (LST_PBB (lst)));
1088 }
1089
1090 /* Print LST to STDERR.  */
1091
1092 DEBUG_FUNCTION void
1093 debug_lst (lst_p lst)
1094 {
1095   print_lst (stderr, lst, 0);
1096 }
1097
1098 /* Pretty print to FILE the loop statement tree LST in DOT format.  */
1099
1100 static void
1101 dot_lst_1 (FILE *file, lst_p lst)
1102 {
1103   if (!lst)
1104     return;
1105
1106   if (LST_LOOP_P (lst))
1107     {
1108       int i;
1109       lst_p l;
1110
1111       if (!LST_LOOP_FATHER (lst))
1112         fprintf (file, "L -> L_%d_%d\n",
1113                  lst_depth (lst),
1114                  lst_dewey_number (lst));
1115       else
1116         fprintf (file, "L_%d_%d -> L_%d_%d\n",
1117                  lst_depth (LST_LOOP_FATHER (lst)),
1118                  lst_dewey_number (LST_LOOP_FATHER (lst)),
1119                  lst_depth (lst),
1120                  lst_dewey_number (lst));
1121
1122       FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
1123         dot_lst_1 (file, l);
1124     }
1125
1126   else
1127     fprintf (file, "L_%d_%d -> S_%d\n",
1128              lst_depth (LST_LOOP_FATHER (lst)),
1129              lst_dewey_number (LST_LOOP_FATHER (lst)),
1130              pbb_index (LST_PBB (lst)));
1131
1132 }
1133
1134 /* Display the LST using dotty.  */
1135
1136 DEBUG_FUNCTION void
1137 dot_lst (lst_p lst)
1138 {
1139   /* When debugging, enable the following code.  This cannot be used
1140      in production compilers because it calls "system".  */
1141 #if 0
1142   FILE *stream = fopen ("/tmp/lst.dot", "w");
1143   gcc_assert (stream);
1144
1145   fputs ("digraph all {\n", stream);
1146   dot_lst_1 (stream, lst);
1147   fputs ("}\n\n", stream);
1148   fclose (stream);
1149
1150   system ("dotty /tmp/lst.dot &");
1151 #else
1152   fputs ("digraph all {\n", stderr);
1153   dot_lst_1 (stderr, lst);
1154   fputs ("}\n\n", stderr);
1155
1156 #endif
1157 }
1158
1159 /* Reverse the loop around PBB at level DEPTH.  */
1160
1161 isl_map *
1162 reverse_loop_at_level (poly_bb_p pbb, int depth)
1163 {
1164   unsigned i, depth_dim = psct_dynamic_dim (pbb, depth);
1165   isl_space *d = isl_map_get_space (pbb->transformed);
1166   isl_space *d1 = isl_space_range (d);
1167   unsigned n = isl_space_dim (d1, isl_dim_out);
1168   isl_space *d2 = isl_space_add_dims (d1, isl_dim_in, n);
1169   isl_map *x = isl_map_universe (isl_space_copy (d2));
1170   isl_constraint *c = isl_equality_alloc (isl_local_space_from_space (d2));
1171
1172   for (i = 0; i < n; i++)
1173     if (i != depth_dim)
1174       x = isl_map_equate (x, isl_dim_in, i, isl_dim_out, i);
1175
1176   c = isl_constraint_set_coefficient_si (c, isl_dim_in, depth_dim, 1);
1177   c = isl_constraint_set_coefficient_si (c, isl_dim_out, depth_dim, 1);
1178   x = isl_map_add_constraint (x, c);
1179   return x;
1180 }
1181
1182 /* Reverse the loop at level DEPTH for all the PBBS.  */
1183
1184 isl_union_map *
1185 reverse_loop_for_pbbs (scop_p scop, vec<poly_bb_p> pbbs, int depth)
1186 {
1187   poly_bb_p pbb;
1188   int i;
1189   isl_space *space = isl_space_from_domain (isl_set_get_space (scop->context));
1190   isl_union_map *res = isl_union_map_empty (space);
1191
1192   for (i = 0; pbbs.iterate (i, &pbb); i++)
1193     res = isl_union_map_add_map (res, reverse_loop_at_level (pbb, depth));
1194
1195   return res;
1196 }
1197
1198
1199 #endif  /* HAVE_isl */
1200