1 /* Gimple Represented as Polyhedra.
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@inria.fr>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_GRAPHITE_H
22 #define GCC_GRAPHITE_H
24 #include "tree-data-ref.h"
26 typedef struct graphite_bb *graphite_bb_p;
27 DEF_VEC_P(graphite_bb_p);
28 DEF_VEC_ALLOC_P (graphite_bb_p, heap);
31 DEF_VEC_ALLOC_P (scop_p, heap);
33 static inline int scop_nb_loops (scop_p scop);
34 static inline unsigned scop_nb_params (scop_p scop);
35 static inline bool scop_contains_loop (scop_p scop, struct loop *loop);
37 typedef struct graphite_bb
42 /* The static schedule contains the textual order for every loop layer.
88 - One SCoP can never contain two bbs with the same schedule timestamp.
89 - All bbs at the same loop depth have a consecutive ordering (no gaps). */
90 lambda_vector static_schedule;
92 /* The iteration domain of this bb. It contains this columns:
93 - In/Eq: If this line is a equation or inequation.
94 - For every loop iterator one column.
95 - One column for every parameter in this SCoP.
96 - The constant column to add integers to the (in)equations.
100 for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
101 for (j = 2; j <= 2*i + 5; j++)
102 for (k = 0; k <= 5; k++)
105 Loop iterators: i, j, k
110 1 1 0 0 -1 7 -8 # i >= a - 7b + 8
111 1 -1 0 0 3 13 20 # i <= 3a + 13b + 20
112 1 0 1 0 0 0 -2 # j >= 2
113 1 2 -1 0 0 0 5 # j <= 2i + 5
114 1 0 0 1 0 0 0 # k >= 0
115 1 0 0 -1 0 0 5 # k <= 5
117 The number of loop iterators may change and is not connected to the
118 number of loops, that surrounded this bb in the gimple code. */
121 /* Lists containing the restrictions of the conditional statements
122 dominating this bb. This bb can only be executed, if all conditions
127 for (i = 0; i <= 20; i++)
135 So for B there is an additional condition (2i <= 8).
137 TODO: Add these restrictions to the domain matrix.
139 List of COND_EXPR and SWITCH_EXPR. A COND_EXPR is true only if the
140 corresponding element in CONDITION_CASES is not NULL_TREE. For a
141 SWITCH_EXPR the corresponding element in CONDITION_CASES is a
143 VEC (gimple, heap) *conditions;
144 VEC (gimple, heap) *condition_cases;
146 /* LOOPS contains for every column in the graphite domain the corresponding
147 gimple loop. If there exists no corresponding gimple loop LOOPS contains
154 for (i = 0; i <= 20; i++)
155 for (j = 5; j <= 10; j++)
166 Original loops vector:
170 After some changes (Exchange i and j, strip-mine i):
176 1 0 0 -1 0 20 # i <= 20
178 1 -1 0 0 0 10 # j <= 10
179 1 0 -1 1 0 0 # ii <= i
180 1 0 1 -1 0 1 # ii + 1 >= i
181 1 0 -1 0 2 0 # ii <= 2k
182 1 0 1 0 -2 0 # ii >= 2k
186 Loop j NULL Loop i NULL
188 Means the original loop i is now at column two of the domain and
189 loop j in the original loop nest is now at column 0. Column 1 and
191 VEC (loop_p, heap) *loops;
193 lambda_vector compressed_alpha_matrix;
194 CloogMatrix *dynamic_schedule;
195 VEC (data_reference_p, heap) *data_refs;
198 #define GBB_BB(GBB) GBB->bb
199 #define GBB_SCOP(GBB) GBB->scop
200 #define GBB_STATIC_SCHEDULE(GBB) GBB->static_schedule
201 #define GBB_DATA_REFS(GBB) GBB->data_refs
202 #define GBB_ALPHA(GBB) GBB->compressed_alpha_matrix
203 #define GBB_DYNAMIC_SCHEDULE(GBB) GBB->dynamic_schedule
204 #define GBB_DOMAIN(GBB) GBB->domain
205 #define GBB_CONDITIONS(GBB) GBB->conditions
206 #define GBB_CONDITION_CASES(GBB) GBB->condition_cases
207 #define GBB_LOOPS(GBB) GBB->loops
209 /* Return the loop that contains the basic block GBB. */
211 static inline struct loop *
212 gbb_loop (struct graphite_bb *gbb)
214 return GBB_BB (gbb)->loop_father;
217 /* Calculate the number of loops around GB in the current SCOP. Only
218 works if GBB_DOMAIN is built. */
221 gbb_nb_loops (const struct graphite_bb *gb)
223 scop_p scop = GBB_SCOP (gb);
225 if (GBB_DOMAIN (gb) == NULL)
228 return GBB_DOMAIN (gb)->NbColumns - scop_nb_params (scop) - 2;
231 /* Returns the gimple loop, that corresponds to the loop_iterator_INDEX.
232 If there is no corresponding gimple loop, we return NULL. */
235 gbb_loop_at_index (graphite_bb_p gb, int index)
237 return VEC_index (loop_p, GBB_LOOPS (gb), index);
240 /* Returns the index of LOOP in the loop nest around GB. */
243 gbb_loop_index (graphite_bb_p gb, loop_p loop)
248 for (i = 0; VEC_iterate (loop_p, GBB_LOOPS (gb), i, l); i++)
255 struct loop_to_cloog_loop_str
257 unsigned int loop_num;
258 unsigned int loop_position; /* The column that represents this loop. */
259 CloogLoop *cloog_loop;
262 typedef struct name_tree
269 DEF_VEC_P(name_tree);
270 DEF_VEC_ALLOC_P (name_tree, heap);
272 /* A Single Entry, Single Exit region is a part of the CFG delimited
279 #define SESE_ENTRY(S) (S->entry)
280 #define SESE_EXIT(S) (S->exit)
282 /* A SCOP is a Static Control Part of the program, simple enough to be
283 represented in polyhedral form. */
286 /* A SCOP is defined as a SESE region. */
289 /* All the basic blocks in this scop. They have extra information
290 attached to them, in the graphite_bb structure. */
291 VEC (graphite_bb_p, heap) *bbs;
293 /* Set for a basic block index when it belongs to this SCOP. */
296 lambda_vector static_schedule;
298 /* Parameters used within the SCOP. */
299 VEC (name_tree, heap) *params;
301 /* A collection of old induction variables*/
302 VEC (name_tree, heap) *old_ivs;
304 /* Loops completely contained in the SCOP. */
306 VEC (loop_p, heap) *loop_nest;
308 /* ??? It looks like a global mapping loop_id -> cloog_loop would work. */
309 htab_t loop2cloog_loop;
311 /* Cloog representation of this scop. */
312 CloogProgram *program;
314 /* Are we allowed to add more params? This is for debugging purpose. We
315 can only add new params before generating the bb domains, otherwise they
320 #define SCOP_BBS(S) S->bbs
321 #define SCOP_BBS_B(S) S->bbs_b
322 #define SCOP_REGION(S) S->region
323 /* SCOP_ENTRY bb dominates all the bbs of the scop. SCOP_EXIT bb
324 post-dominates all the bbs of the scop. SCOP_EXIT potentially
325 contains non affine data accesses, side effect statements or
326 difficult constructs, and thus is not considered part of the scop,
327 but just a boundary. SCOP_ENTRY is considered part of the scop. */
328 #define SCOP_ENTRY(S) (SESE_ENTRY (SCOP_REGION (S))->dest)
329 #define SCOP_EXIT(S) (SESE_EXIT (SCOP_REGION (S))->dest)
330 #define SCOP_STATIC_SCHEDULE(S) S->static_schedule
331 #define SCOP_LOOPS(S) S->loops
332 #define SCOP_LOOP_NEST(S) S->loop_nest
333 #define SCOP_ADD_PARAMS(S) S->add_params
334 #define SCOP_PARAMS(S) S->params
335 #define SCOP_OLDIVS(S) S->old_ivs
336 #define SCOP_PROG(S) S->program
337 #define SCOP_LOOP2CLOOG_LOOP(S) S->loop2cloog_loop
338 #define SCOP_LOOPS_MAPPING(S) S->loops_mapping
340 extern void debug_scop (scop_p, int);
341 extern void debug_scops (int);
342 extern void print_graphite_bb (FILE *, graphite_bb_p, int, int);
343 extern void debug_gbb (graphite_bb_p, int);
344 extern void dot_scop (scop_p);
345 extern void dot_all_scops (void);
346 extern void debug_clast_stmt (struct clast_stmt *);
347 extern void debug_rename_map (htab_t);
348 extern void debug_loop_vec (graphite_bb_p gb);
349 extern void debug_oldivs (scop_p);
351 /* Describes the type of an iv stack entry. */
353 iv_stack_entry_unknown = 0,
356 } iv_stack_entry_kind;
358 /* Data contained in an iv stack entry. */
359 typedef union iv_stack_entry_data_union
363 } iv_stack_entry_data;
365 /* Datatype for loop iv stack entry. */
366 typedef struct iv_stack_entry_struct
368 iv_stack_entry_kind kind;
369 iv_stack_entry_data data;
372 typedef iv_stack_entry *iv_stack_entry_p;
374 DEF_VEC_P(iv_stack_entry_p);
375 DEF_VEC_ALLOC_P(iv_stack_entry_p,heap);
377 typedef VEC(iv_stack_entry_p, heap) **loop_iv_stack;
378 extern void debug_loop_iv_stack (loop_iv_stack);
380 /* Return the number of gimple loops contained in SCOP. */
383 scop_nb_loops (scop_p scop)
385 return VEC_length (loop_p, SCOP_LOOP_NEST (scop));
388 /* Returns the number of parameters for SCOP. */
390 static inline unsigned
391 scop_nb_params (scop_p scop)
393 return VEC_length (name_tree, SCOP_PARAMS (scop));
396 /* Return the dimension of the domains for SCOP. */
399 scop_dim_domain (scop_p scop)
401 return scop_nb_loops (scop) + scop_nb_params (scop) + 1;
404 /* Return the dimension of the domains for GB. */
407 gbb_dim_domain (graphite_bb_p gb)
409 return scop_dim_domain (GBB_SCOP (gb));
412 /* Returns the dimensionality of a loop iteration domain for a given
413 loop, identified by LOOP_NUM, with respect to SCOP. */
416 loop_domain_dim (unsigned int loop_num, scop_p scop)
418 struct loop_to_cloog_loop_str tmp, *slot;
419 htab_t tab = SCOP_LOOP2CLOOG_LOOP (scop);
421 tmp.loop_num = loop_num;
422 slot = (struct loop_to_cloog_loop_str *) htab_find (tab, &tmp);
424 /* The loop containing the entry of the scop is not always part of
425 the SCoP, and it is not registered in SCOP_LOOP2CLOOG_LOOP. */
427 return scop_nb_params (scop) + 2;
429 return cloog_domain_dim (cloog_loop_domain (slot->cloog_loop)) + 2;
432 /* Returns the dimensionality of a loop iteration vector in a loop
433 iteration domain for a given loop (identified by LOOP_NUM) with
437 loop_iteration_vector_dim (unsigned int loop_num, scop_p scop)
439 return loop_domain_dim (loop_num, scop) - 2 - scop_nb_params (scop);
442 /* Checks, if SCOP contains LOOP. */
445 scop_contains_loop (scop_p scop, struct loop *loop)
447 return bitmap_bit_p (SCOP_LOOPS (scop), loop->num);
450 /* Returns the index of LOOP in the domain matrix for the SCOP. */
453 scop_loop_index (scop_p scop, struct loop *loop)
458 gcc_assert (scop_contains_loop (scop, loop));
460 for (i = 0; VEC_iterate (loop_p, SCOP_LOOP_NEST (scop), i, l); i++)
467 /* Return the index of innermost loop that contains the basic block
471 gbb_inner_most_loop_index (scop_p scop, graphite_bb_p gb)
473 return scop_loop_index(scop, gbb_loop (gb));
476 /* Return the outermost loop that contains the loop LOOP. The outer
477 loops are searched until a sibling for the outer loop is found. */
480 outer_most_loop_1 (scop_p scop, struct loop* loop, struct loop* current_outer)
482 return (!scop_contains_loop (scop, loop)) ? current_outer :
483 (loop->next != NULL) ? loop :
484 outer_most_loop_1 (scop, loop_outer (loop), loop);
487 /* Return the outermost loop that contains the loop LOOP. */
490 outer_most_loop (scop_p scop, struct loop *loop)
492 return outer_most_loop_1 (scop, loop, NULL);
495 /* Return the index of the outermost loop that contains the basic
499 gbb_outer_most_loop_index (scop_p scop, graphite_bb_p gb)
501 return scop_loop_index (scop, outer_most_loop (scop, gbb_loop (gb)));
504 /* Return the loop depth of LOOP in SCOP. */
506 static inline unsigned int
507 scop_gimple_loop_depth (scop_p scop, loop_p loop)
509 unsigned int depth = 0;
511 loop = loop_outer (loop);
513 while (scop_contains_loop (scop, loop))
516 loop = loop_outer (loop);
522 /* Static inline function prototypes. */
524 static inline unsigned scop_nb_params (scop_p scop);
526 /* Returns true when BB is in SCOP. */
529 bb_in_scop_p (basic_block bb, scop_p scop)
531 return bitmap_bit_p (SCOP_BBS_B (scop), bb->index);
534 /* Returns true when LOOP is in SCOP. */
537 loop_in_scop_p (struct loop *loop, scop_p scop)
539 return (bb_in_scop_p (loop->header, scop)
540 && bb_in_scop_p (loop->latch, scop));
543 /* Calculate the number of loops around LOOP in the SCOP. */
546 nb_loops_around_loop_in_scop (struct loop *l, scop_p scop)
550 for (; loop_in_scop_p (l, scop); d++, l = loop_outer (l));
555 /* Calculate the number of loops around GB in the current SCOP. */
558 nb_loops_around_gb (graphite_bb_p gb)
560 return nb_loops_around_loop_in_scop (gbb_loop (gb), GBB_SCOP (gb));
563 /* Returns the dimensionality of an enclosing loop iteration domain
564 with respect to enclosing SCoP for a given data reference REF. The
565 returned dimensionality is homogeneous (depth of loop nest + number
566 of SCoP parameters + const). */
569 ref_nb_loops (data_reference_p ref)
571 loop_p loop = loop_containing_stmt (DR_STMT (ref));
572 scop_p scop = DR_SCOP (ref);
574 return nb_loops_around_loop_in_scop (loop, scop) + scop_nb_params (scop) + 2;
577 #endif /* GCC_GRAPHITE_H */