1 /*
  2  * Copyright (c) 2011, 2020, 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   assert(!is_c1_or_interpreter_only(), "JVMCI is launched, it's not c1/interpreter only mode");
 46   if (!UseCompiler || !EnableJVMCI || !UseJVMCICompiler || !should_perform_init()) {
 47     return;
 48   }
 49 
 50   set_state(initialized);
 51 }
 52 
 53 void JVMCICompiler::bootstrap(TRAPS) {
 54   assert(THREAD->is_Java_thread(), "must be");
 55   if (Arguments::mode() == Arguments::_int) {
 56     // Nothing to do in -Xint mode
 57     return;
 58   }
 59   _bootstrapping = true;
 60   ResourceMark rm;
 61   HandleMark hm;
 62   if (PrintBootstrap) {
 63     tty->print("Bootstrapping JVMCI");
 64   }
 65   jlong start = os::javaTimeNanos();
 66 
 67   Array<Method*>* objectMethods = SystemDictionary::Object_klass()->methods();
 68   // Initialize compile queue with a selected set of methods.
 69   int len = objectMethods->length();
 70   for (int i = 0; i < len; i++) {
 71     methodHandle mh(THREAD, objectMethods->at(i));
 72     if (!mh->is_native() && !mh->is_static() && !mh->is_initializer()) {
 73       ResourceMark rm;
 74       int hot_count = 10; // TODO: what's the appropriate value?
 75       CompileBroker::compile_method(mh, InvocationEntryBci, CompLevel_full_optimization, mh, hot_count, CompileTask::Reason_Bootstrap, THREAD);
 76     }
 77   }
 78 
 79   int qsize;
 80   bool first_round = true;
 81   int z = 0;
 82   do {
 83     // Loop until there is something in the queue.
 84     do {
 85       ((JavaThread*)THREAD)->sleep(100);
 86       qsize = CompileBroker::queue_size(CompLevel_full_optimization);
 87     } while (!_bootstrap_compilation_request_handled && first_round && qsize == 0);
 88     first_round = false;
 89     if (PrintBootstrap) {
 90       while (z < (_methods_compiled / 100)) {
 91         ++z;
 92         tty->print_raw(".");
 93       }
 94     }
 95   } while (qsize != 0);
 96 
 97   if (PrintBootstrap) {
 98     tty->print_cr(" in " JLONG_FORMAT " ms (compiled %d methods)",
 99                   (jlong)nanos_to_millis(os::javaTimeNanos() - start), _methods_compiled);
100   }
101   _bootstrapping = false;
102   JVMCI::java_runtime()->bootstrap_finished(CHECK);
103 }
104 
105 bool JVMCICompiler::force_comp_at_level_simple(const methodHandle& method) {
106   if (_bootstrapping) {
107     // When bootstrapping, the JVMCI compiler can compile its own methods.
108     return false;
109   }
110   if (UseJVMCINativeLibrary) {
111     // This mechanism exists to force compilation of a JVMCI compiler by C1
112     // to reduce the compilation time spent on the JVMCI compiler itself. In
113     // +UseJVMCINativeLibrary mode, the JVMCI compiler is AOT compiled.
114     return false;
115   } else {
116     JVMCIRuntime* runtime = JVMCI::java_runtime();
117     if (runtime != NULL) {
118       JVMCIObject receiver = runtime->probe_HotSpotJVMCIRuntime();
119       if (receiver.is_null()) {
120         return false;
121       }
122       JVMCIEnv* ignored_env = NULL;
123       objArrayHandle excludeModules(JavaThread::current(), HotSpotJVMCI::HotSpotJVMCIRuntime::excludeFromJVMCICompilation(ignored_env, HotSpotJVMCI::resolve(receiver)));
124       if (excludeModules.not_null()) {
125         ModuleEntry* moduleEntry = method->method_holder()->module();
126         for (int i = 0; i < excludeModules->length(); i++) {
127           if (excludeModules->obj_at(i) == moduleEntry->module()) {
128             return true;
129           }
130         }
131       }
132     }
133     return false;
134   }
135 }
136 
137 // Compilation entry point for methods
138 void JVMCICompiler::compile_method(ciEnv* env, ciMethod* target, int entry_bci, bool install_code, DirectiveSet* directive) {
139   ShouldNotReachHere();
140 }
141 
142 // Print compilation timers and statistics
143 void JVMCICompiler::print_timers() {
144   print_compilation_timers();
145 }
146 
147 // Print compilation timers and statistics
148 void JVMCICompiler::print_compilation_timers() {
149   JVMCI_event_1("JVMCICompiler::print_timers");
150   tty->print_cr("       JVMCI code install time:        %6.3f s",    _codeInstallTimer.seconds());
151 }