2 * ssa.c: Static single assign form support for the JIT compiler.
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/mempool.h>
14 #include <mono/metadata/mempool-internals.h>
23 #define USE_ORIGINAL_VARS
24 #define CREATE_PRUNED_SSA
28 #define NEW_PHI(cfg,dest,val) do { \
29 MONO_INST_NEW ((cfg), (dest), OP_PHI); \
30 (dest)->inst_c0 = (val); \
39 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
43 for (i = 0; i < bb->out_count; i++) {
44 if (bb->out_bb [i] == target) {
45 bb->out_bb [i] = bb->out_bb [--bb->out_count];
49 for (i = 0; i < target->in_count; i++) {
50 if (target->in_bb [i] == bb) {
51 target->in_bb [i] = target->in_bb [--target->in_count];
59 unlink_unused_bblocks (MonoCompile *cfg)
64 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
66 if (G_UNLIKELY (cfg->verbose_level > 1))
67 printf ("\nUNLINK UNUSED BBLOCKS:\n");
69 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
70 if (!(bb->next_bb->flags & BB_REACHABLE)) {
71 bb->next_bb = bb->next_bb->next_bb;
76 for (i = 1; i < cfg->num_bblocks; i++) {
77 bb = cfg->bblocks [i];
79 if (!(bb->flags & BB_REACHABLE)) {
80 for (j = 0; j < bb->in_count; j++) {
81 unlink_target (bb->in_bb [j], bb);
83 for (j = 0; j < bb->out_count; j++) {
84 unlink_target (bb, bb->out_bb [j]);
86 if (G_UNLIKELY (cfg->verbose_level > 1))
87 printf ("\tUnlinked BB%d\n", bb->block_num);
94 * remove_bb_from_phis:
96 * Remove BB from the PHI statements in TARGET.
99 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
104 for (i = 0; i < target->in_count; i++) {
105 if (target->in_bb [i] == bb) {
109 g_assert (i < target->in_count);
111 for (ins = target->code; ins; ins = ins->next) {
112 if (MONO_IS_PHI (ins)) {
113 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
114 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
115 ins->inst_phi_args [0] --;
123 op_phi_to_move (int opcode)
135 g_assert_not_reached ();
142 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
145 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
147 info = MONO_VARINFO (cfg, var->inst_c0);
151 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
160 * mono_ssa_rename_vars:
162 * Implement renaming of SSA variables. Also compute def-use information in parallel.
163 * @stack_history points to an area of memory which can be used for storing changes
164 * made to the stack, so they can be reverted later.
167 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)
169 MonoInst *ins, *new_var;
172 int stack_history_len = 0;
174 if (cfg->verbose_level >= 4)
175 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
177 /* First pass: Create new vars */
178 for (ins = bb->code; ins; ins = ins->next) {
179 const char *spec = INS_INFO (ins->opcode);
181 int sregs [MONO_MAX_SRC_REGS];
184 printf ("\tProcessing "); mono_print_ins (ins);
186 if (ins->opcode == OP_NOP)
190 num_sregs = mono_inst_get_src_registers (ins, sregs);
191 for (i = 0; i < num_sregs; ++i) {
192 if (spec [MONO_INST_SRC1 + i] != ' ') {
193 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
194 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
195 int idx = var->inst_c0;
197 if (var->opcode != OP_ARG)
198 g_assert (stack [idx]);
199 sregs [i] = stack [idx]->dreg;
200 record_use (cfg, stack [idx], bb, ins);
203 record_use (cfg, var, bb, ins);
205 else if (G_UNLIKELY (!var && lvreg_stack [sregs [i]]))
206 sregs [i] = lvreg_stack [sregs [i]];
209 mono_inst_set_src_registers (ins, sregs);
211 if (MONO_IS_STORE_MEMBASE (ins)) {
212 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
213 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
214 int idx = var->inst_c0;
216 if (var->opcode != OP_ARG)
217 g_assert (stack [idx]);
218 ins->dreg = stack [idx]->dreg;
219 record_use (cfg, stack [idx], bb, ins);
222 record_use (cfg, var, bb, ins);
224 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
225 ins->dreg = lvreg_stack [ins->dreg];
229 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
230 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
233 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
235 g_assert (idx < max_vars);
237 if (var->opcode == OP_ARG)
238 originals_used [idx] = TRUE;
241 g_assert (stack_history_len < stack_history_size);
242 stack_history [stack_history_len].var = stack [idx];
243 stack_history [stack_history_len].idx = idx;
244 stack_history_len ++;
246 if (originals_used [idx]) {
247 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
248 new_var->flags = var->flags;
249 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
251 if (cfg->verbose_level >= 4)
252 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
254 stack [idx] = new_var;
256 ins->dreg = new_var->dreg;
261 originals_used [idx] = TRUE;
264 info = MONO_VARINFO (cfg, var->inst_c0);
268 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
269 /* Perform renaming for local vregs */
270 lvreg_stack [ins->dreg] = vreg_is_ref (cfg, ins->dreg) ? mono_alloc_ireg_ref (cfg) : mono_alloc_preg (cfg);
271 ins->dreg = lvreg_stack [ins->dreg];
274 lvreg_defined [ins->dreg] = TRUE;
278 printf ("\tAfter processing "); mono_print_ins (ins);
283 /* Rename PHI arguments in succeeding bblocks */
284 for (i = 0; i < bb->out_count; i++) {
285 MonoBasicBlock *n = bb->out_bb [i];
287 for (j = 0; j < n->in_count; j++)
288 if (n->in_bb [j] == bb)
291 for (ins = n->code; ins; ins = ins->next) {
292 if (MONO_IS_PHI (ins)) {
295 new_var = stack [idx];
297 new_var = cfg->varinfo [idx];
299 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
301 ins->inst_phi_args [j + 1] = new_var->dreg;
302 record_use (cfg, new_var, n, ins);
303 if (G_UNLIKELY (cfg->verbose_level >= 4))
304 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
307 /* The phi nodes are at the beginning of the bblock */
313 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
314 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);
319 for (i = stack_history_len - 1; i >= 0; i--) {
320 stack [stack_history [i].idx] = stack_history [i].var;
323 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
327 mono_ssa_compute (MonoCompile *cfg)
329 int i, j, idx, bitsize;
331 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
332 MonoInst *ins, **stack;
333 guint8 *buf, *buf_start;
334 RenameInfo *stack_history;
335 int stack_history_size;
337 guint32 *lvreg_stack;
338 gboolean *lvreg_defined;
340 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
342 g_assert (!cfg->disable_ssa);
344 if (cfg->verbose_level >= 4)
345 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
347 #ifdef CREATE_PRUNED_SSA
348 /* we need liveness for pruned SSA */
349 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
350 mono_analyze_liveness (cfg);
353 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
355 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
356 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
358 for (i = 0; i < cfg->num_varinfo; ++i) {
359 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
362 /* implicit reference at start */
363 if (cfg->varinfo [i]->opcode == OP_ARG)
364 mono_bitset_set_fast (vinfo [i].def_in, 0);
367 for (i = 0; i < cfg->num_bblocks; ++i) {
368 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
369 if (ins->opcode == OP_NOP)
372 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
373 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
378 /* insert phi functions */
379 for (i = 0; i < cfg->num_varinfo; ++i) {
380 MonoInst *var = cfg->varinfo [i];
382 #if SIZEOF_REGISTER == 4
383 if (var->type == STACK_I8 && !COMPILE_LLVM (cfg))
386 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
389 /* Most variables have only one definition */
390 if (mono_bitset_count (vinfo [i].def_in) <= 1)
393 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
395 if (cfg->verbose_level >= 4) {
396 if (mono_bitset_count (set) > 0) {
397 printf ("\tR%d needs PHI functions in ", var->dreg);
398 mono_blockset_print (cfg, set, "", -1);
402 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
403 MonoBasicBlock *bb = cfg->bblocks [idx];
405 /* fixme: create pruned SSA? we would need liveness information for that */
407 if (bb == cfg->bb_exit && !COMPILE_LLVM (cfg))
410 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
411 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
415 NEW_PHI (cfg, ins, i);
423 ins->opcode = OP_PHI;
426 ins->opcode = OP_FPHI;
429 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
433 if (var->inst_vtype->byref)
434 ins->klass = mono_defaults.int_class;
436 ins->klass = var->klass;
438 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
439 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
442 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
443 ins->inst_phi_args [j + 1] = -1;
445 ins->dreg = cfg->varinfo [i]->dreg;
447 mono_bblock_insert_before_ins (bb, bb->code, ins);
450 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
461 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
462 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
464 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
465 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
466 stack_history_size = 10240;
467 stack_history = g_new (RenameInfo, stack_history_size);
468 originals = g_new0 (gboolean, cfg->num_varinfo);
469 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
470 g_free (stack_history);
472 g_free (lvreg_stack);
473 g_free (lvreg_defined);
475 if (cfg->verbose_level >= 4)
476 printf ("\nEND COMPUTE SSA.\n\n");
478 cfg->comp_done |= MONO_COMP_SSA;
482 mono_ssa_remove (MonoCompile *cfg)
484 MonoInst *ins, *var, *move;
485 int bbindex, i, j, first;
487 g_assert (cfg->comp_done & MONO_COMP_SSA);
489 for (i = 0; i < cfg->num_bblocks; ++i) {
490 MonoBasicBlock *bb = cfg->bblocks [i];
492 if (cfg->verbose_level >= 4)
493 printf ("\nREMOVE SSA %d:\n", bb->block_num);
495 for (ins = bb->code; ins; ins = ins->next) {
496 if (MONO_IS_PHI (ins)) {
497 g_assert (ins->inst_phi_args [0] == bb->in_count);
498 var = get_vreg_to_inst (cfg, ins->dreg);
500 /* Check for PHI nodes where all the inputs are the same */
501 first = ins->inst_phi_args [1];
503 for (j = 1; j < bb->in_count; ++j)
504 if (first != ins->inst_phi_args [j + 1])
507 if ((bb->in_count > 1) && (j == bb->in_count)) {
508 ins->opcode = op_phi_to_move (ins->opcode);
509 if (ins->opcode == OP_VMOVE)
510 g_assert (ins->klass);
513 for (j = 0; j < bb->in_count; j++) {
514 MonoBasicBlock *pred = bb->in_bb [j];
515 int sreg = ins->inst_phi_args [j + 1];
517 if (cfg->verbose_level >= 4)
518 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
519 if (var->dreg != sreg) {
520 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
521 if (move->opcode == OP_VMOVE) {
522 g_assert (ins->klass);
523 move->klass = ins->klass;
525 move->dreg = var->dreg;
527 mono_add_ins_to_end (pred, move);
537 if (cfg->verbose_level >= 4) {
538 for (i = 0; i < cfg->num_bblocks; ++i) {
539 MonoBasicBlock *bb = cfg->bblocks [i];
541 mono_print_bb (bb, "AFTER REMOVE SSA:");
546 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
547 * the variables if possible. Since the newly introduced SSA variables don't
548 * have overlapping live ranges (because we don't do agressive optimization), we
549 * can coalesce them into the original variable.
552 for (bbindex = 0; bbindex < cfg->num_bblocks; ++bbindex) {
553 MonoBasicBlock *bb = cfg->bblocks [bbindex];
555 for (ins = bb->code; ins; ins = ins->next) {
556 const char *spec = INS_INFO (ins->opcode);
558 int sregs [MONO_MAX_SRC_REGS];
560 if (ins->opcode == OP_NOP)
563 if (spec [MONO_INST_DEST] != ' ') {
564 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
567 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
570 * The third condition avoids coalescing with variables eliminated
573 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
574 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
575 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
580 num_sregs = mono_inst_get_src_registers (ins, sregs);
581 for (i = 0; i < num_sregs; ++i) {
582 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
585 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
587 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
588 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
589 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
593 mono_inst_set_src_registers (ins, sregs);
597 for (i = 0; i < cfg->num_varinfo; ++i) {
598 MONO_VARINFO (cfg, i)->reg = -1;
601 if (cfg->comp_done & MONO_COMP_REACHABILITY)
602 unlink_unused_bblocks (cfg);
604 cfg->comp_done &= ~MONO_COMP_LIVENESS;
606 cfg->comp_done &= ~MONO_COMP_SSA;
610 mono_ssa_create_def_use (MonoCompile *cfg)
616 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
618 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
619 for (ins = bb->code; ins; ins = ins->next) {
620 const char *spec = INS_INFO (ins->opcode);
623 int sregs [MONO_MAX_SRC_REGS];
625 if (ins->opcode == OP_NOP)
629 num_sregs = mono_inst_get_src_registers (ins, sregs);
630 for (i = 0; i < num_sregs; ++i) {
631 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
632 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
633 record_use (cfg, var, bb, ins);
636 if (MONO_IS_STORE_MEMBASE (ins)) {
637 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
638 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
639 record_use (cfg, var, bb, ins);
642 if (MONO_IS_PHI (ins)) {
643 for (i = ins->inst_phi_args [0]; i > 0; i--) {
644 g_assert (ins->inst_phi_args [i] != -1);
645 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
650 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
651 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
653 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
654 info = MONO_VARINFO (cfg, var->inst_c0);
662 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
666 mono_ssa_copyprop (MonoCompile *cfg)
671 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
673 for (index = 0; index < cfg->num_varinfo; ++index) {
674 MonoInst *var = cfg->varinfo [index];
675 MonoMethodVar *info = MONO_VARINFO (cfg, index);
677 if (info->def && (MONO_IS_MOVE (info->def))) {
678 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
680 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))) {
681 /* Rewrite all uses of var to be uses of var2 */
682 int dreg = var->dreg;
683 int sreg1 = var2->dreg;
687 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
688 MonoInst *ins = u->inst;
689 GList *next = l->next;
691 int sregs [MONO_MAX_SRC_REGS];
693 num_sregs = mono_inst_get_src_registers (ins, sregs);
694 for (i = 0; i < num_sregs; ++i) {
695 if (sregs [i] == dreg)
699 g_assert (sregs [i] == dreg);
701 mono_inst_set_src_registers (ins, sregs);
702 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
704 } else if (MONO_IS_PHI (ins)) {
705 for (i = ins->inst_phi_args [0]; i > 0; i--) {
706 int sreg = ins->inst_phi_args [i];
707 if (sreg == var->dreg)
711 ins->inst_phi_args [i] = sreg1;
714 g_assert_not_reached ();
716 record_use (cfg, var2, u->bb, ins);
726 if (cfg->verbose_level >= 4) {
729 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
730 mono_print_bb (bb, "AFTER SSA COPYPROP");
735 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
737 MonoInst *args [MONO_MAX_SRC_REGS];
738 int rs [MONO_MAX_SRC_REGS];
740 gboolean const_args = TRUE;
741 const char *spec = INS_INFO (ins->opcode);
743 int sregs [MONO_MAX_SRC_REGS];
745 /* Short-circuit this */
746 if (ins->opcode == OP_ICONST) {
751 if (ins->opcode == OP_NOP)
754 num_sregs = mono_inst_get_src_registers (ins, sregs);
755 for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
757 for (i = 0; i < num_sregs; ++i) {
758 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
761 args [i] = carray [sregs [i]];
764 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
765 rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
772 if (num_sregs > 0 && const_args) {
773 g_assert (num_sregs <= 2);
774 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
776 *res = carray [ins->dreg];
779 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
781 if (G_UNLIKELY (cfg->verbose_level > 1)) {
782 printf ("\t cfold -> ");
789 /* Can't cfold this ins */
795 for (i = 0; i < num_sregs; ++i) {
803 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
805 if (info->cpstate >= state)
808 info->cpstate = state;
810 if (G_UNLIKELY (cfg->verbose_level > 1))
811 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
816 carray [cfg->varinfo [info->idx]->dreg] = c0;
818 if (!g_list_find (*cvars, info)) {
819 *cvars = g_list_prepend (*cvars, info);
824 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
826 if (G_UNLIKELY (cfg->verbose_level > 1))
827 printf ("\tAdd BB%d to worklist\n", bb->block_num);
829 if (!(bb->flags & BB_REACHABLE)) {
830 bb->flags |= BB_REACHABLE;
831 *bblist = g_list_prepend (*bblist, bb);
836 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
838 const char *spec = INS_INFO (ins->opcode);
840 if (ins->opcode == OP_NOP)
843 if (cfg->verbose_level > 1)
844 mono_print_ins (ins);
846 /* FIXME: Support longs/floats */
847 /* FIXME: Work on vregs as well */
849 if (MONO_IS_PHI (ins)) {
850 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
854 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
855 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
856 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
857 MonoInst *src = mv->def;
859 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
862 if (!mv->def || !src || mv->cpstate == 2) {
863 change_varstate (cfg, cvars, info, 2, NULL, carray);
867 if (mv->cpstate == 0)
870 g_assert (carray [var->dreg]);
873 c0 = carray [var->dreg];
876 if (c0->opcode != OP_ICONST) {
877 change_varstate (cfg, cvars, info, 2, NULL, carray);
881 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
882 change_varstate (cfg, cvars, info, 2, NULL, carray);
887 if (c0 && info->cpstate < 1) {
888 change_varstate (cfg, cvars, info, 1, c0, carray);
890 g_assert (c0->opcode == OP_ICONST);
893 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
897 if (spec [MONO_INST_DEST] != ' ')
898 var = get_vreg_to_inst (cfg, ins->dreg);
903 state = evaluate_ins (cfg, ins, &c0, carray);
905 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
906 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
908 if (info->cpstate < 2) {
910 change_varstate (cfg, cvars, info, 1, c0, carray);
912 change_varstate (cfg, cvars, info, 2, NULL, carray);
915 else if (!var && (ins->dreg != -1)) {
917 * We don't record def-use information for local vregs since it would be
918 * expensive. Instead, we depend on the fact that all uses of the vreg are in
919 * the same bblock, so they will be examined after the definition.
920 * FIXME: This isn't true if the ins is visited through an SSA edge.
923 carray [ins->dreg] = c0;
925 if (carray [ins->dreg]) {
927 * The state of the vreg changed from constant to non-constant
928 * -> need to rescan the whole bblock.
930 carray [ins->dreg] = NULL;
931 /* FIXME: Speed this up */
933 if (!g_list_find (*bblist, bb))
934 *bblist = g_list_prepend (*bblist, bb);
939 if (MONO_IS_JUMP_TABLE (ins)) {
941 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
943 if (!ins->next || ins->next->opcode != OP_PADD) {
944 /* The PADD was optimized away */
945 /* FIXME: handle this as well */
946 for (i = 0; i < table->table_size; i++)
947 if (table->table [i])
948 add_cprop_bb (cfg, table->table [i], bblist);
952 g_assert (ins->next->opcode == OP_PADD);
953 g_assert (ins->next->sreg1 == ins->dreg);
955 if (carray [ins->next->sreg2]) {
956 #if SIZEOF_REGISTER == 8
957 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
959 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
961 if ((idx < 0) || (idx >= table->table_size))
962 /* Out-of-range, no branch is executed */
965 if (table->table [idx])
966 add_cprop_bb (cfg, table->table [idx], bblist);
969 for (i = 0; i < table->table_size; i++)
970 if (table->table [i])
971 add_cprop_bb (cfg, table->table [i], bblist);
975 if (ins->opcode == OP_SWITCH) {
977 MonoJumpInfoBBTable *table = ins->inst_p0;
979 for (i = 0; i < table->table_size; i++)
980 if (table->table [i])
981 add_cprop_bb (cfg, table->table [i], bblist);
984 /* Handle COMPARE+BRCOND pairs */
985 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
987 g_assert (c0->opcode == OP_ICONST);
990 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
992 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
995 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
998 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1000 } else if (ins->opcode == OP_BR) {
1001 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1002 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1003 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1004 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1005 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1006 if (ins->inst_false_bb)
1007 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1009 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1010 if (ins->inst_false_bb)
1011 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1019 * Replace INS with its constant value, if it exists
1022 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1024 const char *spec = INS_INFO (ins->opcode);
1026 int num_sregs = mono_inst_get_num_src_registers (ins);
1028 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1029 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1030 /* Perform constant folding */
1032 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1033 ins->opcode = OP_ICONST;
1034 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1035 MONO_INST_NULLIFY_SREGS (ins);
1036 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1037 /* Perform op->op_imm conversion */
1038 opcode2 = mono_op_to_op_imm (ins->opcode);
1039 if (opcode2 != -1) {
1040 ins->opcode = opcode2;
1041 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1044 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1045 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1048 /* FIXME: Handle 3 op insns */
1051 if (MONO_IS_JUMP_TABLE (ins)) {
1053 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1055 if (!ins->next || ins->next->opcode != OP_PADD) {
1056 /* The PADD was optimized away */
1057 /* FIXME: handle this as well */
1061 g_assert (ins->next->opcode == OP_PADD);
1062 g_assert (ins->next->sreg1 == ins->dreg);
1063 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1065 if (carray [ins->next->sreg2]) {
1066 /* Convert to a simple branch */
1067 #if SIZEOF_REGISTER == 8
1068 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1070 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1073 if (!((idx >= 0) && (idx < table->table_size))) {
1074 /* Out of range, eliminate the whole switch */
1075 for (i = 0; i < table->table_size; ++i) {
1076 remove_bb_from_phis (cfg, bb, table->table [i]);
1077 mono_unlink_bblock (cfg, bb, table->table [i]);
1081 NULLIFY_INS (ins->next);
1082 NULLIFY_INS (ins->next->next);
1083 if (ins->next->next->next)
1084 NULLIFY_INS (ins->next->next->next);
1089 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1090 /* A one-way switch which got optimized away */
1091 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1092 printf ("\tNo cfold on ");
1093 mono_print_ins (ins);
1098 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1099 printf ("\tcfold on ");
1100 mono_print_ins (ins);
1103 /* Unlink target bblocks */
1104 for (i = 0; i < table->table_size; ++i) {
1105 if (table->table [i] != table->table [idx]) {
1106 remove_bb_from_phis (cfg, bb, table->table [i]);
1107 mono_unlink_bblock (cfg, bb, table->table [i]);
1111 /* Change the OP_BR_REG to a simple branch */
1112 ins->next->next->next->opcode = OP_BR;
1113 ins->next->next->next->inst_target_bb = table->table [idx];
1114 ins->next->next->next->sreg1 = -1;
1116 /* Nullify the other instructions */
1118 NULLIFY_INS (ins->next);
1119 NULLIFY_INS (ins->next->next);
1123 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1124 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1125 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1126 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1127 ins->opcode = OP_BR;
1128 ins->inst_target_bb = ins->inst_true_bb;
1129 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1130 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1131 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1132 ins->opcode = OP_BR;
1133 ins->inst_target_bb = ins->inst_false_bb;
1139 mono_ssa_cprop (MonoCompile *cfg)
1143 GList *bblock_list, *cvars;
1146 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1148 carray = g_new0 (MonoInst*, cfg->next_vreg);
1150 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1151 mono_ssa_create_def_use (cfg);
1153 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1154 cfg->bb_entry->flags |= BB_REACHABLE;
1156 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1158 for (i = 0; i < cfg->num_varinfo; i++) {
1159 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1164 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1166 * FIXME: This should be bb->flags & BB_FLAG_EXCEPTION_HANDLER, but
1167 * that would still allow unreachable try's to be removed.
1170 add_cprop_bb (cfg, bb, &bblock_list);
1175 while (bblock_list) {
1178 bb = (MonoBasicBlock *)bblock_list->data;
1180 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1182 g_assert (bb->flags & BB_REACHABLE);
1185 * Some bblocks are linked to 2 others even through they fall through to the
1188 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins))) {
1189 for (i = 0; i < bb->out_count; ++i)
1190 add_cprop_bb (cfg, bb->out_bb [i], &bblock_list);
1193 if (cfg->verbose_level > 1)
1194 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1196 for (inst = bb->code; inst; inst = inst->next) {
1197 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1201 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1202 cvars = g_list_delete_link (cvars, cvars);
1204 for (tmp = info->uses; tmp; tmp = tmp->next) {
1205 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1206 if (!(ui->bb->flags & BB_REACHABLE))
1208 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1213 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1215 for (inst = bb->code; inst; inst = inst->next) {
1216 fold_ins (cfg, bb, inst, carray);
1222 cfg->comp_done |= MONO_COMP_REACHABILITY;
1224 /* fixme: we should update usage infos during cprop, instead of computing it again */
1225 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1226 for (i = 0; i < cfg->num_varinfo; i++) {
1227 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1234 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1238 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1240 for (tmp = use->uses; tmp; tmp = tmp->next) {
1241 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1242 if (ui->inst == var->def) {
1243 /* from the mempool */
1244 use->uses = g_list_remove_link (use->uses, tmp);
1251 mono_ssa_deadce (MonoCompile *cfg)
1256 g_assert (cfg->comp_done & MONO_COMP_SSA);
1258 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1260 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1261 mono_ssa_create_def_use (cfg);
1263 mono_ssa_copyprop (cfg);
1266 for (i = 0; i < cfg->num_varinfo; i++) {
1267 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1268 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1272 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1273 work_list = g_list_remove_link (work_list, work_list);
1276 * The second part of the condition happens often when PHI nodes have their dreg
1277 * as one of their arguments due to the fact that we use the original vars.
1279 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1280 MonoInst *def = info->def;
1282 /* Eliminating FMOVE could screw up the fp stack */
1283 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1284 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1285 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1286 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1289 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1292 } else if (MONO_IS_PHI (def)) {
1294 for (j = def->inst_phi_args [0]; j > 0; j--) {
1295 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1296 add_to_dce_worklist (cfg, info, u, &work_list);
1301 else if (def->opcode == OP_NOP) {
1304 //mono_print_ins (def);
1312 mono_ssa_strength_reduction (MonoCompile *cfg)
1317 g_assert (cfg->comp_done & MONO_COMP_SSA);
1318 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1319 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1321 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1322 GList *lp = bb->loop_blocks;
1325 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1327 /* we only consider loops with 2 in bblocks */
1328 if (!h->in_count == 2)
1331 for (i = 0; i < cfg->num_varinfo; i++) {
1332 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1334 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1335 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1336 MonoInst *v = info->def->inst_i1;
1339 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1348 mono_ssa_loop_invariant_code_motion (MonoCompile *cfg)
1350 MonoBasicBlock *bb, *h, *idom;
1351 MonoInst *ins, *n, *tins;
1354 g_assert (cfg->comp_done & MONO_COMP_SSA);
1355 if (!(cfg->comp_done & MONO_COMP_LOOPS) || !(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1358 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1359 GList *lp = bb->loop_blocks;
1363 h = (MonoBasicBlock *)lp->data;
1366 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1367 gboolean is_class_init = FALSE;
1370 * Try to move instructions out of loop headers into the preceeding bblock.
1372 if (ins->opcode == OP_VOIDCALL) {
1373 MonoCallInst *call = (MonoCallInst*)ins;
1375 if (call->fptr_is_patch) {
1376 MonoJumpInfo *ji = (MonoJumpInfo*)call->fptr;
1378 if (ji->type == MONO_PATCH_INFO_CLASS_INIT)
1379 is_class_init = TRUE;
1382 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS || ins->opcode == OP_AOTCONST || is_class_init) {
1388 * h->nesting is needed to work around:
1389 * http://llvm.org/bugs/show_bug.cgi?id=17868
1391 if (!(idom && idom->last_ins && idom->last_ins->opcode == OP_BR && idom->last_ins->inst_target_bb == h && h->nesting == 1)) {
1396 * Make sure there are no instructions with side effects before ins.
1399 MONO_BB_FOR_EACH_INS (bb, tins) {
1402 if (!MONO_INS_HAS_NO_SIDE_EFFECT (tins)) {
1409 printf ("%s\n", mono_method_full_name (cfg->method, TRUE));
1410 mono_print_ins (tins);
1415 /* Make sure we don't move the instruction before the def of its sreg */
1416 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS)
1421 MonoInst *tins, *var;
1424 for (tins = ins->prev; tins; tins = tins->prev) {
1425 const char *spec = INS_INFO (tins->opcode);
1427 if (tins->opcode == OP_MOVE && tins->dreg == sreg) {
1429 } if (spec [MONO_INST_DEST] != ' ' && tins->dreg == sreg) {
1436 var = get_vreg_to_inst (cfg, sreg);
1437 if (var && (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1442 if (cfg->verbose_level > 1) {
1443 printf ("licm in BB%d on ", bb->block_num);
1444 mono_print_ins (ins);
1446 //{ static int count = 0; count ++; printf ("%d\n", count); }
1447 MONO_REMOVE_INS (bb, ins);
1448 mono_bblock_insert_before_ins (idom, idom->last_ins, ins);
1449 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN)
1450 idom->has_array_access = TRUE;
1455 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1456 for (i = 0; i < cfg->num_varinfo; i++) {
1457 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1463 #endif /* DISABLE_JIT */