/*
- * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
+ * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
#include "cgana.h"
#include "debug.h"
-#include "beirg_t.h"
+#include "beirg.h"
+#include "beabi.h"
#include "betranshlp.h"
+#include "belive.h"
+#include "benode.h"
typedef struct be_transform_env_t {
ir_graph *irg; /**< The irg, the node should be created in */
void be_set_transformed_node(ir_node *old_node, ir_node *new_node)
{
+ ir_graph *irg = get_irn_irg(old_node);
+
set_irn_link(old_node, new_node);
mark_irn_visited(old_node);
+ hook_dead_node_elim_subst(irg, old_node, new_node);
}
-int be_is_transformed(const ir_node *node) {
+int be_is_transformed(const ir_node *node)
+{
return irn_visited(node);
}
-static inline ir_node *be_get_transformed_node(ir_node *old_node) {
+static inline ir_node *be_get_transformed_node(ir_node *old_node)
+{
if (irn_visited(old_node)) {
- ir_node *new_node = get_irn_link(old_node);
+ ir_node *new_node = (ir_node*)get_irn_link(old_node);
assert(new_node != NULL);
return new_node;
}
return NULL;
}
-void be_duplicate_deps(ir_node *old_node, ir_node *new_node) {
+void be_duplicate_deps(ir_node *old_node, ir_node *new_node)
+{
int i;
int deps = get_irn_deps(old_node);
}
}
-void be_dep_on_frame(ir_node* node)
+void be_set_transform_function(ir_op *op, be_transform_func func)
+{
+ /* shouldn't be assigned twice (except for exchanging the default
+ * be_duplicate_node entries) */
+ assert(op->ops.generic == NULL
+ || op->ops.generic == (op_func) be_duplicate_node);
+ op->ops.generic = (op_func) func;
+}
+
+/**
+ * Transform helper for blocks.
+ */
+static ir_node *transform_block(ir_node *node)
+{
+ ir_graph *irg = get_irn_irg(node);
+ dbg_info *dbgi = get_irn_dbg_info(node);
+ ir_node *block;
+
+ block = new_ir_node(dbgi, irg, NULL, get_irn_op(node), get_irn_mode(node),
+ get_irn_arity(node), get_irn_in(node) + 1);
+ copy_node_attr(irg, node, block);
+ block->node_nr = node->node_nr;
+
+ /* put the preds in the worklist */
+ be_enqueue_preds(node);
+
+ return block;
+}
+
+static ir_node *transform_end(ir_node *node)
{
- ir_graph *const irg = current_ir_graph;
+ /* end has to be duplicated manually because we need a dynamic in array */
+ ir_graph *irg = get_irn_irg(node);
+ dbg_info *dbgi = get_irn_dbg_info(node);
+ ir_node *block = be_transform_node(get_nodes_block(node));
+ int i, arity;
+ ir_node *new_end;
+
+ new_end = new_ir_node(dbgi, irg, block, op_End, mode_X, -1, NULL);
+ copy_node_attr(irg, node, new_end);
+ be_duplicate_deps(node, new_end);
+
+ set_irg_end(irg, new_end);
+
+ /* do not transform predecessors yet to keep the pre-transform
+ * phase from visiting all the graph */
+ arity = get_irn_arity(node);
+ for (i = 0; i < arity; ++i) {
+ ir_node *in = get_irn_n(node, i);
+ add_End_keepalive(new_end, in);
+ }
+ be_enqueue_preds(node);
- if (get_irg_start_block(irg) == get_nodes_block(node))
- add_irn_dep(node, get_irg_frame(irg));
+ return new_end;
+}
+
+void be_start_transform_setup(void)
+{
+ clear_irp_opcodes_generic_func();
+
+ be_set_transform_function(op_Bad, be_duplicate_node);
+ be_set_transform_function(op_be_Copy, be_duplicate_node);
+ be_set_transform_function(op_be_CopyKeep, be_duplicate_node);
+ be_set_transform_function(op_be_IncSP, be_duplicate_node);
+ be_set_transform_function(op_be_Keep, be_duplicate_node);
+ be_set_transform_function(op_be_Return, be_duplicate_node);
+ be_set_transform_function(op_be_Start, be_duplicate_node);
+ be_set_transform_function(op_Block, transform_block);
+ be_set_transform_function(op_End, transform_end);
+ be_set_transform_function(op_NoMem, be_duplicate_node);
+ be_set_transform_function(op_Pin, be_duplicate_node);
+ be_set_transform_function(op_Start, be_duplicate_node);
+ be_set_transform_function(op_Sync, be_duplicate_node);
}
-ir_node *be_duplicate_node(ir_node *node) {
+ir_node *be_duplicate_node(ir_node *node)
+{
ir_node *block = be_transform_node(get_nodes_block(node));
ir_graph *irg = env.irg;
dbg_info *dbgi = get_irn_dbg_info(node);
new_node = new_ir_node(dbgi, irg, block, op, mode, arity, ins);
}
- copy_node_attr(node, new_node);
+ copy_node_attr(irg, node, new_node);
be_duplicate_deps(node, new_node);
-#ifdef DEBUG_libfirm
new_node->node_nr = node->node_nr;
-#endif
-
return new_node;
}
-/**
- * Calls transformation function for given node and marks it visited.
- */
-ir_node *be_transform_node(ir_node *node) {
- ir_op *op;
- ir_node *new_node = be_get_transformed_node(node);
+ir_node *be_transform_node(ir_node *node)
+{
+ ir_op *op;
+ ir_node *new_node = be_get_transformed_node(node);
+ be_transform_func *transform;
if (new_node != NULL)
return new_node;
DEBUG_ONLY(be_set_transformed_node(node, NULL));
op = get_irn_op(node);
- if (op->ops.generic) {
- be_transform_func *transform = (be_transform_func *)op->ops.generic;
-
- new_node = transform(node);
- assert(new_node != NULL);
- } else {
- new_node = be_duplicate_node(node);
+ if (op->ops.generic == NULL) {
+ panic("No transform function registered for node %+F.", node);
}
+ transform = (be_transform_func *)op->ops.generic;
+
+ new_node = transform(node);
+ assert(new_node != NULL);
be_set_transformed_node(node, new_node);
- hook_dead_node_elim_subst(current_ir_graph, node, new_node);
return new_node;
}
-/**
- * enqueue all inputs into the transform queue.
- */
-void be_enqueue_preds(ir_node *node) {
+void be_enqueue_preds(ir_node *node)
+{
int i, arity;
/* put the preds in the worklist */
/**
* Rewire nodes which are potential loops (like Phis) to avoid endless loops.
*/
-static void fix_loops(ir_node *node) {
+static void fix_loops(ir_node *node)
+{
int i, arity;
int changed;
changed = 0;
if (! is_Block(node)) {
ir_node *block = get_nodes_block(node);
- ir_node *new_block = get_irn_link(block);
+ ir_node *new_block = (ir_node*)get_irn_link(block);
if (new_block != NULL) {
set_nodes_block(node, new_block);
arity = get_irn_arity(node);
for (i = 0; i < arity; ++i) {
ir_node *in = get_irn_n(node, i);
- ir_node *nw = get_irn_link(in);
+ ir_node *nw = (ir_node*)get_irn_link(in);
if (nw != NULL && nw != in) {
set_irn_n(node, i, nw);
fix_loops(in);
}
/* fix proj block */
- if(is_Proj(node)) {
+ if (is_Proj(node)) {
set_nodes_block(node, get_nodes_block(get_Proj_pred(node)));
changed = 1;
}
arity = get_irn_deps(node);
for (i = 0; i < arity; ++i) {
ir_node *in = get_irn_dep(node, i);
- ir_node *nw = get_irn_link(in);
+ ir_node *nw = (ir_node*)get_irn_link(in);
if (nw != NULL && nw != in) {
set_irn_dep(node, i, nw);
fix_loops(in);
}
- if(changed) {
- identify_remember(current_ir_graph->value_table, node);
+ if (changed) {
+ identify_remember(node);
}
}
-ir_node *be_pre_transform_node(ir_node *place) {
+ir_node *be_pre_transform_node(ir_node *place)
+{
if (place == NULL)
return NULL;
return be_transform_node(place);
}
-static void pre_transform_anchor(int anchor)
+static void pre_transform_anchor(ir_graph *irg, int anchor)
{
ir_node *old_anchor_node = get_irn_n(env.old_anchor, anchor);
ir_node *transformed = be_transform_node(old_anchor_node);
- set_irg_anchor(current_ir_graph, anchor, transformed);
-}
-
-static void kill_unused_anchor(int anchor)
-{
- ir_node *old_anchor_node = get_irn_n(env.old_anchor, anchor);
- ir_node *old_bad = get_irn_n(env.old_anchor, anchor_bad);
- if(old_anchor_node != NULL && get_irn_n_edges(old_anchor_node) <= 1) {
- set_irn_n(env.old_anchor, anchor, old_bad);
- }
+ set_irg_anchor(irg, anchor, transformed);
}
/**
inc_irg_visited(irg);
- env.irg = irg;
- env.worklist = new_waitq();
- env.old_anchor = irg->anchor;
+ env.irg = irg;
+ env.worklist = new_waitq();
+ env.old_anchor = irg->anchor;
old_end = get_irg_end(irg);
waitq_put(env.worklist, anchor);
}
- new_anchor = new_Anchor(irg);
+ new_anchor = new_r_Anchor(irg);
irg->anchor = new_anchor;
/* pre transform some anchors (so they are available in the other transform
* functions) */
- pre_transform_anchor(anchor_bad);
- pre_transform_anchor(anchor_no_mem);
- pre_transform_anchor(anchor_start_block);
- pre_transform_anchor(anchor_start);
- pre_transform_anchor(anchor_frame);
- kill_unused_anchor(anchor_tls);
+ pre_transform_anchor(irg, anchor_no_mem);
+ pre_transform_anchor(irg, anchor_end_block);
+ pre_transform_anchor(irg, anchor_end);
+ pre_transform_anchor(irg, anchor_start_block);
+ pre_transform_anchor(irg, anchor_start);
+ pre_transform_anchor(irg, anchor_frame);
if (pre_transform)
pre_transform();
/* process worklist (this should transform all nodes in the graph) */
while (! waitq_empty(env.worklist)) {
- ir_node *node = waitq_get(env.worklist);
+ ir_node *node = (ir_node*)waitq_get(env.worklist);
be_transform_node(node);
}
if (anchor == NULL)
continue;
- anchor = get_irn_link(anchor);
+ anchor = (ir_node*)get_irn_link(anchor);
fix_loops(anchor);
set_irn_n(new_anchor, i, anchor);
}
- set_nodes_block(new_anchor, get_irg_anchor(irg, anchor_end_block));
del_waitq(env.worklist);
free_End(old_end);
hook_dead_node_elim(irg, 0);
}
-/**
- * Transform helper for blocks.
- */
-static ir_node *gen_Block(ir_node *node) {
- ir_graph *irg = current_ir_graph;
- dbg_info *dbgi = get_irn_dbg_info(node);
- ir_node *old_start_block = get_irn_n(env.old_anchor, anchor_start_block);
- ir_node *macroblock = get_Block_MacroBlock(node);
- ir_node *block;
-
- /*
- * We replace the ProjX from the start node with a jump,
- * so the startblock has no preds anymore now
- */
- if (node == old_start_block) {
- return new_rd_Block(dbgi, irg, 0, NULL);
- }
-
- /* we use the old blocks for now, because jumps allow cycles in the graph
- * we have to fix this later */
- block = new_ir_node(dbgi, irg, NULL, get_irn_op(node), get_irn_mode(node),
- get_irn_arity(node), get_irn_in(node) + 1);
- copy_node_attr(node, block);
-#ifdef DEBUG_libfirm
- block->node_nr = node->node_nr;
-#endif
- if (node == macroblock) {
- /* this node is a macroblock header */
- set_Block_MacroBlock(block, block);
- } else {
- macroblock = be_transform_node(macroblock);
- set_Block_MacroBlock(block, macroblock);
- }
-
- /* put the preds in the worklist */
- be_enqueue_preds(node);
-
- return block;
-}
-
-static ir_node *gen_End(ir_node *node) {
- /* end has to be duplicated manually because we need a dynamic in array */
- ir_graph *irg = current_ir_graph;
- dbg_info *dbgi = get_irn_dbg_info(node);
- ir_node *block = be_transform_node(get_nodes_block(node));
- int i, arity;
- ir_node *new_end;
-
- new_end = new_ir_node(dbgi, irg, block, op_End, mode_X, -1, NULL);
- copy_node_attr(node, new_end);
- be_duplicate_deps(node, new_end);
-
- set_irg_end(irg, new_end);
-
- /* transform preds */
- arity = get_irn_arity(node);
- for (i = 0; i < arity; ++i) {
- ir_node *in = get_irn_n(node, i);
- ir_node *new_in = be_transform_node(in);
-
- add_End_keepalive(new_end, new_in);
- }
-
- return new_end;
-}
-
-void be_transform_graph(be_irg_t *birg, arch_pretrans_nodes *func)
+void be_transform_graph(ir_graph *irg, arch_pretrans_nodes *func)
{
- ir_graph *irg = birg->irg;
ir_graph *old_current_ir_graph = current_ir_graph;
struct obstack *old_obst = NULL;
struct obstack *new_obst = NULL;
irg->obst = new_obst;
irg->last_node_idx = 0;
- /* create new value table for CSE */
- del_identities(irg->value_table);
- irg->value_table = new_identities();
+ /* invalidate phase info as (at least vrp info) is used inside the
+ * equivalent/compute_value functions and might replace our newly
+ * created nodes with middleend nodes */
+ irg_invalidate_phases(irg);
- /* enter special helper */
- op_Block->ops.generic = (op_func)gen_Block;
- op_End->ops.generic = (op_func)gen_End;
+ /* create new value table for CSE */
+ new_identities(irg);
/* do the main transformation */
transform_nodes(irg, func);
free_loop_information(irg);
set_irg_doms_inconsistent(irg);
- be_liveness_invalidate(be_get_birg_liveness(birg));
+ be_liveness_invalidate(be_get_irg_liveness(irg));
/* Hack for now, something is buggy with invalidate liveness... */
- birg->lv = NULL;
- be_invalidate_dom_front(birg);
+ be_birg_from_irg(irg)->lv = NULL;
+ be_invalidate_dom_front(irg);
/* recalculate edges */
edges_deactivate(irg);
edges_activate(irg);
-
- if (birg->lv) {
- be_liveness_free(birg->lv);
- birg->lv = be_liveness(irg);
- }
}