Copy
[mono.git] / mono / mini / ssa.c
1 /*
2  * ssa.c: Static single assign form support for the JIT compiler.
3  *
4  * Author:
5  *    Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2003 Ximian, Inc.
8  */
9 #include <config.h>
10 #include <string.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/mempool.h>
13 #include <mono/metadata/mempool-internals.h>
14
15 #ifndef DISABLE_JIT
16
17 #include "mini.h"
18 #ifdef HAVE_ALLOCA_H
19 #include <alloca.h>
20 #endif
21
22 #define USE_ORIGINAL_VARS
23 #define CREATE_PRUNED_SSA
24
25 //#define DEBUG_SSA 1
26
27 #define NEW_PHI(cfg,dest,val) do {      \
28                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
29                 (dest)->opcode = OP_PHI;        \
30                 (dest)->inst_c0 = (val);        \
31         (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
32         } while (0)
33
34 typedef struct {
35         MonoBasicBlock *bb;
36         MonoInst *inst;
37 } MonoVarUsageInfo;
38
39 static void 
40 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
41 {
42         int i;
43
44         for (i = 0; i < bb->out_count; i++) {
45                 if (bb->out_bb [i] == target) {
46                         bb->out_bb [i] = bb->out_bb [--bb->out_count];
47                         break;
48                 }
49         }
50         for (i = 0; i < target->in_count; i++) {
51                 if (target->in_bb [i] == bb) {
52                         target->in_bb [i] = target->in_bb [--target->in_count];
53                         break;
54                         
55                 }
56         }
57 }
58
59 static void
60 unlink_unused_bblocks (MonoCompile *cfg) 
61 {
62         int i, j;
63         MonoBasicBlock *bb;
64
65         g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
66
67         if (G_UNLIKELY (cfg->verbose_level > 1))
68                 printf ("\nUNLINK UNUSED BBLOCKS:\n");
69
70         for (bb = cfg->bb_entry; bb && bb->next_bb;) {
71                 if (!(bb->next_bb->flags & BB_REACHABLE)) {
72                         bb->next_bb = bb->next_bb->next_bb;
73                 } else 
74                         bb = bb->next_bb;
75         }
76
77         for (i = 1; i < cfg->num_bblocks; i++) {
78                 bb = cfg->bblocks [i];
79                
80                 if (!(bb->flags & BB_REACHABLE)) {
81                         for (j = 0; j < bb->in_count; j++) {
82                                 unlink_target (bb->in_bb [j], bb);      
83                         }
84                         for (j = 0; j < bb->out_count; j++) {
85                                 unlink_target (bb, bb->out_bb [j]);     
86                         }
87                         if (G_UNLIKELY (cfg->verbose_level > 1))
88                                 printf ("\tUnlinked BB%d\n", bb->block_num);
89                 }
90  
91         }
92 }
93
94 /**
95  * remove_bb_from_phis:
96  *
97  *   Remove BB from the PHI statements in TARGET.
98  */
99 static void
100 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
101 {
102         MonoInst *ins;
103         int i, j;
104
105         for (i = 0; i < target->in_count; i++) {
106                 if (target->in_bb [i] == bb) {
107                         break;
108                 }
109         }
110         g_assert (i < target->in_count);
111
112         for (ins = target->code; ins; ins = ins->next) {
113                 if (MONO_IS_PHI (ins)) {
114                         for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
115                                 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
116                         ins->inst_phi_args [0] --;
117                 }
118                 else
119                         break;
120         }
121 }
122
123 static inline int
124 op_phi_to_move (int opcode)
125 {
126         switch (opcode) {
127         case OP_PHI:
128                 return OP_MOVE;
129         case OP_FPHI:
130                 return OP_FMOVE;
131         case OP_VPHI:
132                 return OP_VMOVE;
133         case OP_XPHI:
134                 return OP_XMOVE;
135         default:
136                 g_assert_not_reached ();
137         }
138
139         return -1;
140 }
141
142 static inline void
143 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
144 {
145         MonoMethodVar *info;
146         MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
147
148         info = MONO_VARINFO (cfg, var->inst_c0);
149         
150         ui->bb = bb;
151         ui->inst = ins;
152         info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
153 }       
154
155 typedef struct {
156         MonoInst *var;
157         int idx;
158 } RenameInfo;
159
160 /**
161  * mono_ssa_rename_vars:
162  *
163  *  Implement renaming of SSA variables. Also compute def-use information in parallel.
164  * @stack_history points to an area of memory which can be used for storing changes 
165  * made to the stack, so they can be reverted later.
166  */
167 static void
168 mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, gboolean *originals_used, MonoInst **stack,   guint32 *lvreg_stack, gboolean *lvreg_defined, RenameInfo *stack_history, int stack_history_size) 
169 {
170         MonoInst *ins, *new_var;
171         int i, j, idx;
172         GSList *tmp;
173         int stack_history_len = 0;
174
175         if (cfg->verbose_level >= 4)
176                 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
177
178         /* First pass: Create new vars */
179         for (ins = bb->code; ins; ins = ins->next) {
180                 const char *spec = INS_INFO (ins->opcode);
181                 int num_sregs;
182                 int sregs [MONO_MAX_SRC_REGS];
183
184 #ifdef DEBUG_SSA
185                 printf ("\tProcessing "); mono_print_ins (ins);
186 #endif
187                 if (ins->opcode == OP_NOP)
188                         continue;
189
190                 /* SREGs */
191                 num_sregs = mono_inst_get_src_registers (ins, sregs);
192                 for (i = 0; i < num_sregs; ++i) {
193                         if (spec [MONO_INST_SRC1 + i] != ' ') {
194                                 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
195                                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
196                                         int idx = var->inst_c0;
197                                         if (stack [idx]) {
198                                                 if (var->opcode != OP_ARG)
199                                                         g_assert (stack [idx]);
200                                                 sregs [i] = stack [idx]->dreg;
201                                                 record_use (cfg, stack [idx], bb, ins);
202                                         }
203                                         else
204                                                 record_use (cfg, var, bb, ins);
205                                 }
206                                 else if (G_UNLIKELY (!var && lvreg_stack [sregs [i]]))
207                                         sregs [i] = lvreg_stack [sregs [i]];
208                         }
209                 }
210                 mono_inst_set_src_registers (ins, sregs);
211
212                 if (MONO_IS_STORE_MEMBASE (ins)) {
213                         MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
214                         if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
215                                 int idx = var->inst_c0;
216                                 if (stack [idx]) {
217                                         if (var->opcode != OP_ARG)
218                                                 g_assert (stack [idx]);
219                                         ins->dreg = stack [idx]->dreg;
220                                         record_use (cfg, stack [idx], bb, ins);
221                                 }
222                                 else
223                                         record_use (cfg, var, bb, ins);
224                         }
225                         else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
226                                 ins->dreg = lvreg_stack [ins->dreg];
227                 }
228
229                 /* DREG */
230                 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
231                         MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
232                         MonoMethodVar *info;
233
234                         if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
235                                 idx = var->inst_c0;
236                                 g_assert (idx < max_vars);
237
238                                 if (var->opcode == OP_ARG)
239                                         originals_used [idx] = TRUE;
240
241                                 /* FIXME: */
242                                 g_assert (stack_history_len < stack_history_size);
243                                 stack_history [stack_history_len].var = stack [idx];
244                                 stack_history [stack_history_len].idx = idx;
245                                 stack_history_len ++;
246
247                                 if (originals_used [idx]) {
248                                         new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
249                                         new_var->flags = var->flags;
250                                         MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
251
252                                         if (cfg->verbose_level >= 4)
253                                                 printf ("  R%d -> R%d\n", var->dreg, new_var->dreg);
254
255                                         stack [idx] = new_var;
256
257                                         ins->dreg = new_var->dreg;
258                                         var = new_var;
259                                 }
260                                 else {
261                                         stack [idx] = var;
262                                         originals_used [idx] = TRUE;
263                                 }
264
265                                 info = MONO_VARINFO (cfg, var->inst_c0);
266                                 info->def = ins;
267                                 info->def_bb = bb;
268                         }
269                         else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
270                                 /* Perform renaming for local vregs */
271                                 lvreg_stack [ins->dreg] = mono_alloc_preg (cfg);
272                                 ins->dreg = lvreg_stack [ins->dreg];
273                         }
274                         else
275                                 lvreg_defined [ins->dreg] = TRUE;
276                 }
277
278 #ifdef DEBUG_SSA
279                 printf ("\tAfter processing "); mono_print_ins (ins);
280 #endif
281
282         }
283
284         /* Rename PHI arguments in succeeding bblocks */
285         for (i = 0; i < bb->out_count; i++) {
286                 MonoBasicBlock *n = bb->out_bb [i];
287
288                 for (j = 0; j < n->in_count; j++)
289                         if (n->in_bb [j] == bb)
290                                 break;
291                 
292                 for (ins = n->code; ins; ins = ins->next) {
293                         if (MONO_IS_PHI (ins)) {
294                                 idx = ins->inst_c0;
295                                 if (stack [idx])
296                                         new_var = stack [idx];
297                                 else
298                                         new_var = cfg->varinfo [idx];
299 #ifdef DEBUG_SSA
300                                 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
301 #endif
302                                 ins->inst_phi_args [j + 1] = new_var->dreg;
303                                 record_use (cfg,  new_var, n, ins);                             
304                                 if (G_UNLIKELY (cfg->verbose_level >= 4))
305                                         printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
306                         }
307                         else
308                                 /* The phi nodes are at the beginning of the bblock */
309                                 break;
310                 }
311         }
312
313         if (bb->dominated) {
314                 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
315                         mono_ssa_rename_vars (cfg, max_vars, (MonoBasicBlock *)tmp->data, originals_used, stack, lvreg_stack, lvreg_defined, stack_history + stack_history_len, stack_history_size - stack_history_len);
316                 }
317         }
318
319         /* Restore stack */
320         for (i = stack_history_len - 1; i >= 0; i--) {
321                 stack [stack_history [i].idx] = stack_history [i].var;
322         }
323
324         cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
325 }
326
327 void
328 mono_ssa_compute (MonoCompile *cfg)
329 {
330         int i, j, idx, bitsize;
331         MonoBitSet *set;
332         MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
333         MonoInst *ins, **stack;
334         guint8 *buf, *buf_start;
335         RenameInfo *stack_history;
336         int stack_history_size;
337         gboolean *originals;
338         guint32 *lvreg_stack;
339         gboolean *lvreg_defined;
340
341         g_assert (!(cfg->comp_done & MONO_COMP_SSA));
342
343         /* we dont support methods containing exception clauses */
344         g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
345         g_assert (!cfg->disable_ssa);
346
347         if (cfg->verbose_level >= 4)
348                 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
349
350 #ifdef CREATE_PRUNED_SSA
351         /* we need liveness for pruned SSA */
352         if (!(cfg->comp_done & MONO_COMP_LIVENESS))
353                 mono_analyze_liveness (cfg);
354 #endif
355
356         mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
357
358         bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
359         buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
360
361         for (i = 0; i < cfg->num_varinfo; ++i) {
362                 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
363                 buf += bitsize;
364                 vinfo [i].idx = i;
365                 /* implicit reference at start */
366                 if (cfg->varinfo [i]->opcode == OP_ARG)
367                         mono_bitset_set_fast (vinfo [i].def_in, 0);
368         }
369
370         for (i = 0; i < cfg->num_bblocks; ++i) {
371                 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
372                         if (ins->opcode == OP_NOP)
373                                 continue;
374
375                         if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
376                                 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
377                         }
378                 }
379         }
380
381         /* insert phi functions */
382         for (i = 0; i < cfg->num_varinfo; ++i) {
383                 MonoInst *var = cfg->varinfo [i];
384
385 #if SIZEOF_REGISTER == 4
386                 if (var->type == STACK_I8 && !COMPILE_LLVM (cfg))
387                         continue;
388 #endif
389                 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
390                         continue;
391
392                 /* Most variables have only one definition */
393                 if (mono_bitset_count (vinfo [i].def_in) <= 1)
394                         continue;
395
396                 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
397
398                 if (cfg->verbose_level >= 4) {
399                         if (mono_bitset_count (set) > 0) {
400                                 printf ("\tR%d needs PHI functions in ", var->dreg);
401                                 mono_blockset_print (cfg, set, "", -1);
402                         }
403                 }
404                         
405                 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
406                         MonoBasicBlock *bb = cfg->bblocks [idx];
407
408                         /* fixme: create pruned SSA? we would need liveness information for that */
409
410                         if (bb == cfg->bb_exit && !COMPILE_LLVM (cfg))
411                                 continue;
412
413                         if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
414                                 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
415                                 continue;
416                         }
417
418                         NEW_PHI (cfg, ins, i);
419
420                         switch (var->type) {
421                         case STACK_I4:
422                         case STACK_I8:
423                         case STACK_PTR:
424                         case STACK_MP:
425                         case STACK_OBJ:
426                                 ins->opcode = OP_PHI;
427                                 break;
428                         case STACK_R8:
429                                 ins->opcode = OP_FPHI;
430                                 break;
431                         case STACK_VTYPE:
432                                 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
433                                 break;
434                         }
435
436                         if (var->inst_vtype->byref)
437                                 ins->klass = mono_defaults.int_class;
438                         else
439                                 ins->klass = var->klass;
440
441                         ins->inst_phi_args =  mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
442                         ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
443
444                         /* For debugging */
445                         for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
446                                 ins->inst_phi_args [j + 1] = -1;
447
448                         ins->dreg = cfg->varinfo [i]->dreg;
449
450                         mono_bblock_insert_before_ins (bb, bb->code, ins);
451
452 #ifdef DEBUG_SSA
453                         printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
454 #endif
455                 }
456         }
457
458         /* free the stuff */
459         g_free (vinfo);
460         g_free (buf_start);
461
462         /* Renaming phase */
463
464         stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
465         memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
466
467         lvreg_stack = g_new0 (guint32, cfg->next_vreg);
468         lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
469         stack_history_size = 10240;
470         stack_history = g_new (RenameInfo, stack_history_size);
471         originals = g_new0 (gboolean, cfg->num_varinfo);
472         mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
473         g_free (stack_history);
474         g_free (originals);
475         g_free (lvreg_stack);
476         g_free (lvreg_defined);
477
478         if (cfg->verbose_level >= 4)
479                 printf ("\nEND COMPUTE SSA.\n\n");
480
481         cfg->comp_done |= MONO_COMP_SSA;
482 }
483
484 void
485 mono_ssa_remove (MonoCompile *cfg)
486 {
487         MonoInst *ins, *var, *move;
488         int i, j, first;
489
490         g_assert (cfg->comp_done & MONO_COMP_SSA);
491
492         for (i = 0; i < cfg->num_bblocks; ++i) {
493                 MonoBasicBlock *bb = cfg->bblocks [i];
494
495                 if (cfg->verbose_level >= 4)
496                         printf ("\nREMOVE SSA %d:\n", bb->block_num);
497
498                 for (ins = bb->code; ins; ins = ins->next) {
499                         if (MONO_IS_PHI (ins)) {
500                                 g_assert (ins->inst_phi_args [0] == bb->in_count);
501                                 var = get_vreg_to_inst (cfg, ins->dreg);
502
503                                 /* Check for PHI nodes where all the inputs are the same */
504                                 first = ins->inst_phi_args [1];
505
506                                 for (j = 1; j < bb->in_count; ++j)
507                                         if (first != ins->inst_phi_args [j + 1])
508                                                 break;
509
510                                 if ((bb->in_count > 1) && (j == bb->in_count)) {
511                                         ins->opcode = op_phi_to_move (ins->opcode);
512                                         if (ins->opcode == OP_VMOVE)
513                                                 g_assert (ins->klass);
514                                         ins->sreg1 = first;
515                                 } else {
516                                         for (j = 0; j < bb->in_count; j++) {
517                                                 MonoBasicBlock *pred = bb->in_bb [j];
518                                                 int sreg = ins->inst_phi_args [j + 1];
519
520                                                 if (cfg->verbose_level >= 4)
521                                                         printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
522                                                 if (var->dreg != sreg) {
523                                                         MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
524                                                         if (move->opcode == OP_VMOVE) {
525                                                                 g_assert (ins->klass);
526                                                                 move->klass = ins->klass;
527                                                         }
528                                                         move->dreg = var->dreg;
529                                                         move->sreg1 = sreg;
530                                                         mono_add_ins_to_end (pred, move);
531                                                 }
532                                         }
533
534                                         ins->opcode = OP_NOP;
535                                         ins->dreg = -1;
536                                 }
537                         }
538                 }
539         }
540
541         if (cfg->verbose_level >= 4) {
542                 for (i = 0; i < cfg->num_bblocks; ++i) {
543                         MonoBasicBlock *bb = cfg->bblocks [i];
544
545                         mono_print_bb (bb, "AFTER REMOVE SSA:");
546                 }
547         }
548
549         /*
550          * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce 
551          * the variables if possible. Since the newly introduced SSA variables don't
552          * have overlapping live ranges (because we don't do agressive optimization), we
553          * can coalesce them into the original variable.
554          */
555
556         for (i = 0; i < cfg->num_bblocks; ++i) {
557                 MonoBasicBlock *bb = cfg->bblocks [i];
558
559                 for (ins = bb->code; ins; ins = ins->next) {
560                         const char *spec = INS_INFO (ins->opcode);
561                         int num_sregs, j;
562                         int sregs [MONO_MAX_SRC_REGS];
563
564                         if (ins->opcode == OP_NOP)
565                                 continue;
566
567                         if (spec [MONO_INST_DEST] != ' ') {
568                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
569
570                                 if (var) {
571                                         MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
572                                         
573                                         /* 
574                                          * The third condition avoids coalescing with variables eliminated
575                                          * during deadce.
576                                          */
577                                         if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
578                                                 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
579                                                 ins->dreg = cfg->varinfo [vmv->reg]->dreg; 
580                                         }
581                                 }
582                         }
583
584                         num_sregs = mono_inst_get_src_registers (ins, sregs);
585                         for (j = 0; j < num_sregs; ++j) {
586                                 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
587
588                                 if (var) {
589                                         MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
590
591                                         if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
592                                                 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
593                                                 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
594                                         }
595                                 }
596                         }
597                         mono_inst_set_src_registers (ins, sregs);
598                 }
599         }
600
601         for (i = 0; i < cfg->num_varinfo; ++i) {
602                 MONO_VARINFO (cfg, i)->reg = -1;
603         }
604
605         if (cfg->comp_done & MONO_COMP_REACHABILITY)
606                 unlink_unused_bblocks (cfg);
607
608         cfg->comp_done &= ~MONO_COMP_LIVENESS;
609
610         cfg->comp_done &= ~MONO_COMP_SSA;
611 }
612
613 static void
614 mono_ssa_create_def_use (MonoCompile *cfg) 
615 {
616         MonoBasicBlock *bb;
617         MonoInst *ins;
618         int i;
619
620         g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
621
622         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
623                 for (ins = bb->code; ins; ins = ins->next) {
624                         const char *spec = INS_INFO (ins->opcode);
625                         MonoMethodVar *info;
626                         int num_sregs;
627                         int sregs [MONO_MAX_SRC_REGS];
628
629                         if (ins->opcode == OP_NOP)
630                                 continue;
631
632                         /* SREGs */
633                         num_sregs = mono_inst_get_src_registers (ins, sregs);
634                         for (i = 0; i < num_sregs; ++i) {
635                                 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
636                                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
637                                         record_use (cfg, var, bb, ins);
638                         }
639
640                         if (MONO_IS_STORE_MEMBASE (ins)) {
641                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
642                                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
643                                         record_use (cfg, var, bb, ins);
644                         }
645
646                         if (MONO_IS_PHI (ins)) {
647                                 for (i = ins->inst_phi_args [0]; i > 0; i--) {
648                                         g_assert (ins->inst_phi_args [i] != -1);
649                                         record_use (cfg,  get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
650                                 }
651                         }
652
653                         /* DREG */
654                         if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
655                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
656
657                                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
658                                         info = MONO_VARINFO (cfg, var->inst_c0);
659                                         info->def = ins;
660                                         info->def_bb = bb;
661                                 }
662                         }
663                 }
664         }
665
666         cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
667 }
668
669 static void
670 mono_ssa_copyprop (MonoCompile *cfg)
671 {
672         int i, index;
673         GList *l;
674
675         g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
676
677         for (index = 0; index < cfg->num_varinfo; ++index) {
678                 MonoInst *var = cfg->varinfo [index];
679                 MonoMethodVar *info = MONO_VARINFO (cfg, index);
680
681                 if (info->def && (MONO_IS_MOVE (info->def))) {
682                         MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
683
684                         if (var2 && !(var2->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && MONO_VARINFO (cfg, var2->inst_c0)->def && (!MONO_IS_PHI (MONO_VARINFO (cfg, var2->inst_c0)->def))) {
685                                 /* Rewrite all uses of var to be uses of var2 */
686                                 int dreg = var->dreg;
687                                 int sreg1 = var2->dreg;
688                                 const char *spec;
689
690                                 l = info->uses;
691                                 while (l) {
692                                         MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
693                                         MonoInst *ins = u->inst;
694                                         GList *next = l->next;
695                                         int num_sregs;
696                                         int sregs [MONO_MAX_SRC_REGS];
697
698                                         spec = INS_INFO (ins->opcode);
699
700                                         num_sregs = mono_inst_get_src_registers (ins, sregs);
701                                         for (i = 0; i < num_sregs; ++i) {
702                                                 if (sregs [i] == dreg)
703                                                         break;
704                                         }
705                                         if (i < num_sregs) {
706                                                 g_assert (sregs [i] == dreg);
707                                                 sregs [i] = sreg1;
708                                                 mono_inst_set_src_registers (ins, sregs);
709                                         } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
710                                                 ins->dreg = sreg1;
711                                         } else if (MONO_IS_PHI (ins)) {
712                                                 for (i = ins->inst_phi_args [0]; i > 0; i--) {
713                                                         int sreg = ins->inst_phi_args [i];
714                                                         if (sreg == var->dreg)
715                                                                 break;
716                                                 }
717                                                 g_assert (i > 0);
718                                                 ins->inst_phi_args [i] = sreg1;
719                                         }
720                                         else
721                                                 g_assert_not_reached ();
722
723                                         record_use (cfg, var2, u->bb, ins);
724
725                                         l = next;
726                                 }
727
728                                 info->uses = NULL;
729                         }
730                 }
731         }
732
733         if (cfg->verbose_level >= 4) {
734                 MonoBasicBlock *bb;
735
736                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
737                         mono_print_bb (bb, "AFTER SSA COPYPROP");
738         }
739 }
740
741 static int
742 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
743 {
744         MonoInst *args [MONO_MAX_SRC_REGS];
745         int rs [MONO_MAX_SRC_REGS];
746         MonoInst *c0;
747         gboolean const_args = TRUE;
748         const char *spec = INS_INFO (ins->opcode);
749         int num_sregs, i;
750         int sregs [MONO_MAX_SRC_REGS];
751
752         /* Short-circuit this */
753         if (ins->opcode == OP_ICONST) {
754                 *res = ins;
755                 return 1;
756         }
757
758         if (ins->opcode == OP_NOP)
759                 return 2;
760
761         num_sregs = mono_inst_get_src_registers (ins, sregs);
762         for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
763                 args [i] = NULL;
764         for (i = 0; i < num_sregs; ++i) {
765                 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
766
767                 rs [i] = 2;
768                 args [i] = carray [sregs [i]];
769                 if (args [i])
770                         rs [i] = 1;
771                 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
772                         rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
773                 if (rs [i] != 1)
774                         const_args = FALSE;
775         }
776
777         c0 = NULL;
778
779         if (num_sregs > 0 && const_args) {
780                 g_assert (num_sregs <= 2);
781                 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
782                         // Cached value
783                         *res = carray [ins->dreg];
784                         return 1;
785                 }
786                 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
787                 if (c0) {
788                         if (G_UNLIKELY (cfg->verbose_level > 1)) {
789                                 printf ("\t cfold -> ");
790                                 mono_print_ins (c0);
791                         }
792                         *res = c0;
793                         return 1;
794                 }
795                 else
796                         /* Can't cfold this ins */
797                         return 2;
798         }
799
800         if (num_sregs == 0)
801                 return 2;
802         for (i = 0; i < num_sregs; ++i) {
803                 if (rs [i] == 2)
804                         return 2;
805         }
806         return 0;
807 }
808
809 static inline void
810 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
811 {
812         if (info->cpstate >= state)
813                 return;
814
815         info->cpstate = state;
816
817         if (G_UNLIKELY (cfg->verbose_level > 1))
818                 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
819
820         if (state == 1)
821                 g_assert (c0);
822
823         carray [cfg->varinfo [info->idx]->dreg] = c0;
824
825         if (!g_list_find (*cvars, info)) {
826                 *cvars = g_list_prepend (*cvars, info);
827         }
828 }
829
830 static inline void
831 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
832 {
833         if (G_UNLIKELY (cfg->verbose_level > 1))
834                 printf ("\tAdd BB%d to worklist\n", bb->block_num);
835
836     if (!(bb->flags &  BB_REACHABLE)) {
837             bb->flags |= BB_REACHABLE;
838                 *bblist = g_list_prepend (*bblist, bb);
839         }
840 }
841
842 static void
843 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
844 {
845         const char *spec = INS_INFO (ins->opcode);
846
847         if (ins->opcode == OP_NOP)
848                 return;
849
850         if (cfg->verbose_level > 1)
851                 mono_print_ins (ins);
852
853         /* FIXME: Support longs/floats */
854         /* FIXME: Work on vregs as well */
855
856         if (MONO_IS_PHI (ins)) {
857                 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
858                 MonoInst *c0 = NULL;
859                 int j;
860
861                 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
862                         MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
863                         MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
864                         MonoInst *src = mv->def;
865
866                         if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
867                                 continue;
868
869                         if (!mv->def || !src || mv->cpstate == 2) {
870                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
871                                 break;
872                         }
873                                         
874                         if (mv->cpstate == 0)
875                                 continue;
876
877                         g_assert (carray [var->dreg]);
878
879                         if (!c0)
880                                 c0 = carray [var->dreg];
881                 
882                         /* FIXME: */
883                         if (c0->opcode != OP_ICONST) {
884                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
885                                 break;
886                         }
887
888                         if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
889                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
890                                 break;
891                         }
892                 }
893                                 
894                 if (c0 && info->cpstate < 1) {
895                         change_varstate (cfg, cvars, info, 1, c0, carray);
896
897                         g_assert (c0->opcode == OP_ICONST);
898                 }
899         }
900         else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
901                 MonoInst *var, *c0;
902                 int state;
903
904                 if (spec [MONO_INST_DEST] !=  ' ')
905                         var = get_vreg_to_inst (cfg, ins->dreg);
906                 else
907                         var = NULL;
908
909                 c0 = NULL;
910                 state = evaluate_ins (cfg, ins, &c0, carray);
911
912                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
913                         MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
914
915                         if (info->cpstate < 2) {
916                                 if (state == 1)
917                                         change_varstate (cfg, cvars, info, 1, c0, carray);
918                                 else if (state == 2)
919                                         change_varstate (cfg, cvars, info, 2, NULL, carray);
920                         }
921                 }
922                 else if (!var && (ins->dreg != -1)) {
923                         /*
924                          * We don't record def-use information for local vregs since it would be 
925                          * expensive. Instead, we depend on the fact that all uses of the vreg are in
926                          * the same bblock, so they will be examined after the definition.
927                          * FIXME: This isn't true if the ins is visited through an SSA edge.
928                          */
929                         if (c0) {
930                                 carray [ins->dreg] = c0;
931                         } else {
932                                 if (carray [ins->dreg]) {
933                                         /* 
934                                          * The state of the vreg changed from constant to non-constant
935                                          * -> need to rescan the whole bblock.
936                                          */
937                                         carray [ins->dreg] = NULL;
938                                         /* FIXME: Speed this up */
939
940                                         if (!g_list_find (*bblist, bb))
941                                                 *bblist = g_list_prepend (*bblist, bb);
942                                 }
943                         }
944                 }
945
946                 if (MONO_IS_JUMP_TABLE (ins)) {
947                         int i;
948                         MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
949
950                         if (!ins->next || ins->next->opcode != OP_PADD) {
951                                 /* The PADD was optimized away */
952                                 /* FIXME: handle this as well */
953                                 for (i = 0; i < table->table_size; i++)
954                                         if (table->table [i])
955                                                 add_cprop_bb (cfg, table->table [i], bblist);
956                                 return;
957                         }
958
959                         g_assert (ins->next->opcode == OP_PADD);
960                         g_assert (ins->next->sreg1 == ins->dreg);
961
962                         if (carray [ins->next->sreg2]) {
963 #if SIZEOF_REGISTER == 8
964                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
965 #else
966                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
967 #endif
968                                 if ((idx < 0) || (idx >= table->table_size))
969                                         /* Out-of-range, no branch is executed */
970                                         return;
971                                 else
972                                         if (table->table [idx])
973                                                 add_cprop_bb (cfg, table->table [idx], bblist);
974                         }
975                         else {
976                                 for (i = 0; i < table->table_size; i++)
977                                         if (table->table [i])
978                                                 add_cprop_bb (cfg, table->table [i], bblist);
979                         }
980                 }
981
982                 if (ins->opcode == OP_SWITCH) {
983                         int i;
984                         MonoJumpInfoBBTable *table = ins->inst_p0;
985
986                         for (i = 0; i < table->table_size; i++)
987                                 if (table->table [i])
988                                         add_cprop_bb (cfg, table->table [i], bblist);
989                 }
990
991                 /* Handle COMPARE+BRCOND pairs */
992                 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
993                         if (c0) {
994                                 g_assert (c0->opcode == OP_ICONST);
995
996                                 if (c0->inst_c0)
997                                         ins->next->flags |= MONO_INST_CFOLD_TAKEN;
998                                 else
999                                         ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1000                         }
1001                         else {
1002                                 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1003                         }
1004
1005                         visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1006                 }
1007         } else if (ins->opcode == OP_BR) {
1008                 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1009         } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1010                 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1011                         add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1012                 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1013                         if (ins->inst_false_bb)
1014                                 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1015         } else {
1016                         add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1017                         if (ins->inst_false_bb)
1018                                 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1019                 }
1020         }
1021 }
1022
1023 /**
1024  * fold_ins:
1025  *
1026  *   Replace INS with its constant value, if it exists
1027  */
1028 static void
1029 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1030 {
1031         const char *spec = INS_INFO (ins->opcode);
1032         int opcode2;
1033         int num_sregs = mono_inst_get_num_src_registers (ins);
1034
1035         if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1036                 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1037                         /* Perform constant folding */
1038                         /* FIXME: */
1039                         g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1040                         ins->opcode = OP_ICONST;
1041                         ins->inst_c0 = carray [ins->dreg]->inst_c0;
1042                         MONO_INST_NULLIFY_SREGS (ins);
1043                 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1044                         /* Perform op->op_imm conversion */
1045                         opcode2 = mono_op_to_op_imm (ins->opcode);
1046                         if (opcode2 != -1) {
1047                                 ins->opcode = opcode2;
1048                                 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1049                                 ins->sreg2 = -1;
1050
1051                                 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1052                                         ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1053                         }
1054                 } else {
1055                         /* FIXME: Handle 3 op insns */
1056                 }
1057
1058                 if (MONO_IS_JUMP_TABLE (ins)) {
1059                         int i;
1060                         MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1061
1062                         if (!ins->next || ins->next->opcode != OP_PADD) {
1063                                 /* The PADD was optimized away */
1064                                 /* FIXME: handle this as well */
1065                                 return;
1066                         }
1067
1068                         g_assert (ins->next->opcode == OP_PADD);
1069                         g_assert (ins->next->sreg1 == ins->dreg);
1070                         g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1071
1072                         if (carray [ins->next->sreg2]) {
1073                                 /* Convert to a simple branch */
1074 #if SIZEOF_REGISTER == 8
1075                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1076 #else
1077                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1078 #endif
1079
1080                                 if (!((idx >= 0) && (idx < table->table_size))) {
1081                                         /* Out of range, eliminate the whole switch */
1082                                         for (i = 0; i < table->table_size; ++i) {
1083                                                 remove_bb_from_phis (cfg, bb, table->table [i]);
1084                                                 mono_unlink_bblock (cfg, bb, table->table [i]);
1085                                         }
1086
1087                                         NULLIFY_INS (ins);
1088                                         NULLIFY_INS (ins->next);
1089                                         NULLIFY_INS (ins->next->next);
1090                                         if (ins->next->next->next)
1091                                                 NULLIFY_INS (ins->next->next->next);
1092
1093                                         return;
1094                                 }
1095
1096                                 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1097                                         /* A one-way switch which got optimized away */
1098                                         if (G_UNLIKELY (cfg->verbose_level > 1)) {
1099                                                 printf ("\tNo cfold on ");
1100                                                 mono_print_ins (ins);
1101                                         }
1102                                         return;
1103                                 }
1104
1105                                 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1106                                         printf ("\tcfold on ");
1107                                         mono_print_ins (ins);
1108                                 }
1109
1110                                 /* Unlink target bblocks */
1111                                 for (i = 0; i < table->table_size; ++i) {
1112                                         if (i != idx) {
1113                                                 remove_bb_from_phis (cfg, bb, table->table [i]);
1114                                                 mono_unlink_bblock (cfg, bb, table->table [i]);
1115                                         }
1116                                 }
1117
1118                                 /* Change the OP_BR_REG to a simple branch */
1119                                 ins->next->next->next->opcode = OP_BR;
1120                                 ins->next->next->next->inst_target_bb = table->table [idx];
1121                                 ins->next->next->next->sreg1 = -1;
1122
1123                                 /* Nullify the other instructions */
1124                                 NULLIFY_INS (ins);
1125                                 NULLIFY_INS (ins->next);
1126                                 NULLIFY_INS (ins->next->next);
1127                         }
1128                 }
1129         } 
1130         else if (MONO_IS_COND_BRANCH_OP (ins)) {
1131                 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1132                         remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1133                         mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1134                         ins->opcode = OP_BR;
1135                         ins->inst_target_bb = ins->inst_true_bb;
1136                 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1137                         remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1138                         mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1139                         ins->opcode = OP_BR;
1140                         ins->inst_target_bb = ins->inst_false_bb;
1141                 }
1142         }
1143 }
1144
1145 void
1146 mono_ssa_cprop (MonoCompile *cfg) 
1147 {
1148         MonoInst **carray;
1149         MonoBasicBlock *bb;
1150         GList *bblock_list, *cvars;
1151         GList *tmp;
1152         int i;
1153         //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1154
1155         carray = g_new0 (MonoInst*, cfg->next_vreg);
1156
1157         if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1158                 mono_ssa_create_def_use (cfg);
1159
1160         bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1161         cfg->bb_entry->flags |= BB_REACHABLE;
1162
1163         memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1164
1165         for (i = 0; i < cfg->num_varinfo; i++) {
1166                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1167                 if (!info->def)
1168                         info->cpstate = 2;
1169         }
1170
1171         cvars = NULL;
1172
1173         while (bblock_list) {
1174                 MonoInst *inst;
1175
1176                 bb = (MonoBasicBlock *)bblock_list->data;
1177
1178                 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1179
1180                 g_assert (bb->flags &  BB_REACHABLE);
1181
1182                 if (bb->out_count == 1) {
1183                         if (!(bb->out_bb [0]->flags &  BB_REACHABLE)) {
1184                                 bb->out_bb [0]->flags |= BB_REACHABLE;
1185                                 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1186                         }
1187                 }
1188
1189                 if (cfg->verbose_level > 1)
1190                         printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1191
1192                 for (inst = bb->code; inst; inst = inst->next) {
1193                         visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1194                 }
1195
1196                 while (cvars) {
1197                         MonoMethodVar *info = (MonoMethodVar *)cvars->data;                     
1198                         cvars = g_list_delete_link (cvars, cvars);
1199
1200                         for (tmp = info->uses; tmp; tmp = tmp->next) {
1201                                 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1202                                 if (!(ui->bb->flags & BB_REACHABLE))
1203                                         continue;
1204                                 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1205                         }
1206                 }
1207         }
1208
1209         for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1210                 MonoInst *inst;
1211                 for (inst = bb->code; inst; inst = inst->next) {
1212                         fold_ins (cfg, bb, inst, carray);
1213                 }
1214         }
1215
1216         g_free (carray);
1217
1218         cfg->comp_done |= MONO_COMP_REACHABILITY;
1219
1220         /* fixme: we should update usage infos during cprop, instead of computing it again */
1221         cfg->comp_done &=  ~MONO_COMP_SSA_DEF_USE;
1222         for (i = 0; i < cfg->num_varinfo; i++) {
1223                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1224                 info->def = NULL;
1225                 info->uses = NULL;
1226         }
1227 }
1228
1229 static inline void
1230 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1231 {
1232         GList *tmp;
1233
1234         *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1235
1236         for (tmp = use->uses; tmp; tmp = tmp->next) {
1237                 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1238                 if (ui->inst == var->def) {
1239                         /* from the mempool */
1240                         use->uses = g_list_remove_link (use->uses, tmp);
1241                         break;
1242                 }
1243         }       
1244 }
1245
1246 void
1247 mono_ssa_deadce (MonoCompile *cfg) 
1248 {
1249         int i;
1250         GList *work_list;
1251
1252         g_assert (cfg->comp_done & MONO_COMP_SSA);
1253
1254         //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1255
1256         if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1257                 mono_ssa_create_def_use (cfg);
1258
1259         mono_ssa_copyprop (cfg);
1260
1261         work_list = NULL;
1262         for (i = 0; i < cfg->num_varinfo; i++) {
1263                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1264                 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1265         }
1266
1267         while (work_list) {
1268                 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1269                 work_list = g_list_remove_link (work_list, work_list);
1270
1271                 /* 
1272                  * The second part of the condition happens often when PHI nodes have their dreg
1273                  * as one of their arguments due to the fact that we use the original vars.
1274                  */
1275                 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1276                         MonoInst *def = info->def;
1277
1278                         /* Eliminating FMOVE could screw up the fp stack */
1279                         if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1280                                 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1281                                 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1282                                         add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1283                                 def->opcode = OP_NOP;
1284                                 def->dreg = def->sreg1 = def->sreg2 = -1;
1285                                 info->reg = -1;
1286                         } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1287                                 def->opcode = OP_NOP;
1288                                 def->dreg = def->sreg1 = def->sreg2 = -1;
1289                                 info->reg = -1;
1290                         } else if (MONO_IS_PHI (def)) {
1291                                 int j;
1292                                 for (j = def->inst_phi_args [0]; j > 0; j--) {
1293                                         MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1294                                         add_to_dce_worklist (cfg, info, u, &work_list);
1295                                 }
1296                                 def->opcode = OP_NOP;
1297                                 def->dreg = def->sreg1 = def->sreg2 = -1;
1298                                 info->reg = -1;
1299                         }
1300                         else if (def->opcode == OP_NOP) {
1301                         }
1302                         //else
1303                         //mono_print_ins (def);
1304                 }
1305
1306         }
1307 }
1308
1309 #if 0
1310 void
1311 mono_ssa_strength_reduction (MonoCompile *cfg)
1312 {
1313         MonoBasicBlock *bb;
1314         int i;
1315
1316         g_assert (cfg->comp_done & MONO_COMP_SSA);
1317         g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1318         g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1319
1320         for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1321                 GList *lp = bb->loop_blocks;
1322
1323                 if (lp) {
1324                         MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1325
1326                         /* we only consider loops with 2 in bblocks */
1327                         if (!h->in_count == 2)
1328                                 continue;
1329
1330                         for (i = 0; i < cfg->num_varinfo; i++) {
1331                                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1332                         
1333                                 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1334                                     info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1335                                         MonoInst *v = info->def->inst_i1;
1336
1337
1338                                         printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1339                                 }
1340                         }
1341                 }
1342         }
1343 }
1344 #endif
1345
1346 #endif /* DISABLE_JIT */