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