< prev index next >

src/share/vm/code/dependencies.cpp

Print this page




   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 
  25 #include "precompiled.hpp"
  26 #include "ci/ciArrayKlass.hpp"
  27 #include "ci/ciEnv.hpp"
  28 #include "ci/ciKlass.hpp"

  29 #include "ci/ciMethod.hpp"
  30 #include "classfile/javaClasses.inline.hpp"
  31 #include "code/dependencies.hpp"
  32 #include "compiler/compileLog.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/handles.hpp"
  35 #include "runtime/handles.inline.hpp"

  36 #include "runtime/thread.inline.hpp"
  37 #include "utilities/copy.hpp"
  38 
  39 
  40 #ifdef ASSERT
  41 static bool must_be_in_vm() {
  42   Thread* thread = Thread::current();
  43   if (thread->is_Java_thread())
  44     return ((JavaThread*)thread)->thread_state() == _thread_in_vm;
  45   else
  46     return true;  //something like this: thread->is_VM_thread();
  47 }
  48 #endif //ASSERT
  49 








  50 void Dependencies::initialize(ciEnv* env) {
  51   Arena* arena = env->arena();
  52   _oop_recorder = env->oop_recorder();
  53   _log = env->log();
  54   _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);
  55   DEBUG_ONLY(_deps[end_marker] = NULL);
  56   for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {
  57     _deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0);
  58   }
  59   _content_bytes = NULL;
  60   _size_in_bytes = (size_t)-1;
  61 
  62   assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");
  63 }
  64 
  65 void Dependencies::assert_evol_method(ciMethod* m) {
  66   assert_common_1(evol_method, m);
  67 }
  68 
  69 void Dependencies::assert_leaf_type(ciKlass* ctxk) {


 103 
 104 void Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) {
 105   check_ctxk(ctxk);
 106   assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2);
 107 }
 108 
 109 void Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) {
 110   check_ctxk(ctxk);
 111   assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2);
 112 }
 113 
 114 void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {
 115   check_ctxk(ctxk);
 116   assert_common_1(no_finalizable_subclasses, ctxk);
 117 }
 118 
 119 void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {
 120   assert_common_2(call_site_target_value, call_site, method_handle);
 121 }
 122 














 123 // Helper function.  If we are adding a new dep. under ctxk2,
 124 // try to find an old dep. under a broader* ctxk1.  If there is
 125 //
 126 bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
 127                                     int ctxk_i, ciKlass* ctxk2) {
 128   ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass();
 129   if (ctxk2->is_subtype_of(ctxk1)) {
 130     return true;  // success, and no need to change
 131   } else if (ctxk1->is_subtype_of(ctxk2)) {
 132     // new context class fully subsumes previous one
 133     deps->at_put(ctxk_i, ctxk2);
 134     return true;
 135   } else {
 136     return false;
 137   }
 138 }
 139 
 140 void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) {
 141   assert(dep_args(dept) == 1, "sanity");
 142   log_dependency(dept, x);


 355 
 356   // check whether the dept byte encoding really works
 357   assert((jbyte)default_context_type_bit != 0, "byte overflow");
 358 
 359   _content_bytes = bytes.buffer();
 360   _size_in_bytes = bytes.position();
 361 }
 362 
 363 
 364 const char* Dependencies::_dep_name[TYPE_LIMIT] = {
 365   "end_marker",
 366   "evol_method",
 367   "leaf_type",
 368   "abstract_with_unique_concrete_subtype",
 369   "abstract_with_no_concrete_subtype",
 370   "concrete_with_no_concrete_subtype",
 371   "unique_concrete_method",
 372   "abstract_with_exclusive_concrete_subtypes_2",
 373   "exclusive_concrete_methods_2",
 374   "no_finalizable_subclasses",
 375   "call_site_target_value"


 376 };
 377 
 378 int Dependencies::_dep_args[TYPE_LIMIT] = {
 379   -1,// end_marker
 380   1, // evol_method m
 381   1, // leaf_type ctxk
 382   2, // abstract_with_unique_concrete_subtype ctxk, k
 383   1, // abstract_with_no_concrete_subtype ctxk
 384   1, // concrete_with_no_concrete_subtype ctxk
 385   2, // unique_concrete_method ctxk, m
 386   3, // unique_concrete_subtypes_2 ctxk, k1, k2
 387   3, // unique_concrete_methods_2 ctxk, m1, m2
 388   1, // no_finalizable_subclasses ctxk
 389   2  // call_site_target_value call_site, method_handle


 390 };
 391 
 392 const char* Dependencies::dep_name(Dependencies::DepType dept) {
 393   if (!dept_in_mask(dept, all_types))  return "?bad-dep?";
 394   return _dep_name[dept];
 395 }
 396 
 397 int Dependencies::dep_args(Dependencies::DepType dept) {
 398   if (!dept_in_mask(dept, all_types))  return -1;
 399   return _dep_args[dept];
 400 }
 401 
 402 void Dependencies::check_valid_dependency_type(DepType dept) {
 403   guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, err_msg("invalid dependency type: %d", (int) dept));
 404 }
 405 
 406 // for the sake of the compiler log, print out current dependencies:
 407 void Dependencies::log_all_dependencies() {
 408   if (log() == NULL)  return;
 409   ResourceMark rm;


1514 
1515 Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) {
1516   assert(!oopDesc::is_null(call_site), "sanity");
1517   assert(!oopDesc::is_null(method_handle), "sanity");
1518   assert(call_site->is_a(SystemDictionary::CallSite_klass()),     "sanity");
1519 
1520   if (changes == NULL) {
1521     // Validate all CallSites
1522     if (java_lang_invoke_CallSite::target(call_site) != method_handle)
1523       return call_site->klass();  // assertion failed
1524   } else {
1525     // Validate the given CallSite
1526     if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {
1527       assert(method_handle != changes->method_handle(), "must be");
1528       return call_site->klass();  // assertion failed
1529     }
1530   }
1531   return NULL;  // assertion still valid
1532 }
1533 
















