Path: blob/master/src/hotspot/share/c1/c1_GraphBuilder.cpp
40931 views
/*1* Copyright (c) 1999, 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 "c1/c1_CFGPrinter.hpp"26#include "c1/c1_Canonicalizer.hpp"27#include "c1/c1_Compilation.hpp"28#include "c1/c1_GraphBuilder.hpp"29#include "c1/c1_InstructionPrinter.hpp"30#include "ci/ciCallSite.hpp"31#include "ci/ciField.hpp"32#include "ci/ciKlass.hpp"33#include "ci/ciMemberName.hpp"34#include "ci/ciSymbols.hpp"35#include "ci/ciUtilities.inline.hpp"36#include "compiler/compilationPolicy.hpp"37#include "compiler/compileBroker.hpp"38#include "compiler/compilerEvent.hpp"39#include "interpreter/bytecode.hpp"40#include "jfr/jfrEvents.hpp"41#include "memory/resourceArea.hpp"42#include "oops/oop.inline.hpp"43#include "runtime/sharedRuntime.hpp"44#include "runtime/vm_version.hpp"45#include "utilities/bitMap.inline.hpp"46#include "utilities/powerOfTwo.hpp"4748class BlockListBuilder {49private:50Compilation* _compilation;51IRScope* _scope;5253BlockList _blocks; // internal list of all blocks54BlockList* _bci2block; // mapping from bci to blocks for GraphBuilder5556// fields used by mark_loops57ResourceBitMap _active; // for iteration of control flow graph58ResourceBitMap _visited; // for iteration of control flow graph59intArray _loop_map; // caches the information if a block is contained in a loop60int _next_loop_index; // next free loop number61int _next_block_number; // for reverse postorder numbering of blocks6263// accessors64Compilation* compilation() const { return _compilation; }65IRScope* scope() const { return _scope; }66ciMethod* method() const { return scope()->method(); }67XHandlers* xhandlers() const { return scope()->xhandlers(); }6869// unified bailout support70void bailout(const char* msg) const { compilation()->bailout(msg); }71bool bailed_out() const { return compilation()->bailed_out(); }7273// helper functions74BlockBegin* make_block_at(int bci, BlockBegin* predecessor);75void handle_exceptions(BlockBegin* current, int cur_bci);76void handle_jsr(BlockBegin* current, int sr_bci, int next_bci);77void store_one(BlockBegin* current, int local);78void store_two(BlockBegin* current, int local);79void set_entries(int osr_bci);80void set_leaders();8182void make_loop_header(BlockBegin* block);83void mark_loops();84int mark_loops(BlockBegin* b, bool in_subroutine);8586// debugging87#ifndef PRODUCT88void print();89#endif9091public:92// creation93BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci);9495// accessors for GraphBuilder96BlockList* bci2block() const { return _bci2block; }97};9899100// Implementation of BlockListBuilder101102BlockListBuilder::BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci)103: _compilation(compilation)104, _scope(scope)105, _blocks(16)106, _bci2block(new BlockList(scope->method()->code_size(), NULL))107, _active() // size not known yet108, _visited() // size not known yet109, _loop_map() // size not known yet110, _next_loop_index(0)111, _next_block_number(0)112{113set_entries(osr_bci);114set_leaders();115CHECK_BAILOUT();116117mark_loops();118NOT_PRODUCT(if (PrintInitialBlockList) print());119120#ifndef PRODUCT121if (PrintCFGToFile) {122stringStream title;123title.print("BlockListBuilder ");124scope->method()->print_name(&title);125CFGPrinter::print_cfg(_bci2block, title.as_string(), false, false);126}127#endif128}129130131void BlockListBuilder::set_entries(int osr_bci) {132// generate start blocks133BlockBegin* std_entry = make_block_at(0, NULL);134if (scope()->caller() == NULL) {135std_entry->set(BlockBegin::std_entry_flag);136}137if (osr_bci != -1) {138BlockBegin* osr_entry = make_block_at(osr_bci, NULL);139osr_entry->set(BlockBegin::osr_entry_flag);140}141142// generate exception entry blocks143XHandlers* list = xhandlers();144const int n = list->length();145for (int i = 0; i < n; i++) {146XHandler* h = list->handler_at(i);147BlockBegin* entry = make_block_at(h->handler_bci(), NULL);148entry->set(BlockBegin::exception_entry_flag);149h->set_entry_block(entry);150}151}152153154BlockBegin* BlockListBuilder::make_block_at(int cur_bci, BlockBegin* predecessor) {155assert(method()->bci_block_start().at(cur_bci), "wrong block starts of MethodLivenessAnalyzer");156157BlockBegin* block = _bci2block->at(cur_bci);158if (block == NULL) {159block = new BlockBegin(cur_bci);160block->init_stores_to_locals(method()->max_locals());161_bci2block->at_put(cur_bci, block);162_blocks.append(block);163164assert(predecessor == NULL || predecessor->bci() < cur_bci, "targets for backward branches must already exist");165}166167if (predecessor != NULL) {168if (block->is_set(BlockBegin::exception_entry_flag)) {169BAILOUT_("Exception handler can be reached by both normal and exceptional control flow", block);170}171172predecessor->add_successor(block);173block->increment_total_preds();174}175176return block;177}178179180inline void BlockListBuilder::store_one(BlockBegin* current, int local) {181current->stores_to_locals().set_bit(local);182}183inline void BlockListBuilder::store_two(BlockBegin* current, int local) {184store_one(current, local);185store_one(current, local + 1);186}187188189void BlockListBuilder::handle_exceptions(BlockBegin* current, int cur_bci) {190// Draws edges from a block to its exception handlers191XHandlers* list = xhandlers();192const int n = list->length();193194for (int i = 0; i < n; i++) {195XHandler* h = list->handler_at(i);196197if (h->covers(cur_bci)) {198BlockBegin* entry = h->entry_block();199assert(entry != NULL && entry == _bci2block->at(h->handler_bci()), "entry must be set");200assert(entry->is_set(BlockBegin::exception_entry_flag), "flag must be set");201202// add each exception handler only once203if (!current->is_successor(entry)) {204current->add_successor(entry);205entry->increment_total_preds();206}207208// stop when reaching catchall209if (h->catch_type() == 0) break;210}211}212}213214void BlockListBuilder::handle_jsr(BlockBegin* current, int sr_bci, int next_bci) {215// start a new block after jsr-bytecode and link this block into cfg216make_block_at(next_bci, current);217218// start a new block at the subroutine entry at mark it with special flag219BlockBegin* sr_block = make_block_at(sr_bci, current);220if (!sr_block->is_set(BlockBegin::subroutine_entry_flag)) {221sr_block->set(BlockBegin::subroutine_entry_flag);222}223}224225226void BlockListBuilder::set_leaders() {227bool has_xhandlers = xhandlers()->has_handlers();228BlockBegin* current = NULL;229230// The information which bci starts a new block simplifies the analysis231// Without it, backward branches could jump to a bci where no block was created232// during bytecode iteration. This would require the creation of a new block at the233// branch target and a modification of the successor lists.234const BitMap& bci_block_start = method()->bci_block_start();235236ciBytecodeStream s(method());237while (s.next() != ciBytecodeStream::EOBC()) {238int cur_bci = s.cur_bci();239240if (bci_block_start.at(cur_bci)) {241current = make_block_at(cur_bci, current);242}243assert(current != NULL, "must have current block");244245if (has_xhandlers && GraphBuilder::can_trap(method(), s.cur_bc())) {246handle_exceptions(current, cur_bci);247}248249switch (s.cur_bc()) {250// track stores to local variables for selective creation of phi functions251case Bytecodes::_iinc: store_one(current, s.get_index()); break;252case Bytecodes::_istore: store_one(current, s.get_index()); break;253case Bytecodes::_lstore: store_two(current, s.get_index()); break;254case Bytecodes::_fstore: store_one(current, s.get_index()); break;255case Bytecodes::_dstore: store_two(current, s.get_index()); break;256case Bytecodes::_astore: store_one(current, s.get_index()); break;257case Bytecodes::_istore_0: store_one(current, 0); break;258case Bytecodes::_istore_1: store_one(current, 1); break;259case Bytecodes::_istore_2: store_one(current, 2); break;260case Bytecodes::_istore_3: store_one(current, 3); break;261case Bytecodes::_lstore_0: store_two(current, 0); break;262case Bytecodes::_lstore_1: store_two(current, 1); break;263case Bytecodes::_lstore_2: store_two(current, 2); break;264case Bytecodes::_lstore_3: store_two(current, 3); break;265case Bytecodes::_fstore_0: store_one(current, 0); break;266case Bytecodes::_fstore_1: store_one(current, 1); break;267case Bytecodes::_fstore_2: store_one(current, 2); break;268case Bytecodes::_fstore_3: store_one(current, 3); break;269case Bytecodes::_dstore_0: store_two(current, 0); break;270case Bytecodes::_dstore_1: store_two(current, 1); break;271case Bytecodes::_dstore_2: store_two(current, 2); break;272case Bytecodes::_dstore_3: store_two(current, 3); break;273case Bytecodes::_astore_0: store_one(current, 0); break;274case Bytecodes::_astore_1: store_one(current, 1); break;275case Bytecodes::_astore_2: store_one(current, 2); break;276case Bytecodes::_astore_3: store_one(current, 3); break;277278// track bytecodes that affect the control flow279case Bytecodes::_athrow: // fall through280case Bytecodes::_ret: // fall through281case Bytecodes::_ireturn: // fall through282case Bytecodes::_lreturn: // fall through283case Bytecodes::_freturn: // fall through284case Bytecodes::_dreturn: // fall through285case Bytecodes::_areturn: // fall through286case Bytecodes::_return:287current = NULL;288break;289290case Bytecodes::_ifeq: // fall through291case Bytecodes::_ifne: // fall through292case Bytecodes::_iflt: // fall through293case Bytecodes::_ifge: // fall through294case Bytecodes::_ifgt: // fall through295case Bytecodes::_ifle: // fall through296case Bytecodes::_if_icmpeq: // fall through297case Bytecodes::_if_icmpne: // fall through298case Bytecodes::_if_icmplt: // fall through299case Bytecodes::_if_icmpge: // fall through300case Bytecodes::_if_icmpgt: // fall through301case Bytecodes::_if_icmple: // fall through302case Bytecodes::_if_acmpeq: // fall through303case Bytecodes::_if_acmpne: // fall through304case Bytecodes::_ifnull: // fall through305case Bytecodes::_ifnonnull:306make_block_at(s.next_bci(), current);307make_block_at(s.get_dest(), current);308current = NULL;309break;310311case Bytecodes::_goto:312make_block_at(s.get_dest(), current);313current = NULL;314break;315316case Bytecodes::_goto_w:317make_block_at(s.get_far_dest(), current);318current = NULL;319break;320321case Bytecodes::_jsr:322handle_jsr(current, s.get_dest(), s.next_bci());323current = NULL;324break;325326case Bytecodes::_jsr_w:327handle_jsr(current, s.get_far_dest(), s.next_bci());328current = NULL;329break;330331case Bytecodes::_tableswitch: {332// set block for each case333Bytecode_tableswitch sw(&s);334int l = sw.length();335for (int i = 0; i < l; i++) {336make_block_at(cur_bci + sw.dest_offset_at(i), current);337}338make_block_at(cur_bci + sw.default_offset(), current);339current = NULL;340break;341}342343case Bytecodes::_lookupswitch: {344// set block for each case345Bytecode_lookupswitch sw(&s);346int l = sw.number_of_pairs();347for (int i = 0; i < l; i++) {348make_block_at(cur_bci + sw.pair_at(i).offset(), current);349}350make_block_at(cur_bci + sw.default_offset(), current);351current = NULL;352break;353}354355default:356break;357}358}359}360361362void BlockListBuilder::mark_loops() {363ResourceMark rm;364365_active.initialize(BlockBegin::number_of_blocks());366_visited.initialize(BlockBegin::number_of_blocks());367_loop_map = intArray(BlockBegin::number_of_blocks(), BlockBegin::number_of_blocks(), 0);368_next_loop_index = 0;369_next_block_number = _blocks.length();370371// recursively iterate the control flow graph372mark_loops(_bci2block->at(0), false);373assert(_next_block_number >= 0, "invalid block numbers");374375// Remove dangling Resource pointers before the ResourceMark goes out-of-scope.376_active.resize(0);377_visited.resize(0);378}379380void BlockListBuilder::make_loop_header(BlockBegin* block) {381if (block->is_set(BlockBegin::exception_entry_flag)) {382// exception edges may look like loops but don't mark them as such383// since it screws up block ordering.384return;385}386if (!block->is_set(BlockBegin::parser_loop_header_flag)) {387block->set(BlockBegin::parser_loop_header_flag);388389assert(_loop_map.at(block->block_id()) == 0, "must not be set yet");390assert(0 <= _next_loop_index && _next_loop_index < BitsPerInt, "_next_loop_index is used as a bit-index in integer");391_loop_map.at_put(block->block_id(), 1 << _next_loop_index);392if (_next_loop_index < 31) _next_loop_index++;393} else {394// block already marked as loop header395assert(is_power_of_2((unsigned int)_loop_map.at(block->block_id())), "exactly one bit must be set");396}397}398399int BlockListBuilder::mark_loops(BlockBegin* block, bool in_subroutine) {400int block_id = block->block_id();401402if (_visited.at(block_id)) {403if (_active.at(block_id)) {404// reached block via backward branch405make_loop_header(block);406}407// return cached loop information for this block408return _loop_map.at(block_id);409}410411if (block->is_set(BlockBegin::subroutine_entry_flag)) {412in_subroutine = true;413}414415// set active and visited bits before successors are processed416_visited.set_bit(block_id);417_active.set_bit(block_id);418419intptr_t loop_state = 0;420for (int i = block->number_of_sux() - 1; i >= 0; i--) {421// recursively process all successors422loop_state |= mark_loops(block->sux_at(i), in_subroutine);423}424425// clear active-bit after all successors are processed426_active.clear_bit(block_id);427428// reverse-post-order numbering of all blocks429block->set_depth_first_number(_next_block_number);430_next_block_number--;431432if (loop_state != 0 || in_subroutine ) {433// block is contained at least in one loop, so phi functions are necessary434// phi functions are also necessary for all locals stored in a subroutine435scope()->requires_phi_function().set_union(block->stores_to_locals());436}437438if (block->is_set(BlockBegin::parser_loop_header_flag)) {439int header_loop_state = _loop_map.at(block_id);440assert(is_power_of_2((unsigned)header_loop_state), "exactly one bit must be set");441442// If the highest bit is set (i.e. when integer value is negative), the method443// has 32 or more loops. This bit is never cleared because it is used for multiple loops444if (header_loop_state >= 0) {445clear_bits(loop_state, header_loop_state);446}447}448449// cache and return loop information for this block450_loop_map.at_put(block_id, loop_state);451return loop_state;452}453454455#ifndef PRODUCT456457int compare_depth_first(BlockBegin** a, BlockBegin** b) {458return (*a)->depth_first_number() - (*b)->depth_first_number();459}460461void BlockListBuilder::print() {462tty->print("----- initial block list of BlockListBuilder for method ");463method()->print_short_name();464tty->cr();465466// better readability if blocks are sorted in processing order467_blocks.sort(compare_depth_first);468469for (int i = 0; i < _blocks.length(); i++) {470BlockBegin* cur = _blocks.at(i);471tty->print("%4d: B%-4d bci: %-4d preds: %-4d ", cur->depth_first_number(), cur->block_id(), cur->bci(), cur->total_preds());472473tty->print(cur->is_set(BlockBegin::std_entry_flag) ? " std" : " ");474tty->print(cur->is_set(BlockBegin::osr_entry_flag) ? " osr" : " ");475tty->print(cur->is_set(BlockBegin::exception_entry_flag) ? " ex" : " ");476tty->print(cur->is_set(BlockBegin::subroutine_entry_flag) ? " sr" : " ");477tty->print(cur->is_set(BlockBegin::parser_loop_header_flag) ? " lh" : " ");478479if (cur->number_of_sux() > 0) {480tty->print(" sux: ");481for (int j = 0; j < cur->number_of_sux(); j++) {482BlockBegin* sux = cur->sux_at(j);483tty->print("B%d ", sux->block_id());484}485}486tty->cr();487}488}489490#endif491492493// A simple growable array of Values indexed by ciFields494class FieldBuffer: public CompilationResourceObj {495private:496GrowableArray<Value> _values;497498public:499FieldBuffer() {}500501void kill() {502_values.trunc_to(0);503}504505Value at(ciField* field) {506assert(field->holder()->is_loaded(), "must be a loaded field");507int offset = field->offset();508if (offset < _values.length()) {509return _values.at(offset);510} else {511return NULL;512}513}514515void at_put(ciField* field, Value value) {516assert(field->holder()->is_loaded(), "must be a loaded field");517int offset = field->offset();518_values.at_put_grow(offset, value, NULL);519}520521};522523524// MemoryBuffer is fairly simple model of the current state of memory.525// It partitions memory into several pieces. The first piece is526// generic memory where little is known about the owner of the memory.527// This is conceptually represented by the tuple <O, F, V> which says528// that the field F of object O has value V. This is flattened so529// that F is represented by the offset of the field and the parallel530// arrays _objects and _values are used for O and V. Loads of O.F can531// simply use V. Newly allocated objects are kept in a separate list532// along with a parallel array for each object which represents the533// current value of its fields. Stores of the default value to fields534// which have never been stored to before are eliminated since they535// are redundant. Once newly allocated objects are stored into536// another object or they are passed out of the current compile they537// are treated like generic memory.538539class MemoryBuffer: public CompilationResourceObj {540private:541FieldBuffer _values;542GrowableArray<Value> _objects;543GrowableArray<Value> _newobjects;544GrowableArray<FieldBuffer*> _fields;545546public:547MemoryBuffer() {}548549StoreField* store(StoreField* st) {550if (!EliminateFieldAccess) {551return st;552}553554Value object = st->obj();555Value value = st->value();556ciField* field = st->field();557if (field->holder()->is_loaded()) {558int offset = field->offset();559int index = _newobjects.find(object);560if (index != -1) {561// newly allocated object with no other stores performed on this field562FieldBuffer* buf = _fields.at(index);563if (buf->at(field) == NULL && is_default_value(value)) {564#ifndef PRODUCT565if (PrintIRDuringConstruction && Verbose) {566tty->print_cr("Eliminated store for object %d:", index);567st->print_line();568}569#endif570return NULL;571} else {572buf->at_put(field, value);573}574} else {575_objects.at_put_grow(offset, object, NULL);576_values.at_put(field, value);577}578579store_value(value);580} else {581// if we held onto field names we could alias based on names but582// we don't know what's being stored to so kill it all.583kill();584}585return st;586}587588589// return true if this value correspond to the default value of a field.590bool is_default_value(Value value) {591Constant* con = value->as_Constant();592if (con) {593switch (con->type()->tag()) {594case intTag: return con->type()->as_IntConstant()->value() == 0;595case longTag: return con->type()->as_LongConstant()->value() == 0;596case floatTag: return jint_cast(con->type()->as_FloatConstant()->value()) == 0;597case doubleTag: return jlong_cast(con->type()->as_DoubleConstant()->value()) == jlong_cast(0);598case objectTag: return con->type() == objectNull;599default: ShouldNotReachHere();600}601}602return false;603}604605606// return either the actual value of a load or the load itself607Value load(LoadField* load) {608if (!EliminateFieldAccess) {609return load;610}611612if (strict_fp_requires_explicit_rounding && load->type()->is_float_kind()) {613#ifdef IA32614if (UseSSE < 2) {615// can't skip load since value might get rounded as a side effect616return load;617}618#else619Unimplemented();620#endif // IA32621}622623ciField* field = load->field();624Value object = load->obj();625if (field->holder()->is_loaded() && !field->is_volatile()) {626int offset = field->offset();627Value result = NULL;628int index = _newobjects.find(object);629if (index != -1) {630result = _fields.at(index)->at(field);631} else if (_objects.at_grow(offset, NULL) == object) {632result = _values.at(field);633}634if (result != NULL) {635#ifndef PRODUCT636if (PrintIRDuringConstruction && Verbose) {637tty->print_cr("Eliminated load: ");638load->print_line();639}640#endif641assert(result->type()->tag() == load->type()->tag(), "wrong types");642return result;643}644}645return load;646}647648// Record this newly allocated object649void new_instance(NewInstance* object) {650int index = _newobjects.length();651_newobjects.append(object);652if (_fields.at_grow(index, NULL) == NULL) {653_fields.at_put(index, new FieldBuffer());654} else {655_fields.at(index)->kill();656}657}658659void store_value(Value value) {660int index = _newobjects.find(value);661if (index != -1) {662// stored a newly allocated object into another object.663// Assume we've lost track of it as separate slice of memory.664// We could do better by keeping track of whether individual665// fields could alias each other.666_newobjects.remove_at(index);667// pull out the field info and store it at the end up the list668// of field info list to be reused later.669_fields.append(_fields.at(index));670_fields.remove_at(index);671}672}673674void kill() {675_newobjects.trunc_to(0);676_objects.trunc_to(0);677_values.kill();678}679};680681682// Implementation of GraphBuilder's ScopeData683684GraphBuilder::ScopeData::ScopeData(ScopeData* parent)685: _parent(parent)686, _bci2block(NULL)687, _scope(NULL)688, _has_handler(false)689, _stream(NULL)690, _work_list(NULL)691, _caller_stack_size(-1)692, _continuation(NULL)693, _parsing_jsr(false)694, _jsr_xhandlers(NULL)695, _num_returns(0)696, _cleanup_block(NULL)697, _cleanup_return_prev(NULL)698, _cleanup_state(NULL)699, _ignore_return(false)700{701if (parent != NULL) {702_max_inline_size = (intx) ((float) NestedInliningSizeRatio * (float) parent->max_inline_size() / 100.0f);703} else {704_max_inline_size = C1MaxInlineSize;705}706if (_max_inline_size < C1MaxTrivialSize) {707_max_inline_size = C1MaxTrivialSize;708}709}710711712void GraphBuilder::kill_all() {713if (UseLocalValueNumbering) {714vmap()->kill_all();715}716_memory->kill();717}718719720BlockBegin* GraphBuilder::ScopeData::block_at(int bci) {721if (parsing_jsr()) {722// It is necessary to clone all blocks associated with a723// subroutine, including those for exception handlers in the scope724// of the method containing the jsr (because those exception725// handlers may contain ret instructions in some cases).726BlockBegin* block = bci2block()->at(bci);727if (block != NULL && block == parent()->bci2block()->at(bci)) {728BlockBegin* new_block = new BlockBegin(block->bci());729if (PrintInitialBlockList) {730tty->print_cr("CFG: cloned block %d (bci %d) as block %d for jsr",731block->block_id(), block->bci(), new_block->block_id());732}733// copy data from cloned blocked734new_block->set_depth_first_number(block->depth_first_number());735if (block->is_set(BlockBegin::parser_loop_header_flag)) new_block->set(BlockBegin::parser_loop_header_flag);736// Preserve certain flags for assertion checking737if (block->is_set(BlockBegin::subroutine_entry_flag)) new_block->set(BlockBegin::subroutine_entry_flag);738if (block->is_set(BlockBegin::exception_entry_flag)) new_block->set(BlockBegin::exception_entry_flag);739740// copy was_visited_flag to allow early detection of bailouts741// if a block that is used in a jsr has already been visited before,742// it is shared between the normal control flow and a subroutine743// BlockBegin::try_merge returns false when the flag is set, this leads744// to a compilation bailout745if (block->is_set(BlockBegin::was_visited_flag)) new_block->set(BlockBegin::was_visited_flag);746747bci2block()->at_put(bci, new_block);748block = new_block;749}750return block;751} else {752return bci2block()->at(bci);753}754}755756757XHandlers* GraphBuilder::ScopeData::xhandlers() const {758if (_jsr_xhandlers == NULL) {759assert(!parsing_jsr(), "");760return scope()->xhandlers();761}762assert(parsing_jsr(), "");763return _jsr_xhandlers;764}765766767void GraphBuilder::ScopeData::set_scope(IRScope* scope) {768_scope = scope;769bool parent_has_handler = false;770if (parent() != NULL) {771parent_has_handler = parent()->has_handler();772}773_has_handler = parent_has_handler || scope->xhandlers()->has_handlers();774}775776777void GraphBuilder::ScopeData::set_inline_cleanup_info(BlockBegin* block,778Instruction* return_prev,779ValueStack* return_state) {780_cleanup_block = block;781_cleanup_return_prev = return_prev;782_cleanup_state = return_state;783}784785786void GraphBuilder::ScopeData::add_to_work_list(BlockBegin* block) {787if (_work_list == NULL) {788_work_list = new BlockList();789}790791if (!block->is_set(BlockBegin::is_on_work_list_flag)) {792// Do not start parsing the continuation block while in a793// sub-scope794if (parsing_jsr()) {795if (block == jsr_continuation()) {796return;797}798} else {799if (block == continuation()) {800return;801}802}803block->set(BlockBegin::is_on_work_list_flag);804_work_list->push(block);805806sort_top_into_worklist(_work_list, block);807}808}809810811void GraphBuilder::sort_top_into_worklist(BlockList* worklist, BlockBegin* top) {812assert(worklist->top() == top, "");813// sort block descending into work list814const int dfn = top->depth_first_number();815assert(dfn != -1, "unknown depth first number");816int i = worklist->length()-2;817while (i >= 0) {818BlockBegin* b = worklist->at(i);819if (b->depth_first_number() < dfn) {820worklist->at_put(i+1, b);821} else {822break;823}824i --;825}826if (i >= -1) worklist->at_put(i + 1, top);827}828829830BlockBegin* GraphBuilder::ScopeData::remove_from_work_list() {831if (is_work_list_empty()) {832return NULL;833}834return _work_list->pop();835}836837838bool GraphBuilder::ScopeData::is_work_list_empty() const {839return (_work_list == NULL || _work_list->length() == 0);840}841842843void GraphBuilder::ScopeData::setup_jsr_xhandlers() {844assert(parsing_jsr(), "");845// clone all the exception handlers from the scope846XHandlers* handlers = new XHandlers(scope()->xhandlers());847const int n = handlers->length();848for (int i = 0; i < n; i++) {849// The XHandlers need to be adjusted to dispatch to the cloned850// handler block instead of the default one but the synthetic851// unlocker needs to be handled specially. The synthetic unlocker852// should be left alone since there can be only one and all code853// should dispatch to the same one.854XHandler* h = handlers->handler_at(i);855assert(h->handler_bci() != SynchronizationEntryBCI, "must be real");856h->set_entry_block(block_at(h->handler_bci()));857}858_jsr_xhandlers = handlers;859}860861862int GraphBuilder::ScopeData::num_returns() {863if (parsing_jsr()) {864return parent()->num_returns();865}866return _num_returns;867}868869870void GraphBuilder::ScopeData::incr_num_returns() {871if (parsing_jsr()) {872parent()->incr_num_returns();873} else {874++_num_returns;875}876}877878879// Implementation of GraphBuilder880881#define INLINE_BAILOUT(msg) { inline_bailout(msg); return false; }882883884void GraphBuilder::load_constant() {885ciConstant con = stream()->get_constant();886if (con.basic_type() == T_ILLEGAL) {887// FIXME: an unresolved Dynamic constant can get here,888// and that should not terminate the whole compilation.889BAILOUT("could not resolve a constant");890} else {891ValueType* t = illegalType;892ValueStack* patch_state = NULL;893switch (con.basic_type()) {894case T_BOOLEAN: t = new IntConstant (con.as_boolean()); break;895case T_BYTE : t = new IntConstant (con.as_byte ()); break;896case T_CHAR : t = new IntConstant (con.as_char ()); break;897case T_SHORT : t = new IntConstant (con.as_short ()); break;898case T_INT : t = new IntConstant (con.as_int ()); break;899case T_LONG : t = new LongConstant (con.as_long ()); break;900case T_FLOAT : t = new FloatConstant (con.as_float ()); break;901case T_DOUBLE : t = new DoubleConstant (con.as_double ()); break;902case T_ARRAY : t = new ArrayConstant (con.as_object ()->as_array ()); break;903case T_OBJECT :904{905ciObject* obj = con.as_object();906if (!obj->is_loaded()907|| (PatchALot && obj->klass() != ciEnv::current()->String_klass())) {908// A Class, MethodType, MethodHandle, or String.909// Unloaded condy nodes show up as T_ILLEGAL, above.910patch_state = copy_state_before();911t = new ObjectConstant(obj);912} else {913// Might be a Class, MethodType, MethodHandle, or Dynamic constant914// result, which might turn out to be an array.915if (obj->is_null_object())916t = objectNull;917else if (obj->is_array())918t = new ArrayConstant(obj->as_array());919else920t = new InstanceConstant(obj->as_instance());921}922break;923}924default : ShouldNotReachHere();925}926Value x;927if (patch_state != NULL) {928x = new Constant(t, patch_state);929} else {930x = new Constant(t);931}932push(t, append(x));933}934}935936937void GraphBuilder::load_local(ValueType* type, int index) {938Value x = state()->local_at(index);939assert(x != NULL && !x->type()->is_illegal(), "access of illegal local variable");940push(type, x);941}942943944void GraphBuilder::store_local(ValueType* type, int index) {945Value x = pop(type);946store_local(state(), x, index);947}948949950void GraphBuilder::store_local(ValueStack* state, Value x, int index) {951if (parsing_jsr()) {952// We need to do additional tracking of the location of the return953// address for jsrs since we don't handle arbitrary jsr/ret954// constructs. Here we are figuring out in which circumstances we955// need to bail out.956if (x->type()->is_address()) {957scope_data()->set_jsr_return_address_local(index);958959// Also check parent jsrs (if any) at this time to see whether960// they are using this local. We don't handle skipping over a961// ret.962for (ScopeData* cur_scope_data = scope_data()->parent();963cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope();964cur_scope_data = cur_scope_data->parent()) {965if (cur_scope_data->jsr_return_address_local() == index) {966BAILOUT("subroutine overwrites return address from previous subroutine");967}968}969} else if (index == scope_data()->jsr_return_address_local()) {970scope_data()->set_jsr_return_address_local(-1);971}972}973974state->store_local(index, round_fp(x));975}976977978void GraphBuilder::load_indexed(BasicType type) {979// In case of in block code motion in range check elimination980ValueStack* state_before = copy_state_indexed_access();981compilation()->set_has_access_indexed(true);982Value index = ipop();983Value array = apop();984Value length = NULL;985if (CSEArrayLength ||986(array->as_Constant() != NULL) ||987(array->as_AccessField() && array->as_AccessField()->field()->is_constant()) ||988(array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant()) ||989(array->as_NewMultiArray() && array->as_NewMultiArray()->dims()->at(0)->type()->is_constant())) {990length = append(new ArrayLength(array, state_before));991}992push(as_ValueType(type), append(new LoadIndexed(array, index, length, type, state_before)));993}994995996void GraphBuilder::store_indexed(BasicType type) {997// In case of in block code motion in range check elimination998ValueStack* state_before = copy_state_indexed_access();999compilation()->set_has_access_indexed(true);1000Value value = pop(as_ValueType(type));1001Value index = ipop();1002Value array = apop();1003Value length = NULL;1004if (CSEArrayLength ||1005(array->as_Constant() != NULL) ||1006(array->as_AccessField() && array->as_AccessField()->field()->is_constant()) ||1007(array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant()) ||1008(array->as_NewMultiArray() && array->as_NewMultiArray()->dims()->at(0)->type()->is_constant())) {1009length = append(new ArrayLength(array, state_before));1010}1011ciType* array_type = array->declared_type();1012bool check_boolean = false;1013if (array_type != NULL) {1014if (array_type->is_loaded() &&1015array_type->as_array_klass()->element_type()->basic_type() == T_BOOLEAN) {1016assert(type == T_BYTE, "boolean store uses bastore");1017Value mask = append(new Constant(new IntConstant(1)));1018value = append(new LogicOp(Bytecodes::_iand, value, mask));1019}1020} else if (type == T_BYTE) {1021check_boolean = true;1022}1023StoreIndexed* result = new StoreIndexed(array, index, length, type, value, state_before, check_boolean);1024append(result);1025_memory->store_value(value);10261027if (type == T_OBJECT && is_profiling()) {1028// Note that we'd collect profile data in this method if we wanted it.1029compilation()->set_would_profile(true);10301031if (profile_checkcasts()) {1032result->set_profiled_method(method());1033result->set_profiled_bci(bci());1034result->set_should_profile(true);1035}1036}1037}103810391040void GraphBuilder::stack_op(Bytecodes::Code code) {1041switch (code) {1042case Bytecodes::_pop:1043{ state()->raw_pop();1044}1045break;1046case Bytecodes::_pop2:1047{ state()->raw_pop();1048state()->raw_pop();1049}1050break;1051case Bytecodes::_dup:1052{ Value w = state()->raw_pop();1053state()->raw_push(w);1054state()->raw_push(w);1055}1056break;1057case Bytecodes::_dup_x1:1058{ Value w1 = state()->raw_pop();1059Value w2 = state()->raw_pop();1060state()->raw_push(w1);1061state()->raw_push(w2);1062state()->raw_push(w1);1063}1064break;1065case Bytecodes::_dup_x2:1066{ Value w1 = state()->raw_pop();1067Value w2 = state()->raw_pop();1068Value w3 = state()->raw_pop();1069state()->raw_push(w1);1070state()->raw_push(w3);1071state()->raw_push(w2);1072state()->raw_push(w1);1073}1074break;1075case Bytecodes::_dup2:1076{ Value w1 = state()->raw_pop();1077Value w2 = state()->raw_pop();1078state()->raw_push(w2);1079state()->raw_push(w1);1080state()->raw_push(w2);1081state()->raw_push(w1);1082}1083break;1084case Bytecodes::_dup2_x1:1085{ Value w1 = state()->raw_pop();1086Value w2 = state()->raw_pop();1087Value w3 = state()->raw_pop();1088state()->raw_push(w2);1089state()->raw_push(w1);1090state()->raw_push(w3);1091state()->raw_push(w2);1092state()->raw_push(w1);1093}1094break;1095case Bytecodes::_dup2_x2:1096{ Value w1 = state()->raw_pop();1097Value w2 = state()->raw_pop();1098Value w3 = state()->raw_pop();1099Value w4 = state()->raw_pop();1100state()->raw_push(w2);1101state()->raw_push(w1);1102state()->raw_push(w4);1103state()->raw_push(w3);1104state()->raw_push(w2);1105state()->raw_push(w1);1106}1107break;1108case Bytecodes::_swap:1109{ Value w1 = state()->raw_pop();1110Value w2 = state()->raw_pop();1111state()->raw_push(w1);1112state()->raw_push(w2);1113}1114break;1115default:1116ShouldNotReachHere();1117break;1118}1119}112011211122void GraphBuilder::arithmetic_op(ValueType* type, Bytecodes::Code code, ValueStack* state_before) {1123Value y = pop(type);1124Value x = pop(type);1125Value res = new ArithmeticOp(code, x, y, state_before);1126// Note: currently single-precision floating-point rounding on Intel is handled at the LIRGenerator level1127res = append(res);1128res = round_fp(res);1129push(type, res);1130}113111321133void GraphBuilder::negate_op(ValueType* type) {1134push(type, append(new NegateOp(pop(type))));1135}113611371138void GraphBuilder::shift_op(ValueType* type, Bytecodes::Code code) {1139Value s = ipop();1140Value x = pop(type);1141// try to simplify1142// Note: This code should go into the canonicalizer as soon as it can1143// can handle canonicalized forms that contain more than one node.1144if (CanonicalizeNodes && code == Bytecodes::_iushr) {1145// pattern: x >>> s1146IntConstant* s1 = s->type()->as_IntConstant();1147if (s1 != NULL) {1148// pattern: x >>> s1, with s1 constant1149ShiftOp* l = x->as_ShiftOp();1150if (l != NULL && l->op() == Bytecodes::_ishl) {1151// pattern: (a << b) >>> s11152IntConstant* s0 = l->y()->type()->as_IntConstant();1153if (s0 != NULL) {1154// pattern: (a << s0) >>> s11155const int s0c = s0->value() & 0x1F; // only the low 5 bits are significant for shifts1156const int s1c = s1->value() & 0x1F; // only the low 5 bits are significant for shifts1157if (s0c == s1c) {1158if (s0c == 0) {1159// pattern: (a << 0) >>> 0 => simplify to: a1160ipush(l->x());1161} else {1162// pattern: (a << s0c) >>> s0c => simplify to: a & m, with m constant1163assert(0 < s0c && s0c < BitsPerInt, "adjust code below to handle corner cases");1164const int m = (1 << (BitsPerInt - s0c)) - 1;1165Value s = append(new Constant(new IntConstant(m)));1166ipush(append(new LogicOp(Bytecodes::_iand, l->x(), s)));1167}1168return;1169}1170}1171}1172}1173}1174// could not simplify1175push(type, append(new ShiftOp(code, x, s)));1176}117711781179void GraphBuilder::logic_op(ValueType* type, Bytecodes::Code code) {1180Value y = pop(type);1181Value x = pop(type);1182push(type, append(new LogicOp(code, x, y)));1183}118411851186void GraphBuilder::compare_op(ValueType* type, Bytecodes::Code code) {1187ValueStack* state_before = copy_state_before();1188Value y = pop(type);1189Value x = pop(type);1190ipush(append(new CompareOp(code, x, y, state_before)));1191}119211931194void GraphBuilder::convert(Bytecodes::Code op, BasicType from, BasicType to) {1195push(as_ValueType(to), append(new Convert(op, pop(as_ValueType(from)), as_ValueType(to))));1196}119711981199void GraphBuilder::increment() {1200int index = stream()->get_index();1201int delta = stream()->is_wide() ? (signed short)Bytes::get_Java_u2(stream()->cur_bcp() + 4) : (signed char)(stream()->cur_bcp()[2]);1202load_local(intType, index);1203ipush(append(new Constant(new IntConstant(delta))));1204arithmetic_op(intType, Bytecodes::_iadd);1205store_local(intType, index);1206}120712081209void GraphBuilder::_goto(int from_bci, int to_bci) {1210Goto *x = new Goto(block_at(to_bci), to_bci <= from_bci);1211if (is_profiling()) {1212compilation()->set_would_profile(true);1213x->set_profiled_bci(bci());1214if (profile_branches()) {1215x->set_profiled_method(method());1216x->set_should_profile(true);1217}1218}1219append(x);1220}122112221223void GraphBuilder::if_node(Value x, If::Condition cond, Value y, ValueStack* state_before) {1224BlockBegin* tsux = block_at(stream()->get_dest());1225BlockBegin* fsux = block_at(stream()->next_bci());1226bool is_bb = tsux->bci() < stream()->cur_bci() || fsux->bci() < stream()->cur_bci();1227// In case of loop invariant code motion or predicate insertion1228// before the body of a loop the state is needed1229Instruction *i = append(new If(x, cond, false, y, tsux, fsux, (is_bb || compilation()->is_optimistic()) ? state_before : NULL, is_bb));12301231assert(i->as_Goto() == NULL ||1232(i->as_Goto()->sux_at(0) == tsux && i->as_Goto()->is_safepoint() == tsux->bci() < stream()->cur_bci()) ||1233(i->as_Goto()->sux_at(0) == fsux && i->as_Goto()->is_safepoint() == fsux->bci() < stream()->cur_bci()),1234"safepoint state of Goto returned by canonicalizer incorrect");12351236if (is_profiling()) {1237If* if_node = i->as_If();1238if (if_node != NULL) {1239// Note that we'd collect profile data in this method if we wanted it.1240compilation()->set_would_profile(true);1241// At level 2 we need the proper bci to count backedges1242if_node->set_profiled_bci(bci());1243if (profile_branches()) {1244// Successors can be rotated by the canonicalizer, check for this case.1245if_node->set_profiled_method(method());1246if_node->set_should_profile(true);1247if (if_node->tsux() == fsux) {1248if_node->set_swapped(true);1249}1250}1251return;1252}12531254// Check if this If was reduced to Goto.1255Goto *goto_node = i->as_Goto();1256if (goto_node != NULL) {1257compilation()->set_would_profile(true);1258goto_node->set_profiled_bci(bci());1259if (profile_branches()) {1260goto_node->set_profiled_method(method());1261goto_node->set_should_profile(true);1262// Find out which successor is used.1263if (goto_node->default_sux() == tsux) {1264goto_node->set_direction(Goto::taken);1265} else if (goto_node->default_sux() == fsux) {1266goto_node->set_direction(Goto::not_taken);1267} else {1268ShouldNotReachHere();1269}1270}1271return;1272}1273}1274}127512761277void GraphBuilder::if_zero(ValueType* type, If::Condition cond) {1278Value y = append(new Constant(intZero));1279ValueStack* state_before = copy_state_before();1280Value x = ipop();1281if_node(x, cond, y, state_before);1282}128312841285void GraphBuilder::if_null(ValueType* type, If::Condition cond) {1286Value y = append(new Constant(objectNull));1287ValueStack* state_before = copy_state_before();1288Value x = apop();1289if_node(x, cond, y, state_before);1290}129112921293void GraphBuilder::if_same(ValueType* type, If::Condition cond) {1294ValueStack* state_before = copy_state_before();1295Value y = pop(type);1296Value x = pop(type);1297if_node(x, cond, y, state_before);1298}129913001301void GraphBuilder::jsr(int dest) {1302// We only handle well-formed jsrs (those which are "block-structured").1303// If the bytecodes are strange (jumping out of a jsr block) then we1304// might end up trying to re-parse a block containing a jsr which1305// has already been activated. Watch for this case and bail out.1306for (ScopeData* cur_scope_data = scope_data();1307cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope();1308cur_scope_data = cur_scope_data->parent()) {1309if (cur_scope_data->jsr_entry_bci() == dest) {1310BAILOUT("too-complicated jsr/ret structure");1311}1312}13131314push(addressType, append(new Constant(new AddressConstant(next_bci()))));1315if (!try_inline_jsr(dest)) {1316return; // bailed out while parsing and inlining subroutine1317}1318}131913201321void GraphBuilder::ret(int local_index) {1322if (!parsing_jsr()) BAILOUT("ret encountered while not parsing subroutine");13231324if (local_index != scope_data()->jsr_return_address_local()) {1325BAILOUT("can not handle complicated jsr/ret constructs");1326}13271328// Rets simply become (NON-SAFEPOINT) gotos to the jsr continuation1329append(new Goto(scope_data()->jsr_continuation(), false));1330}133113321333void GraphBuilder::table_switch() {1334Bytecode_tableswitch sw(stream());1335const int l = sw.length();1336if (CanonicalizeNodes && l == 1 && compilation()->env()->comp_level() != CompLevel_full_profile) {1337// total of 2 successors => use If instead of switch1338// Note: This code should go into the canonicalizer as soon as it can1339// can handle canonicalized forms that contain more than one node.1340Value key = append(new Constant(new IntConstant(sw.low_key())));1341BlockBegin* tsux = block_at(bci() + sw.dest_offset_at(0));1342BlockBegin* fsux = block_at(bci() + sw.default_offset());1343bool is_bb = tsux->bci() < bci() || fsux->bci() < bci();1344// In case of loop invariant code motion or predicate insertion1345// before the body of a loop the state is needed1346ValueStack* state_before = copy_state_if_bb(is_bb);1347append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb));1348} else {1349// collect successors1350BlockList* sux = new BlockList(l + 1, NULL);1351int i;1352bool has_bb = false;1353for (i = 0; i < l; i++) {1354sux->at_put(i, block_at(bci() + sw.dest_offset_at(i)));1355if (sw.dest_offset_at(i) < 0) has_bb = true;1356}1357// add default successor1358if (sw.default_offset() < 0) has_bb = true;1359sux->at_put(i, block_at(bci() + sw.default_offset()));1360// In case of loop invariant code motion or predicate insertion1361// before the body of a loop the state is needed1362ValueStack* state_before = copy_state_if_bb(has_bb);1363Instruction* res = append(new TableSwitch(ipop(), sux, sw.low_key(), state_before, has_bb));1364#ifdef ASSERT1365if (res->as_Goto()) {1366for (i = 0; i < l; i++) {1367if (sux->at(i) == res->as_Goto()->sux_at(0)) {1368assert(res->as_Goto()->is_safepoint() == sw.dest_offset_at(i) < 0, "safepoint state of Goto returned by canonicalizer incorrect");1369}1370}1371}1372#endif1373}1374}137513761377void GraphBuilder::lookup_switch() {1378Bytecode_lookupswitch sw(stream());1379const int l = sw.number_of_pairs();1380if (CanonicalizeNodes && l == 1 && compilation()->env()->comp_level() != CompLevel_full_profile) {1381// total of 2 successors => use If instead of switch1382// Note: This code should go into the canonicalizer as soon as it can1383// can handle canonicalized forms that contain more than one node.1384// simplify to If1385LookupswitchPair pair = sw.pair_at(0);1386Value key = append(new Constant(new IntConstant(pair.match())));1387BlockBegin* tsux = block_at(bci() + pair.offset());1388BlockBegin* fsux = block_at(bci() + sw.default_offset());1389bool is_bb = tsux->bci() < bci() || fsux->bci() < bci();1390// In case of loop invariant code motion or predicate insertion1391// before the body of a loop the state is needed1392ValueStack* state_before = copy_state_if_bb(is_bb);;1393append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb));1394} else {1395// collect successors & keys1396BlockList* sux = new BlockList(l + 1, NULL);1397intArray* keys = new intArray(l, l, 0);1398int i;1399bool has_bb = false;1400for (i = 0; i < l; i++) {1401LookupswitchPair pair = sw.pair_at(i);1402if (pair.offset() < 0) has_bb = true;1403sux->at_put(i, block_at(bci() + pair.offset()));1404keys->at_put(i, pair.match());1405}1406// add default successor1407if (sw.default_offset() < 0) has_bb = true;1408sux->at_put(i, block_at(bci() + sw.default_offset()));1409// In case of loop invariant code motion or predicate insertion1410// before the body of a loop the state is needed1411ValueStack* state_before = copy_state_if_bb(has_bb);1412Instruction* res = append(new LookupSwitch(ipop(), sux, keys, state_before, has_bb));1413#ifdef ASSERT1414if (res->as_Goto()) {1415for (i = 0; i < l; i++) {1416if (sux->at(i) == res->as_Goto()->sux_at(0)) {1417assert(res->as_Goto()->is_safepoint() == sw.pair_at(i).offset() < 0, "safepoint state of Goto returned by canonicalizer incorrect");1418}1419}1420}1421#endif1422}1423}14241425void GraphBuilder::call_register_finalizer() {1426// If the receiver requires finalization then emit code to perform1427// the registration on return.14281429// Gather some type information about the receiver1430Value receiver = state()->local_at(0);1431assert(receiver != NULL, "must have a receiver");1432ciType* declared_type = receiver->declared_type();1433ciType* exact_type = receiver->exact_type();1434if (exact_type == NULL &&1435receiver->as_Local() &&1436receiver->as_Local()->java_index() == 0) {1437ciInstanceKlass* ik = compilation()->method()->holder();1438if (ik->is_final()) {1439exact_type = ik;1440} else if (UseCHA && !(ik->has_subklass() || ik->is_interface())) {1441// test class is leaf class1442compilation()->dependency_recorder()->assert_leaf_type(ik);1443exact_type = ik;1444} else {1445declared_type = ik;1446}1447}14481449// see if we know statically that registration isn't required1450bool needs_check = true;1451if (exact_type != NULL) {1452needs_check = exact_type->as_instance_klass()->has_finalizer();1453} else if (declared_type != NULL) {1454ciInstanceKlass* ik = declared_type->as_instance_klass();1455if (!Dependencies::has_finalizable_subclass(ik)) {1456compilation()->dependency_recorder()->assert_has_no_finalizable_subclasses(ik);1457needs_check = false;1458}1459}14601461if (needs_check) {1462// Perform the registration of finalizable objects.1463ValueStack* state_before = copy_state_for_exception();1464load_local(objectType, 0);1465append_split(new Intrinsic(voidType, vmIntrinsics::_Object_init,1466state()->pop_arguments(1),1467true, state_before, true));1468}1469}147014711472void GraphBuilder::method_return(Value x, bool ignore_return) {1473if (RegisterFinalizersAtInit &&1474method()->intrinsic_id() == vmIntrinsics::_Object_init) {1475call_register_finalizer();1476}14771478// The conditions for a memory barrier are described in Parse::do_exits().1479bool need_mem_bar = false;1480if (method()->name() == ciSymbols::object_initializer_name() &&1481(scope()->wrote_final() ||1482(AlwaysSafeConstructors && scope()->wrote_fields()) ||1483(support_IRIW_for_not_multiple_copy_atomic_cpu && scope()->wrote_volatile()))) {1484need_mem_bar = true;1485}14861487BasicType bt = method()->return_type()->basic_type();1488switch (bt) {1489case T_BYTE:1490{1491Value shift = append(new Constant(new IntConstant(24)));1492x = append(new ShiftOp(Bytecodes::_ishl, x, shift));1493x = append(new ShiftOp(Bytecodes::_ishr, x, shift));1494break;1495}1496case T_SHORT:1497{1498Value shift = append(new Constant(new IntConstant(16)));1499x = append(new ShiftOp(Bytecodes::_ishl, x, shift));1500x = append(new ShiftOp(Bytecodes::_ishr, x, shift));1501break;1502}1503case T_CHAR:1504{1505Value mask = append(new Constant(new IntConstant(0xFFFF)));1506x = append(new LogicOp(Bytecodes::_iand, x, mask));1507break;1508}1509case T_BOOLEAN:1510{1511Value mask = append(new Constant(new IntConstant(1)));1512x = append(new LogicOp(Bytecodes::_iand, x, mask));1513break;1514}1515default:1516break;1517}15181519// Check to see whether we are inlining. If so, Return1520// instructions become Gotos to the continuation point.1521if (continuation() != NULL) {15221523int invoke_bci = state()->caller_state()->bci();15241525if (x != NULL && !ignore_return) {1526ciMethod* caller = state()->scope()->caller()->method();1527Bytecodes::Code invoke_raw_bc = caller->raw_code_at_bci(invoke_bci);1528if (invoke_raw_bc == Bytecodes::_invokehandle || invoke_raw_bc == Bytecodes::_invokedynamic) {1529ciType* declared_ret_type = caller->get_declared_signature_at_bci(invoke_bci)->return_type();1530if (declared_ret_type->is_klass() && x->exact_type() == NULL &&1531x->declared_type() != declared_ret_type && declared_ret_type != compilation()->env()->Object_klass()) {1532x = append(new TypeCast(declared_ret_type->as_klass(), x, copy_state_before()));1533}1534}1535}15361537assert(!method()->is_synchronized() || InlineSynchronizedMethods, "can not inline synchronized methods yet");15381539if (compilation()->env()->dtrace_method_probes()) {1540// Report exit from inline methods1541Values* args = new Values(1);1542args->push(append(new Constant(new MethodConstant(method()))));1543append(new RuntimeCall(voidType, "dtrace_method_exit", CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), args));1544}15451546// If the inlined method is synchronized, the monitor must be1547// released before we jump to the continuation block.1548if (method()->is_synchronized()) {1549assert(state()->locks_size() == 1, "receiver must be locked here");1550monitorexit(state()->lock_at(0), SynchronizationEntryBCI);1551}15521553if (need_mem_bar) {1554append(new MemBar(lir_membar_storestore));1555}15561557// State at end of inlined method is the state of the caller1558// without the method parameters on stack, including the1559// return value, if any, of the inlined method on operand stack.1560set_state(state()->caller_state()->copy_for_parsing());1561if (x != NULL) {1562if (!ignore_return) {1563state()->push(x->type(), x);1564}1565if (profile_return() && x->type()->is_object_kind()) {1566ciMethod* caller = state()->scope()->method();1567profile_return_type(x, method(), caller, invoke_bci);1568}1569}1570Goto* goto_callee = new Goto(continuation(), false);15711572// See whether this is the first return; if so, store off some1573// of the state for later examination1574if (num_returns() == 0) {1575set_inline_cleanup_info();1576}15771578// The current bci() is in the wrong scope, so use the bci() of1579// the continuation point.1580append_with_bci(goto_callee, scope_data()->continuation()->bci());1581incr_num_returns();1582return;1583}15841585state()->truncate_stack(0);1586if (method()->is_synchronized()) {1587// perform the unlocking before exiting the method1588Value receiver;1589if (!method()->is_static()) {1590receiver = _initial_state->local_at(0);1591} else {1592receiver = append(new Constant(new ClassConstant(method()->holder())));1593}1594append_split(new MonitorExit(receiver, state()->unlock()));1595}15961597if (need_mem_bar) {1598append(new MemBar(lir_membar_storestore));1599}16001601assert(!ignore_return, "Ignoring return value works only for inlining");1602append(new Return(x));1603}16041605Value GraphBuilder::make_constant(ciConstant field_value, ciField* field) {1606if (!field_value.is_valid()) return NULL;16071608BasicType field_type = field_value.basic_type();1609ValueType* value = as_ValueType(field_value);16101611// Attach dimension info to stable arrays.1612if (FoldStableValues &&1613field->is_stable() && field_type == T_ARRAY && !field_value.is_null_or_zero()) {1614ciArray* array = field_value.as_object()->as_array();1615jint dimension = field->type()->as_array_klass()->dimension();1616value = new StableArrayConstant(array, dimension);1617}16181619switch (field_type) {1620case T_ARRAY:1621case T_OBJECT:1622if (field_value.as_object()->should_be_constant()) {1623return new Constant(value);1624}1625return NULL; // Not a constant.1626default:1627return new Constant(value);1628}1629}16301631void GraphBuilder::access_field(Bytecodes::Code code) {1632bool will_link;1633ciField* field = stream()->get_field(will_link);1634ciInstanceKlass* holder = field->holder();1635BasicType field_type = field->type()->basic_type();1636ValueType* type = as_ValueType(field_type);1637// call will_link again to determine if the field is valid.1638const bool needs_patching = !holder->is_loaded() ||1639!field->will_link(method(), code) ||1640PatchALot;16411642ValueStack* state_before = NULL;1643if (!holder->is_initialized() || needs_patching) {1644// save state before instruction for debug info when1645// deoptimization happens during patching1646state_before = copy_state_before();1647}16481649Value obj = NULL;1650if (code == Bytecodes::_getstatic || code == Bytecodes::_putstatic) {1651if (state_before != NULL) {1652// build a patching constant1653obj = new Constant(new InstanceConstant(holder->java_mirror()), state_before);1654} else {1655obj = new Constant(new InstanceConstant(holder->java_mirror()));1656}1657}16581659if (field->is_final() && (code == Bytecodes::_putfield)) {1660scope()->set_wrote_final();1661}16621663if (code == Bytecodes::_putfield) {1664scope()->set_wrote_fields();1665if (field->is_volatile()) {1666scope()->set_wrote_volatile();1667}1668}16691670const int offset = !needs_patching ? field->offset() : -1;1671switch (code) {1672case Bytecodes::_getstatic: {1673// check for compile-time constants, i.e., initialized static final fields1674Value constant = NULL;1675if (field->is_static_constant() && !PatchALot) {1676ciConstant field_value = field->constant_value();1677assert(!field->is_stable() || !field_value.is_null_or_zero(),1678"stable static w/ default value shouldn't be a constant");1679constant = make_constant(field_value, field);1680}1681if (constant != NULL) {1682push(type, append(constant));1683} else {1684if (state_before == NULL) {1685state_before = copy_state_for_exception();1686}1687push(type, append(new LoadField(append(obj), offset, field, true,1688state_before, needs_patching)));1689}1690break;1691}1692case Bytecodes::_putstatic: {1693Value val = pop(type);1694if (state_before == NULL) {1695state_before = copy_state_for_exception();1696}1697if (field->type()->basic_type() == T_BOOLEAN) {1698Value mask = append(new Constant(new IntConstant(1)));1699val = append(new LogicOp(Bytecodes::_iand, val, mask));1700}1701append(new StoreField(append(obj), offset, field, val, true, state_before, needs_patching));1702break;1703}1704case Bytecodes::_getfield: {1705// Check for compile-time constants, i.e., trusted final non-static fields.1706Value constant = NULL;1707obj = apop();1708ObjectType* obj_type = obj->type()->as_ObjectType();1709if (field->is_constant() && obj_type->is_constant() && !PatchALot) {1710ciObject* const_oop = obj_type->constant_value();1711if (!const_oop->is_null_object() && const_oop->is_loaded()) {1712ciConstant field_value = field->constant_value_of(const_oop);1713if (field_value.is_valid()) {1714constant = make_constant(field_value, field);1715// For CallSite objects add a dependency for invalidation of the optimization.1716if (field->is_call_site_target()) {1717ciCallSite* call_site = const_oop->as_call_site();1718if (!call_site->is_fully_initialized_constant_call_site()) {1719ciMethodHandle* target = field_value.as_object()->as_method_handle();1720dependency_recorder()->assert_call_site_target_value(call_site, target);1721}1722}1723}1724}1725}1726if (constant != NULL) {1727push(type, append(constant));1728} else {1729if (state_before == NULL) {1730state_before = copy_state_for_exception();1731}1732LoadField* load = new LoadField(obj, offset, field, false, state_before, needs_patching);1733Value replacement = !needs_patching ? _memory->load(load) : load;1734if (replacement != load) {1735assert(replacement->is_linked() || !replacement->can_be_linked(), "should already by linked");1736// Writing an (integer) value to a boolean, byte, char or short field includes an implicit narrowing1737// conversion. Emit an explicit conversion here to get the correct field value after the write.1738BasicType bt = field->type()->basic_type();1739switch (bt) {1740case T_BOOLEAN:1741case T_BYTE:1742replacement = append(new Convert(Bytecodes::_i2b, replacement, as_ValueType(bt)));1743break;1744case T_CHAR:1745replacement = append(new Convert(Bytecodes::_i2c, replacement, as_ValueType(bt)));1746break;1747case T_SHORT:1748replacement = append(new Convert(Bytecodes::_i2s, replacement, as_ValueType(bt)));1749break;1750default:1751break;1752}1753push(type, replacement);1754} else {1755push(type, append(load));1756}1757}1758break;1759}1760case Bytecodes::_putfield: {1761Value val = pop(type);1762obj = apop();1763if (state_before == NULL) {1764state_before = copy_state_for_exception();1765}1766if (field->type()->basic_type() == T_BOOLEAN) {1767Value mask = append(new Constant(new IntConstant(1)));1768val = append(new LogicOp(Bytecodes::_iand, val, mask));1769}1770StoreField* store = new StoreField(obj, offset, field, val, false, state_before, needs_patching);1771if (!needs_patching) store = _memory->store(store);1772if (store != NULL) {1773append(store);1774}1775break;1776}1777default:1778ShouldNotReachHere();1779break;1780}1781}178217831784Dependencies* GraphBuilder::dependency_recorder() const {1785assert(DeoptC1, "need debug information");1786return compilation()->dependency_recorder();1787}17881789// How many arguments do we want to profile?1790Values* GraphBuilder::args_list_for_profiling(ciMethod* target, int& start, bool may_have_receiver) {1791int n = 0;1792bool has_receiver = may_have_receiver && Bytecodes::has_receiver(method()->java_code_at_bci(bci()));1793start = has_receiver ? 1 : 0;1794if (profile_arguments()) {1795ciProfileData* data = method()->method_data()->bci_to_data(bci());1796if (data != NULL && (data->is_CallTypeData() || data->is_VirtualCallTypeData())) {1797n = data->is_CallTypeData() ? data->as_CallTypeData()->number_of_arguments() : data->as_VirtualCallTypeData()->number_of_arguments();1798}1799}1800// If we are inlining then we need to collect arguments to profile parameters for the target1801if (profile_parameters() && target != NULL) {1802if (target->method_data() != NULL && target->method_data()->parameters_type_data() != NULL) {1803// The receiver is profiled on method entry so it's included in1804// the number of parameters but here we're only interested in1805// actual arguments.1806n = MAX2(n, target->method_data()->parameters_type_data()->number_of_parameters() - start);1807}1808}1809if (n > 0) {1810return new Values(n);1811}1812return NULL;1813}18141815void GraphBuilder::check_args_for_profiling(Values* obj_args, int expected) {1816#ifdef ASSERT1817bool ignored_will_link;1818ciSignature* declared_signature = NULL;1819ciMethod* real_target = method()->get_method_at_bci(bci(), ignored_will_link, &declared_signature);1820assert(expected == obj_args->max_length() || real_target->is_method_handle_intrinsic(), "missed on arg?");1821#endif1822}18231824// Collect arguments that we want to profile in a list1825Values* GraphBuilder::collect_args_for_profiling(Values* args, ciMethod* target, bool may_have_receiver) {1826int start = 0;1827Values* obj_args = args_list_for_profiling(target, start, may_have_receiver);1828if (obj_args == NULL) {1829return NULL;1830}1831int s = obj_args->max_length();1832// if called through method handle invoke, some arguments may have been popped1833for (int i = start, j = 0; j < s && i < args->length(); i++) {1834if (args->at(i)->type()->is_object_kind()) {1835obj_args->push(args->at(i));1836j++;1837}1838}1839check_args_for_profiling(obj_args, s);1840return obj_args;1841}184218431844void GraphBuilder::invoke(Bytecodes::Code code) {1845bool will_link;1846ciSignature* declared_signature = NULL;1847ciMethod* target = stream()->get_method(will_link, &declared_signature);1848ciKlass* holder = stream()->get_declared_method_holder();1849const Bytecodes::Code bc_raw = stream()->cur_bc_raw();1850assert(declared_signature != NULL, "cannot be null");1851assert(will_link == target->is_loaded(), "");18521853ciInstanceKlass* klass = target->holder();1854assert(!target->is_loaded() || klass->is_loaded(), "loaded target must imply loaded klass");18551856// check if CHA possible: if so, change the code to invoke_special1857ciInstanceKlass* calling_klass = method()->holder();1858ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder);1859ciInstanceKlass* actual_recv = callee_holder;18601861CompileLog* log = compilation()->log();1862if (log != NULL)1863log->elem("call method='%d' instr='%s'",1864log->identify(target),1865Bytecodes::name(code));18661867// invoke-special-super1868if (bc_raw == Bytecodes::_invokespecial && !target->is_object_initializer()) {1869ciInstanceKlass* sender_klass = calling_klass;1870if (sender_klass->is_interface()) {1871int index = state()->stack_size() - (target->arg_size_no_receiver() + 1);1872Value receiver = state()->stack_at(index);1873CheckCast* c = new CheckCast(sender_klass, receiver, copy_state_before());1874c->set_invokespecial_receiver_check();1875state()->stack_at_put(index, append_split(c));1876}1877}18781879// Some methods are obviously bindable without any type checks so1880// convert them directly to an invokespecial or invokestatic.1881if (target->is_loaded() && !target->is_abstract() && target->can_be_statically_bound()) {1882switch (bc_raw) {1883case Bytecodes::_invokevirtual:1884code = Bytecodes::_invokespecial;1885break;1886case Bytecodes::_invokehandle:1887code = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokespecial;1888break;1889default:1890break;1891}1892} else {1893if (bc_raw == Bytecodes::_invokehandle) {1894assert(!will_link, "should come here only for unlinked call");1895code = Bytecodes::_invokespecial;1896}1897}18981899// Push appendix argument (MethodType, CallSite, etc.), if one.1900bool patch_for_appendix = false;1901int patching_appendix_arg = 0;1902if (Bytecodes::has_optional_appendix(bc_raw) && (!will_link || PatchALot)) {1903Value arg = append(new Constant(new ObjectConstant(compilation()->env()->unloaded_ciinstance()), copy_state_before()));1904apush(arg);1905patch_for_appendix = true;1906patching_appendix_arg = (will_link && stream()->has_appendix()) ? 0 : 1;1907} else if (stream()->has_appendix()) {1908ciObject* appendix = stream()->get_appendix();1909Value arg = append(new Constant(new ObjectConstant(appendix)));1910apush(arg);1911}19121913ciMethod* cha_monomorphic_target = NULL;1914ciMethod* exact_target = NULL;1915Value better_receiver = NULL;1916if (UseCHA && DeoptC1 && target->is_loaded() &&1917!(// %%% FIXME: Are both of these relevant?1918target->is_method_handle_intrinsic() ||1919target->is_compiled_lambda_form()) &&1920!patch_for_appendix) {1921Value receiver = NULL;1922ciInstanceKlass* receiver_klass = NULL;1923bool type_is_exact = false;1924// try to find a precise receiver type1925if (will_link && !target->is_static()) {1926int index = state()->stack_size() - (target->arg_size_no_receiver() + 1);1927receiver = state()->stack_at(index);1928ciType* type = receiver->exact_type();1929if (type != NULL && type->is_loaded() &&1930type->is_instance_klass() && !type->as_instance_klass()->is_interface()) {1931receiver_klass = (ciInstanceKlass*) type;1932type_is_exact = true;1933}1934if (type == NULL) {1935type = receiver->declared_type();1936if (type != NULL && type->is_loaded() &&1937type->is_instance_klass() && !type->as_instance_klass()->is_interface()) {1938receiver_klass = (ciInstanceKlass*) type;1939if (receiver_klass->is_leaf_type() && !receiver_klass->is_final()) {1940// Insert a dependency on this type since1941// find_monomorphic_target may assume it's already done.1942dependency_recorder()->assert_leaf_type(receiver_klass);1943type_is_exact = true;1944}1945}1946}1947}1948if (receiver_klass != NULL && type_is_exact &&1949receiver_klass->is_loaded() && code != Bytecodes::_invokespecial) {1950// If we have the exact receiver type we can bind directly to1951// the method to call.1952exact_target = target->resolve_invoke(calling_klass, receiver_klass);1953if (exact_target != NULL) {1954target = exact_target;1955code = Bytecodes::_invokespecial;1956}1957}1958if (receiver_klass != NULL &&1959receiver_klass->is_subtype_of(actual_recv) &&1960actual_recv->is_initialized()) {1961actual_recv = receiver_klass;1962}19631964if ((code == Bytecodes::_invokevirtual && callee_holder->is_initialized()) ||1965(code == Bytecodes::_invokeinterface && callee_holder->is_initialized() && !actual_recv->is_interface())) {1966// Use CHA on the receiver to select a more precise method.1967cha_monomorphic_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv);1968} else if (code == Bytecodes::_invokeinterface && callee_holder->is_loaded() && receiver != NULL) {1969assert(callee_holder->is_interface(), "invokeinterface to non interface?");1970// If there is only one implementor of this interface then we1971// may be able bind this invoke directly to the implementing1972// klass but we need both a dependence on the single interface1973// and on the method we bind to. Additionally since all we know1974// about the receiver type is the it's supposed to implement the1975// interface we have to insert a check that it's the class we1976// expect. Interface types are not checked by the verifier so1977// they are roughly equivalent to Object.1978// The number of implementors for declared_interface is less or1979// equal to the number of implementors for target->holder() so1980// if number of implementors of target->holder() == 1 then1981// number of implementors for decl_interface is 0 or 1. If1982// it's 0 then no class implements decl_interface and there's1983// no point in inlining.1984ciInstanceKlass* declared_interface = callee_holder;1985ciInstanceKlass* singleton = declared_interface->unique_implementor();1986if (singleton != NULL) {1987assert(singleton != declared_interface, "not a unique implementor");1988cha_monomorphic_target = target->find_monomorphic_target(calling_klass, declared_interface, singleton);1989if (cha_monomorphic_target != NULL) {1990if (cha_monomorphic_target->holder() != compilation()->env()->Object_klass()) {1991// If CHA is able to bind this invoke then update the class1992// to match that class, otherwise klass will refer to the1993// interface.1994klass = cha_monomorphic_target->holder();1995actual_recv = declared_interface;19961997// insert a check it's really the expected class.1998CheckCast* c = new CheckCast(klass, receiver, copy_state_for_exception());1999c->set_incompatible_class_change_check();2000c->set_direct_compare(klass->is_final());2001// pass the result of the checkcast so that the compiler has2002// more accurate type info in the inlinee2003better_receiver = append_split(c);2004} else {2005cha_monomorphic_target = NULL; // subtype check against Object is useless2006}2007}2008}2009}2010}20112012if (cha_monomorphic_target != NULL) {2013assert(!target->can_be_statically_bound() || target == cha_monomorphic_target, "");2014assert(!cha_monomorphic_target->is_abstract(), "");2015if (!cha_monomorphic_target->can_be_statically_bound(actual_recv)) {2016// If we inlined because CHA revealed only a single target method,2017// then we are dependent on that target method not getting overridden2018// by dynamic class loading. Be sure to test the "static" receiver2019// dest_method here, as opposed to the actual receiver, which may2020// falsely lead us to believe that the receiver is final or private.2021dependency_recorder()->assert_unique_concrete_method(actual_recv, cha_monomorphic_target, callee_holder, target);2022}2023code = Bytecodes::_invokespecial;2024}20252026// check if we could do inlining2027if (!PatchALot && Inline && target->is_loaded() && callee_holder->is_linked() && !patch_for_appendix) {2028// callee is known => check if we have static binding2029if ((code == Bytecodes::_invokestatic && callee_holder->is_initialized()) || // invokestatic involves an initialization barrier on resolved klass2030code == Bytecodes::_invokespecial ||2031(code == Bytecodes::_invokevirtual && target->is_final_method()) ||2032code == Bytecodes::_invokedynamic) {2033// static binding => check if callee is ok2034ciMethod* inline_target = (cha_monomorphic_target != NULL) ? cha_monomorphic_target : target;2035bool holder_known = (cha_monomorphic_target != NULL) || (exact_target != NULL);2036bool success = try_inline(inline_target, holder_known, false /* ignore_return */, code, better_receiver);20372038CHECK_BAILOUT();2039clear_inline_bailout();20402041if (success) {2042// Register dependence if JVMTI has either breakpoint2043// setting or hotswapping of methods capabilities since they may2044// cause deoptimization.2045if (compilation()->env()->jvmti_can_hotswap_or_post_breakpoint()) {2046dependency_recorder()->assert_evol_method(inline_target);2047}2048return;2049}2050} else {2051print_inlining(target, "no static binding", /*success*/ false);2052}2053} else {2054print_inlining(target, "not inlineable", /*success*/ false);2055}20562057// If we attempted an inline which did not succeed because of a2058// bailout during construction of the callee graph, the entire2059// compilation has to be aborted. This is fairly rare and currently2060// seems to only occur for jasm-generated classes which contain2061// jsr/ret pairs which are not associated with finally clauses and2062// do not have exception handlers in the containing method, and are2063// therefore not caught early enough to abort the inlining without2064// corrupting the graph. (We currently bail out with a non-empty2065// stack at a ret in these situations.)2066CHECK_BAILOUT();20672068// inlining not successful => standard invoke2069ValueType* result_type = as_ValueType(declared_signature->return_type());2070ValueStack* state_before = copy_state_exhandling();20712072// The bytecode (code) might change in this method so we are checking this very late.2073const bool has_receiver =2074code == Bytecodes::_invokespecial ||2075code == Bytecodes::_invokevirtual ||2076code == Bytecodes::_invokeinterface;2077Values* args = state()->pop_arguments(target->arg_size_no_receiver() + patching_appendix_arg);2078Value recv = has_receiver ? apop() : NULL;20792080// A null check is required here (when there is a receiver) for any of the following cases2081// - invokespecial, always need a null check.2082// - invokevirtual, when the target is final and loaded. Calls to final targets will become optimized2083// and require null checking. If the target is loaded a null check is emitted here.2084// If the target isn't loaded the null check must happen after the call resolution. We achieve that2085// by using the target methods unverified entry point (see CompiledIC::compute_monomorphic_entry).2086// (The JVM specification requires that LinkageError must be thrown before a NPE. An unloaded target may2087// potentially fail, and can't have the null check before the resolution.)2088// - A call that will be profiled. (But we can't add a null check when the target is unloaded, by the same2089// reason as above, so calls with a receiver to unloaded targets can't be profiled.)2090//2091// Normal invokevirtual will perform the null check during lookup20922093bool need_null_check = (code == Bytecodes::_invokespecial) ||2094(target->is_loaded() && (target->is_final_method() || (is_profiling() && profile_calls())));20952096if (need_null_check) {2097if (recv != NULL) {2098null_check(recv);2099}21002101if (is_profiling()) {2102// Note that we'd collect profile data in this method if we wanted it.2103compilation()->set_would_profile(true);21042105if (profile_calls()) {2106assert(cha_monomorphic_target == NULL || exact_target == NULL, "both can not be set");2107ciKlass* target_klass = NULL;2108if (cha_monomorphic_target != NULL) {2109target_klass = cha_monomorphic_target->holder();2110} else if (exact_target != NULL) {2111target_klass = exact_target->holder();2112}2113profile_call(target, recv, target_klass, collect_args_for_profiling(args, NULL, false), false);2114}2115}2116}21172118Invoke* result = new Invoke(code, result_type, recv, args, target, state_before);2119// push result2120append_split(result);21212122if (result_type != voidType) {2123push(result_type, round_fp(result));2124}2125if (profile_return() && result_type->is_object_kind()) {2126profile_return_type(result, target);2127}2128}212921302131void GraphBuilder::new_instance(int klass_index) {2132ValueStack* state_before = copy_state_exhandling();2133bool will_link;2134ciKlass* klass = stream()->get_klass(will_link);2135assert(klass->is_instance_klass(), "must be an instance klass");2136NewInstance* new_instance = new NewInstance(klass->as_instance_klass(), state_before, stream()->is_unresolved_klass());2137_memory->new_instance(new_instance);2138apush(append_split(new_instance));2139}214021412142void GraphBuilder::new_type_array() {2143ValueStack* state_before = copy_state_exhandling();2144apush(append_split(new NewTypeArray(ipop(), (BasicType)stream()->get_index(), state_before)));2145}214621472148void GraphBuilder::new_object_array() {2149bool will_link;2150ciKlass* klass = stream()->get_klass(will_link);2151ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_exhandling();2152NewArray* n = new NewObjectArray(klass, ipop(), state_before);2153apush(append_split(n));2154}215521562157bool GraphBuilder::direct_compare(ciKlass* k) {2158if (k->is_loaded() && k->is_instance_klass() && !UseSlowPath) {2159ciInstanceKlass* ik = k->as_instance_klass();2160if (ik->is_final()) {2161return true;2162} else {2163if (DeoptC1 && UseCHA && !(ik->has_subklass() || ik->is_interface())) {2164// test class is leaf class2165dependency_recorder()->assert_leaf_type(ik);2166return true;2167}2168}2169}2170return false;2171}217221732174void GraphBuilder::check_cast(int klass_index) {2175bool will_link;2176ciKlass* klass = stream()->get_klass(will_link);2177ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_for_exception();2178CheckCast* c = new CheckCast(klass, apop(), state_before);2179apush(append_split(c));2180c->set_direct_compare(direct_compare(klass));21812182if (is_profiling()) {2183// Note that we'd collect profile data in this method if we wanted it.2184compilation()->set_would_profile(true);21852186if (profile_checkcasts()) {2187c->set_profiled_method(method());2188c->set_profiled_bci(bci());2189c->set_should_profile(true);2190}2191}2192}219321942195void GraphBuilder::instance_of(int klass_index) {2196bool will_link;2197ciKlass* klass = stream()->get_klass(will_link);2198ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_exhandling();2199InstanceOf* i = new InstanceOf(klass, apop(), state_before);2200ipush(append_split(i));2201i->set_direct_compare(direct_compare(klass));22022203if (is_profiling()) {2204// Note that we'd collect profile data in this method if we wanted it.2205compilation()->set_would_profile(true);22062207if (profile_checkcasts()) {2208i->set_profiled_method(method());2209i->set_profiled_bci(bci());2210i->set_should_profile(true);2211}2212}2213}221422152216void GraphBuilder::monitorenter(Value x, int bci) {2217// save state before locking in case of deoptimization after a NullPointerException2218ValueStack* state_before = copy_state_for_exception_with_bci(bci);2219append_with_bci(new MonitorEnter(x, state()->lock(x), state_before), bci);2220kill_all();2221}222222232224void GraphBuilder::monitorexit(Value x, int bci) {2225append_with_bci(new MonitorExit(x, state()->unlock()), bci);2226kill_all();2227}222822292230void GraphBuilder::new_multi_array(int dimensions) {2231bool will_link;2232ciKlass* klass = stream()->get_klass(will_link);2233ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_exhandling();22342235Values* dims = new Values(dimensions, dimensions, NULL);2236// fill in all dimensions2237int i = dimensions;2238while (i-- > 0) dims->at_put(i, ipop());2239// create array2240NewArray* n = new NewMultiArray(klass, dims, state_before);2241apush(append_split(n));2242}224322442245void GraphBuilder::throw_op(int bci) {2246// We require that the debug info for a Throw be the "state before"2247// the Throw (i.e., exception oop is still on TOS)2248ValueStack* state_before = copy_state_before_with_bci(bci);2249Throw* t = new Throw(apop(), state_before);2250// operand stack not needed after a throw2251state()->truncate_stack(0);2252append_with_bci(t, bci);2253}225422552256Value GraphBuilder::round_fp(Value fp_value) {2257if (strict_fp_requires_explicit_rounding) {2258#ifdef IA322259// no rounding needed if SSE2 is used2260if (UseSSE < 2) {2261// Must currently insert rounding node for doubleword values that2262// are results of expressions (i.e., not loads from memory or2263// constants)2264if (fp_value->type()->tag() == doubleTag &&2265fp_value->as_Constant() == NULL &&2266fp_value->as_Local() == NULL && // method parameters need no rounding2267fp_value->as_RoundFP() == NULL) {2268return append(new RoundFP(fp_value));2269}2270}2271#else2272Unimplemented();2273#endif // IA322274}2275return fp_value;2276}227722782279Instruction* GraphBuilder::append_with_bci(Instruction* instr, int bci) {2280Canonicalizer canon(compilation(), instr, bci);2281Instruction* i1 = canon.canonical();2282if (i1->is_linked() || !i1->can_be_linked()) {2283// Canonicalizer returned an instruction which was already2284// appended so simply return it.2285return i1;2286}22872288if (UseLocalValueNumbering) {2289// Lookup the instruction in the ValueMap and add it to the map if2290// it's not found.2291Instruction* i2 = vmap()->find_insert(i1);2292if (i2 != i1) {2293// found an entry in the value map, so just return it.2294assert(i2->is_linked(), "should already be linked");2295return i2;2296}2297ValueNumberingEffects vne(vmap());2298i1->visit(&vne);2299}23002301// i1 was not eliminated => append it2302assert(i1->next() == NULL, "shouldn't already be linked");2303_last = _last->set_next(i1, canon.bci());23042305if (++_instruction_count >= InstructionCountCutoff && !bailed_out()) {2306// set the bailout state but complete normal processing. We2307// might do a little more work before noticing the bailout so we2308// want processing to continue normally until it's noticed.2309bailout("Method and/or inlining is too large");2310}23112312#ifndef PRODUCT2313if (PrintIRDuringConstruction) {2314InstructionPrinter ip;2315ip.print_line(i1);2316if (Verbose) {2317state()->print();2318}2319}2320#endif23212322// save state after modification of operand stack for StateSplit instructions2323StateSplit* s = i1->as_StateSplit();2324if (s != NULL) {2325if (EliminateFieldAccess) {2326Intrinsic* intrinsic = s->as_Intrinsic();2327if (s->as_Invoke() != NULL || (intrinsic && !intrinsic->preserves_state())) {2328_memory->kill();2329}2330}2331s->set_state(state()->copy(ValueStack::StateAfter, canon.bci()));2332}23332334// set up exception handlers for this instruction if necessary2335if (i1->can_trap()) {2336i1->set_exception_handlers(handle_exception(i1));2337assert(i1->exception_state() != NULL || !i1->needs_exception_state() || bailed_out(), "handle_exception must set exception state");2338}2339return i1;2340}234123422343Instruction* GraphBuilder::append(Instruction* instr) {2344assert(instr->as_StateSplit() == NULL || instr->as_BlockEnd() != NULL, "wrong append used");2345return append_with_bci(instr, bci());2346}234723482349Instruction* GraphBuilder::append_split(StateSplit* instr) {2350return append_with_bci(instr, bci());2351}235223532354void GraphBuilder::null_check(Value value) {2355if (value->as_NewArray() != NULL || value->as_NewInstance() != NULL) {2356return;2357} else {2358Constant* con = value->as_Constant();2359if (con) {2360ObjectType* c = con->type()->as_ObjectType();2361if (c && c->is_loaded()) {2362ObjectConstant* oc = c->as_ObjectConstant();2363if (!oc || !oc->value()->is_null_object()) {2364return;2365}2366}2367}2368}2369append(new NullCheck(value, copy_state_for_exception()));2370}2371237223732374XHandlers* GraphBuilder::handle_exception(Instruction* instruction) {2375if (!has_handler() && (!instruction->needs_exception_state() || instruction->exception_state() != NULL)) {2376assert(instruction->exception_state() == NULL2377|| instruction->exception_state()->kind() == ValueStack::EmptyExceptionState2378|| (instruction->exception_state()->kind() == ValueStack::ExceptionState && _compilation->env()->should_retain_local_variables()),2379"exception_state should be of exception kind");2380return new XHandlers();2381}23822383XHandlers* exception_handlers = new XHandlers();2384ScopeData* cur_scope_data = scope_data();2385ValueStack* cur_state = instruction->state_before();2386ValueStack* prev_state = NULL;2387int scope_count = 0;23882389assert(cur_state != NULL, "state_before must be set");2390do {2391int cur_bci = cur_state->bci();2392assert(cur_scope_data->scope() == cur_state->scope(), "scopes do not match");2393assert(cur_bci == SynchronizationEntryBCI || cur_bci == cur_scope_data->stream()->cur_bci(), "invalid bci");23942395// join with all potential exception handlers2396XHandlers* list = cur_scope_data->xhandlers();2397const int n = list->length();2398for (int i = 0; i < n; i++) {2399XHandler* h = list->handler_at(i);2400if (h->covers(cur_bci)) {2401// h is a potential exception handler => join it2402compilation()->set_has_exception_handlers(true);24032404BlockBegin* entry = h->entry_block();2405if (entry == block()) {2406// It's acceptable for an exception handler to cover itself2407// but we don't handle that in the parser currently. It's2408// very rare so we bailout instead of trying to handle it.2409BAILOUT_("exception handler covers itself", exception_handlers);2410}2411assert(entry->bci() == h->handler_bci(), "must match");2412assert(entry->bci() == -1 || entry == cur_scope_data->block_at(entry->bci()), "blocks must correspond");24132414// previously this was a BAILOUT, but this is not necessary2415// now because asynchronous exceptions are not handled this way.2416assert(entry->state() == NULL || cur_state->total_locks_size() == entry->state()->total_locks_size(), "locks do not match");24172418// xhandler start with an empty expression stack2419if (cur_state->stack_size() != 0) {2420cur_state = cur_state->copy(ValueStack::ExceptionState, cur_state->bci());2421}2422if (instruction->exception_state() == NULL) {2423instruction->set_exception_state(cur_state);2424}24252426// Note: Usually this join must work. However, very2427// complicated jsr-ret structures where we don't ret from2428// the subroutine can cause the objects on the monitor2429// stacks to not match because blocks can be parsed twice.2430// The only test case we've seen so far which exhibits this2431// problem is caught by the infinite recursion test in2432// GraphBuilder::jsr() if the join doesn't work.2433if (!entry->try_merge(cur_state)) {2434BAILOUT_("error while joining with exception handler, prob. due to complicated jsr/rets", exception_handlers);2435}24362437// add current state for correct handling of phi functions at begin of xhandler2438int phi_operand = entry->add_exception_state(cur_state);24392440// add entry to the list of xhandlers of this block2441_block->add_exception_handler(entry);24422443// add back-edge from xhandler entry to this block2444if (!entry->is_predecessor(_block)) {2445entry->add_predecessor(_block);2446}24472448// clone XHandler because phi_operand and scope_count can not be shared2449XHandler* new_xhandler = new XHandler(h);2450new_xhandler->set_phi_operand(phi_operand);2451new_xhandler->set_scope_count(scope_count);2452exception_handlers->append(new_xhandler);24532454// fill in exception handler subgraph lazily2455assert(!entry->is_set(BlockBegin::was_visited_flag), "entry must not be visited yet");2456cur_scope_data->add_to_work_list(entry);24572458// stop when reaching catchall2459if (h->catch_type() == 0) {2460return exception_handlers;2461}2462}2463}24642465if (exception_handlers->length() == 0) {2466// This scope and all callees do not handle exceptions, so the local2467// variables of this scope are not needed. However, the scope itself is2468// required for a correct exception stack trace -> clear out the locals.2469if (_compilation->env()->should_retain_local_variables()) {2470cur_state = cur_state->copy(ValueStack::ExceptionState, cur_state->bci());2471} else {2472cur_state = cur_state->copy(ValueStack::EmptyExceptionState, cur_state->bci());2473}2474if (prev_state != NULL) {2475prev_state->set_caller_state(cur_state);2476}2477if (instruction->exception_state() == NULL) {2478instruction->set_exception_state(cur_state);2479}2480}24812482// Set up iteration for next time.2483// If parsing a jsr, do not grab exception handlers from the2484// parent scopes for this method (already got them, and they2485// needed to be cloned)24862487while (cur_scope_data->parsing_jsr()) {2488cur_scope_data = cur_scope_data->parent();2489}24902491assert(cur_scope_data->scope() == cur_state->scope(), "scopes do not match");2492assert(cur_state->locks_size() == 0 || cur_state->locks_size() == 1, "unlocking must be done in a catchall exception handler");24932494prev_state = cur_state;2495cur_state = cur_state->caller_state();2496cur_scope_data = cur_scope_data->parent();2497scope_count++;2498} while (cur_scope_data != NULL);24992500return exception_handlers;2501}250225032504// Helper class for simplifying Phis.2505class PhiSimplifier : public BlockClosure {2506private:2507bool _has_substitutions;2508Value simplify(Value v);25092510public:2511PhiSimplifier(BlockBegin* start) : _has_substitutions(false) {2512start->iterate_preorder(this);2513if (_has_substitutions) {2514SubstitutionResolver sr(start);2515}2516}2517void block_do(BlockBegin* b);2518bool has_substitutions() const { return _has_substitutions; }2519};252025212522Value PhiSimplifier::simplify(Value v) {2523Phi* phi = v->as_Phi();25242525if (phi == NULL) {2526// no phi function2527return v;2528} else if (v->has_subst()) {2529// already substituted; subst can be phi itself -> simplify2530return simplify(v->subst());2531} else if (phi->is_set(Phi::cannot_simplify)) {2532// already tried to simplify phi before2533return phi;2534} else if (phi->is_set(Phi::visited)) {2535// break cycles in phi functions2536return phi;2537} else if (phi->type()->is_illegal()) {2538// illegal phi functions are ignored anyway2539return phi;25402541} else {2542// mark phi function as processed to break cycles in phi functions2543phi->set(Phi::visited);25442545// simplify x = [y, x] and x = [y, y] to y2546Value subst = NULL;2547int opd_count = phi->operand_count();2548for (int i = 0; i < opd_count; i++) {2549Value opd = phi->operand_at(i);2550assert(opd != NULL, "Operand must exist!");25512552if (opd->type()->is_illegal()) {2553// if one operand is illegal, the entire phi function is illegal2554phi->make_illegal();2555phi->clear(Phi::visited);2556return phi;2557}25582559Value new_opd = simplify(opd);2560assert(new_opd != NULL, "Simplified operand must exist!");25612562if (new_opd != phi && new_opd != subst) {2563if (subst == NULL) {2564subst = new_opd;2565} else {2566// no simplification possible2567phi->set(Phi::cannot_simplify);2568phi->clear(Phi::visited);2569return phi;2570}2571}2572}25732574// sucessfully simplified phi function2575assert(subst != NULL, "illegal phi function");2576_has_substitutions = true;2577phi->clear(Phi::visited);2578phi->set_subst(subst);25792580#ifndef PRODUCT2581if (PrintPhiFunctions) {2582tty->print_cr("simplified phi function %c%d to %c%d (Block B%d)", phi->type()->tchar(), phi->id(), subst->type()->tchar(), subst->id(), phi->block()->block_id());2583}2584#endif25852586return subst;2587}2588}258925902591void PhiSimplifier::block_do(BlockBegin* b) {2592for_each_phi_fun(b, phi,2593simplify(phi);2594);25952596#ifdef ASSERT2597for_each_phi_fun(b, phi,2598assert(phi->operand_count() != 1 || phi->subst() != phi || phi->is_illegal(), "missed trivial simplification");2599);26002601ValueStack* state = b->state()->caller_state();2602for_each_state_value(state, value,2603Phi* phi = value->as_Phi();2604assert(phi == NULL || phi->block() != b, "must not have phi function to simplify in caller state");2605);2606#endif2607}26082609// This method is called after all blocks are filled with HIR instructions2610// It eliminates all Phi functions of the form x = [y, y] and x = [y, x]2611void GraphBuilder::eliminate_redundant_phis(BlockBegin* start) {2612PhiSimplifier simplifier(start);2613}261426152616void GraphBuilder::connect_to_end(BlockBegin* beg) {2617// setup iteration2618kill_all();2619_block = beg;2620_state = beg->state()->copy_for_parsing();2621_last = beg;2622iterate_bytecodes_for_block(beg->bci());2623}262426252626BlockEnd* GraphBuilder::iterate_bytecodes_for_block(int bci) {2627#ifndef PRODUCT2628if (PrintIRDuringConstruction) {2629tty->cr();2630InstructionPrinter ip;2631ip.print_instr(_block); tty->cr();2632ip.print_stack(_block->state()); tty->cr();2633ip.print_inline_level(_block);2634ip.print_head();2635tty->print_cr("locals size: %d stack size: %d", state()->locals_size(), state()->stack_size());2636}2637#endif2638_skip_block = false;2639assert(state() != NULL, "ValueStack missing!");2640CompileLog* log = compilation()->log();2641ciBytecodeStream s(method());2642s.reset_to_bci(bci);2643int prev_bci = bci;2644scope_data()->set_stream(&s);2645// iterate2646Bytecodes::Code code = Bytecodes::_illegal;2647bool push_exception = false;26482649if (block()->is_set(BlockBegin::exception_entry_flag) && block()->next() == NULL) {2650// first thing in the exception entry block should be the exception object.2651push_exception = true;2652}26532654bool ignore_return = scope_data()->ignore_return();26552656while (!bailed_out() && last()->as_BlockEnd() == NULL &&2657(code = stream()->next()) != ciBytecodeStream::EOBC() &&2658(block_at(s.cur_bci()) == NULL || block_at(s.cur_bci()) == block())) {2659assert(state()->kind() == ValueStack::Parsing, "invalid state kind");26602661if (log != NULL)2662log->set_context("bc code='%d' bci='%d'", (int)code, s.cur_bci());26632664// Check for active jsr during OSR compilation2665if (compilation()->is_osr_compile()2666&& scope()->is_top_scope()2667&& parsing_jsr()2668&& s.cur_bci() == compilation()->osr_bci()) {2669bailout("OSR not supported while a jsr is active");2670}26712672if (push_exception) {2673apush(append(new ExceptionObject()));2674push_exception = false;2675}26762677// handle bytecode2678switch (code) {2679case Bytecodes::_nop : /* nothing to do */ break;2680case Bytecodes::_aconst_null : apush(append(new Constant(objectNull ))); break;2681case Bytecodes::_iconst_m1 : ipush(append(new Constant(new IntConstant (-1)))); break;2682case Bytecodes::_iconst_0 : ipush(append(new Constant(intZero ))); break;2683case Bytecodes::_iconst_1 : ipush(append(new Constant(intOne ))); break;2684case Bytecodes::_iconst_2 : ipush(append(new Constant(new IntConstant ( 2)))); break;2685case Bytecodes::_iconst_3 : ipush(append(new Constant(new IntConstant ( 3)))); break;2686case Bytecodes::_iconst_4 : ipush(append(new Constant(new IntConstant ( 4)))); break;2687case Bytecodes::_iconst_5 : ipush(append(new Constant(new IntConstant ( 5)))); break;2688case Bytecodes::_lconst_0 : lpush(append(new Constant(new LongConstant ( 0)))); break;2689case Bytecodes::_lconst_1 : lpush(append(new Constant(new LongConstant ( 1)))); break;2690case Bytecodes::_fconst_0 : fpush(append(new Constant(new FloatConstant ( 0)))); break;2691case Bytecodes::_fconst_1 : fpush(append(new Constant(new FloatConstant ( 1)))); break;2692case Bytecodes::_fconst_2 : fpush(append(new Constant(new FloatConstant ( 2)))); break;2693case Bytecodes::_dconst_0 : dpush(append(new Constant(new DoubleConstant( 0)))); break;2694case Bytecodes::_dconst_1 : dpush(append(new Constant(new DoubleConstant( 1)))); break;2695case Bytecodes::_bipush : ipush(append(new Constant(new IntConstant(((signed char*)s.cur_bcp())[1])))); break;2696case Bytecodes::_sipush : ipush(append(new Constant(new IntConstant((short)Bytes::get_Java_u2(s.cur_bcp()+1))))); break;2697case Bytecodes::_ldc : // fall through2698case Bytecodes::_ldc_w : // fall through2699case Bytecodes::_ldc2_w : load_constant(); break;2700case Bytecodes::_iload : load_local(intType , s.get_index()); break;2701case Bytecodes::_lload : load_local(longType , s.get_index()); break;2702case Bytecodes::_fload : load_local(floatType , s.get_index()); break;2703case Bytecodes::_dload : load_local(doubleType , s.get_index()); break;2704case Bytecodes::_aload : load_local(instanceType, s.get_index()); break;2705case Bytecodes::_iload_0 : load_local(intType , 0); break;2706case Bytecodes::_iload_1 : load_local(intType , 1); break;2707case Bytecodes::_iload_2 : load_local(intType , 2); break;2708case Bytecodes::_iload_3 : load_local(intType , 3); break;2709case Bytecodes::_lload_0 : load_local(longType , 0); break;2710case Bytecodes::_lload_1 : load_local(longType , 1); break;2711case Bytecodes::_lload_2 : load_local(longType , 2); break;2712case Bytecodes::_lload_3 : load_local(longType , 3); break;2713case Bytecodes::_fload_0 : load_local(floatType , 0); break;2714case Bytecodes::_fload_1 : load_local(floatType , 1); break;2715case Bytecodes::_fload_2 : load_local(floatType , 2); break;2716case Bytecodes::_fload_3 : load_local(floatType , 3); break;2717case Bytecodes::_dload_0 : load_local(doubleType, 0); break;2718case Bytecodes::_dload_1 : load_local(doubleType, 1); break;2719case Bytecodes::_dload_2 : load_local(doubleType, 2); break;2720case Bytecodes::_dload_3 : load_local(doubleType, 3); break;2721case Bytecodes::_aload_0 : load_local(objectType, 0); break;2722case Bytecodes::_aload_1 : load_local(objectType, 1); break;2723case Bytecodes::_aload_2 : load_local(objectType, 2); break;2724case Bytecodes::_aload_3 : load_local(objectType, 3); break;2725case Bytecodes::_iaload : load_indexed(T_INT ); break;2726case Bytecodes::_laload : load_indexed(T_LONG ); break;2727case Bytecodes::_faload : load_indexed(T_FLOAT ); break;2728case Bytecodes::_daload : load_indexed(T_DOUBLE); break;2729case Bytecodes::_aaload : load_indexed(T_OBJECT); break;2730case Bytecodes::_baload : load_indexed(T_BYTE ); break;2731case Bytecodes::_caload : load_indexed(T_CHAR ); break;2732case Bytecodes::_saload : load_indexed(T_SHORT ); break;2733case Bytecodes::_istore : store_local(intType , s.get_index()); break;2734case Bytecodes::_lstore : store_local(longType , s.get_index()); break;2735case Bytecodes::_fstore : store_local(floatType , s.get_index()); break;2736case Bytecodes::_dstore : store_local(doubleType, s.get_index()); break;2737case Bytecodes::_astore : store_local(objectType, s.get_index()); break;2738case Bytecodes::_istore_0 : store_local(intType , 0); break;2739case Bytecodes::_istore_1 : store_local(intType , 1); break;2740case Bytecodes::_istore_2 : store_local(intType , 2); break;2741case Bytecodes::_istore_3 : store_local(intType , 3); break;2742case Bytecodes::_lstore_0 : store_local(longType , 0); break;2743case Bytecodes::_lstore_1 : store_local(longType , 1); break;2744case Bytecodes::_lstore_2 : store_local(longType , 2); break;2745case Bytecodes::_lstore_3 : store_local(longType , 3); break;2746case Bytecodes::_fstore_0 : store_local(floatType , 0); break;2747case Bytecodes::_fstore_1 : store_local(floatType , 1); break;2748case Bytecodes::_fstore_2 : store_local(floatType , 2); break;2749case Bytecodes::_fstore_3 : store_local(floatType , 3); break;2750case Bytecodes::_dstore_0 : store_local(doubleType, 0); break;2751case Bytecodes::_dstore_1 : store_local(doubleType, 1); break;2752case Bytecodes::_dstore_2 : store_local(doubleType, 2); break;2753case Bytecodes::_dstore_3 : store_local(doubleType, 3); break;2754case Bytecodes::_astore_0 : store_local(objectType, 0); break;2755case Bytecodes::_astore_1 : store_local(objectType, 1); break;2756case Bytecodes::_astore_2 : store_local(objectType, 2); break;2757case Bytecodes::_astore_3 : store_local(objectType, 3); break;2758case Bytecodes::_iastore : store_indexed(T_INT ); break;2759case Bytecodes::_lastore : store_indexed(T_LONG ); break;2760case Bytecodes::_fastore : store_indexed(T_FLOAT ); break;2761case Bytecodes::_dastore : store_indexed(T_DOUBLE); break;2762case Bytecodes::_aastore : store_indexed(T_OBJECT); break;2763case Bytecodes::_bastore : store_indexed(T_BYTE ); break;2764case Bytecodes::_castore : store_indexed(T_CHAR ); break;2765case Bytecodes::_sastore : store_indexed(T_SHORT ); break;2766case Bytecodes::_pop : // fall through2767case Bytecodes::_pop2 : // fall through2768case Bytecodes::_dup : // fall through2769case Bytecodes::_dup_x1 : // fall through2770case Bytecodes::_dup_x2 : // fall through2771case Bytecodes::_dup2 : // fall through2772case Bytecodes::_dup2_x1 : // fall through2773case Bytecodes::_dup2_x2 : // fall through2774case Bytecodes::_swap : stack_op(code); break;2775case Bytecodes::_iadd : arithmetic_op(intType , code); break;2776case Bytecodes::_ladd : arithmetic_op(longType , code); break;2777case Bytecodes::_fadd : arithmetic_op(floatType , code); break;2778case Bytecodes::_dadd : arithmetic_op(doubleType, code); break;2779case Bytecodes::_isub : arithmetic_op(intType , code); break;2780case Bytecodes::_lsub : arithmetic_op(longType , code); break;2781case Bytecodes::_fsub : arithmetic_op(floatType , code); break;2782case Bytecodes::_dsub : arithmetic_op(doubleType, code); break;2783case Bytecodes::_imul : arithmetic_op(intType , code); break;2784case Bytecodes::_lmul : arithmetic_op(longType , code); break;2785case Bytecodes::_fmul : arithmetic_op(floatType , code); break;2786case Bytecodes::_dmul : arithmetic_op(doubleType, code); break;2787case Bytecodes::_idiv : arithmetic_op(intType , code, copy_state_for_exception()); break;2788case Bytecodes::_ldiv : arithmetic_op(longType , code, copy_state_for_exception()); break;2789case Bytecodes::_fdiv : arithmetic_op(floatType , code); break;2790case Bytecodes::_ddiv : arithmetic_op(doubleType, code); break;2791case Bytecodes::_irem : arithmetic_op(intType , code, copy_state_for_exception()); break;2792case Bytecodes::_lrem : arithmetic_op(longType , code, copy_state_for_exception()); break;2793case Bytecodes::_frem : arithmetic_op(floatType , code); break;2794case Bytecodes::_drem : arithmetic_op(doubleType, code); break;2795case Bytecodes::_ineg : negate_op(intType ); break;2796case Bytecodes::_lneg : negate_op(longType ); break;2797case Bytecodes::_fneg : negate_op(floatType ); break;2798case Bytecodes::_dneg : negate_op(doubleType); break;2799case Bytecodes::_ishl : shift_op(intType , code); break;2800case Bytecodes::_lshl : shift_op(longType, code); break;2801case Bytecodes::_ishr : shift_op(intType , code); break;2802case Bytecodes::_lshr : shift_op(longType, code); break;2803case Bytecodes::_iushr : shift_op(intType , code); break;2804case Bytecodes::_lushr : shift_op(longType, code); break;2805case Bytecodes::_iand : logic_op(intType , code); break;2806case Bytecodes::_land : logic_op(longType, code); break;2807case Bytecodes::_ior : logic_op(intType , code); break;2808case Bytecodes::_lor : logic_op(longType, code); break;2809case Bytecodes::_ixor : logic_op(intType , code); break;2810case Bytecodes::_lxor : logic_op(longType, code); break;2811case Bytecodes::_iinc : increment(); break;2812case Bytecodes::_i2l : convert(code, T_INT , T_LONG ); break;2813case Bytecodes::_i2f : convert(code, T_INT , T_FLOAT ); break;2814case Bytecodes::_i2d : convert(code, T_INT , T_DOUBLE); break;2815case Bytecodes::_l2i : convert(code, T_LONG , T_INT ); break;2816case Bytecodes::_l2f : convert(code, T_LONG , T_FLOAT ); break;2817case Bytecodes::_l2d : convert(code, T_LONG , T_DOUBLE); break;2818case Bytecodes::_f2i : convert(code, T_FLOAT , T_INT ); break;2819case Bytecodes::_f2l : convert(code, T_FLOAT , T_LONG ); break;2820case Bytecodes::_f2d : convert(code, T_FLOAT , T_DOUBLE); break;2821case Bytecodes::_d2i : convert(code, T_DOUBLE, T_INT ); break;2822case Bytecodes::_d2l : convert(code, T_DOUBLE, T_LONG ); break;2823case Bytecodes::_d2f : convert(code, T_DOUBLE, T_FLOAT ); break;2824case Bytecodes::_i2b : convert(code, T_INT , T_BYTE ); break;2825case Bytecodes::_i2c : convert(code, T_INT , T_CHAR ); break;2826case Bytecodes::_i2s : convert(code, T_INT , T_SHORT ); break;2827case Bytecodes::_lcmp : compare_op(longType , code); break;2828case Bytecodes::_fcmpl : compare_op(floatType , code); break;2829case Bytecodes::_fcmpg : compare_op(floatType , code); break;2830case Bytecodes::_dcmpl : compare_op(doubleType, code); break;2831case Bytecodes::_dcmpg : compare_op(doubleType, code); break;2832case Bytecodes::_ifeq : if_zero(intType , If::eql); break;2833case Bytecodes::_ifne : if_zero(intType , If::neq); break;2834case Bytecodes::_iflt : if_zero(intType , If::lss); break;2835case Bytecodes::_ifge : if_zero(intType , If::geq); break;2836case Bytecodes::_ifgt : if_zero(intType , If::gtr); break;2837case Bytecodes::_ifle : if_zero(intType , If::leq); break;2838case Bytecodes::_if_icmpeq : if_same(intType , If::eql); break;2839case Bytecodes::_if_icmpne : if_same(intType , If::neq); break;2840case Bytecodes::_if_icmplt : if_same(intType , If::lss); break;2841case Bytecodes::_if_icmpge : if_same(intType , If::geq); break;2842case Bytecodes::_if_icmpgt : if_same(intType , If::gtr); break;2843case Bytecodes::_if_icmple : if_same(intType , If::leq); break;2844case Bytecodes::_if_acmpeq : if_same(objectType, If::eql); break;2845case Bytecodes::_if_acmpne : if_same(objectType, If::neq); break;2846case Bytecodes::_goto : _goto(s.cur_bci(), s.get_dest()); break;2847case Bytecodes::_jsr : jsr(s.get_dest()); break;2848case Bytecodes::_ret : ret(s.get_index()); break;2849case Bytecodes::_tableswitch : table_switch(); break;2850case Bytecodes::_lookupswitch : lookup_switch(); break;2851case Bytecodes::_ireturn : method_return(ipop(), ignore_return); break;2852case Bytecodes::_lreturn : method_return(lpop(), ignore_return); break;2853case Bytecodes::_freturn : method_return(fpop(), ignore_return); break;2854case Bytecodes::_dreturn : method_return(dpop(), ignore_return); break;2855case Bytecodes::_areturn : method_return(apop(), ignore_return); break;2856case Bytecodes::_return : method_return(NULL , ignore_return); break;2857case Bytecodes::_getstatic : // fall through2858case Bytecodes::_putstatic : // fall through2859case Bytecodes::_getfield : // fall through2860case Bytecodes::_putfield : access_field(code); break;2861case Bytecodes::_invokevirtual : // fall through2862case Bytecodes::_invokespecial : // fall through2863case Bytecodes::_invokestatic : // fall through2864case Bytecodes::_invokedynamic : // fall through2865case Bytecodes::_invokeinterface: invoke(code); break;2866case Bytecodes::_new : new_instance(s.get_index_u2()); break;2867case Bytecodes::_newarray : new_type_array(); break;2868case Bytecodes::_anewarray : new_object_array(); break;2869case Bytecodes::_arraylength : { ValueStack* state_before = copy_state_for_exception(); ipush(append(new ArrayLength(apop(), state_before))); break; }2870case Bytecodes::_athrow : throw_op(s.cur_bci()); break;2871case Bytecodes::_checkcast : check_cast(s.get_index_u2()); break;2872case Bytecodes::_instanceof : instance_of(s.get_index_u2()); break;2873case Bytecodes::_monitorenter : monitorenter(apop(), s.cur_bci()); break;2874case Bytecodes::_monitorexit : monitorexit (apop(), s.cur_bci()); break;2875case Bytecodes::_wide : ShouldNotReachHere(); break;2876case Bytecodes::_multianewarray : new_multi_array(s.cur_bcp()[3]); break;2877case Bytecodes::_ifnull : if_null(objectType, If::eql); break;2878case Bytecodes::_ifnonnull : if_null(objectType, If::neq); break;2879case Bytecodes::_goto_w : _goto(s.cur_bci(), s.get_far_dest()); break;2880case Bytecodes::_jsr_w : jsr(s.get_far_dest()); break;2881case Bytecodes::_breakpoint : BAILOUT_("concurrent setting of breakpoint", NULL);2882default : ShouldNotReachHere(); break;2883}28842885if (log != NULL)2886log->clear_context(); // skip marker if nothing was printed28872888// save current bci to setup Goto at the end2889prev_bci = s.cur_bci();28902891}2892CHECK_BAILOUT_(NULL);2893// stop processing of this block (see try_inline_full)2894if (_skip_block) {2895_skip_block = false;2896assert(_last && _last->as_BlockEnd(), "");2897return _last->as_BlockEnd();2898}2899// if there are any, check if last instruction is a BlockEnd instruction2900BlockEnd* end = last()->as_BlockEnd();2901if (end == NULL) {2902// all blocks must end with a BlockEnd instruction => add a Goto2903end = new Goto(block_at(s.cur_bci()), false);2904append(end);2905}2906assert(end == last()->as_BlockEnd(), "inconsistency");29072908assert(end->state() != NULL, "state must already be present");2909assert(end->as_Return() == NULL || end->as_Throw() == NULL || end->state()->stack_size() == 0, "stack not needed for return and throw");29102911// connect to begin & set state2912// NOTE that inlining may have changed the block we are parsing2913block()->set_end(end);2914// propagate state2915for (int i = end->number_of_sux() - 1; i >= 0; i--) {2916BlockBegin* sux = end->sux_at(i);2917assert(sux->is_predecessor(block()), "predecessor missing");2918// be careful, bailout if bytecodes are strange2919if (!sux->try_merge(end->state())) BAILOUT_("block join failed", NULL);2920scope_data()->add_to_work_list(end->sux_at(i));2921}29222923scope_data()->set_stream(NULL);29242925// done2926return end;2927}292829292930void GraphBuilder::iterate_all_blocks(bool start_in_current_block_for_inlining) {2931do {2932if (start_in_current_block_for_inlining && !bailed_out()) {2933iterate_bytecodes_for_block(0);2934start_in_current_block_for_inlining = false;2935} else {2936BlockBegin* b;2937while ((b = scope_data()->remove_from_work_list()) != NULL) {2938if (!b->is_set(BlockBegin::was_visited_flag)) {2939if (b->is_set(BlockBegin::osr_entry_flag)) {2940// we're about to parse the osr entry block, so make sure2941// we setup the OSR edge leading into this block so that2942// Phis get setup correctly.2943setup_osr_entry_block();2944// this is no longer the osr entry block, so clear it.2945b->clear(BlockBegin::osr_entry_flag);2946}2947b->set(BlockBegin::was_visited_flag);2948connect_to_end(b);2949}2950}2951}2952} while (!bailed_out() && !scope_data()->is_work_list_empty());2953}295429552956bool GraphBuilder::_can_trap [Bytecodes::number_of_java_codes];29572958void GraphBuilder::initialize() {2959// the following bytecodes are assumed to potentially2960// throw exceptions in compiled code - note that e.g.2961// monitorexit & the return bytecodes do not throw2962// exceptions since monitor pairing proved that they2963// succeed (if monitor pairing succeeded)2964Bytecodes::Code can_trap_list[] =2965{ Bytecodes::_ldc2966, Bytecodes::_ldc_w2967, Bytecodes::_ldc2_w2968, Bytecodes::_iaload2969, Bytecodes::_laload2970, Bytecodes::_faload2971, Bytecodes::_daload2972, Bytecodes::_aaload2973, Bytecodes::_baload2974, Bytecodes::_caload2975, Bytecodes::_saload2976, Bytecodes::_iastore2977, Bytecodes::_lastore2978, Bytecodes::_fastore2979, Bytecodes::_dastore2980, Bytecodes::_aastore2981, Bytecodes::_bastore2982, Bytecodes::_castore2983, Bytecodes::_sastore2984, Bytecodes::_idiv2985, Bytecodes::_ldiv2986, Bytecodes::_irem2987, Bytecodes::_lrem2988, Bytecodes::_getstatic2989, Bytecodes::_putstatic2990, Bytecodes::_getfield2991, Bytecodes::_putfield2992, Bytecodes::_invokevirtual2993, Bytecodes::_invokespecial2994, Bytecodes::_invokestatic2995, Bytecodes::_invokedynamic2996, Bytecodes::_invokeinterface2997, Bytecodes::_new2998, Bytecodes::_newarray2999, Bytecodes::_anewarray3000, Bytecodes::_arraylength3001, Bytecodes::_athrow3002, Bytecodes::_checkcast3003, Bytecodes::_instanceof3004, Bytecodes::_monitorenter3005, Bytecodes::_multianewarray3006};30073008// inititialize trap tables3009for (int i = 0; i < Bytecodes::number_of_java_codes; i++) {3010_can_trap[i] = false;3011}3012// set standard trap info3013for (uint j = 0; j < ARRAY_SIZE(can_trap_list); j++) {3014_can_trap[can_trap_list[j]] = true;3015}3016}301730183019BlockBegin* GraphBuilder::header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state) {3020assert(entry->is_set(f), "entry/flag mismatch");3021// create header block3022BlockBegin* h = new BlockBegin(entry->bci());3023h->set_depth_first_number(0);30243025Value l = h;3026BlockEnd* g = new Goto(entry, false);3027l->set_next(g, entry->bci());3028h->set_end(g);3029h->set(f);3030// setup header block end state3031ValueStack* s = state->copy(ValueStack::StateAfter, entry->bci()); // can use copy since stack is empty (=> no phis)3032assert(s->stack_is_empty(), "must have empty stack at entry point");3033g->set_state(s);3034return h;3035}3036303730383039BlockBegin* GraphBuilder::setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* state) {3040BlockBegin* start = new BlockBegin(0);30413042// This code eliminates the empty start block at the beginning of3043// each method. Previously, each method started with the3044// start-block created below, and this block was followed by the3045// header block that was always empty. This header block is only3046// necesary if std_entry is also a backward branch target because3047// then phi functions may be necessary in the header block. It's3048// also necessary when profiling so that there's a single block that3049// can increment the interpreter_invocation_count.3050BlockBegin* new_header_block;3051if (std_entry->number_of_preds() > 0 || count_invocations() || count_backedges()) {3052new_header_block = header_block(std_entry, BlockBegin::std_entry_flag, state);3053} else {3054new_header_block = std_entry;3055}30563057// setup start block (root for the IR graph)3058Base* base =3059new Base(3060new_header_block,3061osr_entry3062);3063start->set_next(base, 0);3064start->set_end(base);3065// create & setup state for start block3066start->set_state(state->copy(ValueStack::StateAfter, std_entry->bci()));3067base->set_state(state->copy(ValueStack::StateAfter, std_entry->bci()));30683069if (base->std_entry()->state() == NULL) {3070// setup states for header blocks3071base->std_entry()->merge(state);3072}30733074assert(base->std_entry()->state() != NULL, "");3075return start;3076}307730783079void GraphBuilder::setup_osr_entry_block() {3080assert(compilation()->is_osr_compile(), "only for osrs");30813082int osr_bci = compilation()->osr_bci();3083ciBytecodeStream s(method());3084s.reset_to_bci(osr_bci);3085s.next();3086scope_data()->set_stream(&s);30873088// create a new block to be the osr setup code3089_osr_entry = new BlockBegin(osr_bci);3090_osr_entry->set(BlockBegin::osr_entry_flag);3091_osr_entry->set_depth_first_number(0);3092BlockBegin* target = bci2block()->at(osr_bci);3093assert(target != NULL && target->is_set(BlockBegin::osr_entry_flag), "must be there");3094// the osr entry has no values for locals3095ValueStack* state = target->state()->copy();3096_osr_entry->set_state(state);30973098kill_all();3099_block = _osr_entry;3100_state = _osr_entry->state()->copy();3101assert(_state->bci() == osr_bci, "mismatch");3102_last = _osr_entry;3103Value e = append(new OsrEntry());3104e->set_needs_null_check(false);31053106// OSR buffer is3107//3108// locals[nlocals-1..0]3109// monitors[number_of_locks-1..0]3110//3111// locals is a direct copy of the interpreter frame so in the osr buffer3112// so first slot in the local array is the last local from the interpreter3113// and last slot is local[0] (receiver) from the interpreter3114//3115// Similarly with locks. The first lock slot in the osr buffer is the nth lock3116// from the interpreter frame, the nth lock slot in the osr buffer is 0th lock3117// in the interpreter frame (the method lock if a sync method)31183119// Initialize monitors in the compiled activation.31203121int index;3122Value local;31233124// find all the locals that the interpreter thinks contain live oops3125const ResourceBitMap live_oops = method()->live_local_oops_at_bci(osr_bci);31263127// compute the offset into the locals so that we can treat the buffer3128// as if the locals were still in the interpreter frame3129int locals_offset = BytesPerWord * (method()->max_locals() - 1);3130for_each_local_value(state, index, local) {3131int offset = locals_offset - (index + local->type()->size() - 1) * BytesPerWord;3132Value get;3133if (local->type()->is_object_kind() && !live_oops.at(index)) {3134// The interpreter thinks this local is dead but the compiler3135// doesn't so pretend that the interpreter passed in null.3136get = append(new Constant(objectNull));3137} else {3138get = append(new UnsafeGetRaw(as_BasicType(local->type()), e,3139append(new Constant(new IntConstant(offset))),31400,3141true /*unaligned*/, true /*wide*/));3142}3143_state->store_local(index, get);3144}31453146// the storage for the OSR buffer is freed manually in the LIRGenerator.31473148assert(state->caller_state() == NULL, "should be top scope");3149state->clear_locals();3150Goto* g = new Goto(target, false);3151append(g);3152_osr_entry->set_end(g);3153target->merge(_osr_entry->end()->state());31543155scope_data()->set_stream(NULL);3156}315731583159ValueStack* GraphBuilder::state_at_entry() {3160ValueStack* state = new ValueStack(scope(), NULL);31613162// Set up locals for receiver3163int idx = 0;3164if (!method()->is_static()) {3165// we should always see the receiver3166state->store_local(idx, new Local(method()->holder(), objectType, idx, true));3167idx = 1;3168}31693170// Set up locals for incoming arguments3171ciSignature* sig = method()->signature();3172for (int i = 0; i < sig->count(); i++) {3173ciType* type = sig->type_at(i);3174BasicType basic_type = type->basic_type();3175// don't allow T_ARRAY to propagate into locals types3176if (is_reference_type(basic_type)) basic_type = T_OBJECT;3177ValueType* vt = as_ValueType(basic_type);3178state->store_local(idx, new Local(type, vt, idx, false));3179idx += type->size();3180}31813182// lock synchronized method3183if (method()->is_synchronized()) {3184state->lock(NULL);3185}31863187return state;3188}318931903191GraphBuilder::GraphBuilder(Compilation* compilation, IRScope* scope)3192: _scope_data(NULL)3193, _compilation(compilation)3194, _memory(new MemoryBuffer())3195, _inline_bailout_msg(NULL)3196, _instruction_count(0)3197, _osr_entry(NULL)3198{3199int osr_bci = compilation->osr_bci();32003201// determine entry points and bci2block mapping3202BlockListBuilder blm(compilation, scope, osr_bci);3203CHECK_BAILOUT();32043205BlockList* bci2block = blm.bci2block();3206BlockBegin* start_block = bci2block->at(0);32073208push_root_scope(scope, bci2block, start_block);32093210// setup state for std entry3211_initial_state = state_at_entry();3212start_block->merge(_initial_state);32133214// complete graph3215_vmap = new ValueMap();3216switch (scope->method()->intrinsic_id()) {3217case vmIntrinsics::_dabs : // fall through3218case vmIntrinsics::_dsqrt : // fall through3219case vmIntrinsics::_dsin : // fall through3220case vmIntrinsics::_dcos : // fall through3221case vmIntrinsics::_dtan : // fall through3222case vmIntrinsics::_dlog : // fall through3223case vmIntrinsics::_dlog10 : // fall through3224case vmIntrinsics::_dexp : // fall through3225case vmIntrinsics::_dpow : // fall through3226{3227// Compiles where the root method is an intrinsic need a special3228// compilation environment because the bytecodes for the method3229// shouldn't be parsed during the compilation, only the special3230// Intrinsic node should be emitted. If this isn't done the the3231// code for the inlined version will be different than the root3232// compiled version which could lead to monotonicity problems on3233// intel.3234if (CheckIntrinsics && !scope->method()->intrinsic_candidate()) {3235BAILOUT("failed to inline intrinsic, method not annotated");3236}32373238// Set up a stream so that appending instructions works properly.3239ciBytecodeStream s(scope->method());3240s.reset_to_bci(0);3241scope_data()->set_stream(&s);3242s.next();32433244// setup the initial block state3245_block = start_block;3246_state = start_block->state()->copy_for_parsing();3247_last = start_block;3248load_local(doubleType, 0);3249if (scope->method()->intrinsic_id() == vmIntrinsics::_dpow) {3250load_local(doubleType, 2);3251}32523253// Emit the intrinsic node.3254bool result = try_inline_intrinsics(scope->method());3255if (!result) BAILOUT("failed to inline intrinsic");3256method_return(dpop());32573258// connect the begin and end blocks and we're all done.3259BlockEnd* end = last()->as_BlockEnd();3260block()->set_end(end);3261break;3262}32633264case vmIntrinsics::_Reference_get:3265{3266{3267// With java.lang.ref.reference.get() we must go through the3268// intrinsic - when G1 is enabled - even when get() is the root3269// method of the compile so that, if necessary, the value in3270// the referent field of the reference object gets recorded by3271// the pre-barrier code.3272// Specifically, if G1 is enabled, the value in the referent3273// field is recorded by the G1 SATB pre barrier. This will3274// result in the referent being marked live and the reference3275// object removed from the list of discovered references during3276// reference processing.3277if (CheckIntrinsics && !scope->method()->intrinsic_candidate()) {3278BAILOUT("failed to inline intrinsic, method not annotated");3279}32803281// Also we need intrinsic to prevent commoning reads from this field3282// across safepoint since GC can change its value.32833284// Set up a stream so that appending instructions works properly.3285ciBytecodeStream s(scope->method());3286s.reset_to_bci(0);3287scope_data()->set_stream(&s);3288s.next();32893290// setup the initial block state3291_block = start_block;3292_state = start_block->state()->copy_for_parsing();3293_last = start_block;3294load_local(objectType, 0);32953296// Emit the intrinsic node.3297bool result = try_inline_intrinsics(scope->method());3298if (!result) BAILOUT("failed to inline intrinsic");3299method_return(apop());33003301// connect the begin and end blocks and we're all done.3302BlockEnd* end = last()->as_BlockEnd();3303block()->set_end(end);3304break;3305}3306// Otherwise, fall thru3307}33083309default:3310scope_data()->add_to_work_list(start_block);3311iterate_all_blocks();3312break;3313}3314CHECK_BAILOUT();33153316_start = setup_start_block(osr_bci, start_block, _osr_entry, _initial_state);33173318eliminate_redundant_phis(_start);33193320NOT_PRODUCT(if (PrintValueNumbering && Verbose) print_stats());3321// for osr compile, bailout if some requirements are not fulfilled3322if (osr_bci != -1) {3323BlockBegin* osr_block = blm.bci2block()->at(osr_bci);3324if (!osr_block->is_set(BlockBegin::was_visited_flag)) {3325BAILOUT("osr entry must have been visited for osr compile");3326}33273328// check if osr entry point has empty stack - we cannot handle non-empty stacks at osr entry points3329if (!osr_block->state()->stack_is_empty()) {3330BAILOUT("stack not empty at OSR entry point");3331}3332}3333#ifndef PRODUCT3334if (PrintCompilation && Verbose) tty->print_cr("Created %d Instructions", _instruction_count);3335#endif3336}333733383339ValueStack* GraphBuilder::copy_state_before() {3340return copy_state_before_with_bci(bci());3341}33423343ValueStack* GraphBuilder::copy_state_exhandling() {3344return copy_state_exhandling_with_bci(bci());3345}33463347ValueStack* GraphBuilder::copy_state_for_exception() {3348return copy_state_for_exception_with_bci(bci());3349}33503351ValueStack* GraphBuilder::copy_state_before_with_bci(int bci) {3352return state()->copy(ValueStack::StateBefore, bci);3353}33543355ValueStack* GraphBuilder::copy_state_exhandling_with_bci(int bci) {3356if (!has_handler()) return NULL;3357return state()->copy(ValueStack::StateBefore, bci);3358}33593360ValueStack* GraphBuilder::copy_state_for_exception_with_bci(int bci) {3361ValueStack* s = copy_state_exhandling_with_bci(bci);3362if (s == NULL) {3363if (_compilation->env()->should_retain_local_variables()) {3364s = state()->copy(ValueStack::ExceptionState, bci);3365} else {3366s = state()->copy(ValueStack::EmptyExceptionState, bci);3367}3368}3369return s;3370}33713372int GraphBuilder::recursive_inline_level(ciMethod* cur_callee) const {3373int recur_level = 0;3374for (IRScope* s = scope(); s != NULL; s = s->caller()) {3375if (s->method() == cur_callee) {3376++recur_level;3377}3378}3379return recur_level;3380}338133823383bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc, Value receiver) {3384const char* msg = NULL;33853386// clear out any existing inline bailout condition3387clear_inline_bailout();33883389// exclude methods we don't want to inline3390msg = should_not_inline(callee);3391if (msg != NULL) {3392print_inlining(callee, msg, /*success*/ false);3393return false;3394}33953396// method handle invokes3397if (callee->is_method_handle_intrinsic()) {3398if (try_method_handle_inline(callee, ignore_return)) {3399if (callee->has_reserved_stack_access()) {3400compilation()->set_has_reserved_stack_access(true);3401}3402return true;3403}3404return false;3405}34063407// handle intrinsics3408if (callee->intrinsic_id() != vmIntrinsics::_none &&3409callee->check_intrinsic_candidate()) {3410if (try_inline_intrinsics(callee, ignore_return)) {3411print_inlining(callee, "intrinsic");3412if (callee->has_reserved_stack_access()) {3413compilation()->set_has_reserved_stack_access(true);3414}3415return true;3416}3417// try normal inlining3418}34193420// certain methods cannot be parsed at all3421msg = check_can_parse(callee);3422if (msg != NULL) {3423print_inlining(callee, msg, /*success*/ false);3424return false;3425}34263427// If bytecode not set use the current one.3428if (bc == Bytecodes::_illegal) {3429bc = code();3430}3431if (try_inline_full(callee, holder_known, ignore_return, bc, receiver)) {3432if (callee->has_reserved_stack_access()) {3433compilation()->set_has_reserved_stack_access(true);3434}3435return true;3436}34373438// Entire compilation could fail during try_inline_full call.3439// In that case printing inlining decision info is useless.3440if (!bailed_out())3441print_inlining(callee, _inline_bailout_msg, /*success*/ false);34423443return false;3444}344534463447const char* GraphBuilder::check_can_parse(ciMethod* callee) const {3448// Certain methods cannot be parsed at all:3449if ( callee->is_native()) return "native method";3450if ( callee->is_abstract()) return "abstract method";3451if (!callee->can_be_parsed()) return "cannot be parsed";3452return NULL;3453}34543455// negative filter: should callee NOT be inlined? returns NULL, ok to inline, or rejection msg3456const char* GraphBuilder::should_not_inline(ciMethod* callee) const {3457if ( compilation()->directive()->should_not_inline(callee)) return "disallowed by CompileCommand";3458if ( callee->dont_inline()) return "don't inline by annotation";3459return NULL;3460}34613462void GraphBuilder::build_graph_for_intrinsic(ciMethod* callee, bool ignore_return) {3463vmIntrinsics::ID id = callee->intrinsic_id();3464assert(id != vmIntrinsics::_none, "must be a VM intrinsic");34653466// Some intrinsics need special IR nodes.3467switch(id) {3468case vmIntrinsics::_getReference : append_unsafe_get_obj(callee, T_OBJECT, false); return;3469case vmIntrinsics::_getBoolean : append_unsafe_get_obj(callee, T_BOOLEAN, false); return;3470case vmIntrinsics::_getByte : append_unsafe_get_obj(callee, T_BYTE, false); return;3471case vmIntrinsics::_getShort : append_unsafe_get_obj(callee, T_SHORT, false); return;3472case vmIntrinsics::_getChar : append_unsafe_get_obj(callee, T_CHAR, false); return;3473case vmIntrinsics::_getInt : append_unsafe_get_obj(callee, T_INT, false); return;3474case vmIntrinsics::_getLong : append_unsafe_get_obj(callee, T_LONG, false); return;3475case vmIntrinsics::_getFloat : append_unsafe_get_obj(callee, T_FLOAT, false); return;3476case vmIntrinsics::_getDouble : append_unsafe_get_obj(callee, T_DOUBLE, false); return;3477case vmIntrinsics::_putReference : append_unsafe_put_obj(callee, T_OBJECT, false); return;3478case vmIntrinsics::_putBoolean : append_unsafe_put_obj(callee, T_BOOLEAN, false); return;3479case vmIntrinsics::_putByte : append_unsafe_put_obj(callee, T_BYTE, false); return;3480case vmIntrinsics::_putShort : append_unsafe_put_obj(callee, T_SHORT, false); return;3481case vmIntrinsics::_putChar : append_unsafe_put_obj(callee, T_CHAR, false); return;3482case vmIntrinsics::_putInt : append_unsafe_put_obj(callee, T_INT, false); return;3483case vmIntrinsics::_putLong : append_unsafe_put_obj(callee, T_LONG, false); return;3484case vmIntrinsics::_putFloat : append_unsafe_put_obj(callee, T_FLOAT, false); return;3485case vmIntrinsics::_putDouble : append_unsafe_put_obj(callee, T_DOUBLE, false); return;3486case vmIntrinsics::_getShortUnaligned : append_unsafe_get_obj(callee, T_SHORT, false); return;3487case vmIntrinsics::_getCharUnaligned : append_unsafe_get_obj(callee, T_CHAR, false); return;3488case vmIntrinsics::_getIntUnaligned : append_unsafe_get_obj(callee, T_INT, false); return;3489case vmIntrinsics::_getLongUnaligned : append_unsafe_get_obj(callee, T_LONG, false); return;3490case vmIntrinsics::_putShortUnaligned : append_unsafe_put_obj(callee, T_SHORT, false); return;3491case vmIntrinsics::_putCharUnaligned : append_unsafe_put_obj(callee, T_CHAR, false); return;3492case vmIntrinsics::_putIntUnaligned : append_unsafe_put_obj(callee, T_INT, false); return;3493case vmIntrinsics::_putLongUnaligned : append_unsafe_put_obj(callee, T_LONG, false); return;3494case vmIntrinsics::_getReferenceVolatile : append_unsafe_get_obj(callee, T_OBJECT, true); return;3495case vmIntrinsics::_getBooleanVolatile : append_unsafe_get_obj(callee, T_BOOLEAN, true); return;3496case vmIntrinsics::_getByteVolatile : append_unsafe_get_obj(callee, T_BYTE, true); return;3497case vmIntrinsics::_getShortVolatile : append_unsafe_get_obj(callee, T_SHORT, true); return;3498case vmIntrinsics::_getCharVolatile : append_unsafe_get_obj(callee, T_CHAR, true); return;3499case vmIntrinsics::_getIntVolatile : append_unsafe_get_obj(callee, T_INT, true); return;3500case vmIntrinsics::_getLongVolatile : append_unsafe_get_obj(callee, T_LONG, true); return;3501case vmIntrinsics::_getFloatVolatile : append_unsafe_get_obj(callee, T_FLOAT, true); return;3502case vmIntrinsics::_getDoubleVolatile : append_unsafe_get_obj(callee, T_DOUBLE, true); return;3503case vmIntrinsics::_putReferenceVolatile : append_unsafe_put_obj(callee, T_OBJECT, true); return;3504case vmIntrinsics::_putBooleanVolatile : append_unsafe_put_obj(callee, T_BOOLEAN, true); return;3505case vmIntrinsics::_putByteVolatile : append_unsafe_put_obj(callee, T_BYTE, true); return;3506case vmIntrinsics::_putShortVolatile : append_unsafe_put_obj(callee, T_SHORT, true); return;3507case vmIntrinsics::_putCharVolatile : append_unsafe_put_obj(callee, T_CHAR, true); return;3508case vmIntrinsics::_putIntVolatile : append_unsafe_put_obj(callee, T_INT, true); return;3509case vmIntrinsics::_putLongVolatile : append_unsafe_put_obj(callee, T_LONG, true); return;3510case vmIntrinsics::_putFloatVolatile : append_unsafe_put_obj(callee, T_FLOAT, true); return;3511case vmIntrinsics::_putDoubleVolatile : append_unsafe_put_obj(callee, T_DOUBLE, true); return;3512case vmIntrinsics::_compareAndSetLong:3513case vmIntrinsics::_compareAndSetInt:3514case vmIntrinsics::_compareAndSetReference : append_unsafe_CAS(callee); return;3515case vmIntrinsics::_getAndAddInt:3516case vmIntrinsics::_getAndAddLong : append_unsafe_get_and_set_obj(callee, true); return;3517case vmIntrinsics::_getAndSetInt :3518case vmIntrinsics::_getAndSetLong :3519case vmIntrinsics::_getAndSetReference : append_unsafe_get_and_set_obj(callee, false); return;3520case vmIntrinsics::_getCharStringU : append_char_access(callee, false); return;3521case vmIntrinsics::_putCharStringU : append_char_access(callee, true); return;3522default:3523break;3524}35253526// create intrinsic node3527const bool has_receiver = !callee->is_static();3528ValueType* result_type = as_ValueType(callee->return_type());3529ValueStack* state_before = copy_state_for_exception();35303531Values* args = state()->pop_arguments(callee->arg_size());35323533if (is_profiling()) {3534// Don't profile in the special case where the root method3535// is the intrinsic3536if (callee != method()) {3537// Note that we'd collect profile data in this method if we wanted it.3538compilation()->set_would_profile(true);3539if (profile_calls()) {3540Value recv = NULL;3541if (has_receiver) {3542recv = args->at(0);3543null_check(recv);3544}3545profile_call(callee, recv, NULL, collect_args_for_profiling(args, callee, true), true);3546}3547}3548}35493550Intrinsic* result = new Intrinsic(result_type, callee->intrinsic_id(),3551args, has_receiver, state_before,3552vmIntrinsics::preserves_state(id),3553vmIntrinsics::can_trap(id));3554// append instruction & push result3555Value value = append_split(result);3556if (result_type != voidType && !ignore_return) {3557push(result_type, value);3558}35593560if (callee != method() && profile_return() && result_type->is_object_kind()) {3561profile_return_type(result, callee);3562}3563}35643565bool GraphBuilder::try_inline_intrinsics(ciMethod* callee, bool ignore_return) {3566// For calling is_intrinsic_available we need to transition to3567// the '_thread_in_vm' state because is_intrinsic_available()3568// accesses critical VM-internal data.3569bool is_available = false;3570{3571VM_ENTRY_MARK;3572methodHandle mh(THREAD, callee->get_Method());3573is_available = _compilation->compiler()->is_intrinsic_available(mh, _compilation->directive());3574}35753576if (!is_available) {3577if (!InlineNatives) {3578// Return false and also set message that the inlining of3579// intrinsics has been disabled in general.3580INLINE_BAILOUT("intrinsic method inlining disabled");3581} else {3582return false;3583}3584}3585build_graph_for_intrinsic(callee, ignore_return);3586return true;3587}358835893590bool GraphBuilder::try_inline_jsr(int jsr_dest_bci) {3591// Introduce a new callee continuation point - all Ret instructions3592// will be replaced with Gotos to this point.3593BlockBegin* cont = block_at(next_bci());3594assert(cont != NULL, "continuation must exist (BlockListBuilder starts a new block after a jsr");35953596// Note: can not assign state to continuation yet, as we have to3597// pick up the state from the Ret instructions.35983599// Push callee scope3600push_scope_for_jsr(cont, jsr_dest_bci);36013602// Temporarily set up bytecode stream so we can append instructions3603// (only using the bci of this stream)3604scope_data()->set_stream(scope_data()->parent()->stream());36053606BlockBegin* jsr_start_block = block_at(jsr_dest_bci);3607assert(jsr_start_block != NULL, "jsr start block must exist");3608assert(!jsr_start_block->is_set(BlockBegin::was_visited_flag), "should not have visited jsr yet");3609Goto* goto_sub = new Goto(jsr_start_block, false);3610// Must copy state to avoid wrong sharing when parsing bytecodes3611assert(jsr_start_block->state() == NULL, "should have fresh jsr starting block");3612jsr_start_block->set_state(copy_state_before_with_bci(jsr_dest_bci));3613append(goto_sub);3614_block->set_end(goto_sub);3615_last = _block = jsr_start_block;36163617// Clear out bytecode stream3618scope_data()->set_stream(NULL);36193620scope_data()->add_to_work_list(jsr_start_block);36213622// Ready to resume parsing in subroutine3623iterate_all_blocks();36243625// If we bailed out during parsing, return immediately (this is bad news)3626CHECK_BAILOUT_(false);36273628// Detect whether the continuation can actually be reached. If not,3629// it has not had state set by the join() operations in3630// iterate_bytecodes_for_block()/ret() and we should not touch the3631// iteration state. The calling activation of3632// iterate_bytecodes_for_block will then complete normally.3633if (cont->state() != NULL) {3634if (!cont->is_set(BlockBegin::was_visited_flag)) {3635// add continuation to work list instead of parsing it immediately3636scope_data()->parent()->add_to_work_list(cont);3637}3638}36393640assert(jsr_continuation() == cont, "continuation must not have changed");3641assert(!jsr_continuation()->is_set(BlockBegin::was_visited_flag) ||3642jsr_continuation()->is_set(BlockBegin::parser_loop_header_flag),3643"continuation can only be visited in case of backward branches");3644assert(_last && _last->as_BlockEnd(), "block must have end");36453646// continuation is in work list, so end iteration of current block3647_skip_block = true;3648pop_scope_for_jsr();36493650return true;3651}365236533654// Inline the entry of a synchronized method as a monitor enter and3655// register the exception handler which releases the monitor if an3656// exception is thrown within the callee. Note that the monitor enter3657// cannot throw an exception itself, because the receiver is3658// guaranteed to be non-null by the explicit null check at the3659// beginning of inlining.3660void GraphBuilder::inline_sync_entry(Value lock, BlockBegin* sync_handler) {3661assert(lock != NULL && sync_handler != NULL, "lock or handler missing");36623663monitorenter(lock, SynchronizationEntryBCI);3664assert(_last->as_MonitorEnter() != NULL, "monitor enter expected");3665_last->set_needs_null_check(false);36663667sync_handler->set(BlockBegin::exception_entry_flag);3668sync_handler->set(BlockBegin::is_on_work_list_flag);36693670ciExceptionHandler* desc = new ciExceptionHandler(method()->holder(), 0, method()->code_size(), -1, 0);3671XHandler* h = new XHandler(desc);3672h->set_entry_block(sync_handler);3673scope_data()->xhandlers()->append(h);3674scope_data()->set_has_handler();3675}367636773678// If an exception is thrown and not handled within an inlined3679// synchronized method, the monitor must be released before the3680// exception is rethrown in the outer scope. Generate the appropriate3681// instructions here.3682void GraphBuilder::fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler) {3683BlockBegin* orig_block = _block;3684ValueStack* orig_state = _state;3685Instruction* orig_last = _last;3686_last = _block = sync_handler;3687_state = sync_handler->state()->copy();36883689assert(sync_handler != NULL, "handler missing");3690assert(!sync_handler->is_set(BlockBegin::was_visited_flag), "is visited here");36913692assert(lock != NULL || default_handler, "lock or handler missing");36933694XHandler* h = scope_data()->xhandlers()->remove_last();3695assert(h->entry_block() == sync_handler, "corrupt list of handlers");36963697block()->set(BlockBegin::was_visited_flag);3698Value exception = append_with_bci(new ExceptionObject(), SynchronizationEntryBCI);3699assert(exception->is_pinned(), "must be");37003701int bci = SynchronizationEntryBCI;3702if (compilation()->env()->dtrace_method_probes()) {3703// Report exit from inline methods. We don't have a stream here3704// so pass an explicit bci of SynchronizationEntryBCI.3705Values* args = new Values(1);3706args->push(append_with_bci(new Constant(new MethodConstant(method())), bci));3707append_with_bci(new RuntimeCall(voidType, "dtrace_method_exit", CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), args), bci);3708}37093710if (lock) {3711assert(state()->locks_size() > 0 && state()->lock_at(state()->locks_size() - 1) == lock, "lock is missing");3712if (!lock->is_linked()) {3713lock = append_with_bci(lock, bci);3714}37153716// exit the monitor in the context of the synchronized method3717monitorexit(lock, bci);37183719// exit the context of the synchronized method3720if (!default_handler) {3721pop_scope();3722bci = _state->caller_state()->bci();3723_state = _state->caller_state()->copy_for_parsing();3724}3725}37263727// perform the throw as if at the the call site3728apush(exception);3729throw_op(bci);37303731BlockEnd* end = last()->as_BlockEnd();3732block()->set_end(end);37333734_block = orig_block;3735_state = orig_state;3736_last = orig_last;3737}373837393740bool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc, Value receiver) {3741assert(!callee->is_native(), "callee must not be native");3742if (CompilationPolicy::should_not_inline(compilation()->env(), callee)) {3743INLINE_BAILOUT("inlining prohibited by policy");3744}3745// first perform tests of things it's not possible to inline3746if (callee->has_exception_handlers() &&3747!InlineMethodsWithExceptionHandlers) INLINE_BAILOUT("callee has exception handlers");3748if (callee->is_synchronized() &&3749!InlineSynchronizedMethods ) INLINE_BAILOUT("callee is synchronized");3750if (!callee->holder()->is_linked()) INLINE_BAILOUT("callee's klass not linked yet");3751if (bc == Bytecodes::_invokestatic &&3752!callee->holder()->is_initialized()) INLINE_BAILOUT("callee's klass not initialized yet");3753if (!callee->has_balanced_monitors()) INLINE_BAILOUT("callee's monitors do not match");37543755// Proper inlining of methods with jsrs requires a little more work.3756if (callee->has_jsrs() ) INLINE_BAILOUT("jsrs not handled properly by inliner yet");37573758if (is_profiling() && !callee->ensure_method_data()) {3759INLINE_BAILOUT("mdo allocation failed");3760}37613762const bool is_invokedynamic = (bc == Bytecodes::_invokedynamic);3763const bool has_receiver = (bc != Bytecodes::_invokestatic && !is_invokedynamic);37643765const int args_base = state()->stack_size() - callee->arg_size();3766assert(args_base >= 0, "stack underflow during inlining");37673768Value recv = NULL;3769if (has_receiver) {3770assert(!callee->is_static(), "callee must not be static");3771assert(callee->arg_size() > 0, "must have at least a receiver");37723773recv = state()->stack_at(args_base);3774if (recv->is_null_obj()) {3775INLINE_BAILOUT("receiver is always null");3776}3777}37783779// now perform tests that are based on flag settings3780bool inlinee_by_directive = compilation()->directive()->should_inline(callee);3781if (callee->force_inline() || inlinee_by_directive) {3782if (inline_level() > MaxForceInlineLevel ) INLINE_BAILOUT("MaxForceInlineLevel");3783if (recursive_inline_level(callee) > C1MaxRecursiveInlineLevel) INLINE_BAILOUT("recursive inlining too deep");37843785const char* msg = "";3786if (callee->force_inline()) msg = "force inline by annotation";3787if (inlinee_by_directive) msg = "force inline by CompileCommand";3788print_inlining(callee, msg);3789} else {3790// use heuristic controls on inlining3791if (inline_level() > C1MaxInlineLevel ) INLINE_BAILOUT("inlining too deep");3792int callee_recursive_level = recursive_inline_level(callee);3793if (callee_recursive_level > C1MaxRecursiveInlineLevel ) INLINE_BAILOUT("recursive inlining too deep");3794if (callee->code_size_for_inlining() > max_inline_size() ) INLINE_BAILOUT("callee is too large");3795// Additional condition to limit stack usage for non-recursive calls.3796if ((callee_recursive_level == 0) &&3797(callee->max_stack() + callee->max_locals() - callee->size_of_parameters() > C1InlineStackLimit)) {3798INLINE_BAILOUT("callee uses too much stack");3799}38003801// don't inline throwable methods unless the inlining tree is rooted in a throwable class3802if (callee->name() == ciSymbols::object_initializer_name() &&3803callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {3804// Throwable constructor call3805IRScope* top = scope();3806while (top->caller() != NULL) {3807top = top->caller();3808}3809if (!top->method()->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {3810INLINE_BAILOUT("don't inline Throwable constructors");3811}3812}38133814if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) {3815INLINE_BAILOUT("total inlining greater than DesiredMethodLimit");3816}3817// printing3818print_inlining(callee, "inline", /*success*/ true);3819}38203821assert(bc != Bytecodes::_invokestatic || callee->holder()->is_initialized(), "required");38223823// NOTE: Bailouts from this point on, which occur at the3824// GraphBuilder level, do not cause bailout just of the inlining but3825// in fact of the entire compilation.38263827BlockBegin* orig_block = block();38283829// Insert null check if necessary3830if (has_receiver) {3831// note: null check must happen even if first instruction of callee does3832// an implicit null check since the callee is in a different scope3833// and we must make sure exception handling does the right thing3834null_check(recv);3835}38363837if (is_profiling()) {3838// Note that we'd collect profile data in this method if we wanted it.3839// this may be redundant here...3840compilation()->set_would_profile(true);38413842if (profile_calls()) {3843int start = 0;3844Values* obj_args = args_list_for_profiling(callee, start, has_receiver);3845if (obj_args != NULL) {3846int s = obj_args->max_length();3847// if called through method handle invoke, some arguments may have been popped3848for (int i = args_base+start, j = 0; j < obj_args->max_length() && i < state()->stack_size(); ) {3849Value v = state()->stack_at_inc(i);3850if (v->type()->is_object_kind()) {3851obj_args->push(v);3852j++;3853}3854}3855check_args_for_profiling(obj_args, s);3856}3857profile_call(callee, recv, holder_known ? callee->holder() : NULL, obj_args, true);3858}3859}38603861// Introduce a new callee continuation point - if the callee has3862// more than one return instruction or the return does not allow3863// fall-through of control flow, all return instructions of the3864// callee will need to be replaced by Goto's pointing to this3865// continuation point.3866BlockBegin* cont = block_at(next_bci());3867bool continuation_existed = true;3868if (cont == NULL) {3869cont = new BlockBegin(next_bci());3870// low number so that continuation gets parsed as early as possible3871cont->set_depth_first_number(0);3872if (PrintInitialBlockList) {3873tty->print_cr("CFG: created block %d (bci %d) as continuation for inline at bci %d",3874cont->block_id(), cont->bci(), bci());3875}3876continuation_existed = false;3877}3878// Record number of predecessors of continuation block before3879// inlining, to detect if inlined method has edges to its3880// continuation after inlining.3881int continuation_preds = cont->number_of_preds();38823883// Push callee scope3884push_scope(callee, cont);38853886// the BlockListBuilder for the callee could have bailed out3887if (bailed_out())3888return false;38893890// Temporarily set up bytecode stream so we can append instructions3891// (only using the bci of this stream)3892scope_data()->set_stream(scope_data()->parent()->stream());38933894// Pass parameters into callee state: add assignments3895// note: this will also ensure that all arguments are computed before being passed3896ValueStack* callee_state = state();3897ValueStack* caller_state = state()->caller_state();3898for (int i = args_base; i < caller_state->stack_size(); ) {3899const int arg_no = i - args_base;3900Value arg = caller_state->stack_at_inc(i);3901store_local(callee_state, arg, arg_no);3902}39033904// Remove args from stack.3905// Note that we preserve locals state in case we can use it later3906// (see use of pop_scope() below)3907caller_state->truncate_stack(args_base);3908assert(callee_state->stack_size() == 0, "callee stack must be empty");39093910Value lock = NULL;3911BlockBegin* sync_handler = NULL;39123913// Inline the locking of the receiver if the callee is synchronized3914if (callee->is_synchronized()) {3915lock = callee->is_static() ? append(new Constant(new InstanceConstant(callee->holder()->java_mirror())))3916: state()->local_at(0);3917sync_handler = new BlockBegin(SynchronizationEntryBCI);3918inline_sync_entry(lock, sync_handler);3919}39203921if (compilation()->env()->dtrace_method_probes()) {3922Values* args = new Values(1);3923args->push(append(new Constant(new MethodConstant(method()))));3924append(new RuntimeCall(voidType, "dtrace_method_entry", CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), args));3925}39263927if (profile_inlined_calls()) {3928profile_invocation(callee, copy_state_before_with_bci(SynchronizationEntryBCI));3929}39303931BlockBegin* callee_start_block = block_at(0);3932if (callee_start_block != NULL) {3933assert(callee_start_block->is_set(BlockBegin::parser_loop_header_flag), "must be loop header");3934Goto* goto_callee = new Goto(callee_start_block, false);3935// The state for this goto is in the scope of the callee, so use3936// the entry bci for the callee instead of the call site bci.3937append_with_bci(goto_callee, 0);3938_block->set_end(goto_callee);3939callee_start_block->merge(callee_state);39403941_last = _block = callee_start_block;39423943scope_data()->add_to_work_list(callee_start_block);3944}39453946// Clear out bytecode stream3947scope_data()->set_stream(NULL);3948scope_data()->set_ignore_return(ignore_return);39493950CompileLog* log = compilation()->log();3951if (log != NULL) log->head("parse method='%d'", log->identify(callee));39523953// Ready to resume parsing in callee (either in the same block we3954// were in before or in the callee's start block)3955iterate_all_blocks(callee_start_block == NULL);39563957if (log != NULL) log->done("parse");39583959// If we bailed out during parsing, return immediately (this is bad news)3960if (bailed_out())3961return false;39623963// iterate_all_blocks theoretically traverses in random order; in3964// practice, we have only traversed the continuation if we are3965// inlining into a subroutine3966assert(continuation_existed ||3967!continuation()->is_set(BlockBegin::was_visited_flag),3968"continuation should not have been parsed yet if we created it");39693970// At this point we are almost ready to return and resume parsing of3971// the caller back in the GraphBuilder. The only thing we want to do3972// first is an optimization: during parsing of the callee we3973// generated at least one Goto to the continuation block. If we3974// generated exactly one, and if the inlined method spanned exactly3975// one block (and we didn't have to Goto its entry), then we snip3976// off the Goto to the continuation, allowing control to fall3977// through back into the caller block and effectively performing3978// block merging. This allows load elimination and CSE to take place3979// across multiple callee scopes if they are relatively simple, and3980// is currently essential to making inlining profitable.3981if (num_returns() == 13982&& block() == orig_block3983&& block() == inline_cleanup_block()) {3984_last = inline_cleanup_return_prev();3985_state = inline_cleanup_state();3986} else if (continuation_preds == cont->number_of_preds()) {3987// Inlining caused that the instructions after the invoke in the3988// caller are not reachable any more. So skip filling this block3989// with instructions!3990assert(cont == continuation(), "");3991assert(_last && _last->as_BlockEnd(), "");3992_skip_block = true;3993} else {3994// Resume parsing in continuation block unless it was already parsed.3995// Note that if we don't change _last here, iteration in3996// iterate_bytecodes_for_block will stop when we return.3997if (!continuation()->is_set(BlockBegin::was_visited_flag)) {3998// add continuation to work list instead of parsing it immediately3999assert(_last && _last->as_BlockEnd(), "");4000scope_data()->parent()->add_to_work_list(continuation());4001_skip_block = true;4002}4003}40044005// Fill the exception handler for synchronized methods with instructions4006if (callee->is_synchronized() && sync_handler->state() != NULL) {4007fill_sync_handler(lock, sync_handler);4008} else {4009pop_scope();4010}40114012compilation()->notice_inlined_method(callee);40134014return true;4015}401640174018bool GraphBuilder::try_method_handle_inline(ciMethod* callee, bool ignore_return) {4019ValueStack* state_before = copy_state_before();4020vmIntrinsics::ID iid = callee->intrinsic_id();4021switch (iid) {4022case vmIntrinsics::_invokeBasic:4023{4024// get MethodHandle receiver4025const int args_base = state()->stack_size() - callee->arg_size();4026ValueType* type = state()->stack_at(args_base)->type();4027if (type->is_constant()) {4028ciMethod* target = type->as_ObjectType()->constant_value()->as_method_handle()->get_vmtarget();4029// We don't do CHA here so only inline static and statically bindable methods.4030if (target->is_static() || target->can_be_statically_bound()) {4031if (ciMethod::is_consistent_info(callee, target)) {4032Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual;4033ignore_return = ignore_return || (callee->return_type()->is_void() && !target->return_type()->is_void());4034if (try_inline(target, /*holder_known*/ !callee->is_static(), ignore_return, bc)) {4035return true;4036}4037} else {4038print_inlining(target, "signatures mismatch", /*success*/ false);4039}4040} else {4041print_inlining(target, "not static or statically bindable", /*success*/ false);4042}4043} else {4044print_inlining(callee, "receiver not constant", /*success*/ false);4045}4046}4047break;40484049case vmIntrinsics::_linkToVirtual:4050case vmIntrinsics::_linkToStatic:4051case vmIntrinsics::_linkToSpecial:4052case vmIntrinsics::_linkToInterface:4053{4054// pop MemberName argument4055const int args_base = state()->stack_size() - callee->arg_size();4056ValueType* type = apop()->type();4057if (type->is_constant()) {4058ciMethod* target = type->as_ObjectType()->constant_value()->as_member_name()->get_vmtarget();4059ignore_return = ignore_return || (callee->return_type()->is_void() && !target->return_type()->is_void());4060// If the target is another method handle invoke, try to recursively get4061// a better target.4062if (target->is_method_handle_intrinsic()) {4063if (try_method_handle_inline(target, ignore_return)) {4064return true;4065}4066} else if (!ciMethod::is_consistent_info(callee, target)) {4067print_inlining(target, "signatures mismatch", /*success*/ false);4068} else {4069ciSignature* signature = target->signature();4070const int receiver_skip = target->is_static() ? 0 : 1;4071// Cast receiver to its type.4072if (!target->is_static()) {4073ciKlass* tk = signature->accessing_klass();4074Value obj = state()->stack_at(args_base);4075if (obj->exact_type() == NULL &&4076obj->declared_type() != tk && tk != compilation()->env()->Object_klass()) {4077TypeCast* c = new TypeCast(tk, obj, state_before);4078append(c);4079state()->stack_at_put(args_base, c);4080}4081}4082// Cast reference arguments to its type.4083for (int i = 0, j = 0; i < signature->count(); i++) {4084ciType* t = signature->type_at(i);4085if (t->is_klass()) {4086ciKlass* tk = t->as_klass();4087Value obj = state()->stack_at(args_base + receiver_skip + j);4088if (obj->exact_type() == NULL &&4089obj->declared_type() != tk && tk != compilation()->env()->Object_klass()) {4090TypeCast* c = new TypeCast(t, obj, state_before);4091append(c);4092state()->stack_at_put(args_base + receiver_skip + j, c);4093}4094}4095j += t->size(); // long and double take two slots4096}4097// We don't do CHA here so only inline static and statically bindable methods.4098if (target->is_static() || target->can_be_statically_bound()) {4099Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual;4100if (try_inline(target, /*holder_known*/ !callee->is_static(), ignore_return, bc)) {4101return true;4102}4103} else {4104print_inlining(target, "not static or statically bindable", /*success*/ false);4105}4106}4107} else {4108print_inlining(callee, "MemberName not constant", /*success*/ false);4109}4110}4111break;41124113case vmIntrinsics::_linkToNative:4114break; // TODO: NYI41154116default:4117fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));4118break;4119}4120set_state(state_before->copy_for_parsing());4121return false;4122}412341244125void GraphBuilder::inline_bailout(const char* msg) {4126assert(msg != NULL, "inline bailout msg must exist");4127_inline_bailout_msg = msg;4128}412941304131void GraphBuilder::clear_inline_bailout() {4132_inline_bailout_msg = NULL;4133}413441354136void GraphBuilder::push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start) {4137ScopeData* data = new ScopeData(NULL);4138data->set_scope(scope);4139data->set_bci2block(bci2block);4140_scope_data = data;4141_block = start;4142}414341444145void GraphBuilder::push_scope(ciMethod* callee, BlockBegin* continuation) {4146IRScope* callee_scope = new IRScope(compilation(), scope(), bci(), callee, -1, false);4147scope()->add_callee(callee_scope);41484149BlockListBuilder blb(compilation(), callee_scope, -1);4150CHECK_BAILOUT();41514152if (!blb.bci2block()->at(0)->is_set(BlockBegin::parser_loop_header_flag)) {4153// this scope can be inlined directly into the caller so remove4154// the block at bci 0.4155blb.bci2block()->at_put(0, NULL);4156}41574158set_state(new ValueStack(callee_scope, state()->copy(ValueStack::CallerState, bci())));41594160ScopeData* data = new ScopeData(scope_data());4161data->set_scope(callee_scope);4162data->set_bci2block(blb.bci2block());4163data->set_continuation(continuation);4164_scope_data = data;4165}416641674168void GraphBuilder::push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci) {4169ScopeData* data = new ScopeData(scope_data());4170data->set_parsing_jsr();4171data->set_jsr_entry_bci(jsr_dest_bci);4172data->set_jsr_return_address_local(-1);4173// Must clone bci2block list as we will be mutating it in order to4174// properly clone all blocks in jsr region as well as exception4175// handlers containing rets4176BlockList* new_bci2block = new BlockList(bci2block()->length());4177new_bci2block->appendAll(bci2block());4178data->set_bci2block(new_bci2block);4179data->set_scope(scope());4180data->setup_jsr_xhandlers();4181data->set_continuation(continuation());4182data->set_jsr_continuation(jsr_continuation);4183_scope_data = data;4184}418541864187void GraphBuilder::pop_scope() {4188int number_of_locks = scope()->number_of_locks();4189_scope_data = scope_data()->parent();4190// accumulate minimum number of monitor slots to be reserved4191scope()->set_min_number_of_locks(number_of_locks);4192}419341944195void GraphBuilder::pop_scope_for_jsr() {4196_scope_data = scope_data()->parent();4197}41984199void GraphBuilder::append_unsafe_get_obj(ciMethod* callee, BasicType t, bool is_volatile) {4200Values* args = state()->pop_arguments(callee->arg_size());4201null_check(args->at(0));4202Instruction* offset = args->at(2);4203#ifndef _LP644204offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));4205#endif4206Instruction* op = append(new UnsafeGetObject(t, args->at(1), offset, is_volatile));4207push(op->type(), op);4208compilation()->set_has_unsafe_access(true);4209}421042114212void GraphBuilder::append_unsafe_put_obj(ciMethod* callee, BasicType t, bool is_volatile) {4213Values* args = state()->pop_arguments(callee->arg_size());4214null_check(args->at(0));4215Instruction* offset = args->at(2);4216#ifndef _LP644217offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));4218#endif4219Value val = args->at(3);4220if (t == T_BOOLEAN) {4221Value mask = append(new Constant(new IntConstant(1)));4222val = append(new LogicOp(Bytecodes::_iand, val, mask));4223}4224Instruction* op = append(new UnsafePutObject(t, args->at(1), offset, val, is_volatile));4225compilation()->set_has_unsafe_access(true);4226kill_all();4227}422842294230void GraphBuilder::append_unsafe_get_raw(ciMethod* callee, BasicType t) {4231Values* args = state()->pop_arguments(callee->arg_size());4232null_check(args->at(0));4233Instruction* op = append(new UnsafeGetRaw(t, args->at(1), false));4234push(op->type(), op);4235compilation()->set_has_unsafe_access(true);4236}423742384239void GraphBuilder::append_unsafe_put_raw(ciMethod* callee, BasicType t) {4240Values* args = state()->pop_arguments(callee->arg_size());4241null_check(args->at(0));4242Instruction* op = append(new UnsafePutRaw(t, args->at(1), args->at(2)));4243compilation()->set_has_unsafe_access(true);4244}424542464247void GraphBuilder::append_unsafe_CAS(ciMethod* callee) {4248ValueStack* state_before = copy_state_for_exception();4249ValueType* result_type = as_ValueType(callee->return_type());4250assert(result_type->is_int(), "int result");4251Values* args = state()->pop_arguments(callee->arg_size());42524253// Pop off some args to specially handle, then push back4254Value newval = args->pop();4255Value cmpval = args->pop();4256Value offset = args->pop();4257Value src = args->pop();4258Value unsafe_obj = args->pop();42594260// Separately handle the unsafe arg. It is not needed for code4261// generation, but must be null checked4262null_check(unsafe_obj);42634264#ifndef _LP644265offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));4266#endif42674268args->push(src);4269args->push(offset);4270args->push(cmpval);4271args->push(newval);42724273// An unsafe CAS can alias with other field accesses, but we don't4274// know which ones so mark the state as no preserved. This will4275// cause CSE to invalidate memory across it.4276bool preserves_state = false;4277Intrinsic* result = new Intrinsic(result_type, callee->intrinsic_id(), args, false, state_before, preserves_state);4278append_split(result);4279push(result_type, result);4280compilation()->set_has_unsafe_access(true);4281}42824283void GraphBuilder::append_char_access(ciMethod* callee, bool is_store) {4284// This intrinsic accesses byte[] array as char[] array. Computing the offsets4285// correctly requires matched array shapes.4286assert (arrayOopDesc::base_offset_in_bytes(T_CHAR) == arrayOopDesc::base_offset_in_bytes(T_BYTE),4287"sanity: byte[] and char[] bases agree");4288assert (type2aelembytes(T_CHAR) == type2aelembytes(T_BYTE)*2,4289"sanity: byte[] and char[] scales agree");42904291ValueStack* state_before = copy_state_indexed_access();4292compilation()->set_has_access_indexed(true);4293Values* args = state()->pop_arguments(callee->arg_size());4294Value array = args->at(0);4295Value index = args->at(1);4296if (is_store) {4297Value value = args->at(2);4298Instruction* store = append(new StoreIndexed(array, index, NULL, T_CHAR, value, state_before, false, true));4299store->set_flag(Instruction::NeedsRangeCheckFlag, false);4300_memory->store_value(value);4301} else {4302Instruction* load = append(new LoadIndexed(array, index, NULL, T_CHAR, state_before, true));4303load->set_flag(Instruction::NeedsRangeCheckFlag, false);4304push(load->type(), load);4305}4306}43074308void GraphBuilder::print_inlining(ciMethod* callee, const char* msg, bool success) {4309CompileLog* log = compilation()->log();4310if (log != NULL) {4311assert(msg != NULL, "inlining msg should not be null!");4312if (success) {4313log->inline_success(msg);4314} else {4315log->inline_fail(msg);4316}4317}4318EventCompilerInlining event;4319if (event.should_commit()) {4320CompilerEvent::InlineEvent::post(event, compilation()->env()->task()->compile_id(), method()->get_Method(), callee, success, msg, bci());4321}43224323CompileTask::print_inlining_ul(callee, scope()->level(), bci(), msg);43244325if (!compilation()->directive()->PrintInliningOption) {4326return;4327}4328CompileTask::print_inlining_tty(callee, scope()->level(), bci(), msg);4329if (success && CIPrintMethodCodes) {4330callee->print_codes();4331}4332}43334334void GraphBuilder::append_unsafe_get_and_set_obj(ciMethod* callee, bool is_add) {4335Values* args = state()->pop_arguments(callee->arg_size());4336BasicType t = callee->return_type()->basic_type();4337null_check(args->at(0));4338Instruction* offset = args->at(2);4339#ifndef _LP644340offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));4341#endif4342Instruction* op = append(new UnsafeGetAndSetObject(t, args->at(1), offset, args->at(3), is_add));4343compilation()->set_has_unsafe_access(true);4344kill_all();4345push(op->type(), op);4346}43474348#ifndef PRODUCT4349void GraphBuilder::print_stats() {4350vmap()->print();4351}4352#endif // PRODUCT43534354void GraphBuilder::profile_call(ciMethod* callee, Value recv, ciKlass* known_holder, Values* obj_args, bool inlined) {4355assert(known_holder == NULL || (known_holder->is_instance_klass() &&4356(!known_holder->is_interface() ||4357((ciInstanceKlass*)known_holder)->has_nonstatic_concrete_methods())), "should be non-static concrete method");4358if (known_holder != NULL) {4359if (known_holder->exact_klass() == NULL) {4360known_holder = compilation()->cha_exact_type(known_holder);4361}4362}43634364append(new ProfileCall(method(), bci(), callee, recv, known_holder, obj_args, inlined));4365}43664367void GraphBuilder::profile_return_type(Value ret, ciMethod* callee, ciMethod* m, int invoke_bci) {4368assert((m == NULL) == (invoke_bci < 0), "invalid method and invalid bci together");4369if (m == NULL) {4370m = method();4371}4372if (invoke_bci < 0) {4373invoke_bci = bci();4374}4375ciMethodData* md = m->method_data_or_null();4376ciProfileData* data = md->bci_to_data(invoke_bci);4377if (data != NULL && (data->is_CallTypeData() || data->is_VirtualCallTypeData())) {4378bool has_return = data->is_CallTypeData() ? ((ciCallTypeData*)data)->has_return() : ((ciVirtualCallTypeData*)data)->has_return();4379if (has_return) {4380append(new ProfileReturnType(m , invoke_bci, callee, ret));4381}4382}4383}43844385void GraphBuilder::profile_invocation(ciMethod* callee, ValueStack* state) {4386append(new ProfileInvoke(callee, state));4387}438843894390