2 * branch-opts.c: Branch optimizations support
5 * Patrik Torstensson (Patrik.Torstesson at gmail.com)
7 * (C) 2005 Ximian, Inc. http://www.ximian.com
8 * Copyright 2011 Xamarin Inc. http://www.xamarin.com
16 * Returns true if @bb is a basic block which falls through the next block.
17 * TODO verify if it helps to check if the bb last ins is a branch to its successor.
20 mono_bb_is_fall_through (MonoCompile *cfg, MonoBasicBlock *bb)
22 return bb->next_bb && bb->next_bb->region == bb->region && /*fall throught between regions is not really interesting or useful*/
23 (bb->last_ins == NULL || !MONO_IS_BRANCH_OP (bb->last_ins)); /*and the last op can't be a branch too*/
27 * Used by the arch code to replace the exception handling
28 * with a direct branch. This is safe to do if the
29 * exception object isn't used, no rethrow statement and
30 * no filter statement (verify).
34 mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname)
36 MonoMethodHeader *header = cfg->header;
37 MonoExceptionClause *clause;
41 if (!(cfg->opt & MONO_OPT_EXCEPTION))
44 if (bb->region == -1 || !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
47 exclass = mono_class_from_name (mono_get_corlib (), "System", exname);
48 /* search for the handler */
49 for (i = 0; i < header->num_clauses; ++i) {
50 clause = &header->clauses [i];
51 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset)) {
52 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE && clause->data.catch_class && mono_class_is_assignable_from (clause->data.catch_class, exclass)) {
55 /* get the basic block for the handler and
56 * check if the exception object is used.
57 * Flag is set during method_to_ir due to
58 * pop-op is optmized away in codegen (burg).
60 tbb = cfg->cil_offset_to_bb [clause->handler_offset];
61 if (tbb && tbb->flags & BB_EXCEPTION_DEAD_OBJ && !(tbb->flags & BB_EXCEPTION_UNSAFE)) {
62 MonoBasicBlock *targetbb = tbb;
63 gboolean unsafe = FALSE;
65 /* Check if this catch clause is ok to optimize by
66 * looking for the BB_EXCEPTION_UNSAFE in every BB that
67 * belongs to the same region.
69 * UNSAFE flag is set during method_to_ir (OP_RETHROW)
71 while (!unsafe && tbb->next_bb && tbb->region == tbb->next_bb->region) {
72 if (tbb->next_bb->flags & BB_EXCEPTION_UNSAFE) {
82 /* Create dummy inst to allow easier integration in
83 * arch dependent code (opcode ignored)
85 MONO_INST_NEW (cfg, jump, OP_BR);
87 /* Allocate memory for our branch target */
88 jump->inst_i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
89 jump->inst_true_bb = targetbb;
91 if (cfg->verbose_level > 2)
92 g_print ("found exception to optimize - returning branch to BB%d (%s) (instead of throw) for method %s:%s\n", targetbb->block_num, clause->data.catch_class->name, cfg->method->klass->name, cfg->method->name);
99 /* Branching to an outer clause could skip inner clauses */
103 /* Branching to an outer clause could skip inner clauses */
112 static const int int_cmov_opcodes [] = {
125 static const int long_cmov_opcodes [] = {
138 static G_GNUC_UNUSED int
139 br_to_br_un (int opcode)
155 g_assert_not_reached ();
163 * Replace INS with its decomposition which is stored in a series of bblocks starting
164 * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS.
165 * On return, it will be set to the last ins of the decomposition.
168 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
170 MonoInst *next = ins->next;
172 if (next && next->opcode == OP_NOP) {
173 /* Avoid NOPs following branches */
174 ins->next = next->next;
178 if (first_bb == last_bb) {
180 * Only one replacement bb, merge the code into
184 /* Delete links between the first_bb and its successors */
185 while (first_bb->out_count)
186 mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
190 (*prev)->next = first_bb->code;
191 first_bb->code->prev = (*prev);
193 bb->code = first_bb->code;
197 last_bb->last_ins->next = next;
199 next->prev = last_bb->last_ins;
201 bb->last_ins = last_bb->last_ins;
202 *prev = last_bb->last_ins;
203 bb->has_array_access |= first_bb->has_array_access;
206 MonoBasicBlock **tmp_bblocks, *tmp;
212 for (tmp = first_bb; tmp; tmp = tmp->next_bb)
213 tmp->region = bb->region;
215 /* Split the original bb */
217 ins->next->prev = NULL;
221 /* Merge the second part of the original bb into the last bb */
222 if (last_bb->last_ins) {
223 last_bb->last_ins->next = next;
225 next->prev = last_bb->last_ins;
227 last_bb->code = next;
229 last_bb->has_array_access |= bb->has_array_access;
232 for (last = next; last->next != NULL; last = last->next)
234 last_bb->last_ins = last;
237 for (i = 0; i < bb->out_count; ++i)
238 mono_link_bblock (cfg, last_bb, bb->out_bb [i]);
240 /* Merge the first (dummy) bb to the original bb */
242 (*prev)->next = first_bb->code;
243 first_bb->code->prev = (*prev);
245 bb->code = first_bb->code;
247 bb->last_ins = first_bb->last_ins;
248 bb->has_array_access |= first_bb->has_array_access;
250 /* Delete the links between the original bb and its successors */
251 tmp_bblocks = bb->out_bb;
252 count = bb->out_count;
253 for (i = 0; i < count; ++i)
254 mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
256 /* Add links between the original bb and the first_bb's successors */
257 for (i = 0; i < first_bb->out_count; ++i) {
258 MonoBasicBlock *out_bb = first_bb->out_bb [i];
260 mono_link_bblock (cfg, bb, out_bb);
262 /* Delete links between the first_bb and its successors */
263 for (i = 0; i < bb->out_count; ++i) {
264 MonoBasicBlock *out_bb = bb->out_bb [i];
266 mono_unlink_bblock (cfg, first_bb, out_bb);
268 last_bb->next_bb = bb->next_bb;
269 bb->next_bb = first_bb->next_bb;
276 mono_if_conversion (MonoCompile *cfg)
278 #ifdef MONO_ARCH_HAVE_CMOV_OPS
280 gboolean changed = FALSE;
281 int filter = FILTER_NOP | FILTER_IL_SEQ_POINT;
283 if (!(cfg->opt & MONO_OPT_CMOV))
286 // FIXME: Make this work with extended bblocks
289 * This pass requires somewhat optimized IR code so it should be run after
290 * local cprop/deadce. Also, it should be run before dominator computation, since
291 * it changes control flow.
293 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
294 MonoBasicBlock *bb1, *bb2;
297 /* Look for the IR code generated from cond ? a : b
308 if (!(bb->out_count == 2 && !bb->extended))
311 bb1 = bb->out_bb [0];
312 bb2 = bb->out_bb [1];
314 if (bb1->in_count == 1 && bb2->in_count == 1 && bb1->out_count == 1 && bb2->out_count == 1 && bb1->out_bb [0] == bb2->out_bb [0]) {
315 MonoInst *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
316 MonoBasicBlock *true_bb, *false_bb;
317 gboolean simple, ret;
321 branch = mono_bb_last_inst (bb, filter);
323 if (!branch || branch->opcode == OP_BR_REG || branch->opcode == OP_BR)
326 /* Find the compare instruction */
327 compare = mono_inst_prev (branch, filter);
331 if (!MONO_IS_COND_BRANCH_OP (branch))
332 /* This can happen if a cond branch is optimized away */
335 true_bb = branch->inst_true_bb;
336 false_bb = branch->inst_false_bb;
339 * Check that bb1 and bb2 are 'simple' and both assign to the same
342 /* FIXME: Get rid of the nops earlier */
343 ins1 = mono_bb_first_inst (true_bb, filter);
344 ins2 = mono_bb_first_inst (false_bb, filter);
346 if (!(ins1 && ins2 && ins1->dreg == ins2->dreg && ins1->dreg != -1))
350 for (tmp = ins1->next; tmp; tmp = tmp->next)
351 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_IL_SEQ_POINT) || (tmp->opcode == OP_BR)))
354 for (tmp = ins2->next; tmp; tmp = tmp->next)
355 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_IL_SEQ_POINT) || (tmp->opcode == OP_BR)))
361 /* We move ins1/ins2 before the compare so they should have no side effect */
362 if (!(MONO_INS_HAS_NO_SIDE_EFFECT (ins1) && MONO_INS_HAS_NO_SIDE_EFFECT (ins2)))
365 /* Moving ins1/ins2 could change the comparison */
367 if (!((compare->sreg1 != ins1->dreg) && (compare->sreg2 != ins1->dreg)))
371 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
372 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
376 /* ins->type might not be set */
377 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
380 if (cfg->verbose_level > 2) {
381 printf ("\tBranch -> CMove optimization in BB%d on\n", bb->block_num);
382 printf ("\t\t"); mono_print_ins (compare);
383 printf ("\t\t"); mono_print_ins (mono_inst_next (compare, filter));
384 printf ("\t\t"); mono_print_ins (ins1);
385 printf ("\t\t"); mono_print_ins (ins2);
392 /* Assignments to the return register must remain at the end of bbs */
394 ret = ins1->dreg == cfg->ret->dreg;
398 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
401 /* Rewrite ins1 to emit to tmp_reg */
402 ins1->dreg = tmp_reg;
405 dreg = mono_alloc_dreg (cfg, STACK_I4);
409 /* Remove ins1/ins2 from bb1/bb2 */
410 MONO_REMOVE_INS (true_bb, ins1);
411 MONO_REMOVE_INS (false_bb, ins2);
413 /* Move ins1 and ins2 before the comparison */
414 /* ins1 comes first to avoid ins1 overwriting an argument of ins2 */
415 mono_bblock_insert_before_ins (bb, compare, ins2);
416 mono_bblock_insert_before_ins (bb, ins2, ins1);
418 /* Add cmov instruction */
419 MONO_INST_NEW (cfg, cmov, OP_NOP);
422 cmov->sreg2 = tmp_reg;
423 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
425 cmov->opcode = int_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
428 cmov->opcode = long_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
431 g_assert_not_reached ();
433 mono_bblock_insert_after_ins (bb, compare, cmov);
436 /* Add an extra move */
437 MONO_INST_NEW (cfg, move, OP_MOVE);
438 move->dreg = cfg->ret->dreg;
440 mono_bblock_insert_after_ins (bb, cmov, move);
443 /* Rewrite the branch */
444 branch->opcode = OP_BR;
445 branch->inst_target_bb = true_bb->out_bb [0];
446 mono_link_bblock (cfg, bb, branch->inst_target_bb);
448 /* Reorder bblocks */
449 mono_unlink_bblock (cfg, bb, true_bb);
450 mono_unlink_bblock (cfg, bb, false_bb);
451 mono_unlink_bblock (cfg, true_bb, true_bb->out_bb [0]);
452 mono_unlink_bblock (cfg, false_bb, false_bb->out_bb [0]);
453 mono_remove_bblock (cfg, true_bb);
454 mono_remove_bblock (cfg, false_bb);
456 /* Merge bb and its successor if possible */
457 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
458 (bb->region == bb->out_bb [0]->region)) {
459 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
464 /* Look for the IR code generated from if (cond) <var> <- <a>
473 if ((bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) ||
474 (bb1->in_count == 1 && bb1->out_count == 1 && bb1->out_bb [0] == bb2)) {
475 MonoInst *compare, *branch, *ins1, *cmov, *tmp;
480 MonoBasicBlock *next_bb, *code_bb;
482 /* code_bb is the bblock containing code, next_bb is the successor bblock */
483 if (bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) {
491 ins1 = mono_bb_first_inst (code_bb, filter);
496 /* Check that code_bb is simple */
498 for (tmp = ins1; tmp; tmp = tmp->next)
499 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_IL_SEQ_POINT) || (tmp->opcode == OP_BR)))
505 /* We move ins1 before the compare so it should have no side effect */
506 if (!MONO_INS_HAS_NO_SIDE_EFFECT (ins1))
509 branch = mono_bb_last_inst (bb, filter);
511 if (!branch || branch->opcode == OP_BR_REG)
514 /* Find the compare instruction */
515 compare = mono_inst_prev (branch, filter);
519 if (!MONO_IS_COND_BRANCH_OP (branch))
520 /* This can happen if a cond branch is optimized away */
524 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
525 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
529 /* ins->type might not be set */
530 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
534 if (cfg->ret && ins1->dreg == cfg->ret->dreg)
537 if (!(cfg->opt & MONO_OPT_DEADCE))
539 * It is possible that dreg is never set before, so we can't use
540 * it as an sreg of the cmov instruction (#582322).
544 if (cfg->verbose_level > 2) {
545 printf ("\tBranch -> CMove optimization (2) in BB%d on\n", bb->block_num);
546 printf ("\t\t"); mono_print_ins (compare);
547 printf ("\t\t"); mono_print_ins (mono_inst_next (compare, filter));
548 printf ("\t\t"); mono_print_ins (ins1);
555 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
558 /* Rewrite ins1 to emit to tmp_reg */
559 ins1->dreg = tmp_reg;
561 /* Remove ins1 from code_bb */
562 MONO_REMOVE_INS (code_bb, ins1);
564 /* Move ins1 before the comparison */
565 mono_bblock_insert_before_ins (bb, compare, ins1);
567 /* Add cmov instruction */
568 MONO_INST_NEW (cfg, cmov, OP_NOP);
571 cmov->sreg2 = tmp_reg;
572 cond = mono_opcode_to_cond (branch->opcode);
573 if (branch->inst_false_bb == code_bb)
574 cond = mono_negate_cond (cond);
575 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
577 cmov->opcode = int_cmov_opcodes [cond];
580 cmov->opcode = long_cmov_opcodes [cond];
583 g_assert_not_reached ();
585 mono_bblock_insert_after_ins (bb, compare, cmov);
587 /* Rewrite the branch */
588 branch->opcode = OP_BR;
589 branch->inst_target_bb = next_bb;
590 mono_link_bblock (cfg, bb, branch->inst_target_bb);
592 /* Nullify the branch at the end of code_bb */
594 branch = code_bb->code;
595 MONO_DELETE_INS (code_bb, branch);
598 /* Reorder bblocks */
599 mono_unlink_bblock (cfg, bb, code_bb);
600 mono_unlink_bblock (cfg, code_bb, next_bb);
602 /* Merge bb and its successor if possible */
603 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
604 (bb->region == bb->out_bb [0]->region)) {
605 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
608 * bbn might have fallen through to the next bb without a branch,
609 * have to add one now (#474718).
610 * FIXME: Maybe need to do this more generally in
611 * merge_basic_blocks () ?
613 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) && bb->out_count) {
614 MONO_INST_NEW (cfg, ins1, OP_BR);
615 ins1->inst_target_bb = bb->out_bb [0];
616 MONO_ADD_INS (bb, ins1);
624 * Optimize checks like: if (v < 0 || v > limit) by changing then to unsigned
625 * compares. This isn't really if conversion, but it easier to do here than in
626 * optimize_branches () since the IR is already optimized.
628 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
629 MonoBasicBlock *bb1, *bb2, *true_bb, *false_bb, *next_bb;
630 MonoInst *branch1, *branch2, *compare1, *ins, *next;
632 /* Look for the IR code generated from if (<var> < 0 || v > <limit>)
633 * after branch opts which is:
638 * icompare_imm R [<limit>]
641 if (!(bb->out_count == 2 && !bb->extended))
644 bb1 = bb->out_bb [0];
645 bb2 = bb->out_bb [1];
647 // FIXME: Add more cases
649 /* Check structure */
650 if (!(bb1->in_count == 2 && bb1->in_bb [0] == bb && bb1->in_bb [1] == bb2 && bb2->in_count == 1 && bb2->out_count == 2))
655 /* Check first branch */
656 branch1 = mono_bb_last_inst (bb, filter);
657 if (!(branch1 && ((branch1->opcode == OP_IBLT) || (branch1->opcode == OP_LBLT)) && (branch1->inst_false_bb == next_bb)))
660 true_bb = branch1->inst_true_bb;
662 /* Check second branch */
663 branch2 = mono_bb_last_inst (next_bb, filter);
667 /* mcs sometimes generates inverted branches */
668 if (((branch2->opcode == OP_IBGT) || (branch2->opcode == OP_LBGT)) && branch2->inst_true_bb == branch1->inst_true_bb)
669 false_bb = branch2->inst_false_bb;
670 else if (((branch2->opcode == OP_IBLE) || (branch2->opcode == OP_LBLE)) && branch2->inst_false_bb == branch1->inst_true_bb)
671 false_bb = branch2->inst_true_bb;
675 /* Check first compare */
676 compare1 = mono_inst_prev (mono_bb_last_inst (bb, filter), filter);
677 if (!(compare1 && ((compare1->opcode == OP_ICOMPARE_IMM) || (compare1->opcode == OP_LCOMPARE_IMM)) && compare1->inst_imm == 0))
680 /* Check second bblock */
681 ins = mono_bb_first_inst (next_bb, filter);
684 next = mono_inst_next (ins, filter);
685 if (((ins->opcode == OP_ICOMPARE_IMM) || (ins->opcode == OP_LCOMPARE_IMM)) && ins->sreg1 == compare1->sreg1 && next == branch2) {
686 /* The second arg must be positive */
687 if (ins->inst_imm < 0)
689 } else if (((ins->opcode == OP_LDLEN) || (ins->opcode == OP_STRLEN)) && ins->dreg != compare1->sreg1 && next && next->opcode == OP_ICOMPARE && next->sreg1 == compare1->sreg1 && next->sreg2 == ins->dreg && mono_inst_next (next, filter) == branch2) {
690 /* Another common case: if (index < 0 || index > arr.Length) */
695 if (cfg->verbose_level > 2) {
696 printf ("\tSigned->unsigned compare optimization in BB%d on\n", bb->block_num);
697 printf ("\t\t"); mono_print_ins (compare1);
698 printf ("\t\t"); mono_print_ins (mono_inst_next (compare1, filter));
699 printf ("\t\t"); mono_print_ins (ins);
702 /* Rewrite the first compare+branch */
703 MONO_DELETE_INS (bb, compare1);
704 branch1->opcode = OP_BR;
705 mono_unlink_bblock (cfg, bb, branch1->inst_true_bb);
706 mono_unlink_bblock (cfg, bb, branch1->inst_false_bb);
707 branch1->inst_target_bb = next_bb;
708 mono_link_bblock (cfg, bb, next_bb);
710 /* Rewrite the second branch */
711 branch2->opcode = br_to_br_un (branch2->opcode);
713 mono_merge_basic_blocks (cfg, bb, next_bb);
717 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
718 MonoBasicBlock *bb1, *bb2;
719 MonoInst *prev, *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
720 gboolean simple, ret;
724 /* Look for the IR code generated from if (cond) <var> <- <a>
725 * after branch opts which is:
732 if (!(bb->out_count == 1 && bb->extended && bb->code && bb->code->next && bb->code->next->next))
735 mono_print_bb (bb, "");
737 /* Find the compare instruction */
741 while (compare->next->next && compare->next->next != bb->last_ins) {
743 compare = compare->next;
745 branch = compare->next;
746 if (!MONO_IS_COND_BRANCH_OP (branch))
752 if (cfg->opt & MONO_OPT_BRANCH)
753 mono_optimize_branches (cfg);
754 /* Merging bblocks could make some variables local */
755 mono_handle_global_vregs (cfg);
756 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
757 mono_local_cprop (cfg);
758 if (cfg->opt & MONO_OPT_DEADCE)
759 mono_local_deadce (cfg);
765 mono_nullify_basic_block (MonoBasicBlock *bb)
772 bb->code = bb->last_ins = NULL;
777 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
781 for (i = 0; i < bb->out_count; i++) {
782 MonoBasicBlock *ob = bb->out_bb [i];
785 if (bb->out_count > 1) {
786 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
790 bb->out_bb [i] = repl;
797 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
801 for (i = 0; i < bb->in_count; i++) {
802 MonoBasicBlock *ib = bb->in_bb [i];
805 if (bb->in_count > 1) {
806 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
810 bb->in_bb [i] = repl;
817 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
820 #if defined(__native_client_codegen__)
821 /* Need to maintain this flag for the new block because */
822 /* we can't jump indirectly to a non-aligned block. */
823 if (orig->flags & BB_INDIRECT_JUMP_TARGET)
825 repl->flags |= BB_INDIRECT_JUMP_TARGET;
829 for (ins = bb->code; ins != NULL; ins = ins->next) {
830 switch (ins->opcode) {
832 if (ins->inst_target_bb == orig)
833 ins->inst_target_bb = repl;
835 case OP_CALL_HANDLER:
836 if (ins->inst_target_bb == orig)
837 ins->inst_target_bb = repl;
841 int n = GPOINTER_TO_INT (ins->klass);
842 for (i = 0; i < n; i++ ) {
843 if (ins->inst_many_bb [i] == orig)
844 ins->inst_many_bb [i] = repl;
849 if (MONO_IS_COND_BRANCH_OP (ins)) {
850 if (ins->inst_true_bb == orig)
851 ins->inst_true_bb = repl;
852 if (ins->inst_false_bb == orig)
853 ins->inst_false_bb = repl;
854 } else if (MONO_IS_JUMP_TABLE (ins)) {
856 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
857 for (i = 0; i < table->table_size; i++ ) {
858 if (table->table [i] == orig)
859 table->table [i] = repl;
869 * Check if a bb is useless (is just made of NOPs and ends with an
870 * unconditional branch, or nothing).
871 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
872 * Otherwise, return FALSE;
875 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
876 MonoBasicBlock *target_bb = NULL;
879 /* Do not touch handlers */
880 if (bb->region != -1) {
881 bb->not_useless = TRUE;
885 MONO_BB_FOR_EACH_INS (bb, inst) {
886 switch (inst->opcode) {
888 case OP_IL_SEQ_POINT:
891 target_bb = inst->inst_target_bb;
894 bb->not_useless = TRUE;
899 if (target_bb == NULL) {
900 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
901 target_bb = bb->next_bb;
903 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
908 /* Do not touch BBs following a switch (they are the "default" branch) */
909 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == OP_SWITCH)) {
913 /* Do not touch BBs following the entry BB and jumping to something that is not */
914 /* thiry "next" bb (the entry BB cannot contain the branch) */
915 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
920 * Do not touch BBs following a try block as the code in
921 * mini_method_compile needs them to compute the length of the try block.
923 if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
926 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
927 if ((target_bb != NULL) && (target_bb != bb)) {
930 if (cfg->verbose_level > 1) {
931 printf ("remove_block_if_useless, removed BB%d\n", bb->block_num);
934 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
935 while (bb->in_count) {
936 MonoBasicBlock *in_bb = bb->in_bb [0];
937 mono_unlink_bblock (cfg, in_bb, bb);
938 mono_link_bblock (cfg, in_bb, target_bb);
939 replace_out_block_in_code (in_bb, bb, target_bb);
942 mono_unlink_bblock (cfg, bb, target_bb);
943 if (previous_bb != cfg->bb_entry && mono_bb_is_fall_through (cfg, previous_bb)) {
944 for (i = 0; i < previous_bb->out_count; i++) {
945 if (previous_bb->out_bb [i] == target_bb) {
947 MONO_INST_NEW (cfg, jump, OP_BR);
948 MONO_ADD_INS (previous_bb, jump);
949 jump->cil_code = previous_bb->cil_code;
950 jump->inst_target_bb = target_bb;
956 previous_bb->next_bb = bb->next_bb;
957 mono_nullify_basic_block (bb);
966 mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn)
969 MonoBasicBlock *prev_bb;
972 bb->has_array_access |= bbn->has_array_access;
973 bb->extended |= bbn->extended;
975 /* Compute prev_bb if possible to avoid the linear search below */
977 for (i = 0; i < bbn->in_count; ++i)
978 if (bbn->in_bb [0]->next_bb == bbn)
979 prev_bb = bbn->in_bb [0];
981 mono_unlink_bblock (cfg, bb, bbn);
982 for (i = 0; i < bbn->out_count; ++i)
983 mono_link_bblock (cfg, bb, bbn->out_bb [i]);
984 while (bbn->out_count)
985 mono_unlink_bblock (cfg, bbn, bbn->out_bb [0]);
987 /* Handle the branch at the end of the bb */
988 if (bb->has_call_handler) {
989 for (inst = bb->code; inst != NULL; inst = inst->next) {
990 if (inst->opcode == OP_CALL_HANDLER) {
991 g_assert (inst->inst_target_bb == bbn);
996 if (bb->has_jump_table) {
997 for (inst = bb->code; inst != NULL; inst = inst->next) {
998 if (MONO_IS_JUMP_TABLE (inst)) {
1000 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (inst);
1001 for (i = 0; i < table->table_size; i++ ) {
1002 /* Might be already NULL from a previous merge */
1003 if (table->table [i])
1004 g_assert (table->table [i] == bbn);
1005 table->table [i] = NULL;
1007 /* Can't nullify this as later instructions depend on it */
1011 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
1012 g_assert (bb->last_ins->inst_false_bb == bbn);
1013 bb->last_ins->inst_false_bb = NULL;
1014 bb->extended = TRUE;
1015 } else if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
1016 NULLIFY_INS (bb->last_ins);
1019 bb->has_call_handler |= bbn->has_call_handler;
1020 bb->has_jump_table |= bbn->has_jump_table;
1024 bb->last_ins->next = bbn->code;
1025 bbn->code->prev = bb->last_ins;
1026 bb->last_ins = bbn->last_ins;
1029 bb->code = bbn->code;
1030 bb->last_ins = bbn->last_ins;
1034 for (prev_bb = cfg->bb_entry; prev_bb && prev_bb->next_bb != bbn; prev_bb = prev_bb->next_bb)
1038 prev_bb->next_bb = bbn->next_bb;
1040 /* bbn might not be in the bb list yet */
1041 if (bb->next_bb == bbn)
1042 bb->next_bb = bbn->next_bb;
1044 mono_nullify_basic_block (bbn);
1047 * If bbn fell through to its next bblock, have to add a branch, since bb
1048 * will not fall though to the same bblock (#513931).
1050 if (bb->last_ins && bb->out_count == 1 && bb->out_bb [0] != bb->next_bb && !MONO_IS_BRANCH_OP (bb->last_ins)) {
1051 MONO_INST_NEW (cfg, inst, OP_BR);
1052 inst->inst_target_bb = bb->out_bb [0];
1053 MONO_ADD_INS (bb, inst);
1058 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
1060 MonoBasicBlock *bbn, *next;
1064 /* Find the previous */
1065 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
1068 bbn->next_bb = bb->next_bb;
1072 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
1078 if (next && (!bb->last_ins || ((bb->last_ins->opcode != OP_NOT_REACHED) && (bb->last_ins->opcode != OP_BR) && (bb->last_ins->opcode != OP_BR_REG) && (!MONO_IS_COND_BRANCH_OP (bb->last_ins))))) {
1081 MONO_INST_NEW (cfg, ins, OP_BR);
1082 MONO_ADD_INS (bb, ins);
1083 mono_link_bblock (cfg, bb, next);
1084 ins->inst_target_bb = next;
1089 * mono_remove_block:
1091 * Remove BB from the control flow graph
1094 mono_remove_bblock (MonoCompile *cfg, MonoBasicBlock *bb)
1096 MonoBasicBlock *tmp_bb;
1098 for (tmp_bb = cfg->bb_entry; tmp_bb && tmp_bb->next_bb != bb; tmp_bb = tmp_bb->next_bb)
1102 tmp_bb->next_bb = bb->next_bb;
1106 mono_remove_critical_edges (MonoCompile *cfg)
1109 MonoBasicBlock *previous_bb;
1111 if (cfg->verbose_level > 3) {
1112 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1114 printf ("remove_critical_edges, BEFORE BB%d (in:", bb->block_num);
1115 for (i = 0; i < bb->in_count; i++) {
1116 printf (" %d", bb->in_bb [i]->block_num);
1119 for (i = 0; i < bb->out_count; i++) {
1120 printf (" %d", bb->out_bb [i]->block_num);
1123 if (bb->last_ins != NULL) {
1125 mono_print_ins (bb->last_ins);
1131 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
1132 if (bb->in_count > 1) {
1134 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
1135 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
1137 * Have to remove non-critical edges whose source ends with a BR_REG
1138 * ins too, since inserting a computation before the BR_REG could
1139 * overwrite the sreg1 of the ins.
1141 if ((in_bb->out_count > 1) || (in_bb->out_count == 1 && in_bb->last_ins && in_bb->last_ins->opcode == OP_BR_REG)) {
1142 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1143 new_bb->block_num = cfg->num_bblocks++;
1144 // new_bb->real_offset = bb->real_offset;
1145 new_bb->region = bb->region;
1147 /* Do not alter the CFG while altering the BB list */
1148 if (mono_bb_is_fall_through (cfg, previous_bb)) {
1149 if (previous_bb != cfg->bb_entry) {
1151 /* Make sure previous_bb really falls through bb */
1152 for (i = 0; i < previous_bb->out_count; i++) {
1153 if (previous_bb->out_bb [i] == bb) {
1155 MONO_INST_NEW (cfg, jump, OP_BR);
1156 MONO_ADD_INS (previous_bb, jump);
1157 jump->cil_code = previous_bb->cil_code;
1158 jump->inst_target_bb = bb;
1163 /* We cannot add any inst to the entry BB, so we must */
1164 /* put a new BB in the middle to hold the OP_BR */
1166 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1167 new_bb_after_entry->block_num = cfg->num_bblocks++;
1168 // new_bb_after_entry->real_offset = bb->real_offset;
1169 new_bb_after_entry->region = bb->region;
1171 MONO_INST_NEW (cfg, jump, OP_BR);
1172 MONO_ADD_INS (new_bb_after_entry, jump);
1173 jump->cil_code = bb->cil_code;
1174 jump->inst_target_bb = bb;
1176 mono_unlink_bblock (cfg, previous_bb, bb);
1177 mono_link_bblock (cfg, new_bb_after_entry, bb);
1178 mono_link_bblock (cfg, previous_bb, new_bb_after_entry);
1180 previous_bb->next_bb = new_bb_after_entry;
1181 previous_bb = new_bb_after_entry;
1183 if (cfg->verbose_level > 2) {
1184 printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry->block_num, bb->block_num);
1189 /* Insert new_bb in the BB list */
1190 previous_bb->next_bb = new_bb;
1191 new_bb->next_bb = bb;
1192 previous_bb = new_bb;
1194 /* Setup in_bb and out_bb */
1195 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1196 new_bb->in_bb [0] = in_bb;
1197 new_bb->in_count = 1;
1198 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1199 new_bb->out_bb [0] = bb;
1200 new_bb->out_count = 1;
1202 /* Relink in_bb and bb to (from) new_bb */
1203 replace_out_block (in_bb, bb, new_bb);
1204 replace_out_block_in_code (in_bb, bb, new_bb);
1205 replace_in_block (bb, in_bb, new_bb);
1207 if (cfg->verbose_level > 2) {
1208 printf ("remove_critical_edges, removed critical edge from BB%d to BB%d (added BB%d)\n", in_bb->block_num, bb->block_num, new_bb->block_num);
1215 if (cfg->verbose_level > 3) {
1216 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1218 printf ("remove_critical_edges, AFTER BB%d (in:", bb->block_num);
1219 for (i = 0; i < bb->in_count; i++) {
1220 printf (" %d", bb->in_bb [i]->block_num);
1223 for (i = 0; i < bb->out_count; i++) {
1224 printf (" %d", bb->out_bb [i]->block_num);
1227 if (bb->last_ins != NULL) {
1229 mono_print_ins (bb->last_ins);
1237 * Optimizes the branches on the Control Flow Graph
1241 mono_optimize_branches (MonoCompile *cfg)
1243 int i, changed = FALSE;
1244 MonoBasicBlock *bb, *bbn;
1245 guint32 niterations;
1246 MonoInst *bbn_first_inst;
1247 int filter = FILTER_IL_SEQ_POINT;
1250 * Some crazy loops could cause the code below to go into an infinite
1251 * loop, see bug #53003 for an example. To prevent this, we put an upper
1252 * bound on the number of iterations.
1254 if (cfg->num_bblocks > 1000)
1255 niterations = cfg->num_bblocks * 2;
1260 MonoBasicBlock *previous_bb;
1264 /* we skip the entry block (exit is handled specially instead ) */
1265 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
1266 /* dont touch code inside exception clauses */
1267 if (bb->region != -1)
1270 if (!bb->not_useless && remove_block_if_useless (cfg, bb, previous_bb)) {
1275 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1276 if (cfg->verbose_level > 2)
1277 g_print ("nullify block triggered %d\n", bbn->block_num);
1279 bb->next_bb = bbn->next_bb;
1281 for (i = 0; i < bbn->out_count; i++)
1282 replace_in_block (bbn->out_bb [i], bbn, NULL);
1284 mono_nullify_basic_block (bbn);
1288 if (bb->out_count == 1) {
1289 bbn = bb->out_bb [0];
1291 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
1292 if (bb->last_ins && (bb->last_ins->opcode != OP_BR) && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
1293 bb->last_ins->opcode = OP_BR;
1294 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
1296 if (cfg->verbose_level > 2)
1297 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
1300 if (bb->region == bbn->region && bb->next_bb == bbn) {
1301 /* the block are in sequence anyway ... */
1303 /* branches to the following block can be removed */
1304 if (bb->last_ins && bb->last_ins->opcode == OP_BR && !bbn->out_of_line) {
1305 NULLIFY_INS (bb->last_ins);
1307 if (cfg->verbose_level > 2)
1308 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
1311 if (bbn->in_count == 1 && !bb->extended) {
1312 if (bbn != cfg->bb_exit) {
1313 if (cfg->verbose_level > 2)
1314 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
1315 mono_merge_basic_blocks (cfg, bb, bbn);
1320 //mono_print_bb_code (bb);
1325 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1326 if (cfg->verbose_level > 2) {
1327 g_print ("nullify block triggered %d\n", bbn->block_num);
1329 bb->next_bb = bbn->next_bb;
1331 for (i = 0; i < bbn->out_count; i++)
1332 replace_in_block (bbn->out_bb [i], bbn, NULL);
1334 mono_nullify_basic_block (bbn);
1339 if (bb->out_count == 1) {
1340 bbn = bb->out_bb [0];
1342 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
1343 bbn = bb->last_ins->inst_target_bb;
1344 bbn_first_inst = mono_bb_first_inst (bbn, filter);
1345 if (bb->region == bbn->region && bbn_first_inst && bbn_first_inst->opcode == OP_BR &&
1346 bbn_first_inst->inst_target_bb != bbn &&
1347 bbn_first_inst->inst_target_bb->region == bb->region) {
1349 if (cfg->verbose_level > 2)
1350 g_print ("branch to branch triggered %d -> %d -> %d\n", bb->block_num, bbn->block_num, bbn_first_inst->inst_target_bb->block_num);
1352 replace_in_block (bbn, bb, NULL);
1353 replace_out_block (bb, bbn, bbn_first_inst->inst_target_bb);
1354 mono_link_bblock (cfg, bb, bbn_first_inst->inst_target_bb);
1355 bb->last_ins->inst_target_bb = bbn_first_inst->inst_target_bb;
1360 } else if (bb->out_count == 2) {
1361 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1363 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
1365 if (bb->last_ins->flags & MONO_INST_CFOLD_TAKEN)
1366 branch_result = BRANCH_TAKEN;
1367 else if (bb->last_ins->flags & MONO_INST_CFOLD_NOT_TAKEN)
1368 branch_result = BRANCH_NOT_TAKEN;
1370 branch_result = BRANCH_UNDEF;
1372 if (branch_result == BRANCH_TAKEN) {
1373 taken_branch_target = bb->last_ins->inst_true_bb;
1374 untaken_branch_target = bb->last_ins->inst_false_bb;
1375 } else if (branch_result == BRANCH_NOT_TAKEN) {
1376 taken_branch_target = bb->last_ins->inst_false_bb;
1377 untaken_branch_target = bb->last_ins->inst_true_bb;
1379 if (taken_branch_target) {
1380 /* if mono_eval_cond_branch () is ever taken to handle
1381 * non-constant values to compare, issue a pop here.
1383 bb->last_ins->opcode = OP_BR;
1384 bb->last_ins->inst_target_bb = taken_branch_target;
1386 mono_unlink_bblock (cfg, bb, untaken_branch_target);
1390 bbn = bb->last_ins->inst_true_bb;
1391 bbn_first_inst = mono_bb_first_inst (bbn, filter);
1392 if (bb->region == bbn->region && bbn_first_inst && bbn_first_inst->opcode == OP_BR &&
1393 bbn_first_inst->inst_target_bb->region == bb->region) {
1394 if (cfg->verbose_level > 2)
1395 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
1396 bb->block_num, bbn->block_num, bbn_first_inst->inst_target_bb->block_num,
1397 bbn_first_inst->opcode);
1400 * Unlink, then relink bblocks to avoid various
1401 * tricky situations when the two targets of the branch
1402 * are equal, or will become equal after the change.
1404 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1405 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1407 bb->last_ins->inst_true_bb = bbn_first_inst->inst_target_bb;
1409 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1410 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1416 bbn = bb->last_ins->inst_false_bb;
1417 bbn_first_inst = mono_bb_first_inst (bbn, filter);
1418 if (bbn && bb->region == bbn->region && bbn_first_inst && bbn_first_inst->opcode == OP_BR &&
1419 bbn_first_inst->inst_target_bb->region == bb->region) {
1420 if (cfg->verbose_level > 2)
1421 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
1422 bb->block_num, bbn->block_num, bbn_first_inst->inst_target_bb->block_num,
1423 bbn_first_inst->opcode);
1425 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1426 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1428 bb->last_ins->inst_false_bb = bbn_first_inst->inst_target_bb;
1430 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1431 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1437 bbn = bb->last_ins->inst_false_bb;
1439 * If bb is an extended bb, it could contain an inside branch to bbn.
1440 * FIXME: Enable the optimization if that is not true.
1441 * If bblocks_linked () is true, then merging bb and bbn
1442 * would require addition of an extra branch at the end of bbn
1443 * slowing down loops.
1445 if (bbn && bb->region == bbn->region && bbn->in_count == 1 && cfg->enable_extended_bblocks && bbn != cfg->bb_exit && !bb->extended && !bbn->out_of_line && !mono_bblocks_linked (bbn, bb)) {
1446 g_assert (bbn->in_bb [0] == bb);
1447 if (cfg->verbose_level > 2)
1448 g_print ("merge false branch target triggered BB%d -> BB%d\n", bb->block_num, bbn->block_num);
1449 mono_merge_basic_blocks (cfg, bb, bbn);
1455 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1456 if (bb->last_ins->inst_false_bb && bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region) && !cfg->disable_out_of_line_bblocks) {
1457 /* Reverse the branch */
1458 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
1459 bbn = bb->last_ins->inst_false_bb;
1460 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
1461 bb->last_ins->inst_true_bb = bbn;
1463 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
1464 if (cfg->verbose_level > 2)
1465 g_print ("cbranch to throw block triggered %d.\n",
1471 } while (changed && (niterations > 0));
1474 #endif /* DISABLE_JIT */