New Graphite files.
[gcc.git] / gcc / sese.h
1 /* Single entry single exit control flow regions.
2 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Jan Sjodin <jan.sjodin@amd.com> and
4 Sebastian Pop <sebastian.pop@amd.com>.
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_SESE_H
23 #define GCC_SESE_H
24
25 /* A Single Entry, Single Exit region is a part of the CFG delimited
26 by two edges. */
27 typedef struct sese_s
28 {
29 /* Single ENTRY and single EXIT from the SESE region. */
30 edge entry, exit;
31
32 /* Parameters used within the SCOP. */
33 VEC (tree, heap) *params;
34
35 /* Used to quickly retrieve the index of a parameter in PARAMS. */
36 htab_t params_index;
37
38 /* Store the names of the parameters that are passed to CLooG. */
39 char **params_names;
40
41 /* Loops completely contained in the SCOP. */
42 bitmap loops;
43 VEC (loop_p, heap) *loop_nest;
44
45 /* Are we allowed to add more params? This is for debugging purpose. We
46 can only add new params before generating the bb domains, otherwise they
47 become invalid. */
48 bool add_params;
49 } *sese;
50
51 #define SESE_ENTRY(S) (S->entry)
52 #define SESE_ENTRY_BB(S) (S->entry->dest)
53 #define SESE_EXIT(S) (S->exit)
54 #define SESE_EXIT_BB(S) (S->exit->dest)
55 #define SESE_PARAMS(S) (S->params)
56 #define SESE_PARAMS_INDEX(S) (S->params_index)
57 #define SESE_PARAMS_NAMES(S) (S->params_names)
58 #define SESE_LOOPS(S) (S->loops)
59 #define SESE_LOOP_NEST(S) (S->loop_nest)
60 #define SESE_ADD_PARAMS(S) (S->add_params)
61
62 extern sese new_sese (edge, edge);
63 extern void free_sese (sese);
64 extern void sese_insert_phis_for_liveouts (sese, basic_block, edge, edge);
65 extern void sese_adjust_liveout_phis (sese, htab_t, basic_block, edge, edge);
66 extern void build_sese_loop_nests (sese);
67 extern edge copy_bb_and_scalar_dependences (basic_block, sese, edge, htab_t);
68 extern struct loop *outermost_loop_in_sese (sese, basic_block);
69 extern void insert_loop_close_phis (htab_t, loop_p);
70 extern void insert_guard_phis (basic_block, edge, edge, htab_t, htab_t);
71 extern void sese_reset_aux_in_loops (sese);
72 extern tree scalar_evolution_in_region (sese, loop_p, tree);
73
74 /* Check that SESE contains LOOP. */
75
76 static inline bool
77 sese_contains_loop (sese sese, struct loop *loop)
78 {
79 return bitmap_bit_p (SESE_LOOPS (sese), loop->num);
80 }
81
82 /* The number of parameters in REGION. */
83
84 static inline unsigned
85 sese_nb_params (sese region)
86 {
87 return VEC_length (tree, SESE_PARAMS (region));
88 }
89
90 /* Checks whether BB is contained in the region delimited by ENTRY and
91 EXIT blocks. */
92
93 static inline bool
94 bb_in_region (basic_block bb, basic_block entry, basic_block exit)
95 {
96 #ifdef ENABLE_CHECKING
97 {
98 edge e;
99 edge_iterator ei;
100
101 /* Check that there are no edges coming in the region: all the
102 predecessors of EXIT are dominated by ENTRY. */
103 FOR_EACH_EDGE (e, ei, exit->preds)
104 dominated_by_p (CDI_DOMINATORS, e->src, entry);
105
106 /* Check that there are no edges going out of the region: the
107 entry is post-dominated by the exit. FIXME: This cannot be
108 checked right now as the CDI_POST_DOMINATORS are needed. */
109 }
110 #endif
111
112 return dominated_by_p (CDI_DOMINATORS, bb, entry)
113 && !(dominated_by_p (CDI_DOMINATORS, bb, exit)
114 && !dominated_by_p (CDI_DOMINATORS, entry, exit));
115 }
116
117 /* Checks whether BB is contained in the region delimited by ENTRY and
118 EXIT blocks. */
119
120 static inline bool
121 bb_in_sese_p (basic_block bb, sese region)
122 {
123 basic_block entry = SESE_ENTRY_BB (region);
124 basic_block exit = SESE_EXIT_BB (region);
125
126 return bb_in_region (bb, entry, exit);
127 }
128
129 /* Returns true when NAME is defined in REGION. */
130
131 static inline bool
132 defined_in_sese_p (tree name, sese region)
133 {
134 gimple stmt = SSA_NAME_DEF_STMT (name);
135 basic_block bb = gimple_bb (stmt);
136
137 return bb && bb_in_sese_p (bb, region);
138 }
139
140 /* Returns true when LOOP is in REGION. */
141
142 static inline bool
143 loop_in_sese_p (struct loop *loop, sese region)
144 {
145 return (bb_in_sese_p (loop->header, region)
146 && bb_in_sese_p (loop->latch, region));
147 }
148
149 /* Returns the loop depth of LOOP in REGION. The loop depth
150 is the same as the normal loop depth, but limited by a region.
151
152 Example:
153
154 loop_0
155 loop_1
156 {
157 S0
158 <- region start
159 S1
160
161 loop_2
162 S2
163
164 S3
165 <- region end
166 }
167
168 loop_0 does not exist in the region -> invalid
169 loop_1 exists, but is not completely contained in the region -> depth 0
170 loop_2 is completely contained -> depth 1 */
171
172 static inline unsigned int
173 sese_loop_depth (sese region, loop_p loop)
174 {
175 unsigned int depth = 0;
176
177 gcc_assert ((!loop_in_sese_p (loop, region)
178 && (SESE_ENTRY_BB (region)->loop_father == loop
179 || SESE_EXIT (region)->src->loop_father == loop))
180 || loop_in_sese_p (loop, region));
181
182 while (loop_in_sese_p (loop, region))
183 {
184 depth++;
185 loop = loop_outer (loop);
186 }
187
188 return depth;
189 }
190
191 /* Returns the block preceding the entry of a SESE. */
192
193 static inline basic_block
194 block_before_sese (sese sese)
195 {
196 return SESE_ENTRY (sese)->src;
197 }
198
199 /* Stores the INDEX in a vector for a given clast NAME. */
200
201 typedef struct clast_name_index {
202 int index;
203 const char *name;
204 } *clast_name_index_p;
205
206 /* Returns a pointer to a new element of type clast_name_index_p built
207 from NAME and INDEX. */
208
209 static inline clast_name_index_p
210 new_clast_name_index (const char *name, int index)
211 {
212 clast_name_index_p res = XNEW (struct clast_name_index);
213
214 res->name = name;
215 res->index = index;
216 return res;
217 }
218
219 /* For a given clast NAME, returns -1 if it does not correspond to any
220 parameter, or otherwise, returns the index in the PARAMS or
221 SCATTERING_DIMENSIONS vector. */
222
223 static inline int
224 clast_name_to_index (const char *name, htab_t index_table)
225 {
226 struct clast_name_index tmp;
227 PTR *slot;
228
229 tmp.name = name;
230 slot = htab_find_slot (index_table, &tmp, NO_INSERT);
231
232 if (slot && *slot)
233 return ((struct clast_name_index *) *slot)->index;
234
235 return -1;
236 }
237
238 /* Records in INDEX_TABLE the INDEX for NAME. */
239
240 static inline void
241 save_clast_name_index (htab_t index_table, const char *name, int index)
242 {
243 struct clast_name_index tmp;
244 PTR *slot;
245
246 tmp.name = name;
247 slot = htab_find_slot (index_table, &tmp, INSERT);
248
249 if (slot)
250 *slot = new_clast_name_index (name, index);
251 }
252
253 /* Print to stderr the element ELT. */
254
255 static inline void
256 debug_clast_name_index (clast_name_index_p elt)
257 {
258 fprintf (stderr, "(index = %d, name = %s)\n", elt->index, elt->name);
259 }
260
261 /* Helper function for debug_rename_map. */
262
263 static inline int
264 debug_clast_name_indexes_1 (void **slot, void *s ATTRIBUTE_UNUSED)
265 {
266 struct clast_name_index *entry = (struct clast_name_index *) *slot;
267 debug_clast_name_index (entry);
268 return 1;
269 }
270
271 /* Print to stderr all the elements of MAP. */
272
273 static inline void
274 debug_clast_name_indexes (htab_t map)
275 {
276 htab_traverse (map, debug_clast_name_indexes_1, NULL);
277 }
278
279 /* Computes a hash function for database element ELT. */
280
281 static inline hashval_t
282 clast_name_index_elt_info (const void *elt)
283 {
284 return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
285 }
286
287 /* Compares database elements E1 and E2. */
288
289 static inline int
290 eq_clast_name_indexes (const void *e1, const void *e2)
291 {
292 const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
293 const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
294
295 return (elt1->name == elt2->name);
296 }
297
298 \f
299
300 /* A single entry single exit specialized for conditions. */
301
302 typedef struct ifsese_s {
303 sese region;
304 sese true_region;
305 sese false_region;
306 } *ifsese;
307
308 extern void if_region_set_false_region (ifsese, sese);
309 extern ifsese create_if_region_on_edge (edge, tree);
310 extern ifsese move_sese_in_condition (sese);
311 extern edge get_true_edge_from_guard_bb (basic_block);
312 extern edge get_false_edge_from_guard_bb (basic_block);
313
314 static inline edge
315 if_region_entry (ifsese if_region)
316 {
317 return SESE_ENTRY (if_region->region);
318 }
319
320 static inline edge
321 if_region_exit (ifsese if_region)
322 {
323 return SESE_EXIT (if_region->region);
324 }
325
326 static inline basic_block
327 if_region_get_condition_block (ifsese if_region)
328 {
329 return if_region_entry (if_region)->dest;
330 }
331
332 /* Structure containing the mapping between the old names and the new
333 names used after block copy in the new loop context. */
334 typedef struct rename_map_elt_s
335 {
336 tree old_name, expr;
337 } *rename_map_elt;
338
339 DEF_VEC_P(rename_map_elt);
340 DEF_VEC_ALLOC_P (rename_map_elt, heap);
341
342 extern void debug_rename_map (htab_t);
343 extern hashval_t rename_map_elt_info (const void *);
344 extern int eq_rename_map_elts (const void *, const void *);
345 extern void set_rename (htab_t, tree, tree);
346
347 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
348
349 static inline rename_map_elt
350 new_rename_map_elt (tree old_name, tree expr)
351 {
352 rename_map_elt res;
353
354 res = XNEW (struct rename_map_elt_s);
355 res->old_name = old_name;
356 res->expr = expr;
357
358 return res;
359 }
360
361 /* Structure containing the mapping between the CLooG's induction
362 variable and the type of the old induction variable. */
363 typedef struct ivtype_map_elt_s
364 {
365 tree type;
366 const char *cloog_iv;
367 } *ivtype_map_elt;
368
369 extern void debug_ivtype_map (htab_t);
370 extern hashval_t ivtype_map_elt_info (const void *);
371 extern int eq_ivtype_map_elts (const void *, const void *);
372
373 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
374
375 static inline ivtype_map_elt
376 new_ivtype_map_elt (const char *cloog_iv, tree type)
377 {
378 ivtype_map_elt res;
379
380 res = XNEW (struct ivtype_map_elt_s);
381 res->cloog_iv = cloog_iv;
382 res->type = type;
383
384 return res;
385 }
386
387 /* Free and compute again all the dominators information. */
388
389 static inline void
390 recompute_all_dominators (void)
391 {
392 mark_irreducible_loops ();
393 free_dominance_info (CDI_DOMINATORS);
394 free_dominance_info (CDI_POST_DOMINATORS);
395 calculate_dominance_info (CDI_DOMINATORS);
396 calculate_dominance_info (CDI_POST_DOMINATORS);
397 }
398
399 typedef struct gimple_bb
400 {
401 basic_block bb;
402
403 /* Lists containing the restrictions of the conditional statements
404 dominating this bb. This bb can only be executed, if all conditions
405 are true.
406
407 Example:
408
409 for (i = 0; i <= 20; i++)
410 {
411 A
412
413 if (2i <= 8)
414 B
415 }
416
417 So for B there is an additional condition (2i <= 8).
418
419 List of COND_EXPR and SWITCH_EXPR. A COND_EXPR is true only if the
420 corresponding element in CONDITION_CASES is not NULL_TREE. For a
421 SWITCH_EXPR the corresponding element in CONDITION_CASES is a
422 CASE_LABEL_EXPR. */
423 VEC (gimple, heap) *conditions;
424 VEC (gimple, heap) *condition_cases;
425 VEC (data_reference_p, heap) *data_refs;
426 htab_t cloog_iv_types;
427 } *gimple_bb_p;
428
429 #define GBB_BB(GBB) GBB->bb
430 #define GBB_DATA_REFS(GBB) GBB->data_refs
431 #define GBB_CONDITIONS(GBB) GBB->conditions
432 #define GBB_CONDITION_CASES(GBB) GBB->condition_cases
433 #define GBB_CLOOG_IV_TYPES(GBB) GBB->cloog_iv_types
434
435 /* Return the innermost loop that contains the basic block GBB. */
436
437 static inline struct loop *
438 gbb_loop (struct gimple_bb *gbb)
439 {
440 return GBB_BB (gbb)->loop_father;
441 }
442
443 /* Returns the gimple loop, that corresponds to the loop_iterator_INDEX.
444 If there is no corresponding gimple loop, we return NULL. */
445
446 static inline loop_p
447 gbb_loop_at_index (gimple_bb_p gbb, sese region, int index)
448 {
449 loop_p loop = gbb_loop (gbb);
450 int depth = sese_loop_depth (region, loop);
451
452 while (--depth > index)
453 loop = loop_outer (loop);
454
455 gcc_assert (sese_contains_loop (region, loop));
456
457 return loop;
458 }
459
460 /* The number of common loops in REGION for GBB1 and GBB2. */
461
462 static inline int
463 nb_common_loops (sese region, gimple_bb_p gbb1, gimple_bb_p gbb2)
464 {
465 loop_p l1 = gbb_loop (gbb1);
466 loop_p l2 = gbb_loop (gbb2);
467 loop_p common = find_common_loop (l1, l2);
468
469 return sese_loop_depth (region, common);
470 }
471
472 extern void print_gimple_bb (FILE *, gimple_bb_p, int, int);
473 extern void debug_gbb (gimple_bb_p, int);
474
475 #endif