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