[amd64] Remove the callee saved registers from MonoLMF, save/restore them normally...
[mono-project.git] / mono / mini / ssa.c
blob801826b96e8b098caf1a5f102a3cde6b0aabce33
1 /*
2 * ssa.c: Static single assign form support for the JIT compiler.
4 * Author:
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
9 */
10 #include <config.h>
11 #include <string.h>
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/mempool.h>
14 #include <mono/metadata/mempool-internals.h>
16 #ifndef DISABLE_JIT
18 #include "mini.h"
19 #ifdef HAVE_ALLOCA_H
20 #include <alloca.h>
21 #endif
23 #define USE_ORIGINAL_VARS
24 #define CREATE_PRUNED_SSA
26 //#define DEBUG_SSA 1
28 #define NEW_PHI(cfg,dest,val) do { \
29 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
30 (dest)->opcode = OP_PHI; \
31 (dest)->inst_c0 = (val); \
32 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
33 } while (0)
35 typedef struct {
36 MonoBasicBlock *bb;
37 MonoInst *inst;
38 } MonoVarUsageInfo;
40 static void
41 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
43 int i;
45 for (i = 0; i < bb->out_count; i++) {
46 if (bb->out_bb [i] == target) {
47 bb->out_bb [i] = bb->out_bb [--bb->out_count];
48 break;
51 for (i = 0; i < target->in_count; i++) {
52 if (target->in_bb [i] == bb) {
53 target->in_bb [i] = target->in_bb [--target->in_count];
54 break;
60 static void
61 unlink_unused_bblocks (MonoCompile *cfg)
63 int i, j;
64 MonoBasicBlock *bb;
66 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
68 if (G_UNLIKELY (cfg->verbose_level > 1))
69 printf ("\nUNLINK UNUSED BBLOCKS:\n");
71 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
72 if (!(bb->next_bb->flags & BB_REACHABLE)) {
73 bb->next_bb = bb->next_bb->next_bb;
74 } else
75 bb = bb->next_bb;
78 for (i = 1; i < cfg->num_bblocks; i++) {
79 bb = cfg->bblocks [i];
81 if (!(bb->flags & BB_REACHABLE)) {
82 for (j = 0; j < bb->in_count; j++) {
83 unlink_target (bb->in_bb [j], bb);
85 for (j = 0; j < bb->out_count; j++) {
86 unlink_target (bb, bb->out_bb [j]);
88 if (G_UNLIKELY (cfg->verbose_level > 1))
89 printf ("\tUnlinked BB%d\n", bb->block_num);
95 /**
96 * remove_bb_from_phis:
98 * Remove BB from the PHI statements in TARGET.
100 static void
101 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
103 MonoInst *ins;
104 int i, j;
106 for (i = 0; i < target->in_count; i++) {
107 if (target->in_bb [i] == bb) {
108 break;
111 g_assert (i < target->in_count);
113 for (ins = target->code; ins; ins = ins->next) {
114 if (MONO_IS_PHI (ins)) {
115 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
116 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
117 ins->inst_phi_args [0] --;
119 else
120 break;
124 static inline int
125 op_phi_to_move (int opcode)
127 switch (opcode) {
128 case OP_PHI:
129 return OP_MOVE;
130 case OP_FPHI:
131 return OP_FMOVE;
132 case OP_VPHI:
133 return OP_VMOVE;
134 case OP_XPHI:
135 return OP_XMOVE;
136 default:
137 g_assert_not_reached ();
140 return -1;
143 static inline void
144 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
146 MonoMethodVar *info;
147 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
149 info = MONO_VARINFO (cfg, var->inst_c0);
151 ui->bb = bb;
152 ui->inst = ins;
153 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
156 typedef struct {
157 MonoInst *var;
158 int idx;
159 } RenameInfo;
162 * mono_ssa_rename_vars:
164 * Implement renaming of SSA variables. Also compute def-use information in parallel.
165 * @stack_history points to an area of memory which can be used for storing changes
166 * made to the stack, so they can be reverted later.
168 static void
169 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)
171 MonoInst *ins, *new_var;
172 int i, j, idx;
173 GSList *tmp;
174 int stack_history_len = 0;
176 if (cfg->verbose_level >= 4)
177 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
179 /* First pass: Create new vars */
180 for (ins = bb->code; ins; ins = ins->next) {
181 const char *spec = INS_INFO (ins->opcode);
182 int num_sregs;
183 int sregs [MONO_MAX_SRC_REGS];
185 #ifdef DEBUG_SSA
186 printf ("\tProcessing "); mono_print_ins (ins);
187 #endif
188 if (ins->opcode == OP_NOP)
189 continue;
191 /* SREGs */
192 num_sregs = mono_inst_get_src_registers (ins, sregs);
193 for (i = 0; i < num_sregs; ++i) {
194 if (spec [MONO_INST_SRC1 + i] != ' ') {
195 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
196 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
197 int idx = var->inst_c0;
198 if (stack [idx]) {
199 if (var->opcode != OP_ARG)
200 g_assert (stack [idx]);
201 sregs [i] = stack [idx]->dreg;
202 record_use (cfg, stack [idx], bb, ins);
204 else
205 record_use (cfg, var, bb, ins);
207 else if (G_UNLIKELY (!var && lvreg_stack [sregs [i]]))
208 sregs [i] = lvreg_stack [sregs [i]];
211 mono_inst_set_src_registers (ins, sregs);
213 if (MONO_IS_STORE_MEMBASE (ins)) {
214 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
215 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
216 int idx = var->inst_c0;
217 if (stack [idx]) {
218 if (var->opcode != OP_ARG)
219 g_assert (stack [idx]);
220 ins->dreg = stack [idx]->dreg;
221 record_use (cfg, stack [idx], bb, ins);
223 else
224 record_use (cfg, var, bb, ins);
226 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
227 ins->dreg = lvreg_stack [ins->dreg];
230 /* DREG */
231 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
232 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
233 MonoMethodVar *info;
235 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
236 idx = var->inst_c0;
237 g_assert (idx < max_vars);
239 if (var->opcode == OP_ARG)
240 originals_used [idx] = TRUE;
242 /* FIXME: */
243 g_assert (stack_history_len < stack_history_size);
244 stack_history [stack_history_len].var = stack [idx];
245 stack_history [stack_history_len].idx = idx;
246 stack_history_len ++;
248 if (originals_used [idx]) {
249 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
250 new_var->flags = var->flags;
251 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
253 if (cfg->verbose_level >= 4)
254 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
256 stack [idx] = new_var;
258 ins->dreg = new_var->dreg;
259 var = new_var;
261 else {
262 stack [idx] = var;
263 originals_used [idx] = TRUE;
266 info = MONO_VARINFO (cfg, var->inst_c0);
267 info->def = ins;
268 info->def_bb = bb;
270 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
271 /* Perform renaming for local vregs */
272 lvreg_stack [ins->dreg] = vreg_is_ref (cfg, ins->dreg) ? mono_alloc_ireg_ref (cfg) : mono_alloc_preg (cfg);
273 ins->dreg = lvreg_stack [ins->dreg];
275 else
276 lvreg_defined [ins->dreg] = TRUE;
279 #ifdef DEBUG_SSA
280 printf ("\tAfter processing "); mono_print_ins (ins);
281 #endif
285 /* Rename PHI arguments in succeeding bblocks */
286 for (i = 0; i < bb->out_count; i++) {
287 MonoBasicBlock *n = bb->out_bb [i];
289 for (j = 0; j < n->in_count; j++)
290 if (n->in_bb [j] == bb)
291 break;
293 for (ins = n->code; ins; ins = ins->next) {
294 if (MONO_IS_PHI (ins)) {
295 idx = ins->inst_c0;
296 if (stack [idx])
297 new_var = stack [idx];
298 else
299 new_var = cfg->varinfo [idx];
300 #ifdef DEBUG_SSA
301 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
302 #endif
303 ins->inst_phi_args [j + 1] = new_var->dreg;
304 record_use (cfg, new_var, n, ins);
305 if (G_UNLIKELY (cfg->verbose_level >= 4))
306 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
308 else
309 /* The phi nodes are at the beginning of the bblock */
310 break;
314 if (bb->dominated) {
315 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
316 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);
320 /* Restore stack */
321 for (i = stack_history_len - 1; i >= 0; i--) {
322 stack [stack_history [i].idx] = stack_history [i].var;
325 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
328 void
329 mono_ssa_compute (MonoCompile *cfg)
331 int i, j, idx, bitsize;
332 MonoBitSet *set;
333 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
334 MonoInst *ins, **stack;
335 guint8 *buf, *buf_start;
336 RenameInfo *stack_history;
337 int stack_history_size;
338 gboolean *originals;
339 guint32 *lvreg_stack;
340 gboolean *lvreg_defined;
342 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
344 g_assert (!cfg->disable_ssa);
346 if (cfg->verbose_level >= 4)
347 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
349 #ifdef CREATE_PRUNED_SSA
350 /* we need liveness for pruned SSA */
351 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
352 mono_analyze_liveness (cfg);
353 #endif
355 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
357 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
358 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
360 for (i = 0; i < cfg->num_varinfo; ++i) {
361 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
362 buf += bitsize;
363 vinfo [i].idx = i;
364 /* implicit reference at start */
365 if (cfg->varinfo [i]->opcode == OP_ARG)
366 mono_bitset_set_fast (vinfo [i].def_in, 0);
369 for (i = 0; i < cfg->num_bblocks; ++i) {
370 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
371 if (ins->opcode == OP_NOP)
372 continue;
374 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
375 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
380 /* insert phi functions */
381 for (i = 0; i < cfg->num_varinfo; ++i) {
382 MonoInst *var = cfg->varinfo [i];
384 #if SIZEOF_REGISTER == 4
385 if (var->type == STACK_I8 && !COMPILE_LLVM (cfg))
386 continue;
387 #endif
388 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
389 continue;
391 /* Most variables have only one definition */
392 if (mono_bitset_count (vinfo [i].def_in) <= 1)
393 continue;
395 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
397 if (cfg->verbose_level >= 4) {
398 if (mono_bitset_count (set) > 0) {
399 printf ("\tR%d needs PHI functions in ", var->dreg);
400 mono_blockset_print (cfg, set, "", -1);
404 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
405 MonoBasicBlock *bb = cfg->bblocks [idx];
407 /* fixme: create pruned SSA? we would need liveness information for that */
409 if (bb == cfg->bb_exit && !COMPILE_LLVM (cfg))
410 continue;
412 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
413 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
414 continue;
417 NEW_PHI (cfg, ins, i);
419 switch (var->type) {
420 case STACK_I4:
421 case STACK_I8:
422 case STACK_PTR:
423 case STACK_MP:
424 case STACK_OBJ:
425 ins->opcode = OP_PHI;
426 break;
427 case STACK_R8:
428 ins->opcode = OP_FPHI;
429 break;
430 case STACK_VTYPE:
431 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
432 break;
435 if (var->inst_vtype->byref)
436 ins->klass = mono_defaults.int_class;
437 else
438 ins->klass = var->klass;
440 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
441 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
443 /* For debugging */
444 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
445 ins->inst_phi_args [j + 1] = -1;
447 ins->dreg = cfg->varinfo [i]->dreg;
449 mono_bblock_insert_before_ins (bb, bb->code, ins);
451 #ifdef DEBUG_SSA
452 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
453 #endif
457 /* free the stuff */
458 g_free (vinfo);
459 g_free (buf_start);
461 /* Renaming phase */
463 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
464 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
466 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
467 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
468 stack_history_size = 10240;
469 stack_history = g_new (RenameInfo, stack_history_size);
470 originals = g_new0 (gboolean, cfg->num_varinfo);
471 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
472 g_free (stack_history);
473 g_free (originals);
474 g_free (lvreg_stack);
475 g_free (lvreg_defined);
477 if (cfg->verbose_level >= 4)
478 printf ("\nEND COMPUTE SSA.\n\n");
480 cfg->comp_done |= MONO_COMP_SSA;
483 void
484 mono_ssa_remove (MonoCompile *cfg)
486 MonoInst *ins, *var, *move;
487 int bbindex, i, j, first;
489 g_assert (cfg->comp_done & MONO_COMP_SSA);
491 for (i = 0; i < cfg->num_bblocks; ++i) {
492 MonoBasicBlock *bb = cfg->bblocks [i];
494 if (cfg->verbose_level >= 4)
495 printf ("\nREMOVE SSA %d:\n", bb->block_num);
497 for (ins = bb->code; ins; ins = ins->next) {
498 if (MONO_IS_PHI (ins)) {
499 g_assert (ins->inst_phi_args [0] == bb->in_count);
500 var = get_vreg_to_inst (cfg, ins->dreg);
502 /* Check for PHI nodes where all the inputs are the same */
503 first = ins->inst_phi_args [1];
505 for (j = 1; j < bb->in_count; ++j)
506 if (first != ins->inst_phi_args [j + 1])
507 break;
509 if ((bb->in_count > 1) && (j == bb->in_count)) {
510 ins->opcode = op_phi_to_move (ins->opcode);
511 if (ins->opcode == OP_VMOVE)
512 g_assert (ins->klass);
513 ins->sreg1 = first;
514 } else {
515 for (j = 0; j < bb->in_count; j++) {
516 MonoBasicBlock *pred = bb->in_bb [j];
517 int sreg = ins->inst_phi_args [j + 1];
519 if (cfg->verbose_level >= 4)
520 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
521 if (var->dreg != sreg) {
522 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
523 if (move->opcode == OP_VMOVE) {
524 g_assert (ins->klass);
525 move->klass = ins->klass;
527 move->dreg = var->dreg;
528 move->sreg1 = sreg;
529 mono_add_ins_to_end (pred, move);
533 ins->opcode = OP_NOP;
534 ins->dreg = -1;
540 if (cfg->verbose_level >= 4) {
541 for (i = 0; i < cfg->num_bblocks; ++i) {
542 MonoBasicBlock *bb = cfg->bblocks [i];
544 mono_print_bb (bb, "AFTER REMOVE SSA:");
549 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
550 * the variables if possible. Since the newly introduced SSA variables don't
551 * have overlapping live ranges (because we don't do agressive optimization), we
552 * can coalesce them into the original variable.
555 for (bbindex = 0; bbindex < cfg->num_bblocks; ++bbindex) {
556 MonoBasicBlock *bb = cfg->bblocks [bbindex];
558 for (ins = bb->code; ins; ins = ins->next) {
559 const char *spec = INS_INFO (ins->opcode);
560 int num_sregs;
561 int sregs [MONO_MAX_SRC_REGS];
563 if (ins->opcode == OP_NOP)
564 continue;
566 if (spec [MONO_INST_DEST] != ' ') {
567 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
569 if (var) {
570 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
573 * The third condition avoids coalescing with variables eliminated
574 * during deadce.
576 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
577 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
578 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
583 num_sregs = mono_inst_get_src_registers (ins, sregs);
584 for (i = 0; i < num_sregs; ++i) {
585 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
587 if (var) {
588 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
590 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
591 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
592 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
596 mono_inst_set_src_registers (ins, sregs);
600 for (i = 0; i < cfg->num_varinfo; ++i) {
601 MONO_VARINFO (cfg, i)->reg = -1;
604 if (cfg->comp_done & MONO_COMP_REACHABILITY)
605 unlink_unused_bblocks (cfg);
607 cfg->comp_done &= ~MONO_COMP_LIVENESS;
609 cfg->comp_done &= ~MONO_COMP_SSA;
612 static void
613 mono_ssa_create_def_use (MonoCompile *cfg)
615 MonoBasicBlock *bb;
616 MonoInst *ins;
617 int i;
619 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
621 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
622 for (ins = bb->code; ins; ins = ins->next) {
623 const char *spec = INS_INFO (ins->opcode);
624 MonoMethodVar *info;
625 int num_sregs;
626 int sregs [MONO_MAX_SRC_REGS];
628 if (ins->opcode == OP_NOP)
629 continue;
631 /* SREGs */
632 num_sregs = mono_inst_get_src_registers (ins, sregs);
633 for (i = 0; i < num_sregs; ++i) {
634 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
635 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
636 record_use (cfg, var, bb, ins);
639 if (MONO_IS_STORE_MEMBASE (ins)) {
640 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
641 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
642 record_use (cfg, var, bb, ins);
645 if (MONO_IS_PHI (ins)) {
646 for (i = ins->inst_phi_args [0]; i > 0; i--) {
647 g_assert (ins->inst_phi_args [i] != -1);
648 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
652 /* DREG */
653 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
654 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
656 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
657 info = MONO_VARINFO (cfg, var->inst_c0);
658 info->def = ins;
659 info->def_bb = bb;
665 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
668 static void
669 mono_ssa_copyprop (MonoCompile *cfg)
671 int i, index;
672 GList *l;
674 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
676 for (index = 0; index < cfg->num_varinfo; ++index) {
677 MonoInst *var = cfg->varinfo [index];
678 MonoMethodVar *info = MONO_VARINFO (cfg, index);
680 if (info->def && (MONO_IS_MOVE (info->def))) {
681 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
683 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))) {
684 /* Rewrite all uses of var to be uses of var2 */
685 int dreg = var->dreg;
686 int sreg1 = var2->dreg;
688 l = info->uses;
689 while (l) {
690 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
691 MonoInst *ins = u->inst;
692 GList *next = l->next;
693 int num_sregs;
694 int sregs [MONO_MAX_SRC_REGS];
696 num_sregs = mono_inst_get_src_registers (ins, sregs);
697 for (i = 0; i < num_sregs; ++i) {
698 if (sregs [i] == dreg)
699 break;
701 if (i < num_sregs) {
702 g_assert (sregs [i] == dreg);
703 sregs [i] = sreg1;
704 mono_inst_set_src_registers (ins, sregs);
705 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
706 ins->dreg = sreg1;
707 } else if (MONO_IS_PHI (ins)) {
708 for (i = ins->inst_phi_args [0]; i > 0; i--) {
709 int sreg = ins->inst_phi_args [i];
710 if (sreg == var->dreg)
711 break;
713 g_assert (i > 0);
714 ins->inst_phi_args [i] = sreg1;
716 else
717 g_assert_not_reached ();
719 record_use (cfg, var2, u->bb, ins);
721 l = next;
724 info->uses = NULL;
729 if (cfg->verbose_level >= 4) {
730 MonoBasicBlock *bb;
732 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
733 mono_print_bb (bb, "AFTER SSA COPYPROP");
737 static int
738 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
740 MonoInst *args [MONO_MAX_SRC_REGS];
741 int rs [MONO_MAX_SRC_REGS];
742 MonoInst *c0;
743 gboolean const_args = TRUE;
744 const char *spec = INS_INFO (ins->opcode);
745 int num_sregs, i;
746 int sregs [MONO_MAX_SRC_REGS];
748 /* Short-circuit this */
749 if (ins->opcode == OP_ICONST) {
750 *res = ins;
751 return 1;
754 if (ins->opcode == OP_NOP)
755 return 2;
757 num_sregs = mono_inst_get_src_registers (ins, sregs);
758 for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
759 args [i] = NULL;
760 for (i = 0; i < num_sregs; ++i) {
761 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
763 rs [i] = 2;
764 args [i] = carray [sregs [i]];
765 if (args [i])
766 rs [i] = 1;
767 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
768 rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
769 if (rs [i] != 1)
770 const_args = FALSE;
773 c0 = NULL;
775 if (num_sregs > 0 && const_args) {
776 g_assert (num_sregs <= 2);
777 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
778 // Cached value
779 *res = carray [ins->dreg];
780 return 1;
782 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
783 if (c0) {
784 if (G_UNLIKELY (cfg->verbose_level > 1)) {
785 printf ("\t cfold -> ");
786 mono_print_ins (c0);
788 *res = c0;
789 return 1;
791 else
792 /* Can't cfold this ins */
793 return 2;
796 if (num_sregs == 0)
797 return 2;
798 for (i = 0; i < num_sregs; ++i) {
799 if (rs [i] == 2)
800 return 2;
802 return 0;
805 static inline void
806 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
808 if (info->cpstate >= state)
809 return;
811 info->cpstate = state;
813 if (G_UNLIKELY (cfg->verbose_level > 1))
814 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
816 if (state == 1)
817 g_assert (c0);
819 carray [cfg->varinfo [info->idx]->dreg] = c0;
821 if (!g_list_find (*cvars, info)) {
822 *cvars = g_list_prepend (*cvars, info);
826 static inline void
827 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
829 if (G_UNLIKELY (cfg->verbose_level > 1))
830 printf ("\tAdd BB%d to worklist\n", bb->block_num);
832 if (!(bb->flags & BB_REACHABLE)) {
833 bb->flags |= BB_REACHABLE;
834 *bblist = g_list_prepend (*bblist, bb);
838 static void
839 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
841 const char *spec = INS_INFO (ins->opcode);
843 if (ins->opcode == OP_NOP)
844 return;
846 if (cfg->verbose_level > 1)
847 mono_print_ins (ins);
849 /* FIXME: Support longs/floats */
850 /* FIXME: Work on vregs as well */
852 if (MONO_IS_PHI (ins)) {
853 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
854 MonoInst *c0 = NULL;
855 int j;
857 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
858 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
859 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
860 MonoInst *src = mv->def;
862 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
863 continue;
865 if (!mv->def || !src || mv->cpstate == 2) {
866 change_varstate (cfg, cvars, info, 2, NULL, carray);
867 break;
870 if (mv->cpstate == 0)
871 continue;
873 g_assert (carray [var->dreg]);
875 if (!c0)
876 c0 = carray [var->dreg];
878 /* FIXME: */
879 if (c0->opcode != OP_ICONST) {
880 change_varstate (cfg, cvars, info, 2, NULL, carray);
881 break;
884 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
885 change_varstate (cfg, cvars, info, 2, NULL, carray);
886 break;
890 if (c0 && info->cpstate < 1) {
891 change_varstate (cfg, cvars, info, 1, c0, carray);
893 g_assert (c0->opcode == OP_ICONST);
896 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
897 MonoInst *var, *c0;
898 int state;
900 if (spec [MONO_INST_DEST] != ' ')
901 var = get_vreg_to_inst (cfg, ins->dreg);
902 else
903 var = NULL;
905 c0 = NULL;
906 state = evaluate_ins (cfg, ins, &c0, carray);
908 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
909 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
911 if (info->cpstate < 2) {
912 if (state == 1)
913 change_varstate (cfg, cvars, info, 1, c0, carray);
914 else if (state == 2)
915 change_varstate (cfg, cvars, info, 2, NULL, carray);
918 else if (!var && (ins->dreg != -1)) {
920 * We don't record def-use information for local vregs since it would be
921 * expensive. Instead, we depend on the fact that all uses of the vreg are in
922 * the same bblock, so they will be examined after the definition.
923 * FIXME: This isn't true if the ins is visited through an SSA edge.
925 if (c0) {
926 carray [ins->dreg] = c0;
927 } else {
928 if (carray [ins->dreg]) {
930 * The state of the vreg changed from constant to non-constant
931 * -> need to rescan the whole bblock.
933 carray [ins->dreg] = NULL;
934 /* FIXME: Speed this up */
936 if (!g_list_find (*bblist, bb))
937 *bblist = g_list_prepend (*bblist, bb);
942 if (MONO_IS_JUMP_TABLE (ins)) {
943 int i;
944 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
946 if (!ins->next || ins->next->opcode != OP_PADD) {
947 /* The PADD was optimized away */
948 /* FIXME: handle this as well */
949 for (i = 0; i < table->table_size; i++)
950 if (table->table [i])
951 add_cprop_bb (cfg, table->table [i], bblist);
952 return;
955 g_assert (ins->next->opcode == OP_PADD);
956 g_assert (ins->next->sreg1 == ins->dreg);
958 if (carray [ins->next->sreg2]) {
959 #if SIZEOF_REGISTER == 8
960 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
961 #else
962 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
963 #endif
964 if ((idx < 0) || (idx >= table->table_size))
965 /* Out-of-range, no branch is executed */
966 return;
967 else
968 if (table->table [idx])
969 add_cprop_bb (cfg, table->table [idx], bblist);
971 else {
972 for (i = 0; i < table->table_size; i++)
973 if (table->table [i])
974 add_cprop_bb (cfg, table->table [i], bblist);
978 if (ins->opcode == OP_SWITCH) {
979 int i;
980 MonoJumpInfoBBTable *table = ins->inst_p0;
982 for (i = 0; i < table->table_size; i++)
983 if (table->table [i])
984 add_cprop_bb (cfg, table->table [i], bblist);
987 /* Handle COMPARE+BRCOND pairs */
988 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
989 if (c0) {
990 g_assert (c0->opcode == OP_ICONST);
992 if (c0->inst_c0)
993 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
994 else
995 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
997 else {
998 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1001 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1003 } else if (ins->opcode == OP_BR) {
1004 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1005 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1006 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1007 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1008 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1009 if (ins->inst_false_bb)
1010 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1011 } else {
1012 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1013 if (ins->inst_false_bb)
1014 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1020 * fold_ins:
1022 * Replace INS with its constant value, if it exists
1024 static void
1025 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1027 const char *spec = INS_INFO (ins->opcode);
1028 int opcode2;
1029 int num_sregs = mono_inst_get_num_src_registers (ins);
1031 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1032 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1033 /* Perform constant folding */
1034 /* FIXME: */
1035 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1036 ins->opcode = OP_ICONST;
1037 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1038 MONO_INST_NULLIFY_SREGS (ins);
1039 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1040 /* Perform op->op_imm conversion */
1041 opcode2 = mono_op_to_op_imm (ins->opcode);
1042 if (opcode2 != -1) {
1043 ins->opcode = opcode2;
1044 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1045 ins->sreg2 = -1;
1047 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1048 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1050 } else {
1051 /* FIXME: Handle 3 op insns */
1054 if (MONO_IS_JUMP_TABLE (ins)) {
1055 int i;
1056 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1058 if (!ins->next || ins->next->opcode != OP_PADD) {
1059 /* The PADD was optimized away */
1060 /* FIXME: handle this as well */
1061 return;
1064 g_assert (ins->next->opcode == OP_PADD);
1065 g_assert (ins->next->sreg1 == ins->dreg);
1066 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1068 if (carray [ins->next->sreg2]) {
1069 /* Convert to a simple branch */
1070 #if SIZEOF_REGISTER == 8
1071 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1072 #else
1073 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1074 #endif
1076 if (!((idx >= 0) && (idx < table->table_size))) {
1077 /* Out of range, eliminate the whole switch */
1078 for (i = 0; i < table->table_size; ++i) {
1079 remove_bb_from_phis (cfg, bb, table->table [i]);
1080 mono_unlink_bblock (cfg, bb, table->table [i]);
1083 NULLIFY_INS (ins);
1084 NULLIFY_INS (ins->next);
1085 NULLIFY_INS (ins->next->next);
1086 if (ins->next->next->next)
1087 NULLIFY_INS (ins->next->next->next);
1089 return;
1092 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1093 /* A one-way switch which got optimized away */
1094 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1095 printf ("\tNo cfold on ");
1096 mono_print_ins (ins);
1098 return;
1101 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1102 printf ("\tcfold on ");
1103 mono_print_ins (ins);
1106 /* Unlink target bblocks */
1107 for (i = 0; i < table->table_size; ++i) {
1108 if (table->table [i] != table->table [idx]) {
1109 remove_bb_from_phis (cfg, bb, table->table [i]);
1110 mono_unlink_bblock (cfg, bb, table->table [i]);
1114 /* Change the OP_BR_REG to a simple branch */
1115 ins->next->next->next->opcode = OP_BR;
1116 ins->next->next->next->inst_target_bb = table->table [idx];
1117 ins->next->next->next->sreg1 = -1;
1119 /* Nullify the other instructions */
1120 NULLIFY_INS (ins);
1121 NULLIFY_INS (ins->next);
1122 NULLIFY_INS (ins->next->next);
1126 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1127 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1128 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1129 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1130 ins->opcode = OP_BR;
1131 ins->inst_target_bb = ins->inst_true_bb;
1132 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1133 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1134 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1135 ins->opcode = OP_BR;
1136 ins->inst_target_bb = ins->inst_false_bb;
1141 void
1142 mono_ssa_cprop (MonoCompile *cfg)
1144 MonoInst **carray;
1145 MonoBasicBlock *bb;
1146 GList *bblock_list, *cvars;
1147 GList *tmp;
1148 int i;
1149 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1151 carray = g_new0 (MonoInst*, cfg->next_vreg);
1153 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1154 mono_ssa_create_def_use (cfg);
1156 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1157 cfg->bb_entry->flags |= BB_REACHABLE;
1159 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1161 for (i = 0; i < cfg->num_varinfo; i++) {
1162 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1163 if (!info->def)
1164 info->cpstate = 2;
1167 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1169 * FIXME: This should be bb->flags & BB_FLAG_EXCEPTION_HANDLER, but
1170 * that would still allow unreachable try's to be removed.
1172 if (bb->region)
1173 add_cprop_bb (cfg, bb, &bblock_list);
1176 cvars = NULL;
1178 while (bblock_list) {
1179 MonoInst *inst;
1181 bb = (MonoBasicBlock *)bblock_list->data;
1183 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1185 g_assert (bb->flags & BB_REACHABLE);
1188 * Some bblocks are linked to 2 others even through they fall through to the
1189 * next bblock.
1191 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins))) {
1192 for (i = 0; i < bb->out_count; ++i)
1193 add_cprop_bb (cfg, bb->out_bb [i], &bblock_list);
1196 if (cfg->verbose_level > 1)
1197 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1199 for (inst = bb->code; inst; inst = inst->next) {
1200 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1203 while (cvars) {
1204 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1205 cvars = g_list_delete_link (cvars, cvars);
1207 for (tmp = info->uses; tmp; tmp = tmp->next) {
1208 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1209 if (!(ui->bb->flags & BB_REACHABLE))
1210 continue;
1211 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1216 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1217 MonoInst *inst;
1218 for (inst = bb->code; inst; inst = inst->next) {
1219 fold_ins (cfg, bb, inst, carray);
1223 g_free (carray);
1225 cfg->comp_done |= MONO_COMP_REACHABILITY;
1227 /* fixme: we should update usage infos during cprop, instead of computing it again */
1228 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1229 for (i = 0; i < cfg->num_varinfo; i++) {
1230 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1231 info->def = NULL;
1232 info->uses = NULL;
1236 static inline void
1237 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1239 GList *tmp;
1241 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1243 for (tmp = use->uses; tmp; tmp = tmp->next) {
1244 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1245 if (ui->inst == var->def) {
1246 /* from the mempool */
1247 use->uses = g_list_remove_link (use->uses, tmp);
1248 break;
1253 void
1254 mono_ssa_deadce (MonoCompile *cfg)
1256 int i;
1257 GList *work_list;
1259 g_assert (cfg->comp_done & MONO_COMP_SSA);
1261 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1263 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1264 mono_ssa_create_def_use (cfg);
1266 mono_ssa_copyprop (cfg);
1268 work_list = NULL;
1269 for (i = 0; i < cfg->num_varinfo; i++) {
1270 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1271 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1274 while (work_list) {
1275 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1276 work_list = g_list_remove_link (work_list, work_list);
1279 * The second part of the condition happens often when PHI nodes have their dreg
1280 * as one of their arguments due to the fact that we use the original vars.
1282 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1283 MonoInst *def = info->def;
1285 /* Eliminating FMOVE could screw up the fp stack */
1286 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1287 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1288 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1289 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1290 def->opcode = OP_NOP;
1291 def->dreg = def->sreg1 = def->sreg2 = -1;
1292 info->reg = -1;
1293 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1294 def->opcode = OP_NOP;
1295 def->dreg = def->sreg1 = def->sreg2 = -1;
1296 info->reg = -1;
1297 } else if (MONO_IS_PHI (def)) {
1298 int j;
1299 for (j = def->inst_phi_args [0]; j > 0; j--) {
1300 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1301 add_to_dce_worklist (cfg, info, u, &work_list);
1303 def->opcode = OP_NOP;
1304 def->dreg = def->sreg1 = def->sreg2 = -1;
1305 info->reg = -1;
1307 else if (def->opcode == OP_NOP) {
1309 //else
1310 //mono_print_ins (def);
1316 #if 0
1317 void
1318 mono_ssa_strength_reduction (MonoCompile *cfg)
1320 MonoBasicBlock *bb;
1321 int i;
1323 g_assert (cfg->comp_done & MONO_COMP_SSA);
1324 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1325 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1327 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1328 GList *lp = bb->loop_blocks;
1330 if (lp) {
1331 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1333 /* we only consider loops with 2 in bblocks */
1334 if (!h->in_count == 2)
1335 continue;
1337 for (i = 0; i < cfg->num_varinfo; i++) {
1338 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1340 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1341 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1342 MonoInst *v = info->def->inst_i1;
1345 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1351 #endif
1353 void
1354 mono_ssa_loop_invariant_code_motion (MonoCompile *cfg)
1356 MonoBasicBlock *bb, *h, *idom;
1357 MonoInst *ins, *n, *tins;
1358 int i;
1360 g_assert (cfg->comp_done & MONO_COMP_SSA);
1361 if (!(cfg->comp_done & MONO_COMP_LOOPS) || !(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1362 return;
1364 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1365 GList *lp = bb->loop_blocks;
1367 if (!lp)
1368 continue;
1369 h = (MonoBasicBlock *)lp->data;
1370 if (bb != h)
1371 continue;
1372 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1373 gboolean is_class_init = FALSE;
1376 * Try to move instructions out of loop headers into the preceeding bblock.
1378 if (ins->opcode == OP_VOIDCALL) {
1379 MonoCallInst *call = (MonoCallInst*)ins;
1381 if (call->fptr_is_patch) {
1382 MonoJumpInfo *ji = (MonoJumpInfo*)call->fptr;
1384 if (ji->type == MONO_PATCH_INFO_CLASS_INIT)
1385 is_class_init = TRUE;
1388 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS || ins->opcode == OP_AOTCONST || is_class_init) {
1389 gboolean skip;
1390 int sreg;
1392 idom = h->idom;
1394 * h->nesting is needed to work around:
1395 * http://llvm.org/bugs/show_bug.cgi?id=17868
1397 if (!(idom && idom->last_ins && idom->last_ins->opcode == OP_BR && idom->last_ins->inst_target_bb == h && h->nesting == 1)) {
1398 continue;
1402 * Make sure there are no instructions with side effects before ins.
1404 skip = FALSE;
1405 MONO_BB_FOR_EACH_INS (bb, tins) {
1406 if (tins == ins)
1407 break;
1408 if (!MONO_INS_HAS_NO_SIDE_EFFECT (tins)) {
1409 skip = TRUE;
1410 break;
1413 if (skip) {
1415 printf ("%s\n", mono_method_full_name (cfg->method, TRUE));
1416 mono_print_ins (tins);
1418 continue;
1421 /* Make sure we don't move the instruction before the def of its sreg */
1422 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS)
1423 sreg = ins->sreg1;
1424 else
1425 sreg = -1;
1426 if (sreg != -1) {
1427 MonoInst *tins, *var;
1429 skip = FALSE;
1430 for (tins = ins->prev; tins; tins = tins->prev) {
1431 const char *spec = INS_INFO (tins->opcode);
1433 if (tins->opcode == OP_MOVE && tins->dreg == sreg) {
1434 sreg = tins->sreg1;
1435 } if (spec [MONO_INST_DEST] != ' ' && tins->dreg == sreg) {
1436 skip = TRUE;
1437 break;
1440 if (skip)
1441 continue;
1442 var = get_vreg_to_inst (cfg, sreg);
1443 if (var && (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1444 continue;
1445 ins->sreg1 = sreg;
1448 if (cfg->verbose_level > 1) {
1449 printf ("licm in BB%d on ", bb->block_num);
1450 mono_print_ins (ins);
1452 //{ static int count = 0; count ++; printf ("%d\n", count); }
1453 MONO_REMOVE_INS (bb, ins);
1454 mono_bblock_insert_before_ins (idom, idom->last_ins, ins);
1455 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN)
1456 idom->has_array_access = TRUE;
1461 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1462 for (i = 0; i < cfg->num_varinfo; i++) {
1463 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1464 info->def = NULL;
1465 info->uses = NULL;
1469 #endif /* DISABLE_JIT */