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 MonoMethodHeader
*header
= cfg
->header
;
36 MonoExceptionClause
*clause
;
40 if (!(cfg
->opt
& MONO_OPT_EXCEPTION
))
43 if (bb
->region
== -1 || !MONO_BBLOCK_IS_IN_REGION (bb
, MONO_REGION_TRY
))
46 exclass
= mono_class_from_name (mono_get_corlib (), "System", exname
);
47 /* search for the handler */
48 for (i
= 0; i
< header
->num_clauses
; ++i
) {
49 clause
= &header
->clauses
[i
];
50 if (MONO_OFFSET_IN_CLAUSE (clause
, bb
->real_offset
)) {
51 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
&& clause
->data
.catch_class
&& mono_class_is_assignable_from (clause
->data
.catch_class
, exclass
)) {
54 /* get the basic block for the handler and
55 * check if the exception object is used.
56 * Flag is set during method_to_ir due to
57 * pop-op is optmized away in codegen (burg).
59 tbb
= cfg
->cil_offset_to_bb
[clause
->handler_offset
];
60 if (tbb
&& tbb
->flags
& BB_EXCEPTION_DEAD_OBJ
&& !(tbb
->flags
& BB_EXCEPTION_UNSAFE
)) {
61 MonoBasicBlock
*targetbb
= tbb
;
62 gboolean unsafe
= FALSE
;
64 /* Check if this catch clause is ok to optimize by
65 * looking for the BB_EXCEPTION_UNSAFE in every BB that
66 * belongs to the same region.
68 * UNSAFE flag is set during method_to_ir (OP_RETHROW)
70 while (!unsafe
&& tbb
->next_bb
&& tbb
->region
== tbb
->next_bb
->region
) {
71 if (tbb
->next_bb
->flags
& BB_EXCEPTION_UNSAFE
) {
81 /* Create dummy inst to allow easier integration in
82 * arch dependent code (opcode ignored)
84 MONO_INST_NEW (cfg
, jump
, OP_BR
);
86 /* Allocate memory for our branch target */
87 jump
->inst_i1
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
));
88 jump
->inst_true_bb
= targetbb
;
90 if (cfg
->verbose_level
> 2)
91 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
);
98 /* Branching to an outer clause could skip inner clauses */
102 /* Branching to an outer clause could skip inner clauses */
111 static const int int_cmov_opcodes
[] = {
124 static const int long_cmov_opcodes
[] = {
138 br_to_br_un (int opcode
)
154 g_assert_not_reached ();
162 * Replace INS with its decomposition which is stored in a series of bblocks starting
163 * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS.
164 * On return, it will be set to the last ins of the decomposition.
167 mono_replace_ins (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
**prev
, MonoBasicBlock
*first_bb
, MonoBasicBlock
*last_bb
)
169 MonoInst
*next
= ins
->next
;
171 if (next
&& next
->opcode
== OP_NOP
) {
172 /* Avoid NOPs following branches */
173 ins
->next
= next
->next
;
177 if (first_bb
== last_bb
) {
179 * Only one replacement bb, merge the code into
183 /* Delete links between the first_bb and its successors */
184 while (first_bb
->out_count
)
185 mono_unlink_bblock (cfg
, first_bb
, first_bb
->out_bb
[0]);
189 (*prev
)->next
= first_bb
->code
;
190 first_bb
->code
->prev
= (*prev
);
192 bb
->code
= first_bb
->code
;
196 last_bb
->last_ins
->next
= next
;
198 next
->prev
= last_bb
->last_ins
;
200 bb
->last_ins
= last_bb
->last_ins
;
201 *prev
= last_bb
->last_ins
;
202 bb
->has_array_access
|= first_bb
->has_array_access
;
205 MonoBasicBlock
**tmp_bblocks
, *tmp
;
211 for (tmp
= first_bb
; tmp
; tmp
= tmp
->next_bb
)
212 tmp
->region
= bb
->region
;
214 /* Split the original bb */
216 ins
->next
->prev
= NULL
;
220 /* Merge the second part of the original bb into the last bb */
221 if (last_bb
->last_ins
) {
222 last_bb
->last_ins
->next
= next
;
224 next
->prev
= last_bb
->last_ins
;
226 last_bb
->code
= next
;
228 last_bb
->has_array_access
|= bb
->has_array_access
;
231 for (last
= next
; last
->next
!= NULL
; last
= last
->next
)
233 last_bb
->last_ins
= last
;
236 for (i
= 0; i
< bb
->out_count
; ++i
)
237 mono_link_bblock (cfg
, last_bb
, bb
->out_bb
[i
]);
239 /* Merge the first (dummy) bb to the original bb */
241 (*prev
)->next
= first_bb
->code
;
242 first_bb
->code
->prev
= (*prev
);
244 bb
->code
= first_bb
->code
;
246 bb
->last_ins
= first_bb
->last_ins
;
247 bb
->has_array_access
|= first_bb
->has_array_access
;
249 /* Delete the links between the original bb and its successors */
250 tmp_bblocks
= bb
->out_bb
;
251 count
= bb
->out_count
;
252 for (i
= 0; i
< count
; ++i
)
253 mono_unlink_bblock (cfg
, bb
, tmp_bblocks
[i
]);
255 /* Add links between the original bb and the first_bb's successors */
256 for (i
= 0; i
< first_bb
->out_count
; ++i
) {
257 MonoBasicBlock
*out_bb
= first_bb
->out_bb
[i
];
259 mono_link_bblock (cfg
, bb
, out_bb
);
261 /* Delete links between the first_bb and its successors */
262 for (i
= 0; i
< bb
->out_count
; ++i
) {
263 MonoBasicBlock
*out_bb
= bb
->out_bb
[i
];
265 mono_unlink_bblock (cfg
, first_bb
, out_bb
);
267 last_bb
->next_bb
= bb
->next_bb
;
268 bb
->next_bb
= first_bb
->next_bb
;
275 mono_if_conversion (MonoCompile
*cfg
)
277 #ifdef MONO_ARCH_HAVE_CMOV_OPS
279 gboolean changed
= FALSE
;
281 if (!(cfg
->opt
& MONO_OPT_CMOV
))
284 // FIXME: Make this work with extended bblocks
287 * This pass requires somewhat optimized IR code so it should be run after
288 * local cprop/deadce. Also, it should be run before dominator computation, since
289 * it changes control flow.
291 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
292 MonoBasicBlock
*bb1
, *bb2
;
295 /* Look for the IR code generated from cond ? a : b
306 if (!(bb
->out_count
== 2 && !bb
->extended
))
309 bb1
= bb
->out_bb
[0];
310 bb2
= bb
->out_bb
[1];
312 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]) {
313 MonoInst
*compare
, *branch
, *ins1
, *ins2
, *cmov
, *move
, *tmp
;
314 MonoBasicBlock
*true_bb
, *false_bb
;
315 gboolean simple
, ret
;
319 if (bb
->last_ins
&& (bb
->last_ins
->opcode
== OP_BR_REG
|| bb
->last_ins
->opcode
== OP_BR
))
322 /* Find the compare instruction */
323 if (!bb
->last_ins
|| !bb
->last_ins
->prev
)
325 branch
= bb
->last_ins
;
326 compare
= branch
->prev
;
328 if (!MONO_IS_COND_BRANCH_OP (branch
))
329 /* This can happen if a cond branch is optimized away */
332 true_bb
= branch
->inst_true_bb
;
333 false_bb
= branch
->inst_false_bb
;
336 * Check that bb1 and bb2 are 'simple' and both assign to the same
339 /* FIXME: Get rid of the nops earlier */
340 ins1
= true_bb
->code
;
341 while (ins1
&& ins1
->opcode
== OP_NOP
)
343 ins2
= false_bb
->code
;
344 while (ins2
&& ins2
->opcode
== OP_NOP
)
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_BR
)))
354 for (tmp
= ins2
->next
; tmp
; tmp
= tmp
->next
)
355 if (!((tmp
->opcode
== OP_NOP
) || (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 (compare
->next
);
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
= code_bb
->code
;
496 /* Check that code_bb is simple */
498 for (tmp
= ins1
->next
; tmp
; tmp
= tmp
->next
)
499 if (!((tmp
->opcode
== OP_NOP
) || (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 if (bb
->last_ins
&& bb
->last_ins
->opcode
== OP_BR_REG
)
512 /* Find the compare instruction */
514 if (!bb
->last_ins
|| !bb
->last_ins
->prev
)
516 branch
= bb
->last_ins
;
517 compare
= branch
->prev
;
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 (compare
->next
);
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
;
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
= bb
->last_ins
;
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
= next_bb
->last_ins
;
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
= bb
->last_ins
->prev
;
677 if (!(compare1
&& ((compare1
->opcode
== OP_ICOMPARE_IMM
) || (compare1
->opcode
== OP_LCOMPARE_IMM
)) && compare1
->inst_imm
== 0))
680 /* Check second bblock */
684 if (((ins
->opcode
== OP_ICOMPARE_IMM
) || (ins
->opcode
== OP_LCOMPARE_IMM
)) && ins
->sreg1
== compare1
->sreg1
&& ins
->next
== branch2
) {
685 /* The second arg must be positive */
686 if (ins
->inst_imm
< 0)
688 } 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
) {
689 /* Another common case: if (index < 0 || index > arr.Length) */
694 if (cfg
->verbose_level
> 2) {
695 printf ("\tSigned->unsigned compare optimization in BB%d on\n", bb
->block_num
);
696 printf ("\t\t"); mono_print_ins (compare1
);
697 printf ("\t\t"); mono_print_ins (compare1
->next
);
698 printf ("\t\t"); mono_print_ins (ins
);
701 /* Rewrite the first compare+branch */
702 MONO_DELETE_INS (bb
, compare1
);
703 branch1
->opcode
= OP_BR
;
704 mono_unlink_bblock (cfg
, bb
, branch1
->inst_true_bb
);
705 mono_unlink_bblock (cfg
, bb
, branch1
->inst_false_bb
);
706 branch1
->inst_target_bb
= next_bb
;
707 mono_link_bblock (cfg
, bb
, next_bb
);
709 /* Rewrite the second branch */
710 branch2
->opcode
= br_to_br_un (branch2
->opcode
);
712 mono_merge_basic_blocks (cfg
, bb
, next_bb
);
716 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
717 MonoBasicBlock
*bb1
, *bb2
;
718 MonoInst
*prev
, *compare
, *branch
, *ins1
, *ins2
, *cmov
, *move
, *tmp
;
719 gboolean simple
, ret
;
723 /* Look for the IR code generated from if (cond) <var> <- <a>
724 * after branch opts which is:
731 if (!(bb
->out_count
== 1 && bb
->extended
&& bb
->code
&& bb
->code
->next
&& bb
->code
->next
->next
))
734 mono_print_bb (bb
, "");
736 /* Find the compare instruction */
740 while (compare
->next
->next
&& compare
->next
->next
!= bb
->last_ins
) {
742 compare
= compare
->next
;
744 branch
= compare
->next
;
745 if (!MONO_IS_COND_BRANCH_OP (branch
))
751 if (cfg
->opt
& MONO_OPT_BRANCH
)
752 mono_optimize_branches (cfg
);
753 /* Merging bblocks could make some variables local */
754 mono_handle_global_vregs (cfg
);
755 if (cfg
->opt
& (MONO_OPT_CONSPROP
| MONO_OPT_COPYPROP
))
756 mono_local_cprop (cfg
);
757 if (cfg
->opt
& MONO_OPT_DEADCE
)
758 mono_local_deadce (cfg
);
764 mono_nullify_basic_block (MonoBasicBlock
*bb
)
771 bb
->code
= bb
->last_ins
= NULL
;
776 replace_out_block (MonoBasicBlock
*bb
, MonoBasicBlock
*orig
, MonoBasicBlock
*repl
)
780 for (i
= 0; i
< bb
->out_count
; i
++) {
781 MonoBasicBlock
*ob
= bb
->out_bb
[i
];
784 if (bb
->out_count
> 1) {
785 bb
->out_bb
[i
] = bb
->out_bb
[bb
->out_count
- 1];
789 bb
->out_bb
[i
] = repl
;
796 replace_in_block (MonoBasicBlock
*bb
, MonoBasicBlock
*orig
, MonoBasicBlock
*repl
)
800 for (i
= 0; i
< bb
->in_count
; i
++) {
801 MonoBasicBlock
*ib
= bb
->in_bb
[i
];
804 if (bb
->in_count
> 1) {
805 bb
->in_bb
[i
] = bb
->in_bb
[bb
->in_count
- 1];
809 bb
->in_bb
[i
] = repl
;
816 replace_out_block_in_code (MonoBasicBlock
*bb
, MonoBasicBlock
*orig
, MonoBasicBlock
*repl
) {
819 for (ins
= bb
->code
; ins
!= NULL
; ins
= ins
->next
) {
820 switch (ins
->opcode
) {
822 if (ins
->inst_target_bb
== orig
)
823 ins
->inst_target_bb
= repl
;
825 case OP_CALL_HANDLER
:
826 if (ins
->inst_target_bb
== orig
)
827 ins
->inst_target_bb
= repl
;
831 int n
= GPOINTER_TO_INT (ins
->klass
);
832 for (i
= 0; i
< n
; i
++ ) {
833 if (ins
->inst_many_bb
[i
] == orig
)
834 ins
->inst_many_bb
[i
] = repl
;
839 if (MONO_IS_COND_BRANCH_OP (ins
)) {
840 if (ins
->inst_true_bb
== orig
)
841 ins
->inst_true_bb
= repl
;
842 if (ins
->inst_false_bb
== orig
)
843 ins
->inst_false_bb
= repl
;
844 } else if (MONO_IS_JUMP_TABLE (ins
)) {
846 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (ins
);
847 for (i
= 0; i
< table
->table_size
; i
++ ) {
848 if (table
->table
[i
] == orig
)
849 table
->table
[i
] = repl
;
859 * Check if a bb is useless (is just made of NOPs and ends with an
860 * unconditional branch, or nothing).
861 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
862 * Otherwise, return FALSE;
865 remove_block_if_useless (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoBasicBlock
*previous_bb
) {
866 MonoBasicBlock
*target_bb
= NULL
;
869 /* Do not touch handlers */
870 if (bb
->region
!= -1) {
871 bb
->not_useless
= TRUE
;
875 MONO_BB_FOR_EACH_INS (bb
, inst
) {
876 switch (inst
->opcode
) {
880 target_bb
= inst
->inst_target_bb
;
883 bb
->not_useless
= TRUE
;
888 if (target_bb
== NULL
) {
889 if ((bb
->out_count
== 1) && (bb
->out_bb
[0] == bb
->next_bb
)) {
890 target_bb
= bb
->next_bb
;
892 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
897 /* Do not touch BBs following a switch (they are the "default" branch) */
898 if ((previous_bb
->last_ins
!= NULL
) && (previous_bb
->last_ins
->opcode
== OP_SWITCH
)) {
902 /* Do not touch BBs following the entry BB and jumping to something that is not */
903 /* thiry "next" bb (the entry BB cannot contain the branch) */
904 if ((previous_bb
== cfg
->bb_entry
) && (bb
->next_bb
!= target_bb
)) {
909 * Do not touch BBs following a try block as the code in
910 * mini_method_compile needs them to compute the length of the try block.
912 if (MONO_BBLOCK_IS_IN_REGION (previous_bb
, MONO_REGION_TRY
))
915 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
916 if ((target_bb
!= NULL
) && (target_bb
!= bb
)) {
919 if (cfg
->verbose_level
> 1) {
920 printf ("remove_block_if_useless, removed BB%d\n", bb
->block_num
);
923 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
924 while (bb
->in_count
) {
925 MonoBasicBlock
*in_bb
= bb
->in_bb
[0];
926 mono_unlink_bblock (cfg
, in_bb
, bb
);
927 mono_link_bblock (cfg
, in_bb
, target_bb
);
928 replace_out_block_in_code (in_bb
, bb
, target_bb
);
931 mono_unlink_bblock (cfg
, bb
, target_bb
);
932 if (previous_bb
!= cfg
->bb_entry
&& mono_bb_is_fall_through (cfg
, previous_bb
)) {
933 for (i
= 0; i
< previous_bb
->out_count
; i
++) {
934 if (previous_bb
->out_bb
[i
] == target_bb
) {
936 MONO_INST_NEW (cfg
, jump
, OP_BR
);
937 MONO_ADD_INS (previous_bb
, jump
);
938 jump
->cil_code
= previous_bb
->cil_code
;
939 jump
->inst_target_bb
= target_bb
;
945 previous_bb
->next_bb
= bb
->next_bb
;
946 mono_nullify_basic_block (bb
);
955 mono_merge_basic_blocks (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoBasicBlock
*bbn
)
958 MonoBasicBlock
*prev_bb
;
961 bb
->has_array_access
|= bbn
->has_array_access
;
962 bb
->extended
|= bbn
->extended
;
964 mono_unlink_bblock (cfg
, bb
, bbn
);
965 for (i
= 0; i
< bbn
->out_count
; ++i
)
966 mono_link_bblock (cfg
, bb
, bbn
->out_bb
[i
]);
967 while (bbn
->out_count
)
968 mono_unlink_bblock (cfg
, bbn
, bbn
->out_bb
[0]);
970 /* Handle the branch at the end of the bb */
971 if (bb
->has_call_handler
) {
972 for (inst
= bb
->code
; inst
!= NULL
; inst
= inst
->next
) {
973 if (inst
->opcode
== OP_CALL_HANDLER
) {
974 g_assert (inst
->inst_target_bb
== bbn
);
979 if (bb
->has_jump_table
) {
980 for (inst
= bb
->code
; inst
!= NULL
; inst
= inst
->next
) {
981 if (MONO_IS_JUMP_TABLE (inst
)) {
983 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (inst
);
984 for (i
= 0; i
< table
->table_size
; i
++ ) {
985 /* Might be already NULL from a previous merge */
986 if (table
->table
[i
])
987 g_assert (table
->table
[i
] == bbn
);
988 table
->table
[i
] = NULL
;
990 /* Can't nullify this as later instructions depend on it */
994 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
995 g_assert (bb
->last_ins
->inst_false_bb
== bbn
);
996 bb
->last_ins
->inst_false_bb
= NULL
;
998 } else if (bb
->last_ins
&& MONO_IS_BRANCH_OP (bb
->last_ins
)) {
999 NULLIFY_INS (bb
->last_ins
);
1002 bb
->has_call_handler
|= bbn
->has_call_handler
;
1003 bb
->has_jump_table
|= bbn
->has_jump_table
;
1007 bb
->last_ins
->next
= bbn
->code
;
1008 bbn
->code
->prev
= bb
->last_ins
;
1009 bb
->last_ins
= bbn
->last_ins
;
1012 bb
->code
= bbn
->code
;
1013 bb
->last_ins
= bbn
->last_ins
;
1016 for (prev_bb
= cfg
->bb_entry
; prev_bb
&& prev_bb
->next_bb
!= bbn
; prev_bb
= prev_bb
->next_bb
)
1019 prev_bb
->next_bb
= bbn
->next_bb
;
1021 /* bbn might not be in the bb list yet */
1022 if (bb
->next_bb
== bbn
)
1023 bb
->next_bb
= bbn
->next_bb
;
1025 mono_nullify_basic_block (bbn
);
1028 * If bbn fell through to its next bblock, have to add a branch, since bb
1029 * will not fall though to the same bblock (#513931).
1031 if (bb
->last_ins
&& bb
->out_count
== 1 && bb
->out_bb
[0] != bb
->next_bb
&& !MONO_IS_BRANCH_OP (bb
->last_ins
)) {
1032 MONO_INST_NEW (cfg
, inst
, OP_BR
);
1033 inst
->inst_target_bb
= bb
->out_bb
[0];
1034 MONO_ADD_INS (bb
, inst
);
1039 move_basic_block_to_end (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1041 MonoBasicBlock
*bbn
, *next
;
1045 /* Find the previous */
1046 for (bbn
= cfg
->bb_entry
; bbn
->next_bb
&& bbn
->next_bb
!= bb
; bbn
= bbn
->next_bb
)
1049 bbn
->next_bb
= bb
->next_bb
;
1053 for (bbn
= cfg
->bb_entry
; bbn
->next_bb
; bbn
= bbn
->next_bb
)
1059 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
))))) {
1062 MONO_INST_NEW (cfg
, ins
, OP_BR
);
1063 MONO_ADD_INS (bb
, ins
);
1064 mono_link_bblock (cfg
, bb
, next
);
1065 ins
->inst_target_bb
= next
;
1070 * mono_remove_block:
1072 * Remove BB from the control flow graph
1075 mono_remove_bblock (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1077 MonoBasicBlock
*tmp_bb
;
1079 for (tmp_bb
= cfg
->bb_entry
; tmp_bb
&& tmp_bb
->next_bb
!= bb
; tmp_bb
= tmp_bb
->next_bb
)
1083 tmp_bb
->next_bb
= bb
->next_bb
;
1087 mono_remove_critical_edges (MonoCompile
*cfg
)
1090 MonoBasicBlock
*previous_bb
;
1092 if (cfg
->verbose_level
> 3) {
1093 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
1095 printf ("remove_critical_edges, BEFORE BB%d (in:", bb
->block_num
);
1096 for (i
= 0; i
< bb
->in_count
; i
++) {
1097 printf (" %d", bb
->in_bb
[i
]->block_num
);
1100 for (i
= 0; i
< bb
->out_count
; i
++) {
1101 printf (" %d", bb
->out_bb
[i
]->block_num
);
1104 if (bb
->last_ins
!= NULL
) {
1106 mono_print_ins (bb
->last_ins
);
1112 for (previous_bb
= cfg
->bb_entry
, bb
= previous_bb
->next_bb
; bb
!= NULL
; previous_bb
= previous_bb
->next_bb
, bb
= bb
->next_bb
) {
1113 if (bb
->in_count
> 1) {
1115 for (in_bb_index
= 0; in_bb_index
< bb
->in_count
; in_bb_index
++) {
1116 MonoBasicBlock
*in_bb
= bb
->in_bb
[in_bb_index
];
1118 * Have to remove non-critical edges whose source ends with a BR_REG
1119 * ins too, since inserting a computation before the BR_REG could
1120 * overwrite the sreg1 of the ins.
1122 if ((in_bb
->out_count
> 1) || (in_bb
->out_count
== 1 && in_bb
->last_ins
&& in_bb
->last_ins
->opcode
== OP_BR_REG
)) {
1123 MonoBasicBlock
*new_bb
= mono_mempool_alloc0 ((cfg
)->mempool
, sizeof (MonoBasicBlock
));
1124 new_bb
->block_num
= cfg
->num_bblocks
++;
1125 // new_bb->real_offset = bb->real_offset;
1126 new_bb
->region
= bb
->region
;
1128 /* Do not alter the CFG while altering the BB list */
1129 if (mono_bb_is_fall_through (cfg
, previous_bb
)) {
1130 if (previous_bb
!= cfg
->bb_entry
) {
1132 /* Make sure previous_bb really falls through bb */
1133 for (i
= 0; i
< previous_bb
->out_count
; i
++) {
1134 if (previous_bb
->out_bb
[i
] == bb
) {
1136 MONO_INST_NEW (cfg
, jump
, OP_BR
);
1137 MONO_ADD_INS (previous_bb
, jump
);
1138 jump
->cil_code
= previous_bb
->cil_code
;
1139 jump
->inst_target_bb
= bb
;
1144 /* We cannot add any inst to the entry BB, so we must */
1145 /* put a new BB in the middle to hold the OP_BR */
1147 MonoBasicBlock
*new_bb_after_entry
= mono_mempool_alloc0 ((cfg
)->mempool
, sizeof (MonoBasicBlock
));
1148 new_bb_after_entry
->block_num
= cfg
->num_bblocks
++;
1149 // new_bb_after_entry->real_offset = bb->real_offset;
1150 new_bb_after_entry
->region
= bb
->region
;
1152 MONO_INST_NEW (cfg
, jump
, OP_BR
);
1153 MONO_ADD_INS (new_bb_after_entry
, jump
);
1154 jump
->cil_code
= bb
->cil_code
;
1155 jump
->inst_target_bb
= bb
;
1157 mono_unlink_bblock (cfg
, previous_bb
, bb
);
1158 mono_link_bblock (cfg
, new_bb_after_entry
, bb
);
1159 mono_link_bblock (cfg
, previous_bb
, new_bb_after_entry
);
1161 previous_bb
->next_bb
= new_bb_after_entry
;
1162 previous_bb
= new_bb_after_entry
;
1164 if (cfg
->verbose_level
> 2) {
1165 printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry
->block_num
, bb
->block_num
);
1170 /* Insert new_bb in the BB list */
1171 previous_bb
->next_bb
= new_bb
;
1172 new_bb
->next_bb
= bb
;
1173 previous_bb
= new_bb
;
1175 /* Setup in_bb and out_bb */
1176 new_bb
->in_bb
= mono_mempool_alloc ((cfg
)->mempool
, sizeof (MonoBasicBlock
*));
1177 new_bb
->in_bb
[0] = in_bb
;
1178 new_bb
->in_count
= 1;
1179 new_bb
->out_bb
= mono_mempool_alloc ((cfg
)->mempool
, sizeof (MonoBasicBlock
*));
1180 new_bb
->out_bb
[0] = bb
;
1181 new_bb
->out_count
= 1;
1183 /* Relink in_bb and bb to (from) new_bb */
1184 replace_out_block (in_bb
, bb
, new_bb
);
1185 replace_out_block_in_code (in_bb
, bb
, new_bb
);
1186 replace_in_block (bb
, in_bb
, new_bb
);
1188 if (cfg
->verbose_level
> 2) {
1189 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
);
1196 if (cfg
->verbose_level
> 3) {
1197 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
1199 printf ("remove_critical_edges, AFTER BB%d (in:", bb
->block_num
);
1200 for (i
= 0; i
< bb
->in_count
; i
++) {
1201 printf (" %d", bb
->in_bb
[i
]->block_num
);
1204 for (i
= 0; i
< bb
->out_count
; i
++) {
1205 printf (" %d", bb
->out_bb
[i
]->block_num
);
1208 if (bb
->last_ins
!= NULL
) {
1210 mono_print_ins (bb
->last_ins
);
1218 * Optimizes the branches on the Control Flow Graph
1222 mono_optimize_branches (MonoCompile
*cfg
)
1224 int i
, changed
= FALSE
;
1225 MonoBasicBlock
*bb
, *bbn
;
1226 guint32 niterations
;
1229 * Some crazy loops could cause the code below to go into an infinite
1230 * loop, see bug #53003 for an example. To prevent this, we put an upper
1231 * bound on the number of iterations.
1233 if (cfg
->num_bblocks
> 1000)
1234 niterations
= cfg
->num_bblocks
* 2;
1239 MonoBasicBlock
*previous_bb
;
1243 /* we skip the entry block (exit is handled specially instead ) */
1244 for (previous_bb
= cfg
->bb_entry
, bb
= cfg
->bb_entry
->next_bb
; bb
; previous_bb
= bb
, bb
= bb
->next_bb
) {
1245 /* dont touch code inside exception clauses */
1246 if (bb
->region
!= -1)
1249 if (!bb
->not_useless
&& remove_block_if_useless (cfg
, bb
, previous_bb
)) {
1254 if ((bbn
= bb
->next_bb
) && bbn
->in_count
== 0 && bbn
!= cfg
->bb_exit
&& bb
->region
== bbn
->region
) {
1255 if (cfg
->verbose_level
> 2)
1256 g_print ("nullify block triggered %d\n", bbn
->block_num
);
1258 bb
->next_bb
= bbn
->next_bb
;
1260 for (i
= 0; i
< bbn
->out_count
; i
++)
1261 replace_in_block (bbn
->out_bb
[i
], bbn
, NULL
);
1263 mono_nullify_basic_block (bbn
);
1267 if (bb
->out_count
== 1) {
1268 bbn
= bb
->out_bb
[0];
1270 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
1271 if (bb
->last_ins
&& (bb
->last_ins
->opcode
!= OP_BR
) && MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1272 bb
->last_ins
->opcode
= OP_BR
;
1273 bb
->last_ins
->inst_target_bb
= bb
->last_ins
->inst_true_bb
;
1275 if (cfg
->verbose_level
> 2)
1276 g_print ("cond branch removal triggered in %d %d\n", bb
->block_num
, bb
->out_count
);
1279 if (bb
->region
== bbn
->region
&& bb
->next_bb
== bbn
) {
1280 /* the block are in sequence anyway ... */
1282 /* branches to the following block can be removed */
1283 if (bb
->last_ins
&& bb
->last_ins
->opcode
== OP_BR
) {
1284 bb
->last_ins
->opcode
= OP_NOP
;
1286 if (cfg
->verbose_level
> 2)
1287 g_print ("br removal triggered %d -> %d\n", bb
->block_num
, bbn
->block_num
);
1290 if (bbn
->in_count
== 1 && !bb
->extended
) {
1291 if (bbn
!= cfg
->bb_exit
) {
1292 if (cfg
->verbose_level
> 2)
1293 g_print ("block merge triggered %d -> %d\n", bb
->block_num
, bbn
->block_num
);
1294 mono_merge_basic_blocks (cfg
, bb
, bbn
);
1299 //mono_print_bb_code (bb);
1304 if ((bbn
= bb
->next_bb
) && bbn
->in_count
== 0 && bbn
!= cfg
->bb_exit
&& bb
->region
== bbn
->region
) {
1305 if (cfg
->verbose_level
> 2) {
1306 g_print ("nullify block triggered %d\n", bbn
->block_num
);
1308 bb
->next_bb
= bbn
->next_bb
;
1310 for (i
= 0; i
< bbn
->out_count
; i
++)
1311 replace_in_block (bbn
->out_bb
[i
], bbn
, NULL
);
1313 mono_nullify_basic_block (bbn
);
1318 if (bb
->out_count
== 1) {
1319 bbn
= bb
->out_bb
[0];
1321 if (bb
->last_ins
&& bb
->last_ins
->opcode
== OP_BR
) {
1322 bbn
= bb
->last_ins
->inst_target_bb
;
1323 if (bb
->region
== bbn
->region
&& bbn
->code
&& bbn
->code
->opcode
== OP_BR
&&
1324 bbn
->code
->inst_target_bb
!= bbn
&&
1325 bbn
->code
->inst_target_bb
->region
== bb
->region
) {
1327 if (cfg
->verbose_level
> 2)
1328 g_print ("branch to branch triggered %d -> %d -> %d\n", bb
->block_num
, bbn
->block_num
, bbn
->code
->inst_target_bb
->block_num
);
1330 replace_in_block (bbn
, bb
, NULL
);
1331 replace_out_block (bb
, bbn
, bbn
->code
->inst_target_bb
);
1332 mono_link_bblock (cfg
, bb
, bbn
->code
->inst_target_bb
);
1333 bb
->last_ins
->inst_target_bb
= bbn
->code
->inst_target_bb
;
1338 } else if (bb
->out_count
== 2) {
1339 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_NOFP (bb
->last_ins
)) {
1341 MonoBasicBlock
*taken_branch_target
= NULL
, *untaken_branch_target
= NULL
;
1343 if (bb
->last_ins
->flags
& MONO_INST_CFOLD_TAKEN
)
1344 branch_result
= BRANCH_TAKEN
;
1345 else if (bb
->last_ins
->flags
& MONO_INST_CFOLD_NOT_TAKEN
)
1346 branch_result
= BRANCH_NOT_TAKEN
;
1348 branch_result
= BRANCH_UNDEF
;
1350 if (branch_result
== BRANCH_TAKEN
) {
1351 taken_branch_target
= bb
->last_ins
->inst_true_bb
;
1352 untaken_branch_target
= bb
->last_ins
->inst_false_bb
;
1353 } else if (branch_result
== BRANCH_NOT_TAKEN
) {
1354 taken_branch_target
= bb
->last_ins
->inst_false_bb
;
1355 untaken_branch_target
= bb
->last_ins
->inst_true_bb
;
1357 if (taken_branch_target
) {
1358 /* if mono_eval_cond_branch () is ever taken to handle
1359 * non-constant values to compare, issue a pop here.
1361 bb
->last_ins
->opcode
= OP_BR
;
1362 bb
->last_ins
->inst_target_bb
= taken_branch_target
;
1364 mono_unlink_bblock (cfg
, bb
, untaken_branch_target
);
1368 bbn
= bb
->last_ins
->inst_true_bb
;
1369 if (bb
->region
== bbn
->region
&& bbn
->code
&& bbn
->code
->opcode
== OP_BR
&&
1370 bbn
->code
->inst_target_bb
->region
== bb
->region
) {
1371 if (cfg
->verbose_level
> 2)
1372 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
1373 bb
->block_num
, bbn
->block_num
, bbn
->code
->inst_target_bb
->block_num
,
1377 * Unlink, then relink bblocks to avoid various
1378 * tricky situations when the two targets of the branch
1379 * are equal, or will become equal after the change.
1381 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1382 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1384 bb
->last_ins
->inst_true_bb
= bbn
->code
->inst_target_bb
;
1386 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1387 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1393 bbn
= bb
->last_ins
->inst_false_bb
;
1394 if (bbn
&& bb
->region
== bbn
->region
&& bbn
->code
&& bbn
->code
->opcode
== OP_BR
&&
1395 bbn
->code
->inst_target_bb
->region
== bb
->region
) {
1396 if (cfg
->verbose_level
> 2)
1397 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
1398 bb
->block_num
, bbn
->block_num
, bbn
->code
->inst_target_bb
->block_num
,
1401 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1402 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1404 bb
->last_ins
->inst_false_bb
= bbn
->code
->inst_target_bb
;
1406 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1407 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1413 bbn
= bb
->last_ins
->inst_false_bb
;
1415 * If bb is an extended bb, it could contain an inside branch to bbn.
1416 * FIXME: Enable the optimization if that is not true.
1417 * If bblocks_linked () is true, then merging bb and bbn
1418 * would require addition of an extra branch at the end of bbn
1419 * slowing down loops.
1421 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
)) {
1422 g_assert (bbn
->in_bb
[0] == bb
);
1423 if (cfg
->verbose_level
> 2)
1424 g_print ("merge false branch target triggered BB%d -> BB%d\n", bb
->block_num
, bbn
->block_num
);
1425 mono_merge_basic_blocks (cfg
, bb
, bbn
);
1431 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_NOFP (bb
->last_ins
)) {
1432 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
) {
1433 /* Reverse the branch */
1434 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
1435 bbn
= bb
->last_ins
->inst_false_bb
;
1436 bb
->last_ins
->inst_false_bb
= bb
->last_ins
->inst_true_bb
;
1437 bb
->last_ins
->inst_true_bb
= bbn
;
1439 move_basic_block_to_end (cfg
, bb
->last_ins
->inst_true_bb
);
1440 if (cfg
->verbose_level
> 2)
1441 g_print ("cbranch to throw block triggered %d.\n",
1447 } while (changed
&& (niterations
> 0));
1450 #endif /* DISABLE_JIT */