sese.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* Single entry single exit control flow regions.
  2. Copyright (C) 2008-2019 Free Software Foundation, Inc.
  3. Contributed by Jan Sjodin <jan.sjodin@amd.com> and
  4. Sebastian Pop <sebastian.pop@amd.com>.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #ifndef GCC_SESE_H
  18. #define GCC_SESE_H
  19. typedef struct ifsese_s *ifsese;
  20. /* A Single Entry, Single Exit region is a part of the CFG delimited
  21. by two edges. */
  22. struct sese_l
  23. {
  24. sese_l (edge e, edge x) : entry (e), exit (x) {}
  25. operator bool () const { return entry && exit; }
  26. edge entry;
  27. edge exit;
  28. };
  29. void print_edge (FILE *file, const_edge e);
  30. void print_sese (FILE *file, const sese_l &s);
  31. void dump_edge (const_edge e);
  32. void dump_sese (const sese_l &);
  33. /* Get the entry of an sese S. */
  34. static inline basic_block
  35. get_entry_bb (sese_l &s)
  36. {
  37. return s.entry->dest;
  38. }
  39. /* Get the exit of an sese S. */
  40. static inline basic_block
  41. get_exit_bb (sese_l &s)
  42. {
  43. return s.exit->src;
  44. }
  45. /* Returns the index of V where ELEM can be found. -1 Otherwise. */
  46. template<typename T>
  47. int
  48. vec_find (const vec<T> &v, const T &elem)
  49. {
  50. int i;
  51. T t;
  52. FOR_EACH_VEC_ELT (v, i, t)
  53. if (elem == t)
  54. return i;
  55. return -1;
  56. }
  57. /* A helper structure for bookkeeping information about a scop in graphite. */
  58. typedef struct sese_info_t
  59. {
  60. /* The SESE region. */
  61. sese_l region;
  62. /* Liveout vars. */
  63. bitmap liveout;
  64. /* Liveout in debug stmts. */
  65. bitmap debug_liveout;
  66. /* Parameters used within the SCOP. */
  67. vec<tree> params;
  68. /* Maps an old name to a new decl. */
  69. hash_map<tree, tree> *rename_map;
  70. /* Basic blocks contained in this SESE. */
  71. vec<basic_block> bbs;
  72. /* The condition region generated for this sese. */
  73. ifsese if_region;
  74. } *sese_info_p;
  75. extern sese_info_p new_sese_info (edge, edge);
  76. extern void free_sese_info (sese_info_p);
  77. extern void sese_insert_phis_for_liveouts (sese_info_p, basic_block, edge, edge);
  78. extern struct loop *outermost_loop_in_sese (sese_l &, basic_block);
  79. extern tree scalar_evolution_in_region (const sese_l &, loop_p, tree);
  80. extern bool scev_analyzable_p (tree, sese_l &);
  81. extern bool invariant_in_sese_p_rec (tree, const sese_l &, bool *);
  82. extern void sese_build_liveouts (sese_info_p);
  83. extern bool sese_trivially_empty_bb_p (basic_block);
  84. /* The number of parameters in REGION. */
  85. static inline unsigned
  86. sese_nb_params (sese_info_p region)
  87. {
  88. return region->params.length ();
  89. }
  90. /* Checks whether BB is contained in the region delimited by ENTRY and
  91. EXIT blocks. */
  92. static inline bool
  93. bb_in_region (const_basic_block bb, const_basic_block entry, const_basic_block exit)
  94. {
  95. return dominated_by_p (CDI_DOMINATORS, bb, entry)
  96. && !(dominated_by_p (CDI_DOMINATORS, bb, exit)
  97. && !dominated_by_p (CDI_DOMINATORS, entry, exit));
  98. }
  99. /* Checks whether BB is contained in the region delimited by ENTRY and
  100. EXIT blocks. */
  101. static inline bool
  102. bb_in_sese_p (basic_block bb, const sese_l &r)
  103. {
  104. return bb_in_region (bb, r.entry->dest, r.exit->dest);
  105. }
  106. /* Returns true when STMT is defined in REGION. */
  107. static inline bool
  108. stmt_in_sese_p (gimple *stmt, const sese_l &r)
  109. {
  110. basic_block bb = gimple_bb (stmt);
  111. return bb && bb_in_sese_p (bb, r);
  112. }
  113. /* Returns true when NAME is defined in REGION. */
  114. static inline bool
  115. defined_in_sese_p (tree name, const sese_l &r)
  116. {
  117. return stmt_in_sese_p (SSA_NAME_DEF_STMT (name), r);
  118. }
  119. /* Returns true when LOOP is in REGION. */
  120. static inline bool
  121. loop_in_sese_p (struct loop *loop, const sese_l &region)
  122. {
  123. return (bb_in_sese_p (loop->header, region)
  124. && bb_in_sese_p (loop->latch, region));
  125. }
  126. /* Returns the loop depth of LOOP in REGION. The loop depth
  127. is the same as the normal loop depth, but limited by a region.
  128. Example:
  129. loop_0
  130. loop_1
  131. {
  132. S0
  133. <- region start
  134. S1
  135. loop_2
  136. S2
  137. S3
  138. <- region end
  139. }
  140. loop_0 does not exist in the region -> invalid
  141. loop_1 exists, but is not completely contained in the region -> depth 0
  142. loop_2 is completely contained -> depth 1 */
  143. static inline unsigned int
  144. sese_loop_depth (const sese_l &region, loop_p loop)
  145. {
  146. unsigned int depth = 0;
  147. while (loop_in_sese_p (loop, region))
  148. {
  149. depth++;
  150. loop = loop_outer (loop);
  151. }
  152. return depth;
  153. }
  154. /* A single entry single exit specialized for conditions. */
  155. typedef struct ifsese_s {
  156. sese_info_p region;
  157. sese_info_p true_region;
  158. sese_info_p false_region;
  159. } *ifsese;
  160. extern ifsese move_sese_in_condition (sese_info_p);
  161. extern void set_ifsese_condition (ifsese, tree);
  162. extern edge get_true_edge_from_guard_bb (basic_block);
  163. extern edge get_false_edge_from_guard_bb (basic_block);
  164. static inline edge
  165. if_region_entry (ifsese if_region)
  166. {
  167. return if_region->region->region.entry;
  168. }
  169. static inline edge
  170. if_region_exit (ifsese if_region)
  171. {
  172. return if_region->region->region.exit;
  173. }
  174. static inline basic_block
  175. if_region_get_condition_block (ifsese if_region)
  176. {
  177. return if_region_entry (if_region)->dest;
  178. }
  179. typedef std::pair <gimple *, tree> scalar_use;
  180. typedef struct gimple_poly_bb
  181. {
  182. basic_block bb;
  183. struct poly_bb *pbb;
  184. /* Lists containing the restrictions of the conditional statements
  185. dominating this bb. This bb can only be executed, if all conditions
  186. are true.
  187. Example:
  188. for (i = 0; i <= 20; i++)
  189. {
  190. A
  191. if (2i <= 8)
  192. B
  193. }
  194. So for B there is an additional condition (2i <= 8).
  195. List of COND_EXPR and SWITCH_EXPR. A COND_EXPR is true only if the
  196. corresponding element in CONDITION_CASES is not NULL_TREE. For a
  197. SWITCH_EXPR the corresponding element in CONDITION_CASES is a
  198. CASE_LABEL_EXPR. */
  199. vec<gimple *> conditions;
  200. vec<gimple *> condition_cases;
  201. vec<data_reference_p> data_refs;
  202. vec<scalar_use> read_scalar_refs;
  203. vec<tree> write_scalar_refs;
  204. } *gimple_poly_bb_p;
  205. #define GBB_BB(GBB) (GBB)->bb
  206. #define GBB_PBB(GBB) (GBB)->pbb
  207. #define GBB_DATA_REFS(GBB) (GBB)->data_refs
  208. #define GBB_CONDITIONS(GBB) (GBB)->conditions
  209. #define GBB_CONDITION_CASES(GBB) (GBB)->condition_cases
  210. /* Return the innermost loop that contains the basic block GBB. */
  211. static inline struct loop *
  212. gbb_loop (gimple_poly_bb_p gbb)
  213. {
  214. return GBB_BB (gbb)->loop_father;
  215. }
  216. /* Returns the gimple loop, that corresponds to the loop_iterator_INDEX.
  217. If there is no corresponding gimple loop, we return NULL. */
  218. static inline loop_p
  219. gbb_loop_at_index (gimple_poly_bb_p gbb, sese_l &region, int index)
  220. {
  221. loop_p loop = gbb_loop (gbb);
  222. int depth = sese_loop_depth (region, loop);
  223. while (--depth > index)
  224. loop = loop_outer (loop);
  225. gcc_assert (loop_in_sese_p (loop, region));
  226. return loop;
  227. }
  228. /* The number of common loops in REGION for GBB1 and GBB2. */
  229. static inline int
  230. nb_common_loops (sese_l &region, gimple_poly_bb_p gbb1, gimple_poly_bb_p gbb2)
  231. {
  232. loop_p l1 = gbb_loop (gbb1);
  233. loop_p l2 = gbb_loop (gbb2);
  234. loop_p common = find_common_loop (l1, l2);
  235. return sese_loop_depth (region, common);
  236. }
  237. #endif