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