1534 void Dependencies::DepStream::trace_and_log_witness(Klass* witness) {
1535   if (witness != NULL) {
1536     if (TraceDependencies) {
1537       print_dependency(witness, /*verbose=*/ true);
1538     }
1539     // The following is a no-op unless logging is enabled:
1540     log_dependency(witness);
1541   }
1542 }
1543 
1544 
1545 Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {
1546   assert_locked_or_safepoint(Compile_lock);
1547   Dependencies::check_valid_dependency_type(type());
1548 
1549   Klass* witness = NULL;
1550   switch (type()) {
1551   case evol_method:
1552     witness = check_evol_method(method_argument(0));
1553     break;


1594     witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes);
1595     break;
1596   default:
1597     witness = NULL;
1598     break;
1599   }
1600   trace_and_log_witness(witness);
1601   return witness;
1602 }
1603 
1604 
1605 Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {
1606   // Handle klass dependency
1607   if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))
1608     return check_klass_dependency(changes.as_klass_change());
1609 
1610   // Handle CallSite dependency
1611   if (changes.is_call_site_change())
1612     return check_call_site_dependency(changes.as_call_site_change());
1613 



















1614   // irrelevant dependency; skip it
1615   return NULL;
1616 }
1617 
1618 
1619 void DepChange::print() {






1620   int nsup = 0, nint = 0;
1621   for (ContextStream str(*this); str.next(); ) {
1622     Klass* k = str.klass();
1623     switch (str.change_type()) {
1624     case Change_new_type:
1625       tty->print_cr("  dependee = %s", InstanceKlass::cast(k)->external_name());
1626       break;
1627     case Change_new_sub:
1628       if (!WizardMode) {
1629         ++nsup;
1630       } else {
1631         tty->print_cr("  context super = %s", InstanceKlass::cast(k)->external_name());
1632       }
1633       break;
1634     case Change_new_impl:
1635       if (!WizardMode) {
1636         ++nint;
1637       } else {
1638         tty->print_cr("  context interface = %s", InstanceKlass::cast(k)->external_name());
1639       }




   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 
  25 #include "precompiled.hpp"
  26 #include "ci/ciArrayKlass.hpp"
  27 #include "ci/ciEnv.hpp"
  28 #include "ci/ciKlass.hpp"
  29 #include "ci/ciField.hpp"
  30 #include "ci/ciMethod.hpp"
  31 #include "classfile/javaClasses.inline.hpp"
  32 #include "code/dependencies.hpp"
  33 #include "compiler/compileLog.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/handles.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/perfData.hpp"
  38 #include "runtime/thread.inline.hpp"
  39 #include "utilities/copy.hpp"
  40 

  41 #ifdef ASSERT
  42 static bool must_be_in_vm() {
  43   Thread* thread = Thread::current();
  44   if (thread->is_Java_thread())
  45     return ((JavaThread*)thread)->thread_state() == _thread_in_vm;
  46   else
  47     return true;  //something like this: thread->is_VM_thread();
  48 }
  49 #endif //ASSERT
  50 
  51 // Globals
  52 
  53 PerfCounter*    Dependencies::_perf_dependency_checking_time = NULL;
  54 PerfCounter*    Dependencies::_perf_dependencies_checked_count = NULL;
  55 PerfCounter*    Dependencies::_perf_dependencies_invalidated = NULL;
  56 PerfCounter*    Dependencies::_perf_dependencies_total_count = NULL;
  57 PerfCounter*    Dependencies::_perf_dependencies_context_traversals = NULL;
  58 
  59 void Dependencies::initialize(ciEnv* env) {
  60   Arena* arena = env->arena();
  61   _oop_recorder = env->oop_recorder();
  62   _log = env->log();
  63   _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);
  64   DEBUG_ONLY(_deps[end_marker] = NULL);
  65   for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {
  66     _deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0);
  67   }
  68   _content_bytes = NULL;
  69   _size_in_bytes = (size_t)-1;
  70 
  71   assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");
  72 }
  73 
  74 void Dependencies::assert_evol_method(ciMethod* m) {
  75   assert_common_1(evol_method, m);
  76 }
  77 
  78 void Dependencies::assert_leaf_type(ciKlass* ctxk) {


 112 
 113 void Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) {
 114   check_ctxk(ctxk);
 115   assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2);
 116 }
 117 
 118 void Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) {
 119   check_ctxk(ctxk);
 120   assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2);
 121 }
 122 
 123 void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {
 124   check_ctxk(ctxk);
 125   assert_common_1(no_finalizable_subclasses, ctxk);
 126 }
 127 
 128 void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {
 129   assert_common_2(call_site_target_value, call_site, method_handle);
 130 }
 131 
 132 void Dependencies::assert_constant_field_value_klass(ciField* field, ciKlass* ctxk) {
 133   // FIXME: how to record a field? no metadata associated; offset is int
 134   assert_common_1(constant_field_value_klass, ctxk /*, field*/);
 135 }
 136 
 137 void Dependencies::assert_constant_field_value_instance(ciField* field, ciObject* obj) {
 138   if (field->holder()->set_finals()) {
 139     // FIXME: how to record a field? no metadata associated; offset is int
 140     assert_common_2(constant_field_value_instance, field->holder(), /*field,*/ obj);
 141   } else {
 142     assert_constant_field_value_klass(field, field->holder());
 143   }
 144 }
 145 
 146 // Helper function.  If we are adding a new dep. under ctxk2,
 147 // try to find an old dep. under a broader* ctxk1.  If there is
 148 //
 149 bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
 150                                     int ctxk_i, ciKlass* ctxk2) {
 151   ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass();
 152   if (ctxk2->is_subtype_of(ctxk1)) {
 153     return true;  // success, and no need to change
 154   } else if (ctxk1->is_subtype_of(ctxk2)) {
 155     // new context class fully subsumes previous one
 156     deps->at_put(ctxk_i, ctxk2);
 157     return true;
 158   } else {
 159     return false;
 160   }
 161 }
 162 
 163 void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) {
 164   assert(dep_args(dept) == 1, "sanity");
 165   log_dependency(dept, x);


 378 
 379   // check whether the dept byte encoding really works
 380   assert((jbyte)default_context_type_bit != 0, "byte overflow");
 381 
 382   _content_bytes = bytes.buffer();
 383   _size_in_bytes = bytes.position();
 384 }
 385 
 386 
 387 const char* Dependencies::_dep_name[TYPE_LIMIT] = {
 388   "end_marker",
 389   "evol_method",
 390   "leaf_type",
 391   "abstract_with_unique_concrete_subtype",
 392   "abstract_with_no_concrete_subtype",
 393   "concrete_with_no_concrete_subtype",
 394   "unique_concrete_method",
 395   "abstract_with_exclusive_concrete_subtypes_2",
 396   "exclusive_concrete_methods_2",
 397   "no_finalizable_subclasses",
 398   "call_site_target_value",
 399   "constant_field_value_instance",
 400   "constant_field_value_klass"
 401 };
 402 
 403 int Dependencies::_dep_args[TYPE_LIMIT] = {
 404   -1,// end_marker
 405   1, // evol_method m
 406   1, // leaf_type ctxk
 407   2, // abstract_with_unique_concrete_subtype ctxk, k
 408   1, // abstract_with_no_concrete_subtype ctxk
 409   1, // concrete_with_no_concrete_subtype ctxk
 410   2, // unique_concrete_method ctxk, m
 411   3, // unique_concrete_subtypes_2 ctxk, k1, k2
 412   3, // unique_concrete_methods_2 ctxk, m1, m2
 413   1, // no_finalizable_subclasses ctxk
 414   2, // call_site_target_value call_site, method_handle
 415   2, // constant_field_value_instance ctxk oop
 416   1  // constant_field_value_klass ctxk
 417 };
 418 
 419 const char* Dependencies::dep_name(Dependencies::DepType dept) {
 420   if (!dept_in_mask(dept, all_types))  return "?bad-dep?";
 421   return _dep_name[dept];
 422 }
 423 
 424 int Dependencies::dep_args(Dependencies::DepType dept) {
 425   if (!dept_in_mask(dept, all_types))  return -1;
 426   return _dep_args[dept];
 427 }
 428 
 429 void Dependencies::check_valid_dependency_type(DepType dept) {
 430   guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, err_msg("invalid dependency type: %d", (int) dept));
 431 }
 432 
 433 // for the sake of the compiler log, print out current dependencies:
 434 void Dependencies::log_all_dependencies() {
 435   if (log() == NULL)  return;
 436   ResourceMark rm;


1541 
1542 Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) {
1543   assert(!oopDesc::is_null(call_site), "sanity");
1544   assert(!oopDesc::is_null(method_handle), "sanity");
1545   assert(call_site->is_a(SystemDictionary::CallSite_klass()),     "sanity");
1546 
1547   if (changes == NULL) {
1548     // Validate all CallSites
1549     if (java_lang_invoke_CallSite::target(call_site) != method_handle)
1550       return call_site->klass();  // assertion failed
1551   } else {
1552     // Validate the given CallSite
1553     if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {
1554       assert(method_handle != changes->method_handle(), "must be");
1555       return call_site->klass();  // assertion failed
1556     }
1557   }
1558   return NULL;  // assertion still valid
1559 }
1560 
1561 void Dependencies::invalidate_dependent_nmethods(instanceKlassHandle ctxk, DepChange& changes, TRAPS) {
1562   MutexLocker mu(Compile_lock, THREAD);
1563 
1564   int marked = 0;
1565   {
1566     MutexLockerEx mu2(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1567     marked = ctxk->mark_dependent_nmethods(changes);
1568   }
1569   if (marked > 0) {
1570     ctxk->set_finals(true);
1571     // At least one nmethod has been marked for deoptimization
1572     VM_Deoptimize op;
1573     VMThread::execute(&op);
1574   }
1575 }
1576 
1577 void Dependencies::DepStream::trace_and_log_witness(Klass* witness) {
1578   if (witness != NULL) {
1579     if (TraceDependencies) {
1580       print_dependency(witness, /*verbose=*/ true);
1581     }
1582     // The following is a no-op unless logging is enabled:
1583     log_dependency(witness);
1584   }
1585 }
1586 
1587 
1588 Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {
1589   assert_locked_or_safepoint(Compile_lock);
1590   Dependencies::check_valid_dependency_type(type());
1591 
1592   Klass* witness = NULL;
1593   switch (type()) {
1594   case evol_method:
1595     witness = check_evol_method(method_argument(0));
1596     break;


1637     witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes);
1638     break;
1639   default:
1640     witness = NULL;
1641     break;
1642   }
1643   trace_and_log_witness(witness);
1644   return witness;
1645 }
1646 
1647 
1648 Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {
1649   // Handle klass dependency
1650   if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))
1651     return check_klass_dependency(changes.as_klass_change());
1652 
1653   // Handle CallSite dependency
1654   if (changes.is_call_site_change())
1655     return check_call_site_dependency(changes.as_call_site_change());
1656 
1657   if (changes.is_constant_field_change()) {
1658     Handle holder = changes.as_constant_field_change()->holder();
1659     int offset = changes.as_constant_field_change()->offset();
1660     int dep_offset = -1; // TODO: store offset in dependency
1661     switch (type()) {
1662       case constant_field_value_instance:
1663         if (holder.is_null())        return context_type(); // all oops
1664         if (holder() == argument_oop(1)) {
1665           if (offset == -1)          return context_type(); // all fields
1666           if (offset == dep_offset)  return context_type(); // same field
1667         }
1668         break;
1669       case constant_field_value_klass:
1670         if (offset == -1)          return context_type(); // all fields
1671         if (offset == dep_offset)  return context_type(); // same field
1672         break;
1673     }
1674   }
1675 
1676   // irrelevant dependency; skip it
1677   return NULL;
1678 }
1679 
1680 
1681 void DepChange::print() {
1682   if (is_klass_change())
1683     tty->print_cr("klass_change");
1684   if (is_call_site_change())
1685     tty->print_cr("call_site_change");
1686   if (is_constant_field_change())
1687     tty->print_cr("constant_field_change: offset=%d %s", as_constant_field_change()->offset(), as_constant_field_change()->holder()->print_string());
1688   int nsup = 0, nint = 0;
1689   for (ContextStream str(*this); str.next(); ) {
1690     Klass* k = str.klass();
1691     switch (str.change_type()) {
1692     case Change_new_type:
1693       tty->print_cr("  dependee = %s", InstanceKlass::cast(k)->external_name());
1694       break;
1695     case Change_new_sub:
1696       if (!WizardMode) {
1697         ++nsup;
1698       } else {
1699         tty->print_cr("  context super = %s", InstanceKlass::cast(k)->external_name());
1700       }
1701       break;
1702     case Change_new_impl:
1703       if (!WizardMode) {
1704         ++nint;
1705       } else {
1706         tty->print_cr("  context interface = %s", InstanceKlass::cast(k)->external_name());
1707       }


< prev index next >