fixed svn properties
[libfirm] / ir / opt / ldstopt.c
index b660daa..b2db657 100644 (file)
@@ -356,7 +356,7 @@ static ir_entity *find_constant_entity(ir_node *ptr)
  */
 static long get_Sel_array_index_long(ir_node *n, int dim) {
        ir_node *index = get_Sel_index(n, dim);
-       assert(get_irn_op(index) == op_Const);
+       assert(is_Const(index));
        return get_tarval_long(get_Const_tarval(index));
 }  /* get_Sel_array_index_long */
 
@@ -426,7 +426,8 @@ static void handle_load_update(ir_node *load) {
 
                /* a Load which value is neither used nor exception checked, remove it */
                exchange(info->projs[pn_Load_M], mem);
-               exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
+               if (info->projs[pn_Load_X_regular])
+                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
                exchange(load, new_Bad());
                reduce_adr_usage(ptr);
        }
@@ -757,79 +758,80 @@ static unsigned optimize_load(ir_node *load)
                                res |= CF_CHANGED;
                        }
 
-                       if (variability_constant == get_entity_variability(ent)
-                               && is_atomic_entity(ent)) {
-                               /* Might not be atomic after
-                                  lowering of Sels.  In this
-                                  case we could also load, but
-                                  it's more complicated. */
-                               /* more simpler case: we load the content of a constant value:
-                                * replace it by the constant itself
-                                */
-
-                               /* no memory */
-                               if (info->projs[pn_Load_M]) {
-                                       exchange(info->projs[pn_Load_M], mem);
-                                       res |= DF_CHANGED;
-                               }
-                               /* no result :-) */
-                               if (info->projs[pn_Load_res]) {
-                                       if (is_atomic_entity(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);
-                                               res |= DF_CHANGED;
-                                       }
-                               }
-                               exchange(load, new_Bad());
-                               reduce_adr_usage(ptr);
-                               return res;
-                       } else if (variability_constant == get_entity_variability(ent)) {
-                               compound_graph_path *path = get_accessed_path(ptr);
-
-                               if (path) {
-                                       ir_node *c;
-
-                                       assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
-                                       /*
-                                       {
-                                               int j;
-                                               for (j = 0; j < get_compound_graph_path_length(path); ++j) {
-                                                       ir_entity *node = get_compound_graph_path_node(path, j);
-                                                       fprintf(stdout, ".%s", get_entity_name(node));
-                                                       if (is_Array_type(get_entity_owner(node)))
-                                                               fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
-                                               }
-                                               printf("\n");
-                                       }
-                                       */
-
-                                       c = get_compound_ent_value_by_path(ent, path);
-                                       free_compound_graph_path(path);
-
-                                       /* printf("  cons: "); DDMN(c); */
-
+                       if (variability_constant == get_entity_variability(ent)) {
+                               if (is_atomic_entity(ent)) {
+                                       /* Might not be atomic after
+                                          lowering of Sels.  In this
+                                          case we could also load, but
+                                          it's more complicated. */
+                                       /* more simpler case: we load the content of a constant value:
+                                        * replace it by the constant itself
+                                        */
+
+                                       /* no memory */
                                        if (info->projs[pn_Load_M]) {
                                                exchange(info->projs[pn_Load_M], mem);
                                                res |= DF_CHANGED;
                                        }
+                                       /* no result :-) */
                                        if (info->projs[pn_Load_res]) {
-                                               exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c));
-                                               res |= DF_CHANGED;
+                                               if (is_atomic_entity(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);
+                                                       res |= DF_CHANGED;
+                                               }
                                        }
                                        exchange(load, new_Bad());
                                        reduce_adr_usage(ptr);
                                        return res;
                                } else {
-                                       /*  We can not determine a correct access path.  E.g., in jack, we load
-                                       a byte from an object to generate an exception.   Happens in test program
-                                       Reflectiontest.
-                                       printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
-                                       get_entity_name(get_irg_entity(current_ir_graph)));
-                                       printf("  load: "); DDMN(load);
-                                       printf("  ptr:  "); DDMN(ptr);
-                                       */
+                                       compound_graph_path *path = get_accessed_path(ptr);
+
+                                       if (path) {
+                                               ir_node *c;
+
+                                               assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
+                                               /*
+                                               {
+                                                       int j;
+                                                       for (j = 0; j < get_compound_graph_path_length(path); ++j) {
+                                                               ir_entity *node = get_compound_graph_path_node(path, j);
+                                                               fprintf(stdout, ".%s", get_entity_name(node));
+                                                               if (is_Array_type(get_entity_owner(node)))
+                                                                       fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
+                                                       }
+                                                       printf("\n");
+                                               }
+                                               */
+
+                                               c = get_compound_ent_value_by_path(ent, path);
+                                               free_compound_graph_path(path);
+
+                                               /* printf("  cons: "); DDMN(c); */
+
+                                               if (info->projs[pn_Load_M]) {
+                                                       exchange(info->projs[pn_Load_M], mem);
+                                                       res |= DF_CHANGED;
+                                               }
+                                               if (info->projs[pn_Load_res]) {
+                                                       exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c));
+                                                       res |= DF_CHANGED;
+                                               }
+                                               exchange(load, new_Bad());
+                                               reduce_adr_usage(ptr);
+                                               return res;
+                                       } else {
+                                               /*  We can not determine a correct access path.  E.g., in jack, we load
+                                               a byte from an object to generate an exception.   Happens in test program
+                                               Reflectiontest.
+                                               printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
+                                               get_entity_name(get_irg_entity(current_ir_graph)));
+                                               printf("  load: "); DDMN(load);
+                                               printf("  ptr:  "); DDMN(ptr);
+                                               */
+                                       }
                                }
                        }
                }
