- refactor emitter next/prev block handling a bit
[libfirm] / ir / be / ia32 / ia32_optimize.c
index b9c1c73..1d3dd8c 100644 (file)
@@ -61,96 +61,27 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 static const arch_env_t *arch_env;
 static ia32_code_gen_t  *cg;
 
-#if 0
-static void peephole_ia32_Store_IncSP_to_push(ir_node *node)
-{
-       ir_node  *base  = get_irn_n(node, n_ia32_Store_base);
-       ir_node  *index = get_irn_n(node, n_ia32_Store_index);
-       ir_node  *mem   = get_irn_n(node, n_ia32_Store_mem);
-       ir_node  *incsp = base;
-       ir_node  *val;
-       ir_node  *noreg;
-       ir_graph *irg;
-       ir_node  *block;
-       dbg_info *dbgi;
-       ir_mode  *mode;
-       ir_node  *push;
-       ir_node  *proj;
-       int       offset;
-       int       node_offset;
-
-       /* nomem inidicates the store doesn't alias with anything else */
-       if(!is_NoMem(mem))
-               return;
-
-       /* find an IncSP in front of us, we might have to skip barriers for this */
-       while(is_Proj(incsp)) {
-               ir_node *proj_pred = get_Proj_pred(incsp);
-               if(!be_is_Barrier(proj_pred))
-                       return;
-               incsp = get_irn_n(proj_pred, get_Proj_proj(incsp));
-       }
-       if(!be_is_IncSP(incsp))
-               return;
-
-       be_peephole_IncSP_IncSP(incsp);
-
-       /* must be in the same block */
-       if(get_nodes_block(incsp) != get_nodes_block(node))
-               return;
-
-       if(!is_ia32_NoReg_GP(index) || get_ia32_am_sc(node) != NULL) {
-               panic("Invalid storeAM found (%+F)", node);
-       }
-
-       /* we should be the store to the end of the stackspace */
-       offset      = be_get_IncSP_offset(incsp);
-       mode        = get_ia32_ls_mode(node);
-       node_offset = get_ia32_am_offs_int(node);
-       if(node_offset != offset - get_mode_size_bytes(mode))
-               return;
-
-       /* we can use a push instead of the store */
-       irg   = current_ir_graph;
-       block = get_nodes_block(node);
-       dbgi  = get_irn_dbg_info(node);
-       noreg = ia32_new_NoReg_gp(cg);
-       base  = be_get_IncSP_pred(incsp);
-       val   = get_irn_n(node, n_ia32_Store_val);
-       push  = new_rd_ia32_Push(dbgi, irg, block, noreg, noreg, mem, val, base);
-
-       proj  = new_r_Proj(irg, block, push, mode_M, pn_ia32_Push_M);
-
-       be_set_IncSP_offset(incsp, offset - get_mode_size_bytes(mode));
-
-       sched_add_before(node, push);
-       sched_remove(node);
-
-       be_peephole_before_exchange(node, proj);
-       exchange(node, proj);
-       be_peephole_after_exchange(proj);
-}
-
-static void peephole_ia32_Store(ir_node *node)
-{
-       peephole_ia32_Store_IncSP_to_push(node);
-}
-#endif
-
+/**
+ * Returns non-zero if the given node produces
+ * a zero flag.
+ *
+ * @param node  the node to check
+ * @param pn    if >= 0, the projection number of the used result
+ */
 static int produces_zero_flag(ir_node *node, int pn)
 {
        ir_node                     *count;
        const ia32_immediate_attr_t *imm_attr;
 
-       if(!is_ia32_irn(node))
+       if (!is_ia32_irn(node))
                return 0;
 
-       if(pn >= 0) {
-               if(pn != pn_ia32_res)
+       if (pn >= 0) {
+               if (pn != pn_ia32_res)
                        return 0;
        }
 
-       switch(get_ia32_irn_opcode(node)) {
+       switch (get_ia32_irn_opcode(node)) {
        case iro_ia32_Add:
        case iro_ia32_Adc:
        case iro_ia32_And:
@@ -171,20 +102,20 @@ static int produces_zero_flag(ir_node *node, int pn)
                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)) {
+               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);
                }
                /* when shift count is zero the flags are not affected, so we can only
                 * do this for constants != 0 */
-               if(!is_ia32_Immediate(count))
+               if (!is_ia32_Immediate(count))
                        return 0;
 
                imm_attr = get_ia32_immediate_attr_const(count);
-               if(imm_attr->symconst != NULL)
+               if (imm_attr->symconst != NULL)
                        return 0;
-               if((imm_attr->offset & 0x1f) == 0)
+               if ((imm_attr->offset & 0x1f) == 0)
                        return 0;
                return 1;
 
@@ -194,6 +125,13 @@ static int produces_zero_flag(ir_node *node, int pn)
        return 0;
 }
 
