* Removed all Id tags.
[cacao.git] / src / vm / jit / jit.c
index 41b31243f16714aa9eac7fae633d188fc7ed68d7..b8d8cdc2e36c6080ff19684e74aff8934b453390 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/jit/jit.c - calls the code generation functions
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
    J. Wenninger, Institut f. Computersprachen - TU Wien
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Andreas Krall
-            Reinhard Grafl
-
-   Changes: Edwin Steiner
-            Christian Thalinger
-            Christian Ullrich
-
-   $Id: jit.c 5906 2006-11-04 23:37:10Z edwin $
-
 */
 
 
 #include "config.h"
-#include "vm/types.h"
 
 #include <assert.h>
 
+#include "vm/types.h"
+
 #include "mm/memory.h"
+
 #include "native/native.h"
+
 #include "toolbox/logging.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/lock.h"
-#else
-# include "threads/none/lock.h"
-#endif
+#include "threads/lock-common.h"
+#include "threads/threads-common.h"
 
-#include "vm/class.h"
 #include "vm/global.h"
 #include "vm/initialize.h"
-#include "vm/loader.h"
-#include "vm/method.h"
-#include "vm/options.h"
-#include "vm/statistics.h"
+
 #include "vm/jit/asmpart.h"
 
 # include "vm/jit/cfg.h"
@@ -66,6 +51,7 @@
 #include "vm/jit/disass.h"
 #include "vm/jit/dseg.h"
 #include "vm/jit/jit.h"
+#include "vm/jit/md.h"
 #include "vm/jit/parse.h"
 #include "vm/jit/reg.h"
 
 # include "vm/jit/optimizing/ssa.h"
 #endif
 
+#if defined(ENABLE_INLINING)
+# include "vm/jit/inline/inline.h"
+#endif
+
 #include "vm/jit/loop/analyze.h"
 #include "vm/jit/loop/graph.h"
 #include "vm/jit/loop/loop.h"
 #include "vm/jit/optimizing/reorder.h"
 
 #include "vm/jit/verify/typecheck.h"
-#include "vm/rt-timing.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#endif
+#include "vmcore/class.h"
+#include "vmcore/loader.h"
+#include "vmcore/method.h"
+#include "vmcore/options.h"
+#include "vmcore/rt-timing.h"
+#include "vmcore/statistics.h"
 
 
 /* debug macros ***************************************************************/
@@ -908,6 +900,10 @@ void jit_init(void)
        /* initialize codegen subsystem */
 
        codegen_init();
+
+       /* initialize code subsystem */
+
+       (void) code_init();
 }
 
 
@@ -958,9 +954,15 @@ jitdata *jit_jitdata_new(methodinfo *m)
 
        /* initialize variables */
 
-       jd->flags = 0;
-       jd->exceptiontable = NULL;
+       jd->flags                = 0;
+       jd->exceptiontable       = NULL;
        jd->exceptiontablelength = 0;
+       jd->returncount          = 0;
+       jd->branchtoentry        = false;
+       jd->branchtoend          = false;
+       jd->returncount          = 0;
+       jd->returnblock          = NULL;
+       jd->maxlocals            = m->maxlocals;
 
 #if defined(ENABLE_THREADS)
        if (checksync && (m->flags & ACC_SYNCHRONIZED))
@@ -1059,6 +1061,11 @@ u1 *jit_compile(methodinfo *m)
                jd->flags |= JITDATA_FLAG_IFCONV;
 #endif
 
+#if defined(ENABLE_INLINING) && defined(ENABLE_INLINING_DEBUG)
+       if (opt_inlining && opt_inline_debug_all)
+               jd->flags |= JITDATA_FLAG_INLINE;
+#endif
+
        if (opt_showintermediate)
                jd->flags |= JITDATA_FLAG_SHOWINTERMEDIATE;
 
@@ -1068,6 +1075,11 @@ u1 *jit_compile(methodinfo *m)
        if (opt_verbosecall)
                jd->flags |= JITDATA_FLAG_VERBOSECALL;
 
+#if defined(ENABLE_REPLACEMENT) && defined(ENABLE_INLINING)
+       if (opt_inlining)
+               jd->flags |= JITDATA_FLAG_COUNTDOWN;
+#endif
+
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
        if (!opt_intrp)
@@ -1093,11 +1105,13 @@ u1 *jit_compile(methodinfo *m)
 
                code_codeinfo_free(jd->code);
 
+#if defined(ENABLE_PROFILING)
                /* Release memory for basic block profiling information. */
 
                if (JITDATA_HAS_FLAG_INSTRUMENT(jd))
                        if (jd->code->bbfrequency != NULL)
                                MFREE(jd->code->bbfrequency, u4, jd->code->basicblockcount);
