[runtime] Cycle the finalizer thread if there are too many outstanding hazard pointer...
[mono.git] / mono / mini / local-propagation.c
1 /*
2  * local-propagation.c: Local constant, copy and tree propagation.
3  *
4  * To make some sense of the tree mover, read mono/docs/tree-mover.txt
5  *
6  * Author:
7  *   Paolo Molaro (lupus@ximian.com)
8  *   Dietmar Maurer (dietmar@ximian.com)
9  *   Massimiliano Mantione (massi@ximian.com)
10  *
11  * (C) 2006 Novell, Inc.  http://www.novell.com
12  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13  */
14
15 #include <config.h>
16 #ifndef DISABLE_JIT
17
18 #include <string.h>
19 #include <stdio.h>
20 #ifdef HAVE_ALLOCA_H
21 #include <alloca.h>
22 #endif
23
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/mempool.h>
26 #include <mono/metadata/opcodes.h>
27 #include "mini.h"
28 #include "ir-emit.h"
29
30 #ifndef MONO_ARCH_IS_OP_MEMBASE
31 #define MONO_ARCH_IS_OP_MEMBASE(opcode) FALSE
32 #endif
33
34 static inline MonoBitSet* 
35 mono_bitset_mp_new_noinit (MonoMemPool *mp,  guint32 max_size)
36 {
37         int size = mono_bitset_alloc_size (max_size, 0);
38         gpointer mem;
39
40         mem = mono_mempool_alloc (mp, size);
41         return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
42 }
43
44 /*
45  * Replaces ins with optimized opcodes.
46  *
47  * We can emit to cbb the equivalent instructions which will be used as
48  * replacement for ins, or simply change the fields of ins. Spec needs to
49  * be updated if we silently change the opcode of ins.
50  *
51  * Returns TRUE if additional vregs were allocated.
52  */
53 static gboolean
54 mono_strength_reduction_ins (MonoCompile *cfg, MonoInst *ins, const char **spec)
55 {
56         gboolean allocated_vregs = FALSE;
57
58         /* FIXME: Add long/float */
59         switch (ins->opcode) {
60         case OP_MOVE:
61         case OP_XMOVE:
62                 if (ins->dreg == ins->sreg1) {
63                         NULLIFY_INS (ins);
64                 }
65                 break;
66         case OP_ADD_IMM:
67         case OP_IADD_IMM:
68         case OP_SUB_IMM:
69         case OP_ISUB_IMM:
70 #if SIZEOF_REGISTER == 8
71         case OP_LADD_IMM:
72         case OP_LSUB_IMM:
73 #endif
74                 if (ins->inst_imm == 0) {
75                         ins->opcode = OP_MOVE;
76                 }
77                 break;
78         case OP_MUL_IMM:
79         case OP_IMUL_IMM:
80 #if SIZEOF_REGISTER == 8
81         case OP_LMUL_IMM:
82 #endif
83                 if (ins->inst_imm == 0) {
84                         ins->opcode = (ins->opcode == OP_LMUL_IMM) ? OP_I8CONST : OP_ICONST;
85                         ins->inst_c0 = 0;
86                         ins->sreg1 = -1;
87                 } else if (ins->inst_imm == 1) {
88                         ins->opcode = OP_MOVE;
89                 } else if ((ins->opcode == OP_IMUL_IMM) && (ins->inst_imm == -1)) {
90                         ins->opcode = OP_INEG;
91                 } else if ((ins->opcode == OP_LMUL_IMM) && (ins->inst_imm == -1)) {
92                         ins->opcode = OP_LNEG;
93                 } else {
94                         int power2 = mono_is_power_of_two (ins->inst_imm);
95                         if (power2 >= 0) {
96                                 ins->opcode = (ins->opcode == OP_MUL_IMM) ? OP_SHL_IMM : ((ins->opcode == OP_LMUL_IMM) ? OP_LSHL_IMM : OP_ISHL_IMM);
97                                 ins->inst_imm = power2;
98                         }
99                 }
100                 break;
101         case OP_IREM_UN_IMM:
102         case OP_IDIV_UN_IMM: {
103                 int c = ins->inst_imm;
104                 int power2 = mono_is_power_of_two (c);
105
106                 if (power2 >= 0) {
107                         if (ins->opcode == OP_IREM_UN_IMM) {
108                                 ins->opcode = OP_IAND_IMM;
109                                 ins->sreg2 = -1;
110                                 ins->inst_imm = (1 << power2) - 1;
111                         } else if (ins->opcode == OP_IDIV_UN_IMM) {
112                                 ins->opcode = OP_ISHR_UN_IMM;
113                                 ins->sreg2 = -1;
114                                 ins->inst_imm = power2;
115                         }
116                 }
117                 break;
118         }
119         case OP_IDIV_IMM: {
120                 int c = ins->inst_imm;
121                 int power2 = mono_is_power_of_two (c);
122
123                 if (power2 == 1) {
124                         int r1 = mono_alloc_ireg (cfg);
125
126                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, r1, ins->sreg1, 31);
127                         MONO_EMIT_NEW_BIALU (cfg, OP_IADD, r1, r1, ins->sreg1);
128                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, ins->dreg, r1, 1);
129
130                         allocated_vregs = TRUE;
131                 } else if (power2 > 0 && power2 < 31) {
132                         int r1 = mono_alloc_ireg (cfg);
133
134                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, r1, ins->sreg1, 31);
135                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, r1, r1, (32 - power2));
136                         MONO_EMIT_NEW_BIALU (cfg, OP_IADD, r1, r1, ins->sreg1);
137                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, ins->dreg, r1, power2);
138
139                         allocated_vregs = TRUE;
140                 }
141                 break;
142         }
143 #if SIZEOF_REGISTER == 8
144         case OP_LREM_IMM:
145 #endif
146         case OP_IREM_IMM: {
147                 int power = mono_is_power_of_two (ins->inst_imm);
148                 if (ins->inst_imm == 1) {
149                         ins->opcode = OP_ICONST;
150                         MONO_INST_NULLIFY_SREGS (ins);
151                         ins->inst_c0 = 0;
152 #if __s390__
153                 }
154 #else
155                 } else if ((ins->inst_imm > 0) && (ins->inst_imm < (1LL << 32)) && (power != -1)) {
156                         gboolean is_long = ins->opcode == OP_LREM_IMM;
157                         int compensator_reg = alloc_ireg (cfg);
158                         int intermediate_reg;
159
160                         /* Based on gcc code */
161
162                         /* Add compensation for negative numerators */
163
164                         if (power > 1) {
165                                 intermediate_reg = compensator_reg;
166                                 MONO_EMIT_NEW_BIALU_IMM (cfg, is_long ? OP_LSHR_IMM : OP_ISHR_IMM, intermediate_reg, ins->sreg1, is_long ? 63 : 31);
167                         } else {
168                                 intermediate_reg = ins->sreg1;
169                         }
170
171                         MONO_EMIT_NEW_BIALU_IMM (cfg, is_long ? OP_LSHR_UN_IMM : OP_ISHR_UN_IMM, compensator_reg, intermediate_reg, (is_long ? 64 : 32) - power);
172                         MONO_EMIT_NEW_BIALU (cfg, is_long ? OP_LADD : OP_IADD, ins->dreg, ins->sreg1, compensator_reg);
173                         /* Compute remainder */
174                         MONO_EMIT_NEW_BIALU_IMM (cfg, is_long ? OP_LAND_IMM : OP_AND_IMM, ins->dreg, ins->dreg, (1 << power) - 1);
175                         /* Remove compensation */
176                         MONO_EMIT_NEW_BIALU (cfg, is_long ? OP_LSUB : OP_ISUB, ins->dreg, ins->dreg, compensator_reg);
177
178                         allocated_vregs = TRUE;
179                 }
180 #endif
181                 break;
182         }
183
184         default:
185                 break;
186         }
187
188         *spec = INS_INFO (ins->opcode);
189         return allocated_vregs;
190 }
191
192 /*
193  * mono_local_cprop:
194  *
195  *  A combined local copy and constant propagation pass.
196  */
197 void
198 mono_local_cprop (MonoCompile *cfg)
199 {
200         MonoBasicBlock *bb, *bb_opt;
201         MonoInst **defs;
202         gint32 *def_index;
203         int max;
204         int filter = FILTER_IL_SEQ_POINT;
205         int initial_max_vregs = cfg->next_vreg;
206
207         max = cfg->next_vreg;
208         defs = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * cfg->next_vreg);
209         def_index = (gint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
210         cfg->cbb = bb_opt = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
211
212         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
213                 MonoInst *ins;
214                 int ins_index;
215                 int last_call_index;
216
217                 /* Manually init the defs entries used by the bblock */
218                 MONO_BB_FOR_EACH_INS (bb, ins) {
219                         int sregs [MONO_MAX_SRC_REGS];
220                         int num_sregs, i;
221
222                         if (ins->dreg != -1) {
223 #if SIZEOF_REGISTER == 4
224                                 const char *spec = INS_INFO (ins->opcode);
225                                 if (spec [MONO_INST_DEST] == 'l') {
226                                         defs [ins->dreg + 1] = NULL;
227                                         defs [ins->dreg + 2] = NULL;
228                                 }
229 #endif
230                                 defs [ins->dreg] = NULL;
231                         }
232
233                         num_sregs = mono_inst_get_src_registers (ins, sregs);
234                         for (i = 0; i < num_sregs; ++i) {
235                                 int sreg = sregs [i];
236 #if SIZEOF_REGISTER == 4
237                                 const char *spec = INS_INFO (ins->opcode);
238                                 if (spec [MONO_INST_SRC1 + i] == 'l') {
239                                         defs [sreg + 1] = NULL;
240                                         defs [sreg + 2] = NULL;
241                                 }
242 #endif
243                                 defs [sreg] = NULL;
244                         }
245                 }
246
247                 ins_index = 0;
248                 last_call_index = -1;
249                 MONO_BB_FOR_EACH_INS (bb, ins) {
250                         const char *spec = INS_INFO (ins->opcode);
251                         int regtype, srcindex, sreg;
252                         int num_sregs;
253                         int sregs [MONO_MAX_SRC_REGS];
254
255                         if (ins->opcode == OP_NOP) {
256                                 MONO_DELETE_INS (bb, ins);
257                                 continue;
258                         }
259
260                         g_assert (ins->opcode > MONO_CEE_LAST);
261
262                         /* FIXME: Optimize this */
263                         if (ins->opcode == OP_LDADDR) {
264                                 MonoInst *var = (MonoInst *)ins->inst_p0;
265
266                                 defs [var->dreg] = NULL;
267                                 /*
268                                 if (!MONO_TYPE_ISSTRUCT (var->inst_vtype))
269                                         break;
270                                 */
271                         }
272
273                         if (MONO_IS_STORE_MEMBASE (ins)) {
274                                 sreg = ins->dreg;
275                                 regtype = 'i';
276
277                                 if ((regtype == 'i') && (sreg != -1) && defs [sreg]) {
278                                         MonoInst *def = defs [sreg];
279
280                                         if ((def->opcode == OP_MOVE) && (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) && !vreg_is_volatile (cfg, def->sreg1)) {
281                                                 int vreg = def->sreg1;
282                                                 if (cfg->verbose_level > 2) printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
283                                                 ins->dreg = vreg;
284                                         }
285                                 }
286                         }
287
288                         num_sregs = mono_inst_get_src_registers (ins, sregs);
289                         for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
290                                 MonoInst *def;
291
292                                 mono_inst_get_src_registers (ins, sregs);
293
294                                 regtype = spec [MONO_INST_SRC1 + srcindex];
295                                 sreg = sregs [srcindex];
296
297                                 if ((regtype == ' ') || (sreg == -1) || (!defs [sreg]))
298                                         continue;
299
300                                 def = defs [sreg];
301
302                                 /* Copy propagation */
303                                 /* 
304                                  * The first check makes sure the source of the copy did not change since 
305                                  * the copy was made.
306                                  * The second check avoids volatile variables.
307                                  * The third check avoids copy propagating local vregs through a call, 
308                                  * since the lvreg will be spilled 
309                                  * The fourth check avoids copy propagating a vreg in cases where
310                                  * it would be eliminated anyway by reverse copy propagation later,
311                                  * because propagating it would create another use for it, thus making 
312                                  * it impossible to use reverse copy propagation.
313                                  */
314                                 /* Enabling this for floats trips up the fp stack */
315                                 /* 
316                                  * Enabling this for floats on amd64 seems to cause a failure in 
317                                  * basic-math.cs, most likely because it gets rid of some r8->r4 
318                                  * conversions.
319                                  */
320                                 if (MONO_IS_MOVE (def) &&
321                                         (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) &&
322                                         !vreg_is_volatile (cfg, def->sreg1) &&
323                                         /* This avoids propagating local vregs across calls */
324                                         ((get_vreg_to_inst (cfg, def->sreg1) || !defs [def->sreg1] || (def_index [def->sreg1] >= last_call_index) || (def->opcode == OP_VMOVE))) &&
325                                         !(defs [def->sreg1] && mono_inst_next (defs [def->sreg1], filter) == def) &&
326                                         (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE)) &&
327                                         (def->opcode != OP_FMOVE)) {
328                                         int vreg = def->sreg1;
329
330                                         if (cfg->verbose_level > 2) printf ("CCOPY/2: R%d -> R%d\n", sreg, vreg);
331                                         sregs [srcindex] = vreg;
332                                         mono_inst_set_src_registers (ins, sregs);
333
334                                         /* Allow further iterations */
335                                         srcindex = -1;
336                                         continue;
337                                 }
338
339                                 /* Constant propagation */
340                                 /* FIXME: Make is_inst_imm a macro */
341                                 /* FIXME: Make is_inst_imm take an opcode argument */
342                                 /* is_inst_imm is only needed for binops */
343                                 if ((((def->opcode == OP_ICONST) || ((sizeof (gpointer) == 8) && (def->opcode == OP_I8CONST))) &&
344                                          (((srcindex == 0) && (ins->sreg2 == -1)) || mono_arch_is_inst_imm (def->inst_c0))) || 
345                                         (!MONO_ARCH_USE_FPSTACK && (def->opcode == OP_R8CONST))) {
346                                         guint32 opcode2;
347
348                                         /* srcindex == 1 -> binop, ins->sreg2 == -1 -> unop */
349                                         if ((srcindex == 1) && (ins->sreg1 != -1) && defs [ins->sreg1] && (defs [ins->sreg1]->opcode == OP_ICONST) && defs [ins->sreg2]) {
350                                                 /* Both arguments are constants, perform cfold */
351                                                 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
352                                         } else if ((srcindex == 0) && (ins->sreg2 != -1) && defs [ins->sreg2]) {
353                                                 /* Arg 1 is constant, swap arguments if possible */
354                                                 int opcode = ins->opcode;
355                                                 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
356                                                 if (ins->opcode != opcode) {
357                                                         /* Allow further iterations */
358                                                         srcindex = -1;
359                                                         continue;
360                                                 }
361                                         } else if ((srcindex == 0) && (ins->sreg2 == -1)) {
362                                                 /* Constant unop, perform cfold */
363                                                 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], NULL, TRUE);
364                                         }
365
366                                         opcode2 = mono_op_to_op_imm (ins->opcode);
367                                         if ((opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0) && ((srcindex == 1) || (ins->sreg2 == -1))) {
368                                                 ins->opcode = opcode2;
369                                                 if ((def->opcode == OP_I8CONST) && (sizeof (gpointer) == 4)) {
370                                                         ins->inst_ls_word = def->inst_ls_word;
371                                                         ins->inst_ms_word = def->inst_ms_word;
372                                                 } else {
373                                                         ins->inst_imm = def->inst_c0;
374                                                 }
375                                                 sregs [srcindex] = -1;
376                                                 mono_inst_set_src_registers (ins, sregs);
377
378                                                 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
379                                                         ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
380
381                                                 /* Allow further iterations */
382                                                 srcindex = -1;
383                                                 continue;
384                                         }
385                                         else {
386                                                 /* Special cases */
387 #if defined(TARGET_X86) || defined(TARGET_AMD64)
388                                                 if ((ins->opcode == OP_X86_LEA) && (srcindex == 1)) {
389 #if SIZEOF_REGISTER == 8
390                                                         /* FIXME: Use OP_PADD_IMM when the new JIT is done */
391                                                         ins->opcode = OP_LADD_IMM;
392 #else
393                                                         ins->opcode = OP_ADD_IMM;
394 #endif
395                                                         ins->inst_imm += def->inst_c0 << ins->backend.shift_amount;
396                                                         ins->sreg2 = -1;
397                                                 }
398 #endif
399                                                 opcode2 = mono_load_membase_to_load_mem (ins->opcode);
400                                                 if ((srcindex == 0) && (opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0)) {
401                                                         ins->opcode = opcode2;
402                                                         ins->inst_imm = def->inst_c0 + ins->inst_offset;
403                                                         ins->sreg1 = -1;
404                                                 }
405                                         }
406                                 }
407                                 else if (((def->opcode == OP_ADD_IMM) || (def->opcode == OP_LADD_IMM)) && (MONO_IS_LOAD_MEMBASE (ins) || MONO_ARCH_IS_OP_MEMBASE (ins->opcode))) {
408                                         /* ADD_IMM is created by spill_global_vars */
409                                         /* 
410                                          * We have to guarantee that def->sreg1 haven't changed since def->dreg
411                                          * was defined. cfg->frame_reg is assumed to remain constant.
412                                          */
413                                         if ((def->sreg1 == cfg->frame_reg) || ((mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1))) {
414                                                 ins->inst_basereg = def->sreg1;
415                                                 ins->inst_offset += def->inst_imm;
416                                         }
417                                 } else if ((ins->opcode == OP_ISUB_IMM) && (def->opcode == OP_IADD_IMM) && (mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1)) {
418                                         ins->sreg1 = def->sreg1;
419                                         ins->inst_imm -= def->inst_imm;
420                                 } else if ((ins->opcode == OP_IADD_IMM) && (def->opcode == OP_ISUB_IMM) && (mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1)) {
421                                         ins->sreg1 = def->sreg1;
422                                         ins->inst_imm -= def->inst_imm;
423                                 } else if (ins->opcode == OP_STOREI1_MEMBASE_REG &&
424                                                    (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)) &&
425                                                    (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
426                                         /* Avoid needless sign extension */
427                                         ins->sreg1 = def->sreg1;
428                                 } else if (ins->opcode == OP_STOREI2_MEMBASE_REG &&
429                                                    (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)) &&
430                                                    (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
431                                         /* Avoid needless sign extension */
432                                         ins->sreg1 = def->sreg1;
433                                 } else if (ins->opcode == OP_COMPARE_IMM && def->opcode == OP_LDADDR && ins->inst_imm == 0) {
434                                         MonoInst dummy_arg1;
435
436                                         memset (&dummy_arg1, 0, sizeof (MonoInst));
437                                         dummy_arg1.opcode = OP_ICONST;
438                                         dummy_arg1.inst_c0 = 1;
439
440                                         mono_constant_fold_ins (cfg, ins, &dummy_arg1, NULL, TRUE);
441                                 }
442                         }
443
444                         g_assert (cfg->cbb == bb_opt);
445                         g_assert (!bb_opt->code);
446                         /* Do strength reduction here */
447                         if (mono_strength_reduction_ins (cfg, ins, &spec) && max < cfg->next_vreg) {
448                                 MonoInst **defs_prev = defs;
449                                 gint32 *def_index_prev = def_index;
450                                 guint32 prev_max = max;
451                                 guint32 additional_vregs = cfg->next_vreg - initial_max_vregs;
452
453                                 /* We have more vregs so we need to reallocate defs and def_index arrays */
454                                 max  = initial_max_vregs + additional_vregs * 2;
455                                 defs = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * max);
456                                 def_index = (gint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * max);
457
458                                 /* Keep the entries for the previous vregs, zero the rest */
459                                 memcpy (defs, defs_prev, sizeof (MonoInst*) * prev_max);
460                                 memset (defs + prev_max, 0, sizeof (MonoInst*) * (max - prev_max));
461                                 memcpy (def_index, def_index_prev, sizeof (guint32) * prev_max);
462                                 memset (def_index + prev_max, 0, sizeof (guint32) * (max - prev_max));
463                         }
464
465                         if (cfg->cbb->code || (cfg->cbb != bb_opt)) {
466                                 MonoInst *saved_prev = ins->prev;
467
468                                 /* If we have code in cbb, we need to replace ins with the decomposition */
469                                 mono_replace_ins (cfg, bb, ins, &ins->prev, bb_opt, cfg->cbb);
470                                 bb_opt->code = bb_opt->last_ins = NULL;
471                                 bb_opt->in_count = bb_opt->out_count = 0;
472                                 cfg->cbb = bb_opt;
473
474                                 /* ins is hanging, continue scanning the emitted code */
475                                 ins = saved_prev;
476                                 continue;
477                         }
478
479                         if (spec [MONO_INST_DEST] != ' ') {
480                                 MonoInst *def = defs [ins->dreg];
481
482                                 if (def && (def->opcode == OP_ADD_IMM) && (def->sreg1 == cfg->frame_reg) && (MONO_IS_STORE_MEMBASE (ins))) {
483                                         /* ADD_IMM is created by spill_global_vars */
484                                         /* cfg->frame_reg is assumed to remain constant */
485                                         ins->inst_destbasereg = def->sreg1;
486                                         ins->inst_offset += def->inst_imm;
487                                 }
488                         }
489                         
490                         if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins) && !vreg_is_volatile (cfg, ins->dreg)) {
491                                 defs [ins->dreg] = ins;
492                                 def_index [ins->dreg] = ins_index;
493                         }
494
495                         if (MONO_IS_CALL (ins))
496                                 last_call_index = ins_index;
497
498                         ins_index ++;
499                 }
500         }
501 }
502
503 static inline gboolean
504 reg_is_softreg_no_fpstack (int reg, const char spec)
505 {
506         return (spec == 'i' && reg >= MONO_MAX_IREGS)
507                 || ((spec == 'f' && reg >= MONO_MAX_FREGS) && !MONO_ARCH_USE_FPSTACK)
508 #ifdef MONO_ARCH_SIMD_INTRINSICS
509                 || (spec == 'x' && reg >= MONO_MAX_XREGS)
510 #endif
511                 || (spec == 'v');
512 }
513                 
514 static inline gboolean
515 reg_is_softreg (int reg, const char spec)
516 {
517         return (spec == 'i' && reg >= MONO_MAX_IREGS)
518                 || (spec == 'f' && reg >= MONO_MAX_FREGS)
519 #ifdef MONO_ARCH_SIMD_INTRINSICS
520                 || (spec == 'x' && reg >= MONO_MAX_XREGS)
521 #endif
522                 || (spec == 'v');
523 }
524
525 static inline gboolean
526 mono_is_simd_accessor (MonoInst *ins)
527 {
528         switch (ins->opcode) {
529 #ifdef MONO_ARCH_SIMD_INTRINSICS
530         case OP_INSERT_I1:
531         case OP_INSERT_I2:
532         case OP_INSERT_I4:
533         case OP_INSERT_I8:
534         case OP_INSERT_R4:
535         case OP_INSERT_R8:
536
537         case OP_INSERTX_U1_SLOW:
538         case OP_INSERTX_I4_SLOW:
539         case OP_INSERTX_R4_SLOW:
540         case OP_INSERTX_R8_SLOW:
541         case OP_INSERTX_I8_SLOW:
542                 return TRUE;
543 #endif
544         default:
545                 return FALSE;
546         }
547 }
548
549 /**
550  * mono_local_deadce:
551  *
552  *   Get rid of the dead assignments to local vregs like the ones created by the 
553  * copyprop pass.
554  */
555 void
556 mono_local_deadce (MonoCompile *cfg)
557 {
558         MonoBasicBlock *bb;
559         MonoInst *ins, *prev;
560         MonoBitSet *used, *defined;
561
562         //mono_print_code (cfg, "BEFORE LOCAL-DEADCE");
563
564         /*
565          * Assignments to global vregs can't be eliminated so this pass must come
566          * after the handle_global_vregs () pass.
567          */
568
569         used = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
570         defined = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
571
572         /* First pass: collect liveness info */
573         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
574                 /* Manually init the defs entries used by the bblock */
575                 MONO_BB_FOR_EACH_INS (bb, ins) {
576                         const char *spec = INS_INFO (ins->opcode);
577                         int sregs [MONO_MAX_SRC_REGS];
578                         int num_sregs, i;
579
580                         if (spec [MONO_INST_DEST] != ' ') {
581                                 mono_bitset_clear_fast (used, ins->dreg);
582                                 mono_bitset_clear_fast (defined, ins->dreg);
583 #if SIZEOF_REGISTER == 4
584                                 /* Regpairs */
585                                 mono_bitset_clear_fast (used, ins->dreg + 1);
586                                 mono_bitset_clear_fast (defined, ins->dreg + 1);
587 #endif
588                         }
589                         num_sregs = mono_inst_get_src_registers (ins, sregs);
590                         for (i = 0; i < num_sregs; ++i) {
591                                 mono_bitset_clear_fast (used, sregs [i]);
592 #if SIZEOF_REGISTER == 4
593                                 mono_bitset_clear_fast (used, sregs [i] + 1);
594 #endif
595                         }
596                 }
597
598                 /*
599                  * Make a reverse pass over the instruction list
600                  */
601                 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
602                         const char *spec = INS_INFO (ins->opcode);
603                         int sregs [MONO_MAX_SRC_REGS];
604                         int num_sregs, i;
605                         MonoInst *prev_f = mono_inst_prev (ins, FILTER_NOP | FILTER_IL_SEQ_POINT);
606
607                         if (ins->opcode == OP_NOP) {
608                                 MONO_DELETE_INS (bb, ins);
609                                 continue;
610                         }
611
612                         g_assert (ins->opcode > MONO_CEE_LAST);
613
614                         if (MONO_IS_NON_FP_MOVE (ins) && prev_f) {
615                                 MonoInst *def;
616                                 const char *spec2;
617
618                                 def = prev_f;
619                                 spec2 = INS_INFO (def->opcode);
620
621                                 /* 
622                                  * Perform a limited kind of reverse copy propagation, i.e.
623                                  * transform B <- FOO; A <- B into A <- FOO
624                                  * This isn't copyprop, not deadce, but it can only be performed
625                                  * after handle_global_vregs () has run.
626                                  */
627                                 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)) {
628                                         if (cfg->verbose_level > 2) {
629                                                 printf ("\tReverse copyprop in BB%d on ", bb->block_num);
630                                                 mono_print_ins (ins);
631                                         }
632
633                                         def->dreg = ins->dreg;
634                                         MONO_DELETE_INS (bb, ins);
635                                         spec = INS_INFO (ins->opcode);
636                                 }
637                         }
638
639                         /* Enabling this on x86 could screw up the fp stack */
640                         if (reg_is_softreg_no_fpstack (ins->dreg, spec [MONO_INST_DEST])) {
641                                 /* 
642                                  * Assignments to global vregs can only be eliminated if there is another
643                                  * assignment to the same vreg later in the same bblock.
644                                  */
645                                 if (!mono_bitset_test_fast (used, ins->dreg) && 
646                                         (!get_vreg_to_inst (cfg, ins->dreg) || (!bb->extended && !vreg_is_volatile (cfg, ins->dreg) && mono_bitset_test_fast (defined, ins->dreg))) &&
647                                         MONO_INS_HAS_NO_SIDE_EFFECT (ins)) {
648                                         /* Happens with CMOV instructions */
649                                         if (prev_f && prev_f->opcode == OP_ICOMPARE_IMM) {
650                                                 MonoInst *prev = prev_f;
651                                                 /* 
652                                                  * Can't use DELETE_INS since that would interfere with the
653                                                  * FOR_EACH_INS loop.
654                                                  */
655                                                 NULLIFY_INS (prev);
656                                         }
657                                         //printf ("DEADCE: "); mono_print_ins (ins);
658                                         MONO_DELETE_INS (bb, ins);
659                                         spec = INS_INFO (ins->opcode);
660                                 }
661
662                                 if (spec [MONO_INST_DEST] != ' ')
663                                         mono_bitset_clear_fast (used, ins->dreg);
664                         }
665
666                         if (spec [MONO_INST_DEST] != ' ')
667                                 mono_bitset_set_fast (defined, ins->dreg);
668                         num_sregs = mono_inst_get_src_registers (ins, sregs);
669                         for (i = 0; i < num_sregs; ++i)
670                                 mono_bitset_set_fast (used, sregs [i]);
671                         if (MONO_IS_STORE_MEMBASE (ins))
672                                 mono_bitset_set_fast (used, ins->dreg);
673
674                         if (MONO_IS_CALL (ins)) {
675                                 MonoCallInst *call = (MonoCallInst*)ins;
676                                 GSList *l;
677
678                                 if (call->out_ireg_args) {
679                                         for (l = call->out_ireg_args; l; l = l->next) {
680                                                 guint32 regpair, reg;
681
682                                                 regpair = (guint32)(gssize)(l->data);
683                                                 reg = regpair & 0xffffff;
684                                         
685                                                 mono_bitset_set_fast (used, reg);
686                                         }
687                                 }
688
689                                 if (call->out_freg_args) {
690                                         for (l = call->out_freg_args; l; l = l->next) {
691                                                 guint32 regpair, reg;
692
693                                                 regpair = (guint32)(gssize)(l->data);
694                                                 reg = regpair & 0xffffff;
695                                         
696                                                 mono_bitset_set_fast (used, reg);
697                                         }
698                                 }
699                         }
700                 }
701         }
702
703         //mono_print_code (cfg, "AFTER LOCAL-DEADCE");
704 }
705
706 #endif /* DISABLE_JIT */