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>
11 #include <mono/metadata/mempool.h>
12 #include <mono/metadata/mempool-internals.h>
18 #define USE_ORIGINAL_VARS
19 #define CREATE_PRUNED_SSA
23 #define NEW_PHI(cfg,dest,val) do { \
24 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
25 (dest)->opcode = OP_PHI; \
26 (dest)->inst_c0 = (val); \
29 #define NEW_ICONST(cfg,dest,val) do { \
30 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
31 (dest)->opcode = OP_ICONST; \
32 (dest)->inst_c0 = (val); \
33 (dest)->type = STACK_I4; \
37 unlink_target (MonoBasicBlock *bb, MonoBasicBlock *target)
41 for (i = 0; i < bb->out_count; i++) {
42 if (bb->out_bb [i] == target) {
43 bb->out_bb [i] = bb->out_bb [--bb->out_count];
47 for (i = 0; i < target->in_count; i++) {
48 if (target->in_bb [i] == bb) {
49 target->in_bb [i] = target->in_bb [--target->in_count];
57 unlink_unused_bblocks (MonoCompile *cfg)
62 g_assert (cfg->comp_done & MONO_COMP_REACHABILITY);
64 for (bb = cfg->bb_entry; bb && bb->next_bb;) {
65 if (!(bb->next_bb->flags & BB_REACHABLE)) {
66 bb->next_bb = bb->next_bb->next_bb;
71 for (i = 1; i < cfg->num_bblocks; i++) {
72 bb = cfg->bblocks [i];
74 if (!(bb->flags & BB_REACHABLE)) {
75 for (j = 0; j < bb->in_count; j++) {
76 unlink_target (bb->in_bb [j], bb);
78 for (j = 0; j < bb->out_count; j++) {
79 unlink_target (bb, bb->out_bb [j]);
89 replace_usage (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **stack)
96 arity = mono_burg_arity [inst->opcode];
98 if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) &&
99 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
101 int idx = inst->inst_i0->inst_c0;
104 new_var = stack [idx];
106 new_var = cfg->varinfo [idx];
108 if ((new_var->opcode != OP_ARG) && (new_var->opcode != OP_LOCAL)) {
109 /* uninitialized variable ? */
110 g_warning ("using uninitialized variables %d in BB%d (%s)", idx, bb->block_num,
111 mono_method_full_name (cfg->method, TRUE));
112 //g_assert_not_reached ();
116 printf ("REPLACE BB%d %d %d\n", bb->block_num, idx, new_var->inst_c0);
118 inst->inst_i0 = new_var;
122 if (inst->ssa_op != MONO_SSA_STORE)
123 replace_usage (cfg, bb, inst->inst_left, stack);
125 replace_usage (cfg, bb, inst->inst_right, stack);
133 extends_live (MonoInst *inst)
140 arity = mono_burg_arity [inst->opcode];
142 if (inst->ssa_op == MONO_SSA_LOAD &&
143 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
147 if (inst->ssa_op != MONO_SSA_STORE)
148 if (extends_live (inst->inst_left))
151 if (extends_live (inst->inst_right))
160 replace_usage_new (MonoCompile *cfg, MonoInst *inst, int varnum, MonoInst *rep)
167 arity = mono_burg_arity [inst->opcode];
169 if ((inst->ssa_op == MONO_SSA_LOAD) &&
170 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
171 inst->inst_i0->inst_c0 == varnum && rep->type == inst->type) {
176 if (inst->ssa_op != MONO_SSA_STORE)
177 if (replace_usage_new (cfg, inst->inst_left, varnum, rep))
180 if (replace_usage_new (cfg, inst->inst_right, varnum, rep))
191 mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, MonoInst **stack)
193 MonoInst *inst, *new_var;
196 MonoInst **new_stack;
199 printf ("RENAME VARS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
202 MONO_BB_FOR_EACH_INS (bb, inst) {
203 if (inst->opcode != OP_PHI)
204 replace_usage (cfg, bb, inst, stack);
206 if (inst->ssa_op == MONO_SSA_STORE &&
207 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
208 idx = inst->inst_i0->inst_c0;
209 g_assert (idx < max_vars);
211 if ((!stack [idx]) && (bb == cfg->bb_init) && (inst->inst_i0->opcode != OP_ARG)) {
212 new_var = cfg->varinfo [idx];
214 new_var = mono_compile_create_var (cfg, inst->inst_i0->inst_vtype, inst->inst_i0->opcode);
215 new_var->flags = inst->inst_i0->flags;
218 printf ("DEF %d %d\n", idx, new_var->inst_c0);
220 inst->inst_i0 = new_var;
222 #ifdef USE_ORIGINAL_VARS
223 MONO_VARINFO (cfg, new_var->inst_c0)->reg = idx;
226 stack [idx] = new_var;
230 for (i = 0; i < bb->out_count; i++) {
231 MonoBasicBlock *n = bb->out_bb [i];
233 for (j = 0; j < n->in_count; j++)
234 if (n->in_bb [j] == bb)
237 MONO_BB_FOR_EACH_INS (n, inst) {
238 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->opcode == OP_PHI) {
239 idx = inst->inst_i1->inst_c0;
241 new_var = stack [idx];
243 new_var = cfg->varinfo [idx];
245 printf ("FOUND PHI %d (%d, %d)\n", idx, j, new_var->inst_c0);
247 inst->inst_i1->inst_phi_args [j + 1] = new_var->inst_c0;
254 new_stack = g_new (MonoInst*, max_vars);
255 for (tmp = bb->dominated; tmp; tmp = tmp->next) {
256 memcpy (new_stack, stack, sizeof (MonoInst *) * max_vars);
257 mono_ssa_rename_vars (cfg, max_vars, (MonoBasicBlock *)tmp->data, new_stack);
264 mono_ssa_compute (MonoCompile *cfg)
268 MonoMethodVar *vinfo = g_new0 (MonoMethodVar, cfg->num_varinfo);
269 MonoInst *inst, *store, **stack;
271 g_assert (!(cfg->comp_done & MONO_COMP_SSA));
273 /* we dont support methods containing exception clauses */
274 g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
275 g_assert (!cfg->disable_ssa);
277 //printf ("COMPUTS SSA %s %d\n", mono_method_full_name (cfg->method, TRUE), cfg->num_varinfo);
279 #ifdef CREATE_PRUNED_SSA
280 /* we need liveness for pruned SSA */
281 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
282 mono_analyze_liveness (cfg);
285 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
287 for (i = 0; i < cfg->num_varinfo; ++i) {
288 vinfo [i].def_in = mono_bitset_new (cfg->num_bblocks, 0);
290 /* implizit reference at start */
291 mono_bitset_set (vinfo [i].def_in, 0);
293 for (i = 0; i < cfg->num_bblocks; ++i) {
294 MONO_BB_FOR_EACH_INS (cfg->bblocks [i], inst) {
295 if (inst->ssa_op == MONO_SSA_STORE) {
296 idx = inst->inst_i0->inst_c0;
297 g_assert (idx < cfg->num_varinfo);
298 mono_bitset_set (vinfo [idx].def_in, i);
303 /* insert phi functions */
304 for (i = 0; i < cfg->num_varinfo; ++i) {
305 set = mono_compile_iterated_dfrontier (cfg, vinfo [i].def_in);
306 mono_bitset_foreach_bit (set, idx, cfg->num_bblocks) {
307 MonoBasicBlock *bb = cfg->bblocks [idx];
309 /* fixme: create pruned SSA? we would need liveness information for that */
311 if (bb == cfg->bb_exit)
314 if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
315 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
319 NEW_PHI (cfg, inst, i);
321 inst->inst_phi_args = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
322 inst->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
324 store = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));
325 if (!cfg->varinfo [i]->inst_vtype->type)
326 g_assert_not_reached ();
327 store->opcode = mono_type_to_stind (cfg->varinfo [i]->inst_vtype);
328 store->ssa_op = MONO_SSA_STORE;
329 store->inst_i0 = cfg->varinfo [i];
330 store->inst_i1 = inst;
331 store->klass = store->inst_i0->klass;
333 store->next = bb->code;
336 bb->last_ins = bb->code;
339 printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
345 for (i = 0; i < cfg->num_varinfo; ++i)
346 mono_bitset_free (vinfo [i].def_in);
350 stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
352 for (i = 0; i < cfg->num_varinfo; i++)
355 mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, stack);
357 cfg->comp_done |= MONO_COMP_SSA;
360 #ifndef USE_ORIGINAL_VARS
362 mono_ssa_get_allocatable_vars (MonoCompile *cfg)
364 GHashTable *type_hash;
365 GPtrArray *varlist_array = g_ptr_array_new ();
368 g_assert (cfg->comp_done & MONO_COMP_LIVENESS);
370 type_hash = g_hash_table_new (NULL, NULL);
372 for (i = 0; i < cfg->num_varinfo; i++) {
373 MonoInst *ins = cfg->varinfo [i];
374 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
377 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
380 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) ||
381 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG) || vmv->reg != -1)
384 g_assert (ins->inst_vtype);
385 g_assert (vmv->reg == -1);
386 g_assert (i == vmv->idx);
388 if (!(tidx = (int)g_hash_table_lookup (type_hash, ins->inst_vtype))) {
389 GList *vars = g_list_append (NULL, vmv);
390 g_ptr_array_add (varlist_array, vars);
391 g_hash_table_insert (type_hash, ins->inst_vtype, (gpointer)varlist_array->len);
394 g_ptr_array_index (varlist_array, tidx) =
395 mono_varlist_insert_sorted (cfg, g_ptr_array_index (varlist_array, tidx), vmv, FALSE);
399 g_hash_table_destroy (type_hash);
401 return varlist_array;
406 mono_ssa_replace_copies (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, char *is_live)
413 arity = mono_burg_arity [inst->opcode];
415 if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN || inst->ssa_op == MONO_SSA_STORE) &&
416 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
418 int idx = inst->inst_i0->inst_c0;
419 MonoMethodVar *mv = MONO_VARINFO (cfg, idx);
421 if (mv->reg != -1 && mv->reg != mv->idx) {
423 is_live [mv->reg] = 1;
425 new_var = cfg->varinfo [mv->reg];
428 printf ("REPLACE COPY BB%d %d %d\n", bb->block_num, idx, new_var->inst_c0);
429 g_assert (cfg->varinfo [mv->reg]->inst_vtype == cfg->varinfo [idx]->inst_vtype);
431 inst->inst_p0 = new_var;
433 is_live [mv->idx] = 1;
439 mono_ssa_replace_copies (cfg, bb, inst->inst_left, is_live);
441 mono_ssa_replace_copies (cfg, bb, inst->inst_right, is_live);
444 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->ssa_op == MONO_SSA_LOAD &&
445 inst->inst_i0->inst_c0 == inst->inst_i1->inst_i0->inst_c0) {
446 inst->ssa_op = MONO_SSA_NOP;
447 inst->opcode = OP_NOP;
453 mono_ssa_remove (MonoCompile *cfg)
455 MonoInst *inst, *phi;
458 #ifndef USE_ORIGINAL_VARS
459 GPtrArray *varlist_array;
462 g_assert (cfg->comp_done & MONO_COMP_SSA);
464 for (i = 0; i < cfg->num_bblocks; ++i) {
465 MonoBasicBlock *bb = cfg->bblocks [i];
466 MONO_BB_FOR_EACH_INS (bb, inst) {
467 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i1->opcode == OP_PHI) {
470 g_assert (phi->inst_phi_args [0] == bb->in_count);
472 for (j = 0; j < bb->in_count; j++) {
473 MonoBasicBlock *pred = bb->in_bb [j];
474 int idx = phi->inst_phi_args [j + 1];
475 MonoMethodVar *mv = MONO_VARINFO (cfg, idx);
477 if (mv->reg != -1 && mv->reg != mv->idx) {
478 //printf ("PHICOPY %d %d -> %d\n", idx, mv->reg, inst->inst_i0->inst_c0);
483 if (idx != inst->inst_i0->inst_c0) {
485 printf ("MOVE %d to %d in BB%d\n", idx, inst->inst_i0->inst_c0, pred->block_num);
487 mono_add_varcopy_to_end (cfg, pred, idx, inst->inst_i0->inst_c0);
491 /* remove the phi functions */
492 inst->opcode = OP_NOP;
493 inst->ssa_op = MONO_SSA_NOP;
498 #ifndef USE_ORIGINAL_VARS
499 /* we compute liveness again */
500 cfg->comp_done &= ~MONO_COMP_LIVENESS;
501 mono_analyze_liveness (cfg);
503 varlist_array = mono_ssa_get_allocatable_vars (cfg);
505 for (i = 0; i < varlist_array->len; i++) {
506 GList *l, *regs, *vars = g_ptr_array_index (varlist_array, i);
507 MonoMethodVar *vmv, *amv;
509 if (g_list_length (vars) <= 1) {
516 for (l = vars; l; l = l->next) {
519 /* expire old intervals in active */
521 amv = (MonoMethodVar *)active->data;
523 if (amv->range.last_use.abs_pos >= vmv->range.first_use.abs_pos)
526 active = g_list_delete_link (active, active);
527 regs = g_list_prepend (regs, (gpointer)amv->reg);
531 regs = g_list_prepend (regs, (gpointer)vmv->idx);
533 vmv->reg = (int)regs->data;
534 regs = g_list_delete_link (regs, regs);
535 active = mono_varlist_insert_sorted (cfg, active, vmv, TRUE);
538 g_list_free (active);
543 g_ptr_array_free (varlist_array, TRUE);
547 is_live = alloca (cfg->num_varinfo);
548 memset (is_live, 0, cfg->num_varinfo);
550 for (i = 0; i < cfg->num_bblocks; ++i) {
551 MonoBasicBlock *bb = cfg->bblocks [i];
553 MONO_BB_FOR_EACH_INS (bb, inst)
554 mono_ssa_replace_copies (cfg, bb, inst, is_live);
557 for (i = 0; i < cfg->num_varinfo; ++i) {
558 MONO_VARINFO (cfg, i)->reg = -1;
559 if (!is_live [i] && !(cfg->varinfo [i]->flags & MONO_INST_VOLATILE)) {
560 cfg->varinfo [i]->flags |= MONO_INST_IS_DEAD;
564 if (cfg->comp_done & MONO_COMP_REACHABILITY)
565 unlink_unused_bblocks (cfg);
567 cfg->comp_done &= ~MONO_COMP_SSA;
571 #define IS_CALL(op) (op == CEE_CALLI || op == OP_CALL || op == OP_CALLVIRT || (op >= OP_VOIDCALL && op <= OP_CALL_MEMBASE))
582 * Returns TRUE if the tree can have side effects.
585 analyze_dev_use (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *root, MonoInst *inst)
589 gboolean has_side_effects;
594 arity = mono_burg_arity [inst->opcode];
595 switch (inst->opcode) {
596 #define ANALYZE_DEV_USE_SPECIFIC_OPS 1
597 #define OPDEF(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) case a1:
598 #include "simple-cee-ops.h"
600 #define MINI_OP(a1,a2) case a1:
601 #include "simple-mini-ops.h"
603 #undef ANALYZE_DEV_USE_SPECIFIC_OPS
604 has_side_effects = FALSE;
607 has_side_effects = TRUE;
610 if ((inst->ssa_op == MONO_SSA_STORE) &&
611 (inst->inst_i0->opcode == OP_LOCAL /*|| inst->inst_i0->opcode == OP_ARG */)) {
612 idx = inst->inst_i0->inst_c0;
613 info = MONO_VARINFO (cfg, idx);
614 //printf ("%d defined in BB%d %p\n", idx, bb->block_num, root);
616 g_warning ("more than one definition of variable %d in %s", idx,
617 mono_method_full_name (cfg->method, TRUE));
618 g_assert_not_reached ();
620 if (!IS_CALL (inst->inst_i1->opcode) /* && inst->inst_i1->opcode == OP_ICONST */) {
621 g_assert (inst == root);
626 if (inst->inst_i1->opcode == OP_PHI) {
627 for (i = inst->inst_i1->inst_phi_args [0]; i > 0; i--) {
628 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
629 idx = inst->inst_i1->inst_phi_args [i];
630 info = MONO_VARINFO (cfg, idx);
631 //printf ("FOUND %d\n", idx);
634 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
639 if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) &&
640 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
641 MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
642 idx = inst->inst_i0->inst_c0;
643 info = MONO_VARINFO (cfg, idx);
644 //printf ("FOUND %d\n", idx);
647 info->uses = g_list_prepend_mempool (cfg->mempool, info->uses, ui);
650 //if (inst->ssa_op != MONO_SSA_STORE)
651 if (analyze_dev_use (cfg, bb, root, inst->inst_left))
652 has_side_effects = TRUE;
654 if (analyze_dev_use (cfg, bb, root, inst->inst_right))
655 has_side_effects = TRUE;
659 return has_side_effects;
663 /* avoid unnecessary copies of variables:
664 * Y <= X; Z = Y; is translated to Z = X;
667 mono_ssa_avoid_copies (MonoCompile *cfg)
669 MonoInst *inst, *next;
671 MonoMethodVar *i1, *i2;
673 g_assert ((cfg->comp_done & MONO_COMP_SSA_DEF_USE));
675 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
676 MONO_BB_FOR_EACH_INS (bb, inst) {
677 if (inst->ssa_op == MONO_SSA_STORE && inst->inst_i0->opcode == OP_LOCAL &&
678 !IS_CALL (inst->inst_i1->opcode) && inst->inst_i1->opcode != OP_PHI && !inst->flags) {
679 i1 = MONO_VARINFO (cfg, inst->inst_i0->inst_c0);
681 /* fixme: compiling mcs does not work when I enable this */
683 if (g_list_length (i1->uses) == 1 && !extends_live (inst->inst_i1)) {
684 MonoVarUsageInfo *vi = (MonoVarUsageInfo *)i1->uses->data;
687 //printf ("VAR %d %s\n", i1->idx, mono_method_full_name (cfg->method, TRUE));
688 //mono_print_tree (inst); printf ("\n");
689 //mono_print_tree (u); printf ("\n");
691 if (replace_usage_new (cfg, u, inst->inst_i0->inst_c0, inst->inst_i1)) {
693 //mono_print_tree (u); printf ("\n");
695 inst->opcode = OP_NOP;
696 inst->ssa_op = MONO_SSA_NOP;
701 if (next && next->ssa_op == MONO_SSA_STORE &&
702 next->inst_i0->opcode == OP_LOCAL &&
703 next->inst_i1->ssa_op == MONO_SSA_LOAD &&
704 next->inst_i1->inst_i0->opcode == OP_LOCAL &&
705 next->inst_i1->inst_i0->inst_c0 == inst->inst_i0->inst_c0 &&
706 g_list_length (i1->uses) == 1 &&
707 inst->opcode == next->opcode &&
708 inst->inst_i0->type == next->inst_i0->type) {
709 i2 = MONO_VARINFO (cfg, next->inst_i0->inst_c0);
710 //printf ("ELIM. COPY in BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
711 inst->inst_i0 = next->inst_i0;
715 next->opcode = OP_NOP;
716 next->ssa_op = MONO_SSA_NOP;
724 mono_ssa_create_def_use (MonoCompile *cfg)
728 g_assert (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE));
730 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
732 MONO_BB_FOR_EACH_INS (bb, inst) {
733 gboolean has_side_effects = analyze_dev_use (cfg, bb, inst, inst);
734 if (has_side_effects && (inst->ssa_op == MONO_SSA_STORE) &&
735 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
736 inst->inst_i0->flags |= MONO_INST_DEFINITION_HAS_SIDE_EFFECTS;
741 cfg->comp_done |= MONO_COMP_SSA_DEF_USE;
745 simulate_compare (int opcode, int a, int b)
761 return (unsigned)a >= (unsigned)b;
763 return (unsigned)a > (unsigned)b;
765 return (unsigned)a <= (unsigned)b;
767 return (unsigned)a < (unsigned)b;
769 g_assert_not_reached ();
776 simulate_long_compare (int opcode, gint64 a, gint64 b)
792 return (guint64)a >= (guint64)b;
794 return (guint64)a > (guint64)b;
796 return (guint64)a <= (guint64)b;
798 return (guint64)a < (guint64)b;
800 g_assert_not_reached ();
806 #define EVAL_CXX(name,op,cast) \
808 if ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE)) { \
809 r1 = evaluate_const_tree (cfg, inst->inst_i0->inst_i0, &a, carray); \
810 r2 = evaluate_const_tree (cfg, inst->inst_i0->inst_i1, &b, carray); \
811 if (r1 == 1 && r2 == 1) { \
812 *res = ((cast)a op (cast)b); \
815 return MAX (r1, r2); \
820 #define EVAL_BINOP(name,op) \
822 r1 = evaluate_const_tree (cfg, inst->inst_i0, &a, carray); \
823 r2 = evaluate_const_tree (cfg, inst->inst_i1, &b, carray); \
824 if (r1 == 1 && r2 == 1) { \
828 return MAX (r1, r2); \
833 /* fixme: this only works for interger constants, but not for other types (long, float) */
835 evaluate_const_tree (MonoCompile *cfg, MonoInst *inst, int *res, MonoInst **carray)
843 if (inst->ssa_op == MONO_SSA_LOAD &&
844 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
845 (c0 = carray [inst->inst_i0->inst_c0])) {
850 switch (inst->opcode) {
852 *res = inst->inst_c0;
855 EVAL_CXX (OP_CEQ,==,gint32)
856 EVAL_CXX (OP_CGT,>,gint32)
857 EVAL_CXX (OP_CGT_UN,>,guint32)
858 EVAL_CXX (OP_CLT,<,gint32)
859 EVAL_CXX (OP_CLT_UN,<,guint32)
861 EVAL_BINOP (CEE_ADD,+)
862 EVAL_BINOP (CEE_SUB,-)
863 EVAL_BINOP (CEE_MUL,*)
864 EVAL_BINOP (CEE_AND,&)
865 EVAL_BINOP (CEE_OR,|)
866 EVAL_BINOP (CEE_XOR,^)
867 EVAL_BINOP (CEE_SHL,<<)
868 EVAL_BINOP (CEE_SHR,>>)
878 fold_tree (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **carray)
886 arity = mono_burg_arity [inst->opcode];
888 if (inst->ssa_op == MONO_SSA_STORE &&
889 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
890 inst->inst_i1->opcode == OP_PHI && (c0 = carray [inst->inst_i0->inst_c0])) {
891 //{static int cn = 0; printf ("PHICONST %d %d %s\n", cn++, c0->inst_c0, mono_method_full_name (cfg->method, TRUE));}
892 *inst->inst_i1 = *c0;
893 } else if (inst->ssa_op == MONO_SSA_LOAD &&
894 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG) &&
895 (c0 = carray [inst->inst_i0->inst_c0])) {
896 //{static int cn = 0; printf ("YCCOPY %d %d %s\n", cn++, c0->inst_c0, mono_method_full_name (cfg->method, TRUE));}
901 fold_tree (cfg, bb, inst->inst_left, carray);
903 fold_tree (cfg, bb, inst->inst_right, carray);
904 mono_constant_fold_inst (inst, NULL);
908 if ((inst->opcode >= CEE_BEQ && inst->opcode <= CEE_BLT_UN) &&
909 ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE))) {
910 MonoInst *v0 = inst->inst_i0->inst_i0;
911 MonoInst *v1 = inst->inst_i0->inst_i1;
912 MonoBasicBlock *target = NULL;
914 /* hack for longs to optimize the simply cases */
915 if (v0->opcode == OP_I8CONST && v1->opcode == OP_I8CONST) {
916 if (simulate_long_compare (inst->opcode, v0->inst_l, v1->inst_l)) {
917 //unlink_target (bb, inst->inst_false_bb);
918 target = inst->inst_true_bb;
920 //unlink_target (bb, inst->inst_true_bb);
921 target = inst->inst_false_bb;
923 } else if (evaluate_const_tree (cfg, v0, &a, carray) == 1 &&
924 evaluate_const_tree (cfg, v1, &b, carray) == 1) {
925 if (simulate_compare (inst->opcode, a, b)) {
926 //unlink_target (bb, inst->inst_false_bb);
927 target = inst->inst_true_bb;
929 //unlink_target (bb, inst->inst_true_bb);
930 target = inst->inst_false_bb;
935 bb->out_bb [0] = target;
937 inst->opcode = OP_BR;
938 inst->inst_target_bb = target;
940 } else if (inst->opcode == OP_SWITCH && (evaluate_const_tree (cfg, inst->inst_left, &a, carray) == 1) && (a >= 0) && (a < GPOINTER_TO_INT (inst->klass))) {
941 bb->out_bb [0] = inst->inst_many_bb [a];
943 inst->inst_target_bb = bb->out_bb [0];
944 inst->opcode = OP_BR;
950 change_varstate (MonoCompile *cfg, GList **cvars, MonoMethodVar *info, int state, MonoInst *c0, MonoInst **carray)
952 if (info->cpstate >= state)
955 info->cpstate = state;
957 //printf ("SETSTATE %d to %d\n", info->idx, info->cpstate);
960 carray [info->idx] = c0;
962 carray [info->idx] = NULL;
964 if (!g_list_find (*cvars, info)) {
965 *cvars = g_list_prepend (*cvars, info);
970 visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, GList **cvars, GList **bblist, MonoInst **carray)
974 if (inst->opcode == OP_SWITCH) {
976 int cases = GPOINTER_TO_INT (inst->klass);
978 r1 = evaluate_const_tree (cfg, inst->inst_left, &a, carray);
979 if ((r1 == 1) && ((a < 0) || (a >= cases)))
982 MonoBasicBlock *tb = inst->inst_many_bb [a];
983 if (!(tb->flags & BB_REACHABLE)) {
984 tb->flags |= BB_REACHABLE;
985 *bblist = g_list_prepend (*bblist, tb);
987 } else if (r1 == 2) {
988 for (i = GPOINTER_TO_INT (inst->klass); i >= 0; i--) {
989 MonoBasicBlock *tb = inst->inst_many_bb [i];
990 if (!(tb->flags & BB_REACHABLE)) {
991 tb->flags |= BB_REACHABLE;
992 *bblist = g_list_prepend (*bblist, tb);
996 } else if (inst->opcode == OP_BR) {
997 MonoBasicBlock *target = inst->inst_target_bb;
999 if (!(target->flags & BB_REACHABLE)) {
1000 target->flags |= BB_REACHABLE;
1001 *bblist = g_list_prepend (*bblist, target);
1003 } else if ((inst->opcode >= CEE_BEQ && inst->opcode <= CEE_BLT_UN) &&
1004 ((inst->inst_i0->opcode == OP_COMPARE) || (inst->inst_i0->opcode == OP_LCOMPARE))) {
1006 MonoInst *v0 = inst->inst_i0->inst_i0;
1007 MonoInst *v1 = inst->inst_i0->inst_i1;
1009 r1 = evaluate_const_tree (cfg, v0, &a, carray);
1010 r2 = evaluate_const_tree (cfg, v1, &b, carray);
1012 if (r1 == 1 && r2 == 1) {
1013 MonoBasicBlock *target;
1015 if (simulate_compare (inst->opcode, a, b)) {
1016 target = inst->inst_true_bb;
1018 target = inst->inst_false_bb;
1020 if (!(target->flags & BB_REACHABLE)) {
1021 target->flags |= BB_REACHABLE;
1022 *bblist = g_list_prepend (*bblist, target);
1024 } else if (r1 == 2 || r2 == 2) {
1025 if (!(inst->inst_true_bb->flags & BB_REACHABLE)) {
1026 inst->inst_true_bb->flags |= BB_REACHABLE;
1027 *bblist = g_list_prepend (*bblist, inst->inst_true_bb);
1029 if (!(inst->inst_false_bb->flags & BB_REACHABLE)) {
1030 inst->inst_false_bb->flags |= BB_REACHABLE;
1031 *bblist = g_list_prepend (*bblist, inst->inst_false_bb);
1034 } else if (inst->ssa_op == MONO_SSA_STORE &&
1035 (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
1036 MonoMethodVar *info = MONO_VARINFO (cfg, inst->inst_i0->inst_c0);
1037 MonoInst *i1 = inst->inst_i1;
1040 if (info->cpstate < 2) {
1041 if (i1->opcode == OP_ICONST) {
1042 change_varstate (cfg, cvars, info, 1, i1, carray);
1043 } else if (i1->opcode == OP_PHI) {
1044 MonoInst *c0 = NULL;
1047 for (j = 1; j <= i1->inst_phi_args [0]; j++) {
1048 MonoMethodVar *mv = MONO_VARINFO (cfg, i1->inst_phi_args [j]);
1049 MonoInst *src = mv->def;
1051 if (mv->def_bb && !(mv->def_bb->flags & BB_REACHABLE)) {
1055 if (!mv->def || !src || src->ssa_op != MONO_SSA_STORE ||
1056 !(src->inst_i0->opcode == OP_LOCAL || src->inst_i0->opcode == OP_ARG) ||
1058 change_varstate (cfg, cvars, info, 2, NULL, carray);
1062 if (mv->cpstate == 0)
1065 //g_assert (src->inst_i1->opcode == OP_ICONST);
1066 g_assert (carray [mv->idx]);
1069 c0 = carray [mv->idx];
1072 if (carray [mv->idx]->inst_c0 != c0->inst_c0) {
1073 change_varstate (cfg, cvars, info, 2, NULL, carray);
1078 if (c0 && info->cpstate < 1) {
1079 change_varstate (cfg, cvars, info, 1, c0, carray);
1082 int state = evaluate_const_tree (cfg, i1, &res, carray);
1084 NEW_ICONST (cfg, i1, res);
1085 change_varstate (cfg, cvars, info, 1, i1, carray);
1087 change_varstate (cfg, cvars, info, 2, NULL, carray);
1095 mono_ssa_cprop (MonoCompile *cfg)
1099 GList *bblock_list, *cvars;
1102 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1104 carray = g_new0 (MonoInst*, cfg->num_varinfo);
1106 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1107 mono_ssa_create_def_use (cfg);
1109 bblock_list = g_list_prepend (NULL, cfg->bb_entry);
1110 cfg->bb_entry->flags |= BB_REACHABLE;
1112 memset (carray, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1114 for (i = 0; i < cfg->num_varinfo; i++) {
1115 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1122 while (bblock_list) {
1125 bb = (MonoBasicBlock *)bblock_list->data;
1127 bblock_list = g_list_delete_link (bblock_list, bblock_list);
1129 g_assert (bb->flags & BB_REACHABLE);
1131 if (bb->out_count == 1) {
1132 if (!(bb->out_bb [0]->flags & BB_REACHABLE)) {
1133 bb->out_bb [0]->flags |= BB_REACHABLE;
1134 bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
1138 MONO_BB_FOR_EACH_INS (bb, inst)
1139 visit_inst (cfg, bb, inst, &cvars, &bblock_list, carray);
1142 MonoMethodVar *info = (MonoMethodVar *)cvars->data;
1143 cvars = g_list_delete_link (cvars, cvars);
1145 for (tmp = info->uses; tmp; tmp = tmp->next) {
1146 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1147 if (!(ui->bb->flags & BB_REACHABLE))
1149 visit_inst (cfg, ui->bb, ui->inst, &cvars, &bblock_list, carray);
1154 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1156 MONO_BB_FOR_EACH_INS (bb, inst)
1157 fold_tree (cfg, bb, inst, carray);
1162 cfg->comp_done |= MONO_COMP_REACHABILITY;
1166 add_to_dce_worklist (MonoCompile *cfg, MonoMethodVar *var, MonoMethodVar *use, GList **wl)
1170 *wl = g_list_prepend (*wl, use);
1172 for (tmp = use->uses; tmp; tmp = tmp->next) {
1173 MonoVarUsageInfo *ui = (MonoVarUsageInfo *)tmp->data;
1174 if (ui->inst == var->def) {
1175 /* from the mempool */
1176 use->uses = g_list_remove_link (use->uses, tmp);
1183 mono_ssa_deadce (MonoCompile *cfg)
1188 g_assert (cfg->comp_done & MONO_COMP_SSA);
1190 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1192 /* fixme: we should update usage infos during cprop, instead of computing it again */
1193 cfg->comp_done &= ~MONO_COMP_SSA_DEF_USE;
1194 for (i = 0; i < cfg->num_varinfo; i++) {
1195 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1200 if (!(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
1201 mono_ssa_create_def_use (cfg);
1203 mono_ssa_avoid_copies (cfg);
1206 for (i = 0; i < cfg->num_varinfo; i++) {
1207 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1208 work_list = g_list_prepend (work_list, info);
1210 //if ((info->def != NULL) && (info->def->inst_i1->opcode != OP_PHI)) printf ("SSA DEADCE TOTAL LOCAL\n");
1214 MonoMethodVar *info = (MonoMethodVar *)work_list->data;
1215 work_list = g_list_delete_link (work_list, work_list);
1217 if (!info->uses && info->def && (!(cfg->varinfo [info->idx]->flags & (MONO_INST_DEFINITION_HAS_SIDE_EFFECTS|MONO_INST_VOLATILE|MONO_INST_INDIRECT)))) {
1219 //printf ("ELIMINATE %s: ", mono_method_full_name (cfg->method, TRUE)); mono_print_tree (info->def); printf ("\n");
1221 i1 = info->def->inst_i1;
1222 if (i1->opcode == OP_PHI) {
1224 for (j = i1->inst_phi_args [0]; j > 0; j--) {
1225 MonoMethodVar *u = MONO_VARINFO (cfg, i1->inst_phi_args [j]);
1226 add_to_dce_worklist (cfg, info, u, &work_list);
1228 } else if (i1->ssa_op == MONO_SSA_LOAD &&
1229 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG)) {
1230 MonoMethodVar *u = MONO_VARINFO (cfg, i1->inst_i0->inst_c0);
1231 add_to_dce_worklist (cfg, info, u, &work_list);
1233 //if (i1->opcode != OP_PHI) printf ("SSA DEADCE DEAD LOCAL\n");
1235 info->def->opcode = OP_NOP;
1236 info->def->ssa_op = MONO_SSA_NOP;
1244 mono_ssa_strength_reduction (MonoCompile *cfg)
1249 g_assert (cfg->comp_done & MONO_COMP_SSA);
1250 g_assert (cfg->comp_done & MONO_COMP_LOOPS);
1251 g_assert (cfg->comp_done & MONO_COMP_SSA_DEF_USE);
1253 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
1254 GList *lp = bb->loop_blocks;
1257 MonoBasicBlock *h = (MonoBasicBlock *)lp->data;
1259 /* we only consider loops with 2 in bblocks */
1260 if (!h->in_count == 2)
1263 for (i = 0; i < cfg->num_varinfo; i++) {
1264 MonoMethodVar *info = MONO_VARINFO (cfg, i);
1266 if (info->def && info->def->ssa_op == MONO_SSA_STORE &&
1267 info->def->inst_i0->opcode == OP_LOCAL && g_list_find (lp, info->def_bb)) {
1268 MonoInst *v = info->def->inst_i1;
1271 printf ("FOUND %d in %s\n", info->idx, mono_method_full_name (cfg->method, TRUE));
1279 #endif /* DISABLE_SSA */