[interp] Fix interp logging (#17636)
[mono-project.git] / mono / mini / branch-opts.c
blob15c2a76d674130330e6fbda67f50105d7cdcb233
1 /**
2 * \file
3 * Branch optimizations support
5 * Authors:
6 * Patrik Torstensson (Patrik.Torstesson at gmail.com)
8 * (C) 2005 Ximian, Inc. http://www.ximian.com
9 * Copyright 2011 Xamarin Inc. http://www.xamarin.com
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include "config.h"
14 #include <mono/utils/mono-compiler.h>
15 #ifndef DISABLE_JIT
17 #include "mini.h"
18 #include "mini-runtime.h"
21 * Returns true if @bb is a basic block which falls through the next block.
22 * TODO verify if it helps to check if the bb last ins is a branch to its successor.
24 static gboolean
25 mono_bb_is_fall_through (MonoCompile *cfg, MonoBasicBlock *bb)
27 return bb->next_bb && bb->next_bb->region == bb->region && /*fall throught between regions is not really interesting or useful*/
28 (bb->last_ins == NULL || !MONO_IS_BRANCH_OP (bb->last_ins)); /*and the last op can't be a branch too*/
32 * Used by the arch code to replace the exception handling
33 * with a direct branch. This is safe to do if the
34 * exception object isn't used, no rethrow statement and
35 * no filter statement (verify).
38 MonoInst *
39 mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname)
41 MonoMethodHeader *header = cfg->header;
42 MonoExceptionClause *clause;
43 MonoClass *exclass;
44 int i;
46 if (!(cfg->opt & MONO_OPT_EXCEPTION))
47 return NULL;
49 if (bb->region == -1 || !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
50 return NULL;
52 exclass = mono_class_load_from_name (mono_get_corlib (), "System", exname);
53 /* search for the handler */
54 for (i = 0; i < header->num_clauses; ++i) {
55 clause = &header->clauses [i];
56 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset)) {
57 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE && clause->data.catch_class && mono_class_is_assignable_from_internal (clause->data.catch_class, exclass)) {
58 MonoBasicBlock *tbb;
60 /* get the basic block for the handler and
61 * check if the exception object is used.
62 * Flag is set during method_to_ir due to
63 * pop-op is optmized away in codegen (burg).
65 tbb = cfg->cil_offset_to_bb [clause->handler_offset];
66 if (tbb && tbb->flags & BB_EXCEPTION_DEAD_OBJ && !(tbb->flags & BB_EXCEPTION_UNSAFE)) {
67 MonoBasicBlock *targetbb = tbb;
68 gboolean unsafe = FALSE;
70 /* Check if this catch clause is ok to optimize by
71 * looking for the BB_EXCEPTION_UNSAFE in every BB that
72 * belongs to the same region.
74 * UNSAFE flag is set during method_to_ir (OP_RETHROW)
76 while (!unsafe && tbb->next_bb && tbb->region == tbb->next_bb->region) {
77 if (tbb->next_bb->flags & BB_EXCEPTION_UNSAFE) {
78 unsafe = TRUE;
79 break;
81 tbb = tbb->next_bb;
84 if (!unsafe) {
85 MonoInst *jump;
87 /* Create dummy inst to allow easier integration in
88 * arch dependent code (opcode ignored)
90 MONO_INST_NEW (cfg, jump, OP_BR);
92 /* Allocate memory for our branch target */
93 jump->inst_i1 = (MonoInst *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
94 jump->inst_true_bb = targetbb;
96 if (cfg->verbose_level > 2)
97 g_print ("found exception to optimize - returning branch to BB%d (%s) (instead of throw) for method %s:%s\n", targetbb->block_num, m_class_get_name (clause->data.catch_class), m_class_get_name (cfg->method->klass), cfg->method->name);
99 return jump;
102 return NULL;
103 } else {
104 /* Branching to an outer clause could skip inner clauses */
105 return NULL;
107 } else {
108 /* Branching to an outer clause could skip inner clauses */
109 return NULL;
114 return NULL;
117 #ifdef MONO_ARCH_HAVE_CMOV_OPS
118 static const int int_cmov_opcodes [] = {
119 OP_CMOV_IEQ,
120 OP_CMOV_INE_UN,
121 OP_CMOV_ILE,
122 OP_CMOV_IGE,
123 OP_CMOV_ILT,
124 OP_CMOV_IGT,
125 OP_CMOV_ILE_UN,
126 OP_CMOV_IGE_UN,
127 OP_CMOV_ILT_UN,
128 OP_CMOV_IGT_UN
131 static const int long_cmov_opcodes [] = {
132 OP_CMOV_LEQ,
133 OP_CMOV_LNE_UN,
134 OP_CMOV_LLE,
135 OP_CMOV_LGE,
136 OP_CMOV_LLT,
137 OP_CMOV_LGT,
138 OP_CMOV_LLE_UN,
139 OP_CMOV_LGE_UN,
140 OP_CMOV_LLT_UN,
141 OP_CMOV_LGT_UN
144 static int
145 br_to_br_un (int opcode)
147 switch (opcode) {
148 case OP_IBGT:
149 return OP_IBGT_UN;
150 break;
151 case OP_IBLE:
152 return OP_IBLE_UN;
153 break;
154 case OP_LBGT:
155 return OP_LBGT_UN;
156 break;
157 case OP_LBLE:
158 return OP_LBLE_UN;
159 break;
160 default:
161 g_assert_not_reached ();
162 return -1;
165 #endif
168 * mono_replace_ins:
170 * Replace INS with its decomposition which is stored in a series of bblocks starting
171 * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS.
172 * On return, it will be set to the last ins of the decomposition.
174 void
175 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
177 MonoInst *next = ins->next;
179 if (next && next->opcode == OP_NOP) {
180 /* Avoid NOPs following branches */
181 ins->next = next->next;
182 next = next->next;
185 if (first_bb == last_bb) {
187 * Only one replacement bb, merge the code into
188 * the current bb.
191 /* Delete links between the first_bb and its successors */
192 while (first_bb->out_count)
193 mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
195 /* Head */
196 if (*prev) {
197 (*prev)->next = first_bb->code;
198 first_bb->code->prev = (*prev);
199 } else {
200 bb->code = first_bb->code;
203 /* Tail */
204 last_bb->last_ins->next = next;
205 if (next)
206 next->prev = last_bb->last_ins;
207 else
208 bb->last_ins = last_bb->last_ins;
209 *prev = last_bb->last_ins;
210 bb->needs_decompose |= first_bb->needs_decompose;
211 } else {
212 int i, count;
213 MonoBasicBlock **tmp_bblocks, *tmp;
214 MonoInst *last;
216 /* Multiple BBs */
218 /* Set region/real_offset */
219 for (tmp = first_bb; tmp; tmp = tmp->next_bb) {
220 tmp->region = bb->region;
221 tmp->real_offset = bb->real_offset;
224 /* Split the original bb */
225 if (ins->next)
226 ins->next->prev = NULL;
227 ins->next = NULL;
228 bb->last_ins = ins;
230 /* Merge the second part of the original bb into the last bb */
231 if (last_bb->last_ins) {
232 last_bb->last_ins->next = next;
233 if (next)
234 next->prev = last_bb->last_ins;
235 } else {
236 last_bb->code = next;
238 last_bb->needs_decompose |= bb->needs_decompose;
240 if (next) {
241 for (last = next; last->next != NULL; last = last->next)
243 last_bb->last_ins = last;
246 for (i = 0; i < bb->out_count; ++i)
247 mono_link_bblock (cfg, last_bb, bb->out_bb [i]);
249 /* Merge the first (dummy) bb to the original bb */
250 if (*prev) {
251 (*prev)->next = first_bb->code;
252 first_bb->code->prev = (*prev);
253 } else {
254 bb->code = first_bb->code;
256 bb->last_ins = first_bb->last_ins;
257 bb->needs_decompose |= first_bb->needs_decompose;
259 /* Delete the links between the original bb and its successors */
260 tmp_bblocks = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * bb->out_count);
261 memcpy (tmp_bblocks, bb->out_bb, sizeof (MonoBasicBlock*) * bb->out_count);
262 count = bb->out_count;
263 for (i = 0; i < count; ++i)
264 mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
266 /* Add links between the original bb and the first_bb's successors */
267 for (i = 0; i < first_bb->out_count; ++i) {
268 MonoBasicBlock *out_bb = first_bb->out_bb [i];
270 mono_link_bblock (cfg, bb, out_bb);
272 /* Delete links between the first_bb and its successors */
273 for (i = 0; i < bb->out_count; ++i) {
274 MonoBasicBlock *out_bb = bb->out_bb [i];
276 mono_unlink_bblock (cfg, first_bb, out_bb);
278 last_bb->next_bb = bb->next_bb;
279 bb->next_bb = first_bb->next_bb;
281 *prev = NULL;
285 void
286 mono_if_conversion (MonoCompile *cfg)
288 #ifdef MONO_ARCH_HAVE_CMOV_OPS
289 MonoBasicBlock *bb;
290 gboolean changed = FALSE;
291 int filter = FILTER_NOP | FILTER_IL_SEQ_POINT;
293 if (!(cfg->opt & MONO_OPT_CMOV))
294 return;
296 // FIXME: Make this work with extended bblocks
299 * This pass requires somewhat optimized IR code so it should be run after
300 * local cprop/deadce. Also, it should be run before dominator computation, since
301 * it changes control flow.
303 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
304 MonoBasicBlock *bb1, *bb2;
306 restart:
307 /* Look for the IR code generated from cond ? a : b
308 * which is:
309 * BB:
310 * b<cond> [BB1BB2]
311 * BB1:
312 * <var> <- <a>
313 * br BB3
314 * BB2:
315 * <var> <- <b>
316 * br BB3
318 if (!(bb->out_count == 2 && !bb->extended))
319 continue;
321 bb1 = bb->out_bb [0];
322 bb2 = bb->out_bb [1];
324 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]) {
325 MonoInst *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
326 MonoBasicBlock *true_bb, *false_bb;
327 gboolean simple, ret;
328 int dreg, tmp_reg;
329 CompType comp_type;
331 branch = mono_bb_last_inst (bb, filter);
333 if (!branch || branch->opcode == OP_BR_REG || branch->opcode == OP_BR)
334 continue;
336 /* Find the compare instruction */
337 compare = mono_inst_prev (branch, filter);
338 if (!compare)
339 continue;
341 if (!MONO_IS_COND_BRANCH_OP (branch))
342 /* This can happen if a cond branch is optimized away */
343 continue;
345 true_bb = branch->inst_true_bb;
346 false_bb = branch->inst_false_bb;
349 * Check that bb1 and bb2 are 'simple' and both assign to the same
350 * variable.
352 /* FIXME: Get rid of the nops earlier */
353 ins1 = mono_bb_first_inst (true_bb, filter);
354 ins2 = mono_bb_first_inst (false_bb, filter);
356 if (!(ins1 && ins2 && ins1->dreg == ins2->dreg && ins1->dreg != -1))
357 continue;
359 simple = TRUE;
360 for (tmp = ins1->next; tmp; tmp = tmp->next)
361 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_IL_SEQ_POINT) || (tmp->opcode == OP_BR)))
362 simple = FALSE;
364 for (tmp = ins2->next; tmp; tmp = tmp->next)
365 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_IL_SEQ_POINT) || (tmp->opcode == OP_BR)))
366 simple = FALSE;
368 if (!simple)
369 continue;
371 /* We move ins1/ins2 before the compare so they should have no side effect */
372 if (!(MONO_INS_HAS_NO_SIDE_EFFECT (ins1) && MONO_INS_HAS_NO_SIDE_EFFECT (ins2)))
373 continue;
375 /* Moving ins1/ins2 could change the comparison */
376 /* FIXME: */
377 if (!((compare->sreg1 != ins1->dreg) && (compare->sreg2 != ins1->dreg)))
378 continue;
380 /* FIXME: */
381 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
382 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
383 continue;
385 /* FIXME: */
386 /* ins->type might not be set */
387 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
388 continue;
390 if (cfg->verbose_level > 2) {
391 printf ("\tBranch -> CMove optimization in BB%d on\n", bb->block_num);
392 printf ("\t\t"); mono_print_ins (compare);
393 printf ("\t\t"); mono_print_ins (mono_inst_next (compare, filter));
394 printf ("\t\t"); mono_print_ins (ins1);
395 printf ("\t\t"); mono_print_ins (ins2);
398 changed = TRUE;
400 //printf ("HIT!\n");
402 /* Assignments to the return register must remain at the end of bbs */
403 if (cfg->ret)
404 ret = ins1->dreg == cfg->ret->dreg;
405 else
406 ret = FALSE;
408 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
409 dreg = ins1->dreg;
411 /* Rewrite ins1 to emit to tmp_reg */
412 ins1->dreg = tmp_reg;
414 if (ret) {
415 dreg = mono_alloc_dreg (cfg, STACK_I4);
416 ins2->dreg = dreg;
419 /* Remove ins1/ins2 from bb1/bb2 */
420 MONO_REMOVE_INS (true_bb, ins1);
421 MONO_REMOVE_INS (false_bb, ins2);
423 /* Move ins1 and ins2 before the comparison */
424 /* ins1 comes first to avoid ins1 overwriting an argument of ins2 */
425 mono_bblock_insert_before_ins (bb, compare, ins2);
426 mono_bblock_insert_before_ins (bb, ins2, ins1);
428 /* Add cmov instruction */
429 MONO_INST_NEW (cfg, cmov, OP_NOP);
430 cmov->dreg = dreg;
431 cmov->sreg1 = dreg;
432 cmov->sreg2 = tmp_reg;
433 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
434 case CMP_TYPE_I:
435 cmov->opcode = int_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
436 break;
437 case CMP_TYPE_L:
438 cmov->opcode = long_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
439 break;
440 default:
441 g_assert_not_reached ();
443 mono_bblock_insert_after_ins (bb, compare, cmov);
445 if (ret) {
446 /* Add an extra move */
447 MONO_INST_NEW (cfg, move, OP_MOVE);
448 move->dreg = cfg->ret->dreg;
449 move->sreg1 = dreg;
450 mono_bblock_insert_after_ins (bb, cmov, move);
453 /* Rewrite the branch */
454 branch->opcode = OP_BR;
455 branch->inst_target_bb = true_bb->out_bb [0];
456 mono_link_bblock (cfg, bb, branch->inst_target_bb);
458 /* Reorder bblocks */
459 mono_unlink_bblock (cfg, bb, true_bb);
460 mono_unlink_bblock (cfg, bb, false_bb);
461 mono_unlink_bblock (cfg, true_bb, true_bb->out_bb [0]);
462 mono_unlink_bblock (cfg, false_bb, false_bb->out_bb [0]);
463 mono_remove_bblock (cfg, true_bb);
464 mono_remove_bblock (cfg, false_bb);
466 /* Merge bb and its successor if possible */
467 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
468 (bb->region == bb->out_bb [0]->region)) {
469 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
470 goto restart;
474 /* Look for the IR code generated from if (cond) <var> <- <a>
475 * which is:
476 * BB:
477 * b<cond> [BB1BB2]
478 * BB1:
479 * <var> <- <a>
480 * br BB2
483 if ((bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) ||
484 (bb1->in_count == 1 && bb1->out_count == 1 && bb1->out_bb [0] == bb2)) {
485 MonoInst *compare, *branch, *ins1, *cmov, *tmp;
486 gboolean simple;
487 int dreg, tmp_reg;
488 CompType comp_type;
489 CompRelation cond;
490 MonoBasicBlock *next_bb, *code_bb;
492 /* code_bb is the bblock containing code, next_bb is the successor bblock */
493 if (bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) {
494 code_bb = bb2;
495 next_bb = bb1;
496 } else {
497 code_bb = bb1;
498 next_bb = bb2;
501 ins1 = mono_bb_first_inst (code_bb, filter);
503 if (!ins1)
504 continue;
506 /* Check that code_bb is simple */
507 simple = TRUE;
508 for (tmp = ins1; tmp; tmp = tmp->next)
509 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_IL_SEQ_POINT) || (tmp->opcode == OP_BR)))
510 simple = FALSE;
512 if (!simple)
513 continue;
515 /* We move ins1 before the compare so it should have no side effect */
516 if (!MONO_INS_HAS_NO_SIDE_EFFECT (ins1))
517 continue;
519 branch = mono_bb_last_inst (bb, filter);
521 if (!branch || branch->opcode == OP_BR_REG)
522 continue;
524 /* Find the compare instruction */
525 compare = mono_inst_prev (branch, filter);
526 if (!compare)
527 continue;
529 if (!MONO_IS_COND_BRANCH_OP (branch))
530 /* This can happen if a cond branch is optimized away */
531 continue;
533 /* FIXME: */
534 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
535 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
536 continue;
538 /* FIXME: */
539 /* ins->type might not be set */
540 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
541 continue;
543 /* FIXME: */
544 if (cfg->ret && ins1->dreg == cfg->ret->dreg)
545 continue;
547 if (!(cfg->opt & MONO_OPT_DEADCE))
549 * It is possible that dreg is never set before, so we can't use
550 * it as an sreg of the cmov instruction (#582322).
552 continue;
554 if (cfg->verbose_level > 2) {
555 printf ("\tBranch -> CMove optimization (2) in BB%d on\n", bb->block_num);
556 printf ("\t\t"); mono_print_ins (compare);
557 printf ("\t\t"); mono_print_ins (mono_inst_next (compare, filter));
558 printf ("\t\t"); mono_print_ins (ins1);
561 changed = TRUE;
563 //printf ("HIT!\n");
565 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
566 dreg = ins1->dreg;
568 /* Rewrite ins1 to emit to tmp_reg */
569 ins1->dreg = tmp_reg;
571 /* Remove ins1 from code_bb */
572 MONO_REMOVE_INS (code_bb, ins1);
574 /* Move ins1 before the comparison */
575 mono_bblock_insert_before_ins (bb, compare, ins1);
577 /* Add cmov instruction */
578 MONO_INST_NEW (cfg, cmov, OP_NOP);
579 cmov->dreg = dreg;
580 cmov->sreg1 = dreg;
581 cmov->sreg2 = tmp_reg;
582 cond = mono_opcode_to_cond (branch->opcode);
583 if (branch->inst_false_bb == code_bb)
584 cond = mono_negate_cond (cond);
585 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
586 case CMP_TYPE_I:
587 cmov->opcode = int_cmov_opcodes [cond];
588 break;
589 case CMP_TYPE_L:
590 cmov->opcode = long_cmov_opcodes [cond];
591 break;
592 default:
593 g_assert_not_reached ();
595 mono_bblock_insert_after_ins (bb, compare, cmov);
597 /* Rewrite the branch */
598 branch->opcode = OP_BR;
599 branch->inst_target_bb = next_bb;
600 mono_link_bblock (cfg, bb, branch->inst_target_bb);
602 /* Nullify the branch at the end of code_bb */
603 if (code_bb->code) {
604 branch = code_bb->code;
605 MONO_DELETE_INS (code_bb, branch);
608 /* Reorder bblocks */
609 mono_unlink_bblock (cfg, bb, code_bb);
610 mono_unlink_bblock (cfg, code_bb, next_bb);
612 /* Merge bb and its successor if possible */
613 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
614 (bb->region == bb->out_bb [0]->region)) {
615 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
618 * bbn might have fallen through to the next bb without a branch,
619 * have to add one now (#474718).
620 * FIXME: Maybe need to do this more generally in
621 * merge_basic_blocks () ?
623 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) && bb->out_count) {
624 MONO_INST_NEW (cfg, ins1, OP_BR);
625 ins1->inst_target_bb = bb->out_bb [0];
626 MONO_ADD_INS (bb, ins1);
628 goto restart;
634 * Optimize checks like: if (v < 0 || v > limit) by changing then to unsigned
635 * compares. This isn't really if conversion, but it easier to do here than in
636 * optimize_branches () since the IR is already optimized.
638 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
639 MonoBasicBlock *bb1, *bb2, *next_bb;
640 MonoInst *branch1, *branch2, *compare1, *ins, *next;
642 /* Look for the IR code generated from if (<var> < 0 || v > <limit>)
643 * after branch opts which is:
644 * BB:
645 * icompare_imm R [0]
646 * int_blt [BB1BB2]
647 * BB2:
648 * icompare_imm R [<limit>]
649 * int_ble [BB3BB1]
651 if (!(bb->out_count == 2 && !bb->extended))
652 continue;
654 bb1 = bb->out_bb [0];
655 bb2 = bb->out_bb [1];
657 // FIXME: Add more cases
659 /* Check structure */
660 if (!(bb1->in_count == 2 && bb1->in_bb [0] == bb && bb1->in_bb [1] == bb2 && bb2->in_count == 1 && bb2->out_count == 2))
661 continue;
663 next_bb = bb2;
665 /* Check first branch */
666 branch1 = mono_bb_last_inst (bb, filter);
667 if (!(branch1 && ((branch1->opcode == OP_IBLT) || (branch1->opcode == OP_LBLT)) && (branch1->inst_false_bb == next_bb)))
668 continue;
670 /* Check second branch */
671 branch2 = mono_bb_last_inst (next_bb, filter);
672 if (!branch2)
673 continue;
675 /* mcs sometimes generates inverted branches */
676 if (((branch2->opcode == OP_IBGT) || (branch2->opcode == OP_LBGT)) && branch2->inst_true_bb == branch1->inst_true_bb)
678 else if (((branch2->opcode == OP_IBLE) || (branch2->opcode == OP_LBLE)) && branch2->inst_false_bb == branch1->inst_true_bb)
680 else
681 continue;
683 /* Check first compare */
684 compare1 = mono_inst_prev (mono_bb_last_inst (bb, filter), filter);
685 if (!(compare1 && ((compare1->opcode == OP_ICOMPARE_IMM) || (compare1->opcode == OP_LCOMPARE_IMM)) && compare1->inst_imm == 0))
686 continue;
688 /* Check second bblock */
689 ins = mono_bb_first_inst (next_bb, filter);
690 if (!ins)
691 continue;
692 next = mono_inst_next (ins, filter);
693 if (((ins->opcode == OP_ICOMPARE_IMM) || (ins->opcode == OP_LCOMPARE_IMM)) && ins->sreg1 == compare1->sreg1 && next == branch2) {
694 /* The second arg must be positive */
695 if (ins->inst_imm < 0)
696 continue;
697 } else if (((ins->opcode == OP_LDLEN) || (ins->opcode == OP_STRLEN)) && ins->dreg != compare1->sreg1 && next && next->opcode == OP_ICOMPARE && next->sreg1 == compare1->sreg1 && next->sreg2 == ins->dreg && mono_inst_next (next, filter) == branch2) {
698 /* Another common case: if (index < 0 || index > arr.Length) */
699 } else {
700 continue;
703 if (cfg->verbose_level > 2) {
704 printf ("\tSigned->unsigned compare optimization in BB%d on\n", bb->block_num);
705 printf ("\t\t"); mono_print_ins (compare1);
706 printf ("\t\t"); mono_print_ins (mono_inst_next (compare1, filter));
707 printf ("\t\t"); mono_print_ins (ins);
710 /* Rewrite the first compare+branch */
711 MONO_DELETE_INS (bb, compare1);
712 branch1->opcode = OP_BR;
713 mono_unlink_bblock (cfg, bb, branch1->inst_true_bb);
714 mono_unlink_bblock (cfg, bb, branch1->inst_false_bb);
715 branch1->inst_target_bb = next_bb;
716 mono_link_bblock (cfg, bb, next_bb);
718 /* Rewrite the second branch */
719 branch2->opcode = br_to_br_un (branch2->opcode);
721 mono_merge_basic_blocks (cfg, bb, next_bb);
724 #if 0
725 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
726 MonoBasicBlock *bb1, *bb2;
727 MonoInst *prev, *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
728 gboolean simple, ret;
729 int dreg, tmp_reg;
730 CompType comp_type;
732 /* Look for the IR code generated from if (cond) <var> <- <a>
733 * after branch opts which is:
734 * BB:
735 * compare
736 * b<cond> [BB1]
737 * <var> <- <a>
738 * BB1:
740 if (!(bb->out_count == 1 && bb->extended && bb->code && bb->code->next && bb->code->next->next))
741 continue;
743 mono_print_bb (bb, "");
745 /* Find the compare instruction */
746 prev = NULL;
747 compare = bb->code;
748 g_assert (compare);
749 while (compare->next->next && compare->next->next != bb->last_ins) {
750 prev = compare;
751 compare = compare->next;
753 branch = compare->next;
754 if (!MONO_IS_COND_BRANCH_OP (branch))
755 continue;
757 #endif
759 if (changed) {
760 if (cfg->opt & MONO_OPT_BRANCH)
761 mono_optimize_branches (cfg);
762 /* Merging bblocks could make some variables local */
763 mono_handle_global_vregs (cfg);
764 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
765 mono_local_cprop (cfg);
766 if (cfg->opt & MONO_OPT_DEADCE)
767 mono_local_deadce (cfg);
769 #endif
772 void
773 mono_nullify_basic_block (MonoBasicBlock *bb)
775 bb->in_count = 0;
776 bb->out_count = 0;
777 bb->in_bb = NULL;
778 bb->out_bb = NULL;
779 bb->next_bb = NULL;
780 bb->code = bb->last_ins = NULL;
781 bb->cil_code = NULL;
784 static void
785 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
787 int i;
789 for (i = 0; i < bb->out_count; i++) {
790 MonoBasicBlock *ob = bb->out_bb [i];
791 if (ob == orig) {
792 if (!repl) {
793 if (bb->out_count > 1) {
794 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
796 bb->out_count--;
797 } else {
798 bb->out_bb [i] = repl;
804 static void
805 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
807 int i;
809 for (i = 0; i < bb->in_count; i++) {
810 MonoBasicBlock *ib = bb->in_bb [i];
811 if (ib == orig) {
812 if (!repl) {
813 if (bb->in_count > 1) {
814 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
816 bb->in_count--;
817 } else {
818 bb->in_bb [i] = repl;
824 static void
825 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
827 MonoInst *ins;
829 for (ins = bb->code; ins != NULL; ins = ins->next) {
830 switch (ins->opcode) {
831 case OP_BR:
832 if (ins->inst_target_bb == orig)
833 ins->inst_target_bb = repl;
834 break;
835 case OP_CALL_HANDLER:
836 if (ins->inst_target_bb == orig)
837 ins->inst_target_bb = repl;
838 break;
839 case OP_SWITCH: {
840 int i;
841 int n = GPOINTER_TO_INT (ins->klass);
842 for (i = 0; i < n; i++ ) {
843 if (ins->inst_many_bb [i] == orig)
844 ins->inst_many_bb [i] = repl;
846 break;
848 default:
849 if (MONO_IS_COND_BRANCH_OP (ins)) {
850 if (ins->inst_true_bb == orig)
851 ins->inst_true_bb = repl;
852 if (ins->inst_false_bb == orig)
853 ins->inst_false_bb = repl;
854 } else if (MONO_IS_JUMP_TABLE (ins)) {
855 int i;
856 MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)MONO_JUMP_TABLE_FROM_INS (ins);
857 for (i = 0; i < table->table_size; i++ ) {
858 if (table->table [i] == orig)
859 table->table [i] = repl;
863 break;
869 * Check if a bb is useless (is just made of NOPs and ends with an
870 * unconditional branch, or nothing).
871 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
872 * Otherwise, return FALSE;
874 static gboolean
875 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
876 MonoBasicBlock *target_bb = NULL;
877 MonoInst *inst;
879 /* Do not touch handlers */
880 if (bb->region != -1) {
881 bb->not_useless = TRUE;
882 return FALSE;
885 MONO_BB_FOR_EACH_INS (bb, inst) {
886 switch (inst->opcode) {
887 case OP_NOP:
888 case OP_IL_SEQ_POINT:
889 break;
890 case OP_BR:
891 target_bb = inst->inst_target_bb;
892 break;
893 default:
894 bb->not_useless = TRUE;
895 return FALSE;
899 if (target_bb == NULL) {
900 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
901 target_bb = bb->next_bb;
902 } else {
903 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
904 return FALSE;
908 /* Do not touch BBs following a switch (they are the "default" branch) */
909 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == OP_SWITCH)) {
910 return FALSE;
913 /* Do not touch BBs following the entry BB and jumping to something that is not */
914 /* thiry "next" bb (the entry BB cannot contain the branch) */
915 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
916 return FALSE;
920 * Do not touch BBs following a try block as the code in
921 * mini_method_compile needs them to compute the length of the try block.
923 if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
924 return FALSE;
926 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
927 if ((target_bb != NULL) && (target_bb != bb)) {
928 int i;
930 if (cfg->verbose_level > 1) {
931 printf ("remove_block_if_useless, removed BB%d\n", bb->block_num);
934 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
935 while (bb->in_count) {
936 MonoBasicBlock *in_bb = bb->in_bb [0];
937 mono_unlink_bblock (cfg, in_bb, bb);
938 mono_link_bblock (cfg, in_bb, target_bb);
939 replace_out_block_in_code (in_bb, bb, target_bb);
942 mono_unlink_bblock (cfg, bb, target_bb);
943 if (previous_bb != cfg->bb_entry && mono_bb_is_fall_through (cfg, previous_bb)) {
944 for (i = 0; i < previous_bb->out_count; i++) {
945 if (previous_bb->out_bb [i] == target_bb) {
946 MonoInst *jump;
947 MONO_INST_NEW (cfg, jump, OP_BR);
948 MONO_ADD_INS (previous_bb, jump);
949 jump->cil_code = previous_bb->cil_code;
950 jump->inst_target_bb = target_bb;
951 break;
956 previous_bb->next_bb = bb->next_bb;
957 mono_nullify_basic_block (bb);
959 return TRUE;
960 } else {
961 return FALSE;
965 void
966 mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn)
968 MonoInst *inst;
969 MonoBasicBlock *prev_bb;
970 int i;
972 /* There may be only one control flow edge between two BBs that we merge, and it should connect these BBs together. */
973 g_assert (bb->out_count == 1 && bbn->in_count == 1 && bb->out_bb [0] == bbn && bbn->in_bb [0] == bb);
975 bb->needs_decompose |= bbn->needs_decompose;
976 bb->extended |= bbn->extended;
978 mono_unlink_bblock (cfg, bb, bbn);
979 for (i = 0; i < bbn->out_count; ++i)
980 mono_link_bblock (cfg, bb, bbn->out_bb [i]);
981 while (bbn->out_count)
982 mono_unlink_bblock (cfg, bbn, bbn->out_bb [0]);
984 /* Handle the branch at the end of the bb */
985 if (bb->has_call_handler) {
986 for (inst = bb->code; inst != NULL; inst = inst->next) {
987 if (inst->opcode == OP_CALL_HANDLER) {
988 g_assert (inst->inst_target_bb == bbn);
989 NULLIFY_INS (inst);
993 if (bb->has_jump_table) {
994 for (inst = bb->code; inst != NULL; inst = inst->next) {
995 if (MONO_IS_JUMP_TABLE (inst)) {
996 int i;
997 MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)MONO_JUMP_TABLE_FROM_INS (inst);
998 for (i = 0; i < table->table_size; i++ ) {
999 /* Might be already NULL from a previous merge */
1000 if (table->table [i])
1001 g_assert (table->table [i] == bbn);
1002 table->table [i] = NULL;
1004 /* Can't nullify this as later instructions depend on it */
1008 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
1009 g_assert (bb->last_ins->inst_false_bb == bbn);
1010 bb->last_ins->inst_false_bb = NULL;
1011 bb->extended = TRUE;
1012 } else if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
1013 NULLIFY_INS (bb->last_ins);
1016 bb->has_call_handler |= bbn->has_call_handler;
1017 bb->has_jump_table |= bbn->has_jump_table;
1019 if (bb->last_ins) {
1020 if (bbn->code) {
1021 bb->last_ins->next = bbn->code;
1022 bbn->code->prev = bb->last_ins;
1023 bb->last_ins = bbn->last_ins;
1025 } else {
1026 bb->code = bbn->code;
1027 bb->last_ins = bbn->last_ins;
1031 /* Check if the control flow predecessor is also the linear IL predecessor. */
1032 if (bbn->in_bb [0]->next_bb == bbn)
1033 prev_bb = bbn->in_bb [0];
1034 else
1035 /* If it isn't, look for one among all basic blocks. */
1036 for (prev_bb = cfg->bb_entry; prev_bb && prev_bb->next_bb != bbn; prev_bb = prev_bb->next_bb)
1038 if (prev_bb) {
1039 prev_bb->next_bb = bbn->next_bb;
1040 } else {
1041 /* bbn might not be in the bb list yet */
1042 if (bb->next_bb == bbn)
1043 bb->next_bb = bbn->next_bb;
1045 mono_nullify_basic_block (bbn);
1048 * If bbn fell through to its next bblock, have to add a branch, since bb
1049 * will not fall though to the same bblock (#513931).
1051 if (bb->last_ins && bb->out_count == 1 && bb->out_bb [0] != bb->next_bb && !MONO_IS_BRANCH_OP (bb->last_ins)) {
1052 MONO_INST_NEW (cfg, inst, OP_BR);
1053 inst->inst_target_bb = bb->out_bb [0];
1054 MONO_ADD_INS (bb, inst);
1058 static void
1059 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
1061 MonoBasicBlock *bbn, *next;
1063 next = bb->next_bb;
1065 /* Find the previous */
1066 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
1068 if (bbn->next_bb) {
1069 bbn->next_bb = bb->next_bb;
1072 /* Find the last */
1073 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
1075 bbn->next_bb = bb;
1076 bb->next_bb = NULL;
1078 /* Add a branch */
1079 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))))) {
1080 MonoInst *ins;
1082 MONO_INST_NEW (cfg, ins, OP_BR);
1083 MONO_ADD_INS (bb, ins);
1084 mono_link_bblock (cfg, bb, next);
1085 ins->inst_target_bb = next;
1090 * mono_remove_block:
1092 * Remove BB from the control flow graph
1094 void
1095 mono_remove_bblock (MonoCompile *cfg, MonoBasicBlock *bb)
1097 MonoBasicBlock *tmp_bb;
1099 for (tmp_bb = cfg->bb_entry; tmp_bb && tmp_bb->next_bb != bb; tmp_bb = tmp_bb->next_bb)
1102 g_assert (tmp_bb);
1103 tmp_bb->next_bb = bb->next_bb;
1106 void
1107 mono_remove_critical_edges (MonoCompile *cfg)
1109 MonoBasicBlock *bb;
1110 MonoBasicBlock *previous_bb;
1112 if (cfg->verbose_level > 3) {
1113 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1114 int i;
1115 printf ("remove_critical_edges, BEFORE BB%d (in:", bb->block_num);
1116 for (i = 0; i < bb->in_count; i++) {
1117 printf (" %d", bb->in_bb [i]->block_num);
1119 printf (") (out:");
1120 for (i = 0; i < bb->out_count; i++) {
1121 printf (" %d", bb->out_bb [i]->block_num);
1123 printf (")");
1124 if (bb->last_ins != NULL) {
1125 printf (" ");
1126 mono_print_ins (bb->last_ins);
1128 printf ("\n");
1132 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
1133 if (bb->in_count > 1) {
1134 int in_bb_index;
1135 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
1136 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
1138 * Have to remove non-critical edges whose source ends with a BR_REG
1139 * ins too, since inserting a computation before the BR_REG could
1140 * overwrite the sreg1 of the ins.
1142 if ((in_bb->out_count > 1) || (in_bb->out_count == 1 && in_bb->last_ins && in_bb->last_ins->opcode == OP_BR_REG)) {
1143 MonoBasicBlock *new_bb = (MonoBasicBlock *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1144 new_bb->block_num = cfg->num_bblocks++;
1145 // new_bb->real_offset = bb->real_offset;
1146 new_bb->region = bb->region;
1148 /* Do not alter the CFG while altering the BB list */
1149 if (mono_bb_is_fall_through (cfg, previous_bb)) {
1150 if (previous_bb != cfg->bb_entry) {
1151 int i;
1152 /* Make sure previous_bb really falls through bb */
1153 for (i = 0; i < previous_bb->out_count; i++) {
1154 if (previous_bb->out_bb [i] == bb) {
1155 MonoInst *jump;
1156 MONO_INST_NEW (cfg, jump, OP_BR);
1157 MONO_ADD_INS (previous_bb, jump);
1158 jump->cil_code = previous_bb->cil_code;
1159 jump->inst_target_bb = bb;
1160 break;
1163 } else {
1164 /* We cannot add any inst to the entry BB, so we must */
1165 /* put a new BB in the middle to hold the OP_BR */
1166 MonoInst *jump;
1167 MonoBasicBlock *new_bb_after_entry = (MonoBasicBlock *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1168 new_bb_after_entry->block_num = cfg->num_bblocks++;
1169 // new_bb_after_entry->real_offset = bb->real_offset;
1170 new_bb_after_entry->region = bb->region;
1172 MONO_INST_NEW (cfg, jump, OP_BR);
1173 MONO_ADD_INS (new_bb_after_entry, jump);
1174 jump->cil_code = bb->cil_code;
1175 jump->inst_target_bb = bb;
1177 mono_unlink_bblock (cfg, previous_bb, bb);
1178 mono_link_bblock (cfg, new_bb_after_entry, bb);
1179 mono_link_bblock (cfg, previous_bb, new_bb_after_entry);
1181 previous_bb->next_bb = new_bb_after_entry;
1182 previous_bb = new_bb_after_entry;
1184 if (cfg->verbose_level > 2) {
1185 printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry->block_num, bb->block_num);
1190 /* Insert new_bb in the BB list */
1191 previous_bb->next_bb = new_bb;
1192 new_bb->next_bb = bb;
1193 previous_bb = new_bb;
1195 /* Setup in_bb and out_bb */
1196 new_bb->in_bb = (MonoBasicBlock **)mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1197 new_bb->in_bb [0] = in_bb;
1198 new_bb->in_count = 1;
1199 new_bb->out_bb = (MonoBasicBlock **)mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1200 new_bb->out_bb [0] = bb;
1201 new_bb->out_count = 1;
1203 /* Relink in_bb and bb to (from) new_bb */
1204 replace_out_block (in_bb, bb, new_bb);
1205 replace_out_block_in_code (in_bb, bb, new_bb);
1206 replace_in_block (bb, in_bb, new_bb);
1208 if (cfg->verbose_level > 2) {
1209 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);
1216 if (cfg->verbose_level > 3) {
1217 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1218 int i;
1219 printf ("remove_critical_edges, AFTER BB%d (in:", bb->block_num);
1220 for (i = 0; i < bb->in_count; i++) {
1221 printf (" %d", bb->in_bb [i]->block_num);
1223 printf (") (out:");
1224 for (i = 0; i < bb->out_count; i++) {
1225 printf (" %d", bb->out_bb [i]->block_num);
1227 printf (")");
1228 if (bb->last_ins != NULL) {
1229 printf (" ");
1230 mono_print_ins (bb->last_ins);
1232 printf ("\n");
1238 * Optimizes the branches on the Control Flow Graph
1241 void
1242 mono_optimize_branches (MonoCompile *cfg)
1244 int i, count = 0, changed = FALSE;
1245 MonoBasicBlock *bb, *bbn;
1246 guint32 niterations;
1247 MonoInst *bbn_first_inst;
1248 int filter = FILTER_IL_SEQ_POINT;
1251 * Some crazy loops could cause the code below to go into an infinite
1252 * loop, see bug #53003 for an example. To prevent this, we put an upper
1253 * bound on the number of iterations.
1255 if (cfg->num_bblocks > 1000)
1256 niterations = cfg->num_bblocks * 2;
1257 else
1258 niterations = 1000;
1260 do {
1261 MonoBasicBlock *previous_bb;
1262 changed = FALSE;
1263 niterations --;
1265 /* we skip the entry block (exit is handled specially instead ) */
1266 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
1267 count ++;
1268 if (count == 1000) {
1269 mono_threads_safepoint ();
1270 count = 0;
1272 /* dont touch code inside exception clauses */
1273 if (bb->region != -1)
1274 continue;
1276 if (!bb->not_useless && remove_block_if_useless (cfg, bb, previous_bb)) {
1277 changed = TRUE;
1278 continue;
1281 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1282 if (cfg->verbose_level > 2)
1283 g_print ("nullify block triggered %d\n", bbn->block_num);
1285 bb->next_bb = bbn->next_bb;
1287 for (i = 0; i < bbn->out_count; i++)
1288 replace_in_block (bbn->out_bb [i], bbn, NULL);
1290 mono_nullify_basic_block (bbn);
1291 changed = TRUE;
1294 if (bb->out_count == 1) {
1295 bbn = bb->out_bb [0];
1297 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
1298 if (bb->last_ins && (bb->last_ins->opcode != OP_BR) && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
1299 bb->last_ins->opcode = OP_BR;
1300 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
1301 changed = TRUE;
1302 if (cfg->verbose_level > 2)
1303 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
1306 if (bb->region == bbn->region && bb->next_bb == bbn) {
1307 /* the block are in sequence anyway ... */
1309 /* branches to the following block can be removed */
1310 if (bb->last_ins && bb->last_ins->opcode == OP_BR && !bbn->out_of_line) {
1311 NULLIFY_INS (bb->last_ins);
1312 changed = TRUE;
1313 if (cfg->verbose_level > 2)
1314 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
1317 if (bbn->in_count == 1 && !bb->extended) {
1318 if (bbn != cfg->bb_exit) {
1319 if (cfg->verbose_level > 2)
1320 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
1321 mono_merge_basic_blocks (cfg, bb, bbn);
1322 changed = TRUE;
1323 continue;
1326 //mono_print_bb_code (bb);
1331 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1332 if (cfg->verbose_level > 2) {
1333 g_print ("nullify block triggered %d\n", bbn->block_num);
1335 bb->next_bb = bbn->next_bb;
1337 for (i = 0; i < bbn->out_count; i++)
1338 replace_in_block (bbn->out_bb [i], bbn, NULL);
1340 mono_nullify_basic_block (bbn);
1341 changed = TRUE;
1342 continue;
1345 if (bb->out_count == 1) {
1346 bbn = bb->out_bb [0];
1348 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
1349 bbn = bb->last_ins->inst_target_bb;
1350 bbn_first_inst = mono_bb_first_inst (bbn, filter);
1351 if (bb->region == bbn->region && bbn_first_inst && bbn_first_inst->opcode == OP_BR &&
1352 bbn_first_inst->inst_target_bb != bbn &&
1353 bbn_first_inst->inst_target_bb->region == bb->region) {
1355 if (cfg->verbose_level > 2)
1356 g_print ("branch to branch triggered %d -> %d -> %d\n", bb->block_num, bbn->block_num, bbn_first_inst->inst_target_bb->block_num);
1358 replace_in_block (bbn, bb, NULL);
1359 replace_out_block (bb, bbn, bbn_first_inst->inst_target_bb);
1360 mono_link_bblock (cfg, bb, bbn_first_inst->inst_target_bb);
1361 bb->last_ins->inst_target_bb = bbn_first_inst->inst_target_bb;
1362 changed = TRUE;
1363 continue;
1366 } else if (bb->out_count == 2) {
1367 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1368 int branch_result;
1369 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
1371 if (bb->last_ins->flags & MONO_INST_CFOLD_TAKEN)
1372 branch_result = BRANCH_TAKEN;
1373 else if (bb->last_ins->flags & MONO_INST_CFOLD_NOT_TAKEN)
1374 branch_result = BRANCH_NOT_TAKEN;
1375 else
1376 branch_result = BRANCH_UNDEF;
1378 if (branch_result == BRANCH_TAKEN) {
1379 taken_branch_target = bb->last_ins->inst_true_bb;
1380 untaken_branch_target = bb->last_ins->inst_false_bb;
1381 } else if (branch_result == BRANCH_NOT_TAKEN) {
1382 taken_branch_target = bb->last_ins->inst_false_bb;
1383 untaken_branch_target = bb->last_ins->inst_true_bb;
1385 if (taken_branch_target) {
1386 /* if mono_eval_cond_branch () is ever taken to handle
1387 * non-constant values to compare, issue a pop here.
1389 bb->last_ins->opcode = OP_BR;
1390 bb->last_ins->inst_target_bb = taken_branch_target;
1391 if (!bb->extended)
1392 mono_unlink_bblock (cfg, bb, untaken_branch_target);
1393 changed = TRUE;
1394 continue;
1396 bbn = bb->last_ins->inst_true_bb;
1397 bbn_first_inst = mono_bb_first_inst (bbn, filter);
1398 if (bb->region == bbn->region && bbn_first_inst && bbn_first_inst->opcode == OP_BR &&
1399 bbn_first_inst->inst_target_bb->region == bb->region) {
1400 if (cfg->verbose_level > 2)
1401 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
1402 bb->block_num, bbn->block_num, bbn_first_inst->inst_target_bb->block_num,
1403 bbn_first_inst->opcode);
1406 * Unlink, then relink bblocks to avoid various
1407 * tricky situations when the two targets of the branch
1408 * are equal, or will become equal after the change.
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_true_bb = bbn_first_inst->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);
1418 changed = TRUE;
1419 continue;
1422 bbn = bb->last_ins->inst_false_bb;
1423 bbn_first_inst = mono_bb_first_inst (bbn, filter);
1424 if (bbn && bb->region == bbn->region && bbn_first_inst && bbn_first_inst->opcode == OP_BR &&
1425 bbn_first_inst->inst_target_bb->region == bb->region) {
1426 if (cfg->verbose_level > 2)
1427 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
1428 bb->block_num, bbn->block_num, bbn_first_inst->inst_target_bb->block_num,
1429 bbn_first_inst->opcode);
1431 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1432 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1434 bb->last_ins->inst_false_bb = bbn_first_inst->inst_target_bb;
1436 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1437 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1439 changed = TRUE;
1440 continue;
1443 bbn = bb->last_ins->inst_false_bb;
1445 * If bb is an extended bb, it could contain an inside branch to bbn.
1446 * FIXME: Enable the optimization if that is not true.
1447 * If bblocks_linked () is true, then merging bb and bbn
1448 * would require addition of an extra branch at the end of bbn
1449 * slowing down loops.
1451 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)) {
1452 g_assert (bbn->in_bb [0] == bb);
1453 if (cfg->verbose_level > 2)
1454 g_print ("merge false branch target triggered BB%d -> BB%d\n", bb->block_num, bbn->block_num);
1455 mono_merge_basic_blocks (cfg, bb, bbn);
1456 changed = TRUE;
1457 continue;
1461 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1462 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) {
1463 /* Reverse the branch */
1464 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
1465 bbn = bb->last_ins->inst_false_bb;
1466 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
1467 bb->last_ins->inst_true_bb = bbn;
1469 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
1470 if (cfg->verbose_level > 2)
1471 g_print ("cbranch to throw block triggered %d.\n",
1472 bb->block_num);
1477 } while (changed && (niterations > 0));
1480 #else /* !DISABLE_JIT */
1482 MONO_EMPTY_SOURCE_FILE (branch_opts);
1484 #endif /* !DISABLE_JIT */