[sockets] Fixed reading blocking flag
[mono.git] / mono / mini / branch-opts.c
1 /*
2  * branch-opts.c: Branch optimizations support 
3  *
4  * Authors:
5  *   Patrik Torstensson (Patrik.Torstesson at gmail.com)
6  *
7  * (C) 2005 Ximian, Inc.  http://www.ximian.com
8  */
9  #include "mini.h"
10
11 #ifndef DISABLE_JIT
12  
13
14 /*
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. 
17  */
18 static gboolean
19 mono_bb_is_fall_through (MonoCompile *cfg, MonoBasicBlock *bb)
20 {
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*/
23 }
24
25 /*
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).
30  *
31  */
32 MonoInst *
33 mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname)
34 {
35         MonoMethodHeader *header = cfg->header;
36         MonoExceptionClause *clause;
37         MonoClass *exclass;
38         int i;
39
40         if (!(cfg->opt & MONO_OPT_EXCEPTION))
41                 return NULL;
42
43         if (bb->region == -1 || !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
44                 return NULL;
45
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)) {
52                                 MonoBasicBlock *tbb;
53
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).
58                                  */
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;
63
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. 
67                                          *
68                                          * UNSAFE flag is set during method_to_ir (OP_RETHROW)
69                                          */
70                                         while (!unsafe && tbb->next_bb && tbb->region == tbb->next_bb->region) {
71                                                 if (tbb->next_bb->flags & BB_EXCEPTION_UNSAFE)  {
72                                                         unsafe = TRUE;
73                                                         break;
74                                                 }
75                                                 tbb = tbb->next_bb;
76                                         }
77
78                                         if (!unsafe) {
79                                                 MonoInst *jump;
80
81                                                 /* Create dummy inst to allow easier integration in
82                                                  * arch dependent code (opcode ignored)
83                                                  */
84                                                 MONO_INST_NEW (cfg, jump, OP_BR);
85
86                                                 /* Allocate memory for our branch target */
87                                                 jump->inst_i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
88                                                 jump->inst_true_bb = targetbb;
89
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);
92
93                                                 return jump;
94                                         } 
95
96                                         return NULL;
97                                 } else {
98                                         /* Branching to an outer clause could skip inner clauses */
99                                         return NULL;
100                                 }
101                         } else {
102                                 /* Branching to an outer clause could skip inner clauses */
103                                 return NULL;
104                         }
105                 }
106         }
107
108         return NULL;
109 }
110
111 static const int int_cmov_opcodes [] = {
112         OP_CMOV_IEQ,
113         OP_CMOV_INE_UN,
114         OP_CMOV_ILE,
115         OP_CMOV_IGE,
116         OP_CMOV_ILT,
117         OP_CMOV_IGT,
118         OP_CMOV_ILE_UN,
119         OP_CMOV_IGE_UN,
120         OP_CMOV_ILT_UN,
121         OP_CMOV_IGT_UN
122 };
123
124 static const int long_cmov_opcodes [] = {
125         OP_CMOV_LEQ,
126         OP_CMOV_LNE_UN,
127         OP_CMOV_LLE,
128         OP_CMOV_LGE,
129         OP_CMOV_LLT,
130         OP_CMOV_LGT,
131         OP_CMOV_LLE_UN,
132         OP_CMOV_LGE_UN,
133         OP_CMOV_LLT_UN,
134         OP_CMOV_LGT_UN
135 };
136
137 static int
138 br_to_br_un (int opcode)
139 {
140         switch (opcode) {
141         case OP_IBGT:
142                 return OP_IBGT_UN;
143                 break;
144         case OP_IBLE:
145                 return OP_IBLE_UN;
146                 break;
147         case OP_LBGT:
148                 return OP_LBGT_UN;
149                 break;
150         case OP_LBLE:
151                 return OP_LBLE_UN;
152                 break;
153         default:
154                 g_assert_not_reached ();
155                 return -1;
156         }
157 }
158
159 /**
160  * mono_replace_ins:
161  *
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.
165  */
166 void
167 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
168 {
169         MonoInst *next = ins->next;
170
171         if (next && next->opcode == OP_NOP) {
172                 /* Avoid NOPs following branches */
173                 ins->next = next->next;
174                 next = next->next;
175         }
176
177         if (first_bb == last_bb) {
178                 /* 
179                  * Only one replacement bb, merge the code into
180                  * the current bb.
181                  */
182
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]);
186
187                 /* Head */
188                 if (*prev) {
189                         (*prev)->next = first_bb->code;
190                         first_bb->code->prev = (*prev);
191                 } else {
192                         bb->code = first_bb->code;
193                 }
194
195                 /* Tail */
196                 last_bb->last_ins->next = next;
197                 if (next)
198                         next->prev = last_bb->last_ins;
199                 else
200                         bb->last_ins = last_bb->last_ins;
201                 *prev = last_bb->last_ins;
202                 bb->has_array_access |= first_bb->has_array_access;
203         } else {
204                 int i, count;
205                 MonoBasicBlock **tmp_bblocks, *tmp;
206                 MonoInst *last;
207
208                 /* Multiple BBs */
209
210                 /* Set region */
211                 for (tmp = first_bb; tmp; tmp = tmp->next_bb)
212                         tmp->region = bb->region;
213
214                 /* Split the original bb */
215                 if (ins->next)
216                         ins->next->prev = NULL;
217                 ins->next = NULL;
218                 bb->last_ins = ins;
219
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;
223                         if (next)
224                                 next->prev = last_bb->last_ins;
225                 } else {
226                         last_bb->code = next;
227                 }
228                 last_bb->has_array_access |= bb->has_array_access;
229
230                 if (next) {
231                         for (last = next; last->next != NULL; last = last->next)
232                                 ;
233                         last_bb->last_ins = last;
234                 }
235
236                 for (i = 0; i < bb->out_count; ++i)
237                         mono_link_bblock (cfg, last_bb, bb->out_bb [i]);
238
239                 /* Merge the first (dummy) bb to the original bb */
240                 if (*prev) {
241                         (*prev)->next = first_bb->code;
242                         first_bb->code->prev = (*prev);
243                 } else {
244                         bb->code = first_bb->code;
245                 }
246                 bb->last_ins = first_bb->last_ins;
247                 bb->has_array_access |= first_bb->has_array_access;
248
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]);
254
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];
258
259                         mono_link_bblock (cfg, bb, out_bb);
260                 }
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];
264
265                         mono_unlink_bblock (cfg, first_bb, out_bb);
266                 }
267                 last_bb->next_bb = bb->next_bb;
268                 bb->next_bb = first_bb->next_bb;
269
270                 *prev = NULL;
271         }
272 }
273
274 void
275 mono_if_conversion (MonoCompile *cfg)
276 {
277 #ifdef MONO_ARCH_HAVE_CMOV_OPS
278         MonoBasicBlock *bb;
279         gboolean changed = FALSE;
280
281         if (!(cfg->opt & MONO_OPT_CMOV))
282                 return;
283
284         // FIXME: Make this work with extended bblocks
285
286         /* 
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.
290          */
291         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
292                 MonoBasicBlock *bb1, *bb2;
293
294         restart:
295                 /* Look for the IR code generated from cond ? a : b
296                  * which is:
297                  * BB:
298                  * b<cond> [BB1BB2]
299                  * BB1:
300                  * <var> <- <a>
301                  * br BB3
302                  * BB2:
303                  * <var> <- <b>
304                  * br BB3
305                  */
306                 if (!(bb->out_count == 2 && !bb->extended))
307                         continue;
308
309                 bb1 = bb->out_bb [0];
310                 bb2 = bb->out_bb [1];
311
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;
316                         int dreg, tmp_reg;
317                         CompType comp_type;
318
319                         if (bb->last_ins && (bb->last_ins->opcode == OP_BR_REG || bb->last_ins->opcode == OP_BR))
320                                 continue;
321
322                         /* Find the compare instruction */
323                         if (!bb->last_ins || !bb->last_ins->prev)
324                                 continue;
325                         branch = bb->last_ins;
326                         compare = branch->prev;
327
328                         if (!MONO_IS_COND_BRANCH_OP (branch))
329                                 /* This can happen if a cond branch is optimized away */
330                                 continue;
331
332                         true_bb = branch->inst_true_bb;
333                         false_bb = branch->inst_false_bb;
334
335                         /* 
336                          * Check that bb1 and bb2 are 'simple' and both assign to the same
337                          * variable.
338                          */
339                         /* FIXME: Get rid of the nops earlier */
340                         ins1 = true_bb->code;
341                         while (ins1 && ins1->opcode == OP_NOP)
342                                 ins1 = ins1->next;
343                         ins2 = false_bb->code;
344                         while (ins2 && ins2->opcode == OP_NOP)
345                                 ins2 = ins2->next;
346                         if (!(ins1 && ins2 && ins1->dreg == ins2->dreg && ins1->dreg != -1))
347                                 continue;
348
349                         simple = TRUE;
350                         for (tmp = ins1->next; tmp; tmp = tmp->next)
351                                 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
352                                         simple = FALSE;
353                                         
354                         for (tmp = ins2->next; tmp; tmp = tmp->next)
355                                 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
356                                         simple = FALSE;
357
358                         if (!simple)
359                                 continue;
360
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)))
363                                 continue;
364
365                         /* Moving ins1/ins2 could change the comparison */
366                         /* FIXME: */
367                         if (!((compare->sreg1 != ins1->dreg) && (compare->sreg2 != ins1->dreg)))
368                                 continue;
369
370                         /* FIXME: */
371                         comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
372                         if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
373                                 continue;
374
375                         /* FIXME: */
376                         /* ins->type might not be set */
377                         if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
378                                 continue;
379
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);
386                         }
387
388                         changed = TRUE;
389
390                         //printf ("HIT!\n");
391
392                         /* Assignments to the return register must remain at the end of bbs */
393                         if (cfg->ret)
394                                 ret = ins1->dreg == cfg->ret->dreg;
395                         else
396                                 ret = FALSE;
397
398                         tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
399                         dreg = ins1->dreg;
400
401                         /* Rewrite ins1 to emit to tmp_reg */
402                         ins1->dreg = tmp_reg;
403
404                         if (ret) {
405                                 dreg = mono_alloc_dreg (cfg, STACK_I4);
406                                 ins2->dreg = dreg;
407                         }
408
409                         /* Remove ins1/ins2 from bb1/bb2 */
410                         MONO_REMOVE_INS (true_bb, ins1);
411                         MONO_REMOVE_INS (false_bb, ins2);
412
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);
417
418                         /* Add cmov instruction */
419                         MONO_INST_NEW (cfg, cmov, OP_NOP);
420                         cmov->dreg = dreg;
421                         cmov->sreg1 = dreg;
422                         cmov->sreg2 = tmp_reg;
423                         switch (mono_opcode_to_type (branch->opcode, compare->opcode)) {
424                         case CMP_TYPE_I:
425                                 cmov->opcode = int_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
426                                 break;
427                         case CMP_TYPE_L:
428                                 cmov->opcode = long_cmov_opcodes [mono_opcode_to_cond (branch->opcode)];
429                                 break;
430                         default:
431                                 g_assert_not_reached ();
432                         }
433                         mono_bblock_insert_after_ins (bb, compare, cmov);
434
435                         if (ret) {
436                                 /* Add an extra move */
437                                 MONO_INST_NEW (cfg, move, OP_MOVE);
438                                 move->dreg = cfg->ret->dreg;
439                                 move->sreg1 = dreg;
440                                 mono_bblock_insert_after_ins (bb, cmov, move);
441                         }
442
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);
447
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);
455
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]);
460                                 goto restart;
461                         }
462                 }
463
464                 /* Look for the IR code generated from if (cond) <var> <- <a>
465                  * which is:
466                  * BB:
467                  * b<cond> [BB1BB2]
468                  * BB1:
469                  * <var> <- <a>
470                  * br BB2
471                  */
472
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;
476                         gboolean simple;
477                         int dreg, tmp_reg;
478                         CompType comp_type;
479                         CompRelation cond;
480                         MonoBasicBlock *next_bb, *code_bb;
481
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) {
484                                 code_bb = bb2;
485                                 next_bb = bb1;
486                         } else {
487                                 code_bb = bb1;
488                                 next_bb = bb2;
489                         }
490
491                         ins1 = code_bb->code;
492
493                         if (!ins1)
494                                 continue;
495
496                         /* Check that code_bb is simple */
497                         simple = TRUE;
498                         for (tmp = ins1->next; tmp; tmp = tmp->next)
499                                 if (!((tmp->opcode == OP_NOP) || (tmp->opcode == OP_BR)))
500                                         simple = FALSE;
501
502                         if (!simple)
503                                 continue;
504
505                         /* We move ins1 before the compare so it should have no side effect */
506                         if (!MONO_INS_HAS_NO_SIDE_EFFECT (ins1))
507                                 continue;
508
509                         if (bb->last_ins && bb->last_ins->opcode == OP_BR_REG)
510                                 continue;
511
512                         /* Find the compare instruction */
513
514                         if (!bb->last_ins || !bb->last_ins->prev)
515                                 continue;
516                         branch = bb->last_ins;
517                         compare = branch->prev;
518
519                         if (!MONO_IS_COND_BRANCH_OP (branch))
520                                 /* This can happen if a cond branch is optimized away */
521                                 continue;
522
523                         /* FIXME: */
524                         comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
525                         if (!((comp_type == CMP_TYPE_I) || (comp_type == CMP_TYPE_L)))
526                                 continue;
527
528                         /* FIXME: */
529                         /* ins->type might not be set */
530                         if (INS_INFO (ins1->opcode) [MONO_INST_DEST] != 'i')
531                                 continue;
532
533                         /* FIXME: */
534                         if (cfg->ret && ins1->dreg == cfg->ret->dreg)
535                                 continue;
536
537                         if (!(cfg->opt & MONO_OPT_DEADCE))
538                                 /* 
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).
541                                  */
542                                 continue;
543
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);
549                         }
550
551                         changed = TRUE;
552
553                         //printf ("HIT!\n");
554
555                         tmp_reg = mono_alloc_dreg (cfg, STACK_I4);
556                         dreg = ins1->dreg;
557
558                         /* Rewrite ins1 to emit to tmp_reg */
559                         ins1->dreg = tmp_reg;
560
561                         /* Remove ins1 from code_bb */
562                         MONO_REMOVE_INS (code_bb, ins1);
563
564                         /* Move ins1 before the comparison */
565                         mono_bblock_insert_before_ins (bb, compare, ins1);
566
567                         /* Add cmov instruction */
568                         MONO_INST_NEW (cfg, cmov, OP_NOP);
569                         cmov->dreg = dreg;
570                         cmov->sreg1 = dreg;
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)) {
576                         case CMP_TYPE_I:
577                                 cmov->opcode = int_cmov_opcodes [cond];
578                                 break;
579                         case CMP_TYPE_L:
580                                 cmov->opcode = long_cmov_opcodes [cond];
581                                 break;
582                         default:
583                                 g_assert_not_reached ();
584                         }
585                         mono_bblock_insert_after_ins (bb, compare, cmov);
586
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);
591
592                         /* Nullify the branch at the end of code_bb */
593                         if (code_bb->code) {
594                                 branch = code_bb->code;
595                                 MONO_DELETE_INS (code_bb, branch);
596                         }
597
598                         /* Reorder bblocks */
599                         mono_unlink_bblock (cfg, bb, code_bb);
600                         mono_unlink_bblock (cfg, code_bb, next_bb);
601
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]);
606
607                                 /* 
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 () ?
612                                  */
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);
617                                 }
618                                 goto restart;
619                         }
620                 }
621         }
622
623         /*
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.
627          */
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;
631
632                 /* Look for the IR code generated from if (<var> < 0 || v > <limit>)
633                  * after branch opts which is:
634                  * BB:
635                  * icompare_imm R [0]
636                  * int_blt [BB1BB2]
637                  * BB2:
638                  * icompare_imm R [<limit>]
639                  * int_ble [BB3BB1]
640                  */
641                 if (!(bb->out_count == 2 && !bb->extended))
642                         continue;
643
644                 bb1 = bb->out_bb [0];
645                 bb2 = bb->out_bb [1];
646
647                 // FIXME: Add more cases
648
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))
651                         continue;
652
653                 next_bb = bb2;
654
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)))
658                         continue;
659
660                 true_bb = branch1->inst_true_bb;
661
662                 /* Check second branch */
663                 branch2 = next_bb->last_ins;
664                 if (!branch2)
665                         continue;
666
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;
672                 else
673                         continue;
674
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))
678                         continue;
679
680                 /* Check second bblock */
681                 ins = next_bb->code;
682                 if (!ins)
683                         continue;
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)
687                                 continue;
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) */
690                 } else {
691                         continue;
692                 }
693
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);
699                 }
700
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);            
708
709                 /* Rewrite the second branch */
710                 branch2->opcode = br_to_br_un (branch2->opcode);
711
712                 mono_merge_basic_blocks (cfg, bb, next_bb);
713         }
714
715 #if 0
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;
720                 int dreg, tmp_reg;
721                 CompType comp_type;
722
723                 /* Look for the IR code generated from if (cond) <var> <- <a>
724                  * after branch opts which is:
725                  * BB:
726                  * compare
727                  * b<cond> [BB1]
728                  * <var> <- <a>
729                  * BB1:
730                  */
731                 if (!(bb->out_count == 1 && bb->extended && bb->code && bb->code->next && bb->code->next->next))
732                         continue;
733
734                 mono_print_bb (bb, "");
735
736                 /* Find the compare instruction */
737                 prev = NULL;
738                 compare = bb->code;
739                 g_assert (compare);
740                 while (compare->next->next && compare->next->next != bb->last_ins) {
741                         prev = compare;
742                         compare = compare->next;
743                 }
744                 branch = compare->next;
745                 if (!MONO_IS_COND_BRANCH_OP (branch))
746                         continue;
747         }
748 #endif
749
750         if (changed) {
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);
759         }
760 #endif
761 }
762
763 void
764 mono_nullify_basic_block (MonoBasicBlock *bb) 
765 {
766         bb->in_count = 0;
767         bb->out_count = 0;
768         bb->in_bb = NULL;
769         bb->out_bb = NULL;
770         bb->next_bb = NULL;
771         bb->code = bb->last_ins = NULL;
772         bb->cil_code = NULL;
773 }
774
775 static void 
776 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig,  MonoBasicBlock *repl)
777 {
778         int i;
779
780         for (i = 0; i < bb->out_count; i++) {
781                 MonoBasicBlock *ob = bb->out_bb [i];
782                 if (ob == orig) {
783                         if (!repl) {
784                                 if (bb->out_count > 1) {
785                                         bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
786                                 }
787                                 bb->out_count--;
788                         } else {
789                                 bb->out_bb [i] = repl;
790                         }
791                 }
792         }
793 }
794
795 static void 
796 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
797 {
798         int i;
799
800         for (i = 0; i < bb->in_count; i++) {
801                 MonoBasicBlock *ib = bb->in_bb [i];
802                 if (ib == orig) {
803                         if (!repl) {
804                                 if (bb->in_count > 1) {
805                                         bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
806                                 }
807                                 bb->in_count--;
808                         } else {
809                                 bb->in_bb [i] = repl;
810                         }
811                 }
812         }
813 }
814
815 static void
816 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
817         MonoInst *ins;
818         
819         for (ins = bb->code; ins != NULL; ins = ins->next) {
820                 switch (ins->opcode) {
821                 case OP_BR:
822                         if (ins->inst_target_bb == orig)
823                                 ins->inst_target_bb = repl;
824                         break;
825                 case OP_CALL_HANDLER:
826                         if (ins->inst_target_bb == orig)
827                                 ins->inst_target_bb = repl;
828                         break;
829                 case OP_SWITCH: {
830                         int i;
831                         int n = GPOINTER_TO_INT (ins->klass);
832                         for (i = 0; i < n; i++ ) {
833                                 if (ins->inst_many_bb [i] == orig)
834                                         ins->inst_many_bb [i] = repl;
835                         }
836                         break;
837                 }
838                 default:
839                         if (MONO_IS_COND_BRANCH_OP (ins)) {
840                                 if (ins->inst_true_bb == orig)
841                                         ins->inst_true_bb = repl;
842                                 if (ins->inst_false_bb == orig)
843                                         ins->inst_false_bb = repl;
844                         } else if (MONO_IS_JUMP_TABLE (ins)) {
845                                 int i;
846                                 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
847                                 for (i = 0; i < table->table_size; i++ ) {
848                                         if (table->table [i] == orig)
849                                                 table->table [i] = repl;
850                                 }
851                         }
852
853                         break;
854                 }
855         }
856 }
857
858 /**
859   * Check if a bb is useless (is just made of NOPs and ends with an
860   * unconditional branch, or nothing).
861   * If it is so, unlink it from the CFG and nullify it, and return TRUE.
862   * Otherwise, return FALSE;
863   */
864 static gboolean
865 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
866         MonoBasicBlock *target_bb = NULL;
867         MonoInst *inst;
868
869         /* Do not touch handlers */
870         if (bb->region != -1) {
871                 bb->not_useless = TRUE;
872                 return FALSE;
873         }
874         
875         MONO_BB_FOR_EACH_INS (bb, inst) {
876                 switch (inst->opcode) {
877                 case OP_NOP:
878                         break;
879                 case OP_BR:
880                         target_bb = inst->inst_target_bb;
881                         break;
882                 default:
883                         bb->not_useless = TRUE;
884                         return FALSE;
885                 }
886         }
887         
888         if (target_bb == NULL) {
889                 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
890                         target_bb = bb->next_bb;
891                 } else {
892                         /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
893                         return FALSE;
894                 }
895         }
896         
897         /* Do not touch BBs following a switch (they are the "default" branch) */
898         if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == OP_SWITCH)) {
899                 return FALSE;
900         }
901         
902         /* Do not touch BBs following the entry BB and jumping to something that is not */
903         /* thiry "next" bb (the entry BB cannot contain the branch) */
904         if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
905                 return FALSE;
906         }
907
908         /* 
909          * Do not touch BBs following a try block as the code in 
910          * mini_method_compile needs them to compute the length of the try block.
911          */
912         if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
913                 return FALSE;
914         
915         /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
916         if ((target_bb != NULL) && (target_bb != bb)) {
917                 int i;
918
919                 if (cfg->verbose_level > 1) {
920                         printf ("remove_block_if_useless, removed BB%d\n", bb->block_num);
921                 }
922                 
923                 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
924                 while (bb->in_count) {
925                         MonoBasicBlock *in_bb = bb->in_bb [0];
926                         mono_unlink_bblock (cfg, in_bb, bb);
927                         mono_link_bblock (cfg, in_bb, target_bb);
928                         replace_out_block_in_code (in_bb, bb, target_bb);
929                 }
930                 
931                 mono_unlink_bblock (cfg, bb, target_bb);
932                 if (previous_bb != cfg->bb_entry && mono_bb_is_fall_through (cfg, previous_bb)) {
933                         for (i = 0; i < previous_bb->out_count; i++) {
934                                 if (previous_bb->out_bb [i] == target_bb) {
935                                         MonoInst *jump;
936                                         MONO_INST_NEW (cfg, jump, OP_BR);
937                                         MONO_ADD_INS (previous_bb, jump);
938                                         jump->cil_code = previous_bb->cil_code;
939                                         jump->inst_target_bb = target_bb;
940                                         break;
941                                 }
942                         }
943                 }
944                 
945                 previous_bb->next_bb = bb->next_bb;
946                 mono_nullify_basic_block (bb);
947                 
948                 return TRUE;
949         } else {
950                 return FALSE;
951         }
952 }
953
954 void
955 mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn) 
956 {
957         MonoInst *inst;
958         MonoBasicBlock *prev_bb;
959         int i;
960
961         bb->has_array_access |= bbn->has_array_access;
962         bb->extended |= bbn->extended;
963
964         mono_unlink_bblock (cfg, bb, bbn);
965         for (i = 0; i < bbn->out_count; ++i)
966                 mono_link_bblock (cfg, bb, bbn->out_bb [i]);
967         while (bbn->out_count)
968                 mono_unlink_bblock (cfg, bbn, bbn->out_bb [0]);
969
970         /* Handle the branch at the end of the bb */
971         if (bb->has_call_handler) {
972                 for (inst = bb->code; inst != NULL; inst = inst->next) {
973                         if (inst->opcode == OP_CALL_HANDLER) {
974                                 g_assert (inst->inst_target_bb == bbn);
975                                 NULLIFY_INS (inst);
976                         }
977                 }
978         }
979         if (bb->has_jump_table) {
980                 for (inst = bb->code; inst != NULL; inst = inst->next) {
981                         if (MONO_IS_JUMP_TABLE (inst)) {
982                                 int i;
983                                 MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (inst);
984                                 for (i = 0; i < table->table_size; i++ ) {
985                                         /* Might be already NULL from a previous merge */
986                                         if (table->table [i])
987                                                 g_assert (table->table [i] == bbn);
988                                         table->table [i] = NULL;
989                                 }
990                                 /* Can't nullify this as later instructions depend on it */
991                         }
992                 }
993         }
994         if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
995                 g_assert (bb->last_ins->inst_false_bb == bbn);
996                 bb->last_ins->inst_false_bb = NULL;
997                 bb->extended = TRUE;
998         } else if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
999                 NULLIFY_INS (bb->last_ins);
1000         }
1001
1002         bb->has_call_handler |= bbn->has_call_handler;
1003         bb->has_jump_table |= bbn->has_jump_table;
1004
1005         if (bb->last_ins) {
1006                 if (bbn->code) {
1007                         bb->last_ins->next = bbn->code;
1008                         bbn->code->prev = bb->last_ins;
1009                         bb->last_ins = bbn->last_ins;
1010                 }
1011         } else {
1012                 bb->code = bbn->code;
1013                 bb->last_ins = bbn->last_ins;
1014         }
1015
1016         for (prev_bb = cfg->bb_entry; prev_bb && prev_bb->next_bb != bbn; prev_bb = prev_bb->next_bb)
1017                 ;
1018         if (prev_bb) {
1019                 prev_bb->next_bb = bbn->next_bb;
1020         } else {
1021                 /* bbn might not be in the bb list yet */
1022                 if (bb->next_bb == bbn)
1023                         bb->next_bb = bbn->next_bb;
1024         }
1025         mono_nullify_basic_block (bbn);
1026
1027         /* 
1028          * If bbn fell through to its next bblock, have to add a branch, since bb
1029          * will not fall though to the same bblock (#513931).
1030          */
1031         if (bb->last_ins && bb->out_count == 1 && bb->out_bb [0] != bb->next_bb && !MONO_IS_BRANCH_OP (bb->last_ins)) {
1032                 MONO_INST_NEW (cfg, inst, OP_BR);
1033                 inst->inst_target_bb = bb->out_bb [0];
1034                 MONO_ADD_INS (bb, inst);
1035         }
1036 }
1037
1038 static void
1039 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
1040 {
1041         MonoBasicBlock *bbn, *next;
1042
1043         next = bb->next_bb;
1044
1045         /* Find the previous */
1046         for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
1047                 ;
1048         if (bbn->next_bb) {
1049                 bbn->next_bb = bb->next_bb;
1050         }
1051
1052         /* Find the last */
1053         for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
1054                 ;
1055         bbn->next_bb = bb;
1056         bb->next_bb = NULL;
1057
1058         /* Add a branch */
1059         if (next && (!bb->last_ins || ((bb->last_ins->opcode != OP_NOT_REACHED) && (bb->last_ins->opcode != OP_BR) && (bb->last_ins->opcode != OP_BR_REG) && (!MONO_IS_COND_BRANCH_OP (bb->last_ins))))) {
1060                 MonoInst *ins;
1061
1062                 MONO_INST_NEW (cfg, ins, OP_BR);
1063                 MONO_ADD_INS (bb, ins);
1064                 mono_link_bblock (cfg, bb, next);
1065                 ins->inst_target_bb = next;
1066         }               
1067 }
1068
1069 /*
1070  * mono_remove_block:
1071  *
1072  *   Remove BB from the control flow graph
1073  */
1074 void
1075 mono_remove_bblock (MonoCompile *cfg, MonoBasicBlock *bb) 
1076 {
1077         MonoBasicBlock *tmp_bb;
1078
1079         for (tmp_bb = cfg->bb_entry; tmp_bb && tmp_bb->next_bb != bb; tmp_bb = tmp_bb->next_bb)
1080                 ;
1081
1082         g_assert (tmp_bb);
1083         tmp_bb->next_bb = bb->next_bb;
1084 }
1085
1086 void
1087 mono_remove_critical_edges (MonoCompile *cfg)
1088 {
1089         MonoBasicBlock *bb;
1090         MonoBasicBlock *previous_bb;
1091         
1092         if (cfg->verbose_level > 3) {
1093                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1094                         int i;
1095                         printf ("remove_critical_edges, BEFORE BB%d (in:", bb->block_num);
1096                         for (i = 0; i < bb->in_count; i++) {
1097                                 printf (" %d", bb->in_bb [i]->block_num);
1098                         }
1099                         printf (") (out:");
1100                         for (i = 0; i < bb->out_count; i++) {
1101                                 printf (" %d", bb->out_bb [i]->block_num);
1102                         }
1103                         printf (")");
1104                         if (bb->last_ins != NULL) {
1105                                 printf (" ");
1106                                 mono_print_ins (bb->last_ins);
1107                         }
1108                         printf ("\n");
1109                 }
1110         }
1111         
1112         for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
1113                 if (bb->in_count > 1) {
1114                         int in_bb_index;
1115                         for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
1116                                 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
1117                                 /* 
1118                                  * Have to remove non-critical edges whose source ends with a BR_REG
1119                                  * ins too, since inserting a computation before the BR_REG could 
1120                                  * overwrite the sreg1 of the ins.
1121                                  */
1122                                 if ((in_bb->out_count > 1) || (in_bb->out_count == 1 && in_bb->last_ins && in_bb->last_ins->opcode == OP_BR_REG)) {
1123                                         MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1124                                         new_bb->block_num = cfg->num_bblocks++;
1125 //                                      new_bb->real_offset = bb->real_offset;
1126                                         new_bb->region = bb->region;
1127                                         
1128                                         /* Do not alter the CFG while altering the BB list */
1129                                         if (mono_bb_is_fall_through (cfg, previous_bb)) {
1130                                                 if (previous_bb != cfg->bb_entry) {
1131                                                         int i;
1132                                                         /* Make sure previous_bb really falls through bb */
1133                                                         for (i = 0; i < previous_bb->out_count; i++) {
1134                                                                 if (previous_bb->out_bb [i] == bb) {
1135                                                                         MonoInst *jump;
1136                                                                         MONO_INST_NEW (cfg, jump, OP_BR);
1137                                                                         MONO_ADD_INS (previous_bb, jump);
1138                                                                         jump->cil_code = previous_bb->cil_code;
1139                                                                         jump->inst_target_bb = bb;
1140                                                                         break;
1141                                                                 }
1142                                                         }
1143                                                 } else {
1144                                                         /* We cannot add any inst to the entry BB, so we must */
1145                                                         /* put a new BB in the middle to hold the OP_BR */
1146                                                         MonoInst *jump;
1147                                                         MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
1148                                                         new_bb_after_entry->block_num = cfg->num_bblocks++;
1149 //                                                      new_bb_after_entry->real_offset = bb->real_offset;
1150                                                         new_bb_after_entry->region = bb->region;
1151                                                         
1152                                                         MONO_INST_NEW (cfg, jump, OP_BR);
1153                                                         MONO_ADD_INS (new_bb_after_entry, jump);
1154                                                         jump->cil_code = bb->cil_code;
1155                                                         jump->inst_target_bb = bb;
1156
1157                                                         mono_unlink_bblock (cfg, previous_bb, bb);
1158                                                         mono_link_bblock (cfg, new_bb_after_entry, bb);
1159                                                         mono_link_bblock (cfg, previous_bb, new_bb_after_entry);
1160                                                         
1161                                                         previous_bb->next_bb = new_bb_after_entry;
1162                                                         previous_bb = new_bb_after_entry;
1163
1164                                                         if (cfg->verbose_level > 2) {
1165                                                                 printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry->block_num, bb->block_num);
1166                                                         }
1167                                                 }
1168                                         }
1169                                         
1170                                         /* Insert new_bb in the BB list */
1171                                         previous_bb->next_bb = new_bb;
1172                                         new_bb->next_bb = bb;
1173                                         previous_bb = new_bb;
1174                                         
1175                                         /* Setup in_bb and out_bb */
1176                                         new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1177                                         new_bb->in_bb [0] = in_bb;
1178                                         new_bb->in_count = 1;
1179                                         new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
1180                                         new_bb->out_bb [0] = bb;
1181                                         new_bb->out_count = 1;
1182                                         
1183                                         /* Relink in_bb and bb to (from) new_bb */
1184                                         replace_out_block (in_bb, bb, new_bb);
1185                                         replace_out_block_in_code (in_bb, bb, new_bb);
1186                                         replace_in_block (bb, in_bb, new_bb);
1187                                         
1188                                         if (cfg->verbose_level > 2) {
1189                                                 printf ("remove_critical_edges, removed critical edge from BB%d to BB%d (added BB%d)\n", in_bb->block_num, bb->block_num, new_bb->block_num);
1190                                         }
1191                                 }
1192                         }
1193                 }
1194         }
1195         
1196         if (cfg->verbose_level > 3) {
1197                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1198                         int i;
1199                         printf ("remove_critical_edges, AFTER BB%d (in:", bb->block_num);
1200                         for (i = 0; i < bb->in_count; i++) {
1201                                 printf (" %d", bb->in_bb [i]->block_num);
1202                         }
1203                         printf (") (out:");
1204                         for (i = 0; i < bb->out_count; i++) {
1205                                 printf (" %d", bb->out_bb [i]->block_num);
1206                         }
1207                         printf (")");
1208                         if (bb->last_ins != NULL) {
1209                                 printf (" ");
1210                                 mono_print_ins (bb->last_ins);
1211                         }
1212                         printf ("\n");
1213                 }
1214         }
1215 }
1216
1217 /*
1218  * Optimizes the branches on the Control Flow Graph
1219  *
1220  */
1221 void
1222 mono_optimize_branches (MonoCompile *cfg)
1223 {
1224         int i, changed = FALSE;
1225         MonoBasicBlock *bb, *bbn;
1226         guint32 niterations;
1227
1228         /*
1229          * Some crazy loops could cause the code below to go into an infinite
1230          * loop, see bug #53003 for an example. To prevent this, we put an upper
1231          * bound on the number of iterations.
1232          */
1233         if (cfg->num_bblocks > 1000)
1234                 niterations = cfg->num_bblocks * 2;
1235         else
1236                 niterations = 1000;
1237         
1238         do {
1239                 MonoBasicBlock *previous_bb;
1240                 changed = FALSE;
1241                 niterations --;
1242
1243                 /* we skip the entry block (exit is handled specially instead ) */
1244                 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
1245                         /* dont touch code inside exception clauses */
1246                         if (bb->region != -1)
1247                                 continue;
1248
1249                         if (!bb->not_useless && remove_block_if_useless (cfg, bb, previous_bb)) {
1250                                 changed = TRUE;
1251                                 continue;
1252                         }
1253
1254                         if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1255                                 if (cfg->verbose_level > 2)
1256                                         g_print ("nullify block triggered %d\n", bbn->block_num);
1257
1258                                 bb->next_bb = bbn->next_bb;
1259
1260                                 for (i = 0; i < bbn->out_count; i++)
1261                                         replace_in_block (bbn->out_bb [i], bbn, NULL);
1262
1263                                 mono_nullify_basic_block (bbn);                 
1264                                 changed = TRUE;
1265                         }
1266
1267                         if (bb->out_count == 1) {
1268                                 bbn = bb->out_bb [0];
1269
1270                                 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
1271                                 if (bb->last_ins && (bb->last_ins->opcode != OP_BR) && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
1272                                         bb->last_ins->opcode = OP_BR;
1273                                         bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
1274                                         changed = TRUE;
1275                                         if (cfg->verbose_level > 2)
1276                                                 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
1277                                 }
1278
1279                                 if (bb->region == bbn->region && bb->next_bb == bbn) {
1280                                         /* the block are in sequence anyway ... */
1281
1282                                         /* branches to the following block can be removed */
1283                                         if (bb->last_ins && bb->last_ins->opcode == OP_BR && !bbn->out_of_line) {
1284                                                 bb->last_ins->opcode = OP_NOP;
1285                                                 changed = TRUE;
1286                                                 if (cfg->verbose_level > 2)
1287                                                         g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
1288                                         }
1289
1290                                         if (bbn->in_count == 1 && !bb->extended) {
1291                                                 if (bbn != cfg->bb_exit) {
1292                                                         if (cfg->verbose_level > 2)
1293                                                                 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
1294                                                         mono_merge_basic_blocks (cfg, bb, bbn);
1295                                                         changed = TRUE;
1296                                                         continue;
1297                                                 }
1298
1299                                                 //mono_print_bb_code (bb);
1300                                         }
1301                                 }
1302                         }
1303
1304                         if ((bbn = bb->next_bb) && bbn->in_count == 0 && bbn != cfg->bb_exit && bb->region == bbn->region) {
1305                                 if (cfg->verbose_level > 2) {
1306                                         g_print ("nullify block triggered %d\n", bbn->block_num);
1307                                 }
1308                                 bb->next_bb = bbn->next_bb;
1309
1310                                 for (i = 0; i < bbn->out_count; i++)
1311                                         replace_in_block (bbn->out_bb [i], bbn, NULL);
1312
1313                                 mono_nullify_basic_block (bbn);                 
1314                                 changed = TRUE;
1315                                 continue;
1316                         }
1317
1318                         if (bb->out_count == 1) {
1319                                 bbn = bb->out_bb [0];
1320
1321                                 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
1322                                         bbn = bb->last_ins->inst_target_bb;
1323                                         if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1324                                                 bbn->code->inst_target_bb != bbn &&
1325                                             bbn->code->inst_target_bb->region == bb->region) {
1326                                                 
1327                                                 if (cfg->verbose_level > 2)
1328                                                         g_print ("branch to branch triggered %d -> %d -> %d\n", bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
1329
1330                                                 replace_in_block (bbn, bb, NULL);
1331                                                 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
1332                                                 mono_link_bblock (cfg, bb, bbn->code->inst_target_bb);
1333                                                 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
1334                                                 changed = TRUE;
1335                                                 continue;
1336                                         }
1337                                 }
1338                         } else if (bb->out_count == 2) {
1339                                 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1340                                         int branch_result;
1341                                         MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
1342
1343                                         if (bb->last_ins->flags & MONO_INST_CFOLD_TAKEN)
1344                                                 branch_result = BRANCH_TAKEN;
1345                                         else if (bb->last_ins->flags & MONO_INST_CFOLD_NOT_TAKEN)
1346                                                 branch_result = BRANCH_NOT_TAKEN;
1347                                         else
1348                                                 branch_result = BRANCH_UNDEF;
1349
1350                                         if (branch_result == BRANCH_TAKEN) {
1351                                                 taken_branch_target = bb->last_ins->inst_true_bb;
1352                                                 untaken_branch_target = bb->last_ins->inst_false_bb;
1353                                         } else if (branch_result == BRANCH_NOT_TAKEN) {
1354                                                 taken_branch_target = bb->last_ins->inst_false_bb;
1355                                                 untaken_branch_target = bb->last_ins->inst_true_bb;
1356                                         }
1357                                         if (taken_branch_target) {
1358                                                 /* if mono_eval_cond_branch () is ever taken to handle 
1359                                                  * non-constant values to compare, issue a pop here.
1360                                                  */
1361                                                 bb->last_ins->opcode = OP_BR;
1362                                                 bb->last_ins->inst_target_bb = taken_branch_target;
1363                                                 if (!bb->extended)
1364                                                         mono_unlink_bblock (cfg, bb, untaken_branch_target);
1365                                                 changed = TRUE;
1366                                                 continue;
1367                                         }
1368                                         bbn = bb->last_ins->inst_true_bb;
1369                                         if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1370                                             bbn->code->inst_target_bb->region == bb->region) {
1371                                                 if (cfg->verbose_level > 2)             
1372                                                         g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n", 
1373                                                                  bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num, 
1374                                                                  bbn->code->opcode);
1375
1376                                                 /* 
1377                                                  * Unlink, then relink bblocks to avoid various
1378                                                  * tricky situations when the two targets of the branch
1379                                                  * are equal, or will become equal after the change.
1380                                                  */
1381                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1382                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1383
1384                                                 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
1385
1386                                                 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1387                                                 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1388
1389                                                 changed = TRUE;
1390                                                 continue;
1391                                         }
1392
1393                                         bbn = bb->last_ins->inst_false_bb;
1394                                         if (bbn && bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
1395                                             bbn->code->inst_target_bb->region == bb->region) {
1396                                                 if (cfg->verbose_level > 2)
1397                                                         g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n", 
1398                                                                  bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num, 
1399                                                                  bbn->code->opcode);
1400
1401                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1402                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1403
1404                                                 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
1405
1406                                                 mono_link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
1407                                                 mono_link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
1408
1409                                                 changed = TRUE;
1410                                                 continue;
1411                                         }
1412
1413                                         bbn = bb->last_ins->inst_false_bb;
1414                                         /*
1415                                          * If bb is an extended bb, it could contain an inside branch to bbn.
1416                                          * FIXME: Enable the optimization if that is not true.
1417                                          * If bblocks_linked () is true, then merging bb and bbn
1418                                          * would require addition of an extra branch at the end of bbn 
1419                                          * slowing down loops.
1420                                          */
1421                                         if (bbn && bb->region == bbn->region && bbn->in_count == 1 && cfg->enable_extended_bblocks && bbn != cfg->bb_exit && !bb->extended && !bbn->out_of_line && !mono_bblocks_linked (bbn, bb)) {
1422                                                 g_assert (bbn->in_bb [0] == bb);
1423                                                 if (cfg->verbose_level > 2)
1424                                                         g_print ("merge false branch target triggered BB%d -> BB%d\n", bb->block_num, bbn->block_num);
1425                                                 mono_merge_basic_blocks (cfg, bb, bbn);
1426                                                 changed = TRUE;
1427                                                 continue;
1428                                         }
1429                                 }
1430
1431                                 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
1432                                         if (bb->last_ins->inst_false_bb && bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region) && !cfg->disable_out_of_line_bblocks) {
1433                                                 /* Reverse the branch */
1434                                                 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
1435                                                 bbn = bb->last_ins->inst_false_bb;
1436                                                 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
1437                                                 bb->last_ins->inst_true_bb = bbn;
1438
1439                                                 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
1440                                                 if (cfg->verbose_level > 2)
1441                                                         g_print ("cbranch to throw block triggered %d.\n", 
1442                                                                          bb->block_num);
1443                                         }
1444                                 }
1445                         }
1446                 }
1447         } while (changed && (niterations > 0));
1448 }
1449
1450 #endif /* DISABLE_JIT */