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