[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / graphite-poly.c
blob84ecee09c376a9b84544d87dc149efe1caca66d9
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>.
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
24 #ifdef HAVE_isl
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
26 #include <stddef.h>
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>
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)
44 #endif
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"
61 #define OPENSCOP_MAX_STRING 256
64 /* Print to STDERR the GMP value VAL. */
66 DEBUG_FUNCTION void
67 debug_gmp_value (mpz_t val)
69 gmp_fprintf (stderr, "%Zd", val);
72 /* Prints to FILE the iteration domain of PBB, at some VERBOSITY
73 level. */
75 void
76 print_iteration_domain (FILE *file, poly_bb_p pbb, int verbosity)
78 print_pbb_domain (file, pbb, verbosity);
81 /* Prints to FILE the iteration domains of every PBB of SCOP, at some
82 VERBOSITY level. */
84 void
85 print_iteration_domains (FILE *file, scop_p scop, int verbosity)
87 int i;
88 poly_bb_p pbb;
90 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
91 print_iteration_domain (file, pbb, verbosity);
94 /* Prints to STDERR the iteration domain of PBB, at some VERBOSITY
95 level. */
97 DEBUG_FUNCTION void
98 debug_iteration_domain (poly_bb_p pbb, int verbosity)
100 print_iteration_domain (stderr, pbb, verbosity);
103 /* Prints to STDERR the iteration domains of every PBB of SCOP, at
104 some VERBOSITY level. */
106 DEBUG_FUNCTION void
107 debug_iteration_domains (scop_p scop, int verbosity)
109 print_iteration_domains (stderr, scop, verbosity);
112 /* Apply graphite transformations to all the basic blocks of SCOP. */
114 bool
115 apply_poly_transforms (scop_p scop)
117 bool transform_done = false;
119 /* Generate code even if we did not apply any real transformation.
120 This also allows to check the performance for the identity
121 transformation: GIMPLE -> GRAPHITE -> GIMPLE. */
122 if (flag_graphite_identity)
123 transform_done = true;
125 if (flag_loop_parallelize_all)
126 transform_done = true;
128 if (flag_loop_optimize_isl)
129 transform_done |= optimize_isl (scop);
131 return transform_done;
134 /* Create a new polyhedral data reference and add it to PBB. It is
135 defined by its ACCESSES, its TYPE, and the number of subscripts
136 NB_SUBSCRIPTS. */
138 void
139 new_poly_dr (poly_bb_p pbb, enum poly_dr_type type, data_reference_p cdr,
140 graphite_dim_t nb_subscripts,
141 isl_map *acc, isl_set *subscript_sizes)
143 static int id = 0;
144 poly_dr_p pdr = XNEW (struct poly_dr);
146 PDR_ID (pdr) = id++;
147 PDR_NB_REFS (pdr) = 1;
148 PDR_PBB (pdr) = pbb;
149 pdr->accesses = acc;
150 pdr->subscript_sizes = subscript_sizes;
151 PDR_TYPE (pdr) = type;
152 PDR_CDR (pdr) = cdr;
153 PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
154 PBB_DRS (pbb).safe_push (pdr);
157 /* Free polyhedral data reference PDR. */
159 void
160 free_poly_dr (poly_dr_p pdr)
162 isl_map_free (pdr->accesses);
163 isl_set_free (pdr->subscript_sizes);
164 XDELETE (pdr);
167 /* Create a new polyhedral black box. */
169 poly_bb_p
170 new_poly_bb (scop_p scop, gimple_poly_bb_p black_box)
172 poly_bb_p pbb = XNEW (struct poly_bb);
174 pbb->domain = NULL;
175 pbb->schedule = NULL;
176 pbb->transformed = NULL;
177 pbb->saved = NULL;
178 PBB_SCOP (pbb) = scop;
179 pbb_set_black_box (pbb, black_box);
180 PBB_DRS (pbb).create (3);
181 PBB_IS_REDUCTION (pbb) = false;
182 GBB_PBB ((gimple_poly_bb_p) black_box) = pbb;
184 return pbb;
187 /* Free polyhedral black box. */
189 void
190 free_poly_bb (poly_bb_p pbb)
192 int i;
193 poly_dr_p pdr;
195 isl_set_free (pbb->domain);
196 isl_map_free (pbb->schedule);
197 isl_map_free (pbb->transformed);
198 isl_map_free (pbb->saved);
200 if (PBB_DRS (pbb).exists ())
201 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
202 free_poly_dr (pdr);
204 PBB_DRS (pbb).release ();
205 XDELETE (pbb);
208 /* Prints to FILE the polyhedral data reference PDR, at some VERBOSITY
209 level. */
211 void
212 print_pdr (FILE *file, poly_dr_p pdr, int verbosity)
214 if (verbosity > 1)
216 fprintf (file, "# pdr_%d (", PDR_ID (pdr));
218 switch (PDR_TYPE (pdr))
220 case PDR_READ:
221 fprintf (file, "read \n");
222 break;
224 case PDR_WRITE:
225 fprintf (file, "write \n");
226 break;
228 case PDR_MAY_WRITE:
229 fprintf (file, "may_write \n");
230 break;
232 default:
233 gcc_unreachable ();
236 dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
239 if (verbosity > 0)
241 fprintf (file, "# data accesses (\n");
242 print_isl_map (file, pdr->accesses);
243 print_isl_set (file, pdr->subscript_sizes);
244 fprintf (file, "#)\n");
246 if (verbosity > 1)
247 fprintf (file, "#)\n");
250 /* Prints to STDERR the polyhedral data reference PDR, at some
251 VERBOSITY level. */
253 DEBUG_FUNCTION void
254 debug_pdr (poly_dr_p pdr, int verbosity)
256 print_pdr (stderr, pdr, verbosity);
259 /* Store the GRAPHITE representation of BB. */
261 gimple_poly_bb_p
262 new_gimple_poly_bb (basic_block bb, vec<data_reference_p> drs)
264 gimple_poly_bb_p gbb;
266 gbb = XNEW (struct gimple_poly_bb);
267 bb->aux = gbb;
268 GBB_BB (gbb) = bb;
269 GBB_DATA_REFS (gbb) = drs;
270 GBB_CONDITIONS (gbb).create (0);
271 GBB_CONDITION_CASES (gbb).create (0);
273 return gbb;
276 /* Frees GBB. */
278 void
279 free_gimple_poly_bb (gimple_poly_bb_p gbb)
281 free_data_refs (GBB_DATA_REFS (gbb));
283 GBB_CONDITIONS (gbb).release ();
284 GBB_CONDITION_CASES (gbb).release ();
285 GBB_BB (gbb)->aux = 0;
286 XDELETE (gbb);
289 /* Deletes all gimple bbs in SCOP. */
291 static void
292 remove_gbbs_in_scop (scop_p scop)
294 int i;
295 poly_bb_p pbb;
297 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
298 free_gimple_poly_bb (PBB_BLACK_BOX (pbb));
301 /* Creates a new SCOP containing the region (ENTRY, EXIT). */
303 scop_p
304 new_scop (edge entry, edge exit)
306 sese_info_p region = new_sese_info (entry, exit);
307 scop_p scop = XNEW (struct scop);
309 scop->param_context = NULL;
310 scop->must_raw = NULL;
311 scop->may_raw = NULL;
312 scop->must_raw_no_source = NULL;
313 scop->may_raw_no_source = NULL;
314 scop->must_war = NULL;
315 scop->may_war = NULL;
316 scop->must_war_no_source = NULL;
317 scop->may_war_no_source = NULL;
318 scop->must_waw = NULL;
319 scop->may_waw = NULL;
320 scop->must_waw_no_source = NULL;
321 scop->may_waw_no_source = NULL;
322 scop_set_region (scop, region);
323 scop->pbbs.create (3);
324 POLY_SCOP_P (scop) = false;
325 scop->drs.create (3);
327 return scop;
330 /* Deletes SCOP. */
332 void
333 free_scop (scop_p scop)
335 int i;
336 poly_bb_p pbb;
338 remove_gbbs_in_scop (scop);
339 free_sese_info (SCOP_REGION (scop));
341 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
342 free_poly_bb (pbb);
344 scop->pbbs.release ();
346 isl_set_free (scop->param_context);
347 isl_union_map_free (scop->must_raw);
348 isl_union_map_free (scop->may_raw);
349 isl_union_map_free (scop->must_raw_no_source);
350 isl_union_map_free (scop->may_raw_no_source);
351 isl_union_map_free (scop->must_war);
352 isl_union_map_free (scop->may_war);
353 isl_union_map_free (scop->must_war_no_source);
354 isl_union_map_free (scop->may_war_no_source);
355 isl_union_map_free (scop->must_waw);
356 isl_union_map_free (scop->may_waw);
357 isl_union_map_free (scop->must_waw_no_source);
358 isl_union_map_free (scop->may_waw_no_source);
359 XDELETE (scop);
362 /* Print to FILE the domain of PBB, at some VERBOSITY level. */
364 void
365 print_pbb_domain (FILE *file, poly_bb_p pbb, int verbosity ATTRIBUTE_UNUSED)
367 print_isl_set (file, pbb->domain);
370 /* Dump the cases of a graphite basic block GBB on FILE. */
372 static void
373 dump_gbb_cases (FILE *file, gimple_poly_bb_p gbb)
375 int i;
376 gimple *stmt;
377 vec<gimple *> cases;
379 if (!gbb)
380 return;
382 cases = GBB_CONDITION_CASES (gbb);
383 if (cases.is_empty ())
384 return;
386 fprintf (file, "# cases bb_%d (\n", GBB_BB (gbb)->index);
388 FOR_EACH_VEC_ELT (cases, i, stmt)
390 fprintf (file, "# ");
391 print_gimple_stmt (file, stmt, 0, 0);
394 fprintf (file, "#)\n");
397 /* Dump conditions of a graphite basic block GBB on FILE. */
399 static void
400 dump_gbb_conditions (FILE *file, gimple_poly_bb_p gbb)
402 int i;
403 gimple *stmt;
404 vec<gimple *> conditions;
406 if (!gbb)
407 return;
409 conditions = GBB_CONDITIONS (gbb);
410 if (conditions.is_empty ())
411 return;
413 fprintf (file, "# conditions bb_%d (\n", GBB_BB (gbb)->index);
415 FOR_EACH_VEC_ELT (conditions, i, stmt)
417 fprintf (file, "# ");
418 print_gimple_stmt (file, stmt, 0, 0);
421 fprintf (file, "#)\n");
424 /* Print to FILE all the data references of PBB, at some VERBOSITY
425 level. */
427 void
428 print_pdrs (FILE *file, poly_bb_p pbb, int verbosity)
430 int i;
431 poly_dr_p pdr;
432 int nb_reads = 0;
433 int nb_writes = 0;
435 if (PBB_DRS (pbb).length () == 0)
437 if (verbosity > 0)
438 fprintf (file, "# Access informations are not provided\n");\
439 fprintf (file, "0\n");
440 return;
443 if (verbosity > 1)
444 fprintf (file, "# Data references (\n");
446 if (verbosity > 0)
447 fprintf (file, "# Access informations are provided\n");
448 fprintf (file, "1\n");
450 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
451 if (PDR_TYPE (pdr) == PDR_READ)
452 nb_reads++;
453 else
454 nb_writes++;
456 if (verbosity > 1)
457 fprintf (file, "# Read data references (\n");
459 if (verbosity > 0)
460 fprintf (file, "# Read access informations\n");
461 fprintf (file, "%d\n", nb_reads);
463 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
464 if (PDR_TYPE (pdr) == PDR_READ)
465 print_pdr (file, pdr, verbosity);
467 if (verbosity > 1)
468 fprintf (file, "#)\n");
470 if (verbosity > 1)
471 fprintf (file, "# Write data references (\n");
473 if (verbosity > 0)
474 fprintf (file, "# Write access informations\n");
475 fprintf (file, "%d\n", nb_writes);
477 FOR_EACH_VEC_ELT (PBB_DRS (pbb), i, pdr)
478 if (PDR_TYPE (pdr) != PDR_READ)
479 print_pdr (file, pdr, verbosity);
481 if (verbosity > 1)
482 fprintf (file, "#)\n");
484 if (verbosity > 1)
485 fprintf (file, "#)\n");
488 /* Print to STDERR all the data references of PBB. */
490 DEBUG_FUNCTION void
491 debug_pdrs (poly_bb_p pbb, int verbosity)
493 print_pdrs (stderr, pbb, verbosity);
496 /* Print to FILE the body of PBB, at some VERBOSITY level.
497 If statement_body_provided is false statement body is not printed. */
499 static void
500 print_pbb_body (FILE *file, poly_bb_p pbb, int verbosity,
501 bool statement_body_provided)
503 if (verbosity > 1)
504 fprintf (file, "# Body (\n");
506 if (!statement_body_provided)
508 if (verbosity > 0)
509 fprintf (file, "# Statement body is not provided\n");
511 fprintf (file, "0\n");
513 if (verbosity > 1)
514 fprintf (file, "#)\n");
515 return;
518 if (verbosity > 0)
519 fprintf (file, "# Statement body is provided\n");
520 fprintf (file, "1\n");
522 if (verbosity > 0)
523 fprintf (file, "# Original iterator names\n# Iterator names are not provided yet.\n");
525 if (verbosity > 0)
526 fprintf (file, "# Statement body\n");
528 fprintf (file, "{\n");
529 dump_bb (file, pbb_bb (pbb), 0, 0);
530 fprintf (file, "}\n");
532 if (verbosity > 1)
533 fprintf (file, "#)\n");
536 /* Print to FILE the domain and scattering function of PBB, at some
537 VERBOSITY level. */
539 void
540 print_pbb (FILE *file, poly_bb_p pbb, int verbosity)
542 if (verbosity > 1)
544 fprintf (file, "# pbb_%d (\n", pbb_index (pbb));
545 dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
546 dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
549 print_pbb_domain (file, pbb, verbosity);
550 print_pdrs (file, pbb, verbosity);
551 print_pbb_body (file, pbb, verbosity, false);
553 if (verbosity > 1)
554 fprintf (file, "#)\n");
557 /* Print to FILE the parameters of SCOP, at some VERBOSITY level. */
559 void
560 print_scop_params (FILE *file, scop_p scop, int verbosity)
562 int i;
563 tree t;
565 if (verbosity > 1)
566 fprintf (file, "# parameters (\n");
568 if (SESE_PARAMS (SCOP_REGION (scop)).length ())
570 if (verbosity > 0)
571 fprintf (file, "# Parameter names are provided\n");
573 fprintf (file, "1\n");
575 if (verbosity > 0)
576 fprintf (file, "# Parameter names\n");
578 else
580 if (verbosity > 0)
581 fprintf (file, "# Parameter names are not provided\n");
582 fprintf (file, "0\n");
585 FOR_EACH_VEC_ELT (SESE_PARAMS (SCOP_REGION (scop)), i, t)
587 print_generic_expr (file, t, 0);
588 fprintf (file, " ");
591 fprintf (file, "\n");
593 if (verbosity > 1)
594 fprintf (file, "#)\n");
597 /* Print to FILE the context of SCoP, at some VERBOSITY level. */
599 void
600 print_scop_context (FILE *file, scop_p scop, int verbosity)
602 if (verbosity > 0)
603 fprintf (file, "# Context (\n");
605 if (scop->param_context)
606 print_isl_set (file, scop->param_context);
608 if (verbosity > 0)
609 fprintf (file, "# )\n");
612 /* Print to FILE the SCOP, at some VERBOSITY level. */
614 void
615 print_scop (FILE *file, scop_p scop, int verbosity)
617 int i;
618 poly_bb_p pbb;
620 fprintf (file, "SCoP 1\n#(\n");
621 fprintf (file, "# Language\nGimple\n");
622 print_scop_context (file, scop, verbosity);
623 print_scop_params (file, scop, verbosity);
625 if (verbosity > 0)
626 fprintf (file, "# Number of statements\n");
628 fprintf (file, "%d\n", scop->pbbs.length ());
630 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
631 print_pbb (file, pbb, verbosity);
633 fprintf (file, "#)\n");
636 /* Print to STDERR the domain of PBB, at some VERBOSITY level. */
638 DEBUG_FUNCTION void
639 debug_pbb_domain (poly_bb_p pbb, int verbosity)
641 print_pbb_domain (stderr, pbb, verbosity);
644 /* Print to FILE the domain and scattering function of PBB, at some
645 VERBOSITY level. */
647 DEBUG_FUNCTION void
648 debug_pbb (poly_bb_p pbb, int verbosity)
650 print_pbb (stderr, pbb, verbosity);
653 /* Print to STDERR the context of SCOP, at some VERBOSITY level. */
655 DEBUG_FUNCTION void
656 debug_scop_context (scop_p scop, int verbosity)
658 print_scop_context (stderr, scop, verbosity);
661 /* Print to STDERR the SCOP, at some VERBOSITY level. */
663 DEBUG_FUNCTION void
664 debug_scop (scop_p scop, int verbosity)
666 print_scop (stderr, scop, verbosity);
669 /* Print to STDERR the parameters of SCOP, at some VERBOSITY
670 level. */
672 DEBUG_FUNCTION void
673 debug_scop_params (scop_p scop, int verbosity)
675 print_scop_params (stderr, scop, verbosity);
678 extern isl_ctx *the_isl_ctx;
679 void
680 print_isl_set (FILE *f, isl_set *set)
682 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
683 p = isl_printer_print_set (p, set);
684 p = isl_printer_print_str (p, "\n");
685 isl_printer_free (p);
688 DEBUG_FUNCTION void
689 debug_isl_set (isl_set *set)
691 print_isl_set (stderr, set);
694 void
695 print_isl_map (FILE *f, isl_map *map)
697 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
698 p = isl_printer_print_map (p, map);
699 p = isl_printer_print_str (p, "\n");
700 isl_printer_free (p);
703 DEBUG_FUNCTION void
704 debug_isl_map (isl_map *map)
706 print_isl_map (stderr, map);
709 void
710 print_isl_aff (FILE *f, isl_aff *aff)
712 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
713 p = isl_printer_print_aff (p, aff);
714 p = isl_printer_print_str (p, "\n");
715 isl_printer_free (p);
718 DEBUG_FUNCTION void
719 debug_isl_aff (isl_aff *aff)
721 print_isl_aff (stderr, aff);
724 void
725 print_isl_constraint (FILE *f, isl_constraint *c)
727 isl_printer *p = isl_printer_to_file (the_isl_ctx, f);
728 p = isl_printer_print_constraint (p, c);
729 p = isl_printer_print_str (p, "\n");
730 isl_printer_free (p);
733 DEBUG_FUNCTION void
734 debug_isl_constraint (isl_constraint *c)
736 print_isl_constraint (stderr, c);
739 /* Returns the number of iterations RES of the loop around PBB at
740 time(scattering) dimension TIME_DEPTH. */
742 void
743 pbb_number_of_iterations_at_time (poly_bb_p pbb,
744 graphite_dim_t time_depth,
745 mpz_t res)
747 isl_set *transdomain;
748 isl_space *dc;
749 isl_aff *aff;
750 isl_val *isllb, *islub;
752 /* Map the iteration domain through the current scatter, and work
753 on the resulting set. */
754 transdomain = isl_set_apply (isl_set_copy (pbb->domain),
755 isl_map_copy (pbb->transformed));
757 /* Select the time_depth' dimension via an affine expression. */
758 dc = isl_set_get_space (transdomain);
759 aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
760 aff = isl_aff_set_coefficient_si (aff, isl_dim_in, time_depth, 1);
762 /* And find the min/max for that function. */
763 /* XXX isl check results? */
764 isllb = isl_set_min_val (transdomain, aff);
765 islub = isl_set_max_val (transdomain, aff);
767 islub = isl_val_sub (islub, isllb);
768 islub = isl_val_add_ui (islub, 1);
769 isl_val_get_num_gmp (islub, res);
771 isl_val_free (islub);
772 isl_aff_free (aff);
773 isl_set_free (transdomain);
776 #endif /* HAVE_isl */