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