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 "jvm.h"
  26 #include "memory/oopFactory.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/javaCalls.hpp"
  30 #include "runtime/handles.hpp"
  31 #include "jvmci/jvmciCompiler.hpp"
  32 #include "jvmci/jvmciEnv.hpp"
  33 #include "jvmci/jvmciRuntime.hpp"
  34 #include "runtime/compilationPolicy.hpp"
  35 #include "runtime/globals_extension.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 
  38 JVMCICompiler* JVMCICompiler::_instance = NULL;
  39 elapsedTimer JVMCICompiler::_codeInstallTimer;
  40 
  41 JVMCICompiler::JVMCICompiler() : AbstractCompiler(compiler_jvmci) {
  42   _bootstrapping = false;
  43   _bootstrap_compilation_request_handled = false;
  44   _methods_compiled = 0;
  45   assert(_instance == NULL, "only one instance allowed");
  46   _instance = this;
  47 }
  48 
  49 // Initialization
  50 void JVMCICompiler::initialize() {
  51   if (!UseCompiler || !EnableJVMCI || !UseJVMCICompiler || !should_perform_init()) {
  52     return;
  53   }
  54 
  55   set_state(initialized);
  56 }
  57 
  58 void JVMCICompiler::bootstrap(TRAPS) {
  59   if (Arguments::mode() == Arguments::_int) {
  60     // Nothing to do in -Xint mode
  61     return;
  62   }
  63   _bootstrapping = true;
  64   ResourceMark rm;
  65   HandleMark hm;
  66   if (PrintBootstrap) {
  67     tty->print("Bootstrapping JVMCI");
  68   }
  69   jlong start = os::javaTimeMillis();
  70 
  71   Array<Method*>* objectMethods = SystemDictionary::Object_klass()->methods();
  72   // Initialize compile queue with a selected set of methods.
  73   int len = objectMethods->length();
  74   for (int i = 0; i < len; i++) {
  75     methodHandle mh = objectMethods->at(i);
  76     if (!mh->is_native() && !mh->is_static() && !mh->is_initializer()) {
  77       ResourceMark rm;
  78       int hot_count = 10; // TODO: what's the appropriate value?
  79       CompileBroker::compile_method(mh, InvocationEntryBci, CompLevel_full_optimization, mh, hot_count, CompileTask::Reason_Bootstrap, THREAD);
  80     }
  81   }
  82 
  83   int qsize;
  84   bool first_round = true;
  85   int z = 0;
  86   do {
  87     // Loop until there is something in the queue.
  88     do {
  89       os::sleep(THREAD, 100, true);
  90       qsize = CompileBroker::queue_size(CompLevel_full_optimization);
  91     } while (!_bootstrap_compilation_request_handled && first_round && qsize == 0);
  92     first_round = false;
  93     if (PrintBootstrap) {
  94       while (z < (_methods_compiled / 100)) {
  95         ++z;
  96         tty->print_raw(".");
  97       }
  98     }
  99   } while (qsize != 0);
 100 
 101   if (PrintBootstrap) {
 102     tty->print_cr(" in " JLONG_FORMAT " ms (compiled %d methods)", os::javaTimeMillis() - start, _methods_compiled);
 103   }
 104   _bootstrapping = false;
 105   JVMCI::compiler_runtime()->bootstrap_finished(CHECK);
 106 }
 107 
 108 // Compilation entry point for methods
 109 void JVMCICompiler::compile_method(ciEnv* env, ciMethod* target, int entry_bci, DirectiveSet* directive) {
 110   ShouldNotReachHere();
 111 }
 112 
 113 // Print compilation timers and statistics
 114 void JVMCICompiler::print_timers() {
 115   print_compilation_timers();
 116 }
 117 
 118 // Print compilation timers and statistics
 119 void JVMCICompiler::print_compilation_timers() {
 120   TRACE_jvmci_1("JVMCICompiler::print_timers");
 121   tty->print_cr("       JVMCI code install time:        %6.3f s",    _codeInstallTimer.seconds());
 122 }