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