Merge pull request #2532 from lambdageek/monoerror-mono_object_new
[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) && (ins->dreg < max)) {
223                                 defs [ins->dreg] = NULL;
224 #if SIZEOF_REGISTER == 4
225                                 defs [ins->dreg + 1] = NULL;
226 #endif
227                         }
228
229                         num_sregs = mono_inst_get_src_registers (ins, sregs);
230                         for (i = 0; i < num_sregs; ++i) {
231                                 int sreg = sregs [i];
232                                 if (sreg < max) {
233                                         defs [sreg] = NULL;
234 #if SIZEOF_REGISTER == 4
235                                         defs [sreg + 1] = NULL;
236 #endif
237                                 }
238                         }
239                 }
240
241                 ins_index = 0;
242                 last_call_index = -1;
243                 MONO_BB_FOR_EACH_INS (bb, ins) {
244                         const char *spec = INS_INFO (ins->opcode);
245                         int regtype, srcindex, sreg;
246                         int num_sregs;
247                         int sregs [MONO_MAX_SRC_REGS];
248
249                         if (ins->opcode == OP_NOP) {
250                                 MONO_DELETE_INS (bb, ins);
251                                 continue;
252                         }
253
254                         g_assert (ins->opcode > MONO_CEE_LAST);
255
256                         /* FIXME: Optimize this */
257                         if (ins->opcode == OP_LDADDR) {
258                                 MonoInst *var = (MonoInst *)ins->inst_p0;
259
260                                 defs [var->dreg] = NULL;
261                                 /*
262                                 if (!MONO_TYPE_ISSTRUCT (var->inst_vtype))
263                                         break;
264                                 */
265                         }
266
267                         if (MONO_IS_STORE_MEMBASE (ins)) {
268                                 sreg = ins->dreg;
269                                 regtype = 'i';
270
271                                 if ((regtype == 'i') && (sreg != -1) && defs [sreg]) {
272                                         MonoInst *def = defs [sreg];
273
274                                         if ((def->opcode == OP_MOVE) && (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) && !vreg_is_volatile (cfg, def->sreg1)) {
275                                                 int vreg = def->sreg1;
276                                                 if (cfg->verbose_level > 2) printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
277                                                 ins->dreg = vreg;
278                                         }
279                                 }
280                         }
281
282                         num_sregs = mono_inst_get_src_registers (ins, sregs);
283                         for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
284                                 MonoInst *def;
285
286                                 mono_inst_get_src_registers (ins, sregs);
287
288                                 regtype = spec [MONO_INST_SRC1 + srcindex];
289                                 sreg = sregs [srcindex];
290
291                                 if ((regtype == ' ') || (sreg == -1) || (!defs [sreg]))
292                                         continue;
293
294                                 def = defs [sreg];
295
296                                 /* Copy propagation */
297                                 /* 
298                                  * The first check makes sure the source of the copy did not change since 
299                                  * the copy was made.
300                                  * The second check avoids volatile variables.
301                                  * The third check avoids copy propagating local vregs through a call, 
302                                  * since the lvreg will be spilled 
303                                  * The fourth check avoids copy propagating a vreg in cases where
304                                  * it would be eliminated anyway by reverse copy propagation later,
305                                  * because propagating it would create another use for it, thus making 
306                                  * it impossible to use reverse copy propagation.
307                                  */
308                                 /* Enabling this for floats trips up the fp stack */
309                                 /* 
310                                  * Enabling this for floats on amd64 seems to cause a failure in 
311                                  * basic-math.cs, most likely because it gets rid of some r8->r4 
312                                  * conversions.
313                                  */
314                                 if (MONO_IS_MOVE (def) &&
315                                         (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) &&
316                                         !vreg_is_volatile (cfg, def->sreg1) &&
317                                         /* This avoids propagating local vregs across calls */
318                                         ((get_vreg_to_inst (cfg, def->sreg1) || !defs [def->sreg1] || (def_index [def->sreg1] >= last_call_index) || (def->opcode == OP_VMOVE))) &&
319                                         !(defs [def->sreg1] && mono_inst_next (defs [def->sreg1], filter) == def) &&
320                                         (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE)) &&
321                                         (def->opcode != OP_FMOVE)) {
322                                         int vreg = def->sreg1;
323
324                                         if (cfg->verbose_level > 2) printf ("CCOPY/2: R%d -> R%d\n", sreg, vreg);
325                                         sregs [srcindex] = vreg;
326                                         mono_inst_set_src_registers (ins, sregs);
327
328                                         /* Allow further iterations */
329                                         srcindex = -1;
330                                         continue;
331                                 }
332
333                                 /* Constant propagation */
334                                 /* FIXME: Make is_inst_imm a macro */
335                                 /* FIXME: Make is_inst_imm take an opcode argument */
336                                 /* is_inst_imm is only needed for binops */
337                                 if ((((def->opcode == OP_ICONST) || ((sizeof (gpointer) == 8) && (def->opcode == OP_I8CONST))) &&
338                                          (((srcindex == 0) && (ins->sreg2 == -1)) || mono_arch_is_inst_imm (def->inst_c0))) || 
339                                         (!MONO_ARCH_USE_FPSTACK && (def->opcode == OP_R8CONST))) {
340                                         guint32 opcode2;
341
342                                         /* srcindex == 1 -> binop, ins->sreg2 == -1 -> unop */
343                                         if ((srcindex == 1) && (ins->sreg1 != -1) && defs [ins->sreg1] && (defs [ins->sreg1]->opcode == OP_ICONST) && defs [ins->sreg2]) {
344                                                 /* Both arguments are constants, perform cfold */
345                                                 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
346                                         } else if ((srcindex == 0) && (ins->sreg2 != -1) && defs [ins->sreg2]) {
347                                                 /* Arg 1 is constant, swap arguments if possible */
348                                                 int opcode = ins->opcode;
349                                                 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
350                                                 if (ins->opcode != opcode) {
351                                                         /* Allow further iterations */
352                                                         srcindex = -1;
353                                                         continue;
354                                                 }
355                                         } else if ((srcindex == 0) && (ins->sreg2 == -1)) {
356                                                 /* Constant unop, perform cfold */
357                                                 mono_constant_fold_ins (cfg, ins, defs [ins->sreg1], NULL, TRUE);
358                                         }
359
360                                         opcode2 = mono_op_to_op_imm (ins->opcode);
361                                         if ((opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0) && ((srcindex == 1) || (ins->sreg2 == -1))) {
362                                                 ins->opcode = opcode2;
363                                                 if ((def->opcode == OP_I8CONST) && (sizeof (gpointer) == 4)) {
364                                                         ins->inst_ls_word = def->inst_ls_word;
365                                                         ins->inst_ms_word = def->inst_ms_word;
366                                                 } else {
367                                                         ins->inst_imm = def->inst_c0;
368                                                 }
369                                                 sregs [srcindex] = -1;
370                                                 mono_inst_set_src_registers (ins, sregs);
371
372                                                 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
373                                                         ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
374
375                                                 /* Allow further iterations */
376                                                 srcindex = -1;
377                                                 continue;
378                                         }
379                                         else {
380                                                 /* Special cases */
381 #if defined(TARGET_X86) || defined(TARGET_AMD64)
382                                                 if ((ins->opcode == OP_X86_LEA) && (srcindex == 1)) {
383 #if SIZEOF_REGISTER == 8
384                                                         /* FIXME: Use OP_PADD_IMM when the new JIT is done */
385                                                         ins->opcode = OP_LADD_IMM;
386 #else
387                                                         ins->opcode = OP_ADD_IMM;
388 #endif
389                                                         ins->inst_imm += def->inst_c0 << ins->backend.shift_amount;
390                                                         ins->sreg2 = -1;
391                                                 }
392 #endif
393                                                 opcode2 = mono_load_membase_to_load_mem (ins->opcode);
394                                                 if ((srcindex == 0) && (opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0)) {
395                                                         ins->opcode = opcode2;
396                                                         ins->inst_imm = def->inst_c0 + ins->inst_offset;
397                                                         ins->sreg1 = -1;
398                                                 }
399                                         }
400                                 }
401                                 else if (((def->opcode == OP_ADD_IMM) || (def->opcode == OP_LADD_IMM)) && (MONO_IS_LOAD_MEMBASE (ins) || MONO_ARCH_IS_OP_MEMBASE (ins->opcode))) {
402                                         /* ADD_IMM is created by spill_global_vars */
403                                         /* 
404                                          * We have to guarantee that def->sreg1 haven't changed since def->dreg
405                                          * was defined. cfg->frame_reg is assumed to remain constant.
406                                          */
407                                         if ((def->sreg1 == cfg->frame_reg) || ((mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1))) {
408                                                 ins->inst_basereg = def->sreg1;
409                                                 ins->inst_offset += def->inst_imm;
410                                         }
411                                 } else if ((ins->opcode == OP_ISUB_IMM) && (def->opcode == OP_IADD_IMM) && (mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1)) {
412                                         ins->sreg1 = def->sreg1;
413                                         ins->inst_imm -= def->inst_imm;
414                                 } else if ((ins->opcode == OP_IADD_IMM) && (def->opcode == OP_ISUB_IMM) && (mono_inst_next (def, filter) == ins) && (def->dreg != def->sreg1)) {
415                                         ins->sreg1 = def->sreg1;
416                                         ins->inst_imm -= def->inst_imm;
417                                 } else if (ins->opcode == OP_STOREI1_MEMBASE_REG &&
418                                                    (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)) &&
419                                                    (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
420                                         /* Avoid needless sign extension */
421                                         ins->sreg1 = def->sreg1;
422                                 } else if (ins->opcode == OP_STOREI2_MEMBASE_REG &&
423                                                    (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)) &&
424                                                    (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
425                                         /* Avoid needless sign extension */
426                                         ins->sreg1 = def->sreg1;
427                                 }
428                         }
429
430                         g_assert (cfg->cbb == bb_opt);
431                         g_assert (!bb_opt->code);
432                         /* Do strength reduction here */
433                         if (mono_strength_reduction_ins (cfg, ins, &spec) && max < cfg->next_vreg) {
434                                 MonoInst **defs_prev = defs;
435                                 gint32 *def_index_prev = def_index;
436                                 guint32 prev_max = max;
437                                 guint32 additional_vregs = cfg->next_vreg - initial_max_vregs;
438
439                                 /* We have more vregs so we need to reallocate defs and def_index arrays */
440                                 max  = initial_max_vregs + additional_vregs * 2;
441                                 defs = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * max);
442                                 def_index = (gint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * max);
443
444                                 /* Keep the entries for the previous vregs, zero the rest */
445                                 memcpy (defs, defs_prev, sizeof (MonoInst*) * prev_max);
446                                 memset (defs + prev_max, 0, sizeof (MonoInst*) * (max - prev_max));
447                                 memcpy (def_index, def_index_prev, sizeof (guint32) * prev_max);
448                                 memset (def_index + prev_max, 0, sizeof (guint32) * (max - prev_max));
449                         }
450
451                         if (cfg->cbb->code || (cfg->cbb != bb_opt)) {
452                                 MonoInst *saved_prev = ins->prev;
453
454                                 /* If we have code in cbb, we need to replace ins with the decomposition */
455                                 mono_replace_ins (cfg, bb, ins, &ins->prev, bb_opt, cfg->cbb);
456                                 bb_opt->code = bb_opt->last_ins = NULL;
457                                 bb_opt->in_count = bb_opt->out_count = 0;
458                                 cfg->cbb = bb_opt;
459
460                                 /* ins is hanging, continue scanning the emitted code */
461                                 ins = saved_prev;
462                                 continue;
463                         }
464
465                         if (spec [MONO_INST_DEST] != ' ') {
466                                 MonoInst *def = defs [ins->dreg];
467
468                                 if (def && (def->opcode == OP_ADD_IMM) && (def->sreg1 == cfg->frame_reg) && (MONO_IS_STORE_MEMBASE (ins))) {
469                                         /* ADD_IMM is created by spill_global_vars */
470                                         /* cfg->frame_reg is assumed to remain constant */
471                                         ins->inst_destbasereg = def->sreg1;
472                                         ins->inst_offset += def->inst_imm;
473                                 }
474                         }
475                         
476                         if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins) && !vreg_is_volatile (cfg, ins->dreg)) {
477                                 defs [ins->dreg] = ins;
478                                 def_index [ins->dreg] = ins_index;
479                         }
480
481                         if (MONO_IS_CALL (ins))
482                                 last_call_index = ins_index;
483
484                         ins_index ++;
485                 }
486         }
487 }
488
489 static inline gboolean
490 reg_is_softreg_no_fpstack (int reg, const char spec)
491 {
492         return (spec == 'i' && reg >= MONO_MAX_IREGS)
493                 || ((spec == 'f' && reg >= MONO_MAX_FREGS) && !MONO_ARCH_USE_FPSTACK)
494 #ifdef MONO_ARCH_SIMD_INTRINSICS
495                 || (spec == 'x' && reg >= MONO_MAX_XREGS)
496 #endif
497                 || (spec == 'v');
498 }
499                 
500 static inline gboolean
501 reg_is_softreg (int reg, const char spec)
502 {
503         return (spec == 'i' && reg >= MONO_MAX_IREGS)
504                 || (spec == 'f' && reg >= MONO_MAX_FREGS)
505 #ifdef MONO_ARCH_SIMD_INTRINSICS
506                 || (spec == 'x' && reg >= MONO_MAX_XREGS)
507 #endif
508                 || (spec == 'v');
509 }
510
511 static inline gboolean
512 mono_is_simd_accessor (MonoInst *ins)
513 {
514         switch (ins->opcode) {
515 #ifdef MONO_ARCH_SIMD_INTRINSICS
516         case OP_INSERT_I1:
517         case OP_INSERT_I2:
518         case OP_INSERT_I4:
519         case OP_INSERT_I8:
520         case OP_INSERT_R4:
521         case OP_INSERT_R8:
522
523         case OP_INSERTX_U1_SLOW:
524         case OP_INSERTX_I4_SLOW:
525         case OP_INSERTX_R4_SLOW:
526         case OP_INSERTX_R8_SLOW:
527         case OP_INSERTX_I8_SLOW:
528                 return TRUE;
529 #endif
530         default:
531                 return FALSE;
532         }
533 }
534
535 /**
536  * mono_local_deadce:
537  *
538  *   Get rid of the dead assignments to local vregs like the ones created by the 
539  * copyprop pass.
540  */
541 void
542 mono_local_deadce (MonoCompile *cfg)
543 {
544         MonoBasicBlock *bb;
545         MonoInst *ins, *prev;
546         MonoBitSet *used, *defined;
547
548         //mono_print_code (cfg, "BEFORE LOCAL-DEADCE");
549
550         /*
551          * Assignments to global vregs can't be eliminated so this pass must come
552          * after the handle_global_vregs () pass.
553          */
554
555         used = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
556         defined = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
557
558         /* First pass: collect liveness info */
559         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
560                 /* Manually init the defs entries used by the bblock */
561                 MONO_BB_FOR_EACH_INS (bb, ins) {
562                         const char *spec = INS_INFO (ins->opcode);
563                         int sregs [MONO_MAX_SRC_REGS];
564                         int num_sregs, i;
565
566                         if (spec [MONO_INST_DEST] != ' ') {
567                                 mono_bitset_clear_fast (used, ins->dreg);
568                                 mono_bitset_clear_fast (defined, ins->dreg);
569 #if SIZEOF_REGISTER == 4
570                                 /* Regpairs */
571                                 mono_bitset_clear_fast (used, ins->dreg + 1);
572                                 mono_bitset_clear_fast (defined, ins->dreg + 1);
573 #endif
574                         }
575                         num_sregs = mono_inst_get_src_registers (ins, sregs);
576                         for (i = 0; i < num_sregs; ++i) {
577                                 mono_bitset_clear_fast (used, sregs [i]);
578 #if SIZEOF_REGISTER == 4
579                                 mono_bitset_clear_fast (used, sregs [i] + 1);
580 #endif
581                         }
582                 }
583
584                 /*
585                  * Make a reverse pass over the instruction list
586                  */
587                 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
588                         const char *spec = INS_INFO (ins->opcode);
589                         int sregs [MONO_MAX_SRC_REGS];
590                         int num_sregs, i;
591                         MonoInst *prev_f = mono_inst_prev (ins, FILTER_NOP | FILTER_IL_SEQ_POINT);
592
593                         if (ins->opcode == OP_NOP) {
594                                 MONO_DELETE_INS (bb, ins);
595                                 continue;
596                         }
597
598                         g_assert (ins->opcode > MONO_CEE_LAST);
599
600                         if (MONO_IS_NON_FP_MOVE (ins) && prev_f) {
601                                 MonoInst *def;
602                                 const char *spec2;
603
604                                 def = prev_f;
605                                 spec2 = INS_INFO (def->opcode);
606
607                                 /* 
608                                  * Perform a limited kind of reverse copy propagation, i.e.
609                                  * transform B <- FOO; A <- B into A <- FOO
610                                  * This isn't copyprop, not deadce, but it can only be performed
611                                  * after handle_global_vregs () has run.
612                                  */
613                                 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)) {
614                                         if (cfg->verbose_level > 2) {
615                                                 printf ("\tReverse copyprop in BB%d on ", bb->block_num);
616                                                 mono_print_ins (ins);
617                                         }
618
619                                         def->dreg = ins->dreg;
620                                         MONO_DELETE_INS (bb, ins);
621                                         spec = INS_INFO (ins->opcode);
622                                 }
623                         }
624
625                         /* Enabling this on x86 could screw up the fp stack */
626                         if (reg_is_softreg_no_fpstack (ins->dreg, spec [MONO_INST_DEST])) {
627                                 /* 
628                                  * Assignments to global vregs can only be eliminated if there is another
629                                  * assignment to the same vreg later in the same bblock.
630                                  */
631                                 if (!mono_bitset_test_fast (used, ins->dreg) && 
632                                         (!get_vreg_to_inst (cfg, ins->dreg) || (!bb->extended && !vreg_is_volatile (cfg, ins->dreg) && mono_bitset_test_fast (defined, ins->dreg))) &&
633                                         MONO_INS_HAS_NO_SIDE_EFFECT (ins)) {
634                                         /* Happens with CMOV instructions */
635                                         if (prev_f && prev_f->opcode == OP_ICOMPARE_IMM) {
636                                                 MonoInst *prev = prev_f;
637                                                 /* 
638                                                  * Can't use DELETE_INS since that would interfere with the
639                                                  * FOR_EACH_INS loop.
640                                                  */
641                                                 NULLIFY_INS (prev);
642                                         }
643                                         //printf ("DEADCE: "); mono_print_ins (ins);
644                                         MONO_DELETE_INS (bb, ins);
645                                         spec = INS_INFO (ins->opcode);
646                                 }
647
648                                 if (spec [MONO_INST_DEST] != ' ')
649                                         mono_bitset_clear_fast (used, ins->dreg);
650                         }
651
652                         if (spec [MONO_INST_DEST] != ' ')
653                                 mono_bitset_set_fast (defined, ins->dreg);
654                         num_sregs = mono_inst_get_src_registers (ins, sregs);
655                         for (i = 0; i < num_sregs; ++i)
656                                 mono_bitset_set_fast (used, sregs [i]);
657                         if (MONO_IS_STORE_MEMBASE (ins))
658                                 mono_bitset_set_fast (used, ins->dreg);
659
660                         if (MONO_IS_CALL (ins)) {
661                                 MonoCallInst *call = (MonoCallInst*)ins;
662                                 GSList *l;
663
664                                 if (call->out_ireg_args) {
665                                         for (l = call->out_ireg_args; l; l = l->next) {
666                                                 guint32 regpair, reg;
667
668                                                 regpair = (guint32)(gssize)(l->data);
669                                                 reg = regpair & 0xffffff;
670                                         
671                                                 mono_bitset_set_fast (used, reg);
672                                         }
673                                 }
674
675                                 if (call->out_freg_args) {
676                                         for (l = call->out_freg_args; l; l = l->next) {
677                                                 guint32 regpair, reg;
678
679                                                 regpair = (guint32)(gssize)(l->data);
680                                                 reg = regpair & 0xffffff;
681                                         
682                                                 mono_bitset_set_fast (used, reg);
683                                         }
684                                 }
685                         }
686                 }
687         }
688
689         //mono_print_code (cfg, "AFTER LOCAL-DEADCE");
690 }
691
692 #endif /* DISABLE_JIT */