Copy
[mono-project.git] / mono / mini / ssa.c
blobdf6d69ff167bdbec702ad6404a5444b8a40c3f8a
1 /*
2 * ssa.c: Static single assign form support for the JIT compiler.
4 * Author:
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
8 */
9 #include <config.h>
10 #include <string.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/mempool.h>
13 #include <mono/metadata/mempool-internals.h>
15 #ifndef DISABLE_JIT
17 #include "mini.h"
18 #ifdef HAVE_ALLOCA_H
19 #include <alloca.h>
20 #endif
22 #define USE_ORIGINAL_VARS
23 #define CREATE_PRUNED_SSA
25 //#define DEBUG_SSA 1
27 #define NEW_PHI(cfg,dest,val) do { \
28 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
29 (dest)->opcode = OP_PHI; \
30 (dest)->inst_c0 = (val); \
31 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
32 } while (0)
34 typedef struct {
35 MonoBasicBlock *bb;
36 MonoInst *inst;
37 } MonoVarUsageInfo;
39 static void
40 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
42 int i;
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;
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;
59 static void
60 unlink_unused_bblocks (MonoCompile *cfg)
62 int i, j;
63 MonoBasicBlock *bb;
65 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
67 if (G_UNLIKELY (cfg->verbose_level > 1))
68 printf ("\nUNLINK UNUSED BBLOCKS:\n");
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;
77 for (i = 1; i < cfg->num_bblocks; i++) {
78 bb = cfg->bblocks [i];
80 if (!(bb->flags & BB_REACHABLE)) {
81 for (j = 0; j < bb->in_count; j++) {
82 unlink_target (bb->in_bb [j], bb);
84 for (j = 0; j < bb->out_count; j++) {
85 unlink_target (bb, bb->out_bb [j]);
87 if (G_UNLIKELY (cfg->verbose_level > 1))
88 printf ("\tUnlinked BB%d\n", bb->block_num);
94 /**
95 * remove_bb_from_phis:
97 * Remove BB from the PHI statements in TARGET.
99 static void
100 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
102 MonoInst *ins;
103 int i, j;
105 for (i = 0; i < target->in_count; i++) {
106 if (target->in_bb [i] == bb) {
107 break;
110 g_assert (i < target->in_count);
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] --;
118 else
119 break;
123 static inline int
124 op_phi_to_move (int opcode)
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 ();
139 return -1;
142 static inline void
143 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
145 MonoMethodVar *info;
146 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
148 info = MONO_VARINFO (cfg, var->inst_c0);
150 ui->bb = bb;
151 ui->inst = ins;
152 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
155 typedef struct {
156 MonoInst *var;
157 int idx;
158 } RenameInfo;
161 * mono_ssa_rename_vars:
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.
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)
170 MonoInst *ins, *new_var;
171 int i, j, idx;
172 GSList *tmp;
173 int stack_history_len = 0;
175 if (cfg->verbose_level >= 4)
176 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
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];
184 #ifdef DEBUG_SSA
185 printf ("\tProcessing "); mono_print_ins (ins);
186 #endif
187 if (ins->opcode == OP_NOP)
188 continue;
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);
203 else
204 record_use (cfg, var, bb, ins);
206 else if (G_UNLIKELY (!var && lvreg_stack [sregs [i]]))
207 sregs [i] = lvreg_stack [sregs [i]];
210 mono_inst_set_src_registers (ins, sregs);
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);
222 else
223 record_use (cfg, var, bb, ins);
225 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
226 ins->dreg = lvreg_stack [ins->dreg];
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;
234 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
235 idx = var->inst_c0;
236 g_assert (idx < max_vars);
238 if (var->opcode == OP_ARG)
239 originals_used [idx] = TRUE;
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 ++;
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;
252 if (cfg->verbose_level >= 4)
253 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
255 stack [idx] = new_var;
257 ins->dreg = new_var->dreg;
258 var = new_var;
260 else {
261 stack [idx] = var;
262 originals_used [idx] = TRUE;
265 info = MONO_VARINFO (cfg, var->inst_c0);
266 info->def = ins;
267 info->def_bb = bb;
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] = mono_alloc_preg (cfg);
272 ins->dreg = lvreg_stack [ins->dreg];
274 else
275 lvreg_defined [ins->dreg] = TRUE;
278 #ifdef DEBUG_SSA
279 printf ("\tAfter processing "); mono_print_ins (ins);
280 #endif
284 /* Rename PHI arguments in succeeding bblocks */
285 for (i = 0; i < bb->out_count; i++) {
286 MonoBasicBlock *n = bb->out_bb [i];
288 for (j = 0; j < n->in_count; j++)
289 if (n->in_bb [j] == bb)
290 break;
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);
307 else
308 /* The phi nodes are at the beginning of the bblock */
309 break;
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);
319 /* Restore stack */
320 for (i = stack_history_len - 1; i >= 0; i--) {
321 stack [stack_history [i].idx] = stack_history [i].var;
324 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
327 void
328 mono_ssa_compute (MonoCompile *cfg)
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;
341 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
343 /* we dont support methods containing exception clauses */
344 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
345 g_assert (!cfg->disable_ssa);
347 if (cfg->verbose_level >= 4)
348 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
350 #ifdef CREATE_PRUNED_SSA
351 /* we need liveness for pruned SSA */
352 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
353 mono_analyze_liveness (cfg);
354 #endif
356 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
358 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
359 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
361 for (i = 0; i < cfg->num_varinfo; ++i) {
362 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
363 buf += bitsize;
364 vinfo [i].idx = i;
365 /* implicit reference at start */
366 if (cfg->varinfo [i]->opcode == OP_ARG)
367 mono_bitset_set_fast (vinfo [i].def_in, 0);
370 for (i = 0; i < cfg->num_bblocks; ++i) {
371 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
372 if (ins->opcode == OP_NOP)
373 continue;
375 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
376 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
381 /* insert phi functions */
382 for (i = 0; i < cfg->num_varinfo; ++i) {
383 MonoInst *var = cfg->varinfo [i];
385 #if SIZEOF_REGISTER == 4
386 if (var->type == STACK_I8 && !COMPILE_LLVM (cfg))
387 continue;
388 #endif
389 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
390 continue;
392 /* Most variables have only one definition */
393 if (mono_bitset_count (vinfo [i].def_in) <= 1)
394 continue;
396 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
398 if (cfg->verbose_level >= 4) {
399 if (mono_bitset_count (set) > 0) {
400 printf ("\tR%d needs PHI functions in ", var->dreg);
401 mono_blockset_print (cfg, set, "", -1);
405 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
406 MonoBasicBlock *bb = cfg->bblocks [idx];
408 /* fixme: create pruned SSA? we would need liveness information for that */
410 if (bb == cfg->bb_exit && !COMPILE_LLVM (cfg))
411 continue;
413 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
414 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
415 continue;
418 NEW_PHI (cfg, ins, i);
420 switch (var->type) {
421 case STACK_I4:
422 case STACK_I8:
423 case STACK_PTR:
424 case STACK_MP:
425 case STACK_OBJ:
426 ins->opcode = OP_PHI;
427 break;
428 case STACK_R8:
429 ins->opcode = OP_FPHI;
430 break;
431 case STACK_VTYPE:
432 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
433 break;
436 if (var->inst_vtype->byref)
437 ins->klass = mono_defaults.int_class;
438 else
439 ins->klass = var->klass;
441 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
442 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
444 /* For debugging */
445 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
446 ins->inst_phi_args [j + 1] = -1;
448 ins->dreg = cfg->varinfo [i]->dreg;
450 mono_bblock_insert_before_ins (bb, bb->code, ins);
452 #ifdef DEBUG_SSA
453 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
454 #endif
458 /* free the stuff */
459 g_free (vinfo);
460 g_free (buf_start);
462 /* Renaming phase */
464 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
465 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
467 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
468 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
469 stack_history_size = 10240;
470 stack_history = g_new (RenameInfo, stack_history_size);
471 originals = g_new0 (gboolean, cfg->num_varinfo);
472 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
473 g_free (stack_history);
474 g_free (originals);
475 g_free (lvreg_stack);
476 g_free (lvreg_defined);
478 if (cfg->verbose_level >= 4)
479 printf ("\nEND COMPUTE SSA.\n\n");
481 cfg->comp_done |= MONO_COMP_SSA;
484 void
485 mono_ssa_remove (MonoCompile *cfg)
487 MonoInst *ins, *var, *move;
488 int i, j, first;
490 g_assert (cfg->comp_done & MONO_COMP_SSA);
492 for (i = 0; i < cfg->num_bblocks; ++i) {
493 MonoBasicBlock *bb = cfg->bblocks [i];
495 if (cfg->verbose_level >= 4)
496 printf ("\nREMOVE SSA %d:\n", bb->block_num);
498 for (ins = bb->code; ins; ins = ins->next) {
499 if (MONO_IS_PHI (ins)) {
500 g_assert (ins->inst_phi_args [0] == bb->in_count);
501 var = get_vreg_to_inst (cfg, ins->dreg);
503 /* Check for PHI nodes where all the inputs are the same */
504 first = ins->inst_phi_args [1];
506 for (j = 1; j < bb->in_count; ++j)
507 if (first != ins->inst_phi_args [j + 1])
508 break;
510 if ((bb->in_count > 1) && (j == bb->in_count)) {
511 ins->opcode = op_phi_to_move (ins->opcode);
512 if (ins->opcode == OP_VMOVE)
513 g_assert (ins->klass);
514 ins->sreg1 = first;
515 } else {
516 for (j = 0; j < bb->in_count; j++) {
517 MonoBasicBlock *pred = bb->in_bb [j];
518 int sreg = ins->inst_phi_args [j + 1];
520 if (cfg->verbose_level >= 4)
521 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
522 if (var->dreg != sreg) {
523 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
524 if (move->opcode == OP_VMOVE) {
525 g_assert (ins->klass);
526 move->klass = ins->klass;
528 move->dreg = var->dreg;
529 move->sreg1 = sreg;
530 mono_add_ins_to_end (pred, move);
534 ins->opcode = OP_NOP;
535 ins->dreg = -1;
541 if (cfg->verbose_level >= 4) {
542 for (i = 0; i < cfg->num_bblocks; ++i) {
543 MonoBasicBlock *bb = cfg->bblocks [i];
545 mono_print_bb (bb, "AFTER REMOVE SSA:");
550 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
551 * the variables if possible. Since the newly introduced SSA variables don't
552 * have overlapping live ranges (because we don't do agressive optimization), we
553 * can coalesce them into the original variable.
556 for (i = 0; i < cfg->num_bblocks; ++i) {
557 MonoBasicBlock *bb = cfg->bblocks [i];
559 for (ins = bb->code; ins; ins = ins->next) {
560 const char *spec = INS_INFO (ins->opcode);
561 int num_sregs, j;
562 int sregs [MONO_MAX_SRC_REGS];
564 if (ins->opcode == OP_NOP)
565 continue;
567 if (spec [MONO_INST_DEST] != ' ') {
568 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
570 if (var) {
571 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
574 * The third condition avoids coalescing with variables eliminated
575 * during deadce.
577 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
578 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
579 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
584 num_sregs = mono_inst_get_src_registers (ins, sregs);
585 for (j = 0; j < num_sregs; ++j) {
586 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
588 if (var) {
589 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
591 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
592 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
593 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
597 mono_inst_set_src_registers (ins, sregs);
601 for (i = 0; i < cfg->num_varinfo; ++i) {
602 MONO_VARINFO (cfg, i)->reg = -1;
605 if (cfg->comp_done & MONO_COMP_REACHABILITY)
606 unlink_unused_bblocks (cfg);
608 cfg->comp_done &= ~MONO_COMP_LIVENESS;
610 cfg->comp_done &= ~MONO_COMP_SSA;
613 static void
614 mono_ssa_create_def_use (MonoCompile *cfg)
616 MonoBasicBlock *bb;
617 MonoInst *ins;
618 int i;
620 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
622 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
623 for (ins = bb->code; ins; ins = ins->next) {
624 const char *spec = INS_INFO (ins->opcode);
625 MonoMethodVar *info;
626 int num_sregs;
627 int sregs [MONO_MAX_SRC_REGS];
629 if (ins->opcode == OP_NOP)
630 continue;
632 /* SREGs */
633 num_sregs = mono_inst_get_src_registers (ins, sregs);
634 for (i = 0; i < num_sregs; ++i) {
635 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
636 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
637 record_use (cfg, var, bb, ins);
640 if (MONO_IS_STORE_MEMBASE (ins)) {
641 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
642 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
643 record_use (cfg, var, bb, ins);
646 if (MONO_IS_PHI (ins)) {
647 for (i = ins->inst_phi_args [0]; i > 0; i--) {
648 g_assert (ins->inst_phi_args [i] != -1);
649 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
653 /* DREG */
654 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
655 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
657 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
658 info = MONO_VARINFO (cfg, var->inst_c0);
659 info->def = ins;
660 info->def_bb = bb;
666 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
669 static void
670 mono_ssa_copyprop (MonoCompile *cfg)
672 int i, index;
673 GList *l;
675 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
677 for (index = 0; index < cfg->num_varinfo; ++index) {
678 MonoInst *var = cfg->varinfo [index];
679 MonoMethodVar *info = MONO_VARINFO (cfg, index);
681 if (info->def && (MONO_IS_MOVE (info->def))) {
682 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
684 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))) {
685 /* Rewrite all uses of var to be uses of var2 */
686 int dreg = var->dreg;
687 int sreg1 = var2->dreg;
688 const char *spec;
690 l = info->uses;
691 while (l) {
692 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
693 MonoInst *ins = u->inst;
694 GList *next = l->next;
695 int num_sregs;
696 int sregs [MONO_MAX_SRC_REGS];
698 spec = INS_INFO (ins->opcode);
700 num_sregs = mono_inst_get_src_registers (ins, sregs);
701 for (i = 0; i < num_sregs; ++i) {
702 if (sregs [i] == dreg)
703 break;
705 if (i < num_sregs) {
706 g_assert (sregs [i] == dreg);
707 sregs [i] = sreg1;
708 mono_inst_set_src_registers (ins, sregs);
709 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
710 ins->dreg = sreg1;
711 } else if (MONO_IS_PHI (ins)) {
712 for (i = ins->inst_phi_args [0]; i > 0; i--) {
713 int sreg = ins->inst_phi_args [i];
714 if (sreg == var->dreg)
715 break;
717 g_assert (i > 0);
718 ins->inst_phi_args [i] = sreg1;
720 else
721 g_assert_not_reached ();
723 record_use (cfg, var2, u->bb, ins);
725 l = next;
728 info->uses = NULL;
733 if (cfg->verbose_level >= 4) {
734 MonoBasicBlock *bb;
736 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
737 mono_print_bb (bb, "AFTER SSA COPYPROP");
741 static int
742 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
744 MonoInst *args [MONO_MAX_SRC_REGS];
745 int rs [MONO_MAX_SRC_REGS];
746 MonoInst *c0;
747 gboolean const_args = TRUE;
748 const char *spec = INS_INFO (ins->opcode);
749 int num_sregs, i;
750 int sregs [MONO_MAX_SRC_REGS];
752 /* Short-circuit this */
753 if (ins->opcode == OP_ICONST) {
754 *res = ins;
755 return 1;
758 if (ins->opcode == OP_NOP)
759 return 2;
761 num_sregs = mono_inst_get_src_registers (ins, sregs);
762 for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
763 args [i] = NULL;
764 for (i = 0; i < num_sregs; ++i) {
765 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
767 rs [i] = 2;
768 args [i] = carray [sregs [i]];
769 if (args [i])
770 rs [i] = 1;
771 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
772 rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
773 if (rs [i] != 1)
774 const_args = FALSE;
777 c0 = NULL;
779 if (num_sregs > 0 && const_args) {
780 g_assert (num_sregs <= 2);
781 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
782 // Cached value
783 *res = carray [ins->dreg];
784 return 1;
786 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
787 if (c0) {
788 if (G_UNLIKELY (cfg->verbose_level > 1)) {
789 printf ("\t cfold -> ");
790 mono_print_ins (c0);
792 *res = c0;
793 return 1;
795 else
796 /* Can't cfold this ins */
797 return 2;
800 if (num_sregs == 0)
801 return 2;
802 for (i = 0; i < num_sregs; ++i) {
803 if (rs [i] == 2)
804 return 2;
806 return 0;
809 static inline void
810 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
812 if (info->cpstate >= state)
813 return;
815 info->cpstate = state;
817 if (G_UNLIKELY (cfg->verbose_level > 1))
818 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
820 if (state == 1)
821 g_assert (c0);
823 carray [cfg->varinfo [info->idx]->dreg] = c0;
825 if (!g_list_find (*cvars, info)) {
826 *cvars = g_list_prepend (*cvars, info);
830 static inline void
831 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
833 if (G_UNLIKELY (cfg->verbose_level > 1))
834 printf ("\tAdd BB%d to worklist\n", bb->block_num);
836 if (!(bb->flags & BB_REACHABLE)) {
837 bb->flags |= BB_REACHABLE;
838 *bblist = g_list_prepend (*bblist, bb);
842 static void
843 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
845 const char *spec = INS_INFO (ins->opcode);
847 if (ins->opcode == OP_NOP)
848 return;
850 if (cfg->verbose_level > 1)
851 mono_print_ins (ins);
853 /* FIXME: Support longs/floats */
854 /* FIXME: Work on vregs as well */
856 if (MONO_IS_PHI (ins)) {
857 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
858 MonoInst *c0 = NULL;
859 int j;
861 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
862 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
863 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
864 MonoInst *src = mv->def;
866 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
867 continue;
869 if (!mv->def || !src || mv->cpstate == 2) {
870 change_varstate (cfg, cvars, info, 2, NULL, carray);
871 break;
874 if (mv->cpstate == 0)
875 continue;
877 g_assert (carray [var->dreg]);
879 if (!c0)
880 c0 = carray [var->dreg];
882 /* FIXME: */
883 if (c0->opcode != OP_ICONST) {
884 change_varstate (cfg, cvars, info, 2, NULL, carray);
885 break;
888 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
889 change_varstate (cfg, cvars, info, 2, NULL, carray);
890 break;
894 if (c0 && info->cpstate < 1) {
895 change_varstate (cfg, cvars, info, 1, c0, carray);
897 g_assert (c0->opcode == OP_ICONST);
900 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
901 MonoInst *var, *c0;
902 int state;
904 if (spec [MONO_INST_DEST] != ' ')
905 var = get_vreg_to_inst (cfg, ins->dreg);
906 else
907 var = NULL;
909 c0 = NULL;
910 state = evaluate_ins (cfg, ins, &c0, carray);
912 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
913 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
915 if (info->cpstate < 2) {
916 if (state == 1)
917 change_varstate (cfg, cvars, info, 1, c0, carray);
918 else if (state == 2)
919 change_varstate (cfg, cvars, info, 2, NULL, carray);
922 else if (!var && (ins->dreg != -1)) {
924 * We don't record def-use information for local vregs since it would be
925 * expensive. Instead, we depend on the fact that all uses of the vreg are in
926 * the same bblock, so they will be examined after the definition.
927 * FIXME: This isn't true if the ins is visited through an SSA edge.
929 if (c0) {
930 carray [ins->dreg] = c0;
931 } else {
932 if (carray [ins->dreg]) {
934 * The state of the vreg changed from constant to non-constant
935 * -> need to rescan the whole bblock.
937 carray [ins->dreg] = NULL;
938 /* FIXME: Speed this up */
940 if (!g_list_find (*bblist, bb))
941 *bblist = g_list_prepend (*bblist, bb);
946 if (MONO_IS_JUMP_TABLE (ins)) {
947 int i;
948 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
950 if (!ins->next || ins->next->opcode != OP_PADD) {
951 /* The PADD was optimized away */
952 /* FIXME: handle this as well */
953 for (i = 0; i < table->table_size; i++)
954 if (table->table [i])
955 add_cprop_bb (cfg, table->table [i], bblist);
956 return;
959 g_assert (ins->next->opcode == OP_PADD);
960 g_assert (ins->next->sreg1 == ins->dreg);
962 if (carray [ins->next->sreg2]) {
963 #if SIZEOF_REGISTER == 8
964 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
965 #else
966 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
967 #endif
968 if ((idx < 0) || (idx >= table->table_size))
969 /* Out-of-range, no branch is executed */
970 return;
971 else
972 if (table->table [idx])
973 add_cprop_bb (cfg, table->table [idx], bblist);
975 else {
976 for (i = 0; i < table->table_size; i++)
977 if (table->table [i])
978 add_cprop_bb (cfg, table->table [i], bblist);
982 if (ins->opcode == OP_SWITCH) {
983 int i;
984 MonoJumpInfoBBTable *table = ins->inst_p0;
986 for (i = 0; i < table->table_size; i++)
987 if (table->table [i])
988 add_cprop_bb (cfg, table->table [i], bblist);
991 /* Handle COMPARE+BRCOND pairs */
992 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
993 if (c0) {
994 g_assert (c0->opcode == OP_ICONST);
996 if (c0->inst_c0)
997 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
998 else
999 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1001 else {
1002 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1005 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1007 } else if (ins->opcode == OP_BR) {
1008 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1009 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1010 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1011 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1012 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1013 if (ins->inst_false_bb)
1014 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1015 } else {
1016 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1017 if (ins->inst_false_bb)
1018 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1024 * fold_ins:
1026 * Replace INS with its constant value, if it exists
1028 static void
1029 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1031 const char *spec = INS_INFO (ins->opcode);
1032 int opcode2;
1033 int num_sregs = mono_inst_get_num_src_registers (ins);
1035 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1036 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1037 /* Perform constant folding */
1038 /* FIXME: */
1039 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1040 ins->opcode = OP_ICONST;
1041 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1042 MONO_INST_NULLIFY_SREGS (ins);
1043 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1044 /* Perform op->op_imm conversion */
1045 opcode2 = mono_op_to_op_imm (ins->opcode);
1046 if (opcode2 != -1) {
1047 ins->opcode = opcode2;
1048 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1049 ins->sreg2 = -1;
1051 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1052 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1054 } else {
1055 /* FIXME: Handle 3 op insns */
1058 if (MONO_IS_JUMP_TABLE (ins)) {
1059 int i;
1060 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1062 if (!ins->next || ins->next->opcode != OP_PADD) {
1063 /* The PADD was optimized away */
1064 /* FIXME: handle this as well */
1065 return;
1068 g_assert (ins->next->opcode == OP_PADD);
1069 g_assert (ins->next->sreg1 == ins->dreg);
1070 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1072 if (carray [ins->next->sreg2]) {
1073 /* Convert to a simple branch */
1074 #if SIZEOF_REGISTER == 8
1075 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1076 #else
1077 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1078 #endif
1080 if (!((idx >= 0) && (idx < table->table_size))) {
1081 /* Out of range, eliminate the whole switch */
1082 for (i = 0; i < table->table_size; ++i) {
1083 remove_bb_from_phis (cfg, bb, table->table [i]);
1084 mono_unlink_bblock (cfg, bb, table->table [i]);
1087 NULLIFY_INS (ins);
1088 NULLIFY_INS (ins->next);
1089 NULLIFY_INS (ins->next->next);
1090 if (ins->next->next->next)
1091 NULLIFY_INS (ins->next->next->next);
1093 return;
1096 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1097 /* A one-way switch which got optimized away */
1098 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1099 printf ("\tNo cfold on ");
1100 mono_print_ins (ins);
1102 return;
1105 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1106 printf ("\tcfold on ");
1107 mono_print_ins (ins);
1110 /* Unlink target bblocks */
1111 for (i = 0; i < table->table_size; ++i) {
1112 if (i != idx) {
1113 remove_bb_from_phis (cfg, bb, table->table [i]);
1114 mono_unlink_bblock (cfg, bb, table->table [i]);
1118 /* Change the OP_BR_REG to a simple branch */
1119 ins->next->next->next->opcode = OP_BR;
1120 ins->next->next->next->inst_target_bb = table->table [idx];
1121 ins->next->next->next->sreg1 = -1;
1123 /* Nullify the other instructions */
1124 NULLIFY_INS (ins);
1125 NULLIFY_INS (ins->next);
1126 NULLIFY_INS (ins->next->next);
1130 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1131 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1132 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1133 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1134 ins->opcode = OP_BR;
1135 ins->inst_target_bb = ins->inst_true_bb;
1136 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1137 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1138 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1139 ins->opcode = OP_BR;
1140 ins->inst_target_bb = ins->inst_false_bb;
1145 void
1146 mono_ssa_cprop (MonoCompile *cfg)
1148 MonoInst **carray;
1149 MonoBasicBlock *bb;
1150 GList *bblock_list, *cvars;
1151 GList *tmp;
1152 int i;
1153 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1155 carray = g_new0 (MonoInst*, cfg->next_vreg);
1157 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1158 mono_ssa_create_def_use (cfg);
1160 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1161 cfg->bb_entry->flags |= BB_REACHABLE;
1163 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1165 for (i = 0; i < cfg->num_varinfo; i++) {
1166 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1167 if (!info->def)
1168 info->cpstate = 2;
1171 cvars = NULL;
1173 while (bblock_list) {
1174 MonoInst *inst;
1176 bb = (MonoBasicBlock *)bblock_list->data;
1178 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1180 g_assert (bb->flags & BB_REACHABLE);
1182 if (bb->out_count == 1) {
1183 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1184 bb->out_bb [0]->flags |= BB_REACHABLE;
1185 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1189 if (cfg->verbose_level > 1)
1190 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1192 for (inst = bb->code; inst; inst = inst->next) {
1193 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1196 while (cvars) {
1197 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1198 cvars = g_list_delete_link (cvars, cvars);
1200 for (tmp = info->uses; tmp; tmp = tmp->next) {
1201 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1202 if (!(ui->bb->flags & BB_REACHABLE))
1203 continue;
1204 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1209 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1210 MonoInst *inst;
1211 for (inst = bb->code; inst; inst = inst->next) {
1212 fold_ins (cfg, bb, inst, carray);
1216 g_free (carray);
1218 cfg->comp_done |= MONO_COMP_REACHABILITY;
1220 /* fixme: we should update usage infos during cprop, instead of computing it again */
1221 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1222 for (i = 0; i < cfg->num_varinfo; i++) {
1223 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1224 info->def = NULL;
1225 info->uses = NULL;
1229 static inline void
1230 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1232 GList *tmp;
1234 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1236 for (tmp = use->uses; tmp; tmp = tmp->next) {
1237 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1238 if (ui->inst == var->def) {
1239 /* from the mempool */
1240 use->uses = g_list_remove_link (use->uses, tmp);
1241 break;
1246 void
1247 mono_ssa_deadce (MonoCompile *cfg)
1249 int i;
1250 GList *work_list;
1252 g_assert (cfg->comp_done & MONO_COMP_SSA);
1254 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1256 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1257 mono_ssa_create_def_use (cfg);
1259 mono_ssa_copyprop (cfg);
1261 work_list = NULL;
1262 for (i = 0; i < cfg->num_varinfo; i++) {
1263 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1264 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1267 while (work_list) {
1268 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1269 work_list = g_list_remove_link (work_list, work_list);
1272 * The second part of the condition happens often when PHI nodes have their dreg
1273 * as one of their arguments due to the fact that we use the original vars.
1275 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1276 MonoInst *def = info->def;
1278 /* Eliminating FMOVE could screw up the fp stack */
1279 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1280 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1281 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1282 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1283 def->opcode = OP_NOP;
1284 def->dreg = def->sreg1 = def->sreg2 = -1;
1285 info->reg = -1;
1286 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1287 def->opcode = OP_NOP;
1288 def->dreg = def->sreg1 = def->sreg2 = -1;
1289 info->reg = -1;
1290 } else if (MONO_IS_PHI (def)) {
1291 int j;
1292 for (j = def->inst_phi_args [0]; j > 0; j--) {
1293 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1294 add_to_dce_worklist (cfg, info, u, &work_list);
1296 def->opcode = OP_NOP;
1297 def->dreg = def->sreg1 = def->sreg2 = -1;
1298 info->reg = -1;
1300 else if (def->opcode == OP_NOP) {
1302 //else
1303 //mono_print_ins (def);
1309 #if 0
1310 void
1311 mono_ssa_strength_reduction (MonoCompile *cfg)
1313 MonoBasicBlock *bb;
1314 int i;
1316 g_assert (cfg->comp_done & MONO_COMP_SSA);
1317 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1318 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1320 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1321 GList *lp = bb->loop_blocks;
1323 if (lp) {
1324 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1326 /* we only consider loops with 2 in bblocks */
1327 if (!h->in_count == 2)
1328 continue;
1330 for (i = 0; i < cfg->num_varinfo; i++) {
1331 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1333 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1334 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1335 MonoInst *v = info->def->inst_i1;
1338 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1344 #endif
1346 #endif /* DISABLE_JIT */