hotspot/src/share/vm/aot/aotCompiledMethod.cpp
changeset 42650 1f304d0c888b
child 46260 5de61384fba6
child 43459 80727d67b5e2
equal deleted inserted replaced
42649:28238583a459 42650:1f304d0c888b
       
     1 /*
       
     2  * Copyright (c) 2016, 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 
       
    26 #include "aot/aotCodeHeap.hpp"
       
    27 #include "aot/aotLoader.hpp"
       
    28 #include "aot/compiledIC_aot.hpp"
       
    29 #include "code/codeCache.hpp"
       
    30 #include "code/compiledIC.hpp"
       
    31 #include "code/nativeInst.hpp"
       
    32 #include "compiler/compilerOracle.hpp"
       
    33 #include "gc/shared/cardTableModRefBS.hpp"
       
    34 #include "gc/shared/collectedHeap.hpp"
       
    35 #include "gc/shared/gcLocker.hpp"
       
    36 #include "jvmci/compilerRuntime.hpp"
       
    37 #include "jvmci/jvmciRuntime.hpp"
       
    38 #include "oops/method.hpp"
       
    39 #include "runtime/java.hpp"
       
    40 #include "runtime/os.hpp"
       
    41 #include "runtime/sharedRuntime.hpp"
       
    42 #include "utilities/array.hpp"
       
    43 #include "utilities/xmlstream.hpp"
       
    44 
       
    45 #include <dlfcn.h>
       
    46 #include <stdio.h>
       
    47 
       
    48 #if 0
       
    49 static void metadata_oops_do(Metadata** metadata_begin, Metadata **metadata_end, OopClosure* f) {
       
    50   // Visit the metadata/oops section
       
    51   for (Metadata** p = metadata_begin; p < metadata_end; p++) {
       
    52     Metadata* m = *p;
       
    53 
       
    54     intptr_t meta = (intptr_t)m;
       
    55     if ((meta & 1) == 1) {
       
    56       // already resolved
       
    57       m = (Metadata*)(meta & ~1);
       
    58     } else {
       
    59       continue;
       
    60     }
       
    61     assert(Metaspace::contains(m), "");
       
    62     if (m->is_method()) {
       
    63       m = ((Method*)m)->method_holder();
       
    64     }
       
    65     assert(m->is_klass(), "must be");
       
    66     oop o = ((Klass*)m)->klass_holder();
       
    67     if (o != NULL) {
       
    68       f->do_oop(&o);
       
    69     }
       
    70   }
       
    71 }
       
    72 #endif
       
    73 
       
    74 void AOTCompiledMethod::oops_do(OopClosure* f) {
       
    75   if (_oop != NULL) {
       
    76     f->do_oop(&_oop);
       
    77   }
       
    78 #if 0
       
    79   metadata_oops_do(metadata_begin(), metadata_end(), f);
       
    80 #endif
       
    81 }
       
    82 
       
    83 bool AOTCompiledMethod::do_unloading_oops(address low_boundary, BoolObjectClosure* is_alive, bool unloading_occurred) {
       
    84   return false;
       
    85 }
       
    86 
       
    87 oop AOTCompiledMethod::oop_at(int index) const {
       
    88   if (index == 0) { // 0 is reserved
       
    89     return NULL;
       
    90   }
       
    91   Metadata** entry = _metadata_got + (index - 1);
       
    92   intptr_t meta = (intptr_t)*entry;
       
    93   if ((meta & 1) == 1) {
       
    94     // already resolved
       
    95     Klass* k = (Klass*)(meta & ~1);
       
    96     return k->java_mirror();
       
    97   }
       
    98   // The entry is string which we need to resolve.
       
    99   const char* meta_name = _heap->get_name_at((int)meta);
       
   100   int klass_len = build_u2_from((address)meta_name);
       
   101   const char* klass_name = meta_name + 2;
       
   102   // Quick check the current method's holder.
       
   103   Klass* k = _method->method_holder();
       
   104 
       
   105   ResourceMark rm; // for signature_name()
       
   106   if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
       
   107     // Search klass in got cells in DSO which have this compiled method.
       
   108     k = _heap->get_klass_from_got(klass_name, klass_len, _method);
       
   109   }
       
   110   int method_name_len = build_u2_from((address)klass_name + klass_len);
       
   111   guarantee(method_name_len == 0, "only klass is expected here");
       
   112   meta = ((intptr_t)k) | 1;
       
   113   *entry = (Metadata*)meta; // Should be atomic on x64
       
   114   return k->java_mirror();
       
   115 }
       
   116 
       
   117 Metadata* AOTCompiledMethod::metadata_at(int index) const {
       
   118   if (index == 0) { // 0 is reserved
       
   119     return NULL;
       
   120   }
       
   121   assert(index - 1 < _metadata_size, "");
       
   122   {
       
   123     Metadata** entry = _metadata_got + (index - 1);
       
   124     intptr_t meta = (intptr_t)*entry;
       
   125     if ((meta & 1) == 1) {
       
   126       // already resolved
       
   127       Metadata *m = (Metadata*)(meta & ~1);
       
   128       return m;
       
   129     }
       
   130     // The entry is string which we need to resolve.
       
   131     const char* meta_name = _heap->get_name_at((int)meta);
       
   132     int klass_len = build_u2_from((address)meta_name);
       
   133     const char* klass_name = meta_name + 2;
       
   134     // Quick check the current method's holder.
       
   135     Klass* k = _method->method_holder();
       
   136     bool klass_matched = true;
       
   137 
       
   138     ResourceMark rm; // for signature_name() and find_method()
       
   139     if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
       
   140       // Search klass in got cells in DSO which have this compiled method.
       
   141       k = _heap->get_klass_from_got(klass_name, klass_len, _method);
       
   142       klass_matched = false;
       
   143     }
       
   144     int method_name_len = build_u2_from((address)klass_name + klass_len);
       
   145     if (method_name_len == 0) { // Array or Klass name only?
       
   146       meta = ((intptr_t)k) | 1;
       
   147       *entry = (Metadata*)meta; // Should be atomic on x64
       
   148       return (Metadata*)k;
       
   149     } else { // Method
       
   150       // Quick check the current method's name.
       
   151       Method* m = _method;
       
   152       int signature_len = build_u2_from((address)klass_name + klass_len + 2 + method_name_len);
       
   153       int full_len = 2 + klass_len + 2 + method_name_len + 2 + signature_len;
       
   154       if (!klass_matched || memcmp(_name, meta_name, full_len) != 0) { // Does not match?
       
   155         Thread* thread = Thread::current();
       
   156         KlassHandle klass = KlassHandle(thread, k);
       
   157         const char* method_name = klass_name + klass_len;
       
   158         m = AOTCodeHeap::find_method(klass, thread, method_name);
       
   159       }
       
   160       meta = ((intptr_t)m) | 1;
       
   161       *entry = (Metadata*)meta; // Should be atomic on x64
       
   162       return (Metadata*)m;
       
   163     }
       
   164     // need to resolve it here..., patching of GOT need to be CAS or atomic operation.
       
   165     // FIXIT: need methods for debuginfo.
       
   166     // return _method;
       
   167   }
       
   168   ShouldNotReachHere(); return NULL;
       
   169 }
       
   170 
       
   171 bool AOTCompiledMethod::make_not_entrant_helper(int new_state) {
       
   172   // Make sure the method is not flushed in case of a safepoint in code below.
       
   173   methodHandle the_method(method());
       
   174   NoSafepointVerifier nsv;
       
   175 
       
   176   {
       
   177     // Enter critical section.  Does not block for safepoint.
       
   178     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
       
   179 
       
   180     if (*_state_adr == new_state) {
       
   181       // another thread already performed this transition so nothing
       
   182       // to do, but return false to indicate this.
       
   183       return false;
       
   184     }
       
   185 
       
   186     // Change state
       
   187     OrderAccess::storestore();
       
   188     *_state_adr = new_state;
       
   189 
       
   190     // Log the transition once
       
   191     log_state_change();
       
   192 
       
   193 #ifdef TIERED
       
   194     // Remain non-entrant forever
       
   195     if (new_state == not_entrant && method() != NULL) {
       
   196         method()->set_aot_code(NULL);
       
   197     }
       
   198 #endif
       
   199 
       
   200     // Remove AOTCompiledMethod from method.
       
   201     if (method() != NULL && (method()->code() == this ||
       
   202                              method()->from_compiled_entry() == verified_entry_point())) {
       
   203       HandleMark hm;
       
   204       method()->clear_code(false /* already owns Patching_lock */);
       
   205     }
       
   206   } // leave critical region under Patching_lock
       
   207 
       
   208 
       
   209   if (TraceCreateZombies) {
       
   210     ResourceMark m;
       
   211     const char *new_state_str = (new_state == not_entrant) ? "not entrant" : "not used";
       
   212     tty->print_cr("aot method <" INTPTR_FORMAT "> %s code made %s", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null", new_state_str);
       
   213   }
       
   214 
       
   215   return true;
       
   216 }
       
   217 
       
   218 bool AOTCompiledMethod::make_entrant() {
       
   219   assert(!method()->is_old(), "reviving evolved method!");
       
   220   assert(*_state_adr != not_entrant, "%s", method()->has_aot_code() ? "has_aot_code() not cleared" : "caller didn't check has_aot_code()");
       
   221 
       
   222   // Make sure the method is not flushed in case of a safepoint in code below.
       
   223   methodHandle the_method(method());
       
   224   NoSafepointVerifier nsv;
       
   225 
       
   226   {
       
   227     // Enter critical section.  Does not block for safepoint.
       
   228     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
       
   229 
       
   230     if (*_state_adr == in_use) {
       
   231       // another thread already performed this transition so nothing
       
   232       // to do, but return false to indicate this.
       
   233       return false;
       
   234     }
       
   235 
       
   236     // Change state
       
   237     OrderAccess::storestore();
       
   238     *_state_adr = in_use;
       
   239 
       
   240     // Log the transition once
       
   241     log_state_change();
       
   242   } // leave critical region under Patching_lock
       
   243 
       
   244 
       
   245   if (TraceCreateZombies) {
       
   246     ResourceMark m;
       
   247     tty->print_cr("aot method <" INTPTR_FORMAT "> %s code made entrant", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null");
       
   248   }
       
   249 
       
   250   return true;
       
   251 }
       
   252 
       
   253 // We don't have full dependencies for AOT methods, so flushing is
       
   254 // more conservative than for nmethods.
       
   255 void AOTCompiledMethod::flush_evol_dependents_on(instanceKlassHandle dependee) {
       
   256   if (is_java_method()) {
       
   257     cleanup_inline_caches();
       
   258     mark_for_deoptimization();
       
   259     make_not_entrant();
       
   260   }
       
   261 }
       
   262 
       
   263 // Iterate over metadata calling this function.   Used by RedefineClasses
       
   264 // Copied from nmethod::metadata_do
       
   265 void AOTCompiledMethod::metadata_do(void f(Metadata*)) {
       
   266   address low_boundary = verified_entry_point();
       
   267   {
       
   268     // Visit all immediate references that are embedded in the instruction stream.
       
   269     RelocIterator iter(this, low_boundary);
       
   270     while (iter.next()) {
       
   271       if (iter.type() == relocInfo::metadata_type ) {
       
   272         metadata_Relocation* r = iter.metadata_reloc();
       
   273         // In this metadata, we must only follow those metadatas directly embedded in
       
   274         // the code.  Other metadatas (oop_index>0) are seen as part of
       
   275         // the metadata section below.
       
   276         assert(1 == (r->metadata_is_immediate()) +
       
   277                (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()),
       
   278                "metadata must be found in exactly one place");
       
   279         if (r->metadata_is_immediate() && r->metadata_value() != NULL) {
       
   280           Metadata* md = r->metadata_value();
       
   281           if (md != _method) f(md);
       
   282         }
       
   283       } else if (iter.type() == relocInfo::virtual_call_type) {
       
   284         // Check compiledIC holders associated with this nmethod
       
   285         CompiledIC *ic = CompiledIC_at(&iter);
       
   286         if (ic->is_icholder_call()) {
       
   287           CompiledICHolder* cichk = ic->cached_icholder();
       
   288           f(cichk->holder_method());
       
   289           f(cichk->holder_klass());
       
   290         } else {
       
   291           Metadata* ic_oop = ic->cached_metadata();
       
   292           if (ic_oop != NULL) {
       
   293             f(ic_oop);
       
   294           }
       
   295         }
       
   296       }
       
   297     }
       
   298   }
       
   299 
       
   300   // Visit the metadata section
       
   301   for (Metadata** p = metadata_begin(); p < metadata_end(); p++) {
       
   302     Metadata* m = *p;
       
   303 
       
   304     intptr_t meta = (intptr_t)m;
       
   305     if ((meta & 1) == 1) {
       
   306       // already resolved
       
   307       m = (Metadata*)(meta & ~1);
       
   308     } else {
       
   309       continue;
       
   310     }
       
   311     assert(Metaspace::contains(m), "");
       
   312     f(m);
       
   313   }
       
   314 
       
   315   // Visit metadata not embedded in the other places.
       
   316   if (_method != NULL) f(_method);
       
   317 }
       
   318 
       
   319 void AOTCompiledMethod::print() const {
       
   320   print_on(tty, "AOTCompiledMethod");
       
   321 }
       
   322 
       
   323 void AOTCompiledMethod::print_on(outputStream* st) const {
       
   324   print_on(st, "AOTCompiledMethod");
       
   325 }
       
   326 
       
   327 // Print out more verbose output usually for a newly created aot method.
       
   328 void AOTCompiledMethod::print_on(outputStream* st, const char* msg) const {
       
   329   if (st != NULL) {
       
   330     ttyLocker ttyl;
       
   331     st->print("%7d ", (int) st->time_stamp().milliseconds());
       
   332     st->print("%4d ", _aot_id);    // print compilation number
       
   333     st->print("    aot[%2d]", _heap->dso_id());
       
   334     // Stubs have _method == NULL
       
   335     st->print("   %s", (_method == NULL ? _name : _method->name_and_sig_as_C_string()));
       
   336     if (Verbose) {
       
   337       st->print(" entry at " INTPTR_FORMAT, p2i(_code));
       
   338     }
       
   339     if (msg != NULL) {
       
   340       st->print("   %s", msg);
       
   341     }
       
   342     st->cr();
       
   343   }
       
   344 }
       
   345 
       
   346 void AOTCompiledMethod::print_value_on(outputStream* st) const {
       
   347   st->print("AOTCompiledMethod ");
       
   348   print_on(st, NULL);
       
   349 }
       
   350 
       
   351 // Print a short set of xml attributes to identify this aot method.  The
       
   352 // output should be embedded in some other element.
       
   353 void AOTCompiledMethod::log_identity(xmlStream* log) const {
       
   354   log->print(" aot_id='%d'", _aot_id);
       
   355   log->print(" aot='%2d'", _heap->dso_id());
       
   356 }
       
   357 
       
   358 void AOTCompiledMethod::log_state_change() const {
       
   359   if (LogCompilation) {
       
   360     ResourceMark m;
       
   361     if (xtty != NULL) {
       
   362       ttyLocker ttyl;  // keep the following output all in one block
       
   363       if (*_state_adr == not_entrant) {
       
   364         xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'",
       
   365                          os::current_thread_id());
       
   366       } else if (*_state_adr == not_used) {
       
   367         xtty->begin_elem("make_not_used thread='" UINTX_FORMAT "'",
       
   368                          os::current_thread_id());
       
   369       } else if (*_state_adr == in_use) {
       
   370         xtty->begin_elem("make_entrant thread='" UINTX_FORMAT "'",
       
   371                          os::current_thread_id());
       
   372       }
       
   373       log_identity(xtty);
       
   374       xtty->stamp();
       
   375       xtty->end_elem();
       
   376     }
       
   377   }
       
   378   if (PrintCompilation) {
       
   379     ResourceMark m;
       
   380     if (*_state_adr == not_entrant) {
       
   381       print_on(tty, "made not entrant");
       
   382     } else if (*_state_adr == not_used) {
       
   383       print_on(tty, "made not used");
       
   384     } else if (*_state_adr == in_use) {
       
   385       print_on(tty, "made entrant");
       
   386     }
       
   387   }
       
   388 }
       
   389 
       
   390 
       
   391 NativeInstruction* PltNativeCallWrapper::get_load_instruction(virtual_call_Relocation* r) const {
       
   392   return nativeLoadGot_at(_call->plt_load_got());
       
   393 }
       
   394 
       
   395 void PltNativeCallWrapper::verify_resolve_call(address dest) const {
       
   396   CodeBlob* db = CodeCache::find_blob_unsafe(dest);
       
   397   if (db == NULL) {
       
   398     assert(dest == _call->plt_resolve_call(), "sanity");
       
   399   }
       
   400 }
       
   401 
       
   402 void PltNativeCallWrapper::set_to_interpreted(const methodHandle& method, CompiledICInfo& info) {
       
   403   assert(!info.to_aot(), "only for nmethod");
       
   404   CompiledPltStaticCall* csc = CompiledPltStaticCall::at(instruction_address());
       
   405   csc->set_to_interpreted(method, info.entry());
       
   406 }
       
   407 
       
   408 NativeCallWrapper* AOTCompiledMethod::call_wrapper_at(address call) const {
       
   409   return new PltNativeCallWrapper((NativePltCall*) call);
       
   410 }
       
   411 
       
   412 NativeCallWrapper* AOTCompiledMethod::call_wrapper_before(address return_pc) const {
       
   413   return new PltNativeCallWrapper(nativePltCall_before(return_pc));
       
   414 }
       
   415 
       
   416 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(Relocation* call_site) const {
       
   417   return CompiledPltStaticCall::at(call_site);
       
   418 }
       
   419 
       
   420 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(address call_site) const {
       
   421   return CompiledPltStaticCall::at(call_site);
       
   422 }
       
   423 
       
   424 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_before(address return_addr) const {
       
   425   return CompiledPltStaticCall::before(return_addr);
       
   426 }
       
   427 
       
   428 address AOTCompiledMethod::call_instruction_address(address pc) const {
       
   429   NativePltCall* pltcall = nativePltCall_before(pc);
       
   430   return pltcall->instruction_address();
       
   431 }
       
   432 
       
   433 bool AOTCompiledMethod::is_evol_dependent_on(Klass* dependee) {
       
   434   return !is_aot_runtime_stub() && _heap->is_dependent_method(dependee, this);
       
   435 }
       
   436 
       
   437 void AOTCompiledMethod::clear_inline_caches() {
       
   438   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
       
   439   if (is_zombie()) {
       
   440     return;
       
   441   }
       
   442 
       
   443   RelocIterator iter(this);
       
   444   while (iter.next()) {
       
   445     iter.reloc()->clear_inline_cache();
       
   446     if (iter.type() == relocInfo::opt_virtual_call_type) {
       
   447       CompiledIC* cic = CompiledIC_at(&iter);
       
   448       assert(cic->is_clean(), "!");
       
   449       nativePltCall_at(iter.addr())->set_stub_to_clean();
       
   450     }
       
   451   }
       
   452 }
       
   453