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