Undo changes to the PDR representation.
[gcc.git] / gcc / graphite-poly.h
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
22 #ifndef GCC_GRAPHITE_POLY_H
23 #define GCC_GRAPHITE_POLY_H
24
25 typedef struct poly_dr *poly_dr_p;
26 DEF_VEC_P(poly_dr_p);
27 DEF_VEC_ALLOC_P (poly_dr_p, heap);
28
29 typedef struct poly_bb *poly_bb_p;
30 DEF_VEC_P(poly_bb_p);
31 DEF_VEC_ALLOC_P (poly_bb_p, heap);
32
33 typedef struct scop *scop_p;
34 DEF_VEC_P(scop_p);
35 DEF_VEC_ALLOC_P (scop_p, heap);
36
37 typedef ppl_dimension_type graphite_dim_t;
38
39 static inline graphite_dim_t pbb_dim_iter_domain (const struct poly_bb *);
40 static inline graphite_dim_t pbb_nb_params (const struct poly_bb *);
41 static inline graphite_dim_t scop_nb_params (scop_p);
42
43 /* A data reference can write or read some memory or we
44 just know it may write some memory. */
45 enum POLY_DR_TYPE
46 {
47 PDR_READ,
48 /* PDR_MAY_READs are represented using PDR_READS. This does not limit the
49 expressiveness. */
50 PDR_WRITE,
51 PDR_MAY_WRITE
52 };
53
54 struct poly_dr
55 {
56 /* A pointer to compiler's data reference description. */
57 void *compiler_dr;
58
59 /* A pointer to the PBB that contains this data reference. */
60 poly_bb_p pbb;
61
62 enum POLY_DR_TYPE type;
63
64 /* The access polyhedron contains the polyhedral space this data
65 reference will access.
66
67 The polyhedron contains these dimensions:
68
69 - The alias set (a):
70 Every memory access is classified in at least one alias set.
71
72 - The subscripts (s_0, ..., s_n):
73 The memory is accessed using zero or more subscript dimensions.
74
75 - The iteration domain (variables and parameters)
76
77 Do not hardcode the dimensions. Use the following accessor functions:
78 - pdr_alias_set_dim
79 - pdr_subscript_dim
80 - pdr_iterator_dim
81 - pdr_parameter_dim
82
83 Example:
84
85 | int A[1335][123];
86 | int *p = malloc ();
87 |
88 | k = ...
89 | for i
90 | {
91 | if (unknown_function ())
92 | p = A;
93 | ... = p[?][?];
94 | for j
95 | A[i][j+b] = m;
96 | }
97
98 The data access A[i][j+k] in alias set "5" is described like this:
99
100 | i j k a s0 s1 1
101 | 0 0 0 1 0 0 -5 = 0
102 |-1 0 0 0 1 0 0 = 0
103 | 0 -1 -1 0 0 1 0 = 0
104 | 0 0 0 0 1 0 0 >= 0 # The last four lines describe the
105 | 0 0 0 0 0 1 0 >= 0 # array size.
106 | 0 0 0 0 -1 0 1335 >= 0
107 | 0 0 0 0 0 -1 123 >= 0
108
109 The pointer "*p" in alias set "5" and "7" is described as a union of
110 polyhedron:
111
112
113 | i k a s0 1
114 | 0 0 1 0 -5 = 0
115 | 0 0 0 1 0 >= 0
116
117 "or"
118
119 | i k a s0 1
120 | 0 0 1 0 -7 = 0
121 | 0 0 0 1 0 >= 0
122
123 "*p" accesses all of the object allocated with 'malloc'.
124
125 The scalar data access "m" is represented as an array with zero subscript
126 dimensions.
127
128 | i j k a 1
129 | 0 0 0 -1 15 = 0 */
130 ppl_Pointset_Powerset_C_Polyhedron_t accesses;
131 };
132
133 #define PDR_CDR(PDR) (PDR->compiler_dr)
134 #define PDR_PBB(PDR) (PDR->pbb)
135 #define PDR_TYPE(PDR) (PDR->type)
136 #define PDR_ACCESSES(PDR) (PDR->accesses)
137
138 void new_poly_dr (poly_bb_p, ppl_Pointset_Powerset_C_Polyhedron_t,
139 enum POLY_DR_TYPE, void *);
140 void free_poly_dr (poly_dr_p);
141 void debug_pdr (poly_dr_p);
142 void print_pdr (FILE *, poly_dr_p);
143 static inline scop_p pdr_scop (poly_dr_p pdr);
144
145 /* The number of subscripts of the PDR. */
146
147 static inline graphite_dim_t
148 pdr_nb_subscripts (poly_dr_p pdr)
149 {
150 poly_bb_p pbb = PDR_PBB (pdr);
151 ppl_dimension_type dim;
152
153 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PDR_ACCESSES (pdr),
154 &dim);
155 return dim - pbb_dim_iter_domain (pbb) - pbb_nb_params (pbb) - 1;
156 }
157
158 /* The dimension of the iteration domain of the scop of PDR. */
159
160 static inline ppl_dimension_type
161 pdr_dim_iter_domain (poly_dr_p pdr)
162 {
163 return pbb_dim_iter_domain (PDR_PBB (pdr));
164 }
165
166 /* The number of parameters of the scop of PDR. */
167
168 static inline ppl_dimension_type
169 pdr_nb_params (poly_dr_p pdr)
170 {
171 return scop_nb_params (pdr_scop (pdr));
172 }
173
174 /* The dimension of the accesses polyhedron of PDR. */
175
176 static inline graphite_dim_t
177 pdr_dim (poly_dr_p pdr)
178 {
179 graphite_dim_t alias_nb_dimensions = 1;
180
181 return pbb_dim_iter_domain (PDR_PBB (pdr)) + alias_nb_dimensions
182 + pdr_nb_subscripts (pdr) + scop_nb_params (pdr_scop (pdr));
183 }
184
185 /* The dimension of the alias set in PDR. */
186
187 static inline ppl_dimension_type
188 pdr_alias_set_dim (poly_dr_p pdr)
189 {
190 poly_bb_p pbb = PDR_PBB (pdr);
191
192 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
193 }
194
195 /* The dimension in PDR containing subscript S. */
196
197 static inline ppl_dimension_type
198 pdr_subscript_dim (poly_dr_p pdr, graphite_dim_t s)
199 {
200 poly_bb_p pbb = PDR_PBB (pdr);
201
202 return pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb) + 1 + s;
203 }
204
205 /* The dimension in PDR containing the loop iterator ITER. */
206
207 static inline ppl_dimension_type
208 pdr_iterator_dim (poly_dr_p pdr ATTRIBUTE_UNUSED, graphite_dim_t iter)
209 {
210 return iter;
211 }
212
213 /* The dimension in PDR containing parameter PARAM. */
214
215 static inline ppl_dimension_type
216 pdr_parameter_dim (poly_dr_p pdr, graphite_dim_t param)
217 {
218 poly_bb_p pbb = PDR_PBB (pdr);
219
220 return pbb_dim_iter_domain (pbb) + param;
221 }
222
223 typedef struct poly_scattering *poly_scattering_p;
224
225 struct poly_scattering
226 {
227 /* The scattering function containing the transformations. */
228 ppl_Polyhedron_t scattering;
229
230 /* The number of local variables. */
231 int nb_local_variables;
232
233 /* The number of scattering dimensions. */
234 int nb_scattering;
235 };
236
237 /* POLY_BB represents a blackbox in the polyhedral model. */
238
239 struct poly_bb
240 {
241 void *black_box;
242
243 scop_p scop;
244
245 /* The iteration domain of this bb.
246 Example:
247
248 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
249 for (j = 2; j <= 2*i + 5; j++)
250 for (k = 0; k <= 5; k++)
251 S (i,j,k)
252
253 Loop iterators: i, j, k
254 Parameters: a, b
255
256 | i >= a - 7b + 8
257 | i <= 3a + 13b + 20
258 | j >= 2
259 | j <= 2i + 5
260 | k >= 0
261 | k <= 5
262
263 The number of variables in the DOMAIN may change and is not
264 related to the number of loops in the original code. */
265 ppl_Pointset_Powerset_C_Polyhedron_t domain;
266
267 /* The data references we access. */
268 VEC (poly_dr_p, heap) *drs;
269
270 /* The original scattering. */
271 poly_scattering_p original;
272
273 /* The transformed scattering. */
274 poly_scattering_p transformed;
275
276 /* A copy of the transformed scattering. */
277 poly_scattering_p saved;
278 };
279
280 #define PBB_BLACK_BOX(PBB) ((gimple_bb_p) PBB->black_box)
281 #define PBB_SCOP(PBB) (PBB->scop)
282 #define PBB_DOMAIN(PBB) (PBB->domain)
283 #define PBB_DRS(PBB) (PBB->drs)
284 #define PBB_ORIGINAL(PBB) (PBB->original)
285 #define PBB_ORIGINAL_SCATTERING(PBB) (PBB->original->scattering)
286 #define PBB_TRANSFORMED(PBB) (PBB->transformed)
287 #define PBB_TRANSFORMED_SCATTERING(PBB) (PBB->transformed->scattering)
288 #define PBB_SAVED(PBB) (PBB->saved)
289 #define PBB_NB_LOCAL_VARIABLES(PBB) (PBB->transformed->nb_local_variables)
290 #define PBB_NB_SCATTERING_TRANSFORM(PBB) (PBB->transformed->nb_scattering)
291
292 extern void new_poly_bb (scop_p, void *);
293 extern void free_poly_bb (poly_bb_p);
294 extern void debug_loop_vec (poly_bb_p);
295 extern void schedule_to_scattering (poly_bb_p, int);
296 extern void print_pbb_domain (FILE *, poly_bb_p);
297 extern void print_pbb (FILE *, poly_bb_p);
298 extern void print_scop_context (FILE *, scop_p);
299 extern void print_scop (FILE *, scop_p);
300 extern void debug_pbb_domain (poly_bb_p);
301 extern void debug_pbb (poly_bb_p);
302 extern void print_pdrs (FILE *, poly_bb_p);
303 extern void debug_pdrs (poly_bb_p);
304 extern void debug_scop_context (scop_p);
305 extern void debug_scop (scop_p);
306 extern void print_scop_params (FILE *, scop_p);
307 extern void debug_scop_params (scop_p);
308 extern void print_iteration_domain (FILE *, poly_bb_p);
309 extern void print_iteration_domains (FILE *, scop_p);
310 extern void debug_iteration_domain (poly_bb_p);
311 extern void debug_iteration_domains (scop_p);
312 extern bool scop_do_interchange (scop_p);
313 extern bool scop_do_strip_mine (scop_p);
314 extern void pbb_number_of_iterations (poly_bb_p, graphite_dim_t, Value);
315
316 /* The scop that contains the PDR. */
317
318 static inline scop_p pdr_scop (poly_dr_p pdr)
319 {
320 return PBB_SCOP (PDR_PBB (pdr));
321 }
322
323 /* Set black box of PBB to BLACKBOX. */
324
325 static inline void
326 pbb_set_black_box (poly_bb_p pbb, void *black_box)
327 {
328 pbb->black_box = black_box;
329 }
330
331 /* The number of loops around PBB: the dimension of the iteration
332 domain. */
333
334 static inline graphite_dim_t
335 pbb_dim_iter_domain (const struct poly_bb *pbb)
336 {
337 scop_p scop = PBB_SCOP (pbb);
338 ppl_dimension_type dim;
339
340 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
341 return dim - scop_nb_params (scop);
342 }
343
344 /* The number of params defined in PBB. */
345
346 static inline graphite_dim_t
347 pbb_nb_params (const struct poly_bb *pbb)
348 {
349 scop_p scop = PBB_SCOP (pbb);
350
351 return scop_nb_params (scop);
352 }
353
354 /* The number of scattering dimensions in the SCATTERING polyhedron
355 of a PBB for a given SCOP. */
356
357 static inline graphite_dim_t
358 pbb_nb_scattering_orig (const struct poly_bb *pbb)
359 {
360 return 2 * pbb_dim_iter_domain (pbb) + 1;
361 }
362
363 /* The number of scattering dimensions in PBB. */
364
365 static inline graphite_dim_t
366 pbb_nb_scattering_transform (const struct poly_bb *pbb)
367 {
368 return PBB_NB_SCATTERING_TRANSFORM (pbb);
369 }
370
371 /* Returns the number of local variables used in the transformed
372 scattering polyhedron of PBB. */
373
374 static inline graphite_dim_t
375 pbb_nb_local_vars (const struct poly_bb *pbb)
376 {
377 /* For now we do not have any local variables, as we do not do strip
378 mining for example. */
379 return PBB_NB_LOCAL_VARIABLES (pbb);
380 }
381
382 /* The dimension in the domain of PBB containing the iterator ITER. */
383
384 static inline ppl_dimension_type
385 pbb_iterator_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t iter)
386 {
387 return iter;
388 }
389
390 /* The dimension in the domain of PBB containing the iterator ITER. */
391
392 static inline ppl_dimension_type
393 pbb_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
394 {
395 return param
396 + pbb_dim_iter_domain (pbb);
397 }
398
399 /* The dimension in the original scattering polyhedron of PBB
400 containing the scattering iterator SCATTER. */
401
402 static inline ppl_dimension_type
403 psco_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
404 {
405 gcc_assert (scatter < pbb_nb_scattering_orig (pbb));
406 return scatter;
407 }
408
409 /* The dimension in the transformed scattering polyhedron of PBB
410 containing the scattering iterator SCATTER. */
411
412 static inline ppl_dimension_type
413 psct_scattering_dim (poly_bb_p pbb ATTRIBUTE_UNUSED, graphite_dim_t scatter)
414 {
415 gcc_assert (scatter <= pbb_nb_scattering_transform (pbb));
416 return scatter;
417 }
418
419 ppl_dimension_type psct_scattering_dim_for_loop_depth (poly_bb_p,
420 graphite_dim_t);
421
422 /* The dimension in the transformed scattering polyhedron of PBB of
423 the local variable LV. */
424
425 static inline ppl_dimension_type
426 psct_local_var_dim (poly_bb_p pbb, graphite_dim_t lv)
427 {
428 gcc_assert (lv <= pbb_nb_local_vars (pbb));
429 return lv + pbb_nb_scattering_transform (pbb);
430 }
431
432 /* The dimension in the original scattering polyhedron of PBB
433 containing the loop iterator ITER. */
434
435 static inline ppl_dimension_type
436 psco_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
437 {
438 gcc_assert (iter < pbb_dim_iter_domain (pbb));
439 return iter + pbb_nb_scattering_orig (pbb);
440 }
441
442 /* The dimension in the transformed scattering polyhedron of PBB
443 containing the loop iterator ITER. */
444
445 static inline ppl_dimension_type
446 psct_iterator_dim (poly_bb_p pbb, graphite_dim_t iter)
447 {
448 gcc_assert (iter < pbb_dim_iter_domain (pbb));
449 return iter
450 + pbb_nb_scattering_transform (pbb)
451 + pbb_nb_local_vars (pbb);
452 }
453
454 /* The dimension in the original scattering polyhedron of PBB
455 containing parameter PARAM. */
456
457 static inline ppl_dimension_type
458 psco_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
459 {
460 gcc_assert (param < pbb_nb_params (pbb));
461 return param
462 + pbb_nb_scattering_orig (pbb)
463 + pbb_dim_iter_domain (pbb);
464 }
465
466 /* The dimension in the transformed scattering polyhedron of PBB
467 containing parameter PARAM. */
468
469 static inline ppl_dimension_type
470 psct_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
471 {
472 gcc_assert (param < pbb_nb_params (pbb));
473 return param
474 + pbb_nb_scattering_transform (pbb)
475 + pbb_nb_local_vars (pbb)
476 + pbb_dim_iter_domain (pbb);
477 }
478
479 /* Adds to the transformed scattering polyhedron of PBB a new local
480 variable and returns its index. */
481
482 static inline graphite_dim_t
483 psct_add_local_variable (poly_bb_p pbb)
484 {
485 graphite_dim_t nlv = pbb_nb_local_vars (pbb);
486 ppl_dimension_type lv_column = psct_local_var_dim (pbb, nlv);
487 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), lv_column, 1);
488 PBB_NB_LOCAL_VARIABLES (pbb) += 1;
489 return nlv;
490 }
491
492 /* Adds a dimension to the transformed scattering polyhedron of PBB at
493 INDEX. */
494
495 static inline void
496 psct_add_scattering_dimension (poly_bb_p pbb, ppl_dimension_type index)
497 {
498 gcc_assert (index < pbb_nb_scattering_transform (pbb));
499
500 ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb), index, 1);
501 PBB_NB_SCATTERING_TRANSFORM (pbb) += 1;
502 }
503
504 /* A SCOP is a Static Control Part of the program, simple enough to be
505 represented in polyhedral form. */
506 struct scop
507 {
508 /* A SCOP is defined as a SESE region. */
509 void *region;
510
511 /* Number of parameters in SCoP. */
512 graphite_dim_t nb_params;
513
514 /* All the basic blocks in this scop that contain memory references
515 and that will be represented as statements in the polyhedral
516 representation. */
517 VEC (poly_bb_p, heap) *bbs;
518
519 /* Data dependence graph for this SCoP. */
520 struct graph *dep_graph;
521
522 /* The context describes known restrictions concerning the parameters
523 and relations in between the parameters.
524
525 void f (int8_t a, uint_16_t b) {
526 c = 2 a + b;
527 ...
528 }
529
530 Here we can add these restrictions to the context:
531
532 -128 >= a >= 127
533 0 >= b >= 65,535
534 c = 2a + b */
535 ppl_Pointset_Powerset_C_Polyhedron_t context;
536
537 /* A hashtable of the original pairs of dependent data references.
538 For each pair of dependent data references, the dependence
539 polyhedron is stored also. */
540 htab_t original_pdr_pairs;
541 };
542
543 #define SCOP_BBS(S) (S->bbs)
544 #define SCOP_REGION(S) ((sese) S->region)
545 #define SCOP_DEP_GRAPH(S) (S->dep_graph)
546 #define SCOP_CONTEXT(S) (S->context)
547 #define SCOP_ORIGINAL_PDR_PAIRS(S) (S->original_pdr_pairs)
548
549 extern scop_p new_scop (void *);
550 extern void free_scop (scop_p);
551 extern void free_scops (VEC (scop_p, heap) *);
552 extern void print_generated_program (FILE *, scop_p);
553 extern void debug_generated_program (scop_p);
554 extern void print_scattering_function (FILE *, poly_bb_p);
555 extern void print_scattering_functions (FILE *, scop_p);
556 extern void debug_scattering_function (poly_bb_p);
557 extern void debug_scattering_functions (scop_p);
558 extern int scop_max_loop_depth (scop_p);
559 extern int unify_scattering_dimensions (scop_p);
560 extern bool apply_poly_transforms (scop_p);
561 extern bool graphite_legal_transform (scop_p);
562
563 /* Set the region of SCOP to REGION. */
564
565 static inline void
566 scop_set_region (scop_p scop, void *region)
567 {
568 scop->region = region;
569 }
570
571 /* Returns the number of parameters for SCOP. */
572
573 static inline graphite_dim_t
574 scop_nb_params (scop_p scop)
575 {
576 return scop->nb_params;
577 }
578
579 /* Set the number of params of SCOP to NB_PARAMS. */
580
581 static inline void
582 scop_set_nb_params (scop_p scop, graphite_dim_t nb_params)
583 {
584 scop->nb_params = nb_params;
585 }
586
587 /* Allocates a new empty poly_scattering structure. */
588
589 static inline poly_scattering_p
590 poly_scattering_new (void)
591 {
592 poly_scattering_p res = XNEW (struct poly_scattering);
593
594 res->scattering = NULL;
595 res->nb_local_variables = 0;
596 res->nb_scattering = 0;
597 return res;
598 }
599
600 /* Free a poly_scattering structure. */
601
602 static inline void
603 poly_scattering_free (poly_scattering_p s)
604 {
605 ppl_delete_Polyhedron (s->scattering);
606 free (s);
607 }
608
609 /* Copies S and return a new scattering. */
610
611 static inline poly_scattering_p
612 poly_scattering_copy (poly_scattering_p s)
613 {
614 poly_scattering_p res = poly_scattering_new ();
615
616 ppl_new_C_Polyhedron_from_C_Polyhedron (&(res->scattering), s->scattering);
617 res->nb_local_variables = s->nb_local_variables;
618 res->nb_scattering = s->nb_scattering;
619 return res;
620 }
621
622 /* Saves the transformed scattering of PBB. */
623
624 static inline void
625 store_scattering_pbb (poly_bb_p pbb)
626 {
627 gcc_assert (PBB_TRANSFORMED (pbb));
628
629 if (PBB_SAVED (pbb))
630 poly_scattering_free (PBB_SAVED (pbb));
631
632 PBB_SAVED (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
633 }
634
635 /* Saves the scattering for all the pbbs in the SCOP. */
636
637 static inline void
638 store_scattering (scop_p scop)
639 {
640 int i;
641 poly_bb_p pbb;
642
643 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
644 store_scattering_pbb (pbb);
645 }
646
647 /* Restores the scattering of PBB. */
648
649 static inline void
650 restore_scattering_pbb (poly_bb_p pbb)
651 {
652 gcc_assert (PBB_SAVED (pbb));
653
654 poly_scattering_free (PBB_TRANSFORMED (pbb));
655 PBB_TRANSFORMED (pbb) = poly_scattering_copy (PBB_SAVED (pbb));
656 }
657
658 /* Restores the scattering for all the pbbs in the SCOP. */
659
660 static inline void
661 restore_scattering (scop_p scop)
662 {
663 int i;
664 poly_bb_p pbb;
665
666 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
667 restore_scattering_pbb (pbb);
668 }
669
670 #endif