1469 default:
1470 ShouldNotReachHere();
1471 }
1472
1473 // Setting +ProfileTraps fixes the following, on all platforms:
1474 // 4852688: ProfileInterpreter is off by default for ia64. The result is
1475 // infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the
1476 // recompile relies on a MethodData* to record heroic opt failures.
1477
1478 // Whether the interpreter is producing MDO data or not, we also need
1479 // to use the MDO to detect hot deoptimization points and control
1480 // aggressive optimization.
1481 bool inc_recompile_count = false;
1482 ProfileData* pdata = NULL;
1483 if (ProfileTraps && update_trap_state && trap_mdo != NULL) {
1484 assert(trap_mdo == get_method_data(thread, trap_method, false), "sanity");
1485 uint this_trap_count = 0;
1486 bool maybe_prior_trap = false;
1487 bool maybe_prior_recompile = false;
1488 pdata = query_update_method_data(trap_mdo, trap_bci, reason,
1489 //outputs:
1490 this_trap_count,
1491 maybe_prior_trap,
1492 maybe_prior_recompile);
1493 // Because the interpreter also counts null, div0, range, and class
1494 // checks, these traps from compiled code are double-counted.
1495 // This is harmless; it just means that the PerXTrapLimit values
1496 // are in effect a little smaller than they look.
1497
1498 DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
1499 if (per_bc_reason != Reason_none) {
1500 // Now take action based on the partially known per-BCI history.
1501 if (maybe_prior_trap
1502 && this_trap_count >= (uint)PerBytecodeTrapLimit) {
1503 // If there are too many traps at this BCI, force a recompile.
1504 // This will allow the compiler to see the limit overflow, and
1505 // take corrective action, if possible. The compiler generally
1506 // does not use the exact PerBytecodeTrapLimit value, but instead
1507 // changes its tactics if it sees any traps at all. This provides
1508 // a little hysteresis, delaying a recompile until a trap happens
1514 // one hot trap site, but begins to get fuzzy if there are
1515 // many sites. For example, if there are ten sites each
1516 // trapping two or more times, they each get the blame for
1517 // all of their traps.
1518 make_not_entrant = true;
1519 }
1520
1521 // Detect repeated recompilation at the same BCI, and enforce a limit.
1522 if (make_not_entrant && maybe_prior_recompile) {
1523 // More than one recompile at this point.
1524 inc_recompile_count = maybe_prior_trap;
1525 }
1526 } else {
1527 // For reasons which are not recorded per-bytecode, we simply
1528 // force recompiles unconditionally.
1529 // (Note that PerMethodRecompilationCutoff is enforced elsewhere.)
1530 make_not_entrant = true;
1531 }
1532
1533 // Go back to the compiler if there are too many traps in this method.
1534 if (this_trap_count >= (uint)PerMethodTrapLimit) {
1535 // If there are too many traps in this method, force a recompile.
1536 // This will allow the compiler to see the limit overflow, and
1537 // take corrective action, if possible.
1538 // (This condition is an unlikely backstop only, because the
1539 // PerBytecodeTrapLimit is more likely to take effect first,
1540 // if it is applicable.)
1541 make_not_entrant = true;
1542 }
1543
1544 // Here's more hysteresis: If there has been a recompile at
1545 // this trap point already, run the method in the interpreter
1546 // for a while to exercise it more thoroughly.
1547 if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) {
1548 reprofile = true;
1549 }
1550
1551 }
1552
1553 // Take requested actions on the method:
1554
1602 bool create_if_missing) {
1603 Thread* THREAD = thread;
1604 MethodData* mdo = m()->method_data();
1605 if (mdo == NULL && create_if_missing && !HAS_PENDING_EXCEPTION) {
1606 // Build an MDO. Ignore errors like OutOfMemory;
1607 // that simply means we won't have an MDO to update.
1608 Method::build_interpreter_method_data(m, THREAD);
1609 if (HAS_PENDING_EXCEPTION) {
1610 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
1611 CLEAR_PENDING_EXCEPTION;
1612 }
1613 mdo = m()->method_data();
1614 }
1615 return mdo;
1616 }
1617
1618 ProfileData*
1619 Deoptimization::query_update_method_data(MethodData* trap_mdo,
1620 int trap_bci,
1621 Deoptimization::DeoptReason reason,
1622 //outputs:
1623 uint& ret_this_trap_count,
1624 bool& ret_maybe_prior_trap,
1625 bool& ret_maybe_prior_recompile) {
1626 uint prior_trap_count = trap_mdo->trap_count(reason);
1627 uint this_trap_count = trap_mdo->inc_trap_count(reason);
1628
1629 // If the runtime cannot find a place to store trap history,
1630 // it is estimated based on the general condition of the method.
1631 // If the method has ever been recompiled, or has ever incurred
1632 // a trap with the present reason , then this BCI is assumed
1633 // (pessimistically) to be the culprit.
1634 bool maybe_prior_trap = (prior_trap_count != 0);
1635 bool maybe_prior_recompile = (trap_mdo->decompile_count() != 0);
1636 ProfileData* pdata = NULL;
1637
1638
1639 // For reasons which are recorded per bytecode, we check per-BCI data.
1640 DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
1641 if (per_bc_reason != Reason_none) {
1642 // Find the profile data for this BCI. If there isn't one,
1643 // try to allocate one from the MDO's set of spares.
1644 // This will let us detect a repeated trap at this point.
1645 pdata = trap_mdo->allocate_bci_to_data(trap_bci);
1646
1647 if (pdata != NULL) {
1648 // Query the trap state of this profile datum.
1649 int tstate0 = pdata->trap_state();
1650 if (!trap_state_has_reason(tstate0, per_bc_reason))
1651 maybe_prior_trap = false;
1652 if (!trap_state_is_recompiled(tstate0))
1653 maybe_prior_recompile = false;
1654
1655 // Update the trap state of this profile datum.
1656 int tstate1 = tstate0;
1657 // Record the reason.
1658 tstate1 = trap_state_add_reason(tstate1, per_bc_reason);
1659 // Store the updated state on the MDO, for next time.
1660 if (tstate1 != tstate0)
1661 pdata->set_trap_state(tstate1);
1662 } else {
1663 if (LogCompilation && xtty != NULL) {
1664 ttyLocker ttyl;
1665 // Missing MDP? Leave a small complaint in the log.
1666 xtty->elem("missing_mdp bci='%d'", trap_bci);
1667 }
1668 }
1669 }
1670
1671 // Return results:
1672 ret_this_trap_count = this_trap_count;
1673 ret_maybe_prior_trap = maybe_prior_trap;
1674 ret_maybe_prior_recompile = maybe_prior_recompile;
1675 return pdata;
1676 }
1677
1678 void
1679 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
1680 ResourceMark rm;
1681 // Ignored outputs:
1682 uint ignore_this_trap_count;
1683 bool ignore_maybe_prior_trap;
1684 bool ignore_maybe_prior_recompile;
1685 query_update_method_data(trap_mdo, trap_bci,
1686 (DeoptReason)reason,
1687 ignore_this_trap_count,
1688 ignore_maybe_prior_trap,
1689 ignore_maybe_prior_recompile);
1690 }
1691
1692 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* thread, jint trap_request) {
1693
1694 // Still in Java no safepoints
1695 {
1696 // This enters VM and may safepoint
1697 uncommon_trap_inner(thread, trap_request);
1698 }
1699 return fetch_unroll_info_helper(thread);
1700 }
1701
1702 // Local derived constants.
1703 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
1704 const int DS_REASON_MASK = DataLayout::trap_mask >> 1;
1705 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
1706
1794 Deoptimization::DeoptAction Deoptimization::_unloaded_action
1795 = Deoptimization::Action_reinterpret;
1796 const char* Deoptimization::_trap_reason_name[Reason_LIMIT] = {
1797 // Note: Keep this in sync. with enum DeoptReason.
1798 "none",
1799 "null_check",
1800 "null_assert",
1801 "range_check",
1802 "class_check",
1803 "array_check",
1804 "intrinsic",
1805 "bimorphic",
1806 "unloaded",
1807 "uninitialized",
1808 "unreached",
1809 "unhandled",
1810 "constraint",
1811 "div0_check",
1812 "age",
1813 "predicate",
1814 "loop_limit_check"
1815 };
1816 const char* Deoptimization::_trap_action_name[Action_LIMIT] = {
1817 // Note: Keep this in sync. with enum DeoptAction.
1818 "none",
1819 "maybe_recompile",
1820 "reinterpret",
1821 "make_not_entrant",
1822 "make_not_compilable"
1823 };
1824
1825 const char* Deoptimization::trap_reason_name(int reason) {
1826 if (reason == Reason_many) return "many";
1827 if ((uint)reason < Reason_LIMIT)
1828 return _trap_reason_name[reason];
1829 static char buf[20];
1830 sprintf(buf, "reason%d", reason);
1831 return buf;
1832 }
1833 const char* Deoptimization::trap_action_name(int action) {
1834 if ((uint)action < Action_LIMIT)
|
1469 default:
1470 ShouldNotReachHere();
1471 }
1472
1473 // Setting +ProfileTraps fixes the following, on all platforms:
1474 // 4852688: ProfileInterpreter is off by default for ia64. The result is
1475 // infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the
1476 // recompile relies on a MethodData* to record heroic opt failures.
1477
1478 // Whether the interpreter is producing MDO data or not, we also need
1479 // to use the MDO to detect hot deoptimization points and control
1480 // aggressive optimization.
1481 bool inc_recompile_count = false;
1482 ProfileData* pdata = NULL;
1483 if (ProfileTraps && update_trap_state && trap_mdo != NULL) {
1484 assert(trap_mdo == get_method_data(thread, trap_method, false), "sanity");
1485 uint this_trap_count = 0;
1486 bool maybe_prior_trap = false;
1487 bool maybe_prior_recompile = false;
1488 pdata = query_update_method_data(trap_mdo, trap_bci, reason,
1489 nm->method(),
1490 //outputs:
1491 this_trap_count,
1492 maybe_prior_trap,
1493 maybe_prior_recompile);
1494 // Because the interpreter also counts null, div0, range, and class
1495 // checks, these traps from compiled code are double-counted.
1496 // This is harmless; it just means that the PerXTrapLimit values
1497 // are in effect a little smaller than they look.
1498
1499 DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
1500 if (per_bc_reason != Reason_none) {
1501 // Now take action based on the partially known per-BCI history.
1502 if (maybe_prior_trap
1503 && this_trap_count >= (uint)PerBytecodeTrapLimit) {
1504 // If there are too many traps at this BCI, force a recompile.
1505 // This will allow the compiler to see the limit overflow, and
1506 // take corrective action, if possible. The compiler generally
1507 // does not use the exact PerBytecodeTrapLimit value, but instead
1508 // changes its tactics if it sees any traps at all. This provides
1509 // a little hysteresis, delaying a recompile until a trap happens
1515 // one hot trap site, but begins to get fuzzy if there are
1516 // many sites. For example, if there are ten sites each
1517 // trapping two or more times, they each get the blame for
1518 // all of their traps.
1519 make_not_entrant = true;
1520 }
1521
1522 // Detect repeated recompilation at the same BCI, and enforce a limit.
1523 if (make_not_entrant && maybe_prior_recompile) {
1524 // More than one recompile at this point.
1525 inc_recompile_count = maybe_prior_trap;
1526 }
1527 } else {
1528 // For reasons which are not recorded per-bytecode, we simply
1529 // force recompiles unconditionally.
1530 // (Note that PerMethodRecompilationCutoff is enforced elsewhere.)
1531 make_not_entrant = true;
1532 }
1533
1534 // Go back to the compiler if there are too many traps in this method.
1535 if (this_trap_count >= per_method_trap_limit(reason)) {
1536 // If there are too many traps in this method, force a recompile.
1537 // This will allow the compiler to see the limit overflow, and
1538 // take corrective action, if possible.
1539 // (This condition is an unlikely backstop only, because the
1540 // PerBytecodeTrapLimit is more likely to take effect first,
1541 // if it is applicable.)
1542 make_not_entrant = true;
1543 }
1544
1545 // Here's more hysteresis: If there has been a recompile at
1546 // this trap point already, run the method in the interpreter
1547 // for a while to exercise it more thoroughly.
1548 if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) {
1549 reprofile = true;
1550 }
1551
1552 }
1553
1554 // Take requested actions on the method:
1555
1603 bool create_if_missing) {
1604 Thread* THREAD = thread;
1605 MethodData* mdo = m()->method_data();
1606 if (mdo == NULL && create_if_missing && !HAS_PENDING_EXCEPTION) {
1607 // Build an MDO. Ignore errors like OutOfMemory;
1608 // that simply means we won't have an MDO to update.
1609 Method::build_interpreter_method_data(m, THREAD);
1610 if (HAS_PENDING_EXCEPTION) {
1611 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
1612 CLEAR_PENDING_EXCEPTION;
1613 }
1614 mdo = m()->method_data();
1615 }
1616 return mdo;
1617 }
1618
1619 ProfileData*
1620 Deoptimization::query_update_method_data(MethodData* trap_mdo,
1621 int trap_bci,
1622 Deoptimization::DeoptReason reason,
1623 Method* compiled_method,
1624 //outputs:
1625 uint& ret_this_trap_count,
1626 bool& ret_maybe_prior_trap,
1627 bool& ret_maybe_prior_recompile) {
1628 uint prior_trap_count = trap_mdo->trap_count(reason);
1629 uint this_trap_count = trap_mdo->inc_trap_count(reason);
1630
1631 // If the runtime cannot find a place to store trap history,
1632 // it is estimated based on the general condition of the method.
1633 // If the method has ever been recompiled, or has ever incurred
1634 // a trap with the present reason , then this BCI is assumed
1635 // (pessimistically) to be the culprit.
1636 bool maybe_prior_trap = (prior_trap_count != 0);
1637 bool maybe_prior_recompile = (trap_mdo->decompile_count() != 0);
1638 ProfileData* pdata = NULL;
1639
1640
1641 // For reasons which are recorded per bytecode, we check per-BCI data.
1642 DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
1643 if (per_bc_reason != Reason_none) {
1644 // Find the profile data for this BCI. If there isn't one,
1645 // try to allocate one from the MDO's set of spares.
1646 // This will let us detect a repeated trap at this point.
1647 pdata = trap_mdo->allocate_bci_to_data(trap_bci, reason_is_speculate(reason) ? compiled_method : NULL);
1648
1649 if (pdata != NULL) {
1650 if (reason_is_speculate(reason) && !pdata->is_SpeculativeTrapData()) {
1651 if (LogCompilation && xtty != NULL) {
1652 ttyLocker ttyl;
1653 // no more room for speculative traps in this MDO
1654 xtty->elem("speculative_traps_oom");
1655 }
1656 }
1657 // Query the trap state of this profile datum.
1658 int tstate0 = pdata->trap_state();
1659 if (!trap_state_has_reason(tstate0, per_bc_reason))
1660 maybe_prior_trap = false;
1661 if (!trap_state_is_recompiled(tstate0))
1662 maybe_prior_recompile = false;
1663
1664 // Update the trap state of this profile datum.
1665 int tstate1 = tstate0;
1666 // Record the reason.
1667 tstate1 = trap_state_add_reason(tstate1, per_bc_reason);
1668 // Store the updated state on the MDO, for next time.
1669 if (tstate1 != tstate0)
1670 pdata->set_trap_state(tstate1);
1671 } else {
1672 if (LogCompilation && xtty != NULL) {
1673 ttyLocker ttyl;
1674 // Missing MDP? Leave a small complaint in the log.
1675 xtty->elem("missing_mdp bci='%d'", trap_bci);
1676 }
1677 }
1678 }
1679
1680 // Return results:
1681 ret_this_trap_count = this_trap_count;
1682 ret_maybe_prior_trap = maybe_prior_trap;
1683 ret_maybe_prior_recompile = maybe_prior_recompile;
1684 return pdata;
1685 }
1686
1687 void
1688 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
1689 ResourceMark rm;
1690 // Ignored outputs:
1691 uint ignore_this_trap_count;
1692 bool ignore_maybe_prior_trap;
1693 bool ignore_maybe_prior_recompile;
1694 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
1695 query_update_method_data(trap_mdo, trap_bci,
1696 (DeoptReason)reason,
1697 NULL,
1698 ignore_this_trap_count,
1699 ignore_maybe_prior_trap,
1700 ignore_maybe_prior_recompile);
1701 }
1702
1703 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* thread, jint trap_request) {
1704
1705 // Still in Java no safepoints
1706 {
1707 // This enters VM and may safepoint
1708 uncommon_trap_inner(thread, trap_request);
1709 }
1710 return fetch_unroll_info_helper(thread);
1711 }
1712
1713 // Local derived constants.
1714 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
1715 const int DS_REASON_MASK = DataLayout::trap_mask >> 1;
1716 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
1717
1805 Deoptimization::DeoptAction Deoptimization::_unloaded_action
1806 = Deoptimization::Action_reinterpret;
1807 const char* Deoptimization::_trap_reason_name[Reason_LIMIT] = {
1808 // Note: Keep this in sync. with enum DeoptReason.
1809 "none",
1810 "null_check",
1811 "null_assert",
1812 "range_check",
1813 "class_check",
1814 "array_check",
1815 "intrinsic",
1816 "bimorphic",
1817 "unloaded",
1818 "uninitialized",
1819 "unreached",
1820 "unhandled",
1821 "constraint",
1822 "div0_check",
1823 "age",
1824 "predicate",
1825 "loop_limit_check",
1826 "speculate_class_check"
1827 };
1828 const char* Deoptimization::_trap_action_name[Action_LIMIT] = {
1829 // Note: Keep this in sync. with enum DeoptAction.
1830 "none",
1831 "maybe_recompile",
1832 "reinterpret",
1833 "make_not_entrant",
1834 "make_not_compilable"
1835 };
1836
1837 const char* Deoptimization::trap_reason_name(int reason) {
1838 if (reason == Reason_many) return "many";
1839 if ((uint)reason < Reason_LIMIT)
1840 return _trap_reason_name[reason];
1841 static char buf[20];
1842 sprintf(buf, "reason%d", reason);
1843 return buf;
1844 }
1845 const char* Deoptimization::trap_action_name(int action) {
1846 if ((uint)action < Action_LIMIT)
|