OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / sese.h
1 /* Single entry single exit control flow regions.
2    Copyright (C) 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Jan Sjodin <jan.sjodin@amd.com> and
5    Sebastian Pop <sebastian.pop@amd.com>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #ifndef GCC_SESE_H
24 #define GCC_SESE_H
25
26 /* A Single Entry, Single Exit region is a part of the CFG delimited
27    by two edges.  */
28 typedef struct sese_s
29 {
30   /* Single ENTRY and single EXIT from the SESE region.  */
31   edge entry, exit;
32
33   /* Parameters used within the SCOP.  */
34   VEC (tree, heap) *params;
35
36   /* Loops completely contained in the SCOP.  */
37   bitmap loops;
38   VEC (loop_p, heap) *loop_nest;
39
40   /* Are we allowed to add more params?  This is for debugging purpose.  We
41      can only add new params before generating the bb domains, otherwise they
42      become invalid.  */
43   bool add_params;
44 } *sese;
45
46 #define SESE_ENTRY(S) (S->entry)
47 #define SESE_ENTRY_BB(S) (S->entry->dest)
48 #define SESE_EXIT(S) (S->exit)
49 #define SESE_EXIT_BB(S) (S->exit->dest)
50 #define SESE_PARAMS(S) (S->params)
51 #define SESE_LOOPS(S) (S->loops)
52 #define SESE_LOOP_NEST(S) (S->loop_nest)
53 #define SESE_ADD_PARAMS(S) (S->add_params)
54
55 extern sese new_sese (edge, edge);
56 extern void free_sese (sese);
57 extern void sese_insert_phis_for_liveouts (sese, basic_block, edge, edge);
58 extern void build_sese_loop_nests (sese);
59 extern edge copy_bb_and_scalar_dependences (basic_block, sese, edge,
60                                             VEC (tree, heap) *);
61 extern struct loop *outermost_loop_in_sese (sese, basic_block);
62 extern void insert_loop_close_phis (htab_t, loop_p);
63 extern void insert_guard_phis (basic_block, edge, edge, htab_t, htab_t);
64 extern tree scalar_evolution_in_region (sese, loop_p, tree);
65
66 /* Check that SESE contains LOOP.  */
67
68 static inline bool
69 sese_contains_loop (sese sese, struct loop *loop)
70 {
71   return bitmap_bit_p (SESE_LOOPS (sese), loop->num);
72 }
73
74 /* The number of parameters in REGION. */
75
76 static inline unsigned
77 sese_nb_params (sese region)
78 {
79   return VEC_length (tree, SESE_PARAMS (region));
80 }
81
82 /* Checks whether BB is contained in the region delimited by ENTRY and
83    EXIT blocks.  */
84
85 static inline bool
86 bb_in_region (basic_block bb, basic_block entry, basic_block exit)
87 {
88 #ifdef ENABLE_CHECKING
89   {
90     edge e;
91     edge_iterator ei;
92
93     /* Check that there are no edges coming in the region: all the
94        predecessors of EXIT are dominated by ENTRY.  */
95     FOR_EACH_EDGE (e, ei, exit->preds)
96       dominated_by_p (CDI_DOMINATORS, e->src, entry);
97   }
98 #endif
99
100   return dominated_by_p (CDI_DOMINATORS, bb, entry)
101          && !(dominated_by_p (CDI_DOMINATORS, bb, exit)
102               && !dominated_by_p (CDI_DOMINATORS, entry, exit));
103 }
104
105 /* Checks whether BB is contained in the region delimited by ENTRY and
106    EXIT blocks.  */
107
108 static inline bool
109 bb_in_sese_p (basic_block bb, sese region)
110 {
111   basic_block entry = SESE_ENTRY_BB (region);
112   basic_block exit = SESE_EXIT_BB (region);
113
114   return bb_in_region (bb, entry, exit);
115 }
116
117 /* Returns true when NAME is defined in REGION.  */
118
119 static inline bool
120 defined_in_sese_p (tree name, sese region)
121 {
122   gimple stmt = SSA_NAME_DEF_STMT (name);
123   basic_block bb = gimple_bb (stmt);
124
125   return bb && bb_in_sese_p (bb, region);
126 }
127
128 /* Returns true when LOOP is in REGION.  */
129
130 static inline bool
131 loop_in_sese_p (struct loop *loop, sese region)
132 {
133   return (bb_in_sese_p (loop->header, region)
134           && bb_in_sese_p (loop->latch, region));
135 }
136
137 /* Returns the loop depth of LOOP in REGION.  The loop depth
138    is the same as the normal loop depth, but limited by a region.
139
140    Example:
141
142    loop_0
143      loop_1
144        {
145          S0
146             <- region start
147          S1
148
149          loop_2
150            S2
151
152          S3
153             <- region end
154        }
155
156     loop_0 does not exist in the region -> invalid
157     loop_1 exists, but is not completely contained in the region -> depth 0
158     loop_2 is completely contained -> depth 1  */
159
160 static inline unsigned int
161 sese_loop_depth (sese region, loop_p loop)
162 {
163   unsigned int depth = 0;
164
165   gcc_assert ((!loop_in_sese_p (loop, region)
166                && (SESE_ENTRY_BB (region)->loop_father == loop
167                    || SESE_EXIT (region)->src->loop_father == loop))
168               || loop_in_sese_p (loop, region));
169
170   while (loop_in_sese_p (loop, region))
171     {
172       depth++;
173       loop = loop_outer (loop);
174     }
175
176   return depth;
177 }
178
179 /* Splits BB to make a single entry single exit region.  */
180
181 static inline sese
182 split_region_for_bb (basic_block bb)
183 {
184   edge entry, exit;
185
186   if (single_pred_p (bb))
187     entry = single_pred_edge (bb);
188   else
189     {
190       entry = split_block_after_labels (bb);
191       bb = single_succ (bb);
192     }
193
194   if (single_succ_p (bb))
195     exit = single_succ_edge (bb);
196   else
197     {
198       gimple_stmt_iterator gsi = gsi_last_bb (bb);
199       gsi_prev (&gsi);
200       exit = split_block (bb, gsi_stmt (gsi));
201     }
202
203   return new_sese (entry, exit);
204 }
205
206 /* Returns the block preceding the entry of a SESE.  */
207
208 static inline basic_block
209 block_before_sese (sese sese)
210 {
211   return SESE_ENTRY (sese)->src;
212 }
213
214 \f
215
216 /* A single entry single exit specialized for conditions.  */
217
218 typedef struct ifsese_s {
219   sese region;
220   sese true_region;
221   sese false_region;
222 } *ifsese;
223
224 extern void if_region_set_false_region (ifsese, sese);
225 extern ifsese move_sese_in_condition (sese);
226 extern edge get_true_edge_from_guard_bb (basic_block);
227 extern edge get_false_edge_from_guard_bb (basic_block);
228 extern void set_ifsese_condition (ifsese, tree);
229
230 static inline edge
231 if_region_entry (ifsese if_region)
232 {
233   return SESE_ENTRY (if_region->region);
234 }
235
236 static inline edge
237 if_region_exit (ifsese if_region)
238 {
239   return SESE_EXIT (if_region->region);
240 }
241
242 static inline basic_block
243 if_region_get_condition_block (ifsese if_region)
244 {
245   return if_region_entry (if_region)->dest;
246 }
247
248 /* Structure containing the mapping between the old names and the new
249    names used after block copy in the new loop context.  */
250 typedef struct rename_map_elt_s
251 {
252   tree old_name, expr;
253 } *rename_map_elt;
254
255 DEF_VEC_P(rename_map_elt);
256 DEF_VEC_ALLOC_P (rename_map_elt, heap);
257
258 extern void debug_rename_map (htab_t);
259 extern hashval_t rename_map_elt_info (const void *);
260 extern int eq_rename_map_elts (const void *, const void *);
261
262 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW.  */
263
264 static inline rename_map_elt
265 new_rename_map_elt (tree old_name, tree expr)
266 {
267   rename_map_elt res;
268
269   res = XNEW (struct rename_map_elt_s);
270   res->old_name = old_name;
271   res->expr = expr;
272
273   return res;
274 }
275
276 /* Structure containing the mapping between the CLooG's induction
277    variable and the type of the old induction variable.  */
278 typedef struct ivtype_map_elt_s
279 {
280   tree type;
281   const char *cloog_iv;
282 } *ivtype_map_elt;
283
284 extern void debug_ivtype_map (htab_t);
285 extern hashval_t ivtype_map_elt_info (const void *);
286 extern int eq_ivtype_map_elts (const void *, const void *);
287
288 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW.  */
289
290 static inline ivtype_map_elt
291 new_ivtype_map_elt (const char *cloog_iv, tree type)
292 {
293   ivtype_map_elt res;
294
295   res = XNEW (struct ivtype_map_elt_s);
296   res->cloog_iv = cloog_iv;
297   res->type = type;
298
299   return res;
300 }
301
302 /* Free and compute again all the dominators information.  */
303
304 static inline void
305 recompute_all_dominators (void)
306 {
307   mark_irreducible_loops ();
308   free_dominance_info (CDI_DOMINATORS);
309   calculate_dominance_info (CDI_DOMINATORS);
310 }
311
312 typedef struct gimple_bb
313 {
314   basic_block bb;
315
316   /* Lists containing the restrictions of the conditional statements
317      dominating this bb.  This bb can only be executed, if all conditions
318      are true.
319
320      Example:
321
322      for (i = 0; i <= 20; i++)
323      {
324        A
325
326        if (2i <= 8)
327          B
328      }
329
330      So for B there is an additional condition (2i <= 8).
331
332      List of COND_EXPR and SWITCH_EXPR.  A COND_EXPR is true only if the
333      corresponding element in CONDITION_CASES is not NULL_TREE.  For a
334      SWITCH_EXPR the corresponding element in CONDITION_CASES is a
335      CASE_LABEL_EXPR.  */
336   VEC (gimple, heap) *conditions;
337   VEC (gimple, heap) *condition_cases;
338   VEC (data_reference_p, heap) *data_refs;
339 } *gimple_bb_p;
340
341 #define GBB_BB(GBB) GBB->bb
342 #define GBB_DATA_REFS(GBB) GBB->data_refs
343 #define GBB_CONDITIONS(GBB) GBB->conditions
344 #define GBB_CONDITION_CASES(GBB) GBB->condition_cases
345
346 /* Return the innermost loop that contains the basic block GBB.  */
347
348 static inline struct loop *
349 gbb_loop (struct gimple_bb *gbb)
350 {
351   return GBB_BB (gbb)->loop_father;
352 }
353
354 /* Returns the gimple loop, that corresponds to the loop_iterator_INDEX.
355    If there is no corresponding gimple loop, we return NULL.  */
356
357 static inline loop_p
358 gbb_loop_at_index (gimple_bb_p gbb, sese region, int index)
359 {
360   loop_p loop = gbb_loop (gbb);
361   int depth = sese_loop_depth (region, loop);
362
363   while (--depth > index)
364     loop = loop_outer (loop);
365
366   gcc_assert (sese_contains_loop (region, loop));
367
368   return loop;
369 }
370
371 /* The number of common loops in REGION for GBB1 and GBB2.  */
372
373 static inline int
374 nb_common_loops (sese region, gimple_bb_p gbb1, gimple_bb_p gbb2)
375 {
376   loop_p l1 = gbb_loop (gbb1);
377   loop_p l2 = gbb_loop (gbb2);
378   loop_p common = find_common_loop (l1, l2);
379
380   return sese_loop_depth (region, common);
381 }
382
383 /* Return true when DEF can be analyzed in REGION by the scalar
384    evolution analyzer.  */
385
386 static inline bool
387 scev_analyzable_p (tree def, sese region)
388 {
389   loop_p loop;
390   tree scev;
391   tree type = TREE_TYPE (def);
392
393   /* When Graphite generates code for a scev, the code generator
394      expresses the scev in function of a single induction variable.
395      This is unsafe for floating point computations, as it may replace
396      a floating point sum reduction with a multiplication.  The
397      following test returns false for non integer types to avoid such
398      problems.  */
399   if (!INTEGRAL_TYPE_P (type)
400       && !POINTER_TYPE_P (type))
401     return false;
402
403   loop = loop_containing_stmt (SSA_NAME_DEF_STMT (def));
404   scev = scalar_evolution_in_region (region, loop, def);
405
406   return !chrec_contains_undetermined (scev)
407     && TREE_CODE (scev) != SSA_NAME
408     && (tree_does_not_contain_chrecs (scev)
409         || evolution_function_is_affine_p (scev));
410 }
411
412 #endif