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 #if defined(__native_client_codegen__)
820 /* Need to maintain this flag for the new block because */
821 /* we can't jump indirectly to a non-aligned block. */
822 if (orig
->flags
& BB_INDIRECT_JUMP_TARGET
)
824 repl
->flags
|= BB_INDIRECT_JUMP_TARGET
;
828 for (ins
= bb
->code
; ins
!= NULL
; ins
= ins
->next
) {
829 switch (ins
->opcode
) {
831 if (ins
->inst_target_bb
== orig
)
832 ins
->inst_target_bb
= repl
;
834 case OP_CALL_HANDLER
:
835 if (ins
->inst_target_bb
== orig
)
836 ins
->inst_target_bb
= repl
;
840 int n
= GPOINTER_TO_INT (ins
->klass
);
841 for (i
= 0; i
< n
; i
++ ) {
842 if (ins
->inst_many_bb
[i
] == orig
)
843 ins
->inst_many_bb
[i
] = repl
;
848 if (MONO_IS_COND_BRANCH_OP (ins
)) {
849 if (ins
->inst_true_bb
== orig
)
850 ins
->inst_true_bb
= repl
;
851 if (ins
->inst_false_bb
== orig
)
852 ins
->inst_false_bb
= repl
;
853 } else if (MONO_IS_JUMP_TABLE (ins
)) {
855 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (ins
);
856 for (i
= 0; i
< table
->table_size
; i
++ ) {
857 if (table
->table
[i
] == orig
)
858 table
->table
[i
] = repl
;
868 * Check if a bb is useless (is just made of NOPs and ends with an
869 * unconditional branch, or nothing).
870 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
871 * Otherwise, return FALSE;
874 remove_block_if_useless (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoBasicBlock
*previous_bb
) {
875 MonoBasicBlock
*target_bb
= NULL
;
878 /* Do not touch handlers */
879 if (bb
->region
!= -1) {
880 bb
->not_useless
= TRUE
;
884 MONO_BB_FOR_EACH_INS (bb
, inst
) {
885 switch (inst
->opcode
) {
889 target_bb
= inst
->inst_target_bb
;
892 bb
->not_useless
= TRUE
;
897 if (target_bb
== NULL
) {
898 if ((bb
->out_count
== 1) && (bb
->out_bb
[0] == bb
->next_bb
)) {
899 target_bb
= bb
->next_bb
;
901 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
906 /* Do not touch BBs following a switch (they are the "default" branch) */
907 if ((previous_bb
->last_ins
!= NULL
) && (previous_bb
->last_ins
->opcode
== OP_SWITCH
)) {
911 /* Do not touch BBs following the entry BB and jumping to something that is not */
912 /* thiry "next" bb (the entry BB cannot contain the branch) */
913 if ((previous_bb
== cfg
->bb_entry
) && (bb
->next_bb
!= target_bb
)) {
918 * Do not touch BBs following a try block as the code in
919 * mini_method_compile needs them to compute the length of the try block.
921 if (MONO_BBLOCK_IS_IN_REGION (previous_bb
, MONO_REGION_TRY
))
924 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
925 if ((target_bb
!= NULL
) && (target_bb
!= bb
)) {
928 if (cfg
->verbose_level
> 1) {
929 printf ("remove_block_if_useless, removed BB%d\n", bb
->block_num
);
932 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
933 while (bb
->in_count
) {
934 MonoBasicBlock
*in_bb
= bb
->in_bb
[0];
935 mono_unlink_bblock (cfg
, in_bb
, bb
);
936 mono_link_bblock (cfg
, in_bb
, target_bb
);
937 replace_out_block_in_code (in_bb
, bb
, target_bb
);
940 mono_unlink_bblock (cfg
, bb
, target_bb
);
941 if (previous_bb
!= cfg
->bb_entry
&& mono_bb_is_fall_through (cfg
, previous_bb
)) {
942 for (i
= 0; i
< previous_bb
->out_count
; i
++) {
943 if (previous_bb
->out_bb
[i
] == target_bb
) {
945 MONO_INST_NEW (cfg
, jump
, OP_BR
);
946 MONO_ADD_INS (previous_bb
, jump
);
947 jump
->cil_code
= previous_bb
->cil_code
;
948 jump
->inst_target_bb
= target_bb
;
954 previous_bb
->next_bb
= bb
->next_bb
;
955 mono_nullify_basic_block (bb
);
964 mono_merge_basic_blocks (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoBasicBlock
*bbn
)
967 MonoBasicBlock
*prev_bb
;
970 bb
->has_array_access
|= bbn
->has_array_access
;
971 bb
->extended
|= bbn
->extended
;
973 mono_unlink_bblock (cfg
, bb
, bbn
);
974 for (i
= 0; i
< bbn
->out_count
; ++i
)
975 mono_link_bblock (cfg
, bb
, bbn
->out_bb
[i
]);
976 while (bbn
->out_count
)
977 mono_unlink_bblock (cfg
, bbn
, bbn
->out_bb
[0]);
979 /* Handle the branch at the end of the bb */
980 if (bb
->has_call_handler
) {
981 for (inst
= bb
->code
; inst
!= NULL
; inst
= inst
->next
) {
982 if (inst
->opcode
== OP_CALL_HANDLER
) {
983 g_assert (inst
->inst_target_bb
== bbn
);
988 if (bb
->has_jump_table
) {
989 for (inst
= bb
->code
; inst
!= NULL
; inst
= inst
->next
) {
990 if (MONO_IS_JUMP_TABLE (inst
)) {
992 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (inst
);
993 for (i
= 0; i
< table
->table_size
; i
++ ) {
994 /* Might be already NULL from a previous merge */
995 if (table
->table
[i
])
996 g_assert (table
->table
[i
] == bbn
);
997 table
->table
[i
] = NULL
;
999 /* Can't nullify this as later instructions depend on it */
1003 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1004 g_assert (bb
->last_ins
->inst_false_bb
== bbn
);
1005 bb
->last_ins
->inst_false_bb
= NULL
;
1006 bb
->extended
= TRUE
;
1007 } else if (bb
->last_ins
&& MONO_IS_BRANCH_OP (bb
->last_ins
)) {
1008 NULLIFY_INS (bb
->last_ins
);
1011 bb
->has_call_handler
|= bbn
->has_call_handler
;
1012 bb
->has_jump_table
|= bbn
->has_jump_table
;
1016 bb
->last_ins
->next
= bbn
->code
;
1017 bbn
->code
->prev
= bb
->last_ins
;
1018 bb
->last_ins
= bbn
->last_ins
;
1021 bb
->code
= bbn
->code
;
1022 bb
->last_ins
= bbn
->last_ins
;
1025 for (prev_bb
= cfg
->bb_entry
; prev_bb
&& prev_bb
->next_bb
!= bbn
; prev_bb
= prev_bb
->next_bb
)
1028 prev_bb
->next_bb
= bbn
->next_bb
;
1030 /* bbn might not be in the bb list yet */
1031 if (bb
->next_bb
== bbn
)
1032 bb
->next_bb
= bbn
->next_bb
;
1034 mono_nullify_basic_block (bbn
);
1037 * If bbn fell through to its next bblock, have to add a branch, since bb
1038 * will not fall though to the same bblock (#513931).
1040 if (bb
->last_ins
&& bb
->out_count
== 1 && bb
->out_bb
[0] != bb
->next_bb
&& !MONO_IS_BRANCH_OP (bb
->last_ins
)) {
1041 MONO_INST_NEW (cfg
, inst
, OP_BR
);
1042 inst
->inst_target_bb
= bb
->out_bb
[0];
1043 MONO_ADD_INS (bb
, inst
);
1048 move_basic_block_to_end (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1050 MonoBasicBlock
*bbn
, *next
;
1054 /* Find the previous */
1055 for (bbn
= cfg
->bb_entry
; bbn
->next_bb
&& bbn
->next_bb
!= bb
; bbn
= bbn
->next_bb
)
1058 bbn
->next_bb
= bb
->next_bb
;
1062 for (bbn
= cfg
->bb_entry
; bbn
->next_bb
; bbn
= bbn
->next_bb
)
1068 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
))))) {
1071 MONO_INST_NEW (cfg
, ins
, OP_BR
);
1072 MONO_ADD_INS (bb
, ins
);
1073 mono_link_bblock (cfg
, bb
, next
);
1074 ins
->inst_target_bb
= next
;
1079 * mono_remove_block:
1081 * Remove BB from the control flow graph
1084 mono_remove_bblock (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1086 MonoBasicBlock
*tmp_bb
;
1088 for (tmp_bb
= cfg
->bb_entry
; tmp_bb
&& tmp_bb
->next_bb
!= bb
; tmp_bb
= tmp_bb
->next_bb
)
1092 tmp_bb
->next_bb
= bb
->next_bb
;
1096 mono_remove_critical_edges (MonoCompile
*cfg
)
1099 MonoBasicBlock
*previous_bb
;
1101 if (cfg
->verbose_level
> 3) {
1102 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
1104 printf ("remove_critical_edges, BEFORE BB%d (in:", bb
->block_num
);
1105 for (i
= 0; i
< bb
->in_count
; i
++) {
1106 printf (" %d", bb
->in_bb
[i
]->block_num
);
1109 for (i
= 0; i
< bb
->out_count
; i
++) {
1110 printf (" %d", bb
->out_bb
[i
]->block_num
);
1113 if (bb
->last_ins
!= NULL
) {
1115 mono_print_ins (bb
->last_ins
);
1121 for (previous_bb
= cfg
->bb_entry
, bb
= previous_bb
->next_bb
; bb
!= NULL
; previous_bb
= previous_bb
->next_bb
, bb
= bb
->next_bb
) {
1122 if (bb
->in_count
> 1) {
1124 for (in_bb_index
= 0; in_bb_index
< bb
->in_count
; in_bb_index
++) {
1125 MonoBasicBlock
*in_bb
= bb
->in_bb
[in_bb_index
];
1127 * Have to remove non-critical edges whose source ends with a BR_REG
1128 * ins too, since inserting a computation before the BR_REG could
1129 * overwrite the sreg1 of the ins.
1131 if ((in_bb
->out_count
> 1) || (in_bb
->out_count
== 1 && in_bb
->last_ins
&& in_bb
->last_ins
->opcode
== OP_BR_REG
)) {
1132 MonoBasicBlock
*new_bb
= mono_mempool_alloc0 ((cfg
)->mempool
, sizeof (MonoBasicBlock
));
1133 new_bb
->block_num
= cfg
->num_bblocks
++;
1134 // new_bb->real_offset = bb->real_offset;
1135 new_bb
->region
= bb
->region
;
1137 /* Do not alter the CFG while altering the BB list */
1138 if (mono_bb_is_fall_through (cfg
, previous_bb
)) {
1139 if (previous_bb
!= cfg
->bb_entry
) {
1141 /* Make sure previous_bb really falls through bb */
1142 for (i
= 0; i
< previous_bb
->out_count
; i
++) {
1143 if (previous_bb
->out_bb
[i
] == bb
) {
1145 MONO_INST_NEW (cfg
, jump
, OP_BR
);
1146 MONO_ADD_INS (previous_bb
, jump
);
1147 jump
->cil_code
= previous_bb
->cil_code
;
1148 jump
->inst_target_bb
= bb
;
1153 /* We cannot add any inst to the entry BB, so we must */
1154 /* put a new BB in the middle to hold the OP_BR */
1156 MonoBasicBlock
*new_bb_after_entry
= mono_mempool_alloc0 ((cfg
)->mempool
, sizeof (MonoBasicBlock
));
1157 new_bb_after_entry
->block_num
= cfg
->num_bblocks
++;
1158 // new_bb_after_entry->real_offset = bb->real_offset;
1159 new_bb_after_entry
->region
= bb
->region
;
1161 MONO_INST_NEW (cfg
, jump
, OP_BR
);
1162 MONO_ADD_INS (new_bb_after_entry
, jump
);
1163 jump
->cil_code
= bb
->cil_code
;
1164 jump
->inst_target_bb
= bb
;
1166 mono_unlink_bblock (cfg
, previous_bb
, bb
);
1167 mono_link_bblock (cfg
, new_bb_after_entry
, bb
);
1168 mono_link_bblock (cfg
, previous_bb
, new_bb_after_entry
);
1170 previous_bb
->next_bb
= new_bb_after_entry
;
1171 previous_bb
= new_bb_after_entry
;
1173 if (cfg
->verbose_level
> 2) {
1174 printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry
->block_num
, bb
->block_num
);
1179 /* Insert new_bb in the BB list */
1180 previous_bb
->next_bb
= new_bb
;
1181 new_bb
->next_bb
= bb
;
1182 previous_bb
= new_bb
;
1184 /* Setup in_bb and out_bb */
1185 new_bb
->in_bb
= mono_mempool_alloc ((cfg
)->mempool
, sizeof (MonoBasicBlock
*));
1186 new_bb
->in_bb
[0] = in_bb
;
1187 new_bb
->in_count
= 1;
1188 new_bb
->out_bb
= mono_mempool_alloc ((cfg
)->mempool
, sizeof (MonoBasicBlock
*));
1189 new_bb
->out_bb
[0] = bb
;
1190 new_bb
->out_count
= 1;
1192 /* Relink in_bb and bb to (from) new_bb */
1193 replace_out_block (in_bb
, bb
, new_bb
);
1194 replace_out_block_in_code (in_bb
, bb
, new_bb
);
1195 replace_in_block (bb
, in_bb
, new_bb
);
1197 if (cfg
->verbose_level
> 2) {
1198 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
);
1205 if (cfg
->verbose_level
> 3) {
1206 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
1208 printf ("remove_critical_edges, AFTER BB%d (in:", bb
->block_num
);
1209 for (i
= 0; i
< bb
->in_count
; i
++) {
1210 printf (" %d", bb
->in_bb
[i
]->block_num
);
1213 for (i
= 0; i
< bb
->out_count
; i
++) {
1214 printf (" %d", bb
->out_bb
[i
]->block_num
);
1217 if (bb
->last_ins
!= NULL
) {
1219 mono_print_ins (bb
->last_ins
);
1227 * Optimizes the branches on the Control Flow Graph
1231 mono_optimize_branches (MonoCompile
*cfg
)
1233 int i
, changed
= FALSE
;
1234 MonoBasicBlock
*bb
, *bbn
;
1235 guint32 niterations
;
1238 * Some crazy loops could cause the code below to go into an infinite
1239 * loop, see bug #53003 for an example. To prevent this, we put an upper
1240 * bound on the number of iterations.
1242 if (cfg
->num_bblocks
> 1000)
1243 niterations
= cfg
->num_bblocks
* 2;
1248 MonoBasicBlock
*previous_bb
;
1252 /* we skip the entry block (exit is handled specially instead ) */
1253 for (previous_bb
= cfg
->bb_entry
, bb
= cfg
->bb_entry
->next_bb
; bb
; previous_bb
= bb
, bb
= bb
->next_bb
) {
1254 /* dont touch code inside exception clauses */
1255 if (bb
->region
!= -1)
1258 if (!bb
->not_useless
&& remove_block_if_useless (cfg
, bb
, previous_bb
)) {
1263 if ((bbn
= bb
->next_bb
) && bbn
->in_count
== 0 && bbn
!= cfg
->bb_exit
&& bb
->region
== bbn
->region
) {
1264 if (cfg
->verbose_level
> 2)
1265 g_print ("nullify block triggered %d\n", bbn
->block_num
);
1267 bb
->next_bb
= bbn
->next_bb
;
1269 for (i
= 0; i
< bbn
->out_count
; i
++)
1270 replace_in_block (bbn
->out_bb
[i
], bbn
, NULL
);
1272 mono_nullify_basic_block (bbn
);
1276 if (bb
->out_count
== 1) {
1277 bbn
= bb
->out_bb
[0];
1279 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
1280 if (bb
->last_ins
&& (bb
->last_ins
->opcode
!= OP_BR
) && MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1281 bb
->last_ins
->opcode
= OP_BR
;
1282 bb
->last_ins
->inst_target_bb
= bb
->last_ins
->inst_true_bb
;
1284 if (cfg
->verbose_level
> 2)
1285 g_print ("cond branch removal triggered in %d %d\n", bb
->block_num
, bb
->out_count
);
1288 if (bb
->region
== bbn
->region
&& bb
->next_bb
== bbn
) {
1289 /* the block are in sequence anyway ... */
1291 /* branches to the following block can be removed */
1292 if (bb
->last_ins
&& bb
->last_ins
->opcode
== OP_BR
&& !bbn
->out_of_line
) {
1293 bb
->last_ins
->opcode
= OP_NOP
;
1295 if (cfg
->verbose_level
> 2)
1296 g_print ("br removal triggered %d -> %d\n", bb
->block_num
, bbn
->block_num
);
1299 if (bbn
->in_count
== 1 && !bb
->extended
) {
1300 if (bbn
!= cfg
->bb_exit
) {
1301 if (cfg
->verbose_level
> 2)
1302 g_print ("block merge triggered %d -> %d\n", bb
->block_num
, bbn
->block_num
);
1303 mono_merge_basic_blocks (cfg
, bb
, bbn
);
1308 //mono_print_bb_code (bb);
1313 if ((bbn
= bb
->next_bb
) && bbn
->in_count
== 0 && bbn
!= cfg
->bb_exit
&& bb
->region
== bbn
->region
) {
1314 if (cfg
->verbose_level
> 2) {
1315 g_print ("nullify block triggered %d\n", bbn
->block_num
);
1317 bb
->next_bb
= bbn
->next_bb
;
1319 for (i
= 0; i
< bbn
->out_count
; i
++)
1320 replace_in_block (bbn
->out_bb
[i
], bbn
, NULL
);
1322 mono_nullify_basic_block (bbn
);
1327 if (bb
->out_count
== 1) {
1328 bbn
= bb
->out_bb
[0];
1330 if (bb
->last_ins
&& bb
->last_ins
->opcode
== OP_BR
) {
1331 bbn
= bb
->last_ins
->inst_target_bb
;
1332 if (bb
->region
== bbn
->region
&& bbn
->code
&& bbn
->code
->opcode
== OP_BR
&&
1333 bbn
->code
->inst_target_bb
!= bbn
&&
1334 bbn
->code
->inst_target_bb
->region
== bb
->region
) {
1336 if (cfg
->verbose_level
> 2)
1337 g_print ("branch to branch triggered %d -> %d -> %d\n", bb
->block_num
, bbn
->block_num
, bbn
->code
->inst_target_bb
->block_num
);
1339 replace_in_block (bbn
, bb
, NULL
);
1340 replace_out_block (bb
, bbn
, bbn
->code
->inst_target_bb
);
1341 mono_link_bblock (cfg
, bb
, bbn
->code
->inst_target_bb
);
1342 bb
->last_ins
->inst_target_bb
= bbn
->code
->inst_target_bb
;
1347 } else if (bb
->out_count
== 2) {
1348 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_NOFP (bb
->last_ins
)) {
1350 MonoBasicBlock
*taken_branch_target
= NULL
, *untaken_branch_target
= NULL
;
1352 if (bb
->last_ins
->flags
& MONO_INST_CFOLD_TAKEN
)
1353 branch_result
= BRANCH_TAKEN
;
1354 else if (bb
->last_ins
->flags
& MONO_INST_CFOLD_NOT_TAKEN
)
1355 branch_result
= BRANCH_NOT_TAKEN
;
1357 branch_result
= BRANCH_UNDEF
;
1359 if (branch_result
== BRANCH_TAKEN
) {
1360 taken_branch_target
= bb
->last_ins
->inst_true_bb
;
1361 untaken_branch_target
= bb
->last_ins
->inst_false_bb
;
1362 } else if (branch_result
== BRANCH_NOT_TAKEN
) {
1363 taken_branch_target
= bb
->last_ins
->inst_false_bb
;
1364 untaken_branch_target
= bb
->last_ins
->inst_true_bb
;
1366 if (taken_branch_target
) {
1367 /* if mono_eval_cond_branch () is ever taken to handle
1368 * non-constant values to compare, issue a pop here.
1370 bb
->last_ins
->opcode
= OP_BR
;
1371 bb
->last_ins
->inst_target_bb
= taken_branch_target
;
1373 mono_unlink_bblock (cfg
, bb
, untaken_branch_target
);
1377 bbn
= bb
->last_ins
->inst_true_bb
;
1378 if (bb
->region
== bbn
->region
&& bbn
->code
&& bbn
->code
->opcode
== OP_BR
&&
1379 bbn
->code
->inst_target_bb
->region
== bb
->region
) {
1380 if (cfg
->verbose_level
> 2)
1381 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
1382 bb
->block_num
, bbn
->block_num
, bbn
->code
->inst_target_bb
->block_num
,
1386 * Unlink, then relink bblocks to avoid various
1387 * tricky situations when the two targets of the branch
1388 * are equal, or will become equal after the change.
1390 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1391 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1393 bb
->last_ins
->inst_true_bb
= bbn
->code
->inst_target_bb
;
1395 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1396 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1402 bbn
= bb
->last_ins
->inst_false_bb
;
1403 if (bbn
&& bb
->region
== bbn
->region
&& bbn
->code
&& bbn
->code
->opcode
== OP_BR
&&
1404 bbn
->code
->inst_target_bb
->region
== bb
->region
) {
1405 if (cfg
->verbose_level
> 2)
1406 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
1407 bb
->block_num
, bbn
->block_num
, bbn
->code
->inst_target_bb
->block_num
,
1410 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1411 mono_unlink_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1413 bb
->last_ins
->inst_false_bb
= bbn
->code
->inst_target_bb
;
1415 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_true_bb
);
1416 mono_link_bblock (cfg
, bb
, bb
->last_ins
->inst_false_bb
);
1422 bbn
= bb
->last_ins
->inst_false_bb
;
1424 * If bb is an extended bb, it could contain an inside branch to bbn.
1425 * FIXME: Enable the optimization if that is not true.
1426 * If bblocks_linked () is true, then merging bb and bbn
1427 * would require addition of an extra branch at the end of bbn
1428 * slowing down loops.
1430 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
)) {
1431 g_assert (bbn
->in_bb
[0] == bb
);
1432 if (cfg
->verbose_level
> 2)
1433 g_print ("merge false branch target triggered BB%d -> BB%d\n", bb
->block_num
, bbn
->block_num
);
1434 mono_merge_basic_blocks (cfg
, bb
, bbn
);
1440 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_NOFP (bb
->last_ins
)) {
1441 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
) {
1442 /* Reverse the branch */
1443 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
1444 bbn
= bb
->last_ins
->inst_false_bb
;
1445 bb
->last_ins
->inst_false_bb
= bb
->last_ins
->inst_true_bb
;
1446 bb
->last_ins
->inst_true_bb
= bbn
;
1448 move_basic_block_to_end (cfg
, bb
->last_ins
->inst_true_bb
);
1449 if (cfg
->verbose_level
> 2)
1450 g_print ("cbranch to throw block triggered %d.\n",
1456 } while (changed
&& (niterations
> 0));
1459 #endif /* DISABLE_JIT */