Path: blob/master/src/hotspot/share/opto/compile.cpp
64440 views
/*1* Copyright (c) 1997, 2021, 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 "jvm_io.h"26#include "asm/macroAssembler.hpp"27#include "asm/macroAssembler.inline.hpp"28#include "ci/ciReplay.hpp"29#include "classfile/javaClasses.hpp"30#include "code/exceptionHandlerTable.hpp"31#include "code/nmethod.hpp"32#include "compiler/compileBroker.hpp"33#include "compiler/compileLog.hpp"34#include "compiler/disassembler.hpp"35#include "compiler/oopMap.hpp"36#include "gc/shared/barrierSet.hpp"37#include "gc/shared/c2/barrierSetC2.hpp"38#include "jfr/jfrEvents.hpp"39#include "memory/resourceArea.hpp"40#include "opto/addnode.hpp"41#include "opto/block.hpp"42#include "opto/c2compiler.hpp"43#include "opto/callGenerator.hpp"44#include "opto/callnode.hpp"45#include "opto/castnode.hpp"46#include "opto/cfgnode.hpp"47#include "opto/chaitin.hpp"48#include "opto/compile.hpp"49#include "opto/connode.hpp"50#include "opto/convertnode.hpp"51#include "opto/divnode.hpp"52#include "opto/escape.hpp"53#include "opto/idealGraphPrinter.hpp"54#include "opto/loopnode.hpp"55#include "opto/machnode.hpp"56#include "opto/macro.hpp"57#include "opto/matcher.hpp"58#include "opto/mathexactnode.hpp"59#include "opto/memnode.hpp"60#include "opto/mulnode.hpp"61#include "opto/narrowptrnode.hpp"62#include "opto/node.hpp"63#include "opto/opcodes.hpp"64#include "opto/output.hpp"65#include "opto/parse.hpp"66#include "opto/phaseX.hpp"67#include "opto/rootnode.hpp"68#include "opto/runtime.hpp"69#include "opto/stringopts.hpp"70#include "opto/type.hpp"71#include "opto/vector.hpp"72#include "opto/vectornode.hpp"73#include "runtime/globals_extension.hpp"74#include "runtime/sharedRuntime.hpp"75#include "runtime/signature.hpp"76#include "runtime/stubRoutines.hpp"77#include "runtime/timer.hpp"78#include "utilities/align.hpp"79#include "utilities/copy.hpp"80#include "utilities/macros.hpp"81#include "utilities/resourceHash.hpp"828384// -------------------- Compile::mach_constant_base_node -----------------------85// Constant table base node singleton.86MachConstantBaseNode* Compile::mach_constant_base_node() {87if (_mach_constant_base_node == NULL) {88_mach_constant_base_node = new MachConstantBaseNode();89_mach_constant_base_node->add_req(C->root());90}91return _mach_constant_base_node;92}939495/// Support for intrinsics.9697// Return the index at which m must be inserted (or already exists).98// The sort order is by the address of the ciMethod, with is_virtual as minor key.99class IntrinsicDescPair {100private:101ciMethod* _m;102bool _is_virtual;103public:104IntrinsicDescPair(ciMethod* m, bool is_virtual) : _m(m), _is_virtual(is_virtual) {}105static int compare(IntrinsicDescPair* const& key, CallGenerator* const& elt) {106ciMethod* m= elt->method();107ciMethod* key_m = key->_m;108if (key_m < m) return -1;109else if (key_m > m) return 1;110else {111bool is_virtual = elt->is_virtual();112bool key_virtual = key->_is_virtual;113if (key_virtual < is_virtual) return -1;114else if (key_virtual > is_virtual) return 1;115else return 0;116}117}118};119int Compile::intrinsic_insertion_index(ciMethod* m, bool is_virtual, bool& found) {120#ifdef ASSERT121for (int i = 1; i < _intrinsics.length(); i++) {122CallGenerator* cg1 = _intrinsics.at(i-1);123CallGenerator* cg2 = _intrinsics.at(i);124assert(cg1->method() != cg2->method()125? cg1->method() < cg2->method()126: cg1->is_virtual() < cg2->is_virtual(),127"compiler intrinsics list must stay sorted");128}129#endif130IntrinsicDescPair pair(m, is_virtual);131return _intrinsics.find_sorted<IntrinsicDescPair*, IntrinsicDescPair::compare>(&pair, found);132}133134void Compile::register_intrinsic(CallGenerator* cg) {135bool found = false;136int index = intrinsic_insertion_index(cg->method(), cg->is_virtual(), found);137assert(!found, "registering twice");138_intrinsics.insert_before(index, cg);139assert(find_intrinsic(cg->method(), cg->is_virtual()) == cg, "registration worked");140}141142CallGenerator* Compile::find_intrinsic(ciMethod* m, bool is_virtual) {143assert(m->is_loaded(), "don't try this on unloaded methods");144if (_intrinsics.length() > 0) {145bool found = false;146int index = intrinsic_insertion_index(m, is_virtual, found);147if (found) {148return _intrinsics.at(index);149}150}151// Lazily create intrinsics for intrinsic IDs well-known in the runtime.152if (m->intrinsic_id() != vmIntrinsics::_none &&153m->intrinsic_id() <= vmIntrinsics::LAST_COMPILER_INLINE) {154CallGenerator* cg = make_vm_intrinsic(m, is_virtual);155if (cg != NULL) {156// Save it for next time:157register_intrinsic(cg);158return cg;159} else {160gather_intrinsic_statistics(m->intrinsic_id(), is_virtual, _intrinsic_disabled);161}162}163return NULL;164}165166// Compile::make_vm_intrinsic is defined in library_call.cpp.167168#ifndef PRODUCT169// statistics gathering...170171juint Compile::_intrinsic_hist_count[vmIntrinsics::number_of_intrinsics()] = {0};172jubyte Compile::_intrinsic_hist_flags[vmIntrinsics::number_of_intrinsics()] = {0};173174inline int as_int(vmIntrinsics::ID id) {175return vmIntrinsics::as_int(id);176}177178bool Compile::gather_intrinsic_statistics(vmIntrinsics::ID id, bool is_virtual, int flags) {179assert(id > vmIntrinsics::_none && id < vmIntrinsics::ID_LIMIT, "oob");180int oflags = _intrinsic_hist_flags[as_int(id)];181assert(flags != 0, "what happened?");182if (is_virtual) {183flags |= _intrinsic_virtual;184}185bool changed = (flags != oflags);186if ((flags & _intrinsic_worked) != 0) {187juint count = (_intrinsic_hist_count[as_int(id)] += 1);188if (count == 1) {189changed = true; // first time190}191// increment the overall count also:192_intrinsic_hist_count[as_int(vmIntrinsics::_none)] += 1;193}194if (changed) {195if (((oflags ^ flags) & _intrinsic_virtual) != 0) {196// Something changed about the intrinsic's virtuality.197if ((flags & _intrinsic_virtual) != 0) {198// This is the first use of this intrinsic as a virtual call.199if (oflags != 0) {200// We already saw it as a non-virtual, so note both cases.201flags |= _intrinsic_both;202}203} else if ((oflags & _intrinsic_both) == 0) {204// This is the first use of this intrinsic as a non-virtual205flags |= _intrinsic_both;206}207}208_intrinsic_hist_flags[as_int(id)] = (jubyte) (oflags | flags);209}210// update the overall flags also:211_intrinsic_hist_flags[as_int(vmIntrinsics::_none)] |= (jubyte) flags;212return changed;213}214215static char* format_flags(int flags, char* buf) {216buf[0] = 0;217if ((flags & Compile::_intrinsic_worked) != 0) strcat(buf, ",worked");218if ((flags & Compile::_intrinsic_failed) != 0) strcat(buf, ",failed");219if ((flags & Compile::_intrinsic_disabled) != 0) strcat(buf, ",disabled");220if ((flags & Compile::_intrinsic_virtual) != 0) strcat(buf, ",virtual");221if ((flags & Compile::_intrinsic_both) != 0) strcat(buf, ",nonvirtual");222if (buf[0] == 0) strcat(buf, ",");223assert(buf[0] == ',', "must be");224return &buf[1];225}226227void Compile::print_intrinsic_statistics() {228char flagsbuf[100];229ttyLocker ttyl;230if (xtty != NULL) xtty->head("statistics type='intrinsic'");231tty->print_cr("Compiler intrinsic usage:");232juint total = _intrinsic_hist_count[as_int(vmIntrinsics::_none)];233if (total == 0) total = 1; // avoid div0 in case of no successes234#define PRINT_STAT_LINE(name, c, f) \235tty->print_cr(" %4d (%4.1f%%) %s (%s)", (int)(c), ((c) * 100.0) / total, name, f);236for (auto id : EnumRange<vmIntrinsicID>{}) {237int flags = _intrinsic_hist_flags[as_int(id)];238juint count = _intrinsic_hist_count[as_int(id)];239if ((flags | count) != 0) {240PRINT_STAT_LINE(vmIntrinsics::name_at(id), count, format_flags(flags, flagsbuf));241}242}243PRINT_STAT_LINE("total", total, format_flags(_intrinsic_hist_flags[as_int(vmIntrinsics::_none)], flagsbuf));244if (xtty != NULL) xtty->tail("statistics");245}246247void Compile::print_statistics() {248{ ttyLocker ttyl;249if (xtty != NULL) xtty->head("statistics type='opto'");250Parse::print_statistics();251PhaseCCP::print_statistics();252PhaseRegAlloc::print_statistics();253PhaseOutput::print_statistics();254PhasePeephole::print_statistics();255PhaseIdealLoop::print_statistics();256if (xtty != NULL) xtty->tail("statistics");257}258if (_intrinsic_hist_flags[as_int(vmIntrinsics::_none)] != 0) {259// put this under its own <statistics> element.260print_intrinsic_statistics();261}262}263#endif //PRODUCT264265void Compile::gvn_replace_by(Node* n, Node* nn) {266for (DUIterator_Last imin, i = n->last_outs(imin); i >= imin; ) {267Node* use = n->last_out(i);268bool is_in_table = initial_gvn()->hash_delete(use);269uint uses_found = 0;270for (uint j = 0; j < use->len(); j++) {271if (use->in(j) == n) {272if (j < use->req())273use->set_req(j, nn);274else275use->set_prec(j, nn);276uses_found++;277}278}279if (is_in_table) {280// reinsert into table281initial_gvn()->hash_find_insert(use);282}283record_for_igvn(use);284i -= uses_found; // we deleted 1 or more copies of this edge285}286}287288289// Identify all nodes that are reachable from below, useful.290// Use breadth-first pass that records state in a Unique_Node_List,291// recursive traversal is slower.292void Compile::identify_useful_nodes(Unique_Node_List &useful) {293int estimated_worklist_size = live_nodes();294useful.map( estimated_worklist_size, NULL ); // preallocate space295296// Initialize worklist297if (root() != NULL) { useful.push(root()); }298// If 'top' is cached, declare it useful to preserve cached node299if( cached_top_node() ) { useful.push(cached_top_node()); }300301// Push all useful nodes onto the list, breadthfirst302for( uint next = 0; next < useful.size(); ++next ) {303assert( next < unique(), "Unique useful nodes < total nodes");304Node *n = useful.at(next);305uint max = n->len();306for( uint i = 0; i < max; ++i ) {307Node *m = n->in(i);308if (not_a_node(m)) continue;309useful.push(m);310}311}312}313314// Update dead_node_list with any missing dead nodes using useful315// list. Consider all non-useful nodes to be useless i.e., dead nodes.316void Compile::update_dead_node_list(Unique_Node_List &useful) {317uint max_idx = unique();318VectorSet& useful_node_set = useful.member_set();319320for (uint node_idx = 0; node_idx < max_idx; node_idx++) {321// If node with index node_idx is not in useful set,322// mark it as dead in dead node list.323if (!useful_node_set.test(node_idx)) {324record_dead_node(node_idx);325}326}327}328329void Compile::remove_useless_late_inlines(GrowableArray<CallGenerator*>* inlines, Unique_Node_List &useful) {330int shift = 0;331for (int i = 0; i < inlines->length(); i++) {332CallGenerator* cg = inlines->at(i);333if (useful.member(cg->call_node())) {334if (shift > 0) {335inlines->at_put(i - shift, cg);336}337} else {338shift++; // skip over the dead element339}340}341if (shift > 0) {342inlines->trunc_to(inlines->length() - shift); // remove last elements from compacted array343}344}345346void Compile::remove_useless_late_inlines(GrowableArray<CallGenerator*>* inlines, Node* dead) {347assert(dead != NULL && dead->is_Call(), "sanity");348int found = 0;349for (int i = 0; i < inlines->length(); i++) {350if (inlines->at(i)->call_node() == dead) {351inlines->remove_at(i);352found++;353NOT_DEBUG( break; ) // elements are unique, so exit early354}355}356assert(found <= 1, "not unique");357}358359void Compile::remove_useless_nodes(GrowableArray<Node*>& node_list, Unique_Node_List& useful) {360for (int i = node_list.length() - 1; i >= 0; i--) {361Node* n = node_list.at(i);362if (!useful.member(n)) {363node_list.delete_at(i); // replaces i-th with last element which is known to be useful (already processed)364}365}366}367368void Compile::remove_useless_node(Node* dead) {369remove_modified_node(dead);370371// Constant node that has no out-edges and has only one in-edge from372// root is usually dead. However, sometimes reshaping walk makes373// it reachable by adding use edges. So, we will NOT count Con nodes374// as dead to be conservative about the dead node count at any375// given time.376if (!dead->is_Con()) {377record_dead_node(dead->_idx);378}379if (dead->is_macro()) {380remove_macro_node(dead);381}382if (dead->is_expensive()) {383remove_expensive_node(dead);384}385if (dead->Opcode() == Op_Opaque4) {386remove_skeleton_predicate_opaq(dead);387}388if (dead->for_post_loop_opts_igvn()) {389remove_from_post_loop_opts_igvn(dead);390}391if (dead->is_Call()) {392remove_useless_late_inlines( &_late_inlines, dead);393remove_useless_late_inlines( &_string_late_inlines, dead);394remove_useless_late_inlines( &_boxing_late_inlines, dead);395remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);396}397BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();398bs->unregister_potential_barrier_node(dead);399}400401// Disconnect all useless nodes by disconnecting those at the boundary.402void Compile::remove_useless_nodes(Unique_Node_List &useful) {403uint next = 0;404while (next < useful.size()) {405Node *n = useful.at(next++);406if (n->is_SafePoint()) {407// We're done with a parsing phase. Replaced nodes are not valid408// beyond that point.409n->as_SafePoint()->delete_replaced_nodes();410}411// Use raw traversal of out edges since this code removes out edges412int max = n->outcnt();413for (int j = 0; j < max; ++j) {414Node* child = n->raw_out(j);415if (!useful.member(child)) {416assert(!child->is_top() || child != top(),417"If top is cached in Compile object it is in useful list");418// Only need to remove this out-edge to the useless node419n->raw_del_out(j);420--j;421--max;422}423}424if (n->outcnt() == 1 && n->has_special_unique_user()) {425record_for_igvn(n->unique_out());426}427}428429remove_useless_nodes(_macro_nodes, useful); // remove useless macro nodes430remove_useless_nodes(_predicate_opaqs, useful); // remove useless predicate opaque nodes431remove_useless_nodes(_skeleton_predicate_opaqs, useful);432remove_useless_nodes(_expensive_nodes, useful); // remove useless expensive nodes433remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass434remove_useless_coarsened_locks(useful); // remove useless coarsened locks nodes435436BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();437bs->eliminate_useless_gc_barriers(useful, this);438// clean up the late inline lists439remove_useless_late_inlines( &_late_inlines, useful);440remove_useless_late_inlines( &_string_late_inlines, useful);441remove_useless_late_inlines( &_boxing_late_inlines, useful);442remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);443debug_only(verify_graph_edges(true/*check for no_dead_code*/);)444}445446// ============================================================================447//------------------------------CompileWrapper---------------------------------448class CompileWrapper : public StackObj {449Compile *const _compile;450public:451CompileWrapper(Compile* compile);452453~CompileWrapper();454};455456CompileWrapper::CompileWrapper(Compile* compile) : _compile(compile) {457// the Compile* pointer is stored in the current ciEnv:458ciEnv* env = compile->env();459assert(env == ciEnv::current(), "must already be a ciEnv active");460assert(env->compiler_data() == NULL, "compile already active?");461env->set_compiler_data(compile);462assert(compile == Compile::current(), "sanity");463464compile->set_type_dict(NULL);465compile->set_clone_map(new Dict(cmpkey, hashkey, _compile->comp_arena()));466compile->clone_map().set_clone_idx(0);467compile->set_type_last_size(0);468compile->set_last_tf(NULL, NULL);469compile->set_indexSet_arena(NULL);470compile->set_indexSet_free_block_list(NULL);471compile->init_type_arena();472Type::Initialize(compile);473_compile->begin_method();474_compile->clone_map().set_debug(_compile->has_method() && _compile->directive()->CloneMapDebugOption);475}476CompileWrapper::~CompileWrapper() {477_compile->end_method();478_compile->env()->set_compiler_data(NULL);479}480481482//----------------------------print_compile_messages---------------------------483void Compile::print_compile_messages() {484#ifndef PRODUCT485// Check if recompiling486if (_subsume_loads == false && PrintOpto) {487// Recompiling without allowing machine instructions to subsume loads488tty->print_cr("*********************************************************");489tty->print_cr("** Bailout: Recompile without subsuming loads **");490tty->print_cr("*********************************************************");491}492if (_do_escape_analysis != DoEscapeAnalysis && PrintOpto) {493// Recompiling without escape analysis494tty->print_cr("*********************************************************");495tty->print_cr("** Bailout: Recompile without escape analysis **");496tty->print_cr("*********************************************************");497}498if (_eliminate_boxing != EliminateAutoBox && PrintOpto) {499// Recompiling without boxing elimination500tty->print_cr("*********************************************************");501tty->print_cr("** Bailout: Recompile without boxing elimination **");502tty->print_cr("*********************************************************");503}504if ((_do_locks_coarsening != EliminateLocks) && PrintOpto) {505// Recompiling without locks coarsening506tty->print_cr("*********************************************************");507tty->print_cr("** Bailout: Recompile without locks coarsening **");508tty->print_cr("*********************************************************");509}510if (env()->break_at_compile()) {511// Open the debugger when compiling this method.512tty->print("### Breaking when compiling: ");513method()->print_short_name();514tty->cr();515BREAKPOINT;516}517518if( PrintOpto ) {519if (is_osr_compilation()) {520tty->print("[OSR]%3d", _compile_id);521} else {522tty->print("%3d", _compile_id);523}524}525#endif526}527528// ============================================================================529//------------------------------Compile standard-------------------------------530debug_only( int Compile::_debug_idx = 100000; )531532// Compile a method. entry_bci is -1 for normal compilations and indicates533// the continuation bci for on stack replacement.534535536Compile::Compile( ciEnv* ci_env, ciMethod* target, int osr_bci,537bool subsume_loads, bool do_escape_analysis, bool eliminate_boxing,538bool do_locks_coarsening, bool install_code, DirectiveSet* directive)539: Phase(Compiler),540_compile_id(ci_env->compile_id()),541_subsume_loads(subsume_loads),542_do_escape_analysis(do_escape_analysis),543_install_code(install_code),544_eliminate_boxing(eliminate_boxing),545_do_locks_coarsening(do_locks_coarsening),546_method(target),547_entry_bci(osr_bci),548_stub_function(NULL),549_stub_name(NULL),550_stub_entry_point(NULL),551_max_node_limit(MaxNodeLimit),552_post_loop_opts_phase(false),553_inlining_progress(false),554_inlining_incrementally(false),555_do_cleanup(false),556_has_reserved_stack_access(target->has_reserved_stack_access()),557#ifndef PRODUCT558_igv_idx(0),559_trace_opto_output(directive->TraceOptoOutputOption),560_print_ideal(directive->PrintIdealOption),561#endif562_has_method_handle_invokes(false),563_clinit_barrier_on_entry(false),564_stress_seed(0),565_comp_arena(mtCompiler),566_barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),567_env(ci_env),568_directive(directive),569_log(ci_env->log()),570_failure_reason(NULL),571_intrinsics (comp_arena(), 0, 0, NULL),572_macro_nodes (comp_arena(), 8, 0, NULL),573_predicate_opaqs (comp_arena(), 8, 0, NULL),574_skeleton_predicate_opaqs (comp_arena(), 8, 0, NULL),575_expensive_nodes (comp_arena(), 8, 0, NULL),576_for_post_loop_igvn(comp_arena(), 8, 0, NULL),577_coarsened_locks (comp_arena(), 8, 0, NULL),578_congraph(NULL),579NOT_PRODUCT(_printer(NULL) COMMA)580_dead_node_list(comp_arena()),581_dead_node_count(0),582_node_arena(mtCompiler),583_old_arena(mtCompiler),584_mach_constant_base_node(NULL),585_Compile_types(mtCompiler),586_initial_gvn(NULL),587_for_igvn(NULL),588_late_inlines(comp_arena(), 2, 0, NULL),589_string_late_inlines(comp_arena(), 2, 0, NULL),590_boxing_late_inlines(comp_arena(), 2, 0, NULL),591_vector_reboxing_late_inlines(comp_arena(), 2, 0, NULL),592_late_inlines_pos(0),593_number_of_mh_late_inlines(0),594_native_invokers(comp_arena(), 1, 0, NULL),595_print_inlining_stream(NULL),596_print_inlining_list(NULL),597_print_inlining_idx(0),598_print_inlining_output(NULL),599_replay_inline_data(NULL),600_java_calls(0),601_inner_loops(0),602_interpreter_frame_size(0)603#ifndef PRODUCT604, _in_dump_cnt(0)605#endif606{607C = this;608CompileWrapper cw(this);609610if (CITimeVerbose) {611tty->print(" ");612target->holder()->name()->print();613tty->print(".");614target->print_short_name();615tty->print(" ");616}617TraceTime t1("Total compilation time", &_t_totalCompilation, CITime, CITimeVerbose);618TraceTime t2(NULL, &_t_methodCompilation, CITime, false);619620#if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)621bool print_opto_assembly = directive->PrintOptoAssemblyOption;622// We can always print a disassembly, either abstract (hex dump) or623// with the help of a suitable hsdis library. Thus, we should not624// couple print_assembly and print_opto_assembly controls.625// But: always print opto and regular assembly on compile command 'print'.626bool print_assembly = directive->PrintAssemblyOption;627set_print_assembly(print_opto_assembly || print_assembly);628#else629set_print_assembly(false); // must initialize.630#endif631632#ifndef PRODUCT633set_parsed_irreducible_loop(false);634635if (directive->ReplayInlineOption) {636_replay_inline_data = ciReplay::load_inline_data(method(), entry_bci(), ci_env->comp_level());637}638#endif639set_print_inlining(directive->PrintInliningOption || PrintOptoInlining);640set_print_intrinsics(directive->PrintIntrinsicsOption);641set_has_irreducible_loop(true); // conservative until build_loop_tree() reset it642643if (ProfileTraps RTM_OPT_ONLY( || UseRTMLocking )) {644// Make sure the method being compiled gets its own MDO,645// so we can at least track the decompile_count().646// Need MDO to record RTM code generation state.647method()->ensure_method_data();648}649650Init(::AliasLevel);651652653print_compile_messages();654655_ilt = InlineTree::build_inline_tree_root();656657// Even if NO memory addresses are used, MergeMem nodes must have at least 1 slice658assert(num_alias_types() >= AliasIdxRaw, "");659660#define MINIMUM_NODE_HASH 1023661// Node list that Iterative GVN will start with662Unique_Node_List for_igvn(comp_arena());663set_for_igvn(&for_igvn);664665// GVN that will be run immediately on new nodes666uint estimated_size = method()->code_size()*4+64;667estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);668PhaseGVN gvn(node_arena(), estimated_size);669set_initial_gvn(&gvn);670671print_inlining_init();672{ // Scope for timing the parser673TracePhase tp("parse", &timers[_t_parser]);674675// Put top into the hash table ASAP.676initial_gvn()->transform_no_reclaim(top());677678// Set up tf(), start(), and find a CallGenerator.679CallGenerator* cg = NULL;680if (is_osr_compilation()) {681const TypeTuple *domain = StartOSRNode::osr_domain();682const TypeTuple *range = TypeTuple::make_range(method()->signature());683init_tf(TypeFunc::make(domain, range));684StartNode* s = new StartOSRNode(root(), domain);685initial_gvn()->set_type_bottom(s);686init_start(s);687cg = CallGenerator::for_osr(method(), entry_bci());688} else {689// Normal case.690init_tf(TypeFunc::make(method()));691StartNode* s = new StartNode(root(), tf()->domain());692initial_gvn()->set_type_bottom(s);693init_start(s);694if (method()->intrinsic_id() == vmIntrinsics::_Reference_get) {695// With java.lang.ref.reference.get() we must go through the696// intrinsic - even when get() is the root697// method of the compile - so that, if necessary, the value in698// the referent field of the reference object gets recorded by699// the pre-barrier code.700cg = find_intrinsic(method(), false);701}702if (cg == NULL) {703float past_uses = method()->interpreter_invocation_count();704float expected_uses = past_uses;705cg = CallGenerator::for_inline(method(), expected_uses);706}707}708if (failing()) return;709if (cg == NULL) {710record_method_not_compilable("cannot parse method");711return;712}713JVMState* jvms = build_start_state(start(), tf());714if ((jvms = cg->generate(jvms)) == NULL) {715if (!failure_reason_is(C2Compiler::retry_class_loading_during_parsing())) {716record_method_not_compilable("method parse failed");717}718return;719}720GraphKit kit(jvms);721722if (!kit.stopped()) {723// Accept return values, and transfer control we know not where.724// This is done by a special, unique ReturnNode bound to root.725return_values(kit.jvms());726}727728if (kit.has_exceptions()) {729// Any exceptions that escape from this call must be rethrown730// to whatever caller is dynamically above us on the stack.731// This is done by a special, unique RethrowNode bound to root.732rethrow_exceptions(kit.transfer_exceptions_into_jvms());733}734735assert(IncrementalInline || (_late_inlines.length() == 0 && !has_mh_late_inlines()), "incremental inlining is off");736737if (_late_inlines.length() == 0 && !has_mh_late_inlines() && !failing() && has_stringbuilder()) {738inline_string_calls(true);739}740741if (failing()) return;742743print_method(PHASE_BEFORE_REMOVEUSELESS, 3);744745// Remove clutter produced by parsing.746if (!failing()) {747ResourceMark rm;748PhaseRemoveUseless pru(initial_gvn(), &for_igvn);749}750}751752// Note: Large methods are capped off in do_one_bytecode().753if (failing()) return;754755// After parsing, node notes are no longer automagic.756// They must be propagated by register_new_node_with_optimizer(),757// clone(), or the like.758set_default_node_notes(NULL);759760#ifndef PRODUCT761if (should_print(1)) {762_printer->print_inlining();763}764#endif765766if (failing()) return;767NOT_PRODUCT( verify_graph_edges(); )768769// If any phase is randomized for stress testing, seed random number770// generation and log the seed for repeatability.771if (StressLCM || StressGCM || StressIGVN || StressCCP) {772if (FLAG_IS_DEFAULT(StressSeed) || (FLAG_IS_ERGO(StressSeed) && RepeatCompilation)) {773_stress_seed = static_cast<uint>(Ticks::now().nanoseconds());774FLAG_SET_ERGO(StressSeed, _stress_seed);775} else {776_stress_seed = StressSeed;777}778if (_log != NULL) {779_log->elem("stress_test seed='%u'", _stress_seed);780}781}782783// Now optimize784Optimize();785if (failing()) return;786NOT_PRODUCT( verify_graph_edges(); )787788#ifndef PRODUCT789if (print_ideal()) {790ttyLocker ttyl; // keep the following output all in one block791// This output goes directly to the tty, not the compiler log.792// To enable tools to match it up with the compilation activity,793// be sure to tag this tty output with the compile ID.794if (xtty != NULL) {795xtty->head("ideal compile_id='%d'%s", compile_id(),796is_osr_compilation() ? " compile_kind='osr'" :797"");798}799root()->dump(9999);800if (xtty != NULL) {801xtty->tail("ideal");802}803}804#endif805806#ifdef ASSERT807BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();808bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);809#endif810811// Dump compilation data to replay it.812if (directive->DumpReplayOption) {813env()->dump_replay_data(_compile_id);814}815if (directive->DumpInlineOption && (ilt() != NULL)) {816env()->dump_inline_data(_compile_id);817}818819// Now that we know the size of all the monitors we can add a fixed slot820// for the original deopt pc.821int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);822set_fixed_slots(next_slot);823824// Compute when to use implicit null checks. Used by matching trap based825// nodes and NullCheck optimization.826set_allowed_deopt_reasons();827828// Now generate code829Code_Gen();830}831832//------------------------------Compile----------------------------------------833// Compile a runtime stub834Compile::Compile( ciEnv* ci_env,835TypeFunc_generator generator,836address stub_function,837const char *stub_name,838int is_fancy_jump,839bool pass_tls,840bool return_pc,841DirectiveSet* directive)842: Phase(Compiler),843_compile_id(0),844_subsume_loads(true),845_do_escape_analysis(false),846_install_code(true),847_eliminate_boxing(false),848_do_locks_coarsening(false),849_method(NULL),850_entry_bci(InvocationEntryBci),851_stub_function(stub_function),852_stub_name(stub_name),853_stub_entry_point(NULL),854_max_node_limit(MaxNodeLimit),855_post_loop_opts_phase(false),856_inlining_progress(false),857_inlining_incrementally(false),858_has_reserved_stack_access(false),859#ifndef PRODUCT860_igv_idx(0),861_trace_opto_output(directive->TraceOptoOutputOption),862_print_ideal(directive->PrintIdealOption),863#endif864_has_method_handle_invokes(false),865_clinit_barrier_on_entry(false),866_stress_seed(0),867_comp_arena(mtCompiler),868_barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),869_env(ci_env),870_directive(directive),871_log(ci_env->log()),872_failure_reason(NULL),873_congraph(NULL),874NOT_PRODUCT(_printer(NULL) COMMA)875_dead_node_list(comp_arena()),876_dead_node_count(0),877_node_arena(mtCompiler),878_old_arena(mtCompiler),879_mach_constant_base_node(NULL),880_Compile_types(mtCompiler),881_initial_gvn(NULL),882_for_igvn(NULL),883_number_of_mh_late_inlines(0),884_native_invokers(),885_print_inlining_stream(NULL),886_print_inlining_list(NULL),887_print_inlining_idx(0),888_print_inlining_output(NULL),889_replay_inline_data(NULL),890_java_calls(0),891_inner_loops(0),892_interpreter_frame_size(0),893#ifndef PRODUCT894_in_dump_cnt(0),895#endif896_allowed_reasons(0) {897C = this;898899TraceTime t1(NULL, &_t_totalCompilation, CITime, false);900TraceTime t2(NULL, &_t_stubCompilation, CITime, false);901902#ifndef PRODUCT903set_print_assembly(PrintFrameConverterAssembly);904set_parsed_irreducible_loop(false);905#else906set_print_assembly(false); // Must initialize.907#endif908set_has_irreducible_loop(false); // no loops909910CompileWrapper cw(this);911Init(/*AliasLevel=*/ 0);912init_tf((*generator)());913914{915// The following is a dummy for the sake of GraphKit::gen_stub916Unique_Node_List for_igvn(comp_arena());917set_for_igvn(&for_igvn); // not used, but some GraphKit guys push on this918PhaseGVN gvn(Thread::current()->resource_area(),255);919set_initial_gvn(&gvn); // not significant, but GraphKit guys use it pervasively920gvn.transform_no_reclaim(top());921922GraphKit kit;923kit.gen_stub(stub_function, stub_name, is_fancy_jump, pass_tls, return_pc);924}925926NOT_PRODUCT( verify_graph_edges(); )927928Code_Gen();929}930931//------------------------------Init-------------------------------------------932// Prepare for a single compilation933void Compile::Init(int aliaslevel) {934_unique = 0;935_regalloc = NULL;936937_tf = NULL; // filled in later938_top = NULL; // cached later939_matcher = NULL; // filled in later940_cfg = NULL; // filled in later941942IA32_ONLY( set_24_bit_selection_and_mode(true, false); )943944_node_note_array = NULL;945_default_node_notes = NULL;946DEBUG_ONLY( _modified_nodes = NULL; ) // Used in Optimize()947948_immutable_memory = NULL; // filled in at first inquiry949950// Globally visible Nodes951// First set TOP to NULL to give safe behavior during creation of RootNode952set_cached_top_node(NULL);953set_root(new RootNode());954// Now that you have a Root to point to, create the real TOP955set_cached_top_node( new ConNode(Type::TOP) );956set_recent_alloc(NULL, NULL);957958// Create Debug Information Recorder to record scopes, oopmaps, etc.959env()->set_oop_recorder(new OopRecorder(env()->arena()));960env()->set_debug_info(new DebugInformationRecorder(env()->oop_recorder()));961env()->set_dependencies(new Dependencies(env()));962963_fixed_slots = 0;964set_has_split_ifs(false);965set_has_loops(false); // first approximation966set_has_stringbuilder(false);967set_has_boxed_value(false);968_trap_can_recompile = false; // no traps emitted yet969_major_progress = true; // start out assuming good things will happen970set_has_unsafe_access(false);971set_max_vector_size(0);972set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers973Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));974set_decompile_count(0);975976set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);977_loop_opts_cnt = LoopOptsCount;978set_do_inlining(Inline);979set_max_inline_size(MaxInlineSize);980set_freq_inline_size(FreqInlineSize);981set_do_scheduling(OptoScheduling);982983set_do_vector_loop(false);984985if (AllowVectorizeOnDemand) {986if (has_method() && (_directive->VectorizeOption || _directive->VectorizeDebugOption)) {987set_do_vector_loop(true);988NOT_PRODUCT(if (do_vector_loop() && Verbose) {tty->print("Compile::Init: do vectorized loops (SIMD like) for method %s\n", method()->name()->as_quoted_ascii());})989} else if (has_method() && method()->name() != 0 &&990method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {991set_do_vector_loop(true);992}993}994set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally995NOT_PRODUCT(if (use_cmove() && Verbose && has_method()) {tty->print("Compile::Init: use CMove without profitability tests for method %s\n", method()->name()->as_quoted_ascii());})996997set_age_code(has_method() && method()->profile_aging());998set_rtm_state(NoRTM); // No RTM lock eliding by default999_max_node_limit = _directive->MaxNodeLimitOption;10001001#if INCLUDE_RTM_OPT1002if (UseRTMLocking && has_method() && (method()->method_data_or_null() != NULL)) {1003int rtm_state = method()->method_data()->rtm_state();1004if (method_has_option(CompileCommand::NoRTMLockEliding) || ((rtm_state & NoRTM) != 0)) {1005// Don't generate RTM lock eliding code.1006set_rtm_state(NoRTM);1007} else if (method_has_option(CompileCommand::UseRTMLockEliding) || ((rtm_state & UseRTM) != 0) || !UseRTMDeopt) {1008// Generate RTM lock eliding code without abort ratio calculation code.1009set_rtm_state(UseRTM);1010} else if (UseRTMDeopt) {1011// Generate RTM lock eliding code and include abort ratio calculation1012// code if UseRTMDeopt is on.1013set_rtm_state(ProfileRTM);1014}1015}1016#endif1017if (VM_Version::supports_fast_class_init_checks() && has_method() && !is_osr_compilation() && method()->needs_clinit_barrier()) {1018set_clinit_barrier_on_entry(true);1019}1020if (debug_info()->recording_non_safepoints()) {1021set_node_note_array(new(comp_arena()) GrowableArray<Node_Notes*>1022(comp_arena(), 8, 0, NULL));1023set_default_node_notes(Node_Notes::make(this));1024}10251026// // -- Initialize types before each compile --1027// // Update cached type information1028// if( _method && _method->constants() )1029// Type::update_loaded_types(_method, _method->constants());10301031// Init alias_type map.1032if (!_do_escape_analysis && aliaslevel == 3)1033aliaslevel = 2; // No unique types without escape analysis1034_AliasLevel = aliaslevel;1035const int grow_ats = 16;1036_max_alias_types = grow_ats;1037_alias_types = NEW_ARENA_ARRAY(comp_arena(), AliasType*, grow_ats);1038AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, grow_ats);1039Copy::zero_to_bytes(ats, sizeof(AliasType)*grow_ats);1040{1041for (int i = 0; i < grow_ats; i++) _alias_types[i] = &ats[i];1042}1043// Initialize the first few types.1044_alias_types[AliasIdxTop]->Init(AliasIdxTop, NULL);1045_alias_types[AliasIdxBot]->Init(AliasIdxBot, TypePtr::BOTTOM);1046_alias_types[AliasIdxRaw]->Init(AliasIdxRaw, TypeRawPtr::BOTTOM);1047_num_alias_types = AliasIdxRaw+1;1048// Zero out the alias type cache.1049Copy::zero_to_bytes(_alias_cache, sizeof(_alias_cache));1050// A NULL adr_type hits in the cache right away. Preload the right answer.1051probe_alias_cache(NULL)->_index = AliasIdxTop;10521053#ifdef ASSERT1054_type_verify_symmetry = true;1055_phase_optimize_finished = false;1056_exception_backedge = false;1057#endif1058}10591060//---------------------------init_start----------------------------------------1061// Install the StartNode on this compile object.1062void Compile::init_start(StartNode* s) {1063if (failing())1064return; // already failing1065assert(s == start(), "");1066}10671068/**1069* Return the 'StartNode'. We must not have a pending failure, since the ideal graph1070* can be in an inconsistent state, i.e., we can get segmentation faults when traversing1071* the ideal graph.1072*/1073StartNode* Compile::start() const {1074assert (!failing(), "Must not have pending failure. Reason is: %s", failure_reason());1075for (DUIterator_Fast imax, i = root()->fast_outs(imax); i < imax; i++) {1076Node* start = root()->fast_out(i);1077if (start->is_Start()) {1078return start->as_Start();1079}1080}1081fatal("Did not find Start node!");1082return NULL;1083}10841085//-------------------------------immutable_memory-------------------------------------1086// Access immutable memory1087Node* Compile::immutable_memory() {1088if (_immutable_memory != NULL) {1089return _immutable_memory;1090}1091StartNode* s = start();1092for (DUIterator_Fast imax, i = s->fast_outs(imax); true; i++) {1093Node *p = s->fast_out(i);1094if (p != s && p->as_Proj()->_con == TypeFunc::Memory) {1095_immutable_memory = p;1096return _immutable_memory;1097}1098}1099ShouldNotReachHere();1100return NULL;1101}11021103//----------------------set_cached_top_node------------------------------------1104// Install the cached top node, and make sure Node::is_top works correctly.1105void Compile::set_cached_top_node(Node* tn) {1106if (tn != NULL) verify_top(tn);1107Node* old_top = _top;1108_top = tn;1109// Calling Node::setup_is_top allows the nodes the chance to adjust1110// their _out arrays.1111if (_top != NULL) _top->setup_is_top();1112if (old_top != NULL) old_top->setup_is_top();1113assert(_top == NULL || top()->is_top(), "");1114}11151116#ifdef ASSERT1117uint Compile::count_live_nodes_by_graph_walk() {1118Unique_Node_List useful(comp_arena());1119// Get useful node list by walking the graph.1120identify_useful_nodes(useful);1121return useful.size();1122}11231124void Compile::print_missing_nodes() {11251126// Return if CompileLog is NULL and PrintIdealNodeCount is false.1127if ((_log == NULL) && (! PrintIdealNodeCount)) {1128return;1129}11301131// This is an expensive function. It is executed only when the user1132// specifies VerifyIdealNodeCount option or otherwise knows the1133// additional work that needs to be done to identify reachable nodes1134// by walking the flow graph and find the missing ones using1135// _dead_node_list.11361137Unique_Node_List useful(comp_arena());1138// Get useful node list by walking the graph.1139identify_useful_nodes(useful);11401141uint l_nodes = C->live_nodes();1142uint l_nodes_by_walk = useful.size();11431144if (l_nodes != l_nodes_by_walk) {1145if (_log != NULL) {1146_log->begin_head("mismatched_nodes count='%d'", abs((int) (l_nodes - l_nodes_by_walk)));1147_log->stamp();1148_log->end_head();1149}1150VectorSet& useful_member_set = useful.member_set();1151int last_idx = l_nodes_by_walk;1152for (int i = 0; i < last_idx; i++) {1153if (useful_member_set.test(i)) {1154if (_dead_node_list.test(i)) {1155if (_log != NULL) {1156_log->elem("mismatched_node_info node_idx='%d' type='both live and dead'", i);1157}1158if (PrintIdealNodeCount) {1159// Print the log message to tty1160tty->print_cr("mismatched_node idx='%d' both live and dead'", i);1161useful.at(i)->dump();1162}1163}1164}1165else if (! _dead_node_list.test(i)) {1166if (_log != NULL) {1167_log->elem("mismatched_node_info node_idx='%d' type='neither live nor dead'", i);1168}1169if (PrintIdealNodeCount) {1170// Print the log message to tty1171tty->print_cr("mismatched_node idx='%d' type='neither live nor dead'", i);1172}1173}1174}1175if (_log != NULL) {1176_log->tail("mismatched_nodes");1177}1178}1179}1180void Compile::record_modified_node(Node* n) {1181if (_modified_nodes != NULL && !_inlining_incrementally && !n->is_Con()) {1182_modified_nodes->push(n);1183}1184}11851186void Compile::remove_modified_node(Node* n) {1187if (_modified_nodes != NULL) {1188_modified_nodes->remove(n);1189}1190}1191#endif11921193#ifndef PRODUCT1194void Compile::verify_top(Node* tn) const {1195if (tn != NULL) {1196assert(tn->is_Con(), "top node must be a constant");1197assert(((ConNode*)tn)->type() == Type::TOP, "top node must have correct type");1198assert(tn->in(0) != NULL, "must have live top node");1199}1200}1201#endif120212031204///-------------------Managing Per-Node Debug & Profile Info-------------------12051206void Compile::grow_node_notes(GrowableArray<Node_Notes*>* arr, int grow_by) {1207guarantee(arr != NULL, "");1208int num_blocks = arr->length();1209if (grow_by < num_blocks) grow_by = num_blocks;1210int num_notes = grow_by * _node_notes_block_size;1211Node_Notes* notes = NEW_ARENA_ARRAY(node_arena(), Node_Notes, num_notes);1212Copy::zero_to_bytes(notes, num_notes * sizeof(Node_Notes));1213while (num_notes > 0) {1214arr->append(notes);1215notes += _node_notes_block_size;1216num_notes -= _node_notes_block_size;1217}1218assert(num_notes == 0, "exact multiple, please");1219}12201221bool Compile::copy_node_notes_to(Node* dest, Node* source) {1222if (source == NULL || dest == NULL) return false;12231224if (dest->is_Con())1225return false; // Do not push debug info onto constants.12261227#ifdef ASSERT1228// Leave a bread crumb trail pointing to the original node:1229if (dest != NULL && dest != source && dest->debug_orig() == NULL) {1230dest->set_debug_orig(source);1231}1232#endif12331234if (node_note_array() == NULL)1235return false; // Not collecting any notes now.12361237// This is a copy onto a pre-existing node, which may already have notes.1238// If both nodes have notes, do not overwrite any pre-existing notes.1239Node_Notes* source_notes = node_notes_at(source->_idx);1240if (source_notes == NULL || source_notes->is_clear()) return false;1241Node_Notes* dest_notes = node_notes_at(dest->_idx);1242if (dest_notes == NULL || dest_notes->is_clear()) {1243return set_node_notes_at(dest->_idx, source_notes);1244}12451246Node_Notes merged_notes = (*source_notes);1247// The order of operations here ensures that dest notes will win...1248merged_notes.update_from(dest_notes);1249return set_node_notes_at(dest->_idx, &merged_notes);1250}125112521253//--------------------------allow_range_check_smearing-------------------------1254// Gating condition for coalescing similar range checks.1255// Sometimes we try 'speculatively' replacing a series of a range checks by a1256// single covering check that is at least as strong as any of them.1257// If the optimization succeeds, the simplified (strengthened) range check1258// will always succeed. If it fails, we will deopt, and then give up1259// on the optimization.1260bool Compile::allow_range_check_smearing() const {1261// If this method has already thrown a range-check,1262// assume it was because we already tried range smearing1263// and it failed.1264uint already_trapped = trap_count(Deoptimization::Reason_range_check);1265return !already_trapped;1266}126712681269//------------------------------flatten_alias_type-----------------------------1270const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {1271int offset = tj->offset();1272TypePtr::PTR ptr = tj->ptr();12731274// Known instance (scalarizable allocation) alias only with itself.1275bool is_known_inst = tj->isa_oopptr() != NULL &&1276tj->is_oopptr()->is_known_instance();12771278// Process weird unsafe references.1279if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {1280assert(InlineUnsafeOps, "indeterminate pointers come only from unsafe ops");1281assert(!is_known_inst, "scalarizable allocation should not have unsafe references");1282tj = TypeOopPtr::BOTTOM;1283ptr = tj->ptr();1284offset = tj->offset();1285}12861287// Array pointers need some flattening1288const TypeAryPtr *ta = tj->isa_aryptr();1289if (ta && ta->is_stable()) {1290// Erase stability property for alias analysis.1291tj = ta = ta->cast_to_stable(false);1292}1293if( ta && is_known_inst ) {1294if ( offset != Type::OffsetBot &&1295offset > arrayOopDesc::length_offset_in_bytes() ) {1296offset = Type::OffsetBot; // Flatten constant access into array body only1297tj = ta = TypeAryPtr::make(ptr, ta->ary(), ta->klass(), true, offset, ta->instance_id());1298}1299} else if( ta && _AliasLevel >= 2 ) {1300// For arrays indexed by constant indices, we flatten the alias1301// space to include all of the array body. Only the header, klass1302// and array length can be accessed un-aliased.1303if( offset != Type::OffsetBot ) {1304if( ta->const_oop() ) { // MethodData* or Method*1305offset = Type::OffsetBot; // Flatten constant access into array body1306tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),ta->ary(),ta->klass(),false,offset);1307} else if( offset == arrayOopDesc::length_offset_in_bytes() ) {1308// range is OK as-is.1309tj = ta = TypeAryPtr::RANGE;1310} else if( offset == oopDesc::klass_offset_in_bytes() ) {1311tj = TypeInstPtr::KLASS; // all klass loads look alike1312ta = TypeAryPtr::RANGE; // generic ignored junk1313ptr = TypePtr::BotPTR;1314} else if( offset == oopDesc::mark_offset_in_bytes() ) {1315tj = TypeInstPtr::MARK;1316ta = TypeAryPtr::RANGE; // generic ignored junk1317ptr = TypePtr::BotPTR;1318} else { // Random constant offset into array body1319offset = Type::OffsetBot; // Flatten constant access into array body1320tj = ta = TypeAryPtr::make(ptr,ta->ary(),ta->klass(),false,offset);1321}1322}1323// Arrays of fixed size alias with arrays of unknown size.1324if (ta->size() != TypeInt::POS) {1325const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);1326tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,ta->klass(),false,offset);1327}1328// Arrays of known objects become arrays of unknown objects.1329if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {1330const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());1331tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,offset);1332}1333if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {1334const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());1335tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,offset);1336}1337// Arrays of bytes and of booleans both use 'bastore' and 'baload' so1338// cannot be distinguished by bytecode alone.1339if (ta->elem() == TypeInt::BOOL) {1340const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());1341ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);1342tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,offset);1343}1344// During the 2nd round of IterGVN, NotNull castings are removed.1345// Make sure the Bottom and NotNull variants alias the same.1346// Also, make sure exact and non-exact variants alias the same.1347if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != NULL) {1348tj = ta = TypeAryPtr::make(TypePtr::BotPTR,ta->ary(),ta->klass(),false,offset);1349}1350}13511352// Oop pointers need some flattening1353const TypeInstPtr *to = tj->isa_instptr();1354if( to && _AliasLevel >= 2 && to != TypeOopPtr::BOTTOM ) {1355ciInstanceKlass *k = to->klass()->as_instance_klass();1356if( ptr == TypePtr::Constant ) {1357if (to->klass() != ciEnv::current()->Class_klass() ||1358offset < k->layout_helper_size_in_bytes()) {1359// No constant oop pointers (such as Strings); they alias with1360// unknown strings.1361assert(!is_known_inst, "not scalarizable allocation");1362tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,offset);1363}1364} else if( is_known_inst ) {1365tj = to; // Keep NotNull and klass_is_exact for instance type1366} else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {1367// During the 2nd round of IterGVN, NotNull castings are removed.1368// Make sure the Bottom and NotNull variants alias the same.1369// Also, make sure exact and non-exact variants alias the same.1370tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,offset);1371}1372if (to->speculative() != NULL) {1373tj = to = TypeInstPtr::make(to->ptr(),to->klass(),to->klass_is_exact(),to->const_oop(),to->offset(), to->instance_id());1374}1375// Canonicalize the holder of this field1376if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {1377// First handle header references such as a LoadKlassNode, even if the1378// object's klass is unloaded at compile time (4965979).1379if (!is_known_inst) { // Do it only for non-instance types1380tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, NULL, offset);1381}1382} else if (offset < 0 || offset >= k->layout_helper_size_in_bytes()) {1383// Static fields are in the space above the normal instance1384// fields in the java.lang.Class instance.1385if (to->klass() != ciEnv::current()->Class_klass()) {1386to = NULL;1387tj = TypeOopPtr::BOTTOM;1388offset = tj->offset();1389}1390} else {1391ciInstanceKlass *canonical_holder = k->get_canonical_holder(offset);1392assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");1393if (!k->equals(canonical_holder) || tj->offset() != offset) {1394if( is_known_inst ) {1395tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, true, NULL, offset, to->instance_id());1396} else {1397tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, false, NULL, offset);1398}1399}1400}1401}14021403// Klass pointers to object array klasses need some flattening1404const TypeKlassPtr *tk = tj->isa_klassptr();1405if( tk ) {1406// If we are referencing a field within a Klass, we need1407// to assume the worst case of an Object. Both exact and1408// inexact types must flatten to the same alias class so1409// use NotNull as the PTR.1410if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {14111412tj = tk = TypeKlassPtr::make(TypePtr::NotNull,1413TypeKlassPtr::OBJECT->klass(),1414offset);1415}14161417ciKlass* klass = tk->klass();1418if( klass->is_obj_array_klass() ) {1419ciKlass* k = TypeAryPtr::OOPS->klass();1420if( !k || !k->is_loaded() ) // Only fails for some -Xcomp runs1421k = TypeInstPtr::BOTTOM->klass();1422tj = tk = TypeKlassPtr::make( TypePtr::NotNull, k, offset );1423}14241425// Check for precise loads from the primary supertype array and force them1426// to the supertype cache alias index. Check for generic array loads from1427// the primary supertype array and also force them to the supertype cache1428// alias index. Since the same load can reach both, we need to merge1429// these 2 disparate memories into the same alias class. Since the1430// primary supertype array is read-only, there's no chance of confusion1431// where we bypass an array load and an array store.1432int primary_supers_offset = in_bytes(Klass::primary_supers_offset());1433if (offset == Type::OffsetBot ||1434(offset >= primary_supers_offset &&1435offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||1436offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {1437offset = in_bytes(Klass::secondary_super_cache_offset());1438tj = tk = TypeKlassPtr::make( TypePtr::NotNull, tk->klass(), offset );1439}1440}14411442// Flatten all Raw pointers together.1443if (tj->base() == Type::RawPtr)1444tj = TypeRawPtr::BOTTOM;14451446if (tj->base() == Type::AnyPtr)1447tj = TypePtr::BOTTOM; // An error, which the caller must check for.14481449// Flatten all to bottom for now1450switch( _AliasLevel ) {1451case 0:1452tj = TypePtr::BOTTOM;1453break;1454case 1: // Flatten to: oop, static, field or array1455switch (tj->base()) {1456//case Type::AryPtr: tj = TypeAryPtr::RANGE; break;1457case Type::RawPtr: tj = TypeRawPtr::BOTTOM; break;1458case Type::AryPtr: // do not distinguish arrays at all1459case Type::InstPtr: tj = TypeInstPtr::BOTTOM; break;1460case Type::KlassPtr: tj = TypeKlassPtr::OBJECT; break;1461case Type::AnyPtr: tj = TypePtr::BOTTOM; break; // caller checks it1462default: ShouldNotReachHere();1463}1464break;1465case 2: // No collapsing at level 2; keep all splits1466case 3: // No collapsing at level 3; keep all splits1467break;1468default:1469Unimplemented();1470}14711472offset = tj->offset();1473assert( offset != Type::OffsetTop, "Offset has fallen from constant" );14741475assert( (offset != Type::OffsetBot && tj->base() != Type::AryPtr) ||1476(offset == Type::OffsetBot && tj->base() == Type::AryPtr) ||1477(offset == Type::OffsetBot && tj == TypeOopPtr::BOTTOM) ||1478(offset == Type::OffsetBot && tj == TypePtr::BOTTOM) ||1479(offset == oopDesc::mark_offset_in_bytes() && tj->base() == Type::AryPtr) ||1480(offset == oopDesc::klass_offset_in_bytes() && tj->base() == Type::AryPtr) ||1481(offset == arrayOopDesc::length_offset_in_bytes() && tj->base() == Type::AryPtr),1482"For oops, klasses, raw offset must be constant; for arrays the offset is never known" );1483assert( tj->ptr() != TypePtr::TopPTR &&1484tj->ptr() != TypePtr::AnyNull &&1485tj->ptr() != TypePtr::Null, "No imprecise addresses" );1486// assert( tj->ptr() != TypePtr::Constant ||1487// tj->base() == Type::RawPtr ||1488// tj->base() == Type::KlassPtr, "No constant oop addresses" );14891490return tj;1491}14921493void Compile::AliasType::Init(int i, const TypePtr* at) {1494assert(AliasIdxTop <= i && i < Compile::current()->_max_alias_types, "Invalid alias index");1495_index = i;1496_adr_type = at;1497_field = NULL;1498_element = NULL;1499_is_rewritable = true; // default1500const TypeOopPtr *atoop = (at != NULL) ? at->isa_oopptr() : NULL;1501if (atoop != NULL && atoop->is_known_instance()) {1502const TypeOopPtr *gt = atoop->cast_to_instance_id(TypeOopPtr::InstanceBot);1503_general_index = Compile::current()->get_alias_index(gt);1504} else {1505_general_index = 0;1506}1507}15081509BasicType Compile::AliasType::basic_type() const {1510if (element() != NULL) {1511const Type* element = adr_type()->is_aryptr()->elem();1512return element->isa_narrowoop() ? T_OBJECT : element->array_element_basic_type();1513} if (field() != NULL) {1514return field()->layout_type();1515} else {1516return T_ILLEGAL; // unknown1517}1518}15191520//---------------------------------print_on------------------------------------1521#ifndef PRODUCT1522void Compile::AliasType::print_on(outputStream* st) {1523if (index() < 10)1524st->print("@ <%d> ", index());1525else st->print("@ <%d>", index());1526st->print(is_rewritable() ? " " : " RO");1527int offset = adr_type()->offset();1528if (offset == Type::OffsetBot)1529st->print(" +any");1530else st->print(" +%-3d", offset);1531st->print(" in ");1532adr_type()->dump_on(st);1533const TypeOopPtr* tjp = adr_type()->isa_oopptr();1534if (field() != NULL && tjp) {1535if (tjp->klass() != field()->holder() ||1536tjp->offset() != field()->offset_in_bytes()) {1537st->print(" != ");1538field()->print();1539st->print(" ***");1540}1541}1542}15431544void print_alias_types() {1545Compile* C = Compile::current();1546tty->print_cr("--- Alias types, AliasIdxBot .. %d", C->num_alias_types()-1);1547for (int idx = Compile::AliasIdxBot; idx < C->num_alias_types(); idx++) {1548C->alias_type(idx)->print_on(tty);1549tty->cr();1550}1551}1552#endif155315541555//----------------------------probe_alias_cache--------------------------------1556Compile::AliasCacheEntry* Compile::probe_alias_cache(const TypePtr* adr_type) {1557intptr_t key = (intptr_t) adr_type;1558key ^= key >> logAliasCacheSize;1559return &_alias_cache[key & right_n_bits(logAliasCacheSize)];1560}156115621563//-----------------------------grow_alias_types--------------------------------1564void Compile::grow_alias_types() {1565const int old_ats = _max_alias_types; // how many before?1566const int new_ats = old_ats; // how many more?1567const int grow_ats = old_ats+new_ats; // how many now?1568_max_alias_types = grow_ats;1569_alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);1570AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);1571Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);1572for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];1573}157415751576//--------------------------------find_alias_type------------------------------1577Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {1578if (_AliasLevel == 0)1579return alias_type(AliasIdxBot);15801581AliasCacheEntry* ace = probe_alias_cache(adr_type);1582if (ace->_adr_type == adr_type) {1583return alias_type(ace->_index);1584}15851586// Handle special cases.1587if (adr_type == NULL) return alias_type(AliasIdxTop);1588if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);15891590// Do it the slow way.1591const TypePtr* flat = flatten_alias_type(adr_type);15921593#ifdef ASSERT1594{1595ResourceMark rm;1596assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",1597Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));1598assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",1599Type::str(adr_type));1600if (flat->isa_oopptr() && !flat->isa_klassptr()) {1601const TypeOopPtr* foop = flat->is_oopptr();1602// Scalarizable allocations have exact klass always.1603bool exact = !foop->klass_is_exact() || foop->is_known_instance();1604const TypePtr* xoop = foop->cast_to_exactness(exact)->is_ptr();1605assert(foop == flatten_alias_type(xoop), "exactness must not affect alias type: foop = %s; xoop = %s",1606Type::str(foop), Type::str(xoop));1607}1608}1609#endif16101611int idx = AliasIdxTop;1612for (int i = 0; i < num_alias_types(); i++) {1613if (alias_type(i)->adr_type() == flat) {1614idx = i;1615break;1616}1617}16181619if (idx == AliasIdxTop) {1620if (no_create) return NULL;1621// Grow the array if necessary.1622if (_num_alias_types == _max_alias_types) grow_alias_types();1623// Add a new alias type.1624idx = _num_alias_types++;1625_alias_types[idx]->Init(idx, flat);1626if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);1627if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);1628if (flat->isa_instptr()) {1629if (flat->offset() == java_lang_Class::klass_offset()1630&& flat->is_instptr()->klass() == env()->Class_klass())1631alias_type(idx)->set_rewritable(false);1632}1633if (flat->isa_aryptr()) {1634#ifdef ASSERT1635const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);1636// (T_BYTE has the weakest alignment and size restrictions...)1637assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");1638#endif1639if (flat->offset() == TypePtr::OffsetBot) {1640alias_type(idx)->set_element(flat->is_aryptr()->elem());1641}1642}1643if (flat->isa_klassptr()) {1644if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))1645alias_type(idx)->set_rewritable(false);1646if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))1647alias_type(idx)->set_rewritable(false);1648if (flat->offset() == in_bytes(Klass::access_flags_offset()))1649alias_type(idx)->set_rewritable(false);1650if (flat->offset() == in_bytes(Klass::java_mirror_offset()))1651alias_type(idx)->set_rewritable(false);1652if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))1653alias_type(idx)->set_rewritable(false);1654}1655// %%% (We would like to finalize JavaThread::threadObj_offset(),1656// but the base pointer type is not distinctive enough to identify1657// references into JavaThread.)16581659// Check for final fields.1660const TypeInstPtr* tinst = flat->isa_instptr();1661if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {1662ciField* field;1663if (tinst->const_oop() != NULL &&1664tinst->klass() == ciEnv::current()->Class_klass() &&1665tinst->offset() >= (tinst->klass()->as_instance_klass()->layout_helper_size_in_bytes())) {1666// static field1667ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();1668field = k->get_field_by_offset(tinst->offset(), true);1669} else {1670ciInstanceKlass *k = tinst->klass()->as_instance_klass();1671field = k->get_field_by_offset(tinst->offset(), false);1672}1673assert(field == NULL ||1674original_field == NULL ||1675(field->holder() == original_field->holder() &&1676field->offset() == original_field->offset() &&1677field->is_static() == original_field->is_static()), "wrong field?");1678// Set field() and is_rewritable() attributes.1679if (field != NULL) alias_type(idx)->set_field(field);1680}1681}16821683// Fill the cache for next time.1684ace->_adr_type = adr_type;1685ace->_index = idx;1686assert(alias_type(adr_type) == alias_type(idx), "type must be installed");16871688// Might as well try to fill the cache for the flattened version, too.1689AliasCacheEntry* face = probe_alias_cache(flat);1690if (face->_adr_type == NULL) {1691face->_adr_type = flat;1692face->_index = idx;1693assert(alias_type(flat) == alias_type(idx), "flat type must work too");1694}16951696return alias_type(idx);1697}169816991700Compile::AliasType* Compile::alias_type(ciField* field) {1701const TypeOopPtr* t;1702if (field->is_static())1703t = TypeInstPtr::make(field->holder()->java_mirror());1704else1705t = TypeOopPtr::make_from_klass_raw(field->holder());1706AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);1707assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");1708return atp;1709}171017111712//------------------------------have_alias_type--------------------------------1713bool Compile::have_alias_type(const TypePtr* adr_type) {1714AliasCacheEntry* ace = probe_alias_cache(adr_type);1715if (ace->_adr_type == adr_type) {1716return true;1717}17181719// Handle special cases.1720if (adr_type == NULL) return true;1721if (adr_type == TypePtr::BOTTOM) return true;17221723return find_alias_type(adr_type, true, NULL) != NULL;1724}17251726//-----------------------------must_alias--------------------------------------1727// True if all values of the given address type are in the given alias category.1728bool Compile::must_alias(const TypePtr* adr_type, int alias_idx) {1729if (alias_idx == AliasIdxBot) return true; // the universal category1730if (adr_type == NULL) return true; // NULL serves as TypePtr::TOP1731if (alias_idx == AliasIdxTop) return false; // the empty category1732if (adr_type->base() == Type::AnyPtr) return false; // TypePtr::BOTTOM or its twins17331734// the only remaining possible overlap is identity1735int adr_idx = get_alias_index(adr_type);1736assert(adr_idx != AliasIdxBot && adr_idx != AliasIdxTop, "");1737assert(adr_idx == alias_idx ||1738(alias_type(alias_idx)->adr_type() != TypeOopPtr::BOTTOM1739&& adr_type != TypeOopPtr::BOTTOM),1740"should not be testing for overlap with an unsafe pointer");1741return adr_idx == alias_idx;1742}17431744//------------------------------can_alias--------------------------------------1745// True if any values of the given address type are in the given alias category.1746bool Compile::can_alias(const TypePtr* adr_type, int alias_idx) {1747if (alias_idx == AliasIdxTop) return false; // the empty category1748if (adr_type == NULL) return false; // NULL serves as TypePtr::TOP1749// Known instance doesn't alias with bottom memory1750if (alias_idx == AliasIdxBot) return !adr_type->is_known_instance(); // the universal category1751if (adr_type->base() == Type::AnyPtr) return !C->get_adr_type(alias_idx)->is_known_instance(); // TypePtr::BOTTOM or its twins17521753// the only remaining possible overlap is identity1754int adr_idx = get_alias_index(adr_type);1755assert(adr_idx != AliasIdxBot && adr_idx != AliasIdxTop, "");1756return adr_idx == alias_idx;1757}17581759//---------------------cleanup_loop_predicates-----------------------1760// Remove the opaque nodes that protect the predicates so that all unused1761// checks and uncommon_traps will be eliminated from the ideal graph1762void Compile::cleanup_loop_predicates(PhaseIterGVN &igvn) {1763if (predicate_count()==0) return;1764for (int i = predicate_count(); i > 0; i--) {1765Node * n = predicate_opaque1_node(i-1);1766assert(n->Opcode() == Op_Opaque1, "must be");1767igvn.replace_node(n, n->in(1));1768}1769assert(predicate_count()==0, "should be clean!");1770}17711772void Compile::record_for_post_loop_opts_igvn(Node* n) {1773if (!n->for_post_loop_opts_igvn()) {1774assert(!_for_post_loop_igvn.contains(n), "duplicate");1775n->add_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);1776_for_post_loop_igvn.append(n);1777}1778}17791780void Compile::remove_from_post_loop_opts_igvn(Node* n) {1781n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);1782_for_post_loop_igvn.remove(n);1783}17841785void Compile::process_for_post_loop_opts_igvn(PhaseIterGVN& igvn) {1786// Verify that all previous optimizations produced a valid graph1787// at least to this point, even if no loop optimizations were done.1788PhaseIdealLoop::verify(igvn);17891790C->set_post_loop_opts_phase(); // no more loop opts allowed17911792assert(!C->major_progress(), "not cleared");17931794if (_for_post_loop_igvn.length() > 0) {1795while (_for_post_loop_igvn.length() > 0) {1796Node* n = _for_post_loop_igvn.pop();1797n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);1798igvn._worklist.push(n);1799}1800igvn.optimize();1801assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");18021803// Sometimes IGVN sets major progress (e.g., when processing loop nodes).1804if (C->major_progress()) {1805C->clear_major_progress(); // ensure that major progress is now clear1806}1807}1808}18091810// StringOpts and late inlining of string methods1811void Compile::inline_string_calls(bool parse_time) {1812{1813// remove useless nodes to make the usage analysis simpler1814ResourceMark rm;1815PhaseRemoveUseless pru(initial_gvn(), for_igvn());1816}18171818{1819ResourceMark rm;1820print_method(PHASE_BEFORE_STRINGOPTS, 3);1821PhaseStringOpts pso(initial_gvn(), for_igvn());1822print_method(PHASE_AFTER_STRINGOPTS, 3);1823}18241825// now inline anything that we skipped the first time around1826if (!parse_time) {1827_late_inlines_pos = _late_inlines.length();1828}18291830while (_string_late_inlines.length() > 0) {1831CallGenerator* cg = _string_late_inlines.pop();1832cg->do_late_inline();1833if (failing()) return;1834}1835_string_late_inlines.trunc_to(0);1836}18371838// Late inlining of boxing methods1839void Compile::inline_boxing_calls(PhaseIterGVN& igvn) {1840if (_boxing_late_inlines.length() > 0) {1841assert(has_boxed_value(), "inconsistent");18421843PhaseGVN* gvn = initial_gvn();1844set_inlining_incrementally(true);18451846assert( igvn._worklist.size() == 0, "should be done with igvn" );1847for_igvn()->clear();1848gvn->replace_with(&igvn);18491850_late_inlines_pos = _late_inlines.length();18511852while (_boxing_late_inlines.length() > 0) {1853CallGenerator* cg = _boxing_late_inlines.pop();1854cg->do_late_inline();1855if (failing()) return;1856}1857_boxing_late_inlines.trunc_to(0);18581859inline_incrementally_cleanup(igvn);18601861set_inlining_incrementally(false);1862}1863}18641865bool Compile::inline_incrementally_one() {1866assert(IncrementalInline, "incremental inlining should be on");18671868TracePhase tp("incrementalInline_inline", &timers[_t_incrInline_inline]);18691870set_inlining_progress(false);1871set_do_cleanup(false);18721873for (int i = 0; i < _late_inlines.length(); i++) {1874_late_inlines_pos = i+1;1875CallGenerator* cg = _late_inlines.at(i);1876bool does_dispatch = cg->is_virtual_late_inline() || cg->is_mh_late_inline();1877if (inlining_incrementally() || does_dispatch) { // a call can be either inlined or strength-reduced to a direct call1878cg->do_late_inline();1879assert(_late_inlines.at(i) == cg, "no insertions before current position allowed");1880if (failing()) {1881return false;1882} else if (inlining_progress()) {1883_late_inlines_pos = i+1; // restore the position in case new elements were inserted1884print_method(PHASE_INCREMENTAL_INLINE_STEP, cg->call_node(), 3);1885break; // process one call site at a time1886}1887} else {1888// Ignore late inline direct calls when inlining is not allowed.1889// They are left in the late inline list when node budget is exhausted until the list is fully drained.1890}1891}1892// Remove processed elements.1893_late_inlines.remove_till(_late_inlines_pos);1894_late_inlines_pos = 0;18951896assert(inlining_progress() || _late_inlines.length() == 0, "no progress");18971898bool needs_cleanup = do_cleanup() || over_inlining_cutoff();18991900set_inlining_progress(false);1901set_do_cleanup(false);19021903bool force_cleanup = directive()->IncrementalInlineForceCleanupOption;1904return (_late_inlines.length() > 0) && !needs_cleanup && !force_cleanup;1905}19061907void Compile::inline_incrementally_cleanup(PhaseIterGVN& igvn) {1908{1909TracePhase tp("incrementalInline_pru", &timers[_t_incrInline_pru]);1910ResourceMark rm;1911PhaseRemoveUseless pru(initial_gvn(), for_igvn());1912}1913{1914TracePhase tp("incrementalInline_igvn", &timers[_t_incrInline_igvn]);1915igvn = PhaseIterGVN(initial_gvn());1916igvn.optimize();1917}1918print_method(PHASE_INCREMENTAL_INLINE_CLEANUP, 3);1919}19201921// Perform incremental inlining until bound on number of live nodes is reached1922void Compile::inline_incrementally(PhaseIterGVN& igvn) {1923TracePhase tp("incrementalInline", &timers[_t_incrInline]);19241925set_inlining_incrementally(true);1926uint low_live_nodes = 0;19271928while (_late_inlines.length() > 0) {1929if (live_nodes() > (uint)LiveNodeCountInliningCutoff) {1930if (low_live_nodes < (uint)LiveNodeCountInliningCutoff * 8 / 10) {1931TracePhase tp("incrementalInline_ideal", &timers[_t_incrInline_ideal]);1932// PhaseIdealLoop is expensive so we only try it once we are1933// out of live nodes and we only try it again if the previous1934// helped got the number of nodes down significantly1935PhaseIdealLoop::optimize(igvn, LoopOptsNone);1936if (failing()) return;1937low_live_nodes = live_nodes();1938_major_progress = true;1939}19401941if (live_nodes() > (uint)LiveNodeCountInliningCutoff) {1942bool do_print_inlining = print_inlining() || print_intrinsics();1943if (do_print_inlining || log() != NULL) {1944// Print inlining message for candidates that we couldn't inline for lack of space.1945for (int i = 0; i < _late_inlines.length(); i++) {1946CallGenerator* cg = _late_inlines.at(i);1947const char* msg = "live nodes > LiveNodeCountInliningCutoff";1948if (do_print_inlining) {1949cg->print_inlining_late(msg);1950}1951log_late_inline_failure(cg, msg);1952}1953}1954break; // finish1955}1956}19571958for_igvn()->clear();1959initial_gvn()->replace_with(&igvn);19601961while (inline_incrementally_one()) {1962assert(!failing(), "inconsistent");1963}1964if (failing()) return;19651966inline_incrementally_cleanup(igvn);19671968print_method(PHASE_INCREMENTAL_INLINE_STEP, 3);19691970if (failing()) return;19711972if (_late_inlines.length() == 0) {1973break; // no more progress1974}1975}1976assert( igvn._worklist.size() == 0, "should be done with igvn" );19771978if (_string_late_inlines.length() > 0) {1979assert(has_stringbuilder(), "inconsistent");1980for_igvn()->clear();1981initial_gvn()->replace_with(&igvn);19821983inline_string_calls(false);19841985if (failing()) return;19861987inline_incrementally_cleanup(igvn);1988}19891990set_inlining_incrementally(false);1991}19921993void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {1994// "inlining_incrementally() == false" is used to signal that no inlining is allowed1995// (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).1996// Tracking and verification of modified nodes is disabled by setting "_modified_nodes == NULL"1997// as if "inlining_incrementally() == true" were set.1998assert(inlining_incrementally() == false, "not allowed");1999assert(_modified_nodes == NULL, "not allowed");2000assert(_late_inlines.length() > 0, "sanity");20012002while (_late_inlines.length() > 0) {2003for_igvn()->clear();2004initial_gvn()->replace_with(&igvn);20052006while (inline_incrementally_one()) {2007assert(!failing(), "inconsistent");2008}2009if (failing()) return;20102011inline_incrementally_cleanup(igvn);2012}2013}20142015bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {2016if (_loop_opts_cnt > 0) {2017while (major_progress() && (_loop_opts_cnt > 0)) {2018TracePhase tp("idealLoop", &timers[_t_idealLoop]);2019PhaseIdealLoop::optimize(igvn, mode);2020_loop_opts_cnt--;2021if (failing()) return false;2022if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);2023}2024}2025return true;2026}20272028// Remove edges from "root" to each SafePoint at a backward branch.2029// They were inserted during parsing (see add_safepoint()) to make2030// infinite loops without calls or exceptions visible to root, i.e.,2031// useful.2032void Compile::remove_root_to_sfpts_edges(PhaseIterGVN& igvn) {2033Node *r = root();2034if (r != NULL) {2035for (uint i = r->req(); i < r->len(); ++i) {2036Node *n = r->in(i);2037if (n != NULL && n->is_SafePoint()) {2038r->rm_prec(i);2039if (n->outcnt() == 0) {2040igvn.remove_dead_node(n);2041}2042--i;2043}2044}2045// Parsing may have added top inputs to the root node (Path2046// leading to the Halt node proven dead). Make sure we get a2047// chance to clean them up.2048igvn._worklist.push(r);2049igvn.optimize();2050}2051}20522053//------------------------------Optimize---------------------------------------2054// Given a graph, optimize it.2055void Compile::Optimize() {2056TracePhase tp("optimizer", &timers[_t_optimizer]);20572058#ifndef PRODUCT2059if (env()->break_at_compile()) {2060BREAKPOINT;2061}20622063#endif20642065BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();2066#ifdef ASSERT2067bs->verify_gc_barriers(this, BarrierSetC2::BeforeOptimize);2068#endif20692070ResourceMark rm;20712072print_inlining_reinit();20732074NOT_PRODUCT( verify_graph_edges(); )20752076print_method(PHASE_AFTER_PARSING);20772078{2079// Iterative Global Value Numbering, including ideal transforms2080// Initialize IterGVN with types and values from parse-time GVN2081PhaseIterGVN igvn(initial_gvn());2082#ifdef ASSERT2083_modified_nodes = new (comp_arena()) Unique_Node_List(comp_arena());2084#endif2085{2086TracePhase tp("iterGVN", &timers[_t_iterGVN]);2087igvn.optimize();2088}20892090if (failing()) return;20912092print_method(PHASE_ITER_GVN1, 2);20932094inline_incrementally(igvn);20952096print_method(PHASE_INCREMENTAL_INLINE, 2);20972098if (failing()) return;20992100if (eliminate_boxing()) {2101// Inline valueOf() methods now.2102inline_boxing_calls(igvn);21032104if (AlwaysIncrementalInline) {2105inline_incrementally(igvn);2106}21072108print_method(PHASE_INCREMENTAL_BOXING_INLINE, 2);21092110if (failing()) return;2111}21122113// Remove the speculative part of types and clean up the graph from2114// the extra CastPP nodes whose only purpose is to carry them. Do2115// that early so that optimizations are not disrupted by the extra2116// CastPP nodes.2117remove_speculative_types(igvn);21182119// No more new expensive nodes will be added to the list from here2120// so keep only the actual candidates for optimizations.2121cleanup_expensive_nodes(igvn);21222123assert(EnableVectorSupport || !has_vbox_nodes(), "sanity");2124if (EnableVectorSupport && has_vbox_nodes()) {2125TracePhase tp("", &timers[_t_vector]);2126PhaseVector pv(igvn);2127pv.optimize_vector_boxes();21282129print_method(PHASE_ITER_GVN_AFTER_VECTOR, 2);2130}2131assert(!has_vbox_nodes(), "sanity");21322133if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {2134Compile::TracePhase tp("", &timers[_t_renumberLive]);2135initial_gvn()->replace_with(&igvn);2136for_igvn()->clear();2137Unique_Node_List new_worklist(C->comp_arena());2138{2139ResourceMark rm;2140PhaseRenumberLive prl = PhaseRenumberLive(initial_gvn(), for_igvn(), &new_worklist);2141}2142Unique_Node_List* save_for_igvn = for_igvn();2143set_for_igvn(&new_worklist);2144igvn = PhaseIterGVN(initial_gvn());2145igvn.optimize();2146set_for_igvn(save_for_igvn);2147}21482149// Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop2150// safepoints2151remove_root_to_sfpts_edges(igvn);21522153// Perform escape analysis2154if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {2155if (has_loops()) {2156// Cleanup graph (remove dead nodes).2157TracePhase tp("idealLoop", &timers[_t_idealLoop]);2158PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);2159if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);2160if (failing()) return;2161}2162ConnectionGraph::do_analysis(this, &igvn);21632164if (failing()) return;21652166// Optimize out fields loads from scalar replaceable allocations.2167igvn.optimize();2168print_method(PHASE_ITER_GVN_AFTER_EA, 2);21692170if (failing()) return;21712172if (congraph() != NULL && macro_count() > 0) {2173TracePhase tp("macroEliminate", &timers[_t_macroEliminate]);2174PhaseMacroExpand mexp(igvn);2175mexp.eliminate_macro_nodes();2176igvn.set_delay_transform(false);21772178igvn.optimize();2179print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);21802181if (failing()) return;2182}2183}21842185// Loop transforms on the ideal graph. Range Check Elimination,2186// peeling, unrolling, etc.21872188// Set loop opts counter2189if((_loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {2190{2191TracePhase tp("idealLoop", &timers[_t_idealLoop]);2192PhaseIdealLoop::optimize(igvn, LoopOptsDefault);2193_loop_opts_cnt--;2194if (major_progress()) print_method(PHASE_PHASEIDEALLOOP1, 2);2195if (failing()) return;2196}2197// Loop opts pass if partial peeling occurred in previous pass2198if(PartialPeelLoop && major_progress() && (_loop_opts_cnt > 0)) {2199TracePhase tp("idealLoop", &timers[_t_idealLoop]);2200PhaseIdealLoop::optimize(igvn, LoopOptsSkipSplitIf);2201_loop_opts_cnt--;2202if (major_progress()) print_method(PHASE_PHASEIDEALLOOP2, 2);2203if (failing()) return;2204}2205// Loop opts pass for loop-unrolling before CCP2206if(major_progress() && (_loop_opts_cnt > 0)) {2207TracePhase tp("idealLoop", &timers[_t_idealLoop]);2208PhaseIdealLoop::optimize(igvn, LoopOptsSkipSplitIf);2209_loop_opts_cnt--;2210if (major_progress()) print_method(PHASE_PHASEIDEALLOOP3, 2);2211}2212if (!failing()) {2213// Verify that last round of loop opts produced a valid graph2214PhaseIdealLoop::verify(igvn);2215}2216}2217if (failing()) return;22182219// Conditional Constant Propagation;2220PhaseCCP ccp( &igvn );2221assert( true, "Break here to ccp.dump_nodes_and_types(_root,999,1)");2222{2223TracePhase tp("ccp", &timers[_t_ccp]);2224ccp.do_transform();2225}2226print_method(PHASE_CCP1, 2);22272228assert( true, "Break here to ccp.dump_old2new_map()");22292230// Iterative Global Value Numbering, including ideal transforms2231{2232TracePhase tp("iterGVN2", &timers[_t_iterGVN2]);2233igvn = ccp;2234igvn.optimize();2235}2236print_method(PHASE_ITER_GVN2, 2);22372238if (failing()) return;22392240// Loop transforms on the ideal graph. Range Check Elimination,2241// peeling, unrolling, etc.2242if (!optimize_loops(igvn, LoopOptsDefault)) {2243return;2244}22452246if (failing()) return;22472248C->clear_major_progress(); // ensure that major progress is now clear22492250process_for_post_loop_opts_igvn(igvn);22512252#ifdef ASSERT2253bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);2254#endif22552256{2257TracePhase tp("macroExpand", &timers[_t_macroExpand]);2258PhaseMacroExpand mex(igvn);2259if (mex.expand_macro_nodes()) {2260assert(failing(), "must bail out w/ explicit message");2261return;2262}2263print_method(PHASE_MACRO_EXPANSION, 2);2264}22652266{2267TracePhase tp("barrierExpand", &timers[_t_barrierExpand]);2268if (bs->expand_barriers(this, igvn)) {2269assert(failing(), "must bail out w/ explicit message");2270return;2271}2272print_method(PHASE_BARRIER_EXPANSION, 2);2273}22742275if (C->max_vector_size() > 0) {2276C->optimize_logic_cones(igvn);2277igvn.optimize();2278}22792280DEBUG_ONLY( _modified_nodes = NULL; )22812282assert(igvn._worklist.size() == 0, "not empty");22832284assert(_late_inlines.length() == 0 || IncrementalInlineMH || IncrementalInlineVirtual, "not empty");22852286if (_late_inlines.length() > 0) {2287// More opportunities to optimize virtual and MH calls.2288// Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.2289process_late_inline_calls_no_inline(igvn);2290}2291} // (End scope of igvn; run destructor if necessary for asserts.)22922293check_no_dead_use();22942295process_print_inlining();22962297// A method with only infinite loops has no edges entering loops from root2298{2299TracePhase tp("graphReshape", &timers[_t_graphReshaping]);2300if (final_graph_reshaping()) {2301assert(failing(), "must bail out w/ explicit message");2302return;2303}2304}23052306print_method(PHASE_OPTIMIZE_FINISHED, 2);2307DEBUG_ONLY(set_phase_optimize_finished();)2308}23092310#ifdef ASSERT2311void Compile::check_no_dead_use() const {2312ResourceMark rm;2313Unique_Node_List wq;2314wq.push(root());2315for (uint i = 0; i < wq.size(); ++i) {2316Node* n = wq.at(i);2317for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {2318Node* u = n->fast_out(j);2319if (u->outcnt() == 0 && !u->is_Con()) {2320u->dump();2321fatal("no reachable node should have no use");2322}2323wq.push(u);2324}2325}2326}2327#endif23282329void Compile::inline_vector_reboxing_calls() {2330if (C->_vector_reboxing_late_inlines.length() > 0) {2331_late_inlines_pos = C->_late_inlines.length();2332while (_vector_reboxing_late_inlines.length() > 0) {2333CallGenerator* cg = _vector_reboxing_late_inlines.pop();2334cg->do_late_inline();2335if (failing()) return;2336print_method(PHASE_INLINE_VECTOR_REBOX, cg->call_node());2337}2338_vector_reboxing_late_inlines.trunc_to(0);2339}2340}23412342bool Compile::has_vbox_nodes() {2343if (C->_vector_reboxing_late_inlines.length() > 0) {2344return true;2345}2346for (int macro_idx = C->macro_count() - 1; macro_idx >= 0; macro_idx--) {2347Node * n = C->macro_node(macro_idx);2348assert(n->is_macro(), "only macro nodes expected here");2349if (n->Opcode() == Op_VectorUnbox || n->Opcode() == Op_VectorBox || n->Opcode() == Op_VectorBoxAllocate) {2350return true;2351}2352}2353return false;2354}23552356//---------------------------- Bitwise operation packing optimization ---------------------------23572358static bool is_vector_unary_bitwise_op(Node* n) {2359return n->Opcode() == Op_XorV &&2360VectorNode::is_vector_bitwise_not_pattern(n);2361}23622363static bool is_vector_binary_bitwise_op(Node* n) {2364switch (n->Opcode()) {2365case Op_AndV:2366case Op_OrV:2367return true;23682369case Op_XorV:2370return !is_vector_unary_bitwise_op(n);23712372default:2373return false;2374}2375}23762377static bool is_vector_ternary_bitwise_op(Node* n) {2378return n->Opcode() == Op_MacroLogicV;2379}23802381static bool is_vector_bitwise_op(Node* n) {2382return is_vector_unary_bitwise_op(n) ||2383is_vector_binary_bitwise_op(n) ||2384is_vector_ternary_bitwise_op(n);2385}23862387static bool is_vector_bitwise_cone_root(Node* n) {2388if (n->bottom_type()->isa_vectmask() || !is_vector_bitwise_op(n)) {2389return false;2390}2391for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {2392if (is_vector_bitwise_op(n->fast_out(i))) {2393return false;2394}2395}2396return true;2397}23982399static uint collect_unique_inputs(Node* n, Unique_Node_List& partition, Unique_Node_List& inputs) {2400uint cnt = 0;2401if (is_vector_bitwise_op(n)) {2402if (VectorNode::is_vector_bitwise_not_pattern(n)) {2403for (uint i = 1; i < n->req(); i++) {2404Node* in = n->in(i);2405bool skip = VectorNode::is_all_ones_vector(in);2406if (!skip && !inputs.member(in)) {2407inputs.push(in);2408cnt++;2409}2410}2411assert(cnt <= 1, "not unary");2412} else {2413uint last_req = n->req();2414if (is_vector_ternary_bitwise_op(n)) {2415last_req = n->req() - 1; // skip last input2416}2417for (uint i = 1; i < last_req; i++) {2418Node* def = n->in(i);2419if (!inputs.member(def)) {2420inputs.push(def);2421cnt++;2422}2423}2424}2425partition.push(n);2426} else { // not a bitwise operations2427if (!inputs.member(n)) {2428inputs.push(n);2429cnt++;2430}2431}2432return cnt;2433}24342435void Compile::collect_logic_cone_roots(Unique_Node_List& list) {2436Unique_Node_List useful_nodes;2437C->identify_useful_nodes(useful_nodes);24382439for (uint i = 0; i < useful_nodes.size(); i++) {2440Node* n = useful_nodes.at(i);2441if (is_vector_bitwise_cone_root(n)) {2442list.push(n);2443}2444}2445}24462447Node* Compile::xform_to_MacroLogicV(PhaseIterGVN& igvn,2448const TypeVect* vt,2449Unique_Node_List& partition,2450Unique_Node_List& inputs) {2451assert(partition.size() == 2 || partition.size() == 3, "not supported");2452assert(inputs.size() == 2 || inputs.size() == 3, "not supported");2453assert(Matcher::match_rule_supported_vector(Op_MacroLogicV, vt->length(), vt->element_basic_type()), "not supported");24542455Node* in1 = inputs.at(0);2456Node* in2 = inputs.at(1);2457Node* in3 = (inputs.size() == 3 ? inputs.at(2) : in2);24582459uint func = compute_truth_table(partition, inputs);2460return igvn.transform(MacroLogicVNode::make(igvn, in3, in2, in1, func, vt));2461}24622463static uint extract_bit(uint func, uint pos) {2464return (func & (1 << pos)) >> pos;2465}24662467//2468// A macro logic node represents a truth table. It has 4 inputs,2469// First three inputs corresponds to 3 columns of a truth table2470// and fourth input captures the logic function.2471//2472// eg. fn = (in1 AND in2) OR in3;2473//2474// MacroNode(in1,in2,in3,fn)2475//2476// -----------------2477// in1 in2 in3 fn2478// -----------------2479// 0 0 0 02480// 0 0 1 12481// 0 1 0 02482// 0 1 1 12483// 1 0 0 02484// 1 0 1 12485// 1 1 0 12486// 1 1 1 12487//24882489uint Compile::eval_macro_logic_op(uint func, uint in1 , uint in2, uint in3) {2490int res = 0;2491for (int i = 0; i < 8; i++) {2492int bit1 = extract_bit(in1, i);2493int bit2 = extract_bit(in2, i);2494int bit3 = extract_bit(in3, i);24952496int func_bit_pos = (bit1 << 2 | bit2 << 1 | bit3);2497int func_bit = extract_bit(func, func_bit_pos);24982499res |= func_bit << i;2500}2501return res;2502}25032504static uint eval_operand(Node* n, ResourceHashtable<Node*,uint>& eval_map) {2505assert(n != NULL, "");2506assert(eval_map.contains(n), "absent");2507return *(eval_map.get(n));2508}25092510static void eval_operands(Node* n,2511uint& func1, uint& func2, uint& func3,2512ResourceHashtable<Node*,uint>& eval_map) {2513assert(is_vector_bitwise_op(n), "");25142515if (is_vector_unary_bitwise_op(n)) {2516Node* opnd = n->in(1);2517if (VectorNode::is_vector_bitwise_not_pattern(n) && VectorNode::is_all_ones_vector(opnd)) {2518opnd = n->in(2);2519}2520func1 = eval_operand(opnd, eval_map);2521} else if (is_vector_binary_bitwise_op(n)) {2522func1 = eval_operand(n->in(1), eval_map);2523func2 = eval_operand(n->in(2), eval_map);2524} else {2525assert(is_vector_ternary_bitwise_op(n), "unknown operation");2526func1 = eval_operand(n->in(1), eval_map);2527func2 = eval_operand(n->in(2), eval_map);2528func3 = eval_operand(n->in(3), eval_map);2529}2530}25312532uint Compile::compute_truth_table(Unique_Node_List& partition, Unique_Node_List& inputs) {2533assert(inputs.size() <= 3, "sanity");2534ResourceMark rm;2535uint res = 0;2536ResourceHashtable<Node*,uint> eval_map;25372538// Populate precomputed functions for inputs.2539// Each input corresponds to one column of 3 input truth-table.2540uint input_funcs[] = { 0xAA, // (_, _, a) -> a25410xCC, // (_, b, _) -> b25420xF0 }; // (c, _, _) -> c2543for (uint i = 0; i < inputs.size(); i++) {2544eval_map.put(inputs.at(i), input_funcs[i]);2545}25462547for (uint i = 0; i < partition.size(); i++) {2548Node* n = partition.at(i);25492550uint func1 = 0, func2 = 0, func3 = 0;2551eval_operands(n, func1, func2, func3, eval_map);25522553switch (n->Opcode()) {2554case Op_OrV:2555assert(func3 == 0, "not binary");2556res = func1 | func2;2557break;2558case Op_AndV:2559assert(func3 == 0, "not binary");2560res = func1 & func2;2561break;2562case Op_XorV:2563if (VectorNode::is_vector_bitwise_not_pattern(n)) {2564assert(func2 == 0 && func3 == 0, "not unary");2565res = (~func1) & 0xFF;2566} else {2567assert(func3 == 0, "not binary");2568res = func1 ^ func2;2569}2570break;2571case Op_MacroLogicV:2572// Ordering of inputs may change during evaluation of sub-tree2573// containing MacroLogic node as a child node, thus a re-evaluation2574// makes sure that function is evaluated in context of current2575// inputs.2576res = eval_macro_logic_op(n->in(4)->get_int(), func1, func2, func3);2577break;25782579default: assert(false, "not supported: %s", n->Name());2580}2581assert(res <= 0xFF, "invalid");2582eval_map.put(n, res);2583}2584return res;2585}25862587bool Compile::compute_logic_cone(Node* n, Unique_Node_List& partition, Unique_Node_List& inputs) {2588assert(partition.size() == 0, "not empty");2589assert(inputs.size() == 0, "not empty");2590if (is_vector_ternary_bitwise_op(n)) {2591return false;2592}25932594bool is_unary_op = is_vector_unary_bitwise_op(n);2595if (is_unary_op) {2596assert(collect_unique_inputs(n, partition, inputs) == 1, "not unary");2597return false; // too few inputs2598}25992600assert(is_vector_binary_bitwise_op(n), "not binary");2601Node* in1 = n->in(1);2602Node* in2 = n->in(2);26032604int in1_unique_inputs_cnt = collect_unique_inputs(in1, partition, inputs);2605int in2_unique_inputs_cnt = collect_unique_inputs(in2, partition, inputs);2606partition.push(n);26072608// Too many inputs?2609if (inputs.size() > 3) {2610partition.clear();2611inputs.clear();2612{ // Recompute in2 inputs2613Unique_Node_List not_used;2614in2_unique_inputs_cnt = collect_unique_inputs(in2, not_used, not_used);2615}2616// Pick the node with minimum number of inputs.2617if (in1_unique_inputs_cnt >= 3 && in2_unique_inputs_cnt >= 3) {2618return false; // still too many inputs2619}2620// Recompute partition & inputs.2621Node* child = (in1_unique_inputs_cnt < in2_unique_inputs_cnt ? in1 : in2);2622collect_unique_inputs(child, partition, inputs);26232624Node* other_input = (in1_unique_inputs_cnt < in2_unique_inputs_cnt ? in2 : in1);2625inputs.push(other_input);26262627partition.push(n);2628}26292630return (partition.size() == 2 || partition.size() == 3) &&2631(inputs.size() == 2 || inputs.size() == 3);2632}263326342635void Compile::process_logic_cone_root(PhaseIterGVN &igvn, Node *n, VectorSet &visited) {2636assert(is_vector_bitwise_op(n), "not a root");26372638visited.set(n->_idx);26392640// 1) Do a DFS walk over the logic cone.2641for (uint i = 1; i < n->req(); i++) {2642Node* in = n->in(i);2643if (!visited.test(in->_idx) && is_vector_bitwise_op(in)) {2644process_logic_cone_root(igvn, in, visited);2645}2646}26472648// 2) Bottom up traversal: Merge node[s] with2649// the parent to form macro logic node.2650Unique_Node_List partition;2651Unique_Node_List inputs;2652if (compute_logic_cone(n, partition, inputs)) {2653const TypeVect* vt = n->bottom_type()->is_vect();2654Node* macro_logic = xform_to_MacroLogicV(igvn, vt, partition, inputs);2655igvn.replace_node(n, macro_logic);2656}2657}26582659void Compile::optimize_logic_cones(PhaseIterGVN &igvn) {2660ResourceMark rm;2661if (Matcher::match_rule_supported(Op_MacroLogicV)) {2662Unique_Node_List list;2663collect_logic_cone_roots(list);26642665while (list.size() > 0) {2666Node* n = list.pop();2667const TypeVect* vt = n->bottom_type()->is_vect();2668bool supported = Matcher::match_rule_supported_vector(Op_MacroLogicV, vt->length(), vt->element_basic_type());2669if (supported) {2670VectorSet visited(comp_arena());2671process_logic_cone_root(igvn, n, visited);2672}2673}2674}2675}26762677//------------------------------Code_Gen---------------------------------------2678// Given a graph, generate code for it2679void Compile::Code_Gen() {2680if (failing()) {2681return;2682}26832684// Perform instruction selection. You might think we could reclaim Matcher2685// memory PDQ, but actually the Matcher is used in generating spill code.2686// Internals of the Matcher (including some VectorSets) must remain live2687// for awhile - thus I cannot reclaim Matcher memory lest a VectorSet usage2688// set a bit in reclaimed memory.26892690// In debug mode can dump m._nodes.dump() for mapping of ideal to machine2691// nodes. Mapping is only valid at the root of each matched subtree.2692NOT_PRODUCT( verify_graph_edges(); )26932694Matcher matcher;2695_matcher = &matcher;2696{2697TracePhase tp("matcher", &timers[_t_matcher]);2698matcher.match();2699if (failing()) {2700return;2701}2702}2703// In debug mode can dump m._nodes.dump() for mapping of ideal to machine2704// nodes. Mapping is only valid at the root of each matched subtree.2705NOT_PRODUCT( verify_graph_edges(); )27062707// If you have too many nodes, or if matching has failed, bail out2708check_node_count(0, "out of nodes matching instructions");2709if (failing()) {2710return;2711}27122713print_method(PHASE_MATCHING, 2);27142715// Build a proper-looking CFG2716PhaseCFG cfg(node_arena(), root(), matcher);2717_cfg = &cfg;2718{2719TracePhase tp("scheduler", &timers[_t_scheduler]);2720bool success = cfg.do_global_code_motion();2721if (!success) {2722return;2723}27242725print_method(PHASE_GLOBAL_CODE_MOTION, 2);2726NOT_PRODUCT( verify_graph_edges(); )2727cfg.verify();2728}27292730PhaseChaitin regalloc(unique(), cfg, matcher, false);2731_regalloc = ®alloc;2732{2733TracePhase tp("regalloc", &timers[_t_registerAllocation]);2734// Perform register allocation. After Chaitin, use-def chains are2735// no longer accurate (at spill code) and so must be ignored.2736// Node->LRG->reg mappings are still accurate.2737_regalloc->Register_Allocate();27382739// Bail out if the allocator builds too many nodes2740if (failing()) {2741return;2742}2743}27442745// Prior to register allocation we kept empty basic blocks in case the2746// the allocator needed a place to spill. After register allocation we2747// are not adding any new instructions. If any basic block is empty, we2748// can now safely remove it.2749{2750TracePhase tp("blockOrdering", &timers[_t_blockOrdering]);2751cfg.remove_empty_blocks();2752if (do_freq_based_layout()) {2753PhaseBlockLayout layout(cfg);2754} else {2755cfg.set_loop_alignment();2756}2757cfg.fixup_flow();2758}27592760// Apply peephole optimizations2761if( OptoPeephole ) {2762TracePhase tp("peephole", &timers[_t_peephole]);2763PhasePeephole peep( _regalloc, cfg);2764peep.do_transform();2765}27662767// Do late expand if CPU requires this.2768if (Matcher::require_postalloc_expand) {2769TracePhase tp("postalloc_expand", &timers[_t_postalloc_expand]);2770cfg.postalloc_expand(_regalloc);2771}27722773// Convert Nodes to instruction bits in a buffer2774{2775TracePhase tp("output", &timers[_t_output]);2776PhaseOutput output;2777output.Output();2778if (failing()) return;2779output.install();2780}27812782print_method(PHASE_FINAL_CODE);27832784// He's dead, Jim.2785_cfg = (PhaseCFG*)((intptr_t)0xdeadbeef);2786_regalloc = (PhaseChaitin*)((intptr_t)0xdeadbeef);2787}27882789//------------------------------Final_Reshape_Counts---------------------------2790// This class defines counters to help identify when a method2791// may/must be executed using hardware with only 24-bit precision.2792struct Final_Reshape_Counts : public StackObj {2793int _call_count; // count non-inlined 'common' calls2794int _float_count; // count float ops requiring 24-bit precision2795int _double_count; // count double ops requiring more precision2796int _java_call_count; // count non-inlined 'java' calls2797int _inner_loop_count; // count loops which need alignment2798VectorSet _visited; // Visitation flags2799Node_List _tests; // Set of IfNodes & PCTableNodes28002801Final_Reshape_Counts() :2802_call_count(0), _float_count(0), _double_count(0),2803_java_call_count(0), _inner_loop_count(0) { }28042805void inc_call_count () { _call_count ++; }2806void inc_float_count () { _float_count ++; }2807void inc_double_count() { _double_count++; }2808void inc_java_call_count() { _java_call_count++; }2809void inc_inner_loop_count() { _inner_loop_count++; }28102811int get_call_count () const { return _call_count ; }2812int get_float_count () const { return _float_count ; }2813int get_double_count() const { return _double_count; }2814int get_java_call_count() const { return _java_call_count; }2815int get_inner_loop_count() const { return _inner_loop_count; }2816};28172818// Eliminate trivially redundant StoreCMs and accumulate their2819// precedence edges.2820void Compile::eliminate_redundant_card_marks(Node* n) {2821assert(n->Opcode() == Op_StoreCM, "expected StoreCM");2822if (n->in(MemNode::Address)->outcnt() > 1) {2823// There are multiple users of the same address so it might be2824// possible to eliminate some of the StoreCMs2825Node* mem = n->in(MemNode::Memory);2826Node* adr = n->in(MemNode::Address);2827Node* val = n->in(MemNode::ValueIn);2828Node* prev = n;2829bool done = false;2830// Walk the chain of StoreCMs eliminating ones that match. As2831// long as it's a chain of single users then the optimization is2832// safe. Eliminating partially redundant StoreCMs would require2833// cloning copies down the other paths.2834while (mem->Opcode() == Op_StoreCM && mem->outcnt() == 1 && !done) {2835if (adr == mem->in(MemNode::Address) &&2836val == mem->in(MemNode::ValueIn)) {2837// redundant StoreCM2838if (mem->req() > MemNode::OopStore) {2839// Hasn't been processed by this code yet.2840n->add_prec(mem->in(MemNode::OopStore));2841} else {2842// Already converted to precedence edge2843for (uint i = mem->req(); i < mem->len(); i++) {2844// Accumulate any precedence edges2845if (mem->in(i) != NULL) {2846n->add_prec(mem->in(i));2847}2848}2849// Everything above this point has been processed.2850done = true;2851}2852// Eliminate the previous StoreCM2853prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));2854assert(mem->outcnt() == 0, "should be dead");2855mem->disconnect_inputs(this);2856} else {2857prev = mem;2858}2859mem = prev->in(MemNode::Memory);2860}2861}2862}28632864//------------------------------final_graph_reshaping_impl----------------------2865// Implement items 1-5 from final_graph_reshaping below.2866void Compile::final_graph_reshaping_impl( Node *n, Final_Reshape_Counts &frc) {28672868if ( n->outcnt() == 0 ) return; // dead node2869uint nop = n->Opcode();28702871// Check for 2-input instruction with "last use" on right input.2872// Swap to left input. Implements item (2).2873if( n->req() == 3 && // two-input instruction2874n->in(1)->outcnt() > 1 && // left use is NOT a last use2875(!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop2876n->in(2)->outcnt() == 1 &&// right use IS a last use2877!n->in(2)->is_Con() ) { // right use is not a constant2878// Check for commutative opcode2879switch( nop ) {2880case Op_AddI: case Op_AddF: case Op_AddD: case Op_AddL:2881case Op_MaxI: case Op_MaxL: case Op_MaxF: case Op_MaxD:2882case Op_MinI: case Op_MinL: case Op_MinF: case Op_MinD:2883case Op_MulI: case Op_MulF: case Op_MulD: case Op_MulL:2884case Op_AndL: case Op_XorL: case Op_OrL:2885case Op_AndI: case Op_XorI: case Op_OrI: {2886// Move "last use" input to left by swapping inputs2887n->swap_edges(1, 2);2888break;2889}2890default:2891break;2892}2893}28942895#ifdef ASSERT2896if( n->is_Mem() ) {2897int alias_idx = get_alias_index(n->as_Mem()->adr_type());2898assert( n->in(0) != NULL || alias_idx != Compile::AliasIdxRaw ||2899// oop will be recorded in oop map if load crosses safepoint2900n->is_Load() && (n->as_Load()->bottom_type()->isa_oopptr() ||2901LoadNode::is_immutable_value(n->in(MemNode::Address))),2902"raw memory operations should have control edge");2903}2904if (n->is_MemBar()) {2905MemBarNode* mb = n->as_MemBar();2906if (mb->trailing_store() || mb->trailing_load_store()) {2907assert(mb->leading_membar()->trailing_membar() == mb, "bad membar pair");2908Node* mem = BarrierSet::barrier_set()->barrier_set_c2()->step_over_gc_barrier(mb->in(MemBarNode::Precedent));2909assert((mb->trailing_store() && mem->is_Store() && mem->as_Store()->is_release()) ||2910(mb->trailing_load_store() && mem->is_LoadStore()), "missing mem op");2911} else if (mb->leading()) {2912assert(mb->trailing_membar()->leading_membar() == mb, "bad membar pair");2913}2914}2915#endif2916// Count FPU ops and common calls, implements item (3)2917bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->final_graph_reshaping(this, n, nop);2918if (!gc_handled) {2919final_graph_reshaping_main_switch(n, frc, nop);2920}29212922// Collect CFG split points2923if (n->is_MultiBranch() && !n->is_RangeCheck()) {2924frc._tests.push(n);2925}2926}29272928void Compile::final_graph_reshaping_main_switch(Node* n, Final_Reshape_Counts& frc, uint nop) {2929switch( nop ) {2930// Count all float operations that may use FPU2931case Op_AddF:2932case Op_SubF:2933case Op_MulF:2934case Op_DivF:2935case Op_NegF:2936case Op_ModF:2937case Op_ConvI2F:2938case Op_ConF:2939case Op_CmpF:2940case Op_CmpF3:2941case Op_StoreF:2942case Op_LoadF:2943// case Op_ConvL2F: // longs are split into 32-bit halves2944frc.inc_float_count();2945break;29462947case Op_ConvF2D:2948case Op_ConvD2F:2949frc.inc_float_count();2950frc.inc_double_count();2951break;29522953// Count all double operations that may use FPU2954case Op_AddD:2955case Op_SubD:2956case Op_MulD:2957case Op_DivD:2958case Op_NegD:2959case Op_ModD:2960case Op_ConvI2D:2961case Op_ConvD2I:2962// case Op_ConvL2D: // handled by leaf call2963// case Op_ConvD2L: // handled by leaf call2964case Op_ConD:2965case Op_CmpD:2966case Op_CmpD3:2967case Op_StoreD:2968case Op_LoadD:2969case Op_LoadD_unaligned:2970frc.inc_double_count();2971break;2972case Op_Opaque1: // Remove Opaque Nodes before matching2973case Op_Opaque2: // Remove Opaque Nodes before matching2974case Op_Opaque3:2975n->subsume_by(n->in(1), this);2976break;2977case Op_CallStaticJava:2978case Op_CallJava:2979case Op_CallDynamicJava:2980frc.inc_java_call_count(); // Count java call site;2981case Op_CallRuntime:2982case Op_CallLeaf:2983case Op_CallLeafVector:2984case Op_CallNative:2985case Op_CallLeafNoFP: {2986assert (n->is_Call(), "");2987CallNode *call = n->as_Call();2988// Count call sites where the FP mode bit would have to be flipped.2989// Do not count uncommon runtime calls:2990// uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,2991// _new_Java, _new_typeArray, _new_objArray, _rethrow_Java, ...2992if (!call->is_CallStaticJava() || !call->as_CallStaticJava()->_name) {2993frc.inc_call_count(); // Count the call site2994} else { // See if uncommon argument is shared2995Node *n = call->in(TypeFunc::Parms);2996int nop = n->Opcode();2997// Clone shared simple arguments to uncommon calls, item (1).2998if (n->outcnt() > 1 &&2999!n->is_Proj() &&3000nop != Op_CreateEx &&3001nop != Op_CheckCastPP &&3002nop != Op_DecodeN &&3003nop != Op_DecodeNKlass &&3004!n->is_Mem() &&3005!n->is_Phi()) {3006Node *x = n->clone();3007call->set_req(TypeFunc::Parms, x);3008}3009}3010break;3011}30123013case Op_StoreCM:3014{3015// Convert OopStore dependence into precedence edge3016Node* prec = n->in(MemNode::OopStore);3017n->del_req(MemNode::OopStore);3018n->add_prec(prec);3019eliminate_redundant_card_marks(n);3020}30213022// fall through30233024case Op_StoreB:3025case Op_StoreC:3026case Op_StorePConditional:3027case Op_StoreI:3028case Op_StoreL:3029case Op_StoreIConditional:3030case Op_StoreLConditional:3031case Op_CompareAndSwapB:3032case Op_CompareAndSwapS:3033case Op_CompareAndSwapI:3034case Op_CompareAndSwapL:3035case Op_CompareAndSwapP:3036case Op_CompareAndSwapN:3037case Op_WeakCompareAndSwapB:3038case Op_WeakCompareAndSwapS:3039case Op_WeakCompareAndSwapI:3040case Op_WeakCompareAndSwapL:3041case Op_WeakCompareAndSwapP:3042case Op_WeakCompareAndSwapN:3043case Op_CompareAndExchangeB:3044case Op_CompareAndExchangeS:3045case Op_CompareAndExchangeI:3046case Op_CompareAndExchangeL:3047case Op_CompareAndExchangeP:3048case Op_CompareAndExchangeN:3049case Op_GetAndAddS:3050case Op_GetAndAddB:3051case Op_GetAndAddI:3052case Op_GetAndAddL:3053case Op_GetAndSetS:3054case Op_GetAndSetB:3055case Op_GetAndSetI:3056case Op_GetAndSetL:3057case Op_GetAndSetP:3058case Op_GetAndSetN:3059case Op_StoreP:3060case Op_StoreN:3061case Op_StoreNKlass:3062case Op_LoadB:3063case Op_LoadUB:3064case Op_LoadUS:3065case Op_LoadI:3066case Op_LoadKlass:3067case Op_LoadNKlass:3068case Op_LoadL:3069case Op_LoadL_unaligned:3070case Op_LoadPLocked:3071case Op_LoadP:3072case Op_LoadN:3073case Op_LoadRange:3074case Op_LoadS:3075break;30763077case Op_AddP: { // Assert sane base pointers3078Node *addp = n->in(AddPNode::Address);3079assert( !addp->is_AddP() ||3080addp->in(AddPNode::Base)->is_top() || // Top OK for allocation3081addp->in(AddPNode::Base) == n->in(AddPNode::Base),3082"Base pointers must match (addp %u)", addp->_idx );3083#ifdef _LP643084if ((UseCompressedOops || UseCompressedClassPointers) &&3085addp->Opcode() == Op_ConP &&3086addp == n->in(AddPNode::Base) &&3087n->in(AddPNode::Offset)->is_Con()) {3088// If the transformation of ConP to ConN+DecodeN is beneficial depends3089// on the platform and on the compressed oops mode.3090// Use addressing with narrow klass to load with offset on x86.3091// Some platforms can use the constant pool to load ConP.3092// Do this transformation here since IGVN will convert ConN back to ConP.3093const Type* t = addp->bottom_type();3094bool is_oop = t->isa_oopptr() != NULL;3095bool is_klass = t->isa_klassptr() != NULL;30963097if ((is_oop && Matcher::const_oop_prefer_decode() ) ||3098(is_klass && Matcher::const_klass_prefer_decode())) {3099Node* nn = NULL;31003101int op = is_oop ? Op_ConN : Op_ConNKlass;31023103// Look for existing ConN node of the same exact type.3104Node* r = root();3105uint cnt = r->outcnt();3106for (uint i = 0; i < cnt; i++) {3107Node* m = r->raw_out(i);3108if (m!= NULL && m->Opcode() == op &&3109m->bottom_type()->make_ptr() == t) {3110nn = m;3111break;3112}3113}3114if (nn != NULL) {3115// Decode a narrow oop to match address3116// [R12 + narrow_oop_reg<<3 + offset]3117if (is_oop) {3118nn = new DecodeNNode(nn, t);3119} else {3120nn = new DecodeNKlassNode(nn, t);3121}3122// Check for succeeding AddP which uses the same Base.3123// Otherwise we will run into the assertion above when visiting that guy.3124for (uint i = 0; i < n->outcnt(); ++i) {3125Node *out_i = n->raw_out(i);3126if (out_i && out_i->is_AddP() && out_i->in(AddPNode::Base) == addp) {3127out_i->set_req(AddPNode::Base, nn);3128#ifdef ASSERT3129for (uint j = 0; j < out_i->outcnt(); ++j) {3130Node *out_j = out_i->raw_out(j);3131assert(out_j == NULL || !out_j->is_AddP() || out_j->in(AddPNode::Base) != addp,3132"more than 2 AddP nodes in a chain (out_j %u)", out_j->_idx);3133}3134#endif3135}3136}3137n->set_req(AddPNode::Base, nn);3138n->set_req(AddPNode::Address, nn);3139if (addp->outcnt() == 0) {3140addp->disconnect_inputs(this);3141}3142}3143}3144}3145#endif3146break;3147}31483149case Op_CastPP: {3150// Remove CastPP nodes to gain more freedom during scheduling but3151// keep the dependency they encode as control or precedence edges3152// (if control is set already) on memory operations. Some CastPP3153// nodes don't have a control (don't carry a dependency): skip3154// those.3155if (n->in(0) != NULL) {3156ResourceMark rm;3157Unique_Node_List wq;3158wq.push(n);3159for (uint next = 0; next < wq.size(); ++next) {3160Node *m = wq.at(next);3161for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {3162Node* use = m->fast_out(i);3163if (use->is_Mem() || use->is_EncodeNarrowPtr()) {3164use->ensure_control_or_add_prec(n->in(0));3165} else {3166switch(use->Opcode()) {3167case Op_AddP:3168case Op_DecodeN:3169case Op_DecodeNKlass:3170case Op_CheckCastPP:3171case Op_CastPP:3172wq.push(use);3173break;3174}3175}3176}3177}3178}3179const bool is_LP64 = LP64_ONLY(true) NOT_LP64(false);3180if (is_LP64 && n->in(1)->is_DecodeN() && Matcher::gen_narrow_oop_implicit_null_checks()) {3181Node* in1 = n->in(1);3182const Type* t = n->bottom_type();3183Node* new_in1 = in1->clone();3184new_in1->as_DecodeN()->set_type(t);31853186if (!Matcher::narrow_oop_use_complex_address()) {3187//3188// x86, ARM and friends can handle 2 adds in addressing mode3189// and Matcher can fold a DecodeN node into address by using3190// a narrow oop directly and do implicit NULL check in address:3191//3192// [R12 + narrow_oop_reg<<3 + offset]3193// NullCheck narrow_oop_reg3194//3195// On other platforms (Sparc) we have to keep new DecodeN node and3196// use it to do implicit NULL check in address:3197//3198// decode_not_null narrow_oop_reg, base_reg3199// [base_reg + offset]3200// NullCheck base_reg3201//3202// Pin the new DecodeN node to non-null path on these platform (Sparc)3203// to keep the information to which NULL check the new DecodeN node3204// corresponds to use it as value in implicit_null_check().3205//3206new_in1->set_req(0, n->in(0));3207}32083209n->subsume_by(new_in1, this);3210if (in1->outcnt() == 0) {3211in1->disconnect_inputs(this);3212}3213} else {3214n->subsume_by(n->in(1), this);3215if (n->outcnt() == 0) {3216n->disconnect_inputs(this);3217}3218}3219break;3220}3221#ifdef _LP643222case Op_CmpP:3223// Do this transformation here to preserve CmpPNode::sub() and3224// other TypePtr related Ideal optimizations (for example, ptr nullness).3225if (n->in(1)->is_DecodeNarrowPtr() || n->in(2)->is_DecodeNarrowPtr()) {3226Node* in1 = n->in(1);3227Node* in2 = n->in(2);3228if (!in1->is_DecodeNarrowPtr()) {3229in2 = in1;3230in1 = n->in(2);3231}3232assert(in1->is_DecodeNarrowPtr(), "sanity");32333234Node* new_in2 = NULL;3235if (in2->is_DecodeNarrowPtr()) {3236assert(in2->Opcode() == in1->Opcode(), "must be same node type");3237new_in2 = in2->in(1);3238} else if (in2->Opcode() == Op_ConP) {3239const Type* t = in2->bottom_type();3240if (t == TypePtr::NULL_PTR) {3241assert(in1->is_DecodeN(), "compare klass to null?");3242// Don't convert CmpP null check into CmpN if compressed3243// oops implicit null check is not generated.3244// This will allow to generate normal oop implicit null check.3245if (Matcher::gen_narrow_oop_implicit_null_checks())3246new_in2 = ConNode::make(TypeNarrowOop::NULL_PTR);3247//3248// This transformation together with CastPP transformation above3249// will generated code for implicit NULL checks for compressed oops.3250//3251// The original code after Optimize()3252//3253// LoadN memory, narrow_oop_reg3254// decode narrow_oop_reg, base_reg3255// CmpP base_reg, NULL3256// CastPP base_reg // NotNull3257// Load [base_reg + offset], val_reg3258//3259// after these transformations will be3260//3261// LoadN memory, narrow_oop_reg3262// CmpN narrow_oop_reg, NULL3263// decode_not_null narrow_oop_reg, base_reg3264// Load [base_reg + offset], val_reg3265//3266// and the uncommon path (== NULL) will use narrow_oop_reg directly3267// since narrow oops can be used in debug info now (see the code in3268// final_graph_reshaping_walk()).3269//3270// At the end the code will be matched to3271// on x86:3272//3273// Load_narrow_oop memory, narrow_oop_reg3274// Load [R12 + narrow_oop_reg<<3 + offset], val_reg3275// NullCheck narrow_oop_reg3276//3277// and on sparc:3278//3279// Load_narrow_oop memory, narrow_oop_reg3280// decode_not_null narrow_oop_reg, base_reg3281// Load [base_reg + offset], val_reg3282// NullCheck base_reg3283//3284} else if (t->isa_oopptr()) {3285new_in2 = ConNode::make(t->make_narrowoop());3286} else if (t->isa_klassptr()) {3287new_in2 = ConNode::make(t->make_narrowklass());3288}3289}3290if (new_in2 != NULL) {3291Node* cmpN = new CmpNNode(in1->in(1), new_in2);3292n->subsume_by(cmpN, this);3293if (in1->outcnt() == 0) {3294in1->disconnect_inputs(this);3295}3296if (in2->outcnt() == 0) {3297in2->disconnect_inputs(this);3298}3299}3300}3301break;33023303case Op_DecodeN:3304case Op_DecodeNKlass:3305assert(!n->in(1)->is_EncodeNarrowPtr(), "should be optimized out");3306// DecodeN could be pinned when it can't be fold into3307// an address expression, see the code for Op_CastPP above.3308assert(n->in(0) == NULL || (UseCompressedOops && !Matcher::narrow_oop_use_complex_address()), "no control");3309break;33103311case Op_EncodeP:3312case Op_EncodePKlass: {3313Node* in1 = n->in(1);3314if (in1->is_DecodeNarrowPtr()) {3315n->subsume_by(in1->in(1), this);3316} else if (in1->Opcode() == Op_ConP) {3317const Type* t = in1->bottom_type();3318if (t == TypePtr::NULL_PTR) {3319assert(t->isa_oopptr(), "null klass?");3320n->subsume_by(ConNode::make(TypeNarrowOop::NULL_PTR), this);3321} else if (t->isa_oopptr()) {3322n->subsume_by(ConNode::make(t->make_narrowoop()), this);3323} else if (t->isa_klassptr()) {3324n->subsume_by(ConNode::make(t->make_narrowklass()), this);3325}3326}3327if (in1->outcnt() == 0) {3328in1->disconnect_inputs(this);3329}3330break;3331}33323333case Op_Proj: {3334if (OptimizeStringConcat || IncrementalInline) {3335ProjNode* proj = n->as_Proj();3336if (proj->_is_io_use) {3337assert(proj->_con == TypeFunc::I_O || proj->_con == TypeFunc::Memory, "");3338// Separate projections were used for the exception path which3339// are normally removed by a late inline. If it wasn't inlined3340// then they will hang around and should just be replaced with3341// the original one. Merge them.3342Node* non_io_proj = proj->in(0)->as_Multi()->proj_out_or_null(proj->_con, false /*is_io_use*/);3343if (non_io_proj != NULL) {3344proj->subsume_by(non_io_proj , this);3345}3346}3347}3348break;3349}33503351case Op_Phi:3352if (n->as_Phi()->bottom_type()->isa_narrowoop() || n->as_Phi()->bottom_type()->isa_narrowklass()) {3353// The EncodeP optimization may create Phi with the same edges3354// for all paths. It is not handled well by Register Allocator.3355Node* unique_in = n->in(1);3356assert(unique_in != NULL, "");3357uint cnt = n->req();3358for (uint i = 2; i < cnt; i++) {3359Node* m = n->in(i);3360assert(m != NULL, "");3361if (unique_in != m)3362unique_in = NULL;3363}3364if (unique_in != NULL) {3365n->subsume_by(unique_in, this);3366}3367}3368break;33693370#endif33713372#ifdef ASSERT3373case Op_CastII:3374// Verify that all range check dependent CastII nodes were removed.3375if (n->isa_CastII()->has_range_check()) {3376n->dump(3);3377assert(false, "Range check dependent CastII node was not removed");3378}3379break;3380#endif33813382case Op_ModI:3383if (UseDivMod) {3384// Check if a%b and a/b both exist3385Node* d = n->find_similar(Op_DivI);3386if (d) {3387// Replace them with a fused divmod if supported3388if (Matcher::has_match_rule(Op_DivModI)) {3389DivModINode* divmod = DivModINode::make(n);3390d->subsume_by(divmod->div_proj(), this);3391n->subsume_by(divmod->mod_proj(), this);3392} else {3393// replace a%b with a-((a/b)*b)3394Node* mult = new MulINode(d, d->in(2));3395Node* sub = new SubINode(d->in(1), mult);3396n->subsume_by(sub, this);3397}3398}3399}3400break;34013402case Op_ModL:3403if (UseDivMod) {3404// Check if a%b and a/b both exist3405Node* d = n->find_similar(Op_DivL);3406if (d) {3407// Replace them with a fused divmod if supported3408if (Matcher::has_match_rule(Op_DivModL)) {3409DivModLNode* divmod = DivModLNode::make(n);3410d->subsume_by(divmod->div_proj(), this);3411n->subsume_by(divmod->mod_proj(), this);3412} else {3413// replace a%b with a-((a/b)*b)3414Node* mult = new MulLNode(d, d->in(2));3415Node* sub = new SubLNode(d->in(1), mult);3416n->subsume_by(sub, this);3417}3418}3419}3420break;34213422case Op_LoadVector:3423case Op_StoreVector:3424case Op_LoadVectorGather:3425case Op_StoreVectorScatter:3426case Op_VectorCmpMasked:3427case Op_VectorMaskGen:3428case Op_LoadVectorMasked:3429case Op_StoreVectorMasked:3430break;34313432case Op_AddReductionVI:3433case Op_AddReductionVL:3434case Op_AddReductionVF:3435case Op_AddReductionVD:3436case Op_MulReductionVI:3437case Op_MulReductionVL:3438case Op_MulReductionVF:3439case Op_MulReductionVD:3440case Op_MinReductionV:3441case Op_MaxReductionV:3442case Op_AndReductionV:3443case Op_OrReductionV:3444case Op_XorReductionV:3445break;34463447case Op_PackB:3448case Op_PackS:3449case Op_PackI:3450case Op_PackF:3451case Op_PackL:3452case Op_PackD:3453if (n->req()-1 > 2) {3454// Replace many operand PackNodes with a binary tree for matching3455PackNode* p = (PackNode*) n;3456Node* btp = p->binary_tree_pack(1, n->req());3457n->subsume_by(btp, this);3458}3459break;3460case Op_Loop:3461assert(!n->as_Loop()->is_transformed_long_inner_loop() || _loop_opts_cnt == 0, "should have been turned into a counted loop");3462case Op_CountedLoop:3463case Op_LongCountedLoop:3464case Op_OuterStripMinedLoop:3465if (n->as_Loop()->is_inner_loop()) {3466frc.inc_inner_loop_count();3467}3468n->as_Loop()->verify_strip_mined(0);3469break;3470case Op_LShiftI:3471case Op_RShiftI:3472case Op_URShiftI:3473case Op_LShiftL:3474case Op_RShiftL:3475case Op_URShiftL:3476if (Matcher::need_masked_shift_count) {3477// The cpu's shift instructions don't restrict the count to the3478// lower 5/6 bits. We need to do the masking ourselves.3479Node* in2 = n->in(2);3480juint mask = (n->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1);3481const TypeInt* t = in2->find_int_type();3482if (t != NULL && t->is_con()) {3483juint shift = t->get_con();3484if (shift > mask) { // Unsigned cmp3485n->set_req(2, ConNode::make(TypeInt::make(shift & mask)));3486}3487} else {3488if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {3489Node* shift = new AndINode(in2, ConNode::make(TypeInt::make(mask)));3490n->set_req(2, shift);3491}3492}3493if (in2->outcnt() == 0) { // Remove dead node3494in2->disconnect_inputs(this);3495}3496}3497break;3498case Op_MemBarStoreStore:3499case Op_MemBarRelease:3500// Break the link with AllocateNode: it is no longer useful and3501// confuses register allocation.3502if (n->req() > MemBarNode::Precedent) {3503n->set_req(MemBarNode::Precedent, top());3504}3505break;3506case Op_MemBarAcquire: {3507if (n->as_MemBar()->trailing_load() && n->req() > MemBarNode::Precedent) {3508// At parse time, the trailing MemBarAcquire for a volatile load3509// is created with an edge to the load. After optimizations,3510// that input may be a chain of Phis. If those phis have no3511// other use, then the MemBarAcquire keeps them alive and3512// register allocation can be confused.3513ResourceMark rm;3514Unique_Node_List wq;3515wq.push(n->in(MemBarNode::Precedent));3516n->set_req(MemBarNode::Precedent, top());3517while (wq.size() > 0) {3518Node* m = wq.pop();3519if (m->outcnt() == 0 && m != top()) {3520for (uint j = 0; j < m->req(); j++) {3521Node* in = m->in(j);3522if (in != NULL) {3523wq.push(in);3524}3525}3526m->disconnect_inputs(this);3527}3528}3529}3530break;3531}3532case Op_Blackhole:3533break;3534case Op_RangeCheck: {3535RangeCheckNode* rc = n->as_RangeCheck();3536Node* iff = new IfNode(rc->in(0), rc->in(1), rc->_prob, rc->_fcnt);3537n->subsume_by(iff, this);3538frc._tests.push(iff);3539break;3540}3541case Op_ConvI2L: {3542if (!Matcher::convi2l_type_required) {3543// Code generation on some platforms doesn't need accurate3544// ConvI2L types. Widening the type can help remove redundant3545// address computations.3546n->as_Type()->set_type(TypeLong::INT);3547ResourceMark rm;3548Unique_Node_List wq;3549wq.push(n);3550for (uint next = 0; next < wq.size(); next++) {3551Node *m = wq.at(next);35523553for(;;) {3554// Loop over all nodes with identical inputs edges as m3555Node* k = m->find_similar(m->Opcode());3556if (k == NULL) {3557break;3558}3559// Push their uses so we get a chance to remove node made3560// redundant3561for (DUIterator_Fast imax, i = k->fast_outs(imax); i < imax; i++) {3562Node* u = k->fast_out(i);3563if (u->Opcode() == Op_LShiftL ||3564u->Opcode() == Op_AddL ||3565u->Opcode() == Op_SubL ||3566u->Opcode() == Op_AddP) {3567wq.push(u);3568}3569}3570// Replace all nodes with identical edges as m with m3571k->subsume_by(m, this);3572}3573}3574}3575break;3576}3577case Op_CmpUL: {3578if (!Matcher::has_match_rule(Op_CmpUL)) {3579// No support for unsigned long comparisons3580ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));3581Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);3582Node* orl = new OrLNode(n->in(1), sign_bit_mask);3583ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));3584Node* andl = new AndLNode(orl, remove_sign_mask);3585Node* cmp = new CmpLNode(andl, n->in(2));3586n->subsume_by(cmp, this);3587}3588break;3589}3590default:3591assert(!n->is_Call(), "");3592assert(!n->is_Mem(), "");3593assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");3594break;3595}3596}35973598//------------------------------final_graph_reshaping_walk---------------------3599// Replacing Opaque nodes with their input in final_graph_reshaping_impl(),3600// requires that the walk visits a node's inputs before visiting the node.3601void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {3602Unique_Node_List sfpt;36033604frc._visited.set(root->_idx); // first, mark node as visited3605uint cnt = root->req();3606Node *n = root;3607uint i = 0;3608while (true) {3609if (i < cnt) {3610// Place all non-visited non-null inputs onto stack3611Node* m = n->in(i);3612++i;3613if (m != NULL && !frc._visited.test_set(m->_idx)) {3614if (m->is_SafePoint() && m->as_SafePoint()->jvms() != NULL) {3615// compute worst case interpreter size in case of a deoptimization3616update_interpreter_frame_size(m->as_SafePoint()->jvms()->interpreter_frame_size());36173618sfpt.push(m);3619}3620cnt = m->req();3621nstack.push(n, i); // put on stack parent and next input's index3622n = m;3623i = 0;3624}3625} else {3626// Now do post-visit work3627final_graph_reshaping_impl( n, frc );3628if (nstack.is_empty())3629break; // finished3630n = nstack.node(); // Get node from stack3631cnt = n->req();3632i = nstack.index();3633nstack.pop(); // Shift to the next node on stack3634}3635}36363637// Skip next transformation if compressed oops are not used.3638if ((UseCompressedOops && !Matcher::gen_narrow_oop_implicit_null_checks()) ||3639(!UseCompressedOops && !UseCompressedClassPointers))3640return;36413642// Go over safepoints nodes to skip DecodeN/DecodeNKlass nodes for debug edges.3643// It could be done for an uncommon traps or any safepoints/calls3644// if the DecodeN/DecodeNKlass node is referenced only in a debug info.3645while (sfpt.size() > 0) {3646n = sfpt.pop();3647JVMState *jvms = n->as_SafePoint()->jvms();3648assert(jvms != NULL, "sanity");3649int start = jvms->debug_start();3650int end = n->req();3651bool is_uncommon = (n->is_CallStaticJava() &&3652n->as_CallStaticJava()->uncommon_trap_request() != 0);3653for (int j = start; j < end; j++) {3654Node* in = n->in(j);3655if (in->is_DecodeNarrowPtr()) {3656bool safe_to_skip = true;3657if (!is_uncommon ) {3658// Is it safe to skip?3659for (uint i = 0; i < in->outcnt(); i++) {3660Node* u = in->raw_out(i);3661if (!u->is_SafePoint() ||3662(u->is_Call() && u->as_Call()->has_non_debug_use(n))) {3663safe_to_skip = false;3664}3665}3666}3667if (safe_to_skip) {3668n->set_req(j, in->in(1));3669}3670if (in->outcnt() == 0) {3671in->disconnect_inputs(this);3672}3673}3674}3675}3676}36773678//------------------------------final_graph_reshaping--------------------------3679// Final Graph Reshaping.3680//3681// (1) Clone simple inputs to uncommon calls, so they can be scheduled late3682// and not commoned up and forced early. Must come after regular3683// optimizations to avoid GVN undoing the cloning. Clone constant3684// inputs to Loop Phis; these will be split by the allocator anyways.3685// Remove Opaque nodes.3686// (2) Move last-uses by commutative operations to the left input to encourage3687// Intel update-in-place two-address operations and better register usage3688// on RISCs. Must come after regular optimizations to avoid GVN Ideal3689// calls canonicalizing them back.3690// (3) Count the number of double-precision FP ops, single-precision FP ops3691// and call sites. On Intel, we can get correct rounding either by3692// forcing singles to memory (requires extra stores and loads after each3693// FP bytecode) or we can set a rounding mode bit (requires setting and3694// clearing the mode bit around call sites). The mode bit is only used3695// if the relative frequency of single FP ops to calls is low enough.3696// This is a key transform for SPEC mpeg_audio.3697// (4) Detect infinite loops; blobs of code reachable from above but not3698// below. Several of the Code_Gen algorithms fail on such code shapes,3699// so we simply bail out. Happens a lot in ZKM.jar, but also happens3700// from time to time in other codes (such as -Xcomp finalizer loops, etc).3701// Detection is by looking for IfNodes where only 1 projection is3702// reachable from below or CatchNodes missing some targets.3703// (5) Assert for insane oop offsets in debug mode.37043705bool Compile::final_graph_reshaping() {3706// an infinite loop may have been eliminated by the optimizer,3707// in which case the graph will be empty.3708if (root()->req() == 1) {3709record_method_not_compilable("trivial infinite loop");3710return true;3711}37123713// Expensive nodes have their control input set to prevent the GVN3714// from freely commoning them. There's no GVN beyond this point so3715// no need to keep the control input. We want the expensive nodes to3716// be freely moved to the least frequent code path by gcm.3717assert(OptimizeExpensiveOps || expensive_count() == 0, "optimization off but list non empty?");3718for (int i = 0; i < expensive_count(); i++) {3719_expensive_nodes.at(i)->set_req(0, NULL);3720}37213722Final_Reshape_Counts frc;37233724// Visit everybody reachable!3725// Allocate stack of size C->live_nodes()/2 to avoid frequent realloc3726Node_Stack nstack(live_nodes() >> 1);3727final_graph_reshaping_walk(nstack, root(), frc);37283729// Check for unreachable (from below) code (i.e., infinite loops).3730for( uint i = 0; i < frc._tests.size(); i++ ) {3731MultiBranchNode *n = frc._tests[i]->as_MultiBranch();3732// Get number of CFG targets.3733// Note that PCTables include exception targets after calls.3734uint required_outcnt = n->required_outcnt();3735if (n->outcnt() != required_outcnt) {3736// Check for a few special cases. Rethrow Nodes never take the3737// 'fall-thru' path, so expected kids is 1 less.3738if (n->is_PCTable() && n->in(0) && n->in(0)->in(0)) {3739if (n->in(0)->in(0)->is_Call()) {3740CallNode* call = n->in(0)->in(0)->as_Call();3741if (call->entry_point() == OptoRuntime::rethrow_stub()) {3742required_outcnt--; // Rethrow always has 1 less kid3743} else if (call->req() > TypeFunc::Parms &&3744call->is_CallDynamicJava()) {3745// Check for null receiver. In such case, the optimizer has3746// detected that the virtual call will always result in a null3747// pointer exception. The fall-through projection of this CatchNode3748// will not be populated.3749Node* arg0 = call->in(TypeFunc::Parms);3750if (arg0->is_Type() &&3751arg0->as_Type()->type()->higher_equal(TypePtr::NULL_PTR)) {3752required_outcnt--;3753}3754} else if (call->entry_point() == OptoRuntime::new_array_Java() ||3755call->entry_point() == OptoRuntime::new_array_nozero_Java()) {3756// Check for illegal array length. In such case, the optimizer has3757// detected that the allocation attempt will always result in an3758// exception. There is no fall-through projection of this CatchNode .3759assert(call->is_CallStaticJava(), "static call expected");3760assert(call->req() == call->jvms()->endoff() + 1, "missing extra input");3761Node* valid_length_test = call->in(call->req()-1);3762call->del_req(call->req()-1);3763if (valid_length_test->find_int_con(1) == 0) {3764required_outcnt--;3765}3766assert(n->outcnt() == required_outcnt, "malformed control flow");3767continue;3768}3769}3770}3771// Recheck with a better notion of 'required_outcnt'3772if (n->outcnt() != required_outcnt) {3773record_method_not_compilable("malformed control flow");3774return true; // Not all targets reachable!3775}3776} else if (n->is_PCTable() && n->in(0) && n->in(0)->in(0) && n->in(0)->in(0)->is_Call()) {3777CallNode* call = n->in(0)->in(0)->as_Call();3778if (call->entry_point() == OptoRuntime::new_array_Java() ||3779call->entry_point() == OptoRuntime::new_array_nozero_Java()) {3780assert(call->is_CallStaticJava(), "static call expected");3781assert(call->req() == call->jvms()->endoff() + 1, "missing extra input");3782call->del_req(call->req()-1); // valid length test useless now3783}3784}3785// Check that I actually visited all kids. Unreached kids3786// must be infinite loops.3787for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++)3788if (!frc._visited.test(n->fast_out(j)->_idx)) {3789record_method_not_compilable("infinite loop");3790return true; // Found unvisited kid; must be unreach3791}37923793// Here so verification code in final_graph_reshaping_walk()3794// always see an OuterStripMinedLoopEnd3795if (n->is_OuterStripMinedLoopEnd() || n->is_LongCountedLoopEnd()) {3796IfNode* init_iff = n->as_If();3797Node* iff = new IfNode(init_iff->in(0), init_iff->in(1), init_iff->_prob, init_iff->_fcnt);3798n->subsume_by(iff, this);3799}3800}38013802#ifdef IA323803// If original bytecodes contained a mixture of floats and doubles3804// check if the optimizer has made it homogenous, item (3).3805if (UseSSE == 0 &&3806frc.get_float_count() > 32 &&3807frc.get_double_count() == 0 &&3808(10 * frc.get_call_count() < frc.get_float_count()) ) {3809set_24_bit_selection_and_mode(false, true);3810}3811#endif // IA3238123813set_java_calls(frc.get_java_call_count());3814set_inner_loops(frc.get_inner_loop_count());38153816// No infinite loops, no reason to bail out.3817return false;3818}38193820//-----------------------------too_many_traps----------------------------------3821// Report if there are too many traps at the current method and bci.3822// Return true if there was a trap, and/or PerMethodTrapLimit is exceeded.3823bool Compile::too_many_traps(ciMethod* method,3824int bci,3825Deoptimization::DeoptReason reason) {3826ciMethodData* md = method->method_data();3827if (md->is_empty()) {3828// Assume the trap has not occurred, or that it occurred only3829// because of a transient condition during start-up in the interpreter.3830return false;3831}3832ciMethod* m = Deoptimization::reason_is_speculate(reason) ? this->method() : NULL;3833if (md->has_trap_at(bci, m, reason) != 0) {3834// Assume PerBytecodeTrapLimit==0, for a more conservative heuristic.3835// Also, if there are multiple reasons, or if there is no per-BCI record,3836// assume the worst.3837if (log())3838log()->elem("observe trap='%s' count='%d'",3839Deoptimization::trap_reason_name(reason),3840md->trap_count(reason));3841return true;3842} else {3843// Ignore method/bci and see if there have been too many globally.3844return too_many_traps(reason, md);3845}3846}38473848// Less-accurate variant which does not require a method and bci.3849bool Compile::too_many_traps(Deoptimization::DeoptReason reason,3850ciMethodData* logmd) {3851if (trap_count(reason) >= Deoptimization::per_method_trap_limit(reason)) {3852// Too many traps globally.3853// Note that we use cumulative trap_count, not just md->trap_count.3854if (log()) {3855int mcount = (logmd == NULL)? -1: (int)logmd->trap_count(reason);3856log()->elem("observe trap='%s' count='0' mcount='%d' ccount='%d'",3857Deoptimization::trap_reason_name(reason),3858mcount, trap_count(reason));3859}3860return true;3861} else {3862// The coast is clear.3863return false;3864}3865}38663867//--------------------------too_many_recompiles--------------------------------3868// Report if there are too many recompiles at the current method and bci.3869// Consults PerBytecodeRecompilationCutoff and PerMethodRecompilationCutoff.3870// Is not eager to return true, since this will cause the compiler to use3871// Action_none for a trap point, to avoid too many recompilations.3872bool Compile::too_many_recompiles(ciMethod* method,3873int bci,3874Deoptimization::DeoptReason reason) {3875ciMethodData* md = method->method_data();3876if (md->is_empty()) {3877// Assume the trap has not occurred, or that it occurred only3878// because of a transient condition during start-up in the interpreter.3879return false;3880}3881// Pick a cutoff point well within PerBytecodeRecompilationCutoff.3882uint bc_cutoff = (uint) PerBytecodeRecompilationCutoff / 8;3883uint m_cutoff = (uint) PerMethodRecompilationCutoff / 2 + 1; // not zero3884Deoptimization::DeoptReason per_bc_reason3885= Deoptimization::reason_recorded_per_bytecode_if_any(reason);3886ciMethod* m = Deoptimization::reason_is_speculate(reason) ? this->method() : NULL;3887if ((per_bc_reason == Deoptimization::Reason_none3888|| md->has_trap_at(bci, m, reason) != 0)3889// The trap frequency measure we care about is the recompile count:3890&& md->trap_recompiled_at(bci, m)3891&& md->overflow_recompile_count() >= bc_cutoff) {3892// Do not emit a trap here if it has already caused recompilations.3893// Also, if there are multiple reasons, or if there is no per-BCI record,3894// assume the worst.3895if (log())3896log()->elem("observe trap='%s recompiled' count='%d' recompiles2='%d'",3897Deoptimization::trap_reason_name(reason),3898md->trap_count(reason),3899md->overflow_recompile_count());3900return true;3901} else if (trap_count(reason) != 03902&& decompile_count() >= m_cutoff) {3903// Too many recompiles globally, and we have seen this sort of trap.3904// Use cumulative decompile_count, not just md->decompile_count.3905if (log())3906log()->elem("observe trap='%s' count='%d' mcount='%d' decompiles='%d' mdecompiles='%d'",3907Deoptimization::trap_reason_name(reason),3908md->trap_count(reason), trap_count(reason),3909md->decompile_count(), decompile_count());3910return true;3911} else {3912// The coast is clear.3913return false;3914}3915}39163917// Compute when not to trap. Used by matching trap based nodes and3918// NullCheck optimization.3919void Compile::set_allowed_deopt_reasons() {3920_allowed_reasons = 0;3921if (is_method_compilation()) {3922for (int rs = (int)Deoptimization::Reason_none+1; rs < Compile::trapHistLength; rs++) {3923assert(rs < BitsPerInt, "recode bit map");3924if (!too_many_traps((Deoptimization::DeoptReason) rs)) {3925_allowed_reasons |= nth_bit(rs);3926}3927}3928}3929}39303931bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {3932return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);3933}39343935bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {3936return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);3937}39383939bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {3940if (holder->is_initialized()) {3941return false;3942}3943if (holder->is_being_initialized()) {3944if (accessing_method->holder() == holder) {3945// Access inside a class. The barrier can be elided when access happens in <clinit>,3946// <init>, or a static method. In all those cases, there was an initialization3947// barrier on the holder klass passed.3948if (accessing_method->is_static_initializer() ||3949accessing_method->is_object_initializer() ||3950accessing_method->is_static()) {3951return false;3952}3953} else if (accessing_method->holder()->is_subclass_of(holder)) {3954// Access from a subclass. The barrier can be elided only when access happens in <clinit>.3955// In case of <init> or a static method, the barrier is on the subclass is not enough:3956// child class can become fully initialized while its parent class is still being initialized.3957if (accessing_method->is_static_initializer()) {3958return false;3959}3960}3961ciMethod* root = method(); // the root method of compilation3962if (root != accessing_method) {3963return needs_clinit_barrier(holder, root); // check access in the context of compilation root3964}3965}3966return true;3967}39683969#ifndef PRODUCT3970//------------------------------verify_graph_edges---------------------------3971// Walk the Graph and verify that there is a one-to-one correspondence3972// between Use-Def edges and Def-Use edges in the graph.3973void Compile::verify_graph_edges(bool no_dead_code) {3974if (VerifyGraphEdges) {3975Unique_Node_List visited;3976// Call recursive graph walk to check edges3977_root->verify_edges(visited);3978if (no_dead_code) {3979// Now make sure that no visited node is used by an unvisited node.3980bool dead_nodes = false;3981Unique_Node_List checked;3982while (visited.size() > 0) {3983Node* n = visited.pop();3984checked.push(n);3985for (uint i = 0; i < n->outcnt(); i++) {3986Node* use = n->raw_out(i);3987if (checked.member(use)) continue; // already checked3988if (visited.member(use)) continue; // already in the graph3989if (use->is_Con()) continue; // a dead ConNode is OK3990// At this point, we have found a dead node which is DU-reachable.3991if (!dead_nodes) {3992tty->print_cr("*** Dead nodes reachable via DU edges:");3993dead_nodes = true;3994}3995use->dump(2);3996tty->print_cr("---");3997checked.push(use); // No repeats; pretend it is now checked.3998}3999}4000assert(!dead_nodes, "using nodes must be reachable from root");4001}4002}4003}4004#endif40054006// The Compile object keeps track of failure reasons separately from the ciEnv.4007// This is required because there is not quite a 1-1 relation between the4008// ciEnv and its compilation task and the Compile object. Note that one4009// ciEnv might use two Compile objects, if C2Compiler::compile_method decides4010// to backtrack and retry without subsuming loads. Other than this backtracking4011// behavior, the Compile's failure reason is quietly copied up to the ciEnv4012// by the logic in C2Compiler.4013void Compile::record_failure(const char* reason) {4014if (log() != NULL) {4015log()->elem("failure reason='%s' phase='compile'", reason);4016}4017if (_failure_reason == NULL) {4018// Record the first failure reason.4019_failure_reason = reason;4020}40214022if (!C->failure_reason_is(C2Compiler::retry_no_subsuming_loads())) {4023C->print_method(PHASE_FAILURE);4024}4025_root = NULL; // flush the graph, too4026}40274028Compile::TracePhase::TracePhase(const char* name, elapsedTimer* accumulator)4029: TraceTime(name, accumulator, CITime, CITimeVerbose),4030_phase_name(name), _dolog(CITimeVerbose)4031{4032if (_dolog) {4033C = Compile::current();4034_log = C->log();4035} else {4036C = NULL;4037_log = NULL;4038}4039if (_log != NULL) {4040_log->begin_head("phase name='%s' nodes='%d' live='%d'", _phase_name, C->unique(), C->live_nodes());4041_log->stamp();4042_log->end_head();4043}4044}40454046Compile::TracePhase::~TracePhase() {40474048C = Compile::current();4049if (_dolog) {4050_log = C->log();4051} else {4052_log = NULL;4053}40544055#ifdef ASSERT4056if (PrintIdealNodeCount) {4057tty->print_cr("phase name='%s' nodes='%d' live='%d' live_graph_walk='%d'",4058_phase_name, C->unique(), C->live_nodes(), C->count_live_nodes_by_graph_walk());4059}40604061if (VerifyIdealNodeCount) {4062Compile::current()->print_missing_nodes();4063}4064#endif40654066if (_log != NULL) {4067_log->done("phase name='%s' nodes='%d' live='%d'", _phase_name, C->unique(), C->live_nodes());4068}4069}40704071//----------------------------static_subtype_check-----------------------------4072// Shortcut important common cases when superklass is exact:4073// (0) superklass is java.lang.Object (can occur in reflective code)4074// (1) subklass is already limited to a subtype of superklass => always ok4075// (2) subklass does not overlap with superklass => always fail4076// (3) superklass has NO subtypes and we can check with a simple compare.4077int Compile::static_subtype_check(ciKlass* superk, ciKlass* subk) {4078if (StressReflectiveCode) {4079return SSC_full_test; // Let caller generate the general case.4080}40814082if (superk == env()->Object_klass()) {4083return SSC_always_true; // (0) this test cannot fail4084}40854086ciType* superelem = superk;4087ciType* subelem = subk;4088if (superelem->is_array_klass()) {4089superelem = superelem->as_array_klass()->base_element_type();4090}4091if (subelem->is_array_klass()) {4092subelem = subelem->as_array_klass()->base_element_type();4093}40944095if (!subk->is_interface()) { // cannot trust static interface types yet4096if (subk->is_subtype_of(superk)) {4097return SSC_always_true; // (1) false path dead; no dynamic test needed4098}4099if (!(superelem->is_klass() && superelem->as_klass()->is_interface()) &&4100!(subelem->is_klass() && subelem->as_klass()->is_interface()) &&4101!superk->is_subtype_of(subk)) {4102return SSC_always_false; // (2) true path dead; no dynamic test needed4103}4104}41054106// If casting to an instance klass, it must have no subtypes4107if (superk->is_interface()) {4108// Cannot trust interfaces yet.4109// %%% S.B. superk->nof_implementors() == 14110} else if (superelem->is_instance_klass()) {4111ciInstanceKlass* ik = superelem->as_instance_klass();4112if (!ik->has_subklass() && !ik->is_interface()) {4113if (!ik->is_final()) {4114// Add a dependency if there is a chance of a later subclass.4115dependencies()->assert_leaf_type(ik);4116}4117return SSC_easy_test; // (3) caller can do a simple ptr comparison4118}4119} else {4120// A primitive array type has no subtypes.4121return SSC_easy_test; // (3) caller can do a simple ptr comparison4122}41234124return SSC_full_test;4125}41264127Node* Compile::conv_I2X_index(PhaseGVN* phase, Node* idx, const TypeInt* sizetype, Node* ctrl) {4128#ifdef _LP644129// The scaled index operand to AddP must be a clean 64-bit value.4130// Java allows a 32-bit int to be incremented to a negative4131// value, which appears in a 64-bit register as a large4132// positive number. Using that large positive number as an4133// operand in pointer arithmetic has bad consequences.4134// On the other hand, 32-bit overflow is rare, and the possibility4135// can often be excluded, if we annotate the ConvI2L node with4136// a type assertion that its value is known to be a small positive4137// number. (The prior range check has ensured this.)4138// This assertion is used by ConvI2LNode::Ideal.4139int index_max = max_jint - 1; // array size is max_jint, index is one less4140if (sizetype != NULL) index_max = sizetype->_hi - 1;4141const TypeInt* iidxtype = TypeInt::make(0, index_max, Type::WidenMax);4142idx = constrained_convI2L(phase, idx, iidxtype, ctrl);4143#endif4144return idx;4145}41464147// Convert integer value to a narrowed long type dependent on ctrl (for example, a range check)4148Node* Compile::constrained_convI2L(PhaseGVN* phase, Node* value, const TypeInt* itype, Node* ctrl, bool carry_dependency) {4149if (ctrl != NULL) {4150// Express control dependency by a CastII node with a narrow type.4151value = new CastIINode(value, itype, carry_dependency ? ConstraintCastNode::StrongDependency : ConstraintCastNode::RegularDependency, true /* range check dependency */);4152// Make the CastII node dependent on the control input to prevent the narrowed ConvI2L4153// node from floating above the range check during loop optimizations. Otherwise, the4154// ConvI2L node may be eliminated independently of the range check, causing the data path4155// to become TOP while the control path is still there (although it's unreachable).4156value->set_req(0, ctrl);4157value = phase->transform(value);4158}4159const TypeLong* ltype = TypeLong::make(itype->_lo, itype->_hi, itype->_widen);4160return phase->transform(new ConvI2LNode(value, ltype));4161}41624163void Compile::print_inlining_stream_free() {4164if (_print_inlining_stream != NULL) {4165_print_inlining_stream->~stringStream();4166_print_inlining_stream = NULL;4167}4168}41694170// The message about the current inlining is accumulated in4171// _print_inlining_stream and transfered into the _print_inlining_list4172// once we know whether inlining succeeds or not. For regular4173// inlining, messages are appended to the buffer pointed by4174// _print_inlining_idx in the _print_inlining_list. For late inlining,4175// a new buffer is added after _print_inlining_idx in the list. This4176// way we can update the inlining message for late inlining call site4177// when the inlining is attempted again.4178void Compile::print_inlining_init() {4179if (print_inlining() || print_intrinsics()) {4180// print_inlining_init is actually called several times.4181print_inlining_stream_free();4182_print_inlining_stream = new stringStream();4183_print_inlining_list = new (comp_arena())GrowableArray<PrintInliningBuffer*>(comp_arena(), 1, 1, new PrintInliningBuffer());4184}4185}41864187void Compile::print_inlining_reinit() {4188if (print_inlining() || print_intrinsics()) {4189print_inlining_stream_free();4190// Re allocate buffer when we change ResourceMark4191_print_inlining_stream = new stringStream();4192}4193}41944195void Compile::print_inlining_reset() {4196_print_inlining_stream->reset();4197}41984199void Compile::print_inlining_commit() {4200assert(print_inlining() || print_intrinsics(), "PrintInlining off?");4201// Transfer the message from _print_inlining_stream to the current4202// _print_inlining_list buffer and clear _print_inlining_stream.4203_print_inlining_list->at(_print_inlining_idx)->ss()->write(_print_inlining_stream->base(), _print_inlining_stream->size());4204print_inlining_reset();4205}42064207void Compile::print_inlining_push() {4208// Add new buffer to the _print_inlining_list at current position4209_print_inlining_idx++;4210_print_inlining_list->insert_before(_print_inlining_idx, new PrintInliningBuffer());4211}42124213Compile::PrintInliningBuffer* Compile::print_inlining_current() {4214return _print_inlining_list->at(_print_inlining_idx);4215}42164217void Compile::print_inlining_update(CallGenerator* cg) {4218if (print_inlining() || print_intrinsics()) {4219if (cg->is_late_inline()) {4220if (print_inlining_current()->cg() != cg &&4221(print_inlining_current()->cg() != NULL ||4222print_inlining_current()->ss()->size() != 0)) {4223print_inlining_push();4224}4225print_inlining_commit();4226print_inlining_current()->set_cg(cg);4227} else {4228if (print_inlining_current()->cg() != NULL) {4229print_inlining_push();4230}4231print_inlining_commit();4232}4233}4234}42354236void Compile::print_inlining_move_to(CallGenerator* cg) {4237// We resume inlining at a late inlining call site. Locate the4238// corresponding inlining buffer so that we can update it.4239if (print_inlining() || print_intrinsics()) {4240for (int i = 0; i < _print_inlining_list->length(); i++) {4241if (_print_inlining_list->at(i)->cg() == cg) {4242_print_inlining_idx = i;4243return;4244}4245}4246ShouldNotReachHere();4247}4248}42494250void Compile::print_inlining_update_delayed(CallGenerator* cg) {4251if (print_inlining() || print_intrinsics()) {4252assert(_print_inlining_stream->size() > 0, "missing inlining msg");4253assert(print_inlining_current()->cg() == cg, "wrong entry");4254// replace message with new message4255_print_inlining_list->at_put(_print_inlining_idx, new PrintInliningBuffer());4256print_inlining_commit();4257print_inlining_current()->set_cg(cg);4258}4259}42604261void Compile::print_inlining_assert_ready() {4262assert(!_print_inlining || _print_inlining_stream->size() == 0, "loosing data");4263}42644265void Compile::process_print_inlining() {4266assert(_late_inlines.length() == 0, "not drained yet");4267if (print_inlining() || print_intrinsics()) {4268ResourceMark rm;4269stringStream ss;4270assert(_print_inlining_list != NULL, "process_print_inlining should be called only once.");4271for (int i = 0; i < _print_inlining_list->length(); i++) {4272PrintInliningBuffer* pib = _print_inlining_list->at(i);4273ss.print("%s", pib->ss()->as_string());4274delete pib;4275DEBUG_ONLY(_print_inlining_list->at_put(i, NULL));4276}4277// Reset _print_inlining_list, it only contains destructed objects.4278// It is on the arena, so it will be freed when the arena is reset.4279_print_inlining_list = NULL;4280// _print_inlining_stream won't be used anymore, either.4281print_inlining_stream_free();4282size_t end = ss.size();4283_print_inlining_output = NEW_ARENA_ARRAY(comp_arena(), char, end+1);4284strncpy(_print_inlining_output, ss.base(), end+1);4285_print_inlining_output[end] = 0;4286}4287}42884289void Compile::dump_print_inlining() {4290if (_print_inlining_output != NULL) {4291tty->print_raw(_print_inlining_output);4292}4293}42944295void Compile::log_late_inline(CallGenerator* cg) {4296if (log() != NULL) {4297log()->head("late_inline method='%d' inline_id='" JLONG_FORMAT "'", log()->identify(cg->method()),4298cg->unique_id());4299JVMState* p = cg->call_node()->jvms();4300while (p != NULL) {4301log()->elem("jvms bci='%d' method='%d'", p->bci(), log()->identify(p->method()));4302p = p->caller();4303}4304log()->tail("late_inline");4305}4306}43074308void Compile::log_late_inline_failure(CallGenerator* cg, const char* msg) {4309log_late_inline(cg);4310if (log() != NULL) {4311log()->inline_fail(msg);4312}4313}43144315void Compile::log_inline_id(CallGenerator* cg) {4316if (log() != NULL) {4317// The LogCompilation tool needs a unique way to identify late4318// inline call sites. This id must be unique for this call site in4319// this compilation. Try to have it unique across compilations as4320// well because it can be convenient when grepping through the log4321// file.4322// Distinguish OSR compilations from others in case CICountOSR is4323// on.4324jlong id = ((jlong)unique()) + (((jlong)compile_id()) << 33) + (CICountOSR && is_osr_compilation() ? ((jlong)1) << 32 : 0);4325cg->set_unique_id(id);4326log()->elem("inline_id id='" JLONG_FORMAT "'", id);4327}4328}43294330void Compile::log_inline_failure(const char* msg) {4331if (C->log() != NULL) {4332C->log()->inline_fail(msg);4333}4334}433543364337// Dump inlining replay data to the stream.4338// Don't change thread state and acquire any locks.4339void Compile::dump_inline_data(outputStream* out) {4340InlineTree* inl_tree = ilt();4341if (inl_tree != NULL) {4342out->print(" inline %d", inl_tree->count());4343inl_tree->dump_replay_data(out);4344}4345}43464347int Compile::cmp_expensive_nodes(Node* n1, Node* n2) {4348if (n1->Opcode() < n2->Opcode()) return -1;4349else if (n1->Opcode() > n2->Opcode()) return 1;43504351assert(n1->req() == n2->req(), "can't compare %s nodes: n1->req() = %d, n2->req() = %d", NodeClassNames[n1->Opcode()], n1->req(), n2->req());4352for (uint i = 1; i < n1->req(); i++) {4353if (n1->in(i) < n2->in(i)) return -1;4354else if (n1->in(i) > n2->in(i)) return 1;4355}43564357return 0;4358}43594360int Compile::cmp_expensive_nodes(Node** n1p, Node** n2p) {4361Node* n1 = *n1p;4362Node* n2 = *n2p;43634364return cmp_expensive_nodes(n1, n2);4365}43664367void Compile::sort_expensive_nodes() {4368if (!expensive_nodes_sorted()) {4369_expensive_nodes.sort(cmp_expensive_nodes);4370}4371}43724373bool Compile::expensive_nodes_sorted() const {4374for (int i = 1; i < _expensive_nodes.length(); i++) {4375if (cmp_expensive_nodes(_expensive_nodes.adr_at(i), _expensive_nodes.adr_at(i-1)) < 0) {4376return false;4377}4378}4379return true;4380}43814382bool Compile::should_optimize_expensive_nodes(PhaseIterGVN &igvn) {4383if (_expensive_nodes.length() == 0) {4384return false;4385}43864387assert(OptimizeExpensiveOps, "optimization off?");43884389// Take this opportunity to remove dead nodes from the list4390int j = 0;4391for (int i = 0; i < _expensive_nodes.length(); i++) {4392Node* n = _expensive_nodes.at(i);4393if (!n->is_unreachable(igvn)) {4394assert(n->is_expensive(), "should be expensive");4395_expensive_nodes.at_put(j, n);4396j++;4397}4398}4399_expensive_nodes.trunc_to(j);44004401// Then sort the list so that similar nodes are next to each other4402// and check for at least two nodes of identical kind with same data4403// inputs.4404sort_expensive_nodes();44054406for (int i = 0; i < _expensive_nodes.length()-1; i++) {4407if (cmp_expensive_nodes(_expensive_nodes.adr_at(i), _expensive_nodes.adr_at(i+1)) == 0) {4408return true;4409}4410}44114412return false;4413}44144415void Compile::cleanup_expensive_nodes(PhaseIterGVN &igvn) {4416if (_expensive_nodes.length() == 0) {4417return;4418}44194420assert(OptimizeExpensiveOps, "optimization off?");44214422// Sort to bring similar nodes next to each other and clear the4423// control input of nodes for which there's only a single copy.4424sort_expensive_nodes();44254426int j = 0;4427int identical = 0;4428int i = 0;4429bool modified = false;4430for (; i < _expensive_nodes.length()-1; i++) {4431assert(j <= i, "can't write beyond current index");4432if (_expensive_nodes.at(i)->Opcode() == _expensive_nodes.at(i+1)->Opcode()) {4433identical++;4434_expensive_nodes.at_put(j++, _expensive_nodes.at(i));4435continue;4436}4437if (identical > 0) {4438_expensive_nodes.at_put(j++, _expensive_nodes.at(i));4439identical = 0;4440} else {4441Node* n = _expensive_nodes.at(i);4442igvn.replace_input_of(n, 0, NULL);4443igvn.hash_insert(n);4444modified = true;4445}4446}4447if (identical > 0) {4448_expensive_nodes.at_put(j++, _expensive_nodes.at(i));4449} else if (_expensive_nodes.length() >= 1) {4450Node* n = _expensive_nodes.at(i);4451igvn.replace_input_of(n, 0, NULL);4452igvn.hash_insert(n);4453modified = true;4454}4455_expensive_nodes.trunc_to(j);4456if (modified) {4457igvn.optimize();4458}4459}44604461void Compile::add_expensive_node(Node * n) {4462assert(!_expensive_nodes.contains(n), "duplicate entry in expensive list");4463assert(n->is_expensive(), "expensive nodes with non-null control here only");4464assert(!n->is_CFG() && !n->is_Mem(), "no cfg or memory nodes here");4465if (OptimizeExpensiveOps) {4466_expensive_nodes.append(n);4467} else {4468// Clear control input and let IGVN optimize expensive nodes if4469// OptimizeExpensiveOps is off.4470n->set_req(0, NULL);4471}4472}44734474/**4475* Track coarsened Lock and Unlock nodes.4476*/44774478class Lock_List : public Node_List {4479uint _origin_cnt;4480public:4481Lock_List(Arena *a, uint cnt) : Node_List(a), _origin_cnt(cnt) {}4482uint origin_cnt() const { return _origin_cnt; }4483};44844485void Compile::add_coarsened_locks(GrowableArray<AbstractLockNode*>& locks) {4486int length = locks.length();4487if (length > 0) {4488// Have to keep this list until locks elimination during Macro nodes elimination.4489Lock_List* locks_list = new (comp_arena()) Lock_List(comp_arena(), length);4490for (int i = 0; i < length; i++) {4491AbstractLockNode* lock = locks.at(i);4492assert(lock->is_coarsened(), "expecting only coarsened AbstractLock nodes, but got '%s'[%d] node", lock->Name(), lock->_idx);4493locks_list->push(lock);4494}4495_coarsened_locks.append(locks_list);4496}4497}44984499void Compile::remove_useless_coarsened_locks(Unique_Node_List& useful) {4500int count = coarsened_count();4501for (int i = 0; i < count; i++) {4502Node_List* locks_list = _coarsened_locks.at(i);4503for (uint j = 0; j < locks_list->size(); j++) {4504Node* lock = locks_list->at(j);4505assert(lock->is_AbstractLock(), "sanity");4506if (!useful.member(lock)) {4507locks_list->yank(lock);4508}4509}4510}4511}45124513void Compile::remove_coarsened_lock(Node* n) {4514if (n->is_AbstractLock()) {4515int count = coarsened_count();4516for (int i = 0; i < count; i++) {4517Node_List* locks_list = _coarsened_locks.at(i);4518locks_list->yank(n);4519}4520}4521}45224523bool Compile::coarsened_locks_consistent() {4524int count = coarsened_count();4525for (int i = 0; i < count; i++) {4526bool unbalanced = false;4527bool modified = false; // track locks kind modifications4528Lock_List* locks_list = (Lock_List*)_coarsened_locks.at(i);4529uint size = locks_list->size();4530if (size == 0) {4531unbalanced = false; // All locks were eliminated - good4532} else if (size != locks_list->origin_cnt()) {4533unbalanced = true; // Some locks were removed from list4534} else {4535for (uint j = 0; j < size; j++) {4536Node* lock = locks_list->at(j);4537// All nodes in group should have the same state (modified or not)4538if (!lock->as_AbstractLock()->is_coarsened()) {4539if (j == 0) {4540// first on list was modified, the rest should be too for consistency4541modified = true;4542} else if (!modified) {4543// this lock was modified but previous locks on the list were not4544unbalanced = true;4545break;4546}4547} else if (modified) {4548// previous locks on list were modified but not this lock4549unbalanced = true;4550break;4551}4552}4553}4554if (unbalanced) {4555// unbalanced monitor enter/exit - only some [un]lock nodes were removed or modified4556#ifdef ASSERT4557if (PrintEliminateLocks) {4558tty->print_cr("=== unbalanced coarsened locks ===");4559for (uint l = 0; l < size; l++) {4560locks_list->at(l)->dump();4561}4562}4563#endif4564record_failure(C2Compiler::retry_no_locks_coarsening());4565return false;4566}4567}4568return true;4569}45704571/**4572* Remove the speculative part of types and clean up the graph4573*/4574void Compile::remove_speculative_types(PhaseIterGVN &igvn) {4575if (UseTypeSpeculation) {4576Unique_Node_List worklist;4577worklist.push(root());4578int modified = 0;4579// Go over all type nodes that carry a speculative type, drop the4580// speculative part of the type and enqueue the node for an igvn4581// which may optimize it out.4582for (uint next = 0; next < worklist.size(); ++next) {4583Node *n = worklist.at(next);4584if (n->is_Type()) {4585TypeNode* tn = n->as_Type();4586const Type* t = tn->type();4587const Type* t_no_spec = t->remove_speculative();4588if (t_no_spec != t) {4589bool in_hash = igvn.hash_delete(n);4590assert(in_hash, "node should be in igvn hash table");4591tn->set_type(t_no_spec);4592igvn.hash_insert(n);4593igvn._worklist.push(n); // give it a chance to go away4594modified++;4595}4596}4597// Iterate over outs - endless loops is unreachable from below4598for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {4599Node *m = n->fast_out(i);4600if (not_a_node(m)) {4601continue;4602}4603worklist.push(m);4604}4605}4606// Drop the speculative part of all types in the igvn's type table4607igvn.remove_speculative_types();4608if (modified > 0) {4609igvn.optimize();4610}4611#ifdef ASSERT4612// Verify that after the IGVN is over no speculative type has resurfaced4613worklist.clear();4614worklist.push(root());4615for (uint next = 0; next < worklist.size(); ++next) {4616Node *n = worklist.at(next);4617const Type* t = igvn.type_or_null(n);4618assert((t == NULL) || (t == t->remove_speculative()), "no more speculative types");4619if (n->is_Type()) {4620t = n->as_Type()->type();4621assert(t == t->remove_speculative(), "no more speculative types");4622}4623// Iterate over outs - endless loops is unreachable from below4624for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {4625Node *m = n->fast_out(i);4626if (not_a_node(m)) {4627continue;4628}4629worklist.push(m);4630}4631}4632igvn.check_no_speculative_types();4633#endif4634}4635}46364637// Auxiliary methods to support randomized stressing/fuzzing.46384639int Compile::random() {4640_stress_seed = os::next_random(_stress_seed);4641return static_cast<int>(_stress_seed);4642}46434644// This method can be called the arbitrary number of times, with current count4645// as the argument. The logic allows selecting a single candidate from the4646// running list of candidates as follows:4647// int count = 0;4648// Cand* selected = null;4649// while(cand = cand->next()) {4650// if (randomized_select(++count)) {4651// selected = cand;4652// }4653// }4654//4655// Including count equalizes the chances any candidate is "selected".4656// This is useful when we don't have the complete list of candidates to choose4657// from uniformly. In this case, we need to adjust the randomicity of the4658// selection, or else we will end up biasing the selection towards the latter4659// candidates.4660//4661// Quick back-envelope calculation shows that for the list of n candidates4662// the equal probability for the candidate to persist as "best" can be4663// achieved by replacing it with "next" k-th candidate with the probability4664// of 1/k. It can be easily shown that by the end of the run, the4665// probability for any candidate is converged to 1/n, thus giving the4666// uniform distribution among all the candidates.4667//4668// We don't care about the domain size as long as (RANDOMIZED_DOMAIN / count) is large.4669#define RANDOMIZED_DOMAIN_POW 294670#define RANDOMIZED_DOMAIN (1 << RANDOMIZED_DOMAIN_POW)4671#define RANDOMIZED_DOMAIN_MASK ((1 << (RANDOMIZED_DOMAIN_POW + 1)) - 1)4672bool Compile::randomized_select(int count) {4673assert(count > 0, "only positive");4674return (random() & RANDOMIZED_DOMAIN_MASK) < (RANDOMIZED_DOMAIN / count);4675}46764677CloneMap& Compile::clone_map() { return _clone_map; }4678void Compile::set_clone_map(Dict* d) { _clone_map._dict = d; }46794680void NodeCloneInfo::dump() const {4681tty->print(" {%d:%d} ", idx(), gen());4682}46834684void CloneMap::clone(Node* old, Node* nnn, int gen) {4685uint64_t val = value(old->_idx);4686NodeCloneInfo cio(val);4687assert(val != 0, "old node should be in the map");4688NodeCloneInfo cin(cio.idx(), gen + cio.gen());4689insert(nnn->_idx, cin.get());4690#ifndef PRODUCT4691if (is_debug()) {4692tty->print_cr("CloneMap::clone inserted node %d info {%d:%d} into CloneMap", nnn->_idx, cin.idx(), cin.gen());4693}4694#endif4695}46964697void CloneMap::verify_insert_and_clone(Node* old, Node* nnn, int gen) {4698NodeCloneInfo cio(value(old->_idx));4699if (cio.get() == 0) {4700cio.set(old->_idx, 0);4701insert(old->_idx, cio.get());4702#ifndef PRODUCT4703if (is_debug()) {4704tty->print_cr("CloneMap::verify_insert_and_clone inserted node %d info {%d:%d} into CloneMap", old->_idx, cio.idx(), cio.gen());4705}4706#endif4707}4708clone(old, nnn, gen);4709}47104711int CloneMap::max_gen() const {4712int g = 0;4713DictI di(_dict);4714for(; di.test(); ++di) {4715int t = gen(di._key);4716if (g < t) {4717g = t;4718#ifndef PRODUCT4719if (is_debug()) {4720tty->print_cr("CloneMap::max_gen() update max=%d from %d", g, _2_node_idx_t(di._key));4721}4722#endif4723}4724}4725return g;4726}47274728void CloneMap::dump(node_idx_t key) const {4729uint64_t val = value(key);4730if (val != 0) {4731NodeCloneInfo ni(val);4732ni.dump();4733}4734}47354736// Move Allocate nodes to the start of the list4737void Compile::sort_macro_nodes() {4738int count = macro_count();4739int allocates = 0;4740for (int i = 0; i < count; i++) {4741Node* n = macro_node(i);4742if (n->is_Allocate()) {4743if (i != allocates) {4744Node* tmp = macro_node(allocates);4745_macro_nodes.at_put(allocates, n);4746_macro_nodes.at_put(i, tmp);4747}4748allocates++;4749}4750}4751}47524753void Compile::print_method(CompilerPhaseType cpt, const char *name, int level) {4754EventCompilerPhase event;4755if (event.should_commit()) {4756CompilerEvent::PhaseEvent::post(event, C->_latest_stage_start_counter, cpt, C->_compile_id, level);4757}4758#ifndef PRODUCT4759if (should_print(level)) {4760_printer->print_method(name, level);4761}4762#endif4763C->_latest_stage_start_counter.stamp();4764}47654766void Compile::print_method(CompilerPhaseType cpt, int level, int idx) {4767char output[1024];4768#ifndef PRODUCT4769if (idx != 0) {4770jio_snprintf(output, sizeof(output), "%s:%d", CompilerPhaseTypeHelper::to_string(cpt), idx);4771} else {4772jio_snprintf(output, sizeof(output), "%s", CompilerPhaseTypeHelper::to_string(cpt));4773}4774#endif4775print_method(cpt, output, level);4776}47774778void Compile::print_method(CompilerPhaseType cpt, Node* n, int level) {4779ResourceMark rm;4780stringStream ss;4781ss.print_raw(CompilerPhaseTypeHelper::to_string(cpt));4782if (n != NULL) {4783ss.print(": %d %s ", n->_idx, NodeClassNames[n->Opcode()]);4784} else {4785ss.print_raw(": NULL");4786}4787C->print_method(cpt, ss.as_string(), level);4788}47894790void Compile::end_method(int level) {4791EventCompilerPhase event;4792if (event.should_commit()) {4793CompilerEvent::PhaseEvent::post(event, C->_latest_stage_start_counter, PHASE_END, C->_compile_id, level);4794}47954796#ifndef PRODUCT4797if (_method != NULL && should_print(level)) {4798_printer->end_method();4799}4800#endif4801}480248034804#ifndef PRODUCT4805IdealGraphPrinter* Compile::_debug_file_printer = NULL;4806IdealGraphPrinter* Compile::_debug_network_printer = NULL;48074808// Called from debugger. Prints method to the default file with the default phase name.4809// This works regardless of any Ideal Graph Visualizer flags set or not.4810void igv_print() {4811Compile::current()->igv_print_method_to_file();4812}48134814// Same as igv_print() above but with a specified phase name.4815void igv_print(const char* phase_name) {4816Compile::current()->igv_print_method_to_file(phase_name);4817}48184819// Called from debugger. Prints method with the default phase name to the default network or the one specified with4820// the network flags for the Ideal Graph Visualizer, or to the default file depending on the 'network' argument.4821// This works regardless of any Ideal Graph Visualizer flags set or not.4822void igv_print(bool network) {4823if (network) {4824Compile::current()->igv_print_method_to_network();4825} else {4826Compile::current()->igv_print_method_to_file();4827}4828}48294830// Same as igv_print(bool network) above but with a specified phase name.4831void igv_print(bool network, const char* phase_name) {4832if (network) {4833Compile::current()->igv_print_method_to_network(phase_name);4834} else {4835Compile::current()->igv_print_method_to_file(phase_name);4836}4837}48384839// Called from debugger. Normal write to the default _printer. Only works if Ideal Graph Visualizer printing flags are set.4840void igv_print_default() {4841Compile::current()->print_method(PHASE_DEBUG, 0);4842}48434844// Called from debugger, especially when replaying a trace in which the program state cannot be altered like with rr replay.4845// A method is appended to an existing default file with the default phase name. This means that igv_append() must follow4846// an earlier igv_print(*) call which sets up the file. This works regardless of any Ideal Graph Visualizer flags set or not.4847void igv_append() {4848Compile::current()->igv_print_method_to_file("Debug", true);4849}48504851// Same as igv_append() above but with a specified phase name.4852void igv_append(const char* phase_name) {4853Compile::current()->igv_print_method_to_file(phase_name, true);4854}48554856void Compile::igv_print_method_to_file(const char* phase_name, bool append) {4857const char* file_name = "custom_debug.xml";4858if (_debug_file_printer == NULL) {4859_debug_file_printer = new IdealGraphPrinter(C, file_name, append);4860} else {4861_debug_file_printer->update_compiled_method(C->method());4862}4863tty->print_cr("Method %s to %s", append ? "appended" : "printed", file_name);4864_debug_file_printer->print(phase_name, (Node*)C->root());4865}48664867void Compile::igv_print_method_to_network(const char* phase_name) {4868if (_debug_network_printer == NULL) {4869_debug_network_printer = new IdealGraphPrinter(C);4870} else {4871_debug_network_printer->update_compiled_method(C->method());4872}4873tty->print_cr("Method printed over network stream to IGV");4874_debug_network_printer->print(phase_name, (Node*)C->root());4875}4876#endif48774878void Compile::add_native_invoker(RuntimeStub* stub) {4879_native_invokers.append(stub);4880}48814882Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {4883if (type != NULL && phase->type(value)->higher_equal(type)) {4884return value;4885}4886Node* result = NULL;4887if (bt == T_BYTE) {4888result = phase->transform(new LShiftINode(value, phase->intcon(24)));4889result = new RShiftINode(result, phase->intcon(24));4890} else if (bt == T_BOOLEAN) {4891result = new AndINode(value, phase->intcon(0xFF));4892} else if (bt == T_CHAR) {4893result = new AndINode(value,phase->intcon(0xFFFF));4894} else {4895assert(bt == T_SHORT, "unexpected narrow type");4896result = phase->transform(new LShiftINode(value, phase->intcon(16)));4897result = new RShiftINode(result, phase->intcon(16));4898}4899if (transform_res) {4900result = phase->transform(result);4901}4902return result;4903}4904490549064907