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