2 * ssa.c: Static single assign form support for the JIT compiler.
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/mempool.h>
14 #include <mono/metadata/mempool-internals.h>
23 #define USE_ORIGINAL_VARS
24 #define CREATE_PRUNED_SSA
28 #define NEW_PHI(cfg,dest,val) do { \
29 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
30 (dest)->opcode = OP_PHI; \
31 (dest)->inst_c0 = (val); \
32 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
41 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
45 for (i = 0; i < bb->out_count; i++) {
46 if (bb->out_bb [i] == target) {
47 bb->out_bb [i] = bb->out_bb [--bb->out_count];
51 for (i = 0; i < target->in_count; i++) {
52 if (target->in_bb [i] == bb) {
53 target->in_bb [i] = target->in_bb [--target->in_count];
61 unlink_unused_bblocks (MonoCompile *cfg)
66 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
68 if (G_UNLIKELY (cfg->verbose_level > 1))
69 printf ("\nUNLINK UNUSED BBLOCKS:\n");
71 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
72 if (!(bb->next_bb->flags & BB_REACHABLE)) {
73 bb->next_bb = bb->next_bb->next_bb;
78 for (i = 1; i < cfg->num_bblocks; i++) {
79 bb = cfg->bblocks [i];
81 if (!(bb->flags & BB_REACHABLE)) {
82 for (j = 0; j < bb->in_count; j++) {
83 unlink_target (bb->in_bb [j], bb);
85 for (j = 0; j < bb->out_count; j++) {
86 unlink_target (bb, bb->out_bb [j]);
88 if (G_UNLIKELY (cfg->verbose_level > 1))
89 printf ("\tUnlinked BB%d\n", bb->block_num);
96 * remove_bb_from_phis:
98 * Remove BB from the PHI statements in TARGET.
101 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
106 for (i = 0; i < target->in_count; i++) {
107 if (target->in_bb [i] == bb) {
111 g_assert (i < target->in_count);
113 for (ins = target->code; ins; ins = ins->next) {
114 if (MONO_IS_PHI (ins)) {
115 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
116 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
117 ins->inst_phi_args [0] --;
125 op_phi_to_move (int opcode)
137 g_assert_not_reached ();
144 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
147 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
149 info = MONO_VARINFO (cfg, var->inst_c0);
153 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
162 * mono_ssa_rename_vars:
164 * Implement renaming of SSA variables. Also compute def-use information in parallel.
165 * @stack_history points to an area of memory which can be used for storing changes
166 * made to the stack, so they can be reverted later.
169 mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, gboolean *originals_used, MonoInst **stack, guint32 *lvreg_stack, gboolean *lvreg_defined, RenameInfo *stack_history, int stack_history_size)
171 MonoInst *ins, *new_var;
174 int stack_history_len = 0;
176 if (cfg->verbose_level >= 4)
177 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
179 /* First pass: Create new vars */
180 for (ins = bb->code; ins; ins = ins->next) {
181 const char *spec = INS_INFO (ins->opcode);
183 int sregs [MONO_MAX_SRC_REGS];
186 printf ("\tProcessing "); mono_print_ins (ins);
188 if (ins->opcode == OP_NOP)
192 num_sregs = mono_inst_get_src_registers (ins, sregs);
193 for (i = 0; i < num_sregs; ++i) {
194 if (spec [MONO_INST_SRC1 + i] != ' ') {
195 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
196 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
197 int idx = var->inst_c0;
199 if (var->opcode != OP_ARG)
200 g_assert (stack [idx]);
201 sregs [i] = stack [idx]->dreg;
202 record_use (cfg, stack [idx], bb, ins);
205 record_use (cfg, var, bb, ins);
207 else if (G_UNLIKELY (!var && lvreg_stack [sregs [i]]))
208 sregs [i] = lvreg_stack [sregs [i]];
211 mono_inst_set_src_registers (ins, sregs);
213 if (MONO_IS_STORE_MEMBASE (ins)) {
214 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
215 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
216 int idx = var->inst_c0;
218 if (var->opcode != OP_ARG)
219 g_assert (stack [idx]);
220 ins->dreg = stack [idx]->dreg;
221 record_use (cfg, stack [idx], bb, ins);
224 record_use (cfg, var, bb, ins);
226 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
227 ins->dreg = lvreg_stack [ins->dreg];
231 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
232 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
235 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
237 g_assert (idx < max_vars);
239 if (var->opcode == OP_ARG)
240 originals_used [idx] = TRUE;
243 g_assert (stack_history_len < stack_history_size);
244 stack_history [stack_history_len].var = stack [idx];
245 stack_history [stack_history_len].idx = idx;
246 stack_history_len ++;
248 if (originals_used [idx]) {
249 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
250 new_var->flags = var->flags;
251 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
253 if (cfg->verbose_level >= 4)
254 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
256 stack [idx] = new_var;
258 ins->dreg = new_var->dreg;
263 originals_used [idx] = TRUE;
266 info = MONO_VARINFO (cfg, var->inst_c0);
270 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
271 /* Perform renaming for local vregs */
272 lvreg_stack [ins->dreg] = vreg_is_ref (cfg, ins->dreg) ? mono_alloc_ireg_ref (cfg) : mono_alloc_preg (cfg);
273 ins->dreg = lvreg_stack [ins->dreg];
276 lvreg_defined [ins->dreg] = TRUE;
280 printf ("\tAfter processing "); mono_print_ins (ins);
285 /* Rename PHI arguments in succeeding bblocks */
286 for (i = 0; i < bb->out_count; i++) {
287 MonoBasicBlock *n = bb->out_bb [i];
289 for (j = 0; j < n->in_count; j++)
290 if (n->in_bb [j] == bb)
293 for (ins = n->code; ins; ins = ins->next) {
294 if (MONO_IS_PHI (ins)) {
297 new_var = stack [idx];
299 new_var = cfg->varinfo [idx];
301 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
303 ins->inst_phi_args [j + 1] = new_var->dreg;
304 record_use (cfg, new_var, n, ins);
305 if (G_UNLIKELY (cfg->verbose_level >= 4))
306 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
309 /* The phi nodes are at the beginning of the bblock */
315 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
316 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);
321 for (i = stack_history_len - 1; i >= 0; i--) {
322 stack [stack_history [i].idx] = stack_history [i].var;
325 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
329 mono_ssa_compute (MonoCompile *cfg)
331 int i, j, idx, bitsize;
333 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
334 MonoInst *ins, **stack;
335 guint8 *buf, *buf_start;
336 RenameInfo *stack_history;
337 int stack_history_size;
339 guint32 *lvreg_stack;
340 gboolean *lvreg_defined;
342 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
344 g_assert (!cfg->disable_ssa);
346 if (cfg->verbose_level >= 4)
347 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
349 #ifdef CREATE_PRUNED_SSA
350 /* we need liveness for pruned SSA */
351 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
352 mono_analyze_liveness (cfg);
355 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
357 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
358 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
360 for (i = 0; i < cfg->num_varinfo; ++i) {
361 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
364 /* implicit reference at start */
365 if (cfg->varinfo [i]->opcode == OP_ARG)
366 mono_bitset_set_fast (vinfo [i].def_in, 0);
369 for (i = 0; i < cfg->num_bblocks; ++i) {
370 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
371 if (ins->opcode == OP_NOP)
374 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
375 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
380 /* insert phi functions */
381 for (i = 0; i < cfg->num_varinfo; ++i) {
382 MonoInst *var = cfg->varinfo [i];
384 #if SIZEOF_REGISTER == 4
385 if (var->type == STACK_I8 && !COMPILE_LLVM (cfg))
388 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
391 /* Most variables have only one definition */
392 if (mono_bitset_count (vinfo [i].def_in) <= 1)
395 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
397 if (cfg->verbose_level >= 4) {
398 if (mono_bitset_count (set) > 0) {
399 printf ("\tR%d needs PHI functions in ", var->dreg);
400 mono_blockset_print (cfg, set, "", -1);
404 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
405 MonoBasicBlock *bb = cfg->bblocks [idx];
407 /* fixme: create pruned SSA? we would need liveness information for that */
409 if (bb == cfg->bb_exit && !COMPILE_LLVM (cfg))
412 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
413 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
417 NEW_PHI (cfg, ins, i);
425 ins->opcode = OP_PHI;
428 ins->opcode = OP_FPHI;
431 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
435 if (var->inst_vtype->byref)
436 ins->klass = mono_defaults.int_class;
438 ins->klass = var->klass;
440 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
441 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
444 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
445 ins->inst_phi_args [j + 1] = -1;
447 ins->dreg = cfg->varinfo [i]->dreg;
449 mono_bblock_insert_before_ins (bb, bb->code, ins);
452 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
463 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
464 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
466 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
467 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
468 stack_history_size = 10240;
469 stack_history = g_new (RenameInfo, stack_history_size);
470 originals = g_new0 (gboolean, cfg->num_varinfo);
471 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
472 g_free (stack_history);
474 g_free (lvreg_stack);
475 g_free (lvreg_defined);
477 if (cfg->verbose_level >= 4)
478 printf ("\nEND COMPUTE SSA.\n\n");
480 cfg->comp_done |= MONO_COMP_SSA;
484 mono_ssa_remove (MonoCompile *cfg)
486 MonoInst *ins, *var, *move;
487 int bbindex, i, j, first;
489 g_assert (cfg->comp_done & MONO_COMP_SSA);
491 for (i = 0; i < cfg->num_bblocks; ++i) {
492 MonoBasicBlock *bb = cfg->bblocks [i];
494 if (cfg->verbose_level >= 4)
495 printf ("\nREMOVE SSA %d:\n", bb->block_num);
497 for (ins = bb->code; ins; ins = ins->next) {
498 if (MONO_IS_PHI (ins)) {
499 g_assert (ins->inst_phi_args [0] == bb->in_count);
500 var = get_vreg_to_inst (cfg, ins->dreg);
502 /* Check for PHI nodes where all the inputs are the same */
503 first = ins->inst_phi_args [1];
505 for (j = 1; j < bb->in_count; ++j)
506 if (first != ins->inst_phi_args [j + 1])
509 if ((bb->in_count > 1) && (j == bb->in_count)) {
510 ins->opcode = op_phi_to_move (ins->opcode);
511 if (ins->opcode == OP_VMOVE)
512 g_assert (ins->klass);
515 for (j = 0; j < bb->in_count; j++) {
516 MonoBasicBlock *pred = bb->in_bb [j];
517 int sreg = ins->inst_phi_args [j + 1];
519 if (cfg->verbose_level >= 4)
520 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
521 if (var->dreg != sreg) {
522 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
523 if (move->opcode == OP_VMOVE) {
524 g_assert (ins->klass);
525 move->klass = ins->klass;
527 move->dreg = var->dreg;
529 mono_add_ins_to_end (pred, move);
533 ins->opcode = OP_NOP;
540 if (cfg->verbose_level >= 4) {
541 for (i = 0; i < cfg->num_bblocks; ++i) {
542 MonoBasicBlock *bb = cfg->bblocks [i];
544 mono_print_bb (bb, "AFTER REMOVE SSA:");
549 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
550 * the variables if possible. Since the newly introduced SSA variables don't
551 * have overlapping live ranges (because we don't do agressive optimization), we
552 * can coalesce them into the original variable.
555 for (bbindex = 0; bbindex < cfg->num_bblocks; ++bbindex) {
556 MonoBasicBlock *bb = cfg->bblocks [bbindex];
558 for (ins = bb->code; ins; ins = ins->next) {
559 const char *spec = INS_INFO (ins->opcode);
561 int sregs [MONO_MAX_SRC_REGS];
563 if (ins->opcode == OP_NOP)
566 if (spec [MONO_INST_DEST] != ' ') {
567 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
570 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
573 * The third condition avoids coalescing with variables eliminated
576 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
577 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
578 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
583 num_sregs = mono_inst_get_src_registers (ins, sregs);
584 for (i = 0; i < num_sregs; ++i) {
585 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
588 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
590 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
591 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
592 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
596 mono_inst_set_src_registers (ins, sregs);
600 for (i = 0; i < cfg->num_varinfo; ++i) {
601 MONO_VARINFO (cfg, i)->reg = -1;
604 if (cfg->comp_done & MONO_COMP_REACHABILITY)
605 unlink_unused_bblocks (cfg);
607 cfg->comp_done &= ~MONO_COMP_LIVENESS;
609 cfg->comp_done &= ~MONO_COMP_SSA;
613 mono_ssa_create_def_use (MonoCompile *cfg)
619 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
621 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
622 for (ins = bb->code; ins; ins = ins->next) {
623 const char *spec = INS_INFO (ins->opcode);
626 int sregs [MONO_MAX_SRC_REGS];
628 if (ins->opcode == OP_NOP)
632 num_sregs = mono_inst_get_src_registers (ins, sregs);
633 for (i = 0; i < num_sregs; ++i) {
634 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
635 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
636 record_use (cfg, var, bb, ins);
639 if (MONO_IS_STORE_MEMBASE (ins)) {
640 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
641 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
642 record_use (cfg, var, bb, ins);
645 if (MONO_IS_PHI (ins)) {
646 for (i = ins->inst_phi_args [0]; i > 0; i--) {
647 g_assert (ins->inst_phi_args [i] != -1);
648 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
653 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
654 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
656 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
657 info = MONO_VARINFO (cfg, var->inst_c0);
665 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
669 mono_ssa_copyprop (MonoCompile *cfg)
674 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
676 for (index = 0; index < cfg->num_varinfo; ++index) {
677 MonoInst *var = cfg->varinfo [index];
678 MonoMethodVar *info = MONO_VARINFO (cfg, index);
680 if (info->def && (MONO_IS_MOVE (info->def))) {
681 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
683 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))) {
684 /* Rewrite all uses of var to be uses of var2 */
685 int dreg = var->dreg;
686 int sreg1 = var2->dreg;
690 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
691 MonoInst *ins = u->inst;
692 GList *next = l->next;
694 int sregs [MONO_MAX_SRC_REGS];
696 num_sregs = mono_inst_get_src_registers (ins, sregs);
697 for (i = 0; i < num_sregs; ++i) {
698 if (sregs [i] == dreg)
702 g_assert (sregs [i] == dreg);
704 mono_inst_set_src_registers (ins, sregs);
705 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
707 } else if (MONO_IS_PHI (ins)) {
708 for (i = ins->inst_phi_args [0]; i > 0; i--) {
709 int sreg = ins->inst_phi_args [i];
710 if (sreg == var->dreg)
714 ins->inst_phi_args [i] = sreg1;
717 g_assert_not_reached ();
719 record_use (cfg, var2, u->bb, ins);
729 if (cfg->verbose_level >= 4) {
732 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
733 mono_print_bb (bb, "AFTER SSA COPYPROP");
738 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
740 MonoInst *args [MONO_MAX_SRC_REGS];
741 int rs [MONO_MAX_SRC_REGS];
743 gboolean const_args = TRUE;
744 const char *spec = INS_INFO (ins->opcode);
746 int sregs [MONO_MAX_SRC_REGS];
748 /* Short-circuit this */
749 if (ins->opcode == OP_ICONST) {
754 if (ins->opcode == OP_NOP)
757 num_sregs = mono_inst_get_src_registers (ins, sregs);
758 for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
760 for (i = 0; i < num_sregs; ++i) {
761 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
764 args [i] = carray [sregs [i]];
767 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
768 rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
775 if (num_sregs > 0 && const_args) {
776 g_assert (num_sregs <= 2);
777 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
779 *res = carray [ins->dreg];
782 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
784 if (G_UNLIKELY (cfg->verbose_level > 1)) {
785 printf ("\t cfold -> ");
792 /* Can't cfold this ins */
798 for (i = 0; i < num_sregs; ++i) {
806 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
808 if (info->cpstate >= state)
811 info->cpstate = state;
813 if (G_UNLIKELY (cfg->verbose_level > 1))
814 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
819 carray [cfg->varinfo [info->idx]->dreg] = c0;
821 if (!g_list_find (*cvars, info)) {
822 *cvars = g_list_prepend (*cvars, info);
827 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
829 if (G_UNLIKELY (cfg->verbose_level > 1))
830 printf ("\tAdd BB%d to worklist\n", bb->block_num);
832 if (!(bb->flags & BB_REACHABLE)) {
833 bb->flags |= BB_REACHABLE;
834 *bblist = g_list_prepend (*bblist, bb);
839 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
841 const char *spec = INS_INFO (ins->opcode);
843 if (ins->opcode == OP_NOP)
846 if (cfg->verbose_level > 1)
847 mono_print_ins (ins);
849 /* FIXME: Support longs/floats */
850 /* FIXME: Work on vregs as well */
852 if (MONO_IS_PHI (ins)) {
853 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
857 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
858 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
859 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
860 MonoInst *src = mv->def;
862 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
865 if (!mv->def || !src || mv->cpstate == 2) {
866 change_varstate (cfg, cvars, info, 2, NULL, carray);
870 if (mv->cpstate == 0)
873 g_assert (carray [var->dreg]);
876 c0 = carray [var->dreg];
879 if (c0->opcode != OP_ICONST) {
880 change_varstate (cfg, cvars, info, 2, NULL, carray);
884 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
885 change_varstate (cfg, cvars, info, 2, NULL, carray);
890 if (c0 && info->cpstate < 1) {
891 change_varstate (cfg, cvars, info, 1, c0, carray);
893 g_assert (c0->opcode == OP_ICONST);
896 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
900 if (spec [MONO_INST_DEST] != ' ')
901 var = get_vreg_to_inst (cfg, ins->dreg);
906 state = evaluate_ins (cfg, ins, &c0, carray);
908 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
909 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
911 if (info->cpstate < 2) {
913 change_varstate (cfg, cvars, info, 1, c0, carray);
915 change_varstate (cfg, cvars, info, 2, NULL, carray);
918 else if (!var && (ins->dreg != -1)) {
920 * We don't record def-use information for local vregs since it would be
921 * expensive. Instead, we depend on the fact that all uses of the vreg are in
922 * the same bblock, so they will be examined after the definition.
923 * FIXME: This isn't true if the ins is visited through an SSA edge.
926 carray [ins->dreg] = c0;
928 if (carray [ins->dreg]) {
930 * The state of the vreg changed from constant to non-constant
931 * -> need to rescan the whole bblock.
933 carray [ins->dreg] = NULL;
934 /* FIXME: Speed this up */
936 if (!g_list_find (*bblist, bb))
937 *bblist = g_list_prepend (*bblist, bb);
942 if (MONO_IS_JUMP_TABLE (ins)) {
944 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
946 if (!ins->next || ins->next->opcode != OP_PADD) {
947 /* The PADD was optimized away */
948 /* FIXME: handle this as well */
949 for (i = 0; i < table->table_size; i++)
950 if (table->table [i])
951 add_cprop_bb (cfg, table->table [i], bblist);
955 g_assert (ins->next->opcode == OP_PADD);
956 g_assert (ins->next->sreg1 == ins->dreg);
958 if (carray [ins->next->sreg2]) {
959 #if SIZEOF_REGISTER == 8
960 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
962 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
964 if ((idx < 0) || (idx >= table->table_size))
965 /* Out-of-range, no branch is executed */
968 if (table->table [idx])
969 add_cprop_bb (cfg, table->table [idx], bblist);
972 for (i = 0; i < table->table_size; i++)
973 if (table->table [i])
974 add_cprop_bb (cfg, table->table [i], bblist);
978 if (ins->opcode == OP_SWITCH) {
980 MonoJumpInfoBBTable *table = ins->inst_p0;
982 for (i = 0; i < table->table_size; i++)
983 if (table->table [i])
984 add_cprop_bb (cfg, table->table [i], bblist);
987 /* Handle COMPARE+BRCOND pairs */
988 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
990 g_assert (c0->opcode == OP_ICONST);
993 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
995 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
998 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1001 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1003 } else if (ins->opcode == OP_BR) {
1004 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1005 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1006 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1007 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1008 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1009 if (ins->inst_false_bb)
1010 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1012 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1013 if (ins->inst_false_bb)
1014 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1022 * Replace INS with its constant value, if it exists
1025 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1027 const char *spec = INS_INFO (ins->opcode);
1029 int num_sregs = mono_inst_get_num_src_registers (ins);
1031 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1032 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1033 /* Perform constant folding */
1035 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1036 ins->opcode = OP_ICONST;
1037 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1038 MONO_INST_NULLIFY_SREGS (ins);
1039 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1040 /* Perform op->op_imm conversion */
1041 opcode2 = mono_op_to_op_imm (ins->opcode);
1042 if (opcode2 != -1) {
1043 ins->opcode = opcode2;
1044 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1047 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1048 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1051 /* FIXME: Handle 3 op insns */
1054 if (MONO_IS_JUMP_TABLE (ins)) {
1056 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1058 if (!ins->next || ins->next->opcode != OP_PADD) {
1059 /* The PADD was optimized away */
1060 /* FIXME: handle this as well */
1064 g_assert (ins->next->opcode == OP_PADD);
1065 g_assert (ins->next->sreg1 == ins->dreg);
1066 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1068 if (carray [ins->next->sreg2]) {
1069 /* Convert to a simple branch */
1070 #if SIZEOF_REGISTER == 8
1071 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1073 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1076 if (!((idx >= 0) && (idx < table->table_size))) {
1077 /* Out of range, eliminate the whole switch */
1078 for (i = 0; i < table->table_size; ++i) {
1079 remove_bb_from_phis (cfg, bb, table->table [i]);
1080 mono_unlink_bblock (cfg, bb, table->table [i]);
1084 NULLIFY_INS (ins->next);
1085 NULLIFY_INS (ins->next->next);
1086 if (ins->next->next->next)
1087 NULLIFY_INS (ins->next->next->next);
1092 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1093 /* A one-way switch which got optimized away */
1094 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1095 printf ("\tNo cfold on ");
1096 mono_print_ins (ins);
1101 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1102 printf ("\tcfold on ");
1103 mono_print_ins (ins);
1106 /* Unlink target bblocks */
1107 for (i = 0; i < table->table_size; ++i) {
1109 remove_bb_from_phis (cfg, bb, table->table [i]);
1110 mono_unlink_bblock (cfg, bb, table->table [i]);
1114 /* Change the OP_BR_REG to a simple branch */
1115 ins->next->next->next->opcode = OP_BR;
1116 ins->next->next->next->inst_target_bb = table->table [idx];
1117 ins->next->next->next->sreg1 = -1;
1119 /* Nullify the other instructions */
1121 NULLIFY_INS (ins->next);
1122 NULLIFY_INS (ins->next->next);
1126 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1127 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1128 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1129 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1130 ins->opcode = OP_BR;
1131 ins->inst_target_bb = ins->inst_true_bb;
1132 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1133 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1134 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1135 ins->opcode = OP_BR;
1136 ins->inst_target_bb = ins->inst_false_bb;
1142 mono_ssa_cprop (MonoCompile *cfg)
1146 GList *bblock_list, *cvars;
1149 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1151 carray = g_new0 (MonoInst*, cfg->next_vreg);
1153 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1154 mono_ssa_create_def_use (cfg);
1156 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1157 cfg->bb_entry->flags |= BB_REACHABLE;
1159 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1161 for (i = 0; i < cfg->num_varinfo; i++) {
1162 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1167 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1169 * FIXME: This should be bb->flags & BB_FLAG_EXCEPTION_HANDLER, but
1170 * that would still allow unreachable try's to be removed.
1173 add_cprop_bb (cfg, bb, &bblock_list);
1178 while (bblock_list) {
1181 bb = (MonoBasicBlock *)bblock_list->data;
1183 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1185 g_assert (bb->flags & BB_REACHABLE);
1188 * Some bblocks are linked to 2 others even through they fall through to the
1191 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins))) {
1192 for (i = 0; i < bb->out_count; ++i)
1193 add_cprop_bb (cfg, bb->out_bb [i], &bblock_list);
1196 if (cfg->verbose_level > 1)
1197 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1199 for (inst = bb->code; inst; inst = inst->next) {
1200 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1204 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1205 cvars = g_list_delete_link (cvars, cvars);
1207 for (tmp = info->uses; tmp; tmp = tmp->next) {
1208 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1209 if (!(ui->bb->flags & BB_REACHABLE))
1211 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1216 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1218 for (inst = bb->code; inst; inst = inst->next) {
1219 fold_ins (cfg, bb, inst, carray);
1225 cfg->comp_done |= MONO_COMP_REACHABILITY;
1227 /* fixme: we should update usage infos during cprop, instead of computing it again */
1228 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1229 for (i = 0; i < cfg->num_varinfo; i++) {
1230 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1237 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1241 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1243 for (tmp = use->uses; tmp; tmp = tmp->next) {
1244 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1245 if (ui->inst == var->def) {
1246 /* from the mempool */
1247 use->uses = g_list_remove_link (use->uses, tmp);
1254 mono_ssa_deadce (MonoCompile *cfg)
1259 g_assert (cfg->comp_done & MONO_COMP_SSA);
1261 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1263 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1264 mono_ssa_create_def_use (cfg);
1266 mono_ssa_copyprop (cfg);
1269 for (i = 0; i < cfg->num_varinfo; i++) {
1270 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1271 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1275 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1276 work_list = g_list_remove_link (work_list, work_list);
1279 * The second part of the condition happens often when PHI nodes have their dreg
1280 * as one of their arguments due to the fact that we use the original vars.
1282 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1283 MonoInst *def = info->def;
1285 /* Eliminating FMOVE could screw up the fp stack */
1286 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1287 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1288 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1289 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1290 def->opcode = OP_NOP;
1291 def->dreg = def->sreg1 = def->sreg2 = -1;
1293 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1294 def->opcode = OP_NOP;
1295 def->dreg = def->sreg1 = def->sreg2 = -1;
1297 } else if (MONO_IS_PHI (def)) {
1299 for (j = def->inst_phi_args [0]; j > 0; j--) {
1300 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1301 add_to_dce_worklist (cfg, info, u, &work_list);
1303 def->opcode = OP_NOP;
1304 def->dreg = def->sreg1 = def->sreg2 = -1;
1307 else if (def->opcode == OP_NOP) {
1310 //mono_print_ins (def);
1318 mono_ssa_strength_reduction (MonoCompile *cfg)
1323 g_assert (cfg->comp_done & MONO_COMP_SSA);
1324 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1325 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1327 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1328 GList *lp = bb->loop_blocks;
1331 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1333 /* we only consider loops with 2 in bblocks */
1334 if (!h->in_count == 2)
1337 for (i = 0; i < cfg->num_varinfo; i++) {
1338 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1340 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1341 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1342 MonoInst *v = info->def->inst_i1;
1345 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1353 #endif /* DISABLE_JIT */