tree-ssa-live.h 9.3 KB

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