#ifndef _TREE_SSA_LIVE_H
#define _TREE_SSA_LIVE_H 1
#include "partition.h"
#include "vecprim.h"
typedef struct _var_map
{
partition var_partition;
int *partition_to_compact;
int *compact_to_partition;
tree *partition_to_var;
unsigned int num_partitions;
unsigned int partition_size;
int *ref_count;
} *var_map;
#define VAR_ANN_PARTITION(ann) (ann->partition)
#define VAR_ANN_ROOT_INDEX(ann) (ann->root_index)
#define NO_PARTITION -1
#define VARMAP_NORMAL 0
#define VARMAP_NO_SINGLE_DEFS 1
extern var_map init_var_map (int);
extern void delete_var_map (var_map);
extern void dump_var_map (FILE *, var_map);
extern int var_union (var_map, tree, tree);
extern void change_partition_var (var_map, tree, int);
extern void compact_var_map (var_map, int);
#ifdef ENABLE_CHECKING
extern void register_ssa_partition_check (tree ssa_var);
#endif
static inline unsigned num_var_partitions (var_map);
static inline tree var_to_partition_to_var (var_map, tree);
static inline tree partition_to_var (var_map, int);
static inline int var_to_partition (var_map, tree);
static inline tree version_to_var (var_map, int);
static inline int version_ref_count (var_map, tree);
static inline void register_ssa_partition (var_map, tree, bool);
#define SSA_VAR_MAP_REF_COUNT 0x01
extern var_map create_ssa_var_map (int);
static inline unsigned
num_var_partitions (var_map map)
{
return map->num_partitions;
}
static inline int
version_ref_count (var_map map, tree ssa_var)
{
int version = SSA_NAME_VERSION (ssa_var);
gcc_assert (map->ref_count);
return map->ref_count[version];
}
static inline tree
partition_to_var (var_map map, int i)
{
if (map->compact_to_partition)
i = map->compact_to_partition[i];
i = partition_find (map->var_partition, i);
return map->partition_to_var[i];
}
static inline tree version_to_var (var_map map, int version)
{
int part;
part = partition_find (map->var_partition, version);
if (map->partition_to_compact)
part = map->partition_to_compact[part];
if (part == NO_PARTITION)
return NULL_TREE;
return partition_to_var (map, part);
}
static inline int
var_to_partition (var_map map, tree var)
{
var_ann_t ann;
int part;
if (TREE_CODE (var) == SSA_NAME)
{
part = partition_find (map->var_partition, SSA_NAME_VERSION (var));
if (map->partition_to_compact)
part = map->partition_to_compact[part];
}
else
{
ann = var_ann (var);
if (ann->out_of_ssa_tag)
part = VAR_ANN_PARTITION (ann);
else
part = NO_PARTITION;
}
return part;
}
static inline tree
var_to_partition_to_var (var_map map, tree var)
{
int part;
part = var_to_partition (map, var);
if (part == NO_PARTITION)
return NULL_TREE;
return partition_to_var (map, part);
}
static inline void
register_ssa_partition (var_map map, tree ssa_var, bool is_use)
{
int version;
#if defined ENABLE_CHECKING
register_ssa_partition_check (ssa_var);
#endif
version = SSA_NAME_VERSION (ssa_var);
if (is_use && map->ref_count)
map->ref_count[version]++;
if (map->partition_to_var[version] == NULL_TREE)
map->partition_to_var[SSA_NAME_VERSION (ssa_var)] = ssa_var;
}
typedef struct tree_live_info_d
{
var_map map;
bitmap global;
bitmap *livein;
int num_blocks;
bitmap *liveout;
} *tree_live_info_p;
extern tree_live_info_p calculate_live_on_entry (var_map);
extern void calculate_live_on_exit (tree_live_info_p);
extern void delete_tree_live_info (tree_live_info_p);
#define LIVEDUMP_ENTRY 0x01
#define LIVEDUMP_EXIT 0x02
#define LIVEDUMP_ALL (LIVEDUMP_ENTRY | LIVEDUMP_EXIT)
extern void dump_live_info (FILE *, tree_live_info_p, int);
static inline int partition_is_global (tree_live_info_p, int);
static inline bitmap live_entry_blocks (tree_live_info_p, int);
static inline bitmap live_on_exit (tree_live_info_p, basic_block);
static inline var_map live_var_map (tree_live_info_p);
static inline void live_merge_and_clear (tree_live_info_p, int, int);
static inline void make_live_on_entry (tree_live_info_p, basic_block, int);
static inline int
partition_is_global (tree_live_info_p live, int p)
{
gcc_assert (live->global);
return bitmap_bit_p (live->global, p);
}
static inline bitmap
live_entry_blocks (tree_live_info_p live, int p)
{
gcc_assert (live->livein);
return live->livein[p];
}
static inline bitmap
live_on_exit (tree_live_info_p live, basic_block bb)
{
gcc_assert (live->liveout);
gcc_assert (bb != ENTRY_BLOCK_PTR);
gcc_assert (bb != EXIT_BLOCK_PTR);
return live->liveout[bb->index];
}
static inline var_map
live_var_map (tree_live_info_p live)
{
return live->map;
}
static inline void
live_merge_and_clear (tree_live_info_p live, int p1, int p2)
{
bitmap_ior_into (live->livein[p1], live->livein[p2]);
bitmap_zero (live->livein[p2]);
}
static inline void
make_live_on_entry (tree_live_info_p live, basic_block bb , int p)
{
bitmap_set_bit (live->livein[p], bb->index);
bitmap_set_bit (live->global, p);
}
typedef struct tree_partition_associator_d
{
VEC(tree,heap) *trees;
VEC(int,heap) *first_partition;
int *next_partition;
int *partition_to_tree_map;
int num_trees;
int uncompressed_num;
var_map map;
} *tpa_p;
#define TPA_NONE -1
static inline tree tpa_tree (tpa_p, int);
static inline int tpa_first_partition (tpa_p, int);
static inline int tpa_next_partition (tpa_p, int);
static inline int tpa_num_trees (tpa_p);
static inline int tpa_find_tree (tpa_p, int);
static inline void tpa_decompact (tpa_p);
extern void tpa_delete (tpa_p);
extern void tpa_dump (FILE *, tpa_p);
extern void tpa_remove_partition (tpa_p, int, int);
extern int tpa_compact (tpa_p);
static inline int
tpa_num_trees (tpa_p tpa)
{
return tpa->num_trees;
}
static inline tree
tpa_tree (tpa_p tpa, int i)
{
return VEC_index (tree, tpa->trees, i);
}
static inline int
tpa_first_partition (tpa_p tpa, int i)
{
return VEC_index (int, tpa->first_partition, i);
}
static inline int
tpa_next_partition (tpa_p tpa, int i)
{
return tpa->next_partition[i];
}
static inline int
tpa_find_tree (tpa_p tpa, int i)
{
int index;
index = tpa->partition_to_tree_map[i];
if (index != TPA_NONE && index >= tpa_num_trees (tpa))
{
gcc_assert (tpa->uncompressed_num != -1);
index = TPA_NONE;
}
return index;
}
static inline void
tpa_decompact(tpa_p tpa)
{
gcc_assert (tpa->uncompressed_num != -1);
tpa->num_trees = tpa->uncompressed_num;
}
typedef tpa_p root_var_p;
static inline tree root_var (root_var_p, int);
static inline int root_var_first_partition (root_var_p, int);
static inline int root_var_next_partition (root_var_p, int);
static inline int root_var_num (root_var_p);
static inline void root_var_dump (FILE *, root_var_p);
static inline void root_var_remove_partition (root_var_p, int, int);
static inline void root_var_delete (root_var_p);
static inline int root_var_find (root_var_p, int);
static inline int root_var_compact (root_var_p);
static inline void root_var_decompact (tpa_p);
extern root_var_p root_var_init (var_map);
#define ROOT_VAR_NONE TPA_NONE
static inline int
root_var_num (root_var_p rv)
{
return tpa_num_trees (rv);
}
static inline tree
root_var (root_var_p rv, int i)
{
return tpa_tree (rv, i);
}
static inline int
root_var_first_partition (root_var_p rv, int i)
{
return tpa_first_partition (rv, i);
}
static inline int
root_var_next_partition (root_var_p rv, int i)
{
return tpa_next_partition (rv, i);
}
static inline void
root_var_dump (FILE *f, root_var_p rv)
{
fprintf (f, "\nRoot Var dump\n");
tpa_dump (f, rv);
fprintf (f, "\n");
}
static inline void
root_var_delete (root_var_p rv)
{
tpa_delete (rv);
}
static inline void
root_var_remove_partition (root_var_p rv, int root_index, int partition_index)
{
tpa_remove_partition (rv, root_index, partition_index);
}
static inline int
root_var_find (root_var_p rv, int i)
{
return tpa_find_tree (rv, i);
}
static inline int
root_var_compact (root_var_p rv)
{
return tpa_compact (rv);
}
static inline void
root_var_decompact (root_var_p rv)
{
tpa_decompact (rv);
}
typedef tpa_p type_var_p;
static inline tree type_var (type_var_p, int);
static inline int type_var_first_partition (type_var_p, int);
static inline int type_var_next_partition (type_var_p, int);
static inline int type_var_num (type_var_p);
static inline void type_var_dump (FILE *, type_var_p);
static inline void type_var_remove_partition (type_var_p, int, int);
static inline void type_var_delete (type_var_p);
static inline int type_var_find (type_var_p, int);
static inline int type_var_compact (type_var_p);
static inline void type_var_decompact (type_var_p);
extern type_var_p type_var_init (var_map);
#define TYPE_VAR_NONE TPA_NONE
static inline int
type_var_num (type_var_p tv)
{
return tpa_num_trees (tv);
}
static inline tree
type_var (type_var_p tv, int i)
{
return tpa_tree (tv, i);
}
static inline int
type_var_first_partition (type_var_p tv, int i)
{
return tpa_first_partition (tv, i);
}
static inline int
type_var_next_partition (type_var_p tv, int i)
{
return tpa_next_partition (tv, i);
}
static inline void
type_var_dump (FILE *f, type_var_p tv)
{
fprintf (f, "\nType Var dump\n");
tpa_dump (f, tv);
fprintf (f, "\n");
}
static inline void
type_var_delete (type_var_p tv)
{
tpa_delete (tv);
}
static inline void
type_var_remove_partition (type_var_p tv, int type_index, int partition_index)
{
tpa_remove_partition (tv, type_index, partition_index);
}
static inline int
type_var_find (type_var_p tv, int i)
{
return tpa_find_tree (tv, i);
}
static inline int
type_var_compact (type_var_p tv)
{
return tpa_compact (tv);
}
static inline void
type_var_decompact (type_var_p tv)
{
tpa_decompact (tv);
}
typedef struct partition_pair_d
{
int first_partition;
int second_partition;
int cost;
struct partition_pair_d *next;
} *partition_pair_p;
typedef struct coalesce_list_d
{
var_map map;
partition_pair_p *list;
bool add_mode;
} *coalesce_list_p;
extern coalesce_list_p create_coalesce_list (var_map);
extern void add_coalesce (coalesce_list_p, int, int, int);
extern int coalesce_cost (int, bool, bool);
extern void sort_coalesce_list (coalesce_list_p);
extern void dump_coalesce_list (FILE *, coalesce_list_p);
extern void delete_coalesce_list (coalesce_list_p);
#define NO_BEST_COALESCE -1
extern conflict_graph build_tree_conflict_graph (tree_live_info_p, tpa_p,
coalesce_list_p);
extern void coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
coalesce_list_p cl, FILE *);
#endif