[jit] refactor basic block ordering in separate function
[mono.git] / mono / mini / mini.c
index 0dfd1ce1487ddc05e00f6211a8484bda71471c3f..850d2d68e45bfbe76c051e087833729d876a5441 100644 (file)
@@ -2711,6 +2711,42 @@ compute_reachable (MonoBasicBlock *bb)
        }
 }
 
+static void mono_bb_ordering (MonoCompile *cfg)
+{
+       int dfn = 0;
+       /* Depth-first ordering on basic blocks */
+       cfg->bblocks = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
+
+       cfg->max_block_num = cfg->num_bblocks;
+
+       df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
+       if (cfg->num_bblocks != dfn + 1) {
+               MonoBasicBlock *bb;
+
+               cfg->num_bblocks = dfn + 1;
+
+               /* remove unreachable code, because the code in them may be 
+                * inconsistent  (access to dead variables for example) */
+               for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
+                       bb->flags &= ~BB_VISITED;
+               compute_reachable (cfg->bb_entry);
+               for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
+                       if (bb->flags & BB_EXCEPTION_HANDLER)
+                               compute_reachable (bb);
+               for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+                       if (!(bb->flags & BB_VISITED)) {
+                               if (cfg->verbose_level > 1)
+                                       g_print ("found unreachable code in BB%d\n", bb->block_num);
+                               bb->code = bb->last_ins = NULL;
+                               while (bb->out_count)
+                                       mono_unlink_bblock (cfg, bb, bb->out_bb [0]);
+                       }
+               }
+               for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
+                       bb->flags &= ~BB_VISITED;
+       }
+}
+
 static void
 mono_handle_out_of_line_bblock (MonoCompile *cfg)
 {
@@ -3301,7 +3337,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFl
        MonoMethodSignature *sig;
        MonoError err;
        MonoCompile *cfg;
-       int dfn, i, code_size_ratio;
+       int i, code_size_ratio;
        gboolean try_generic_shared, try_llvm = FALSE;
        MonoMethod *method_to_compile, *method_to_register;
        gboolean method_is_gshared = FALSE;
@@ -3741,38 +3777,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFl
 
        mono_threads_safepoint ();
 
-       /* Depth-first ordering on basic blocks */
-       cfg->bblocks = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
-
-       cfg->max_block_num = cfg->num_bblocks;
-
-       dfn = 0;
-       df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
-       if (cfg->num_bblocks != dfn + 1) {
-               MonoBasicBlock *bb;
-
-               cfg->num_bblocks = dfn + 1;
-
-               /* remove unreachable code, because the code in them may be 
-                * inconsistent  (access to dead variables for example) */
-               for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
-                       bb->flags &= ~BB_VISITED;
-               compute_reachable (cfg->bb_entry);
-               for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
-                       if (bb->flags & BB_EXCEPTION_HANDLER)
-                               compute_reachable (bb);
-               for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
-                       if (!(bb->flags & BB_VISITED)) {
-                               if (cfg->verbose_level > 1)
-                                       g_print ("found unreachable code in BB%d\n", bb->block_num);
-                               bb->code = bb->last_ins = NULL;
-                               while (bb->out_count)
-                                       mono_unlink_bblock (cfg, bb, bb->out_bb [0]);
-                       }
-               }
-               for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
-                       bb->flags &= ~BB_VISITED;
-       }
+       mono_bb_ordering (cfg);
 
        if (((cfg->num_varinfo > 2000) || (cfg->num_bblocks > 1000)) && !cfg->compile_aot) {
                /* 
@@ -4184,6 +4189,31 @@ create_jit_info_for_trampoline (MonoMethod *wrapper, MonoTrampInfo *info)
        return jinfo;
 }
 
+GTimer *mono_time_track_start ()
+{
+       return g_timer_new ();
+}
+
+void mono_time_track_end (double *time, GTimer *timer)
+{
+       g_timer_stop (timer);
+       *time += g_timer_elapsed (timer, NULL);
+       g_timer_destroy (timer);
+}
+
+void mono_update_jit_stats (MonoCompile *cfg)
+{
+       mono_jit_stats.allocate_var += cfg->stat_allocate_var;
+       mono_jit_stats.locals_stack_size += cfg->stat_locals_stack_size;
+       mono_jit_stats.basic_blocks += cfg->stat_basic_blocks;
+       mono_jit_stats.max_basic_blocks = MAX (cfg->stat_basic_blocks, mono_jit_stats.max_basic_blocks);
+       mono_jit_stats.cil_code_size += cfg->stat_cil_code_size;
+       mono_jit_stats.regvars += cfg->stat_n_regvars;
+       mono_jit_stats.inlineable_methods += cfg->stat_inlineable_methods;
+       mono_jit_stats.inlined_methods += cfg->stat_inlined_methods;
+       mono_jit_stats.code_reallocs += cfg->stat_code_reallocs;
+}
+
 /*
  * mono_jit_compile_method_inner:
  *
@@ -4317,14 +4347,11 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
                return NULL;
        }
 
-       jit_timer = g_timer_new ();
-
+       jit_timer = mono_time_track_start ();
        cfg = mini_method_compile (method, opt, target_domain, JIT_FLAG_RUN_CCTORS, 0, -1);
-       prof_method = cfg->method;
+       mono_time_track_end (&mono_jit_stats.jit_time, jit_timer);
 
-       g_timer_stop (jit_timer);
-       mono_jit_stats.jit_time += g_timer_elapsed (jit_timer, NULL);
-       g_timer_destroy (jit_timer);
+       prof_method = cfg->method;
 
        switch (cfg->exception_type) {
        case MONO_EXCEPTION_NONE:
@@ -4412,15 +4439,7 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
         * Update global stats while holding a lock, instead of doing many
         * InterlockedIncrement operations during JITting.
         */
-       mono_jit_stats.allocate_var += cfg->stat_allocate_var;
-       mono_jit_stats.locals_stack_size += cfg->stat_locals_stack_size;
-       mono_jit_stats.basic_blocks += cfg->stat_basic_blocks;
-       mono_jit_stats.max_basic_blocks = MAX (cfg->stat_basic_blocks, mono_jit_stats.max_basic_blocks);
-       mono_jit_stats.cil_code_size += cfg->stat_cil_code_size;
-       mono_jit_stats.regvars += cfg->stat_n_regvars;
-       mono_jit_stats.inlineable_methods += cfg->stat_inlineable_methods;
-       mono_jit_stats.inlined_methods += cfg->stat_inlined_methods;
-       mono_jit_stats.code_reallocs += cfg->stat_code_reallocs;
+       mono_update_jit_stats (cfg);
 
        mono_destroy_compile (cfg);