Make ia32_get_admissible_noreg() static.
[libfirm] / ir / be / ia32 / ia32_optimize.c
index 0a9ba1c..719c158 100644 (file)
@@ -62,6 +62,16 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 static const arch_env_t *arch_env;
 static ia32_code_gen_t  *cg;
 
+static void copy_mark(const ir_node *old, ir_node *new)
+{
+       if (is_ia32_is_reload(old))
+               set_ia32_is_reload(new);
+       if (is_ia32_is_spill(old))
+               set_ia32_is_spill(new);
+       if (is_ia32_is_remat(old))
+               set_ia32_is_remat(new);
+}
+
 /**
  * Returns non-zero if the given node produces
  * a zero flag.
@@ -97,17 +107,17 @@ static int produces_zero_flag(ir_node *node, int pn)
 
        case iro_ia32_ShlD:
        case iro_ia32_ShrD:
+               assert(n_ia32_ShlD_count == n_ia32_ShrD_count);
+               count = get_irn_n(node, n_ia32_ShlD_count);
+               goto check_shift_amount;
+
        case iro_ia32_Shl:
        case iro_ia32_Shr:
        case iro_ia32_Sar:
-               assert(n_ia32_ShlD_count == n_ia32_ShrD_count);
                assert(n_ia32_Shl_count == n_ia32_Shr_count
                                && n_ia32_Shl_count == n_ia32_Sar_count);
-               if (is_ia32_ShlD(node) || is_ia32_ShrD(node)) {
-                       count = get_irn_n(node, n_ia32_ShlD_count);
-               } else {
-                       count = get_irn_n(node, n_ia32_Shl_count);
-               }
+               count = get_irn_n(node, n_ia32_Shl_count);
+check_shift_amount:
                /* when shift count is zero the flags are not affected, so we can only
                 * do this for constants != 0 */
                if (!is_ia32_Immediate(count))
@@ -222,6 +232,7 @@ static void peephole_ia32_Cmp(ir_node *const node)
        }
 
        sched_add_before(node, test);
+       copy_mark(node, test);
        be_peephole_exchange(node, test);
 }
 
@@ -331,9 +342,7 @@ static void peephole_ia32_Return(ir_node *node) {
                }
        }
 
-       /* ensure, that the 3 byte return is generated
-        * actually the emitter tests again if the block beginning has a label and
-        * isn't just a fallthrough */
+       /* ensure, that the 3 byte return is generated */
        be_Return_set_emit_pop(node, 1);
 }
 
@@ -441,6 +450,7 @@ static void peephole_IncSP_Store_to_push(ir_node *irn)
                spreg = arch_get_irn_register(cg->arch_env, curr_sp);
 
                push = new_rd_ia32_Push(get_irn_dbg_info(store), irg, block, noreg, noreg, mem, val, curr_sp);
+               copy_mark(store, push);
 
                if (first_push == NULL)
                        first_push = push;
@@ -473,6 +483,90 @@ static void peephole_IncSP_Store_to_push(ir_node *irn)
        be_set_IncSP_offset(irn, inc_ofs);
 }
 
