2 * ssa.c: Static single assign form support for the JIT compiler.
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
10 #include <mono/metadata/debug-helpers.h>
16 #define USE_ORIGINAL_VARS
17 #define CREATE_PRUNED_SSA
21 #define NEW_PHI(cfg,dest,val) do { \
22 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
23 (dest)->opcode = OP_PHI; \
24 (dest)->inst_c0 = (val); \
27 #define NEW_ICONST(cfg,dest,val) do { \
28 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
29 (dest)->opcode = OP_ICONST; \
30 (dest)->inst_c0 = (val); \
31 (dest)->type = STACK_I4; \
36 g_list_prepend_mempool (GList* l, MonoMemPool* mp, gpointer datum)
38 GList* n = mono_mempool_alloc (mp, sizeof (GList));
46 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
50 for (i = 0; i < bb->out_count; i++) {
51 if (bb->out_bb [i] == target) {
52 bb->out_bb [i] = bb->out_bb [--bb->out_count];
56 for (i = 0; i < target->in_count; i++) {
57 if (target->in_bb [i] == bb) {
58 target->in_bb [i] = target->in_bb [--target->in_count];
66 unlink_unused_bblocks (MonoCompile *cfg)
71 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
73 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
74 if (!(bb->next_bb->flags & BB_REACHABLE)) {
75 bb->next_bb = bb->next_bb->next_bb;
80 for (i = 1; i < cfg->num_bblocks; i++) {
81 bb = cfg->bblocks [i];
83 if (!(bb->flags & BB_REACHABLE)) {
84 for (j = 0; j < bb->in_count; j++) {
85 unlink_target (bb->in_bb [j], bb);
87 for (j = 0; j < bb->out_count; j++) {
88 unlink_target (bb, bb->out_bb [j]);
98 replace_usage (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **stack)
105 arity = mono_burg_arity [inst->opcode];
107 if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) &&
108 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
110 int idx = inst->inst_i0->inst_c0;
113 new_var = stack [idx];
115 new_var = cfg->varinfo [idx];
117 if ((new_var->opcode != OP_ARG) && (new_var->opcode != OP_LOCAL)) {
118 /* uninitialized variable ? */
119 g_warning ("using uninitialized variables %d in BB%d (%s)", idx, bb->block_num,
120 mono_method_full_name (cfg->method, TRUE));
121 //g_assert_not_reached ();
125 printf ("REPLACE BB%d %d %d\n", bb->block_num, idx, new_var->inst_c0);
127 inst->inst_i0 = new_var;
131 if (inst->ssa_op != MONO_SSA_STORE)
132 replace_usage (cfg, bb, inst->inst_left, stack);
134 replace_usage (cfg, bb, inst->inst_right, stack);
140 extends_live (MonoInst *inst)
147 arity = mono_burg_arity [inst->opcode];
149 if (inst->ssa_op == MONO_SSA_LOAD &&
150 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
154 if (inst->ssa_op != MONO_SSA_STORE)
155 if (extends_live (inst->inst_left))
158 if (extends_live (inst->inst_right))
167 replace_usage_new (MonoCompile *cfg, MonoInst *inst, int varnum, MonoInst *rep)
174 arity = mono_burg_arity [inst->opcode];
176 if ((inst->ssa_op == MONO_SSA_LOAD) &&
177 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
178 inst->inst_i0->inst_c0 == varnum && rep->type == inst->type) {
183 if (inst->ssa_op != MONO_SSA_STORE)
184 if (replace_usage_new (cfg, inst->inst_left, varnum, rep))
187 if (replace_usage_new (cfg, inst->inst_right, varnum, rep))
196 mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, MonoInst **stack)
198 MonoInst *inst, *new_var;
201 MonoInst **new_stack;
204 printf ("RENAME VARS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
207 for (inst = bb->code; inst; inst = inst->next) {
208 if (inst->opcode != OP_PHI)
209 replace_usage (cfg, bb, inst, stack);
211 if (inst->ssa_op == MONO_SSA_STORE &&
212 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
213 idx = inst->inst_i0->inst_c0;
214 g_assert (idx < max_vars);
216 if ((!stack [idx]) && (bb == cfg->bb_init) && (inst->inst_i0->opcode != OP_ARG)) {
217 new_var = cfg->varinfo [idx];
219 new_var = mono_compile_create_var (cfg, inst->inst_i0->inst_vtype, inst->inst_i0->opcode);
220 new_var->flags = inst->inst_i0->flags;
223 printf ("DEF %d %d\n", idx, new_var->inst_c0);
225 inst->inst_i0 = new_var;
227 #ifdef USE_ORIGINAL_VARS
228 cfg->vars [new_var->inst_c0]->reg = idx;
231 stack [idx] = new_var;
235 for (i = 0; i < bb->out_count; i++) {
236 MonoBasicBlock *n = bb->out_bb [i];
238 for (j = 0; j < n->in_count; j++)
239 if (n->in_bb [j] == bb)
242 for (inst = n->code; inst; inst = inst->next) {
243 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->opcode == OP_PHI) {
244 idx = inst->inst_i1->inst_c0;
246 new_var = stack [idx];
248 new_var = cfg->varinfo [idx];
250 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
252 inst->inst_i1->inst_phi_args [j + 1] = new_var->inst_c0;
259 new_stack = g_new (MonoInst*, max_vars);
260 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
261 memcpy (new_stack, stack, sizeof (MonoInst *) * max_vars);
262 mono_ssa_rename_vars (cfg, max_vars, (MonoBasicBlock *)tmp->data, new_stack);
269 mono_ssa_compute (MonoCompile *cfg)
273 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
274 MonoInst *inst, *store, **stack;
276 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
278 /* we dont support methods containing exception clauses */
279 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
280 g_assert (!cfg->disable_ssa);
282 //printf ("COMPUTS SSA %s %d\n", mono_method_full_name (cfg->method, TRUE), cfg->num_varinfo);
284 #ifdef CREATE_PRUNED_SSA
285 /* we need liveness for pruned SSA */
286 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
287 mono_analyze_liveness (cfg);
290 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
292 for (i = 0; i < cfg->num_varinfo; ++i) {
293 vinfo [i].def_in = mono_bitset_new (cfg->num_bblocks, 0);
295 /* implizit reference at start */
296 mono_bitset_set (vinfo [i].def_in, 0);
298 for (i = 0; i < cfg->num_bblocks; ++i) {
299 for (inst = cfg->bblocks [i]->code; inst; inst = inst->next) {
300 if (inst->ssa_op == MONO_SSA_STORE) {
301 idx = inst->inst_i0->inst_c0;
302 g_assert (idx < cfg->num_varinfo);
303 mono_bitset_set (vinfo [idx].def_in, i);
308 /* insert phi functions */
309 for (i = 0; i < cfg->num_varinfo; ++i) {
310 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
311 vinfo [i].dfrontier = set;
312 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
313 MonoBasicBlock *bb = cfg->bblocks [idx];
315 /* fixme: create pruned SSA? we would need liveness information for that */
317 if (bb == cfg->bb_exit)
320 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
321 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
325 NEW_PHI (cfg, inst, i);
327 inst->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
328 inst->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
330 store = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));
331 if (!cfg->varinfo [i]->inst_vtype->type)
332 g_assert_not_reached ();
333 store->opcode = mono_type_to_stind (cfg->varinfo [i]->inst_vtype);
334 store->ssa_op = MONO_SSA_STORE;
335 store->inst_i0 = cfg->varinfo [i];
336 store->inst_i1 = inst;
337 store->klass = store->inst_i0->klass;
339 store->next = bb->code;
342 bb->last_ins = bb->code;
345 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
351 for (i = 0; i < cfg->num_varinfo; ++i)
352 mono_bitset_free (vinfo [i].def_in);
356 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
358 for (i = 0; i < cfg->num_varinfo; i++)
361 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, stack);
363 cfg->comp_done |= MONO_COMP_SSA;
366 #ifndef USE_ORIGINAL_VARS
368 mono_ssa_get_allocatable_vars (MonoCompile *cfg)
370 GHashTable *type_hash;
371 GPtrArray *varlist_array = g_ptr_array_new ();
374 g_assert (cfg->comp_done & MONO_COMP_LIVENESS);
376 type_hash = g_hash_table_new (NULL, NULL);
378 for (i = 0; i < cfg->num_varinfo; i++) {
379 MonoInst *ins = cfg->varinfo [i];
380 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
383 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
386 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) ||
387 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG) || vmv->reg != -1)
390 g_assert (ins->inst_vtype);
391 g_assert (vmv->reg == -1);
392 g_assert (i == vmv->idx);
394 if (!(tidx = (int)g_hash_table_lookup (type_hash, ins->inst_vtype))) {
395 GList *vars = g_list_append (NULL, vmv);
396 g_ptr_array_add (varlist_array, vars);
397 g_hash_table_insert (type_hash, ins->inst_vtype, (gpointer)varlist_array->len);
400 g_ptr_array_index (varlist_array, tidx) =
401 mono_varlist_insert_sorted (cfg, g_ptr_array_index (varlist_array, tidx), vmv, FALSE);
405 g_hash_table_destroy (type_hash);
407 return varlist_array;
412 mono_ssa_replace_copies (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, char *is_live)
419 arity = mono_burg_arity [inst->opcode];
421 if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN || inst->ssa_op == MONO_SSA_STORE) &&
422 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
424 int idx = inst->inst_i0->inst_c0;
425 MonoMethodVar *mv = cfg->vars [idx];
427 if (mv->reg != -1 && mv->reg != mv->idx) {
429 is_live [mv->reg] = 1;
431 new_var = cfg->varinfo [mv->reg];
434 printf ("REPLACE COPY BB%d %d %d\n", bb->block_num, idx, new_var->inst_c0);
435 g_assert (cfg->varinfo [mv->reg]->inst_vtype == cfg->varinfo [idx]->inst_vtype);
437 inst->inst_i0 = new_var;
439 is_live [mv->idx] = 1;
445 mono_ssa_replace_copies (cfg, bb, inst->inst_left, is_live);
447 mono_ssa_replace_copies (cfg, bb, inst->inst_right, is_live);
450 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->ssa_op == MONO_SSA_LOAD &&
451 inst->inst_i0->inst_c0 == inst->inst_i1->inst_i0->inst_c0) {
452 inst->ssa_op = MONO_SSA_NOP;
453 inst->opcode = OP_NOP;
459 mono_ssa_remove (MonoCompile *cfg)
461 MonoInst *inst, *phi;
464 #ifndef USE_ORIGINAL_VARS
465 GPtrArray *varlist_array;
468 g_assert (cfg->comp_done & MONO_COMP_SSA);
470 for (i = 0; i < cfg->num_bblocks; ++i) {
471 MonoBasicBlock *bb = cfg->bblocks [i];
472 for (inst = bb->code; inst; inst = inst->next) {
473 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->opcode == OP_PHI) {
476 g_assert (phi->inst_phi_args [0] == bb->in_count);
478 for (j = 0; j < bb->in_count; j++) {
479 MonoBasicBlock *pred = bb->in_bb [j];
480 int idx = phi->inst_phi_args [j + 1];
481 MonoMethodVar *mv = cfg->vars [idx];
483 if (mv->reg != -1 && mv->reg != mv->idx) {
484 //printf ("PHICOPY %d %d -> %d\n", idx, mv->reg, inst->inst_i0->inst_c0);
489 if (idx != inst->inst_i0->inst_c0) {
491 printf ("MOVE %d to %d in BB%d\n", idx, inst->inst_i0->inst_c0, pred->block_num);
493 mono_add_varcopy_to_end (cfg, pred, idx, inst->inst_i0->inst_c0);
497 /* remove the phi functions */
498 inst->opcode = OP_NOP;
499 inst->ssa_op = MONO_SSA_NOP;
504 #ifndef USE_ORIGINAL_VARS
505 /* we compute liveness again */
506 cfg->comp_done &= ~MONO_COMP_LIVENESS;
507 mono_analyze_liveness (cfg);
509 varlist_array = mono_ssa_get_allocatable_vars (cfg);
511 for (i = 0; i < varlist_array->len; i++) {
512 GList *l, *regs, *vars = g_ptr_array_index (varlist_array, i);
513 MonoMethodVar *vmv, *amv;
515 if (g_list_length (vars) <= 1) {
522 for (l = vars; l; l = l->next) {
525 /* expire old intervals in active */
527 amv = (MonoMethodVar *)active->data;
529 if (amv->range.last_use.abs_pos >= vmv->range.first_use.abs_pos)
532 active = g_list_delete_link (active, active);
533 regs = g_list_prepend (regs, (gpointer)amv->reg);
537 regs = g_list_prepend (regs, (gpointer)vmv->idx);
539 vmv->reg = (int)regs->data;
540 regs = g_list_delete_link (regs, regs);
541 active = mono_varlist_insert_sorted (cfg, active, vmv, TRUE);
544 g_list_free (active);
549 g_ptr_array_free (varlist_array, TRUE);
553 is_live = alloca (cfg->num_varinfo);
554 memset (is_live, 0, cfg->num_varinfo);
556 for (i = 0; i < cfg->num_bblocks; ++i) {
557 MonoBasicBlock *bb = cfg->bblocks [i];
559 for (inst = bb->code; inst; inst = inst->next)
560 mono_ssa_replace_copies (cfg, bb, inst, is_live);
563 for (i = 0; i < cfg->num_varinfo; ++i) {
564 cfg->vars [i]->reg = -1;
566 cfg->varinfo [i]->flags |= MONO_INST_IS_DEAD;
570 if (cfg->comp_done & MONO_COMP_REACHABILITY)
571 unlink_unused_bblocks (cfg);
573 cfg->comp_done &= ~MONO_COMP_SSA;
577 #define IS_CALL(op) (op == CEE_CALLI || op == CEE_CALL || op == CEE_CALLVIRT || (op >= OP_VOIDCALL && op <= OP_CALL_MEMBASE))
588 * Returns TRUE if the tree can have side effects.
591 analyze_dev_use (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *root, MonoInst *inst)
595 gboolean has_side_effects;
600 arity = mono_burg_arity [inst->opcode];
601 switch (inst->opcode) {
602 #define ANALYZE_DEV_USE_SPECIFIC_OPS 1
603 #define OPDEF(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) case a1:
604 #include "simple-cee-ops.h"
606 #define MINI_OP(a1,a2) case a1:
607 #include "simple-mini-ops.h"
609 #undef ANALYZE_DEV_USE_SPECIFIC_OPS
610 has_side_effects = FALSE;
613 has_side_effects = TRUE;
616 if ((inst->ssa_op == MONO_SSA_STORE) &&
617 (inst->inst_i0->opcode == OP_LOCAL /*|| inst->inst_i0->opcode == OP_ARG */)) {
618 idx = inst->inst_i0->inst_c0;
619 info = cfg->vars [idx];
620 //printf ("%d defined in BB%d %p\n", idx, bb->block_num, root);
622 g_warning ("more than one definition of variable %d in %s", idx,
623 mono_method_full_name (cfg->method, TRUE));
624 g_assert_not_reached ();
626 if (!IS_CALL (inst->inst_i1->opcode) /* && inst->inst_i1->opcode == OP_ICONST */) {
627 g_assert (inst == root);
632 if (inst->inst_i1->opcode == OP_PHI) {
633 for (i = inst->inst_i1->inst_phi_args [0]; i > 0; i--) {
634 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
635 idx = inst->inst_i1->inst_phi_args [i];
636 info = cfg->vars [idx];
637 //printf ("FOUND %d\n", idx);
640 info->uses = g_list_prepend_mempool (info->uses, cfg->mempool, ui);
645 if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) &&
646 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
647 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
648 idx = inst->inst_i0->inst_c0;
649 info = cfg->vars [idx];
650 //printf ("FOUND %d\n", idx);
653 info->uses = g_list_prepend_mempool (info->uses, cfg->mempool, ui);
656 //if (inst->ssa_op != MONO_SSA_STORE)
657 if (analyze_dev_use (cfg, bb, root, inst->inst_left))
658 has_side_effects = TRUE;
660 if (analyze_dev_use (cfg, bb, root, inst->inst_right))
661 has_side_effects = TRUE;
665 return has_side_effects;
669 /* avoid unnecessary copies of variables:
670 * Y <= X; Z = Y; is translated to Z = X;
673 mono_ssa_avoid_copies (MonoCompile *cfg)
675 MonoInst *inst, *next;
677 MonoMethodVar *i1, *i2;
679 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
681 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
682 for (inst = bb->code; inst; inst = inst->next) {
683 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i0->opcode == OP_LOCAL &&
684 !IS_CALL (inst->inst_i1->opcode) && inst->inst_i1->opcode != OP_PHI && !inst->flags) {
685 i1 = cfg->vars [inst->inst_i0->inst_c0];
687 /* fixme: compiling mcs does not work when I enable this */
689 if (g_list_length (i1->uses) == 1 && !extends_live (inst->inst_i1)) {
690 MonoVarUsageInfo *vi = (MonoVarUsageInfo *)i1->uses->data;
693 //printf ("VAR %d %s\n", i1->idx, mono_method_full_name (cfg->method, TRUE));
694 //mono_print_tree (inst); printf ("\n");
695 //mono_print_tree (u); printf ("\n");
697 if (replace_usage_new (cfg, u, inst->inst_i0->inst_c0, inst->inst_i1)) {
699 //mono_print_tree (u); printf ("\n");
701 inst->opcode = OP_NOP;
702 inst->ssa_op = MONO_SSA_NOP;
706 if ((next = inst->next) && next->ssa_op == MONO_SSA_STORE && next->inst_i0->opcode == OP_LOCAL &&
707 next->inst_i1->ssa_op == MONO_SSA_LOAD && next->inst_i1->inst_i0->opcode == OP_LOCAL &&
708 next->inst_i1->inst_i0->inst_c0 == inst->inst_i0->inst_c0 && g_list_length (i1->uses) == 1 &&
709 inst->opcode == next->opcode && inst->inst_i0->type == next->inst_i0->type) {
710 i2 = cfg->vars [next->inst_i0->inst_c0];
711 //printf ("ELIM. COPY in BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
712 inst->inst_i0 = next->inst_i0;
716 next->opcode = OP_NOP;
717 next->ssa_op = MONO_SSA_NOP;
725 mono_ssa_create_def_use (MonoCompile *cfg)
729 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
731 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
733 for (inst = bb->code; inst; inst = inst->next) {
734 gboolean has_side_effects = analyze_dev_use (cfg, bb, inst, inst);
735 if (has_side_effects && (inst->ssa_op == MONO_SSA_STORE) &&
736 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
737 inst->inst_i0->flags |= MONO_INST_DEFINITION_HAS_SIDE_EFFECTS;
742 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
746 simulate_compare (int opcode, int a, int b)
762 return (unsigned)a >= (unsigned)b;
764 return (unsigned)a > (unsigned)b;
766 return (unsigned)a <= (unsigned)b;
768 return (unsigned)a < (unsigned)b;
770 g_assert_not_reached ();
777 simulate_long_compare (int opcode, gint64 a, gint64 b)
793 return (guint64)a >= (guint64)b;
795 return (guint64)a > (guint64)b;
797 return (guint64)a <= (guint64)b;
799 return (guint64)a < (guint64)b;
801 g_assert_not_reached ();
807 #define EVAL_CXX(name,op,cast) \
809 if ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE)) { \
810 r1 = evaluate_const_tree (cfg, inst->inst_i0->inst_i0, &a, carray); \
811 r2 = evaluate_const_tree (cfg, inst->inst_i0->inst_i1, &b, carray); \
812 if (r1 == 1 && r2 == 1) { \
813 *res = ((cast)a op (cast)b); \
816 return MAX (r1, r2); \
821 #define EVAL_BINOP(name,op) \
823 r1 = evaluate_const_tree (cfg, inst->inst_i0, &a, carray); \
824 r2 = evaluate_const_tree (cfg, inst->inst_i1, &b, carray); \
825 if (r1 == 1 && r2 == 1) { \
829 return MAX (r1, r2); \
834 /* fixme: this only works for interger constants, but not for other types (long, float) */
836 evaluate_const_tree (MonoCompile *cfg, MonoInst *inst, int *res, MonoInst **carray)
844 if (inst->ssa_op == MONO_SSA_LOAD &&
845 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
846 (c0 = carray [inst->inst_i0->inst_c0])) {
851 switch (inst->opcode) {
853 *res = inst->inst_c0;
856 EVAL_CXX (OP_CEQ,==,gint32)
857 EVAL_CXX (OP_CGT,>,gint32)
858 EVAL_CXX (OP_CGT_UN,>,guint32)
859 EVAL_CXX (OP_CLT,<,gint32)
860 EVAL_CXX (OP_CLT_UN,<,guint32)
862 EVAL_BINOP (CEE_ADD,+)
863 EVAL_BINOP (CEE_SUB,-)
864 EVAL_BINOP (CEE_MUL,*)
865 EVAL_BINOP (CEE_AND,&)
866 EVAL_BINOP (CEE_OR,|)
867 EVAL_BINOP (CEE_XOR,^)
868 EVAL_BINOP (CEE_SHL,<<)
869 EVAL_BINOP (CEE_SHR,>>)
879 fold_tree (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **carray)
887 arity = mono_burg_arity [inst->opcode];
889 if (inst->ssa_op == MONO_SSA_STORE &&
890 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
891 inst->inst_i1->opcode == OP_PHI && (c0 = carray [inst->inst_i0->inst_c0])) {
892 //{static int cn = 0; printf ("PHICONST %d %d %s\n", cn++, c0->inst_c0, mono_method_full_name (cfg->method, TRUE));}
893 *inst->inst_i1 = *c0;
894 } else if (inst->ssa_op == MONO_SSA_LOAD &&
895 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
896 (c0 = carray [inst->inst_i0->inst_c0])) {
897 //{static int cn = 0; printf ("YCCOPY %d %d %s\n", cn++, c0->inst_c0, mono_method_full_name (cfg->method, TRUE));}
902 fold_tree (cfg, bb, inst->inst_left, carray);
904 fold_tree (cfg, bb, inst->inst_right, carray);
905 mono_constant_fold_inst (inst, NULL);
909 if ((inst->opcode >= CEE_BEQ && inst->opcode <= CEE_BLT_UN) &&
910 ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE))) {
911 MonoInst *v0 = inst->inst_i0->inst_i0;
912 MonoInst *v1 = inst->inst_i0->inst_i1;
913 MonoBasicBlock *target = NULL;
915 /* hack for longs to optimize the simply cases */
916 if (v0->opcode == OP_I8CONST && v1->opcode == OP_I8CONST) {
917 if (simulate_long_compare (inst->opcode, v0->inst_l, v1->inst_l)) {
918 //unlink_target (bb, inst->inst_false_bb);
919 target = inst->inst_true_bb;
921 //unlink_target (bb, inst->inst_true_bb);
922 target = inst->inst_false_bb;
924 } else if (evaluate_const_tree (cfg, v0, &a, carray) == 1 &&
925 evaluate_const_tree (cfg, v1, &b, carray) == 1) {
926 if (simulate_compare (inst->opcode, a, b)) {
927 //unlink_target (bb, inst->inst_false_bb);
928 target = inst->inst_true_bb;
930 //unlink_target (bb, inst->inst_true_bb);
931 target = inst->inst_false_bb;
936 bb->out_bb [0] = target;
938 inst->opcode = OP_BR;
939 inst->inst_target_bb = target;
941 } else if (inst->opcode == OP_SWITCH && (evaluate_const_tree (cfg, inst->inst_left, &a, carray) == 1) && (a >= 0) && (a < GPOINTER_TO_INT (inst->klass))) {
942 bb->out_bb [0] = inst->inst_many_bb [a];
944 inst->inst_target_bb = bb->out_bb [0];
945 inst->opcode = OP_BR;
951 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
953 if (info->cpstate >= state)
956 info->cpstate = state;
958 //printf ("SETSTATE %d to %d\n", info->idx, info->cpstate);
961 carray [info->idx] = c0;
963 carray [info->idx] = NULL;
965 if (!g_list_find (*cvars, info)) {
966 *cvars = g_list_prepend (*cvars, info);
971 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, GList **cvars, GList **bblist, MonoInst **carray)
975 if (inst->opcode == OP_SWITCH) {
977 int cases = GPOINTER_TO_INT (inst->klass);
979 r1 = evaluate_const_tree (cfg, inst->inst_left, &a, carray);
980 if ((r1 == 1) && ((a < 0) || (a >= cases)))
983 MonoBasicBlock *tb = inst->inst_many_bb [a];
984 if (!(tb->flags & BB_REACHABLE)) {
985 tb->flags |= BB_REACHABLE;
986 *bblist = g_list_prepend (*bblist, tb);
988 } else if (r1 == 2) {
989 for (i = GPOINTER_TO_INT (inst->klass); i >= 0; i--) {
990 MonoBasicBlock *tb = inst->inst_many_bb [i];
991 if (!(tb->flags & BB_REACHABLE)) {
992 tb->flags |= BB_REACHABLE;
993 *bblist = g_list_prepend (*bblist, tb);
997 } else if (inst->opcode == OP_BR) {
998 MonoBasicBlock *target = inst->inst_target_bb;
1000 if (!(target->flags & BB_REACHABLE)) {
1001 target->flags |= BB_REACHABLE;
1002 *bblist = g_list_prepend (*bblist, target);
1004 } else if ((inst->opcode >= CEE_BEQ && inst->opcode <= CEE_BLT_UN) &&
1005 ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE))) {
1007 MonoInst *v0 = inst->inst_i0->inst_i0;
1008 MonoInst *v1 = inst->inst_i0->inst_i1;
1010 r1 = evaluate_const_tree (cfg, v0, &a, carray);
1011 r2 = evaluate_const_tree (cfg, v1, &b, carray);
1013 if (r1 == 1 && r2 == 1) {
1014 MonoBasicBlock *target;
1016 if (simulate_compare (inst->opcode, a, b)) {
1017 target = inst->inst_true_bb;
1019 target = inst->inst_false_bb;
1021 if (!(target->flags & BB_REACHABLE)) {
1022 target->flags |= BB_REACHABLE;
1023 *bblist = g_list_prepend (*bblist, target);
1025 } else if (r1 == 2 || r2 == 2) {
1026 if (!(inst->inst_true_bb->flags & BB_REACHABLE)) {
1027 inst->inst_true_bb->flags |= BB_REACHABLE;
1028 *bblist = g_list_prepend (*bblist, inst->inst_true_bb);
1030 if (!(inst->inst_false_bb->flags & BB_REACHABLE)) {
1031 inst->inst_false_bb->flags |= BB_REACHABLE;
1032 *bblist = g_list_prepend (*bblist, inst->inst_false_bb);
1035 } else if (inst->ssa_op == MONO_SSA_STORE &&
1036 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
1037 MonoMethodVar *info = cfg->vars [inst->inst_i0->inst_c0];
1038 MonoInst *i1 = inst->inst_i1;
1041 if (info->cpstate < 2) {
1042 if (i1->opcode == OP_ICONST) {
1043 change_varstate (cfg, cvars, info, 1, i1, carray);
1044 } else if (i1->opcode == OP_PHI) {
1045 MonoInst *c0 = NULL;
1048 for (j = 1; j <= i1->inst_phi_args [0]; j++) {
1049 MonoMethodVar *mv = cfg->vars [i1->inst_phi_args [j]];
1050 MonoInst *src = mv->def;
1052 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE)) {
1056 if (!mv->def || !src || src->ssa_op != MONO_SSA_STORE ||
1057 !(src->inst_i0->opcode == OP_LOCAL || src->inst_i0->opcode == OP_ARG) ||
1059 change_varstate (cfg, cvars, info, 2, NULL, carray);
1063 if (mv->cpstate == 0)
1066 //g_assert (src->inst_i1->opcode == OP_ICONST);
1067 g_assert (carray [mv->idx]);
1070 c0 = carray [mv->idx];
1073 if (carray [mv->idx]->inst_c0 != c0->inst_c0) {
1074 change_varstate (cfg, cvars, info, 2, NULL, carray);
1079 if (c0 && info->cpstate < 1) {
1080 change_varstate (cfg, cvars, info, 1, c0, carray);
1083 int state = evaluate_const_tree (cfg, i1, &res, carray);
1085 NEW_ICONST (cfg, i1, res);
1086 change_varstate (cfg, cvars, info, 1, i1, carray);
1088 change_varstate (cfg, cvars, info, 2, NULL, carray);
1096 mono_ssa_cprop (MonoCompile *cfg)
1100 GList *bblock_list, *cvars;
1103 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1105 carray = g_new0 (MonoInst*, cfg->num_varinfo);
1107 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1108 mono_ssa_create_def_use (cfg);
1110 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1111 cfg->bb_entry->flags |= BB_REACHABLE;
1113 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1115 for (i = 0; i < cfg->num_varinfo; i++) {
1116 MonoMethodVar *info = cfg->vars [i];
1123 while (bblock_list) {
1126 bb = (MonoBasicBlock *)bblock_list->data;
1128 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1130 g_assert (bb->flags & BB_REACHABLE);
1132 if (bb->out_count == 1) {
1133 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1134 bb->out_bb [0]->flags |= BB_REACHABLE;
1135 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1139 for (inst = bb->code; inst; inst = inst->next) {
1140 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1144 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1145 cvars = g_list_delete_link (cvars, cvars);
1147 for (tmp = info->uses; tmp; tmp = tmp->next) {
1148 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1149 if (!(ui->bb->flags & BB_REACHABLE))
1151 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1156 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1158 for (inst = bb->code; inst; inst = inst->next) {
1159 fold_tree (cfg, bb, inst, carray);
1165 cfg->comp_done |= MONO_COMP_REACHABILITY;
1169 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1173 *wl = g_list_prepend (*wl, use);
1175 for (tmp = use->uses; tmp; tmp = tmp->next) {
1176 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1177 if (ui->inst == var->def) {
1178 /* from the mempool */
1179 use->uses = g_list_remove_link (use->uses, tmp);
1186 mono_ssa_deadce (MonoCompile *cfg)
1191 g_assert (cfg->comp_done & MONO_COMP_SSA);
1193 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1195 /* fixme: we should update usage infos during cprop, instead of computing it again */
1196 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1197 for (i = 0; i < cfg->num_varinfo; i++) {
1198 MonoMethodVar *info = cfg->vars [i];
1203 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1204 mono_ssa_create_def_use (cfg);
1206 mono_ssa_avoid_copies (cfg);
1209 for (i = 0; i < cfg->num_varinfo; i++) {
1210 MonoMethodVar *info = cfg->vars [i];
1211 work_list = g_list_prepend (work_list, info);
1213 //if ((info->def != NULL) && (info->def->inst_i1->opcode != OP_PHI)) printf ("SSA DEADCE TOTAL LOCAL\n");
1217 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1218 work_list = g_list_delete_link (work_list, work_list);
1220 if (!info->uses && info->def && (!(cfg->varinfo [info->idx]->flags & (MONO_INST_DEFINITION_HAS_SIDE_EFFECTS|MONO_INST_VOLATILE|MONO_INST_INDIRECT)))) {
1222 //printf ("ELIMINATE %s: ", mono_method_full_name (cfg->method, TRUE)); mono_print_tree (info->def); printf ("\n");
1224 i1 = info->def->inst_i1;
1225 if (i1->opcode == OP_PHI) {
1227 for (j = i1->inst_phi_args [0]; j > 0; j--) {
1228 MonoMethodVar *u = cfg->vars [i1->inst_phi_args [j]];
1229 add_to_dce_worklist (cfg, info, u, &work_list);
1231 } else if (i1->ssa_op == MONO_SSA_LOAD &&
1232 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG)) {
1233 MonoMethodVar *u = cfg->vars [i1->inst_i0->inst_c0];
1234 add_to_dce_worklist (cfg, info, u, &work_list);
1236 //if (i1->opcode != OP_PHI) printf ("SSA DEADCE DEAD LOCAL\n");
1238 info->def->opcode = OP_NOP;
1239 info->def->ssa_op = MONO_SSA_NOP;
1247 mono_ssa_strength_reduction (MonoCompile *cfg)
1252 g_assert (cfg->comp_done & MONO_COMP_SSA);
1253 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1254 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1256 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1257 GList *lp = bb->loop_blocks;
1260 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1262 /* we only consider loops with 2 in bblocks */
1263 if (!h->in_count == 2)
1266 for (i = 0; i < cfg->num_varinfo; i++) {
1267 MonoMethodVar *info = cfg->vars [i];
1269 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1270 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1271 MonoInst *v = info->def->inst_i1;
1274 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1282 #endif /* DISABLE_SSA */