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