tree-ssa-live.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Routines for liveness in SSA trees.
  2. Copyright (C) 2003-2019 Free Software Foundation, Inc.
  3. Contributed by Andrew MacLeod <amacleod@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _TREE_SSA_LIVE_H
  17. #define _TREE_SSA_LIVE_H 1
  18. #include "partition.h"
  19. /* Used to create the variable mapping when we go out of SSA form.
  20. Mapping from an ssa_name to a partition number is maintained, as well as
  21. partition number back to ssa_name.
  22. This data structure also supports "views", which work on a subset of all
  23. partitions. This allows the coalescer to decide what partitions are
  24. interesting to it, and only work with those partitions. Whenever the view
  25. is changed, the partition numbers change, but none of the partition groupings
  26. change. (ie, it is truly a view since it doesn't change anything)
  27. The final component of the data structure is the basevar map. This provides
  28. a list of all the different base variables which occur in a partition view,
  29. and a unique index for each one. Routines are provided to quickly produce
  30. the base variable of a partition.
  31. Note that members of a partition MUST all have the same base variable. */
  32. typedef struct _var_map
  33. {
  34. /* The partition manager of all variables. */
  35. partition var_partition;
  36. /* Vector for managing partitions views. */
  37. int *partition_to_view;
  38. int *view_to_partition;
  39. /* Current number of partitions in var_map based on the current view. */
  40. unsigned int num_partitions;
  41. /* Original full partition size. */
  42. unsigned int partition_size;
  43. /* Number of base variables in the base var list. */
  44. int num_basevars;
  45. /* Map of partitions numbers to base variable table indexes. */
  46. int *partition_to_base_index;
  47. /* Bitmap of basic block. It describes the region within which the analysis
  48. is done. Using pointer avoids allocating memory in out-of-ssa case. */
  49. bitmap bmp_bbs;
  50. /* Vector of basic block in the region. */
  51. vec<basic_block> vec_bbs;
  52. /* True if this map is for out-of-ssa, otherwise for live range
  53. computation. When for out-of-ssa, it also means the var map is computed
  54. for whole current function. */
  55. bool outofssa_p;
  56. } *var_map;
  57. /* Value used to represent no partition number. */
  58. #define NO_PARTITION -1
  59. extern var_map init_var_map (int, struct loop* = NULL);
  60. extern void delete_var_map (var_map);
  61. extern int var_union (var_map, tree, tree);
  62. extern void partition_view_normal (var_map);
  63. extern void partition_view_bitmap (var_map, bitmap);
  64. extern void dump_scope_blocks (FILE *, dump_flags_t);
  65. extern void debug_scope_block (tree, dump_flags_t);
  66. extern void debug_scope_blocks (dump_flags_t);
  67. extern void remove_unused_locals (void);
  68. extern void dump_var_map (FILE *, var_map);
  69. extern void debug (_var_map &ref);
  70. extern void debug (_var_map *ptr);
  71. /* Return TRUE if region of the MAP contains basic block BB. */
  72. inline bool
  73. region_contains_p (var_map map, basic_block bb)
  74. {
  75. /* It's possible that the function is called with ENTRY_BLOCK/EXIT_BLOCK. */
  76. if (map->outofssa_p)
  77. return (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK);
  78. return bitmap_bit_p (map->bmp_bbs, bb->index);
  79. }
  80. /* Return number of partitions in MAP. */
  81. static inline unsigned
  82. num_var_partitions (var_map map)
  83. {
  84. return map->num_partitions;
  85. }
  86. /* Given partition index I from MAP, return the variable which represents that
  87. partition. */
  88. static inline tree
  89. partition_to_var (var_map map, int i)
  90. {
  91. tree name;
  92. if (map->view_to_partition)
  93. i = map->view_to_partition[i];
  94. i = partition_find (map->var_partition, i);
  95. name = ssa_name (i);
  96. return name;
  97. }
  98. /* Given ssa_name VERSION, if it has a partition in MAP, return the var it
  99. is associated with. Otherwise return NULL. */
  100. static inline tree
  101. version_to_var (var_map map, int version)
  102. {
  103. int part;
  104. part = partition_find (map->var_partition, version);
  105. if (map->partition_to_view)
  106. part = map->partition_to_view[part];
  107. if (part == NO_PARTITION)
  108. return NULL_TREE;
  109. return partition_to_var (map, part);
  110. }
  111. /* Given VAR, return the partition number in MAP which contains it.
  112. NO_PARTITION is returned if it's not in any partition. */
  113. static inline int
  114. var_to_partition (var_map map, tree var)
  115. {
  116. int part;
  117. part = partition_find (map->var_partition, SSA_NAME_VERSION (var));
  118. if (map->partition_to_view)
  119. part = map->partition_to_view[part];
  120. return part;
  121. }
  122. /* Given VAR, return the variable which represents the entire partition
  123. it is a member of in MAP. NULL is returned if it is not in a partition. */
  124. static inline tree
  125. var_to_partition_to_var (var_map map, tree var)
  126. {
  127. int part;
  128. part = var_to_partition (map, var);
  129. if (part == NO_PARTITION)
  130. return NULL_TREE;
  131. return partition_to_var (map, part);
  132. }
  133. /* Return the index into the basevar table for PARTITION's base in MAP. */
  134. static inline int
  135. basevar_index (var_map map, int partition)
  136. {
  137. gcc_checking_assert (partition >= 0
  138. && partition <= (int) num_var_partitions (map));
  139. return map->partition_to_base_index[partition];
  140. }
  141. /* Return the number of different base variables in MAP. */
  142. static inline int
  143. num_basevars (var_map map)
  144. {
  145. return map->num_basevars;
  146. }
  147. /* ---------------- live on entry/exit info ------------------------------
  148. This structure is used to represent live range information on SSA based
  149. trees. A partition map must be provided, and based on the active partitions,
  150. live-on-entry information and live-on-exit information can be calculated.
  151. As well, partitions are marked as to whether they are global (live
  152. outside the basic block they are defined in).
  153. The live-on-entry information is per block. It provide a bitmap for
  154. each block which has a bit set for each partition that is live on entry to
  155. that block.
  156. The live-on-exit information is per block. It provides a bitmap for each
  157. block indicating which partitions are live on exit from the block.
  158. For the purposes of this implementation, we treat the elements of a PHI
  159. as follows:
  160. Uses in a PHI are considered LIVE-ON-EXIT to the block from which they
  161. originate. They are *NOT* considered live on entry to the block
  162. containing the PHI node.
  163. The Def of a PHI node is *not* considered live on entry to the block.
  164. It is considered to be "define early" in the block. Picture it as each
  165. block having a stmt (or block-preheader) before the first real stmt in
  166. the block which defines all the variables that are defined by PHIs.
  167. ----------------------------------------------------------------------- */
  168. typedef struct tree_live_info_d
  169. {
  170. /* Var map this relates to. */
  171. var_map map;
  172. /* Bitmap indicating which partitions are global. */
  173. bitmap global;
  174. /* Bitmaps of live on entry blocks for partition elements. */
  175. bitmap_head *livein;
  176. /* Bitmaps of what variables are live on exit for a basic blocks. */
  177. bitmap_head *liveout;
  178. /* Number of basic blocks when live on exit calculated. */
  179. int num_blocks;
  180. /* Vector used when creating live ranges as a visited stack. */
  181. int *work_stack;
  182. /* Top of workstack. */
  183. int *stack_top;
  184. /* Obstacks to allocate the bitmaps on. */
  185. bitmap_obstack livein_obstack;
  186. bitmap_obstack liveout_obstack;
  187. } *tree_live_info_p;
  188. #define LIVEDUMP_ENTRY 0x01
  189. #define LIVEDUMP_EXIT 0x02
  190. #define LIVEDUMP_ALL (LIVEDUMP_ENTRY | LIVEDUMP_EXIT)
  191. extern void delete_tree_live_info (tree_live_info_p);
  192. extern tree_live_info_p calculate_live_ranges (var_map, bool);
  193. extern void debug (tree_live_info_d &ref);
  194. extern void debug (tree_live_info_d *ptr);
  195. extern void dump_live_info (FILE *, tree_live_info_p, int);
  196. /* Return TRUE if P is marked as a global in LIVE. */
  197. static inline int
  198. partition_is_global (tree_live_info_p live, int p)
  199. {
  200. gcc_checking_assert (live->global);
  201. return bitmap_bit_p (live->global, p);
  202. }
  203. /* Return the bitmap from LIVE representing the live on entry blocks for
  204. partition P. */
  205. static inline bitmap
  206. live_on_entry (tree_live_info_p live, basic_block bb)
  207. {
  208. gcc_checking_assert (live->livein
  209. && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  210. && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
  211. return &live->livein[bb->index];
  212. }
  213. /* Return the bitmap from LIVE representing the live on exit partitions from
  214. block BB. */
  215. static inline bitmap
  216. live_on_exit (tree_live_info_p live, basic_block bb)
  217. {
  218. gcc_checking_assert (live->liveout
  219. && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  220. && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
  221. return &live->liveout[bb->index];
  222. }
  223. /* Return the partition map which the information in LIVE utilizes. */
  224. static inline var_map
  225. live_var_map (tree_live_info_p live)
  226. {
  227. return live->map;
  228. }
  229. /* Mark partition P as live on entry to basic block BB in LIVE. */
  230. static inline void
  231. make_live_on_entry (tree_live_info_p live, basic_block bb , int p)
  232. {
  233. bitmap_set_bit (&live->livein[bb->index], p);
  234. bitmap_set_bit (live->global, p);
  235. }
  236. #endif /* _TREE_SSA_LIVE_H */