@@ -1316,9 +1318,11 @@ static void move_loads_in_loops(scc *pscc, loop_env *env) {
                next = ne->next;
 
                /* check all memory Phi's */
-               if (! is_Phi(phi) || get_irn_mode(phi) != mode_M)
+               if (! is_Phi(phi))
                        continue;
 
+               assert(get_irn_mode(phi) == mode_M && "DFS geturn non-memory Phi");
+
                for (j = get_irn_arity(phi) - 1; j >= 0; --j) {
                        ir_node    *pred = get_irn_n(phi, j);
                        node_entry *pe   = get_irn_ne(pred, env);
@@ -1334,6 +1338,8 @@ static void move_loads_in_loops(scc *pscc, loop_env *env) {
                        }
                }
        }
+       /* no Phis no fun */
+       assert(phi_list != NULL && "DFS found a loop without Phi");
 
        for (load = pscc->head; load; load = next) {
                ir_mode *load_mode;
@@ -1352,13 +1358,7 @@ static void move_loads_in_loops(scc *pscc, loop_env *env) {
                        if (! is_SymConst(ptr) || get_SymConst_kind(ptr) != symconst_addr_ent)
                                continue;
                        ent = get_SymConst_entity(ptr);
-
                        load_mode = get_Load_mode(load);
-                       if (get_entity_address_taken(ent) == ir_address_not_taken) {
-                               /* Shortcut: If the addres is never taken, this address if complete alias free*/
-                               goto can_move;
-                       }
-
                        for (other = pscc->head; other != NULL; other = next_other) {
                                node_entry *ne = get_irn_ne(other, env);
                                next_other = ne->next;
@@ -1378,7 +1378,7 @@ static void move_loads_in_loops(scc *pscc, loop_env *env) {
                                ldst_info_t *ninfo;
                                phi_entry   *pe;
                                dbg_info    *db;
-can_move:
+
                                /* for now, we cannot handle more than one input */
                                if (phi_list->next != NULL)
                                        return;
@@ -1395,7 +1395,7 @@ can_move:
                                        ir_node *irn, *mem;
 
                                        pe->load = irn = new_rd_Load(db, current_ir_graph, pred, get_Phi_pred(phi, pos), ptr, load_mode);
-                                       ninfo = get_ldst_info(pred, phase_obst(&env->ph));
+                                       ninfo = get_ldst_info(irn, phase_obst(&env->ph));
 
                                        ninfo->projs[pn_Load_M] = mem = new_r_Proj(current_ir_graph, pred, irn, mode_M, pn_Load_M);
                                        set_Phi_pred(phi, pos, mem);
@@ -1565,6 +1565,7 @@ static void process_scc(scc *pscc, loop_env *env) {
                process_loop(pscc, env);
        }
 }  /* process_scc */
+
 /**
  * Do Tarjan's SCC algorithm and drive load/store optimization.
  *
@@ -1578,27 +1579,12 @@ static void dfs(ir_node *irn, loop_env *env)
 
        mark_irn_visited(irn);
 
-       /* do not put blocks into the scc */
-       if (is_Block(irn)) {
-               n = get_irn_arity(irn);
-               for (i = 0; i < n; ++i) {
-                       ir_node *pred = get_irn_n(irn, i);
-
-                       if (irn_not_visited(pred))
-                               dfs(pred, env);
-               }
-       }
-       else {
-               ir_node *block = get_nodes_block(irn);
-
-               node->DFSnum = env->nextDFSnum++;
-               node->low    = node->DFSnum;
-               push(env, irn);
-
-               /* handle the block */
-               if (irn_not_visited(block))
-                       dfs(block, env);
+       node->DFSnum = env->nextDFSnum++;
+       node->low    = node->DFSnum;
+       push(env, irn);
 
+       /* handle preds */
+       if (is_Phi(irn) || is_Sync(irn)) {
                n = get_irn_arity(irn);
                for (i = 0; i < n; ++i) {
                        ir_node *pred = get_irn_n(irn, i);
@@ -1611,49 +1597,86 @@ static void dfs(ir_node *irn, loop_env *env)
                        if (o->DFSnum < node->DFSnum && o->in_stack)
                                node->low = MIN(o->DFSnum, node->low);
                }
-               if (node->low == node->DFSnum) {
-                       scc *pscc = phase_alloc(&env->ph, sizeof(*pscc));
-                       ir_node *x;
-
-                       pscc->head = NULL;
-                       do {
-                               node_entry *e;
-
-                               x = pop(env);
-                               e = get_irn_ne(x, env);
-                               e->pscc    = pscc;
-                               e->next    = pscc->head;
-                               pscc->head = x;
-                       } while (x != irn);
-
-                       process_scc(pscc, env);
+       } else if (is_fragile_op(irn)) {
+               ir_node *pred = get_fragile_op_mem(irn);
+               node_entry *o = get_irn_ne(pred, env);
+
+               if (irn_not_visited(pred)) {
+                       dfs(pred, env);
+                       node->low = MIN(node->low, o->low);
+               }
+               if (o->DFSnum < node->DFSnum && o->in_stack)
+                       node->low = MIN(o->DFSnum, node->low);
+       } else if (is_Proj(irn)) {
+               ir_node *pred = get_Proj_pred(irn);
+               node_entry *o = get_irn_ne(pred, env);
+
+               if (irn_not_visited(pred)) {
+                       dfs(pred, env);
+                       node->low = MIN(node->low, o->low);
                }
+               if (o->DFSnum < node->DFSnum && o->in_stack)
+                       node->low = MIN(o->DFSnum, node->low);
+       }
+       else {
+                /* IGNORE predecessors */
+       }
+
+       if (node->low == node->DFSnum) {
+               scc *pscc = phase_alloc(&env->ph, sizeof(*pscc));
+               ir_node *x;
+
+               pscc->head = NULL;
+               do {
+                       node_entry *e;
+
+                       x = pop(env);
+                       e = get_irn_ne(x, env);
+                       e->pscc    = pscc;
+                       e->next    = pscc->head;
+                       pscc->head = x;
+               } while (x != irn);
+
+               process_scc(pscc, env);
        }
 }  /* dfs */
 
 /**
- * Do the DFS by starting at the End node of a graph.
+ * Do the DFS on the memory edges a graph.
  *
  * @param irg  the graph to process
  * @param env  the loop environment
  */
 static void do_dfs(ir_graph *irg, loop_env *env) {
        ir_graph *rem = current_ir_graph;
-       ir_node *end = get_irg_end(irg);
-       int i, n;
+       ir_node  *endblk, *end;
+       int      i;
 
        current_ir_graph = irg;
        inc_irg_visited(irg);
 
-       /* visit all visible nodes */
-       dfs(end, env);
+       /* visit all memory nodes */
+       endblk = get_irg_end_block(irg);
+       for (i = get_Block_n_cfgpreds(endblk) - 1; i >= 0; --i) {
+               ir_node *pred = get_Block_cfgpred(endblk, i);
+
+               if (is_Return(pred))
+                       dfs(get_Return_mem(pred), env);
+               else if (is_Raise(pred))
+                       dfs(get_Raise_mem(pred), env);
+               else if (is_fragile_op(pred))
+                       dfs(get_fragile_op_mem(pred), env);
+               else {
+                       assert(0 && "Unknown EndBlock predecessor");
+               }
+       }
 
        /* visit the keep-alives */
-       n = get_End_n_keepalives(end);
-       for (i = 0; i < n; ++i) {
+       end = get_irg_end(irg);
+       for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
                ir_node *ka = get_End_keepalive(end, i);
 
-               if (irn_not_visited(ka))
+               if (is_Phi(ka) && irn_not_visited(ka))
                        dfs(ka, env);
        }
        current_ir_graph = rem;
@@ -1700,7 +1723,6 @@ void optimize_load_store(ir_graph *irg) {
        walk_env_t env;
 
        FIRM_DBG_REGISTER(dbg, "firm.opt.ldstopt");
-       firm_dbg_set_mask(dbg, SET_LEVEL_1);
 
        assert(get_irg_phase_state(irg) != phase_building);
        assert(get_irg_pinned(irg) != op_pin_state_floats &&
@@ -1714,9 +1736,6 @@ void optimize_load_store(ir_graph *irg) {
 
        edges_assure(irg);
 
-       /* loop optimizations need dominators ... */
-       assure_doms(irg);
-
        /* for Phi optimization post-dominators are needed ... */
        assure_postdoms(irg);