2 * ssa.c: Static single assign form support for the JIT compiler.
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
10 #include <mono/metadata/debug-helpers.h>
11 #include <mono/metadata/mempool.h>
12 #include <mono/metadata/mempool-internals.h>
19 #define USE_ORIGINAL_VARS
20 #define CREATE_PRUNED_SSA
24 #define NEW_PHI(cfg,dest,val) do { \
25 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
26 (dest)->opcode = OP_PHI; \
27 (dest)->inst_c0 = (val); \
28 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
31 #define NEW_ICONST(cfg,dest,val) do { \
32 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
33 (dest)->opcode = OP_ICONST; \
34 (dest)->inst_c0 = (val); \
35 (dest)->type = STACK_I4; \
36 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
45 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
49 for (i = 0; i < bb->out_count; i++) {
50 if (bb->out_bb [i] == target) {
51 bb->out_bb [i] = bb->out_bb [--bb->out_count];
55 for (i = 0; i < target->in_count; i++) {
56 if (target->in_bb [i] == bb) {
57 target->in_bb [i] = target->in_bb [--target->in_count];
65 unlink_unused_bblocks (MonoCompile *cfg)
70 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
72 if (G_UNLIKELY (cfg->verbose_level > 1))
73 printf ("\nUNLINK UNUSED BBLOCKS:\n");
75 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
76 if (!(bb->next_bb->flags & BB_REACHABLE)) {
77 bb->next_bb = bb->next_bb->next_bb;
82 for (i = 1; i < cfg->num_bblocks; i++) {
83 bb = cfg->bblocks [i];
85 if (!(bb->flags & BB_REACHABLE)) {
86 for (j = 0; j < bb->in_count; j++) {
87 unlink_target (bb->in_bb [j], bb);
89 for (j = 0; j < bb->out_count; j++) {
90 unlink_target (bb, bb->out_bb [j]);
92 if (G_UNLIKELY (cfg->verbose_level > 1))
93 printf ("\tUnlinked BB%d\n", bb->block_num);
100 * remove_bb_from_phis:
102 * Remove BB from the PHI statements in TARGET.
105 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
110 for (i = 0; i < target->in_count; i++) {
111 if (target->in_bb [i] == bb) {
115 g_assert (i < target->in_count);
117 for (ins = target->code; ins; ins = ins->next) {
118 if (MONO_IS_PHI (ins)) {
119 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
120 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
121 ins->inst_phi_args [0] --;
129 op_phi_to_move (int opcode)
139 g_assert_not_reached ();
146 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
149 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
151 info = MONO_VARINFO (cfg, var->inst_c0);
155 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
164 * mono_ssa_rename_vars2:
166 * Implement renaming of SSA variables. Also compute def-use information in parallel.
167 * @stack_history points to an area of memory which can be used for storing changes
168 * made to the stack, so they can be reverted later.
171 mono_ssa_rename_vars2 (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)
173 MonoInst *ins, *new_var;
176 int stack_history_len = 0;
178 if (cfg->verbose_level >= 4)
179 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
181 /* First pass: Create new vars */
182 for (ins = bb->code; ins; ins = ins->next) {
183 const char *spec = INS_INFO (ins->opcode);
186 printf ("\tProcessing "); mono_print_ins (ins);
188 if (ins->opcode == OP_NOP)
192 if (spec [MONO_INST_SRC1] != ' ') {
193 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
194 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
195 int idx = var->inst_c0;
197 if (var->opcode != OP_ARG)
198 g_assert (stack [idx]);
199 ins->sreg1 = stack [idx]->dreg;
200 record_use (cfg, stack [idx], bb, ins);
203 record_use (cfg, var, bb, ins);
205 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg1]))
206 ins->sreg1 = lvreg_stack [ins->sreg1];
210 if (spec [MONO_INST_SRC2] != ' ') {
211 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
212 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
213 int idx = var->inst_c0;
215 if (var->opcode != OP_ARG)
216 g_assert (stack [idx]);
218 ins->sreg2 = stack [idx]->dreg;
219 record_use (cfg, stack [idx], bb, ins);
222 record_use (cfg, var, bb, ins);
224 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg2]))
225 ins->sreg2 = lvreg_stack [ins->sreg2];
228 if (MONO_IS_STORE_MEMBASE (ins)) {
229 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
230 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
231 int idx = var->inst_c0;
233 if (var->opcode != OP_ARG)
234 g_assert (stack [idx]);
235 ins->dreg = stack [idx]->dreg;
236 record_use (cfg, stack [idx], bb, ins);
239 record_use (cfg, var, bb, ins);
241 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
242 ins->dreg = lvreg_stack [ins->dreg];
246 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
247 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
250 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
252 g_assert (idx < max_vars);
254 if (var->opcode == OP_ARG)
255 originals_used [idx] = TRUE;
258 g_assert (stack_history_len < stack_history_size);
259 stack_history [stack_history_len].var = stack [idx];
260 stack_history [stack_history_len].idx = idx;
261 stack_history_len ++;
263 if (originals_used [idx]) {
264 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
265 new_var->flags = var->flags;
266 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
268 if (cfg->verbose_level >= 4)
269 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
271 stack [idx] = new_var;
273 ins->dreg = new_var->dreg;
278 originals_used [idx] = TRUE;
281 info = MONO_VARINFO (cfg, var->inst_c0);
285 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
286 /* Perform renaming for local vregs */
287 lvreg_stack [ins->dreg] = mono_alloc_preg (cfg);
288 ins->dreg = lvreg_stack [ins->dreg];
291 lvreg_defined [ins->dreg] = TRUE;
295 printf ("\tAfter processing "); mono_print_ins (ins);
300 /* Rename PHI arguments in succeeding bblocks */
301 for (i = 0; i < bb->out_count; i++) {
302 MonoBasicBlock *n = bb->out_bb [i];
304 for (j = 0; j < n->in_count; j++)
305 if (n->in_bb [j] == bb)
308 for (ins = n->code; ins; ins = ins->next) {
309 if (MONO_IS_PHI (ins)) {
312 new_var = stack [idx];
314 new_var = cfg->varinfo [idx];
316 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
318 ins->inst_phi_args [j + 1] = new_var->dreg;
319 record_use (cfg, new_var, n, ins);
320 if (G_UNLIKELY (cfg->verbose_level >= 4))
321 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
324 /* The phi nodes are at the beginning of the bblock */
330 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
331 mono_ssa_rename_vars2 (cfg, max_vars, (MonoBasicBlock *)tmp->data, originals_used, stack, lvreg_stack, lvreg_defined, stack_history + stack_history_len, stack_history_size - stack_history_len);
336 for (i = stack_history_len - 1; i >= 0; i--) {
337 stack [stack_history [i].idx] = stack_history [i].var;
340 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
344 mono_ssa_compute2 (MonoCompile *cfg)
346 int i, j, idx, bitsize;
348 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
349 MonoInst *ins, **stack;
350 guint8 *buf, *buf_start;
351 RenameInfo *stack_history;
352 int stack_history_size;
354 guint32 *lvreg_stack;
355 gboolean *lvreg_defined;
357 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
359 /* we dont support methods containing exception clauses */
360 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
361 g_assert (!cfg->disable_ssa);
363 if (cfg->verbose_level >= 4)
364 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
366 #ifdef CREATE_PRUNED_SSA
367 /* we need liveness for pruned SSA */
368 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
369 mono_analyze_liveness (cfg);
372 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
374 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
375 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
377 for (i = 0; i < cfg->num_varinfo; ++i) {
378 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
381 /* implicit reference at start */
382 mono_bitset_set_fast (vinfo [i].def_in, 0);
385 for (i = 0; i < cfg->num_bblocks; ++i) {
386 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
387 if (ins->opcode == OP_NOP)
390 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
391 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
396 /* insert phi functions */
397 for (i = 0; i < cfg->num_varinfo; ++i) {
398 MonoInst *var = cfg->varinfo [i];
400 #if SIZEOF_VOID_P == 4
401 if (var->type == STACK_I8)
404 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
407 /* Most variables have only one definition */
408 if (mono_bitset_count (vinfo [i].def_in) <= 1)
411 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
413 if (cfg->verbose_level >= 4) {
414 if (mono_bitset_count (set) > 0) {
415 printf ("\tR%d needs PHI functions in ", var->dreg);
416 mono_blockset_print (cfg, set, "", -1);
420 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
421 MonoBasicBlock *bb = cfg->bblocks [idx];
423 /* fixme: create pruned SSA? we would need liveness information for that */
425 if (bb == cfg->bb_exit)
428 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
429 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
433 NEW_PHI (cfg, ins, i);
441 ins->opcode = OP_PHI;
444 ins->opcode = OP_FPHI;
447 ins->opcode = OP_VPHI;
448 ins->klass = var->klass;
452 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
453 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
456 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
457 ins->inst_phi_args [j + 1] = -1;
459 ins->dreg = cfg->varinfo [i]->dreg;
461 mono_bblock_insert_before_ins (bb, bb->code, ins);
464 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
475 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
476 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
478 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
479 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
480 stack_history_size = 10240;
481 stack_history = g_new (RenameInfo, stack_history_size);
482 originals = g_new0 (gboolean, cfg->num_varinfo);
483 mono_ssa_rename_vars2 (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
484 g_free (stack_history);
487 if (cfg->verbose_level >= 4)
488 printf ("\nEND COMPUTE SSA.\n\n");
490 cfg->comp_done |= MONO_COMP_SSA;
494 mono_ssa_remove2 (MonoCompile *cfg)
496 MonoInst *ins, *var, *move;
499 g_assert (cfg->comp_done & MONO_COMP_SSA);
501 for (i = 0; i < cfg->num_bblocks; ++i) {
502 MonoBasicBlock *bb = cfg->bblocks [i];
504 if (cfg->verbose_level >= 4)
505 printf ("\nREMOVE SSA %d:\n", bb->block_num);
507 for (ins = bb->code; ins; ins = ins->next) {
508 if (MONO_IS_PHI (ins)) {
509 g_assert (ins->inst_phi_args [0] == bb->in_count);
510 var = get_vreg_to_inst (cfg, ins->dreg);
512 /* Check for PHI nodes where all the inputs are the same */
513 first = ins->inst_phi_args [1];
515 for (j = 1; j < bb->in_count; ++j)
516 if (first != ins->inst_phi_args [j + 1])
519 if ((bb->in_count > 1) && (j == bb->in_count)) {
520 ins->opcode = op_phi_to_move (ins->opcode);
521 if (ins->opcode == OP_VMOVE)
522 g_assert (ins->klass);
525 for (j = 0; j < bb->in_count; j++) {
526 MonoBasicBlock *pred = bb->in_bb [j];
527 int sreg = ins->inst_phi_args [j + 1];
529 if (cfg->verbose_level >= 4)
530 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
531 if (var->dreg != sreg) {
532 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
533 if (move->opcode == OP_VMOVE) {
534 g_assert (ins->klass);
535 move->klass = ins->klass;
537 move->dreg = var->dreg;
539 mono_add_ins_to_end (pred, move);
543 ins->opcode = OP_NOP;
550 if (cfg->verbose_level >= 4) {
551 for (i = 0; i < cfg->num_bblocks; ++i) {
552 MonoBasicBlock *bb = cfg->bblocks [i];
554 mono_print_bb (bb, "AFTER REMOVE SSA:");
559 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
560 * the variables if possible. Since the newly introduced SSA variables don't
561 * have overlapping live ranges (because we don't do agressive optimization), we
562 * can coalesce them into the original variable.
565 for (i = 0; i < cfg->num_bblocks; ++i) {
566 MonoBasicBlock *bb = cfg->bblocks [i];
568 for (ins = bb->code; ins; ins = ins->next) {
569 const char *spec = INS_INFO (ins->opcode);
571 if (ins->opcode == OP_NOP)
574 if (spec [MONO_INST_DEST] != ' ') {
575 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
578 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
581 * The third condition avoids coalescing with variables eliminated
584 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
585 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
586 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
591 if (spec [MONO_INST_SRC1] != ' ') {
592 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
595 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
597 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
598 printf ("COALESCE: R%d -> R%d\n", ins->sreg1, cfg->varinfo [vmv->reg]->dreg);
599 ins->sreg1 = cfg->varinfo [vmv->reg]->dreg;
604 if (spec [MONO_INST_SRC2] != ' ') {
605 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
608 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
610 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
611 printf ("COALESCE: R%d -> R%d\n", ins->sreg2, cfg->varinfo [vmv->reg]->dreg);
612 ins->sreg2 = cfg->varinfo [vmv->reg]->dreg;
620 for (i = 0; i < cfg->num_varinfo; ++i) {
621 MONO_VARINFO (cfg, i)->reg = -1;
624 if (cfg->comp_done & MONO_COMP_REACHABILITY)
625 unlink_unused_bblocks (cfg);
627 cfg->comp_done &= ~MONO_COMP_LIVENESS;
629 cfg->comp_done &= ~MONO_COMP_SSA;
633 mono_ssa_create_def_use (MonoCompile *cfg)
639 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
641 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
642 for (ins = bb->code; ins; ins = ins->next) {
643 const char *spec = INS_INFO (ins->opcode);
646 if (ins->opcode == OP_NOP)
650 if (spec [MONO_INST_SRC1] != ' ') {
651 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
652 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
653 record_use (cfg, var, bb, ins);
657 if (spec [MONO_INST_SRC2] != ' ') {
658 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
659 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
660 record_use (cfg, var, bb, ins);
663 if (MONO_IS_STORE_MEMBASE (ins)) {
664 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
665 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
666 record_use (cfg, var, bb, ins);
669 if (MONO_IS_PHI (ins)) {
670 for (i = ins->inst_phi_args [0]; i > 0; i--) {
671 g_assert (ins->inst_phi_args [i] != -1);
672 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
677 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
678 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
680 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
681 info = MONO_VARINFO (cfg, var->inst_c0);
689 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
693 mono_ssa_copyprop (MonoCompile *cfg)
698 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
700 for (index = 0; index < cfg->num_varinfo; ++index) {
701 MonoInst *var = cfg->varinfo [index];
702 MonoMethodVar *info = MONO_VARINFO (cfg, index);
704 if (info->def && (MONO_IS_MOVE (info->def))) {
705 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
707 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))) {
708 /* Rewrite all uses of var to be uses of var2 */
709 int dreg = var->dreg;
710 int sreg1 = var2->dreg;
715 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
716 MonoInst *ins = u->inst;
717 GList *next = l->next;
719 spec = INS_INFO (ins->opcode);
721 if (spec [MONO_INST_SRC1] != ' ' && ins->sreg1 == dreg) {
723 } else if (spec [MONO_INST_SRC2] != ' ' && ins->sreg2 == dreg) {
725 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
727 } else if (MONO_IS_PHI (ins)) {
728 for (i = ins->inst_phi_args [0]; i > 0; i--) {
729 int sreg = ins->inst_phi_args [i];
730 if (sreg == var->dreg)
734 ins->inst_phi_args [i] = sreg1;
737 g_assert_not_reached ();
739 record_use (cfg, var2, u->bb, ins);
749 if (cfg->verbose_level >= 4) {
752 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
753 mono_print_bb (bb, "AFTER SSA COPYPROP");
758 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
760 MonoInst *arg0, *arg1, *c0;
762 gboolean const_args = FALSE;
763 const char *spec = INS_INFO (ins->opcode);
765 /* Short-circuit this */
766 if (ins->opcode == OP_ICONST) {
771 if (ins->opcode == OP_NOP)
775 if (spec [MONO_INST_SRC1] != ' ') {
776 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
779 arg0 = carray [ins->sreg1];
782 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
783 r1 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
789 if (spec [MONO_INST_SRC2] != ' ') {
790 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
793 arg1 = carray [ins->sreg2];
796 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
797 r2 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
804 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
806 const_args = (r1 == 1) && (r2 == 1);
808 else if (spec [MONO_INST_SRC1] != ' ') {
810 const_args = (r1 == 1);
814 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
816 *res = carray [ins->dreg];
819 c0 = mono_constant_fold_ins2 (cfg, ins, arg0, arg1, FALSE);
821 if (G_UNLIKELY (cfg->verbose_level > 1)) {
822 printf ("\t cfold -> ");
829 /* Can't cfold this ins */
833 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
835 if ((r1 == 2) || (r2 == 2))
840 else if (spec [MONO_INST_SRC1] != ' ') {
852 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
854 if (info->cpstate >= state)
857 info->cpstate = state;
859 if (G_UNLIKELY (cfg->verbose_level > 1))
860 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
865 carray [cfg->varinfo [info->idx]->dreg] = c0;
867 if (!g_list_find (*cvars, info)) {
868 *cvars = g_list_prepend (*cvars, info);
873 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
875 if (G_UNLIKELY (cfg->verbose_level > 1))
876 printf ("\tAdd BB%d to worklist\n", bb->block_num);
878 if (!(bb->flags & BB_REACHABLE)) {
879 bb->flags |= BB_REACHABLE;
880 *bblist = g_list_prepend (*bblist, bb);
885 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
887 const char *spec = INS_INFO (ins->opcode);
889 if (ins->opcode == OP_NOP)
892 if (cfg->verbose_level > 1)
893 mono_print_ins (ins);
895 /* FIXME: Support longs/floats */
896 /* FIXME: Work on vregs as well */
898 if (MONO_IS_PHI (ins)) {
899 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
903 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
904 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
905 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
906 MonoInst *src = mv->def;
908 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
911 if (!mv->def || !src || mv->cpstate == 2) {
912 change_varstate (cfg, cvars, info, 2, NULL, carray);
916 if (mv->cpstate == 0)
919 g_assert (carray [var->dreg]);
922 c0 = carray [var->dreg];
925 if (c0->opcode != OP_ICONST) {
926 change_varstate (cfg, cvars, info, 2, NULL, carray);
930 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
931 change_varstate (cfg, cvars, info, 2, NULL, carray);
936 if (c0 && info->cpstate < 1) {
937 change_varstate (cfg, cvars, info, 1, c0, carray);
939 g_assert (c0->opcode == OP_ICONST);
942 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
946 if (spec [MONO_INST_DEST] != ' ')
947 var = get_vreg_to_inst (cfg, ins->dreg);
952 state = evaluate_ins (cfg, ins, &c0, carray);
954 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
955 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
957 if (info->cpstate < 2) {
959 change_varstate (cfg, cvars, info, 1, c0, carray);
961 change_varstate (cfg, cvars, info, 2, NULL, carray);
964 else if (!var && (ins->dreg != -1)) {
966 * We don't record def-use information for local vregs since it would be
967 * expensive. Instead, we depend on the fact that all uses of the vreg are in
968 * the same bblock, so they will be examined after the definition.
969 * FIXME: This isn't true if the ins is visited through an SSA edge.
972 carray [ins->dreg] = c0;
974 if (carray [ins->dreg]) {
976 * The state of the vreg changed from constant to non-constant
977 * -> need to rescan the whole bblock.
979 carray [ins->dreg] = NULL;
980 /* FIXME: Speed this up */
982 if (!g_list_find (*bblist, bb))
983 *bblist = g_list_prepend (*bblist, bb);
988 if (ins->opcode == OP_JUMP_TABLE) {
990 MonoJumpInfoBBTable *table = ins->inst_p0;
992 if (ins->next->opcode != OP_PADD) {
993 /* The PADD was optimized away */
994 /* FIXME: handle this as well */
995 for (i = 0; i < table->table_size; i++)
996 if (table->table [i])
997 add_cprop_bb (cfg, table->table [i], bblist);
1001 g_assert (ins->next->opcode == OP_PADD);
1002 g_assert (ins->next->sreg1 == ins->dreg);
1004 if (carray [ins->next->sreg2]) {
1005 #if SIZEOF_VOID_P == 8
1006 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1008 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1010 if ((idx < 0) || (idx >= table->table_size))
1011 /* Out-of-range, no branch is executed */
1014 if (table->table [idx])
1015 add_cprop_bb (cfg, table->table [idx], bblist);
1018 for (i = 0; i < table->table_size; i++)
1019 if (table->table [i])
1020 add_cprop_bb (cfg, table->table [i], bblist);
1024 if (ins->opcode == OP_SWITCH) {
1026 MonoJumpInfoBBTable *table = ins->inst_p0;
1028 for (i = 0; i < table->table_size; i++)
1029 if (table->table [i])
1030 add_cprop_bb (cfg, table->table [i], bblist);
1033 /* Handle COMPARE+BRCOND pairs */
1034 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
1036 g_assert (c0->opcode == OP_ICONST);
1039 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
1041 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1044 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1047 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1049 } else if (ins->opcode == OP_BR) {
1050 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1051 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1052 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1053 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1054 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1055 if (ins->inst_false_bb)
1056 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1058 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1059 if (ins->inst_false_bb)
1060 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1068 * Replace INS with its constant value, if it exists
1071 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1073 const char *spec = INS_INFO (ins->opcode);
1076 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1077 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1078 /* Perform constant folding */
1080 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1081 ins->opcode = OP_ICONST;
1082 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1083 ins->sreg1 = ins->sreg2 = -1;
1085 else if ((spec [MONO_INST_SRC2] != ' ') && carray [ins->sreg2]) {
1086 /* Perform op->op_imm conversion */
1087 opcode2 = mono_op_to_op_imm (ins->opcode);
1088 if (opcode2 != -1) {
1089 ins->opcode = opcode2;
1090 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1093 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1094 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1098 if (ins->opcode == OP_JUMP_TABLE) {
1100 MonoJumpInfoBBTable *table = ins->inst_p0;
1102 if (ins->next->opcode != OP_PADD) {
1103 /* The PADD was optimized away */
1104 /* FIXME: handle this as well */
1108 g_assert (ins->next->opcode == OP_PADD);
1109 g_assert (ins->next->sreg1 == ins->dreg);
1110 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1112 if (carray [ins->next->sreg2]) {
1113 /* Convert to a simple branch */
1114 #if SIZEOF_VOID_P == 8
1115 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1117 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1120 if (!((idx >= 0) && (idx < table->table_size))) {
1121 /* Out of range, eliminate the whole switch */
1122 for (i = 0; i < table->table_size; ++i) {
1123 remove_bb_from_phis (cfg, bb, table->table [i]);
1124 mono_unlink_bblock (cfg, bb, table->table [i]);
1128 NULLIFY_INS (ins->next);
1129 NULLIFY_INS (ins->next->next);
1130 NULLIFY_INS (ins->next->next->next);
1135 if (ins->next->next->next->opcode != OP_BR_REG) {
1136 /* A one-way switch which got optimized away */
1137 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1138 printf ("\tNo cfold on ");
1139 mono_print_ins (ins);
1144 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1145 printf ("\tcfold on ");
1146 mono_print_ins (ins);
1149 /* Unlink target bblocks */
1150 for (i = 0; i < table->table_size; ++i) {
1152 remove_bb_from_phis (cfg, bb, table->table [i]);
1153 mono_unlink_bblock (cfg, bb, table->table [i]);
1157 /* Change the OP_BR_REG to a simple branch */
1158 ins->next->next->next->opcode = OP_BR;
1159 ins->next->next->next->inst_target_bb = table->table [idx];
1160 ins->next->next->next->sreg1 = -1;
1162 /* Nullify the other instructions */
1164 NULLIFY_INS (ins->next);
1165 NULLIFY_INS (ins->next->next);
1169 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1170 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1171 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1172 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1173 ins->opcode = OP_BR;
1174 ins->inst_target_bb = ins->inst_true_bb;
1175 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1176 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1177 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1178 ins->opcode = OP_BR;
1179 ins->inst_target_bb = ins->inst_false_bb;
1185 mono_ssa_cprop2 (MonoCompile *cfg)
1189 GList *bblock_list, *cvars;
1192 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1194 carray = g_new0 (MonoInst*, cfg->next_vreg);
1196 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1197 mono_ssa_create_def_use (cfg);
1199 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1200 cfg->bb_entry->flags |= BB_REACHABLE;
1202 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1204 for (i = 0; i < cfg->num_varinfo; i++) {
1205 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1212 while (bblock_list) {
1215 bb = (MonoBasicBlock *)bblock_list->data;
1217 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1219 g_assert (bb->flags & BB_REACHABLE);
1221 if (bb->out_count == 1) {
1222 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1223 bb->out_bb [0]->flags |= BB_REACHABLE;
1224 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1228 if (cfg->verbose_level > 1)
1229 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1231 for (inst = bb->code; inst; inst = inst->next) {
1232 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1236 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1237 cvars = g_list_delete_link (cvars, cvars);
1239 for (tmp = info->uses; tmp; tmp = tmp->next) {
1240 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1241 if (!(ui->bb->flags & BB_REACHABLE))
1243 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1248 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1250 for (inst = bb->code; inst; inst = inst->next) {
1251 fold_ins (cfg, bb, inst, carray);
1257 cfg->comp_done |= MONO_COMP_REACHABILITY;
1259 /* fixme: we should update usage infos during cprop, instead of computing it again */
1260 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1261 for (i = 0; i < cfg->num_varinfo; i++) {
1262 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1269 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1273 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1275 for (tmp = use->uses; tmp; tmp = tmp->next) {
1276 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1277 if (ui->inst == var->def) {
1278 /* from the mempool */
1279 use->uses = g_list_remove_link (use->uses, tmp);
1286 mono_ssa_deadce2 (MonoCompile *cfg)
1291 g_assert (cfg->comp_done & MONO_COMP_SSA);
1293 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1295 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1296 mono_ssa_create_def_use (cfg);
1298 mono_ssa_copyprop (cfg);
1301 for (i = 0; i < cfg->num_varinfo; i++) {
1302 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1303 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1307 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1308 work_list = g_list_remove_link (work_list, work_list);
1311 * The second part of the condition happens often when PHI nodes have their dreg
1312 * as one of their arguments due to the fact that we use the original vars.
1314 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1315 MonoInst *def = info->def;
1317 /* Eliminating FMOVE could screw up the fp stack */
1318 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1319 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1320 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1321 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1322 def->opcode = OP_NOP;
1323 def->dreg = def->sreg1 = def->sreg2 = -1;
1325 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || (def->opcode == OP_VZERO)) {
1326 def->opcode = OP_NOP;
1327 def->dreg = def->sreg1 = def->sreg2 = -1;
1329 } else if (MONO_IS_PHI (def)) {
1331 for (j = def->inst_phi_args [0]; j > 0; j--) {
1332 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1333 add_to_dce_worklist (cfg, info, u, &work_list);
1335 def->opcode = OP_NOP;
1336 def->dreg = def->sreg1 = def->sreg2 = -1;
1339 else if (def->opcode == OP_NOP) {
1342 //mono_print_ins (def);
1350 mono_ssa_strength_reduction (MonoCompile *cfg)
1355 g_assert (cfg->comp_done & MONO_COMP_SSA);
1356 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1357 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1359 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1360 GList *lp = bb->loop_blocks;
1363 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1365 /* we only consider loops with 2 in bblocks */
1366 if (!h->in_count == 2)
1369 for (i = 0; i < cfg->num_varinfo; i++) {
1370 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1372 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1373 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1374 MonoInst *v = info->def->inst_i1;
1377 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1385 #endif /* DISABLE_JIT */