2009-03-11 Zoltan Varga <vargaz@gmail.com>
[mono-debugger.git] / mono / mini / ssa.c
blob4b286b3329fc8fe3a1d230ce8cf24926ee92e240
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 */
9 #include <config.h>
10 #include <string.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/mempool.h>
13 #include <mono/metadata/mempool-internals.h>
15 #ifndef DISABLE_JIT
17 #include "mini.h"
18 #ifdef HAVE_ALLOCA_H
19 #include <alloca.h>
20 #endif
22 #define USE_ORIGINAL_VARS
23 #define CREATE_PRUNED_SSA
25 //#define DEBUG_SSA 1
27 #define NEW_PHI(cfg,dest,val) do { \
28 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
29 (dest)->opcode = OP_PHI; \
30 (dest)->inst_c0 = (val); \
31 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
32 } while (0)
34 typedef struct {
35 MonoBasicBlock *bb;
36 MonoInst *inst;
37 } MonoVarUsageInfo;
39 static void
40 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
42 int i;
44 for (i = 0; i < bb->out_count; i++) {
45 if (bb->out_bb [i] == target) {
46 bb->out_bb [i] = bb->out_bb [--bb->out_count];
47 break;
50 for (i = 0; i < target->in_count; i++) {
51 if (target->in_bb [i] == bb) {
52 target->in_bb [i] = target->in_bb [--target->in_count];
53 break;
59 static void
60 unlink_unused_bblocks (MonoCompile *cfg)
62 int i, j;
63 MonoBasicBlock *bb;
65 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
67 if (G_UNLIKELY (cfg->verbose_level > 1))
68 printf ("\nUNLINK UNUSED BBLOCKS:\n");
70 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
71 if (!(bb->next_bb->flags & BB_REACHABLE)) {
72 bb->next_bb = bb->next_bb->next_bb;
73 } else
74 bb = bb->next_bb;
77 for (i = 1; i < cfg->num_bblocks; i++) {
78 bb = cfg->bblocks [i];
80 if (!(bb->flags & BB_REACHABLE)) {
81 for (j = 0; j < bb->in_count; j++) {
82 unlink_target (bb->in_bb [j], bb);
84 for (j = 0; j < bb->out_count; j++) {
85 unlink_target (bb, bb->out_bb [j]);
87 if (G_UNLIKELY (cfg->verbose_level > 1))
88 printf ("\tUnlinked BB%d\n", bb->block_num);
94 /**
95 * remove_bb_from_phis:
97 * Remove BB from the PHI statements in TARGET.
99 static void
100 remove_bb_from_phis (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *target)
102 MonoInst *ins;
103 int i, j;
105 for (i = 0; i < target->in_count; i++) {
106 if (target->in_bb [i] == bb) {
107 break;
110 g_assert (i < target->in_count);
112 for (ins = target->code; ins; ins = ins->next) {
113 if (MONO_IS_PHI (ins)) {
114 for (j = i; j < ins->inst_phi_args [0] - 1; ++j)
115 ins->inst_phi_args [j + 1] = ins->inst_phi_args [j + 2];
116 ins->inst_phi_args [0] --;
118 else
119 break;
123 static inline int
124 op_phi_to_move (int opcode)
126 switch (opcode) {
127 case OP_PHI:
128 return OP_MOVE;
129 case OP_FPHI:
130 return OP_FMOVE;
131 case OP_VPHI:
132 return OP_VMOVE;
133 case OP_XPHI:
134 return OP_XMOVE;
135 default:
136 g_assert_not_reached ();
139 return -1;
142 static inline void
143 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
145 MonoMethodVar *info;
146 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
148 info = MONO_VARINFO (cfg, var->inst_c0);
150 ui->bb = bb;
151 ui->inst = ins;
152 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
155 typedef struct {
156 MonoInst *var;
157 int idx;
158 } RenameInfo;
161 * mono_ssa_rename_vars:
163 * Implement renaming of SSA variables. Also compute def-use information in parallel.
164 * @stack_history points to an area of memory which can be used for storing changes
165 * made to the stack, so they can be reverted later.
167 static void
168 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)
170 MonoInst *ins, *new_var;
171 int i, j, idx;
172 GSList *tmp;
173 int stack_history_len = 0;
175 if (cfg->verbose_level >= 4)
176 printf ("\nRENAME VARS BLOCK %d:\n", bb->block_num);
178 /* First pass: Create new vars */
179 for (ins = bb->code; ins; ins = ins->next) {
180 const char *spec = INS_INFO (ins->opcode);
182 #ifdef DEBUG_SSA
183 printf ("\tProcessing "); mono_print_ins (ins);
184 #endif
185 if (ins->opcode == OP_NOP)
186 continue;
188 /* SREG1 */
189 if (spec [MONO_INST_SRC1] != ' ') {
190 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
191 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
192 int idx = var->inst_c0;
193 if (stack [idx]) {
194 if (var->opcode != OP_ARG)
195 g_assert (stack [idx]);
196 ins->sreg1 = stack [idx]->dreg;
197 record_use (cfg, stack [idx], bb, ins);
199 else
200 record_use (cfg, var, bb, ins);
202 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg1]))
203 ins->sreg1 = lvreg_stack [ins->sreg1];
206 /* SREG2 */
207 if (spec [MONO_INST_SRC2] != ' ') {
208 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
209 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
210 int idx = var->inst_c0;
211 if (stack [idx]) {
212 if (var->opcode != OP_ARG)
213 g_assert (stack [idx]);
215 ins->sreg2 = stack [idx]->dreg;
216 record_use (cfg, stack [idx], bb, ins);
218 else
219 record_use (cfg, var, bb, ins);
221 else if (G_UNLIKELY (!var && lvreg_stack [ins->sreg2]))
222 ins->sreg2 = lvreg_stack [ins->sreg2];
225 if (MONO_IS_STORE_MEMBASE (ins)) {
226 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
227 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
228 int idx = var->inst_c0;
229 if (stack [idx]) {
230 if (var->opcode != OP_ARG)
231 g_assert (stack [idx]);
232 ins->dreg = stack [idx]->dreg;
233 record_use (cfg, stack [idx], bb, ins);
235 else
236 record_use (cfg, var, bb, ins);
238 else if (G_UNLIKELY (!var && lvreg_stack [ins->dreg]))
239 ins->dreg = lvreg_stack [ins->dreg];
242 /* DREG */
243 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
244 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
245 MonoMethodVar *info;
247 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
248 idx = var->inst_c0;
249 g_assert (idx < max_vars);
251 if (var->opcode == OP_ARG)
252 originals_used [idx] = TRUE;
254 /* FIXME: */
255 g_assert (stack_history_len < stack_history_size);
256 stack_history [stack_history_len].var = stack [idx];
257 stack_history [stack_history_len].idx = idx;
258 stack_history_len ++;
260 if (originals_used [idx]) {
261 new_var = mono_compile_create_var (cfg, var->inst_vtype, OP_LOCAL);
262 new_var->flags = var->flags;
263 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
265 if (cfg->verbose_level >= 4)
266 printf (" R%d -> R%d\n", var->dreg, new_var->dreg);
268 stack [idx] = new_var;
270 ins->dreg = new_var->dreg;
271 var = new_var;
273 else {
274 stack [idx] = var;
275 originals_used [idx] = TRUE;
278 info = MONO_VARINFO (cfg, var->inst_c0);
279 info->def = ins;
280 info->def_bb = bb;
282 else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
283 /* Perform renaming for local vregs */
284 lvreg_stack [ins->dreg] = mono_alloc_preg (cfg);
285 ins->dreg = lvreg_stack [ins->dreg];
287 else
288 lvreg_defined [ins->dreg] = TRUE;
291 #ifdef DEBUG_SSA
292 printf ("\tAfter processing "); mono_print_ins (ins);
293 #endif
297 /* Rename PHI arguments in succeeding bblocks */
298 for (i = 0; i < bb->out_count; i++) {
299 MonoBasicBlock *n = bb->out_bb [i];
301 for (j = 0; j < n->in_count; j++)
302 if (n->in_bb [j] == bb)
303 break;
305 for (ins = n->code; ins; ins = ins->next) {
306 if (MONO_IS_PHI (ins)) {
307 idx = ins->inst_c0;
308 if (stack [idx])
309 new_var = stack [idx];
310 else
311 new_var = cfg->varinfo [idx];
312 #ifdef DEBUG_SSA
313 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
314 #endif
315 ins->inst_phi_args [j + 1] = new_var->dreg;
316 record_use (cfg, new_var, n, ins);
317 if (G_UNLIKELY (cfg->verbose_level >= 4))
318 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins->dreg, new_var->dreg, n->block_num);
320 else
321 /* The phi nodes are at the beginning of the bblock */
322 break;
326 if (bb->dominated) {
327 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
328 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);
332 /* Restore stack */
333 for (i = stack_history_len - 1; i >= 0; i--) {
334 stack [stack_history [i].idx] = stack_history [i].var;
337 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
340 void
341 mono_ssa_compute (MonoCompile *cfg)
343 int i, j, idx, bitsize;
344 MonoBitSet *set;
345 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
346 MonoInst *ins, **stack;
347 guint8 *buf, *buf_start;
348 RenameInfo *stack_history;
349 int stack_history_size;
350 gboolean *originals;
351 guint32 *lvreg_stack;
352 gboolean *lvreg_defined;
354 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
356 /* we dont support methods containing exception clauses */
357 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
358 g_assert (!cfg->disable_ssa);
360 if (cfg->verbose_level >= 4)
361 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg->num_varinfo, cfg->next_vreg);
363 #ifdef CREATE_PRUNED_SSA
364 /* we need liveness for pruned SSA */
365 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
366 mono_analyze_liveness (cfg);
367 #endif
369 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
371 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
372 buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
374 for (i = 0; i < cfg->num_varinfo; ++i) {
375 vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
376 buf += bitsize;
377 vinfo [i].idx = i;
378 /* implicit reference at start */
379 if (cfg->varinfo [i]->opcode == OP_ARG)
380 mono_bitset_set_fast (vinfo [i].def_in, 0);
383 for (i = 0; i < cfg->num_bblocks; ++i) {
384 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], ins) {
385 if (ins->opcode == OP_NOP)
386 continue;
388 if (!MONO_IS_STORE_MEMBASE (ins) && get_vreg_to_inst (cfg, ins->dreg)) {
389 mono_bitset_set_fast (vinfo [get_vreg_to_inst (cfg, ins->dreg)->inst_c0].def_in, i);
394 /* insert phi functions */
395 for (i = 0; i < cfg->num_varinfo; ++i) {
396 MonoInst *var = cfg->varinfo [i];
398 #if SIZEOF_REGISTER == 4
399 if (var->type == STACK_I8)
400 continue;
401 #endif
402 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
403 continue;
405 /* Most variables have only one definition */
406 if (mono_bitset_count (vinfo [i].def_in) <= 1)
407 continue;
409 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
411 if (cfg->verbose_level >= 4) {
412 if (mono_bitset_count (set) > 0) {
413 printf ("\tR%d needs PHI functions in ", var->dreg);
414 mono_blockset_print (cfg, set, "", -1);
418 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
419 MonoBasicBlock *bb = cfg->bblocks [idx];
421 /* fixme: create pruned SSA? we would need liveness information for that */
423 if (bb == cfg->bb_exit)
424 continue;
426 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
427 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
428 continue;
431 NEW_PHI (cfg, ins, i);
433 switch (var->type) {
434 case STACK_I4:
435 case STACK_I8:
436 case STACK_PTR:
437 case STACK_MP:
438 case STACK_OBJ:
439 ins->opcode = OP_PHI;
440 break;
441 case STACK_R8:
442 ins->opcode = OP_FPHI;
443 break;
444 case STACK_VTYPE:
445 ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
446 ins->klass = var->klass;
447 break;
450 ins->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
451 ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
453 /* For debugging */
454 for (j = 0; j < cfg->bblocks [idx]->in_count; ++j)
455 ins->inst_phi_args [j + 1] = -1;
457 ins->dreg = cfg->varinfo [i]->dreg;
459 mono_bblock_insert_before_ins (bb, bb->code, ins);
461 #ifdef DEBUG_SSA
462 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
463 #endif
467 /* free the stuff */
468 g_free (vinfo);
469 g_free (buf_start);
471 /* Renaming phase */
473 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
474 memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
476 lvreg_stack = g_new0 (guint32, cfg->next_vreg);
477 lvreg_defined = g_new0 (gboolean, cfg->next_vreg);
478 stack_history_size = 10240;
479 stack_history = g_new (RenameInfo, stack_history_size);
480 originals = g_new0 (gboolean, cfg->num_varinfo);
481 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
482 g_free (stack_history);
483 g_free (originals);
485 if (cfg->verbose_level >= 4)
486 printf ("\nEND COMPUTE SSA.\n\n");
488 cfg->comp_done |= MONO_COMP_SSA;
491 void
492 mono_ssa_remove (MonoCompile *cfg)
494 MonoInst *ins, *var, *move;
495 int i, j, first;
497 g_assert (cfg->comp_done & MONO_COMP_SSA);
499 for (i = 0; i < cfg->num_bblocks; ++i) {
500 MonoBasicBlock *bb = cfg->bblocks [i];
502 if (cfg->verbose_level >= 4)
503 printf ("\nREMOVE SSA %d:\n", bb->block_num);
505 for (ins = bb->code; ins; ins = ins->next) {
506 if (MONO_IS_PHI (ins)) {
507 g_assert (ins->inst_phi_args [0] == bb->in_count);
508 var = get_vreg_to_inst (cfg, ins->dreg);
510 /* Check for PHI nodes where all the inputs are the same */
511 first = ins->inst_phi_args [1];
513 for (j = 1; j < bb->in_count; ++j)
514 if (first != ins->inst_phi_args [j + 1])
515 break;
517 if ((bb->in_count > 1) && (j == bb->in_count)) {
518 ins->opcode = op_phi_to_move (ins->opcode);
519 if (ins->opcode == OP_VMOVE)
520 g_assert (ins->klass);
521 ins->sreg1 = first;
522 } else {
523 for (j = 0; j < bb->in_count; j++) {
524 MonoBasicBlock *pred = bb->in_bb [j];
525 int sreg = ins->inst_phi_args [j + 1];
527 if (cfg->verbose_level >= 4)
528 printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
529 if (var->dreg != sreg) {
530 MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
531 if (move->opcode == OP_VMOVE) {
532 g_assert (ins->klass);
533 move->klass = ins->klass;
535 move->dreg = var->dreg;
536 move->sreg1 = sreg;
537 mono_add_ins_to_end (pred, move);
541 ins->opcode = OP_NOP;
542 ins->dreg = -1;
548 if (cfg->verbose_level >= 4) {
549 for (i = 0; i < cfg->num_bblocks; ++i) {
550 MonoBasicBlock *bb = cfg->bblocks [i];
552 mono_print_bb (bb, "AFTER REMOVE SSA:");
557 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
558 * the variables if possible. Since the newly introduced SSA variables don't
559 * have overlapping live ranges (because we don't do agressive optimization), we
560 * can coalesce them into the original variable.
563 for (i = 0; i < cfg->num_bblocks; ++i) {
564 MonoBasicBlock *bb = cfg->bblocks [i];
566 for (ins = bb->code; ins; ins = ins->next) {
567 const char *spec = INS_INFO (ins->opcode);
569 if (ins->opcode == OP_NOP)
570 continue;
572 if (spec [MONO_INST_DEST] != ' ') {
573 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
575 if (var) {
576 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
579 * The third condition avoids coalescing with variables eliminated
580 * during deadce.
582 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
583 printf ("COALESCE: R%d -> R%d\n", ins->dreg, cfg->varinfo [vmv->reg]->dreg);
584 ins->dreg = cfg->varinfo [vmv->reg]->dreg;
589 if (spec [MONO_INST_SRC1] != ' ') {
590 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
592 if (var) {
593 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
595 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
596 printf ("COALESCE: R%d -> R%d\n", ins->sreg1, cfg->varinfo [vmv->reg]->dreg);
597 ins->sreg1 = cfg->varinfo [vmv->reg]->dreg;
602 if (spec [MONO_INST_SRC2] != ' ') {
603 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
605 if (var) {
606 MonoMethodVar *vmv = MONO_VARINFO (cfg, var->inst_c0);
608 if ((vmv->reg != -1) && (vmv->idx != vmv->reg) && (MONO_VARINFO (cfg, vmv->reg)->reg != -1)) {
609 printf ("COALESCE: R%d -> R%d\n", ins->sreg2, cfg->varinfo [vmv->reg]->dreg);
610 ins->sreg2 = cfg->varinfo [vmv->reg]->dreg;
618 for (i = 0; i < cfg->num_varinfo; ++i) {
619 MONO_VARINFO (cfg, i)->reg = -1;
622 if (cfg->comp_done & MONO_COMP_REACHABILITY)
623 unlink_unused_bblocks (cfg);
625 cfg->comp_done &= ~MONO_COMP_LIVENESS;
627 cfg->comp_done &= ~MONO_COMP_SSA;
630 static void
631 mono_ssa_create_def_use (MonoCompile *cfg)
633 MonoBasicBlock *bb;
634 MonoInst *ins;
635 int i;
637 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
639 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
640 for (ins = bb->code; ins; ins = ins->next) {
641 const char *spec = INS_INFO (ins->opcode);
642 MonoMethodVar *info;
644 if (ins->opcode == OP_NOP)
645 continue;
647 /* SREG1 */
648 if (spec [MONO_INST_SRC1] != ' ') {
649 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
650 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
651 record_use (cfg, var, bb, ins);
654 /* SREG2 */
655 if (spec [MONO_INST_SRC2] != ' ') {
656 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
657 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
658 record_use (cfg, var, bb, ins);
661 if (MONO_IS_STORE_MEMBASE (ins)) {
662 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
663 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
664 record_use (cfg, var, bb, ins);
667 if (MONO_IS_PHI (ins)) {
668 for (i = ins->inst_phi_args [0]; i > 0; i--) {
669 g_assert (ins->inst_phi_args [i] != -1);
670 record_use (cfg, get_vreg_to_inst (cfg, ins->inst_phi_args [i]), bb, ins);
674 /* DREG */
675 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
676 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
678 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
679 info = MONO_VARINFO (cfg, var->inst_c0);
680 info->def = ins;
681 info->def_bb = bb;
687 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
690 static void
691 mono_ssa_copyprop (MonoCompile *cfg)
693 int i, index;
694 GList *l;
696 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
698 for (index = 0; index < cfg->num_varinfo; ++index) {
699 MonoInst *var = cfg->varinfo [index];
700 MonoMethodVar *info = MONO_VARINFO (cfg, index);
702 if (info->def && (MONO_IS_MOVE (info->def))) {
703 MonoInst *var2 = get_vreg_to_inst (cfg, info->def->sreg1);
705 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))) {
706 /* Rewrite all uses of var to be uses of var2 */
707 int dreg = var->dreg;
708 int sreg1 = var2->dreg;
709 const char *spec;
711 l = info->uses;
712 while (l) {
713 MonoVarUsageInfo *u = (MonoVarUsageInfo*)l->data;
714 MonoInst *ins = u->inst;
715 GList *next = l->next;
717 spec = INS_INFO (ins->opcode);
719 if (spec [MONO_INST_SRC1] != ' ' && ins->sreg1 == dreg) {
720 ins->sreg1 = sreg1;
721 } else if (spec [MONO_INST_SRC2] != ' ' && ins->sreg2 == dreg) {
722 ins->sreg2 = sreg1;
723 } else if (MONO_IS_STORE_MEMBASE (ins) && ins->dreg == dreg) {
724 ins->dreg = sreg1;
725 } else if (MONO_IS_PHI (ins)) {
726 for (i = ins->inst_phi_args [0]; i > 0; i--) {
727 int sreg = ins->inst_phi_args [i];
728 if (sreg == var->dreg)
729 break;
731 g_assert (i > 0);
732 ins->inst_phi_args [i] = sreg1;
734 else
735 g_assert_not_reached ();
737 record_use (cfg, var2, u->bb, ins);
739 l = next;
742 info->uses = NULL;
747 if (cfg->verbose_level >= 4) {
748 MonoBasicBlock *bb;
750 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
751 mono_print_bb (bb, "AFTER SSA COPYPROP");
755 static int
756 evaluate_ins (MonoCompile *cfg, MonoInst *ins, MonoInst **res, MonoInst **carray)
758 MonoInst *arg0, *arg1, *c0;
759 int r1, r2;
760 gboolean const_args = FALSE;
761 const char *spec = INS_INFO (ins->opcode);
763 /* Short-circuit this */
764 if (ins->opcode == OP_ICONST) {
765 *res = ins;
766 return 1;
769 if (ins->opcode == OP_NOP)
770 return 2;
772 arg0 = NULL;
773 if (spec [MONO_INST_SRC1] != ' ') {
774 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
776 r1 = 2;
777 arg0 = carray [ins->sreg1];
778 if (arg0)
779 r1 = 1;
780 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
781 r1 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
782 } else {
783 r1 = 2;
786 arg1 = NULL;
787 if (spec [MONO_INST_SRC2] != ' ') {
788 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
790 r2 = 2;
791 arg1 = carray [ins->sreg2];
792 if (arg1)
793 r2 = 1;
794 else if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
795 r2 = MONO_VARINFO (cfg, var->inst_c0)->cpstate;
797 else {
798 r2 = 0;
801 c0 = NULL;
802 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
803 /* Binop */
804 const_args = (r1 == 1) && (r2 == 1);
806 else if (spec [MONO_INST_SRC1] != ' ') {
807 /* Unop */
808 const_args = (r1 == 1);
811 if (const_args) {
812 if ((spec [MONO_INST_DEST] != ' ') && carray [ins->dreg]) {
813 // Cached value
814 *res = carray [ins->dreg];
815 return 1;
817 c0 = mono_constant_fold_ins (cfg, ins, arg0, arg1, FALSE);
818 if (c0) {
819 if (G_UNLIKELY (cfg->verbose_level > 1)) {
820 printf ("\t cfold -> ");
821 mono_print_ins (c0);
823 *res = c0;
824 return 1;
826 else
827 /* Can't cfold this ins */
828 return 2;
831 if ((spec [MONO_INST_SRC1] != ' ') && (spec [MONO_INST_SRC2] != ' ')) {
832 /* Binop */
833 if ((r1 == 2) || (r2 == 2))
834 return 2;
835 else
836 return 0;
838 else if (spec [MONO_INST_SRC1] != ' ') {
839 /* Unop */
840 if (r1 == 2)
841 return 2;
842 else
843 return 0;
845 else
846 return 2;
849 static inline void
850 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
852 if (info->cpstate >= state)
853 return;
855 info->cpstate = state;
857 if (G_UNLIKELY (cfg->verbose_level > 1))
858 printf ("\tState of R%d set to %d\n", cfg->varinfo [info->idx]->dreg, info->cpstate);
860 if (state == 1)
861 g_assert (c0);
863 carray [cfg->varinfo [info->idx]->dreg] = c0;
865 if (!g_list_find (*cvars, info)) {
866 *cvars = g_list_prepend (*cvars, info);
870 static inline void
871 add_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, GList **bblist)
873 if (G_UNLIKELY (cfg->verbose_level > 1))
874 printf ("\tAdd BB%d to worklist\n", bb->block_num);
876 if (!(bb->flags & BB_REACHABLE)) {
877 bb->flags |= BB_REACHABLE;
878 *bblist = g_list_prepend (*bblist, bb);
882 static void
883 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars, GList **bblist, MonoInst **carray)
885 const char *spec = INS_INFO (ins->opcode);
887 if (ins->opcode == OP_NOP)
888 return;
890 if (cfg->verbose_level > 1)
891 mono_print_ins (ins);
893 /* FIXME: Support longs/floats */
894 /* FIXME: Work on vregs as well */
896 if (MONO_IS_PHI (ins)) {
897 MonoMethodVar *info = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, ins->dreg)->inst_c0);
898 MonoInst *c0 = NULL;
899 int j;
901 for (j = 1; j <= ins->inst_phi_args [0]; j++) {
902 MonoInst *var = get_vreg_to_inst (cfg, ins->inst_phi_args [j]);
903 MonoMethodVar *mv = MONO_VARINFO (cfg, var->inst_c0);
904 MonoInst *src = mv->def;
906 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE))
907 continue;
909 if (!mv->def || !src || mv->cpstate == 2) {
910 change_varstate (cfg, cvars, info, 2, NULL, carray);
911 break;
914 if (mv->cpstate == 0)
915 continue;
917 g_assert (carray [var->dreg]);
919 if (!c0)
920 c0 = carray [var->dreg];
922 /* FIXME: */
923 if (c0->opcode != OP_ICONST) {
924 change_varstate (cfg, cvars, info, 2, NULL, carray);
925 break;
928 if (carray [var->dreg]->inst_c0 != c0->inst_c0) {
929 change_varstate (cfg, cvars, info, 2, NULL, carray);
930 break;
934 if (c0 && info->cpstate < 1) {
935 change_varstate (cfg, cvars, info, 1, c0, carray);
937 g_assert (c0->opcode == OP_ICONST);
940 else if (!MONO_IS_STORE_MEMBASE (ins) && ((spec [MONO_INST_SRC1] != ' ') || (spec [MONO_INST_SRC2] != ' ') || (spec [MONO_INST_DEST] != ' '))) {
941 MonoInst *var, *c0;
942 int state;
944 if (spec [MONO_INST_DEST] != ' ')
945 var = get_vreg_to_inst (cfg, ins->dreg);
946 else
947 var = NULL;
949 c0 = NULL;
950 state = evaluate_ins (cfg, ins, &c0, carray);
952 if (var && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
953 MonoMethodVar *info = MONO_VARINFO (cfg, var->inst_c0);
955 if (info->cpstate < 2) {
956 if (state == 1)
957 change_varstate (cfg, cvars, info, 1, c0, carray);
958 else if (state == 2)
959 change_varstate (cfg, cvars, info, 2, NULL, carray);
962 else if (!var && (ins->dreg != -1)) {
964 * We don't record def-use information for local vregs since it would be
965 * expensive. Instead, we depend on the fact that all uses of the vreg are in
966 * the same bblock, so they will be examined after the definition.
967 * FIXME: This isn't true if the ins is visited through an SSA edge.
969 if (c0) {
970 carray [ins->dreg] = c0;
971 } else {
972 if (carray [ins->dreg]) {
974 * The state of the vreg changed from constant to non-constant
975 * -> need to rescan the whole bblock.
977 carray [ins->dreg] = NULL;
978 /* FIXME: Speed this up */
980 if (!g_list_find (*bblist, bb))
981 *bblist = g_list_prepend (*bblist, bb);
986 if (MONO_IS_JUMP_TABLE (ins)) {
987 int i;
988 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
990 if (!ins->next || ins->next->opcode != OP_PADD) {
991 /* The PADD was optimized away */
992 /* FIXME: handle this as well */
993 for (i = 0; i < table->table_size; i++)
994 if (table->table [i])
995 add_cprop_bb (cfg, table->table [i], bblist);
996 return;
999 g_assert (ins->next->opcode == OP_PADD);
1000 g_assert (ins->next->sreg1 == ins->dreg);
1002 if (carray [ins->next->sreg2]) {
1003 #if SIZEOF_REGISTER == 8
1004 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1005 #else
1006 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1007 #endif
1008 if ((idx < 0) || (idx >= table->table_size))
1009 /* Out-of-range, no branch is executed */
1010 return;
1011 else
1012 if (table->table [idx])
1013 add_cprop_bb (cfg, table->table [idx], bblist);
1015 else {
1016 for (i = 0; i < table->table_size; i++)
1017 if (table->table [i])
1018 add_cprop_bb (cfg, table->table [i], bblist);
1022 if (ins->opcode == OP_SWITCH) {
1023 int i;
1024 MonoJumpInfoBBTable *table = ins->inst_p0;
1026 for (i = 0; i < table->table_size; i++)
1027 if (table->table [i])
1028 add_cprop_bb (cfg, table->table [i], bblist);
1031 /* Handle COMPARE+BRCOND pairs */
1032 if (ins->next && MONO_IS_COND_BRANCH_OP (ins->next)) {
1033 if (c0) {
1034 g_assert (c0->opcode == OP_ICONST);
1036 if (c0->inst_c0)
1037 ins->next->flags |= MONO_INST_CFOLD_TAKEN;
1038 else
1039 ins->next->flags |= MONO_INST_CFOLD_NOT_TAKEN;
1041 else {
1042 ins->next->flags &= ~(MONO_INST_CFOLD_TAKEN | MONO_INST_CFOLD_NOT_TAKEN);
1045 visit_inst (cfg, bb, ins->next, cvars, bblist, carray);
1047 } else if (ins->opcode == OP_BR) {
1048 add_cprop_bb (cfg, ins->inst_target_bb, bblist);
1049 } else if (MONO_IS_COND_BRANCH_OP (ins)) {
1050 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1051 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1052 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1053 if (ins->inst_false_bb)
1054 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1055 } else {
1056 add_cprop_bb (cfg, ins->inst_true_bb, bblist);
1057 if (ins->inst_false_bb)
1058 add_cprop_bb (cfg, ins->inst_false_bb, bblist);
1064 * fold_ins:
1066 * Replace INS with its constant value, if it exists
1068 static void
1069 fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray)
1071 const char *spec = INS_INFO (ins->opcode);
1072 int opcode2;
1074 if ((ins->opcode != OP_NOP) && (ins->dreg != -1) && !MONO_IS_STORE_MEMBASE (ins)) {
1075 if (carray [ins->dreg] && (spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) {
1076 /* Perform constant folding */
1077 /* FIXME: */
1078 g_assert (carray [ins->dreg]->opcode == OP_ICONST);
1079 ins->opcode = OP_ICONST;
1080 ins->inst_c0 = carray [ins->dreg]->inst_c0;
1081 ins->sreg1 = ins->sreg2 = -1;
1083 else if ((spec [MONO_INST_SRC2] != ' ') && carray [ins->sreg2]) {
1084 /* Perform op->op_imm conversion */
1085 opcode2 = mono_op_to_op_imm (ins->opcode);
1086 if (opcode2 != -1) {
1087 ins->opcode = opcode2;
1088 ins->inst_imm = carray [ins->sreg2]->inst_c0;
1089 ins->sreg2 = -1;
1091 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1092 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1096 if (MONO_IS_JUMP_TABLE (ins)) {
1097 int i;
1098 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
1100 if (!ins->next || ins->next->opcode != OP_PADD) {
1101 /* The PADD was optimized away */
1102 /* FIXME: handle this as well */
1103 return;
1106 g_assert (ins->next->opcode == OP_PADD);
1107 g_assert (ins->next->sreg1 == ins->dreg);
1108 g_assert (ins->next->next->opcode == OP_LOAD_MEMBASE);
1110 if (carray [ins->next->sreg2]) {
1111 /* Convert to a simple branch */
1112 #if SIZEOF_REGISTER == 8
1113 int idx = carray [ins->next->sreg2]->inst_c0 >> 3;
1114 #else
1115 int idx = carray [ins->next->sreg2]->inst_c0 >> 2;
1116 #endif
1118 if (!((idx >= 0) && (idx < table->table_size))) {
1119 /* Out of range, eliminate the whole switch */
1120 for (i = 0; i < table->table_size; ++i) {
1121 remove_bb_from_phis (cfg, bb, table->table [i]);
1122 mono_unlink_bblock (cfg, bb, table->table [i]);
1125 NULLIFY_INS (ins);
1126 NULLIFY_INS (ins->next);
1127 NULLIFY_INS (ins->next->next);
1128 if (ins->next->next->next)
1129 NULLIFY_INS (ins->next->next->next);
1131 return;
1134 if (!ins->next->next->next || ins->next->next->next->opcode != OP_BR_REG) {
1135 /* A one-way switch which got optimized away */
1136 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1137 printf ("\tNo cfold on ");
1138 mono_print_ins (ins);
1140 return;
1143 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1144 printf ("\tcfold on ");
1145 mono_print_ins (ins);
1148 /* Unlink target bblocks */
1149 for (i = 0; i < table->table_size; ++i) {
1150 if (i != idx) {
1151 remove_bb_from_phis (cfg, bb, table->table [i]);
1152 mono_unlink_bblock (cfg, bb, table->table [i]);
1156 /* Change the OP_BR_REG to a simple branch */
1157 ins->next->next->next->opcode = OP_BR;
1158 ins->next->next->next->inst_target_bb = table->table [idx];
1159 ins->next->next->next->sreg1 = -1;
1161 /* Nullify the other instructions */
1162 NULLIFY_INS (ins);
1163 NULLIFY_INS (ins->next);
1164 NULLIFY_INS (ins->next->next);
1168 else if (MONO_IS_COND_BRANCH_OP (ins)) {
1169 if (ins->flags & MONO_INST_CFOLD_TAKEN) {
1170 remove_bb_from_phis (cfg, bb, ins->inst_false_bb);
1171 mono_unlink_bblock (cfg, bb, ins->inst_false_bb);
1172 ins->opcode = OP_BR;
1173 ins->inst_target_bb = ins->inst_true_bb;
1174 } else if (ins->flags & MONO_INST_CFOLD_NOT_TAKEN) {
1175 remove_bb_from_phis (cfg, bb, ins->inst_true_bb);
1176 mono_unlink_bblock (cfg, bb, ins->inst_true_bb);
1177 ins->opcode = OP_BR;
1178 ins->inst_target_bb = ins->inst_false_bb;
1183 void
1184 mono_ssa_cprop (MonoCompile *cfg)
1186 MonoInst **carray;
1187 MonoBasicBlock *bb;
1188 GList *bblock_list, *cvars;
1189 GList *tmp;
1190 int i;
1191 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1193 carray = g_new0 (MonoInst*, cfg->next_vreg);
1195 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1196 mono_ssa_create_def_use (cfg);
1198 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1199 cfg->bb_entry->flags |= BB_REACHABLE;
1201 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1203 for (i = 0; i < cfg->num_varinfo; i++) {
1204 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1205 if (!info->def)
1206 info->cpstate = 2;
1209 cvars = NULL;
1211 while (bblock_list) {
1212 MonoInst *inst;
1214 bb = (MonoBasicBlock *)bblock_list->data;
1216 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1218 g_assert (bb->flags & BB_REACHABLE);
1220 if (bb->out_count == 1) {
1221 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1222 bb->out_bb [0]->flags |= BB_REACHABLE;
1223 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1227 if (cfg->verbose_level > 1)
1228 printf ("\nSSA CONSPROP BB%d:\n", bb->block_num);
1230 for (inst = bb->code; inst; inst = inst->next) {
1231 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1234 while (cvars) {
1235 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1236 cvars = g_list_delete_link (cvars, cvars);
1238 for (tmp = info->uses; tmp; tmp = tmp->next) {
1239 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1240 if (!(ui->bb->flags & BB_REACHABLE))
1241 continue;
1242 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1247 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1248 MonoInst *inst;
1249 for (inst = bb->code; inst; inst = inst->next) {
1250 fold_ins (cfg, bb, inst, carray);
1254 g_free (carray);
1256 cfg->comp_done |= MONO_COMP_REACHABILITY;
1258 /* fixme: we should update usage infos during cprop, instead of computing it again */
1259 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1260 for (i = 0; i < cfg->num_varinfo; i++) {
1261 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1262 info->def = NULL;
1263 info->uses = NULL;
1267 static inline void
1268 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1270 GList *tmp;
1272 *wl = g_list_prepend_mempool (cfg->mempool, *wl, use);
1274 for (tmp = use->uses; tmp; tmp = tmp->next) {
1275 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1276 if (ui->inst == var->def) {
1277 /* from the mempool */
1278 use->uses = g_list_remove_link (use->uses, tmp);
1279 break;
1284 void
1285 mono_ssa_deadce (MonoCompile *cfg)
1287 int i;
1288 GList *work_list;
1290 g_assert (cfg->comp_done & MONO_COMP_SSA);
1292 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1294 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1295 mono_ssa_create_def_use (cfg);
1297 mono_ssa_copyprop (cfg);
1299 work_list = NULL;
1300 for (i = 0; i < cfg->num_varinfo; i++) {
1301 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1302 work_list = g_list_prepend_mempool (cfg->mempool, work_list, info);
1305 while (work_list) {
1306 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1307 work_list = g_list_remove_link (work_list, work_list);
1310 * The second part of the condition happens often when PHI nodes have their dreg
1311 * as one of their arguments due to the fact that we use the original vars.
1313 if (info->def && (!info->uses || ((info->uses->next == NULL) && (((MonoVarUsageInfo*)info->uses->data)->inst == info->def)))) {
1314 MonoInst *def = info->def;
1316 /* Eliminating FMOVE could screw up the fp stack */
1317 if (MONO_IS_MOVE (def) && (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE))) {
1318 MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
1319 if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
1320 add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
1321 def->opcode = OP_NOP;
1322 def->dreg = def->sreg1 = def->sreg2 = -1;
1323 info->reg = -1;
1324 } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
1325 def->opcode = OP_NOP;
1326 def->dreg = def->sreg1 = def->sreg2 = -1;
1327 info->reg = -1;
1328 } else if (MONO_IS_PHI (def)) {
1329 int j;
1330 for (j = def->inst_phi_args [0]; j > 0; j--) {
1331 MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
1332 add_to_dce_worklist (cfg, info, u, &work_list);
1334 def->opcode = OP_NOP;
1335 def->dreg = def->sreg1 = def->sreg2 = -1;
1336 info->reg = -1;
1338 else if (def->opcode == OP_NOP) {
1340 //else
1341 //mono_print_ins (def);
1347 #if 0
1348 void
1349 mono_ssa_strength_reduction (MonoCompile *cfg)
1351 MonoBasicBlock *bb;
1352 int i;
1354 g_assert (cfg->comp_done & MONO_COMP_SSA);
1355 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1356 g_assert (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;
1361 if (lp) {
1362 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1364 /* we only consider loops with 2 in bblocks */
1365 if (!h->in_count == 2)
1366 continue;
1368 for (i = 0; i < cfg->num_varinfo; i++) {
1369 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1371 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1372 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1373 MonoInst *v = info->def->inst_i1;
1376 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1382 #endif
1384 #endif /* DISABLE_JIT */