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