+#endif
        }
        else {
                DEBUG_JIT_COMPILEVERBOSE("Running: ");
@@ -1139,14 +1153,16 @@ u1 *jit_recompile(methodinfo *m)
 
        /* check for max. optimization level */
 
-       optlevel = m->code->optlevel;
+       optlevel = (m->code) ? m->code->optlevel : 0;
 
+#if 0
        if (optlevel == 1) {
 /*             log_message_method("not recompiling: ", m); */
                return NULL;
        }
+#endif
 
-       log_message_method("Recompiling start: ", m);
+       DEBUG_JIT_COMPILEVERBOSE("Recompiling start: ");
 
        STATISTICS(count_jit_calls++);
 
@@ -1175,10 +1191,18 @@ u1 *jit_recompile(methodinfo *m)
        jd->flags |= JITDATA_FLAG_VERIFY;
 #endif
 
-       jd->flags |= JITDATA_FLAG_REORDER;
-       jd->flags |= JITDATA_FLAG_SHOWINTERMEDIATE;
-       jd->flags |= JITDATA_FLAG_SHOWDISASSEMBLE;
-/*     jd->flags |= JITDATA_FLAG_VERBOSECALL; */
+       /* jd->flags |= JITDATA_FLAG_REORDER; */
+       if (opt_showintermediate)
+               jd->flags |= JITDATA_FLAG_SHOWINTERMEDIATE;
+       if (opt_showdisassemble)
+               jd->flags |= JITDATA_FLAG_SHOWDISASSEMBLE;
+       if (opt_verbosecall)
+               jd->flags |= JITDATA_FLAG_VERBOSECALL;
+
+#if defined(ENABLE_INLINING)
+       if (opt_inlining)
+               jd->flags |= JITDATA_FLAG_INLINE;
+#endif
 
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
@@ -1216,7 +1240,7 @@ u1 *jit_recompile(methodinfo *m)
                compilingtime_stop();
 #endif
 
-       log_message_method("Recompiling done: ", m);
+       DEBUG_JIT_COMPILEVERBOSE("Recompiling done: ");
 
        /* return pointer to the methods entry point */
 
@@ -1239,7 +1263,7 @@ static u1 *jit_compile_intern(jitdata *jd)
 #if defined(ENABLE_RT_TIMING)
        struct timespec time_start,time_checks,time_parse,time_stack,
                                        time_typecheck,time_loop,time_ifconv,time_alloc,
-                                       time_rplpoints,time_codegen;
+                                       time_codegen;
 #endif
        
        RT_TIMING_GET_TIME(time_start);
@@ -1257,6 +1281,10 @@ static u1 *jit_compile_intern(jitdata *jd)
 
        DEBUG_JIT_COMPILEVERBOSE("Compiling: ");
 
+#if defined(ENABLE_DEBUG_FILTER)
+       show_filters_apply(jd->m);
+#endif
+
        /* handle native methods and create a native stub */
 
        if (m->flags & ACC_NATIVE) {
@@ -1271,7 +1299,7 @@ static u1 *jit_compile_intern(jitdata *jd)
                f = NULL;
 #endif
 
-               code = codegen_createnativestub(f, m);
+               code = codegen_generate_stub_native(m, f);
 
                assert(!m->code); /* native methods are never recompiled */
                m->code = code;
@@ -1373,6 +1401,16 @@ static u1 *jit_compile_intern(jitdata *jd)
 #endif
                RT_TIMING_GET_TIME(time_ifconv);
 
+               /* inlining */
+
+#if defined(ENABLE_INLINING)
+               if (JITDATA_HAS_FLAG_INLINE(jd)) {
+                       if (!inline_inline(jd))
+                               return NULL;
+               }
+#endif
+
+#if defined(ENABLE_PROFILING)
                /* Basic block reordering.  I think this should be done after
                   if-conversion, as we could lose the ability to do the
                   if-conversion. */
@@ -1382,6 +1420,7 @@ static u1 *jit_compile_intern(jitdata *jd)
                                return NULL;
                        jit_renumber_basicblocks(jd);
                }
+#endif
 
                DEBUG_JIT_COMPILEVERBOSE("Allocating registers: ");
 
@@ -1406,7 +1445,7 @@ static u1 *jit_compile_intern(jitdata *jd)
                } else
 # endif /* defined(ENABLE_SSA) */
                {
-                       STATISTICS(count_locals_conflicts += (cd->maxlocals - 1) * (cd->maxlocals));
+                       STATISTICS(count_locals_conflicts += (jd->maxlocals - 1) * (jd->maxlocals));
 
                        regalloc(jd);
                }
@@ -1420,22 +1459,17 @@ static u1 *jit_compile_intern(jitdata *jd)
 #endif /* defined(ENABLE_JIT) */
        RT_TIMING_GET_TIME(time_alloc);
 
+#if defined(ENABLE_PROFILING)
        /* Allocate memory for basic block profiling information. This
           _must_ be done after loop optimization and register allocation,
           since they can change the basic block count. */
 
        if (JITDATA_HAS_FLAG_INSTRUMENT(jd))
                code->bbfrequency = MNEW(u4, jd->basicblockcount);
+#endif
 
        DEBUG_JIT_COMPILEVERBOSE("Generating code: ");
 
-       /* create the replacement points */
-#if 0
-       if (!replace_create_replacement_points(jd))
-               return NULL;
-#endif
-       RT_TIMING_GET_TIME(time_rplpoints);
-
        /* now generate the machine code */
 
 #if defined(ENABLE_JIT)