+#if 0
+static void peephole_store_incsp(ir_node *store)
+{
+       dbg_info *dbgi;
+       ir_node  *node;
+       ir_node  *block;
+       ir_node  *noref;
+       ir_node  *mem;
+       ir_node  *push;
+       ir_node  *val;
+       ir_node  *am_base = get_irn_n(store, n_ia32_Store_base);
+       if (!be_is_IncSP(am_base)
+                       || get_nodes_block(am_base) != get_nodes_block(store))
+               return;
+       mem = get_irn_n(store, n_ia32_Store_mem);
+       if (!is_ia32_NoReg_GP(get_irn_n(store, n_ia32_Store_index))
+                       || !is_NoMem(mem))
+               return;
+
+       int incsp_offset = be_get_IncSP_offset(am_base);
+       if (incsp_offset <= 0)
+               return;
+
+       /* we have to be at offset 0 */
+       int my_offset = get_ia32_am_offs_int(store);
+       if (my_offset != 0) {
+               /* TODO here: find out wether there is a store with offset 0 before
+                * us and wether we can move it down to our place */
+               return;
+       }
+       ir_mode *ls_mode = get_ia32_ls_mode(store);
+       int my_store_size = get_mode_size_bytes(ls_mode);
+
+       if (my_offset + my_store_size > incsp_offset)
+               return;
+
+       /* correctness checking:
+               - noone else must write to that stackslot
+                   (because after translation incsp won't allocate it anymore)
+       */
+       sched_foreach_reverse_from(store, node) {
+               int i, arity;
+
+               if (node == am_base)
+                       break;
+
+               /* make sure noone else can use the space on the stack */
+               arity = get_irn_arity(node);
+               for (i = 0; i < arity; ++i) {
+                       ir_node *pred = get_irn_n(node, i);
+                       if (pred != am_base)
+                               continue;
+
+                       if (i == n_ia32_base &&
+                                       (get_ia32_op_type(node) == ia32_AddrModeS
+                                        || get_ia32_op_type(node) == ia32_AddrModeD)) {
+                               int      node_offset  = get_ia32_am_offs_int(node);
+                               ir_mode *node_ls_mode = get_ia32_ls_mode(node);
+                               int      node_size    = get_mode_size_bytes(node);
+                               /* overlapping with our position? abort */
+                               if (node_offset < my_offset + my_store_size
+                                               && node_offset + node_size >= my_offset)
+                                       return;
+                               /* otherwise it's fine */
+                               continue;
+                       }
+
+                       /* strange use of esp: abort */
+                       return;
+               }
+       }
+
+       /* all ok, change to push */
+       dbgi  = get_irn_dbg_info(store);
+       block = get_nodes_block(store);
+       noreg = ia32_new_NoReg_gp(cg);
+       val   = get_ia32_
+
+       push  = new_rd_ia32_Push(dbgi, irg, block, noreg, noreg, mem,
+
+       create_push(dbgi, current_ir_graph, block, am_base, store);
+}
+#endif
+
 /**
  * Return true if a mode can be stored in the GP register set
  */
@@ -515,7 +609,6 @@ static void peephole_Load_IncSP_to_pop(ir_node *irn)
        maxslot = -1;
        pred_sp = be_get_IncSP_pred(irn);
        for (node = sched_prev(irn); !sched_is_end(node); node = sched_prev(node)) {
-               ir_node *mem;
                int offset;
                int loadslot;
                const arch_register_t *sreg, *dreg;
@@ -554,13 +647,9 @@ static void peephole_Load_IncSP_to_pop(ir_node *irn)
                         * but we do not check this */
                        break;
                }
-               /* Load has to be attached to Spill-Mem */
-               mem = skip_Proj(get_irn_n(node, n_ia32_mem));
-               if (!is_Phi(mem) && !is_ia32_Store(mem) && !is_ia32_Push(mem))
-                       break;
 
                /* should have NO index */
-               if (get_ia32_am_scale(node) > 0 || !is_ia32_NoReg_GP(get_irn_n(node, n_ia32_index)))
+               if (!is_ia32_NoReg_GP(get_irn_n(node, n_ia32_index)))
                        break;
 
                offset = get_ia32_am_offs_int(node);
@@ -629,6 +718,8 @@ static void peephole_Load_IncSP_to_pop(ir_node *irn)
                pop = new_rd_ia32_Pop(get_irn_dbg_info(load), irg, block, mem, pred_sp);
                arch_set_irn_register(arch_env, pop, reg);
 
+               copy_mark(load, pop);
+
                /* create stackpointer Proj */
                pred_sp = new_r_Proj(irg, block, pop, mode_Iu, pn_ia32_Pop_stack);
                arch_set_irn_register(arch_env, pred_sp, esp);
@@ -840,6 +931,7 @@ static void peephole_ia32_Const(ir_node *node)
        sched_add_before(node, produceval);
        sched_add_before(node, xor);
 
+       copy_mark(node, xor);
        be_peephole_exchange(node, xor);
 }
 
@@ -1046,6 +1138,7 @@ exchange:
 
        /* exchange the Add and the LEA */
        sched_add_before(node, res);
+       copy_mark(node, res);
        be_peephole_exchange(node, res);
 }
 
@@ -1078,18 +1171,14 @@ static void peephole_ia32_Imul_split(ir_node *imul) {
        load = new_rd_ia32_Load(dbgi, irg, block, base, index, mem);
 
        /* copy all attributes */
-       set_irn_pinned(load, get_irn_pinned(imul));
-       set_ia32_op_type(load, ia32_AddrModeS);
-       set_ia32_ls_mode(load, get_ia32_ls_mode(imul));
-
-       set_ia32_am_scale(load, get_ia32_am_scale(imul));
-       set_ia32_am_sc(load, get_ia32_am_sc(imul));
-       set_ia32_am_offs_int(load, get_ia32_am_offs_int(imul));
-       if (is_ia32_am_sc_sign(imul))
-               set_ia32_am_sc_sign(load);
-       if (is_ia32_use_frame(imul))
-               set_ia32_use_frame(load);
-       set_ia32_frame_ent(load, get_ia32_frame_ent(imul));
+       set_irn_pinned(    load, get_irn_pinned(imul));
+       set_ia32_op_type(  load, ia32_AddrModeS);
+       ia32_copy_am_attrs(load, imul);
+
+       set_ia32_am_offs_int( imul, 0);
+       set_ia32_am_sc(       imul, NULL);
+       set_ia32_am_scale(    imul, 0);
+       clear_ia32_am_sc_sign(imul);
 
        sched_add_before(imul, load);
 
@@ -1101,7 +1190,9 @@ static void peephole_ia32_Imul_split(ir_node *imul) {
 
        set_irn_n(imul, n_ia32_IMul_mem, mem);
        noreg = get_irn_n(imul, n_ia32_IMul_left);
-       set_irn_n(imul, n_ia32_IMul_left, res);
+       set_irn_n(imul, n_ia32_IMul_base,  noreg);
+       set_irn_n(imul, n_ia32_IMul_index, noreg);
+       set_irn_n(imul, n_ia32_IMul_left,  res);
        set_ia32_op_type(imul, ia32_Normal);
 }