sese.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Single entry single exit control flow regions.
  2. Copyright (C) 2008-2015 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. /* A Single Entry, Single Exit region is a part of the CFG delimited
  20. by two edges. */
  21. typedef struct sese_s
  22. {
  23. /* Single ENTRY and single EXIT from the SESE region. */
  24. edge entry, exit;
  25. /* Parameters used within the SCOP. */
  26. vec<tree> params;
  27. /* Loops completely contained in the SCOP. */
  28. bitmap loops;
  29. vec<loop_p> loop_nest;
  30. /* Are we allowed to add more params? This is for debugging purpose. We
  31. can only add new params before generating the bb domains, otherwise they
  32. become invalid. */
  33. bool add_params;
  34. } *sese;
  35. #define SESE_ENTRY(S) (S->entry)
  36. #define SESE_ENTRY_BB(S) (S->entry->dest)
  37. #define SESE_EXIT(S) (S->exit)
  38. #define SESE_EXIT_BB(S) (S->exit->dest)
  39. #define SESE_PARAMS(S) (S->params)
  40. #define SESE_LOOPS(S) (S->loops)
  41. #define SESE_LOOP_NEST(S) (S->loop_nest)
  42. #define SESE_ADD_PARAMS(S) (S->add_params)
  43. extern sese new_sese (edge, edge);
  44. extern void free_sese (sese);
  45. extern void sese_insert_phis_for_liveouts (sese, basic_block, edge, edge);
  46. extern void build_sese_loop_nests (sese);
  47. extern edge copy_bb_and_scalar_dependences (basic_block, sese, edge,
  48. vec<tree> , bool *);
  49. extern struct loop *outermost_loop_in_sese (sese, basic_block);
  50. extern tree scalar_evolution_in_region (sese, loop_p, tree);
  51. /* Check that SESE contains LOOP. */
  52. static inline bool
  53. sese_contains_loop (sese sese, struct loop *loop)
  54. {
  55. return bitmap_bit_p (SESE_LOOPS (sese), loop->num);
  56. }
  57. /* The number of parameters in REGION. */
  58. static inline unsigned
  59. sese_nb_params (sese region)
  60. {
  61. return SESE_PARAMS (region).length ();
  62. }
  63. /* Checks whether BB is contained in the region delimited by ENTRY and
  64. EXIT blocks. */
  65. static inline bool
  66. bb_in_region (basic_block bb, basic_block entry, basic_block exit)
  67. {
  68. #ifdef ENABLE_CHECKING
  69. {
  70. edge e;
  71. edge_iterator ei;
  72. /* Check that there are no edges coming in the region: all the
  73. predecessors of EXIT are dominated by ENTRY. */
  74. FOR_EACH_EDGE (e, ei, exit->preds)
  75. dominated_by_p (CDI_DOMINATORS, e->src, entry);
  76. }
  77. #endif
  78. return dominated_by_p (CDI_DOMINATORS, bb, entry)
  79. && !(dominated_by_p (CDI_DOMINATORS, bb, exit)
  80. && !dominated_by_p (CDI_DOMINATORS, entry, exit));
  81. }
  82. /* Checks whether BB is contained in the region delimited by ENTRY and
  83. EXIT blocks. */
  84. static inline bool
  85. bb_in_sese_p (basic_block bb, sese region)
  86. {
  87. basic_block entry = SESE_ENTRY_BB (region);
  88. basic_block exit = SESE_EXIT_BB (region);
  89. return bb_in_region (bb, entry, exit);
  90. }
  91. /* Returns true when STMT is defined in REGION. */
  92. static inline bool
  93. stmt_in_sese_p (gimple stmt, sese region)
  94. {
  95. basic_block bb = gimple_bb (stmt);
  96. return bb && bb_in_sese_p (bb, region);
  97. }
  98. /* Returns true when NAME is defined in REGION. */
  99. static inline bool
  100. defined_in_sese_p (tree name, sese region)
  101. {
  102. gimple stmt = SSA_NAME_DEF_STMT (name);
  103. return stmt_in_sese_p (stmt, region);
  104. }
  105. /* Returns true when LOOP is in REGION. */
  106. static inline bool
  107. loop_in_sese_p (struct loop *loop, sese region)
  108. {
  109. return (bb_in_sese_p (loop->header, region)
  110. && bb_in_sese_p (loop->latch, region));
  111. }
  112. /* Returns the loop depth of LOOP in REGION. The loop depth
  113. is the same as the normal loop depth, but limited by a region.
  114. Example:
  115. loop_0
  116. loop_1
  117. {
  118. S0
  119. <- region start
  120. S1
  121. loop_2
  122. S2
  123. S3
  124. <- region end
  125. }
  126. loop_0 does not exist in the region -> invalid
  127. loop_1 exists, but is not completely contained in the region -> depth 0
  128. loop_2 is completely contained -> depth 1 */
  129. static inline unsigned int
  130. sese_loop_depth (sese region, loop_p loop)
  131. {
  132. unsigned int depth = 0;
  133. gcc_assert ((!loop_in_sese_p (loop, region)
  134. && (SESE_ENTRY_BB (region)->loop_father == loop
  135. || SESE_EXIT (region)->src->loop_father == loop))
  136. || loop_in_sese_p (loop, region));
  137. while (loop_in_sese_p (loop, region))
  138. {
  139. depth++;
  140. loop = loop_outer (loop);
  141. }
  142. return depth;
  143. }
  144. /* Splits BB to make a single entry single exit region. */
  145. static inline sese
  146. split_region_for_bb (basic_block bb)
  147. {
  148. edge entry, exit;
  149. if (single_pred_p (bb))
  150. entry = single_pred_edge (bb);
  151. else
  152. {
  153. entry = split_block_after_labels (bb);
  154. bb = single_succ (bb);
  155. }
  156. if (single_succ_p (bb))
  157. exit = single_succ_edge (bb);
  158. else
  159. {
  160. gimple_stmt_iterator gsi = gsi_last_bb (bb);
  161. gsi_prev (&gsi);
  162. exit = split_block (bb, gsi_stmt (gsi));
  163. }
  164. return new_sese (entry, exit);
  165. }
  166. /* Returns the block preceding the entry of a SESE. */
  167. static inline basic_block
  168. block_before_sese (sese sese)
  169. {
  170. return SESE_ENTRY (sese)->src;
  171. }
  172. /* A single entry single exit specialized for conditions. */
  173. typedef struct ifsese_s {
  174. sese region;
  175. sese true_region;
  176. sese false_region;
  177. } *ifsese;
  178. extern void if_region_set_false_region (ifsese, sese);
  179. extern ifsese move_sese_in_condition (sese);
  180. extern edge get_true_edge_from_guard_bb (basic_block);
  181. extern edge get_false_edge_from_guard_bb (basic_block);
  182. extern void set_ifsese_condition (ifsese, tree);
  183. static inline edge
  184. if_region_entry (ifsese if_region)
  185. {
  186. return SESE_ENTRY (if_region->region);
  187. }
  188. static inline edge
  189. if_region_exit (ifsese if_region)
  190. {
  191. return SESE_EXIT (if_region->region);
  192. }
  193. static inline basic_block
  194. if_region_get_condition_block (ifsese if_region)
  195. {
  196. return if_region_entry (if_region)->dest;
  197. }
  198. /* Free and compute again all the dominators information. */
  199. static inline void
  200. recompute_all_dominators (void)
  201. {
  202. mark_irreducible_loops ();
  203. free_dominance_info (CDI_DOMINATORS);
  204. calculate_dominance_info (CDI_DOMINATORS);
  205. }
  206. typedef struct gimple_bb
  207. {
  208. basic_block bb;
  209. struct poly_bb *pbb;
  210. /* Lists containing the restrictions of the conditional statements
  211. dominating this bb. This bb can only be executed, if all conditions
  212. are true.
  213. Example:
  214. for (i = 0; i <= 20; i++)
  215. {
  216. A
  217. if (2i <= 8)
  218. B
  219. }
  220. So for B there is an additional condition (2i <= 8).
  221. List of COND_EXPR and SWITCH_EXPR. A COND_EXPR is true only if the
  222. corresponding element in CONDITION_CASES is not NULL_TREE. For a
  223. SWITCH_EXPR the corresponding element in CONDITION_CASES is a
  224. CASE_LABEL_EXPR. */
  225. vec<gimple> conditions;
  226. vec<gimple> condition_cases;
  227. vec<data_reference_p> data_refs;
  228. } *gimple_bb_p;
  229. #define GBB_BB(GBB) (GBB)->bb
  230. #define GBB_PBB(GBB) (GBB)->pbb
  231. #define GBB_DATA_REFS(GBB) (GBB)->data_refs
  232. #define GBB_CONDITIONS(GBB) (GBB)->conditions
  233. #define GBB_CONDITION_CASES(GBB) (GBB)->condition_cases
  234. /* Return the innermost loop that contains the basic block GBB. */
  235. static inline struct loop *
  236. gbb_loop (struct gimple_bb *gbb)
  237. {
  238. return GBB_BB (gbb)->loop_father;
  239. }
  240. /* Returns the gimple loop, that corresponds to the loop_iterator_INDEX.
  241. If there is no corresponding gimple loop, we return NULL. */
  242. static inline loop_p
  243. gbb_loop_at_index (gimple_bb_p gbb, sese region, int index)
  244. {
  245. loop_p loop = gbb_loop (gbb);
  246. int depth = sese_loop_depth (region, loop);
  247. while (--depth > index)
  248. loop = loop_outer (loop);
  249. gcc_assert (sese_contains_loop (region, loop));
  250. return loop;
  251. }
  252. /* The number of common loops in REGION for GBB1 and GBB2. */
  253. static inline int
  254. nb_common_loops (sese region, gimple_bb_p gbb1, gimple_bb_p gbb2)
  255. {
  256. loop_p l1 = gbb_loop (gbb1);
  257. loop_p l2 = gbb_loop (gbb2);
  258. loop_p common = find_common_loop (l1, l2);
  259. return sese_loop_depth (region, common);
  260. }
  261. /* Return true when DEF can be analyzed in REGION by the scalar
  262. evolution analyzer. */
  263. static inline bool
  264. scev_analyzable_p (tree def, sese region)
  265. {
  266. loop_p loop;
  267. tree scev;
  268. tree type = TREE_TYPE (def);
  269. /* When Graphite generates code for a scev, the code generator
  270. expresses the scev in function of a single induction variable.
  271. This is unsafe for floating point computations, as it may replace
  272. a floating point sum reduction with a multiplication. The
  273. following test returns false for non integer types to avoid such
  274. problems. */
  275. if (!INTEGRAL_TYPE_P (type)
  276. && !POINTER_TYPE_P (type))
  277. return false;
  278. loop = loop_containing_stmt (SSA_NAME_DEF_STMT (def));
  279. scev = scalar_evolution_in_region (region, loop, def);
  280. return !chrec_contains_undetermined (scev)
  281. && (TREE_CODE (scev) != SSA_NAME
  282. || !defined_in_sese_p (scev, region))
  283. && (tree_does_not_contain_chrecs (scev)
  284. || evolution_function_is_affine_p (scev));
  285. }
  286. #endif