@@ -1459,7 +1493,7 @@ static u1 *jit_compile_intern(jitdata *jd)
        } else
 # endif
                {
-                       if (!codegen(jd)) {
+                       if (!codegen_generate(jd)) {
                                DEBUG_JIT_COMPILEVERBOSE("Exception while generating code: ");
 
                                return NULL;
@@ -1477,20 +1511,25 @@ static u1 *jit_compile_intern(jitdata *jd)
        DEBUG_JIT_COMPILEVERBOSE("Generating code done: ");
 
 #if !defined(NDEBUG)
-       /* intermediate and assembly code listings */
+#if defined(ENABLE_DEBUG_FILTER)
+       if (jd->m->filtermatches & SHOW_FILTER_FLAG_SHOW_METHOD)
+#endif
+       {
+               /* intermediate and assembly code listings */
                
-       if (JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd)) {
-               show_method(jd, SHOW_CODE);
-       }
-       else if (JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd)) {
+               if (JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd)) {
+                       show_method(jd, SHOW_CODE);
+               }
+               else if (JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd)) {
 # if defined(ENABLE_DISASSEMBLER)
-               DISASSEMBLE(code->entrypoint,
-                                       code->entrypoint + (code->mcodelength - cd->dseglen));
+                       DISASSEMBLE(code->entrypoint,
+                                               code->entrypoint + (code->mcodelength - cd->dseglen));
 # endif
-       }
+               }
 
-       if (opt_showddatasegment)
-               dseg_display(jd);
+               if (opt_showddatasegment)
+                       dseg_display(jd);
+       }
 #endif
 
        DEBUG_JIT_COMPILEVERBOSE("Compiling done: ");
@@ -1511,8 +1550,7 @@ static u1 *jit_compile_intern(jitdata *jd)
        RT_TIMING_TIME_DIFF(time_stack,time_typecheck,RT_TIMING_JIT_TYPECHECK);
        RT_TIMING_TIME_DIFF(time_typecheck,time_loop,RT_TIMING_JIT_LOOP);
        RT_TIMING_TIME_DIFF(time_loop,time_alloc,RT_TIMING_JIT_ALLOC);
-       RT_TIMING_TIME_DIFF(time_alloc,time_rplpoints,RT_TIMING_JIT_RPLPOINTS);
-       RT_TIMING_TIME_DIFF(time_rplpoints,time_codegen,RT_TIMING_JIT_CODEGEN);
+       RT_TIMING_TIME_DIFF(time_alloc,time_codegen,RT_TIMING_JIT_CODEGEN);
        RT_TIMING_TIME_DIFF(time_start,time_codegen,RT_TIMING_JIT_TOTAL);
 
        /* return pointer to the methods entry point */
@@ -1521,6 +1559,92 @@ static u1 *jit_compile_intern(jitdata *jd)
 } 
 
 
+/* jit_invalidate_code *********************************************************
+
+   Mark the compiled code of the given method as invalid and take care that
+   it is replaced if necessary.
+
+   XXX Not fully implemented, yet.
+
+*******************************************************************************/
+
+void jit_invalidate_code(methodinfo *m)
+{
+       codeinfo *code;
+
+       code = m->code;
+       if (code == NULL || CODE_IS_INVALID(code))
+               return;
+
+       CODE_SETFLAG_INVALID(code);
+
+       /* activate mappable replacement points */
+
+#if defined(ENABLE_REPLACEMENT)
+       replace_activate_replacement_points(code, true);
+#else
+       vm_abort("invalidating code only works with ENABLE_REPLACEMENT");
+#endif
+}
+
+
+/* jit_request_optimization ****************************************************
+
+   Request optimization of the given method. If the code of the method is
+   unoptimized, it will be invalidated, so the next jit_get_current_code(m)
+   triggers an optimized recompilation.
+   If the method is already optimized, this function does nothing.
+
+   IN:
+       m................the method
+
+*******************************************************************************/
+
+void jit_request_optimization(methodinfo *m)
+{
+       codeinfo *code;
+
+       code = m->code;
+
+       if (code && code->optlevel == 0)
+               jit_invalidate_code(m);
+}
+
+
+/* jit_get_current_code ********************************************************
+
+   Get the currently valid code for the given method. If there is no valid
+   code, (re)compile the method.
+
+   IN:
+       m................the method
+
+   RETURN VALUE:
+       the codeinfo* for the current code, or
+          NULL if an exception has been thrown during recompilation.
+
+*******************************************************************************/
+
+codeinfo *jit_get_current_code(methodinfo *m)
+{
+       assert(m);
+
+       /* if we have valid code, return it */
+
+       if (m->code && CODE_IS_VALID(m->code))
+               return m->code;
+
+       /* otherwise: recompile */
+
+       if (!jit_recompile(m))
+               return NULL;
+
+       assert(m->code);
+
+       return m->code;
+}
+
+
 /* jit_asm_compile *************************************************************
 
    This method is called from asm_vm_call_method and does: