Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/ci/ciMethodData.cpp
32285 views
/*1* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "ci/ciMetadata.hpp"26#include "ci/ciMethodData.hpp"27#include "ci/ciReplay.hpp"28#include "ci/ciUtilities.hpp"29#include "memory/allocation.inline.hpp"30#include "memory/resourceArea.hpp"31#include "runtime/deoptimization.hpp"32#include "utilities/copy.hpp"3334// ciMethodData3536// ------------------------------------------------------------------37// ciMethodData::ciMethodData38//39ciMethodData::ciMethodData(MethodData* md) : ciMetadata(md) {40assert(md != NULL, "no null method data");41Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord));42_data = NULL;43_data_size = 0;44_extra_data_size = 0;45_current_mileage = 0;46_invocation_counter = 0;47_backedge_counter = 0;48_state = empty_state;49_saw_free_extra_data = false;50// Set an initial hint. Don't use set_hint_di() because51// first_di() may be out of bounds if data_size is 0.52_hint_di = first_di();53// Initialize the escape information (to "don't know.");54_eflags = _arg_local = _arg_stack = _arg_returned = 0;55_parameters = NULL;56}5758// ------------------------------------------------------------------59// ciMethodData::ciMethodData60//61// No MethodData*.62ciMethodData::ciMethodData() : ciMetadata(NULL) {63Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord));64_data = NULL;65_data_size = 0;66_extra_data_size = 0;67_current_mileage = 0;68_invocation_counter = 0;69_backedge_counter = 0;70_state = empty_state;71_saw_free_extra_data = false;72// Set an initial hint. Don't use set_hint_di() because73// first_di() may be out of bounds if data_size is 0.74_hint_di = first_di();75// Initialize the escape information (to "don't know.");76_eflags = _arg_local = _arg_stack = _arg_returned = 0;77_parameters = NULL;78}7980void ciMethodData::load_extra_data() {81MethodData* mdo = get_MethodData();8283// speculative trap entries also hold a pointer to a Method so need to be translated84DataLayout* dp_src = mdo->extra_data_base();85DataLayout* end_src = mdo->extra_data_limit();86DataLayout* dp_dst = extra_data_base();87for (;; dp_src = MethodData::next_extra(dp_src), dp_dst = MethodData::next_extra(dp_dst)) {88assert(dp_src < end_src, "moved past end of extra data");89// New traps in the MDO can be added as we translate the copy so90// look at the entries in the copy.91switch(dp_dst->tag()) {92case DataLayout::speculative_trap_data_tag: {93ciSpeculativeTrapData* data_dst = new ciSpeculativeTrapData(dp_dst);94SpeculativeTrapData* data_src = new SpeculativeTrapData(dp_src);95data_dst->translate_from(data_src);96break;97}98case DataLayout::bit_data_tag:99break;100case DataLayout::no_tag:101case DataLayout::arg_info_data_tag:102// An empty slot or ArgInfoData entry marks the end of the trap data103return;104default:105fatal(err_msg("bad tag = %d", dp_dst->tag()));106}107}108}109110void ciMethodData::load_data() {111MethodData* mdo = get_MethodData();112if (mdo == NULL) {113return;114}115116// To do: don't copy the data if it is not "ripe" -- require a minimum #117// of invocations.118119// Snapshot the data -- actually, take an approximate snapshot of120// the data. Any concurrently executing threads may be changing the121// data as we copy it.122Copy::disjoint_words_atomic((HeapWord*) mdo,123(HeapWord*) &_orig,124sizeof(_orig) / HeapWordSize);125Arena* arena = CURRENT_ENV->arena();126_data_size = mdo->data_size();127_extra_data_size = mdo->extra_data_size();128int total_size = _data_size + _extra_data_size;129_data = (intptr_t *) arena->Amalloc(total_size);130Copy::disjoint_words_atomic((HeapWord*) mdo->data_base(),131(HeapWord*) _data,132total_size / HeapWordSize);133134// Traverse the profile data, translating any oops into their135// ci equivalents.136ResourceMark rm;137ciProfileData* ci_data = first_data();138ProfileData* data = mdo->first_data();139while (is_valid(ci_data)) {140ci_data->translate_from(data);141ci_data = next_data(ci_data);142data = mdo->next_data(data);143}144if (mdo->parameters_type_data() != NULL) {145_parameters = data_layout_at(mdo->parameters_type_data_di());146ciParametersTypeData* parameters = new ciParametersTypeData(_parameters);147parameters->translate_from(mdo->parameters_type_data());148}149150load_extra_data();151152// Note: Extra data are all BitData, and do not need translation.153_current_mileage = MethodData::mileage_of(mdo->method());154_invocation_counter = mdo->invocation_count();155_backedge_counter = mdo->backedge_count();156_state = mdo->is_mature()? mature_state: immature_state;157158_eflags = mdo->eflags();159_arg_local = mdo->arg_local();160_arg_stack = mdo->arg_stack();161_arg_returned = mdo->arg_returned();162#ifndef PRODUCT163if (ReplayCompiles) {164ciReplay::initialize(this);165}166#endif167}168169void ciReceiverTypeData::translate_receiver_data_from(const ProfileData* data) {170for (uint row = 0; row < row_limit(); row++) {171Klass* k = data->as_ReceiverTypeData()->receiver(row);172if (k != NULL) {173ciKlass* klass = CURRENT_ENV->get_klass(k);174CURRENT_ENV->ensure_metadata_alive(klass);175set_receiver(row, klass);176}177}178}179180181void ciTypeStackSlotEntries::translate_type_data_from(const TypeStackSlotEntries* entries) {182for (int i = 0; i < _number_of_entries; i++) {183intptr_t k = entries->type(i);184TypeStackSlotEntries::set_type(i, translate_klass(k));185}186}187188void ciReturnTypeEntry::translate_type_data_from(const ReturnTypeEntry* ret) {189intptr_t k = ret->type();190set_type(translate_klass(k));191}192193void ciSpeculativeTrapData::translate_from(const ProfileData* data) {194Method* m = data->as_SpeculativeTrapData()->method();195ciMethod* ci_m = CURRENT_ENV->get_method(m);196CURRENT_ENV->ensure_metadata_alive(ci_m);197set_method(ci_m);198}199200// Get the data at an arbitrary (sort of) data index.201ciProfileData* ciMethodData::data_at(int data_index) {202if (out_of_bounds(data_index)) {203return NULL;204}205DataLayout* data_layout = data_layout_at(data_index);206207switch (data_layout->tag()) {208case DataLayout::no_tag:209default:210ShouldNotReachHere();211return NULL;212case DataLayout::bit_data_tag:213return new ciBitData(data_layout);214case DataLayout::counter_data_tag:215return new ciCounterData(data_layout);216case DataLayout::jump_data_tag:217return new ciJumpData(data_layout);218case DataLayout::receiver_type_data_tag:219return new ciReceiverTypeData(data_layout);220case DataLayout::virtual_call_data_tag:221return new ciVirtualCallData(data_layout);222case DataLayout::ret_data_tag:223return new ciRetData(data_layout);224case DataLayout::branch_data_tag:225return new ciBranchData(data_layout);226case DataLayout::multi_branch_data_tag:227return new ciMultiBranchData(data_layout);228case DataLayout::arg_info_data_tag:229return new ciArgInfoData(data_layout);230case DataLayout::call_type_data_tag:231return new ciCallTypeData(data_layout);232case DataLayout::virtual_call_type_data_tag:233return new ciVirtualCallTypeData(data_layout);234case DataLayout::parameters_type_data_tag:235return new ciParametersTypeData(data_layout);236};237}238239// Iteration over data.240ciProfileData* ciMethodData::next_data(ciProfileData* current) {241int current_index = dp_to_di(current->dp());242int next_index = current_index + current->size_in_bytes();243ciProfileData* next = data_at(next_index);244return next;245}246247ciProfileData* ciMethodData::bci_to_extra_data(int bci, ciMethod* m, bool& two_free_slots) {248// bci_to_extra_data(bci) ...249DataLayout* dp = data_layout_at(data_size());250DataLayout* end = data_layout_at(data_size() + extra_data_size());251two_free_slots = false;252for (;dp < end; dp = MethodData::next_extra(dp)) {253switch(dp->tag()) {254case DataLayout::no_tag:255_saw_free_extra_data = true; // observed an empty slot (common case)256two_free_slots = (MethodData::next_extra(dp)->tag() == DataLayout::no_tag);257return NULL;258case DataLayout::arg_info_data_tag:259return NULL; // ArgInfoData is at the end of extra data section.260case DataLayout::bit_data_tag:261if (m == NULL && dp->bci() == bci) {262return new ciBitData(dp);263}264break;265case DataLayout::speculative_trap_data_tag: {266ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp);267// data->method() might be null if the MDO is snapshotted268// concurrently with a trap269if (m != NULL && data->method() == m && dp->bci() == bci) {270return data;271}272break;273}274default:275fatal(err_msg("bad tag = %d", dp->tag()));276}277}278return NULL;279}280281// Translate a bci to its corresponding data, or NULL.282ciProfileData* ciMethodData::bci_to_data(int bci, ciMethod* m) {283// If m is not NULL we look for a SpeculativeTrapData entry284if (m == NULL) {285ciProfileData* data = data_before(bci);286for ( ; is_valid(data); data = next_data(data)) {287if (data->bci() == bci) {288set_hint_di(dp_to_di(data->dp()));289return data;290} else if (data->bci() > bci) {291break;292}293}294}295bool two_free_slots = false;296ciProfileData* result = bci_to_extra_data(bci, m, two_free_slots);297if (result != NULL) {298return result;299}300if (m != NULL && !two_free_slots) {301// We were looking for a SpeculativeTrapData entry we didn't302// find. Room is not available for more SpeculativeTrapData303// entries, look in the non SpeculativeTrapData entries.304return bci_to_data(bci, NULL);305}306return NULL;307}308309// Conservatively decode the trap_state of a ciProfileData.310int ciMethodData::has_trap_at(ciProfileData* data, int reason) {311typedef Deoptimization::DeoptReason DR_t;312int per_bc_reason313= Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason);314if (trap_count(reason) == 0) {315// Impossible for this trap to have occurred, regardless of trap_state.316// Note: This happens if the MDO is empty.317return 0;318} else if (per_bc_reason == Deoptimization::Reason_none) {319// We cannot conclude anything; a trap happened somewhere, maybe here.320return -1;321} else if (data == NULL) {322// No profile here, not even an extra_data record allocated on the fly.323// If there are empty extra_data records, and there had been a trap,324// there would have been a non-null data pointer. If there are no325// free extra_data records, we must return a conservative -1.326if (_saw_free_extra_data)327return 0; // Q.E.D.328else329return -1; // bail with a conservative answer330} else {331return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason);332}333}334335int ciMethodData::trap_recompiled_at(ciProfileData* data) {336if (data == NULL) {337return (_saw_free_extra_data? 0: -1); // (see previous method)338} else {339return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0;340}341}342343void ciMethodData::clear_escape_info() {344VM_ENTRY_MARK;345MethodData* mdo = get_MethodData();346if (mdo != NULL) {347mdo->clear_escape_info();348ArgInfoData *aid = arg_info();349int arg_count = (aid == NULL) ? 0 : aid->number_of_args();350for (int i = 0; i < arg_count; i++) {351set_arg_modified(i, 0);352}353}354_eflags = _arg_local = _arg_stack = _arg_returned = 0;355}356357// copy our escape info to the MethodData* if it exists358void ciMethodData::update_escape_info() {359VM_ENTRY_MARK;360MethodData* mdo = get_MethodData();361if ( mdo != NULL) {362mdo->set_eflags(_eflags);363mdo->set_arg_local(_arg_local);364mdo->set_arg_stack(_arg_stack);365mdo->set_arg_returned(_arg_returned);366int arg_count = mdo->method()->size_of_parameters();367for (int i = 0; i < arg_count; i++) {368mdo->set_arg_modified(i, arg_modified(i));369}370}371}372373void ciMethodData::set_compilation_stats(short loops, short blocks) {374VM_ENTRY_MARK;375MethodData* mdo = get_MethodData();376if (mdo != NULL) {377mdo->set_num_loops(loops);378mdo->set_num_blocks(blocks);379}380}381382void ciMethodData::set_would_profile(bool p) {383VM_ENTRY_MARK;384MethodData* mdo = get_MethodData();385if (mdo != NULL) {386mdo->set_would_profile(p);387}388}389390void ciMethodData::set_argument_type(int bci, int i, ciKlass* k) {391VM_ENTRY_MARK;392MethodData* mdo = get_MethodData();393if (mdo != NULL) {394ProfileData* data = mdo->bci_to_data(bci);395if (data != NULL) {396if (data->is_CallTypeData()) {397data->as_CallTypeData()->set_argument_type(i, k->get_Klass());398} else {399assert(data->is_VirtualCallTypeData(), "no arguments!");400data->as_VirtualCallTypeData()->set_argument_type(i, k->get_Klass());401}402}403}404}405406void ciMethodData::set_parameter_type(int i, ciKlass* k) {407VM_ENTRY_MARK;408MethodData* mdo = get_MethodData();409if (mdo != NULL) {410mdo->parameters_type_data()->set_type(i, k->get_Klass());411}412}413414void ciMethodData::set_return_type(int bci, ciKlass* k) {415VM_ENTRY_MARK;416MethodData* mdo = get_MethodData();417if (mdo != NULL) {418ProfileData* data = mdo->bci_to_data(bci);419if (data != NULL) {420if (data->is_CallTypeData()) {421data->as_CallTypeData()->set_return_type(k->get_Klass());422} else {423assert(data->is_VirtualCallTypeData(), "no arguments!");424data->as_VirtualCallTypeData()->set_return_type(k->get_Klass());425}426}427}428}429430bool ciMethodData::has_escape_info() {431return eflag_set(MethodData::estimated);432}433434void ciMethodData::set_eflag(MethodData::EscapeFlag f) {435set_bits(_eflags, f);436}437438void ciMethodData::clear_eflag(MethodData::EscapeFlag f) {439clear_bits(_eflags, f);440}441442bool ciMethodData::eflag_set(MethodData::EscapeFlag f) const {443return mask_bits(_eflags, f) != 0;444}445446void ciMethodData::set_arg_local(int i) {447set_nth_bit(_arg_local, i);448}449450void ciMethodData::set_arg_stack(int i) {451set_nth_bit(_arg_stack, i);452}453454void ciMethodData::set_arg_returned(int i) {455set_nth_bit(_arg_returned, i);456}457458void ciMethodData::set_arg_modified(int arg, uint val) {459ArgInfoData *aid = arg_info();460if (aid == NULL)461return;462assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");463aid->set_arg_modified(arg, val);464}465466bool ciMethodData::is_arg_local(int i) const {467return is_set_nth_bit(_arg_local, i);468}469470bool ciMethodData::is_arg_stack(int i) const {471return is_set_nth_bit(_arg_stack, i);472}473474bool ciMethodData::is_arg_returned(int i) const {475return is_set_nth_bit(_arg_returned, i);476}477478uint ciMethodData::arg_modified(int arg) const {479ArgInfoData *aid = arg_info();480if (aid == NULL)481return 0;482assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");483return aid->arg_modified(arg);484}485486ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) {487// Get offset within MethodData* of the data array488ByteSize data_offset = MethodData::data_offset();489490// Get cell offset of the ProfileData within data array491int cell_offset = dp_to_di(data->dp());492493// Add in counter_offset, the # of bytes into the ProfileData of counter or flag494int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data);495496return in_ByteSize(offset);497}498499ciArgInfoData *ciMethodData::arg_info() const {500// Should be last, have to skip all traps.501DataLayout* dp = data_layout_at(data_size());502DataLayout* end = data_layout_at(data_size() + extra_data_size());503for (; dp < end; dp = MethodData::next_extra(dp)) {504if (dp->tag() == DataLayout::arg_info_data_tag)505return new ciArgInfoData(dp);506}507return NULL;508}509510511// Implementation of the print method.512void ciMethodData::print_impl(outputStream* st) {513ciMetadata::print_impl(st);514}515516void ciMethodData::dump_replay_data(outputStream* out) {517ResourceMark rm;518MethodData* mdo = get_MethodData();519Method* method = mdo->method();520Klass* holder = method->method_holder();521out->print("ciMethodData %s %s %s %d %d",522holder->name()->as_quoted_ascii(),523method->name()->as_quoted_ascii(),524method->signature()->as_quoted_ascii(),525_state,526current_mileage());527528// dump the contents of the MDO header as raw data529unsigned char* orig = (unsigned char*)&_orig;530int length = sizeof(_orig);531out->print(" orig %d", length);532for (int i = 0; i < length; i++) {533out->print(" %d", orig[i]);534}535536// dump the MDO data as raw data537int elements = data_size() / sizeof(intptr_t);538out->print(" data %d", elements);539for (int i = 0; i < elements; i++) {540// We could use INTPTR_FORMAT here but that's a zero justified541// which makes comparing it with the SA version of this output542// harder.543#ifdef _LP64544out->print(" 0x%" FORMAT64_MODIFIER "x", data()[i]);545#else546out->print(" 0x%x", data()[i]);547#endif548}549550// The MDO contained oop references as ciObjects, so scan for those551// and emit pairs of offset and klass name so that they can be552// reconstructed at runtime. The first round counts the number of553// oop references and the second actually emits them.554int count = 0;555for (int round = 0; round < 2; round++) {556if (round == 1) out->print(" oops %d", count);557ProfileData* pdata = first_data();558for ( ; is_valid(pdata); pdata = next_data(pdata)) {559if (pdata->is_ReceiverTypeData()) {560ciReceiverTypeData* vdata = (ciReceiverTypeData*)pdata;561for (uint i = 0; i < vdata->row_limit(); i++) {562ciKlass* k = vdata->receiver(i);563if (k != NULL) {564if (round == 0) {565count++;566} else {567out->print(" %d %s", (int)(dp_to_di(vdata->dp() + in_bytes(vdata->receiver_offset(i))) / sizeof(intptr_t)), k->name()->as_quoted_ascii());568}569}570}571} else if (pdata->is_VirtualCallData()) {572ciVirtualCallData* vdata = (ciVirtualCallData*)pdata;573for (uint i = 0; i < vdata->row_limit(); i++) {574ciKlass* k = vdata->receiver(i);575if (k != NULL) {576if (round == 0) {577count++;578} else {579out->print(" %d %s", (int)(dp_to_di(vdata->dp() + in_bytes(vdata->receiver_offset(i))) / sizeof(intptr_t)), k->name()->as_quoted_ascii());580}581}582}583}584}585}586out->cr();587}588589#ifndef PRODUCT590void ciMethodData::print() {591print_data_on(tty);592}593594void ciMethodData::print_data_on(outputStream* st) {595ResourceMark rm;596ciProfileData* data;597for (data = first_data(); is_valid(data); data = next_data(data)) {598st->print("%d", dp_to_di(data->dp()));599st->fill_to(6);600data->print_data_on(st);601}602st->print_cr("--- Extra data:");603DataLayout* dp = data_layout_at(data_size());604DataLayout* end = data_layout_at(data_size() + extra_data_size());605for (;; dp = MethodData::next_extra(dp)) {606assert(dp < end, "moved past end of extra data");607switch (dp->tag()) {608case DataLayout::no_tag:609continue;610case DataLayout::bit_data_tag:611data = new BitData(dp);612break;613case DataLayout::arg_info_data_tag:614data = new ciArgInfoData(dp);615dp = end; // ArgInfoData is at the end of extra data section.616break;617default:618fatal(err_msg("unexpected tag %d", dp->tag()));619}620st->print("%d", dp_to_di(data->dp()));621st->fill_to(6);622data->print_data_on(st);623if (dp >= end) return;624}625}626627void ciTypeEntries::print_ciklass(outputStream* st, intptr_t k) {628if (TypeEntries::is_type_none(k)) {629st->print("none");630} else if (TypeEntries::is_type_unknown(k)) {631st->print("unknown");632} else {633valid_ciklass(k)->print_name_on(st);634}635if (TypeEntries::was_null_seen(k)) {636st->print(" (null seen)");637}638}639640void ciTypeStackSlotEntries::print_data_on(outputStream* st) const {641for (int i = 0; i < _number_of_entries; i++) {642_pd->tab(st);643st->print("%d: stack (%u) ", i, stack_slot(i));644print_ciklass(st, type(i));645st->cr();646}647}648649void ciReturnTypeEntry::print_data_on(outputStream* st) const {650_pd->tab(st);651st->print("ret ");652print_ciklass(st, type());653st->cr();654}655656void ciCallTypeData::print_data_on(outputStream* st, const char* extra) const {657print_shared(st, "ciCallTypeData", extra);658if (has_arguments()) {659tab(st, true);660st->print("argument types");661args()->print_data_on(st);662}663if (has_return()) {664tab(st, true);665st->print("return type");666ret()->print_data_on(st);667}668}669670void ciReceiverTypeData::print_receiver_data_on(outputStream* st) const {671uint row;672int entries = 0;673for (row = 0; row < row_limit(); row++) {674if (receiver(row) != NULL) entries++;675}676st->print_cr("count(%u) entries(%u)", count(), entries);677for (row = 0; row < row_limit(); row++) {678if (receiver(row) != NULL) {679tab(st);680receiver(row)->print_name_on(st);681st->print_cr("(%u)", receiver_count(row));682}683}684}685686void ciReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {687print_shared(st, "ciReceiverTypeData", extra);688print_receiver_data_on(st);689}690691void ciVirtualCallData::print_data_on(outputStream* st, const char* extra) const {692print_shared(st, "ciVirtualCallData", extra);693rtd_super()->print_receiver_data_on(st);694}695696void ciVirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {697print_shared(st, "ciVirtualCallTypeData", extra);698rtd_super()->print_receiver_data_on(st);699if (has_arguments()) {700tab(st, true);701st->print("argument types");702args()->print_data_on(st);703}704if (has_return()) {705tab(st, true);706st->print("return type");707ret()->print_data_on(st);708}709}710711void ciParametersTypeData::print_data_on(outputStream* st, const char* extra) const {712st->print_cr("ciParametersTypeData");713parameters()->print_data_on(st);714}715716void ciSpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {717st->print_cr("ciSpeculativeTrapData");718tab(st);719method()->print_short_name(st);720st->cr();721}722#endif723724725