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