2 * local-propagation.c: Local constant, copy and tree propagation.
4 * To make some sense of the tree mover, read mono/docs/tree-mover.txt
7 * Paolo Molaro (lupus@ximian.com)
8 * Dietmar Maurer (dietmar@ximian.com)
9 * Massimiliano Mantione (massi@ximian.com)
11 * (C) 2006 Novell, Inc. http://www.novell.com
12 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/mempool.h>
26 #include <mono/metadata/opcodes.h>
30 #ifndef MONO_ARCH_IS_OP_MEMBASE
31 #define MONO_ARCH_IS_OP_MEMBASE(opcode) FALSE
34 static inline MonoBitSet*
35 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 max_size)
37 int size = mono_bitset_alloc_size (max_size, 0);
40 mem = mono_mempool_alloc (mp, size);
41 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
47 * A combined local copy and constant propagation pass.
50 mono_local_cprop (MonoCompile *cfg)
56 int filter = FILTER_IL_SEQ_POINT;
61 defs = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * (cfg->next_vreg + 1));
62 def_index = mono_mempool_alloc (cfg->mempool, sizeof (guint32) * (cfg->next_vreg + 1));
64 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
69 /* Manually init the defs entries used by the bblock */
70 MONO_BB_FOR_EACH_INS (bb, ins) {
71 int sregs [MONO_MAX_SRC_REGS];
74 if ((ins->dreg != -1) && (ins->dreg < max)) {
75 defs [ins->dreg] = NULL;
76 #if SIZEOF_REGISTER == 4
77 defs [ins->dreg + 1] = NULL;
81 num_sregs = mono_inst_get_src_registers (ins, sregs);
82 for (i = 0; i < num_sregs; ++i) {
86 #if SIZEOF_REGISTER == 4
87 defs [sreg + 1] = NULL;
95 MONO_BB_FOR_EACH_INS (bb, ins) {
96 const char *spec = INS_INFO (ins->opcode);
97 int regtype, srcindex, sreg;
99 int sregs [MONO_MAX_SRC_REGS];
101 if (ins->opcode == OP_NOP) {
102 MONO_DELETE_INS (bb, ins);
106 g_assert (ins->opcode > MONO_CEE_LAST);
108 /* FIXME: Optimize this */
109 if (ins->opcode == OP_LDADDR) {
110 MonoInst *var = ins->inst_p0;
112 defs [var->dreg] = NULL;
114 if (!MONO_TYPE_ISSTRUCT (var->inst_vtype))
119 if (MONO_IS_STORE_MEMBASE (ins)) {
123 if ((regtype == 'i') && (sreg != -1) && defs [sreg]) {
124 MonoInst *def = defs [sreg];
126 if ((def->opcode == OP_MOVE) && (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) && !vreg_is_volatile (cfg, def->sreg1)) {
127 int vreg = def->sreg1;
128 if (cfg->verbose_level > 2) printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
134 num_sregs = mono_inst_get_src_registers (ins, sregs);
135 for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
138 mono_inst_get_src_registers (ins, sregs);
140 regtype = spec [MONO_INST_SRC1 + srcindex];
141 sreg = sregs [srcindex];
143 if ((regtype == ' ') || (sreg == -1) || (!defs [sreg]))
148 /* Copy propagation */
150 * The first check makes sure the source of the copy did not change since
152 * The second check avoids volatile variables.
153 * The third check avoids copy propagating local vregs through a call,
154 * since the lvreg will be spilled
155 * The fourth check avoids copy propagating a vreg in cases where
156 * it would be eliminated anyway by reverse copy propagation later,
157 * because propagating it would create another use for it, thus making
158 * it impossible to use reverse copy propagation.
160 /* Enabling this for floats trips up the fp stack */
162 * Enabling this for floats on amd64 seems to cause a failure in
163 * basic-math.cs, most likely because it gets rid of some r8->r4
166 if (MONO_IS_MOVE (def) &&
167 (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) &&
168 !vreg_is_volatile (cfg, def->sreg1) &&
169 /* This avoids propagating local vregs across calls */
170 ((get_vreg_to_inst (cfg, def->sreg1) || !defs [def->sreg1] || (def_index [def->sreg1] >= last_call_index) || (def->opcode == OP_VMOVE))) &&
171 !(defs [def->sreg1] && mono_inst_next (defs [def->sreg1], filter) == def) &&
172 (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE)) &&
173 (def->opcode != OP_FMOVE)) {
174 int vreg = def->sreg1;
176 if (cfg->verbose_level > 2) printf ("CCOPY/2: R%d -> R%d\n", sreg, vreg);
177 sregs [srcindex] = vreg;
178 mono_inst_set_src_registers (ins, sregs);
180 /* Allow further iterations */
185 /* Constant propagation */
186 /* FIXME: Make is_inst_imm a macro */
187 /* FIXME: Make is_inst_imm take an opcode argument */
188 /* is_inst_imm is only needed for binops */
189 if ((((def->opcode == OP_ICONST) || ((sizeof (gpointer) == 8) && (def->opcode == OP_I8CONST))) &&
190 (((srcindex == 0) && (ins->sreg2 == -1)) || mono_arch_is_inst_imm (def->inst_c0))) ||
191 (!MONO_ARCH_USE_FPSTACK && (def->opcode == OP_R8CONST))) {
194 /* srcindex == 1 -> binop, ins->sreg2 == -1 -> unop */
195 if ((srcindex == 1) && (ins->sreg1 != -1) && defs [ins->sreg1] && (defs [ins->sreg1]->opcode == OP_ICONST) && defs [ins->sreg2]) {
196 /* Both arguments are constants, perform cfold */
197 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
198 } else if ((srcindex == 0) && (ins->sreg2 != -1) && defs [ins->sreg2]) {
199 /* Arg 1 is constant, swap arguments if possible */
200 int opcode = ins->opcode;
201 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
202 if (ins->opcode != opcode) {
203 /* Allow further iterations */
207 } else if ((srcindex == 0) && (ins->sreg2 == -1)) {
208 /* Constant unop, perform cfold */
209 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], NULL, TRUE);
212 opcode2 = mono_op_to_op_imm (ins->opcode);
213 if ((opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0) && ((srcindex == 1) || (ins->sreg2 == -1))) {
214 ins->opcode = opcode2;
215 if ((def->opcode == OP_I8CONST) && (sizeof (gpointer) == 4)) {
216 ins->inst_ls_word = def->inst_ls_word;
217 ins->inst_ms_word = def->inst_ms_word;
219 ins->inst_imm = def->inst_c0;
221 sregs [srcindex] = -1;
222 mono_inst_set_src_registers (ins, sregs);
224 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
225 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
227 /* Allow further iterations */
233 #if defined(TARGET_X86) || defined(TARGET_AMD64)
234 if ((ins->opcode == OP_X86_LEA) && (srcindex == 1)) {
235 #if SIZEOF_REGISTER == 8
236 /* FIXME: Use OP_PADD_IMM when the new JIT is done */
237 ins->opcode = OP_LADD_IMM;
239 ins->opcode = OP_ADD_IMM;
241 ins->inst_imm += def->inst_c0 << ins->backend.shift_amount;
245 opcode2 = mono_load_membase_to_load_mem (ins->opcode);
246 if ((srcindex == 0) && (opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0)) {
247 ins->opcode = opcode2;
248 ins->inst_imm = def->inst_c0 + ins->inst_offset;
253 else if (((def->opcode == OP_ADD_IMM) || (def->opcode == OP_LADD_IMM)) && (MONO_IS_LOAD_MEMBASE (ins) || MONO_ARCH_IS_OP_MEMBASE (ins->opcode))) {
254 /* ADD_IMM is created by spill_global_vars */
256 * We have to guarantee that def->sreg1 haven't changed since def->dreg
257 * was defined. cfg->frame_reg is assumed to remain constant.
259 if ((def->sreg1 == cfg->frame_reg) || ((mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1))) {
260 ins->inst_basereg = def->sreg1;
261 ins->inst_offset += def->inst_imm;
263 } else if ((ins->opcode == OP_ISUB_IMM) && (def->opcode == OP_IADD_IMM) && (mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1)) {
264 ins->sreg1 = def->sreg1;
265 ins->inst_imm -= def->inst_imm;
266 } else if ((ins->opcode == OP_IADD_IMM) && (def->opcode == OP_ISUB_IMM) && (mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1)) {
267 ins->sreg1 = def->sreg1;
268 ins->inst_imm -= def->inst_imm;
269 } else if (ins->opcode == OP_STOREI1_MEMBASE_REG &&
270 (def->opcode == OP_ICONV_TO_U1 || def->opcode == OP_ICONV_TO_I1 || def->opcode == OP_SEXT_I4 || (SIZEOF_REGISTER == 8 && def->opcode == OP_LCONV_TO_U1)) &&
271 (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
272 /* Avoid needless sign extension */
273 ins->sreg1 = def->sreg1;
274 } else if (ins->opcode == OP_STOREI2_MEMBASE_REG &&
275 (def->opcode == OP_ICONV_TO_U2 || def->opcode == OP_ICONV_TO_I2 || def->opcode == OP_SEXT_I4 || (SIZEOF_REGISTER == 8 && def->opcode == OP_LCONV_TO_I2)) &&
276 (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
277 /* Avoid needless sign extension */
278 ins->sreg1 = def->sreg1;
282 /* Do strength reduction here */
283 /* FIXME: Add long/float */
284 switch (ins->opcode) {
287 if (ins->dreg == ins->sreg1) {
288 MONO_DELETE_INS (bb, ins);
289 spec = INS_INFO (ins->opcode);
296 #if SIZEOF_REGISTER == 8
300 if (ins->inst_imm == 0) {
301 ins->opcode = OP_MOVE;
302 spec = INS_INFO (ins->opcode);
307 #if SIZEOF_REGISTER == 8
310 if (ins->inst_imm == 0) {
311 ins->opcode = (ins->opcode == OP_LMUL_IMM) ? OP_I8CONST : OP_ICONST;
314 } else if (ins->inst_imm == 1) {
315 ins->opcode = OP_MOVE;
316 } else if ((ins->opcode == OP_IMUL_IMM) && (ins->inst_imm == -1)) {
317 ins->opcode = OP_INEG;
318 } else if ((ins->opcode == OP_LMUL_IMM) && (ins->inst_imm == -1)) {
319 ins->opcode = OP_LNEG;
321 int power2 = mono_is_power_of_two (ins->inst_imm);
323 ins->opcode = (ins->opcode == OP_MUL_IMM) ? OP_SHL_IMM : ((ins->opcode == OP_LMUL_IMM) ? OP_LSHL_IMM : OP_ISHL_IMM);
324 ins->inst_imm = power2;
327 spec = INS_INFO (ins->opcode);
330 case OP_IDIV_UN_IMM: {
331 int c = ins->inst_imm;
332 int power2 = mono_is_power_of_two (c);
335 if (ins->opcode == OP_IREM_UN_IMM) {
336 ins->opcode = OP_IAND_IMM;
338 ins->inst_imm = (1 << power2) - 1;
339 } else if (ins->opcode == OP_IDIV_UN_IMM) {
340 ins->opcode = OP_ISHR_UN_IMM;
342 ins->inst_imm = power2;
345 spec = INS_INFO (ins->opcode);
349 int c = ins->inst_imm;
350 int power2 = mono_is_power_of_two (c);
351 MonoInst *tmp1, *tmp2, *tmp3, *tmp4;
353 /* FIXME: Move this elsewhere cause its hard to implement it here */
355 int r1 = mono_alloc_ireg (cfg);
357 NEW_BIALU_IMM (cfg, tmp1, OP_ISHR_UN_IMM, r1, ins->sreg1, 31);
358 mono_bblock_insert_after_ins (bb, ins, tmp1);
359 NEW_BIALU (cfg, tmp2, OP_IADD, r1, r1, ins->sreg1);
360 mono_bblock_insert_after_ins (bb, tmp1, tmp2);
361 NEW_BIALU_IMM (cfg, tmp3, OP_ISHR_IMM, ins->dreg, r1, 1);
362 mono_bblock_insert_after_ins (bb, tmp2, tmp3);
366 // We allocated a new vreg, so need to restart
368 } else if (power2 > 0) {
369 int r1 = mono_alloc_ireg (cfg);
371 NEW_BIALU_IMM (cfg, tmp1, OP_ISHR_IMM, r1, ins->sreg1, 31);
372 mono_bblock_insert_after_ins (bb, ins, tmp1);
373 NEW_BIALU_IMM (cfg, tmp2, OP_ISHR_UN_IMM, r1, r1, (32 - power2));
374 mono_bblock_insert_after_ins (bb, tmp1, tmp2);
375 NEW_BIALU (cfg, tmp3, OP_IADD, r1, r1, ins->sreg1);
376 mono_bblock_insert_after_ins (bb, tmp2, tmp3);
377 NEW_BIALU_IMM (cfg, tmp4, OP_ISHR_IMM, ins->dreg, r1, power2);
378 mono_bblock_insert_after_ins (bb, tmp3, tmp4);
382 // We allocated a new vreg, so need to restart
389 if (spec [MONO_INST_DEST] != ' ') {
390 MonoInst *def = defs [ins->dreg];
392 if (def && (def->opcode == OP_ADD_IMM) && (def->sreg1 == cfg->frame_reg) && (MONO_IS_STORE_MEMBASE (ins))) {
393 /* ADD_IMM is created by spill_global_vars */
394 /* cfg->frame_reg is assumed to remain constant */
395 ins->inst_destbasereg = def->sreg1;
396 ins->inst_offset += def->inst_imm;
400 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins) && !vreg_is_volatile (cfg, ins->dreg)) {
401 defs [ins->dreg] = ins;
402 def_index [ins->dreg] = ins_index;
405 if (MONO_IS_CALL (ins))
406 last_call_index = ins_index;
413 static inline gboolean
414 reg_is_softreg_no_fpstack (int reg, const char spec)
416 return (spec == 'i' && reg >= MONO_MAX_IREGS)
417 || ((spec == 'f' && reg >= MONO_MAX_FREGS) && !MONO_ARCH_USE_FPSTACK)
418 #ifdef MONO_ARCH_SIMD_INTRINSICS
419 || (spec == 'x' && reg >= MONO_MAX_XREGS)
424 static inline gboolean
425 reg_is_softreg (int reg, const char spec)
427 return (spec == 'i' && reg >= MONO_MAX_IREGS)
428 || (spec == 'f' && reg >= MONO_MAX_FREGS)
429 #ifdef MONO_ARCH_SIMD_INTRINSICS
430 || (spec == 'x' && reg >= MONO_MAX_XREGS)
435 static inline gboolean
436 mono_is_simd_accessor (MonoInst *ins)
438 switch (ins->opcode) {
439 #ifdef MONO_ARCH_SIMD_INTRINSICS
447 case OP_INSERTX_U1_SLOW:
448 case OP_INSERTX_I4_SLOW:
449 case OP_INSERTX_R4_SLOW:
450 case OP_INSERTX_R8_SLOW:
451 case OP_INSERTX_I8_SLOW:
462 * Get rid of the dead assignments to local vregs like the ones created by the
466 mono_local_deadce (MonoCompile *cfg)
469 MonoInst *ins, *prev;
470 MonoBitSet *used, *defined;
472 //mono_print_code (cfg, "BEFORE LOCAL-DEADCE");
475 * Assignments to global vregs can't be eliminated so this pass must come
476 * after the handle_global_vregs () pass.
479 used = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
480 defined = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
482 /* First pass: collect liveness info */
483 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
484 /* Manually init the defs entries used by the bblock */
485 MONO_BB_FOR_EACH_INS (bb, ins) {
486 const char *spec = INS_INFO (ins->opcode);
487 int sregs [MONO_MAX_SRC_REGS];
490 if (spec [MONO_INST_DEST] != ' ') {
491 mono_bitset_clear_fast (used, ins->dreg);
492 mono_bitset_clear_fast (defined, ins->dreg);
493 #if SIZEOF_REGISTER == 4
495 mono_bitset_clear_fast (used, ins->dreg + 1);
496 mono_bitset_clear_fast (defined, ins->dreg + 1);
499 num_sregs = mono_inst_get_src_registers (ins, sregs);
500 for (i = 0; i < num_sregs; ++i) {
501 mono_bitset_clear_fast (used, sregs [i]);
502 #if SIZEOF_REGISTER == 4
503 mono_bitset_clear_fast (used, sregs [i] + 1);
509 * Make a reverse pass over the instruction list
511 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
512 const char *spec = INS_INFO (ins->opcode);
513 int sregs [MONO_MAX_SRC_REGS];
515 MonoInst *prev_f = mono_inst_prev (ins, FILTER_NOP | FILTER_IL_SEQ_POINT);
517 if (ins->opcode == OP_NOP) {
518 MONO_DELETE_INS (bb, ins);
522 g_assert (ins->opcode > MONO_CEE_LAST);
524 if (MONO_IS_NON_FP_MOVE (ins) && prev_f) {
529 spec2 = INS_INFO (def->opcode);
532 * Perform a limited kind of reverse copy propagation, i.e.
533 * transform B <- FOO; A <- B into A <- FOO
534 * This isn't copyprop, not deadce, but it can only be performed
535 * after handle_global_vregs () has run.
537 if (!get_vreg_to_inst (cfg, ins->sreg1) && (spec2 [MONO_INST_DEST] != ' ') && (def->dreg == ins->sreg1) && !mono_bitset_test_fast (used, ins->sreg1) && !MONO_IS_STORE_MEMBASE (def) && reg_is_softreg (ins->sreg1, spec [MONO_INST_DEST]) && !mono_is_simd_accessor (def)) {
538 if (cfg->verbose_level > 2) {
539 printf ("\tReverse copyprop in BB%d on ", bb->block_num);
540 mono_print_ins (ins);
543 def->dreg = ins->dreg;
544 MONO_DELETE_INS (bb, ins);
545 spec = INS_INFO (ins->opcode);
549 /* Enabling this on x86 could screw up the fp stack */
550 if (reg_is_softreg_no_fpstack (ins->dreg, spec [MONO_INST_DEST])) {
552 * Assignments to global vregs can only be eliminated if there is another
553 * assignment to the same vreg later in the same bblock.
555 if (!mono_bitset_test_fast (used, ins->dreg) &&
556 (!get_vreg_to_inst (cfg, ins->dreg) || (!bb->extended && !vreg_is_volatile (cfg, ins->dreg) && mono_bitset_test_fast (defined, ins->dreg))) &&
557 MONO_INS_HAS_NO_SIDE_EFFECT (ins)) {
558 /* Happens with CMOV instructions */
559 if (prev_f && prev_f->opcode == OP_ICOMPARE_IMM) {
560 MonoInst *prev = prev_f;
562 * Can't use DELETE_INS since that would interfere with the
567 //printf ("DEADCE: "); mono_print_ins (ins);
568 MONO_DELETE_INS (bb, ins);
569 spec = INS_INFO (ins->opcode);
572 if (spec [MONO_INST_DEST] != ' ')
573 mono_bitset_clear_fast (used, ins->dreg);
576 if (spec [MONO_INST_DEST] != ' ')
577 mono_bitset_set_fast (defined, ins->dreg);
578 num_sregs = mono_inst_get_src_registers (ins, sregs);
579 for (i = 0; i < num_sregs; ++i)
580 mono_bitset_set_fast (used, sregs [i]);
581 if (MONO_IS_STORE_MEMBASE (ins))
582 mono_bitset_set_fast (used, ins->dreg);
584 if (MONO_IS_CALL (ins)) {
585 MonoCallInst *call = (MonoCallInst*)ins;
588 if (call->out_ireg_args) {
589 for (l = call->out_ireg_args; l; l = l->next) {
590 guint32 regpair, reg;
592 regpair = (guint32)(gssize)(l->data);
593 reg = regpair & 0xffffff;
595 mono_bitset_set_fast (used, reg);
599 if (call->out_freg_args) {
600 for (l = call->out_freg_args; l; l = l->next) {
601 guint32 regpair, reg;
603 regpair = (guint32)(gssize)(l->data);
604 reg = regpair & 0xffffff;
606 mono_bitset_set_fast (used, reg);
613 //mono_print_code (cfg, "AFTER LOCAL-DEADCE");
616 #endif /* DISABLE_JIT */