2007-02-28 Eric Christopher <echristo@apple.com>
[official-gcc.git] / gcc / tree-ssa-live.h
blobd0aeedf08df8c88199c227817527d789239c468c
1 /* Routines for liveness in SSA trees.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #ifndef _TREE_SSA_LIVE_H
24 #define _TREE_SSA_LIVE_H 1
26 #include "partition.h"
27 #include "vecprim.h"
31 /* Used to create the variable mapping when we go out of SSA form.
33 Mapping from an ssa_name to a partition number is maintained, as well as
34 partition number to back to ssa_name. A partition can also be represented
35 by a non-ssa_name variable. This allows ssa_names and their partition to
36 be coalesced with live on entry compiler variables, as well as eventually
37 having real compiler variables assigned to each partition as part of the
38 final stage of going of of ssa.
40 Non-ssa_names maintain their partition index in the variable annotation.
42 This data structure also supports "views", which work on a subset of all
43 partitions. This allows the coalescer to decide what partitions are
44 interesting to it, and only work with those partitions. Whenever the view
45 is changed, the partition numbers change, but none of the partition groupings
46 change. (ie, it is truly a view since it doesn't change anything)
48 The final component of the data structure is the basevar map. This provides
49 a list of all the different base variables which occur in a partition view,
50 and a unique index for each one. Routines are provided to quickly produce
51 the base variable of a partition.
53 Note that members of a partition MUST all have the same base variable. */
55 typedef struct _var_map
57 /* The partition manager of all variables. */
58 partition var_partition;
60 /* Vector for managing partitions views. */
61 int *partition_to_view;
62 int *view_to_partition;
64 /* Mapping of partition numbers to variables. */
65 tree *partition_to_var;
67 /* Current number of partitions in var_map based on the current view. */
68 unsigned int num_partitions;
70 /* Original full partition size. */
71 unsigned int partition_size;
73 /* Number of base variables in the base var list. */
74 int num_basevars;
76 /* Map of partitions numbers to base variable table indexes. */
77 int *partition_to_base_index;
79 /* Table of base variable's. */
80 VEC (tree, heap) *basevars;
81 } *var_map;
84 /* Partition number of a non ssa-name variable. */
85 #define VAR_ANN_PARTITION(ann) (ann->partition)
86 /* Index iot the basevar table of a non ssa-name variable. */
87 #define VAR_ANN_BASE_INDEX(ann) (ann->base_index)
90 /* Value used to represent no partition number. */
91 #define NO_PARTITION -1
93 extern var_map init_var_map (int);
94 extern void delete_var_map (var_map);
95 extern void dump_var_map (FILE *, var_map);
96 extern int var_union (var_map, tree, tree);
97 extern void change_partition_var (var_map, tree, int);
98 extern void partition_view_normal (var_map, bool);
99 extern void partition_view_bitmap (var_map, bitmap, bool);
100 #ifdef ENABLE_CHECKING
101 extern void register_ssa_partition_check (tree ssa_var);
102 #endif
105 /* Return number of partitions in MAP. */
107 static inline unsigned
108 num_var_partitions (var_map map)
110 return map->num_partitions;
114 /* Given partition index I from MAP, return the variable which represents that
115 partition. */
117 static inline tree
118 partition_to_var (var_map map, int i)
120 if (map->view_to_partition)
121 i = map->view_to_partition[i];
122 i = partition_find (map->var_partition, i);
123 return map->partition_to_var[i];
127 /* Given ssa_name VERSION, if it has a partition in MAP, return the var it
128 is associated with. Otherwise return NULL. */
130 static inline tree
131 version_to_var (var_map map, int version)
133 int part;
134 part = partition_find (map->var_partition, version);
135 if (map->partition_to_view)
136 part = map->partition_to_view[part];
137 if (part == NO_PARTITION)
138 return NULL_TREE;
140 return partition_to_var (map, part);
144 /* Given VAR, return the partition number in MAP which contains it.
145 NO_PARTITION is returned if it's not in any partition. */
147 static inline int
148 var_to_partition (var_map map, tree var)
150 var_ann_t ann;
151 int part;
153 if (TREE_CODE (var) == SSA_NAME)
155 part = partition_find (map->var_partition, SSA_NAME_VERSION (var));
156 if (map->partition_to_view)
157 part = map->partition_to_view[part];
159 else
161 ann = var_ann (var);
162 if (ann && ann->out_of_ssa_tag)
163 part = VAR_ANN_PARTITION (ann);
164 else
165 part = NO_PARTITION;
167 return part;
171 /* Given VAR, return the variable which represents the entire partition
172 it is a member of in MAP. NULL is returned if it is not in a partition. */
174 static inline tree
175 var_to_partition_to_var (var_map map, tree var)
177 int part;
179 part = var_to_partition (map, var);
180 if (part == NO_PARTITION)
181 return NULL_TREE;
182 return partition_to_var (map, part);
186 /* Return the index into the basevar table for PARTITION's base in MAP. */
188 static inline int
189 basevar_index (var_map map, int partition)
191 gcc_assert (partition >= 0
192 && partition <= (int) num_var_partitions (map));
193 return map->partition_to_base_index[partition];
197 /* Return the number of different base variables in MAP. */
199 static inline int
200 num_basevars (var_map map)
202 return map->num_basevars;
207 /* This routine registers a partition for SSA_VAR with MAP. Any unregistered
208 partitions may be filtered out by a view later. */
210 static inline void
211 register_ssa_partition (var_map map, tree ssa_var)
213 int version;
215 #if defined ENABLE_CHECKING
216 register_ssa_partition_check (ssa_var);
217 #endif
219 version = SSA_NAME_VERSION (ssa_var);
220 if (map->partition_to_var[version] == NULL_TREE)
221 map->partition_to_var[version] = ssa_var;
225 /* ---------------- live on entry/exit info ------------------------------
227 This structure is used to represent live range information on SSA based
228 trees. A partition map must be provided, and based on the active partitions,
229 live-on-entry information and live-on-exit information can be calculated.
230 As well, partitions are marked as to whether they are global (live
231 outside the basic block they are defined in).
233 The live-on-entry information is per block. It provide a bitmap for
234 each block which has a bit set for each partition that is live on entry to
235 that block.
237 The live-on-exit information is per block. It provides a bitmap for each
238 block indicating which partitions are live on exit from the block.
240 For the purposes of this implementation, we treat the elements of a PHI
241 as follows:
243 Uses in a PHI are considered LIVE-ON-EXIT to the block from which they
244 originate. They are *NOT* considered live on entry to the block
245 containing the PHI node.
247 The Def of a PHI node is *not* considered live on entry to the block.
248 It is considered to be "define early" in the block. Picture it as each
249 block having a stmt (or block-preheader) before the first real stmt in
250 the block which defines all the variables that are defined by PHIs.
252 ----------------------------------------------------------------------- */
255 typedef struct tree_live_info_d
257 /* Var map this relates to. */
258 var_map map;
260 /* Bitmap indicating which partitions are global. */
261 bitmap global;
263 /* Bitmap of live on entry blocks for partition elements. */
264 bitmap *livein;
266 /* Number of basic blocks when live on exit calculated. */
267 int num_blocks;
269 /* Vector used when creating live ranges as a visited stack. */
270 int *work_stack;
272 /* Top of workstack. */
273 int *stack_top;
275 /* Bitmap of what variables are live on exit for a basic blocks. */
276 bitmap *liveout;
277 } *tree_live_info_p;
280 extern tree_live_info_p calculate_live_ranges (var_map);
281 extern void calculate_live_on_exit (tree_live_info_p);
282 extern void delete_tree_live_info (tree_live_info_p);
284 #define LIVEDUMP_ENTRY 0x01
285 #define LIVEDUMP_EXIT 0x02
286 #define LIVEDUMP_ALL (LIVEDUMP_ENTRY | LIVEDUMP_EXIT)
287 extern void dump_live_info (FILE *, tree_live_info_p, int);
290 /* Return TRUE if P is marked as a global in LIVE. */
292 static inline int
293 partition_is_global (tree_live_info_p live, int p)
295 gcc_assert (live->global);
296 return bitmap_bit_p (live->global, p);
300 /* Return the bitmap from LIVE representing the live on entry blocks for
301 partition P. */
303 static inline bitmap
304 live_on_entry (tree_live_info_p live, basic_block bb)
306 gcc_assert (live->livein);
307 gcc_assert (bb != ENTRY_BLOCK_PTR);
308 gcc_assert (bb != EXIT_BLOCK_PTR);
310 return live->livein[bb->index];
314 /* Return the bitmap from LIVE representing the live on exit partitions from
315 block BB. */
317 static inline bitmap
318 live_on_exit (tree_live_info_p live, basic_block bb)
320 gcc_assert (live->liveout);
321 gcc_assert (bb != ENTRY_BLOCK_PTR);
322 gcc_assert (bb != EXIT_BLOCK_PTR);
324 return live->liveout[bb->index];
328 /* Return the partition map which the information in LIVE utilizes. */
330 static inline var_map
331 live_var_map (tree_live_info_p live)
333 return live->map;
337 /* Merge the live on entry information in LIVE for partitions P1 and P2. Place
338 the result into P1. Clear P2. */
340 static inline void
341 live_merge_and_clear (tree_live_info_p live, int p1, int p2)
343 gcc_assert (live->livein[p1]);
344 gcc_assert (live->livein[p2]);
345 bitmap_ior_into (live->livein[p1], live->livein[p2]);
346 bitmap_zero (live->livein[p2]);
350 /* Mark partition P as live on entry to basic block BB in LIVE. */
352 static inline void
353 make_live_on_entry (tree_live_info_p live, basic_block bb , int p)
355 bitmap_set_bit (live->livein[bb->index], p);
356 bitmap_set_bit (live->global, p);
360 /* From tree-ssa-coalesce.c */
361 extern var_map coalesce_ssa_name (void);
364 /* From tree-ssa-ter.c */
365 extern tree *find_replaceable_exprs (var_map);
366 extern void dump_replaceable_exprs (FILE *, tree *);
369 #endif /* _TREE_SSA_LIVE_H */