X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fir%2Fircons.c;h=f38994b2827e0098a139b6e027f10b64c1934b22;hb=7d070c58e7bef1ed875fac066b57f29de449724b;hp=4b43f07f79753aeae0458ea6978da21c4e160e0c;hpb=e88385016800d3c56c3fa09770e9f7995c42e106;p=libfirm diff --git a/ir/ir/ircons.c b/ir/ir/ircons.c index 4b43f07f7..f38994b28 100644 --- a/ir/ir/ircons.c +++ b/ir/ir/ircons.c @@ -185,6 +185,42 @@ static inline ir_node *new_rd_Phi0(dbg_info *dbgi, ir_node *block, static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode); +static void try_remove_unnecessary_phi(ir_node *phi) +{ + ir_node *phi_value = NULL; + int arity = get_irn_arity(phi); + int i; + + /* see if all inputs are either pointing to a single value or + * are self references */ + for (i = 0; i < arity; ++i) { + ir_node *in = get_irn_n(phi, i); + if (in == phi) + continue; + if (in == phi_value) + continue; + /** found a different value from the one we already found, can't remove + * the phi (yet) */ + if (phi_value != NULL) + return; + phi_value = in; + } + if (phi_value == NULL) + return; + + /* if we're here then all phi inputs have been either phi_value + * or self-references, we can replace the phi by phi_value. + * We do this with an Id-node */ + exchange(phi, phi_value); + + /* recursively check phi_value, because it could be that we were the last + * phi-node in a loop-body. Then our arguments is an unnecessary phi in + * the loop header which can be eliminated now */ + if (is_Phi(phi_value)) { + try_remove_unnecessary_phi(phi_value); + } +} + /** * Computes the predecessors for the real phi node, and then * allocates and returns this node. The routine called to allocate the @@ -198,8 +234,6 @@ static ir_node *set_phi_arguments(ir_node *phi, int pos) int arity = get_irn_arity(block); ir_node **in = ALLOCAN(ir_node*, arity); ir_mode *mode = get_irn_mode(phi); - ir_node *phi_value = NULL; - bool no_phi_value = false; int i; /* This loop goes to all predecessor blocks of the block the Phi node @@ -211,35 +245,25 @@ static ir_node *set_phi_arguments(ir_node *phi, int pos) if (is_Bad(cfgpred)) { value = new_r_Bad(irg); } else { + inc_irg_visited(irg); + value = get_r_value_internal(cfgpred, pos, mode); } - if (!no_phi_value && value != phi) { - if (phi_value == NULL) { - phi_value = value; - } else if (value != phi_value) { - no_phi_value = true; - phi_value = NULL; - } - } in[i] = value; } - if (phi_value != NULL && !no_phi_value) { - exchange(phi, phi_value); - return phi_value; - } - phi->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity); set_irn_in(phi, arity, in); set_irn_op(phi, op_Phi); - phi = optimize_in_place_2(phi); irn_verify_irg(phi, irg); /* Memory Phis in endless loops must be kept alive. As we can't distinguish these easily we keep all of them alive. */ if (is_Phi(phi) && mode == mode_M) add_End_keepalive(get_irg_end(irg), phi); + + try_remove_unnecessary_phi(phi); return phi; } @@ -255,12 +279,19 @@ static ir_node *set_phi_arguments(ir_node *phi, int pos) */ static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode) { - ir_node *res = block->attr.block.graph_arr[pos]; + ir_node *res = block->attr.block.graph_arr[pos]; + ir_graph *irg = get_irn_irg(block); if (res != NULL) return res; - /* in a matured block we can immediated determine the phi arguments */ - if (block->attr.block.is_matured) { + /* We ran into a cycle. This may happen in unreachable loops. */ + if (irn_visited_else_mark(block)) { + /* Since the loop is unreachable, return a Bad. */ + return new_r_Bad(irg); + } + + /* in a matured block we can immediately determine the phi arguments */ + if (get_Block_matured(block)) { int arity = get_irn_arity(block); /* no predecessors: use unknown value */ if (arity == 0 && block == get_irg_start_block(get_irn_irg(block))) { @@ -335,7 +366,7 @@ void mature_immBlock(ir_node *block) } } - block->attr.block.is_matured = 1; + set_Block_matured(block, 1); /* Now, as the block is a finished Firm node, we can optimize it. Since other nodes have been allocated since the block was created @@ -435,9 +466,9 @@ ir_node *new_rd_DivRL(dbg_info *dbgi, ir_node *block, ir_node * irn_mem, ir_node in[2] = irn_right; res = new_ir_node(dbgi, irg, block, op_Div, mode_T, 3, in); - res->attr.divmod.resmode = resmode; - res->attr.divmod.no_remainder = 1; - res->attr.divmod.exc.pin_state = pin_state; + res->attr.div.resmode = resmode; + res->attr.div.no_remainder = 1; + res->attr.div.exc.pin_state = pin_state; res = optimize_node(res); irn_verify_irg(res, irg); return res; @@ -469,8 +500,7 @@ ir_node *new_rd_immBlock(dbg_info *dbgi, ir_graph *irg) /* creates a new dynamic in-array as length of in is -1 */ res = new_ir_node(dbgi, irg, NULL, op_Block, mode_BB, -1, NULL); - res->attr.block.is_matured = 0; - res->attr.block.is_dead = 0; + set_Block_matured(res, 0); res->attr.block.irg.irg = irg; res->attr.block.backedge = NULL; res->attr.block.in_cg = NULL; @@ -511,7 +541,7 @@ void add_immBlock_pred(ir_node *block, ir_node *jmp) int n = ARR_LEN(block->in) - 1; assert(is_Block(block) && "Error: Must be a Block"); - assert(!block->attr.block.is_matured && "Error: Block already matured!\n"); + assert(!get_Block_matured(block) && "Error: Block already matured!\n"); assert(is_ir_node(jmp)); ARR_APP1(ir_node *, block->in, jmp); @@ -521,11 +551,13 @@ void add_immBlock_pred(ir_node *block, ir_node *jmp) void set_cur_block(ir_node *target) { + assert(target == NULL || current_ir_graph == get_irn_irg(target)); current_ir_graph->current_block = target; } void set_r_cur_block(ir_graph *irg, ir_node *target) { + assert(target == NULL || irg == get_irn_irg(target)); irg->current_block = target; } @@ -543,6 +575,7 @@ ir_node *get_r_value(ir_graph *irg, int pos, ir_mode *mode) { assert(get_irg_phase_state(irg) == phase_building); assert(pos >= 0); + inc_irg_visited(irg); return get_r_value_internal(irg->current_block, pos + 1, mode); } @@ -562,9 +595,8 @@ static ir_mode *guess_recursively(ir_node *block, int pos) int n_preds; int i; - if (irn_visited(block)) + if (irn_visited_else_mark(block)) return NULL; - mark_irn_visited(block); /* already have a defintion -> we can simply look at its mode */ value = block->attr.block.graph_arr[pos]; @@ -623,12 +655,13 @@ void set_value(int pos, ir_node *value) int r_find_value(ir_graph *irg, ir_node *value) { - int i; + size_t i; ir_node *bl = irg->current_block; - for (i = ARR_LEN(bl->attr.block.graph_arr) - 1; i >= 1; --i) - if (bl->attr.block.graph_arr[i] == value) + for (i = ARR_LEN(bl->attr.block.graph_arr); i > 1;) { + if (bl->attr.block.graph_arr[--i] == value) return i - 1; + } return -1; } @@ -640,6 +673,7 @@ int find_value(ir_node *value) ir_node *get_r_store(ir_graph *irg) { assert(get_irg_phase_state(irg) == phase_building); + inc_irg_visited(irg); return get_r_value_internal(irg->current_block, 0, mode_M); } @@ -691,14 +725,10 @@ void set_store(ir_node *store) set_r_store(current_ir_graph, store); } -void r_keep_alive(ir_graph *irg, ir_node *ka) -{ - add_End_keepalive(get_irg_end(irg), ka); -} - void keep_alive(ir_node *ka) { - r_keep_alive(current_ir_graph, ka); + ir_graph *irg = get_irn_irg(ka); + add_End_keepalive(get_irg_end(irg), ka); } void ir_set_uninitialized_local_variable_func( @@ -714,8 +744,8 @@ void irg_finalize_cons(ir_graph *irg) void irp_finalize_cons(void) { - int i; - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { + size_t i, n; + for (i = 0, n = get_irp_n_irgs(); i < n; ++i) { irg_finalize_cons(get_irp_irg(i)); } irp->phase_state = phase_high;