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