1 /*
  2  * Copyright (c) 2011, 2021, 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 "classfile/vmClasses.hpp"
 26 #include "compiler/compileBroker.hpp"
 27 #include "classfile/moduleEntry.hpp"
 28 #include "classfile/vmSymbols.hpp"
 29 #include "jvmci/jvmciEnv.hpp"
 30 #include "jvmci/jvmciRuntime.hpp"
 31 #include "oops/objArrayOop.inline.hpp"
 32 #include "runtime/arguments.hpp"
 33 #include "runtime/handles.inline.hpp"
 34 
 35 JVMCICompiler* JVMCICompiler::_instance = NULL;
 36 elapsedTimer JVMCICompiler::_codeInstallTimer;
 37 elapsedTimer JVMCICompiler::_hostedCodeInstallTimer;
 38 
 39 JVMCICompiler::JVMCICompiler() : AbstractCompiler(compiler_jvmci) {
 40   _bootstrapping = false;
 41   _bootstrap_compilation_request_handled = false;
 42   _methods_compiled = 0;
 43   _global_compilation_ticks = 0;
 44   assert(_instance == NULL, "only one instance allowed");
 45   _instance = this;
 46 }
 47 
 48 JVMCICompiler* JVMCICompiler::instance(bool require_non_null, TRAPS) {
 49   if (!EnableJVMCI) {
 50     THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled")
 51   }
 52   if (_instance == NULL && require_non_null) {
 53     THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "The JVMCI compiler instance has not been created");
 54   }
 55   return _instance;
 56 }
 57 
 58 // Initialization
 59 void JVMCICompiler::initialize() {
 60   assert(!CompilerConfig::is_c1_or_interpreter_only_no_jvmci(), "JVMCI is launched, it's not c1/interpreter only mode");
 61   if (!UseCompiler || !EnableJVMCI || !UseJVMCICompiler || !should_perform_init()) {
 62     return;
 63   }
 64 
 65   set_state(initialized);
 66 }
 67 
 68 void JVMCICompiler::bootstrap(TRAPS) {
 69   if (Arguments::mode() == Arguments::_int) {
 70     // Nothing to do in -Xint mode
 71     return;
 72   }
 73   _bootstrapping = true;
 74   ResourceMark rm(THREAD);
 75   HandleMark hm(THREAD);
 76   if (PrintBootstrap) {
 77     tty->print("Bootstrapping JVMCI");
 78   }
 79   jlong start = os::javaTimeNanos();
 80 
 81   Array<Method*>* objectMethods = vmClasses::Object_klass()->methods();
 82   // Initialize compile queue with a selected set of methods.
 83   int len = objectMethods->length();
 84   for (int i = 0; i < len; i++) {
 85     methodHandle mh(THREAD, objectMethods->at(i));
 86     if (!mh->is_native() && !mh->is_static() && !mh->is_initializer()) {
 87       ResourceMark rm;
 88       int hot_count = 10; // TODO: what's the appropriate value?
 89       CompileBroker::compile_method(mh, InvocationEntryBci, CompLevel_full_optimization, mh, hot_count, CompileTask::Reason_Bootstrap, CHECK);
 90     }
 91   }
 92 
 93   int qsize;
 94   bool first_round = true;
 95   int z = 0;
 96   do {
 97     // Loop until there is something in the queue.
 98     do {
 99       THREAD->sleep(100);
100       qsize = CompileBroker::queue_size(CompLevel_full_optimization);
101     } while (!_bootstrap_compilation_request_handled && first_round && qsize == 0);
102     first_round = false;
103     if (PrintBootstrap) {
104       while (z < (_methods_compiled / 100)) {
105         ++z;
106         tty->print_raw(".");
107       }
108     }
109   } while (qsize != 0);
110 
111   if (PrintBootstrap) {
112     tty->print_cr(" in " JLONG_FORMAT " ms (compiled %d methods)",
113                   (jlong)nanos_to_millis(os::javaTimeNanos() - start), _methods_compiled);
114   }
115   _bootstrapping = false;
116   JVMCI::java_runtime()->bootstrap_finished(CHECK);
117 }
118 
119 bool JVMCICompiler::force_comp_at_level_simple(const methodHandle& method) {
120   if (_bootstrapping) {
121     // When bootstrapping, the JVMCI compiler can compile its own methods.
122     return false;
123   }
124   if (UseJVMCINativeLibrary) {
125     // This mechanism exists to force compilation of a JVMCI compiler by C1
126     // to reduce the compilation time spent on the JVMCI compiler itself. In
127     // +UseJVMCINativeLibrary mode, the JVMCI compiler is AOT compiled.
128     return false;
129   } else {
130     JVMCIRuntime* runtime = JVMCI::java_runtime();
131     if (runtime != NULL) {
132       JVMCIObject receiver = runtime->probe_HotSpotJVMCIRuntime();
133       if (receiver.is_null()) {
134         return false;
135       }
136       JVMCIEnv* ignored_env = NULL;
137       objArrayHandle excludeModules(JavaThread::current(), HotSpotJVMCI::HotSpotJVMCIRuntime::excludeFromJVMCICompilation(ignored_env, HotSpotJVMCI::resolve(receiver)));
138       if (excludeModules.not_null()) {
139         ModuleEntry* moduleEntry = method->method_holder()->module();
140         for (int i = 0; i < excludeModules->length(); i++) {
141           if (excludeModules->obj_at(i) == moduleEntry->module()) {
142             return true;
143           }
144         }
145       }
146     }
147     return false;
148   }
149 }
150 
151 // Compilation entry point for methods
152 void JVMCICompiler::compile_method(ciEnv* env, ciMethod* target, int entry_bci, bool install_code, DirectiveSet* directive) {
153   ShouldNotReachHere();
154 }
155 
156 // Print CompileBroker compilation timers
157 void JVMCICompiler::print_timers() {
158   double code_install_time = _codeInstallTimer.seconds();
159   tty->print_cr("    JVMCI CompileBroker Time:");
160   tty->print_cr("       Compile:        %7.3f s", stats()->total_time());
161   tty->print_cr("       Install Code:   %7.3f s", code_install_time);
162 }
163 
164 // Print non-CompileBroker compilation timers
165 void JVMCICompiler::print_hosted_timers() {
166   double code_install_time = _hostedCodeInstallTimer.seconds();
167   tty->print_cr("    JVMCI Hosted Time:");
168   tty->print_cr("       Install Code:   %7.3f s", code_install_time);
169 }
170 
171 void JVMCICompiler::inc_methods_compiled() {
172   Atomic::inc(&_methods_compiled);
173   Atomic::inc(&_global_compilation_ticks);
174 }
175 
176 void JVMCICompiler::inc_global_compilation_ticks() {
177   Atomic::inc(&_global_compilation_ticks);
178 }