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>
17 #define USE_ORIGINAL_VARS
18 #define CREATE_PRUNED_SSA
22 #define NEW_PHI(cfg,dest,val) do { \
23 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
24 (dest)->opcode = OP_PHI; \
25 (dest)->inst_c0 = (val); \
26 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
29 #define NEW_ICONST(cfg,dest,val) do { \
30 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
31 (dest)->opcode = OP_ICONST; \
32 (dest)->inst_c0 = (val); \
33 (dest)->type = STACK_I4; \
34 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
43 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
47 for (i = 0; i < bb->out_count; i++) {
48 if (bb->out_bb [i] == target) {
49 bb->out_bb [i] = bb->out_bb [--bb->out_count];
53 for (i = 0; i < target->in_count; i++) {
54 if (target->in_bb [i] == bb) {
55 target->in_bb [i] = target->in_bb [--target->in_count];
63 unlink_unused_bblocks (MonoCompile *cfg)
68 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
70 if (G_UNLIKELY (cfg->verbose_level > 1))
71 printf ("\nUNLINK UNUSED BBLOCKS:\n");
73 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
74 if (!(bb->next_bb->flags & BB_REACHABLE)) {
75 bb->next_bb = bb->next_bb->next_bb;
80 for (i = 1; i < cfg->num_bblocks; i++) {
81 bb = cfg->bblocks [i];
83 if (!(bb->flags & BB_REACHABLE)) {
84 for (j = 0; j < bb->in_count; j++) {
85 unlink_target (bb->in_bb [j], bb);
87 for (j = 0; j < bb->out_count; j++) {
88 unlink_target (bb, bb->out_bb [j]);
90 if (G_UNLIKELY (cfg->verbose_level > 1))
91 printf ("\tUnlinked BB%d\n", bb->block_num);
98 * remove_bb_from_phis:
100 * Remove BB from the PHI statements in TARGET.
103 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
108 for (i = 0; i < target->in_count; i++) {
109 if (target->in_bb [i] == bb) {
113 g_assert (i < target->in_count);
115 for (ins = target->code; ins; ins = ins->next) {
116 if (MONO_IS_PHI (ins)) {
117 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
118 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
119 ins->inst_phi_args [0] --;
127 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_vars2:
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_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)
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);
184 printf ("\tProcessing "); mono_print_ins (ins);
186 if (ins->opcode == OP_NOP)
190 if (spec [MONO_INST_SRC1] != ' ') {
191 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
192 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
193 int idx = var->inst_c0;
195 if (var->opcode != OP_ARG)
196 g_assert (stack [idx]);
197 ins->sreg1 = stack [idx]->dreg;
198 record_use (cfg, stack [idx], bb, ins);
201 record_use (cfg, var, bb, ins);
203 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg1]))
204 ins->sreg1 = lvreg_stack [ins->sreg1];
208 if (spec [MONO_INST_SRC2] != ' ') {
209 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
210 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
211 int idx = var->inst_c0;
213 if (var->opcode != OP_ARG)
214 g_assert (stack [idx]);
216 ins->sreg2 = stack [idx]->dreg;
217 record_use (cfg, stack [idx], bb, ins);
220 record_use (cfg, var, bb, ins);
222 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg2]))
223 ins->sreg2 = lvreg_stack [ins->sreg2];
226 if (MONO_IS_STORE_MEMBASE (ins)) {
227 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
228 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
229 int idx = var->inst_c0;
231 if (var->opcode != OP_ARG)
232 g_assert (stack [idx]);
233 ins->dreg = stack [idx]->dreg;
234 record_use (cfg, stack [idx], bb, ins);
237 record_use (cfg, var, bb, ins);
239 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
240 ins->dreg = lvreg_stack [ins->dreg];
244 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
245 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
248 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
250 g_assert (idx < max_vars);
252 if (var->opcode == OP_ARG)
253 originals_used [idx] = TRUE;
256 g_assert (stack_history_len < stack_history_size);
257 stack_history [stack_history_len].var = stack [idx];
258 stack_history [stack_history_len].idx = idx;
259 stack_history_len ++;
261 if (originals_used [idx]) {
262 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
263 new_var->flags = var->flags;
264 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
266 if (cfg->verbose_level >= 4)
267 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
269 stack [idx] = new_var;
271 ins->dreg = new_var->dreg;
276 originals_used [idx] = TRUE;
279 info = MONO_VARINFO (cfg, var->inst_c0);
283 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
284 /* Perform renaming for local vregs */
285 lvreg_stack [ins->dreg] = mono_alloc_preg (cfg);
286 ins->dreg = lvreg_stack [ins->dreg];
289 lvreg_defined [ins->dreg] = TRUE;
293 printf ("\tAfter processing "); mono_print_ins (ins);
298 /* Rename PHI arguments in succeeding bblocks */
299 for (i = 0; i < bb->out_count; i++) {
300 MonoBasicBlock *n = bb->out_bb [i];
302 for (j = 0; j < n->in_count; j++)
303 if (n->in_bb [j] == bb)
306 for (ins = n->code; ins; ins = ins->next) {
307 if (MONO_IS_PHI (ins)) {
310 new_var = stack [idx];
312 new_var = cfg->varinfo [idx];
314 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
316 ins->inst_phi_args [j + 1] = new_var->dreg;
317 record_use (cfg, new_var, n, ins);
318 if (G_UNLIKELY (cfg->verbose_level >= 4))
319 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
322 /* The phi nodes are at the beginning of the bblock */
328 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
329 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);
334 for (i = stack_history_len - 1; i >= 0; i--) {
335 stack [stack_history [i].idx] = stack_history [i].var;
338 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
342 mono_ssa_compute2 (MonoCompile *cfg)
344 int i, j, idx, bitsize;
346 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
347 MonoInst *ins, **stack;
348 guint8 *buf, *buf_start;
349 RenameInfo *stack_history;
350 int stack_history_size;
352 guint32 *lvreg_stack;
353 gboolean *lvreg_defined;
355 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
357 /* we dont support methods containing exception clauses */
358 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
359 g_assert (!cfg->disable_ssa);
361 if (cfg->verbose_level >= 4)
362 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
364 #ifdef CREATE_PRUNED_SSA
365 /* we need liveness for pruned SSA */
366 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
367 mono_analyze_liveness (cfg);
370 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
372 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
373 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
375 for (i = 0; i < cfg->num_varinfo; ++i) {
376 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
379 /* implicit reference at start */
380 mono_bitset_set_fast (vinfo [i].def_in, 0);
383 for (i = 0; i < cfg->num_bblocks; ++i) {
384 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
385 if (ins->opcode == OP_NOP)
388 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
389 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
394 /* insert phi functions */
395 for (i = 0; i < cfg->num_varinfo; ++i) {
396 MonoInst *var = cfg->varinfo [i];
398 #if SIZEOF_VOID_P == 4
399 if (var->type == STACK_I8)
402 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
405 /* Most variables have only one definition */
406 if (mono_bitset_count (vinfo [i].def_in) <= 1)
409 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
411 if (cfg->verbose_level >= 4) {
412 if (mono_bitset_count (set) > 0) {
413 printf ("\tR%d needs PHI functions in ", var->dreg);
414 mono_blockset_print (cfg, set, "", -1);
418 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
419 MonoBasicBlock *bb = cfg->bblocks [idx];
421 /* fixme: create pruned SSA? we would need liveness information for that */
423 if (bb == cfg->bb_exit)
426 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
427 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
431 NEW_PHI (cfg, ins, i);
439 ins->opcode = OP_PHI;
442 ins->opcode = OP_FPHI;
445 ins->opcode = OP_VPHI;
446 ins->klass = var->klass;
450 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
451 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
454 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
455 ins->inst_phi_args [j + 1] = -1;
457 ins->dreg = cfg->varinfo [i]->dreg;
459 mono_bblock_insert_before_ins (bb, bb->code, ins);
462 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
473 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
474 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
476 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
477 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
478 stack_history_size = 10240;
479 stack_history = g_new (RenameInfo, stack_history_size);
480 originals = g_new0 (gboolean, cfg->num_varinfo);
481 mono_ssa_rename_vars2 (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
482 g_free (stack_history);
485 if (cfg->verbose_level >= 4)
486 printf ("\nEND COMPUTE SSA.\n\n");
488 cfg->comp_done |= MONO_COMP_SSA;
492 mono_ssa_remove2 (MonoCompile *cfg)
494 MonoInst *ins, *var, *move;
497 g_assert (cfg->comp_done & MONO_COMP_SSA);
499 for (i = 0; i < cfg->num_bblocks; ++i) {
500 MonoBasicBlock *bb = cfg->bblocks [i];
502 if (cfg->verbose_level >= 4)
503 printf ("\nREMOVE SSA %d:\n", bb->block_num);
505 for (ins = bb->code; ins; ins = ins->next) {
506 if (MONO_IS_PHI (ins)) {
507 g_assert (ins->inst_phi_args [0] == bb->in_count);
508 var = get_vreg_to_inst (cfg, ins->dreg);
510 /* Check for PHI nodes where all the inputs are the same */
511 first = ins->inst_phi_args [1];
513 for (j = 1; j < bb->in_count; ++j)
514 if (first != ins->inst_phi_args [j + 1])
517 if ((bb->in_count > 1) && (j == bb->in_count)) {
518 ins->opcode = op_phi_to_move (ins->opcode);
519 if (ins->opcode == OP_VMOVE)
520 g_assert (ins->klass);
523 for (j = 0; j < bb->in_count; j++) {
524 MonoBasicBlock *pred = bb->in_bb [j];
525 int sreg = ins->inst_phi_args [j + 1];
527 if (cfg->verbose_level >= 4)
528 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
529 if (var->dreg != sreg) {
530 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
531 if (move->opcode == OP_VMOVE) {
532 g_assert (ins->klass);
533 move->klass = ins->klass;
535 move->dreg = var->dreg;
537 mono_add_ins_to_end (pred, move);
541 ins->opcode = OP_NOP;
548 if (cfg->verbose_level >= 4) {
549 for (i = 0; i < cfg->num_bblocks; ++i) {
550 MonoBasicBlock *bb = cfg->bblocks [i];
552 mono_print_bb (bb, "AFTER REMOVE SSA:");
557 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
558 * the variables if possible. Since the newly introduced SSA variables don't
559 * have overlapping live ranges (because we don't do agressive optimization), we
560 * can coalesce them into the original variable.
563 for (i = 0; i < cfg->num_bblocks; ++i) {
564 MonoBasicBlock *bb = cfg->bblocks [i];
566 for (ins = bb->code; ins; ins = ins->next) {
567 const char *spec = INS_INFO (ins->opcode);
569 if (ins->opcode == OP_NOP)
572 if (spec [MONO_INST_DEST] != ' ') {
573 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
576 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
579 * The third condition avoids coalescing with variables eliminated
582 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
583 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
584 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
589 if (spec [MONO_INST_SRC1] != ' ') {
590 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
593 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
595 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
596 printf ("COALESCE: R%d -> R%d\n", ins->sreg1, cfg->varinfo [vmv->reg]->dreg);
597 ins->sreg1 = cfg->varinfo [vmv->reg]->dreg;
602 if (spec [MONO_INST_SRC2] != ' ') {
603 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
606 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
608 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
609 printf ("COALESCE: R%d -> R%d\n", ins->sreg2, cfg->varinfo [vmv->reg]->dreg);
610 ins->sreg2 = cfg->varinfo [vmv->reg]->dreg;
618 for (i = 0; i < cfg->num_varinfo; ++i) {
619 MONO_VARINFO (cfg, i)->reg = -1;
622 if (cfg->comp_done & MONO_COMP_REACHABILITY)
623 unlink_unused_bblocks (cfg);
625 cfg->comp_done &= ~MONO_COMP_LIVENESS;
627 cfg->comp_done &= ~MONO_COMP_SSA;
631 mono_ssa_create_def_use (MonoCompile *cfg)
637 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
639 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
640 for (ins = bb->code; ins; ins = ins->next) {
641 const char *spec = INS_INFO (ins->opcode);
644 if (ins->opcode == OP_NOP)
648 if (spec [MONO_INST_SRC1] != ' ') {
649 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
650 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
651 record_use (cfg, var, bb, ins);
655 if (spec [MONO_INST_SRC2] != ' ') {
656 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
657 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
658 record_use (cfg, var, bb, ins);
661 if (MONO_IS_STORE_MEMBASE (ins)) {
662 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
663 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
664 record_use (cfg, var, bb, ins);
667 if (MONO_IS_PHI (ins)) {
668 for (i = ins->inst_phi_args [0]; i > 0; i--) {
669 g_assert (ins->inst_phi_args [i] != -1);
670 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
675 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
676 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
678 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
679 info = MONO_VARINFO (cfg, var->inst_c0);
687 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
691 mono_ssa_copyprop (MonoCompile *cfg)
696 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
698 for (index = 0; index < cfg->num_varinfo; ++index) {
699 MonoInst *var = cfg->varinfo [index];
700 MonoMethodVar *info = MONO_VARINFO (cfg, index);
702 if (info->def && (MONO_IS_MOVE (info->def))) {
703 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
705 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))) {
706 /* Rewrite all uses of var to be uses of var2 */
707 int dreg = var->dreg;
708 int sreg1 = var2->dreg;
713 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
714 MonoInst *ins = u->inst;
715 GList *next = l->next;
717 spec = INS_INFO (ins->opcode);
719 if (spec [MONO_INST_SRC1] != ' ' && ins->sreg1 == dreg) {
721 } else if (spec [MONO_INST_SRC2] != ' ' && ins->sreg2 == dreg) {
723 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
725 } else if (MONO_IS_PHI (ins)) {
726 for (i = ins->inst_phi_args [0]; i > 0; i--) {
727 int sreg = ins->inst_phi_args [i];
728 if (sreg == var->dreg)
732 ins->inst_phi_args [i] = sreg1;
735 g_assert_not_reached ();
737 record_use (cfg, var2, u->bb, ins);
747 if (cfg->verbose_level >= 4) {
750 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
751 mono_print_bb (bb, "AFTER SSA COPYPROP");
756 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
758 MonoInst *arg0, *arg1, *c0;
760 gboolean const_args = FALSE;
761 const char *spec = INS_INFO (ins->opcode);
763 /* Short-circuit this */
764 if (ins->opcode == OP_ICONST) {
769 if (ins->opcode == OP_NOP)
773 if (spec [MONO_INST_SRC1] != ' ') {
774 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
777 arg0 = carray [ins->sreg1];
780 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
781 r1 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
787 if (spec [MONO_INST_SRC2] != ' ') {
788 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
791 arg1 = carray [ins->sreg2];
794 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
795 r2 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
802 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
804 const_args = (r1 == 1) && (r2 == 1);
806 else if (spec [MONO_INST_SRC1] != ' ') {
808 const_args = (r1 == 1);
812 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
814 *res = carray [ins->dreg];
817 c0 = mono_constant_fold_ins2 (cfg, ins, arg0, arg1, FALSE);
819 if (G_UNLIKELY (cfg->verbose_level > 1)) {
820 printf ("\t cfold -> ");
827 /* Can't cfold this ins */
831 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
833 if ((r1 == 2) || (r2 == 2))
838 else if (spec [MONO_INST_SRC1] != ' ') {
850 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
852 if (info->cpstate >= state)
855 info->cpstate = state;
857 if (G_UNLIKELY (cfg->verbose_level > 1))
858 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
863 carray [cfg->varinfo [info->idx]->dreg] = c0;
865 if (!g_list_find (*cvars, info)) {
866 *cvars = g_list_prepend (*cvars, info);
871 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
873 if (G_UNLIKELY (cfg->verbose_level > 1))
874 printf ("\tAdd BB%d to worklist\n", bb->block_num);
876 if (!(bb->flags & BB_REACHABLE)) {
877 bb->flags |= BB_REACHABLE;
878 *bblist = g_list_prepend (*bblist, bb);
883 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
885 const char *spec = INS_INFO (ins->opcode);
887 if (ins->opcode == OP_NOP)
890 if (cfg->verbose_level > 1)
891 mono_print_ins (ins);
893 /* FIXME: Support longs/floats */
894 /* FIXME: Work on vregs as well */
896 if (MONO_IS_PHI (ins)) {
897 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
901 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
902 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
903 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
904 MonoInst *src = mv->def;
906 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
909 if (!mv->def || !src || mv->cpstate == 2) {
910 change_varstate (cfg, cvars, info, 2, NULL, carray);
914 if (mv->cpstate == 0)
917 g_assert (carray [var->dreg]);
920 c0 = carray [var->dreg];
923 if (c0->opcode != OP_ICONST) {
924 change_varstate (cfg, cvars, info, 2, NULL, carray);
928 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
929 change_varstate (cfg, cvars, info, 2, NULL, carray);
934 if (c0 && info->cpstate < 1) {
935 change_varstate (cfg, cvars, info, 1, c0, carray);
937 g_assert (c0->opcode == OP_ICONST);
940 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
944 if (spec [MONO_INST_DEST] != ' ')
945 var = get_vreg_to_inst (cfg, ins->dreg);
950 state = evaluate_ins (cfg, ins, &c0, carray);
952 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
953 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
955 if (info->cpstate < 2) {
957 change_varstate (cfg, cvars, info, 1, c0, carray);
959 change_varstate (cfg, cvars, info, 2, NULL, carray);
962 else if (!var && (ins->dreg != -1)) {
964 * We don't record def-use information for local vregs since it would be
965 * expensive. Instead, we depend on the fact that all uses of the vreg are in
966 * the same bblock, so they will be examined after the definition.
967 * FIXME: This isn't true if the ins is visited through an SSA edge.
970 carray [ins->dreg] = c0;
972 if (carray [ins->dreg]) {
974 * The state of the vreg changed from constant to non-constant
975 * -> need to rescan the whole bblock.
977 carray [ins->dreg] = NULL;
978 /* FIXME: Speed this up */
980 if (!g_list_find (*bblist, bb))
981 *bblist = g_list_prepend (*bblist, bb);
986 if (ins->opcode == OP_JUMP_TABLE) {
988 MonoJumpInfoBBTable *table = ins->inst_p0;
990 if (ins->next->opcode != OP_PADD) {
991 /* The PADD was optimized away */
992 /* FIXME: handle this as well */
993 for (i = 0; i < table->table_size; i++)
994 if (table->table [i])
995 add_cprop_bb (cfg, table->table [i], bblist);
999 g_assert (ins->next->opcode == OP_PADD);
1000 g_assert (ins->next->sreg1 == ins->dreg);
1002 if (carray [ins->next->sreg2]) {
1003 #if SIZEOF_VOID_P == 8
1004 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1006 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1008 if ((idx < 0) || (idx >= table->table_size))
1009 /* Out-of-range, no branch is executed */
1012 if (table->table [idx])
1013 add_cprop_bb (cfg, table->table [idx], bblist);
1016 for (i = 0; i < table->table_size; i++)
1017 if (table->table [i])
1018 add_cprop_bb (cfg, table->table [i], bblist);
1022 if (ins->opcode == OP_SWITCH) {
1024 MonoJumpInfoBBTable *table = ins->inst_p0;
1026 for (i = 0; i < table->table_size; i++)
1027 if (table->table [i])
1028 add_cprop_bb (cfg, table->table [i], bblist);
1031 /* Handle COMPARE+BRCOND pairs */
1032 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
1034 g_assert (c0->opcode == OP_ICONST);
1037 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
1039 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1042 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1045 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1047 } else if (ins->opcode == OP_BR) {
1048 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1049 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1050 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1051 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1052 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1053 if (ins->inst_false_bb)
1054 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1056 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1057 if (ins->inst_false_bb)
1058 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1066 * Replace INS with its constant value, if it exists
1069 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1071 const char *spec = INS_INFO (ins->opcode);
1074 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1075 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1076 /* Perform constant folding */
1078 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1079 ins->opcode = OP_ICONST;
1080 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1081 ins->sreg1 = ins->sreg2 = -1;
1083 else if ((spec [MONO_INST_SRC2] != ' ') && carray [ins->sreg2]) {
1084 /* Perform op->op_imm conversion */
1085 opcode2 = mono_op_to_op_imm (ins->opcode);
1086 if (opcode2 != -1) {
1087 ins->opcode = opcode2;
1088 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1091 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1092 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1096 if (ins->opcode == OP_JUMP_TABLE) {
1098 MonoJumpInfoBBTable *table = ins->inst_p0;
1100 if (ins->next->opcode != OP_PADD) {
1101 /* The PADD was optimized away */
1102 /* FIXME: handle this as well */
1106 g_assert (ins->next->opcode == OP_PADD);
1107 g_assert (ins->next->sreg1 == ins->dreg);
1108 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1110 if (carray [ins->next->sreg2]) {
1111 /* Convert to a simple branch */
1112 #if SIZEOF_VOID_P == 8
1113 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1115 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1118 if (!((idx >= 0) && (idx < table->table_size))) {
1119 /* Out of range, eliminate the whole switch */
1120 for (i = 0; i < table->table_size; ++i) {
1121 remove_bb_from_phis (cfg, bb, table->table [i]);
1122 mono_unlink_bblock (cfg, bb, table->table [i]);
1126 NULLIFY_INS (ins->next);
1127 NULLIFY_INS (ins->next->next);
1128 NULLIFY_INS (ins->next->next->next);
1133 if (ins->next->next->next->opcode != OP_BR_REG) {
1134 /* A one-way switch which got optimized away */
1135 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1136 printf ("\tNo cfold on ");
1137 mono_print_ins (ins);
1142 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1143 printf ("\tcfold on ");
1144 mono_print_ins (ins);
1147 /* Unlink target bblocks */
1148 for (i = 0; i < table->table_size; ++i) {
1150 remove_bb_from_phis (cfg, bb, table->table [i]);
1151 mono_unlink_bblock (cfg, bb, table->table [i]);
1155 /* Change the OP_BR_REG to a simple branch */
1156 ins->next->next->next->opcode = OP_BR;
1157 ins->next->next->next->inst_target_bb = table->table [idx];
1158 ins->next->next->next->sreg1 = -1;
1160 /* Nullify the other instructions */
1162 NULLIFY_INS (ins->next);
1163 NULLIFY_INS (ins->next->next);
1167 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1168 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1169 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1170 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1171 ins->opcode = OP_BR;
1172 ins->inst_target_bb = ins->inst_true_bb;
1173 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1174 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1175 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1176 ins->opcode = OP_BR;
1177 ins->inst_target_bb = ins->inst_false_bb;
1183 mono_ssa_cprop2 (MonoCompile *cfg)
1187 GList *bblock_list, *cvars;
1190 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1192 carray = g_new0 (MonoInst*, cfg->next_vreg);
1194 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1195 mono_ssa_create_def_use (cfg);
1197 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1198 cfg->bb_entry->flags |= BB_REACHABLE;
1200 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1202 for (i = 0; i < cfg->num_varinfo; i++) {
1203 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1210 while (bblock_list) {
1213 bb = (MonoBasicBlock *)bblock_list->data;
1215 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1217 g_assert (bb->flags & BB_REACHABLE);
1219 if (bb->out_count == 1) {
1220 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1221 bb->out_bb [0]->flags |= BB_REACHABLE;
1222 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1226 if (cfg->verbose_level > 1)
1227 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1229 for (inst = bb->code; inst; inst = inst->next) {
1230 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1234 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1235 cvars = g_list_delete_link (cvars, cvars);
1237 for (tmp = info->uses; tmp; tmp = tmp->next) {
1238 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1239 if (!(ui->bb->flags & BB_REACHABLE))
1241 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1246 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1248 for (inst = bb->code; inst; inst = inst->next) {
1249 fold_ins (cfg, bb, inst, carray);
1255 cfg->comp_done |= MONO_COMP_REACHABILITY;
1257 /* fixme: we should update usage infos during cprop, instead of computing it again */
1258 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1259 for (i = 0; i < cfg->num_varinfo; i++) {
1260 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1267 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1271 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1273 for (tmp = use->uses; tmp; tmp = tmp->next) {
1274 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1275 if (ui->inst == var->def) {
1276 /* from the mempool */
1277 use->uses = g_list_remove_link (use->uses, tmp);
1284 mono_ssa_deadce2 (MonoCompile *cfg)
1289 g_assert (cfg->comp_done & MONO_COMP_SSA);
1291 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1293 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1294 mono_ssa_create_def_use (cfg);
1296 mono_ssa_copyprop (cfg);
1299 for (i = 0; i < cfg->num_varinfo; i++) {
1300 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1301 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1305 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1306 work_list = g_list_remove_link (work_list, work_list);
1309 * The second part of the condition happens often when PHI nodes have their dreg
1310 * as one of their arguments due to the fact that we use the original vars.
1312 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1313 MonoInst *def = info->def;
1315 /* Eliminating FMOVE could screw up the fp stack */
1316 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1317 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1318 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1319 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1320 def->opcode = OP_NOP;
1321 def->dreg = def->sreg1 = def->sreg2 = -1;
1323 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || (def->opcode == OP_VZERO)) {
1324 def->opcode = OP_NOP;
1325 def->dreg = def->sreg1 = def->sreg2 = -1;
1327 } else if (MONO_IS_PHI (def)) {
1329 for (j = def->inst_phi_args [0]; j > 0; j--) {
1330 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1331 add_to_dce_worklist (cfg, info, u, &work_list);
1333 def->opcode = OP_NOP;
1334 def->dreg = def->sreg1 = def->sreg2 = -1;
1337 else if (def->opcode == OP_NOP) {
1340 //mono_print_ins (def);
1348 mono_ssa_strength_reduction (MonoCompile *cfg)
1353 g_assert (cfg->comp_done & MONO_COMP_SSA);
1354 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1355 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1357 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1358 GList *lp = bb->loop_blocks;
1361 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1363 /* we only consider loops with 2 in bblocks */
1364 if (!h->in_count == 2)
1367 for (i = 0; i < cfg->num_varinfo; i++) {
1368 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1370 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1371 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1372 MonoInst *v = info->def->inst_i1;
1375 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));