gcc/cp/
[official-gcc.git] / gcc / sese.h
blob300b8441bffddb112c1b6ebe0296c5ac1b4d523e
1 /* Single entry single exit control flow regions.
2 Copyright (C) 2008-2016 Free Software Foundation, Inc.
3 Contributed by Jan Sjodin <jan.sjodin@amd.com> and
4 Sebastian Pop <sebastian.pop@amd.com>.
6 This file is part of GCC.
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.
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.
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/>. */
22 #ifndef GCC_SESE_H
23 #define GCC_SESE_H
25 typedef hash_map<basic_block, vec<basic_block> > bb_map_t;
26 typedef hash_map<tree, vec<tree> > rename_map_t;
27 typedef struct ifsese_s *ifsese;
28 /* First phi is the new codegenerated phi second one is original phi. */
29 typedef std::pair <gphi *, gphi *> phi_rename;
30 /* First edge is the init edge and second is the back edge w.r.t. a loop. */
31 typedef std::pair<edge, edge> init_back_edge_pair_t;
33 /* A Single Entry, Single Exit region is a part of the CFG delimited
34 by two edges. */
35 struct sese_l
37 sese_l (edge e, edge x) : entry (e), exit (x) {}
39 operator bool () const { return entry && exit; }
41 edge entry;
42 edge exit;
45 /* Get the entry of an sese S. */
47 static inline basic_block
48 get_entry_bb (sese_l &s)
50 return s.entry->dest;
53 /* Get the exit of an sese S. */
55 static inline basic_block
56 get_exit_bb (sese_l &s)
58 return s.exit->src;
61 /* Returns the index of V where ELEM can be found. -1 Otherwise. */
62 template<typename T>
63 int
64 vec_find (const vec<T> &v, const T &elem)
66 int i;
67 T t;
68 FOR_EACH_VEC_ELT (v, i, t)
69 if (elem == t)
70 return i;
71 return -1;
74 /* A helper structure for bookkeeping information about a scop in graphite. */
75 typedef struct sese_info_t
77 /* The SESE region. */
78 sese_l region;
80 /* Parameters used within the SCOP. */
81 vec<tree> params;
83 /* Maps an old name to one or more new names. When there are several new
84 names, one has to select the definition corresponding to the immediate
85 dominator. */
86 rename_map_t *rename_map;
88 /* Loops completely contained in this SESE. */
89 bitmap loops;
90 vec<loop_p> loop_nest;
92 /* Basic blocks contained in this SESE. */
93 vec<basic_block> bbs;
95 /* Copied basic blocks indexed by the original bb. */
96 bb_map_t *copied_bb_map;
98 /* A vector of phi nodes to be updated when all arguments are available. The
99 pair contains first the old_phi and second the new_phi. */
100 vec<phi_rename> incomplete_phis;
102 /* The condition region generated for this sese. */
103 ifsese if_region;
105 } *sese_info_p;
107 extern sese_info_p new_sese_info (edge, edge);
108 extern void free_sese_info (sese_info_p);
109 extern void sese_insert_phis_for_liveouts (sese_info_p, basic_block, edge, edge);
110 extern void build_sese_loop_nests (sese_info_p);
111 extern struct loop *outermost_loop_in_sese (sese_l &, basic_block);
112 extern tree scalar_evolution_in_region (const sese_l &, loop_p, tree);
113 extern bool scev_analyzable_p (tree, sese_l &);
114 extern bool invariant_in_sese_p_rec (tree, const sese_l &, bool *);
116 /* Check that SESE contains LOOP. */
118 static inline bool
119 sese_contains_loop (sese_info_p sese, struct loop *loop)
121 return bitmap_bit_p (sese->loops, loop->num);
124 /* The number of parameters in REGION. */
126 static inline unsigned
127 sese_nb_params (sese_info_p region)
129 return region->params.length ();
132 /* Checks whether BB is contained in the region delimited by ENTRY and
133 EXIT blocks. */
135 static inline bool
136 bb_in_region (const_basic_block bb, const_basic_block entry, const_basic_block exit)
138 /* FIXME: PR67842. */
139 #if 0
140 if (flag_checking)
142 edge e;
143 edge_iterator ei;
145 /* Check that there are no edges coming in the region: all the
146 predecessors of EXIT are dominated by ENTRY. */
147 FOR_EACH_EDGE (e, ei, exit->preds)
148 gcc_assert (dominated_by_p (CDI_DOMINATORS, e->src, entry));
150 #endif
152 return dominated_by_p (CDI_DOMINATORS, bb, entry)
153 && !(dominated_by_p (CDI_DOMINATORS, bb, exit)
154 && !dominated_by_p (CDI_DOMINATORS, entry, exit));
157 /* Checks whether BB is contained in the region delimited by ENTRY and
158 EXIT blocks. */
160 static inline bool
161 bb_in_sese_p (basic_block bb, const sese_l &r)
163 return bb_in_region (bb, r.entry->dest, r.exit->dest);
166 /* Returns true when STMT is defined in REGION. */
168 static inline bool
169 stmt_in_sese_p (gimple *stmt, const sese_l &r)
171 basic_block bb = gimple_bb (stmt);
172 return bb && bb_in_sese_p (bb, r);
175 /* Returns true when NAME is defined in REGION. */
177 static inline bool
178 defined_in_sese_p (tree name, const sese_l &r)
180 return stmt_in_sese_p (SSA_NAME_DEF_STMT (name), r);
183 /* Returns true when LOOP is in REGION. */
185 static inline bool
186 loop_in_sese_p (struct loop *loop, const sese_l &region)
188 return (bb_in_sese_p (loop->header, region)
189 && bb_in_sese_p (loop->latch, region));
192 /* Returns the loop depth of LOOP in REGION. The loop depth
193 is the same as the normal loop depth, but limited by a region.
195 Example:
197 loop_0
198 loop_1
201 <- region start
204 loop_2
208 <- region end
211 loop_0 does not exist in the region -> invalid
212 loop_1 exists, but is not completely contained in the region -> depth 0
213 loop_2 is completely contained -> depth 1 */
215 static inline unsigned int
216 sese_loop_depth (sese_l &region, loop_p loop)
218 unsigned int depth = 0;
220 while (loop_in_sese_p (loop, region))
222 depth++;
223 loop = loop_outer (loop);
226 return depth;
229 /* A single entry single exit specialized for conditions. */
231 typedef struct ifsese_s {
232 sese_info_p region;
233 sese_info_p true_region;
234 sese_info_p false_region;
235 } *ifsese;
237 extern void if_region_set_false_region (ifsese, sese_info_p);
238 extern ifsese move_sese_in_condition (sese_info_p);
239 extern void set_ifsese_condition (ifsese, tree);
240 extern edge get_true_edge_from_guard_bb (basic_block);
241 extern edge get_false_edge_from_guard_bb (basic_block);
243 static inline edge
244 if_region_entry (ifsese if_region)
246 return if_region->region->region.entry;
249 static inline edge
250 if_region_exit (ifsese if_region)
252 return if_region->region->region.exit;
255 static inline basic_block
256 if_region_get_condition_block (ifsese if_region)
258 return if_region_entry (if_region)->dest;
261 /* Free and compute again all the dominators information. */
263 static inline void
264 recompute_all_dominators (void)
266 mark_irreducible_loops ();
267 free_dominance_info (CDI_DOMINATORS);
268 calculate_dominance_info (CDI_DOMINATORS);
270 free_dominance_info (CDI_POST_DOMINATORS);
271 calculate_dominance_info (CDI_POST_DOMINATORS);
274 typedef std::pair <gimple *, tree> scalar_use;
276 typedef struct gimple_poly_bb
278 basic_block bb;
279 struct poly_bb *pbb;
281 /* Lists containing the restrictions of the conditional statements
282 dominating this bb. This bb can only be executed, if all conditions
283 are true.
285 Example:
287 for (i = 0; i <= 20; i++)
291 if (2i <= 8)
295 So for B there is an additional condition (2i <= 8).
297 List of COND_EXPR and SWITCH_EXPR. A COND_EXPR is true only if the
298 corresponding element in CONDITION_CASES is not NULL_TREE. For a
299 SWITCH_EXPR the corresponding element in CONDITION_CASES is a
300 CASE_LABEL_EXPR. */
301 vec<gimple *> conditions;
302 vec<gimple *> condition_cases;
303 vec<data_reference_p> data_refs;
304 vec<scalar_use> read_scalar_refs;
305 vec<tree> write_scalar_refs;
306 } *gimple_poly_bb_p;
308 #define GBB_BB(GBB) (GBB)->bb
309 #define GBB_PBB(GBB) (GBB)->pbb
310 #define GBB_DATA_REFS(GBB) (GBB)->data_refs
311 #define GBB_CONDITIONS(GBB) (GBB)->conditions
312 #define GBB_CONDITION_CASES(GBB) (GBB)->condition_cases
314 /* Return the innermost loop that contains the basic block GBB. */
316 static inline struct loop *
317 gbb_loop (gimple_poly_bb_p gbb)
319 return GBB_BB (gbb)->loop_father;
322 /* Returns the gimple loop, that corresponds to the loop_iterator_INDEX.
323 If there is no corresponding gimple loop, we return NULL. */
325 static inline loop_p
326 gbb_loop_at_index (gimple_poly_bb_p gbb, sese_l &region, int index)
328 loop_p loop = gbb_loop (gbb);
329 int depth = sese_loop_depth (region, loop);
331 while (--depth > index)
332 loop = loop_outer (loop);
334 gcc_assert (loop_in_sese_p (loop, region));
336 return loop;
339 /* The number of common loops in REGION for GBB1 and GBB2. */
341 static inline int
342 nb_common_loops (sese_l &region, gimple_poly_bb_p gbb1, gimple_poly_bb_p gbb2)
344 loop_p l1 = gbb_loop (gbb1);
345 loop_p l2 = gbb_loop (gbb2);
346 loop_p common = find_common_loop (l1, l2);
348 return sese_loop_depth (region, common);
351 #endif