Ternary IR ops in mini.
[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)
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)
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                                 ins->klass = var->klass;
434                                 break;
435                         }
436
437                         ins->inst_phi_args =  mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
438                         ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
439
440                         /* For debugging */
441                         for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
442                                 ins->inst_phi_args [j + 1] = -1;
443
444                         ins->dreg = cfg->varinfo [i]->dreg;
445
446                         mono_bblock_insert_before_ins (bb, bb->code, ins);
447
448 #ifdef DEBUG_SSA
449                         printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
450 #endif
451                 }
452         }
453
454         /* free the stuff */
455         g_free (vinfo);
456         g_free (buf_start);
457
458         /* Renaming phase */
459
460         stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
461         memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
462
463         lvreg_stack = g_new0 (guint32, cfg->next_vreg);
464         lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
465         stack_history_size = 10240;
466         stack_history = g_new (RenameInfo, stack_history_size);
467         originals = g_new0 (gboolean, cfg->num_varinfo);
468         mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
469         g_free (stack_history);
470         g_free (originals);
471
472         if (cfg->verbose_level >= 4)
473                 printf ("\nEND COMPUTE SSA.\n\n");
474
475         cfg->comp_done |= MONO_COMP_SSA;
476 }
477
478 void
479 mono_ssa_remove (MonoCompile *cfg)
480 {
481         MonoInst *ins, *var, *move;
482         int i, j, first;
483
484         g_assert (cfg->comp_done & MONO_COMP_SSA);
485
486         for (i = 0; i < cfg->num_bblocks; ++i) {
487                 MonoBasicBlock *bb = cfg->bblocks [i];
488
489                 if (cfg->verbose_level >= 4)
490                         printf ("\nREMOVE SSA %d:\n", bb->block_num);
491
492                 for (ins = bb->code; ins; ins = ins->next) {
493                         if (MONO_IS_PHI (ins)) {
494                                 g_assert (ins->inst_phi_args [0] == bb->in_count);
495                                 var = get_vreg_to_inst (cfg, ins->dreg);
496
497                                 /* Check for PHI nodes where all the inputs are the same */
498                                 first = ins->inst_phi_args [1];
499
500                                 for (j = 1; j < bb->in_count; ++j)
501                                         if (first != ins->inst_phi_args [j + 1])
502                                                 break;
503
504                                 if ((bb->in_count > 1) && (j == bb->in_count)) {
505                                         ins->opcode = op_phi_to_move (ins->opcode);
506                                         if (ins->opcode == OP_VMOVE)
507                                                 g_assert (ins->klass);
508                                         ins->sreg1 = first;
509                                 } else {
510                                         for (j = 0; j < bb->in_count; j++) {
511                                                 MonoBasicBlock *pred = bb->in_bb [j];
512                                                 int sreg = ins->inst_phi_args [j + 1];
513
514                                                 if (cfg->verbose_level >= 4)
515                                                         printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
516                                                 if (var->dreg != sreg) {
517                                                         MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
518                                                         if (move->opcode == OP_VMOVE) {
519                                                                 g_assert (ins->klass);
520                                                                 move->klass = ins->klass;
521                                                         }
522                                                         move->dreg = var->dreg;
523                                                         move->sreg1 = sreg;
524                                                         mono_add_ins_to_end (pred, move);
525                                                 }
526                                         }
527
528                                         ins->opcode = OP_NOP;
529                                         ins->dreg = -1;
530                                 }
531                         }
532                 }
533         }
534
535         if (cfg->verbose_level >= 4) {
536                 for (i = 0; i < cfg->num_bblocks; ++i) {
537                         MonoBasicBlock *bb = cfg->bblocks [i];
538
539                         mono_print_bb (bb, "AFTER REMOVE SSA:");
540                 }
541         }
542
543         /*
544          * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce 
545          * the variables if possible. Since the newly introduced SSA variables don't
546          * have overlapping live ranges (because we don't do agressive optimization), we
547          * can coalesce them into the original variable.
548          */
549
550         for (i = 0; i < cfg->num_bblocks; ++i) {
551                 MonoBasicBlock *bb = cfg->bblocks [i];
552
553                 for (ins = bb->code; ins; ins = ins->next) {
554                         const char *spec = INS_INFO (ins->opcode);
555                         int num_sregs, j;
556                         int sregs [MONO_MAX_SRC_REGS];
557
558                         if (ins->opcode == OP_NOP)
559                                 continue;
560
561                         if (spec [MONO_INST_DEST] != ' ') {
562                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
563
564                                 if (var) {
565                                         MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
566                                         
567                                         /* 
568                                          * The third condition avoids coalescing with variables eliminated
569                                          * during deadce.
570                                          */
571                                         if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
572                                                 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
573                                                 ins->dreg = cfg->varinfo [vmv->reg]->dreg; 
574                                         }
575                                 }
576                         }
577
578                         num_sregs = mono_inst_get_src_registers (ins, sregs);
579                         for (j = 0; j < num_sregs; ++j) {
580                                 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
581
582                                 if (var) {
583                                         MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
584
585                                         if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
586                                                 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
587                                                 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
588                                         }
589                                 }
590                         }
591                         mono_inst_set_src_registers (ins, sregs);
592                 }
593         }
594
595         for (i = 0; i < cfg->num_varinfo; ++i) {
596                 MONO_VARINFO (cfg, i)->reg = -1;
597         }
598
599         if (cfg->comp_done & MONO_COMP_REACHABILITY)
600                 unlink_unused_bblocks (cfg);
601
602         cfg->comp_done &= ~MONO_COMP_LIVENESS;
603
604         cfg->comp_done &= ~MONO_COMP_SSA;
605 }
606
607 static void
608 mono_ssa_create_def_use (MonoCompile *cfg) 
609 {
610         MonoBasicBlock *bb;
611         MonoInst *ins;
612         int i;
613
614         g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
615
616         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
617                 for (ins = bb->code; ins; ins = ins->next) {
618                         const char *spec = INS_INFO (ins->opcode);
619                         MonoMethodVar *info;
620                         int num_sregs;
621                         int sregs [MONO_MAX_SRC_REGS];
622
623                         if (ins->opcode == OP_NOP)
624                                 continue;
625
626                         /* SREGs */
627                         num_sregs = mono_inst_get_src_registers (ins, sregs);
628                         for (i = 0; i < num_sregs; ++i) {
629                                 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
630                                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
631                                         record_use (cfg, var, bb, ins);
632                         }
633
634                         if (MONO_IS_STORE_MEMBASE (ins)) {
635                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
636                                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
637                                         record_use (cfg, var, bb, ins);
638                         }
639
640                         if (MONO_IS_PHI (ins)) {
641                                 for (i = ins->inst_phi_args [0]; i > 0; i--) {
642                                         g_assert (ins->inst_phi_args [i] != -1);
643                                         record_use (cfg,  get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
644                                 }
645                         }
646
647                         /* DREG */
648                         if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
649                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
650
651                                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
652                                         info = MONO_VARINFO (cfg, var->inst_c0);
653                                         info->def = ins;
654                                         info->def_bb = bb;
655                                 }
656                         }
657                 }
658         }
659
660         cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
661 }
662
663 static void
664 mono_ssa_copyprop (MonoCompile *cfg)
665 {
666         int i, index;
667         GList *l;
668
669         g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
670
671         for (index = 0; index < cfg->num_varinfo; ++index) {
672                 MonoInst *var = cfg->varinfo [index];
673                 MonoMethodVar *info = MONO_VARINFO (cfg, index);
674
675                 if (info->def && (MONO_IS_MOVE (info->def))) {
676                         MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
677
678                         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))) {
679                                 /* Rewrite all uses of var to be uses of var2 */
680                                 int dreg = var->dreg;
681                                 int sreg1 = var2->dreg;
682                                 const char *spec;
683
684                                 l = info->uses;
685                                 while (l) {
686                                         MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
687                                         MonoInst *ins = u->inst;
688                                         GList *next = l->next;
689                                         int num_sregs;
690                                         int sregs [MONO_MAX_SRC_REGS];
691
692                                         spec = INS_INFO (ins->opcode);
693
694                                         num_sregs = mono_inst_get_src_registers (ins, sregs);
695                                         for (i = 0; i < num_sregs; ++i) {
696                                                 if (sregs [i] == dreg)
697                                                         break;
698                                         }
699                                         if (i < num_sregs) {
700                                                 g_assert (sregs [i] == dreg);
701                                                 sregs [i] = sreg1;
702                                                 mono_inst_set_src_registers (ins, sregs);
703                                         } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
704                                                 ins->dreg = sreg1;
705                                         } else if (MONO_IS_PHI (ins)) {
706                                                 for (i = ins->inst_phi_args [0]; i > 0; i--) {
707                                                         int sreg = ins->inst_phi_args [i];
708                                                         if (sreg == var->dreg)
709                                                                 break;
710                                                 }
711                                                 g_assert (i > 0);
712                                                 ins->inst_phi_args [i] = sreg1;
713                                         }
714                                         else
715                                                 g_assert_not_reached ();
716
717                                         record_use (cfg, var2, u->bb, ins);
718
719                                         l = next;
720                                 }
721
722                                 info->uses = NULL;
723                         }
724                 }
725         }
726
727         if (cfg->verbose_level >= 4) {
728                 MonoBasicBlock *bb;
729
730                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
731                         mono_print_bb (bb, "AFTER SSA COPYPROP");
732         }
733 }
734
735 static int
736 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
737 {
738         MonoInst *args [MONO_MAX_SRC_REGS];
739         int rs [MONO_MAX_SRC_REGS];
740         MonoInst *c0;
741         gboolean const_args = TRUE;
742         const char *spec = INS_INFO (ins->opcode);
743         int num_sregs, i;
744         int sregs [MONO_MAX_SRC_REGS];
745
746         /* Short-circuit this */
747         if (ins->opcode == OP_ICONST) {
748                 *res = ins;
749                 return 1;
750         }
751
752         if (ins->opcode == OP_NOP)
753                 return 2;
754
755         num_sregs = mono_inst_get_src_registers (ins, sregs);
756         for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
757                 args [i] = NULL;
758         for (i = 0; i < num_sregs; ++i) {
759                 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
760
761                 rs [i] = 2;
762                 args [i] = carray [sregs [i]];
763                 if (args [i])
764                         rs [i] = 1;
765                 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
766                         rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
767                 if (rs [i] != 1)
768                         const_args = FALSE;
769         }
770
771         c0 = NULL;
772
773         if (num_sregs > 0 && const_args) {
774                 g_assert (num_sregs <= 2);
775                 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
776                         // Cached value
777                         *res = carray [ins->dreg];
778                         return 1;
779                 }
780                 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
781                 if (c0) {
782                         if (G_UNLIKELY (cfg->verbose_level > 1)) {
783                                 printf ("\t cfold -> ");
784                                 mono_print_ins (c0);
785                         }
786                         *res = c0;
787                         return 1;
788                 }
789                 else
790                         /* Can't cfold this ins */
791                         return 2;
792         }
793
794         if (num_sregs == 0)
795                 return 2;
796         for (i = 0; i < num_sregs; ++i) {
797                 if (rs [i] == 2)
798                         return 2;
799         }
800         return 0;
801 }
802
803 static inline void
804 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
805 {
806         if (info->cpstate >= state)
807                 return;
808
809         info->cpstate = state;
810
811         if (G_UNLIKELY (cfg->verbose_level > 1))
812                 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
813
814         if (state == 1)
815                 g_assert (c0);
816
817         carray [cfg->varinfo [info->idx]->dreg] = c0;
818
819         if (!g_list_find (*cvars, info)) {
820                 *cvars = g_list_prepend (*cvars, info);
821         }
822 }
823
824 static inline void
825 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
826 {
827         if (G_UNLIKELY (cfg->verbose_level > 1))
828                 printf ("\tAdd BB%d to worklist\n", bb->block_num);
829
830     if (!(bb->flags &  BB_REACHABLE)) {
831             bb->flags |= BB_REACHABLE;
832                 *bblist = g_list_prepend (*bblist, bb);
833         }
834 }
835
836 static void
837 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
838 {
839         const char *spec = INS_INFO (ins->opcode);
840
841         if (ins->opcode == OP_NOP)
842                 return;
843
844         if (cfg->verbose_level > 1)
845                 mono_print_ins (ins);
846
847         /* FIXME: Support longs/floats */
848         /* FIXME: Work on vregs as well */
849
850         if (MONO_IS_PHI (ins)) {
851                 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
852                 MonoInst *c0 = NULL;
853                 int j;
854
855                 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
856                         MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
857                         MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
858                         MonoInst *src = mv->def;
859
860                         if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
861                                 continue;
862
863                         if (!mv->def || !src || mv->cpstate == 2) {
864                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
865                                 break;
866                         }
867                                         
868                         if (mv->cpstate == 0)
869                                 continue;
870
871                         g_assert (carray [var->dreg]);
872
873                         if (!c0)
874                                 c0 = carray [var->dreg];
875                 
876                         /* FIXME: */
877                         if (c0->opcode != OP_ICONST) {
878                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
879                                 break;
880                         }
881
882                         if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
883                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
884                                 break;
885                         }
886                 }
887                                 
888                 if (c0 && info->cpstate < 1) {
889                         change_varstate (cfg, cvars, info, 1, c0, carray);
890
891                         g_assert (c0->opcode == OP_ICONST);
892                 }
893         }
894         else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
895                 MonoInst *var, *c0;
896                 int state;
897
898                 if (spec [MONO_INST_DEST] !=  ' ')
899                         var = get_vreg_to_inst (cfg, ins->dreg);
900                 else
901                         var = NULL;
902
903                 c0 = NULL;
904                 state = evaluate_ins (cfg, ins, &c0, carray);
905
906                 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
907                         MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
908
909                         if (info->cpstate < 2) {
910                                 if (state == 1)
911                                         change_varstate (cfg, cvars, info, 1, c0, carray);
912                                 else if (state == 2)
913                                         change_varstate (cfg, cvars, info, 2, NULL, carray);
914                         }
915                 }
916                 else if (!var && (ins->dreg != -1)) {
917                         /*
918                          * We don't record def-use information for local vregs since it would be 
919                          * expensive. Instead, we depend on the fact that all uses of the vreg are in
920                          * the same bblock, so they will be examined after the definition.
921                          * FIXME: This isn't true if the ins is visited through an SSA edge.
922                          */
923                         if (c0) {
924                                 carray [ins->dreg] = c0;
925                         } else {
926                                 if (carray [ins->dreg]) {
927                                         /* 
928                                          * The state of the vreg changed from constant to non-constant
929                                          * -> need to rescan the whole bblock.
930                                          */
931                                         carray [ins->dreg] = NULL;
932                                         /* FIXME: Speed this up */
933
934                                         if (!g_list_find (*bblist, bb))
935                                                 *bblist = g_list_prepend (*bblist, bb);
936                                 }
937                         }
938                 }
939
940                 if (MONO_IS_JUMP_TABLE (ins)) {
941                         int i;
942                         MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
943
944                         if (!ins->next || ins->next->opcode != OP_PADD) {
945                                 /* The PADD was optimized away */
946                                 /* FIXME: handle this as well */
947                                 for (i = 0; i < table->table_size; i++)
948                                         if (table->table [i])
949                                                 add_cprop_bb (cfg, table->table [i], bblist);
950                                 return;
951                         }
952
953                         g_assert (ins->next->opcode == OP_PADD);
954                         g_assert (ins->next->sreg1 == ins->dreg);
955
956                         if (carray [ins->next->sreg2]) {
957 #if SIZEOF_REGISTER == 8
958                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
959 #else
960                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
961 #endif
962                                 if ((idx < 0) || (idx >= table->table_size))
963                                         /* Out-of-range, no branch is executed */
964                                         return;
965                                 else
966                                         if (table->table [idx])
967                                                 add_cprop_bb (cfg, table->table [idx], bblist);
968                         }
969                         else {
970                                 for (i = 0; i < table->table_size; i++)
971                                         if (table->table [i])
972                                                 add_cprop_bb (cfg, table->table [i], bblist);
973                         }
974                 }
975
976                 if (ins->opcode == OP_SWITCH) {
977                         int i;
978                         MonoJumpInfoBBTable *table = ins->inst_p0;
979
980                         for (i = 0; i < table->table_size; i++)
981                                 if (table->table [i])
982                                         add_cprop_bb (cfg, table->table [i], bblist);
983                 }
984
985                 /* Handle COMPARE+BRCOND pairs */
986                 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
987                         if (c0) {
988                                 g_assert (c0->opcode == OP_ICONST);
989
990                                 if (c0->inst_c0)
991                                         ins->next->flags |= MONO_INST_CFOLD_TAKEN;
992                                 else
993                                         ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
994                         }
995                         else {
996                                 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
997                         }
998
999                         visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1000                 }
1001         } else if (ins->opcode == OP_BR) {
1002                 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1003         } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1004                 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1005                         add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1006                 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1007                         if (ins->inst_false_bb)
1008                                 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1009         } else {
1010                         add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1011                         if (ins->inst_false_bb)
1012                                 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1013                 }
1014         }
1015 }
1016
1017 /**
1018  * fold_ins:
1019  *
1020  *   Replace INS with its constant value, if it exists
1021  */
1022 static void
1023 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1024 {
1025         const char *spec = INS_INFO (ins->opcode);
1026         int opcode2;
1027         int num_sregs = mono_inst_get_num_src_registers (ins);
1028
1029         if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1030                 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1031                         /* Perform constant folding */
1032                         /* FIXME: */
1033                         g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1034                         ins->opcode = OP_ICONST;
1035                         ins->inst_c0 = carray [ins->dreg]->inst_c0;
1036                         MONO_INST_NULLIFY_SREGS (ins);
1037                 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1038                         /* Perform op->op_imm conversion */
1039                         opcode2 = mono_op_to_op_imm (ins->opcode);
1040                         if (opcode2 != -1) {
1041                                 ins->opcode = opcode2;
1042                                 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1043                                 ins->sreg2 = -1;
1044
1045                                 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1046                                         ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1047                         }
1048                 } else {
1049                         /* FIXME: Handle 3 op insns */
1050                 }
1051
1052                 if (MONO_IS_JUMP_TABLE (ins)) {
1053                         int i;
1054                         MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1055
1056                         if (!ins->next || ins->next->opcode != OP_PADD) {
1057                                 /* The PADD was optimized away */
1058                                 /* FIXME: handle this as well */
1059                                 return;
1060                         }
1061
1062                         g_assert (ins->next->opcode == OP_PADD);
1063                         g_assert (ins->next->sreg1 == ins->dreg);
1064                         g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1065
1066                         if (carray [ins->next->sreg2]) {
1067                                 /* Convert to a simple branch */
1068 #if SIZEOF_REGISTER == 8
1069                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1070 #else
1071                                 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1072 #endif
1073
1074                                 if (!((idx >= 0) && (idx < table->table_size))) {
1075                                         /* Out of range, eliminate the whole switch */
1076                                         for (i = 0; i < table->table_size; ++i) {
1077                                                 remove_bb_from_phis (cfg, bb, table->table [i]);
1078                                                 mono_unlink_bblock (cfg, bb, table->table [i]);
1079                                         }
1080
1081                                         NULLIFY_INS (ins);
1082                                         NULLIFY_INS (ins->next);
1083                                         NULLIFY_INS (ins->next->next);
1084                                         if (ins->next->next->next)
1085                                                 NULLIFY_INS (ins->next->next->next);
1086
1087                                         return;
1088                                 }
1089
1090                                 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1091                                         /* A one-way switch which got optimized away */
1092                                         if (G_UNLIKELY (cfg->verbose_level > 1)) {
1093                                                 printf ("\tNo cfold on ");
1094                                                 mono_print_ins (ins);
1095                                         }
1096                                         return;
1097                                 }
1098
1099                                 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1100                                         printf ("\tcfold on ");
1101                                         mono_print_ins (ins);
1102                                 }
1103
1104                                 /* Unlink target bblocks */
1105                                 for (i = 0; i < table->table_size; ++i) {
1106                                         if (i != idx) {
1107                                                 remove_bb_from_phis (cfg, bb, table->table [i]);
1108                                                 mono_unlink_bblock (cfg, bb, table->table [i]);
1109                                         }
1110                                 }
1111
1112                                 /* Change the OP_BR_REG to a simple branch */
1113                                 ins->next->next->next->opcode = OP_BR;
1114                                 ins->next->next->next->inst_target_bb = table->table [idx];
1115                                 ins->next->next->next->sreg1 = -1;
1116
1117                                 /* Nullify the other instructions */
1118                                 NULLIFY_INS (ins);
1119                                 NULLIFY_INS (ins->next);
1120                                 NULLIFY_INS (ins->next->next);
1121                         }
1122                 }
1123         } 
1124         else if (MONO_IS_COND_BRANCH_OP (ins)) {
1125                 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1126                         remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1127                         mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1128                         ins->opcode = OP_BR;
1129                         ins->inst_target_bb = ins->inst_true_bb;
1130                 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1131                         remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1132                         mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1133                         ins->opcode = OP_BR;
1134                         ins->inst_target_bb = ins->inst_false_bb;
1135                 }
1136         }
1137 }
1138
1139 void
1140 mono_ssa_cprop (MonoCompile *cfg) 
1141 {
1142         MonoInst **carray;
1143         MonoBasicBlock *bb;
1144         GList *bblock_list, *cvars;
1145         GList *tmp;
1146         int i;
1147         //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1148
1149         carray = g_new0 (MonoInst*, cfg->next_vreg);
1150
1151         if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1152                 mono_ssa_create_def_use (cfg);
1153
1154         bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1155         cfg->bb_entry->flags |= BB_REACHABLE;
1156
1157         memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1158
1159         for (i = 0; i < cfg->num_varinfo; i++) {
1160                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1161                 if (!info->def)
1162                         info->cpstate = 2;
1163         }
1164
1165         cvars = NULL;
1166
1167         while (bblock_list) {
1168                 MonoInst *inst;
1169
1170                 bb = (MonoBasicBlock *)bblock_list->data;
1171
1172                 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1173
1174                 g_assert (bb->flags &  BB_REACHABLE);
1175
1176                 if (bb->out_count == 1) {
1177                         if (!(bb->out_bb [0]->flags &  BB_REACHABLE)) {
1178                                 bb->out_bb [0]->flags |= BB_REACHABLE;
1179                                 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1180                         }
1181                 }
1182
1183                 if (cfg->verbose_level > 1)
1184                         printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1185
1186                 for (inst = bb->code; inst; inst = inst->next) {
1187                         visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1188                 }
1189
1190                 while (cvars) {
1191                         MonoMethodVar *info = (MonoMethodVar *)cvars->data;                     
1192                         cvars = g_list_delete_link (cvars, cvars);
1193
1194                         for (tmp = info->uses; tmp; tmp = tmp->next) {
1195                                 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1196                                 if (!(ui->bb->flags & BB_REACHABLE))
1197                                         continue;
1198                                 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1199                         }
1200                 }
1201         }
1202
1203         for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1204                 MonoInst *inst;
1205                 for (inst = bb->code; inst; inst = inst->next) {
1206                         fold_ins (cfg, bb, inst, carray);
1207                 }
1208         }
1209
1210         g_free (carray);
1211
1212         cfg->comp_done |= MONO_COMP_REACHABILITY;
1213
1214         /* fixme: we should update usage infos during cprop, instead of computing it again */
1215         cfg->comp_done &=  ~MONO_COMP_SSA_DEF_USE;
1216         for (i = 0; i < cfg->num_varinfo; i++) {
1217                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1218                 info->def = NULL;
1219                 info->uses = NULL;
1220         }
1221 }
1222
1223 static inline void
1224 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1225 {
1226         GList *tmp;
1227
1228         *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1229
1230         for (tmp = use->uses; tmp; tmp = tmp->next) {
1231                 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1232                 if (ui->inst == var->def) {
1233                         /* from the mempool */
1234                         use->uses = g_list_remove_link (use->uses, tmp);
1235                         break;
1236                 }
1237         }       
1238 }
1239
1240 void
1241 mono_ssa_deadce (MonoCompile *cfg) 
1242 {
1243         int i;
1244         GList *work_list;
1245
1246         g_assert (cfg->comp_done & MONO_COMP_SSA);
1247
1248         //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1249
1250         if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1251                 mono_ssa_create_def_use (cfg);
1252
1253         mono_ssa_copyprop (cfg);
1254
1255         work_list = NULL;
1256         for (i = 0; i < cfg->num_varinfo; i++) {
1257                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1258                 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1259         }
1260
1261         while (work_list) {
1262                 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1263                 work_list = g_list_remove_link (work_list, work_list);
1264
1265                 /* 
1266                  * The second part of the condition happens often when PHI nodes have their dreg
1267                  * as one of their arguments due to the fact that we use the original vars.
1268                  */
1269                 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1270                         MonoInst *def = info->def;
1271
1272                         /* Eliminating FMOVE could screw up the fp stack */
1273                         if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1274                                 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1275                                 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1276                                         add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1277                                 def->opcode = OP_NOP;
1278                                 def->dreg = def->sreg1 = def->sreg2 = -1;
1279                                 info->reg = -1;
1280                         } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1281                                 def->opcode = OP_NOP;
1282                                 def->dreg = def->sreg1 = def->sreg2 = -1;
1283                                 info->reg = -1;
1284                         } else if (MONO_IS_PHI (def)) {
1285                                 int j;
1286                                 for (j = def->inst_phi_args [0]; j > 0; j--) {
1287                                         MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1288                                         add_to_dce_worklist (cfg, info, u, &work_list);
1289                                 }
1290                                 def->opcode = OP_NOP;
1291                                 def->dreg = def->sreg1 = def->sreg2 = -1;
1292                                 info->reg = -1;
1293                         }
1294                         else if (def->opcode == OP_NOP) {
1295                         }
1296                         //else
1297                         //mono_print_ins (def);
1298                 }
1299
1300         }
1301 }
1302
1303 #if 0
1304 void
1305 mono_ssa_strength_reduction (MonoCompile *cfg)
1306 {
1307         MonoBasicBlock *bb;
1308         int i;
1309
1310         g_assert (cfg->comp_done & MONO_COMP_SSA);
1311         g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1312         g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1313
1314         for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1315                 GList *lp = bb->loop_blocks;
1316
1317                 if (lp) {
1318                         MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1319
1320                         /* we only consider loops with 2 in bblocks */
1321                         if (!h->in_count == 2)
1322                                 continue;
1323
1324                         for (i = 0; i < cfg->num_varinfo; i++) {
1325                                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1326                         
1327                                 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1328                                     info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1329                                         MonoInst *v = info->def->inst_i1;
1330
1331
1332                                         printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1333                                 }
1334                         }
1335                 }
1336         }
1337 }
1338 #endif
1339
1340 #endif /* DISABLE_JIT */