X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fmini%2Flocal-propagation.c;h=923f53311c7c56f2c819ecca2ec9a8740bf8a8c6;hb=HEAD;hp=5d5dd71e8dea20f4e5b1e753f9593df615c0f275;hpb=3bc3787974ae185937e929590ba435d3fdb03cb8;p=mono.git diff --git a/mono/mini/local-propagation.c b/mono/mini/local-propagation.c index 5d5dd71e8de..923f53311c7 100644 --- a/mono/mini/local-propagation.c +++ b/mono/mini/local-propagation.c @@ -1,5 +1,6 @@ -/* - * local-propagation.c: Local constant, copy and tree propagation. +/** + * \file + * Local constant, copy and tree propagation. * * To make some sense of the tree mover, read mono/docs/tree-mover.txt * @@ -14,6 +15,8 @@ */ #include +#include + #ifndef DISABLE_JIT #include @@ -25,6 +28,7 @@ #include #include #include +#include #include "mini.h" #include "ir-emit.h" @@ -42,7 +46,6 @@ mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 max_size) return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE); } -#if SIZEOF_REGISTER == 8 struct magic_unsigned { guint32 magic_number; gboolean addition; @@ -141,7 +144,6 @@ compute_magic_signed (gint32 divisor) { mag.shift = p - 32; return mag; } -#endif static gboolean mono_strength_reduction_division (MonoCompile *cfg, MonoInst *ins) @@ -152,10 +154,14 @@ mono_strength_reduction_division (MonoCompile *cfg, MonoInst *ins) * platforms we emulate long multiplication, driving the * performance back down. */ -#if SIZEOF_REGISTER == 8 switch (ins->opcode) { case OP_IDIV_UN_IMM: { - guint32 tmp_regl, dividend_reg; + guint32 tmp_regl; +#if SIZEOF_REGISTER == 8 + guint32 dividend_reg; +#else + guint32 tmp_regi; +#endif struct magic_unsigned mag; int power2 = mono_is_power_of_two (ins->inst_imm); @@ -169,6 +175,8 @@ mono_strength_reduction_division (MonoCompile *cfg, MonoInst *ins) ins->inst_imm = power2; break; } + if (cfg->backend->disable_div_with_mul) + break; allocated_vregs = TRUE; /* * Replacement of unsigned division with multiplication, @@ -176,6 +184,7 @@ mono_strength_reduction_division (MonoCompile *cfg, MonoInst *ins) */ mag = compute_magic_unsigned (ins->inst_imm); tmp_regl = alloc_lreg (cfg); +#if SIZEOF_REGISTER == 8 dividend_reg = alloc_lreg (cfg); MONO_EMIT_NEW_I8CONST (cfg, tmp_regl, mag.magic_number); MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, dividend_reg, ins->sreg1); @@ -187,16 +196,43 @@ mono_strength_reduction_division (MonoCompile *cfg, MonoInst *ins) } else { MONO_EMIT_NEW_BIALU_IMM (cfg, OP_LSHR_UN_IMM, ins->dreg, tmp_regl, 32 + mag.shift); } +#else + tmp_regi = alloc_ireg (cfg); + MONO_EMIT_NEW_ICONST (cfg, tmp_regi, mag.magic_number); + MONO_EMIT_NEW_BIALU (cfg, OP_BIGMUL_UN, tmp_regl, ins->sreg1, tmp_regi); + /* Long shifts below will be decomposed during cprop */ + if (mag.addition) { + MONO_EMIT_NEW_BIALU_IMM (cfg, OP_LSHR_UN_IMM, tmp_regl, tmp_regl, 32); + MONO_EMIT_NEW_BIALU (cfg, OP_IADDCC, MONO_LVREG_LS (tmp_regl), MONO_LVREG_LS (tmp_regl), ins->sreg1); + /* MONO_LVREG_MS (tmp_reg) is 0, save in it the carry */ + MONO_EMIT_NEW_BIALU (cfg, OP_IADC, MONO_LVREG_MS (tmp_regl), MONO_LVREG_MS (tmp_regl), MONO_LVREG_MS (tmp_regl)); + MONO_EMIT_NEW_BIALU_IMM (cfg, OP_LSHR_UN_IMM, tmp_regl, tmp_regl, mag.shift); + } else { + MONO_EMIT_NEW_BIALU_IMM (cfg, OP_LSHR_UN_IMM, tmp_regl, tmp_regl, 32 + mag.shift); + } + MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ins->dreg, MONO_LVREG_LS (tmp_regl)); +#endif + UnlockedIncrement (&mono_jit_stats.optimized_divisions); break; } case OP_IDIV_IMM: { - guint32 tmp_regl, dividend_reg; + guint32 tmp_regl; +#if SIZEOF_REGISTER == 8 + guint32 dividend_reg; +#else + guint32 tmp_regi; +#endif struct magic_signed mag; int power2 = mono_is_power_of_two (ins->inst_imm); - /* The decomposition doesn't handle exception throwing */ - if (ins->inst_imm == 0 || ins->inst_imm == -1) + /* Optimization with MUL does not apply for -1, 0 and 1 divisors */ + if (ins->inst_imm == 0 || ins->inst_imm == -1) { break; + } else if (ins->inst_imm == 1) { + ins->opcode = OP_MOVE; + ins->inst_imm = 0; + break; + } allocated_vregs = TRUE; if (power2 == 1) { guint32 r1 = alloc_ireg (cfg); @@ -213,12 +249,15 @@ mono_strength_reduction_division (MonoCompile *cfg, MonoInst *ins) break; } + if (cfg->backend->disable_div_with_mul) + break; /* * Replacement of signed division with multiplication, * shifts and additions Hacker's Delight, chapter 10-6. */ mag = compute_magic_signed (ins->inst_imm); tmp_regl = alloc_lreg (cfg); +#if SIZEOF_REGISTER == 8 dividend_reg = alloc_lreg (cfg); MONO_EMIT_NEW_I8CONST (cfg, tmp_regl, mag.magic_number); MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, dividend_reg, ins->sreg1); @@ -236,10 +275,29 @@ mono_strength_reduction_division (MonoCompile *cfg, MonoInst *ins) } MONO_EMIT_NEW_BIALU_IMM (cfg, OP_LSHR_UN_IMM, ins->dreg, tmp_regl, SIZEOF_REGISTER * 8 - 1); MONO_EMIT_NEW_BIALU (cfg, OP_LADD, ins->dreg, ins->dreg, tmp_regl); +#else + tmp_regi = alloc_ireg (cfg); + MONO_EMIT_NEW_ICONST (cfg, tmp_regi, mag.magic_number); + MONO_EMIT_NEW_BIALU (cfg, OP_BIGMUL, tmp_regl, ins->sreg1, tmp_regi); + if ((ins->inst_imm > 0 && mag.magic_number < 0) || (ins->inst_imm < 0 && mag.magic_number > 0)) { + if (ins->inst_imm > 0 && mag.magic_number < 0) { + /* Opposite sign, cannot overflow */ + MONO_EMIT_NEW_BIALU (cfg, OP_IADD, tmp_regi, MONO_LVREG_MS (tmp_regl), ins->sreg1); + } else if (ins->inst_imm < 0 && mag.magic_number > 0) { + /* Same sign, cannot overflow */ + MONO_EMIT_NEW_BIALU (cfg, OP_ISUB, tmp_regi, MONO_LVREG_MS (tmp_regl), ins->sreg1); + } + MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, tmp_regi, tmp_regi, mag.shift); + } else { + MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, tmp_regi, MONO_LVREG_MS (tmp_regl), mag.shift); + } + MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, ins->dreg, tmp_regi, SIZEOF_REGISTER * 8 - 1); + MONO_EMIT_NEW_BIALU (cfg, OP_IADD, ins->dreg, ins->dreg, tmp_regi); +#endif + UnlockedIncrement (&mono_jit_stats.optimized_divisions); break; } } -#endif return allocated_vregs; } @@ -312,7 +370,8 @@ mono_strength_reduction_ins (MonoCompile *cfg, MonoInst *ins, const char **spec) } case OP_IDIV_UN_IMM: case OP_IDIV_IMM: { - allocated_vregs = mono_strength_reduction_division (cfg, ins); + if (!COMPILE_LLVM (cfg)) + allocated_vregs = mono_strength_reduction_division (cfg, ins); break; } #if SIZEOF_REGISTER == 8 @@ -584,13 +643,15 @@ mono_local_cprop (MonoCompile *cfg) /* FIXME: Make is_inst_imm a macro */ /* FIXME: Make is_inst_imm take an opcode argument */ /* is_inst_imm is only needed for binops */ - if ((((def->opcode == OP_ICONST) || ((sizeof (gpointer) == 8) && (def->opcode == OP_I8CONST))) && + if ((((def->opcode == OP_ICONST) || ((sizeof (gpointer) == 8) && (def->opcode == OP_I8CONST)) || (def->opcode == OP_PCONST)) && (((srcindex == 0) && (ins->sreg2 == -1)) || mono_arch_is_inst_imm (def->inst_c0))) || (!MONO_ARCH_USE_FPSTACK && (def->opcode == OP_R8CONST))) { guint32 opcode2; /* srcindex == 1 -> binop, ins->sreg2 == -1 -> unop */ - if ((srcindex == 1) && (ins->sreg1 != -1) && defs [ins->sreg1] && (defs [ins->sreg1]->opcode == OP_ICONST) && defs [ins->sreg2]) { + if ((srcindex == 1) && (ins->sreg1 != -1) && defs [ins->sreg1] && + ((defs [ins->sreg1]->opcode == OP_ICONST) || defs [ins->sreg1]->opcode == OP_PCONST) && + defs [ins->sreg2]) { /* Both arguments are constants, perform cfold */ mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE); } else if ((srcindex == 0) && (ins->sreg2 != -1) && defs [ins->sreg2]) { @@ -682,6 +743,9 @@ mono_local_cprop (MonoCompile *cfg) dummy_arg1.inst_c0 = 1; mono_constant_fold_ins (cfg, ins, &dummy_arg1, NULL, TRUE); + } else if (srcindex == 0 && ins->opcode == OP_COMPARE && defs [ins->sreg1]->opcode == OP_PCONST && defs [ins->sreg2] && defs [ins->sreg2]->opcode == OP_PCONST) { + /* typeof(T) == typeof(..) */ + mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE); } } @@ -947,4 +1011,8 @@ mono_local_deadce (MonoCompile *cfg) //mono_print_code (cfg, "AFTER LOCAL-DEADCE"); } -#endif /* DISABLE_JIT */ +#else /* !DISABLE_JIT */ + +MONO_EMPTY_SOURCE_FILE (local_propagation); + +#endif /* !DISABLE_JIT */