X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fldstopt.c;h=3517964c3f487e1a2f6913d706c2fae6079bddef;hb=cc273381cbd00c4afddb1d1626b80d5cd3a636be;hp=ab8205fe6dc54bef717f68401cc3504f2584c569;hpb=5efc71acbf0d5c24037e15c4bfbc4bcd617ae50f;p=libfirm diff --git a/ir/opt/ldstopt.c b/ir/opt/ldstopt.c index ab8205fe6..3517964c3 100644 --- a/ir/opt/ldstopt.c +++ b/ir/opt/ldstopt.c @@ -76,6 +76,7 @@ typedef struct _ldst_info_t { ir_node *exc_block; /**< the exception block if available */ int exc_idx; /**< predecessor index in the exception block */ unsigned flags; /**< flags */ + unsigned visited; /**< visited counter for breaking loops */ } ldst_info_t; /** @@ -93,6 +94,13 @@ typedef struct _block_info_t { unsigned flags; /**< flags for the block */ } block_info_t; +/** the master visited flag for loop detection. */ +static unsigned master_visited = 0; + +#define INC_MASTER() ++master_visited +#define MARK_NODE(info) (info)->visited = master_visited +#define NODE_VISITED(info) (info)->visited >= master_visited + /** * get the Load/Store info of a node */ @@ -506,7 +514,7 @@ static unsigned optimize_load(ir_node *load) /* no result :-) */ if (info->projs[pn_Load_res]) { if (is_atomic_entity(ent)) { - ir_node *c = copy_const_value(get_atomic_ent_value(ent)); + ir_node *c = copy_const_value(get_irn_dbg_info(load), get_atomic_ent_value(ent)); DBG_OPT_RC(load, c); exchange(info->projs[pn_Load_res], c); @@ -544,7 +552,7 @@ static unsigned optimize_load(ir_node *load) res |= DF_CHANGED; } if (info->projs[pn_Load_res]) { - exchange(info->projs[pn_Load_res], copy_const_value(c)); + exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c)); return res | DF_CHANGED; } } @@ -567,10 +575,17 @@ static unsigned optimize_load(ir_node *load) if (get_irn_out_n(ptr) <= 1) return res; - /* follow the memory chain as long as there are only Loads - * and try to replace current Load or Store by a previous one + /* + * follow the memory chain as long as there are only Loads + * and try to replace current Load or Store by a previous one. + * Note that in unreachable loops it might happen that we reach + * load again, as well as we can fall into a cycle. + * We break such cycles using a special visited flag. */ - for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) { + INC_MASTER(); + for (pred = skip_Proj(mem); load != pred; pred = skip_Proj(get_Load_mem(pred))) { + ldst_info_t *pred_info = get_irn_link(pred); + /* * BEWARE: one might think that checking the modes is useless, because * if the pointers are identical, they refer to the same object. @@ -580,8 +595,6 @@ static unsigned optimize_load(ir_node *load) if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr && get_irn_mode(get_Store_value(pred)) == load_mode) { - ldst_info_t *pred_info = get_irn_link(pred); - /* * a Load immediately after a Store -- a read after write. * We may remove the Load, if both Load & Store does not have an exception handler @@ -626,8 +639,6 @@ static unsigned optimize_load(ir_node *load) * hander because they would have exact the same exception... */ if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) { - ldst_info_t *pred_info = get_irn_link(pred); - DBG_OPT_RAR(load, pred); if (pred_info->projs[pn_Load_res]) { @@ -664,7 +675,12 @@ static unsigned optimize_load(ir_node *load) /* follow only Load chains */ if (get_irn_op(pred) != op_Load) break; - } + + /* check for cycles */ + if (NODE_VISITED(pred_info)) + break; + MARK_NODE(pred_info); + } return res; } @@ -701,7 +717,8 @@ static unsigned optimize_store(ir_node *store) mode = get_irn_mode(value); /* follow the memory chain as long as there are only Loads */ - for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) { + INC_MASTER(); + for (pred = skip_Proj(mem); pred != store; pred = skip_Proj(get_Load_mem(pred))) { ldst_info_t *pred_info = get_irn_link(pred); if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr && @@ -734,22 +751,29 @@ static unsigned optimize_store(ir_node *store) /* follow only Load chains */ if (get_irn_op(pred) != op_Load) break; + + /* check for cycles */ + if (NODE_VISITED(pred_info)) + break; + MARK_NODE(pred_info); } return res; } /** - * walker, optimizes Phi after Stores: + * walker, optimizes Phi after Stores to identical places: * Does the following optimization: + * @verbatim * * val1 val2 val3 val1 val2 val3 * | | | \ | / * Str Str Str \ | / - * \ | / Phi + * \ | / PhiData * \ | / | * \ | / Str - * Phi + * PhiM * + * @endverbatim * This reduces the number of stores and allows for predicated execution. * Moves Stores back to the end of a function which may be bad. * @@ -802,7 +826,7 @@ static unsigned optimize_phi(ir_node *phi, void *env) if (get_irn_op(pred) != op_Store) return 0; - if (mode != get_irn_mode(get_Store_value(pred)) || ptr != get_Store_ptr(pred)) + if (ptr != get_Store_ptr(pred) || mode != get_irn_mode(get_Store_value(pred))) return 0; info = get_irn_link(pred); @@ -823,17 +847,17 @@ static unsigned optimize_phi(ir_node *phi, void *env) /* * ok, when we are here, we found all predecessors of a Phi that - * are Stores to the same address. That means whatever we do before - * we enter the block of the Phi, we do a Store. - * So, we can move the store to the current block: + * are Stores to the same address and size. That means whatever + * we do before we enter the block of the Phi, we do a Store. + * So, we can move the Store to the current block: * * val1 val2 val3 val1 val2 val3 * | | | \ | / * | Str | | Str | | Str | \ | / - * \ | / Phi + * \ | / PhiData * \ | / | * \ | / Str - * Phi + * PhiM * * Is only allowed if the predecessor blocks have only one successor. */ @@ -896,7 +920,7 @@ static unsigned optimize_phi(ir_node *phi, void *env) } /** - * walker, collects all Load/Store/Proj nodes + * walker, do the optiimizations */ static void do_load_store_optimize(ir_node *n, void *env) { @@ -938,6 +962,7 @@ void optimize_load_store(ir_graph *irg) env.changes = 0; /* init the links, then collect Loads/Stores/Proj's in lists */ + master_visited = 0; irg_walk_graph(irg, firm_clear_link, collect_nodes, &env); /* now we have collected enough information, optimize */