[tests] Reenable enum equals test on interpreter (#18673)
[mono-project.git] / mono / mini / ssa.c
blobfa6ec5bb53d9c9461d02b40adf16a15ae8feaafc
1 /**
2 * \file
3 * Static single assign form support for the JIT compiler.
5 * Author:
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #include <config.h>
13 #include <string.h>
14 #include <mono/metadata/debug-helpers.h>
15 #include <mono/metadata/mempool.h>
16 #include <mono/metadata/mempool-internals.h>
17 #include <mono/utils/mono-compiler.h>
19 #ifndef DISABLE_JIT
21 #include "mini.h"
22 #include "mini-runtime.h"
23 #ifdef HAVE_ALLOCA_H
24 #include <alloca.h>
25 #endif
27 #define CREATE_PRUNED_SSA
29 //#define DEBUG_SSA 1
31 #define NEW_PHI(cfg,dest,val) do { \
32 MONO_INST_NEW ((cfg), (dest), OP_PHI); \
33 (dest)->inst_c0 = (val); \
34 } while (0)
36 typedef struct {
37 MonoBasicBlock *bb;
38 MonoInst *inst;
39 } MonoVarUsageInfo;
41 static void
42 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
44 int i;
46 for (i = 0; i < bb->out_count; i++) {
47 if (bb->out_bb [i] == target) {
48 bb->out_bb [i] = bb->out_bb [--bb->out_count];
49 break;
52 for (i = 0; i < target->in_count; i++) {
53 if (target->in_bb [i] == bb) {
54 target->in_bb [i] = target->in_bb [--target->in_count];
55 break;
61 static void
62 unlink_unused_bblocks (MonoCompile *cfg)
64 int i, j;
65 MonoBasicBlock *bb;
67 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
69 if (G_UNLIKELY (cfg->verbose_level > 1))
70 printf ("\nUNLINK UNUSED BBLOCKS:\n");
72 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
73 if (!(bb->next_bb->flags & BB_REACHABLE)) {
74 bb->next_bb = bb->next_bb->next_bb;
75 } else
76 bb = bb->next_bb;
79 for (i = 1; i < cfg->num_bblocks; i++) {
80 bb = cfg->bblocks [i];
82 if (!(bb->flags & BB_REACHABLE)) {
83 for (j = 0; j < bb->in_count; j++) {
84 unlink_target (bb->in_bb [j], bb);
86 for (j = 0; j < bb->out_count; j++) {
87 unlink_target (bb, bb->out_bb [j]);
89 if (G_UNLIKELY (cfg->verbose_level > 1))
90 printf ("\tUnlinked BB%d\n", bb->block_num);
96 /**
97 * remove_bb_from_phis:
99 * Remove BB from the PHI statements in TARGET.
101 static void
102 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
104 MonoInst *ins;
105 int i, j;
107 for (i = 0; i < target->in_count; i++) {
108 if (target->in_bb [i] == bb) {
109 break;
112 g_assert (i < target->in_count);
114 for (ins = target->code; ins; ins = ins->next) {
115 if (MONO_IS_PHI (ins)) {
116 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
117 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
118 ins->inst_phi_args [0] --;
120 else
121 break;
125 static int
126 op_phi_to_move (int opcode)
128 switch (opcode) {
129 case OP_PHI:
130 return OP_MOVE;
131 case OP_FPHI:
132 return OP_FMOVE;
133 case OP_VPHI:
134 return OP_VMOVE;
135 case OP_XPHI:
136 return OP_XMOVE;
137 default:
138 g_assert_not_reached ();
141 return -1;
144 static void
145 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
147 MonoMethodVar *info;
148 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
150 info = MONO_VARINFO (cfg, var->inst_c0);
152 ui->bb = bb;
153 ui->inst = ins;
154 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
157 typedef struct {
158 MonoInst *var;
159 int idx;
160 } RenameInfo;
163 * mono_ssa_rename_vars:
164 * Implement renaming of SSA variables. Also compute def-use information in parallel.
165 * \p 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 if (stack_history_len + 128 > stack_history_size) {
243 stack_history_size += 1024;
244 RenameInfo *new_history = mono_mempool_alloc (cfg->mempool, sizeof (RenameInfo) * stack_history_size);
245 memcpy (new_history, stack_history, stack_history_len * sizeof (RenameInfo));
246 stack_history = new_history;
249 stack_history [stack_history_len].var = stack [idx];
250 stack_history [stack_history_len].idx = idx;
251 stack_history_len ++;
253 if (originals_used [idx]) {
254 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
255 new_var->flags = var->flags;
256 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
258 if (cfg->verbose_level >= 4)
259 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
261 stack [idx] = new_var;
263 ins->dreg = new_var->dreg;
264 var = new_var;
266 else {
267 stack [idx] = var;
268 originals_used [idx] = TRUE;
271 info = MONO_VARINFO (cfg, var->inst_c0);
272 info->def = ins;
273 info->def_bb = bb;
275 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
276 /* Perform renaming for local vregs */
277 lvreg_stack [ins->dreg] = vreg_is_ref (cfg, ins->dreg) ? mono_alloc_ireg_ref (cfg) : mono_alloc_preg (cfg);
278 ins->dreg = lvreg_stack [ins->dreg];
280 else
281 lvreg_defined [ins->dreg] = TRUE;
284 #ifdef DEBUG_SSA
285 printf ("\tAfter processing "); mono_print_ins (ins);
286 #endif
290 /* Rename PHI arguments in succeeding bblocks */
291 for (i = 0; i < bb->out_count; i++) {
292 MonoBasicBlock *n = bb->out_bb [i];
294 for (j = 0; j < n->in_count; j++)
295 if (n->in_bb [j] == bb)
296 break;
298 for (ins = n->code; ins; ins = ins->next) {
299 if (MONO_IS_PHI (ins)) {
300 idx = ins->inst_c0;
301 if (stack [idx])
302 new_var = stack [idx];
303 else
304 new_var = cfg->varinfo [idx];
305 #ifdef DEBUG_SSA
306 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
307 #endif
308 ins->inst_phi_args [j + 1] = new_var->dreg;
309 record_use (cfg, new_var, n, ins);
310 if (G_UNLIKELY (cfg->verbose_level >= 4))
311 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
313 else
314 /* The phi nodes are at the beginning of the bblock */
315 break;
319 if (bb->dominated) {
320 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
321 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);
325 /* Restore stack */
326 for (i = stack_history_len - 1; i >= 0; i--) {
327 stack [stack_history [i].idx] = stack_history [i].var;
330 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
333 void
334 mono_ssa_compute (MonoCompile *cfg)
336 int i, j, idx, bitsize;
337 MonoBitSet *set;
338 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
339 MonoInst *ins, **stack;
340 guint8 *buf, *buf_start;
341 RenameInfo *stack_history;
342 int stack_history_size;
343 gboolean *originals;
344 guint32 *lvreg_stack;
345 gboolean *lvreg_defined;
347 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
349 g_assert (!cfg->disable_ssa);
351 if (cfg->verbose_level >= 4)
352 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
354 #ifdef CREATE_PRUNED_SSA
355 /* we need liveness for pruned SSA */
356 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
357 mono_analyze_liveness (cfg);
358 #endif
360 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
362 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
363 buf = buf_start = (guint8 *)g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
365 for (i = 0; i < cfg->num_varinfo; ++i) {
366 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
367 buf += bitsize;
368 vinfo [i].idx = i;
369 /* implicit reference at start */
370 if (cfg->varinfo [i]->opcode == OP_ARG)
371 mono_bitset_set_fast (vinfo [i].def_in, 0);
374 for (i = 0; i < cfg->num_bblocks; ++i) {
375 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
376 if (ins->opcode == OP_NOP)
377 continue;
379 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
380 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
385 /* insert phi functions */
386 for (i = 0; i < cfg->num_varinfo; ++i) {
387 MonoInst *var = cfg->varinfo [i];
389 #if SIZEOF_REGISTER == 4
390 if (var->type == STACK_I8 && !COMPILE_LLVM (cfg))
391 continue;
392 #endif
393 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
394 continue;
396 /* Most variables have only one definition */
397 if (mono_bitset_count (vinfo [i].def_in) <= 1)
398 continue;
400 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
402 if (cfg->verbose_level >= 4) {
403 if (mono_bitset_count (set) > 0) {
404 printf ("\tR%d needs PHI functions in ", var->dreg);
405 mono_blockset_print (cfg, set, "", -1);
409 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
410 MonoBasicBlock *bb = cfg->bblocks [idx];
412 /* fixme: create pruned SSA? we would need liveness information for that */
414 if (bb == cfg->bb_exit && !COMPILE_LLVM (cfg))
415 continue;
417 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
418 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
419 continue;
422 NEW_PHI (cfg, ins, i);
424 switch (var->type) {
425 case STACK_I4:
426 case STACK_I8:
427 case STACK_PTR:
428 case STACK_MP:
429 case STACK_OBJ:
430 ins->opcode = OP_PHI;
431 break;
432 case STACK_R8:
433 ins->opcode = OP_FPHI;
434 break;
435 case STACK_VTYPE:
436 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
437 break;
440 if (var->inst_vtype->byref)
441 ins->klass = mono_defaults.int_class;
442 else
443 ins->klass = var->klass;
445 ins->inst_phi_args = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
446 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
448 /* For debugging */
449 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
450 ins->inst_phi_args [j + 1] = -1;
452 ins->dreg = cfg->varinfo [i]->dreg;
454 mono_bblock_insert_before_ins (bb, bb->code, ins);
456 #ifdef DEBUG_SSA
457 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
458 #endif
462 /* free the stuff */
463 g_free (vinfo);
464 g_free (buf_start);
466 /* Renaming phase */
468 stack = g_newa (MonoInst*, cfg->num_varinfo);
469 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
471 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
472 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
473 stack_history_size = 10240;
474 stack_history = g_new (RenameInfo, stack_history_size);
475 originals = g_new0 (gboolean, cfg->num_varinfo);
476 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
477 g_free (stack_history);
478 g_free (originals);
479 g_free (lvreg_stack);
480 g_free (lvreg_defined);
482 if (cfg->verbose_level >= 4)
483 printf ("\nEND COMPUTE SSA.\n\n");
485 cfg->comp_done |= MONO_COMP_SSA;
489 * mono_ssa_remove_gsharedvt:
491 * Same as mono_ssa_remove, but only remove phi nodes for gsharedvt variables.
493 void
494 mono_ssa_remove_gsharedvt (MonoCompile *cfg)
496 MonoInst *ins, *var, *move;
497 int i, j, first;
500 * When compiling gsharedvt code, we need to get rid of the VPHI instructions,
501 * since they cannot be handled later in the llvm backend.
503 g_assert (cfg->comp_done & MONO_COMP_SSA);
505 for (i = 0; i < cfg->num_bblocks; ++i) {
506 MonoBasicBlock *bb = cfg->bblocks [i];
508 if (cfg->verbose_level >= 4)
509 printf ("\nREMOVE SSA %d:\n", bb->block_num);
511 for (ins = bb->code; ins; ins = ins->next) {
512 if (!(MONO_IS_PHI (ins) && ins->opcode == OP_VPHI && mini_is_gsharedvt_variable_type (m_class_get_byval_arg (ins->klass))))
513 continue;
515 g_assert (ins->inst_phi_args [0] == bb->in_count);
516 var = get_vreg_to_inst (cfg, ins->dreg);
518 /* Check for PHI nodes where all the inputs are the same */
519 first = ins->inst_phi_args [1];
521 for (j = 1; j < bb->in_count; ++j)
522 if (first != ins->inst_phi_args [j + 1])
523 break;
525 if ((bb->in_count > 1) && (j == bb->in_count)) {
526 ins->opcode = op_phi_to_move (ins->opcode);
527 if (ins->opcode == OP_VMOVE)
528 g_assert (ins->klass);
529 ins->sreg1 = first;
530 } else {
531 for (j = 0; j < bb->in_count; j++) {
532 MonoBasicBlock *pred = bb->in_bb [j];
533 int sreg = ins->inst_phi_args [j + 1];
535 if (cfg->verbose_level >= 4)
536 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
537 if (var->dreg != sreg) {
538 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
539 if (move->opcode == OP_VMOVE) {
540 g_assert (ins->klass);
541 move->klass = ins->klass;
543 move->dreg = var->dreg;
544 move->sreg1 = sreg;
545 mono_add_ins_to_end (pred, move);
549 NULLIFY_INS (ins);
555 void
556 mono_ssa_remove (MonoCompile *cfg)
558 MonoInst *ins, *var, *move;
559 int bbindex, i, j, first;
561 g_assert (cfg->comp_done & MONO_COMP_SSA);
563 for (i = 0; i < cfg->num_bblocks; ++i) {
564 MonoBasicBlock *bb = cfg->bblocks [i];
566 if (cfg->verbose_level >= 4)
567 printf ("\nREMOVE SSA %d:\n", bb->block_num);
569 for (ins = bb->code; ins; ins = ins->next) {
570 if (MONO_IS_PHI (ins)) {
571 g_assert (ins->inst_phi_args [0] == bb->in_count);
572 var = get_vreg_to_inst (cfg, ins->dreg);
574 /* Check for PHI nodes where all the inputs are the same */
575 first = ins->inst_phi_args [1];
577 for (j = 1; j < bb->in_count; ++j)
578 if (first != ins->inst_phi_args [j + 1])
579 break;
581 if ((bb->in_count > 1) && (j == bb->in_count)) {
582 ins->opcode = op_phi_to_move (ins->opcode);
583 if (ins->opcode == OP_VMOVE)
584 g_assert (ins->klass);
585 ins->sreg1 = first;
586 } else {
587 for (j = 0; j < bb->in_count; j++) {
588 MonoBasicBlock *pred = bb->in_bb [j];
589 int sreg = ins->inst_phi_args [j + 1];
591 if (cfg->verbose_level >= 4)
592 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
593 if (var->dreg != sreg) {
594 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
595 if (move->opcode == OP_VMOVE) {
596 g_assert (ins->klass);
597 move->klass = ins->klass;
599 move->dreg = var->dreg;
600 move->sreg1 = sreg;
601 mono_add_ins_to_end (pred, move);
605 NULLIFY_INS (ins);
611 if (cfg->verbose_level >= 4) {
612 for (i = 0; i < cfg->num_bblocks; ++i) {
613 MonoBasicBlock *bb = cfg->bblocks [i];
615 mono_print_bb (bb, "AFTER REMOVE SSA:");
620 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
621 * the variables if possible. Since the newly introduced SSA variables don't
622 * have overlapping live ranges (because we don't do agressive optimization), we
623 * can coalesce them into the original variable.
626 for (bbindex = 0; bbindex < cfg->num_bblocks; ++bbindex) {
627 MonoBasicBlock *bb = cfg->bblocks [bbindex];
629 for (ins = bb->code; ins; ins = ins->next) {
630 const char *spec = INS_INFO (ins->opcode);
631 int num_sregs;
632 int sregs [MONO_MAX_SRC_REGS];
634 if (ins->opcode == OP_NOP)
635 continue;
637 if (spec [MONO_INST_DEST] != ' ') {
638 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
640 if (var) {
641 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
644 * The third condition avoids coalescing with variables eliminated
645 * during deadce.
647 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
648 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
649 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
654 num_sregs = mono_inst_get_src_registers (ins, sregs);
655 for (i = 0; i < num_sregs; ++i) {
656 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
658 if (var) {
659 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
661 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
662 printf ("COALESCE: R%d -> R%d\n", sregs [i], cfg->varinfo [vmv->reg]->dreg);
663 sregs [i] = cfg->varinfo [vmv->reg]->dreg;
667 mono_inst_set_src_registers (ins, sregs);
671 for (i = 0; i < cfg->num_varinfo; ++i) {
672 MONO_VARINFO (cfg, i)->reg = -1;
675 if (cfg->comp_done & MONO_COMP_REACHABILITY)
676 unlink_unused_bblocks (cfg);
678 cfg->comp_done &= ~MONO_COMP_LIVENESS;
680 cfg->comp_done &= ~MONO_COMP_SSA;
683 static void
684 mono_ssa_create_def_use (MonoCompile *cfg)
686 MonoBasicBlock *bb;
687 MonoInst *ins;
688 int i;
690 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
692 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
693 for (ins = bb->code; ins; ins = ins->next) {
694 const char *spec = INS_INFO (ins->opcode);
695 MonoMethodVar *info;
696 int num_sregs;
697 int sregs [MONO_MAX_SRC_REGS];
699 if (ins->opcode == OP_NOP)
700 continue;
702 /* SREGs */
703 num_sregs = mono_inst_get_src_registers (ins, sregs);
704 for (i = 0; i < num_sregs; ++i) {
705 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
706 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
707 record_use (cfg, var, bb, ins);
710 if (MONO_IS_STORE_MEMBASE (ins)) {
711 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
712 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
713 record_use (cfg, var, bb, ins);
716 if (MONO_IS_PHI (ins)) {
717 for (i = ins->inst_phi_args [0]; i > 0; i--) {
718 g_assert (ins->inst_phi_args [i] != -1);
719 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
723 /* DREG */
724 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
725 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
727 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
728 info = MONO_VARINFO (cfg, var->inst_c0);
729 info->def = ins;
730 info->def_bb = bb;
736 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
739 static void
740 mono_ssa_copyprop (MonoCompile *cfg)
742 int i, index;
743 GList *l;
745 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
747 for (index = 0; index < cfg->num_varinfo; ++index) {
748 MonoInst *var = cfg->varinfo [index];
749 MonoMethodVar *info = MONO_VARINFO (cfg, index);
751 if (info->def && (MONO_IS_MOVE (info->def))) {
752 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
754 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))) {
755 /* Rewrite all uses of var to be uses of var2 */
756 int dreg = var->dreg;
757 int sreg1 = var2->dreg;
759 l = info->uses;
760 while (l) {
761 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
762 MonoInst *ins = u->inst;
763 GList *next = l->next;
764 int num_sregs;
765 int sregs [MONO_MAX_SRC_REGS];
767 num_sregs = mono_inst_get_src_registers (ins, sregs);
768 for (i = 0; i < num_sregs; ++i) {
769 if (sregs [i] == dreg)
770 break;
772 if (i < num_sregs) {
773 g_assert (sregs [i] == dreg);
774 sregs [i] = sreg1;
775 mono_inst_set_src_registers (ins, sregs);
776 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
777 ins->dreg = sreg1;
778 } else if (MONO_IS_PHI (ins)) {
779 for (i = ins->inst_phi_args [0]; i > 0; i--) {
780 int sreg = ins->inst_phi_args [i];
781 if (sreg == var->dreg)
782 break;
784 g_assert (i > 0);
785 ins->inst_phi_args [i] = sreg1;
787 else
788 g_assert_not_reached ();
790 record_use (cfg, var2, u->bb, ins);
792 l = next;
795 info->uses = NULL;
800 if (cfg->verbose_level >= 4) {
801 MonoBasicBlock *bb;
803 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
804 mono_print_bb (bb, "AFTER SSA COPYPROP");
808 static int
809 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
811 MonoInst *args [MONO_MAX_SRC_REGS];
812 int rs [MONO_MAX_SRC_REGS];
813 MonoInst *c0;
814 gboolean const_args = TRUE;
815 const char *spec = INS_INFO (ins->opcode);
816 int num_sregs, i;
817 int sregs [MONO_MAX_SRC_REGS];
819 /* Short-circuit this */
820 if (ins->opcode == OP_ICONST) {
821 *res = ins;
822 return 1;
825 if (ins->opcode == OP_NOP)
826 return 2;
828 num_sregs = mono_inst_get_src_registers (ins, sregs);
830 if (num_sregs > 2)
831 return 2;
833 for (i = 0; i < MONO_MAX_SRC_REGS; ++i)
834 args [i] = NULL;
835 for (i = 0; i < num_sregs; ++i) {
836 MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
838 rs [i] = 2;
839 args [i] = carray [sregs [i]];
840 if (args [i])
841 rs [i] = 1;
842 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
843 rs [i] = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
844 if (rs [i] != 1)
845 const_args = FALSE;
848 c0 = NULL;
850 if (num_sregs > 0 && const_args) {
851 g_assert (num_sregs <= 2);
852 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
853 // Cached value
854 *res = carray [ins->dreg];
855 return 1;
857 c0 = mono_constant_fold_ins (cfg, ins, args [0], args [1], FALSE);
858 if (c0) {
859 if (G_UNLIKELY (cfg->verbose_level > 1)) {
860 printf ("\t cfold -> ");
861 mono_print_ins (c0);
863 *res = c0;
864 return 1;
866 else
867 /* Can't cfold this ins */
868 return 2;
871 if (num_sregs == 0)
872 return 2;
873 for (i = 0; i < num_sregs; ++i) {
874 if (rs [i] == 2)
875 return 2;
877 return 0;
880 static void
881 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
883 if (info->cpstate >= state)
884 return;
886 info->cpstate = state;
888 if (G_UNLIKELY (cfg->verbose_level > 1))
889 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
891 if (state == 1)
892 g_assert (c0);
894 carray [cfg->varinfo [info->idx]->dreg] = c0;
896 if (!g_list_find (*cvars, info)) {
897 *cvars = g_list_prepend (*cvars, info);
901 static void
902 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
904 if (G_UNLIKELY (cfg->verbose_level > 1))
905 printf ("\tAdd BB%d to worklist\n", bb->block_num);
907 if (!(bb->flags & BB_REACHABLE)) {
908 bb->flags |= BB_REACHABLE;
909 *bblist = g_list_prepend (*bblist, bb);
913 static void
914 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
916 const char *spec = INS_INFO (ins->opcode);
918 if (ins->opcode == OP_NOP)
919 return;
921 if (cfg->verbose_level > 1)
922 mono_print_ins (ins);
924 /* FIXME: Support longs/floats */
925 /* FIXME: Work on vregs as well */
927 if (MONO_IS_PHI (ins)) {
928 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
929 MonoInst *c0 = NULL;
930 int j;
932 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
933 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
934 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
935 MonoInst *src = mv->def;
937 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
938 continue;
940 if (!mv->def || !src || mv->cpstate == 2) {
941 change_varstate (cfg, cvars, info, 2, NULL, carray);
942 break;
945 if (mv->cpstate == 0)
946 continue;
948 g_assert (carray [var->dreg]);
950 if (!c0)
951 c0 = carray [var->dreg];
953 /* FIXME: */
954 if (c0->opcode != OP_ICONST) {
955 change_varstate (cfg, cvars, info, 2, NULL, carray);
956 break;
959 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
960 change_varstate (cfg, cvars, info, 2, NULL, carray);
961 break;
965 if (c0 && info->cpstate < 1) {
966 change_varstate (cfg, cvars, info, 1, c0, carray);
968 g_assert (c0->opcode == OP_ICONST);
971 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
972 MonoInst *var, *c0;
973 int state;
975 if (spec [MONO_INST_DEST] != ' ')
976 var = get_vreg_to_inst (cfg, ins->dreg);
977 else
978 var = NULL;
980 c0 = NULL;
981 state = evaluate_ins (cfg, ins, &c0, carray);
983 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
984 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
986 if (info->cpstate < 2) {
987 if (state == 1)
988 change_varstate (cfg, cvars, info, 1, c0, carray);
989 else if (state == 2)
990 change_varstate (cfg, cvars, info, 2, NULL, carray);
993 else if (!var && (ins->dreg != -1)) {
995 * We don't record def-use information for local vregs since it would be
996 * expensive. Instead, we depend on the fact that all uses of the vreg are in
997 * the same bblock, so they will be examined after the definition.
998 * FIXME: This isn't true if the ins is visited through an SSA edge.
1000 if (c0) {
1001 carray [ins->dreg] = c0;
1002 } else {
1003 if (carray [ins->dreg]) {
1005 * The state of the vreg changed from constant to non-constant
1006 * -> need to rescan the whole bblock.
1008 carray [ins->dreg] = NULL;
1009 /* FIXME: Speed this up */
1011 if (!g_list_find (*bblist, bb))
1012 *bblist = g_list_prepend (*bblist, bb);
1017 if (MONO_IS_JUMP_TABLE (ins)) {
1018 int i;
1019 MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)MONO_JUMP_TABLE_FROM_INS (ins);
1021 if (!ins->next || ins->next->opcode != OP_PADD) {
1022 /* The PADD was optimized away */
1023 /* FIXME: handle this as well */
1024 for (i = 0; i < table->table_size; i++)
1025 if (table->table [i])
1026 add_cprop_bb (cfg, table->table [i], bblist);
1027 return;
1030 g_assert (ins->next->opcode == OP_PADD);
1031 g_assert (ins->next->sreg1 == ins->dreg);
1033 if (carray [ins->next->sreg2]) {
1034 #if SIZEOF_REGISTER == 8
1035 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1036 #else
1037 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1038 #endif
1039 if ((idx < 0) || (idx >= table->table_size))
1040 /* Out-of-range, no branch is executed */
1041 return;
1042 else
1043 if (table->table [idx])
1044 add_cprop_bb (cfg, table->table [idx], bblist);
1046 else {
1047 for (i = 0; i < table->table_size; i++)
1048 if (table->table [i])
1049 add_cprop_bb (cfg, table->table [i], bblist);
1053 if (ins->opcode == OP_SWITCH) {
1054 int i;
1055 MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)ins->inst_p0;
1057 for (i = 0; i < table->table_size; i++)
1058 if (table->table [i])
1059 add_cprop_bb (cfg, table->table [i], bblist);
1062 /* Handle COMPARE+BRCOND pairs */
1063 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
1064 if (c0) {
1065 g_assert (c0->opcode == OP_ICONST);
1067 if (c0->inst_c0)
1068 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
1069 else
1070 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1072 else {
1073 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1076 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1078 } else if (ins->opcode == OP_BR) {
1079 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1080 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1081 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1082 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1083 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1084 if (ins->inst_false_bb)
1085 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1086 } else {
1087 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1088 if (ins->inst_false_bb)
1089 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1095 * fold_ins:
1097 * Replace INS with its constant value, if it exists
1099 static void
1100 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1102 const char *spec = INS_INFO (ins->opcode);
1103 int opcode2;
1104 int num_sregs = mono_inst_get_num_src_registers (ins);
1106 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1107 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1108 /* Perform constant folding */
1109 /* FIXME: */
1110 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1111 ins->opcode = OP_ICONST;
1112 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1113 MONO_INST_NULLIFY_SREGS (ins);
1114 } else if (num_sregs == 2 && carray [ins->sreg2]) {
1115 /* Perform op->op_imm conversion */
1116 opcode2 = mono_op_to_op_imm (ins->opcode);
1117 if (opcode2 != -1) {
1118 ins->opcode = opcode2;
1119 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1120 ins->sreg2 = -1;
1122 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1123 ((MonoCallInst*)ins)->fptr = (gpointer)(uintptr_t)ins->inst_imm;
1125 } else {
1126 /* FIXME: Handle 3 op insns */
1129 if (MONO_IS_JUMP_TABLE (ins)) {
1130 int i;
1131 MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)MONO_JUMP_TABLE_FROM_INS (ins);
1133 if (!ins->next || ins->next->opcode != OP_PADD) {
1134 /* The PADD was optimized away */
1135 /* FIXME: handle this as well */
1136 return;
1139 g_assert (ins->next->opcode == OP_PADD);
1140 g_assert (ins->next->sreg1 == ins->dreg);
1141 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1143 if (carray [ins->next->sreg2]) {
1144 /* Convert to a simple branch */
1145 #if SIZEOF_REGISTER == 8
1146 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1147 #else
1148 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1149 #endif
1151 if (!((idx >= 0) && (idx < table->table_size))) {
1152 /* Out of range, eliminate the whole switch */
1153 for (i = 0; i < table->table_size; ++i) {
1154 remove_bb_from_phis (cfg, bb, table->table [i]);
1155 mono_unlink_bblock (cfg, bb, table->table [i]);
1158 NULLIFY_INS (ins);
1159 NULLIFY_INS (ins->next);
1160 NULLIFY_INS (ins->next->next);
1161 if (ins->next->next->next)
1162 NULLIFY_INS (ins->next->next->next);
1164 return;
1167 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1168 /* A one-way switch which got optimized away */
1169 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1170 printf ("\tNo cfold on ");
1171 mono_print_ins (ins);
1173 return;
1176 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1177 printf ("\tcfold on ");
1178 mono_print_ins (ins);
1181 /* Unlink target bblocks */
1182 for (i = 0; i < table->table_size; ++i) {
1183 if (table->table [i] != table->table [idx]) {
1184 remove_bb_from_phis (cfg, bb, table->table [i]);
1185 mono_unlink_bblock (cfg, bb, table->table [i]);
1189 /* Change the OP_BR_REG to a simple branch */
1190 ins->next->next->next->opcode = OP_BR;
1191 ins->next->next->next->inst_target_bb = table->table [idx];
1192 ins->next->next->next->sreg1 = -1;
1194 /* Nullify the other instructions */
1195 NULLIFY_INS (ins);
1196 NULLIFY_INS (ins->next);
1197 NULLIFY_INS (ins->next->next);
1201 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1202 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1203 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1204 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1205 ins->opcode = OP_BR;
1206 ins->inst_target_bb = ins->inst_true_bb;
1207 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1208 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1209 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1210 ins->opcode = OP_BR;
1211 ins->inst_target_bb = ins->inst_false_bb;
1216 void
1217 mono_ssa_cprop (MonoCompile *cfg)
1219 MonoInst **carray;
1220 MonoBasicBlock *bb;
1221 GList *bblock_list, *cvars;
1222 GList *tmp;
1223 int i;
1224 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1226 carray = g_new0 (MonoInst*, cfg->next_vreg);
1228 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1229 mono_ssa_create_def_use (cfg);
1231 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1232 cfg->bb_entry->flags |= BB_REACHABLE;
1234 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1236 for (i = 0; i < cfg->num_varinfo; i++) {
1237 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1238 if (!info->def)
1239 info->cpstate = 2;
1242 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1244 * FIXME: This should be bb->flags & BB_FLAG_EXCEPTION_HANDLER, but
1245 * that would still allow unreachable try's to be removed.
1247 if (bb->region)
1248 add_cprop_bb (cfg, bb, &bblock_list);
1251 cvars = NULL;
1253 while (bblock_list) {
1254 MonoInst *inst;
1256 bb = (MonoBasicBlock *)bblock_list->data;
1258 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1260 g_assert (bb->flags & BB_REACHABLE);
1263 * Some bblocks are linked to 2 others even through they fall through to the
1264 * next bblock.
1266 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins))) {
1267 for (i = 0; i < bb->out_count; ++i)
1268 add_cprop_bb (cfg, bb->out_bb [i], &bblock_list);
1271 if (cfg->verbose_level > 1)
1272 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1274 for (inst = bb->code; inst; inst = inst->next) {
1275 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1278 while (cvars) {
1279 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1280 cvars = g_list_delete_link (cvars, cvars);
1282 for (tmp = info->uses; tmp; tmp = tmp->next) {
1283 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1284 if (!(ui->bb->flags & BB_REACHABLE))
1285 continue;
1286 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1291 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1292 MonoInst *inst;
1293 for (inst = bb->code; inst; inst = inst->next) {
1294 fold_ins (cfg, bb, inst, carray);
1298 g_free (carray);
1300 cfg->comp_done |= MONO_COMP_REACHABILITY;
1302 /* fixme: we should update usage infos during cprop, instead of computing it again */
1303 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1304 for (i = 0; i < cfg->num_varinfo; i++) {
1305 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1306 info->def = NULL;
1307 info->uses = NULL;
1311 static void
1312 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1314 GList *tmp;
1316 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1318 for (tmp = use->uses; tmp; tmp = tmp->next) {
1319 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1320 if (ui->inst == var->def) {
1321 /* from the mempool */
1322 use->uses = g_list_remove_link (use->uses, tmp);
1323 break;
1328 void
1329 mono_ssa_deadce (MonoCompile *cfg)
1331 int i;
1332 GList *work_list;
1334 g_assert (cfg->comp_done & MONO_COMP_SSA);
1336 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1338 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1339 mono_ssa_create_def_use (cfg);
1341 mono_ssa_copyprop (cfg);
1343 work_list = NULL;
1344 for (i = 0; i < cfg->num_varinfo; i++) {
1345 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1346 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1349 while (work_list) {
1350 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1351 work_list = g_list_remove_link (work_list, work_list);
1354 * The second part of the condition happens often when PHI nodes have their dreg
1355 * as one of their arguments due to the fact that we use the original vars.
1357 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1358 MonoInst *def = info->def;
1360 /* Eliminating FMOVE could screw up the fp stack */
1361 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1362 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1363 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1364 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1365 NULLIFY_INS (def);
1366 info->reg = -1;
1367 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1368 NULLIFY_INS (def);
1369 info->reg = -1;
1370 } else if (MONO_IS_PHI (def)) {
1371 int j;
1372 for (j = def->inst_phi_args [0]; j > 0; j--) {
1373 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1374 add_to_dce_worklist (cfg, info, u, &work_list);
1376 NULLIFY_INS (def);
1377 info->reg = -1;
1379 else if (def->opcode == OP_NOP) {
1381 //else
1382 //mono_print_ins (def);
1388 #if 0
1389 void
1390 mono_ssa_strength_reduction (MonoCompile *cfg)
1392 MonoBasicBlock *bb;
1393 int i;
1395 g_assert (cfg->comp_done & MONO_COMP_SSA);
1396 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1397 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1399 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1400 GList *lp = bb->loop_blocks;
1402 if (lp) {
1403 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1405 /* we only consider loops with 2 in bblocks */
1406 if (!h->in_count == 2)
1407 continue;
1409 for (i = 0; i < cfg->num_varinfo; i++) {
1410 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1412 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1413 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1414 MonoInst *v = info->def->inst_i1;
1417 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1423 #endif
1425 void
1426 mono_ssa_loop_invariant_code_motion (MonoCompile *cfg)
1428 MonoBasicBlock *bb, *h, *idom;
1429 MonoInst *ins, *n, *tins;
1430 int i;
1432 g_assert (cfg->comp_done & MONO_COMP_SSA);
1433 if (!(cfg->comp_done & MONO_COMP_LOOPS) || !(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1434 return;
1436 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1437 GList *lp = bb->loop_blocks;
1439 if (!lp)
1440 continue;
1441 h = (MonoBasicBlock *)lp->data;
1442 if (bb != h)
1443 continue;
1444 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1446 * Try to move instructions out of loop headers into the preceeding bblock.
1448 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS || ins->opcode == OP_AOTCONST || ins->opcode == OP_GENERIC_CLASS_INIT) {
1449 gboolean skip;
1450 int sreg;
1452 idom = h->idom;
1454 * h->nesting is needed to work around:
1455 * http://llvm.org/bugs/show_bug.cgi?id=17868
1457 if (!(idom && idom->last_ins && idom->last_ins->opcode == OP_BR && idom->last_ins->inst_target_bb == h && h->nesting == 1)) {
1458 continue;
1462 * Make sure there are no instructions with side effects before ins.
1464 skip = FALSE;
1465 MONO_BB_FOR_EACH_INS (bb, tins) {
1466 if (tins == ins)
1467 break;
1468 if (!MONO_INS_HAS_NO_SIDE_EFFECT (tins)) {
1469 skip = TRUE;
1470 break;
1473 if (skip) {
1475 printf ("%s\n", mono_method_full_name (cfg->method, TRUE));
1476 mono_print_ins (tins);
1478 continue;
1481 /* Make sure we don't move the instruction before the def of its sreg */
1482 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS)
1483 sreg = ins->sreg1;
1484 else
1485 sreg = -1;
1486 if (sreg != -1) {
1487 MonoInst *tins, *var;
1489 skip = FALSE;
1490 for (tins = ins->prev; tins; tins = tins->prev) {
1491 const char *spec = INS_INFO (tins->opcode);
1493 if (tins->opcode == OP_MOVE && tins->dreg == sreg) {
1494 sreg = tins->sreg1;
1495 } if (spec [MONO_INST_DEST] != ' ' && tins->dreg == sreg) {
1496 skip = TRUE;
1497 break;
1500 if (skip)
1501 continue;
1502 var = get_vreg_to_inst (cfg, sreg);
1503 if (var && (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1504 continue;
1505 ins->sreg1 = sreg;
1508 /* if any successor block of the immediate post dominator is an
1509 * exception handler, it's not safe to do the code motion */
1510 skip = FALSE;
1511 for (int j = 0; j < idom->out_count && !skip; j++)
1512 skip |= !!(idom->out_bb [j]->flags & BB_EXCEPTION_HANDLER);
1513 if (skip)
1514 continue;
1516 if (cfg->verbose_level > 1) {
1517 printf ("licm in BB%d on ", bb->block_num);
1518 mono_print_ins (ins);
1520 //{ static int count = 0; count ++; printf ("%d\n", count); }
1521 MONO_REMOVE_INS (bb, ins);
1522 mono_bblock_insert_before_ins (idom, idom->last_ins, ins);
1523 if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN)
1524 idom->needs_decompose = TRUE;
1529 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1530 for (i = 0; i < cfg->num_varinfo; i++) {
1531 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1532 info->def = NULL;
1533 info->uses = NULL;
1537 #else /* !DISABLE_JIT */
1539 MONO_EMPTY_SOURCE_FILE (ssa);
1541 #endif /* !DISABLE_JIT */