2 * branch-opts.c: Branch optimizations support
5 * Patrik Torstensson (Patrik.Torstesson at gmail.com)
7 * (C) 2005 Ximian, Inc. http://www.ximian.com
15 * Returns true if @bb is a basic block which falls through the next block.
16 * TODO verify if it helps to check if the bb last ins is a branch to its successor.
19 mono_bb_is_fall_through (MonoCompile *cfg, MonoBasicBlock *bb)
21 return bb->next_bb && bb->next_bb->region == bb->region && /*fall throught between regions is not really interesting or useful*/
22 (bb->last_ins == NULL || !MONO_IS_BRANCH_OP (bb->last_ins)); /*and the last op can't be a branch too*/
26 * Used by the arch code to replace the exception handling
27 * with a direct branch. This is safe to do if the
28 * exception object isn't used, no rethrow statement and
29 * no filter statement (verify).
33 mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname)
35 MonoMethod *method = cfg->method;
36 MonoMethodHeader *header = mono_method_get_header (method);
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);
100 /* Branching to an outer clause could skip inner clauses */
109 static const int int_cmov_opcodes [] = {
122 static const int long_cmov_opcodes [] = {
136 br_to_br_un (int opcode)
152 g_assert_not_reached ();
160 * Replace INS with its decomposition which is stored in a series of bblocks starting
161 * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS.
162 * On return, it will be set to the last ins of the decomposition.
165 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
167 MonoInst *next = ins->next;
169 if (next && next->opcode == OP_NOP) {
170 /* Avoid NOPs following branches */
171 ins->next = next->next;
175 if (first_bb == last_bb) {
177 * Only one replacement bb, merge the code into
181 /* Delete links between the first_bb and its successors */
182 while (first_bb->out_count)
183 mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
187 (*prev)->next = first_bb->code;
188 first_bb->code->prev = (*prev);
190 bb->code = first_bb->code;
194 last_bb->last_ins->next = next;
196 next->prev = last_bb->last_ins;
198 bb->last_ins = last_bb->last_ins;
199 *prev = last_bb->last_ins;
200 bb->has_array_access |= first_bb->has_array_access;
203 MonoBasicBlock **tmp_bblocks, *tmp;
209 for (tmp = first_bb; tmp; tmp = tmp->next_bb)
210 tmp->region = bb->region;
212 /* Split the original bb */
214 ins->next->prev = NULL;
218 /* Merge the second part of the original bb into the last bb */
219 if (last_bb->last_ins) {
220 last_bb->last_ins->next = next;
222 next->prev = last_bb->last_ins;
224 last_bb->code = next;
226 last_bb->has_array_access |= bb->has_array_access;
229 for (last = next; last->next != NULL; last = last->next)
231 last_bb->last_ins = last;
234 for (i = 0; i < bb->out_count; ++i)
235 mono_link_bblock (cfg, last_bb, bb->out_bb [i]);
237 /* Merge the first (dummy) bb to the original bb */
239 (*prev)->next = first_bb->code;
240 first_bb->code->prev = (*prev);
242 bb->code = first_bb->code;
244 bb->last_ins = first_bb->last_ins;
245 bb->has_array_access |= first_bb->has_array_access;
247 /* Delete the links between the original bb and its successors */
248 tmp_bblocks = bb->out_bb;
249 count = bb->out_count;
250 for (i = 0; i < count; ++i)
251 mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
253 /* Add links between the original bb and the first_bb's successors */
254 for (i = 0; i < first_bb->out_count; ++i) {
255 MonoBasicBlock *out_bb = first_bb->out_bb [i];
257 mono_link_bblock (cfg, bb, out_bb);
259 /* Delete links between the first_bb and its successors */
260 for (i = 0; i < bb->out_count; ++i) {
261 MonoBasicBlock *out_bb = bb->out_bb [i];
263 mono_unlink_bblock (cfg, first_bb, out_bb);
265 last_bb->next_bb = bb->next_bb;
266 bb->next_bb = first_bb->next_bb;
273 mono_if_conversion (MonoCompile *cfg)
275 #ifdef MONO_ARCH_HAVE_CMOV_OPS
277 gboolean changed = FALSE;
279 if (!(cfg->opt & MONO_OPT_CMOV))
282 // FIXME: Make this work with extended bblocks
285 * This pass requires somewhat optimized IR code so it should be run after
286 * local cprop/deadce. Also, it should be run before dominator computation, since
287 * it changes control flow.
289 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
290 MonoBasicBlock *bb1, *bb2;
293 /* Look for the IR code generated from cond ? a : b
304 if (!(bb->out_count == 2 && !bb->extended))
307 bb1 = bb->out_bb [0];
308 bb2 = bb->out_bb [1];
310 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]) {
311 MonoInst *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
312 MonoBasicBlock *true_bb, *false_bb;
313 gboolean simple, ret;
317 if (bb->last_ins && (bb->last_ins->opcode == OP_BR_REG || bb->last_ins->opcode == OP_BR))
320 /* Find the compare instruction */
321 if (!bb->last_ins || !bb->last_ins->prev)
323 branch = bb->last_ins;
324 compare = branch->prev;
326 if (!MONO_IS_COND_BRANCH_OP (branch))
327 /* This can happen if a cond branch is optimized away */
330 true_bb = branch->inst_true_bb;
331 false_bb = branch->inst_false_bb;
334 * Check that bb1 and bb2 are 'simple' and both assign to the same
337 /* FIXME: Get rid of the nops earlier */
338 ins1 = true_bb->code;
339 while (ins1 && ins1->opcode == OP_NOP)
341 ins2 = false_bb->code;
342 while (ins2 && ins2->opcode == OP_NOP)
344 if (!(ins1 && ins2 && ins1->dreg == ins2->dreg && ins1->dreg != -1))
348 for (tmp = ins1->next; tmp; tmp = tmp->next)
349 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
352 for (tmp = ins2->next; tmp; tmp = tmp->next)
353 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
359 /* We move ins1/ins2 before the compare so they should have no side effect */
360 if (!(MONO_INS_HAS_NO_SIDE_EFFECT (ins1) && MONO_INS_HAS_NO_SIDE_EFFECT (ins2)))
363 /* Moving ins1/ins2 could change the comparison */
365 if (!((compare->sreg1 != ins1->dreg) && (compare->sreg2 != ins1->dreg)))
369 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
370 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
374 /* ins->type might not be set */
375 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
378 if (cfg->verbose_level > 2) {
379 printf ("\tBranch -> CMove optimization in BB%d on\n", bb->block_num);
380 printf ("\t\t"); mono_print_ins (compare);
381 printf ("\t\t"); mono_print_ins (compare->next);
382 printf ("\t\t"); mono_print_ins (ins1);
383 printf ("\t\t"); mono_print_ins (ins2);
390 /* Assignments to the return register must remain at the end of bbs */
392 ret = ins1->dreg == cfg->ret->dreg;
396 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
399 /* Rewrite ins1 to emit to tmp_reg */
400 ins1->dreg = tmp_reg;
403 dreg = mono_alloc_dreg (cfg, STACK_I4);
407 /* Remove ins1/ins2 from bb1/bb2 */
408 MONO_REMOVE_INS (true_bb, ins1);
409 MONO_REMOVE_INS (false_bb, ins2);
411 /* Move ins1 and ins2 before the comparison */
412 /* ins1 comes first to avoid ins1 overwriting an argument of ins2 */
413 mono_bblock_insert_before_ins (bb, compare, ins2);
414 mono_bblock_insert_before_ins (bb, ins2, ins1);
416 /* Add cmov instruction */
417 MONO_INST_NEW (cfg, cmov, OP_NOP);
420 cmov->sreg2 = tmp_reg;
421 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
423 cmov->opcode = int_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
426 cmov->opcode = long_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
429 g_assert_not_reached ();
431 mono_bblock_insert_after_ins (bb, compare, cmov);
434 /* Add an extra move */
435 MONO_INST_NEW (cfg, move, OP_MOVE);
436 move->dreg = cfg->ret->dreg;
438 mono_bblock_insert_after_ins (bb, cmov, move);
441 /* Rewrite the branch */
442 branch->opcode = OP_BR;
443 branch->inst_target_bb = true_bb->out_bb [0];
444 mono_link_bblock (cfg, bb, branch->inst_target_bb);
446 /* Reorder bblocks */
447 mono_unlink_bblock (cfg, bb, true_bb);
448 mono_unlink_bblock (cfg, bb, false_bb);
449 mono_unlink_bblock (cfg, true_bb, true_bb->out_bb [0]);
450 mono_unlink_bblock (cfg, false_bb, false_bb->out_bb [0]);
451 mono_remove_bblock (cfg, true_bb);
452 mono_remove_bblock (cfg, false_bb);
454 /* Merge bb and its successor if possible */
455 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
456 (bb->region == bb->out_bb [0]->region)) {
457 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
462 /* Look for the IR code generated from if (cond) <var> <- <a>
471 if ((bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) ||
472 (bb1->in_count == 1 && bb1->out_count == 1 && bb1->out_bb [0] == bb2)) {
473 MonoInst *compare, *branch, *ins1, *cmov, *tmp;
478 MonoBasicBlock *next_bb, *code_bb;
480 /* code_bb is the bblock containing code, next_bb is the successor bblock */
481 if (bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) {
489 ins1 = code_bb->code;
494 /* Check that code_bb is simple */
496 for (tmp = ins1->next; tmp; tmp = tmp->next)
497 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
503 /* We move ins1 before the compare so it should have no side effect */
504 if (!MONO_INS_HAS_NO_SIDE_EFFECT (ins1))
507 if (bb->last_ins && bb->last_ins->opcode == OP_BR_REG)
510 /* Find the compare instruction */
512 if (!bb->last_ins || !bb->last_ins->prev)
514 branch = bb->last_ins;
515 compare = branch->prev;
517 if (!MONO_IS_COND_BRANCH_OP (branch))
518 /* This can happen if a cond branch is optimized away */
522 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
523 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
527 /* ins->type might not be set */
528 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
532 if (cfg->ret && ins1->dreg == cfg->ret->dreg)
535 if (cfg->verbose_level > 2) {
536 printf ("\tBranch -> CMove optimization (2) in BB%d on\n", bb->block_num);
537 printf ("\t\t"); mono_print_ins (compare);
538 printf ("\t\t"); mono_print_ins (compare->next);
539 printf ("\t\t"); mono_print_ins (ins1);
546 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
549 /* Rewrite ins1 to emit to tmp_reg */
550 ins1->dreg = tmp_reg;
552 /* Remove ins1 from code_bb */
553 MONO_REMOVE_INS (code_bb, ins1);
555 /* Move ins1 before the comparison */
556 mono_bblock_insert_before_ins (bb, compare, ins1);
558 /* Add cmov instruction */
559 MONO_INST_NEW (cfg, cmov, OP_NOP);
562 cmov->sreg2 = tmp_reg;
563 cond = mono_opcode_to_cond (branch->opcode);
564 if (branch->inst_false_bb == code_bb)
565 cond = mono_negate_cond (cond);
566 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
568 cmov->opcode = int_cmov_opcodes [cond];
571 cmov->opcode = long_cmov_opcodes [cond];
574 g_assert_not_reached ();
576 mono_bblock_insert_after_ins (bb, compare, cmov);
578 /* Rewrite the branch */
579 branch->opcode = OP_BR;
580 branch->inst_target_bb = next_bb;
581 mono_link_bblock (cfg, bb, branch->inst_target_bb);
583 /* Nullify the branch at the end of code_bb */
585 branch = code_bb->code;
586 MONO_DELETE_INS (code_bb, branch);
589 /* Reorder bblocks */
590 mono_unlink_bblock (cfg, bb, code_bb);
591 mono_unlink_bblock (cfg, code_bb, next_bb);
593 /* Merge bb and its successor if possible */
594 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
595 (bb->region == bb->out_bb [0]->region)) {
596 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
599 * bbn might have fallen through to the next bb without a branch,
600 * have to add one now (#474718).
601 * FIXME: Maybe need to do this more generally in
602 * merge_basic_blocks () ?
604 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) && bb->out_count) {
605 MONO_INST_NEW (cfg, ins1, OP_BR);
606 ins1->inst_target_bb = bb->out_bb [0];
607 MONO_ADD_INS (bb, ins1);
615 * Optimize checks like: if (v < 0 || v > limit) by changing then to unsigned
616 * compares. This isn't really if conversion, but it easier to do here than in
617 * optimize_branches () since the IR is already optimized.
619 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
620 MonoBasicBlock *bb1, *bb2, *true_bb, *false_bb, *next_bb;
621 MonoInst *branch1, *branch2, *compare1, *ins;
623 /* Look for the IR code generated from if (<var> < 0 || v > <limit>)
624 * after branch opts which is:
629 * icompare_imm R [<limit>]
632 if (!(bb->out_count == 2 && !bb->extended))
635 bb1 = bb->out_bb [0];
636 bb2 = bb->out_bb [1];
638 // FIXME: Add more cases
640 /* Check structure */
641 if (!(bb1->in_count == 2 && bb1->in_bb [0] == bb && bb1->in_bb [1] == bb2 && bb2->in_count == 1 && bb2->out_count == 2))
646 /* Check first branch */
647 branch1 = bb->last_ins;
648 if (!(branch1 && ((branch1->opcode == OP_IBLT) || (branch1->opcode == OP_LBLT)) && (branch1->inst_false_bb == next_bb)))
651 true_bb = branch1->inst_true_bb;
653 /* Check second branch */
654 branch2 = next_bb->last_ins;
658 /* mcs sometimes generates inverted branches */
659 if (((branch2->opcode == OP_IBGT) || (branch2->opcode == OP_LBGT)) && branch2->inst_true_bb == branch1->inst_true_bb)
660 false_bb = branch2->inst_false_bb;
661 else if (((branch2->opcode == OP_IBLE) || (branch2->opcode == OP_LBLE)) && branch2->inst_false_bb == branch1->inst_true_bb)
662 false_bb = branch2->inst_true_bb;
666 /* Check first compare */
667 compare1 = bb->last_ins->prev;
668 if (!(compare1 && ((compare1->opcode == OP_ICOMPARE_IMM) || (compare1->opcode == OP_LCOMPARE_IMM)) && compare1->inst_imm == 0))
671 /* Check second bblock */
675 if (((ins->opcode == OP_ICOMPARE_IMM) || (ins->opcode == OP_LCOMPARE_IMM)) && ins->sreg1 == compare1->sreg1 && ins->next == branch2) {
676 /* The second arg must be positive */
677 if (ins->inst_imm < 0)
679 } else if (((ins->opcode == OP_LDLEN) || (ins->opcode == OP_STRLEN)) && ins->dreg != compare1->sreg1 && ins->next && ins->next->opcode == OP_ICOMPARE && ins->next->sreg1 == compare1->sreg1 && ins->next->sreg2 == ins->dreg && ins->next->next == branch2) {
680 /* Another common case: if (index < 0 || index > arr.Length) */
685 if (cfg->verbose_level > 2) {
686 printf ("\tSigned->unsigned compare optimization in BB%d on\n", bb->block_num);
687 printf ("\t\t"); mono_print_ins (compare1);
688 printf ("\t\t"); mono_print_ins (compare1->next);
689 printf ("\t\t"); mono_print_ins (ins);
692 /* Rewrite the first compare+branch */
693 MONO_DELETE_INS (bb, compare1);
694 branch1->opcode = OP_BR;
695 mono_unlink_bblock (cfg, bb, branch1->inst_true_bb);
696 mono_unlink_bblock (cfg, bb, branch1->inst_false_bb);
697 branch1->inst_target_bb = next_bb;
698 mono_link_bblock (cfg, bb, next_bb);
700 /* Rewrite the second branch */
701 branch2->opcode = br_to_br_un (branch2->opcode);
703 mono_merge_basic_blocks (cfg, bb, next_bb);
707 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
708 MonoBasicBlock *bb1, *bb2;
709 MonoInst *prev, *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
710 gboolean simple, ret;
714 /* Look for the IR code generated from if (cond) <var> <- <a>
715 * after branch opts which is:
722 if (!(bb->out_count == 1 && bb->extended && bb->code && bb->code->next && bb->code->next->next))
725 mono_print_bb (bb, "");
727 /* Find the compare instruction */
731 while (compare->next->next && compare->next->next != bb->last_ins) {
733 compare = compare->next;
735 branch = compare->next;
736 if (!MONO_IS_COND_BRANCH_OP (branch))
742 if (cfg->opt & MONO_OPT_BRANCH)
743 mono_optimize_branches (cfg);
744 /* Merging bblocks could make some variables local */
745 mono_handle_global_vregs (cfg);
746 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
747 mono_local_cprop (cfg);
748 mono_local_deadce (cfg);
754 mono_nullify_basic_block (MonoBasicBlock *bb)
761 bb->code = bb->last_ins = NULL;
766 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
770 for (i = 0; i < bb->out_count; i++) {
771 MonoBasicBlock *ob = bb->out_bb [i];
774 if (bb->out_count > 1) {
775 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
779 bb->out_bb [i] = repl;
786 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
790 for (i = 0; i < bb->in_count; i++) {
791 MonoBasicBlock *ib = bb->in_bb [i];
794 if (bb->in_count > 1) {
795 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
799 bb->in_bb [i] = repl;
806 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
809 for (ins = bb->code; ins != NULL; ins = ins->next) {
810 switch (ins->opcode) {
812 if (ins->inst_target_bb == orig)
813 ins->inst_target_bb = repl;
815 case OP_CALL_HANDLER:
816 if (ins->inst_target_bb == orig)
817 ins->inst_target_bb = repl;
821 int n = GPOINTER_TO_INT (ins->klass);
822 for (i = 0; i < n; i++ ) {
823 if (ins->inst_many_bb [i] == orig)
824 ins->inst_many_bb [i] = repl;
829 if (MONO_IS_COND_BRANCH_OP (ins)) {
830 if (ins->inst_true_bb == orig)
831 ins->inst_true_bb = repl;
832 if (ins->inst_false_bb == orig)
833 ins->inst_false_bb = repl;
834 } else if (MONO_IS_JUMP_TABLE (ins)) {
836 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
837 for (i = 0; i < table->table_size; i++ ) {
838 if (table->table [i] == orig)
839 table->table [i] = repl;
849 * Check if a bb is useless (is just made of NOPs and ends with an
850 * unconditional branch, or nothing).
851 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
852 * Otherwise, return FALSE;
855 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
856 MonoBasicBlock *target_bb = NULL;
859 /* Do not touch handlers */
860 if (bb->region != -1) {
861 bb->not_useless = TRUE;
865 MONO_BB_FOR_EACH_INS (bb, inst) {
866 switch (inst->opcode) {
870 target_bb = inst->inst_target_bb;
873 bb->not_useless = TRUE;
878 if (target_bb == NULL) {
879 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
880 target_bb = bb->next_bb;
882 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
887 /* Do not touch BBs following a switch (they are the "default" branch) */
888 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == OP_SWITCH)) {
892 /* Do not touch BBs following the entry BB and jumping to something that is not */
893 /* thiry "next" bb (the entry BB cannot contain the branch) */
894 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
899 * Do not touch BBs following a try block as the code in
900 * mini_method_compile needs them to compute the length of the try block.
902 if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
905 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
906 if ((target_bb != NULL) && (target_bb != bb)) {
909 if (cfg->verbose_level > 1) {
910 printf ("remove_block_if_useless, removed BB%d\n", bb->block_num);
913 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
914 while (bb->in_count) {
915 MonoBasicBlock *in_bb = bb->in_bb [0];
916 mono_unlink_bblock (cfg, in_bb, bb);
917 mono_link_bblock (cfg, in_bb, target_bb);
918 replace_out_block_in_code (in_bb, bb, target_bb);
921 mono_unlink_bblock (cfg, bb, target_bb);
922 if (previous_bb != cfg->bb_entry && mono_bb_is_fall_through (cfg, previous_bb)) {
923 for (i = 0; i < previous_bb->out_count; i++) {
924 if (previous_bb->out_bb [i] == target_bb) {
926 MONO_INST_NEW (cfg, jump, OP_BR);
927 MONO_ADD_INS (previous_bb, jump);
928 jump->cil_code = previous_bb->cil_code;
929 jump->inst_target_bb = target_bb;
935 previous_bb->next_bb = bb->next_bb;
936 mono_nullify_basic_block (bb);
945 mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn)
948 MonoBasicBlock *prev_bb;
951 bb->has_array_access |= bbn->has_array_access;
952 bb->extended |= bbn->extended;
954 mono_unlink_bblock (cfg, bb, bbn);
955 for (i = 0; i < bbn->out_count; ++i)
956 mono_link_bblock (cfg, bb, bbn->out_bb [i]);
957 while (bbn->out_count)
958 mono_unlink_bblock (cfg, bbn, bbn->out_bb [0]);
960 /* Handle the branch at the end of the bb */
961 if (bb->has_call_handler) {
962 for (inst = bb->code; inst != NULL; inst = inst->next) {
963 if (inst->opcode == OP_CALL_HANDLER) {
964 g_assert (inst->inst_target_bb == bbn);
969 if (bb->has_jump_table) {
970 for (inst = bb->code; inst != NULL; inst = inst->next) {
971 if (MONO_IS_JUMP_TABLE (inst)) {
973 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (inst);
974 for (i = 0; i < table->table_size; i++ ) {
975 /* Might be already NULL from a previous merge */
976 if (table->table [i])
977 g_assert (table->table [i] == bbn);
978 table->table [i] = NULL;
980 /* Can't nullify this as later instructions depend on it */
984 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
985 g_assert (bb->last_ins->inst_false_bb == bbn);
986 bb->last_ins->inst_false_bb = NULL;
988 } else if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
989 NULLIFY_INS (bb->last_ins);
992 bb->has_call_handler |= bbn->has_call_handler;
993 bb->has_jump_table |= bbn->has_jump_table;
997 bb->last_ins->next = bbn->code;
998 bbn->code->prev = bb->last_ins;
999 bb->last_ins = bbn->last_ins;
1002 bb->code = bbn->code;
1003 bb->last_ins = bbn->last_ins;
1006 for (prev_bb = cfg->bb_entry; prev_bb && prev_bb->next_bb != bbn; prev_bb = prev_bb->next_bb)
1009 prev_bb->next_bb = bbn->next_bb;
1011 /* bbn might not be in the bb list yet */
1012 if (bb->next_bb == bbn)
1013 bb->next_bb = bbn->next_bb;
1015 mono_nullify_basic_block (bbn);
1018 * If bbn fell through to its next bblock, have to add a branch, since bb
1019 * will not fall though to the same bblock (#513931).
1021 if (bb->last_ins && bb->out_count == 1 && bb->out_bb [0] != bb->next_bb && !MONO_IS_BRANCH_OP (bb->last_ins)) {
1022 MONO_INST_NEW (cfg, inst, OP_BR);
1023 inst->inst_target_bb = bb->out_bb [0];
1024 MONO_ADD_INS (bb, inst);
1029 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
1031 MonoBasicBlock *bbn, *next;
1035 /* Find the previous */
1036 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
1039 bbn->next_bb = bb->next_bb;
1043 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
1049 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))))) {
1052 MONO_INST_NEW (cfg, ins, OP_BR);
1053 MONO_ADD_INS (bb, ins);
1054 mono_link_bblock (cfg, bb, next);
1055 ins->inst_target_bb = next;
1060 * mono_remove_block:
1062 * Remove BB from the control flow graph
1065 mono_remove_bblock (MonoCompile *cfg, MonoBasicBlock *bb)
1067 MonoBasicBlock *tmp_bb;
1069 for (tmp_bb = cfg->bb_entry; tmp_bb && tmp_bb->next_bb != bb; tmp_bb = tmp_bb->next_bb)
1073 tmp_bb->next_bb = bb->next_bb;
1077 mono_remove_critical_edges (MonoCompile *cfg)
1080 MonoBasicBlock *previous_bb;
1082 if (cfg->verbose_level > 3) {
1083 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1085 printf ("remove_critical_edges, BEFORE BB%d (in:", bb->block_num);
1086 for (i = 0; i < bb->in_count; i++) {
1087 printf (" %d", bb->in_bb [i]->block_num);
1090 for (i = 0; i < bb->out_count; i++) {
1091 printf (" %d", bb->out_bb [i]->block_num);
1094 if (bb->last_ins != NULL) {
1096 mono_print_ins (bb->last_ins);
1102 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
1103 if (bb->in_count > 1) {
1105 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
1106 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
1108 * Have to remove non-critical edges whose source ends with a BR_REG
1109 * ins too, since inserting a computation before the BR_REG could
1110 * overwrite the sreg1 of the ins.
1112 if ((in_bb->out_count > 1) || (in_bb->out_count == 1 && in_bb->last_ins && in_bb->last_ins->opcode == OP_BR_REG)) {
1113 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1114 new_bb->block_num = cfg->num_bblocks++;
1115 // new_bb->real_offset = bb->real_offset;
1116 new_bb->region = bb->region;
1118 /* Do not alter the CFG while altering the BB list */
1119 if (mono_bb_is_fall_through (cfg, previous_bb)) {
1120 if (previous_bb != cfg->bb_entry) {
1122 /* Make sure previous_bb really falls through bb */
1123 for (i = 0; i < previous_bb->out_count; i++) {
1124 if (previous_bb->out_bb [i] == bb) {
1126 MONO_INST_NEW (cfg, jump, OP_BR);
1127 MONO_ADD_INS (previous_bb, jump);
1128 jump->cil_code = previous_bb->cil_code;
1129 jump->inst_target_bb = bb;
1134 /* We cannot add any inst to the entry BB, so we must */
1135 /* put a new BB in the middle to hold the OP_BR */
1137 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1138 new_bb_after_entry->block_num = cfg->num_bblocks++;
1139 // new_bb_after_entry->real_offset = bb->real_offset;
1140 new_bb_after_entry->region = bb->region;
1142 MONO_INST_NEW (cfg, jump, OP_BR);
1143 MONO_ADD_INS (new_bb_after_entry, jump);
1144 jump->cil_code = bb->cil_code;
1145 jump->inst_target_bb = bb;
1147 mono_unlink_bblock (cfg, previous_bb, bb);
1148 mono_link_bblock (cfg, new_bb_after_entry, bb);
1149 mono_link_bblock (cfg, previous_bb, new_bb_after_entry);
1151 previous_bb->next_bb = new_bb_after_entry;
1152 previous_bb = new_bb_after_entry;
1154 if (cfg->verbose_level > 2) {
1155 printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry->block_num, bb->block_num);
1160 /* Insert new_bb in the BB list */
1161 previous_bb->next_bb = new_bb;
1162 new_bb->next_bb = bb;
1163 previous_bb = new_bb;
1165 /* Setup in_bb and out_bb */
1166 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1167 new_bb->in_bb [0] = in_bb;
1168 new_bb->in_count = 1;
1169 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1170 new_bb->out_bb [0] = bb;
1171 new_bb->out_count = 1;
1173 /* Relink in_bb and bb to (from) new_bb */
1174 replace_out_block (in_bb, bb, new_bb);
1175 replace_out_block_in_code (in_bb, bb, new_bb);
1176 replace_in_block (bb, in_bb, new_bb);
1178 if (cfg->verbose_level > 2) {
1179 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);
1186 if (cfg->verbose_level > 3) {
1187 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1189 printf ("remove_critical_edges, AFTER BB%d (in:", bb->block_num);
1190 for (i = 0; i < bb->in_count; i++) {
1191 printf (" %d", bb->in_bb [i]->block_num);
1194 for (i = 0; i < bb->out_count; i++) {
1195 printf (" %d", bb->out_bb [i]->block_num);
1198 if (bb->last_ins != NULL) {
1200 mono_print_ins (bb->last_ins);
1208 * Optimizes the branches on the Control Flow Graph
1212 mono_optimize_branches (MonoCompile *cfg)
1214 int i, changed = FALSE;
1215 MonoBasicBlock *bb, *bbn;
1216 guint32 niterations;
1219 * Some crazy loops could cause the code below to go into an infinite
1220 * loop, see bug #53003 for an example. To prevent this, we put an upper
1221 * bound on the number of iterations.
1223 if (cfg->num_bblocks > 1000)
1224 niterations = cfg->num_bblocks * 2;
1229 MonoBasicBlock *previous_bb;
1233 /* we skip the entry block (exit is handled specially instead ) */
1234 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
1235 /* dont touch code inside exception clauses */
1236 if (bb->region != -1)
1239 if (!bb->not_useless && remove_block_if_useless (cfg, bb, previous_bb)) {
1244 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1245 if (cfg->verbose_level > 2)
1246 g_print ("nullify block triggered %d\n", bbn->block_num);
1248 bb->next_bb = bbn->next_bb;
1250 for (i = 0; i < bbn->out_count; i++)
1251 replace_in_block (bbn->out_bb [i], bbn, NULL);
1253 mono_nullify_basic_block (bbn);
1257 if (bb->out_count == 1) {
1258 bbn = bb->out_bb [0];
1260 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
1261 if (bb->last_ins && (bb->last_ins->opcode != OP_BR) && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
1262 bb->last_ins->opcode = OP_BR;
1263 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
1265 if (cfg->verbose_level > 2)
1266 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
1269 if (bb->region == bbn->region && bb->next_bb == bbn) {
1270 /* the block are in sequence anyway ... */
1272 /* branches to the following block can be removed */
1273 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
1274 bb->last_ins->opcode = OP_NOP;
1276 if (cfg->verbose_level > 2)
1277 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
1280 if (bbn->in_count == 1 && !bb->extended) {
1281 if (bbn != cfg->bb_exit) {
1282 if (cfg->verbose_level > 2)
1283 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
1284 mono_merge_basic_blocks (cfg, bb, bbn);
1289 //mono_print_bb_code (bb);
1294 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1295 if (cfg->verbose_level > 2) {
1296 g_print ("nullify block triggered %d\n", bbn->block_num);
1298 bb->next_bb = bbn->next_bb;
1300 for (i = 0; i < bbn->out_count; i++)
1301 replace_in_block (bbn->out_bb [i], bbn, NULL);
1303 mono_nullify_basic_block (bbn);
1308 if (bb->out_count == 1) {
1309 bbn = bb->out_bb [0];
1311 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
1312 bbn = bb->last_ins->inst_target_bb;
1313 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1314 bbn->code->inst_target_bb != bbn &&
1315 bbn->code->inst_target_bb->region == bb->region) {
1317 if (cfg->verbose_level > 2)
1318 g_print ("branch to branch triggered %d -> %d -> %d\n", bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
1320 replace_in_block (bbn, bb, NULL);
1321 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
1322 mono_link_bblock (cfg, bb, bbn->code->inst_target_bb);
1323 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
1328 } else if (bb->out_count == 2) {
1329 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1331 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
1333 if (bb->last_ins->flags & MONO_INST_CFOLD_TAKEN)
1334 branch_result = BRANCH_TAKEN;
1335 else if (bb->last_ins->flags & MONO_INST_CFOLD_NOT_TAKEN)
1336 branch_result = BRANCH_NOT_TAKEN;
1338 branch_result = BRANCH_UNDEF;
1340 if (branch_result == BRANCH_TAKEN) {
1341 taken_branch_target = bb->last_ins->inst_true_bb;
1342 untaken_branch_target = bb->last_ins->inst_false_bb;
1343 } else if (branch_result == BRANCH_NOT_TAKEN) {
1344 taken_branch_target = bb->last_ins->inst_false_bb;
1345 untaken_branch_target = bb->last_ins->inst_true_bb;
1347 if (taken_branch_target) {
1348 /* if mono_eval_cond_branch () is ever taken to handle
1349 * non-constant values to compare, issue a pop here.
1351 bb->last_ins->opcode = OP_BR;
1352 bb->last_ins->inst_target_bb = taken_branch_target;
1354 mono_unlink_bblock (cfg, bb, untaken_branch_target);
1358 bbn = bb->last_ins->inst_true_bb;
1359 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1360 bbn->code->inst_target_bb->region == bb->region) {
1361 if (cfg->verbose_level > 2)
1362 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
1363 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
1367 * Unlink, then relink bblocks to avoid various
1368 * tricky situations when the two targets of the branch
1369 * are equal, or will become equal after the change.
1371 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1372 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1374 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
1376 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1377 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1383 bbn = bb->last_ins->inst_false_bb;
1384 if (bbn && bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1385 bbn->code->inst_target_bb->region == bb->region) {
1386 if (cfg->verbose_level > 2)
1387 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
1388 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
1391 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1392 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1394 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
1396 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1397 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1403 bbn = bb->last_ins->inst_false_bb;
1405 * If bb is an extended bb, it could contain an inside branch to bbn.
1406 * FIXME: Enable the optimization if that is not true.
1407 * If bblocks_linked () is true, then merging bb and bbn
1408 * would require addition of an extra branch at the end of bbn
1409 * slowing down loops.
1411 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)) {
1412 g_assert (bbn->in_bb [0] == bb);
1413 if (cfg->verbose_level > 2)
1414 g_print ("merge false branch target triggered BB%d -> BB%d\n", bb->block_num, bbn->block_num);
1415 mono_merge_basic_blocks (cfg, bb, bbn);
1421 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1422 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)) {
1423 /* Reverse the branch */
1424 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
1425 bbn = bb->last_ins->inst_false_bb;
1426 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
1427 bb->last_ins->inst_true_bb = bbn;
1429 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
1430 if (cfg->verbose_level > 2)
1431 g_print ("cbranch to throw block triggered %d.\n",
1437 } while (changed && (niterations > 0));
1440 #endif /* DISABLE_JIT */