tree-ssa-live.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* Routines for liveness in SSA trees.
  2. Copyright (C) 2003-2018 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. } *var_map;
  48. /* Value used to represent no partition number. */
  49. #define NO_PARTITION -1
  50. extern var_map init_var_map (int);
  51. extern void delete_var_map (var_map);
  52. extern int var_union (var_map, tree, tree);
  53. extern void partition_view_normal (var_map);
  54. extern void partition_view_bitmap (var_map, bitmap);
  55. extern void dump_scope_blocks (FILE *, dump_flags_t);
  56. extern void debug_scope_block (tree, dump_flags_t);
  57. extern void debug_scope_blocks (dump_flags_t);
  58. extern void remove_unused_locals (void);
  59. extern void dump_var_map (FILE *, var_map);
  60. extern void debug (_var_map &ref);
  61. extern void debug (_var_map *ptr);
  62. /* Return number of partitions in MAP. */
  63. static inline unsigned
  64. num_var_partitions (var_map map)
  65. {
  66. return map->num_partitions;
  67. }
  68. /* Given partition index I from MAP, return the variable which represents that
  69. partition. */
  70. static inline tree
  71. partition_to_var (var_map map, int i)
  72. {
  73. tree name;
  74. if (map->view_to_partition)
  75. i = map->view_to_partition[i];
  76. i = partition_find (map->var_partition, i);
  77. name = ssa_name (i);
  78. return name;
  79. }
  80. /* Given ssa_name VERSION, if it has a partition in MAP, return the var it
  81. is associated with. Otherwise return NULL. */
  82. static inline tree
  83. version_to_var (var_map map, int version)
  84. {
  85. int part;
  86. part = partition_find (map->var_partition, version);
  87. if (map->partition_to_view)
  88. part = map->partition_to_view[part];
  89. if (part == NO_PARTITION)
  90. return NULL_TREE;
  91. return partition_to_var (map, part);
  92. }
  93. /* Given VAR, return the partition number in MAP which contains it.
  94. NO_PARTITION is returned if it's not in any partition. */
  95. static inline int
  96. var_to_partition (var_map map, tree var)
  97. {
  98. int part;
  99. part = partition_find (map->var_partition, SSA_NAME_VERSION (var));
  100. if (map->partition_to_view)
  101. part = map->partition_to_view[part];
  102. return part;
  103. }
  104. /* Given VAR, return the variable which represents the entire partition
  105. it is a member of in MAP. NULL is returned if it is not in a partition. */
  106. static inline tree
  107. var_to_partition_to_var (var_map map, tree var)
  108. {
  109. int part;
  110. part = var_to_partition (map, var);
  111. if (part == NO_PARTITION)
  112. return NULL_TREE;
  113. return partition_to_var (map, part);
  114. }
  115. /* Return the index into the basevar table for PARTITION's base in MAP. */
  116. static inline int
  117. basevar_index (var_map map, int partition)
  118. {
  119. gcc_checking_assert (partition >= 0
  120. && partition <= (int) num_var_partitions (map));
  121. return map->partition_to_base_index[partition];
  122. }
  123. /* Return the number of different base variables in MAP. */
  124. static inline int
  125. num_basevars (var_map map)
  126. {
  127. return map->num_basevars;
  128. }
  129. /* ---------------- live on entry/exit info ------------------------------
  130. This structure is used to represent live range information on SSA based
  131. trees. A partition map must be provided, and based on the active partitions,
  132. live-on-entry information and live-on-exit information can be calculated.
  133. As well, partitions are marked as to whether they are global (live
  134. outside the basic block they are defined in).
  135. The live-on-entry information is per block. It provide a bitmap for
  136. each block which has a bit set for each partition that is live on entry to
  137. that block.
  138. The live-on-exit information is per block. It provides a bitmap for each
  139. block indicating which partitions are live on exit from the block.
  140. For the purposes of this implementation, we treat the elements of a PHI
  141. as follows:
  142. Uses in a PHI are considered LIVE-ON-EXIT to the block from which they
  143. originate. They are *NOT* considered live on entry to the block
  144. containing the PHI node.
  145. The Def of a PHI node is *not* considered live on entry to the block.
  146. It is considered to be "define early" in the block. Picture it as each
  147. block having a stmt (or block-preheader) before the first real stmt in
  148. the block which defines all the variables that are defined by PHIs.
  149. ----------------------------------------------------------------------- */
  150. typedef struct tree_live_info_d
  151. {
  152. /* Var map this relates to. */
  153. var_map map;
  154. /* Bitmap indicating which partitions are global. */
  155. bitmap global;
  156. /* Bitmaps of live on entry blocks for partition elements. */
  157. bitmap_head *livein;
  158. /* Bitmaps of what variables are live on exit for a basic blocks. */
  159. bitmap_head *liveout;
  160. /* Number of basic blocks when live on exit calculated. */
  161. int num_blocks;
  162. /* Vector used when creating live ranges as a visited stack. */
  163. int *work_stack;
  164. /* Top of workstack. */
  165. int *stack_top;
  166. /* Obstacks to allocate the bitmaps on. */
  167. bitmap_obstack livein_obstack;
  168. bitmap_obstack liveout_obstack;
  169. } *tree_live_info_p;
  170. #define LIVEDUMP_ENTRY 0x01
  171. #define LIVEDUMP_EXIT 0x02
  172. #define LIVEDUMP_ALL (LIVEDUMP_ENTRY | LIVEDUMP_EXIT)
  173. extern void delete_tree_live_info (tree_live_info_p);
  174. extern tree_live_info_p calculate_live_ranges (var_map, bool);
  175. extern void debug (tree_live_info_d &ref);
  176. extern void debug (tree_live_info_d *ptr);
  177. extern void dump_live_info (FILE *, tree_live_info_p, int);
  178. /* Return TRUE if P is marked as a global in LIVE. */
  179. static inline int
  180. partition_is_global (tree_live_info_p live, int p)
  181. {
  182. gcc_checking_assert (live->global);
  183. return bitmap_bit_p (live->global, p);
  184. }
  185. /* Return the bitmap from LIVE representing the live on entry blocks for
  186. partition P. */
  187. static inline bitmap
  188. live_on_entry (tree_live_info_p live, basic_block bb)
  189. {
  190. gcc_checking_assert (live->livein
  191. && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  192. && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
  193. return &live->livein[bb->index];
  194. }
  195. /* Return the bitmap from LIVE representing the live on exit partitions from
  196. block BB. */
  197. static inline bitmap
  198. live_on_exit (tree_live_info_p live, basic_block bb)
  199. {
  200. gcc_checking_assert (live->liveout
  201. && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  202. && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
  203. return &live->liveout[bb->index];
  204. }
  205. /* Return the partition map which the information in LIVE utilizes. */
  206. static inline var_map
  207. live_var_map (tree_live_info_p live)
  208. {
  209. return live->map;
  210. }
  211. /* Merge the live on entry information in LIVE for partitions P1 and P2. Place
  212. the result into P1. Clear P2. */
  213. static inline void
  214. live_merge_and_clear (tree_live_info_p live, int p1, int p2)
  215. {
  216. gcc_checking_assert (&live->livein[p1] && &live->livein[p2]);
  217. bitmap_ior_into (&live->livein[p1], &live->livein[p2]);
  218. bitmap_clear (&live->livein[p2]);
  219. }
  220. /* Mark partition P as live on entry to basic block BB in LIVE. */
  221. static inline void
  222. make_live_on_entry (tree_live_info_p live, basic_block bb , int p)
  223. {
  224. bitmap_set_bit (&live->livein[bb->index], p);
  225. bitmap_set_bit (live->global, p);
  226. }
  227. #endif /* _TREE_SSA_LIVE_H */