Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/graphKit.cpp
32285 views
/*1* Copyright (c) 2001, 2015, 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 "compiler/compileLog.hpp"26#include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"27#include "gc_implementation/g1/heapRegion.hpp"28#include "gc_interface/collectedHeap.hpp"29#include "gc_implementation/shenandoah/shenandoahHeap.hpp"30#include "memory/barrierSet.hpp"31#include "memory/cardTableModRefBS.hpp"32#include "opto/addnode.hpp"33#include "opto/graphKit.hpp"34#include "opto/idealKit.hpp"35#include "opto/locknode.hpp"36#include "opto/machnode.hpp"37#include "opto/parse.hpp"38#include "opto/rootnode.hpp"39#include "opto/runtime.hpp"40#include "runtime/deoptimization.hpp"41#include "runtime/sharedRuntime.hpp"4243#if INCLUDE_ALL_GCS44#include "gc_implementation/shenandoah/c2/shenandoahBarrierSetC2.hpp"45#include "gc_implementation/shenandoah/c2/shenandoahSupport.hpp"46#endif4748//----------------------------GraphKit-----------------------------------------49// Main utility constructor.50GraphKit::GraphKit(JVMState* jvms)51: Phase(Phase::Parser),52_env(C->env()),53_gvn(*C->initial_gvn())54{55_exceptions = jvms->map()->next_exception();56if (_exceptions != NULL) jvms->map()->set_next_exception(NULL);57set_jvms(jvms);58}5960// Private constructor for parser.61GraphKit::GraphKit()62: Phase(Phase::Parser),63_env(C->env()),64_gvn(*C->initial_gvn())65{66_exceptions = NULL;67set_map(NULL);68debug_only(_sp = -99);69debug_only(set_bci(-99));70}71727374//---------------------------clean_stack---------------------------------------75// Clear away rubbish from the stack area of the JVM state.76// This destroys any arguments that may be waiting on the stack.77void GraphKit::clean_stack(int from_sp) {78SafePointNode* map = this->map();79JVMState* jvms = this->jvms();80int stk_size = jvms->stk_size();81int stkoff = jvms->stkoff();82Node* top = this->top();83for (int i = from_sp; i < stk_size; i++) {84if (map->in(stkoff + i) != top) {85map->set_req(stkoff + i, top);86}87}88}899091//--------------------------------sync_jvms-----------------------------------92// Make sure our current jvms agrees with our parse state.93JVMState* GraphKit::sync_jvms() const {94JVMState* jvms = this->jvms();95jvms->set_bci(bci()); // Record the new bci in the JVMState96jvms->set_sp(sp()); // Record the new sp in the JVMState97assert(jvms_in_sync(), "jvms is now in sync");98return jvms;99}100101//--------------------------------sync_jvms_for_reexecute---------------------102// Make sure our current jvms agrees with our parse state. This version103// uses the reexecute_sp for reexecuting bytecodes.104JVMState* GraphKit::sync_jvms_for_reexecute() {105JVMState* jvms = this->jvms();106jvms->set_bci(bci()); // Record the new bci in the JVMState107jvms->set_sp(reexecute_sp()); // Record the new sp in the JVMState108return jvms;109}110111#ifdef ASSERT112bool GraphKit::jvms_in_sync() const {113Parse* parse = is_Parse();114if (parse == NULL) {115if (bci() != jvms()->bci()) return false;116if (sp() != (int)jvms()->sp()) return false;117return true;118}119if (jvms()->method() != parse->method()) return false;120if (jvms()->bci() != parse->bci()) return false;121int jvms_sp = jvms()->sp();122if (jvms_sp != parse->sp()) return false;123int jvms_depth = jvms()->depth();124if (jvms_depth != parse->depth()) return false;125return true;126}127128// Local helper checks for special internal merge points129// used to accumulate and merge exception states.130// They are marked by the region's in(0) edge being the map itself.131// Such merge points must never "escape" into the parser at large,132// until they have been handed to gvn.transform.133static bool is_hidden_merge(Node* reg) {134if (reg == NULL) return false;135if (reg->is_Phi()) {136reg = reg->in(0);137if (reg == NULL) return false;138}139return reg->is_Region() && reg->in(0) != NULL && reg->in(0)->is_Root();140}141142void GraphKit::verify_map() const {143if (map() == NULL) return; // null map is OK144assert(map()->req() <= jvms()->endoff(), "no extra garbage on map");145assert(!map()->has_exceptions(), "call add_exception_states_from 1st");146assert(!is_hidden_merge(control()), "call use_exception_state, not set_map");147}148149void GraphKit::verify_exception_state(SafePointNode* ex_map) {150assert(ex_map->next_exception() == NULL, "not already part of a chain");151assert(has_saved_ex_oop(ex_map), "every exception state has an ex_oop");152}153#endif154155//---------------------------stop_and_kill_map---------------------------------156// Set _map to NULL, signalling a stop to further bytecode execution.157// First smash the current map's control to a constant, to mark it dead.158void GraphKit::stop_and_kill_map() {159SafePointNode* dead_map = stop();160if (dead_map != NULL) {161dead_map->disconnect_inputs(NULL, C); // Mark the map as killed.162assert(dead_map->is_killed(), "must be so marked");163}164}165166167//--------------------------------stopped--------------------------------------168// Tell if _map is NULL, or control is top.169bool GraphKit::stopped() {170if (map() == NULL) return true;171else if (control() == top()) return true;172else return false;173}174175176//-----------------------------has_ex_handler----------------------------------177// Tell if this method or any caller method has exception handlers.178bool GraphKit::has_ex_handler() {179for (JVMState* jvmsp = jvms(); jvmsp != NULL; jvmsp = jvmsp->caller()) {180if (jvmsp->has_method() && jvmsp->method()->has_exception_handlers()) {181return true;182}183}184return false;185}186187//------------------------------save_ex_oop------------------------------------188// Save an exception without blowing stack contents or other JVM state.189void GraphKit::set_saved_ex_oop(SafePointNode* ex_map, Node* ex_oop) {190assert(!has_saved_ex_oop(ex_map), "clear ex-oop before setting again");191ex_map->add_req(ex_oop);192debug_only(verify_exception_state(ex_map));193}194195inline static Node* common_saved_ex_oop(SafePointNode* ex_map, bool clear_it) {196assert(GraphKit::has_saved_ex_oop(ex_map), "ex_oop must be there");197Node* ex_oop = ex_map->in(ex_map->req()-1);198if (clear_it) ex_map->del_req(ex_map->req()-1);199return ex_oop;200}201202//-----------------------------saved_ex_oop------------------------------------203// Recover a saved exception from its map.204Node* GraphKit::saved_ex_oop(SafePointNode* ex_map) {205return common_saved_ex_oop(ex_map, false);206}207208//--------------------------clear_saved_ex_oop---------------------------------209// Erase a previously saved exception from its map.210Node* GraphKit::clear_saved_ex_oop(SafePointNode* ex_map) {211return common_saved_ex_oop(ex_map, true);212}213214#ifdef ASSERT215//---------------------------has_saved_ex_oop----------------------------------216// Erase a previously saved exception from its map.217bool GraphKit::has_saved_ex_oop(SafePointNode* ex_map) {218return ex_map->req() == ex_map->jvms()->endoff()+1;219}220#endif221222//-------------------------make_exception_state--------------------------------223// Turn the current JVM state into an exception state, appending the ex_oop.224SafePointNode* GraphKit::make_exception_state(Node* ex_oop) {225sync_jvms();226SafePointNode* ex_map = stop(); // do not manipulate this map any more227set_saved_ex_oop(ex_map, ex_oop);228return ex_map;229}230231232//--------------------------add_exception_state--------------------------------233// Add an exception to my list of exceptions.234void GraphKit::add_exception_state(SafePointNode* ex_map) {235if (ex_map == NULL || ex_map->control() == top()) {236return;237}238#ifdef ASSERT239verify_exception_state(ex_map);240if (has_exceptions()) {241assert(ex_map->jvms()->same_calls_as(_exceptions->jvms()), "all collected exceptions must come from the same place");242}243#endif244245// If there is already an exception of exactly this type, merge with it.246// In particular, null-checks and other low-level exceptions common up here.247Node* ex_oop = saved_ex_oop(ex_map);248const Type* ex_type = _gvn.type(ex_oop);249if (ex_oop == top()) {250// No action needed.251return;252}253assert(ex_type->isa_instptr(), "exception must be an instance");254for (SafePointNode* e2 = _exceptions; e2 != NULL; e2 = e2->next_exception()) {255const Type* ex_type2 = _gvn.type(saved_ex_oop(e2));256// We check sp also because call bytecodes can generate exceptions257// both before and after arguments are popped!258if (ex_type2 == ex_type259&& e2->_jvms->sp() == ex_map->_jvms->sp()) {260combine_exception_states(ex_map, e2);261return;262}263}264265// No pre-existing exception of the same type. Chain it on the list.266push_exception_state(ex_map);267}268269//-----------------------add_exception_states_from-----------------------------270void GraphKit::add_exception_states_from(JVMState* jvms) {271SafePointNode* ex_map = jvms->map()->next_exception();272if (ex_map != NULL) {273jvms->map()->set_next_exception(NULL);274for (SafePointNode* next_map; ex_map != NULL; ex_map = next_map) {275next_map = ex_map->next_exception();276ex_map->set_next_exception(NULL);277add_exception_state(ex_map);278}279}280}281282//-----------------------transfer_exceptions_into_jvms-------------------------283JVMState* GraphKit::transfer_exceptions_into_jvms() {284if (map() == NULL) {285// We need a JVMS to carry the exceptions, but the map has gone away.286// Create a scratch JVMS, cloned from any of the exception states...287if (has_exceptions()) {288_map = _exceptions;289_map = clone_map();290_map->set_next_exception(NULL);291clear_saved_ex_oop(_map);292debug_only(verify_map());293} else {294// ...or created from scratch295JVMState* jvms = new (C) JVMState(_method, NULL);296jvms->set_bci(_bci);297jvms->set_sp(_sp);298jvms->set_map(new (C) SafePointNode(TypeFunc::Parms, jvms));299set_jvms(jvms);300for (uint i = 0; i < map()->req(); i++) map()->init_req(i, top());301set_all_memory(top());302while (map()->req() < jvms->endoff()) map()->add_req(top());303}304// (This is a kludge, in case you didn't notice.)305set_control(top());306}307JVMState* jvms = sync_jvms();308assert(!jvms->map()->has_exceptions(), "no exceptions on this map yet");309jvms->map()->set_next_exception(_exceptions);310_exceptions = NULL; // done with this set of exceptions311return jvms;312}313314static inline void add_n_reqs(Node* dstphi, Node* srcphi) {315assert(is_hidden_merge(dstphi), "must be a special merge node");316assert(is_hidden_merge(srcphi), "must be a special merge node");317uint limit = srcphi->req();318for (uint i = PhiNode::Input; i < limit; i++) {319dstphi->add_req(srcphi->in(i));320}321}322static inline void add_one_req(Node* dstphi, Node* src) {323assert(is_hidden_merge(dstphi), "must be a special merge node");324assert(!is_hidden_merge(src), "must not be a special merge node");325dstphi->add_req(src);326}327328//-----------------------combine_exception_states------------------------------329// This helper function combines exception states by building phis on a330// specially marked state-merging region. These regions and phis are331// untransformed, and can build up gradually. The region is marked by332// having a control input of its exception map, rather than NULL. Such333// regions do not appear except in this function, and in use_exception_state.334void GraphKit::combine_exception_states(SafePointNode* ex_map, SafePointNode* phi_map) {335if (failing()) return; // dying anyway...336JVMState* ex_jvms = ex_map->_jvms;337assert(ex_jvms->same_calls_as(phi_map->_jvms), "consistent call chains");338assert(ex_jvms->stkoff() == phi_map->_jvms->stkoff(), "matching locals");339assert(ex_jvms->sp() == phi_map->_jvms->sp(), "matching stack sizes");340assert(ex_jvms->monoff() == phi_map->_jvms->monoff(), "matching JVMS");341assert(ex_jvms->scloff() == phi_map->_jvms->scloff(), "matching scalar replaced objects");342assert(ex_map->req() == phi_map->req(), "matching maps");343uint tos = ex_jvms->stkoff() + ex_jvms->sp();344Node* hidden_merge_mark = root();345Node* region = phi_map->control();346MergeMemNode* phi_mem = phi_map->merged_memory();347MergeMemNode* ex_mem = ex_map->merged_memory();348if (region->in(0) != hidden_merge_mark) {349// The control input is not (yet) a specially-marked region in phi_map.350// Make it so, and build some phis.351region = new (C) RegionNode(2);352_gvn.set_type(region, Type::CONTROL);353region->set_req(0, hidden_merge_mark); // marks an internal ex-state354region->init_req(1, phi_map->control());355phi_map->set_control(region);356Node* io_phi = PhiNode::make(region, phi_map->i_o(), Type::ABIO);357record_for_igvn(io_phi);358_gvn.set_type(io_phi, Type::ABIO);359phi_map->set_i_o(io_phi);360for (MergeMemStream mms(phi_mem); mms.next_non_empty(); ) {361Node* m = mms.memory();362Node* m_phi = PhiNode::make(region, m, Type::MEMORY, mms.adr_type(C));363record_for_igvn(m_phi);364_gvn.set_type(m_phi, Type::MEMORY);365mms.set_memory(m_phi);366}367}368369// Either or both of phi_map and ex_map might already be converted into phis.370Node* ex_control = ex_map->control();371// if there is special marking on ex_map also, we add multiple edges from src372bool add_multiple = (ex_control->in(0) == hidden_merge_mark);373// how wide was the destination phi_map, originally?374uint orig_width = region->req();375376if (add_multiple) {377add_n_reqs(region, ex_control);378add_n_reqs(phi_map->i_o(), ex_map->i_o());379} else {380// ex_map has no merges, so we just add single edges everywhere381add_one_req(region, ex_control);382add_one_req(phi_map->i_o(), ex_map->i_o());383}384for (MergeMemStream mms(phi_mem, ex_mem); mms.next_non_empty2(); ) {385if (mms.is_empty()) {386// get a copy of the base memory, and patch some inputs into it387const TypePtr* adr_type = mms.adr_type(C);388Node* phi = mms.force_memory()->as_Phi()->slice_memory(adr_type);389assert(phi->as_Phi()->region() == mms.base_memory()->in(0), "");390mms.set_memory(phi);391// Prepare to append interesting stuff onto the newly sliced phi:392while (phi->req() > orig_width) phi->del_req(phi->req()-1);393}394// Append stuff from ex_map:395if (add_multiple) {396add_n_reqs(mms.memory(), mms.memory2());397} else {398add_one_req(mms.memory(), mms.memory2());399}400}401uint limit = ex_map->req();402for (uint i = TypeFunc::Parms; i < limit; i++) {403// Skip everything in the JVMS after tos. (The ex_oop follows.)404if (i == tos) i = ex_jvms->monoff();405Node* src = ex_map->in(i);406Node* dst = phi_map->in(i);407if (src != dst) {408PhiNode* phi;409if (dst->in(0) != region) {410dst = phi = PhiNode::make(region, dst, _gvn.type(dst));411record_for_igvn(phi);412_gvn.set_type(phi, phi->type());413phi_map->set_req(i, dst);414// Prepare to append interesting stuff onto the new phi:415while (dst->req() > orig_width) dst->del_req(dst->req()-1);416} else {417assert(dst->is_Phi(), "nobody else uses a hidden region");418phi = dst->as_Phi();419}420if (add_multiple && src->in(0) == ex_control) {421// Both are phis.422add_n_reqs(dst, src);423} else {424while (dst->req() < region->req()) add_one_req(dst, src);425}426const Type* srctype = _gvn.type(src);427if (phi->type() != srctype) {428const Type* dsttype = phi->type()->meet_speculative(srctype);429if (phi->type() != dsttype) {430phi->set_type(dsttype);431_gvn.set_type(phi, dsttype);432}433}434}435}436phi_map->merge_replaced_nodes_with(ex_map);437}438439//--------------------------use_exception_state--------------------------------440Node* GraphKit::use_exception_state(SafePointNode* phi_map) {441if (failing()) { stop(); return top(); }442Node* region = phi_map->control();443Node* hidden_merge_mark = root();444assert(phi_map->jvms()->map() == phi_map, "sanity: 1-1 relation");445Node* ex_oop = clear_saved_ex_oop(phi_map);446if (region->in(0) == hidden_merge_mark) {447// Special marking for internal ex-states. Process the phis now.448region->set_req(0, region); // now it's an ordinary region449set_jvms(phi_map->jvms()); // ...so now we can use it as a map450// Note: Setting the jvms also sets the bci and sp.451set_control(_gvn.transform(region));452uint tos = jvms()->stkoff() + sp();453for (uint i = 1; i < tos; i++) {454Node* x = phi_map->in(i);455if (x->in(0) == region) {456assert(x->is_Phi(), "expected a special phi");457phi_map->set_req(i, _gvn.transform(x));458}459}460for (MergeMemStream mms(merged_memory()); mms.next_non_empty(); ) {461Node* x = mms.memory();462if (x->in(0) == region) {463assert(x->is_Phi(), "nobody else uses a hidden region");464mms.set_memory(_gvn.transform(x));465}466}467if (ex_oop->in(0) == region) {468assert(ex_oop->is_Phi(), "expected a special phi");469ex_oop = _gvn.transform(ex_oop);470}471} else {472set_jvms(phi_map->jvms());473}474475assert(!is_hidden_merge(phi_map->control()), "hidden ex. states cleared");476assert(!is_hidden_merge(phi_map->i_o()), "hidden ex. states cleared");477return ex_oop;478}479480//---------------------------------java_bc-------------------------------------481Bytecodes::Code GraphKit::java_bc() const {482ciMethod* method = this->method();483int bci = this->bci();484if (method != NULL && bci != InvocationEntryBci)485return method->java_code_at_bci(bci);486else487return Bytecodes::_illegal;488}489490void GraphKit::uncommon_trap_if_should_post_on_exceptions(Deoptimization::DeoptReason reason,491bool must_throw) {492// if the exception capability is set, then we will generate code493// to check the JavaThread.should_post_on_exceptions flag to see494// if we actually need to report exception events (for this495// thread). If we don't need to report exception events, we will496// take the normal fast path provided by add_exception_events. If497// exception event reporting is enabled for this thread, we will498// take the uncommon_trap in the BuildCutout below.499500// first must access the should_post_on_exceptions_flag in this thread's JavaThread501Node* jthread = _gvn.transform(new (C) ThreadLocalNode());502Node* adr = basic_plus_adr(top(), jthread, in_bytes(JavaThread::should_post_on_exceptions_flag_offset()));503Node* should_post_flag = make_load(control(), adr, TypeInt::INT, T_INT, Compile::AliasIdxRaw, MemNode::unordered);504505// Test the should_post_on_exceptions_flag vs. 0506Node* chk = _gvn.transform( new (C) CmpINode(should_post_flag, intcon(0)) );507Node* tst = _gvn.transform( new (C) BoolNode(chk, BoolTest::eq) );508509// Branch to slow_path if should_post_on_exceptions_flag was true510{ BuildCutout unless(this, tst, PROB_MAX);511// Do not try anything fancy if we're notifying the VM on every throw.512// Cf. case Bytecodes::_athrow in parse2.cpp.513uncommon_trap(reason, Deoptimization::Action_none,514(ciKlass*)NULL, (char*)NULL, must_throw);515}516517}518519//------------------------------builtin_throw----------------------------------520void GraphKit::builtin_throw(Deoptimization::DeoptReason reason, Node* arg) {521bool must_throw = true;522523if (env()->jvmti_can_post_on_exceptions()) {524// check if we must post exception events, take uncommon trap if so525uncommon_trap_if_should_post_on_exceptions(reason, must_throw);526// here if should_post_on_exceptions is false527// continue on with the normal codegen528}529530// If this particular condition has not yet happened at this531// bytecode, then use the uncommon trap mechanism, and allow for532// a future recompilation if several traps occur here.533// If the throw is hot, try to use a more complicated inline mechanism534// which keeps execution inside the compiled code.535bool treat_throw_as_hot = false;536ciMethodData* md = method()->method_data();537538if (ProfileTraps) {539if (too_many_traps(reason)) {540treat_throw_as_hot = true;541}542// (If there is no MDO at all, assume it is early in543// execution, and that any deopts are part of the544// startup transient, and don't need to be remembered.)545546// Also, if there is a local exception handler, treat all throws547// as hot if there has been at least one in this method.548if (C->trap_count(reason) != 0549&& method()->method_data()->trap_count(reason) != 0550&& has_ex_handler()) {551treat_throw_as_hot = true;552}553}554555// If this throw happens frequently, an uncommon trap might cause556// a performance pothole. If there is a local exception handler,557// and if this particular bytecode appears to be deoptimizing often,558// let us handle the throw inline, with a preconstructed instance.559// Note: If the deopt count has blown up, the uncommon trap560// runtime is going to flush this nmethod, not matter what.561if (treat_throw_as_hot562&& (!StackTraceInThrowable || OmitStackTraceInFastThrow)) {563// If the throw is local, we use a pre-existing instance and564// punt on the backtrace. This would lead to a missing backtrace565// (a repeat of 4292742) if the backtrace object is ever asked566// for its backtrace.567// Fixing this remaining case of 4292742 requires some flavor of568// escape analysis. Leave that for the future.569ciInstance* ex_obj = NULL;570switch (reason) {571case Deoptimization::Reason_null_check:572ex_obj = env()->NullPointerException_instance();573break;574case Deoptimization::Reason_div0_check:575ex_obj = env()->ArithmeticException_instance();576break;577case Deoptimization::Reason_range_check:578ex_obj = env()->ArrayIndexOutOfBoundsException_instance();579break;580case Deoptimization::Reason_class_check:581if (java_bc() == Bytecodes::_aastore) {582ex_obj = env()->ArrayStoreException_instance();583} else {584ex_obj = env()->ClassCastException_instance();585}586break;587}588if (failing()) { stop(); return; } // exception allocation might fail589if (ex_obj != NULL) {590// Cheat with a preallocated exception object.591if (C->log() != NULL)592C->log()->elem("hot_throw preallocated='1' reason='%s'",593Deoptimization::trap_reason_name(reason));594const TypeInstPtr* ex_con = TypeInstPtr::make(ex_obj);595Node* ex_node = _gvn.transform( ConNode::make(C, ex_con) );596597// Clear the detail message of the preallocated exception object.598// Weblogic sometimes mutates the detail message of exceptions599// using reflection.600int offset = java_lang_Throwable::get_detailMessage_offset();601const TypePtr* adr_typ = ex_con->add_offset(offset);602603Node *adr = basic_plus_adr(ex_node, ex_node, offset);604const TypeOopPtr* val_type = TypeOopPtr::make_from_klass(env()->String_klass());605// Conservatively release stores of object references.606Node *store = store_oop_to_object(control(), ex_node, adr, adr_typ, null(), val_type, T_OBJECT, MemNode::release);607608add_exception_state(make_exception_state(ex_node));609return;610}611}612613// %%% Maybe add entry to OptoRuntime which directly throws the exc.?614// It won't be much cheaper than bailing to the interp., since we'll615// have to pass up all the debug-info, and the runtime will have to616// create the stack trace.617618// Usual case: Bail to interpreter.619// Reserve the right to recompile if we haven't seen anything yet.620621assert(!Deoptimization::reason_is_speculate(reason), "unsupported");622Deoptimization::DeoptAction action = Deoptimization::Action_maybe_recompile;623if (treat_throw_as_hot624&& (method()->method_data()->trap_recompiled_at(bci(), NULL)625|| C->too_many_traps(reason))) {626// We cannot afford to take more traps here. Suffer in the interpreter.627if (C->log() != NULL)628C->log()->elem("hot_throw preallocated='0' reason='%s' mcount='%d'",629Deoptimization::trap_reason_name(reason),630C->trap_count(reason));631action = Deoptimization::Action_none;632}633634// "must_throw" prunes the JVM state to include only the stack, if there635// are no local exception handlers. This should cut down on register636// allocation time and code size, by drastically reducing the number637// of in-edges on the call to the uncommon trap.638639uncommon_trap(reason, action, (ciKlass*)NULL, (char*)NULL, must_throw);640}641642643//----------------------------PreserveJVMState---------------------------------644PreserveJVMState::PreserveJVMState(GraphKit* kit, bool clone_map) {645debug_only(kit->verify_map());646_kit = kit;647_map = kit->map(); // preserve the map648_sp = kit->sp();649kit->set_map(clone_map ? kit->clone_map() : NULL);650#ifdef ASSERT651_bci = kit->bci();652Parse* parser = kit->is_Parse();653int block = (parser == NULL || parser->block() == NULL) ? -1 : parser->block()->rpo();654_block = block;655#endif656}657PreserveJVMState::~PreserveJVMState() {658GraphKit* kit = _kit;659#ifdef ASSERT660assert(kit->bci() == _bci, "bci must not shift");661Parse* parser = kit->is_Parse();662int block = (parser == NULL || parser->block() == NULL) ? -1 : parser->block()->rpo();663assert(block == _block, "block must not shift");664#endif665kit->set_map(_map);666kit->set_sp(_sp);667}668669670//-----------------------------BuildCutout-------------------------------------671BuildCutout::BuildCutout(GraphKit* kit, Node* p, float prob, float cnt)672: PreserveJVMState(kit)673{674assert(p->is_Con() || p->is_Bool(), "test must be a bool");675SafePointNode* outer_map = _map; // preserved map is caller's676SafePointNode* inner_map = kit->map();677IfNode* iff = kit->create_and_map_if(outer_map->control(), p, prob, cnt);678outer_map->set_control(kit->gvn().transform( new (kit->C) IfTrueNode(iff) ));679inner_map->set_control(kit->gvn().transform( new (kit->C) IfFalseNode(iff) ));680}681BuildCutout::~BuildCutout() {682GraphKit* kit = _kit;683assert(kit->stopped(), "cutout code must stop, throw, return, etc.");684}685686//---------------------------PreserveReexecuteState----------------------------687PreserveReexecuteState::PreserveReexecuteState(GraphKit* kit) {688assert(!kit->stopped(), "must call stopped() before");689_kit = kit;690_sp = kit->sp();691_reexecute = kit->jvms()->_reexecute;692}693PreserveReexecuteState::~PreserveReexecuteState() {694if (_kit->stopped()) return;695_kit->jvms()->_reexecute = _reexecute;696_kit->set_sp(_sp);697}698699//------------------------------clone_map--------------------------------------700// Implementation of PreserveJVMState701//702// Only clone_map(...) here. If this function is only used in the703// PreserveJVMState class we may want to get rid of this extra704// function eventually and do it all there.705706SafePointNode* GraphKit::clone_map() {707if (map() == NULL) return NULL;708709// Clone the memory edge first710Node* mem = MergeMemNode::make(C, map()->memory());711gvn().set_type_bottom(mem);712713SafePointNode *clonemap = (SafePointNode*)map()->clone();714JVMState* jvms = this->jvms();715JVMState* clonejvms = jvms->clone_shallow(C);716clonemap->set_memory(mem);717clonemap->set_jvms(clonejvms);718clonejvms->set_map(clonemap);719record_for_igvn(clonemap);720gvn().set_type_bottom(clonemap);721return clonemap;722}723724725//-----------------------------set_map_clone-----------------------------------726void GraphKit::set_map_clone(SafePointNode* m) {727_map = m;728_map = clone_map();729_map->set_next_exception(NULL);730debug_only(verify_map());731}732733734//----------------------------kill_dead_locals---------------------------------735// Detect any locals which are known to be dead, and force them to top.736void GraphKit::kill_dead_locals() {737// Consult the liveness information for the locals. If any738// of them are unused, then they can be replaced by top(). This739// should help register allocation time and cut down on the size740// of the deoptimization information.741742// This call is made from many of the bytecode handling743// subroutines called from the Big Switch in do_one_bytecode.744// Every bytecode which might include a slow path is responsible745// for killing its dead locals. The more consistent we746// are about killing deads, the fewer useless phis will be747// constructed for them at various merge points.748749// bci can be -1 (InvocationEntryBci). We return the entry750// liveness for the method.751752if (method() == NULL || method()->code_size() == 0) {753// We are building a graph for a call to a native method.754// All locals are live.755return;756}757758ResourceMark rm;759760// Consult the liveness information for the locals. If any761// of them are unused, then they can be replaced by top(). This762// should help register allocation time and cut down on the size763// of the deoptimization information.764MethodLivenessResult live_locals = method()->liveness_at_bci(bci());765766int len = (int)live_locals.size();767assert(len <= jvms()->loc_size(), "too many live locals");768for (int local = 0; local < len; local++) {769if (!live_locals.at(local)) {770set_local(local, top());771}772}773}774775#ifdef ASSERT776//-------------------------dead_locals_are_killed------------------------------777// Return true if all dead locals are set to top in the map.778// Used to assert "clean" debug info at various points.779bool GraphKit::dead_locals_are_killed() {780if (method() == NULL || method()->code_size() == 0) {781// No locals need to be dead, so all is as it should be.782return true;783}784785// Make sure somebody called kill_dead_locals upstream.786ResourceMark rm;787for (JVMState* jvms = this->jvms(); jvms != NULL; jvms = jvms->caller()) {788if (jvms->loc_size() == 0) continue; // no locals to consult789SafePointNode* map = jvms->map();790ciMethod* method = jvms->method();791int bci = jvms->bci();792if (jvms == this->jvms()) {793bci = this->bci(); // it might not yet be synched794}795MethodLivenessResult live_locals = method->liveness_at_bci(bci);796int len = (int)live_locals.size();797if (!live_locals.is_valid() || len == 0)798// This method is trivial, or is poisoned by a breakpoint.799return true;800assert(len == jvms->loc_size(), "live map consistent with locals map");801for (int local = 0; local < len; local++) {802if (!live_locals.at(local) && map->local(jvms, local) != top()) {803if (PrintMiscellaneous && (Verbose || WizardMode)) {804tty->print_cr("Zombie local %d: ", local);805jvms->dump();806}807return false;808}809}810}811return true;812}813814#endif //ASSERT815816// Helper function for enforcing certain bytecodes to reexecute if817// deoptimization happens818static bool should_reexecute_implied_by_bytecode(JVMState *jvms, bool is_anewarray) {819ciMethod* cur_method = jvms->method();820int cur_bci = jvms->bci();821if (cur_method != NULL && cur_bci != InvocationEntryBci) {822Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci);823return Interpreter::bytecode_should_reexecute(code) ||824is_anewarray && code == Bytecodes::_multianewarray;825// Reexecute _multianewarray bytecode which was replaced with826// sequence of [a]newarray. See Parse::do_multianewarray().827//828// Note: interpreter should not have it set since this optimization829// is limited by dimensions and guarded by flag so in some cases830// multianewarray() runtime calls will be generated and831// the bytecode should not be reexecutes (stack will not be reset).832} else833return false;834}835836// Helper function for adding JVMState and debug information to node837void GraphKit::add_safepoint_edges(SafePointNode* call, bool must_throw) {838// Add the safepoint edges to the call (or other safepoint).839840// Make sure dead locals are set to top. This841// should help register allocation time and cut down on the size842// of the deoptimization information.843assert(dead_locals_are_killed(), "garbage in debug info before safepoint");844845// Walk the inline list to fill in the correct set of JVMState's846// Also fill in the associated edges for each JVMState.847848// If the bytecode needs to be reexecuted we need to put849// the arguments back on the stack.850const bool should_reexecute = jvms()->should_reexecute();851JVMState* youngest_jvms = should_reexecute ? sync_jvms_for_reexecute() : sync_jvms();852853// NOTE: set_bci (called from sync_jvms) might reset the reexecute bit to854// undefined if the bci is different. This is normal for Parse but it855// should not happen for LibraryCallKit because only one bci is processed.856assert(!is_LibraryCallKit() || (jvms()->should_reexecute() == should_reexecute),857"in LibraryCallKit the reexecute bit should not change");858859// If we are guaranteed to throw, we can prune everything but the860// input to the current bytecode.861bool can_prune_locals = false;862uint stack_slots_not_pruned = 0;863int inputs = 0, depth = 0;864if (must_throw) {865assert(method() == youngest_jvms->method(), "sanity");866if (compute_stack_effects(inputs, depth)) {867can_prune_locals = true;868stack_slots_not_pruned = inputs;869}870}871872if (env()->should_retain_local_variables()) {873// At any safepoint, this method can get breakpointed, which would874// then require an immediate deoptimization.875can_prune_locals = false; // do not prune locals876stack_slots_not_pruned = 0;877}878879// do not scribble on the input jvms880JVMState* out_jvms = youngest_jvms->clone_deep(C);881call->set_jvms(out_jvms); // Start jvms list for call node882883// For a known set of bytecodes, the interpreter should reexecute them if884// deoptimization happens. We set the reexecute state for them here885if (out_jvms->is_reexecute_undefined() && //don't change if already specified886should_reexecute_implied_by_bytecode(out_jvms, call->is_AllocateArray())) {887out_jvms->set_should_reexecute(true); //NOTE: youngest_jvms not changed888}889890// Presize the call:891DEBUG_ONLY(uint non_debug_edges = call->req());892call->add_req_batch(top(), youngest_jvms->debug_depth());893assert(call->req() == non_debug_edges + youngest_jvms->debug_depth(), "");894895// Set up edges so that the call looks like this:896// Call [state:] ctl io mem fptr retadr897// [parms:] parm0 ... parmN898// [root:] loc0 ... locN stk0 ... stkSP mon0 obj0 ... monN objN899// [...mid:] loc0 ... locN stk0 ... stkSP mon0 obj0 ... monN objN [...]900// [young:] loc0 ... locN stk0 ... stkSP mon0 obj0 ... monN objN901// Note that caller debug info precedes callee debug info.902903// Fill pointer walks backwards from "young:" to "root:" in the diagram above:904uint debug_ptr = call->req();905906// Loop over the map input edges associated with jvms, add them907// to the call node, & reset all offsets to match call node array.908for (JVMState* in_jvms = youngest_jvms; in_jvms != NULL; ) {909uint debug_end = debug_ptr;910uint debug_start = debug_ptr - in_jvms->debug_size();911debug_ptr = debug_start; // back up the ptr912913uint p = debug_start; // walks forward in [debug_start, debug_end)914uint j, k, l;915SafePointNode* in_map = in_jvms->map();916out_jvms->set_map(call);917918if (can_prune_locals) {919assert(in_jvms->method() == out_jvms->method(), "sanity");920// If the current throw can reach an exception handler in this JVMS,921// then we must keep everything live that can reach that handler.922// As a quick and dirty approximation, we look for any handlers at all.923if (in_jvms->method()->has_exception_handlers()) {924can_prune_locals = false;925}926}927928// Add the Locals929k = in_jvms->locoff();930l = in_jvms->loc_size();931out_jvms->set_locoff(p);932if (!can_prune_locals) {933for (j = 0; j < l; j++)934call->set_req(p++, in_map->in(k+j));935} else {936p += l; // already set to top above by add_req_batch937}938939// Add the Expression Stack940k = in_jvms->stkoff();941l = in_jvms->sp();942out_jvms->set_stkoff(p);943if (!can_prune_locals) {944for (j = 0; j < l; j++)945call->set_req(p++, in_map->in(k+j));946} else if (can_prune_locals && stack_slots_not_pruned != 0) {947// Divide stack into {S0,...,S1}, where S0 is set to top.948uint s1 = stack_slots_not_pruned;949stack_slots_not_pruned = 0; // for next iteration950if (s1 > l) s1 = l;951uint s0 = l - s1;952p += s0; // skip the tops preinstalled by add_req_batch953for (j = s0; j < l; j++)954call->set_req(p++, in_map->in(k+j));955} else {956p += l; // already set to top above by add_req_batch957}958959// Add the Monitors960k = in_jvms->monoff();961l = in_jvms->mon_size();962out_jvms->set_monoff(p);963for (j = 0; j < l; j++)964call->set_req(p++, in_map->in(k+j));965966// Copy any scalar object fields.967k = in_jvms->scloff();968l = in_jvms->scl_size();969out_jvms->set_scloff(p);970for (j = 0; j < l; j++)971call->set_req(p++, in_map->in(k+j));972973// Finish the new jvms.974out_jvms->set_endoff(p);975976assert(out_jvms->endoff() == debug_end, "fill ptr must match");977assert(out_jvms->depth() == in_jvms->depth(), "depth must match");978assert(out_jvms->loc_size() == in_jvms->loc_size(), "size must match");979assert(out_jvms->mon_size() == in_jvms->mon_size(), "size must match");980assert(out_jvms->scl_size() == in_jvms->scl_size(), "size must match");981assert(out_jvms->debug_size() == in_jvms->debug_size(), "size must match");982983// Update the two tail pointers in parallel.984out_jvms = out_jvms->caller();985in_jvms = in_jvms->caller();986}987988assert(debug_ptr == non_debug_edges, "debug info must fit exactly");989990// Test the correctness of JVMState::debug_xxx accessors:991assert(call->jvms()->debug_start() == non_debug_edges, "");992assert(call->jvms()->debug_end() == call->req(), "");993assert(call->jvms()->debug_depth() == call->req() - non_debug_edges, "");994}995996bool GraphKit::compute_stack_effects(int& inputs, int& depth) {997Bytecodes::Code code = java_bc();998if (code == Bytecodes::_wide) {999code = method()->java_code_at_bci(bci() + 1);1000}10011002BasicType rtype = T_ILLEGAL;1003int rsize = 0;10041005if (code != Bytecodes::_illegal) {1006depth = Bytecodes::depth(code); // checkcast=0, athrow=-11007rtype = Bytecodes::result_type(code); // checkcast=P, athrow=V1008if (rtype < T_CONFLICT)1009rsize = type2size[rtype];1010}10111012switch (code) {1013case Bytecodes::_illegal:1014return false;10151016case Bytecodes::_ldc:1017case Bytecodes::_ldc_w:1018case Bytecodes::_ldc2_w:1019inputs = 0;1020break;10211022case Bytecodes::_dup: inputs = 1; break;1023case Bytecodes::_dup_x1: inputs = 2; break;1024case Bytecodes::_dup_x2: inputs = 3; break;1025case Bytecodes::_dup2: inputs = 2; break;1026case Bytecodes::_dup2_x1: inputs = 3; break;1027case Bytecodes::_dup2_x2: inputs = 4; break;1028case Bytecodes::_swap: inputs = 2; break;1029case Bytecodes::_arraylength: inputs = 1; break;10301031case Bytecodes::_getstatic:1032case Bytecodes::_putstatic:1033case Bytecodes::_getfield:1034case Bytecodes::_putfield:1035{1036bool ignored_will_link;1037ciField* field = method()->get_field_at_bci(bci(), ignored_will_link);1038int size = field->type()->size();1039bool is_get = (depth >= 0), is_static = (depth & 1);1040inputs = (is_static ? 0 : 1);1041if (is_get) {1042depth = size - inputs;1043} else {1044inputs += size; // putxxx pops the value from the stack1045depth = - inputs;1046}1047}1048break;10491050case Bytecodes::_invokevirtual:1051case Bytecodes::_invokespecial:1052case Bytecodes::_invokestatic:1053case Bytecodes::_invokedynamic:1054case Bytecodes::_invokeinterface:1055{1056bool ignored_will_link;1057ciSignature* declared_signature = NULL;1058ciMethod* ignored_callee = method()->get_method_at_bci(bci(), ignored_will_link, &declared_signature);1059assert(declared_signature != NULL, "cannot be null");1060inputs = declared_signature->arg_size_for_bc(code);1061int size = declared_signature->return_type()->size();1062depth = size - inputs;1063}1064break;10651066case Bytecodes::_multianewarray:1067{1068ciBytecodeStream iter(method());1069iter.reset_to_bci(bci());1070iter.next();1071inputs = iter.get_dimensions();1072assert(rsize == 1, "");1073depth = rsize - inputs;1074}1075break;10761077case Bytecodes::_ireturn:1078case Bytecodes::_lreturn:1079case Bytecodes::_freturn:1080case Bytecodes::_dreturn:1081case Bytecodes::_areturn:1082assert(rsize = -depth, "");1083inputs = rsize;1084break;10851086case Bytecodes::_jsr:1087case Bytecodes::_jsr_w:1088inputs = 0;1089depth = 1; // S.B. depth=1, not zero1090break;10911092default:1093// bytecode produces a typed result1094inputs = rsize - depth;1095assert(inputs >= 0, "");1096break;1097}10981099#ifdef ASSERT1100// spot check1101int outputs = depth + inputs;1102assert(outputs >= 0, "sanity");1103switch (code) {1104case Bytecodes::_checkcast: assert(inputs == 1 && outputs == 1, ""); break;1105case Bytecodes::_athrow: assert(inputs == 1 && outputs == 0, ""); break;1106case Bytecodes::_aload_0: assert(inputs == 0 && outputs == 1, ""); break;1107case Bytecodes::_return: assert(inputs == 0 && outputs == 0, ""); break;1108case Bytecodes::_drem: assert(inputs == 4 && outputs == 2, ""); break;1109}1110#endif //ASSERT11111112return true;1113}1114111511161117//------------------------------basic_plus_adr---------------------------------1118Node* GraphKit::basic_plus_adr(Node* base, Node* ptr, Node* offset) {1119// short-circuit a common case1120if (offset == intcon(0)) return ptr;1121return _gvn.transform( new (C) AddPNode(base, ptr, offset) );1122}11231124Node* GraphKit::ConvI2L(Node* offset) {1125// short-circuit a common case1126jint offset_con = find_int_con(offset, Type::OffsetBot);1127if (offset_con != Type::OffsetBot) {1128return longcon((jlong) offset_con);1129}1130return _gvn.transform( new (C) ConvI2LNode(offset));1131}11321133Node* GraphKit::ConvI2UL(Node* offset) {1134juint offset_con = (juint) find_int_con(offset, Type::OffsetBot);1135if (offset_con != (juint) Type::OffsetBot) {1136return longcon((julong) offset_con);1137}1138Node* conv = _gvn.transform( new (C) ConvI2LNode(offset));1139Node* mask = _gvn.transform( ConLNode::make(C, (julong) max_juint) );1140return _gvn.transform( new (C) AndLNode(conv, mask) );1141}11421143Node* GraphKit::ConvL2I(Node* offset) {1144// short-circuit a common case1145jlong offset_con = find_long_con(offset, (jlong)Type::OffsetBot);1146if (offset_con != (jlong)Type::OffsetBot) {1147return intcon((int) offset_con);1148}1149return _gvn.transform( new (C) ConvL2INode(offset));1150}11511152//-------------------------load_object_klass-----------------------------------1153Node* GraphKit::load_object_klass(Node* obj) {1154// Special-case a fresh allocation to avoid building nodes:1155Node* akls = AllocateNode::Ideal_klass(obj, &_gvn);1156if (akls != NULL) return akls;1157Node* k_adr = basic_plus_adr(obj, oopDesc::klass_offset_in_bytes());1158return _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), k_adr, TypeInstPtr::KLASS));1159}11601161//-------------------------load_array_length-----------------------------------1162Node* GraphKit::load_array_length(Node* array) {1163// Special-case a fresh allocation to avoid building nodes:1164AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(array, &_gvn);1165Node *alen;1166if (alloc == NULL) {1167Node *r_adr = basic_plus_adr(array, arrayOopDesc::length_offset_in_bytes());1168alen = _gvn.transform( new (C) LoadRangeNode(0, immutable_memory(), r_adr, TypeInt::POS));1169} else {1170alen = alloc->Ideal_length();1171Node* ccast = alloc->make_ideal_length(_gvn.type(array)->is_oopptr(), &_gvn);1172if (ccast != alen) {1173alen = _gvn.transform(ccast);1174}1175}1176return alen;1177}11781179//------------------------------do_null_check----------------------------------1180// Helper function to do a NULL pointer check. Returned value is1181// the incoming address with NULL casted away. You are allowed to use the1182// not-null value only if you are control dependent on the test.1183extern int explicit_null_checks_inserted,1184explicit_null_checks_elided;1185Node* GraphKit::null_check_common(Node* value, BasicType type,1186// optional arguments for variations:1187bool assert_null,1188Node* *null_control) {1189assert(!assert_null || null_control == NULL, "not both at once");1190if (stopped()) return top();1191if (!GenerateCompilerNullChecks && !assert_null && null_control == NULL) {1192// For some performance testing, we may wish to suppress null checking.1193value = cast_not_null(value); // Make it appear to be non-null (4962416).1194return value;1195}1196explicit_null_checks_inserted++;11971198// Construct NULL check1199Node *chk = NULL;1200switch(type) {1201case T_LONG : chk = new (C) CmpLNode(value, _gvn.zerocon(T_LONG)); break;1202case T_INT : chk = new (C) CmpINode(value, _gvn.intcon(0)); break;1203case T_ARRAY : // fall through1204type = T_OBJECT; // simplify further tests1205case T_OBJECT : {1206const Type *t = _gvn.type( value );12071208const TypeOopPtr* tp = t->isa_oopptr();1209if (tp != NULL && tp->klass() != NULL && !tp->klass()->is_loaded()1210// Only for do_null_check, not any of its siblings:1211&& !assert_null && null_control == NULL) {1212// Usually, any field access or invocation on an unloaded oop type1213// will simply fail to link, since the statically linked class is1214// likely also to be unloaded. However, in -Xcomp mode, sometimes1215// the static class is loaded but the sharper oop type is not.1216// Rather than checking for this obscure case in lots of places,1217// we simply observe that a null check on an unloaded class1218// will always be followed by a nonsense operation, so we1219// can just issue the uncommon trap here.1220// Our access to the unloaded class will only be correct1221// after it has been loaded and initialized, which requires1222// a trip through the interpreter.1223#ifndef PRODUCT1224if (WizardMode) { tty->print("Null check of unloaded "); tp->klass()->print(); tty->cr(); }1225#endif1226uncommon_trap(Deoptimization::Reason_unloaded,1227Deoptimization::Action_reinterpret,1228tp->klass(), "!loaded");1229return top();1230}12311232if (assert_null) {1233// See if the type is contained in NULL_PTR.1234// If so, then the value is already null.1235if (t->higher_equal(TypePtr::NULL_PTR)) {1236explicit_null_checks_elided++;1237return value; // Elided null assert quickly!1238}1239} else {1240// See if mixing in the NULL pointer changes type.1241// If so, then the NULL pointer was not allowed in the original1242// type. In other words, "value" was not-null.1243if (t->meet(TypePtr::NULL_PTR) != t->remove_speculative()) {1244// same as: if (!TypePtr::NULL_PTR->higher_equal(t)) ...1245explicit_null_checks_elided++;1246return value; // Elided null check quickly!1247}1248}1249chk = new (C) CmpPNode( value, null() );1250break;1251}12521253default:1254fatal(err_msg_res("unexpected type: %s", type2name(type)));1255}1256assert(chk != NULL, "sanity check");1257chk = _gvn.transform(chk);12581259BoolTest::mask btest = assert_null ? BoolTest::eq : BoolTest::ne;1260BoolNode *btst = new (C) BoolNode( chk, btest);1261Node *tst = _gvn.transform( btst );12621263//-----------1264// if peephole optimizations occurred, a prior test existed.1265// If a prior test existed, maybe it dominates as we can avoid this test.1266if (tst != btst && type == T_OBJECT) {1267// At this point we want to scan up the CFG to see if we can1268// find an identical test (and so avoid this test altogether).1269Node *cfg = control();1270int depth = 0;1271while( depth < 16 ) { // Limit search depth for speed1272if( cfg->Opcode() == Op_IfTrue &&1273cfg->in(0)->in(1) == tst ) {1274// Found prior test. Use "cast_not_null" to construct an identical1275// CastPP (and hence hash to) as already exists for the prior test.1276// Return that casted value.1277if (assert_null) {1278replace_in_map(value, null());1279return null(); // do not issue the redundant test1280}1281Node *oldcontrol = control();1282set_control(cfg);1283Node *res = cast_not_null(value);1284set_control(oldcontrol);1285explicit_null_checks_elided++;1286return res;1287}1288cfg = IfNode::up_one_dom(cfg, /*linear_only=*/ true);1289if (cfg == NULL) break; // Quit at region nodes1290depth++;1291}1292}12931294//-----------1295// Branch to failure if null1296float ok_prob = PROB_MAX; // a priori estimate: nulls never happen1297Deoptimization::DeoptReason reason;1298if (assert_null)1299reason = Deoptimization::Reason_null_assert;1300else if (type == T_OBJECT)1301reason = Deoptimization::Reason_null_check;1302else1303reason = Deoptimization::Reason_div0_check;13041305// %%% Since Reason_unhandled is not recorded on a per-bytecode basis,1306// ciMethodData::has_trap_at will return a conservative -1 if any1307// must-be-null assertion has failed. This could cause performance1308// problems for a method after its first do_null_assert failure.1309// Consider using 'Reason_class_check' instead?13101311// To cause an implicit null check, we set the not-null probability1312// to the maximum (PROB_MAX). For an explicit check the probability1313// is set to a smaller value.1314if (null_control != NULL || too_many_traps(reason)) {1315// probability is less likely1316ok_prob = PROB_LIKELY_MAG(3);1317} else if (!assert_null &&1318(ImplicitNullCheckThreshold > 0) &&1319method() != NULL &&1320(method()->method_data()->trap_count(reason)1321>= (uint)ImplicitNullCheckThreshold)) {1322ok_prob = PROB_LIKELY_MAG(3);1323}13241325if (null_control != NULL) {1326IfNode* iff = create_and_map_if(control(), tst, ok_prob, COUNT_UNKNOWN);1327Node* null_true = _gvn.transform( new (C) IfFalseNode(iff));1328set_control( _gvn.transform( new (C) IfTrueNode(iff)));1329if (null_true == top())1330explicit_null_checks_elided++;1331(*null_control) = null_true;1332} else {1333BuildCutout unless(this, tst, ok_prob);1334// Check for optimizer eliding test at parse time1335if (stopped()) {1336// Failure not possible; do not bother making uncommon trap.1337explicit_null_checks_elided++;1338} else if (assert_null) {1339uncommon_trap(reason,1340Deoptimization::Action_make_not_entrant,1341NULL, "assert_null");1342} else {1343replace_in_map(value, zerocon(type));1344builtin_throw(reason);1345}1346}13471348// Must throw exception, fall-thru not possible?1349if (stopped()) {1350return top(); // No result1351}13521353if (assert_null) {1354// Cast obj to null on this path.1355replace_in_map(value, zerocon(type));1356return zerocon(type);1357}13581359// Cast obj to not-null on this path, if there is no null_control.1360// (If there is a null_control, a non-null value may come back to haunt us.)1361if (type == T_OBJECT) {1362Node* cast = cast_not_null(value, false);1363if (null_control == NULL || (*null_control) == top())1364replace_in_map(value, cast);1365value = cast;1366}13671368return value;1369}137013711372//------------------------------cast_not_null----------------------------------1373// Cast obj to not-null on this path1374Node* GraphKit::cast_not_null(Node* obj, bool do_replace_in_map) {1375const Type *t = _gvn.type(obj);1376const Type *t_not_null = t->join_speculative(TypePtr::NOTNULL);1377// Object is already not-null?1378if( t == t_not_null ) return obj;13791380Node *cast = new (C) CastPPNode(obj,t_not_null);1381cast->init_req(0, control());1382cast = _gvn.transform( cast );13831384// Scan for instances of 'obj' in the current JVM mapping.1385// These instances are known to be not-null after the test.1386if (do_replace_in_map)1387replace_in_map(obj, cast);13881389return cast; // Return casted value1390}139113921393//--------------------------replace_in_map-------------------------------------1394void GraphKit::replace_in_map(Node* old, Node* neww) {1395if (old == neww) {1396return;1397}13981399map()->replace_edge(old, neww);14001401// Note: This operation potentially replaces any edge1402// on the map. This includes locals, stack, and monitors1403// of the current (innermost) JVM state.14041405// don't let inconsistent types from profiling escape this1406// method14071408const Type* told = _gvn.type(old);1409const Type* tnew = _gvn.type(neww);14101411if (!tnew->higher_equal(told)) {1412return;1413}14141415map()->record_replaced_node(old, neww);1416}141714181419//=============================================================================1420//--------------------------------memory---------------------------------------1421Node* GraphKit::memory(uint alias_idx) {1422MergeMemNode* mem = merged_memory();1423Node* p = mem->memory_at(alias_idx);1424_gvn.set_type(p, Type::MEMORY); // must be mapped1425return p;1426}14271428//-----------------------------reset_memory------------------------------------1429Node* GraphKit::reset_memory() {1430Node* mem = map()->memory();1431// do not use this node for any more parsing!1432debug_only( map()->set_memory((Node*)NULL) );1433return _gvn.transform( mem );1434}14351436//------------------------------set_all_memory---------------------------------1437void GraphKit::set_all_memory(Node* newmem) {1438Node* mergemem = MergeMemNode::make(C, newmem);1439gvn().set_type_bottom(mergemem);1440map()->set_memory(mergemem);1441}14421443//------------------------------set_all_memory_call----------------------------1444void GraphKit::set_all_memory_call(Node* call, bool separate_io_proj) {1445Node* newmem = _gvn.transform( new (C) ProjNode(call, TypeFunc::Memory, separate_io_proj) );1446set_all_memory(newmem);1447}14481449//=============================================================================1450//1451// parser factory methods for MemNodes1452//1453// These are layered on top of the factory methods in LoadNode and StoreNode,1454// and integrate with the parser's memory state and _gvn engine.1455//14561457// factory methods in "int adr_idx"1458Node* GraphKit::make_load(Node* ctl, Node* adr, const Type* t, BasicType bt,1459int adr_idx,1460MemNode::MemOrd mo,1461LoadNode::ControlDependency control_dependency,1462bool require_atomic_access,1463bool unaligned,1464bool mismatched) {1465assert(adr_idx != Compile::AliasIdxTop, "use other make_load factory" );1466const TypePtr* adr_type = NULL; // debug-mode-only argument1467debug_only(adr_type = C->get_adr_type(adr_idx));1468Node* mem = memory(adr_idx);1469Node* ld;1470if (require_atomic_access && bt == T_LONG) {1471ld = LoadLNode::make_atomic(C, ctl, mem, adr, adr_type, t, mo, control_dependency);1472} else if (require_atomic_access && bt == T_DOUBLE) {1473ld = LoadDNode::make_atomic(C, ctl, mem, adr, adr_type, t, mo, control_dependency);1474} else {1475ld = LoadNode::make(_gvn, ctl, mem, adr, adr_type, t, bt, mo, control_dependency);1476}1477if (unaligned) {1478ld->as_Load()->set_unaligned_access();1479}1480if (mismatched) {1481ld->as_Load()->set_mismatched_access();1482}1483ld = _gvn.transform(ld);1484if ((bt == T_OBJECT) && C->do_escape_analysis() || C->eliminate_boxing()) {1485// Improve graph before escape analysis and boxing elimination.1486record_for_igvn(ld);1487}1488return ld;1489}14901491Node* GraphKit::store_to_memory(Node* ctl, Node* adr, Node *val, BasicType bt,1492int adr_idx,1493MemNode::MemOrd mo,1494bool require_atomic_access,1495bool unaligned,1496bool mismatched) {1497assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );1498const TypePtr* adr_type = NULL;1499debug_only(adr_type = C->get_adr_type(adr_idx));1500Node *mem = memory(adr_idx);1501Node* st;1502if (require_atomic_access && bt == T_LONG) {1503st = StoreLNode::make_atomic(C, ctl, mem, adr, adr_type, val, mo);1504} else if (require_atomic_access && bt == T_DOUBLE) {1505st = StoreDNode::make_atomic(C, ctl, mem, adr, adr_type, val, mo);1506} else {1507st = StoreNode::make(_gvn, ctl, mem, adr, adr_type, val, bt, mo);1508}1509if (unaligned) {1510st->as_Store()->set_unaligned_access();1511}1512if (mismatched) {1513st->as_Store()->set_mismatched_access();1514}1515st = _gvn.transform(st);1516set_memory(st, adr_idx);1517// Back-to-back stores can only remove intermediate store with DU info1518// so push on worklist for optimizer.1519if (mem->req() > MemNode::Address && adr == mem->in(MemNode::Address))1520record_for_igvn(st);15211522return st;1523}152415251526void GraphKit::pre_barrier(bool do_load,1527Node* ctl,1528Node* obj,1529Node* adr,1530uint adr_idx,1531Node* val,1532const TypeOopPtr* val_type,1533Node* pre_val,1534BasicType bt) {15351536BarrierSet* bs = Universe::heap()->barrier_set();1537set_control(ctl);1538switch (bs->kind()) {1539case BarrierSet::G1SATBCT:1540case BarrierSet::G1SATBCTLogging:1541g1_write_barrier_pre(do_load, obj, adr, adr_idx, val, val_type, pre_val, bt);1542break;1543case BarrierSet::ShenandoahBarrierSet:1544if (ShenandoahSATBBarrier) {1545g1_write_barrier_pre(do_load, obj, adr, adr_idx, val, val_type, pre_val, bt);1546}1547break;1548case BarrierSet::CardTableModRef:1549case BarrierSet::CardTableExtension:1550case BarrierSet::ModRef:1551break;15521553case BarrierSet::Other:1554default :1555ShouldNotReachHere();15561557}1558}15591560bool GraphKit::can_move_pre_barrier() const {1561BarrierSet* bs = Universe::heap()->barrier_set();1562switch (bs->kind()) {1563case BarrierSet::G1SATBCT:1564case BarrierSet::G1SATBCTLogging:1565case BarrierSet::ShenandoahBarrierSet:1566return true; // Can move it if no safepoint15671568case BarrierSet::CardTableModRef:1569case BarrierSet::CardTableExtension:1570case BarrierSet::ModRef:1571return true; // There is no pre-barrier15721573case BarrierSet::Other:1574default :1575ShouldNotReachHere();1576}1577return false;1578}15791580void GraphKit::post_barrier(Node* ctl,1581Node* store,1582Node* obj,1583Node* adr,1584uint adr_idx,1585Node* val,1586BasicType bt,1587bool use_precise) {1588BarrierSet* bs = Universe::heap()->barrier_set();1589set_control(ctl);1590switch (bs->kind()) {1591case BarrierSet::G1SATBCT:1592case BarrierSet::G1SATBCTLogging:1593g1_write_barrier_post(store, obj, adr, adr_idx, val, bt, use_precise);1594break;1595case BarrierSet::ShenandoahBarrierSet:1596if (ShenandoahStoreValEnqueueBarrier) {1597g1_write_barrier_pre(false, NULL, NULL, max_juint, NULL, NULL, val, bt);1598}1599break;1600case BarrierSet::CardTableModRef:1601case BarrierSet::CardTableExtension:1602write_barrier_post(store, obj, adr, adr_idx, val, use_precise);1603break;16041605case BarrierSet::ModRef:1606break;16071608case BarrierSet::Other:1609default :1610ShouldNotReachHere();16111612}1613}16141615Node* GraphKit::store_oop(Node* ctl,1616Node* obj,1617Node* adr,1618const TypePtr* adr_type,1619Node* val,1620const TypeOopPtr* val_type,1621BasicType bt,1622bool use_precise,1623MemNode::MemOrd mo,1624bool mismatched) {1625// Transformation of a value which could be NULL pointer (CastPP #NULL)1626// could be delayed during Parse (for example, in adjust_map_after_if()).1627// Execute transformation here to avoid barrier generation in such case.1628if (_gvn.type(val) == TypePtr::NULL_PTR)1629val = _gvn.makecon(TypePtr::NULL_PTR);16301631set_control(ctl);1632if (stopped()) return top(); // Dead path ?16331634assert(bt == T_OBJECT, "sanity");1635assert(val != NULL, "not dead path");1636uint adr_idx = C->get_alias_index(adr_type);1637assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );16381639pre_barrier(true /* do_load */,1640control(), obj, adr, adr_idx, val, val_type,1641NULL /* pre_val */,1642bt);16431644Node* store = store_to_memory(control(), adr, val, bt, adr_idx, mo, mismatched);1645post_barrier(control(), store, obj, adr, adr_idx, val, bt, use_precise);1646return store;1647}16481649// Could be an array or object we don't know at compile time (unsafe ref.)1650Node* GraphKit::store_oop_to_unknown(Node* ctl,1651Node* obj, // containing obj1652Node* adr, // actual adress to store val at1653const TypePtr* adr_type,1654Node* val,1655BasicType bt,1656MemNode::MemOrd mo,1657bool mismatched) {1658Compile::AliasType* at = C->alias_type(adr_type);1659const TypeOopPtr* val_type = NULL;1660if (adr_type->isa_instptr()) {1661if (at->field() != NULL) {1662// known field. This code is a copy of the do_put_xxx logic.1663ciField* field = at->field();1664if (!field->type()->is_loaded()) {1665val_type = TypeInstPtr::BOTTOM;1666} else {1667val_type = TypeOopPtr::make_from_klass(field->type()->as_klass());1668}1669}1670} else if (adr_type->isa_aryptr()) {1671val_type = adr_type->is_aryptr()->elem()->make_oopptr();1672}1673if (val_type == NULL) {1674val_type = TypeInstPtr::BOTTOM;1675}1676return store_oop(ctl, obj, adr, adr_type, val, val_type, bt, true, mo, mismatched);1677}167816791680//-------------------------array_element_address-------------------------1681Node* GraphKit::array_element_address(Node* ary, Node* idx, BasicType elembt,1682const TypeInt* sizetype, Node* ctrl) {1683uint shift = exact_log2(type2aelembytes(elembt));1684uint header = arrayOopDesc::base_offset_in_bytes(elembt);16851686// short-circuit a common case (saves lots of confusing waste motion)1687jint idx_con = find_int_con(idx, -1);1688if (idx_con >= 0) {1689intptr_t offset = header + ((intptr_t)idx_con << shift);1690return basic_plus_adr(ary, offset);1691}16921693// must be correct type for alignment purposes1694Node* base = basic_plus_adr(ary, header);1695#ifdef _LP641696// The scaled index operand to AddP must be a clean 64-bit value.1697// Java allows a 32-bit int to be incremented to a negative1698// value, which appears in a 64-bit register as a large1699// positive number. Using that large positive number as an1700// operand in pointer arithmetic has bad consequences.1701// On the other hand, 32-bit overflow is rare, and the possibility1702// can often be excluded, if we annotate the ConvI2L node with1703// a type assertion that its value is known to be a small positive1704// number. (The prior range check has ensured this.)1705// This assertion is used by ConvI2LNode::Ideal.1706int index_max = max_jint - 1; // array size is max_jint, index is one less1707if (sizetype != NULL) index_max = sizetype->_hi - 1;1708const TypeInt* iidxtype = TypeInt::make(0, index_max, Type::WidenMax);1709idx = C->constrained_convI2L(&_gvn, idx, iidxtype, ctrl);1710#endif1711Node* scale = _gvn.transform( new (C) LShiftXNode(idx, intcon(shift)) );1712return basic_plus_adr(ary, base, scale);1713}17141715//-------------------------load_array_element-------------------------1716Node* GraphKit::load_array_element(Node* ctl, Node* ary, Node* idx, const TypeAryPtr* arytype) {1717const Type* elemtype = arytype->elem();1718BasicType elembt = elemtype->array_element_basic_type();1719Node* adr = array_element_address(ary, idx, elembt, arytype->size());1720if (elembt == T_NARROWOOP) {1721elembt = T_OBJECT; // To satisfy switch in LoadNode::make()1722}1723Node* ld = make_load(ctl, adr, elemtype, elembt, arytype, MemNode::unordered);1724#if INCLUDE_ALL_GCS1725if (UseShenandoahGC && (elembt == T_OBJECT || elembt == T_ARRAY)) {1726ld = ShenandoahBarrierSetC2::bsc2()->load_reference_barrier(this, ld);1727}1728#endif1729return ld;1730}17311732//-------------------------set_arguments_for_java_call-------------------------1733// Arguments (pre-popped from the stack) are taken from the JVMS.1734void GraphKit::set_arguments_for_java_call(CallJavaNode* call) {1735// Add the call arguments:1736uint nargs = call->method()->arg_size();1737for (uint i = 0; i < nargs; i++) {1738Node* arg = argument(i);1739call->init_req(i + TypeFunc::Parms, arg);1740}1741}17421743//---------------------------set_edges_for_java_call---------------------------1744// Connect a newly created call into the current JVMS.1745// A return value node (if any) is returned from set_edges_for_java_call.1746void GraphKit::set_edges_for_java_call(CallJavaNode* call, bool must_throw, bool separate_io_proj) {17471748// Add the predefined inputs:1749call->init_req( TypeFunc::Control, control() );1750call->init_req( TypeFunc::I_O , i_o() );1751call->init_req( TypeFunc::Memory , reset_memory() );1752call->init_req( TypeFunc::FramePtr, frameptr() );1753call->init_req( TypeFunc::ReturnAdr, top() );17541755add_safepoint_edges(call, must_throw);17561757Node* xcall = _gvn.transform(call);17581759if (xcall == top()) {1760set_control(top());1761return;1762}1763assert(xcall == call, "call identity is stable");17641765// Re-use the current map to produce the result.17661767set_control(_gvn.transform(new (C) ProjNode(call, TypeFunc::Control)));1768set_i_o( _gvn.transform(new (C) ProjNode(call, TypeFunc::I_O , separate_io_proj)));1769set_all_memory_call(xcall, separate_io_proj);17701771//return xcall; // no need, caller already has it1772}17731774Node* GraphKit::set_results_for_java_call(CallJavaNode* call, bool separate_io_proj) {1775if (stopped()) return top(); // maybe the call folded up?17761777// Capture the return value, if any.1778Node* ret;1779if (call->method() == NULL ||1780call->method()->return_type()->basic_type() == T_VOID)1781ret = top();1782else ret = _gvn.transform(new (C) ProjNode(call, TypeFunc::Parms));17831784// Note: Since any out-of-line call can produce an exception,1785// we always insert an I_O projection from the call into the result.17861787make_slow_call_ex(call, env()->Throwable_klass(), separate_io_proj);17881789if (separate_io_proj) {1790// The caller requested separate projections be used by the fall1791// through and exceptional paths, so replace the projections for1792// the fall through path.1793set_i_o(_gvn.transform( new (C) ProjNode(call, TypeFunc::I_O) ));1794set_all_memory(_gvn.transform( new (C) ProjNode(call, TypeFunc::Memory) ));1795}1796return ret;1797}17981799//--------------------set_predefined_input_for_runtime_call--------------------1800// Reading and setting the memory state is way conservative here.1801// The real problem is that I am not doing real Type analysis on memory,1802// so I cannot distinguish card mark stores from other stores. Across a GC1803// point the Store Barrier and the card mark memory has to agree. I cannot1804// have a card mark store and its barrier split across the GC point from1805// either above or below. Here I get that to happen by reading ALL of memory.1806// A better answer would be to separate out card marks from other memory.1807// For now, return the input memory state, so that it can be reused1808// after the call, if this call has restricted memory effects.1809Node* GraphKit::set_predefined_input_for_runtime_call(SafePointNode* call, Node* narrow_mem) {1810// Set fixed predefined input arguments1811Node* memory = reset_memory();1812Node* m = narrow_mem == NULL ? memory : narrow_mem;1813call->init_req( TypeFunc::Control, control() );1814call->init_req( TypeFunc::I_O, top() ); // does no i/o1815call->init_req( TypeFunc::Memory, m ); // may gc ptrs1816call->init_req( TypeFunc::FramePtr, frameptr() );1817call->init_req( TypeFunc::ReturnAdr, top() );1818return memory;1819}18201821//-------------------set_predefined_output_for_runtime_call--------------------1822// Set control and memory (not i_o) from the call.1823// If keep_mem is not NULL, use it for the output state,1824// except for the RawPtr output of the call, if hook_mem is TypeRawPtr::BOTTOM.1825// If hook_mem is NULL, this call produces no memory effects at all.1826// If hook_mem is a Java-visible memory slice (such as arraycopy operands),1827// then only that memory slice is taken from the call.1828// In the last case, we must put an appropriate memory barrier before1829// the call, so as to create the correct anti-dependencies on loads1830// preceding the call.1831void GraphKit::set_predefined_output_for_runtime_call(Node* call,1832Node* keep_mem,1833const TypePtr* hook_mem) {1834// no i/o1835set_control(_gvn.transform( new (C) ProjNode(call,TypeFunc::Control) ));1836if (keep_mem) {1837// First clone the existing memory state1838set_all_memory(keep_mem);1839if (hook_mem != NULL) {1840// Make memory for the call1841Node* mem = _gvn.transform( new (C) ProjNode(call, TypeFunc::Memory) );1842// Set the RawPtr memory state only. This covers all the heap top/GC stuff1843// We also use hook_mem to extract specific effects from arraycopy stubs.1844set_memory(mem, hook_mem);1845}1846// ...else the call has NO memory effects.18471848// Make sure the call advertises its memory effects precisely.1849// This lets us build accurate anti-dependences in gcm.cpp.1850assert(C->alias_type(call->adr_type()) == C->alias_type(hook_mem),1851"call node must be constructed correctly");1852} else {1853assert(hook_mem == NULL, "");1854// This is not a "slow path" call; all memory comes from the call.1855set_all_memory_call(call);1856}1857}185818591860// Replace the call with the current state of the kit.1861void GraphKit::replace_call(CallNode* call, Node* result, bool do_replaced_nodes) {1862JVMState* ejvms = NULL;1863if (has_exceptions()) {1864ejvms = transfer_exceptions_into_jvms();1865}18661867ReplacedNodes replaced_nodes = map()->replaced_nodes();1868ReplacedNodes replaced_nodes_exception;1869Node* ex_ctl = top();18701871SafePointNode* final_state = stop();18721873// Find all the needed outputs of this call1874CallProjections callprojs;1875call->extract_projections(&callprojs, true);18761877Node* init_mem = call->in(TypeFunc::Memory);1878Node* final_mem = final_state->in(TypeFunc::Memory);1879Node* final_ctl = final_state->in(TypeFunc::Control);1880Node* final_io = final_state->in(TypeFunc::I_O);18811882// Replace all the old call edges with the edges from the inlining result1883if (callprojs.fallthrough_catchproj != NULL) {1884C->gvn_replace_by(callprojs.fallthrough_catchproj, final_ctl);1885}1886if (callprojs.fallthrough_memproj != NULL) {1887if (final_mem->is_MergeMem()) {1888// Parser's exits MergeMem was not transformed but may be optimized1889final_mem = _gvn.transform(final_mem);1890}1891C->gvn_replace_by(callprojs.fallthrough_memproj, final_mem);1892}1893if (callprojs.fallthrough_ioproj != NULL) {1894C->gvn_replace_by(callprojs.fallthrough_ioproj, final_io);1895}18961897// Replace the result with the new result if it exists and is used1898if (callprojs.resproj != NULL && result != NULL) {1899C->gvn_replace_by(callprojs.resproj, result);1900}19011902if (ejvms == NULL) {1903// No exception edges to simply kill off those paths1904if (callprojs.catchall_catchproj != NULL) {1905C->gvn_replace_by(callprojs.catchall_catchproj, C->top());1906}1907if (callprojs.catchall_memproj != NULL) {1908C->gvn_replace_by(callprojs.catchall_memproj, C->top());1909}1910if (callprojs.catchall_ioproj != NULL) {1911C->gvn_replace_by(callprojs.catchall_ioproj, C->top());1912}1913// Replace the old exception object with top1914if (callprojs.exobj != NULL) {1915C->gvn_replace_by(callprojs.exobj, C->top());1916}1917} else {1918GraphKit ekit(ejvms);19191920// Load my combined exception state into the kit, with all phis transformed:1921SafePointNode* ex_map = ekit.combine_and_pop_all_exception_states();1922replaced_nodes_exception = ex_map->replaced_nodes();19231924Node* ex_oop = ekit.use_exception_state(ex_map);19251926if (callprojs.catchall_catchproj != NULL) {1927C->gvn_replace_by(callprojs.catchall_catchproj, ekit.control());1928ex_ctl = ekit.control();1929}1930if (callprojs.catchall_memproj != NULL) {1931C->gvn_replace_by(callprojs.catchall_memproj, ekit.reset_memory());1932}1933if (callprojs.catchall_ioproj != NULL) {1934C->gvn_replace_by(callprojs.catchall_ioproj, ekit.i_o());1935}19361937// Replace the old exception object with the newly created one1938if (callprojs.exobj != NULL) {1939C->gvn_replace_by(callprojs.exobj, ex_oop);1940}1941}19421943// Disconnect the call from the graph1944call->disconnect_inputs(NULL, C);1945C->gvn_replace_by(call, C->top());19461947// Clean up any MergeMems that feed other MergeMems since the1948// optimizer doesn't like that.1949if (final_mem->is_MergeMem()) {1950Node_List wl;1951for (SimpleDUIterator i(final_mem); i.has_next(); i.next()) {1952Node* m = i.get();1953if (m->is_MergeMem() && !wl.contains(m)) {1954wl.push(m);1955}1956}1957while (wl.size() > 0) {1958_gvn.transform(wl.pop());1959}1960}19611962if (callprojs.fallthrough_catchproj != NULL && !final_ctl->is_top() && do_replaced_nodes) {1963replaced_nodes.apply(C, final_ctl);1964}1965if (!ex_ctl->is_top() && do_replaced_nodes) {1966replaced_nodes_exception.apply(C, ex_ctl);1967}1968}196919701971//------------------------------increment_counter------------------------------1972// for statistics: increment a VM counter by 119731974void GraphKit::increment_counter(address counter_addr) {1975Node* adr1 = makecon(TypeRawPtr::make(counter_addr));1976increment_counter(adr1);1977}19781979void GraphKit::increment_counter(Node* counter_addr) {1980int adr_type = Compile::AliasIdxRaw;1981Node* ctrl = control();1982Node* cnt = make_load(ctrl, counter_addr, TypeInt::INT, T_INT, adr_type, MemNode::unordered);1983Node* incr = _gvn.transform(new (C) AddINode(cnt, _gvn.intcon(1)));1984store_to_memory(ctrl, counter_addr, incr, T_INT, adr_type, MemNode::unordered);1985}198619871988//------------------------------uncommon_trap----------------------------------1989// Bail out to the interpreter in mid-method. Implemented by calling the1990// uncommon_trap blob. This helper function inserts a runtime call with the1991// right debug info.1992void GraphKit::uncommon_trap(int trap_request,1993ciKlass* klass, const char* comment,1994bool must_throw,1995bool keep_exact_action) {1996if (failing()) stop();1997if (stopped()) return; // trap reachable?19981999// Note: If ProfileTraps is true, and if a deopt. actually2000// occurs here, the runtime will make sure an MDO exists. There is2001// no need to call method()->ensure_method_data() at this point.20022003// Set the stack pointer to the right value for reexecution:2004set_sp(reexecute_sp());20052006#ifdef ASSERT2007if (!must_throw) {2008// Make sure the stack has at least enough depth to execute2009// the current bytecode.2010int inputs, ignored_depth;2011if (compute_stack_effects(inputs, ignored_depth)) {2012assert(sp() >= inputs, err_msg_res("must have enough JVMS stack to execute %s: sp=%d, inputs=%d",2013Bytecodes::name(java_bc()), sp(), inputs));2014}2015}2016#endif20172018Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);2019Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);20202021switch (action) {2022case Deoptimization::Action_maybe_recompile:2023case Deoptimization::Action_reinterpret:2024// Temporary fix for 6529811 to allow virtual calls to be sure they2025// get the chance to go from mono->bi->mega2026if (!keep_exact_action &&2027Deoptimization::trap_request_index(trap_request) < 0 &&2028too_many_recompiles(reason)) {2029// This BCI is causing too many recompilations.2030if (C->log() != NULL) {2031C->log()->elem("observe that='trap_action_change' reason='%s' from='%s' to='none'",2032Deoptimization::trap_reason_name(reason),2033Deoptimization::trap_action_name(action));2034}2035action = Deoptimization::Action_none;2036trap_request = Deoptimization::make_trap_request(reason, action);2037} else {2038C->set_trap_can_recompile(true);2039}2040break;2041case Deoptimization::Action_make_not_entrant:2042C->set_trap_can_recompile(true);2043break;2044#ifdef ASSERT2045case Deoptimization::Action_none:2046case Deoptimization::Action_make_not_compilable:2047break;2048default:2049fatal(err_msg_res("unknown action %d: %s", action, Deoptimization::trap_action_name(action)));2050break;2051#endif2052}20532054if (TraceOptoParse) {2055char buf[100];2056tty->print_cr("Uncommon trap %s at bci:%d",2057Deoptimization::format_trap_request(buf, sizeof(buf),2058trap_request), bci());2059}20602061CompileLog* log = C->log();2062if (log != NULL) {2063int kid = (klass == NULL)? -1: log->identify(klass);2064log->begin_elem("uncommon_trap bci='%d'", bci());2065char buf[100];2066log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf),2067trap_request));2068if (kid >= 0) log->print(" klass='%d'", kid);2069if (comment != NULL) log->print(" comment='%s'", comment);2070log->end_elem();2071}20722073// Make sure any guarding test views this path as very unlikely2074Node *i0 = control()->in(0);2075if (i0 != NULL && i0->is_If()) { // Found a guarding if test?2076IfNode *iff = i0->as_If();2077float f = iff->_prob; // Get prob2078if (control()->Opcode() == Op_IfTrue) {2079if (f > PROB_UNLIKELY_MAG(4))2080iff->_prob = PROB_MIN;2081} else {2082if (f < PROB_LIKELY_MAG(4))2083iff->_prob = PROB_MAX;2084}2085}20862087// Clear out dead values from the debug info.2088kill_dead_locals();20892090// Now insert the uncommon trap subroutine call2091address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point();2092const TypePtr* no_memory_effects = NULL;2093// Pass the index of the class to be loaded2094Node* call = make_runtime_call(RC_NO_LEAF | RC_UNCOMMON |2095(must_throw ? RC_MUST_THROW : 0),2096OptoRuntime::uncommon_trap_Type(),2097call_addr, "uncommon_trap", no_memory_effects,2098intcon(trap_request));2099assert(call->as_CallStaticJava()->uncommon_trap_request() == trap_request,2100"must extract request correctly from the graph");2101assert(trap_request != 0, "zero value reserved by uncommon_trap_request");21022103call->set_req(TypeFunc::ReturnAdr, returnadr());2104// The debug info is the only real input to this call.21052106// Halt-and-catch fire here. The above call should never return!2107HaltNode* halt = new(C) HaltNode(control(), frameptr());2108_gvn.set_type_bottom(halt);2109root()->add_req(halt);21102111stop_and_kill_map();2112}211321142115//--------------------------just_allocated_object------------------------------2116// Report the object that was just allocated.2117// It must be the case that there are no intervening safepoints.2118// We use this to determine if an object is so "fresh" that2119// it does not require card marks.2120Node* GraphKit::just_allocated_object(Node* current_control) {2121if (C->recent_alloc_ctl() == current_control)2122return C->recent_alloc_obj();2123return NULL;2124}212521262127void GraphKit::round_double_arguments(ciMethod* dest_method) {2128// (Note: TypeFunc::make has a cache that makes this fast.)2129const TypeFunc* tf = TypeFunc::make(dest_method);2130int nargs = tf->_domain->_cnt - TypeFunc::Parms;2131for (int j = 0; j < nargs; j++) {2132const Type *targ = tf->_domain->field_at(j + TypeFunc::Parms);2133if( targ->basic_type() == T_DOUBLE ) {2134// If any parameters are doubles, they must be rounded before2135// the call, dstore_rounding does gvn.transform2136Node *arg = argument(j);2137arg = dstore_rounding(arg);2138set_argument(j, arg);2139}2140}2141}21422143/**2144* Record profiling data exact_kls for Node n with the type system so2145* that it can propagate it (speculation)2146*2147* @param n node that the type applies to2148* @param exact_kls type from profiling2149*2150* @return node with improved type2151*/2152Node* GraphKit::record_profile_for_speculation(Node* n, ciKlass* exact_kls) {2153const Type* current_type = _gvn.type(n);2154assert(UseTypeSpeculation, "type speculation must be on");21552156const TypeOopPtr* speculative = current_type->speculative();21572158if (current_type->would_improve_type(exact_kls, jvms()->depth())) {2159const TypeKlassPtr* tklass = TypeKlassPtr::make(exact_kls);2160const TypeOopPtr* xtype = tklass->as_instance_type();2161assert(xtype->klass_is_exact(), "Should be exact");2162// record the new speculative type's depth2163speculative = xtype->with_inline_depth(jvms()->depth());2164}21652166if (speculative != current_type->speculative()) {2167// Build a type with a speculative type (what we think we know2168// about the type but will need a guard when we use it)2169const TypeOopPtr* spec_type = TypeOopPtr::make(TypePtr::BotPTR, Type::OffsetBot, TypeOopPtr::InstanceBot, speculative);2170// We're changing the type, we need a new CheckCast node to carry2171// the new type. The new type depends on the control: what2172// profiling tells us is only valid from here as far as we can2173// tell.2174Node* cast = new(C) CheckCastPPNode(control(), n, current_type->remove_speculative()->join_speculative(spec_type));2175cast = _gvn.transform(cast);2176replace_in_map(n, cast);2177n = cast;2178}21792180return n;2181}21822183/**2184* Record profiling data from receiver profiling at an invoke with the2185* type system so that it can propagate it (speculation)2186*2187* @param n receiver node2188*2189* @return node with improved type2190*/2191Node* GraphKit::record_profiled_receiver_for_speculation(Node* n) {2192if (!UseTypeSpeculation) {2193return n;2194}2195ciKlass* exact_kls = profile_has_unique_klass();2196return record_profile_for_speculation(n, exact_kls);2197}21982199/**2200* Record profiling data from argument profiling at an invoke with the2201* type system so that it can propagate it (speculation)2202*2203* @param dest_method target method for the call2204* @param bc what invoke bytecode is this?2205*/2206void GraphKit::record_profiled_arguments_for_speculation(ciMethod* dest_method, Bytecodes::Code bc) {2207if (!UseTypeSpeculation) {2208return;2209}2210const TypeFunc* tf = TypeFunc::make(dest_method);2211int nargs = tf->_domain->_cnt - TypeFunc::Parms;2212int skip = Bytecodes::has_receiver(bc) ? 1 : 0;2213for (int j = skip, i = 0; j < nargs && i < TypeProfileArgsLimit; j++) {2214const Type *targ = tf->_domain->field_at(j + TypeFunc::Parms);2215if (targ->basic_type() == T_OBJECT || targ->basic_type() == T_ARRAY) {2216ciKlass* better_type = method()->argument_profiled_type(bci(), i);2217if (better_type != NULL) {2218record_profile_for_speculation(argument(j), better_type);2219}2220i++;2221}2222}2223}22242225/**2226* Record profiling data from parameter profiling at an invoke with2227* the type system so that it can propagate it (speculation)2228*/2229void GraphKit::record_profiled_parameters_for_speculation() {2230if (!UseTypeSpeculation) {2231return;2232}2233for (int i = 0, j = 0; i < method()->arg_size() ; i++) {2234if (_gvn.type(local(i))->isa_oopptr()) {2235ciKlass* better_type = method()->parameter_profiled_type(j);2236if (better_type != NULL) {2237record_profile_for_speculation(local(i), better_type);2238}2239j++;2240}2241}2242}22432244void GraphKit::round_double_result(ciMethod* dest_method) {2245// A non-strict method may return a double value which has an extended2246// exponent, but this must not be visible in a caller which is 'strict'2247// If a strict caller invokes a non-strict callee, round a double result22482249BasicType result_type = dest_method->return_type()->basic_type();2250assert( method() != NULL, "must have caller context");2251if( result_type == T_DOUBLE && method()->is_strict() && !dest_method->is_strict() ) {2252// Destination method's return value is on top of stack2253// dstore_rounding() does gvn.transform2254Node *result = pop_pair();2255result = dstore_rounding(result);2256push_pair(result);2257}2258}22592260// rounding for strict float precision conformance2261Node* GraphKit::precision_rounding(Node* n) {2262return UseStrictFP && _method->flags().is_strict()2263&& UseSSE == 0 && Matcher::strict_fp_requires_explicit_rounding2264? _gvn.transform( new (C) RoundFloatNode(0, n) )2265: n;2266}22672268// rounding for strict double precision conformance2269Node* GraphKit::dprecision_rounding(Node *n) {2270return UseStrictFP && _method->flags().is_strict()2271&& UseSSE <= 1 && Matcher::strict_fp_requires_explicit_rounding2272? _gvn.transform( new (C) RoundDoubleNode(0, n) )2273: n;2274}22752276// rounding for non-strict double stores2277Node* GraphKit::dstore_rounding(Node* n) {2278return Matcher::strict_fp_requires_explicit_rounding2279&& UseSSE <= 12280? _gvn.transform( new (C) RoundDoubleNode(0, n) )2281: n;2282}22832284//=============================================================================2285// Generate a fast path/slow path idiom. Graph looks like:2286// [foo] indicates that 'foo' is a parameter2287//2288// [in] NULL2289// \ /2290// CmpP2291// Bool ne2292// If2293// / \2294// True False-<2>2295// / |2296// / cast_not_null2297// Load | | ^2298// [fast_test] | |2299// gvn to opt_test | |2300// / \ | <1>2301// True False |2302// | \\ |2303// [slow_call] \[fast_result]2304// Ctl Val \ \2305// | \ \2306// Catch <1> \ \2307// / \ ^ \ \2308// Ex No_Ex | \ \2309// | \ \ | \ <2> \2310// ... \ [slow_res] | | \ [null_result]2311// \ \--+--+--- | |2312// \ | / \ | /2313// --------Region Phi2314//2315//=============================================================================2316// Code is structured as a series of driver functions all called 'do_XXX' that2317// call a set of helper functions. Helper functions first, then drivers.23182319//------------------------------null_check_oop---------------------------------2320// Null check oop. Set null-path control into Region in slot 3.2321// Make a cast-not-nullness use the other not-null control. Return cast.2322Node* GraphKit::null_check_oop(Node* value, Node* *null_control,2323bool never_see_null, bool safe_for_replace) {2324// Initial NULL check taken path2325(*null_control) = top();2326Node* cast = null_check_common(value, T_OBJECT, false, null_control);23272328// Generate uncommon_trap:2329if (never_see_null && (*null_control) != top()) {2330// If we see an unexpected null at a check-cast we record it and force a2331// recompile; the offending check-cast will be compiled to handle NULLs.2332// If we see more than one offending BCI, then all checkcasts in the2333// method will be compiled to handle NULLs.2334PreserveJVMState pjvms(this);2335set_control(*null_control);2336replace_in_map(value, null());2337uncommon_trap(Deoptimization::Reason_null_check,2338Deoptimization::Action_make_not_entrant);2339(*null_control) = top(); // NULL path is dead2340}2341if ((*null_control) == top() && safe_for_replace) {2342replace_in_map(value, cast);2343}23442345// Cast away null-ness on the result2346return cast;2347}23482349//------------------------------opt_iff----------------------------------------2350// Optimize the fast-check IfNode. Set the fast-path region slot 2.2351// Return slow-path control.2352Node* GraphKit::opt_iff(Node* region, Node* iff) {2353IfNode *opt_iff = _gvn.transform(iff)->as_If();23542355// Fast path taken; set region slot 22356Node *fast_taken = _gvn.transform( new (C) IfFalseNode(opt_iff) );2357region->init_req(2,fast_taken); // Capture fast-control23582359// Fast path not-taken, i.e. slow path2360Node *slow_taken = _gvn.transform( new (C) IfTrueNode(opt_iff) );2361return slow_taken;2362}23632364//-----------------------------make_runtime_call-------------------------------2365Node* GraphKit::make_runtime_call(int flags,2366const TypeFunc* call_type, address call_addr,2367const char* call_name,2368const TypePtr* adr_type,2369// The following parms are all optional.2370// The first NULL ends the list.2371Node* parm0, Node* parm1,2372Node* parm2, Node* parm3,2373Node* parm4, Node* parm5,2374Node* parm6, Node* parm7) {2375// Slow-path call2376bool is_leaf = !(flags & RC_NO_LEAF);2377bool has_io = (!is_leaf && !(flags & RC_NO_IO));2378if (call_name == NULL) {2379assert(!is_leaf, "must supply name for leaf");2380call_name = OptoRuntime::stub_name(call_addr);2381}2382CallNode* call;2383if (!is_leaf) {2384call = new(C) CallStaticJavaNode(call_type, call_addr, call_name,2385bci(), adr_type);2386} else if (flags & RC_NO_FP) {2387call = new(C) CallLeafNoFPNode(call_type, call_addr, call_name, adr_type);2388} else {2389call = new(C) CallLeafNode(call_type, call_addr, call_name, adr_type);2390}23912392// The following is similar to set_edges_for_java_call,2393// except that the memory effects of the call are restricted to AliasIdxRaw.23942395// Slow path call has no side-effects, uses few values2396bool wide_in = !(flags & RC_NARROW_MEM);2397bool wide_out = (C->get_alias_index(adr_type) == Compile::AliasIdxBot);23982399Node* prev_mem = NULL;2400if (wide_in) {2401prev_mem = set_predefined_input_for_runtime_call(call);2402} else {2403assert(!wide_out, "narrow in => narrow out");2404Node* narrow_mem = memory(adr_type);2405prev_mem = set_predefined_input_for_runtime_call(call, narrow_mem);2406}24072408// Hook each parm in order. Stop looking at the first NULL.2409if (parm0 != NULL) { call->init_req(TypeFunc::Parms+0, parm0);2410if (parm1 != NULL) { call->init_req(TypeFunc::Parms+1, parm1);2411if (parm2 != NULL) { call->init_req(TypeFunc::Parms+2, parm2);2412if (parm3 != NULL) { call->init_req(TypeFunc::Parms+3, parm3);2413if (parm4 != NULL) { call->init_req(TypeFunc::Parms+4, parm4);2414if (parm5 != NULL) { call->init_req(TypeFunc::Parms+5, parm5);2415if (parm6 != NULL) { call->init_req(TypeFunc::Parms+6, parm6);2416if (parm7 != NULL) { call->init_req(TypeFunc::Parms+7, parm7);2417/* close each nested if ===> */ } } } } } } } }2418assert(call->in(call->req()-1) != NULL, "must initialize all parms");24192420if (!is_leaf) {2421// Non-leaves can block and take safepoints:2422add_safepoint_edges(call, ((flags & RC_MUST_THROW) != 0));2423}2424// Non-leaves can throw exceptions:2425if (has_io) {2426call->set_req(TypeFunc::I_O, i_o());2427}24282429if (flags & RC_UNCOMMON) {2430// Set the count to a tiny probability. Cf. Estimate_Block_Frequency.2431// (An "if" probability corresponds roughly to an unconditional count.2432// Sort of.)2433call->set_cnt(PROB_UNLIKELY_MAG(4));2434}24352436Node* c = _gvn.transform(call);2437assert(c == call, "cannot disappear");24382439if (wide_out) {2440// Slow path call has full side-effects.2441set_predefined_output_for_runtime_call(call);2442} else {2443// Slow path call has few side-effects, and/or sets few values.2444set_predefined_output_for_runtime_call(call, prev_mem, adr_type);2445}24462447if (has_io) {2448set_i_o(_gvn.transform(new (C) ProjNode(call, TypeFunc::I_O)));2449}2450return call;24512452}24532454//------------------------------merge_memory-----------------------------------2455// Merge memory from one path into the current memory state.2456void GraphKit::merge_memory(Node* new_mem, Node* region, int new_path) {2457for (MergeMemStream mms(merged_memory(), new_mem->as_MergeMem()); mms.next_non_empty2(); ) {2458Node* old_slice = mms.force_memory();2459Node* new_slice = mms.memory2();2460if (old_slice != new_slice) {2461PhiNode* phi;2462if (old_slice->is_Phi() && old_slice->as_Phi()->region() == region) {2463if (mms.is_empty()) {2464// clone base memory Phi's inputs for this memory slice2465assert(old_slice == mms.base_memory(), "sanity");2466phi = PhiNode::make(region, NULL, Type::MEMORY, mms.adr_type(C));2467_gvn.set_type(phi, Type::MEMORY);2468for (uint i = 1; i < phi->req(); i++) {2469phi->init_req(i, old_slice->in(i));2470}2471} else {2472phi = old_slice->as_Phi(); // Phi was generated already2473}2474} else {2475phi = PhiNode::make(region, old_slice, Type::MEMORY, mms.adr_type(C));2476_gvn.set_type(phi, Type::MEMORY);2477}2478phi->set_req(new_path, new_slice);2479mms.set_memory(phi);2480}2481}2482}24832484//------------------------------make_slow_call_ex------------------------------2485// Make the exception handler hookups for the slow call2486void GraphKit::make_slow_call_ex(Node* call, ciInstanceKlass* ex_klass, bool separate_io_proj, bool deoptimize) {2487if (stopped()) return;24882489// Make a catch node with just two handlers: fall-through and catch-all2490Node* i_o = _gvn.transform( new (C) ProjNode(call, TypeFunc::I_O, separate_io_proj) );2491Node* catc = _gvn.transform( new (C) CatchNode(control(), i_o, 2) );2492Node* norm = _gvn.transform( new (C) CatchProjNode(catc, CatchProjNode::fall_through_index, CatchProjNode::no_handler_bci) );2493Node* excp = _gvn.transform( new (C) CatchProjNode(catc, CatchProjNode::catch_all_index, CatchProjNode::no_handler_bci) );24942495{ PreserveJVMState pjvms(this);2496set_control(excp);2497set_i_o(i_o);24982499if (excp != top()) {2500if (deoptimize) {2501// Deoptimize if an exception is caught. Don't construct exception state in this case.2502uncommon_trap(Deoptimization::Reason_unhandled,2503Deoptimization::Action_none);2504} else {2505// Create an exception state also.2506// Use an exact type if the caller has specified a specific exception.2507const Type* ex_type = TypeOopPtr::make_from_klass_unique(ex_klass)->cast_to_ptr_type(TypePtr::NotNull);2508Node* ex_oop = new (C) CreateExNode(ex_type, control(), i_o);2509add_exception_state(make_exception_state(_gvn.transform(ex_oop)));2510}2511}2512}25132514// Get the no-exception control from the CatchNode.2515set_control(norm);2516}251725182519//-------------------------------gen_subtype_check-----------------------------2520// Generate a subtyping check. Takes as input the subtype and supertype.2521// Returns 2 values: sets the default control() to the true path and returns2522// the false path. Only reads invariant memory; sets no (visible) memory.2523// The PartialSubtypeCheckNode sets the hidden 1-word cache in the encoding2524// but that's not exposed to the optimizer. This call also doesn't take in an2525// Object; if you wish to check an Object you need to load the Object's class2526// prior to coming here.2527Node* GraphKit::gen_subtype_check(Node* subklass, Node* superklass) {2528// Fast check for identical types, perhaps identical constants.2529// The types can even be identical non-constants, in cases2530// involving Array.newInstance, Object.clone, etc.2531if (subklass == superklass)2532return top(); // false path is dead; no test needed.25332534if (_gvn.type(superklass)->singleton()) {2535ciKlass* superk = _gvn.type(superklass)->is_klassptr()->klass();2536ciKlass* subk = _gvn.type(subklass)->is_klassptr()->klass();25372538// In the common case of an exact superklass, try to fold up the2539// test before generating code. You may ask, why not just generate2540// the code and then let it fold up? The answer is that the generated2541// code will necessarily include null checks, which do not always2542// completely fold away. If they are also needless, then they turn2543// into a performance loss. Example:2544// Foo[] fa = blah(); Foo x = fa[0]; fa[1] = x;2545// Here, the type of 'fa' is often exact, so the store check2546// of fa[1]=x will fold up, without testing the nullness of x.2547switch (static_subtype_check(superk, subk)) {2548case SSC_always_false:2549{2550Node* always_fail = control();2551set_control(top());2552return always_fail;2553}2554case SSC_always_true:2555return top();2556case SSC_easy_test:2557{2558// Just do a direct pointer compare and be done.2559Node* cmp = _gvn.transform( new(C) CmpPNode(subklass, superklass) );2560Node* bol = _gvn.transform( new(C) BoolNode(cmp, BoolTest::eq) );2561IfNode* iff = create_and_xform_if(control(), bol, PROB_STATIC_FREQUENT, COUNT_UNKNOWN);2562set_control( _gvn.transform( new(C) IfTrueNode (iff) ) );2563return _gvn.transform( new(C) IfFalseNode(iff) );2564}2565case SSC_full_test:2566break;2567default:2568ShouldNotReachHere();2569}2570}25712572// %%% Possible further optimization: Even if the superklass is not exact,2573// if the subklass is the unique subtype of the superklass, the check2574// will always succeed. We could leave a dependency behind to ensure this.25752576// First load the super-klass's check-offset2577Node *p1 = basic_plus_adr( superklass, superklass, in_bytes(Klass::super_check_offset_offset()) );2578Node *chk_off = _gvn.transform(new (C) LoadINode(NULL, memory(p1), p1, _gvn.type(p1)->is_ptr(),2579TypeInt::INT, MemNode::unordered));2580int cacheoff_con = in_bytes(Klass::secondary_super_cache_offset());2581bool might_be_cache = (find_int_con(chk_off, cacheoff_con) == cacheoff_con);25822583// Load from the sub-klass's super-class display list, or a 1-word cache of2584// the secondary superclass list, or a failing value with a sentinel offset2585// if the super-klass is an interface or exceptionally deep in the Java2586// hierarchy and we have to scan the secondary superclass list the hard way.2587// Worst-case type is a little odd: NULL is allowed as a result (usually2588// klass loads can never produce a NULL).2589Node *chk_off_X = ConvI2X(chk_off);2590Node *p2 = _gvn.transform( new (C) AddPNode(subklass,subklass,chk_off_X) );2591// For some types like interfaces the following loadKlass is from a 1-word2592// cache which is mutable so can't use immutable memory. Other2593// types load from the super-class display table which is immutable.2594Node *kmem = might_be_cache ? memory(p2) : immutable_memory();2595Node* nkls = _gvn.transform(LoadKlassNode::make(_gvn, NULL, kmem, p2, _gvn.type(p2)->is_ptr(), TypeKlassPtr::OBJECT_OR_NULL));25962597// Compile speed common case: ARE a subtype and we canNOT fail2598if( superklass == nkls )2599return top(); // false path is dead; no test needed.26002601// See if we get an immediate positive hit. Happens roughly 83% of the2602// time. Test to see if the value loaded just previously from the subklass2603// is exactly the superklass.2604Node *cmp1 = _gvn.transform( new (C) CmpPNode( superklass, nkls ) );2605Node *bol1 = _gvn.transform( new (C) BoolNode( cmp1, BoolTest::eq ) );2606IfNode *iff1 = create_and_xform_if( control(), bol1, PROB_LIKELY(0.83f), COUNT_UNKNOWN );2607Node *iftrue1 = _gvn.transform( new (C) IfTrueNode ( iff1 ) );2608set_control( _gvn.transform( new (C) IfFalseNode( iff1 ) ) );26092610// Compile speed common case: Check for being deterministic right now. If2611// chk_off is a constant and not equal to cacheoff then we are NOT a2612// subklass. In this case we need exactly the 1 test above and we can2613// return those results immediately.2614if (!might_be_cache) {2615Node* not_subtype_ctrl = control();2616set_control(iftrue1); // We need exactly the 1 test above2617return not_subtype_ctrl;2618}26192620// Gather the various success & failures here2621RegionNode *r_ok_subtype = new (C) RegionNode(4);2622record_for_igvn(r_ok_subtype);2623RegionNode *r_not_subtype = new (C) RegionNode(3);2624record_for_igvn(r_not_subtype);26252626r_ok_subtype->init_req(1, iftrue1);26272628// Check for immediate negative hit. Happens roughly 11% of the time (which2629// is roughly 63% of the remaining cases). Test to see if the loaded2630// check-offset points into the subklass display list or the 1-element2631// cache. If it points to the display (and NOT the cache) and the display2632// missed then it's not a subtype.2633Node *cacheoff = _gvn.intcon(cacheoff_con);2634Node *cmp2 = _gvn.transform( new (C) CmpINode( chk_off, cacheoff ) );2635Node *bol2 = _gvn.transform( new (C) BoolNode( cmp2, BoolTest::ne ) );2636IfNode *iff2 = create_and_xform_if( control(), bol2, PROB_LIKELY(0.63f), COUNT_UNKNOWN );2637r_not_subtype->init_req(1, _gvn.transform( new (C) IfTrueNode (iff2) ) );2638set_control( _gvn.transform( new (C) IfFalseNode(iff2) ) );26392640// Check for self. Very rare to get here, but it is taken 1/3 the time.2641// No performance impact (too rare) but allows sharing of secondary arrays2642// which has some footprint reduction.2643Node *cmp3 = _gvn.transform( new (C) CmpPNode( subklass, superklass ) );2644Node *bol3 = _gvn.transform( new (C) BoolNode( cmp3, BoolTest::eq ) );2645IfNode *iff3 = create_and_xform_if( control(), bol3, PROB_LIKELY(0.36f), COUNT_UNKNOWN );2646r_ok_subtype->init_req(2, _gvn.transform( new (C) IfTrueNode ( iff3 ) ) );2647set_control( _gvn.transform( new (C) IfFalseNode( iff3 ) ) );26482649// -- Roads not taken here: --2650// We could also have chosen to perform the self-check at the beginning2651// of this code sequence, as the assembler does. This would not pay off2652// the same way, since the optimizer, unlike the assembler, can perform2653// static type analysis to fold away many successful self-checks.2654// Non-foldable self checks work better here in second position, because2655// the initial primary superclass check subsumes a self-check for most2656// types. An exception would be a secondary type like array-of-interface,2657// which does not appear in its own primary supertype display.2658// Finally, we could have chosen to move the self-check into the2659// PartialSubtypeCheckNode, and from there out-of-line in a platform2660// dependent manner. But it is worthwhile to have the check here,2661// where it can be perhaps be optimized. The cost in code space is2662// small (register compare, branch).26632664// Now do a linear scan of the secondary super-klass array. Again, no real2665// performance impact (too rare) but it's gotta be done.2666// Since the code is rarely used, there is no penalty for moving it2667// out of line, and it can only improve I-cache density.2668// The decision to inline or out-of-line this final check is platform2669// dependent, and is found in the AD file definition of PartialSubtypeCheck.2670Node* psc = _gvn.transform(2671new (C) PartialSubtypeCheckNode(control(), subklass, superklass) );26722673Node *cmp4 = _gvn.transform( new (C) CmpPNode( psc, null() ) );2674Node *bol4 = _gvn.transform( new (C) BoolNode( cmp4, BoolTest::ne ) );2675IfNode *iff4 = create_and_xform_if( control(), bol4, PROB_FAIR, COUNT_UNKNOWN );2676r_not_subtype->init_req(2, _gvn.transform( new (C) IfTrueNode (iff4) ) );2677r_ok_subtype ->init_req(3, _gvn.transform( new (C) IfFalseNode(iff4) ) );26782679// Return false path; set default control to true path.2680set_control( _gvn.transform(r_ok_subtype) );2681return _gvn.transform(r_not_subtype);2682}26832684//----------------------------static_subtype_check-----------------------------2685// Shortcut important common cases when superklass is exact:2686// (0) superklass is java.lang.Object (can occur in reflective code)2687// (1) subklass is already limited to a subtype of superklass => always ok2688// (2) subklass does not overlap with superklass => always fail2689// (3) superklass has NO subtypes and we can check with a simple compare.2690int GraphKit::static_subtype_check(ciKlass* superk, ciKlass* subk) {2691if (StressReflectiveCode) {2692return SSC_full_test; // Let caller generate the general case.2693}26942695if (superk == env()->Object_klass()) {2696return SSC_always_true; // (0) this test cannot fail2697}26982699ciType* superelem = superk;2700if (superelem->is_array_klass())2701superelem = superelem->as_array_klass()->base_element_type();27022703if (!subk->is_interface()) { // cannot trust static interface types yet2704if (subk->is_subtype_of(superk)) {2705return SSC_always_true; // (1) false path dead; no dynamic test needed2706}2707if (!(superelem->is_klass() && superelem->as_klass()->is_interface()) &&2708!superk->is_subtype_of(subk)) {2709return SSC_always_false;2710}2711}27122713// If casting to an instance klass, it must have no subtypes2714if (superk->is_interface()) {2715// Cannot trust interfaces yet.2716// %%% S.B. superk->nof_implementors() == 12717} else if (superelem->is_instance_klass()) {2718ciInstanceKlass* ik = superelem->as_instance_klass();2719if (!ik->has_subklass() && !ik->is_interface()) {2720if (!ik->is_final()) {2721// Add a dependency if there is a chance of a later subclass.2722C->dependencies()->assert_leaf_type(ik);2723}2724return SSC_easy_test; // (3) caller can do a simple ptr comparison2725}2726} else {2727// A primitive array type has no subtypes.2728return SSC_easy_test; // (3) caller can do a simple ptr comparison2729}27302731return SSC_full_test;2732}27332734// Profile-driven exact type check:2735Node* GraphKit::type_check_receiver(Node* receiver, ciKlass* klass,2736float prob,2737Node* *casted_receiver) {2738const TypeKlassPtr* tklass = TypeKlassPtr::make(klass);2739Node* recv_klass = load_object_klass(receiver);2740Node* want_klass = makecon(tklass);2741Node* cmp = _gvn.transform( new(C) CmpPNode(recv_klass, want_klass) );2742Node* bol = _gvn.transform( new(C) BoolNode(cmp, BoolTest::eq) );2743IfNode* iff = create_and_xform_if(control(), bol, prob, COUNT_UNKNOWN);2744set_control( _gvn.transform( new(C) IfTrueNode (iff) ));2745Node* fail = _gvn.transform( new(C) IfFalseNode(iff) );27462747const TypeOopPtr* recv_xtype = tklass->as_instance_type();2748assert(recv_xtype->klass_is_exact(), "");27492750// Subsume downstream occurrences of receiver with a cast to2751// recv_xtype, since now we know what the type will be.2752Node* cast = new(C) CheckCastPPNode(control(), receiver, recv_xtype);2753(*casted_receiver) = _gvn.transform(cast);2754// (User must make the replace_in_map call.)27552756return fail;2757}275827592760//------------------------------seems_never_null-------------------------------2761// Use null_seen information if it is available from the profile.2762// If we see an unexpected null at a type check we record it and force a2763// recompile; the offending check will be recompiled to handle NULLs.2764// If we see several offending BCIs, then all checks in the2765// method will be recompiled.2766bool GraphKit::seems_never_null(Node* obj, ciProfileData* data) {2767if (UncommonNullCast // Cutout for this technique2768&& obj != null() // And not the -Xcomp stupid case?2769&& !too_many_traps(Deoptimization::Reason_null_check)2770) {2771if (data == NULL)2772// Edge case: no mature data. Be optimistic here.2773return true;2774// If the profile has not seen a null, assume it won't happen.2775assert(java_bc() == Bytecodes::_checkcast ||2776java_bc() == Bytecodes::_instanceof ||2777java_bc() == Bytecodes::_aastore, "MDO must collect null_seen bit here");2778return !data->as_BitData()->null_seen();2779}2780return false;2781}27822783//------------------------maybe_cast_profiled_receiver-------------------------2784// If the profile has seen exactly one type, narrow to exactly that type.2785// Subsequent type checks will always fold up.2786Node* GraphKit::maybe_cast_profiled_receiver(Node* not_null_obj,2787ciKlass* require_klass,2788ciKlass* spec_klass,2789bool safe_for_replace) {2790if (!UseTypeProfile || !TypeProfileCasts) return NULL;27912792Deoptimization::DeoptReason reason = spec_klass == NULL ? Deoptimization::Reason_class_check : Deoptimization::Reason_speculate_class_check;27932794// Make sure we haven't already deoptimized from this tactic.2795if (too_many_traps(reason) || too_many_recompiles(reason))2796return NULL;27972798// (No, this isn't a call, but it's enough like a virtual call2799// to use the same ciMethod accessor to get the profile info...)2800// If we have a speculative type use it instead of profiling (which2801// may not help us)2802ciKlass* exact_kls = spec_klass == NULL ? profile_has_unique_klass() : spec_klass;2803if (exact_kls != NULL) {// no cast failures here2804if (require_klass == NULL ||2805static_subtype_check(require_klass, exact_kls) == SSC_always_true) {2806// If we narrow the type to match what the type profile sees or2807// the speculative type, we can then remove the rest of the2808// cast.2809// This is a win, even if the exact_kls is very specific,2810// because downstream operations, such as method calls,2811// will often benefit from the sharper type.2812Node* exact_obj = not_null_obj; // will get updated in place...2813Node* slow_ctl = type_check_receiver(exact_obj, exact_kls, 1.0,2814&exact_obj);2815{ PreserveJVMState pjvms(this);2816set_control(slow_ctl);2817uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);2818}2819if (safe_for_replace) {2820replace_in_map(not_null_obj, exact_obj);2821}2822return exact_obj;2823}2824// assert(ssc == SSC_always_true)... except maybe the profile lied to us.2825}28262827return NULL;2828}28292830/**2831* Cast obj to type and emit guard unless we had too many traps here2832* already2833*2834* @param obj node being casted2835* @param type type to cast the node to2836* @param not_null true if we know node cannot be null2837*/2838Node* GraphKit::maybe_cast_profiled_obj(Node* obj,2839ciKlass* type,2840bool not_null) {2841// type == NULL if profiling tells us this object is always null2842if (type != NULL) {2843Deoptimization::DeoptReason class_reason = Deoptimization::Reason_speculate_class_check;2844Deoptimization::DeoptReason null_reason = Deoptimization::Reason_null_check;2845if (!too_many_traps(null_reason) && !too_many_recompiles(null_reason) &&2846!too_many_traps(class_reason) && !too_many_recompiles(class_reason)) {2847Node* not_null_obj = NULL;2848// not_null is true if we know the object is not null and2849// there's no need for a null check2850if (!not_null) {2851Node* null_ctl = top();2852not_null_obj = null_check_oop(obj, &null_ctl, true, true);2853assert(null_ctl->is_top(), "no null control here");2854} else {2855not_null_obj = obj;2856}28572858Node* exact_obj = not_null_obj;2859ciKlass* exact_kls = type;2860Node* slow_ctl = type_check_receiver(exact_obj, exact_kls, 1.0,2861&exact_obj);2862{2863PreserveJVMState pjvms(this);2864set_control(slow_ctl);2865uncommon_trap_exact(class_reason, Deoptimization::Action_maybe_recompile);2866}2867replace_in_map(not_null_obj, exact_obj);2868obj = exact_obj;2869}2870} else {2871if (!too_many_traps(Deoptimization::Reason_null_assert) &&2872!too_many_recompiles(Deoptimization::Reason_null_assert)) {2873Node* exact_obj = null_assert(obj);2874replace_in_map(obj, exact_obj);2875obj = exact_obj;2876}2877}2878return obj;2879}28802881//-------------------------------gen_instanceof--------------------------------2882// Generate an instance-of idiom. Used by both the instance-of bytecode2883// and the reflective instance-of call.2884Node* GraphKit::gen_instanceof(Node* obj, Node* superklass, bool safe_for_replace) {2885kill_dead_locals(); // Benefit all the uncommon traps2886assert( !stopped(), "dead parse path should be checked in callers" );2887assert(!TypePtr::NULL_PTR->higher_equal(_gvn.type(superklass)->is_klassptr()),2888"must check for not-null not-dead klass in callers");28892890// Make the merge point2891enum { _obj_path = 1, _fail_path, _null_path, PATH_LIMIT };2892RegionNode* region = new(C) RegionNode(PATH_LIMIT);2893Node* phi = new(C) PhiNode(region, TypeInt::BOOL);2894C->set_has_split_ifs(true); // Has chance for split-if optimization28952896ciProfileData* data = NULL;2897if (java_bc() == Bytecodes::_instanceof) { // Only for the bytecode2898data = method()->method_data()->bci_to_data(bci());2899}2900bool never_see_null = (ProfileDynamicTypes // aggressive use of profile2901&& seems_never_null(obj, data));29022903// Null check; get casted pointer; set region slot 32904Node* null_ctl = top();2905Node* not_null_obj = null_check_oop(obj, &null_ctl, never_see_null, safe_for_replace);29062907// If not_null_obj is dead, only null-path is taken2908if (stopped()) { // Doing instance-of on a NULL?2909set_control(null_ctl);2910return intcon(0);2911}2912region->init_req(_null_path, null_ctl);2913phi ->init_req(_null_path, intcon(0)); // Set null path value2914if (null_ctl == top()) {2915// Do this eagerly, so that pattern matches like is_diamond_phi2916// will work even during parsing.2917assert(_null_path == PATH_LIMIT-1, "delete last");2918region->del_req(_null_path);2919phi ->del_req(_null_path);2920}29212922// Do we know the type check always succeed?2923bool known_statically = false;2924if (_gvn.type(superklass)->singleton()) {2925ciKlass* superk = _gvn.type(superklass)->is_klassptr()->klass();2926ciKlass* subk = _gvn.type(obj)->is_oopptr()->klass();2927if (subk != NULL && subk->is_loaded()) {2928int static_res = static_subtype_check(superk, subk);2929known_statically = (static_res == SSC_always_true || static_res == SSC_always_false);2930}2931}29322933if (known_statically && UseTypeSpeculation) {2934// If we know the type check always succeeds then we don't use the2935// profiling data at this bytecode. Don't lose it, feed it to the2936// type system as a speculative type.2937not_null_obj = record_profiled_receiver_for_speculation(not_null_obj);2938} else {2939const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();2940// We may not have profiling here or it may not help us. If we2941// have a speculative type use it to perform an exact cast.2942ciKlass* spec_obj_type = obj_type->speculative_type();2943if (spec_obj_type != NULL || (ProfileDynamicTypes && data != NULL)) {2944Node* cast_obj = maybe_cast_profiled_receiver(not_null_obj, NULL, spec_obj_type, safe_for_replace);2945if (stopped()) { // Profile disagrees with this path.2946set_control(null_ctl); // Null is the only remaining possibility.2947return intcon(0);2948}2949if (cast_obj != NULL) {2950not_null_obj = cast_obj;2951}2952}2953}29542955// Load the object's klass2956Node* obj_klass = load_object_klass(not_null_obj);29572958// Generate the subtype check2959Node* not_subtype_ctrl = gen_subtype_check(obj_klass, superklass);29602961// Plug in the success path to the general merge in slot 1.2962region->init_req(_obj_path, control());2963phi ->init_req(_obj_path, intcon(1));29642965// Plug in the failing path to the general merge in slot 2.2966region->init_req(_fail_path, not_subtype_ctrl);2967phi ->init_req(_fail_path, intcon(0));29682969// Return final merged results2970set_control( _gvn.transform(region) );2971record_for_igvn(region);2972return _gvn.transform(phi);2973}29742975//-------------------------------gen_checkcast---------------------------------2976// Generate a checkcast idiom. Used by both the checkcast bytecode and the2977// array store bytecode. Stack must be as-if BEFORE doing the bytecode so the2978// uncommon-trap paths work. Adjust stack after this call.2979// If failure_control is supplied and not null, it is filled in with2980// the control edge for the cast failure. Otherwise, an appropriate2981// uncommon trap or exception is thrown.2982Node* GraphKit::gen_checkcast(Node *obj, Node* superklass,2983Node* *failure_control) {2984kill_dead_locals(); // Benefit all the uncommon traps2985const TypeKlassPtr *tk = _gvn.type(superklass)->is_klassptr();2986const Type *toop = TypeOopPtr::make_from_klass(tk->klass());29872988// Fast cutout: Check the case that the cast is vacuously true.2989// This detects the common cases where the test will short-circuit2990// away completely. We do this before we perform the null check,2991// because if the test is going to turn into zero code, we don't2992// want a residual null check left around. (Causes a slowdown,2993// for example, in some objArray manipulations, such as a[i]=a[j].)2994if (tk->singleton()) {2995const TypeOopPtr* objtp = _gvn.type(obj)->isa_oopptr();2996if (objtp != NULL && objtp->klass() != NULL) {2997switch (static_subtype_check(tk->klass(), objtp->klass())) {2998case SSC_always_true:2999// If we know the type check always succeed then we don't use3000// the profiling data at this bytecode. Don't lose it, feed it3001// to the type system as a speculative type.3002return record_profiled_receiver_for_speculation(obj);3003case SSC_always_false:3004// It needs a null check because a null will *pass* the cast check.3005// A non-null value will always produce an exception.3006return null_assert(obj);3007}3008}3009}30103011ciProfileData* data = NULL;3012bool safe_for_replace = false;3013if (failure_control == NULL) { // use MDO in regular case only3014assert(java_bc() == Bytecodes::_aastore ||3015java_bc() == Bytecodes::_checkcast,3016"interpreter profiles type checks only for these BCs");3017data = method()->method_data()->bci_to_data(bci());3018safe_for_replace = true;3019}30203021// Make the merge point3022enum { _obj_path = 1, _null_path, PATH_LIMIT };3023RegionNode* region = new (C) RegionNode(PATH_LIMIT);3024Node* phi = new (C) PhiNode(region, toop);3025C->set_has_split_ifs(true); // Has chance for split-if optimization30263027// Use null-cast information if it is available3028bool never_see_null = ((failure_control == NULL) // regular case only3029&& seems_never_null(obj, data));30303031// Null check; get casted pointer; set region slot 33032Node* null_ctl = top();3033Node* not_null_obj = null_check_oop(obj, &null_ctl, never_see_null, safe_for_replace);30343035// If not_null_obj is dead, only null-path is taken3036if (stopped()) { // Doing instance-of on a NULL?3037set_control(null_ctl);3038return null();3039}3040region->init_req(_null_path, null_ctl);3041phi ->init_req(_null_path, null()); // Set null path value3042if (null_ctl == top()) {3043// Do this eagerly, so that pattern matches like is_diamond_phi3044// will work even during parsing.3045assert(_null_path == PATH_LIMIT-1, "delete last");3046region->del_req(_null_path);3047phi ->del_req(_null_path);3048}30493050Node* cast_obj = NULL;3051if (tk->klass_is_exact()) {3052// The following optimization tries to statically cast the speculative type of the object3053// (for example obtained during profiling) to the type of the superklass and then do a3054// dynamic check that the type of the object is what we expect. To work correctly3055// for checkcast and aastore the type of superklass should be exact.3056const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();3057// We may not have profiling here or it may not help us. If we have3058// a speculative type use it to perform an exact cast.3059ciKlass* spec_obj_type = obj_type->speculative_type();3060if (spec_obj_type != NULL ||3061(data != NULL &&3062// Counter has never been decremented (due to cast failure).3063// ...This is a reasonable thing to expect. It is true of3064// all casts inserted by javac to implement generic types.3065data->as_CounterData()->count() >= 0)) {3066cast_obj = maybe_cast_profiled_receiver(not_null_obj, tk->klass(), spec_obj_type, safe_for_replace);3067if (cast_obj != NULL) {3068if (failure_control != NULL) // failure is now impossible3069(*failure_control) = top();3070// adjust the type of the phi to the exact klass:3071phi->raise_bottom_type(_gvn.type(cast_obj)->meet_speculative(TypePtr::NULL_PTR));3072}3073}3074}30753076if (cast_obj == NULL) {3077// Load the object's klass3078Node* obj_klass = load_object_klass(not_null_obj);30793080// Generate the subtype check3081Node* not_subtype_ctrl = gen_subtype_check( obj_klass, superklass );30823083// Plug in success path into the merge3084cast_obj = _gvn.transform(new (C) CheckCastPPNode(control(),3085not_null_obj, toop));3086// Failure path ends in uncommon trap (or may be dead - failure impossible)3087if (failure_control == NULL) {3088if (not_subtype_ctrl != top()) { // If failure is possible3089PreserveJVMState pjvms(this);3090set_control(not_subtype_ctrl);3091builtin_throw(Deoptimization::Reason_class_check, obj_klass);3092}3093} else {3094(*failure_control) = not_subtype_ctrl;3095}3096}30973098region->init_req(_obj_path, control());3099phi ->init_req(_obj_path, cast_obj);31003101// A merge of NULL or Casted-NotNull obj3102Node* res = _gvn.transform(phi);31033104// Note I do NOT always 'replace_in_map(obj,result)' here.3105// if( tk->klass()->can_be_primary_super() )3106// This means that if I successfully store an Object into an array-of-String3107// I 'forget' that the Object is really now known to be a String. I have to3108// do this because we don't have true union types for interfaces - if I store3109// a Baz into an array-of-Interface and then tell the optimizer it's an3110// Interface, I forget that it's also a Baz and cannot do Baz-like field3111// references to it. FIX THIS WHEN UNION TYPES APPEAR!3112// replace_in_map( obj, res );31133114// Return final merged results3115set_control( _gvn.transform(region) );3116record_for_igvn(region);3117return res;3118}31193120//------------------------------next_monitor-----------------------------------3121// What number should be given to the next monitor?3122int GraphKit::next_monitor() {3123int current = jvms()->monitor_depth()* C->sync_stack_slots();3124int next = current + C->sync_stack_slots();3125// Keep the toplevel high water mark current:3126if (C->fixed_slots() < next) C->set_fixed_slots(next);3127return current;3128}31293130//------------------------------insert_mem_bar---------------------------------3131// Memory barrier to avoid floating things around3132// The membar serves as a pinch point between both control and all memory slices.3133Node* GraphKit::insert_mem_bar(int opcode, Node* precedent) {3134MemBarNode* mb = MemBarNode::make(C, opcode, Compile::AliasIdxBot, precedent);3135mb->init_req(TypeFunc::Control, control());3136mb->init_req(TypeFunc::Memory, reset_memory());3137Node* membar = _gvn.transform(mb);3138set_control(_gvn.transform(new (C) ProjNode(membar, TypeFunc::Control)));3139set_all_memory_call(membar);3140return membar;3141}31423143//-------------------------insert_mem_bar_volatile----------------------------3144// Memory barrier to avoid floating things around3145// The membar serves as a pinch point between both control and memory(alias_idx).3146// If you want to make a pinch point on all memory slices, do not use this3147// function (even with AliasIdxBot); use insert_mem_bar() instead.3148Node* GraphKit::insert_mem_bar_volatile(int opcode, int alias_idx, Node* precedent) {3149// When Parse::do_put_xxx updates a volatile field, it appends a series3150// of MemBarVolatile nodes, one for *each* volatile field alias category.3151// The first membar is on the same memory slice as the field store opcode.3152// This forces the membar to follow the store. (Bug 6500685 broke this.)3153// All the other membars (for other volatile slices, including AliasIdxBot,3154// which stands for all unknown volatile slices) are control-dependent3155// on the first membar. This prevents later volatile loads or stores3156// from sliding up past the just-emitted store.31573158MemBarNode* mb = MemBarNode::make(C, opcode, alias_idx, precedent);3159mb->set_req(TypeFunc::Control,control());3160if (alias_idx == Compile::AliasIdxBot) {3161mb->set_req(TypeFunc::Memory, merged_memory()->base_memory());3162} else {3163assert(!(opcode == Op_Initialize && alias_idx != Compile::AliasIdxRaw), "fix caller");3164mb->set_req(TypeFunc::Memory, memory(alias_idx));3165}3166Node* membar = _gvn.transform(mb);3167set_control(_gvn.transform(new (C) ProjNode(membar, TypeFunc::Control)));3168if (alias_idx == Compile::AliasIdxBot) {3169merged_memory()->set_base_memory(_gvn.transform(new (C) ProjNode(membar, TypeFunc::Memory)));3170} else {3171set_memory(_gvn.transform(new (C) ProjNode(membar, TypeFunc::Memory)),alias_idx);3172}3173return membar;3174}31753176//------------------------------shared_lock------------------------------------3177// Emit locking code.3178FastLockNode* GraphKit::shared_lock(Node* obj) {3179// bci is either a monitorenter bc or InvocationEntryBci3180// %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces3181assert(SynchronizationEntryBCI == InvocationEntryBci, "");31823183if( !GenerateSynchronizationCode )3184return NULL; // Not locking things?3185if (stopped()) // Dead monitor?3186return NULL;31873188assert(dead_locals_are_killed(), "should kill locals before sync. point");31893190// Box the stack location3191Node* box = _gvn.transform(new (C) BoxLockNode(next_monitor()));3192Node* mem = reset_memory();31933194FastLockNode * flock = _gvn.transform(new (C) FastLockNode(0, obj, box) )->as_FastLock();3195if (UseBiasedLocking && PrintPreciseBiasedLockingStatistics) {3196// Create the counters for this fast lock.3197flock->create_lock_counter(sync_jvms()); // sync_jvms used to get current bci3198}31993200// Create the rtm counters for this fast lock if needed.3201flock->create_rtm_lock_counter(sync_jvms()); // sync_jvms used to get current bci32023203// Add monitor to debug info for the slow path. If we block inside the3204// slow path and de-opt, we need the monitor hanging around3205map()->push_monitor( flock );32063207const TypeFunc *tf = LockNode::lock_type();3208LockNode *lock = new (C) LockNode(C, tf);32093210lock->init_req( TypeFunc::Control, control() );3211lock->init_req( TypeFunc::Memory , mem );3212lock->init_req( TypeFunc::I_O , top() ) ; // does no i/o3213lock->init_req( TypeFunc::FramePtr, frameptr() );3214lock->init_req( TypeFunc::ReturnAdr, top() );32153216lock->init_req(TypeFunc::Parms + 0, obj);3217lock->init_req(TypeFunc::Parms + 1, box);3218lock->init_req(TypeFunc::Parms + 2, flock);3219add_safepoint_edges(lock);32203221lock = _gvn.transform( lock )->as_Lock();32223223// lock has no side-effects, sets few values3224set_predefined_output_for_runtime_call(lock, mem, TypeRawPtr::BOTTOM);32253226insert_mem_bar(Op_MemBarAcquireLock);32273228// Add this to the worklist so that the lock can be eliminated3229record_for_igvn(lock);32303231#ifndef PRODUCT3232if (PrintLockStatistics) {3233// Update the counter for this lock. Don't bother using an atomic3234// operation since we don't require absolute accuracy.3235lock->create_lock_counter(map()->jvms());3236increment_counter(lock->counter()->addr());3237}3238#endif32393240return flock;3241}324232433244//------------------------------shared_unlock----------------------------------3245// Emit unlocking code.3246void GraphKit::shared_unlock(Node* box, Node* obj) {3247// bci is either a monitorenter bc or InvocationEntryBci3248// %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces3249assert(SynchronizationEntryBCI == InvocationEntryBci, "");32503251if( !GenerateSynchronizationCode )3252return;3253if (stopped()) { // Dead monitor?3254map()->pop_monitor(); // Kill monitor from debug info3255return;3256}32573258// Memory barrier to avoid floating things down past the locked region3259insert_mem_bar(Op_MemBarReleaseLock);32603261const TypeFunc *tf = OptoRuntime::complete_monitor_exit_Type();3262UnlockNode *unlock = new (C) UnlockNode(C, tf);3263#ifdef ASSERT3264unlock->set_dbg_jvms(sync_jvms());3265#endif3266uint raw_idx = Compile::AliasIdxRaw;3267unlock->init_req( TypeFunc::Control, control() );3268unlock->init_req( TypeFunc::Memory , memory(raw_idx) );3269unlock->init_req( TypeFunc::I_O , top() ) ; // does no i/o3270unlock->init_req( TypeFunc::FramePtr, frameptr() );3271unlock->init_req( TypeFunc::ReturnAdr, top() );32723273unlock->init_req(TypeFunc::Parms + 0, obj);3274unlock->init_req(TypeFunc::Parms + 1, box);3275unlock = _gvn.transform(unlock)->as_Unlock();32763277Node* mem = reset_memory();32783279// unlock has no side-effects, sets few values3280set_predefined_output_for_runtime_call(unlock, mem, TypeRawPtr::BOTTOM);32813282// Kill monitor from debug info3283map()->pop_monitor( );3284}32853286//-------------------------------get_layout_helper-----------------------------3287// If the given klass is a constant or known to be an array,3288// fetch the constant layout helper value into constant_value3289// and return (Node*)NULL. Otherwise, load the non-constant3290// layout helper value, and return the node which represents it.3291// This two-faced routine is useful because allocation sites3292// almost always feature constant types.3293Node* GraphKit::get_layout_helper(Node* klass_node, jint& constant_value) {3294const TypeKlassPtr* inst_klass = _gvn.type(klass_node)->isa_klassptr();3295if (!StressReflectiveCode && inst_klass != NULL) {3296ciKlass* klass = inst_klass->klass();3297bool xklass = inst_klass->klass_is_exact();3298if (xklass || klass->is_array_klass()) {3299jint lhelper = klass->layout_helper();3300if (lhelper != Klass::_lh_neutral_value) {3301constant_value = lhelper;3302return (Node*) NULL;3303}3304}3305}3306constant_value = Klass::_lh_neutral_value; // put in a known value3307Node* lhp = basic_plus_adr(klass_node, klass_node, in_bytes(Klass::layout_helper_offset()));3308return make_load(NULL, lhp, TypeInt::INT, T_INT, MemNode::unordered);3309}33103311// We just put in an allocate/initialize with a big raw-memory effect.3312// Hook selected additional alias categories on the initialization.3313static void hook_memory_on_init(GraphKit& kit, int alias_idx,3314MergeMemNode* init_in_merge,3315Node* init_out_raw) {3316DEBUG_ONLY(Node* init_in_raw = init_in_merge->base_memory());3317assert(init_in_merge->memory_at(alias_idx) == init_in_raw, "");33183319Node* prevmem = kit.memory(alias_idx);3320init_in_merge->set_memory_at(alias_idx, prevmem);3321kit.set_memory(init_out_raw, alias_idx);3322}33233324//---------------------------set_output_for_allocation-------------------------3325Node* GraphKit::set_output_for_allocation(AllocateNode* alloc,3326const TypeOopPtr* oop_type,3327bool deoptimize_on_exception) {3328int rawidx = Compile::AliasIdxRaw;3329alloc->set_req( TypeFunc::FramePtr, frameptr() );3330add_safepoint_edges(alloc);3331Node* allocx = _gvn.transform(alloc);3332set_control( _gvn.transform(new (C) ProjNode(allocx, TypeFunc::Control) ) );3333// create memory projection for i_o3334set_memory ( _gvn.transform( new (C) ProjNode(allocx, TypeFunc::Memory, true) ), rawidx );3335make_slow_call_ex(allocx, env()->Throwable_klass(), true, deoptimize_on_exception);33363337// create a memory projection as for the normal control path3338Node* malloc = _gvn.transform(new (C) ProjNode(allocx, TypeFunc::Memory));3339set_memory(malloc, rawidx);33403341// a normal slow-call doesn't change i_o, but an allocation does3342// we create a separate i_o projection for the normal control path3343set_i_o(_gvn.transform( new (C) ProjNode(allocx, TypeFunc::I_O, false) ) );3344Node* rawoop = _gvn.transform( new (C) ProjNode(allocx, TypeFunc::Parms) );33453346// put in an initialization barrier3347InitializeNode* init = insert_mem_bar_volatile(Op_Initialize, rawidx,3348rawoop)->as_Initialize();3349assert(alloc->initialization() == init, "2-way macro link must work");3350assert(init ->allocation() == alloc, "2-way macro link must work");3351{3352// Extract memory strands which may participate in the new object's3353// initialization, and source them from the new InitializeNode.3354// This will allow us to observe initializations when they occur,3355// and link them properly (as a group) to the InitializeNode.3356assert(init->in(InitializeNode::Memory) == malloc, "");3357MergeMemNode* minit_in = MergeMemNode::make(C, malloc);3358init->set_req(InitializeNode::Memory, minit_in);3359record_for_igvn(minit_in); // fold it up later, if possible3360Node* minit_out = memory(rawidx);3361assert(minit_out->is_Proj() && minit_out->in(0) == init, "");3362if (oop_type->isa_aryptr()) {3363const TypePtr* telemref = oop_type->add_offset(Type::OffsetBot);3364int elemidx = C->get_alias_index(telemref);3365hook_memory_on_init(*this, elemidx, minit_in, minit_out);3366} else if (oop_type->isa_instptr()) {3367ciInstanceKlass* ik = oop_type->klass()->as_instance_klass();3368for (int i = 0, len = ik->nof_nonstatic_fields(); i < len; i++) {3369ciField* field = ik->nonstatic_field_at(i);3370if (field->offset() >= TrackedInitializationLimit * HeapWordSize)3371continue; // do not bother to track really large numbers of fields3372// Find (or create) the alias category for this field:3373int fieldidx = C->alias_type(field)->index();3374hook_memory_on_init(*this, fieldidx, minit_in, minit_out);3375}3376}3377}33783379// Cast raw oop to the real thing...3380Node* javaoop = new (C) CheckCastPPNode(control(), rawoop, oop_type);3381javaoop = _gvn.transform(javaoop);3382C->set_recent_alloc(control(), javaoop);3383assert(just_allocated_object(control()) == javaoop, "just allocated");33843385#ifdef ASSERT3386{ // Verify that the AllocateNode::Ideal_allocation recognizers work:3387assert(AllocateNode::Ideal_allocation(rawoop, &_gvn) == alloc,3388"Ideal_allocation works");3389assert(AllocateNode::Ideal_allocation(javaoop, &_gvn) == alloc,3390"Ideal_allocation works");3391if (alloc->is_AllocateArray()) {3392assert(AllocateArrayNode::Ideal_array_allocation(rawoop, &_gvn) == alloc->as_AllocateArray(),3393"Ideal_allocation works");3394assert(AllocateArrayNode::Ideal_array_allocation(javaoop, &_gvn) == alloc->as_AllocateArray(),3395"Ideal_allocation works");3396} else {3397assert(alloc->in(AllocateNode::ALength)->is_top(), "no length, please");3398}3399}3400#endif //ASSERT34013402return javaoop;3403}34043405//---------------------------new_instance--------------------------------------3406// This routine takes a klass_node which may be constant (for a static type)3407// or may be non-constant (for reflective code). It will work equally well3408// for either, and the graph will fold nicely if the optimizer later reduces3409// the type to a constant.3410// The optional arguments are for specialized use by intrinsics:3411// - If 'extra_slow_test' if not null is an extra condition for the slow-path.3412// - If 'return_size_val', report the the total object size to the caller.3413// - deoptimize_on_exception controls how Java exceptions are handled (rethrow vs deoptimize)3414Node* GraphKit::new_instance(Node* klass_node,3415Node* extra_slow_test,3416Node* *return_size_val,3417bool deoptimize_on_exception) {3418// Compute size in doublewords3419// The size is always an integral number of doublewords, represented3420// as a positive bytewise size stored in the klass's layout_helper.3421// The layout_helper also encodes (in a low bit) the need for a slow path.3422jint layout_con = Klass::_lh_neutral_value;3423Node* layout_val = get_layout_helper(klass_node, layout_con);3424int layout_is_con = (layout_val == NULL);34253426if (extra_slow_test == NULL) extra_slow_test = intcon(0);3427// Generate the initial go-slow test. It's either ALWAYS (return a3428// Node for 1) or NEVER (return a NULL) or perhaps (in the reflective3429// case) a computed value derived from the layout_helper.3430Node* initial_slow_test = NULL;3431if (layout_is_con) {3432assert(!StressReflectiveCode, "stress mode does not use these paths");3433bool must_go_slow = Klass::layout_helper_needs_slow_path(layout_con);3434initial_slow_test = must_go_slow? intcon(1): extra_slow_test;34353436} else { // reflective case3437// This reflective path is used by Unsafe.allocateInstance.3438// (It may be stress-tested by specifying StressReflectiveCode.)3439// Basically, we want to get into the VM is there's an illegal argument.3440Node* bit = intcon(Klass::_lh_instance_slow_path_bit);3441initial_slow_test = _gvn.transform( new (C) AndINode(layout_val, bit) );3442if (extra_slow_test != intcon(0)) {3443initial_slow_test = _gvn.transform( new (C) OrINode(initial_slow_test, extra_slow_test) );3444}3445// (Macro-expander will further convert this to a Bool, if necessary.)3446}34473448// Find the size in bytes. This is easy; it's the layout_helper.3449// The size value must be valid even if the slow path is taken.3450Node* size = NULL;3451if (layout_is_con) {3452size = MakeConX(Klass::layout_helper_size_in_bytes(layout_con));3453} else { // reflective case3454// This reflective path is used by clone and Unsafe.allocateInstance.3455size = ConvI2X(layout_val);34563457// Clear the low bits to extract layout_helper_size_in_bytes:3458assert((int)Klass::_lh_instance_slow_path_bit < BytesPerLong, "clear bit");3459Node* mask = MakeConX(~ (intptr_t)right_n_bits(LogBytesPerLong));3460size = _gvn.transform( new (C) AndXNode(size, mask) );3461}3462if (return_size_val != NULL) {3463(*return_size_val) = size;3464}34653466// This is a precise notnull oop of the klass.3467// (Actually, it need not be precise if this is a reflective allocation.)3468// It's what we cast the result to.3469const TypeKlassPtr* tklass = _gvn.type(klass_node)->isa_klassptr();3470if (!tklass) tklass = TypeKlassPtr::OBJECT;3471const TypeOopPtr* oop_type = tklass->as_instance_type();34723473// Now generate allocation code34743475// The entire memory state is needed for slow path of the allocation3476// since GC and deoptimization can happened.3477Node *mem = reset_memory();3478set_all_memory(mem); // Create new memory state34793480AllocateNode* alloc3481= new (C) AllocateNode(C, AllocateNode::alloc_type(Type::TOP),3482control(), mem, i_o(),3483size, klass_node,3484initial_slow_test);34853486return set_output_for_allocation(alloc, oop_type, deoptimize_on_exception);3487}34883489//-------------------------------new_array-------------------------------------3490// helper for both newarray and anewarray3491// The 'length' parameter is (obviously) the length of the array.3492// See comments on new_instance for the meaning of the other arguments.3493Node* GraphKit::new_array(Node* klass_node, // array klass (maybe variable)3494Node* length, // number of array elements3495int nargs, // number of arguments to push back for uncommon trap3496Node* *return_size_val,3497bool deoptimize_on_exception) {3498jint layout_con = Klass::_lh_neutral_value;3499Node* layout_val = get_layout_helper(klass_node, layout_con);3500int layout_is_con = (layout_val == NULL);35013502if (!layout_is_con && !StressReflectiveCode &&3503!too_many_traps(Deoptimization::Reason_class_check)) {3504// This is a reflective array creation site.3505// Optimistically assume that it is a subtype of Object[],3506// so that we can fold up all the address arithmetic.3507layout_con = Klass::array_layout_helper(T_OBJECT);3508Node* cmp_lh = _gvn.transform( new(C) CmpINode(layout_val, intcon(layout_con)) );3509Node* bol_lh = _gvn.transform( new(C) BoolNode(cmp_lh, BoolTest::eq) );3510{ BuildCutout unless(this, bol_lh, PROB_MAX);3511inc_sp(nargs);3512uncommon_trap(Deoptimization::Reason_class_check,3513Deoptimization::Action_maybe_recompile);3514}3515layout_val = NULL;3516layout_is_con = true;3517}35183519// Generate the initial go-slow test. Make sure we do not overflow3520// if length is huge (near 2Gig) or negative! We do not need3521// exact double-words here, just a close approximation of needed3522// double-words. We can't add any offset or rounding bits, lest we3523// take a size -1 of bytes and make it positive. Use an unsigned3524// compare, so negative sizes look hugely positive.3525int fast_size_limit = FastAllocateSizeLimit;3526if (layout_is_con) {3527assert(!StressReflectiveCode, "stress mode does not use these paths");3528// Increase the size limit if we have exact knowledge of array type.3529int log2_esize = Klass::layout_helper_log2_element_size(layout_con);3530fast_size_limit <<= (LogBytesPerLong - log2_esize);3531}35323533Node* initial_slow_cmp = _gvn.transform( new (C) CmpUNode( length, intcon( fast_size_limit ) ) );3534Node* initial_slow_test = _gvn.transform( new (C) BoolNode( initial_slow_cmp, BoolTest::gt ) );35353536// --- Size Computation ---3537// array_size = round_to_heap(array_header + (length << elem_shift));3538// where round_to_heap(x) == round_to(x, MinObjAlignmentInBytes)3539// and round_to(x, y) == ((x + y-1) & ~(y-1))3540// The rounding mask is strength-reduced, if possible.3541int round_mask = MinObjAlignmentInBytes - 1;3542Node* header_size = NULL;3543int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);3544// (T_BYTE has the weakest alignment and size restrictions...)3545if (layout_is_con) {3546int hsize = Klass::layout_helper_header_size(layout_con);3547int eshift = Klass::layout_helper_log2_element_size(layout_con);3548BasicType etype = Klass::layout_helper_element_type(layout_con);3549if ((round_mask & ~right_n_bits(eshift)) == 0)3550round_mask = 0; // strength-reduce it if it goes away completely3551assert((hsize & right_n_bits(eshift)) == 0, "hsize is pre-rounded");3552assert(header_size_min <= hsize, "generic minimum is smallest");3553header_size_min = hsize;3554header_size = intcon(hsize + round_mask);3555} else {3556Node* hss = intcon(Klass::_lh_header_size_shift);3557Node* hsm = intcon(Klass::_lh_header_size_mask);3558Node* hsize = _gvn.transform( new(C) URShiftINode(layout_val, hss) );3559hsize = _gvn.transform( new(C) AndINode(hsize, hsm) );3560Node* mask = intcon(round_mask);3561header_size = _gvn.transform( new(C) AddINode(hsize, mask) );3562}35633564Node* elem_shift = NULL;3565if (layout_is_con) {3566int eshift = Klass::layout_helper_log2_element_size(layout_con);3567if (eshift != 0)3568elem_shift = intcon(eshift);3569} else {3570// There is no need to mask or shift this value.3571// The semantics of LShiftINode include an implicit mask to 0x1F.3572assert(Klass::_lh_log2_element_size_shift == 0, "use shift in place");3573elem_shift = layout_val;3574}35753576// Transition to native address size for all offset calculations:3577Node* lengthx = ConvI2X(length);3578Node* headerx = ConvI2X(header_size);3579#ifdef _LP643580{ const TypeInt* tilen = _gvn.find_int_type(length);3581if (tilen != NULL && tilen->_lo < 0) {3582// Add a manual constraint to a positive range. Cf. array_element_address.3583jlong size_max = fast_size_limit;3584if (size_max > tilen->_hi) size_max = tilen->_hi;3585const TypeInt* tlcon = TypeInt::make(0, size_max, Type::WidenMin);35863587// Only do a narrow I2L conversion if the range check passed.3588IfNode* iff = new (C) IfNode(control(), initial_slow_test, PROB_MIN, COUNT_UNKNOWN);3589_gvn.transform(iff);3590RegionNode* region = new (C) RegionNode(3);3591_gvn.set_type(region, Type::CONTROL);3592lengthx = new (C) PhiNode(region, TypeLong::LONG);3593_gvn.set_type(lengthx, TypeLong::LONG);35943595// Range check passed. Use ConvI2L node with narrow type.3596Node* passed = IfFalse(iff);3597region->init_req(1, passed);3598// Make I2L conversion control dependent to prevent it from3599// floating above the range check during loop optimizations.3600lengthx->init_req(1, C->constrained_convI2L(&_gvn, length, tlcon, passed));36013602// Range check failed. Use ConvI2L with wide type because length may be invalid.3603region->init_req(2, IfTrue(iff));3604lengthx->init_req(2, ConvI2X(length));36053606set_control(region);3607record_for_igvn(region);3608record_for_igvn(lengthx);3609}3610}3611#endif36123613// Combine header size (plus rounding) and body size. Then round down.3614// This computation cannot overflow, because it is used only in two3615// places, one where the length is sharply limited, and the other3616// after a successful allocation.3617Node* abody = lengthx;3618if (elem_shift != NULL)3619abody = _gvn.transform( new(C) LShiftXNode(lengthx, elem_shift) );3620Node* size = _gvn.transform( new(C) AddXNode(headerx, abody) );3621if (round_mask != 0) {3622Node* mask = MakeConX(~round_mask);3623size = _gvn.transform( new(C) AndXNode(size, mask) );3624}3625// else if round_mask == 0, the size computation is self-rounding36263627if (return_size_val != NULL) {3628// This is the size3629(*return_size_val) = size;3630}36313632// Now generate allocation code36333634// The entire memory state is needed for slow path of the allocation3635// since GC and deoptimization can happened.3636Node *mem = reset_memory();3637set_all_memory(mem); // Create new memory state36383639if (initial_slow_test->is_Bool()) {3640// Hide it behind a CMoveI, or else PhaseIdealLoop::split_up will get sick.3641initial_slow_test = initial_slow_test->as_Bool()->as_int_value(&_gvn);3642}36433644// Create the AllocateArrayNode and its result projections3645AllocateArrayNode* alloc3646= new (C) AllocateArrayNode(C, AllocateArrayNode::alloc_type(TypeInt::INT),3647control(), mem, i_o(),3648size, klass_node,3649initial_slow_test,3650length);36513652// Cast to correct type. Note that the klass_node may be constant or not,3653// and in the latter case the actual array type will be inexact also.3654// (This happens via a non-constant argument to inline_native_newArray.)3655// In any case, the value of klass_node provides the desired array type.3656const TypeInt* length_type = _gvn.find_int_type(length);3657const TypeOopPtr* ary_type = _gvn.type(klass_node)->is_klassptr()->as_instance_type();3658if (ary_type->isa_aryptr() && length_type != NULL) {3659// Try to get a better type than POS for the size3660ary_type = ary_type->is_aryptr()->cast_to_size(length_type);3661}36623663Node* javaoop = set_output_for_allocation(alloc, ary_type, deoptimize_on_exception);36643665// Cast length on remaining path to be as narrow as possible3666if (map()->find_edge(length) >= 0) {3667Node* ccast = alloc->make_ideal_length(ary_type, &_gvn);3668if (ccast != length) {3669_gvn.set_type_bottom(ccast);3670record_for_igvn(ccast);3671replace_in_map(length, ccast);3672}3673}36743675return javaoop;3676}36773678// The following "Ideal_foo" functions are placed here because they recognize3679// the graph shapes created by the functions immediately above.36803681//---------------------------Ideal_allocation----------------------------------3682// Given an oop pointer or raw pointer, see if it feeds from an AllocateNode.3683AllocateNode* AllocateNode::Ideal_allocation(Node* ptr, PhaseTransform* phase) {3684if (ptr == NULL) { // reduce dumb test in callers3685return NULL;3686}36873688#if INCLUDE_ALL_GCS3689if (UseShenandoahGC) {3690ptr = ShenandoahBarrierSetC2::bsc2()->step_over_gc_barrier(ptr);3691}3692#endif3693if (ptr->is_CheckCastPP()) { // strip only one raw-to-oop cast3694ptr = ptr->in(1);3695if (ptr == NULL) return NULL;3696}3697// Return NULL for allocations with several casts:3698// j.l.reflect.Array.newInstance(jobject, jint)3699// Object.clone()3700// to keep more precise type from last cast.3701if (ptr->is_Proj()) {3702Node* allo = ptr->in(0);3703if (allo != NULL && allo->is_Allocate()) {3704return allo->as_Allocate();3705}3706}3707// Report failure to match.3708return NULL;3709}37103711// Fancy version which also strips off an offset (and reports it to caller).3712AllocateNode* AllocateNode::Ideal_allocation(Node* ptr, PhaseTransform* phase,3713intptr_t& offset) {3714Node* base = AddPNode::Ideal_base_and_offset(ptr, phase, offset);3715if (base == NULL) return NULL;3716return Ideal_allocation(base, phase);3717}37183719// Trace Initialize <- Proj[Parm] <- Allocate3720AllocateNode* InitializeNode::allocation() {3721Node* rawoop = in(InitializeNode::RawAddress);3722if (rawoop->is_Proj()) {3723Node* alloc = rawoop->in(0);3724if (alloc->is_Allocate()) {3725return alloc->as_Allocate();3726}3727}3728return NULL;3729}37303731// Trace Allocate -> Proj[Parm] -> Initialize3732InitializeNode* AllocateNode::initialization() {3733ProjNode* rawoop = proj_out(AllocateNode::RawAddress);3734if (rawoop == NULL) return NULL;3735for (DUIterator_Fast imax, i = rawoop->fast_outs(imax); i < imax; i++) {3736Node* init = rawoop->fast_out(i);3737if (init->is_Initialize()) {3738assert(init->as_Initialize()->allocation() == this, "2-way link");3739return init->as_Initialize();3740}3741}3742return NULL;3743}37443745//----------------------------- loop predicates ---------------------------37463747//------------------------------add_predicate_impl----------------------------3748void GraphKit::add_predicate_impl(Deoptimization::DeoptReason reason, int nargs) {3749// Too many traps seen?3750if (too_many_traps(reason)) {3751#ifdef ASSERT3752if (TraceLoopPredicate) {3753int tc = C->trap_count(reason);3754tty->print("too many traps=%s tcount=%d in ",3755Deoptimization::trap_reason_name(reason), tc);3756method()->print(); // which method has too many predicate traps3757tty->cr();3758}3759#endif3760// We cannot afford to take more traps here,3761// do not generate predicate.3762return;3763}37643765Node *cont = _gvn.intcon(1);3766Node* opq = _gvn.transform(new (C) Opaque1Node(C, cont));3767Node *bol = _gvn.transform(new (C) Conv2BNode(opq));3768IfNode* iff = create_and_map_if(control(), bol, PROB_MAX, COUNT_UNKNOWN);3769Node* iffalse = _gvn.transform(new (C) IfFalseNode(iff));3770C->add_predicate_opaq(opq);3771{3772PreserveJVMState pjvms(this);3773set_control(iffalse);3774inc_sp(nargs);3775uncommon_trap(reason, Deoptimization::Action_maybe_recompile);3776}3777Node* iftrue = _gvn.transform(new (C) IfTrueNode(iff));3778set_control(iftrue);3779}37803781//------------------------------add_predicate---------------------------------3782void GraphKit::add_predicate(int nargs) {3783if (UseLoopPredicate) {3784add_predicate_impl(Deoptimization::Reason_predicate, nargs);3785}3786// loop's limit check predicate should be near the loop.3787if (LoopLimitCheck) {3788add_predicate_impl(Deoptimization::Reason_loop_limit_check, nargs);3789}3790}37913792//----------------------------- store barriers ----------------------------3793#define __ ideal.37943795void GraphKit::sync_kit(IdealKit& ideal) {3796set_all_memory(__ merged_memory());3797set_i_o(__ i_o());3798set_control(__ ctrl());3799}38003801void GraphKit::final_sync(IdealKit& ideal) {3802// Final sync IdealKit and graphKit.3803sync_kit(ideal);3804}38053806// vanilla/CMS post barrier3807// Insert a write-barrier store. This is to let generational GC work; we have3808// to flag all oop-stores before the next GC point.3809void GraphKit::write_barrier_post(Node* oop_store,3810Node* obj,3811Node* adr,3812uint adr_idx,3813Node* val,3814bool use_precise) {3815// No store check needed if we're storing a NULL or an old object3816// (latter case is probably a string constant). The concurrent3817// mark sweep garbage collector, however, needs to have all nonNull3818// oop updates flagged via card-marks.3819if (val != NULL && val->is_Con()) {3820// must be either an oop or NULL3821const Type* t = val->bottom_type();3822if (t == TypePtr::NULL_PTR || t == Type::TOP)3823// stores of null never (?) need barriers3824return;3825}38263827if (use_ReduceInitialCardMarks()3828&& obj == just_allocated_object(control())) {3829// We can skip marks on a freshly-allocated object in Eden.3830// Keep this code in sync with new_store_pre_barrier() in runtime.cpp.3831// That routine informs GC to take appropriate compensating steps,3832// upon a slow-path allocation, so as to make this card-mark3833// elision safe.3834return;3835}38363837if (!use_precise) {3838// All card marks for a (non-array) instance are in one place:3839adr = obj;3840}3841// (Else it's an array (or unknown), and we want more precise card marks.)3842assert(adr != NULL, "");38433844IdealKit ideal(this, true);38453846// Convert the pointer to an int prior to doing math on it3847Node* cast = __ CastPX(__ ctrl(), adr);38483849// Divide by card size3850assert(Universe::heap()->barrier_set()->kind() == BarrierSet::CardTableModRef,3851"Only one we handle so far.");3852Node* card_offset = __ URShiftX( cast, __ ConI(CardTableModRefBS::card_shift) );38533854// Combine card table base and card offset3855Node* card_adr = __ AddP(__ top(), byte_map_base_node(), card_offset );38563857// Get the alias_index for raw card-mark memory3858int adr_type = Compile::AliasIdxRaw;3859Node* zero = __ ConI(0); // Dirty card value3860BasicType bt = T_BYTE;38613862if (UseCondCardMark) {3863// The classic GC reference write barrier is typically implemented3864// as a store into the global card mark table. Unfortunately3865// unconditional stores can result in false sharing and excessive3866// coherence traffic as well as false transactional aborts.3867// UseCondCardMark enables MP "polite" conditional card mark3868// stores. In theory we could relax the load from ctrl() to3869// no_ctrl, but that doesn't buy much latitude.3870Node* card_val = __ load( __ ctrl(), card_adr, TypeInt::BYTE, bt, adr_type);3871__ if_then(card_val, BoolTest::ne, zero);3872}38733874// Smash zero into card3875if( !UseConcMarkSweepGC ) {3876#if defined(AARCH64)3877__ store(__ ctrl(), card_adr, zero, bt, adr_type, MemNode::unordered);3878#else3879__ store(__ ctrl(), card_adr, zero, bt, adr_type, MemNode::release);3880#endif3881} else {3882// Specialized path for CM store barrier3883__ storeCM(__ ctrl(), card_adr, zero, oop_store, adr_idx, bt, adr_type);3884}38853886if (UseCondCardMark) {3887__ end_if();3888}38893890// Final sync IdealKit and GraphKit.3891final_sync(ideal);3892}38933894// G1 pre/post barriers3895void GraphKit::g1_write_barrier_pre(bool do_load,3896Node* obj,3897Node* adr,3898uint alias_idx,3899Node* val,3900const TypeOopPtr* val_type,3901Node* pre_val,3902BasicType bt) {39033904// Some sanity checks3905// Note: val is unused in this routine.39063907if (do_load) {3908// We need to generate the load of the previous value3909assert(obj != NULL, "must have a base");3910assert(adr != NULL, "where are loading from?");3911assert(pre_val == NULL, "loaded already?");3912assert(val_type != NULL, "need a type");3913} else {3914// In this case both val_type and alias_idx are unused.3915assert(pre_val != NULL, "must be loaded already");3916// Nothing to be done if pre_val is null.3917if (pre_val->bottom_type() == TypePtr::NULL_PTR) return;3918assert(pre_val->bottom_type()->basic_type() == T_OBJECT, "or we shouldn't be here");3919}3920assert(bt == T_OBJECT, "or we shouldn't be here");39213922IdealKit ideal(this, true);39233924Node* tls = __ thread(); // ThreadLocalStorage39253926Node* no_ctrl = NULL;3927Node* no_base = __ top();3928Node* zero = __ ConI(0);3929Node* zeroX = __ ConX(0);39303931float likely = PROB_LIKELY(0.999);3932float unlikely = PROB_UNLIKELY(0.999);39333934BasicType active_type = in_bytes(PtrQueue::byte_width_of_active()) == 4 ? T_INT : T_BYTE;3935assert(in_bytes(PtrQueue::byte_width_of_active()) == 4 || in_bytes(PtrQueue::byte_width_of_active()) == 1, "flag width");39363937// Offsets into the thread3938const int marking_offset = in_bytes(JavaThread::satb_mark_queue_offset() + // 6483939PtrQueue::byte_offset_of_active());3940const int index_offset = in_bytes(JavaThread::satb_mark_queue_offset() + // 6563941PtrQueue::byte_offset_of_index());3942const int buffer_offset = in_bytes(JavaThread::satb_mark_queue_offset() + // 6523943PtrQueue::byte_offset_of_buf());39443945// Now the actual pointers into the thread3946Node* marking_adr = __ AddP(no_base, tls, __ ConX(marking_offset));3947Node* buffer_adr = __ AddP(no_base, tls, __ ConX(buffer_offset));3948Node* index_adr = __ AddP(no_base, tls, __ ConX(index_offset));39493950// Now some of the values3951Node* marking;3952if (UseShenandoahGC) {3953Node* gc_state = __ AddP(no_base, tls, __ ConX(in_bytes(JavaThread::gc_state_offset())));3954Node* ld = __ load(__ ctrl(), gc_state, TypeInt::BYTE, T_BYTE, Compile::AliasIdxRaw);3955marking = __ AndI(ld, __ ConI(ShenandoahHeap::MARKING));3956assert(ShenandoahBarrierC2Support::is_gc_state_load(ld), "Should match the shape");3957} else {3958assert(UseG1GC, "should be");3959marking = __ load(__ ctrl(), marking_adr, TypeInt::INT, active_type, Compile::AliasIdxRaw);3960}39613962// if (!marking)3963__ if_then(marking, BoolTest::ne, zero, unlikely); {3964BasicType index_bt = TypeX_X->basic_type();3965assert(sizeof(size_t) == type2aelembytes(index_bt), "Loading G1 PtrQueue::_index with wrong size.");3966Node* index = __ load(__ ctrl(), index_adr, TypeX_X, index_bt, Compile::AliasIdxRaw);39673968if (do_load) {3969// load original value3970// alias_idx correct??3971pre_val = __ load(__ ctrl(), adr, val_type, bt, alias_idx);3972}39733974// if (pre_val != NULL)3975__ if_then(pre_val, BoolTest::ne, null()); {3976Node* buffer = __ load(__ ctrl(), buffer_adr, TypeRawPtr::NOTNULL, T_ADDRESS, Compile::AliasIdxRaw);39773978// is the queue for this thread full?3979__ if_then(index, BoolTest::ne, zeroX, likely); {39803981// decrement the index3982Node* next_index = _gvn.transform(new (C) SubXNode(index, __ ConX(sizeof(intptr_t))));39833984// Now get the buffer location we will log the previous value into and store it3985Node *log_addr = __ AddP(no_base, buffer, next_index);3986__ store(__ ctrl(), log_addr, pre_val, T_OBJECT, Compile::AliasIdxRaw, MemNode::unordered);3987// update the index3988__ store(__ ctrl(), index_adr, next_index, index_bt, Compile::AliasIdxRaw, MemNode::unordered);39893990} __ else_(); {39913992// logging buffer is full, call the runtime3993const TypeFunc *tf = OptoRuntime::g1_wb_pre_Type();3994__ make_leaf_call(tf, CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_pre), "g1_wb_pre", pre_val, tls);3995} __ end_if(); // (!index)3996} __ end_if(); // (pre_val != NULL)3997} __ end_if(); // (!marking)39983999// Final sync IdealKit and GraphKit.4000final_sync(ideal);40014002#if INCLUDE_ALL_GCS4003if (UseShenandoahGC && adr != NULL) {4004Node* c = control();4005Node* call = c->in(1)->in(1)->in(1)->in(0);4006assert(call->is_g1_wb_pre_call(), "g1_wb_pre call expected");4007call->add_req(adr);4008}4009#endif4010}40114012//4013// Update the card table and add card address to the queue4014//4015void GraphKit::g1_mark_card(IdealKit& ideal,4016Node* card_adr,4017Node* oop_store,4018uint oop_alias_idx,4019Node* index,4020Node* index_adr,4021Node* buffer,4022const TypeFunc* tf) {40234024Node* zero = __ ConI(0);4025Node* zeroX = __ ConX(0);4026Node* no_base = __ top();4027BasicType card_bt = T_BYTE;4028// Smash zero into card. MUST BE ORDERED WRT TO STORE4029__ storeCM(__ ctrl(), card_adr, zero, oop_store, oop_alias_idx, card_bt, Compile::AliasIdxRaw);40304031// Now do the queue work4032__ if_then(index, BoolTest::ne, zeroX); {40334034Node* next_index = _gvn.transform(new (C) SubXNode(index, __ ConX(sizeof(intptr_t))));4035Node* log_addr = __ AddP(no_base, buffer, next_index);40364037// Order, see storeCM.4038__ store(__ ctrl(), log_addr, card_adr, T_ADDRESS, Compile::AliasIdxRaw, MemNode::unordered);4039__ store(__ ctrl(), index_adr, next_index, TypeX_X->basic_type(), Compile::AliasIdxRaw, MemNode::unordered);40404041} __ else_(); {4042__ make_leaf_call(tf, CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_post), "g1_wb_post", card_adr, __ thread());4043} __ end_if();40444045}40464047void GraphKit::g1_write_barrier_post(Node* oop_store,4048Node* obj,4049Node* adr,4050uint alias_idx,4051Node* val,4052BasicType bt,4053bool use_precise) {4054// If we are writing a NULL then we need no post barrier40554056if (val != NULL && val->is_Con() && val->bottom_type() == TypePtr::NULL_PTR) {4057// Must be NULL4058const Type* t = val->bottom_type();4059assert(t == Type::TOP || t == TypePtr::NULL_PTR, "must be NULL");4060// No post barrier if writing NULLx4061return;4062}40634064if (!use_precise) {4065// All card marks for a (non-array) instance are in one place:4066adr = obj;4067}4068// (Else it's an array (or unknown), and we want more precise card marks.)4069assert(adr != NULL, "");40704071IdealKit ideal(this, true);40724073Node* tls = __ thread(); // ThreadLocalStorage40744075Node* no_base = __ top();4076float likely = PROB_LIKELY(0.999);4077float unlikely = PROB_UNLIKELY(0.999);4078Node* young_card = __ ConI((jint)G1SATBCardTableModRefBS::g1_young_card_val());4079Node* dirty_card = __ ConI((jint)CardTableModRefBS::dirty_card_val());4080Node* zeroX = __ ConX(0);40814082// Get the alias_index for raw card-mark memory4083const TypePtr* card_type = TypeRawPtr::BOTTOM;40844085const TypeFunc *tf = OptoRuntime::g1_wb_post_Type();40864087// Offsets into the thread4088const int index_offset = in_bytes(JavaThread::dirty_card_queue_offset() +4089PtrQueue::byte_offset_of_index());4090const int buffer_offset = in_bytes(JavaThread::dirty_card_queue_offset() +4091PtrQueue::byte_offset_of_buf());40924093// Pointers into the thread40944095Node* buffer_adr = __ AddP(no_base, tls, __ ConX(buffer_offset));4096Node* index_adr = __ AddP(no_base, tls, __ ConX(index_offset));40974098// Now some values4099// Use ctrl to avoid hoisting these values past a safepoint, which could4100// potentially reset these fields in the JavaThread.4101Node* index = __ load(__ ctrl(), index_adr, TypeX_X, TypeX_X->basic_type(), Compile::AliasIdxRaw);4102Node* buffer = __ load(__ ctrl(), buffer_adr, TypeRawPtr::NOTNULL, T_ADDRESS, Compile::AliasIdxRaw);41034104// Convert the store obj pointer to an int prior to doing math on it4105// Must use ctrl to prevent "integerized oop" existing across safepoint4106Node* cast = __ CastPX(__ ctrl(), adr);41074108// Divide pointer by card size4109Node* card_offset = __ URShiftX( cast, __ ConI(CardTableModRefBS::card_shift) );41104111// Combine card table base and card offset4112Node* card_adr = __ AddP(no_base, byte_map_base_node(), card_offset );41134114// If we know the value being stored does it cross regions?41154116if (val != NULL) {4117// Does the store cause us to cross regions?41184119// Should be able to do an unsigned compare of region_size instead of4120// and extra shift. Do we have an unsigned compare??4121// Node* region_size = __ ConI(1 << HeapRegion::LogOfHRGrainBytes);4122Node* xor_res = __ URShiftX ( __ XorX( cast, __ CastPX(__ ctrl(), val)), __ ConI(HeapRegion::LogOfHRGrainBytes));41234124// if (xor_res == 0) same region so skip4125__ if_then(xor_res, BoolTest::ne, zeroX); {41264127// No barrier if we are storing a NULL4128__ if_then(val, BoolTest::ne, null(), unlikely); {41294130// Ok must mark the card if not already dirty41314132// load the original value of the card4133Node* card_val = __ load(__ ctrl(), card_adr, TypeInt::INT, T_BYTE, Compile::AliasIdxRaw);41344135__ if_then(card_val, BoolTest::ne, young_card); {4136sync_kit(ideal);4137// Use Op_MemBarVolatile to achieve the effect of a StoreLoad barrier.4138insert_mem_bar(Op_MemBarVolatile, oop_store);4139__ sync_kit(this);41404141Node* card_val_reload = __ load(__ ctrl(), card_adr, TypeInt::INT, T_BYTE, Compile::AliasIdxRaw);4142__ if_then(card_val_reload, BoolTest::ne, dirty_card); {4143g1_mark_card(ideal, card_adr, oop_store, alias_idx, index, index_adr, buffer, tf);4144} __ end_if();4145} __ end_if();4146} __ end_if();4147} __ end_if();4148} else {4149// Object.clone() instrinsic uses this path.4150g1_mark_card(ideal, card_adr, oop_store, alias_idx, index, index_adr, buffer, tf);4151}41524153// Final sync IdealKit and GraphKit.4154final_sync(ideal);4155}4156#undef __4157415841594160Node* GraphKit::load_String_offset(Node* ctrl, Node* str) {4161if (java_lang_String::has_offset_field()) {4162int offset_offset = java_lang_String::offset_offset_in_bytes();4163const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),4164false, NULL, 0);4165const TypePtr* offset_field_type = string_type->add_offset(offset_offset);4166int offset_field_idx = C->get_alias_index(offset_field_type);4167return make_load(ctrl,4168basic_plus_adr(str, str, offset_offset),4169TypeInt::INT, T_INT, offset_field_idx, MemNode::unordered);4170} else {4171return intcon(0);4172}4173}41744175Node* GraphKit::load_String_length(Node* ctrl, Node* str) {4176if (java_lang_String::has_count_field()) {4177int count_offset = java_lang_String::count_offset_in_bytes();4178const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),4179false, NULL, 0);4180const TypePtr* count_field_type = string_type->add_offset(count_offset);4181int count_field_idx = C->get_alias_index(count_field_type);4182return make_load(ctrl,4183basic_plus_adr(str, str, count_offset),4184TypeInt::INT, T_INT, count_field_idx, MemNode::unordered);4185} else {4186return load_array_length(load_String_value(ctrl, str));4187}4188}41894190Node* GraphKit::load_String_value(Node* ctrl, Node* str) {4191int value_offset = java_lang_String::value_offset_in_bytes();4192const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),4193false, NULL, 0);4194const TypePtr* value_field_type = string_type->add_offset(value_offset);4195const TypeAryPtr* value_type = TypeAryPtr::make(TypePtr::NotNull,4196TypeAry::make(TypeInt::CHAR,TypeInt::POS),4197ciTypeArrayKlass::make(T_CHAR), true, 0);4198int value_field_idx = C->get_alias_index(value_field_type);4199Node* load = make_load(ctrl, basic_plus_adr(str, str, value_offset),4200value_type, T_OBJECT, value_field_idx, MemNode::unordered);4201#if INCLUDE_ALL_GCS4202if (UseShenandoahGC) {4203load = ShenandoahBarrierSetC2::bsc2()->load_reference_barrier(this, load);4204}4205#endif4206// String.value field is known to be @Stable.4207if (UseImplicitStableValues) {4208load = cast_array_to_stable(load, value_type);4209}4210return load;4211}42124213void GraphKit::store_String_offset(Node* ctrl, Node* str, Node* value) {4214int offset_offset = java_lang_String::offset_offset_in_bytes();4215const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),4216false, NULL, 0);4217const TypePtr* offset_field_type = string_type->add_offset(offset_offset);4218int offset_field_idx = C->get_alias_index(offset_field_type);4219store_to_memory(ctrl, basic_plus_adr(str, offset_offset),4220value, T_INT, offset_field_idx, MemNode::unordered);4221}42224223void GraphKit::store_String_value(Node* ctrl, Node* str, Node* value) {4224int value_offset = java_lang_String::value_offset_in_bytes();4225const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),4226false, NULL, 0);4227const TypePtr* value_field_type = string_type->add_offset(value_offset);42284229store_oop_to_object(ctrl, str, basic_plus_adr(str, value_offset), value_field_type,4230value, TypeAryPtr::CHARS, T_OBJECT, MemNode::unordered);4231}42324233void GraphKit::store_String_length(Node* ctrl, Node* str, Node* value) {4234int count_offset = java_lang_String::count_offset_in_bytes();4235const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),4236false, NULL, 0);4237const TypePtr* count_field_type = string_type->add_offset(count_offset);4238int count_field_idx = C->get_alias_index(count_field_type);4239store_to_memory(ctrl, basic_plus_adr(str, count_offset),4240value, T_INT, count_field_idx, MemNode::unordered);4241}42424243Node* GraphKit::cast_array_to_stable(Node* ary, const TypeAryPtr* ary_type) {4244// Reify the property as a CastPP node in Ideal graph to comply with monotonicity4245// assumption of CCP analysis.4246return _gvn.transform(new(C) CastPPNode(ary, ary_type->cast_to_stable(true)));4247}424842494250