Commit b482d88d authored by Matthias Braun's avatar Matthias Braun
Browse files

belive: cleanup livness assure/invalidate API

This also plugs liveness related memory leaks
parent 68a3a77f
......@@ -501,16 +501,12 @@ void be_ra_chordal_color(be_chordal_env_t *chordal_env)
{
be_chordal_alloc_env_t env;
char buf[256];
be_lv_t *lv;
const arch_register_class_t *cls = chordal_env->cls;
int colors_n = arch_register_class_n_regs(cls);
ir_graph *irg = chordal_env->irg;
lv = be_assure_liveness(irg);
be_liveness_assure_sets(lv);
be_liveness_assure_chk(lv);
int colors_n = arch_register_class_n_regs(cls);
ir_graph *irg = chordal_env->irg;
be_assure_live_sets(irg);
assure_doms(irg);
env.chordal_env = chordal_env;
......
......@@ -386,17 +386,16 @@ static void draw_block(ir_node *bl, void *data)
static void draw(draw_chordal_env_t *env, const rect_t *start_box)
{
ir_graph *irg = env->chordal_env->irg;
plotter_t *p = env->plotter;
be_lv_t *lv;
rect_t bbox;
bbox.x = bbox.y = 0;
bbox.w = start_box->w + 2 * env->opts->x_margin;
bbox.h = start_box->h + 2 * env->opts->y_margin;
lv = be_assure_liveness(env->chordal_env->irg);
be_liveness_assure_sets(lv);
be_liveness_assure_chk(lv);
be_assure_live_sets(irg);
be_assure_live_chk(irg);
p->vtab->begin(p, &bbox);
irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
......
......@@ -246,8 +246,7 @@ static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
chordal_env->border_heads = pmap_create();
chordal_env->allocatable_regs = bitset_malloc(chordal_env->cls->n_regs);
be_assure_liveness(irg);
be_liveness_assure_chk(be_get_irg_liveness(irg));
be_assure_live_chk(irg);
if (stat_ev_enabled) {
pse->pre_spill_cost = be_estimate_irg_costs(irg, exec_freq);
......@@ -379,8 +378,6 @@ static void be_ra_chordal_main(ir_graph *irg)
be_timer_push(T_RA_PROLOG);
be_assure_liveness(irg);
chordal_env.obst = &obst;
chordal_env.opts = &options;
chordal_env.irg = irg;
......@@ -451,7 +448,7 @@ static void be_ra_chordal_main(ir_graph *irg)
dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
obstack_free(&obst, NULL);
be_liveness_invalidate(be_get_irg_liveness(irg));
be_invalidate_live_sets(irg);
be_timer_pop(T_RA_EPILOG);
be_timer_pop(T_RA_OTHER);
......
......@@ -1031,7 +1031,7 @@ void co_driver(be_chordal_env_t *cenv)
if (selected_copyopt->copyopt == void_algo)
return;
be_liveness_assure_chk(be_get_irg_liveness(cenv->irg));
be_assure_live_chk(cenv->irg);
co = new_copy_opt(cenv, cost_func);
co_build_ou_structure(co);
......
......@@ -31,15 +31,6 @@
#include "belive.h"
#include "bedomfront.h"
be_lv_t *be_assure_liveness(ir_graph *irg)
{
be_irg_t *birg = be_birg_from_irg(irg);
if (birg->lv != NULL)
return birg->lv;
return birg->lv = be_liveness(birg->irg);
}
void be_assure_dom_front(ir_graph *irg)
{
be_irg_t *birg = be_birg_from_irg(irg);
......@@ -59,6 +50,30 @@ void be_invalidate_dom_front(ir_graph *irg)
birg->dom_front = NULL;
}
void be_invalidate_live_sets(ir_graph *irg)
{
be_irg_t *birg = be_birg_from_irg(irg);
be_liveness_invalidate_sets(birg->lv);
}
void be_invalidate_live_chk(ir_graph *irg)
{
be_irg_t *birg = be_birg_from_irg(irg);
be_liveness_invalidate_chk(birg->lv);
}
void be_assure_live_sets(ir_graph *irg)
{
be_irg_t *birg = be_birg_from_irg(irg);
be_liveness_compute_sets(birg->lv);
}
void be_assure_live_chk(ir_graph *irg)
{
be_irg_t *birg = be_birg_from_irg(irg);
be_liveness_compute_chk(birg->lv);
}
void be_free_birg(ir_graph *irg)
{
be_irg_t *birg = be_birg_from_irg(irg);
......
......@@ -31,11 +31,22 @@
#include "be_t.h"
#include "irtypes.h"
be_lv_t *be_assure_liveness(ir_graph *irg);
void be_assure_dom_front(ir_graph *irg);
void be_invalidate_dom_front(ir_graph *irg);
void be_assure_live_sets(ir_graph *irg);
void be_assure_live_chk(ir_graph *irg);
/**
* Liveness is invalid (call when nodes have been added but the control
* flow has not been changed)
*/
void be_invalidate_live_sets(ir_graph *irg);
/**
* Call when control flow has changed.
* be_invalidate_live_sets() is called.
*/
void be_invalidate_live_chk(ir_graph *irg);
/**
* frees all memory allocated by birg structures (liveness, dom_front, ...).
* The memory of the birg structure itself is not freed.
......
......@@ -48,12 +48,6 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
#define LV_STD_SIZE 64
/* if defined, use binary search for already live nodes, else linear */
#define LV_USE_BINARY_SEARCH
#undef LV_INTESIVE_CHECKS
void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc);
/**
* Filter out some nodes for which we never need liveness.
*
......@@ -89,8 +83,6 @@ int (be_is_live_end)(const be_lv_t *lv, const ir_node *block, const ir_node *irn
return _be_is_live_xxx(lv, block, irn, be_lv_state_end);
}
#ifdef LV_USE_BINARY_SEARCH
static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, unsigned idx)
{
be_lv_info_t *payload = arr + 1;
......@@ -120,39 +112,9 @@ static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, unsigned idx)
res = lo;
} while (lo < hi);
#ifdef LV_INTESIVE_CHECKS
{
unsigned i;
for (i = res; i < n; ++i)
assert(payload[i].node.idx >= idx);
for (i = 0; i < res; ++i)
assert(payload[i].node.idx < idx);
}
#endif
return res;
}
#else
/**
* This function searches linearly for the node in the array.
*/
static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, unsigned idx)
{
unsigned n = arr[0].head.n_members;
unsigned i;
for (i = 0; i < n; ++i) {
if (arr[i + 1].node.idx == idx)
return i;
}
return i;
}
#endif
be_lv_info_node_t *be_lv_get(const be_lv_t *li, const ir_node *bl,
const ir_node *irn)
{
......@@ -231,20 +193,6 @@ static be_lv_info_node_t *be_lv_get_or_set(be_lv_t *li, ir_node *bl,
res->flags = 0;
}
#ifdef LV_INTESIVE_CHECKS
{
unsigned i;
unsigned n = irn_live[0].head.n_members;
unsigned last = 0;
be_lv_info_t *payload = &irn_live[1];
for (i = 0; i < n; ++i) {
assert(payload[i].node.idx >= last);
last = payload[i].node.idx;
}
}
#endif
return res;
}
......@@ -457,21 +405,34 @@ static void collect_liveness_nodes(ir_node *irn, void *data)
nodes[get_irn_idx(irn)] = irn;
}
static void compute_liveness(be_lv_t *lv)
void be_liveness_compute_sets(be_lv_t *lv)
{
ir_node **nodes;
int i, n;
int i;
int n;
unsigned last_idx;
if (lv->sets_valid)
return;
be_timer_push(T_LIVE);
last_idx = get_irg_last_idx(lv->irg);
if (last_idx >= bitset_size(lv->nodes)) {
bitset_free(lv->nodes);
lv->nodes = bitset_malloc(last_idx * 2);
} else {
bitset_clear_all(lv->nodes);
}
ir_nodehashmap_init(&lv->map);
obstack_init(&lv->obst);
stat_ev_tim_push();
n = get_irg_last_idx(lv->irg);
nodes = NEW_ARR_F(ir_node *, n);
memset(nodes, 0, sizeof(nodes[0]) * n);
/*
* inserting the variables sorted by their ID is probably
/* inserting the variables sorted by their ID is probably
* more efficient since the binary sorted set insertion
* will not need to move around the data.
*/
* will not need to move around the data. */
irg_walk_graph(lv->irg, NULL, collect_liveness_nodes, nodes);
re.lv = lv;
......@@ -485,96 +446,63 @@ static void compute_liveness(be_lv_t *lv)
DEL_ARR_F(nodes);
free(re.visited);
register_hook(hook_node_info, &lv->hook_info);
stat_ev_tim_pop("be_lv_sets_cons");
}
void be_liveness_assure_sets(be_lv_t *lv)
{
if (!lv->nodes) {
be_timer_push(T_LIVE);
be_timer_pop(T_LIVE);
lv->nodes = bitset_malloc(2 * get_irg_last_idx(lv->irg));
ir_nodehashmap_init(&lv->map);
obstack_init(&lv->obst);
compute_liveness(lv);
/* be_live_chk_compare(lv, lv->lvc); */
lv->sets_valid = true;
}
be_timer_pop(T_LIVE);
}
void be_liveness_compute_chk(be_lv_t *lv)
{
if (lv->lvc != NULL)
return;
lv->lvc = lv_chk_new(lv->irg);
}
void be_liveness_assure_chk(be_lv_t *lv)
void be_liveness_invalidate_sets(be_lv_t *lv)
{
#ifndef USE_LIVE_CHK
be_timer_push(t_verify);
be_liveness_assure_sets(lv);
be_timer_pop(t_verify);
#else
(void) lv;
#endif
if (!lv->sets_valid)
return;
unregister_hook(hook_node_info, &lv->hook_info);
obstack_free(&lv->obst, NULL);
ir_nodehashmap_destroy(&lv->map);
lv->sets_valid = false;
}
void be_liveness_invalidate(be_lv_t *lv)
void be_liveness_invalidate_chk(be_lv_t *lv)
{
if (lv && lv->nodes) {
unregister_hook(hook_node_info, &lv->hook_info);
obstack_free(&lv->obst, NULL);
ir_nodehashmap_destroy(&lv->map);
bitset_free(lv->nodes);
lv->nodes = NULL;
}
be_liveness_invalidate_sets(lv);
if (lv->lvc == NULL)
return;
lv_chk_free(lv->lvc);
lv->lvc = NULL;
}
/* Compute the inter block liveness for a graph. */
be_lv_t *be_liveness(ir_graph *irg)
be_lv_t *be_liveness_new(ir_graph *irg)
{
be_lv_t *lv = XMALLOCZ(be_lv_t);
lv->irg = irg;
#ifdef USE_LIVE_CHK
lv->lvc = lv_chk_new(lv->irg);
#endif
lv->irg = irg;
lv->hook_info.context = lv;
lv->hook_info.hook._hook_node_info = be_dump_liveness_block;
lv->nodes = bitset_malloc(2 * get_irg_last_idx(lv->irg));
return lv;
}
void be_liveness_recompute(be_lv_t *lv)
{
unsigned last_idx;
be_timer_push(T_LIVE);
last_idx = get_irg_last_idx(lv->irg);
if (last_idx >= bitset_size(lv->nodes)) {
bitset_free(lv->nodes);
lv->nodes = bitset_malloc(last_idx * 2);
} else
bitset_clear_all(lv->nodes);
ir_nodehashmap_destroy(&lv->map);
obstack_free(&lv->obst, NULL);
ir_nodehashmap_init(&lv->map);
obstack_init(&lv->obst);
compute_liveness(lv);
be_timer_pop(T_LIVE);
}
void be_liveness_free(be_lv_t *lv)
{
be_liveness_invalidate(lv);
#ifdef USE_LIVE_CHK
lv_chk_free(lv->lvc);
#endif
be_liveness_invalidate_sets(lv);
be_liveness_invalidate_chk(lv);
bitset_free(lv->nodes);
xfree(lv);
}
void be_liveness_remove(be_lv_t *lv, const ir_node *irn)
{
if (lv->nodes) {
if (lv->sets_valid) {
unsigned idx = get_irn_idx(irn);
lv_remove_walker_t w;
......@@ -594,7 +522,7 @@ void be_liveness_remove(be_lv_t *lv, const ir_node *irn)
void be_liveness_introduce(be_lv_t *lv, ir_node *irn)
{
/* Don't compute liveness information for non-data nodes. */
if (lv->nodes && is_liveness_node(irn)) {
if (lv->sets_valid && is_liveness_node(irn)) {
re.lv = lv;
re.visited = bitset_malloc(get_irg_last_idx(lv->irg));
liveness_for_node(irn);
......@@ -686,7 +614,7 @@ static void collect_node(ir_node *irn, void *data)
obstack_ptr_grow(obst, irn);
}
void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc)
static void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc)
{
ir_graph *irg = lv->irg;
......@@ -739,5 +667,6 @@ void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc)
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_live)
void be_init_live(void)
{
(void)be_live_chk_compare;
FIRM_DBG_REGISTER(dbg, "firm.be.liveness");
}
......@@ -47,7 +47,7 @@ typedef enum {
* Compute the inter block liveness for a graph.
* @param irg The graph.
*/
be_lv_t *be_liveness(ir_graph *irg);
be_lv_t *be_liveness_new(ir_graph *irg);
/**
* Free the liveness information.
......@@ -55,9 +55,21 @@ be_lv_t *be_liveness(ir_graph *irg);
void be_liveness_free(be_lv_t *lv);
/**
* Recompute the complete liveness information.
* (Re)compute the liveness information if necessary.
*/
void be_liveness_recompute(be_lv_t *lv);
void be_liveness_compute_sets(be_lv_t *lv);
void be_liveness_compute_chk(be_lv_t *lv);
/**
* Invalidate the liveness information.
* You must call this if you modify the program and do not
* update the liveness with the be_liveness_{update,remove,introduce}
* functions.
* @note If changed the control flow then you must also call
* be_liveness_invalidate_chk()
*/
void be_liveness_invalidate_sets(be_lv_t *lv);
void be_liveness_invalidate_chk(be_lv_t *lv);
/**
* Update the liveness information for a single node.
......@@ -148,25 +160,4 @@ void be_liveness_nodes_live_at(const be_lv_t *lv,
const arch_register_class_t *cls,
const ir_node *pos, ir_nodeset_t *live);
/**
* Make sure the live sets are computed.
* @param lv The liveness information.
*/
void be_liveness_assure_sets(be_lv_t *lv);
/**
* Make sure all information needed for liveness checks is available.
* @param lv The liveness information.
*/
void be_liveness_assure_chk(be_lv_t *lv);
/**
* Invalidate the liveness information.
* You must call this if you modify the program and do not
* update the liveness with the be_liveness_{update,remove,introduce}
* functions.
* @param lv The liveness info.
*/
void be_liveness_invalidate(be_lv_t *lv);
#endif /* FIRM_BE_BELIVE_H */
#endif
......@@ -26,10 +26,12 @@
#ifndef FIRM_BE_BELIVE_T_H
#define FIRM_BE_BELIVE_T_H
#include <stdbool.h>
#include "be_types.h"
#include "irgraph_t.h"
#include "irnodehashmap.h"
#include "irhooks.h"
#include "irlivechk.h"
#include "statev.h"
#include "pset.h"
......@@ -37,21 +39,14 @@
#include "belive.h"
#define USE_LIVE_CHK
#ifdef USE_LIVE_CHK
#include "irlivechk.h"
#endif
struct be_lv_t {
ir_nodehashmap_t map;
struct obstack obst;
bool sets_valid;
ir_graph *irg;
bitset_t *nodes;
hook_entry_t hook_info;
#ifdef USE_LIVE_CHK
lv_chk_t *lvc;
#endif
};
typedef struct be_lv_info_node_t be_lv_info_node_t;
......@@ -102,15 +97,12 @@ static inline unsigned _be_is_live_xxx(const be_lv_t *li, const ir_node *block,
{
unsigned res;
if (li->nodes != NULL) {
if (li->sets_valid) {
be_lv_info_node_t *info = be_lv_get(li, block, irn);
res = info != NULL ? (info->flags & flags) != 0 : 0;
}
#ifdef USE_LIVE_CHK
else
} else {
res = (lv_chk_bl_xxx(li->lvc, block, irn) & flags) != 0;
#endif
}
return res;
}
......
......@@ -832,10 +832,9 @@ void assure_constraints(ir_graph *irg)
ir_nodehashmap_destroy(&cenv.op_set);
obstack_free(&cenv.obst, NULL);
be_liveness_invalidate(be_get_irg_liveness(irg));
be_invalidate_live_sets(irg);
}
/**
* Push nodes that do not need to be permed through the Perm.
* This is commonly a reload cascade at block ends.
......@@ -1014,7 +1013,7 @@ void lower_nodes_after_ra(ir_graph *irg, int do_copy)
env.do_copy = do_copy;
/* we will need interference */
be_liveness_assure_chk(be_get_irg_liveness(irg));
be_assure_live_chk(irg);
irg_walk_graph(irg, NULL, lower_nodes_after_ra_walker, &env);
}
......@@ -429,6 +429,7 @@ static void initialize_birg(be_irg_t *birg, ir_graph *irg, be_main_env_t *env)
birg->irg = irg;
birg->main_env = env;
obstack_init(&birg->obst);
birg->lv = be_liveness_new(irg);
edges_deactivate_kind(irg, EDGE_KIND_DEP);
edges_activate_kind(irg, EDGE_KIND_DEP);
......
......@@ -636,9 +636,8 @@ static void be_pbqp_coloring(be_chordal_env_t *env)
printf("#### ----- === Allocating registers of %s (%s) ===\n", cls->name, get_entity_name(get_irg_entity(irg)));
#endif
lv = be_assure_liveness(irg);
be_liveness_assure_sets(lv);
be_liveness_assure_chk(lv);
be_assure_live_sets(irg);
lv = be_get_irg_liveness(irg);
/* insert perms */
assure_doms(irg);
......
......@@ -324,10 +324,12 @@ ir_node *be_peephole_IncSP_IncSP(ir_node *node)
void be_peephole_opt(ir_graph *irg)
{
#if 0
/* we sometimes find BadE nodes in float apps like optest_float.c or
* kahansum.c for example... */
be_liveness_invalidate(be_get_irg_liveness(irg));
be_liveness_assure_sets(be_assure_liveness(irg));
be_invalidate_live_sets(irg);
#endif
be_assure_live_sets(irg);
arch_env = be_get_irg_arch_env(irg);
lv = be_get_irg_liveness(irg);
......
......@@ -2010,8 +2010,8 @@ static void be_pref_alloc_cls(void)
{
size_t i;
lv = be_assure_liveness(irg);
be_liveness_assure_sets(lv);
be_assure_live_sets(irg);
lv = be_get_irg_liveness(irg);
ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
......@@ -2109,8 +2109,7 @@ static void be_pref_alloc(ir_graph *new_irg)
/* we most probably constructed new Phis so liveness info is invalid
* now */
/* TODO: test liveness_introduce */
be_liveness_invalidate(lv);
be_invalidate_live_sets(irg);
free(normal_regs);
stat_ev_ctx_pop("regcls");
......
......@@ -524,10 +524,10 @@ static trace_env_t *trace_init(ir_graph *irg)
env->curr_time = 0;
env->sched_info = NEW_ARR_F(trace_irn_t, nn);
env->liveness = be_liveness(irg);
env->liveness = be_get_irg_liveness(irg);
FIRM_DBG_REGISTER(env->dbg, "firm.be.sched.trace");
be_liveness_assure_chk(env->liveness);
be_assure_live_chk(irg);
memset(env->sched_info, 0, nn * sizeof(*(env->sched_info)));