+/**
+ * If the given node has not mode_T, creates a mode_T version (with a result Proj).
+ *
+ * @param node  the node to change
+ *
+ * @return the new mode_T node (if the mode was changed) or node itself
+ */
 static ir_node *turn_into_mode_t(ir_node *node)
 {
        ir_node               *block;
@@ -225,16 +163,21 @@ static ir_node *turn_into_mode_t(ir_node *node)
        return new_node;
 }
 
+/**
+ * Peephole optimization for Test instructions.
+ * We can remove the Test, if a zero flags was produced which is still
+ * live.
+ */
 static void peephole_ia32_Test(ir_node *node)
 {
-       ir_node *left  = get_irn_n(node, n_ia32_Test_left);
-       ir_node *right = get_irn_n(node, n_ia32_Test_right);
-       ir_node *flags_proj;
-       ir_node *block;
-       ir_mode *flags_mode;
-       int      pn    = -1;
-       ir_node *schedpoint;
-       const ir_edge_t       *edge;
+       ir_node         *left  = get_irn_n(node, n_ia32_Test_left);
+       ir_node         *right = get_irn_n(node, n_ia32_Test_right);
+       ir_node         *flags_proj;
+       ir_node         *block;
+       ir_mode         *flags_mode;
+       int              pn    = -1;
+       ir_node         *schedpoint;
+       const ir_edge_t *edge;
 
        assert(n_ia32_Test_left == n_ia32_Test8Bit_left
                        && n_ia32_Test_right == n_ia32_Test8Bit_right);
@@ -308,20 +251,6 @@ static void peephole_ia32_Return(ir_node *node) {
 
        block = get_nodes_block(node);
 
-       if (get_Block_n_cfgpreds(block) == 1) {
-               ir_node *pred = get_Block_cfgpred(block, 0);
-
-               if (is_Jmp(pred)) {
-                       /* The block of the return has only one predecessor,
-                          which jumps directly to this block.
-                          This jump will be encoded as a fall through, so we
-                          ignore it here.
-                          However, the predecessor might be empty, so it must be
-                          ensured that empty blocks are gone away ... */
-                       return;
-               }
-       }
-
        /* check if this return is the first on the block */
        sched_foreach_reverse_from(node, irn) {
                switch (get_irn_opcode(irn)) {
@@ -342,37 +271,30 @@ static void peephole_ia32_Return(ir_node *node) {
                        return;
                }
        }
-       /* yep, return is the first real instruction in this block */
-#if 0
-       {
-               /* add an rep prefix to the return */
-               ir_node *rep = new_rd_ia32_RepPrefix(get_irn_dbg_info(node), current_ir_graph, block);
-               keep_alive(rep);
-               sched_add_before(node, rep);
-       }
-#else
-       /* ensure, that the 3 byte return is generated */
+
+       /* 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 */
        be_Return_set_emit_pop(node, 1);
-#endif
 }
 
 /* only optimize up to 48 stores behind IncSPs */
 #define MAXPUSH_OPTIMIZE       48
 
 /**
- * Tries to create pushs from IncSP,Store combinations.
+ * Tries to create Push's from IncSP, Store combinations.
  * The Stores are replaced by Push's, the IncSP is modified
  * (possibly into IncSP 0, but not removed).
  */
 static void peephole_IncSP_Store_to_push(ir_node *irn)
 {
-       int     i, maxslot, inc_ofs;
-       ir_node *node;
-       ir_node *stores[MAXPUSH_OPTIMIZE];
-       ir_node *block = get_nodes_block(irn);
-       ir_graph *irg = cg->irg;
-       ir_node *curr_sp;
-       ir_mode *spmode = get_irn_mode(irn);
+       int      i, maxslot, inc_ofs;
+       ir_node  *node;
+       ir_node  *stores[MAXPUSH_OPTIMIZE];
+       ir_node  *block;
+       ir_graph *irg;
+       ir_node  *curr_sp;
+       ir_mode  *spmode;
 
        memset(stores, 0, sizeof(stores));
 
@@ -384,7 +306,7 @@ static void peephole_IncSP_Store_to_push(ir_node *irn)
 
        /*
         * We first walk the schedule after the IncSP node as long as we find
-        * suitable stores that could be transformed to a push.
+        * suitable Stores that could be transformed to a Push.
         * We save them into the stores array which is sorted by the frame offset/4
         * attached to the node
         */
@@ -436,7 +358,10 @@ static void peephole_IncSP_Store_to_push(ir_node *irn)
 
        curr_sp = be_get_IncSP_pred(irn);
 
-       /* walk through the stores and create Pushs for them */
+       /* walk through the Stores and create Pushs for them */
+       block  = get_nodes_block(irn);
+       spmode = get_irn_mode(irn);
+       irg    = cg->irg;
        for (i = 0; i <= maxslot; ++i) {
                const arch_register_t *spreg;
                ir_node *push;
@@ -465,7 +390,7 @@ static void peephole_IncSP_Store_to_push(ir_node *irn)
                /* use the memproj now */
                exchange(store, mem_proj);
 
-               /* we can remove the store now */
+               /* we can remove the Store now */
                sched_remove(store);
 
                inc_ofs -= 4;
@@ -475,6 +400,186 @@ static void peephole_IncSP_Store_to_push(ir_node *irn)
        be_set_IncSP_pred(irn, curr_sp);
 }
 
+/**
+ * Return true if a mode can be stored in the GP register set
+ */
+static INLINE int mode_needs_gp_reg(ir_mode *mode) {
+        if (mode == mode_fpcw)
+                return 0;
+        if (get_mode_size_bits(mode) > 32)
+                return 0;
+        return mode_is_int(mode) || mode_is_reference(mode) || mode == mode_b;
+}
+
+/**
+ * Tries to create Pops from Load, IncSP combinations.
+ * The Loads are replaced by Pops, the IncSP is modified
+ * (possibly into IncSP 0, but not removed).
+ */
+static void peephole_Load_IncSP_to_pop(ir_node *irn)
+{
+       const arch_register_t *esp = &ia32_gp_regs[REG_ESP];
+       int      i, maxslot, inc_ofs, ofs;
+       ir_node  *node, *pred_sp, *block;
+       ir_node  *loads[MAXPUSH_OPTIMIZE];
+       ir_graph *irg;
+       unsigned regmask = 0;
+       unsigned copymask = ~0;
+
+       memset(loads, 0, sizeof(loads));
+       assert(be_is_IncSP(irn));
+
+       inc_ofs = -be_get_IncSP_offset(irn);
+       if (inc_ofs < 4)
+               return;
+
+       /*
+        * We first walk the schedule before the IncSP node as long as we find
+        * suitable Loads that could be transformed to a Pop.
+        * We save them into the stores array which is sorted by the frame offset/4
+        * attached to the node
+        */
+       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;
+
+               /* it has to be a Load */
+               if (!is_ia32_Load(node)) {
+                       if (be_is_Copy(node)) {
+                               if (!mode_needs_gp_reg(get_irn_mode(node))) {
+                                       /* not a GP copy, ignore */
+                                       continue;
+                               }
+                               dreg = arch_get_irn_register(arch_env, node);
+                               sreg = arch_get_irn_register(arch_env, be_get_Copy_op(node));
+                               if (regmask & copymask & (1 << sreg->index)) {
+                                       break;
+                               }
+                               if (regmask & copymask & (1 << dreg->index)) {
+                                       break;
+                               }
+                               /* we CAN skip Copies if neither the destination nor the source
+                                * is not in our regmask, ie none of our future Pop will overwrite it */
+                               regmask |= (1 << dreg->index) | (1 << sreg->index);
+                               copymask &= ~((1 << dreg->index) | (1 << sreg->index));
+                               continue;
+                       }
+                       break;
+               }
+
+               /* we can handle only GP loads */
+               if (!mode_needs_gp_reg(get_ia32_ls_mode(node)))
+                       continue;
+
+               /* it has to use our predecessor sp value */
+               if (get_irn_n(node, n_ia32_base) != pred_sp) {
+                       /* it would be ok if this load does not use a Pop result,
+                        * 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)))
+                       break;
+
+               offset = get_ia32_am_offs_int(node);
+               /* we should NEVER access uninitialized stack BELOW the current SP */
+               assert(offset >= 0);
+
+               /* storing at half-slots is bad */
+               if ((offset & 3) != 0)
+                       break;
+
+               if (offset < 0 || offset >= MAXPUSH_OPTIMIZE * 4)
+                       continue;
+               /* ignore those outside the possible windows */
+               if (offset > inc_ofs - 4)
+                       continue;
+               loadslot = offset >> 2;
+
+               /* loading from the same slot twice is bad (and shouldn't happen...) */
+               if (loads[loadslot] != NULL)
+                       break;
+
+               dreg = arch_get_irn_register(arch_env, node);
+               if (regmask & (1 << dreg->index)) {
+                       /* this register is already used */
+                       break;
+               }
+               regmask |= 1 << dreg->index;
+
+               loads[loadslot] = node;
+               if (loadslot > maxslot)
+                       maxslot = loadslot;
+       }
+
+       if (maxslot < 0)
+               return;
+
+       /* find the first slot */
+       for (i = maxslot; i >= 0; --i) {
+               ir_node *load = loads[i];
+
+               if (load == NULL)
+                       break;
+       }
+
+       ofs = inc_ofs - (maxslot + 1) * 4;
+       inc_ofs = (i+1) * 4;
+
+       /* create a new IncSP if needed */
+       block = get_nodes_block(irn);
+       irg   = cg->irg;
+       if (inc_ofs > 0) {
+               pred_sp = be_new_IncSP(esp, irg, block, pred_sp, -inc_ofs, be_get_IncSP_align(irn));
+               sched_add_before(irn, pred_sp);
+       }
+
+       /* walk through the Loads and create Pops for them */
+       for (++i; i <= maxslot; ++i) {
+               ir_node *load = loads[i];
+               ir_node *mem, *pop;
+               const ir_edge_t *edge, *tmp;
+               const arch_register_t *reg;
+
+               mem = get_irn_n(load, n_ia32_mem);
+               reg = arch_get_irn_register(arch_env, load);
+
+               pop = new_rd_ia32_Pop(get_irn_dbg_info(load), irg, block, mem, pred_sp);
+               arch_set_irn_register(arch_env, pop, reg);
+
+               /* 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);
+
+               sched_add_before(irn, pop);
+
+               /* rewire now */
+               foreach_out_edge_safe(load, edge, tmp) {
+                       ir_node *proj = get_edge_src_irn(edge);
+
+                       set_Proj_pred(proj, pop);
+               }
+
+
+               /* we can remove the Load now */
+               sched_remove(load);
+               kill_node(load);
+       }
+       be_set_IncSP_offset(irn, -ofs);
+       be_set_IncSP_pred(irn, pred_sp);
+
+}
+
+
 /**
  * Find a free GP register if possible, else return NULL.
  */
@@ -567,7 +672,7 @@ static ir_node *create_push(dbg_info *dbgi, ir_graph *irg, ir_node *block,
 }
 
 /**
- * Optimize an IncSp by replacing it with push/pop
+ * Optimize an IncSp by replacing it with Push/Pop.
  */
 static void peephole_be_IncSP(ir_node *node)
 {
@@ -580,11 +685,14 @@ static void peephole_be_IncSP(ir_node *node)
        int                    offset;
 
        /* first optimize incsp->incsp combinations */
-       be_peephole_IncSP_IncSP(node);
+       node = be_peephole_IncSP_IncSP(node);
 
        /* transform IncSP->Store combinations to Push where possible */
        peephole_IncSP_Store_to_push(node);
 
+       /* transform Load->IncSP combinations to Pop where possible */
+       peephole_Load_IncSP_to_pop(node);
+
        if (arch_get_irn_register(arch_env, node) != esp)
                return;
 
@@ -965,7 +1073,6 @@ void ia32_peephole_optimization(ia32_code_gen_t *new_cg)
        /* register peephole optimisations */
        clear_irp_opcodes_generic_func();
        register_peephole_optimisation(op_ia32_Const, peephole_ia32_Const);
-       //register_peephole_optimisation(op_ia32_Store, peephole_ia32_Store);
        register_peephole_optimisation(op_be_IncSP, peephole_be_IncSP);
        register_peephole_optimisation(op_ia32_Lea, peephole_ia32_Lea);
        register_peephole_optimisation(op_ia32_Test, peephole_ia32_Test);