Move declarations, assign types, renaming.
[platform/upstream/gcc.git] / gcc / graphite-poly.h
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 #ifndef GCC_GRAPHITE_POLY_H
23 #define GCC_GRAPHITE_POLY_H
24
25 #include "sese.h"
26
27 #ifndef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
28 # define isl_stat int
29 # define isl_stat_ok 0
30 #endif
31
32 typedef struct poly_dr *poly_dr_p;
33
34 typedef struct poly_bb *poly_bb_p;
35
36 typedef struct scop *scop_p;
37
38 typedef unsigned graphite_dim_t;
39
40 static inline graphite_dim_t scop_nb_params (scop_p);
41
42 /* A data reference can write or read some memory or we
43    just know it may write some memory.  */
44 enum poly_dr_type
45 {
46   PDR_READ,
47   /* PDR_MAY_READs are represented using PDR_READS.  This does not
48      limit the expressiveness.  */
49   PDR_WRITE,
50   PDR_MAY_WRITE
51 };
52
53 struct poly_dr
54 {
55   /* An identifier for this PDR.  */
56   int id;
57
58   /* The number of data refs identical to this one in the PBB.  */
59   int nb_refs;
60
61   /* A pointer to compiler's data reference description.  */
62   data_reference_p compiler_dr;
63
64   /* A pointer to the PBB that contains this data reference.  */
65   poly_bb_p pbb;
66
67   enum poly_dr_type type;
68
69   /* The access polyhedron contains the polyhedral space this data
70      reference will access.
71
72      The polyhedron contains these dimensions:
73
74      - The alias set (a):
75      Every memory access is classified in at least one alias set.
76
77      - The subscripts (s_0, ..., s_n):
78      The memory is accessed using zero or more subscript dimensions.
79
80      - The iteration domain (variables and parameters)
81
82      Do not hardcode the dimensions.  Use the following accessor functions:
83      - pdr_alias_set_dim
84      - pdr_subscript_dim
85      - pdr_iterator_dim
86      - pdr_parameter_dim
87
88      Example:
89
90      | int A[1335][123];
91      | int *p = malloc ();
92      |
93      | k = ...
94      | for i
95      |   {
96      |     if (unknown_function ())
97      |       p = A;
98      |       ... = p[?][?];
99      |     for j
100      |       A[i][j+k] = m;
101      |   }
102
103      The data access A[i][j+k] in alias set "5" is described like this:
104
105      | i   j   k   a  s0  s1   1
106      | 0   0   0   1   0   0  -5     =  0
107      |-1   0   0   0   1   0   0     =  0
108      | 0  -1  -1   0   0   1   0     =  0
109      | 0   0   0   0   1   0   0     >= 0  # The last four lines describe the
110      | 0   0   0   0   0   1   0     >= 0  # array size.
111      | 0   0   0   0  -1   0 1335    >= 0
112      | 0   0   0   0   0  -1 123     >= 0
113
114      The pointer "*p" in alias set "5" and "7" is described as a union of
115      polyhedron:
116
117
118      | i   k   a  s0   1
119      | 0   0   1   0  -5   =  0
120      | 0   0   0   1   0   >= 0
121
122      "or"
123
124      | i   k   a  s0   1
125      | 0   0   1   0  -7   =  0
126      | 0   0   0   1   0   >= 0
127
128      "*p" accesses all of the object allocated with 'malloc'.
129
130      The scalar data access "m" is represented as an array with zero subscript
131      dimensions.
132
133      | i   j   k   a   1
134      | 0   0   0  -1   15  = 0
135
136      The difference between the graphite internal format for access data and
137      the OpenSop format is in the order of columns.
138      Instead of having:
139
140      | i   j   k   a  s0  s1   1
141      | 0   0   0   1   0   0  -5     =  0
142      |-1   0   0   0   1   0   0     =  0
143      | 0  -1  -1   0   0   1   0     =  0
144      | 0   0   0   0   1   0   0     >= 0  # The last four lines describe the
145      | 0   0   0   0   0   1   0     >= 0  # array size.
146      | 0   0   0   0  -1   0 1335    >= 0
147      | 0   0   0   0   0  -1 123     >= 0
148
149      In OpenScop we have:
150
151      | a  s0  s1   i   j   k   1
152      | 1   0   0   0   0   0  -5     =  0
153      | 0   1   0  -1   0   0   0     =  0
154      | 0   0   1   0  -1  -1   0     =  0
155      | 0   1   0   0   0   0   0     >= 0  # The last four lines describe the
156      | 0   0   1   0   0   0   0     >= 0  # array size.
157      | 0  -1   0   0   0   0 1335    >= 0
158      | 0   0  -1   0   0   0 123     >= 0
159
160      The OpenScop access function is printed as follows:
161
162      | 1  # The number of disjunct components in a union of access functions.
163      | R C O I L P  # Described bellow.
164      | a  s0  s1   i   j   k   1
165      | 1   0   0   0   0   0  -5     =  0
166      | 0   1   0  -1   0   0   0     =  0
167      | 0   0   1   0  -1  -1   0     =  0
168      | 0   1   0   0   0   0   0     >= 0  # The last four lines describe the
169      | 0   0   1   0   0   0   0     >= 0  # array size.
170      | 0  -1   0   0   0   0 1335    >= 0
171      | 0   0  -1   0   0   0 123     >= 0
172
173      Where:
174      - R: Number of rows.
175      - C: Number of columns.
176      - O: Number of output dimensions = alias set + number of subscripts.
177      - I: Number of input dimensions (iterators).
178      - L: Number of local (existentially quantified) dimensions.
179      - P: Number of parameters.
180
181      In the example, the vector "R C O I L P" is "7 7 3 2 0 1".  */
182   isl_map *accesses;
183   isl_set *subscript_sizes;
184
185   /* Data reference's base object set number, we must assure 2 pdrs are in the
186      same base object set before dependency checking.  */
187   int dr_base_object_set;
188
189   /* The number of subscripts.  */
190   graphite_dim_t nb_subscripts;
191 };
192
193 #define PDR_ID(PDR) (PDR->id)
194 #define PDR_NB_REFS(PDR) (PDR->nb_refs)
195 #define PDR_CDR(PDR) (PDR->compiler_dr)
196 #define PDR_PBB(PDR) (PDR->pbb)
197 #define PDR_TYPE(PDR) (PDR->type)
198 #define PDR_ACCESSES(PDR) (NULL)
199 #define PDR_BASE_OBJECT_SET(PDR) (PDR->dr_base_object_set)
200 #define PDR_NB_SUBSCRIPTS(PDR) (PDR->nb_subscripts)
201
202 void new_poly_dr (poly_bb_p, int, enum poly_dr_type, data_reference_p,
203                   graphite_dim_t, isl_map *, isl_set *);
204 void free_poly_dr (poly_dr_p);
205 void debug_pdr (poly_dr_p, int);
206 void print_pdr (FILE *, poly_dr_p, int);
207
208 static inline bool
209 pdr_read_p (poly_dr_p pdr)
210 {
211   return PDR_TYPE (pdr) == PDR_READ;
212 }
213
214 /* Returns true when PDR is a "write".  */
215
216 static inline bool
217 pdr_write_p (poly_dr_p pdr)
218 {
219   return PDR_TYPE (pdr) == PDR_WRITE;
220 }
221
222 /* Returns true when PDR is a "may write".  */
223
224 static inline bool
225 pdr_may_write_p (poly_dr_p pdr)
226 {
227   return PDR_TYPE (pdr) == PDR_MAY_WRITE;
228 }
229
230 /* POLY_BB represents a blackbox in the polyhedral model.  */
231
232 struct poly_bb
233 {
234   /* Pointer to a basic block or a statement in the compiler.  */
235   gimple_poly_bb_p black_box;
236
237   /* Pointer to the SCOP containing this PBB.  */
238   scop_p scop;
239
240   /* The iteration domain of this bb.  The layout of this polyhedron
241      is I|G with I the iteration domain, G the context parameters.
242
243      Example:
244
245      for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
246        for (j = 2; j <= 2*i + 5; j++)
247          for (k = 0; k <= 5; k++)
248            S (i,j,k)
249
250      Loop iterators: i, j, k
251      Parameters: a, b
252
253      | i >=  a -  7b +  8
254      | i <= 3a + 13b + 20
255      | j >= 2
256      | j <= 2i + 5
257      | k >= 0
258      | k <= 5
259
260      The number of variables in the DOMAIN may change and is not
261      related to the number of loops in the original code.  */
262   isl_set *domain;
263
264   /* The data references we access.  */
265   vec<poly_dr_p> drs;
266
267   /* The original scattering.  */
268   isl_map *schedule;
269
270   /* The transformed scattering.  */
271   isl_map *transformed;
272
273   /* A copy of the transformed scattering.  */
274   isl_map *saved;
275
276   /* True when this PBB contains only a reduction statement.  */
277   bool is_reduction;
278 };
279
280 #define PBB_BLACK_BOX(PBB) ((gimple_poly_bb_p) PBB->black_box)
281 #define PBB_SCOP(PBB) (PBB->scop)
282 #define PBB_DRS(PBB) (PBB->drs)
283 #define PBB_IS_REDUCTION(PBB) (PBB->is_reduction)
284
285 extern poly_bb_p new_poly_bb (scop_p, gimple_poly_bb_p);
286 extern void free_poly_bb (poly_bb_p);
287 extern void debug_loop_vec (poly_bb_p);
288 extern void print_pbb_domain (FILE *, poly_bb_p, int);
289 extern void print_pbb (FILE *, poly_bb_p, int);
290 extern void print_scop_context (FILE *, scop_p, int);
291 extern void print_scop (FILE *, scop_p, int);
292 extern void debug_pbb_domain (poly_bb_p, int);
293 extern void debug_pbb (poly_bb_p, int);
294 extern void print_pdrs (FILE *, poly_bb_p, int);
295 extern void debug_pdrs (poly_bb_p, int);
296 extern void debug_scop_context (scop_p, int);
297 extern void debug_scop (scop_p, int);
298 extern void print_scop_params (FILE *, scop_p, int);
299 extern void debug_scop_params (scop_p, int);
300 extern void print_iteration_domain (FILE *, poly_bb_p, int);
301 extern void print_iteration_domains (FILE *, scop_p, int);
302 extern void debug_iteration_domain (poly_bb_p, int);
303 extern void debug_iteration_domains (scop_p, int);
304 extern void print_isl_set (FILE *, isl_set *);
305 extern void print_isl_map (FILE *, isl_map *);
306 extern void print_isl_aff (FILE *, isl_aff *);
307 extern void print_isl_constraint (FILE *, isl_constraint *);
308 extern void debug_isl_set (isl_set *);
309 extern void debug_isl_map (isl_map *);
310 extern void debug_isl_aff (isl_aff *);
311 extern void debug_isl_constraint (isl_constraint *);
312 extern int scop_do_interchange (scop_p);
313 extern int scop_do_strip_mine (scop_p, int);
314 extern bool scop_do_block (scop_p);
315 extern bool flatten_all_loops (scop_p);
316 extern bool optimize_isl (scop_p);
317 extern void pbb_number_of_iterations_at_time (poly_bb_p, graphite_dim_t, mpz_t);
318 extern void debug_gmp_value (mpz_t);
319
320 /* Returns a gimple_bb from BB.  */
321
322 static inline gimple_poly_bb_p
323 gbb_from_bb (basic_block bb)
324 {
325   return (gimple_poly_bb_p) bb->aux;
326 }
327
328 /* The poly_bb of the BB.  */
329
330 static inline poly_bb_p
331 pbb_from_bb (basic_block bb)
332 {
333   return GBB_PBB (gbb_from_bb (bb));
334 }
335
336 /* The basic block of the PBB.  */
337
338 static inline basic_block
339 pbb_bb (poly_bb_p pbb)
340 {
341   return GBB_BB (PBB_BLACK_BOX (pbb));
342 }
343
344 static inline int
345 pbb_index (poly_bb_p pbb)
346 {
347   return pbb_bb (pbb)->index;
348 }
349
350 /* The loop of the PBB.  */
351
352 static inline loop_p
353 pbb_loop (poly_bb_p pbb)
354 {
355   return gbb_loop (PBB_BLACK_BOX (pbb));
356 }
357
358 /* The scop that contains the PDR.  */
359
360 static inline scop_p
361 pdr_scop (poly_dr_p pdr)
362 {
363   return PBB_SCOP (PDR_PBB (pdr));
364 }
365
366 /* Set black box of PBB to BLACKBOX.  */
367
368 static inline void
369 pbb_set_black_box (poly_bb_p pbb, gimple_poly_bb_p black_box)
370 {
371   pbb->black_box = black_box;
372 }
373
374 /* A SCOP is a Static Control Part of the program, simple enough to be
375    represented in polyhedral form.  */
376 struct scop
377 {
378   /* A SCOP is defined as a SESE region.  */
379   sese region;
380
381   /* Number of parameters in SCoP.  */
382   graphite_dim_t nb_params;
383
384   /* All the basic blocks in this scop that contain memory references
385      and that will be represented as statements in the polyhedral
386      representation.  */
387   vec<poly_bb_p> bbs;
388
389   /* The context describes known restrictions concerning the parameters
390      and relations in between the parameters.
391
392   void f (int8_t a, uint_16_t b) {
393     c = 2 a + b;
394     ...
395   }
396
397   Here we can add these restrictions to the context:
398
399   -128 >= a >= 127
400      0 >= b >= 65,535
401      c = 2a + b  */
402   isl_set *param_context;
403
404   /* The context used internally by ISL.  */
405   isl_ctx *isl_context;
406
407   /* The original dependence relations:
408      RAW are read after write dependences,
409      WAR are write after read dependences,
410      WAW are write after write dependences.  */
411   isl_union_map *must_raw, *may_raw, *must_raw_no_source, *may_raw_no_source,
412     *must_war, *may_war, *must_war_no_source, *may_war_no_source,
413     *must_waw, *may_waw, *must_waw_no_source, *may_waw_no_source;
414
415   /* True when the scop has been converted to its polyhedral
416      representation.  */
417   bool poly_scop_p;
418 };
419
420 #define SCOP_BBS(S) (S->bbs)
421 #define SCOP_REGION(S) (S->region)
422 #define SCOP_CONTEXT(S) (NULL)
423 #define POLY_SCOP_P(S) (S->poly_scop_p)
424
425 typedef struct base_alias_pair
426 {
427   int base_obj_set;
428   int *alias_set;
429 } *base_alias_pair_p;
430
431 extern scop_p new_scop (edge, edge);
432 extern void free_scop (scop_p);
433 extern gimple_poly_bb_p new_gimple_poly_bb (basic_block, vec<data_reference_p>);
434 extern void free_gimple_poly_bb (gimple_poly_bb_p);
435 extern void print_generated_program (FILE *, scop_p);
436 extern void debug_generated_program (scop_p);
437 extern int unify_scattering_dimensions (scop_p);
438 extern bool apply_poly_transforms (scop_p);
439 extern bool graphite_legal_transform (scop_p);
440
441 /* Set the region of SCOP to REGION.  */
442
443 static inline void
444 scop_set_region (scop_p scop, sese region)
445 {
446   scop->region = region;
447 }
448
449 /* Returns the number of parameters for SCOP.  */
450
451 static inline graphite_dim_t
452 scop_nb_params (scop_p scop)
453 {
454   return scop->nb_params;
455 }
456
457 /* Set the number of params of SCOP to NB_PARAMS.  */
458
459 static inline void
460 scop_set_nb_params (scop_p scop, graphite_dim_t nb_params)
461 {
462   scop->nb_params = nb_params;
463 }
464
465 bool graphite_legal_transform (scop_p);
466
467 isl_union_map *
468 scop_get_dependences (scop_p scop);
469
470 bool
471 carries_deps (__isl_keep isl_union_map *schedule,
472               __isl_keep isl_union_map *deps,
473               int depth);
474
475 #endif