Commit 94e9283b authored by Matthias Braun's avatar Matthias Braun
Browse files

merge common graph copying code; move dead code elimination into an own file

[r27271]
parent c40a8d84
......@@ -65,122 +65,6 @@ int optimize_graph_df(ir_graph *irg);
*/
ir_graph_pass_t *optimize_graph_df_pass(const char *name);
/** Performs dead node elimination by copying the ir graph to a new obstack.
*
* The major intention of this pass is to free memory occupied by
* dead nodes and outdated analyzes information. Further this
* function removes Bad predecessors from Blocks and the corresponding
* inputs to Phi nodes. This opens optimization potential for other
* optimizations. Further this phase reduces dead Block<->Jmp
* self-cycles to Bad nodes.
*
* Dead_node_elimination is only performed if options `optimize' and
* `opt_dead_node_elimination' are set. The graph may
* not be in state phase_building. The outs datasturcture is freed,
* the outs state set to outs_none. Backedge information is conserved.
* Removes old attributes of nodes. Sets link field to NULL.
* Callee information must be freed (irg_callee_info_none).
*
* @param irg The graph to be optimized.
*/
void dead_node_elimination(ir_graph *irg);
/**
* Creates an ir_graph pass for dead_node_elimination().
*
* @param name the name of this pass or NULL
*
* @return the newly created ir_graph pass
*/
ir_graph_pass_t *dead_node_elimination_pass(const char *name);
typedef struct _survive_dce_t survive_dce_t;
/**
* Make a new Survive DCE environment.
*/
survive_dce_t *new_survive_dce(void);
/**
* Free a Survive DCE environment.
*/
void free_survive_dce(survive_dce_t *sd);
/**
* Register a node pointer to be patched upon DCE.
* When DCE occurs, the node pointer specified by @p place will be
* patched to the new address of the node it is pointing to.
*
* @param sd The Survive DCE environment.
* @param place The address of the node pointer.
*/
void survive_dce_register_irn(survive_dce_t *sd, ir_node **place);
/** Inlines a method at the given call site.
*
* Removes the call node and splits the basic block the call node
* belongs to. Inserts a copy of the called graph between these nodes.
* Assumes that call is a Call node in current_ir_graph and that
* the type in the Call nodes type attribute is the same as the
* type of the called graph.
* Further it assumes that all Phi nodes in a block of current_ir_graph
* are assembled in a "link" list in the link field of the corresponding
* block nodes. Further assumes that all Proj nodes are in a "link" list
* in the nodes producing the tuple. (This is only an optical feature
* for the graph.) Conserves this feature for the old
* nodes of the graph. This precondition can be established by a call to
* collect_phisprojs(), see irgmod.h.
* As dead_node_elimination this function reduces dead Block<->Jmp
* self-cycles to Bad nodes.
*
* Called_graph must be unequal to current_ir_graph. Will not inline
* if they are equal.
* Sets visited masterflag in current_ir_graph to the max of the flag in
* current and called graph.
* Assumes that both, the called and the calling graph are in state
* "op_pin_state_pinned".
* It is recommended to call local_optimize_graph() after inlining as this
* function leaves a set of obscure Tuple nodes, e.g. a Proj-Tuple-Jmp
* combination as control flow operation.
*
* @param call the call node that should be inlined
* @param called_graph the IR-graph that is called at call
*
* @return zero if method could not be inlined (recursion for instance),
* non-zero if all went ok
*/
int inline_method(ir_node *call, ir_graph *called_graph);
/** Code Placement.
*
* Pins all floating nodes to a block where they
* will be executed only if needed. Depends on the flag opt_global_cse.
* Graph may not be in phase_building. Does not schedule control dead
* code. Uses dominator information which it computes if the irg is not
* in state dom_consistent. Destroys the out information as it moves nodes
* to other blocks. Optimizes Tuples in Control edges.
* @todo This is not tested!
*
* Call remove_critical_cf_edges() before place_code(). This normalizes
* the control flow graph so that for all operations a basic block exists
* where they can be optimally placed.
*
* @todo A more powerful code placement would move operations past Phi nodes
* out of loops.
*/
void place_code(ir_graph *irg);
/**
* Creates an ir_graph pass for place_code().
* This pass enables GCSE, runs optimize_graph_df() and finally
* place_code();
*
* @param name the name of this pass or NULL
*
* @return the newly created ir_graph pass
*/
ir_graph_pass_t *place_code_pass(const char *name);
/** Places an empty basic block on critical control flow edges thereby
* removing them.
*
......
......@@ -506,7 +506,7 @@ ir_graph_pass_t *remove_phi_cycles_pass(const char *name);
/** A default threshold. */
#define DEFAULT_CLONE_THRESHOLD 300
#define DEFAULT_CLONE_THRESHOLD 20
/**
* Do procedure cloning. Evaluate a heuristic weight for every
......@@ -978,4 +978,98 @@ void garbage_collect_entities(void);
/** Pass for garbage_collect_entities */
ir_prog_pass_t *garbage_collect_entities_pass(const char *name);
/** Performs dead node elimination by copying the ir graph to a new obstack.
*
* The major intention of this pass is to free memory occupied by
* dead nodes and outdated analyzes information. Further this
* function removes Bad predecessors from Blocks and the corresponding
* inputs to Phi nodes. This opens optimization potential for other
* optimizations. Further this phase reduces dead Block<->Jmp
* self-cycles to Bad nodes.
*
* Dead_node_elimination is only performed if options `optimize' and
* `opt_dead_node_elimination' are set. The graph may
* not be in state phase_building. The outs datasturcture is freed,
* the outs state set to outs_none. Backedge information is conserved.
* Removes old attributes of nodes. Sets link field to NULL.
* Callee information must be freed (irg_callee_info_none).
*
* @param irg The graph to be optimized.
*/
void dead_node_elimination(ir_graph *irg);
/**
* Creates an ir_graph pass for dead_node_elimination().
*
* @param name the name of this pass or NULL
*
* @return the newly created ir_graph pass
*/
ir_graph_pass_t *dead_node_elimination_pass(const char *name);
/** Inlines a method at the given call site.
*
* Removes the call node and splits the basic block the call node
* belongs to. Inserts a copy of the called graph between these nodes.
* Assumes that call is a Call node in current_ir_graph and that
* the type in the Call nodes type attribute is the same as the
* type of the called graph.
* Further it assumes that all Phi nodes in a block of current_ir_graph
* are assembled in a "link" list in the link field of the corresponding
* block nodes. Further assumes that all Proj nodes are in a "link" list
* in the nodes producing the tuple. (This is only an optical feature
* for the graph.) Conserves this feature for the old
* nodes of the graph. This precondition can be established by a call to
* collect_phisprojs(), see irgmod.h.
* As dead_node_elimination this function reduces dead Block<->Jmp
* self-cycles to Bad nodes.
*
* Called_graph must be unequal to current_ir_graph. Will not inline
* if they are equal.
* Sets visited masterflag in current_ir_graph to the max of the flag in
* current and called graph.
* Assumes that both, the called and the calling graph are in state
* "op_pin_state_pinned".
* It is recommended to call local_optimize_graph() after inlining as this
* function leaves a set of obscure Tuple nodes, e.g. a Proj-Tuple-Jmp
* combination as control flow operation.
*
* @param call the call node that should be inlined
* @param called_graph the IR-graph that is called at call
*
* @return zero if method could not be inlined (recursion for instance),
* non-zero if all went ok
*/
int inline_method(ir_node *call, ir_graph *called_graph);
/** Code Placement.
*
* Pins all floating nodes to a block where they
* will be executed only if needed. Depends on the flag opt_global_cse.
* Graph may not be in phase_building. Does not schedule control dead
* code. Uses dominator information which it computes if the irg is not
* in state dom_consistent. Destroys the out information as it moves nodes
* to other blocks. Optimizes Tuples in Control edges.
* @todo This is not tested!
*
* Call remove_critical_cf_edges() before place_code(). This normalizes
* the control flow graph so that for all operations a basic block exists
* where they can be optimally placed.
*
* @todo A more powerful code placement would move operations past Phi nodes
* out of loops.
*/
void place_code(ir_graph *irg);
/**
* Creates an ir_graph pass for place_code().
* This pass enables GCSE, runs optimize_graph_df() and finally
* place_code();
*
* @param name the name of this pass or NULL
*
* @return the newly created ir_graph pass
*/
ir_graph_pass_t *place_code_pass(const char *name);
#endif
......@@ -39,6 +39,7 @@
#include "irprintf_t.h"
#include "irgopt.h"
#include "irbitset.h"
#include "iropt_t.h"
#include "height.h"
#include "pdeq.h"
#include "irtools.h"
......
......@@ -30,7 +30,7 @@
#include "irprog.h"
#include "irprintf.h"
#include "ircons.h"
#include "irgmod.h"
#include "iroptimize.h"
#include "irgopt.h"
#include "irgwalk.h"
#include "iredges.h"
......
......@@ -33,15 +33,14 @@
#include "irtools.h"
#include "irprintf.h"
#include "irpass_t.h"
#include "iropt_t.h"
/* the famous clear_link implementation. */
void firm_clear_link(ir_node *n, void *env)
{
(void) env;
set_irn_link(n, NULL);
}
/* the famous clear_node_and_phi_links() implementation. */
void firm_clear_node_and_phi_links(ir_node *n, void *env)
{
(void) env;
......@@ -52,14 +51,6 @@ void firm_clear_node_and_phi_links(ir_node *n, void *env)
set_Phi_next(n, NULL);
}
/*
* Copies a node to a new irg. The Ins of the new node point to
* the predecessors on the old irg. n->link points to the new node.
*
* Does NOT copy standard nodes like Start, End etc that are fixed
* in an irg. Instead, the corresponding nodes of the new irg are returned.
* Note further, that the new nodes have no block.
*/
void copy_irn_to_irg(ir_node *n, ir_graph *irg)
{
ir_op *op = get_irn_op(n);
......@@ -125,41 +116,93 @@ void copy_irn_to_irg(ir_node *n, ir_graph *irg)
}
}
/*
* Creates an exact copy of a node.
* The copy resides in the same graph in the same block.
*/
ir_node *exact_copy(const ir_node *n)
ir_node *irn_copy_into_irg(const ir_node *node, ir_graph *irg)
{
ir_graph *irg = get_irn_irg(n);
ir_node *res, *block = NULL;
ir_node *block = NULL;
ir_op *op = get_irn_op(node);
int arity = get_irn_arity(node);
dbg_info *dbgi = get_irn_dbg_info(node);
ir_mode *mode = get_irn_mode(node);
ir_node *res;
int n_deps;
int i;
if (op != op_Block)
block = get_nodes_block(node);
if (op->opar == oparity_dynamic) {
int i;
res = new_ir_node(dbgi, irg, block, op, mode, -1, NULL);
for (i = 0; i < arity; ++i) {
ir_node *in = get_irn_n(node, i);
add_irn_n(res, in);
}
} else {
ir_node **ins = get_irn_in(node)+1;
res = new_ir_node(dbgi, irg, block, op, mode, arity, ins);
}
if (is_no_Block(n))
block = get_nodes_block(n);
/* copy the attributes */
copy_node_attr(irg, node, res);
if (op == op_Block) {
set_Block_MacroBlock(res, get_Block_MacroBlock(node));
}
res = new_ir_node(get_irn_dbg_info(n),
irg,
block,
get_irn_op(n),
get_irn_mode(n),
get_irn_arity(n),
get_irn_in(n) + 1);
/* duplicate dependency edges */
n_deps = get_irn_deps(node);
for (i = 0; i < n_deps; ++i) {
ir_node *dep = get_irn_dep(node, i);
add_irn_dep(res, dep);
}
return res;
}
/* Copy the attributes. These might point to additional data. If this
was allocated on the old obstack the pointers now are dangling. This
frees e.g. the memory of the graph_arr allocated in new_immBlock. */
copy_node_attr(irg, n, res);
ir_node *exact_copy(const ir_node *node)
{
return irn_copy_into_irg(node, get_irn_irg(node));
}
static ir_node *get_new_node(const ir_node *old_node)
{
return (ir_node*) get_irn_link(old_node);
}
void irn_rewire_inputs(ir_node *node)
{
ir_graph *new_irg;
ir_node *new_node;
int arity;
int i;
new_node = get_new_node(node);
if (is_Block(node)) {
/* copy the macro block header */
ir_node *mbh = get_Block_MacroBlock(node);
/* get the macro block header */
ir_node *nmbh = get_new_node(mbh);
assert(nmbh != NULL);
set_Block_MacroBlock(new_node, nmbh);
} else {
ir_node *block = get_nodes_block(node);
ir_node *new_block = get_new_node(block);
set_nodes_block(new_node, new_block);
}
if (is_Block(n)) {
set_Block_MacroBlock(res, get_Block_MacroBlock(n));
arity = get_irn_arity(new_node);
for (i = 0; i < arity; ++i) {
ir_node *in = get_irn_n(node, i);
ir_node *new_in = get_new_node(in);
set_irn_n(new_node, i, new_in);
}
return res;
/* Now the new node is complete. We can add it to the hash table for CSE. */
new_irg = get_irn_irg(new_node);
add_identities(new_irg->value_table, new_node);
}
/*
* Dump a pset containing Firm objects.
*/
void firm_pset_dump(pset *set)
{
void *obj;
......
......@@ -82,6 +82,33 @@ void firm_clear_link(ir_node *n, void *env);
void firm_clear_node_and_phi_links(ir_node *n, void *env);
/**
* Creates an exact copy of a node with same inputs and attributes in the
* same block.
*
* @param node the node to copy
*/
ir_node *exact_copy(const ir_node *node);
/**
* Create an exact copy of a node with same inputs and attributes in the same
* block but puts the node on a graph which might be different than the graph
* of the original node.
* Note: You have to fixup the inputs/block later
*/
ir_node *irn_copy_into_irg(const ir_node *node, ir_graph *irg);
/**
* This is a helper function used by some routines copying irg graphs
* This assumes that we have "old" nodes which have been copied to "new"
* nodes; The inputs of the new nodes still point to old nodes.
*
* Given an old(!) node this function rewires the matching new_node
* so that all its inputs point to new nodes afterwards.
*/
void irn_rewire_inputs(ir_node *node);
/**
* @deprecated
* Copies a node to a new irg. The Ins of the new node point to
* the predecessors on the old irg. n->link points to the new node.
*
......@@ -94,15 +121,4 @@ void firm_clear_node_and_phi_links(ir_node *n, void *env);
*/
void copy_irn_to_irg(ir_node *n, ir_graph *irg);
/**
* Creates an exact copy of a node.
* The copy resists on the same graph in the same block.
*
* @param n the node to copy
*
* @note If the copy is not changed, the next CSE operation will
* replace it by the original, so beware.
*/
ir_node *exact_copy(const ir_node *n);
#endif
......@@ -392,36 +392,17 @@ ir_graph *new_const_code_irg(void)
* @param n A node from the original method graph.
* @param env The copied graph.
*/
static void copy_all_nodes(ir_node *n, void *env)
static void copy_all_nodes(ir_node *node, void *env)
{
ir_graph *irg = env;
ir_op *op = get_irn_op(n);
ir_node *nn;
ir_graph *irg = env;
ir_node *new_node = irn_copy_into_irg(node, irg);
nn = new_ir_node(get_irn_dbg_info(n),
irg,
NULL, /* no block yet, will be set later */
op,
get_irn_mode(n),
get_irn_arity(n),
get_irn_in(n) + 1);
/* Copy the attributes. These might point to additional data. If this
was allocated on the old obstack the pointers now are dangling. This
frees e.g. the memory of the graph_arr allocated in new_immBlock. */
copy_node_attr(irg, n, nn);
set_irn_link(n, nn);
/* fix the irg for Blocks: as Bad nodes are NOT copied, no
need t fix them */
if (is_Block(nn))
nn->attr.block.irg.irg = irg;
set_irn_link(node, new_node);
/* fix access to entities on the stack frame */
if (is_Sel(nn)) {
ir_entity *ent = get_Sel_entity(nn);
ir_type *tp = get_entity_owner(ent);
if (is_Sel(new_node)) {
ir_entity *ent = get_Sel_entity(new_node);
ir_type *tp = get_entity_owner(ent);
if (is_frame_type(tp)) {
/* replace by the copied entity */
......@@ -429,7 +410,7 @@ static void copy_all_nodes(ir_node *n, void *env)
assert(is_entity(ent));
assert(get_entity_owner(ent) == get_irg_frame_type(irg));
set_Sel_entity(nn, ent);
set_Sel_entity(new_node, ent);
}
}
}
......@@ -439,32 +420,16 @@ static void copy_all_nodes(ir_node *n, void *env)
* The copied nodes are set as link of their original nodes. The links of
* "irn" predecessors are the predecessors of copied node.
*/
static void set_all_preds(ir_node *irn, void *env)
static void rewire(ir_node *irn, void *env)
{
int i;
ir_node *nn, *pred;
(void) env;
nn = get_irn_link(irn);
if (is_Block(irn)) {
ir_node *mbh = get_Block_MacroBlock(irn);
set_Block_MacroBlock(nn, get_irn_link(mbh));
for (i = get_Block_n_cfgpreds(irn) - 1; i >= 0; i--) {
pred = get_Block_cfgpred(irn, i);
set_Block_cfgpred(nn, i, get_irn_link(pred));
}
} else {
/* First we set the block our copy if it is not a block.*/
set_nodes_block(nn, get_irn_link(get_nodes_block(irn)));
for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
pred = get_irn_n(irn, i);
set_irn_n(nn, i, get_irn_link(pred));
}
}
irn_rewire_inputs(irn);
}
#define NN(irn) get_irn_link(irn)
static ir_node *get_new_node(const ir_node *old_node)
{
return (ir_node*) get_irn_link(old_node);
}
/*
* Create a new graph that is a copy of a given one.
......@@ -500,25 +465,25 @@ ir_graph *create_irg_copy(ir_graph *irg)
ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
/* copy all nodes from the graph irg to the new graph res */
irg_walk_anchors(irg, copy_all_nodes, set_all_preds, res);
irg_walk_anchors(irg, copy_all_nodes, rewire, res);
/* copy the Anchor node */
res->anchor = NN(irg->anchor);
res->anchor = get_new_node(irg->anchor);
/* -- The end block -- */
set_irg_end_block (res, NN(get_irg_end_block(irg)));
set_irg_end (res, NN(get_irg_end(irg)));
set_irg_end_reg (res, NN(get_irg_end_reg(irg)));
set_irg_end_except(res, NN(get_irg_end_except(irg)));
set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
set_irg_end (res, get_new_node(get_irg_end(irg)));
set_irg_end_reg (res, get_new_node(get_irg_end_reg(irg)));
set_irg_end_except(res, get_new_node(get_irg_end_except(irg)));
/* -- The start block -- */
set_irg_start_block(res, NN(get_irg_start_block(irg)));
set_irg_bad (res, NN(get_irg_bad(irg)));
set_irg_no_mem (res, NN(get_irg_no_mem(irg)));
set_irg_start (res, NN(get_irg_start(irg)));
set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
set_irg_bad (res, get_new_node(get_irg_bad(irg)));
set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
set_irg_start (res, get_new_node(get_irg_start(irg)));
/* Proj results of start node */
set_irg_initial_mem(res, NN(get_irg_initial_mem(irg)));
set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
/* Copy the node count estimation. Would be strange if this
is different from the original one. */
......@@ -529,8 +494,6 @@ ir_graph *create_irg_copy(ir_graph *irg)
return res;
}
#undef NN
/* Frees the passed irgraph.
Deallocates all nodes in this graph and the ir_graph structure.
......
......@@ -137,4 +137,31 @@ ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops);
*/
bool is_negated_value(ir_node *a, ir_node *b);
/** NOTE: Survive DCE is considered a bad hack - don't use */
typedef struct _survive_dce_t survive_dce_t;
/**
* Make a new Survive DCE environment.
* NOTE: Survive DCE is considered a bad hack - don't use
*/
survive_dce_t *new_survive_dce(void);
/**
* Free a Survive DCE environment.
* NOTE: Survive DCE is considered a bad hack - don't use
*/
void free_survive_dce(survive_dce_t *sd);
/**
* Register a node pointer to be patched upon DCE.
* When DCE occurs, the node pointer specified by @p place will be
* patched to the new address of the node it is pointing to.
*
* @param sd The Survive DCE environment.
* @param place The address of the node pointer.
*/
void survive_dce_register_irn(survive_dce_t *sd, ir_node **place);
#endif
/*
* Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software