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>
18 #define USE_ORIGINAL_VARS
19 #define CREATE_PRUNED_SSA
23 #define NEW_PHI(cfg,dest,val) do { \
24 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
25 (dest)->opcode = OP_PHI; \
26 (dest)->inst_c0 = (val); \
27 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
36 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
40 for (i = 0; i < bb->out_count; i++) {
41 if (bb->out_bb [i] == target) {
42 bb->out_bb [i] = bb->out_bb [--bb->out_count];
46 for (i = 0; i < target->in_count; i++) {
47 if (target->in_bb [i] == bb) {
48 target->in_bb [i] = target->in_bb [--target->in_count];
56 unlink_unused_bblocks (MonoCompile *cfg)
61 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
63 if (G_UNLIKELY (cfg->verbose_level > 1))
64 printf ("\nUNLINK UNUSED BBLOCKS:\n");
66 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
67 if (!(bb->next_bb->flags & BB_REACHABLE)) {
68 bb->next_bb = bb->next_bb->next_bb;
73 for (i = 1; i < cfg->num_bblocks; i++) {
74 bb = cfg->bblocks [i];
76 if (!(bb->flags & BB_REACHABLE)) {
77 for (j = 0; j < bb->in_count; j++) {
78 unlink_target (bb->in_bb [j], bb);
80 for (j = 0; j < bb->out_count; j++) {
81 unlink_target (bb, bb->out_bb [j]);
83 if (G_UNLIKELY (cfg->verbose_level > 1))
84 printf ("\tUnlinked BB%d\n", bb->block_num);
91 * remove_bb_from_phis:
93 * Remove BB from the PHI statements in TARGET.
96 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
101 for (i = 0; i < target->in_count; i++) {
102 if (target->in_bb [i] == bb) {
106 g_assert (i < target->in_count);
108 for (ins = target->code; ins; ins = ins->next) {
109 if (MONO_IS_PHI (ins)) {
110 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
111 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
112 ins->inst_phi_args [0] --;
120 op_phi_to_move (int opcode)
130 g_assert_not_reached ();
137 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
140 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
142 info = MONO_VARINFO (cfg, var->inst_c0);
146 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
155 * mono_ssa_rename_vars:
157 * Implement renaming of SSA variables. Also compute def-use information in parallel.
158 * @stack_history points to an area of memory which can be used for storing changes
159 * made to the stack, so they can be reverted later.
162 mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, gboolean *originals_used, MonoInst **stack, guint32 *lvreg_stack, gboolean *lvreg_defined, RenameInfo *stack_history, int stack_history_size)
164 MonoInst *ins, *new_var;
167 int stack_history_len = 0;
169 if (cfg->verbose_level >= 4)
170 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
172 /* First pass: Create new vars */
173 for (ins = bb->code; ins; ins = ins->next) {
174 const char *spec = INS_INFO (ins->opcode);
177 printf ("\tProcessing "); mono_print_ins (ins);
179 if (ins->opcode == OP_NOP)
183 if (spec [MONO_INST_SRC1] != ' ') {
184 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
185 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
186 int idx = var->inst_c0;
188 if (var->opcode != OP_ARG)
189 g_assert (stack [idx]);
190 ins->sreg1 = stack [idx]->dreg;
191 record_use (cfg, stack [idx], bb, ins);
194 record_use (cfg, var, bb, ins);
196 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg1]))
197 ins->sreg1 = lvreg_stack [ins->sreg1];
201 if (spec [MONO_INST_SRC2] != ' ') {
202 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
203 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
204 int idx = var->inst_c0;
206 if (var->opcode != OP_ARG)
207 g_assert (stack [idx]);
209 ins->sreg2 = stack [idx]->dreg;
210 record_use (cfg, stack [idx], bb, ins);
213 record_use (cfg, var, bb, ins);
215 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg2]))
216 ins->sreg2 = lvreg_stack [ins->sreg2];
219 if (MONO_IS_STORE_MEMBASE (ins)) {
220 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
221 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
222 int idx = var->inst_c0;
224 if (var->opcode != OP_ARG)
225 g_assert (stack [idx]);
226 ins->dreg = stack [idx]->dreg;
227 record_use (cfg, stack [idx], bb, ins);
230 record_use (cfg, var, bb, ins);
232 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
233 ins->dreg = lvreg_stack [ins->dreg];
237 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
238 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
241 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
243 g_assert (idx < max_vars);
245 if (var->opcode == OP_ARG)
246 originals_used [idx] = TRUE;
249 g_assert (stack_history_len < stack_history_size);
250 stack_history [stack_history_len].var = stack [idx];
251 stack_history [stack_history_len].idx = idx;
252 stack_history_len ++;
254 if (originals_used [idx]) {
255 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
256 new_var->flags = var->flags;
257 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
259 if (cfg->verbose_level >= 4)
260 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
262 stack [idx] = new_var;
264 ins->dreg = new_var->dreg;
269 originals_used [idx] = TRUE;
272 info = MONO_VARINFO (cfg, var->inst_c0);
276 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
277 /* Perform renaming for local vregs */
278 lvreg_stack [ins->dreg] = mono_alloc_preg (cfg);
279 ins->dreg = lvreg_stack [ins->dreg];
282 lvreg_defined [ins->dreg] = TRUE;
286 printf ("\tAfter processing "); mono_print_ins (ins);
291 /* Rename PHI arguments in succeeding bblocks */
292 for (i = 0; i < bb->out_count; i++) {
293 MonoBasicBlock *n = bb->out_bb [i];
295 for (j = 0; j < n->in_count; j++)
296 if (n->in_bb [j] == bb)
299 for (ins = n->code; ins; ins = ins->next) {
300 if (MONO_IS_PHI (ins)) {
303 new_var = stack [idx];
305 new_var = cfg->varinfo [idx];
307 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
309 ins->inst_phi_args [j + 1] = new_var->dreg;
310 record_use (cfg, new_var, n, ins);
311 if (G_UNLIKELY (cfg->verbose_level >= 4))
312 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
315 /* The phi nodes are at the beginning of the bblock */
321 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
322 mono_ssa_rename_vars (cfg, max_vars, (MonoBasicBlock *)tmp->data, originals_used, stack, lvreg_stack, lvreg_defined, stack_history + stack_history_len, stack_history_size - stack_history_len);
327 for (i = stack_history_len - 1; i >= 0; i--) {
328 stack [stack_history [i].idx] = stack_history [i].var;
331 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
335 mono_ssa_compute (MonoCompile *cfg)
337 int i, j, idx, bitsize;
339 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
340 MonoInst *ins, **stack;
341 guint8 *buf, *buf_start;
342 RenameInfo *stack_history;
343 int stack_history_size;
345 guint32 *lvreg_stack;
346 gboolean *lvreg_defined;
348 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
350 /* we dont support methods containing exception clauses */
351 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
352 g_assert (!cfg->disable_ssa);
354 if (cfg->verbose_level >= 4)
355 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
357 #ifdef CREATE_PRUNED_SSA
358 /* we need liveness for pruned SSA */
359 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
360 mono_analyze_liveness (cfg);
363 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
365 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
366 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
368 for (i = 0; i < cfg->num_varinfo; ++i) {
369 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
372 /* implicit reference at start */
373 mono_bitset_set_fast (vinfo [i].def_in, 0);
376 for (i = 0; i < cfg->num_bblocks; ++i) {
377 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
378 if (ins->opcode == OP_NOP)
381 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
382 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
387 /* insert phi functions */
388 for (i = 0; i < cfg->num_varinfo; ++i) {
389 MonoInst *var = cfg->varinfo [i];
391 #if SIZEOF_VOID_P == 4
392 if (var->type == STACK_I8)
395 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
398 /* Most variables have only one definition */
399 if (mono_bitset_count (vinfo [i].def_in) <= 1)
402 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
404 if (cfg->verbose_level >= 4) {
405 if (mono_bitset_count (set) > 0) {
406 printf ("\tR%d needs PHI functions in ", var->dreg);
407 mono_blockset_print (cfg, set, "", -1);
411 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
412 MonoBasicBlock *bb = cfg->bblocks [idx];
414 /* fixme: create pruned SSA? we would need liveness information for that */
416 if (bb == cfg->bb_exit)
419 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
420 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
424 NEW_PHI (cfg, ins, i);
432 ins->opcode = OP_PHI;
435 ins->opcode = OP_FPHI;
438 ins->opcode = OP_VPHI;
439 ins->klass = var->klass;
443 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
444 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
447 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
448 ins->inst_phi_args [j + 1] = -1;
450 ins->dreg = cfg->varinfo [i]->dreg;
452 mono_bblock_insert_before_ins (bb, bb->code, ins);
455 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
466 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
467 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
469 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
470 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
471 stack_history_size = 10240;
472 stack_history = g_new (RenameInfo, stack_history_size);
473 originals = g_new0 (gboolean, cfg->num_varinfo);
474 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
475 g_free (stack_history);
478 if (cfg->verbose_level >= 4)
479 printf ("\nEND COMPUTE SSA.\n\n");
481 cfg->comp_done |= MONO_COMP_SSA;
485 mono_ssa_remove (MonoCompile *cfg)
487 MonoInst *ins, *var, *move;
490 g_assert (cfg->comp_done & MONO_COMP_SSA);
492 for (i = 0; i < cfg->num_bblocks; ++i) {
493 MonoBasicBlock *bb = cfg->bblocks [i];
495 if (cfg->verbose_level >= 4)
496 printf ("\nREMOVE SSA %d:\n", bb->block_num);
498 for (ins = bb->code; ins; ins = ins->next) {
499 if (MONO_IS_PHI (ins)) {
500 g_assert (ins->inst_phi_args [0] == bb->in_count);
501 var = get_vreg_to_inst (cfg, ins->dreg);
503 /* Check for PHI nodes where all the inputs are the same */
504 first = ins->inst_phi_args [1];
506 for (j = 1; j < bb->in_count; ++j)
507 if (first != ins->inst_phi_args [j + 1])
510 if ((bb->in_count > 1) && (j == bb->in_count)) {
511 ins->opcode = op_phi_to_move (ins->opcode);
512 if (ins->opcode == OP_VMOVE)
513 g_assert (ins->klass);
516 for (j = 0; j < bb->in_count; j++) {
517 MonoBasicBlock *pred = bb->in_bb [j];
518 int sreg = ins->inst_phi_args [j + 1];
520 if (cfg->verbose_level >= 4)
521 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
522 if (var->dreg != sreg) {
523 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
524 if (move->opcode == OP_VMOVE) {
525 g_assert (ins->klass);
526 move->klass = ins->klass;
528 move->dreg = var->dreg;
530 mono_add_ins_to_end (pred, move);
534 ins->opcode = OP_NOP;
541 if (cfg->verbose_level >= 4) {
542 for (i = 0; i < cfg->num_bblocks; ++i) {
543 MonoBasicBlock *bb = cfg->bblocks [i];
545 mono_print_bb (bb, "AFTER REMOVE SSA:");
550 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
551 * the variables if possible. Since the newly introduced SSA variables don't
552 * have overlapping live ranges (because we don't do agressive optimization), we
553 * can coalesce them into the original variable.
556 for (i = 0; i < cfg->num_bblocks; ++i) {
557 MonoBasicBlock *bb = cfg->bblocks [i];
559 for (ins = bb->code; ins; ins = ins->next) {
560 const char *spec = INS_INFO (ins->opcode);
562 if (ins->opcode == OP_NOP)
565 if (spec [MONO_INST_DEST] != ' ') {
566 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
569 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
572 * The third condition avoids coalescing with variables eliminated
575 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
576 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
577 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
582 if (spec [MONO_INST_SRC1] != ' ') {
583 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
586 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
588 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
589 printf ("COALESCE: R%d -> R%d\n", ins->sreg1, cfg->varinfo [vmv->reg]->dreg);
590 ins->sreg1 = cfg->varinfo [vmv->reg]->dreg;
595 if (spec [MONO_INST_SRC2] != ' ') {
596 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
599 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
601 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
602 printf ("COALESCE: R%d -> R%d\n", ins->sreg2, cfg->varinfo [vmv->reg]->dreg);
603 ins->sreg2 = cfg->varinfo [vmv->reg]->dreg;
611 for (i = 0; i < cfg->num_varinfo; ++i) {
612 MONO_VARINFO (cfg, i)->reg = -1;
615 if (cfg->comp_done & MONO_COMP_REACHABILITY)
616 unlink_unused_bblocks (cfg);
618 cfg->comp_done &= ~MONO_COMP_LIVENESS;
620 cfg->comp_done &= ~MONO_COMP_SSA;
624 mono_ssa_create_def_use (MonoCompile *cfg)
630 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
632 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
633 for (ins = bb->code; ins; ins = ins->next) {
634 const char *spec = INS_INFO (ins->opcode);
637 if (ins->opcode == OP_NOP)
641 if (spec [MONO_INST_SRC1] != ' ') {
642 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
643 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
644 record_use (cfg, var, bb, ins);
648 if (spec [MONO_INST_SRC2] != ' ') {
649 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
650 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
651 record_use (cfg, var, bb, ins);
654 if (MONO_IS_STORE_MEMBASE (ins)) {
655 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
656 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
657 record_use (cfg, var, bb, ins);
660 if (MONO_IS_PHI (ins)) {
661 for (i = ins->inst_phi_args [0]; i > 0; i--) {
662 g_assert (ins->inst_phi_args [i] != -1);
663 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
668 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
669 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
671 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
672 info = MONO_VARINFO (cfg, var->inst_c0);
680 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
684 mono_ssa_copyprop (MonoCompile *cfg)
689 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
691 for (index = 0; index < cfg->num_varinfo; ++index) {
692 MonoInst *var = cfg->varinfo [index];
693 MonoMethodVar *info = MONO_VARINFO (cfg, index);
695 if (info->def && (MONO_IS_MOVE (info->def))) {
696 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
698 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))) {
699 /* Rewrite all uses of var to be uses of var2 */
700 int dreg = var->dreg;
701 int sreg1 = var2->dreg;
706 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
707 MonoInst *ins = u->inst;
708 GList *next = l->next;
710 spec = INS_INFO (ins->opcode);
712 if (spec [MONO_INST_SRC1] != ' ' && ins->sreg1 == dreg) {
714 } else if (spec [MONO_INST_SRC2] != ' ' && ins->sreg2 == dreg) {
716 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
718 } else if (MONO_IS_PHI (ins)) {
719 for (i = ins->inst_phi_args [0]; i > 0; i--) {
720 int sreg = ins->inst_phi_args [i];
721 if (sreg == var->dreg)
725 ins->inst_phi_args [i] = sreg1;
728 g_assert_not_reached ();
730 record_use (cfg, var2, u->bb, ins);
740 if (cfg->verbose_level >= 4) {
743 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
744 mono_print_bb (bb, "AFTER SSA COPYPROP");
749 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
751 MonoInst *arg0, *arg1, *c0;
753 gboolean const_args = FALSE;
754 const char *spec = INS_INFO (ins->opcode);
756 /* Short-circuit this */
757 if (ins->opcode == OP_ICONST) {
762 if (ins->opcode == OP_NOP)
766 if (spec [MONO_INST_SRC1] != ' ') {
767 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
770 arg0 = carray [ins->sreg1];
773 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
774 r1 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
780 if (spec [MONO_INST_SRC2] != ' ') {
781 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
784 arg1 = carray [ins->sreg2];
787 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
788 r2 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
795 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
797 const_args = (r1 == 1) && (r2 == 1);
799 else if (spec [MONO_INST_SRC1] != ' ') {
801 const_args = (r1 == 1);
805 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
807 *res = carray [ins->dreg];
810 c0 = mono_constant_fold_ins (cfg, ins, arg0, arg1, FALSE);
812 if (G_UNLIKELY (cfg->verbose_level > 1)) {
813 printf ("\t cfold -> ");
820 /* Can't cfold this ins */
824 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
826 if ((r1 == 2) || (r2 == 2))
831 else if (spec [MONO_INST_SRC1] != ' ') {
843 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
845 if (info->cpstate >= state)
848 info->cpstate = state;
850 if (G_UNLIKELY (cfg->verbose_level > 1))
851 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
856 carray [cfg->varinfo [info->idx]->dreg] = c0;
858 if (!g_list_find (*cvars, info)) {
859 *cvars = g_list_prepend (*cvars, info);
864 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
866 if (G_UNLIKELY (cfg->verbose_level > 1))
867 printf ("\tAdd BB%d to worklist\n", bb->block_num);
869 if (!(bb->flags & BB_REACHABLE)) {
870 bb->flags |= BB_REACHABLE;
871 *bblist = g_list_prepend (*bblist, bb);
876 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
878 const char *spec = INS_INFO (ins->opcode);
880 if (ins->opcode == OP_NOP)
883 if (cfg->verbose_level > 1)
884 mono_print_ins (ins);
886 /* FIXME: Support longs/floats */
887 /* FIXME: Work on vregs as well */
889 if (MONO_IS_PHI (ins)) {
890 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
894 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
895 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
896 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
897 MonoInst *src = mv->def;
899 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
902 if (!mv->def || !src || mv->cpstate == 2) {
903 change_varstate (cfg, cvars, info, 2, NULL, carray);
907 if (mv->cpstate == 0)
910 g_assert (carray [var->dreg]);
913 c0 = carray [var->dreg];
916 if (c0->opcode != OP_ICONST) {
917 change_varstate (cfg, cvars, info, 2, NULL, carray);
921 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
922 change_varstate (cfg, cvars, info, 2, NULL, carray);
927 if (c0 && info->cpstate < 1) {
928 change_varstate (cfg, cvars, info, 1, c0, carray);
930 g_assert (c0->opcode == OP_ICONST);
933 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
937 if (spec [MONO_INST_DEST] != ' ')
938 var = get_vreg_to_inst (cfg, ins->dreg);
943 state = evaluate_ins (cfg, ins, &c0, carray);
945 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
946 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
948 if (info->cpstate < 2) {
950 change_varstate (cfg, cvars, info, 1, c0, carray);
952 change_varstate (cfg, cvars, info, 2, NULL, carray);
955 else if (!var && (ins->dreg != -1)) {
957 * We don't record def-use information for local vregs since it would be
958 * expensive. Instead, we depend on the fact that all uses of the vreg are in
959 * the same bblock, so they will be examined after the definition.
960 * FIXME: This isn't true if the ins is visited through an SSA edge.
963 carray [ins->dreg] = c0;
965 if (carray [ins->dreg]) {
967 * The state of the vreg changed from constant to non-constant
968 * -> need to rescan the whole bblock.
970 carray [ins->dreg] = NULL;
971 /* FIXME: Speed this up */
973 if (!g_list_find (*bblist, bb))
974 *bblist = g_list_prepend (*bblist, bb);
979 if (ins->opcode == OP_JUMP_TABLE) {
981 MonoJumpInfoBBTable *table = ins->inst_p0;
983 if (ins->next->opcode != OP_PADD) {
984 /* The PADD was optimized away */
985 /* FIXME: handle this as well */
986 for (i = 0; i < table->table_size; i++)
987 if (table->table [i])
988 add_cprop_bb (cfg, table->table [i], bblist);
992 g_assert (ins->next->opcode == OP_PADD);
993 g_assert (ins->next->sreg1 == ins->dreg);
995 if (carray [ins->next->sreg2]) {
996 #if SIZEOF_VOID_P == 8
997 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
999 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1001 if ((idx < 0) || (idx >= table->table_size))
1002 /* Out-of-range, no branch is executed */
1005 if (table->table [idx])
1006 add_cprop_bb (cfg, table->table [idx], bblist);
1009 for (i = 0; i < table->table_size; i++)
1010 if (table->table [i])
1011 add_cprop_bb (cfg, table->table [i], bblist);
1015 if (ins->opcode == OP_SWITCH) {
1017 MonoJumpInfoBBTable *table = ins->inst_p0;
1019 for (i = 0; i < table->table_size; i++)
1020 if (table->table [i])
1021 add_cprop_bb (cfg, table->table [i], bblist);
1024 /* Handle COMPARE+BRCOND pairs */
1025 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
1027 g_assert (c0->opcode == OP_ICONST);
1030 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
1032 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1035 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1038 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1040 } else if (ins->opcode == OP_BR) {
1041 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1042 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1043 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1044 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1045 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1046 if (ins->inst_false_bb)
1047 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1049 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1050 if (ins->inst_false_bb)
1051 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1059 * Replace INS with its constant value, if it exists
1062 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1064 const char *spec = INS_INFO (ins->opcode);
1067 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1068 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1069 /* Perform constant folding */
1071 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1072 ins->opcode = OP_ICONST;
1073 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1074 ins->sreg1 = ins->sreg2 = -1;
1076 else if ((spec [MONO_INST_SRC2] != ' ') && carray [ins->sreg2]) {
1077 /* Perform op->op_imm conversion */
1078 opcode2 = mono_op_to_op_imm (ins->opcode);
1079 if (opcode2 != -1) {
1080 ins->opcode = opcode2;
1081 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1084 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1085 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1089 if (ins->opcode == OP_JUMP_TABLE) {
1091 MonoJumpInfoBBTable *table = ins->inst_p0;
1093 if (ins->next->opcode != OP_PADD) {
1094 /* The PADD was optimized away */
1095 /* FIXME: handle this as well */
1099 g_assert (ins->next->opcode == OP_PADD);
1100 g_assert (ins->next->sreg1 == ins->dreg);
1101 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1103 if (carray [ins->next->sreg2]) {
1104 /* Convert to a simple branch */
1105 #if SIZEOF_VOID_P == 8
1106 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1108 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1111 if (!((idx >= 0) && (idx < table->table_size))) {
1112 /* Out of range, eliminate the whole switch */
1113 for (i = 0; i < table->table_size; ++i) {
1114 remove_bb_from_phis (cfg, bb, table->table [i]);
1115 mono_unlink_bblock (cfg, bb, table->table [i]);
1119 NULLIFY_INS (ins->next);
1120 NULLIFY_INS (ins->next->next);
1121 NULLIFY_INS (ins->next->next->next);
1126 if (ins->next->next->next->opcode != OP_BR_REG) {
1127 /* A one-way switch which got optimized away */
1128 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1129 printf ("\tNo cfold on ");
1130 mono_print_ins (ins);
1135 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1136 printf ("\tcfold on ");
1137 mono_print_ins (ins);
1140 /* Unlink target bblocks */
1141 for (i = 0; i < table->table_size; ++i) {
1143 remove_bb_from_phis (cfg, bb, table->table [i]);
1144 mono_unlink_bblock (cfg, bb, table->table [i]);
1148 /* Change the OP_BR_REG to a simple branch */
1149 ins->next->next->next->opcode = OP_BR;
1150 ins->next->next->next->inst_target_bb = table->table [idx];
1151 ins->next->next->next->sreg1 = -1;
1153 /* Nullify the other instructions */
1155 NULLIFY_INS (ins->next);
1156 NULLIFY_INS (ins->next->next);
1160 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1161 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1162 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1163 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1164 ins->opcode = OP_BR;
1165 ins->inst_target_bb = ins->inst_true_bb;
1166 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1167 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1168 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1169 ins->opcode = OP_BR;
1170 ins->inst_target_bb = ins->inst_false_bb;
1176 mono_ssa_cprop (MonoCompile *cfg)
1180 GList *bblock_list, *cvars;
1183 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1185 carray = g_new0 (MonoInst*, cfg->next_vreg);
1187 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1188 mono_ssa_create_def_use (cfg);
1190 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1191 cfg->bb_entry->flags |= BB_REACHABLE;
1193 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1195 for (i = 0; i < cfg->num_varinfo; i++) {
1196 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1203 while (bblock_list) {
1206 bb = (MonoBasicBlock *)bblock_list->data;
1208 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1210 g_assert (bb->flags & BB_REACHABLE);
1212 if (bb->out_count == 1) {
1213 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1214 bb->out_bb [0]->flags |= BB_REACHABLE;
1215 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1219 if (cfg->verbose_level > 1)
1220 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1222 for (inst = bb->code; inst; inst = inst->next) {
1223 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1227 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1228 cvars = g_list_delete_link (cvars, cvars);
1230 for (tmp = info->uses; tmp; tmp = tmp->next) {
1231 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1232 if (!(ui->bb->flags & BB_REACHABLE))
1234 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1239 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1241 for (inst = bb->code; inst; inst = inst->next) {
1242 fold_ins (cfg, bb, inst, carray);
1248 cfg->comp_done |= MONO_COMP_REACHABILITY;
1250 /* fixme: we should update usage infos during cprop, instead of computing it again */
1251 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1252 for (i = 0; i < cfg->num_varinfo; i++) {
1253 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1260 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1264 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1266 for (tmp = use->uses; tmp; tmp = tmp->next) {
1267 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1268 if (ui->inst == var->def) {
1269 /* from the mempool */
1270 use->uses = g_list_remove_link (use->uses, tmp);
1277 mono_ssa_deadce (MonoCompile *cfg)
1282 g_assert (cfg->comp_done & MONO_COMP_SSA);
1284 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1286 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1287 mono_ssa_create_def_use (cfg);
1289 mono_ssa_copyprop (cfg);
1292 for (i = 0; i < cfg->num_varinfo; i++) {
1293 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1294 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1298 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1299 work_list = g_list_remove_link (work_list, work_list);
1302 * The second part of the condition happens often when PHI nodes have their dreg
1303 * as one of their arguments due to the fact that we use the original vars.
1305 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1306 MonoInst *def = info->def;
1308 /* Eliminating FMOVE could screw up the fp stack */
1309 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1310 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1311 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1312 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1313 def->opcode = OP_NOP;
1314 def->dreg = def->sreg1 = def->sreg2 = -1;
1316 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1317 def->opcode = OP_NOP;
1318 def->dreg = def->sreg1 = def->sreg2 = -1;
1320 } else if (MONO_IS_PHI (def)) {
1322 for (j = def->inst_phi_args [0]; j > 0; j--) {
1323 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1324 add_to_dce_worklist (cfg, info, u, &work_list);
1326 def->opcode = OP_NOP;
1327 def->dreg = def->sreg1 = def->sreg2 = -1;
1330 else if (def->opcode == OP_NOP) {
1333 //mono_print_ins (def);
1341 mono_ssa_strength_reduction (MonoCompile *cfg)
1346 g_assert (cfg->comp_done & MONO_COMP_SSA);
1347 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1348 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1350 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1351 GList *lp = bb->loop_blocks;
1354 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1356 /* we only consider loops with 2 in bblocks */
1357 if (!h->in_count == 2)
1360 for (i = 0; i < cfg->num_varinfo; i++) {
1361 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1363 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1364 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1365 MonoInst *v = info->def->inst_i1;
1368 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1376 #endif /* DISABLE_JIT */