Author: zoltan
Date: 2006-01-18 12:44:59 -0500 (Wed, 18 Jan 2006)
New Revision: 55743
Modified:
branches/vargaz/mini-linear-il/mono/mono/mini/method-to-ir.c
branches/vargaz/mini-linear-il/mono/mono/mini/mini-codegen.c
branches/vargaz/mini-linear-il/mono/mono/mini/mini-x86.c
branches/vargaz/mini-linear-il/mono/mono/mini/mini.c
Log:
Improve copyprop && add constant propagation
Modified: branches/vargaz/mini-linear-il/mono/mono/mini/method-to-ir.c
===================================================================
--- branches/vargaz/mini-linear-il/mono/mono/mini/method-to-ir.c
2006-01-18 17:43:38 UTC (rev 55742)
+++ branches/vargaz/mini-linear-il/mono/mono/mini/method-to-ir.c
2006-01-18 17:44:59 UTC (rev 55743)
@@ -79,8 +79,9 @@
static int ldind_to_load_membase (int opcode);
static int stind_to_store_membase (int opcode);
-static int alu_to_alu_imm (int opcode);
+int op_to_op_imm (int opcode);
+
gboolean mono_arch_print_tree(MonoInst *tree, int arity);
static void emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src,
@@ -6355,7 +6356,7 @@
/* Use the immediate opcodes if possible */
if ((sp [1]->opcode == OP_ICONST) &&
mono_arch_is_inst_imm (sp [1]->inst_c0)) {
- int imm_opcode = alu_to_alu_imm (ins->opcode);
+ int imm_opcode = op_to_op_imm (ins->opcode);
if (imm_opcode != -1) {
ins->opcode = imm_opcode;
ins->inst_p1 = (gpointer)(gssize)(sp
[1]->inst_c0);
@@ -6398,7 +6399,7 @@
/* Use the immediate opcodes if possible */
if (((sp [1]->opcode == OP_ICONST) || (sp [1]->opcode
== OP_I8CONST)) && mono_arch_is_inst_imm (sp [1]->opcode == OP_ICONST ? sp
[1]->inst_c0 : sp [1]->inst_l)) {
- int imm_opcode = alu_to_alu_imm (ins->opcode);
+ int imm_opcode = op_to_op_imm (ins->opcode);
if (imm_opcode != -1) {
ins->opcode = imm_opcode;
if (sp [1]->opcode == OP_I8CONST) {
@@ -8668,8 +8669,8 @@
return -1;
}
-static int
-alu_to_alu_imm (int opcode)
+int
+op_to_op_imm (int opcode)
{
switch (opcode) {
case OP_IADD:
@@ -8718,6 +8719,16 @@
case OP_LSHR_UN:
return OP_LSHR_UN_IMM;
+ case OP_STORE_MEMBASE_REG:
+ return OP_STORE_MEMBASE_IMM;
+ case OP_STOREI1_MEMBASE_REG:
+ return OP_STOREI1_MEMBASE_IMM;
+ case OP_STOREI2_MEMBASE_REG:
+ return OP_STOREI2_MEMBASE_IMM;
+ case OP_STOREI4_MEMBASE_REG:
+ return OP_STOREI4_MEMBASE_IMM;
+ case OP_X86_PUSH:
+ return OP_X86_PUSH_IMM;
default:
return -1;
}
@@ -9114,23 +9125,30 @@
} else {
g_assert (var->opcode ==
OP_REGOFFSET);
- sreg = alloc_dreg (cfg,
stacktypes [regtype]);
+ /* FIXME: Generalize this */
+ if (ins->opcode == OP_X86_PUSH)
{
+ ins->opcode =
OP_X86_PUSH_MEMBASE;
+ ins->inst_basereg =
var->inst_basereg;
+ ins->inst_offset =
var->inst_offset;
+ } else {
+ sreg = alloc_dreg (cfg,
stacktypes [regtype]);
- if (srcindex == 0)
- ins->sreg1 = sreg;
- else
- ins->sreg2 = sreg;
+ if (srcindex == 0)
+ ins->sreg1 =
sreg;
+ else
+ ins->sreg2 =
sreg;
- if (regtype == 'l') {
- NEW_LOAD_MEMBASE (cfg,
load_ins, OP_LOADI4_MEMBASE, sreg + 1, var->inst_basereg, var->inst_offset +
MINI_MS_WORD_OFFSET);
- insert_before_ins (bb,
ins, load_ins, &prev);
- NEW_LOAD_MEMBASE (cfg,
load_ins, OP_LOADI4_MEMBASE, sreg, var->inst_basereg, var->inst_offset +
MINI_LS_WORD_OFFSET);
- insert_before_ins (bb,
ins, load_ins, &prev);
+ if (regtype == 'l') {
+
NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 1,
var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
+
insert_before_ins (bb, ins, load_ins, &prev);
+
NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg, var->inst_basereg,
var->inst_offset + MINI_LS_WORD_OFFSET);
+
insert_before_ins (bb, ins, load_ins, &prev);
+ }
+ else {
+
NEW_LOAD_MEMBASE (cfg, load_ins, mono_type_to_load_membase (var->inst_vtype),
sreg, var->inst_basereg, var->inst_offset);
+
insert_before_ins (bb, ins, load_ins, &prev);
+ }
}
- else {
- NEW_LOAD_MEMBASE (cfg,
load_ins, mono_type_to_load_membase (var->inst_vtype), sreg, var->inst_basereg,
var->inst_offset);
- insert_before_ins (bb,
ins, load_ins, &prev);
- }
}
}
}
Modified: branches/vargaz/mini-linear-il/mono/mono/mini/mini-codegen.c
===================================================================
--- branches/vargaz/mini-linear-il/mono/mono/mini/mini-codegen.c
2006-01-18 17:43:38 UTC (rev 55742)
+++ branches/vargaz/mini-linear-il/mono/mono/mini/mini-codegen.c
2006-01-18 17:44:59 UTC (rev 55743)
@@ -311,6 +311,9 @@
case OP_COMPARE_IMM:
g_print (" [%d]", (int)ins->inst_c0);
break;
+ case OP_X86_PUSH_IMM:
+ g_print (" [%d]", (int)ins->inst_imm);
+ break;
case OP_ADD_IMM:
g_print (" [%d]", (int)(gssize)ins->inst_p1);
break;
@@ -833,7 +836,7 @@
i = 1;
fpcount = 0;
- DEBUG (g_print ("\nLOCAL regalloc: basic block %d:\n", bb->block_num));
+ DEBUG (g_print ("\nLOCAL REGALLOC: BASIC BLOCK %d:\n", bb->block_num));
/* forward pass on the instructions to collect register liveness info */
ins = bb->code;
@@ -1210,7 +1213,7 @@
dreg_mask &= ~ (regmask (dest_sreg2));
val = rassign (cfg, ins->dreg, fp);
- if (is_soft_reg (ins->dreg, fp) && (val >= 0) && (!(val
& dreg_mask))) {
+ if (is_soft_reg (ins->dreg, fp) && (val >= 0) &&
(!(regmask (val) & dreg_mask))) {
/* DREG is already allocated to a register
needed for sreg1 */
get_register_force_spilling (cfg, tmp, ins,
ins->dreg, FALSE);
mono_regstate_free_int (rs, val);
@@ -1244,7 +1247,7 @@
dreg_mask = (regmask (dest_dreg));
/* FIXME: Do this earlier */
- if ((ins->opcode == OP_MOVE) && !fp && (reginfo
[ins->dreg].last_use == i) && (reginfo [ins->dreg].prev_use == 0)) {
+ if (((ins->opcode == OP_MOVE) || (ins->opcode ==
OP_ICONST)) && !fp && (reginfo [ins->dreg].last_use == i) && (reginfo
[ins->dreg].prev_use == 0)) {
ins->opcode = CEE_NOP;
}
Modified: branches/vargaz/mini-linear-il/mono/mono/mini/mini-x86.c
===================================================================
--- branches/vargaz/mini-linear-il/mono/mono/mini/mini-x86.c 2006-01-18
17:43:38 UTC (rev 55742)
+++ branches/vargaz/mini-linear-il/mono/mono/mini/mini-x86.c 2006-01-18
17:44:59 UTC (rev 55743)
@@ -1426,7 +1426,7 @@
}
/* FIXME: Add more instructions */
-#define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode
== OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG) ||
((ins)->opcode == OP_IXOR))
+#define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode
== OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_IMM) ||
((ins)->opcode == OP_STOREI4_MEMBASE_REG) || ((ins)->opcode == OP_IXOR) ||
((ins)->opcode == OP_MOVE))
static void
peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
Modified: branches/vargaz/mini-linear-il/mono/mono/mini/mini.c
===================================================================
--- branches/vargaz/mini-linear-il/mono/mono/mini/mini.c 2006-01-18
17:43:38 UTC (rev 55742)
+++ branches/vargaz/mini-linear-il/mono/mono/mini/mini.c 2006-01-18
17:44:59 UTC (rev 55743)
@@ -79,6 +79,8 @@
static gpointer mono_jit_compile_method (MonoMethod *method);
static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod
*method);
+int op_to_op_imm (int opcode);
+
static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst
*dest, MonoInst *src,
const unsigned char *ip, MonoClass *klass, gboolean
to_end, gboolean native);
@@ -9306,80 +9308,85 @@
mono_local_cprop_bb2 (MonoCompile *cfg, MonoBasicBlock *bb, int acp_size)
{
MonoInst *ins;
- int i;
- GArray *acp;
+ int ins_index;
+ MonoInst **defs;
+ guint32 *def_index;
/* FIXME: Speed this up */
- acp = g_array_new (FALSE, FALSE, sizeof (int));
+ defs = g_new0 (MonoInst*, cfg->next_vireg);
+ def_index = g_new (guint32, cfg->next_vireg);
+ ins_index = 0;
for (ins = bb->code; ins; ins = ins->next) {
const char *spec = ins_info [ins->opcode - OP_START - 1];
int regtype, srcindex, sreg;
- gboolean store;
if (ins->opcode < MONO_CEE_LAST)
continue;
+ /* FIXME: Optimize this */
+ if (ins->opcode == OP_LDADDR)
+ return;
+
/* FIXME: Store opcodes */
+ for (srcindex = 0; srcindex < 2; ++srcindex) {
+ regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 :
MONO_INST_SRC2];
+ sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
- if (acp->len > 0) {
- for (srcindex = 0; srcindex < 2; ++srcindex) {
- regtype = spec [(srcindex == 0) ?
MONO_INST_SRC1 : MONO_INST_SRC2];
- sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
+ /* FIXME: Add support for floats/longs */
+ if ((regtype == 'i') && (sreg != -1) && defs [sreg]) {
+ MonoInst *def = defs [sreg];
- if ((regtype == 'i') && (sreg != -1)) {
- for (i = 0; i < acp->len; i += 2)
- if (g_array_index (acp, int, i)
== sreg) {
- int vreg =
g_array_index (acp, int, i + 1);
+ if ((def->opcode == OP_MOVE) && (!defs
[def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
+ int vreg = def->sreg1;
#if 0
- {
- static int
count = 0;
- count ++;
- if (count ==
atoi (getenv ("COUNT2")))
- printf
("FOO\n");
- if (count >
atoi (getenv ("COUNT2")))
- vreg =
sreg;
- }
+ {
+ static int count = 0;
+ count ++;
+ if (count == atoi (getenv
("COUNT2")))
+ printf ("FOO\n");
+ if (count > atoi (getenv
("COUNT2")))
+ vreg = sreg;
+ }
#endif
- //printf ("CCOPY: R%d
-> R%d\n", sreg, vreg);
- if (srcindex == 0)
- ins->sreg1 =
vreg;
- else
- ins->sreg2 =
vreg;
- break;
- }
+ //printf ("CCOPY: R%d -> R%d\n", sreg,
vreg);
+ if (srcindex == 0)
+ ins->sreg1 = vreg;
+ else
+ ins->sreg2 = vreg;
+ break;
}
+ else if (def->opcode == OP_ICONST) {
+ guint32 opcode2 = op_to_op_imm
(ins->opcode);
+
+ /* srcindex == 1 -> binop, ins->sreg2
== -1 -> unop */
+ if ((opcode2 != -1) && ((srcindex == 1)
|| (ins->sreg2 == -1))) {
+ //printf ("CONST FOLD: %s ->
%s!\n", mono_inst_name (ins->opcode), mono_inst_name (opcode2));
+ ins->opcode = opcode2;
+ ins->inst_imm = def->inst_c0;
+ if (srcindex == 0)
+ ins->sreg1 = -1;
+ else
+ ins->sreg2 = -1;
+ }
+ }
}
- }
-
+ }
+
if ((ins->dreg != -1) && (spec [MONO_INST_DEST] == 'i')) {
- /* Invalidate values */
- for (i = 0; i < acp->len; i += 2)
- if ((g_array_index (acp, int, i) == ins->dreg)
|| (g_array_index (acp, int, i + 1) == ins->dreg))
- g_array_index (acp, int, i) = -1;
+ defs [ins->dreg] = ins;
+ def_index [ins->dreg] = ins_index;
}
+ }
- if (ins->opcode == OP_LDADDR) {
- return;
- }
+ /*
+ * FIXME: Get rid of dead code created by this pass early so
+ * the spill pass doesn't create loads/stores for it.
+ */
- /* FIXME: Add support for long/float */
- if (ins->opcode == OP_MOVE) {
- for (i = 0; i < acp->len; i += 2)
- if (g_array_index (acp, int, i) == ins->dreg)
- break;
-
- if (i < acp->len)
- g_array_index (acp, int, i + 1) = ins->sreg1;
- else {
- g_array_append_val (acp, ins->dreg);
- g_array_append_val (acp, ins->sreg1);
- }
- }
- }
-
- g_array_free (acp, TRUE);
+ g_free (defs);
+ g_free (def_index);
}
static void
@@ -9392,7 +9399,86 @@
}
}
+/**
+ * mono_local_deadce:
+ *
+ * Get rid of the dead assignments to local vregs left by the copyprop pass.
+ */
static void
+mono_local_deadce (MonoCompile *cfg)
+{
+ MonoBasicBlock *bb;
+ MonoInst *ins;
+ gboolean *used;
+
+ /* FIXME: speed this up */
+
+ used = g_new (gboolean, cfg->next_vireg);
+
+ /* First pass: collect liveness info */
+ for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+ memset (used, 0, sizeof (gboolean) * cfg->next_vireg);
+ for (ins = bb->code; ins; ins = ins->next) {
+ const char *spec = ins_info [ins->opcode - OP_START -
1];
+ int regtype, srcindex, sreg;
+
+ if (ins->opcode < MONO_CEE_LAST)
+ continue;
+
+ /* FIXME: Optimize this */
+ if (ins->opcode == OP_LDADDR)
+ break;
+
+ /* FIXME: Store opcodes */
+ for (srcindex = 0; srcindex < 2; ++srcindex) {
+ regtype = spec [(srcindex == 0) ?
MONO_INST_SRC1 : MONO_INST_SRC2];
+ sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
+
+ if (regtype == 'i')
+ used [sreg] = TRUE;
+
+ switch (ins->opcode) {
+ case OP_STORE:
+ case OP_STORE_MEMBASE_REG:
+ case OP_STOREI1_MEMBASE_REG:
+ case OP_STOREI2_MEMBASE_REG:
+ case OP_STOREI4_MEMBASE_REG:
+ case OP_STOREI8_MEMBASE_REG:
+ case OP_STORER4_MEMBASE_REG:
+ case OP_STORER8_MEMBASE_REG:
+ case OP_STORE_MEMBASE_IMM:
+ case OP_STOREI1_MEMBASE_IMM:
+ case OP_STOREI2_MEMBASE_IMM:
+ case OP_STOREI4_MEMBASE_IMM:
+ case OP_STOREI8_MEMBASE_IMM:
+ used [ins->dreg] = TRUE;
+ }
+ }
+ }
+
+ if (ins && ins->opcode == OP_LDADDR)
+ continue;
+
+ /* Second pass: deadce */
+ for (ins = bb->code; ins; ins = ins->next) {
+ const char *spec = ins_info [ins->opcode - OP_START -
1];
+
+ if (ins->opcode < MONO_CEE_LAST)
+ continue;
+
+ if ((spec [MONO_INST_DEST] == 'i') && (ins->dreg >=
MONO_MAX_IREGS) && !used [ins->dreg] && !get_vreg_to_inst (cfg, 'i',
ins->dreg)) {
+ if ((ins->opcode == OP_MOVE) || (ins->opcode ==
OP_ICONST)) {
+ //printf ("DEADCE: "); mono_print_ins
(ins);
+ ins->opcode = CEE_NOP;
+ }
+ }
+ }
+ }
+
+ g_free (used);
+}
+
+static void
remove_critical_edges (MonoCompile *cfg) {
MonoBasicBlock *bb;
MonoBasicBlock *previous_bb;
@@ -9757,6 +9843,7 @@
if (cfg->new_ir) {
/* This must be done _before_ global reg alloc and _after_
decompose */
mono_handle_global_vregs (cfg);
+ //mono_local_deadce (cfg);
}
if (cfg->opt & MONO_OPT_LINEARS) {
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches