2008-07-12 Zoltan Varga <vargaz@gmail.com>
[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 <string.h>
10 #include <mono/metadata/debug-helpers.h>
11
12 #include "mini.h"
13
14 #ifndef DISABLE_SSA
15
16 #define USE_ORIGINAL_VARS
17 #define CREATE_PRUNED_SSA
18
19 //#define DEBUG_SSA 1
20
21 #define NEW_PHI(cfg,dest,val) do {      \
22                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
23                 (dest)->opcode = OP_PHI;        \
24                 (dest)->inst_c0 = (val);        \
25         } while (0)
26
27 #define NEW_ICONST(cfg,dest,val) do {   \
28                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
29                 (dest)->opcode = OP_ICONST;     \
30                 (dest)->inst_c0 = (val);        \
31                 (dest)->type = STACK_I4;        \
32         } while (0)
33
34
35 static GList*
36 g_list_prepend_mempool (GList* l, MonoMemPool* mp, gpointer datum)
37 {
38         GList* n = mono_mempool_alloc (mp, sizeof (GList));
39         n->next = l;
40         n->prev = NULL;
41         n->data = datum;
42         return n;
43 }
44
45 static void 
46 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
47 {
48         int i;
49
50         for (i = 0; i < bb->out_count; i++) {
51                 if (bb->out_bb [i] == target) {
52                         bb->out_bb [i] = bb->out_bb [--bb->out_count];
53                         break;
54                 }
55         }
56         for (i = 0; i < target->in_count; i++) {
57                 if (target->in_bb [i] == bb) {
58                         target->in_bb [i] = target->in_bb [--target->in_count];
59                         break;
60                         
61                 }
62         }
63 }
64
65 static void
66 unlink_unused_bblocks (MonoCompile *cfg) 
67 {
68         int i, j;
69         MonoBasicBlock *bb;
70
71         g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
72
73         for (bb = cfg->bb_entry; bb && bb->next_bb;) {
74                 if (!(bb->next_bb->flags & BB_REACHABLE)) {
75                         bb->next_bb = bb->next_bb->next_bb;
76                 } else 
77                         bb = bb->next_bb;
78         }
79
80         for (i = 1; i < cfg->num_bblocks; i++) {
81                 bb = cfg->bblocks [i];
82                
83                 if (!(bb->flags & BB_REACHABLE)) {
84                         for (j = 0; j < bb->in_count; j++) {
85                                 unlink_target (bb->in_bb [j], bb);      
86                         }
87                         for (j = 0; j < bb->out_count; j++) {
88                                 unlink_target (bb, bb->out_bb [j]);     
89                         }
90                 }
91  
92         }
93 }
94
95
96
97 static void
98 replace_usage (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **stack)
99 {
100         int arity;
101
102         if (!inst)
103                 return;
104
105         arity = mono_burg_arity [inst->opcode];
106
107         if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) && 
108             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
109                 MonoInst *new_var;
110                 int idx = inst->inst_i0->inst_c0;
111                         
112                 if (stack [idx]) {
113                         new_var = stack [idx];
114                 } else {
115                         new_var = cfg->varinfo [idx];
116
117                         if ((new_var->opcode != OP_ARG) && (new_var->opcode != OP_LOCAL)) {
118                                 /* uninitialized variable ? */
119                                 g_warning ("using uninitialized variables %d in BB%d (%s)", idx, bb->block_num,
120                                            mono_method_full_name (cfg->method, TRUE));
121                                 //g_assert_not_reached ();
122                         }
123                 }
124 #ifdef DEBUG_SSA
125                 printf ("REPLACE BB%d %d %d\n", bb->block_num, idx, new_var->inst_c0);
126 #endif
127                 inst->inst_i0 = new_var;
128         } else {
129
130                 if (arity) {
131                         if (inst->ssa_op != MONO_SSA_STORE)
132                                 replace_usage (cfg, bb, inst->inst_left, stack);
133                         if (arity > 1)
134                                 replace_usage (cfg, bb, inst->inst_right, stack);
135                 }
136         }
137 }
138
139 #if 0
140
141 static int
142 extends_live (MonoInst *inst)
143 {
144         int arity;
145
146         if (!inst)
147                 return 0;
148
149         arity = mono_burg_arity [inst->opcode];
150
151         if (inst->ssa_op == MONO_SSA_LOAD && 
152             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
153                 return 1;
154         } else {
155                 if (arity) {
156                         if (inst->ssa_op != MONO_SSA_STORE)
157                                 if (extends_live (inst->inst_left))
158                                         return 1;
159                         if (arity > 1)
160                                 if (extends_live (inst->inst_right))
161                                         return 1;
162                 }
163         }
164
165         return 0;
166 }
167
168 static int
169 replace_usage_new (MonoCompile *cfg, MonoInst *inst, int varnum, MonoInst *rep)
170 {
171         int arity;
172
173         if (!inst)
174                 return 0;
175
176         arity = mono_burg_arity [inst->opcode];
177
178         if ((inst->ssa_op == MONO_SSA_LOAD) && 
179             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
180             inst->inst_i0->inst_c0 == varnum && rep->type == inst->type) {
181                 *inst = *rep;
182                 return 1;
183         } else {
184                 if (arity) {
185                         if (inst->ssa_op != MONO_SSA_STORE)
186                                 if (replace_usage_new (cfg, inst->inst_left, varnum, rep))
187                                         return 1;
188                         if (arity > 1)
189                                 if (replace_usage_new (cfg, inst->inst_right, varnum, rep))
190                                         return 1;
191                 }
192         }
193
194         return 0;
195 }
196
197 #endif
198
199 static void
200 mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, MonoInst **stack) 
201 {
202         MonoInst *inst, *new_var;
203         int i, j, idx;
204         GSList *tmp;
205         MonoInst **new_stack;
206
207 #ifdef DEBUG_SSA
208         printf ("RENAME VARS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
209 #endif
210
211         MONO_BB_FOR_EACH_INS (bb, inst) {
212                 if (inst->opcode != OP_PHI)
213                         replace_usage (cfg, bb, inst, stack);
214
215                 if (inst->ssa_op == MONO_SSA_STORE && 
216                     (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
217                         idx = inst->inst_i0->inst_c0;
218                         g_assert (idx < max_vars);
219
220                         if ((!stack [idx]) && (bb == cfg->bb_init) && (inst->inst_i0->opcode != OP_ARG)) {
221                                 new_var = cfg->varinfo [idx];
222                         } else {
223                                 new_var = mono_compile_create_var (cfg, inst->inst_i0->inst_vtype,  inst->inst_i0->opcode);
224                                 new_var->flags = inst->inst_i0->flags;
225                         }
226 #ifdef DEBUG_SSA
227                         printf ("DEF %d %d\n", idx, new_var->inst_c0);
228 #endif
229                         inst->inst_i0 = new_var;
230
231 #ifdef USE_ORIGINAL_VARS
232                         MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
233 #endif
234
235                         stack [idx] = new_var;
236                 }
237         }
238
239         for (i = 0; i < bb->out_count; i++) {
240                 MonoBasicBlock *n = bb->out_bb [i];
241
242                 for (j = 0; j < n->in_count; j++)
243                         if (n->in_bb [j] == bb)
244                                 break;
245                 
246                 MONO_BB_FOR_EACH_INS (n, inst) {
247                         if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->opcode == OP_PHI) {
248                                 idx = inst->inst_i1->inst_c0;
249                                 if (stack [idx])
250                                         new_var = stack [idx];
251                                 else
252                                         new_var = cfg->varinfo [idx];
253 #ifdef DEBUG_SSA
254                                 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
255 #endif
256                                 inst->inst_i1->inst_phi_args [j + 1] = new_var->inst_c0;
257                                 
258                         }
259                 }
260         }
261
262         if (bb->dominated) {
263                 new_stack = g_new (MonoInst*, max_vars);
264                 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
265                         memcpy (new_stack, stack, sizeof (MonoInst *) * max_vars); 
266                         mono_ssa_rename_vars (cfg, max_vars, (MonoBasicBlock *)tmp->data, new_stack);
267                 }
268                 g_free (new_stack);
269         }
270 }
271
272 void
273 mono_ssa_compute (MonoCompile *cfg)
274 {
275         int i, idx;
276         MonoBitSet *set;
277         MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
278         MonoInst *inst, *store, **stack;
279
280         g_assert (!(cfg->comp_done & MONO_COMP_SSA));
281
282         /* we dont support methods containing exception clauses */
283         g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
284         g_assert (!cfg->disable_ssa);
285
286         //printf ("COMPUTS SSA %s %d\n", mono_method_full_name (cfg->method, TRUE), cfg->num_varinfo);
287
288 #ifdef CREATE_PRUNED_SSA
289         /* we need liveness for pruned SSA */
290         if (!(cfg->comp_done & MONO_COMP_LIVENESS))
291                 mono_analyze_liveness (cfg);
292 #endif
293
294         mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
295
296         for (i = 0; i < cfg->num_varinfo; ++i) {
297                 vinfo [i].def_in = mono_bitset_new (cfg->num_bblocks, 0);
298                 vinfo [i].idx = i;
299                 /* implizit reference at start */
300                 mono_bitset_set (vinfo [i].def_in, 0);
301         }
302         for (i = 0; i < cfg->num_bblocks; ++i) {
303                 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], inst) {
304                         if (inst->ssa_op == MONO_SSA_STORE) {
305                                 idx = inst->inst_i0->inst_c0;
306                                 g_assert (idx < cfg->num_varinfo);
307                                 mono_bitset_set (vinfo [idx].def_in, i);
308                         } 
309                 }
310         }
311
312         /* insert phi functions */
313         for (i = 0; i < cfg->num_varinfo; ++i) {
314                 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
315                 vinfo [i].dfrontier = set;
316                 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
317                         MonoBasicBlock *bb = cfg->bblocks [idx];
318
319                         /* fixme: create pruned SSA? we would need liveness information for that */
320
321                         if (bb == cfg->bb_exit)
322                                 continue;
323
324                         if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
325                                 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
326                                 continue;
327                         }
328
329                         NEW_PHI (cfg, inst, i);
330
331                         inst->inst_phi_args =  mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
332                         inst->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
333
334                         store = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));
335                         if (!cfg->varinfo [i]->inst_vtype->type)
336                                 g_assert_not_reached ();
337                         store->opcode = mono_type_to_stind (cfg->varinfo [i]->inst_vtype);
338                         store->ssa_op = MONO_SSA_STORE;
339                         store->inst_i0 = cfg->varinfo [i];
340                         store->inst_i1 = inst;
341                         store->klass = store->inst_i0->klass;
342              
343                         MONO_INST_LIST_ADD (&store->node, &bb->ins_list);
344
345 #ifdef DEBUG_SSA
346                         printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
347 #endif
348                 }
349         }
350
351         /* free the stuff */
352         for (i = 0; i < cfg->num_varinfo; ++i)
353                 mono_bitset_free (vinfo [i].def_in);
354         g_free (vinfo);
355
356
357         stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
358                 
359         for (i = 0; i < cfg->num_varinfo; i++)
360                 stack [i] = NULL;
361
362         mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, stack);
363
364         cfg->comp_done |= MONO_COMP_SSA;
365 }
366
367 #ifndef USE_ORIGINAL_VARS
368 static GPtrArray *
369 mono_ssa_get_allocatable_vars (MonoCompile *cfg)
370 {
371         GHashTable *type_hash;
372         GPtrArray *varlist_array = g_ptr_array_new ();
373         int tidx, i;
374
375         g_assert (cfg->comp_done & MONO_COMP_LIVENESS);
376
377         type_hash = g_hash_table_new (NULL, NULL);
378
379         for (i = 0; i < cfg->num_varinfo; i++) {
380                 MonoInst *ins = cfg->varinfo [i];
381                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
382
383                 /* unused vars */
384                 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
385                         continue;
386
387                 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || 
388                     (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG) || vmv->reg != -1)
389                         continue;
390
391                 g_assert (ins->inst_vtype);
392                 g_assert (vmv->reg == -1);
393                 g_assert (i == vmv->idx);
394
395                 if (!(tidx = (int)g_hash_table_lookup (type_hash, ins->inst_vtype))) {
396                         GList *vars = g_list_append (NULL, vmv);
397                         g_ptr_array_add (varlist_array, vars);
398                         g_hash_table_insert (type_hash, ins->inst_vtype, (gpointer)varlist_array->len);
399                 } else {
400                         tidx--;
401                         g_ptr_array_index (varlist_array, tidx) =
402                                 mono_varlist_insert_sorted (cfg, g_ptr_array_index (varlist_array, tidx), vmv, FALSE);
403                 }
404         }
405
406         g_hash_table_destroy (type_hash);
407
408         return varlist_array;
409 }
410 #endif
411
412 static void
413 mono_ssa_replace_copies (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, char *is_live)
414 {
415         int arity;
416
417         if (!inst)
418                 return;
419
420         arity = mono_burg_arity [inst->opcode];
421
422         if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN || inst->ssa_op == MONO_SSA_STORE) && 
423             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
424                 MonoInst *new_var;
425                 int idx = inst->inst_i0->inst_c0;
426                 MonoMethodVar *mv = MONO_VARINFO (cfg, idx);
427
428                 if (mv->reg != -1 && mv->reg != mv->idx) {
429                        
430                         is_live [mv->reg] = 1;
431
432                         new_var = cfg->varinfo [mv->reg];
433
434 #if 0
435                         printf ("REPLACE COPY BB%d %d %d\n", bb->block_num, idx, new_var->inst_c0);
436                         g_assert (cfg->varinfo [mv->reg]->inst_vtype == cfg->varinfo [idx]->inst_vtype);
437 #endif
438                         inst->inst_i0 = new_var;
439                 } else {
440                         is_live [mv->idx] = 1;
441                 }
442         }
443
444
445         if (arity) {
446                 mono_ssa_replace_copies (cfg, bb, inst->inst_left, is_live);
447                 if (arity > 1)
448                         mono_ssa_replace_copies (cfg, bb, inst->inst_right, is_live);
449         }
450
451         if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->ssa_op == MONO_SSA_LOAD &&
452             inst->inst_i0->inst_c0 == inst->inst_i1->inst_i0->inst_c0) {
453                 inst->ssa_op = MONO_SSA_NOP;
454                 inst->opcode = OP_NOP;
455         }
456
457 }
458
459 void
460 mono_ssa_remove (MonoCompile *cfg)
461 {
462         MonoInst *inst, *phi;
463         char *is_live;
464         int i, j;
465 #ifndef USE_ORIGINAL_VARS
466         GPtrArray *varlist_array;
467         GList *active;
468 #endif
469         g_assert (cfg->comp_done & MONO_COMP_SSA);
470
471         for (i = 0; i < cfg->num_bblocks; ++i) {
472                 MonoBasicBlock *bb = cfg->bblocks [i];
473                 MONO_BB_FOR_EACH_INS (bb, inst) {
474                         if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->opcode == OP_PHI) {
475                                 
476                                 phi = inst->inst_i1;
477                                 g_assert (phi->inst_phi_args [0] == bb->in_count);
478
479                                 for (j = 0; j < bb->in_count; j++) {
480                                         MonoBasicBlock *pred = bb->in_bb [j];
481                                         int idx = phi->inst_phi_args [j + 1];
482                                         MonoMethodVar *mv = MONO_VARINFO (cfg, idx);
483
484                                         if (mv->reg != -1 && mv->reg != mv->idx) {
485                                                 //printf ("PHICOPY %d %d -> %d\n", idx, mv->reg, inst->inst_i0->inst_c0);
486                                                 idx = mv->reg;
487                                         }
488
489                                         
490                                         if (idx != inst->inst_i0->inst_c0) {
491 #ifdef DEBUG_SSA
492                                                 printf ("MOVE %d to %d in BB%d\n", idx, inst->inst_i0->inst_c0, pred->block_num);
493 #endif
494                                                 mono_add_varcopy_to_end (cfg, pred, idx, inst->inst_i0->inst_c0);
495                                         }
496                                 }
497
498                                 /* remove the phi functions */
499                                 inst->opcode = OP_NOP;
500                                 inst->ssa_op = MONO_SSA_NOP;
501                         } 
502                 }
503         }
504         
505 #ifndef USE_ORIGINAL_VARS
506         /* we compute liveness again */
507         cfg->comp_done &= ~MONO_COMP_LIVENESS;
508         mono_analyze_liveness (cfg);
509
510         varlist_array = mono_ssa_get_allocatable_vars (cfg);
511
512         for (i = 0; i < varlist_array->len; i++) {
513                 GList *l, *regs, *vars = g_ptr_array_index (varlist_array, i);
514                 MonoMethodVar *vmv, *amv;
515                 
516                 if (g_list_length (vars) <= 1) {
517                         continue;
518                 }
519
520                 active = NULL;
521                 regs = NULL;
522
523                 for (l = vars; l; l = l->next) {
524                         vmv = l->data;
525
526                         /* expire old intervals in active */
527                         while (active) {
528                                 amv = (MonoMethodVar *)active->data;
529
530                                 if (amv->range.last_use.abs_pos >= vmv->range.first_use.abs_pos)
531                                         break;
532
533                                 active = g_list_delete_link (active, active);
534                                 regs = g_list_prepend (regs, (gpointer)amv->reg);
535                         }
536
537                         if (!regs)
538                                 regs = g_list_prepend (regs, (gpointer)vmv->idx);
539
540                         vmv->reg = (int)regs->data;
541                         regs = g_list_delete_link (regs, regs);
542                         active = mono_varlist_insert_sorted (cfg, active, vmv, TRUE);           
543                 }
544
545                 g_list_free (active);
546                 g_list_free (regs);
547                 g_list_free (vars);
548         }
549
550         g_ptr_array_free (varlist_array, TRUE);
551
552 #endif
553
554         is_live = alloca (cfg->num_varinfo);
555         memset (is_live, 0, cfg->num_varinfo);
556
557         for (i = 0; i < cfg->num_bblocks; ++i) {
558                 MonoBasicBlock *bb = cfg->bblocks [i];
559
560                 MONO_BB_FOR_EACH_INS (bb, inst)
561                         mono_ssa_replace_copies (cfg, bb, inst, is_live);
562         }
563
564         for (i = 0; i < cfg->num_varinfo; ++i) {
565                 MONO_VARINFO (cfg, i)->reg = -1;
566                 if (!is_live [i] && !(cfg->varinfo [i]->flags & MONO_INST_VOLATILE)) {
567                         cfg->varinfo [i]->flags |= MONO_INST_IS_DEAD;
568                 }
569         }
570
571         if (cfg->comp_done & MONO_COMP_REACHABILITY)
572                 unlink_unused_bblocks (cfg);
573
574         cfg->comp_done &= ~MONO_COMP_SSA;
575 }
576
577
578 #define IS_CALL(op) (op == CEE_CALLI || op == OP_CALL || op == OP_CALLVIRT || (op >= OP_VOIDCALL && op <= OP_CALL_MEMBASE))
579
580 typedef struct {
581         MonoBasicBlock *bb;
582         MonoInst *inst;
583 } MonoVarUsageInfo;
584
585
586
587
588 /*
589  * Returns TRUE if the tree can have side effects.
590  */
591 static gboolean
592 analyze_dev_use (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *root, MonoInst *inst)
593 {
594         MonoMethodVar *info;
595         int i, idx, arity;
596         gboolean has_side_effects;
597
598         if (!inst)
599                 return FALSE;
600
601         arity = mono_burg_arity [inst->opcode];
602         switch (inst->opcode) {
603 #define ANALYZE_DEV_USE_SPECIFIC_OPS 1
604 #define OPDEF(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) case a1:
605 #include "simple-cee-ops.h"
606 #undef OPDEF
607 #define MINI_OP(a1,a2) case a1:
608 #include "simple-mini-ops.h"
609 #undef MINI_OP
610 #undef ANALYZE_DEV_USE_SPECIFIC_OPS
611                 has_side_effects = FALSE;
612                 break;
613         default:
614                 has_side_effects = TRUE;
615         }
616
617         if ((inst->ssa_op == MONO_SSA_STORE) && 
618             (inst->inst_i0->opcode == OP_LOCAL /*|| inst->inst_i0->opcode == OP_ARG */)) {
619                 idx = inst->inst_i0->inst_c0;
620                 info = MONO_VARINFO (cfg, idx);
621                 //printf ("%d defined in BB%d %p\n", idx, bb->block_num, root);
622                 if (info->def) {
623                         g_warning ("more than one definition of variable %d in %s", idx,
624                                    mono_method_full_name (cfg->method, TRUE));
625                         g_assert_not_reached ();
626                 }
627                 if (!IS_CALL (inst->inst_i1->opcode) /* && inst->inst_i1->opcode == OP_ICONST */) {
628                         g_assert (inst == root);
629                         info->def = root;
630                         info->def_bb = bb;
631                 }
632
633                 if (inst->inst_i1->opcode == OP_PHI) {
634                         for (i = inst->inst_i1->inst_phi_args [0]; i > 0; i--) {
635                                 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
636                                 idx = inst->inst_i1->inst_phi_args [i]; 
637                                 info = MONO_VARINFO (cfg, idx);
638                                 //printf ("FOUND %d\n", idx);
639                                 ui->bb = bb;
640                                 ui->inst = root;
641                                 info->uses = g_list_prepend_mempool (info->uses, cfg->mempool, ui);
642                         }
643                 }
644         }
645
646         if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) && 
647             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
648                 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
649                 idx = inst->inst_i0->inst_c0;   
650                 info = MONO_VARINFO (cfg, idx);
651                 //printf ("FOUND %d\n", idx);
652                 ui->bb = bb;
653                 ui->inst = root;
654                 info->uses = g_list_prepend_mempool (info->uses, cfg->mempool, ui);
655         } else {
656                 if (arity) {
657                         //if (inst->ssa_op != MONO_SSA_STORE)
658                         if (analyze_dev_use (cfg, bb, root, inst->inst_left))
659                                 has_side_effects = TRUE;
660                         if (arity > 1)
661                                 if (analyze_dev_use (cfg, bb, root, inst->inst_right))
662                                         has_side_effects = TRUE;
663                 }
664         }
665         
666         return has_side_effects;
667 }
668
669
670 /* avoid unnecessary copies of variables:
671  * Y <= X; Z = Y; is translated to Z = X;
672  */
673 static void
674 mono_ssa_avoid_copies (MonoCompile *cfg)
675 {
676         MonoInst *inst, *next;
677         MonoBasicBlock *bb;
678         MonoMethodVar *i1, *i2;
679
680         g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
681
682         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
683                 MONO_BB_FOR_EACH_INS (bb, inst) {
684                         if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i0->opcode == OP_LOCAL &&
685                             !IS_CALL (inst->inst_i1->opcode) && inst->inst_i1->opcode != OP_PHI && !inst->flags) {
686                                 i1 = MONO_VARINFO (cfg, inst->inst_i0->inst_c0);
687
688 /* fixme: compiling mcs does not work when I enable this */
689 #if 0
690                                 if (g_list_length (i1->uses) == 1 && !extends_live (inst->inst_i1)) {
691                                         MonoVarUsageInfo *vi = (MonoVarUsageInfo *)i1->uses->data;
692                                         u = vi->inst;
693
694                                         //printf ("VAR %d %s\n", i1->idx, mono_method_full_name (cfg->method, TRUE));
695                                         //mono_print_tree (inst); printf ("\n");
696                                         //mono_print_tree (u); printf ("\n");
697
698                                         if (replace_usage_new (cfg, u, inst->inst_i0->inst_c0,  inst->inst_i1)) {
699                                                                                                                 
700                                                 //mono_print_tree (u); printf ("\n");
701                                                         
702                                                 inst->opcode = OP_NOP;
703                                                 inst->ssa_op = MONO_SSA_NOP;
704                                         }
705                                 }
706 #endif                  
707                                 next = mono_inst_list_next (&inst->node, &bb->ins_list);
708                                 if (next && next->ssa_op == MONO_SSA_STORE &&
709                                                 next->inst_i0->opcode == OP_LOCAL &&
710                                                 next->inst_i1->ssa_op == MONO_SSA_LOAD &&
711                                                 next->inst_i1->inst_i0->opcode == OP_LOCAL &&
712                                                 next->inst_i1->inst_i0->inst_c0 == inst->inst_i0->inst_c0 &&
713                                                 g_list_length (i1->uses) == 1 &&
714                                                 inst->opcode == next->opcode &&
715                                                 inst->inst_i0->type == next->inst_i0->type) {
716                                         i2 = MONO_VARINFO (cfg, next->inst_i0->inst_c0);
717                                         //printf ("ELIM. COPY in BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
718                                         inst->inst_i0 = next->inst_i0;
719                                         i2->def = inst;
720                                         i1->def = NULL;
721                                         i1->uses = NULL;
722                                         next->opcode = OP_NOP;
723                                         next->ssa_op = MONO_SSA_NOP;
724                                 }
725                         }
726                 }
727         }
728 }
729
730 static void
731 mono_ssa_create_def_use (MonoCompile *cfg) 
732 {
733         MonoBasicBlock *bb;
734
735         g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
736
737         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
738                 MonoInst *inst;
739                 MONO_BB_FOR_EACH_INS (bb, inst) {
740                         gboolean has_side_effects = analyze_dev_use (cfg, bb, inst, inst);
741                         if (has_side_effects && (inst->ssa_op == MONO_SSA_STORE) && 
742                                         (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
743                                 inst->inst_i0->flags |= MONO_INST_DEFINITION_HAS_SIDE_EFFECTS;
744                         }
745                 }
746         }
747
748         cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
749 }
750
751 static int
752 simulate_compare (int opcode, int a, int b)
753 {
754         switch (opcode) {
755         case CEE_BEQ:
756                 return a == b;
757         case CEE_BGE:
758                 return a >= b;
759         case CEE_BGT:
760                 return a > b;
761         case CEE_BLE:
762                 return a <= b;
763         case CEE_BLT:
764                 return a < b;
765         case CEE_BNE_UN:
766                 return a != b;
767         case CEE_BGE_UN:
768                 return (unsigned)a >= (unsigned)b;
769         case CEE_BGT_UN:
770                 return (unsigned)a > (unsigned)b;
771         case CEE_BLE_UN:
772                 return (unsigned)a <= (unsigned)b;
773         case CEE_BLT_UN:
774                 return (unsigned)a < (unsigned)b;
775         default:
776                 g_assert_not_reached ();
777         }
778
779         return 0;
780 }
781
782 static int
783 simulate_long_compare (int opcode, gint64 a, gint64 b)
784 {
785         switch (opcode) {
786         case CEE_BEQ:
787                 return a == b;
788         case CEE_BGE:
789                 return a >= b;
790         case CEE_BGT:
791                 return a > b;
792         case CEE_BLE:
793                 return a <= b;
794         case CEE_BLT:
795                 return a < b;
796         case CEE_BNE_UN:
797                 return a != b;
798         case CEE_BGE_UN:
799                 return (guint64)a >= (guint64)b;
800         case CEE_BGT_UN:
801                 return (guint64)a > (guint64)b;
802         case CEE_BLE_UN:
803                 return (guint64)a <= (guint64)b;
804         case CEE_BLT_UN:
805                 return (guint64)a < (guint64)b;
806         default:
807                 g_assert_not_reached ();
808         }
809
810         return 0;
811 }
812
813 #define EVAL_CXX(name,op,cast)  \
814         case name:      \
815                 if ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE)) { \
816                         r1 = evaluate_const_tree (cfg, inst->inst_i0->inst_i0, &a, carray); \
817                         r2 = evaluate_const_tree (cfg, inst->inst_i0->inst_i1, &b, carray); \
818                         if (r1 == 1 && r2 == 1) { \
819                                 *res = ((cast)a op (cast)b); \
820                                 return 1; \
821                         } else { \
822                                 return MAX (r1, r2); \
823                         } \
824                 } \
825                 break;
826
827 #define EVAL_BINOP(name,op)     \
828         case name:      \
829                 r1 = evaluate_const_tree (cfg, inst->inst_i0, &a, carray); \
830                 r2 = evaluate_const_tree (cfg, inst->inst_i1, &b, carray); \
831                 if (r1 == 1 && r2 == 1) { \
832                         *res = (a op b); \
833                         return 1; \
834                 } else { \
835                         return MAX (r1, r2); \
836                 } \
837                 break;
838
839
840 /* fixme: this only works for interger constants, but not for other types (long, float) */
841 static int
842 evaluate_const_tree (MonoCompile *cfg, MonoInst *inst, int *res, MonoInst **carray)
843 {
844         MonoInst *c0;
845         int a, b, r1, r2;
846
847         if (!inst)
848                 return 0;
849
850         if (inst->ssa_op == MONO_SSA_LOAD && 
851             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
852             (c0 = carray [inst->inst_i0->inst_c0])) {
853                 *res = c0->inst_c0;
854                 return 1;
855         }
856
857         switch (inst->opcode) {
858         case OP_ICONST:
859                 *res = inst->inst_c0;
860                 return 1;
861
862         EVAL_CXX (OP_CEQ,==,gint32)
863         EVAL_CXX (OP_CGT,>,gint32)
864         EVAL_CXX (OP_CGT_UN,>,guint32)
865         EVAL_CXX (OP_CLT,<,gint32)
866         EVAL_CXX (OP_CLT_UN,<,guint32)
867
868         EVAL_BINOP (CEE_ADD,+)
869         EVAL_BINOP (CEE_SUB,-)
870         EVAL_BINOP (CEE_MUL,*)
871         EVAL_BINOP (CEE_AND,&)
872         EVAL_BINOP (CEE_OR,|)
873         EVAL_BINOP (CEE_XOR,^)
874         EVAL_BINOP (CEE_SHL,<<)
875         EVAL_BINOP (CEE_SHR,>>)
876
877         default:
878                 return 2;
879         }
880
881         return 2;
882 }
883
884 static void
885 fold_tree (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **carray)
886 {
887         MonoInst *c0;
888         int arity, a, b;
889
890         if (!inst)
891                 return;
892
893         arity = mono_burg_arity [inst->opcode];
894
895         if (inst->ssa_op == MONO_SSA_STORE && 
896             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
897             inst->inst_i1->opcode == OP_PHI && (c0 = carray [inst->inst_i0->inst_c0])) {
898                 //{static int cn = 0; printf ("PHICONST %d %d %s\n", cn++, c0->inst_c0, mono_method_full_name (cfg->method, TRUE));}
899                 *inst->inst_i1 = *c0;           
900         } else if (inst->ssa_op == MONO_SSA_LOAD && 
901             (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
902             (c0 = carray [inst->inst_i0->inst_c0])) {
903                 //{static int cn = 0; printf ("YCCOPY %d %d %s\n", cn++, c0->inst_c0, mono_method_full_name (cfg->method, TRUE));}
904                 *inst = *c0;
905         } else {
906
907                 if (arity) {
908                         fold_tree (cfg, bb, inst->inst_left, carray);
909                         if (arity > 1)
910                                 fold_tree (cfg, bb, inst->inst_right, carray);
911                         mono_constant_fold_inst (inst, NULL); 
912                 }
913         }
914
915         if ((inst->opcode >= CEE_BEQ && inst->opcode <= CEE_BLT_UN) &&
916             ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE))) {
917                 MonoInst *v0 = inst->inst_i0->inst_i0;
918                 MonoInst *v1 = inst->inst_i0->inst_i1;
919                 MonoBasicBlock *target = NULL;
920
921                 /* hack for longs to optimize the simply cases */
922                 if (v0->opcode == OP_I8CONST && v1->opcode == OP_I8CONST) {
923                         if (simulate_long_compare (inst->opcode, v0->inst_l, v1->inst_l)) {
924                                 //unlink_target (bb, inst->inst_false_bb);
925                                 target = inst->inst_true_bb;
926                         } else {
927                                 //unlink_target (bb, inst->inst_true_bb);
928                                 target = inst->inst_false_bb;
929                         }                       
930                 } else if (evaluate_const_tree (cfg, v0, &a, carray) == 1 &&
931                            evaluate_const_tree (cfg, v1, &b, carray) == 1) {                            
932                         if (simulate_compare (inst->opcode, a, b)) {
933                                 //unlink_target (bb, inst->inst_false_bb);
934                                 target = inst->inst_true_bb;
935                         } else {
936                                 //unlink_target (bb, inst->inst_true_bb);
937                                 target = inst->inst_false_bb;
938                         }
939                 }
940
941                 if (target) {
942                         bb->out_bb [0] = target;
943                         bb->out_count = 1;
944                         inst->opcode = OP_BR;
945                         inst->inst_target_bb = target;
946                 }
947         } else if (inst->opcode == OP_SWITCH && (evaluate_const_tree (cfg, inst->inst_left, &a, carray) == 1) && (a >= 0) && (a < GPOINTER_TO_INT (inst->klass))) {
948                 bb->out_bb [0] = inst->inst_many_bb [a];
949                 bb->out_count = 1;
950                 inst->inst_target_bb = bb->out_bb [0];
951                 inst->opcode = OP_BR;
952         }
953
954 }
955
956 static void
957 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
958 {
959         if (info->cpstate >= state)
960                 return;
961
962         info->cpstate = state;
963
964         //printf ("SETSTATE %d to %d\n", info->idx, info->cpstate);
965
966         if (state == 1)
967                 carray [info->idx] = c0;
968         else
969                 carray [info->idx] = NULL;
970
971         if (!g_list_find (*cvars, info)) {
972                 *cvars = g_list_prepend (*cvars, info);
973         }
974 }
975
976 static void
977 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, GList **cvars, GList **bblist, MonoInst **carray)
978 {
979         g_assert (inst);
980
981         if (inst->opcode == OP_SWITCH) {
982                 int r1, i, a;
983                 int cases = GPOINTER_TO_INT (inst->klass);
984
985                 r1 = evaluate_const_tree (cfg, inst->inst_left, &a, carray);
986                 if ((r1 == 1) && ((a < 0) || (a >= cases)))
987                         r1 = 2;
988                 if (r1 == 1) {
989                         MonoBasicBlock *tb = inst->inst_many_bb [a];
990                         if (!(tb->flags &  BB_REACHABLE)) {
991                                 tb->flags |= BB_REACHABLE;
992                                 *bblist = g_list_prepend (*bblist, tb);
993                         }
994                 } else if (r1 == 2) {
995                         for (i = GPOINTER_TO_INT (inst->klass); i >= 0; i--) {
996                                 MonoBasicBlock *tb = inst->inst_many_bb [i];
997                                 if (!(tb->flags &  BB_REACHABLE)) {
998                                         tb->flags |= BB_REACHABLE;
999                                         *bblist = g_list_prepend (*bblist, tb);
1000                                 }
1001                         }
1002                 }
1003         } else if (inst->opcode == OP_BR) {
1004                 MonoBasicBlock *target = inst->inst_target_bb;
1005
1006                 if (!(target->flags &  BB_REACHABLE)) {
1007                         target->flags |= BB_REACHABLE;
1008                         *bblist = g_list_prepend (*bblist, target);
1009                 }
1010         } else if ((inst->opcode >= CEE_BEQ && inst->opcode <= CEE_BLT_UN) &&
1011             ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE))) {
1012                 int a, b, r1, r2;
1013                 MonoInst *v0 = inst->inst_i0->inst_i0;
1014                 MonoInst *v1 = inst->inst_i0->inst_i1;
1015
1016                 r1 = evaluate_const_tree (cfg, v0, &a, carray);
1017                 r2 = evaluate_const_tree (cfg, v1, &b, carray);
1018
1019                 if (r1 == 1 && r2 == 1) {
1020                         MonoBasicBlock *target;
1021                                 
1022                         if (simulate_compare (inst->opcode, a, b)) {
1023                                 target = inst->inst_true_bb;
1024                         } else {
1025                                 target = inst->inst_false_bb;
1026                         }
1027                         if (!(target->flags &  BB_REACHABLE)) {
1028                                 target->flags |= BB_REACHABLE;
1029                                 *bblist = g_list_prepend (*bblist, target);
1030                         }
1031                 } else if (r1 == 2 || r2 == 2) {
1032                         if (!(inst->inst_true_bb->flags &  BB_REACHABLE)) {
1033                                 inst->inst_true_bb->flags |= BB_REACHABLE;
1034                                 *bblist = g_list_prepend (*bblist, inst->inst_true_bb);
1035                         }
1036                         if (!(inst->inst_false_bb->flags &  BB_REACHABLE)) {
1037                                 inst->inst_false_bb->flags |= BB_REACHABLE;
1038                                 *bblist = g_list_prepend (*bblist, inst->inst_false_bb);
1039                         }
1040                 }       
1041         } else if (inst->ssa_op == MONO_SSA_STORE && 
1042                    (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
1043                 MonoMethodVar *info = MONO_VARINFO (cfg, inst->inst_i0->inst_c0);
1044                 MonoInst *i1 = inst->inst_i1;
1045                 int res;
1046                 
1047                 if (info->cpstate < 2) {
1048                         if (i1->opcode == OP_ICONST) { 
1049                                 change_varstate (cfg, cvars, info, 1, i1, carray);
1050                         } else if (i1->opcode == OP_PHI) {
1051                                 MonoInst *c0 = NULL;
1052                                 int j;
1053
1054                                 for (j = 1; j <= i1->inst_phi_args [0]; j++) {
1055                                         MonoMethodVar *mv = MONO_VARINFO (cfg, i1->inst_phi_args [j]);
1056                                         MonoInst *src = mv->def;
1057
1058                                         if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE)) {
1059                                                 continue;
1060                                         }
1061
1062                                         if (!mv->def || !src || src->ssa_op != MONO_SSA_STORE ||
1063                                             !(src->inst_i0->opcode == OP_LOCAL || src->inst_i0->opcode == OP_ARG) ||
1064                                             mv->cpstate == 2) {
1065                                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
1066                                                 break;
1067                                         }
1068                                         
1069                                         if (mv->cpstate == 0)
1070                                                 continue;
1071
1072                                         //g_assert (src->inst_i1->opcode == OP_ICONST);
1073                                         g_assert (carray [mv->idx]);
1074
1075                                         if (!c0) {
1076                                                 c0 = carray [mv->idx];
1077                                         }
1078                                         
1079                                         if (carray [mv->idx]->inst_c0 != c0->inst_c0) {
1080                                                 change_varstate (cfg, cvars, info, 2, NULL, carray);
1081                                                 break;
1082                                         }
1083                                 }
1084                                 
1085                                 if (c0 && info->cpstate < 1) {
1086                                         change_varstate (cfg, cvars, info, 1, c0, carray);
1087                                 }
1088                         } else {
1089                                 int state = evaluate_const_tree (cfg, i1, &res, carray);
1090                                 if (state == 1) {
1091                                         NEW_ICONST (cfg, i1, res);
1092                                         change_varstate (cfg, cvars, info, 1, i1, carray);
1093                                 } else {
1094                                         change_varstate (cfg, cvars, info, 2, NULL, carray);
1095                                 }
1096                         }
1097                 }
1098         }
1099 }
1100
1101 void
1102 mono_ssa_cprop (MonoCompile *cfg) 
1103 {
1104         MonoInst **carray;
1105         MonoBasicBlock *bb;
1106         GList *bblock_list, *cvars;
1107         GList *tmp;
1108         int i;
1109         //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1110
1111         carray = g_new0 (MonoInst*, cfg->num_varinfo);
1112
1113         if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1114                 mono_ssa_create_def_use (cfg);
1115
1116         bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1117         cfg->bb_entry->flags |= BB_REACHABLE;
1118
1119         memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1120
1121         for (i = 0; i < cfg->num_varinfo; i++) {
1122                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1123                 if (!info->def)
1124                         info->cpstate = 2;
1125         }
1126
1127         cvars = NULL;
1128
1129         while (bblock_list) {
1130                 MonoInst *inst;
1131
1132                 bb = (MonoBasicBlock *)bblock_list->data;
1133
1134                 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1135
1136                 g_assert (bb->flags &  BB_REACHABLE);
1137
1138                 if (bb->out_count == 1) {
1139                         if (!(bb->out_bb [0]->flags &  BB_REACHABLE)) {
1140                                 bb->out_bb [0]->flags |= BB_REACHABLE;
1141                                 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1142                         }
1143                 }
1144
1145                 MONO_BB_FOR_EACH_INS (bb, inst)
1146                         visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1147
1148                 while (cvars) {
1149                         MonoMethodVar *info = (MonoMethodVar *)cvars->data;                     
1150                         cvars = g_list_delete_link (cvars, cvars);
1151
1152                         for (tmp = info->uses; tmp; tmp = tmp->next) {
1153                                 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1154                                 if (!(ui->bb->flags & BB_REACHABLE))
1155                                         continue;
1156                                 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1157                         }
1158                 }
1159         }
1160
1161         for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1162                 MonoInst *inst;
1163                 MONO_BB_FOR_EACH_INS (bb, inst)
1164                         fold_tree (cfg, bb, inst, carray);
1165         }
1166
1167         g_free (carray);
1168
1169         cfg->comp_done |= MONO_COMP_REACHABILITY;
1170 }
1171
1172 static void
1173 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1174 {
1175         GList *tmp;
1176
1177         *wl = g_list_prepend (*wl, use);
1178
1179         for (tmp = use->uses; tmp; tmp = tmp->next) {
1180                 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1181                 if (ui->inst == var->def) {
1182                         /* from the mempool */
1183                         use->uses = g_list_remove_link (use->uses, tmp);
1184                         break;
1185                 }
1186         }       
1187 }
1188
1189 void
1190 mono_ssa_deadce (MonoCompile *cfg) 
1191 {
1192         int i;
1193         GList *work_list;
1194
1195         g_assert (cfg->comp_done & MONO_COMP_SSA);
1196
1197         //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1198
1199         /* fixme: we should update usage infos during cprop, instead of computing it again */
1200         cfg->comp_done &=  ~MONO_COMP_SSA_DEF_USE;
1201         for (i = 0; i < cfg->num_varinfo; i++) {
1202                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1203                 info->def = NULL;
1204                 info->uses = NULL;
1205         }
1206
1207         if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1208                 mono_ssa_create_def_use (cfg);
1209
1210         mono_ssa_avoid_copies (cfg);
1211
1212         work_list = NULL;
1213         for (i = 0; i < cfg->num_varinfo; i++) {
1214                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1215                 work_list = g_list_prepend (work_list, info);
1216                 
1217                 //if ((info->def != NULL) && (info->def->inst_i1->opcode != OP_PHI)) printf ("SSA DEADCE TOTAL LOCAL\n");
1218         }
1219
1220         while (work_list) {
1221                 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1222                 work_list = g_list_delete_link (work_list, work_list);
1223
1224                 if (!info->uses && info->def && (!(cfg->varinfo [info->idx]->flags & (MONO_INST_DEFINITION_HAS_SIDE_EFFECTS|MONO_INST_VOLATILE|MONO_INST_INDIRECT)))) {
1225                         MonoInst *i1;
1226                         //printf ("ELIMINATE %s: ", mono_method_full_name (cfg->method, TRUE)); mono_print_tree (info->def); printf ("\n");
1227
1228                         i1 = info->def->inst_i1;
1229                         if (i1->opcode == OP_PHI) {
1230                                 int j;
1231                                 for (j = i1->inst_phi_args [0]; j > 0; j--) {
1232                                         MonoMethodVar *u = MONO_VARINFO (cfg, i1->inst_phi_args [j]);
1233                                         add_to_dce_worklist (cfg, info, u, &work_list);
1234                                 }
1235                         } else if (i1->ssa_op == MONO_SSA_LOAD &&
1236                                    (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG)) {
1237                                         MonoMethodVar *u = MONO_VARINFO (cfg, i1->inst_i0->inst_c0);
1238                                         add_to_dce_worklist (cfg, info, u, &work_list);
1239                         }
1240                         //if (i1->opcode != OP_PHI) printf ("SSA DEADCE DEAD LOCAL\n");
1241
1242                         info->def->opcode = OP_NOP;
1243                         info->def->ssa_op = MONO_SSA_NOP;
1244                 }
1245
1246         }
1247 }
1248
1249 #if 0
1250 void
1251 mono_ssa_strength_reduction (MonoCompile *cfg)
1252 {
1253         MonoBasicBlock *bb;
1254         int i;
1255
1256         g_assert (cfg->comp_done & MONO_COMP_SSA);
1257         g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1258         g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1259
1260         for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1261                 GList *lp = bb->loop_blocks;
1262
1263                 if (lp) {
1264                         MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1265
1266                         /* we only consider loops with 2 in bblocks */
1267                         if (!h->in_count == 2)
1268                                 continue;
1269
1270                         for (i = 0; i < cfg->num_varinfo; i++) {
1271                                 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1272                         
1273                                 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1274                                     info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1275                                         MonoInst *v = info->def->inst_i1;
1276
1277
1278                                         printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1279                                 }
1280                         }
1281                 }
1282         }
1283 }
1284 #endif
1285
1286 #endif /* DISABLE_SSA */
1287