1 /*
   2  * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "compiler/compileBroker.hpp"
  26 #include "classfile/moduleEntry.hpp"
  27 #include "jvmci/jvmciEnv.hpp"
  28 #include "jvmci/jvmciRuntime.hpp"
  29 #include "oops/objArrayOop.inline.hpp"
  30 #include "runtime/handles.inline.hpp"
  31 
  32 JVMCICompiler* JVMCICompiler::_instance = NULL;
  33 elapsedTimer JVMCICompiler::_codeInstallTimer;
  34 
  35 JVMCICompiler::JVMCICompiler() : AbstractCompiler(compiler_jvmci) {
  36   _bootstrapping = false;
  37   _bootstrap_compilation_request_handled = false;
  38   _methods_compiled = 0;
  39   assert(_instance == NULL, "only one instance allowed");
  40   _instance = this;
  41 }
  42 
  43 // Initialization
  44 void JVMCICompiler::initialize() {
  45   if (!UseCompiler || !EnableJVMCI || !UseJVMCICompiler || !should_perform_init()) {
  46     return;
  47   }
  48 
  49   set_state(initialized);
  50 }
  51 
  52 void JVMCICompiler::bootstrap(TRAPS) {
  53   if (Arguments::mode() == Arguments::_int) {
  54     // Nothing to do in -Xint mode
  55     return;
  56   }
  57   _bootstrapping = true;
  58   ResourceMark rm;
  59   HandleMark hm;
  60   if (PrintBootstrap) {
  61     tty->print("Bootstrapping JVMCI");
  62   }
  63   jlong start = os::javaTimeMillis();
  64 
  65   Array<Method*>* objectMethods = SystemDictionary::Object_klass()->methods();
  66   // Initialize compile queue with a selected set of methods.
  67   int len = objectMethods->length();
  68   for (int i = 0; i < len; i++) {
  69     methodHandle mh = objectMethods->at(i);
  70     if (!mh->is_native() && !mh->is_static() && !mh->is_initializer()) {
  71       ResourceMark rm;
  72       int hot_count = 10; // TODO: what's the appropriate value?
  73       CompileBroker::compile_method(mh, InvocationEntryBci, CompLevel_full_optimization, mh, hot_count, CompileTask::Reason_Bootstrap, THREAD);
  74     }
  75   }
  76 
  77   int qsize;
  78   bool first_round = true;
  79   int z = 0;
  80   do {
  81     // Loop until there is something in the queue.
  82     do {
  83       os::sleep(THREAD, 100, true);
  84       qsize = CompileBroker::queue_size(CompLevel_full_optimization);
  85     } while (!_bootstrap_compilation_request_handled && first_round && qsize == 0);
  86     first_round = false;
  87     if (PrintBootstrap) {
  88       while (z < (_methods_compiled / 100)) {
  89         ++z;
  90         tty->print_raw(".");
  91       }
  92     }
  93   } while (qsize != 0);
  94 
  95   if (PrintBootstrap) {
  96     tty->print_cr(" in " JLONG_FORMAT " ms (compiled %d methods)", os::javaTimeMillis() - start, _methods_compiled);
  97   }
  98   _bootstrapping = false;
  99   JVMCI::compiler_runtime()->bootstrap_finished(CHECK);
 100 }
 101 
 102 bool JVMCICompiler::force_comp_at_level_simple(Method *method) {
 103   if (UseJVMCINativeLibrary) {
 104     // This mechanism exists to force compilation of a JVMCI compiler by C1
 105     // to reduces the compilation time spent on the JVMCI compiler itself. In
 106     // +UseJVMCINativeLibrary mode, the JVMCI compiler is AOT compiled.
 107     return false;
 108   }
 109 
 110   if (_bootstrapping) {
 111     // When bootstrapping, the JVMCI compiler can compile its own methods.
 112     return false;
 113   }
 114 
 115   JVMCIRuntime* runtime = JVMCI::compiler_runtime();
 116   if (runtime != NULL && runtime->is_HotSpotJVMCIRuntime_initialized()) {
 117     JavaThread* thread = JavaThread::current();
 118     HandleMark hm(thread);
 119     THREAD_JVMCIENV(thread);
 120     JVMCIObject receiver = runtime->get_HotSpotJVMCIRuntime(JVMCIENV);
 121     objArrayHandle excludeModules(thread, HotSpotJVMCI::HotSpotJVMCIRuntime::excludeFromJVMCICompilation(JVMCIENV, HotSpotJVMCI::resolve(receiver)));
 122     if (excludeModules.not_null()) {
 123       ModuleEntry* moduleEntry = method->method_holder()->module();
 124       for (int i = 0; i < excludeModules->length(); i++) {
 125         if (oopDesc::equals(excludeModules->obj_at(i), moduleEntry->module())) {
 126           return true;
 127         }
 128       }
 129     }
 130   }
 131   return false;
 132 }
 133 
 134 // Compilation entry point for methods
 135 void JVMCICompiler::compile_method(ciEnv* env, ciMethod* target, int entry_bci, DirectiveSet* directive) {
 136   ShouldNotReachHere();
 137 }
 138 
 139 // Print compilation timers and statistics
 140 void JVMCICompiler::print_timers() {
 141   print_compilation_timers();
 142 }
 143 
 144 // Print compilation timers and statistics
 145 void JVMCICompiler::print_compilation_timers() {
 146   TRACE_jvmci_1("JVMCICompiler::print_timers");
 147   tty->print_cr("       JVMCI code install time:        %6.3f s",    _codeInstallTimer.seconds());
 148 }