2 * ssa.c: Static single assign form support for the JIT compiler.
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/mempool.h>
13 #include <mono/metadata/mempool-internals.h>
22 #define USE_ORIGINAL_VARS
23 #define CREATE_PRUNED_SSA
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; \
40 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
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];
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];
60 unlink_unused_bblocks (MonoCompile *cfg)
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;
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);
95 * remove_bb_from_phis:
97 * Remove BB from the PHI statements in TARGET.
100 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
105 for (i = 0; i < target->in_count; i++) {
106 if (target->in_bb [i] == bb) {
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] --;
124 op_phi_to_move (int opcode)
136 g_assert_not_reached ();
143 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
146 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
148 info = MONO_VARINFO (cfg, var->inst_c0);
152 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
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.
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;
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);
182 int sregs [MONO_MAX_SRC_REGS];
185 printf ("\tProcessing "); mono_print_ins (ins);
187 if (ins->opcode == OP_NOP)
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;
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);
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;
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);
223 record_use (cfg, var, bb, ins);
225 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
226 ins->dreg = lvreg_stack [ins->dreg];
230 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
231 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
234 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
236 g_assert (idx < max_vars);
238 if (var->opcode == OP_ARG)
239 originals_used [idx] = TRUE;
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;
262 originals_used [idx] = TRUE;
265 info = MONO_VARINFO (cfg, var->inst_c0);
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];
275 lvreg_defined [ins->dreg] = TRUE;
279 printf ("\tAfter processing "); mono_print_ins (ins);
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)
292 for (ins = n->code; ins; ins = ins->next) {
293 if (MONO_IS_PHI (ins)) {
296 new_var = stack [idx];
298 new_var = cfg->varinfo [idx];
300 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
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);
308 /* The phi nodes are at the beginning of the bblock */
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);
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;
328 mono_ssa_compute (MonoCompile *cfg)
330 int i, j, idx, bitsize;
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;
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);
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);
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)
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)
389 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
392 /* Most variables have only one definition */
393 if (mono_bitset_count (vinfo [i].def_in) <= 1)
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)
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));
418 NEW_PHI (cfg, ins, i);
426 ins->opcode = OP_PHI;
429 ins->opcode = OP_FPHI;
432 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
433 ins->klass = var->klass;
437 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
438 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
441 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
442 ins->inst_phi_args [j + 1] = -1;
444 ins->dreg = cfg->varinfo [i]->dreg;
446 mono_bblock_insert_before_ins (bb, bb->code, ins);
449 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
460 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
461 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
463 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
464 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
465 stack_history_size = 10240;
466 stack_history = g_new (RenameInfo, stack_history_size);
467 originals = g_new0 (gboolean, cfg->num_varinfo);
468 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
469 g_free (stack_history);
472 if (cfg->verbose_level >= 4)
473 printf ("\nEND COMPUTE SSA.\n\n");
475 cfg->comp_done |= MONO_COMP_SSA;
479 mono_ssa_remove (MonoCompile *cfg)
481 MonoInst *ins, *var, *move;
484 g_assert (cfg->comp_done & MONO_COMP_SSA);
486 for (i = 0; i < cfg->num_bblocks; ++i) {
487 MonoBasicBlock *bb = cfg->bblocks [i];
489 if (cfg->verbose_level >= 4)
490 printf ("\nREMOVE SSA %d:\n", bb->block_num);
492 for (ins = bb->code; ins; ins = ins->next) {
493 if (MONO_IS_PHI (ins)) {
494 g_assert (ins->inst_phi_args [0] == bb->in_count);
495 var = get_vreg_to_inst (cfg, ins->dreg);
497 /* Check for PHI nodes where all the inputs are the same */
498 first = ins->inst_phi_args [1];
500 for (j = 1; j < bb->in_count; ++j)
501 if (first != ins->inst_phi_args [j + 1])
504 if ((bb->in_count > 1) && (j == bb->in_count)) {
505 ins->opcode = op_phi_to_move (ins->opcode);
506 if (ins->opcode == OP_VMOVE)
507 g_assert (ins->klass);
510 for (j = 0; j < bb->in_count; j++) {
511 MonoBasicBlock *pred = bb->in_bb [j];
512 int sreg = ins->inst_phi_args [j + 1];
514 if (cfg->verbose_level >= 4)
515 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
516 if (var->dreg != sreg) {
517 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
518 if (move->opcode == OP_VMOVE) {
519 g_assert (ins->klass);
520 move->klass = ins->klass;
522 move->dreg = var->dreg;
524 mono_add_ins_to_end (pred, move);
528 ins->opcode = OP_NOP;
535 if (cfg->verbose_level >= 4) {
536 for (i = 0; i < cfg->num_bblocks; ++i) {
537 MonoBasicBlock *bb = cfg->bblocks [i];
539 mono_print_bb (bb, "AFTER REMOVE SSA:");
544 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
545 * the variables if possible. Since the newly introduced SSA variables don't
546 * have overlapping live ranges (because we don't do agressive optimization), we
547 * can coalesce them into the original variable.
550 for (i = 0; i < cfg->num_bblocks; ++i) {
551 MonoBasicBlock *bb = cfg->bblocks [i];
553 for (ins = bb->code; ins; ins = ins->next) {
554 const char *spec = INS_INFO (ins->opcode);
556 int sregs [MONO_MAX_SRC_REGS];
558 if (ins->opcode == OP_NOP)
561 if (spec [MONO_INST_DEST] != ' ') {
562 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
565 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
568 * The third condition avoids coalescing with variables eliminated
571 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
572 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
573 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
578 num_sregs = mono_inst_get_src_registers (ins, sregs);
579 for (j = 0; j < num_sregs; ++j) {
580 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
583 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
585 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
586 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
587 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
591 mono_inst_set_src_registers (ins, sregs);
595 for (i = 0; i < cfg->num_varinfo; ++i) {
596 MONO_VARINFO (cfg, i)->reg = -1;
599 if (cfg->comp_done & MONO_COMP_REACHABILITY)
600 unlink_unused_bblocks (cfg);
602 cfg->comp_done &= ~MONO_COMP_LIVENESS;
604 cfg->comp_done &= ~MONO_COMP_SSA;
608 mono_ssa_create_def_use (MonoCompile *cfg)
614 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
616 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
617 for (ins = bb->code; ins; ins = ins->next) {
618 const char *spec = INS_INFO (ins->opcode);
621 int sregs [MONO_MAX_SRC_REGS];
623 if (ins->opcode == OP_NOP)
627 num_sregs = mono_inst_get_src_registers (ins, sregs);
628 for (i = 0; i < num_sregs; ++i) {
629 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
630 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
631 record_use (cfg, var, bb, ins);
634 if (MONO_IS_STORE_MEMBASE (ins)) {
635 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
636 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
637 record_use (cfg, var, bb, ins);
640 if (MONO_IS_PHI (ins)) {
641 for (i = ins->inst_phi_args [0]; i > 0; i--) {
642 g_assert (ins->inst_phi_args [i] != -1);
643 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
648 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
649 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
651 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
652 info = MONO_VARINFO (cfg, var->inst_c0);
660 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
664 mono_ssa_copyprop (MonoCompile *cfg)
669 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
671 for (index = 0; index < cfg->num_varinfo; ++index) {
672 MonoInst *var = cfg->varinfo [index];
673 MonoMethodVar *info = MONO_VARINFO (cfg, index);
675 if (info->def && (MONO_IS_MOVE (info->def))) {
676 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
678 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))) {
679 /* Rewrite all uses of var to be uses of var2 */
680 int dreg = var->dreg;
681 int sreg1 = var2->dreg;
686 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
687 MonoInst *ins = u->inst;
688 GList *next = l->next;
690 int sregs [MONO_MAX_SRC_REGS];
692 spec = INS_INFO (ins->opcode);
694 num_sregs = mono_inst_get_src_registers (ins, sregs);
695 for (i = 0; i < num_sregs; ++i) {
696 if (sregs [i] == dreg)
700 g_assert (sregs [i] == dreg);
702 mono_inst_set_src_registers (ins, sregs);
703 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
705 } else if (MONO_IS_PHI (ins)) {
706 for (i = ins->inst_phi_args [0]; i > 0; i--) {
707 int sreg = ins->inst_phi_args [i];
708 if (sreg == var->dreg)
712 ins->inst_phi_args [i] = sreg1;
715 g_assert_not_reached ();
717 record_use (cfg, var2, u->bb, ins);
727 if (cfg->verbose_level >= 4) {
730 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
731 mono_print_bb (bb, "AFTER SSA COPYPROP");
736 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
738 MonoInst *args [MONO_MAX_SRC_REGS];
739 int rs [MONO_MAX_SRC_REGS];
741 gboolean const_args = TRUE;
742 const char *spec = INS_INFO (ins->opcode);
744 int sregs [MONO_MAX_SRC_REGS];
746 /* Short-circuit this */
747 if (ins->opcode == OP_ICONST) {
752 if (ins->opcode == OP_NOP)
755 num_sregs = mono_inst_get_src_registers (ins, sregs);
756 for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
758 for (i = 0; i < num_sregs; ++i) {
759 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
762 args [i] = carray [sregs [i]];
765 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
766 rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
773 if (num_sregs > 0 && const_args) {
774 g_assert (num_sregs <= 2);
775 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
777 *res = carray [ins->dreg];
780 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
782 if (G_UNLIKELY (cfg->verbose_level > 1)) {
783 printf ("\t cfold -> ");
790 /* Can't cfold this ins */
796 for (i = 0; i < num_sregs; ++i) {
804 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
806 if (info->cpstate >= state)
809 info->cpstate = state;
811 if (G_UNLIKELY (cfg->verbose_level > 1))
812 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
817 carray [cfg->varinfo [info->idx]->dreg] = c0;
819 if (!g_list_find (*cvars, info)) {
820 *cvars = g_list_prepend (*cvars, info);
825 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
827 if (G_UNLIKELY (cfg->verbose_level > 1))
828 printf ("\tAdd BB%d to worklist\n", bb->block_num);
830 if (!(bb->flags & BB_REACHABLE)) {
831 bb->flags |= BB_REACHABLE;
832 *bblist = g_list_prepend (*bblist, bb);
837 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
839 const char *spec = INS_INFO (ins->opcode);
841 if (ins->opcode == OP_NOP)
844 if (cfg->verbose_level > 1)
845 mono_print_ins (ins);
847 /* FIXME: Support longs/floats */
848 /* FIXME: Work on vregs as well */
850 if (MONO_IS_PHI (ins)) {
851 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
855 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
856 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
857 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
858 MonoInst *src = mv->def;
860 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
863 if (!mv->def || !src || mv->cpstate == 2) {
864 change_varstate (cfg, cvars, info, 2, NULL, carray);
868 if (mv->cpstate == 0)
871 g_assert (carray [var->dreg]);
874 c0 = carray [var->dreg];
877 if (c0->opcode != OP_ICONST) {
878 change_varstate (cfg, cvars, info, 2, NULL, carray);
882 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
883 change_varstate (cfg, cvars, info, 2, NULL, carray);
888 if (c0 && info->cpstate < 1) {
889 change_varstate (cfg, cvars, info, 1, c0, carray);
891 g_assert (c0->opcode == OP_ICONST);
894 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
898 if (spec [MONO_INST_DEST] != ' ')
899 var = get_vreg_to_inst (cfg, ins->dreg);
904 state = evaluate_ins (cfg, ins, &c0, carray);
906 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
907 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
909 if (info->cpstate < 2) {
911 change_varstate (cfg, cvars, info, 1, c0, carray);
913 change_varstate (cfg, cvars, info, 2, NULL, carray);
916 else if (!var && (ins->dreg != -1)) {
918 * We don't record def-use information for local vregs since it would be
919 * expensive. Instead, we depend on the fact that all uses of the vreg are in
920 * the same bblock, so they will be examined after the definition.
921 * FIXME: This isn't true if the ins is visited through an SSA edge.
924 carray [ins->dreg] = c0;
926 if (carray [ins->dreg]) {
928 * The state of the vreg changed from constant to non-constant
929 * -> need to rescan the whole bblock.
931 carray [ins->dreg] = NULL;
932 /* FIXME: Speed this up */
934 if (!g_list_find (*bblist, bb))
935 *bblist = g_list_prepend (*bblist, bb);
940 if (MONO_IS_JUMP_TABLE (ins)) {
942 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
944 if (!ins->next || ins->next->opcode != OP_PADD) {
945 /* The PADD was optimized away */
946 /* FIXME: handle this as well */
947 for (i = 0; i < table->table_size; i++)
948 if (table->table [i])
949 add_cprop_bb (cfg, table->table [i], bblist);
953 g_assert (ins->next->opcode == OP_PADD);
954 g_assert (ins->next->sreg1 == ins->dreg);
956 if (carray [ins->next->sreg2]) {
957 #if SIZEOF_REGISTER == 8
958 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
960 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
962 if ((idx < 0) || (idx >= table->table_size))
963 /* Out-of-range, no branch is executed */
966 if (table->table [idx])
967 add_cprop_bb (cfg, table->table [idx], bblist);
970 for (i = 0; i < table->table_size; i++)
971 if (table->table [i])
972 add_cprop_bb (cfg, table->table [i], bblist);
976 if (ins->opcode == OP_SWITCH) {
978 MonoJumpInfoBBTable *table = ins->inst_p0;
980 for (i = 0; i < table->table_size; i++)
981 if (table->table [i])
982 add_cprop_bb (cfg, table->table [i], bblist);
985 /* Handle COMPARE+BRCOND pairs */
986 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
988 g_assert (c0->opcode == OP_ICONST);
991 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
993 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
996 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
999 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1001 } else if (ins->opcode == OP_BR) {
1002 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1003 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1004 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1005 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1006 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1007 if (ins->inst_false_bb)
1008 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1010 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1011 if (ins->inst_false_bb)
1012 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1020 * Replace INS with its constant value, if it exists
1023 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1025 const char *spec = INS_INFO (ins->opcode);
1027 int num_sregs = mono_inst_get_num_src_registers (ins);
1029 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1030 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1031 /* Perform constant folding */
1033 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1034 ins->opcode = OP_ICONST;
1035 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1036 MONO_INST_NULLIFY_SREGS (ins);
1037 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1038 /* Perform op->op_imm conversion */
1039 opcode2 = mono_op_to_op_imm (ins->opcode);
1040 if (opcode2 != -1) {
1041 ins->opcode = opcode2;
1042 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1045 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1046 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1049 /* FIXME: Handle 3 op insns */
1052 if (MONO_IS_JUMP_TABLE (ins)) {
1054 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1056 if (!ins->next || ins->next->opcode != OP_PADD) {
1057 /* The PADD was optimized away */
1058 /* FIXME: handle this as well */
1062 g_assert (ins->next->opcode == OP_PADD);
1063 g_assert (ins->next->sreg1 == ins->dreg);
1064 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1066 if (carray [ins->next->sreg2]) {
1067 /* Convert to a simple branch */
1068 #if SIZEOF_REGISTER == 8
1069 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1071 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1074 if (!((idx >= 0) && (idx < table->table_size))) {
1075 /* Out of range, eliminate the whole switch */
1076 for (i = 0; i < table->table_size; ++i) {
1077 remove_bb_from_phis (cfg, bb, table->table [i]);
1078 mono_unlink_bblock (cfg, bb, table->table [i]);
1082 NULLIFY_INS (ins->next);
1083 NULLIFY_INS (ins->next->next);
1084 if (ins->next->next->next)
1085 NULLIFY_INS (ins->next->next->next);
1090 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1091 /* A one-way switch which got optimized away */
1092 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1093 printf ("\tNo cfold on ");
1094 mono_print_ins (ins);
1099 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1100 printf ("\tcfold on ");
1101 mono_print_ins (ins);
1104 /* Unlink target bblocks */
1105 for (i = 0; i < table->table_size; ++i) {
1107 remove_bb_from_phis (cfg, bb, table->table [i]);
1108 mono_unlink_bblock (cfg, bb, table->table [i]);
1112 /* Change the OP_BR_REG to a simple branch */
1113 ins->next->next->next->opcode = OP_BR;
1114 ins->next->next->next->inst_target_bb = table->table [idx];
1115 ins->next->next->next->sreg1 = -1;
1117 /* Nullify the other instructions */
1119 NULLIFY_INS (ins->next);
1120 NULLIFY_INS (ins->next->next);
1124 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1125 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1126 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1127 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1128 ins->opcode = OP_BR;
1129 ins->inst_target_bb = ins->inst_true_bb;
1130 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1131 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1132 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1133 ins->opcode = OP_BR;
1134 ins->inst_target_bb = ins->inst_false_bb;
1140 mono_ssa_cprop (MonoCompile *cfg)
1144 GList *bblock_list, *cvars;
1147 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1149 carray = g_new0 (MonoInst*, cfg->next_vreg);
1151 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1152 mono_ssa_create_def_use (cfg);
1154 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1155 cfg->bb_entry->flags |= BB_REACHABLE;
1157 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1159 for (i = 0; i < cfg->num_varinfo; i++) {
1160 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1167 while (bblock_list) {
1170 bb = (MonoBasicBlock *)bblock_list->data;
1172 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1174 g_assert (bb->flags & BB_REACHABLE);
1176 if (bb->out_count == 1) {
1177 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1178 bb->out_bb [0]->flags |= BB_REACHABLE;
1179 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1183 if (cfg->verbose_level > 1)
1184 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1186 for (inst = bb->code; inst; inst = inst->next) {
1187 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1191 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1192 cvars = g_list_delete_link (cvars, cvars);
1194 for (tmp = info->uses; tmp; tmp = tmp->next) {
1195 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1196 if (!(ui->bb->flags & BB_REACHABLE))
1198 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1203 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1205 for (inst = bb->code; inst; inst = inst->next) {
1206 fold_ins (cfg, bb, inst, carray);
1212 cfg->comp_done |= MONO_COMP_REACHABILITY;
1214 /* fixme: we should update usage infos during cprop, instead of computing it again */
1215 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1216 for (i = 0; i < cfg->num_varinfo; i++) {
1217 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1224 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1228 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1230 for (tmp = use->uses; tmp; tmp = tmp->next) {
1231 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1232 if (ui->inst == var->def) {
1233 /* from the mempool */
1234 use->uses = g_list_remove_link (use->uses, tmp);
1241 mono_ssa_deadce (MonoCompile *cfg)
1246 g_assert (cfg->comp_done & MONO_COMP_SSA);
1248 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1250 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1251 mono_ssa_create_def_use (cfg);
1253 mono_ssa_copyprop (cfg);
1256 for (i = 0; i < cfg->num_varinfo; i++) {
1257 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1258 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1262 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1263 work_list = g_list_remove_link (work_list, work_list);
1266 * The second part of the condition happens often when PHI nodes have their dreg
1267 * as one of their arguments due to the fact that we use the original vars.
1269 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1270 MonoInst *def = info->def;
1272 /* Eliminating FMOVE could screw up the fp stack */
1273 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1274 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1275 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1276 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1277 def->opcode = OP_NOP;
1278 def->dreg = def->sreg1 = def->sreg2 = -1;
1280 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1281 def->opcode = OP_NOP;
1282 def->dreg = def->sreg1 = def->sreg2 = -1;
1284 } else if (MONO_IS_PHI (def)) {
1286 for (j = def->inst_phi_args [0]; j > 0; j--) {
1287 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1288 add_to_dce_worklist (cfg, info, u, &work_list);
1290 def->opcode = OP_NOP;
1291 def->dreg = def->sreg1 = def->sreg2 = -1;
1294 else if (def->opcode == OP_NOP) {
1297 //mono_print_ins (def);
1305 mono_ssa_strength_reduction (MonoCompile *cfg)
1310 g_assert (cfg->comp_done & MONO_COMP_SSA);
1311 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1312 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1314 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1315 GList *lp = bb->loop_blocks;
1318 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1320 /* we only consider loops with 2 in bblocks */
1321 if (!h->in_count == 2)
1324 for (i = 0; i < cfg->num_varinfo; i++) {
1325 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1327 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1328 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1329 MonoInst *v = info->def->inst_i1;
1332 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1340 #endif /* DISABLE_JIT */