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

optimize iredges code

- split edges_notify_edge into add/del/modify and call the correct
  function where we know which is needed
- get rid of some unnecessary flags so the ir_edge_t struct is smaller
parent 5aba9af4
......@@ -72,7 +72,8 @@ FIRM_API const ir_edge_t *get_block_succ_first(const ir_node *block);
* @return The next out edge in @p irn 's out list after @p last.
*/
FIRM_API const ir_edge_t *get_irn_out_edge_next(const ir_node *irn,
const ir_edge_t *last);
const ir_edge_t *last,
ir_edge_kind_t kind);
/**
* A convenience iteration macro over all out edges of a node.
......@@ -82,7 +83,7 @@ FIRM_API const ir_edge_t *get_irn_out_edge_next(const ir_node *irn,
* edge.
*/
#define foreach_out_edge_kind(irn, edge, kind) \
for (ir_edge_t const *edge = get_irn_out_edge_first_kind(irn, kind); edge; edge = get_irn_out_edge_next(irn, edge))
for (ir_edge_t const *edge = get_irn_out_edge_first_kind(irn, kind); edge; edge = get_irn_out_edge_next(irn, edge, kind))
/**
* A convenience iteration macro over all out edges of a node, which is safe
......@@ -94,7 +95,7 @@ FIRM_API const ir_edge_t *get_irn_out_edge_next(const ir_node *irn,
*/
#define foreach_out_edge_kind_safe(irn, edge, kind) \
for (ir_edge_t const *edge = get_irn_out_edge_first_kind((irn), (kind)), *edge##__next; \
edge ? edge##__next = get_irn_out_edge_next((irn), edge), 1 : (edge##__next = NULL, 0); \
edge ? edge##__next = get_irn_out_edge_next((irn), edge, (kind)), 1 : (edge##__next = NULL, 0); \
edge = edge##__next)
/**
......
......@@ -696,17 +696,17 @@ ir_node *new_r_Anchor(ir_graph *irg)
ir_node *res;
size_t i;
memset(in, 0, sizeof(in));
res = new_ir_node(NULL, irg, NULL, op_Anchor, mode_ANY, anchor_last+1, in);
res = new_ir_node(NULL, irg, NULL, op_Anchor, mode_ANY, 0, NULL);
res->attr.anchor.irg.irg = irg;
/* hack to get get_irn_irg working: set block to ourself and allow
* get_Block_irg for anchor */
/* hack to get get_irn_irg in set_irn_in() working */
res->in[0] = res;
/* we can't have NULL inputs so reference ourselves for now */
for (i = 0; i <= (size_t)anchor_last; ++i) {
set_irn_n(res, i, res);
in[i] = res;
}
set_irn_in(res, anchor_last+1, in);
return res;
}
......
......@@ -214,8 +214,6 @@ static inline void verify_list_head(ir_node *irn, ir_edge_kind_t kind)
ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
fprintf(stderr, "- at list entry %d\n", num);
if (edge->invalid)
fprintf(stderr, "- edge(%ld) is invalid\n", edge_get_id(edge));
if (edge->src)
ir_fprintf(stderr, "- edge(%ld) %+F(%d)\n", edge_get_id(edge), edge->src, edge->pos);
err = 1;
......@@ -243,108 +241,115 @@ void edges_dump_kind(ir_graph *irg, ir_edge_kind_t kind)
info = get_irg_edge_info(irg, kind);
edges = &info->edges;
foreach_ir_edgeset(edges, e, iter) {
ir_printf("%+F %d %d\n", e->src, e->pos, e->invalid);
ir_printf("%+F %d\n", e->src, e->pos);
}
}
static void add_edge(ir_node *src, int pos, ir_node *tgt, ir_edge_kind_t kind,
ir_graph *irg)
{
assert(edges_activated_kind(irg, kind));
irg_edge_info_t *info = get_irg_edge_info(irg, kind);
ir_edgeset_t *edges = &info->edges;
struct list_head *head = &get_irn_edge_info(tgt, kind)->outs_head;
assert(head->next && head->prev &&
"target list head must have been initialized");
/* The old target was NULL, thus, the edge is newly created. */
ir_edge_t *edge;
if (list_empty(&info->free_edges)) {
edge = OALLOC(&info->edges_obst, ir_edge_t);
} else {
edge = list_entry(info->free_edges.next, ir_edge_t, list);
list_del(&edge->list);
}
edge->src = src;
edge->pos = pos;
#ifdef DEBUG_libfirm
edge->present = 0;
#endif
ir_edge_t *new_edge = ir_edgeset_insert(edges, edge);
assert(new_edge == edge);
(void)new_edge;
list_add(&edge->list, head);
edge_change_cnt(tgt, kind, +1);
}
static void delete_edge(ir_node *src, int pos, ir_node *old_tgt,
ir_edge_kind_t kind, ir_graph *irg)
{
assert(edges_activated_kind(irg, kind));
irg_edge_info_t *info = get_irg_edge_info(irg, kind);
ir_edgeset_t *edges = &info->edges;
/* Initialize the edge template to search in the set. */
ir_edge_t templ;
templ.src = src;
templ.pos = pos;
/* search the edge in the set. */
ir_edge_t *edge = ir_edgeset_find(edges, &templ);
/* mark the edge invalid if it was found */
if (edge == NULL)
return;
list_del(&edge->list);
ir_edgeset_remove(edges, edge);
list_add(&edge->list, &info->free_edges);
edge->pos = -2;
edge->src = NULL;
edge_change_cnt(old_tgt, kind, -1);
}
void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
ir_node *old_tgt, ir_edge_kind_t kind,
ir_graph *irg)
{
const char *msg = "";
irg_edge_info_t *info;
ir_edgeset_t *edges;
ir_edge_t templ;
if (old_tgt == NULL) {
add_edge(src, pos, tgt, kind, irg);
return;
} else if (tgt == NULL) {
delete_edge(src, pos, old_tgt, kind, irg);
return;
}
/* Only do something, if the old and new target differ. */
if (tgt == old_tgt)
return;
assert(edges_activated_kind(irg, kind));
irg_edge_info_t *info = get_irg_edge_info(irg, kind);
ir_edgeset_t *edges = &info->edges;
/*
* Only do something, if the old and new target differ.
* The target is not NULL and the old target differs
* from the new target, the edge shall be moved (if the
* old target was != NULL) or added (if the old target was
* NULL).
*/
if (tgt == old_tgt)
return;
struct list_head *head = &get_irn_edge_info(tgt, kind)->outs_head;
info = get_irg_edge_info(irg, kind);
edges = &info->edges;
assert(head->next && head->prev &&
"target list head must have been initialized");
/* Initialize the edge template to search in the set. */
ir_edge_t templ;
templ.src = src;
templ.pos = pos;
/*
* If the target is NULL, the edge shall be deleted.
*/
if (tgt == NULL) {
/* search the edge in the set. */
ir_edge_t *edge = ir_edgeset_find(edges, &templ);
/* mark the edge invalid if it was found */
if (edge) {
msg = "deleting";
list_del(&edge->list);
ir_edgeset_remove(edges, edge);
list_add(&edge->list, &info->free_edges);
edge->invalid = 1;
edge->pos = -2;
edge->src = NULL;
edge_change_cnt(old_tgt, kind, -1);
} else {
/* If the edge was not found issue a warning on the debug stream */
msg = "edge to delete not found!\n";
}
} else {
/*
* The target is not NULL and the old target differs
* from the new target, the edge shall be moved (if the
* old target was != NULL) or added (if the old target was
* NULL).
*/
struct list_head *head = &get_irn_edge_info(tgt, kind)->outs_head;
assert(head->next && head->prev &&
"target list head must have been initialized");
/* If the old target is not null, the edge is moved. */
if (old_tgt) {
ir_edge_t *edge = ir_edgeset_find(edges, &templ);
assert(edge && "edge to redirect not found!");
assert(! edge->invalid && "Invalid edge encountered");
msg = "redirecting";
list_move(&edge->list, head);
edge_change_cnt(old_tgt, kind, -1);
} else {
/* The old target was NULL, thus, the edge is newly created. */
ir_edge_t *new_edge;
ir_edge_t *edge;
if (list_empty(&info->free_edges)) {
edge = OALLOC(&info->edges_obst, ir_edge_t);
} else {
edge = list_entry(info->free_edges.next, ir_edge_t, list);
list_del(&edge->list);
}
ir_edge_t *edge = ir_edgeset_find(edges, &templ);
assert(edge && "edge to redirect not found!");
edge->src = src;
edge->pos = pos;
edge->invalid = 0;
edge->present = 0;
edge->kind = kind;
edge->list.next = NULL;
edge->list.prev = NULL;
new_edge = ir_edgeset_insert(edges, edge);
if (new_edge != edge) {
panic("new edge exists already");
}
msg = "adding";
list_add(&edge->list, head);
}
edge_change_cnt(tgt, kind, +1);
}
list_move(&edge->list, head);
edge_change_cnt(old_tgt, kind, -1);
edge_change_cnt(tgt, kind, +1);
#ifndef DEBUG_libfirm
/* verify list heads */
......@@ -355,8 +360,6 @@ void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
verify_list_head(old_tgt, kind);
}
#endif
DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
}
void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt,
......@@ -409,7 +412,7 @@ static void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind)
foreach_tgt(old, i, n, kind) {
ir_node *old_tgt = get_n(old, i, kind);
edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
delete_edge(old, i, old_tgt, kind, irg);
}
}
......@@ -437,7 +440,7 @@ static void edges_node_revival_kind(ir_node *irn, ir_edge_kind_t kind)
foreach_tgt(irn, i, n, kind) {
ir_node *tgt = get_n(irn, i, kind);
edges_notify_edge_kind(irn, i, tgt, NULL, kind, irg);
add_edge(irn, i, tgt, kind, irg);
}
info->edges_built = 1;
}
......@@ -460,7 +463,8 @@ static void build_edges_walker(ir_node *irn, void *data)
foreach_tgt(irn, i, n, kind) {
ir_node *pred = get_n(irn, i, kind);
edges_notify_edge_kind(irn, i, pred, NULL, kind, irg);
if (pred != NULL)
add_edge(irn, i, pred, kind, irg);
}
get_irn_edge_info(irn, kind)->edges_built = 1;
}
......@@ -640,6 +644,7 @@ void edges_reroute_except(ir_node *from, ir_node *to, ir_node *exception)
}
}
#ifdef DEBUG_libfirm
static void verify_set_presence(ir_node *irn, void *data)
{
build_walker *w = (build_walker*)data;
......@@ -686,9 +691,11 @@ static void verify_list_presence(ir_node *irn, void *data)
}
}
}
#endif
int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
{
#ifdef DEBUG_libfirm
struct build_walker w;
ir_edgeset_t *edges = &get_irg_edge_info(irg, kind)->edges;
ir_edge_t *e;
......@@ -711,13 +718,18 @@ int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
* edge set is wrong.
*/
foreach_ir_edgeset(edges, e, iter) {
if (! e->invalid && ! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
if (! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
w.problem_found = 1;
ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d is superfluous\n", edge_get_id(e), e->src, e->pos);
}
}
return w.problem_found;
#else
(void)irg;
(void)kind;
return 0;
#endif
}
#define IGNORE_NODE(irn) (is_Bad((irn)) || is_Block((irn)))
......@@ -928,9 +940,9 @@ const ir_edge_t *(get_block_succ_first)(const ir_node *block)
return get_irn_out_edge_first_kind_(block, EDGE_KIND_BLOCK);
}
const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last, ir_edge_kind_t kind)
{
return get_irn_out_edge_next_(irn, last);
return get_irn_out_edge_next_(irn, last, kind);
}
ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
......
......@@ -40,11 +40,11 @@
#define get_irn_n_edges_kind(irn, kind) get_irn_n_edges_kind_(irn, kind)
#define get_edge_src_irn(edge) get_edge_src_irn_(edge)
#define get_edge_src_pos(edge) get_edge_src_pos_(edge)
#define get_irn_out_edge_next(irn, last) get_irn_out_edge_next_(irn, last)
#define get_irn_out_edge_next(irn, last, kind) get_irn_out_edge_next_(irn, last, kind)
#define get_irn_n_edges(irn) get_irn_n_edges_kind_(irn, EDGE_KIND_NORMAL)
#define get_irn_out_edge_first(irn) get_irn_out_edge_first_kind_(irn, EDGE_KIND_NORMAL)
#define get_block_succ_first(irn) get_irn_out_edge_first_kind_(irn, EDGE_KIND_BLOCK)
#define get_block_succ_next(irn, last) get_irn_out_edge_next_(irn, last)
#define get_block_succ_next(irn, last) get_irn_out_edge_next_(irn, last, EDGE_KIND_BLOCK)
/**
* An edge.
......@@ -52,9 +52,9 @@
struct ir_edge_t {
ir_node *src; /**< The source node of the edge. */
int pos; /**< The position of the edge at @p src. */
unsigned invalid : 1; /**< edges that are removed are marked invalid. */
#ifdef DEBUG_libfirm
unsigned present : 1; /**< Used by the verifier. Don't rely on its content. */
unsigned kind : 4; /**< The kind of the edge. */
#endif
struct list_head list; /**< The list head to queue all out edges at a node. */
};
......@@ -106,11 +106,11 @@ static inline const ir_edge_t *get_irn_out_edge_first_kind_(const ir_node *irn,
* @param last The last out edge you have seen.
* @return The next out edge in @p irn 's out list after @p last.
*/
static inline const ir_edge_t *get_irn_out_edge_next_(const ir_node *irn, const ir_edge_t *last)
static inline const ir_edge_t *get_irn_out_edge_next_(const ir_node *irn, const ir_edge_t *last, ir_edge_kind_t kind)
{
struct list_head *next = last->list.next;
const struct list_head *head
= &get_irn_edge_info_const(irn, (ir_edge_kind_t)last->kind)->outs_head;
= &get_irn_edge_info_const(irn, kind)->outs_head;
return next == head ? NULL : list_entry(next, ir_edge_t, list);
}
......@@ -191,10 +191,7 @@ void edges_invalidate_all(ir_node *irn);
*/
void edges_dump_kind(ir_graph *irg, ir_edge_kind_t kind);
/**
* Notify normal and block edges.
*/
void edges_notify_edge(ir_node *src, int pos, ir_node *tgt,
ir_node *old_tgt, ir_graph *irg);
void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt,
ir_graph *irg);
#endif
......@@ -138,7 +138,8 @@ ir_node *new_ir_node(dbg_info *db, ir_graph *irg, ir_node *block, ir_op *op,
}
/* don't put this into the for loop, arity is -1 for some nodes! */
edges_notify_edge(res, -1, res->in[0], NULL, irg);
if (block != NULL)
edges_notify_edge(res, -1, block, NULL, irg);
for (i = 1; i <= arity; ++i)
edges_notify_edge(res, i - 1, res->in[i], NULL, irg);
......@@ -242,16 +243,37 @@ int add_irn_n(ir_node *node, ir_node *in)
/* Call the hook */
hook_set_irn_n(node, pos, node->in[pos + 1], NULL);
/* update irg flags */
clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
return pos;
}
static void del_irn_n(ir_node *node, int n)
{
ir_graph *irg = get_irn_irg(node);
/* remove the edge */
ir_node *pred = node->in[n+1];
edges_notify_edge(node, n, NULL, pred, irg);
int arity = get_irn_arity(node);
if (n != arity-1) {
/* exchange with the last one */
ir_node *old = node->in[arity];
edges_notify_edge(node, arity-1, NULL, old, irg);
node->in[n+1] = old;
edges_notify_edge(node, n, old, NULL, irg);
}
ARR_SHRINKLEN(node->in, arity);
/* update irg flags */
clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
}
void del_Sync_n(ir_node *n, int i)
{
int arity = get_Sync_n_preds(n);
ir_node *last_pred = get_Sync_pred(n, arity - 1);
set_Sync_pred(n, i, last_pred);
edges_notify_edge(n, arity - 1, NULL, last_pred, get_irn_irg(n));
ARR_SHRINKLEN(get_irn_in(n), arity);
del_irn_n(n, i);
}
int (get_irn_deps)(const ir_node *node)
......@@ -623,9 +645,7 @@ void set_End_keepalives(ir_node *end, int n, ir_node *in[])
void remove_End_keepalive(ir_node *end, ir_node *irn)
{
int n = get_End_n_keepalives(end);
ir_graph *irg;
int n = get_End_n_keepalives(end);
int idx = -1;
for (int i = n;;) {
if (i-- == 0)
......@@ -635,27 +655,12 @@ void remove_End_keepalive(ir_node *end, ir_node *irn)
/* find irn */
if (old_ka == irn) {
idx = i;
idx = END_KEEPALIVE_OFFSET + i;
break;
}
}
irg = get_irn_irg(end);
/* remove the edge */
edges_notify_edge(end, idx, NULL, irn, irg);
if (idx != n - 1) {
/* exchange with the last one */
ir_node *old = end->in[1 + END_KEEPALIVE_OFFSET + n - 1];
edges_notify_edge(end, n - 1, NULL, old, irg);
end->in[1 + END_KEEPALIVE_OFFSET + idx] = old;
edges_notify_edge(end, idx, old, NULL, irg);
}
/* now n - 1 keeps, 1 block input */
ARR_RESIZE(ir_node *, end->in, (n - 1) + 1 + END_KEEPALIVE_OFFSET);
/* update irg flags */
clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
assert(idx != -1);
del_irn_n(end, idx);
}
void remove_End_Bads_and_doublets(ir_node *end)
......@@ -676,24 +681,12 @@ void remove_End_Bads_and_doublets(ir_node *end)
if (is_Bad(ka) || is_NoMem(ka) || pset_new_contains(&keeps, ka)) {
changed = true;
/* remove the edge */
edges_notify_edge(end, idx, NULL, ka, irg);
if (idx != n - 1) {
/* exchange with the last one */
ir_node *old = end->in[1 + END_KEEPALIVE_OFFSET + n - 1];
edges_notify_edge(end, n - 1, NULL, old, irg);
end->in[1 + END_KEEPALIVE_OFFSET + idx] = old;
edges_notify_edge(end, idx, old, NULL, irg);
}
del_irn_n(end, idx - END_KEEPALIVE_OFFSET);
--n;
} else {
pset_new_insert(&keeps, ka);
}
}
/* n keeps, 1 block input */
ARR_RESIZE(ir_node *, end->in, n + 1 + END_KEEPALIVE_OFFSET);
pset_new_destroy(&keeps);
if (changed) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment