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