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>
15 #define USE_ORIGINAL_VARS
16 #define CREATE_PRUNED_SSA
20 #define NEW_PHI(cfg,dest,val) do { \
21 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
22 (dest)->opcode = OP_PHI; \
23 (dest)->inst_c0 = (val); \
24 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
27 #define NEW_ICONST(cfg,dest,val) do { \
28 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
29 (dest)->opcode = OP_ICONST; \
30 (dest)->inst_c0 = (val); \
31 (dest)->type = STACK_I4; \
32 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
41 g_list_prepend_mempool (GList* l, MonoMemPool* mp, gpointer datum)
43 GList* n = mono_mempool_alloc (mp, sizeof (GList));
51 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
55 for (i = 0; i < bb->out_count; i++) {
56 if (bb->out_bb [i] == target) {
57 bb->out_bb [i] = bb->out_bb [--bb->out_count];
61 for (i = 0; i < target->in_count; i++) {
62 if (target->in_bb [i] == bb) {
63 target->in_bb [i] = target->in_bb [--target->in_count];
71 unlink_unused_bblocks (MonoCompile *cfg)
76 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
78 if (G_UNLIKELY (cfg->verbose_level > 1))
79 printf ("\nUNLINK UNUSED BBLOCKS:\n");
81 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
82 if (!(bb->next_bb->flags & BB_REACHABLE)) {
83 bb->next_bb = bb->next_bb->next_bb;
88 for (i = 1; i < cfg->num_bblocks; i++) {
89 bb = cfg->bblocks [i];
91 if (!(bb->flags & BB_REACHABLE)) {
92 for (j = 0; j < bb->in_count; j++) {
93 unlink_target (bb->in_bb [j], bb);
95 for (j = 0; j < bb->out_count; j++) {
96 unlink_target (bb, bb->out_bb [j]);
98 if (G_UNLIKELY (cfg->verbose_level > 1))
99 printf ("\tUnlinked BB%d\n", bb->block_num);
106 * remove_bb_from_phis:
108 * Remove BB from the PHI statements in TARGET.
111 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
116 for (i = 0; i < target->in_count; i++) {
117 if (target->in_bb [i] == bb) {
121 g_assert (i < target->in_count);
123 for (ins = target->code; ins; ins = ins->next) {
124 if (MONO_IS_PHI (ins)) {
125 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
126 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
127 ins->inst_phi_args [0] --;
135 op_phi_to_move (int opcode)
145 g_assert_not_reached ();
152 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
155 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
157 info = MONO_VARINFO (cfg, var->inst_c0);
161 info->uses = g_list_prepend_mempool (info->uses, cfg->mempool, ui);
170 * mono_ssa_rename_vars2:
172 * Implement renaming of SSA variables. Also compute def-use information in parallel.
173 * @stack_history points to an area of memory which can be used for storing changes
174 * made to the stack, so they can be reverted later.
177 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)
179 MonoInst *ins, *new_var;
182 int stack_history_len = 0;
184 if (cfg->verbose_level >= 4)
185 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
187 /* First pass: Create new vars */
188 for (ins = bb->code; ins; ins = ins->next) {
189 const char *spec = INS_INFO (ins->opcode);
192 printf ("\tProcessing "); mono_print_ins (ins);
194 if (ins->opcode == OP_NOP)
198 if (spec [MONO_INST_SRC1] != ' ') {
199 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
200 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
201 int idx = var->inst_c0;
203 if (var->opcode != OP_ARG)
204 g_assert (stack [idx]);
205 ins->sreg1 = stack [idx]->dreg;
206 record_use (cfg, stack [idx], bb, ins);
209 record_use (cfg, var, bb, ins);
211 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg1]))
212 ins->sreg1 = lvreg_stack [ins->sreg1];
216 if (spec [MONO_INST_SRC2] != ' ') {
217 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
218 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
219 int idx = var->inst_c0;
221 if (var->opcode != OP_ARG)
222 g_assert (stack [idx]);
224 ins->sreg2 = stack [idx]->dreg;
225 record_use (cfg, stack [idx], bb, ins);
228 record_use (cfg, var, bb, ins);
230 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg2]))
231 ins->sreg2 = lvreg_stack [ins->sreg2];
234 if (MONO_IS_STORE_MEMBASE (ins)) {
235 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
236 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
237 int idx = var->inst_c0;
239 if (var->opcode != OP_ARG)
240 g_assert (stack [idx]);
241 ins->dreg = stack [idx]->dreg;
242 record_use (cfg, stack [idx], bb, ins);
245 record_use (cfg, var, bb, ins);
247 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
248 ins->dreg = lvreg_stack [ins->dreg];
252 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
253 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
256 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
258 g_assert (idx < max_vars);
260 if (var->opcode == OP_ARG)
261 originals_used [idx] = TRUE;
264 g_assert (stack_history_len < stack_history_size);
265 stack_history [stack_history_len].var = stack [idx];
266 stack_history [stack_history_len].idx = idx;
267 stack_history_len ++;
269 if (originals_used [idx]) {
270 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
271 new_var->flags = var->flags;
272 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
274 if (cfg->verbose_level >= 4)
275 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
277 stack [idx] = new_var;
279 ins->dreg = new_var->dreg;
284 originals_used [idx] = TRUE;
287 info = MONO_VARINFO (cfg, var->inst_c0);
291 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
292 /* Perform renaming for local vregs */
293 lvreg_stack [ins->dreg] = mono_alloc_preg (cfg);
294 ins->dreg = lvreg_stack [ins->dreg];
297 lvreg_defined [ins->dreg] = TRUE;
301 printf ("\tAfter processing "); mono_print_ins (ins);
306 /* Rename PHI arguments in succeeding bblocks */
307 for (i = 0; i < bb->out_count; i++) {
308 MonoBasicBlock *n = bb->out_bb [i];
310 for (j = 0; j < n->in_count; j++)
311 if (n->in_bb [j] == bb)
314 for (ins = n->code; ins; ins = ins->next) {
315 if (MONO_IS_PHI (ins)) {
318 new_var = stack [idx];
320 new_var = cfg->varinfo [idx];
322 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
324 ins->inst_phi_args [j + 1] = new_var->dreg;
325 record_use (cfg, new_var, n, ins);
326 if (G_UNLIKELY (cfg->verbose_level >= 4))
327 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
330 /* The phi nodes are at the beginning of the bblock */
336 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
337 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);
342 for (i = stack_history_len - 1; i >= 0; i--) {
343 stack [stack_history [i].idx] = stack_history [i].var;
346 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
350 mono_ssa_compute2 (MonoCompile *cfg)
352 int i, j, idx, bitsize;
354 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
355 MonoInst *ins, **stack;
356 guint8 *buf, *buf_start;
357 RenameInfo *stack_history;
358 int stack_history_size;
360 guint32 *lvreg_stack;
361 gboolean *lvreg_defined;
363 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
365 /* we dont support methods containing exception clauses */
366 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
367 g_assert (!cfg->disable_ssa);
369 if (cfg->verbose_level >= 4)
370 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
372 #ifdef CREATE_PRUNED_SSA
373 /* we need liveness for pruned SSA */
374 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
375 mono_analyze_liveness (cfg);
378 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
380 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
381 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
383 for (i = 0; i < cfg->num_varinfo; ++i) {
384 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
387 /* implicit reference at start */
388 mono_bitset_set_fast (vinfo [i].def_in, 0);
391 for (i = 0; i < cfg->num_bblocks; ++i) {
392 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
393 if (ins->opcode == OP_NOP)
396 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
397 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
402 /* insert phi functions */
403 for (i = 0; i < cfg->num_varinfo; ++i) {
404 MonoInst *var = cfg->varinfo [i];
406 #if SIZEOF_VOID_P == 4
407 if (var->type == STACK_I8)
410 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
413 /* Most variables have only one definition */
414 if (mono_bitset_count (vinfo [i].def_in) <= 1)
417 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
419 if (cfg->verbose_level >= 4) {
420 if (mono_bitset_count (set) > 0) {
421 printf ("\tR%d needs PHI functions in ", var->dreg);
422 mono_blockset_print (cfg, set, "", -1);
426 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
427 MonoBasicBlock *bb = cfg->bblocks [idx];
429 /* fixme: create pruned SSA? we would need liveness information for that */
431 if (bb == cfg->bb_exit)
434 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
435 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
439 NEW_PHI (cfg, ins, i);
447 ins->opcode = OP_PHI;
450 ins->opcode = OP_FPHI;
453 ins->opcode = OP_VPHI;
454 ins->klass = var->klass;
458 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
459 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
462 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
463 ins->inst_phi_args [j + 1] = -1;
465 ins->dreg = cfg->varinfo [i]->dreg;
467 mono_bblock_insert_before_ins (bb, bb->code, ins);
470 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
481 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
482 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
484 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
485 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
486 stack_history_size = 10240;
487 stack_history = g_new (RenameInfo, stack_history_size);
488 originals = g_new0 (gboolean, cfg->num_varinfo);
489 mono_ssa_rename_vars2 (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
490 g_free (stack_history);
493 if (cfg->verbose_level >= 4)
494 printf ("\nEND COMPUTE SSA.\n\n");
496 cfg->comp_done |= MONO_COMP_SSA;
500 mono_ssa_remove2 (MonoCompile *cfg)
502 MonoInst *ins, *var, *move;
505 g_assert (cfg->comp_done & MONO_COMP_SSA);
507 for (i = 0; i < cfg->num_bblocks; ++i) {
508 MonoBasicBlock *bb = cfg->bblocks [i];
510 if (cfg->verbose_level >= 4)
511 printf ("\nREMOVE SSA %d:\n", bb->block_num);
513 for (ins = bb->code; ins; ins = ins->next) {
514 if (MONO_IS_PHI (ins)) {
515 g_assert (ins->inst_phi_args [0] == bb->in_count);
516 var = get_vreg_to_inst (cfg, ins->dreg);
518 /* Check for PHI nodes where all the inputs are the same */
519 first = ins->inst_phi_args [1];
521 for (j = 1; j < bb->in_count; ++j)
522 if (first != ins->inst_phi_args [j + 1])
525 if ((bb->in_count > 1) && (j == bb->in_count)) {
526 ins->opcode = op_phi_to_move (ins->opcode);
527 if (ins->opcode == OP_VMOVE)
528 g_assert (ins->klass);
531 for (j = 0; j < bb->in_count; j++) {
532 MonoBasicBlock *pred = bb->in_bb [j];
533 int sreg = ins->inst_phi_args [j + 1];
535 if (cfg->verbose_level >= 4)
536 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
537 if (var->dreg != sreg) {
538 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
539 if (move->opcode == OP_VMOVE) {
540 g_assert (ins->klass);
541 move->klass = ins->klass;
543 move->dreg = var->dreg;
545 mono_add_ins_to_end (pred, move);
549 ins->opcode = OP_NOP;
556 if (cfg->verbose_level >= 4) {
557 for (i = 0; i < cfg->num_bblocks; ++i) {
558 MonoBasicBlock *bb = cfg->bblocks [i];
560 mono_print_bb (bb, "AFTER REMOVE SSA:");
565 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
566 * the variables if possible. Since the newly introduced SSA variables don't
567 * have overlapping live ranges (because we don't do agressive optimization), we
568 * can coalesce them into the original variable.
571 for (i = 0; i < cfg->num_bblocks; ++i) {
572 MonoBasicBlock *bb = cfg->bblocks [i];
574 for (ins = bb->code; ins; ins = ins->next) {
575 const char *spec = INS_INFO (ins->opcode);
577 if (ins->opcode == OP_NOP)
580 if (spec [MONO_INST_DEST] != ' ') {
581 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
584 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
587 * The third condition avoids coalescing with variables eliminated
590 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
591 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
592 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
597 if (spec [MONO_INST_SRC1] != ' ') {
598 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
601 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
603 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
604 printf ("COALESCE: R%d -> R%d\n", ins->sreg1, cfg->varinfo [vmv->reg]->dreg);
605 ins->sreg1 = cfg->varinfo [vmv->reg]->dreg;
610 if (spec [MONO_INST_SRC2] != ' ') {
611 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
614 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
616 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
617 printf ("COALESCE: R%d -> R%d\n", ins->sreg2, cfg->varinfo [vmv->reg]->dreg);
618 ins->sreg2 = cfg->varinfo [vmv->reg]->dreg;
626 for (i = 0; i < cfg->num_varinfo; ++i) {
627 MONO_VARINFO (cfg, i)->reg = -1;
630 if (cfg->comp_done & MONO_COMP_REACHABILITY)
631 unlink_unused_bblocks (cfg);
633 cfg->comp_done &= ~MONO_COMP_LIVENESS;
635 cfg->comp_done &= ~MONO_COMP_SSA;
639 mono_ssa_create_def_use (MonoCompile *cfg)
645 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
647 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
648 for (ins = bb->code; ins; ins = ins->next) {
649 const char *spec = INS_INFO (ins->opcode);
652 if (ins->opcode == OP_NOP)
656 if (spec [MONO_INST_SRC1] != ' ') {
657 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
658 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
659 record_use (cfg, var, bb, ins);
663 if (spec [MONO_INST_SRC2] != ' ') {
664 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
665 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
666 record_use (cfg, var, bb, ins);
669 if (MONO_IS_STORE_MEMBASE (ins)) {
670 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
671 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
672 record_use (cfg, var, bb, ins);
675 if (MONO_IS_PHI (ins)) {
676 for (i = ins->inst_phi_args [0]; i > 0; i--) {
677 g_assert (ins->inst_phi_args [i] != -1);
678 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
683 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
684 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
686 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
687 info = MONO_VARINFO (cfg, var->inst_c0);
695 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
699 mono_ssa_copyprop (MonoCompile *cfg)
704 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
706 for (index = 0; index < cfg->num_varinfo; ++index) {
707 MonoInst *var = cfg->varinfo [index];
708 MonoMethodVar *info = MONO_VARINFO (cfg, index);
710 if (info->def && (MONO_IS_MOVE (info->def))) {
711 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
713 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))) {
714 /* Rewrite all uses of var to be uses of var2 */
715 int dreg = var->dreg;
716 int sreg1 = var2->dreg;
721 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
722 MonoInst *ins = u->inst;
723 GList *next = l->next;
725 spec = INS_INFO (ins->opcode);
727 if (spec [MONO_INST_SRC1] != ' ' && ins->sreg1 == dreg) {
729 } else if (spec [MONO_INST_SRC2] != ' ' && ins->sreg2 == dreg) {
731 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
733 } else if (MONO_IS_PHI (ins)) {
734 for (i = ins->inst_phi_args [0]; i > 0; i--) {
735 int sreg = ins->inst_phi_args [i];
736 if (sreg == var->dreg)
740 ins->inst_phi_args [i] = sreg1;
743 g_assert_not_reached ();
745 record_use (cfg, var2, u->bb, ins);
755 if (cfg->verbose_level >= 4) {
758 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
759 mono_print_bb (bb, "AFTER SSA COPYPROP");
764 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
766 MonoInst *arg0, *arg1, *c0;
768 gboolean const_args = FALSE;
769 const char *spec = INS_INFO (ins->opcode);
771 /* Short-circuit this */
772 if (ins->opcode == OP_ICONST) {
777 if (ins->opcode == OP_NOP)
781 if (spec [MONO_INST_SRC1] != ' ') {
782 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
785 arg0 = carray [ins->sreg1];
788 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
789 r1 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
795 if (spec [MONO_INST_SRC2] != ' ') {
796 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
799 arg1 = carray [ins->sreg2];
802 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
803 r2 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
810 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
812 const_args = (r1 == 1) && (r2 == 1);
814 else if (spec [MONO_INST_SRC1] != ' ') {
816 const_args = (r1 == 1);
820 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
822 *res = carray [ins->dreg];
825 c0 = mono_constant_fold_ins2 (cfg, ins, arg0, arg1, FALSE);
827 if (G_UNLIKELY (cfg->verbose_level > 1)) {
828 printf ("\t cfold -> ");
835 /* Can't cfold this ins */
839 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
841 if ((r1 == 2) || (r2 == 2))
846 else if (spec [MONO_INST_SRC1] != ' ') {
858 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
860 if (info->cpstate >= state)
863 info->cpstate = state;
865 if (G_UNLIKELY (cfg->verbose_level > 1))
866 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
871 carray [cfg->varinfo [info->idx]->dreg] = c0;
873 if (!g_list_find (*cvars, info)) {
874 *cvars = g_list_prepend (*cvars, info);
879 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
881 if (G_UNLIKELY (cfg->verbose_level > 1))
882 printf ("\tAdd BB%d to worklist\n", bb->block_num);
884 if (!(bb->flags & BB_REACHABLE)) {
885 bb->flags |= BB_REACHABLE;
886 *bblist = g_list_prepend (*bblist, bb);
891 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
893 const char *spec = INS_INFO (ins->opcode);
895 if (ins->opcode == OP_NOP)
898 if (cfg->verbose_level > 1)
899 mono_print_ins (ins);
901 /* FIXME: Support longs/floats */
902 /* FIXME: Work on vregs as well */
904 if (MONO_IS_PHI (ins)) {
905 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
909 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
910 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
911 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
912 MonoInst *src = mv->def;
914 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
917 if (!mv->def || !src || mv->cpstate == 2) {
918 change_varstate (cfg, cvars, info, 2, NULL, carray);
922 if (mv->cpstate == 0)
925 g_assert (carray [var->dreg]);
928 c0 = carray [var->dreg];
931 if (c0->opcode != OP_ICONST) {
932 change_varstate (cfg, cvars, info, 2, NULL, carray);
936 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
937 change_varstate (cfg, cvars, info, 2, NULL, carray);
942 if (c0 && info->cpstate < 1) {
943 change_varstate (cfg, cvars, info, 1, c0, carray);
945 g_assert (c0->opcode == OP_ICONST);
948 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
952 if (spec [MONO_INST_DEST] != ' ')
953 var = get_vreg_to_inst (cfg, ins->dreg);
958 state = evaluate_ins (cfg, ins, &c0, carray);
960 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
961 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
963 if (info->cpstate < 2) {
965 change_varstate (cfg, cvars, info, 1, c0, carray);
967 change_varstate (cfg, cvars, info, 2, NULL, carray);
970 else if (!var && (ins->dreg != -1)) {
972 * We don't record def-use information for local vregs since it would be
973 * expensive. Instead, we depend on the fact that all uses of the vreg are in
974 * the same bblock, so they will be examined after the definition.
975 * FIXME: This isn't true if the ins is visited through an SSA edge.
978 carray [ins->dreg] = c0;
980 if (carray [ins->dreg]) {
982 * The state of the vreg changed from constant to non-constant
983 * -> need to rescan the whole bblock.
985 carray [ins->dreg] = NULL;
986 /* FIXME: Speed this up */
988 if (!g_list_find (*bblist, bb))
989 *bblist = g_list_prepend (*bblist, bb);
994 if (ins->opcode == OP_JUMP_TABLE) {
996 MonoJumpInfoBBTable *table = ins->inst_p0;
998 if (ins->next->opcode != OP_PADD) {
999 /* The PADD was optimized away */
1000 /* FIXME: handle this as well */
1001 for (i = 0; i < table->table_size; i++)
1002 if (table->table [i])
1003 add_cprop_bb (cfg, table->table [i], bblist);
1007 g_assert (ins->next->opcode == OP_PADD);
1008 g_assert (ins->next->sreg1 == ins->dreg);
1010 if (carray [ins->next->sreg2]) {
1011 #if SIZEOF_VOID_P == 8
1012 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1014 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1016 if ((idx < 0) || (idx >= table->table_size))
1017 /* Out-of-range, no branch is executed */
1020 if (table->table [idx])
1021 add_cprop_bb (cfg, table->table [idx], bblist);
1024 for (i = 0; i < table->table_size; i++)
1025 if (table->table [i])
1026 add_cprop_bb (cfg, table->table [i], bblist);
1030 if (ins->opcode == OP_SWITCH) {
1032 MonoJumpInfoBBTable *table = ins->inst_p0;
1034 for (i = 0; i < table->table_size; i++)
1035 if (table->table [i])
1036 add_cprop_bb (cfg, table->table [i], bblist);
1039 /* Handle COMPARE+BRCOND pairs */
1040 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
1042 g_assert (c0->opcode == OP_ICONST);
1045 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
1047 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1050 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1053 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1055 } else if (ins->opcode == OP_BR) {
1056 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1057 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1058 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1059 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1060 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1061 if (ins->inst_false_bb)
1062 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1064 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1065 if (ins->inst_false_bb)
1066 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1074 * Replace INS with its constant value, if it exists
1077 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1079 const char *spec = INS_INFO (ins->opcode);
1082 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1083 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1084 /* Perform constant folding */
1086 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1087 ins->opcode = OP_ICONST;
1088 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1089 ins->sreg1 = ins->sreg2 = -1;
1091 else if ((spec [MONO_INST_SRC2] != ' ') && carray [ins->sreg2]) {
1092 /* Perform op->op_imm conversion */
1093 opcode2 = mono_op_to_op_imm (ins->opcode);
1094 if (opcode2 != -1) {
1095 ins->opcode = opcode2;
1096 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1099 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1100 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1104 if (ins->opcode == OP_JUMP_TABLE) {
1106 MonoJumpInfoBBTable *table = ins->inst_p0;
1108 if (ins->next->opcode != OP_PADD) {
1109 /* The PADD was optimized away */
1110 /* FIXME: handle this as well */
1114 g_assert (ins->next->opcode == OP_PADD);
1115 g_assert (ins->next->sreg1 == ins->dreg);
1116 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1118 if (carray [ins->next->sreg2]) {
1119 /* Convert to a simple branch */
1120 #if SIZEOF_VOID_P == 8
1121 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1123 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1126 if (!((idx >= 0) && (idx < table->table_size))) {
1127 /* Out of range, eliminate the whole switch */
1128 for (i = 0; i < table->table_size; ++i) {
1129 remove_bb_from_phis (cfg, bb, table->table [i]);
1130 mono_unlink_bblock (cfg, bb, table->table [i]);
1134 NULLIFY_INS (ins->next);
1135 NULLIFY_INS (ins->next->next);
1136 NULLIFY_INS (ins->next->next->next);
1141 if (ins->next->next->next->opcode != OP_BR_REG) {
1142 /* A one-way switch which got optimized away */
1143 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1144 printf ("\tNo cfold on ");
1145 mono_print_ins (ins);
1150 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1151 printf ("\tcfold on ");
1152 mono_print_ins (ins);
1155 /* Unlink target bblocks */
1156 for (i = 0; i < table->table_size; ++i) {
1158 remove_bb_from_phis (cfg, bb, table->table [i]);
1159 mono_unlink_bblock (cfg, bb, table->table [i]);
1163 /* Change the OP_BR_REG to a simple branch */
1164 ins->next->next->next->opcode = OP_BR;
1165 ins->next->next->next->inst_target_bb = table->table [idx];
1166 ins->next->next->next->sreg1 = -1;
1168 /* Nullify the other instructions */
1170 NULLIFY_INS (ins->next);
1171 NULLIFY_INS (ins->next->next);
1175 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1176 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1177 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1178 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1179 ins->opcode = OP_BR;
1180 ins->inst_target_bb = ins->inst_true_bb;
1181 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1182 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1183 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1184 ins->opcode = OP_BR;
1185 ins->inst_target_bb = ins->inst_false_bb;
1191 mono_ssa_cprop2 (MonoCompile *cfg)
1195 GList *bblock_list, *cvars;
1198 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1200 carray = g_new0 (MonoInst*, cfg->next_vreg);
1202 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1203 mono_ssa_create_def_use (cfg);
1205 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1206 cfg->bb_entry->flags |= BB_REACHABLE;
1208 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1210 for (i = 0; i < cfg->num_varinfo; i++) {
1211 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1218 while (bblock_list) {
1221 bb = (MonoBasicBlock *)bblock_list->data;
1223 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1225 g_assert (bb->flags & BB_REACHABLE);
1227 if (bb->out_count == 1) {
1228 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1229 bb->out_bb [0]->flags |= BB_REACHABLE;
1230 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1234 if (cfg->verbose_level > 1)
1235 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1237 for (inst = bb->code; inst; inst = inst->next) {
1238 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1242 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1243 cvars = g_list_delete_link (cvars, cvars);
1245 for (tmp = info->uses; tmp; tmp = tmp->next) {
1246 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1247 if (!(ui->bb->flags & BB_REACHABLE))
1249 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1254 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1256 for (inst = bb->code; inst; inst = inst->next) {
1257 fold_ins (cfg, bb, inst, carray);
1263 cfg->comp_done |= MONO_COMP_REACHABILITY;
1265 /* fixme: we should update usage infos during cprop, instead of computing it again */
1266 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1267 for (i = 0; i < cfg->num_varinfo; i++) {
1268 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1275 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1279 *wl = g_list_prepend_mempool (*wl, cfg->mempool, use);
1281 for (tmp = use->uses; tmp; tmp = tmp->next) {
1282 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1283 if (ui->inst == var->def) {
1284 /* from the mempool */
1285 use->uses = g_list_remove_link (use->uses, tmp);
1292 mono_ssa_deadce2 (MonoCompile *cfg)
1297 g_assert (cfg->comp_done & MONO_COMP_SSA);
1299 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1301 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1302 mono_ssa_create_def_use (cfg);
1304 mono_ssa_copyprop (cfg);
1307 for (i = 0; i < cfg->num_varinfo; i++) {
1308 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1309 work_list = g_list_prepend_mempool (work_list, cfg->mempool, info);
1313 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1314 work_list = g_list_remove_link (work_list, work_list);
1317 * The second part of the condition happens often when PHI nodes have their dreg
1318 * as one of their arguments due to the fact that we use the original vars.
1320 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1321 MonoInst *def = info->def;
1323 /* Eliminating FMOVE could screw up the fp stack */
1324 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1325 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1326 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1327 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1328 def->opcode = OP_NOP;
1329 def->dreg = def->sreg1 = def->sreg2 = -1;
1331 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || (def->opcode == OP_VZERO)) {
1332 def->opcode = OP_NOP;
1333 def->dreg = def->sreg1 = def->sreg2 = -1;
1335 } else if (MONO_IS_PHI (def)) {
1337 for (j = def->inst_phi_args [0]; j > 0; j--) {
1338 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1339 add_to_dce_worklist (cfg, info, u, &work_list);
1341 def->opcode = OP_NOP;
1342 def->dreg = def->sreg1 = def->sreg2 = -1;
1345 else if (def->opcode == OP_NOP) {
1348 //mono_print_ins (def);
1356 mono_ssa_strength_reduction (MonoCompile *cfg)
1361 g_assert (cfg->comp_done & MONO_COMP_SSA);
1362 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1363 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1365 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1366 GList *lp = bb->loop_blocks;
1369 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1371 /* we only consider loops with 2 in bblocks */
1372 if (!h->in_count == 2)
1375 for (i = 0; i < cfg->num_varinfo; i++) {
1376 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1378 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1379 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1380 MonoInst *v = info->def->inst_i1;
1383 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));