Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / mono / mini / branch-opts.c
blobff4de4e3d2c1952a1d684ee61ec16eaa113c2e25
1 /*
2 * branch-opts.c: Branch optimizations support
4 * Authors:
5 * Patrik Torstensson (Patrik.Torstesson at gmail.com)
7 * (C) 2005 Ximian, Inc. http://www.ximian.com
8 */
9 #include "mini.h"
11 #ifndef DISABLE_JIT
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.
18 static gboolean
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).
32 MonoInst *
33 mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname)
35 MonoMethod *method = cfg->method;
36 MonoMethodHeader *header = mono_method_get_header (method);
37 MonoExceptionClause *clause;
38 MonoClass *exclass;
39 int i;
41 if (!(cfg->opt & MONO_OPT_EXCEPTION))
42 return NULL;
44 if (bb->region == -1 || !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
45 return NULL;
47 exclass = mono_class_from_name (mono_get_corlib (), "System", exname);
48 /* search for the handler */
49 for (i = 0; i < header->num_clauses; ++i) {
50 clause = &header->clauses [i];
51 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset)) {
52 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE && clause->data.catch_class && mono_class_is_assignable_from (clause->data.catch_class, exclass)) {
53 MonoBasicBlock *tbb;
55 /* get the basic block for the handler and
56 * check if the exception object is used.
57 * Flag is set during method_to_ir due to
58 * pop-op is optmized away in codegen (burg).
60 tbb = cfg->cil_offset_to_bb [clause->handler_offset];
61 if (tbb && tbb->flags & BB_EXCEPTION_DEAD_OBJ && !(tbb->flags & BB_EXCEPTION_UNSAFE)) {
62 MonoBasicBlock *targetbb = tbb;
63 gboolean unsafe = FALSE;
65 /* Check if this catch clause is ok to optimize by
66 * looking for the BB_EXCEPTION_UNSAFE in every BB that
67 * belongs to the same region.
69 * UNSAFE flag is set during method_to_ir (OP_RETHROW)
71 while (!unsafe && tbb->next_bb && tbb->region == tbb->next_bb->region) {
72 if (tbb->next_bb->flags & BB_EXCEPTION_UNSAFE) {
73 unsafe = TRUE;
74 break;
76 tbb = tbb->next_bb;
79 if (!unsafe) {
80 MonoInst *jump;
82 /* Create dummy inst to allow easier integration in
83 * arch dependent code (opcode ignored)
85 MONO_INST_NEW (cfg, jump, OP_BR);
87 /* Allocate memory for our branch target */
88 jump->inst_i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
89 jump->inst_true_bb = targetbb;
91 if (cfg->verbose_level > 2)
92 g_print ("found exception to optimize - returning branch to BB%d (%s) (instead of throw) for method %s:%s\n", targetbb->block_num, clause->data.catch_class->name, cfg->method->klass->name, cfg->method->name);
94 return jump;
97 return NULL;
99 } else {
100 /* Branching to an outer clause could skip inner clauses */
101 return NULL;
106 return NULL;
109 static const int int_cmov_opcodes [] = {
110 OP_CMOV_IEQ,
111 OP_CMOV_INE_UN,
112 OP_CMOV_ILE,
113 OP_CMOV_IGE,
114 OP_CMOV_ILT,
115 OP_CMOV_IGT,
116 OP_CMOV_ILE_UN,
117 OP_CMOV_IGE_UN,
118 OP_CMOV_ILT_UN,
119 OP_CMOV_IGT_UN
122 static const int long_cmov_opcodes [] = {
123 OP_CMOV_LEQ,
124 OP_CMOV_LNE_UN,
125 OP_CMOV_LLE,
126 OP_CMOV_LGE,
127 OP_CMOV_LLT,
128 OP_CMOV_LGT,
129 OP_CMOV_LLE_UN,
130 OP_CMOV_LGE_UN,
131 OP_CMOV_LLT_UN,
132 OP_CMOV_LGT_UN
135 static int
136 br_to_br_un (int opcode)
138 switch (opcode) {
139 case OP_IBGT:
140 return OP_IBGT_UN;
141 break;
142 case OP_IBLE:
143 return OP_IBLE_UN;
144 break;
145 case OP_LBGT:
146 return OP_LBGT_UN;
147 break;
148 case OP_LBLE:
149 return OP_LBLE_UN;
150 break;
151 default:
152 g_assert_not_reached ();
153 return -1;
158 * mono_replace_ins:
160 * Replace INS with its decomposition which is stored in a series of bblocks starting
161 * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS.
162 * On return, it will be set to the last ins of the decomposition.
164 void
165 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
167 MonoInst *next = ins->next;
169 if (next && next->opcode == OP_NOP) {
170 /* Avoid NOPs following branches */
171 ins->next = next->next;
172 next = next->next;
175 if (first_bb == last_bb) {
177 * Only one replacement bb, merge the code into
178 * the current bb.
181 /* Delete links between the first_bb and its successors */
182 while (first_bb->out_count)
183 mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
185 /* Head */
186 if (*prev) {
187 (*prev)->next = first_bb->code;
188 first_bb->code->prev = (*prev);
189 } else {
190 bb->code = first_bb->code;
193 /* Tail */
194 last_bb->last_ins->next = next;
195 if (next)
196 next->prev = last_bb->last_ins;
197 else
198 bb->last_ins = last_bb->last_ins;
199 *prev = last_bb->last_ins;
200 bb->has_array_access |= first_bb->has_array_access;
201 } else {
202 int i, count;
203 MonoBasicBlock **tmp_bblocks, *tmp;
204 MonoInst *last;
206 /* Multiple BBs */
208 /* Set region */
209 for (tmp = first_bb; tmp; tmp = tmp->next_bb)
210 tmp->region = bb->region;
212 /* Split the original bb */
213 if (ins->next)
214 ins->next->prev = NULL;
215 ins->next = NULL;
216 bb->last_ins = ins;
218 /* Merge the second part of the original bb into the last bb */
219 if (last_bb->last_ins) {
220 last_bb->last_ins->next = next;
221 if (next)
222 next->prev = last_bb->last_ins;
223 } else {
224 last_bb->code = next;
226 last_bb->has_array_access |= bb->has_array_access;
228 if (next) {
229 for (last = next; last->next != NULL; last = last->next)
231 last_bb->last_ins = last;
234 for (i = 0; i < bb->out_count; ++i)
235 mono_link_bblock (cfg, last_bb, bb->out_bb [i]);
237 /* Merge the first (dummy) bb to the original bb */
238 if (*prev) {
239 (*prev)->next = first_bb->code;
240 first_bb->code->prev = (*prev);
241 } else {
242 bb->code = first_bb->code;
244 bb->last_ins = first_bb->last_ins;
245 bb->has_array_access |= first_bb->has_array_access;
247 /* Delete the links between the original bb and its successors */
248 tmp_bblocks = bb->out_bb;
249 count = bb->out_count;
250 for (i = 0; i < count; ++i)
251 mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
253 /* Add links between the original bb and the first_bb's successors */
254 for (i = 0; i < first_bb->out_count; ++i) {
255 MonoBasicBlock *out_bb = first_bb->out_bb [i];
257 mono_link_bblock (cfg, bb, out_bb);
259 /* Delete links between the first_bb and its successors */
260 for (i = 0; i < bb->out_count; ++i) {
261 MonoBasicBlock *out_bb = bb->out_bb [i];
263 mono_unlink_bblock (cfg, first_bb, out_bb);
265 last_bb->next_bb = bb->next_bb;
266 bb->next_bb = first_bb->next_bb;
268 *prev = NULL;
272 void
273 mono_if_conversion (MonoCompile *cfg)
275 #ifdef MONO_ARCH_HAVE_CMOV_OPS
276 MonoBasicBlock *bb;
277 gboolean changed = FALSE;
279 if (!(cfg->opt & MONO_OPT_CMOV))
280 return;
282 // FIXME: Make this work with extended bblocks
285 * This pass requires somewhat optimized IR code so it should be run after
286 * local cprop/deadce. Also, it should be run before dominator computation, since
287 * it changes control flow.
289 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
290 MonoBasicBlock *bb1, *bb2;
292 restart:
293 /* Look for the IR code generated from cond ? a : b
294 * which is:
295 * BB:
296 * b<cond> [BB1BB2]
297 * BB1:
298 * <var> <- <a>
299 * br BB3
300 * BB2:
301 * <var> <- <b>
302 * br BB3
304 if (!(bb->out_count == 2 && !bb->extended))
305 continue;
307 bb1 = bb->out_bb [0];
308 bb2 = bb->out_bb [1];
310 if (bb1->in_count == 1 && bb2->in_count == 1 && bb1->out_count == 1 && bb2->out_count == 1 && bb1->out_bb [0] == bb2->out_bb [0]) {
311 MonoInst *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
312 MonoBasicBlock *true_bb, *false_bb;
313 gboolean simple, ret;
314 int dreg, tmp_reg;
315 CompType comp_type;
317 if (bb->last_ins && (bb->last_ins->opcode == OP_BR_REG || bb->last_ins->opcode == OP_BR))
318 continue;
320 /* Find the compare instruction */
321 if (!bb->last_ins || !bb->last_ins->prev)
322 continue;
323 branch = bb->last_ins;
324 compare = branch->prev;
326 if (!MONO_IS_COND_BRANCH_OP (branch))
327 /* This can happen if a cond branch is optimized away */
328 continue;
330 true_bb = branch->inst_true_bb;
331 false_bb = branch->inst_false_bb;
334 * Check that bb1 and bb2 are 'simple' and both assign to the same
335 * variable.
337 /* FIXME: Get rid of the nops earlier */
338 ins1 = true_bb->code;
339 while (ins1 && ins1->opcode == OP_NOP)
340 ins1 = ins1->next;
341 ins2 = false_bb->code;
342 while (ins2 && ins2->opcode == OP_NOP)
343 ins2 = ins2->next;
344 if (!(ins1 && ins2 && ins1->dreg == ins2->dreg && ins1->dreg != -1))
345 continue;
347 simple = TRUE;
348 for (tmp = ins1->next; tmp; tmp = tmp->next)
349 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
350 simple = FALSE;
352 for (tmp = ins2->next; tmp; tmp = tmp->next)
353 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
354 simple = FALSE;
356 if (!simple)
357 continue;
359 /* We move ins1/ins2 before the compare so they should have no side effect */
360 if (!(MONO_INS_HAS_NO_SIDE_EFFECT (ins1) && MONO_INS_HAS_NO_SIDE_EFFECT (ins2)))
361 continue;
363 /* Moving ins1/ins2 could change the comparison */
364 /* FIXME: */
365 if (!((compare->sreg1 != ins1->dreg) && (compare->sreg2 != ins1->dreg)))
366 continue;
368 /* FIXME: */
369 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
370 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
371 continue;
373 /* FIXME: */
374 /* ins->type might not be set */
375 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
376 continue;
378 if (cfg->verbose_level > 2) {
379 printf ("\tBranch -> CMove optimization in BB%d on\n", bb->block_num);
380 printf ("\t\t"); mono_print_ins (compare);
381 printf ("\t\t"); mono_print_ins (compare->next);
382 printf ("\t\t"); mono_print_ins (ins1);
383 printf ("\t\t"); mono_print_ins (ins2);
386 changed = TRUE;
388 //printf ("HIT!\n");
390 /* Assignments to the return register must remain at the end of bbs */
391 if (cfg->ret)
392 ret = ins1->dreg == cfg->ret->dreg;
393 else
394 ret = FALSE;
396 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
397 dreg = ins1->dreg;
399 /* Rewrite ins1 to emit to tmp_reg */
400 ins1->dreg = tmp_reg;
402 if (ret) {
403 dreg = mono_alloc_dreg (cfg, STACK_I4);
404 ins2->dreg = dreg;
407 /* Remove ins1/ins2 from bb1/bb2 */
408 MONO_REMOVE_INS (true_bb, ins1);
409 MONO_REMOVE_INS (false_bb, ins2);
411 /* Move ins1 and ins2 before the comparison */
412 /* ins1 comes first to avoid ins1 overwriting an argument of ins2 */
413 mono_bblock_insert_before_ins (bb, compare, ins2);
414 mono_bblock_insert_before_ins (bb, ins2, ins1);
416 /* Add cmov instruction */
417 MONO_INST_NEW (cfg, cmov, OP_NOP);
418 cmov->dreg = dreg;
419 cmov->sreg1 = dreg;
420 cmov->sreg2 = tmp_reg;
421 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
422 case CMP_TYPE_I:
423 cmov->opcode = int_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
424 break;
425 case CMP_TYPE_L:
426 cmov->opcode = long_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
427 break;
428 default:
429 g_assert_not_reached ();
431 mono_bblock_insert_after_ins (bb, compare, cmov);
433 if (ret) {
434 /* Add an extra move */
435 MONO_INST_NEW (cfg, move, OP_MOVE);
436 move->dreg = cfg->ret->dreg;
437 move->sreg1 = dreg;
438 mono_bblock_insert_after_ins (bb, cmov, move);
441 /* Rewrite the branch */
442 branch->opcode = OP_BR;
443 branch->inst_target_bb = true_bb->out_bb [0];
444 mono_link_bblock (cfg, bb, branch->inst_target_bb);
446 /* Reorder bblocks */
447 mono_unlink_bblock (cfg, bb, true_bb);
448 mono_unlink_bblock (cfg, bb, false_bb);
449 mono_unlink_bblock (cfg, true_bb, true_bb->out_bb [0]);
450 mono_unlink_bblock (cfg, false_bb, false_bb->out_bb [0]);
451 mono_remove_bblock (cfg, true_bb);
452 mono_remove_bblock (cfg, false_bb);
454 /* Merge bb and its successor if possible */
455 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
456 (bb->region == bb->out_bb [0]->region)) {
457 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
458 goto restart;
462 /* Look for the IR code generated from if (cond) <var> <- <a>
463 * which is:
464 * BB:
465 * b<cond> [BB1BB2]
466 * BB1:
467 * <var> <- <a>
468 * br BB2
471 if ((bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) ||
472 (bb1->in_count == 1 && bb1->out_count == 1 && bb1->out_bb [0] == bb2)) {
473 MonoInst *compare, *branch, *ins1, *cmov, *tmp;
474 gboolean simple;
475 int dreg, tmp_reg;
476 CompType comp_type;
477 CompRelation cond;
478 MonoBasicBlock *next_bb, *code_bb;
480 /* code_bb is the bblock containing code, next_bb is the successor bblock */
481 if (bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) {
482 code_bb = bb2;
483 next_bb = bb1;
484 } else {
485 code_bb = bb1;
486 next_bb = bb2;
489 ins1 = code_bb->code;
491 if (!ins1)
492 continue;
494 /* Check that code_bb is simple */
495 simple = TRUE;
496 for (tmp = ins1->next; tmp; tmp = tmp->next)
497 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
498 simple = FALSE;
500 if (!simple)
501 continue;
503 /* We move ins1 before the compare so it should have no side effect */
504 if (!MONO_INS_HAS_NO_SIDE_EFFECT (ins1))
505 continue;
507 if (bb->last_ins && bb->last_ins->opcode == OP_BR_REG)
508 continue;
510 /* Find the compare instruction */
512 if (!bb->last_ins || !bb->last_ins->prev)
513 continue;
514 branch = bb->last_ins;
515 compare = branch->prev;
517 if (!MONO_IS_COND_BRANCH_OP (branch))
518 /* This can happen if a cond branch is optimized away */
519 continue;
521 /* FIXME: */
522 comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
523 if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
524 continue;
526 /* FIXME: */
527 /* ins->type might not be set */
528 if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
529 continue;
531 /* FIXME: */
532 if (cfg->ret && ins1->dreg == cfg->ret->dreg)
533 continue;
535 if (cfg->verbose_level > 2) {
536 printf ("\tBranch -> CMove optimization (2) in BB%d on\n", bb->block_num);
537 printf ("\t\t"); mono_print_ins (compare);
538 printf ("\t\t"); mono_print_ins (compare->next);
539 printf ("\t\t"); mono_print_ins (ins1);
542 changed = TRUE;
544 //printf ("HIT!\n");
546 tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
547 dreg = ins1->dreg;
549 /* Rewrite ins1 to emit to tmp_reg */
550 ins1->dreg = tmp_reg;
552 /* Remove ins1 from code_bb */
553 MONO_REMOVE_INS (code_bb, ins1);
555 /* Move ins1 before the comparison */
556 mono_bblock_insert_before_ins (bb, compare, ins1);
558 /* Add cmov instruction */
559 MONO_INST_NEW (cfg, cmov, OP_NOP);
560 cmov->dreg = dreg;
561 cmov->sreg1 = dreg;
562 cmov->sreg2 = tmp_reg;
563 cond = mono_opcode_to_cond (branch->opcode);
564 if (branch->inst_false_bb == code_bb)
565 cond = mono_negate_cond (cond);
566 switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
567 case CMP_TYPE_I:
568 cmov->opcode = int_cmov_opcodes [cond];
569 break;
570 case CMP_TYPE_L:
571 cmov->opcode = long_cmov_opcodes [cond];
572 break;
573 default:
574 g_assert_not_reached ();
576 mono_bblock_insert_after_ins (bb, compare, cmov);
578 /* Rewrite the branch */
579 branch->opcode = OP_BR;
580 branch->inst_target_bb = next_bb;
581 mono_link_bblock (cfg, bb, branch->inst_target_bb);
583 /* Nullify the branch at the end of code_bb */
584 if (code_bb->code) {
585 branch = code_bb->code;
586 MONO_DELETE_INS (code_bb, branch);
589 /* Reorder bblocks */
590 mono_unlink_bblock (cfg, bb, code_bb);
591 mono_unlink_bblock (cfg, code_bb, next_bb);
593 /* Merge bb and its successor if possible */
594 if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
595 (bb->region == bb->out_bb [0]->region)) {
596 mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
599 * bbn might have fallen through to the next bb without a branch,
600 * have to add one now (#474718).
601 * FIXME: Maybe need to do this more generally in
602 * merge_basic_blocks () ?
604 if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) && bb->out_count) {
605 MONO_INST_NEW (cfg, ins1, OP_BR);
606 ins1->inst_target_bb = bb->out_bb [0];
607 MONO_ADD_INS (bb, ins1);
609 goto restart;
615 * Optimize checks like: if (v < 0 || v > limit) by changing then to unsigned
616 * compares. This isn't really if conversion, but it easier to do here than in
617 * optimize_branches () since the IR is already optimized.
619 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
620 MonoBasicBlock *bb1, *bb2, *true_bb, *false_bb, *next_bb;
621 MonoInst *branch1, *branch2, *compare1, *ins;
623 /* Look for the IR code generated from if (<var> < 0 || v > <limit>)
624 * after branch opts which is:
625 * BB:
626 * icompare_imm R [0]
627 * int_blt [BB1BB2]
628 * BB2:
629 * icompare_imm R [<limit>]
630 * int_ble [BB3BB1]
632 if (!(bb->out_count == 2 && !bb->extended))
633 continue;
635 bb1 = bb->out_bb [0];
636 bb2 = bb->out_bb [1];
638 // FIXME: Add more cases
640 /* Check structure */
641 if (!(bb1->in_count == 2 && bb1->in_bb [0] == bb && bb1->in_bb [1] == bb2 && bb2->in_count == 1 && bb2->out_count == 2))
642 continue;
644 next_bb = bb2;
646 /* Check first branch */
647 branch1 = bb->last_ins;
648 if (!(branch1 && ((branch1->opcode == OP_IBLT) || (branch1->opcode == OP_LBLT)) && (branch1->inst_false_bb == next_bb)))
649 continue;
651 true_bb = branch1->inst_true_bb;
653 /* Check second branch */
654 branch2 = next_bb->last_ins;
655 if (!branch2)
656 continue;
658 /* mcs sometimes generates inverted branches */
659 if (((branch2->opcode == OP_IBGT) || (branch2->opcode == OP_LBGT)) && branch2->inst_true_bb == branch1->inst_true_bb)
660 false_bb = branch2->inst_false_bb;
661 else if (((branch2->opcode == OP_IBLE) || (branch2->opcode == OP_LBLE)) && branch2->inst_false_bb == branch1->inst_true_bb)
662 false_bb = branch2->inst_true_bb;
663 else
664 continue;
666 /* Check first compare */
667 compare1 = bb->last_ins->prev;
668 if (!(compare1 && ((compare1->opcode == OP_ICOMPARE_IMM) || (compare1->opcode == OP_LCOMPARE_IMM)) && compare1->inst_imm == 0))
669 continue;
671 /* Check second bblock */
672 ins = next_bb->code;
673 if (!ins)
674 continue;
675 if (((ins->opcode == OP_ICOMPARE_IMM) || (ins->opcode == OP_LCOMPARE_IMM)) && ins->sreg1 == compare1->sreg1 && ins->next == branch2) {
676 /* The second arg must be positive */
677 if (ins->inst_imm < 0)
678 continue;
679 } else if (((ins->opcode == OP_LDLEN) || (ins->opcode == OP_STRLEN)) && ins->dreg != compare1->sreg1 && ins->next && ins->next->opcode == OP_ICOMPARE && ins->next->sreg1 == compare1->sreg1 && ins->next->sreg2 == ins->dreg && ins->next->next == branch2) {
680 /* Another common case: if (index < 0 || index > arr.Length) */
681 } else {
682 continue;
685 if (cfg->verbose_level > 2) {
686 printf ("\tSigned->unsigned compare optimization in BB%d on\n", bb->block_num);
687 printf ("\t\t"); mono_print_ins (compare1);
688 printf ("\t\t"); mono_print_ins (compare1->next);
689 printf ("\t\t"); mono_print_ins (ins);
692 /* Rewrite the first compare+branch */
693 MONO_DELETE_INS (bb, compare1);
694 branch1->opcode = OP_BR;
695 mono_unlink_bblock (cfg, bb, branch1->inst_true_bb);
696 mono_unlink_bblock (cfg, bb, branch1->inst_false_bb);
697 branch1->inst_target_bb = next_bb;
698 mono_link_bblock (cfg, bb, next_bb);
700 /* Rewrite the second branch */
701 branch2->opcode = br_to_br_un (branch2->opcode);
703 mono_merge_basic_blocks (cfg, bb, next_bb);
706 #if 0
707 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
708 MonoBasicBlock *bb1, *bb2;
709 MonoInst *prev, *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
710 gboolean simple, ret;
711 int dreg, tmp_reg;
712 CompType comp_type;
714 /* Look for the IR code generated from if (cond) <var> <- <a>
715 * after branch opts which is:
716 * BB:
717 * compare
718 * b<cond> [BB1]
719 * <var> <- <a>
720 * BB1:
722 if (!(bb->out_count == 1 && bb->extended && bb->code && bb->code->next && bb->code->next->next))
723 continue;
725 mono_print_bb (bb, "");
727 /* Find the compare instruction */
728 prev = NULL;
729 compare = bb->code;
730 g_assert (compare);
731 while (compare->next->next && compare->next->next != bb->last_ins) {
732 prev = compare;
733 compare = compare->next;
735 branch = compare->next;
736 if (!MONO_IS_COND_BRANCH_OP (branch))
737 continue;
739 #endif
741 if (changed) {
742 if (cfg->opt & MONO_OPT_BRANCH)
743 mono_optimize_branches (cfg);
744 /* Merging bblocks could make some variables local */
745 mono_handle_global_vregs (cfg);
746 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
747 mono_local_cprop (cfg);
748 mono_local_deadce (cfg);
750 #endif
753 void
754 mono_nullify_basic_block (MonoBasicBlock *bb)
756 bb->in_count = 0;
757 bb->out_count = 0;
758 bb->in_bb = NULL;
759 bb->out_bb = NULL;
760 bb->next_bb = NULL;
761 bb->code = bb->last_ins = NULL;
762 bb->cil_code = NULL;
765 static void
766 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
768 int i;
770 for (i = 0; i < bb->out_count; i++) {
771 MonoBasicBlock *ob = bb->out_bb [i];
772 if (ob == orig) {
773 if (!repl) {
774 if (bb->out_count > 1) {
775 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
777 bb->out_count--;
778 } else {
779 bb->out_bb [i] = repl;
785 static void
786 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
788 int i;
790 for (i = 0; i < bb->in_count; i++) {
791 MonoBasicBlock *ib = bb->in_bb [i];
792 if (ib == orig) {
793 if (!repl) {
794 if (bb->in_count > 1) {
795 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
797 bb->in_count--;
798 } else {
799 bb->in_bb [i] = repl;
805 static void
806 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
807 MonoInst *ins;
809 for (ins = bb->code; ins != NULL; ins = ins->next) {
810 switch (ins->opcode) {
811 case OP_BR:
812 if (ins->inst_target_bb == orig)
813 ins->inst_target_bb = repl;
814 break;
815 case OP_CALL_HANDLER:
816 if (ins->inst_target_bb == orig)
817 ins->inst_target_bb = repl;
818 break;
819 case OP_SWITCH: {
820 int i;
821 int n = GPOINTER_TO_INT (ins->klass);
822 for (i = 0; i < n; i++ ) {
823 if (ins->inst_many_bb [i] == orig)
824 ins->inst_many_bb [i] = repl;
826 break;
828 default:
829 if (MONO_IS_COND_BRANCH_OP (ins)) {
830 if (ins->inst_true_bb == orig)
831 ins->inst_true_bb = repl;
832 if (ins->inst_false_bb == orig)
833 ins->inst_false_bb = repl;
834 } else if (MONO_IS_JUMP_TABLE (ins)) {
835 int i;
836 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
837 for (i = 0; i < table->table_size; i++ ) {
838 if (table->table [i] == orig)
839 table->table [i] = repl;
843 break;
849 * Check if a bb is useless (is just made of NOPs and ends with an
850 * unconditional branch, or nothing).
851 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
852 * Otherwise, return FALSE;
854 static gboolean
855 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
856 MonoBasicBlock *target_bb = NULL;
857 MonoInst *inst;
859 /* Do not touch handlers */
860 if (bb->region != -1) {
861 bb->not_useless = TRUE;
862 return FALSE;
865 MONO_BB_FOR_EACH_INS (bb, inst) {
866 switch (inst->opcode) {
867 case OP_NOP:
868 break;
869 case OP_BR:
870 target_bb = inst->inst_target_bb;
871 break;
872 default:
873 bb->not_useless = TRUE;
874 return FALSE;
878 if (target_bb == NULL) {
879 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
880 target_bb = bb->next_bb;
881 } else {
882 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
883 return FALSE;
887 /* Do not touch BBs following a switch (they are the "default" branch) */
888 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == OP_SWITCH)) {
889 return FALSE;
892 /* Do not touch BBs following the entry BB and jumping to something that is not */
893 /* thiry "next" bb (the entry BB cannot contain the branch) */
894 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
895 return FALSE;
899 * Do not touch BBs following a try block as the code in
900 * mini_method_compile needs them to compute the length of the try block.
902 if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
903 return FALSE;
905 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
906 if ((target_bb != NULL) && (target_bb != bb)) {
907 int i;
909 if (cfg->verbose_level > 1) {
910 printf ("remove_block_if_useless, removed BB%d\n", bb->block_num);
913 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
914 while (bb->in_count) {
915 MonoBasicBlock *in_bb = bb->in_bb [0];
916 mono_unlink_bblock (cfg, in_bb, bb);
917 mono_link_bblock (cfg, in_bb, target_bb);
918 replace_out_block_in_code (in_bb, bb, target_bb);
921 mono_unlink_bblock (cfg, bb, target_bb);
922 if (previous_bb != cfg->bb_entry && mono_bb_is_fall_through (cfg, previous_bb)) {
923 for (i = 0; i < previous_bb->out_count; i++) {
924 if (previous_bb->out_bb [i] == target_bb) {
925 MonoInst *jump;
926 MONO_INST_NEW (cfg, jump, OP_BR);
927 MONO_ADD_INS (previous_bb, jump);
928 jump->cil_code = previous_bb->cil_code;
929 jump->inst_target_bb = target_bb;
930 break;
935 previous_bb->next_bb = bb->next_bb;
936 mono_nullify_basic_block (bb);
938 return TRUE;
939 } else {
940 return FALSE;
944 void
945 mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn)
947 MonoInst *inst;
948 MonoBasicBlock *prev_bb;
949 int i;
951 bb->has_array_access |= bbn->has_array_access;
952 bb->extended |= bbn->extended;
954 mono_unlink_bblock (cfg, bb, bbn);
955 for (i = 0; i < bbn->out_count; ++i)
956 mono_link_bblock (cfg, bb, bbn->out_bb [i]);
957 while (bbn->out_count)
958 mono_unlink_bblock (cfg, bbn, bbn->out_bb [0]);
960 /* Handle the branch at the end of the bb */
961 for (inst = bb->code; inst != NULL; inst = inst->next) {
962 if (inst->opcode == OP_CALL_HANDLER) {
963 g_assert (inst->inst_target_bb == bbn);
964 NULLIFY_INS (inst);
966 if (MONO_IS_JUMP_TABLE (inst)) {
967 int i;
968 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (inst);
969 for (i = 0; i < table->table_size; i++ ) {
970 /* Might be already NULL from a previous merge */
971 if (table->table [i])
972 g_assert (table->table [i] == bbn);
973 table->table [i] = NULL;
975 /* Can't nullify this as later instructions depend on it */
978 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
979 g_assert (bb->last_ins->inst_false_bb == bbn);
980 bb->last_ins->inst_false_bb = NULL;
981 bb->extended = TRUE;
982 } else if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
983 NULLIFY_INS (bb->last_ins);
986 if (bb->last_ins) {
987 if (bbn->code) {
988 bb->last_ins->next = bbn->code;
989 bbn->code->prev = bb->last_ins;
990 bb->last_ins = bbn->last_ins;
992 } else {
993 bb->code = bbn->code;
994 bb->last_ins = bbn->last_ins;
997 for (prev_bb = cfg->bb_entry; prev_bb && prev_bb->next_bb != bbn; prev_bb = prev_bb->next_bb)
999 if (prev_bb) {
1000 prev_bb->next_bb = bbn->next_bb;
1001 } else {
1002 /* bbn might not be in the bb list yet */
1003 if (bb->next_bb == bbn)
1004 bb->next_bb = bbn->next_bb;
1006 mono_nullify_basic_block (bbn);
1009 static void
1010 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
1012 MonoBasicBlock *bbn, *next;
1014 next = bb->next_bb;
1016 /* Find the previous */
1017 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
1019 if (bbn->next_bb) {
1020 bbn->next_bb = bb->next_bb;
1023 /* Find the last */
1024 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
1026 bbn->next_bb = bb;
1027 bb->next_bb = NULL;
1029 /* Add a branch */
1030 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))))) {
1031 MonoInst *ins;
1033 MONO_INST_NEW (cfg, ins, OP_BR);
1034 MONO_ADD_INS (bb, ins);
1035 mono_link_bblock (cfg, bb, next);
1036 ins->inst_target_bb = next;
1041 * mono_remove_block:
1043 * Remove BB from the control flow graph
1045 void
1046 mono_remove_bblock (MonoCompile *cfg, MonoBasicBlock *bb)
1048 MonoBasicBlock *tmp_bb;
1050 for (tmp_bb = cfg->bb_entry; tmp_bb && tmp_bb->next_bb != bb; tmp_bb = tmp_bb->next_bb)
1053 g_assert (tmp_bb);
1054 tmp_bb->next_bb = bb->next_bb;
1057 void
1058 mono_remove_critical_edges (MonoCompile *cfg)
1060 MonoBasicBlock *bb;
1061 MonoBasicBlock *previous_bb;
1063 if (cfg->verbose_level > 3) {
1064 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1065 int i;
1066 printf ("remove_critical_edges, BEFORE BB%d (in:", bb->block_num);
1067 for (i = 0; i < bb->in_count; i++) {
1068 printf (" %d", bb->in_bb [i]->block_num);
1070 printf (") (out:");
1071 for (i = 0; i < bb->out_count; i++) {
1072 printf (" %d", bb->out_bb [i]->block_num);
1074 printf (")");
1075 if (bb->last_ins != NULL) {
1076 printf (" ");
1077 mono_print_ins (bb->last_ins);
1079 printf ("\n");
1083 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
1084 if (bb->in_count > 1) {
1085 int in_bb_index;
1086 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
1087 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
1089 * Have to remove non-critical edges whose source ends with a BR_REG
1090 * ins too, since inserting a computation before the BR_REG could
1091 * overwrite the sreg1 of the ins.
1093 if ((in_bb->out_count > 1) || (in_bb->out_count == 1 && in_bb->last_ins && in_bb->last_ins->opcode == OP_BR_REG)) {
1094 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1095 new_bb->block_num = cfg->num_bblocks++;
1096 // new_bb->real_offset = bb->real_offset;
1097 new_bb->region = bb->region;
1099 /* Do not alter the CFG while altering the BB list */
1100 if (mono_bb_is_fall_through (cfg, previous_bb)) {
1101 if (previous_bb != cfg->bb_entry) {
1102 int i;
1103 /* Make sure previous_bb really falls through bb */
1104 for (i = 0; i < previous_bb->out_count; i++) {
1105 if (previous_bb->out_bb [i] == bb) {
1106 MonoInst *jump;
1107 MONO_INST_NEW (cfg, jump, OP_BR);
1108 MONO_ADD_INS (previous_bb, jump);
1109 jump->cil_code = previous_bb->cil_code;
1110 jump->inst_target_bb = bb;
1111 break;
1114 } else {
1115 /* We cannot add any inst to the entry BB, so we must */
1116 /* put a new BB in the middle to hold the OP_BR */
1117 MonoInst *jump;
1118 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1119 new_bb_after_entry->block_num = cfg->num_bblocks++;
1120 // new_bb_after_entry->real_offset = bb->real_offset;
1121 new_bb_after_entry->region = bb->region;
1123 MONO_INST_NEW (cfg, jump, OP_BR);
1124 MONO_ADD_INS (new_bb_after_entry, jump);
1125 jump->cil_code = bb->cil_code;
1126 jump->inst_target_bb = bb;
1128 mono_unlink_bblock (cfg, previous_bb, bb);
1129 mono_link_bblock (cfg, new_bb_after_entry, bb);
1130 mono_link_bblock (cfg, previous_bb, new_bb_after_entry);
1132 previous_bb->next_bb = new_bb_after_entry;
1133 previous_bb = new_bb_after_entry;
1135 if (cfg->verbose_level > 2) {
1136 printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry->block_num, bb->block_num);
1141 /* Insert new_bb in the BB list */
1142 previous_bb->next_bb = new_bb;
1143 new_bb->next_bb = bb;
1144 previous_bb = new_bb;
1146 /* Setup in_bb and out_bb */
1147 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1148 new_bb->in_bb [0] = in_bb;
1149 new_bb->in_count = 1;
1150 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1151 new_bb->out_bb [0] = bb;
1152 new_bb->out_count = 1;
1154 /* Relink in_bb and bb to (from) new_bb */
1155 replace_out_block (in_bb, bb, new_bb);
1156 replace_out_block_in_code (in_bb, bb, new_bb);
1157 replace_in_block (bb, in_bb, new_bb);
1159 if (cfg->verbose_level > 2) {
1160 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);
1167 if (cfg->verbose_level > 3) {
1168 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1169 int i;
1170 printf ("remove_critical_edges, AFTER BB%d (in:", bb->block_num);
1171 for (i = 0; i < bb->in_count; i++) {
1172 printf (" %d", bb->in_bb [i]->block_num);
1174 printf (") (out:");
1175 for (i = 0; i < bb->out_count; i++) {
1176 printf (" %d", bb->out_bb [i]->block_num);
1178 printf (")");
1179 if (bb->last_ins != NULL) {
1180 printf (" ");
1181 mono_print_ins (bb->last_ins);
1183 printf ("\n");
1189 * Optimizes the branches on the Control Flow Graph
1192 void
1193 mono_optimize_branches (MonoCompile *cfg)
1195 int i, changed = FALSE;
1196 MonoBasicBlock *bb, *bbn;
1197 guint32 niterations;
1200 * Some crazy loops could cause the code below to go into an infinite
1201 * loop, see bug #53003 for an example. To prevent this, we put an upper
1202 * bound on the number of iterations.
1204 if (cfg->num_bblocks > 1000)
1205 niterations = cfg->num_bblocks * 2;
1206 else
1207 niterations = 1000;
1209 do {
1210 MonoBasicBlock *previous_bb;
1211 changed = FALSE;
1212 niterations --;
1214 /* we skip the entry block (exit is handled specially instead ) */
1215 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
1216 /* dont touch code inside exception clauses */
1217 if (bb->region != -1)
1218 continue;
1220 if (!bb->not_useless && remove_block_if_useless (cfg, bb, previous_bb)) {
1221 changed = TRUE;
1222 continue;
1225 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1226 if (cfg->verbose_level > 2)
1227 g_print ("nullify block triggered %d\n", bbn->block_num);
1229 bb->next_bb = bbn->next_bb;
1231 for (i = 0; i < bbn->out_count; i++)
1232 replace_in_block (bbn->out_bb [i], bbn, NULL);
1234 mono_nullify_basic_block (bbn);
1235 changed = TRUE;
1238 if (bb->out_count == 1) {
1239 bbn = bb->out_bb [0];
1241 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
1242 if (bb->last_ins && (bb->last_ins->opcode != OP_BR) && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
1243 bb->last_ins->opcode = OP_BR;
1244 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
1245 changed = TRUE;
1246 if (cfg->verbose_level > 2)
1247 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
1250 if (bb->region == bbn->region && bb->next_bb == bbn) {
1251 /* the block are in sequence anyway ... */
1253 /* branches to the following block can be removed */
1254 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
1255 bb->last_ins->opcode = OP_NOP;
1256 changed = TRUE;
1257 if (cfg->verbose_level > 2)
1258 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
1261 if (bbn->in_count == 1 && !bb->extended) {
1262 if (bbn != cfg->bb_exit) {
1263 if (cfg->verbose_level > 2)
1264 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
1265 mono_merge_basic_blocks (cfg, bb, bbn);
1266 changed = TRUE;
1267 continue;
1270 //mono_print_bb_code (bb);
1275 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1276 if (cfg->verbose_level > 2) {
1277 g_print ("nullify block triggered %d\n", bbn->block_num);
1279 bb->next_bb = bbn->next_bb;
1281 for (i = 0; i < bbn->out_count; i++)
1282 replace_in_block (bbn->out_bb [i], bbn, NULL);
1284 mono_nullify_basic_block (bbn);
1285 changed = TRUE;
1286 continue;
1289 if (bb->out_count == 1) {
1290 bbn = bb->out_bb [0];
1292 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
1293 bbn = bb->last_ins->inst_target_bb;
1294 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1295 bbn->code->inst_target_bb != bbn &&
1296 bbn->code->inst_target_bb->region == bb->region) {
1298 if (cfg->verbose_level > 2)
1299 g_print ("branch to branch triggered %d -> %d -> %d\n", bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
1301 replace_in_block (bbn, bb, NULL);
1302 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
1303 mono_link_bblock (cfg, bb, bbn->code->inst_target_bb);
1304 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
1305 changed = TRUE;
1306 continue;
1309 } else if (bb->out_count == 2) {
1310 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1311 int branch_result;
1312 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
1314 if (bb->last_ins->flags & MONO_INST_CFOLD_TAKEN)
1315 branch_result = BRANCH_TAKEN;
1316 else if (bb->last_ins->flags & MONO_INST_CFOLD_NOT_TAKEN)
1317 branch_result = BRANCH_NOT_TAKEN;
1318 else
1319 branch_result = BRANCH_UNDEF;
1321 if (branch_result == BRANCH_TAKEN) {
1322 taken_branch_target = bb->last_ins->inst_true_bb;
1323 untaken_branch_target = bb->last_ins->inst_false_bb;
1324 } else if (branch_result == BRANCH_NOT_TAKEN) {
1325 taken_branch_target = bb->last_ins->inst_false_bb;
1326 untaken_branch_target = bb->last_ins->inst_true_bb;
1328 if (taken_branch_target) {
1329 /* if mono_eval_cond_branch () is ever taken to handle
1330 * non-constant values to compare, issue a pop here.
1332 bb->last_ins->opcode = OP_BR;
1333 bb->last_ins->inst_target_bb = taken_branch_target;
1334 if (!bb->extended)
1335 mono_unlink_bblock (cfg, bb, untaken_branch_target);
1336 changed = TRUE;
1337 continue;
1339 bbn = bb->last_ins->inst_true_bb;
1340 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1341 bbn->code->inst_target_bb->region == bb->region) {
1342 if (cfg->verbose_level > 2)
1343 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
1344 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
1345 bbn->code->opcode);
1348 * Unlink, then relink bblocks to avoid various
1349 * tricky situations when the two targets of the branch
1350 * are equal, or will become equal after the change.
1352 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1353 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1355 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
1357 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1358 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1360 changed = TRUE;
1361 continue;
1364 bbn = bb->last_ins->inst_false_bb;
1365 if (bbn && bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1366 bbn->code->inst_target_bb->region == bb->region) {
1367 if (cfg->verbose_level > 2)
1368 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
1369 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
1370 bbn->code->opcode);
1372 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1373 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1375 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
1377 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1378 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1380 changed = TRUE;
1381 continue;
1384 bbn = bb->last_ins->inst_false_bb;
1386 * If bb is an extended bb, it could contain an inside branch to bbn.
1387 * FIXME: Enable the optimization if that is not true.
1388 * If bblocks_linked () is true, then merging bb and bbn
1389 * would require addition of an extra branch at the end of bbn
1390 * slowing down loops.
1392 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)) {
1393 g_assert (bbn->in_bb [0] == bb);
1394 if (cfg->verbose_level > 2)
1395 g_print ("merge false branch target triggered BB%d -> BB%d\n", bb->block_num, bbn->block_num);
1396 mono_merge_basic_blocks (cfg, bb, bbn);
1397 changed = TRUE;
1398 continue;
1402 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1403 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)) {
1404 /* Reverse the branch */
1405 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
1406 bbn = bb->last_ins->inst_false_bb;
1407 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
1408 bb->last_ins->inst_true_bb = bbn;
1410 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
1411 if (cfg->verbose_level > 2)
1412 g_print ("cbranch to throw block triggered %d.\n",
1413 bb->block_num);
1418 } while (changed && (niterations > 0));
1421 #endif /* DISABLE_JIT */