Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/c1/c1_LIRGenerator.cpp
83404 views
/*1* Copyright (c) 2005, 2016, 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_Defs.hpp"26#include "c1/c1_Compilation.hpp"27#include "c1/c1_FrameMap.hpp"28#include "c1/c1_Instruction.hpp"29#include "c1/c1_LIRAssembler.hpp"30#include "c1/c1_LIRGenerator.hpp"31#include "c1/c1_ValueStack.hpp"32#include "ci/ciArrayKlass.hpp"33#include "ci/ciInstance.hpp"34#include "ci/ciObjArray.hpp"35#include "runtime/sharedRuntime.hpp"36#include "runtime/stubRoutines.hpp"37#include "utilities/bitMap.inline.hpp"38#include "utilities/macros.hpp"39#if INCLUDE_ALL_GCS40#include "gc_implementation/g1/heapRegion.hpp"41#endif // INCLUDE_ALL_GCS4243#ifdef ASSERT44#define __ gen()->lir(__FILE__, __LINE__)->45#else46#define __ gen()->lir()->47#endif4849#ifndef PATCHED_ADDR50#define PATCHED_ADDR (max_jint)51#endif5253void PhiResolverState::reset(int max_vregs) {54// Initialize array sizes55_virtual_operands.at_put_grow(max_vregs - 1, NULL, NULL);56_virtual_operands.trunc_to(0);57_other_operands.at_put_grow(max_vregs - 1, NULL, NULL);58_other_operands.trunc_to(0);59_vreg_table.at_put_grow(max_vregs - 1, NULL, NULL);60_vreg_table.trunc_to(0);61}62636465//--------------------------------------------------------------66// PhiResolver6768// Resolves cycles:69//70// r1 := r2 becomes temp := r171// r2 := r1 r1 := r272// r2 := temp73// and orders moves:74//75// r2 := r3 becomes r1 := r276// r1 := r2 r2 := r37778PhiResolver::PhiResolver(LIRGenerator* gen, int max_vregs)79: _gen(gen)80, _state(gen->resolver_state())81, _temp(LIR_OprFact::illegalOpr)82{83// reinitialize the shared state arrays84_state.reset(max_vregs);85}868788void PhiResolver::emit_move(LIR_Opr src, LIR_Opr dest) {89assert(src->is_valid(), "");90assert(dest->is_valid(), "");91__ move(src, dest);92}939495void PhiResolver::move_temp_to(LIR_Opr dest) {96assert(_temp->is_valid(), "");97emit_move(_temp, dest);98NOT_PRODUCT(_temp = LIR_OprFact::illegalOpr);99}100101102void PhiResolver::move_to_temp(LIR_Opr src) {103assert(_temp->is_illegal(), "");104_temp = _gen->new_register(src->type());105emit_move(src, _temp);106}107108109// Traverse assignment graph in depth first order and generate moves in post order110// ie. two assignments: b := c, a := b start with node c:111// Call graph: move(NULL, c) -> move(c, b) -> move(b, a)112// Generates moves in this order: move b to a and move c to b113// ie. cycle a := b, b := a start with node a114// Call graph: move(NULL, a) -> move(a, b) -> move(b, a)115// Generates moves in this order: move b to temp, move a to b, move temp to a116void PhiResolver::move(ResolveNode* src, ResolveNode* dest) {117if (!dest->visited()) {118dest->set_visited();119for (int i = dest->no_of_destinations()-1; i >= 0; i --) {120move(dest, dest->destination_at(i));121}122} else if (!dest->start_node()) {123// cylce in graph detected124assert(_loop == NULL, "only one loop valid!");125_loop = dest;126move_to_temp(src->operand());127return;128} // else dest is a start node129130if (!dest->assigned()) {131if (_loop == dest) {132move_temp_to(dest->operand());133dest->set_assigned();134} else if (src != NULL) {135emit_move(src->operand(), dest->operand());136dest->set_assigned();137}138}139}140141142PhiResolver::~PhiResolver() {143int i;144// resolve any cycles in moves from and to virtual registers145for (i = virtual_operands().length() - 1; i >= 0; i --) {146ResolveNode* node = virtual_operands()[i];147if (!node->visited()) {148_loop = NULL;149move(NULL, node);150node->set_start_node();151assert(_temp->is_illegal(), "move_temp_to() call missing");152}153}154155// generate move for move from non virtual register to abitrary destination156for (i = other_operands().length() - 1; i >= 0; i --) {157ResolveNode* node = other_operands()[i];158for (int j = node->no_of_destinations() - 1; j >= 0; j --) {159emit_move(node->operand(), node->destination_at(j)->operand());160}161}162}163164165ResolveNode* PhiResolver::create_node(LIR_Opr opr, bool source) {166ResolveNode* node;167if (opr->is_virtual()) {168int vreg_num = opr->vreg_number();169node = vreg_table().at_grow(vreg_num, NULL);170assert(node == NULL || node->operand() == opr, "");171if (node == NULL) {172node = new ResolveNode(opr);173vreg_table()[vreg_num] = node;174}175// Make sure that all virtual operands show up in the list when176// they are used as the source of a move.177if (source && !virtual_operands().contains(node)) {178virtual_operands().append(node);179}180} else {181assert(source, "");182node = new ResolveNode(opr);183other_operands().append(node);184}185return node;186}187188189void PhiResolver::move(LIR_Opr src, LIR_Opr dest) {190assert(dest->is_virtual(), "");191// tty->print("move "); src->print(); tty->print(" to "); dest->print(); tty->cr();192assert(src->is_valid(), "");193assert(dest->is_valid(), "");194ResolveNode* source = source_node(src);195source->append(destination_node(dest));196}197198199//--------------------------------------------------------------200// LIRItem201202void LIRItem::set_result(LIR_Opr opr) {203assert(value()->operand()->is_illegal() || value()->operand()->is_constant(), "operand should never change");204value()->set_operand(opr);205206if (opr->is_virtual()) {207_gen->_instruction_for_operand.at_put_grow(opr->vreg_number(), value(), NULL);208}209210_result = opr;211}212213void LIRItem::load_item() {214if (result()->is_illegal()) {215// update the items result216_result = value()->operand();217}218if (!result()->is_register()) {219LIR_Opr reg = _gen->new_register(value()->type());220__ move(result(), reg);221if (result()->is_constant()) {222_result = reg;223} else {224set_result(reg);225}226}227}228229230void LIRItem::load_for_store(BasicType type) {231if (_gen->can_store_as_constant(value(), type)) {232_result = value()->operand();233if (!_result->is_constant()) {234_result = LIR_OprFact::value_type(value()->type());235}236} else if (type == T_BYTE || type == T_BOOLEAN) {237load_byte_item();238} else {239load_item();240}241}242243void LIRItem::load_item_force(LIR_Opr reg) {244LIR_Opr r = result();245if (r != reg) {246#if !defined(ARM) && !defined(E500V2)247if (r->type() != reg->type()) {248// moves between different types need an intervening spill slot249r = _gen->force_to_spill(r, reg->type());250}251#endif252__ move(r, reg);253_result = reg;254}255}256257ciObject* LIRItem::get_jobject_constant() const {258ObjectType* oc = type()->as_ObjectType();259if (oc) {260return oc->constant_value();261}262return NULL;263}264265266jint LIRItem::get_jint_constant() const {267assert(is_constant() && value() != NULL, "");268assert(type()->as_IntConstant() != NULL, "type check");269return type()->as_IntConstant()->value();270}271272273jint LIRItem::get_address_constant() const {274assert(is_constant() && value() != NULL, "");275assert(type()->as_AddressConstant() != NULL, "type check");276return type()->as_AddressConstant()->value();277}278279280jfloat LIRItem::get_jfloat_constant() const {281assert(is_constant() && value() != NULL, "");282assert(type()->as_FloatConstant() != NULL, "type check");283return type()->as_FloatConstant()->value();284}285286287jdouble LIRItem::get_jdouble_constant() const {288assert(is_constant() && value() != NULL, "");289assert(type()->as_DoubleConstant() != NULL, "type check");290return type()->as_DoubleConstant()->value();291}292293294jlong LIRItem::get_jlong_constant() const {295assert(is_constant() && value() != NULL, "");296assert(type()->as_LongConstant() != NULL, "type check");297return type()->as_LongConstant()->value();298}299300301302//--------------------------------------------------------------303304305void LIRGenerator::init() {306_bs = Universe::heap()->barrier_set();307}308309310void LIRGenerator::block_do_prolog(BlockBegin* block) {311#ifndef PRODUCT312if (PrintIRWithLIR) {313block->print();314}315#endif316317// set up the list of LIR instructions318assert(block->lir() == NULL, "LIR list already computed for this block");319_lir = new LIR_List(compilation(), block);320block->set_lir(_lir);321322__ branch_destination(block->label());323324if (LIRTraceExecution &&325Compilation::current()->hir()->start()->block_id() != block->block_id() &&326!block->is_set(BlockBegin::exception_entry_flag)) {327assert(block->lir()->instructions_list()->length() == 1, "should come right after br_dst");328trace_block_entry(block);329}330}331332333void LIRGenerator::block_do_epilog(BlockBegin* block) {334#ifndef PRODUCT335if (PrintIRWithLIR) {336tty->cr();337}338#endif339340// LIR_Opr for unpinned constants shouldn't be referenced by other341// blocks so clear them out after processing the block.342for (int i = 0; i < _unpinned_constants.length(); i++) {343_unpinned_constants.at(i)->clear_operand();344}345_unpinned_constants.trunc_to(0);346347// clear our any registers for other local constants348_constants.trunc_to(0);349_reg_for_constants.trunc_to(0);350}351352353void LIRGenerator::block_do(BlockBegin* block) {354CHECK_BAILOUT();355356block_do_prolog(block);357set_block(block);358359for (Instruction* instr = block; instr != NULL; instr = instr->next()) {360if (instr->is_pinned()) do_root(instr);361}362363set_block(NULL);364block_do_epilog(block);365}366367368//-------------------------LIRGenerator-----------------------------369370// This is where the tree-walk starts; instr must be root;371void LIRGenerator::do_root(Value instr) {372CHECK_BAILOUT();373374InstructionMark im(compilation(), instr);375376assert(instr->is_pinned(), "use only with roots");377assert(instr->subst() == instr, "shouldn't have missed substitution");378379instr->visit(this);380381assert(!instr->has_uses() || instr->operand()->is_valid() ||382instr->as_Constant() != NULL || bailed_out(), "invalid item set");383}384385386// This is called for each node in tree; the walk stops if a root is reached387void LIRGenerator::walk(Value instr) {388InstructionMark im(compilation(), instr);389//stop walk when encounter a root390if (instr->is_pinned() && instr->as_Phi() == NULL || instr->operand()->is_valid()) {391assert(instr->operand() != LIR_OprFact::illegalOpr || instr->as_Constant() != NULL, "this root has not yet been visited");392} else {393assert(instr->subst() == instr, "shouldn't have missed substitution");394instr->visit(this);395// assert(instr->use_count() > 0 || instr->as_Phi() != NULL, "leaf instruction must have a use");396}397}398399400CodeEmitInfo* LIRGenerator::state_for(Instruction* x, ValueStack* state, bool ignore_xhandler) {401assert(state != NULL, "state must be defined");402403#ifndef PRODUCT404state->verify();405#endif406407ValueStack* s = state;408for_each_state(s) {409if (s->kind() == ValueStack::EmptyExceptionState) {410assert(s->stack_size() == 0 && s->locals_size() == 0 && (s->locks_size() == 0 || s->locks_size() == 1), "state must be empty");411continue;412}413414int index;415Value value;416for_each_stack_value(s, index, value) {417assert(value->subst() == value, "missed substitution");418if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {419walk(value);420assert(value->operand()->is_valid(), "must be evaluated now");421}422}423424int bci = s->bci();425IRScope* scope = s->scope();426ciMethod* method = scope->method();427428MethodLivenessResult liveness = method->liveness_at_bci(bci);429if (bci == SynchronizationEntryBCI) {430if (x->as_ExceptionObject() || x->as_Throw()) {431// all locals are dead on exit from the synthetic unlocker432liveness.clear();433} else {434assert(x->as_MonitorEnter() || x->as_ProfileInvoke(), "only other cases are MonitorEnter and ProfileInvoke");435}436}437if (!liveness.is_valid()) {438// Degenerate or breakpointed method.439bailout("Degenerate or breakpointed method");440} else {441assert((int)liveness.size() == s->locals_size(), "error in use of liveness");442for_each_local_value(s, index, value) {443assert(value->subst() == value, "missed substition");444if (liveness.at(index) && !value->type()->is_illegal()) {445if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {446walk(value);447assert(value->operand()->is_valid(), "must be evaluated now");448}449} else {450// NULL out this local so that linear scan can assume that all non-NULL values are live.451s->invalidate_local(index);452}453}454}455}456457return new CodeEmitInfo(state, ignore_xhandler ? NULL : x->exception_handlers(), x->check_flag(Instruction::DeoptimizeOnException));458}459460461CodeEmitInfo* LIRGenerator::state_for(Instruction* x) {462return state_for(x, x->exception_state());463}464465466void LIRGenerator::klass2reg_with_patching(LIR_Opr r, ciMetadata* obj, CodeEmitInfo* info, bool need_resolve) {467/* C2 relies on constant pool entries being resolved (ciTypeFlow), so if TieredCompilation468* is active and the class hasn't yet been resolved we need to emit a patch that resolves469* the class. */470if ((TieredCompilation && need_resolve) || !obj->is_loaded() || PatchALot) {471assert(info != NULL, "info must be set if class is not loaded");472__ klass2reg_patch(NULL, r, info);473} else {474// no patching needed475__ metadata2reg(obj->constant_encoding(), r);476}477}478479480void LIRGenerator::array_range_check(LIR_Opr array, LIR_Opr index,481CodeEmitInfo* null_check_info, CodeEmitInfo* range_check_info) {482CodeStub* stub = new RangeCheckStub(range_check_info, index);483if (index->is_constant()) {484cmp_mem_int(lir_cond_belowEqual, array, arrayOopDesc::length_offset_in_bytes(),485index->as_jint(), null_check_info);486__ branch(lir_cond_belowEqual, T_INT, stub); // forward branch487} else {488cmp_reg_mem(lir_cond_aboveEqual, index, array,489arrayOopDesc::length_offset_in_bytes(), T_INT, null_check_info);490__ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch491}492}493494495void LIRGenerator::nio_range_check(LIR_Opr buffer, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info) {496CodeStub* stub = new RangeCheckStub(info, index, true);497if (index->is_constant()) {498cmp_mem_int(lir_cond_belowEqual, buffer, java_nio_Buffer::limit_offset(), index->as_jint(), info);499__ branch(lir_cond_belowEqual, T_INT, stub); // forward branch500} else {501cmp_reg_mem(lir_cond_aboveEqual, index, buffer,502java_nio_Buffer::limit_offset(), T_INT, info);503__ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch504}505__ move(index, result);506}507508509510void LIRGenerator::arithmetic_op(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp_op, CodeEmitInfo* info) {511LIR_Opr result_op = result;512LIR_Opr left_op = left;513LIR_Opr right_op = right;514515if (TwoOperandLIRForm && left_op != result_op) {516assert(right_op != result_op, "malformed");517__ move(left_op, result_op);518left_op = result_op;519}520521switch(code) {522case Bytecodes::_dadd:523case Bytecodes::_fadd:524case Bytecodes::_ladd:525case Bytecodes::_iadd: __ add(left_op, right_op, result_op); break;526case Bytecodes::_fmul:527case Bytecodes::_lmul: __ mul(left_op, right_op, result_op); break;528529case Bytecodes::_dmul:530{531if (is_strictfp) {532__ mul_strictfp(left_op, right_op, result_op, tmp_op); break;533} else {534__ mul(left_op, right_op, result_op); break;535}536}537break;538539case Bytecodes::_imul:540{541bool did_strength_reduce = false;542543if (right->is_constant()) {544jint c = right->as_jint();545if (c > 0 && is_power_of_2(c)) {546// do not need tmp here547__ shift_left(left_op, exact_log2(c), result_op);548did_strength_reduce = true;549} else {550did_strength_reduce = strength_reduce_multiply(left_op, c, result_op, tmp_op);551}552}553// we couldn't strength reduce so just emit the multiply554if (!did_strength_reduce) {555__ mul(left_op, right_op, result_op);556}557}558break;559560case Bytecodes::_dsub:561case Bytecodes::_fsub:562case Bytecodes::_lsub:563case Bytecodes::_isub: __ sub(left_op, right_op, result_op); break;564565case Bytecodes::_fdiv: __ div (left_op, right_op, result_op); break;566// ldiv and lrem are implemented with a direct runtime call567568case Bytecodes::_ddiv:569{570if (is_strictfp) {571__ div_strictfp (left_op, right_op, result_op, tmp_op); break;572} else {573__ div (left_op, right_op, result_op); break;574}575}576break;577578case Bytecodes::_drem:579case Bytecodes::_frem: __ rem (left_op, right_op, result_op); break;580581default: ShouldNotReachHere();582}583}584585586void LIRGenerator::arithmetic_op_int(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) {587arithmetic_op(code, result, left, right, false, tmp);588}589590591void LIRGenerator::arithmetic_op_long(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {592arithmetic_op(code, result, left, right, false, LIR_OprFact::illegalOpr, info);593}594595596void LIRGenerator::arithmetic_op_fpu(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp) {597arithmetic_op(code, result, left, right, is_strictfp, tmp);598}599600601void LIRGenerator::shift_op(Bytecodes::Code code, LIR_Opr result_op, LIR_Opr value, LIR_Opr count, LIR_Opr tmp) {602if (TwoOperandLIRForm && value != result_op) {603assert(count != result_op, "malformed");604__ move(value, result_op);605value = result_op;606}607608assert(count->is_constant() || count->is_register(), "must be");609switch(code) {610case Bytecodes::_ishl:611case Bytecodes::_lshl: __ shift_left(value, count, result_op, tmp); break;612case Bytecodes::_ishr:613case Bytecodes::_lshr: __ shift_right(value, count, result_op, tmp); break;614case Bytecodes::_iushr:615case Bytecodes::_lushr: __ unsigned_shift_right(value, count, result_op, tmp); break;616default: ShouldNotReachHere();617}618}619620621void LIRGenerator::logic_op (Bytecodes::Code code, LIR_Opr result_op, LIR_Opr left_op, LIR_Opr right_op) {622if (TwoOperandLIRForm && left_op != result_op) {623assert(right_op != result_op, "malformed");624__ move(left_op, result_op);625left_op = result_op;626}627628switch(code) {629case Bytecodes::_iand:630case Bytecodes::_land: __ logical_and(left_op, right_op, result_op); break;631632case Bytecodes::_ior:633case Bytecodes::_lor: __ logical_or(left_op, right_op, result_op); break;634635case Bytecodes::_ixor:636case Bytecodes::_lxor: __ logical_xor(left_op, right_op, result_op); break;637638default: ShouldNotReachHere();639}640}641642643void LIRGenerator::monitor_enter(LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no, CodeEmitInfo* info_for_exception, CodeEmitInfo* info) {644if (!GenerateSynchronizationCode) return;645// for slow path, use debug info for state after successful locking646CodeStub* slow_path = new MonitorEnterStub(object, lock, info);647__ load_stack_address_monitor(monitor_no, lock);648// for handling NullPointerException, use debug info representing just the lock stack before this monitorenter649__ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception);650}651652653void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {654if (!GenerateSynchronizationCode) return;655// setup registers656LIR_Opr hdr = lock;657lock = new_hdr;658CodeStub* slow_path = new MonitorExitStub(lock, UseFastLocking, monitor_no);659__ load_stack_address_monitor(monitor_no, lock);660__ unlock_object(hdr, object, lock, scratch, slow_path);661}662663#ifndef PRODUCT664void LIRGenerator::print_if_not_loaded(const NewInstance* new_instance) {665if (PrintNotLoaded && !new_instance->klass()->is_loaded()) {666tty->print_cr(" ###class not loaded at new bci %d", new_instance->printable_bci());667} else if (PrintNotLoaded && (TieredCompilation && new_instance->is_unresolved())) {668tty->print_cr(" ###class not resolved at new bci %d", new_instance->printable_bci());669}670}671#endif672673void LIRGenerator::new_instance(LIR_Opr dst, ciInstanceKlass* klass, bool is_unresolved, LIR_Opr scratch1, LIR_Opr scratch2, LIR_Opr scratch3, LIR_Opr scratch4, LIR_Opr klass_reg, CodeEmitInfo* info) {674klass2reg_with_patching(klass_reg, klass, info, is_unresolved);675// If klass is not loaded we do not know if the klass has finalizers:676if (UseFastNewInstance && klass->is_loaded()677&& !Klass::layout_helper_needs_slow_path(klass->layout_helper())) {678679Runtime1::StubID stub_id = klass->is_initialized() ? Runtime1::fast_new_instance_id : Runtime1::fast_new_instance_init_check_id;680681CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id);682683assert(klass->is_loaded(), "must be loaded");684// allocate space for instance685assert(klass->size_helper() >= 0, "illegal instance size");686const int instance_size = align_object_size(klass->size_helper());687__ allocate_object(dst, scratch1, scratch2, scratch3, scratch4,688oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path);689} else {690CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, Runtime1::new_instance_id);691__ branch(lir_cond_always, T_ILLEGAL, slow_path);692__ branch_destination(slow_path->continuation());693}694}695696697static bool is_constant_zero(Instruction* inst) {698IntConstant* c = inst->type()->as_IntConstant();699if (c) {700return (c->value() == 0);701}702return false;703}704705706static bool positive_constant(Instruction* inst) {707IntConstant* c = inst->type()->as_IntConstant();708if (c) {709return (c->value() >= 0);710}711return false;712}713714715static ciArrayKlass* as_array_klass(ciType* type) {716if (type != NULL && type->is_array_klass() && type->is_loaded()) {717return (ciArrayKlass*)type;718} else {719return NULL;720}721}722723static ciType* phi_declared_type(Phi* phi) {724ciType* t = phi->operand_at(0)->declared_type();725if (t == NULL) {726return NULL;727}728for(int i = 1; i < phi->operand_count(); i++) {729if (t != phi->operand_at(i)->declared_type()) {730return NULL;731}732}733return t;734}735736void LIRGenerator::arraycopy_helper(Intrinsic* x, int* flagsp, ciArrayKlass** expected_typep) {737Instruction* src = x->argument_at(0);738Instruction* src_pos = x->argument_at(1);739Instruction* dst = x->argument_at(2);740Instruction* dst_pos = x->argument_at(3);741Instruction* length = x->argument_at(4);742743// first try to identify the likely type of the arrays involved744ciArrayKlass* expected_type = NULL;745bool is_exact = false, src_objarray = false, dst_objarray = false;746{747ciArrayKlass* src_exact_type = as_array_klass(src->exact_type());748ciArrayKlass* src_declared_type = as_array_klass(src->declared_type());749Phi* phi;750if (src_declared_type == NULL && (phi = src->as_Phi()) != NULL) {751src_declared_type = as_array_klass(phi_declared_type(phi));752}753ciArrayKlass* dst_exact_type = as_array_klass(dst->exact_type());754ciArrayKlass* dst_declared_type = as_array_klass(dst->declared_type());755if (dst_declared_type == NULL && (phi = dst->as_Phi()) != NULL) {756dst_declared_type = as_array_klass(phi_declared_type(phi));757}758759if (src_exact_type != NULL && src_exact_type == dst_exact_type) {760// the types exactly match so the type is fully known761is_exact = true;762expected_type = src_exact_type;763} else if (dst_exact_type != NULL && dst_exact_type->is_obj_array_klass()) {764ciArrayKlass* dst_type = (ciArrayKlass*) dst_exact_type;765ciArrayKlass* src_type = NULL;766if (src_exact_type != NULL && src_exact_type->is_obj_array_klass()) {767src_type = (ciArrayKlass*) src_exact_type;768} else if (src_declared_type != NULL && src_declared_type->is_obj_array_klass()) {769src_type = (ciArrayKlass*) src_declared_type;770}771if (src_type != NULL) {772if (src_type->element_type()->is_subtype_of(dst_type->element_type())) {773is_exact = true;774expected_type = dst_type;775}776}777}778// at least pass along a good guess779if (expected_type == NULL) expected_type = dst_exact_type;780if (expected_type == NULL) expected_type = src_declared_type;781if (expected_type == NULL) expected_type = dst_declared_type;782783src_objarray = (src_exact_type && src_exact_type->is_obj_array_klass()) || (src_declared_type && src_declared_type->is_obj_array_klass());784dst_objarray = (dst_exact_type && dst_exact_type->is_obj_array_klass()) || (dst_declared_type && dst_declared_type->is_obj_array_klass());785}786787// if a probable array type has been identified, figure out if any788// of the required checks for a fast case can be elided.789int flags = LIR_OpArrayCopy::all_flags;790791if (!src_objarray)792flags &= ~LIR_OpArrayCopy::src_objarray;793if (!dst_objarray)794flags &= ~LIR_OpArrayCopy::dst_objarray;795796if (!x->arg_needs_null_check(0))797flags &= ~LIR_OpArrayCopy::src_null_check;798if (!x->arg_needs_null_check(2))799flags &= ~LIR_OpArrayCopy::dst_null_check;800801802if (expected_type != NULL) {803Value length_limit = NULL;804805IfOp* ifop = length->as_IfOp();806if (ifop != NULL) {807// look for expressions like min(v, a.length) which ends up as808// x > y ? y : x or x >= y ? y : x809if ((ifop->cond() == If::gtr || ifop->cond() == If::geq) &&810ifop->x() == ifop->fval() &&811ifop->y() == ifop->tval()) {812length_limit = ifop->y();813}814}815816// try to skip null checks and range checks817NewArray* src_array = src->as_NewArray();818if (src_array != NULL) {819flags &= ~LIR_OpArrayCopy::src_null_check;820if (length_limit != NULL &&821src_array->length() == length_limit &&822is_constant_zero(src_pos)) {823flags &= ~LIR_OpArrayCopy::src_range_check;824}825}826827NewArray* dst_array = dst->as_NewArray();828if (dst_array != NULL) {829flags &= ~LIR_OpArrayCopy::dst_null_check;830if (length_limit != NULL &&831dst_array->length() == length_limit &&832is_constant_zero(dst_pos)) {833flags &= ~LIR_OpArrayCopy::dst_range_check;834}835}836837// check from incoming constant values838if (positive_constant(src_pos))839flags &= ~LIR_OpArrayCopy::src_pos_positive_check;840if (positive_constant(dst_pos))841flags &= ~LIR_OpArrayCopy::dst_pos_positive_check;842if (positive_constant(length))843flags &= ~LIR_OpArrayCopy::length_positive_check;844845// see if the range check can be elided, which might also imply846// that src or dst is non-null.847ArrayLength* al = length->as_ArrayLength();848if (al != NULL) {849if (al->array() == src) {850// it's the length of the source array851flags &= ~LIR_OpArrayCopy::length_positive_check;852flags &= ~LIR_OpArrayCopy::src_null_check;853if (is_constant_zero(src_pos))854flags &= ~LIR_OpArrayCopy::src_range_check;855}856if (al->array() == dst) {857// it's the length of the destination array858flags &= ~LIR_OpArrayCopy::length_positive_check;859flags &= ~LIR_OpArrayCopy::dst_null_check;860if (is_constant_zero(dst_pos))861flags &= ~LIR_OpArrayCopy::dst_range_check;862}863}864if (is_exact) {865flags &= ~LIR_OpArrayCopy::type_check;866}867}868869IntConstant* src_int = src_pos->type()->as_IntConstant();870IntConstant* dst_int = dst_pos->type()->as_IntConstant();871if (src_int && dst_int) {872int s_offs = src_int->value();873int d_offs = dst_int->value();874if (src_int->value() >= dst_int->value()) {875flags &= ~LIR_OpArrayCopy::overlapping;876}877if (expected_type != NULL) {878BasicType t = expected_type->element_type()->basic_type();879int element_size = type2aelembytes(t);880if (((arrayOopDesc::base_offset_in_bytes(t) + s_offs * element_size) % HeapWordSize == 0) &&881((arrayOopDesc::base_offset_in_bytes(t) + d_offs * element_size) % HeapWordSize == 0)) {882flags &= ~LIR_OpArrayCopy::unaligned;883}884}885} else if (src_pos == dst_pos || is_constant_zero(dst_pos)) {886// src and dest positions are the same, or dst is zero so assume887// nonoverlapping copy.888flags &= ~LIR_OpArrayCopy::overlapping;889}890891if (src == dst) {892// moving within a single array so no type checks are needed893if (flags & LIR_OpArrayCopy::type_check) {894flags &= ~LIR_OpArrayCopy::type_check;895}896}897*flagsp = flags;898*expected_typep = (ciArrayKlass*)expected_type;899}900901902LIR_Opr LIRGenerator::round_item(LIR_Opr opr) {903assert(opr->is_register(), "why spill if item is not register?");904905if (RoundFPResults && UseSSE < 1 && opr->is_single_fpu()) {906LIR_Opr result = new_register(T_FLOAT);907set_vreg_flag(result, must_start_in_memory);908assert(opr->is_register(), "only a register can be spilled");909assert(opr->value_type()->is_float(), "rounding only for floats available");910__ roundfp(opr, LIR_OprFact::illegalOpr, result);911return result;912}913return opr;914}915916917LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) {918assert(type2size[t] == type2size[value->type()],919err_msg_res("size mismatch: t=%s, value->type()=%s", type2name(t), type2name(value->type())));920if (!value->is_register()) {921// force into a register922LIR_Opr r = new_register(value->type());923__ move(value, r);924value = r;925}926927// create a spill location928LIR_Opr tmp = new_register(t);929set_vreg_flag(tmp, LIRGenerator::must_start_in_memory);930931// move from register to spill932__ move(value, tmp);933return tmp;934}935936void LIRGenerator::profile_branch(If* if_instr, If::Condition cond) {937if (if_instr->should_profile()) {938ciMethod* method = if_instr->profiled_method();939assert(method != NULL, "method should be set if branch is profiled");940ciMethodData* md = method->method_data_or_null();941assert(md != NULL, "Sanity");942ciProfileData* data = md->bci_to_data(if_instr->profiled_bci());943assert(data != NULL, "must have profiling data");944assert(data->is_BranchData(), "need BranchData for two-way branches");945int taken_count_offset = md->byte_offset_of_slot(data, BranchData::taken_offset());946int not_taken_count_offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());947if (if_instr->is_swapped()) {948int t = taken_count_offset;949taken_count_offset = not_taken_count_offset;950not_taken_count_offset = t;951}952953LIR_Opr md_reg = new_register(T_METADATA);954__ metadata2reg(md->constant_encoding(), md_reg);955956LIR_Opr data_offset_reg = new_pointer_register();957__ cmove(lir_cond(cond),958LIR_OprFact::intptrConst(taken_count_offset),959LIR_OprFact::intptrConst(not_taken_count_offset),960data_offset_reg, as_BasicType(if_instr->x()->type()));961962// MDO cells are intptr_t, so the data_reg width is arch-dependent.963LIR_Opr data_reg = new_pointer_register();964LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());965__ move(data_addr, data_reg);966// Use leal instead of add to avoid destroying condition codes on x86967LIR_Address* fake_incr_value = new LIR_Address(data_reg, DataLayout::counter_increment, T_INT);968__ leal(LIR_OprFact::address(fake_incr_value), data_reg);969__ move(data_reg, data_addr);970}971}972973// Phi technique:974// This is about passing live values from one basic block to the other.975// In code generated with Java it is rather rare that more than one976// value is on the stack from one basic block to the other.977// We optimize our technique for efficient passing of one value978// (of type long, int, double..) but it can be extended.979// When entering or leaving a basic block, all registers and all spill980// slots are release and empty. We use the released registers981// and spill slots to pass the live values from one block982// to the other. The topmost value, i.e., the value on TOS of expression983// stack is passed in registers. All other values are stored in spilling984// area. Every Phi has an index which designates its spill slot985// At exit of a basic block, we fill the register(s) and spill slots.986// At entry of a basic block, the block_prolog sets up the content of phi nodes987// and locks necessary registers and spilling slots.988989990// move current value to referenced phi function991void LIRGenerator::move_to_phi(PhiResolver* resolver, Value cur_val, Value sux_val) {992Phi* phi = sux_val->as_Phi();993// cur_val can be null without phi being null in conjunction with inlining994if (phi != NULL && cur_val != NULL && cur_val != phi && !phi->is_illegal()) {995LIR_Opr operand = cur_val->operand();996if (cur_val->operand()->is_illegal()) {997assert(cur_val->as_Constant() != NULL || cur_val->as_Local() != NULL,998"these can be produced lazily");999operand = operand_for_instruction(cur_val);1000}1001resolver->move(operand, operand_for_instruction(phi));1002}1003}100410051006// Moves all stack values into their PHI position1007void LIRGenerator::move_to_phi(ValueStack* cur_state) {1008BlockBegin* bb = block();1009if (bb->number_of_sux() == 1) {1010BlockBegin* sux = bb->sux_at(0);1011assert(sux->number_of_preds() > 0, "invalid CFG");10121013// a block with only one predecessor never has phi functions1014if (sux->number_of_preds() > 1) {1015int max_phis = cur_state->stack_size() + cur_state->locals_size();1016PhiResolver resolver(this, _virtual_register_number + max_phis * 2);10171018ValueStack* sux_state = sux->state();1019Value sux_value;1020int index;10211022assert(cur_state->scope() == sux_state->scope(), "not matching");1023assert(cur_state->locals_size() == sux_state->locals_size(), "not matching");1024assert(cur_state->stack_size() == sux_state->stack_size(), "not matching");10251026for_each_stack_value(sux_state, index, sux_value) {1027move_to_phi(&resolver, cur_state->stack_at(index), sux_value);1028}10291030for_each_local_value(sux_state, index, sux_value) {1031move_to_phi(&resolver, cur_state->local_at(index), sux_value);1032}10331034assert(cur_state->caller_state() == sux_state->caller_state(), "caller states must be equal");1035}1036}1037}103810391040LIR_Opr LIRGenerator::new_register(BasicType type) {1041int vreg = _virtual_register_number;1042// add a little fudge factor for the bailout, since the bailout is1043// only checked periodically. This gives a few extra registers to1044// hand out before we really run out, which helps us keep from1045// tripping over assertions.1046if (vreg + 20 >= LIR_OprDesc::vreg_max) {1047bailout("out of virtual registers");1048if (vreg + 2 >= LIR_OprDesc::vreg_max) {1049// wrap it around1050_virtual_register_number = LIR_OprDesc::vreg_base;1051}1052}1053_virtual_register_number += 1;1054return LIR_OprFact::virtual_register(vreg, type);1055}105610571058// Try to lock using register in hint1059LIR_Opr LIRGenerator::rlock(Value instr) {1060return new_register(instr->type());1061}106210631064// does an rlock and sets result1065LIR_Opr LIRGenerator::rlock_result(Value x) {1066LIR_Opr reg = rlock(x);1067set_result(x, reg);1068return reg;1069}107010711072// does an rlock and sets result1073LIR_Opr LIRGenerator::rlock_result(Value x, BasicType type) {1074LIR_Opr reg;1075switch (type) {1076case T_BYTE:1077case T_BOOLEAN:1078reg = rlock_byte(type);1079break;1080default:1081reg = rlock(x);1082break;1083}10841085set_result(x, reg);1086return reg;1087}108810891090//---------------------------------------------------------------------1091ciObject* LIRGenerator::get_jobject_constant(Value value) {1092ObjectType* oc = value->type()->as_ObjectType();1093if (oc) {1094return oc->constant_value();1095}1096return NULL;1097}109810991100void LIRGenerator::do_ExceptionObject(ExceptionObject* x) {1101assert(block()->is_set(BlockBegin::exception_entry_flag), "ExceptionObject only allowed in exception handler block");1102assert(block()->next() == x, "ExceptionObject must be first instruction of block");11031104// no moves are created for phi functions at the begin of exception1105// handlers, so assign operands manually here1106for_each_phi_fun(block(), phi,1107operand_for_instruction(phi));11081109LIR_Opr thread_reg = getThreadPointer();1110__ move_wide(new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT),1111exceptionOopOpr());1112__ move_wide(LIR_OprFact::oopConst(NULL),1113new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT));1114__ move_wide(LIR_OprFact::oopConst(NULL),1115new LIR_Address(thread_reg, in_bytes(JavaThread::exception_pc_offset()), T_OBJECT));11161117LIR_Opr result = new_register(T_OBJECT);1118__ move(exceptionOopOpr(), result);1119set_result(x, result);1120}112111221123//----------------------------------------------------------------------1124//----------------------------------------------------------------------1125//----------------------------------------------------------------------1126//----------------------------------------------------------------------1127// visitor functions1128//----------------------------------------------------------------------1129//----------------------------------------------------------------------1130//----------------------------------------------------------------------1131//----------------------------------------------------------------------11321133void LIRGenerator::do_Phi(Phi* x) {1134// phi functions are never visited directly1135ShouldNotReachHere();1136}113711381139// Code for a constant is generated lazily unless the constant is frequently used and can't be inlined.1140void LIRGenerator::do_Constant(Constant* x) {1141if (x->state_before() != NULL) {1142// Any constant with a ValueStack requires patching so emit the patch here1143LIR_Opr reg = rlock_result(x);1144CodeEmitInfo* info = state_for(x, x->state_before());1145__ oop2reg_patch(NULL, reg, info);1146} else if (x->use_count() > 1 && !can_inline_as_constant(x)) {1147if (!x->is_pinned()) {1148// unpinned constants are handled specially so that they can be1149// put into registers when they are used multiple times within a1150// block. After the block completes their operand will be1151// cleared so that other blocks can't refer to that register.1152set_result(x, load_constant(x));1153} else {1154LIR_Opr res = x->operand();1155if (!res->is_valid()) {1156res = LIR_OprFact::value_type(x->type());1157}1158if (res->is_constant()) {1159LIR_Opr reg = rlock_result(x);1160__ move(res, reg);1161} else {1162set_result(x, res);1163}1164}1165} else {1166set_result(x, LIR_OprFact::value_type(x->type()));1167}1168}116911701171void LIRGenerator::do_Local(Local* x) {1172// operand_for_instruction has the side effect of setting the result1173// so there's no need to do it here.1174operand_for_instruction(x);1175}117611771178void LIRGenerator::do_IfInstanceOf(IfInstanceOf* x) {1179Unimplemented();1180}118111821183void LIRGenerator::do_Return(Return* x) {1184if (compilation()->env()->dtrace_method_probes()) {1185BasicTypeList signature;1186signature.append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread1187signature.append(T_METADATA); // Method*1188LIR_OprList* args = new LIR_OprList();1189args->append(getThreadPointer());1190LIR_Opr meth = new_register(T_METADATA);1191__ metadata2reg(method()->constant_encoding(), meth);1192args->append(meth);1193call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), voidType, NULL);1194}11951196if (x->type()->is_void()) {1197__ return_op(LIR_OprFact::illegalOpr);1198} else {1199#ifdef AARCH321200LIR_Opr reg = java_result_register_for(x->type(), /*callee=*/true);1201#else1202LIR_Opr reg = result_register_for(x->type(), /*callee=*/true);1203#endif1204LIRItem result(x->result(), this);12051206result.load_item_force(reg);1207__ return_op(result.result());1208}1209set_no_result(x);1210}12111212// Examble: ref.get()1213// Combination of LoadField and g1 pre-write barrier1214void LIRGenerator::do_Reference_get(Intrinsic* x) {12151216const int referent_offset = java_lang_ref_Reference::referent_offset;1217guarantee(referent_offset > 0, "referent offset not initialized");12181219assert(x->number_of_arguments() == 1, "wrong type");12201221LIRItem reference(x->argument_at(0), this);1222reference.load_item();12231224// need to perform the null check on the reference objecy1225CodeEmitInfo* info = NULL;1226if (x->needs_null_check()) {1227info = state_for(x);1228}12291230LIR_Address* referent_field_adr =1231new LIR_Address(reference.result(), referent_offset, T_OBJECT);12321233LIR_Opr result = rlock_result(x);12341235__ load(referent_field_adr, result, info);12361237// Register the value in the referent field with the pre-barrier1238pre_barrier(LIR_OprFact::illegalOpr /* addr_opr */,1239result /* pre_val */,1240false /* do_load */,1241false /* patch */,1242NULL /* info */);1243}12441245// Example: clazz.isInstance(object)1246void LIRGenerator::do_isInstance(Intrinsic* x) {1247assert(x->number_of_arguments() == 2, "wrong type");12481249// TODO could try to substitute this node with an equivalent InstanceOf1250// if clazz is known to be a constant Class. This will pick up newly found1251// constants after HIR construction. I'll leave this to a future change.12521253// as a first cut, make a simple leaf call to runtime to stay platform independent.1254// could follow the aastore example in a future change.12551256LIRItem clazz(x->argument_at(0), this);1257LIRItem object(x->argument_at(1), this);1258clazz.load_item();1259object.load_item();1260LIR_Opr result = rlock_result(x);12611262// need to perform null check on clazz1263if (x->needs_null_check()) {1264CodeEmitInfo* info = state_for(x);1265__ null_check(clazz.result(), info);1266}12671268LIR_Opr call_result = call_runtime(clazz.value(), object.value(),1269CAST_FROM_FN_PTR(address, Runtime1::is_instance_of),1270x->type(),1271NULL); // NULL CodeEmitInfo results in a leaf call1272__ move(call_result, result);1273}12741275// Example: object.getClass ()1276void LIRGenerator::do_getClass(Intrinsic* x) {1277assert(x->number_of_arguments() == 1, "wrong type");12781279LIRItem rcvr(x->argument_at(0), this);1280rcvr.load_item();1281LIR_Opr temp = new_register(T_METADATA);1282LIR_Opr result = rlock_result(x);12831284// need to perform the null check on the rcvr1285CodeEmitInfo* info = NULL;1286if (x->needs_null_check()) {1287info = state_for(x);1288}12891290// FIXME T_ADDRESS should actually be T_METADATA but it can't because the1291// meaning of these two is mixed up (see JDK-8026837).1292__ move(new LIR_Address(rcvr.result(), oopDesc::klass_offset_in_bytes(), T_ADDRESS), temp, info);1293__ move_wide(new LIR_Address(temp, in_bytes(Klass::java_mirror_offset()), T_OBJECT), result);1294}129512961297// Example: Thread.currentThread()1298void LIRGenerator::do_currentThread(Intrinsic* x) {1299assert(x->number_of_arguments() == 0, "wrong type");1300LIR_Opr reg = rlock_result(x);1301__ move_wide(new LIR_Address(getThreadPointer(), in_bytes(JavaThread::threadObj_offset()), T_OBJECT), reg);1302}130313041305void LIRGenerator::do_RegisterFinalizer(Intrinsic* x) {1306assert(x->number_of_arguments() == 1, "wrong type");1307LIRItem receiver(x->argument_at(0), this);13081309receiver.load_item();1310BasicTypeList signature;1311signature.append(T_OBJECT); // receiver1312LIR_OprList* args = new LIR_OprList();1313args->append(receiver.result());1314CodeEmitInfo* info = state_for(x, x->state());1315call_runtime(&signature, args,1316CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::register_finalizer_id)),1317voidType, info);13181319set_no_result(x);1320}132113221323//------------------------local access--------------------------------------13241325LIR_Opr LIRGenerator::operand_for_instruction(Instruction* x) {1326if (x->operand()->is_illegal()) {1327Constant* c = x->as_Constant();1328if (c != NULL) {1329x->set_operand(LIR_OprFact::value_type(c->type()));1330} else {1331assert(x->as_Phi() || x->as_Local() != NULL, "only for Phi and Local");1332// allocate a virtual register for this local or phi1333x->set_operand(rlock(x));1334_instruction_for_operand.at_put_grow(x->operand()->vreg_number(), x, NULL);1335}1336}1337return x->operand();1338}133913401341Instruction* LIRGenerator::instruction_for_opr(LIR_Opr opr) {1342if (opr->is_virtual()) {1343return instruction_for_vreg(opr->vreg_number());1344}1345return NULL;1346}134713481349Instruction* LIRGenerator::instruction_for_vreg(int reg_num) {1350if (reg_num < _instruction_for_operand.length()) {1351return _instruction_for_operand.at(reg_num);1352}1353return NULL;1354}135513561357void LIRGenerator::set_vreg_flag(int vreg_num, VregFlag f) {1358if (_vreg_flags.size_in_bits() == 0) {1359BitMap2D temp(100, num_vreg_flags);1360temp.clear();1361_vreg_flags = temp;1362}1363_vreg_flags.at_put_grow(vreg_num, f, true);1364}13651366bool LIRGenerator::is_vreg_flag_set(int vreg_num, VregFlag f) {1367if (!_vreg_flags.is_valid_index(vreg_num, f)) {1368return false;1369}1370return _vreg_flags.at(vreg_num, f);1371}137213731374// Block local constant handling. This code is useful for keeping1375// unpinned constants and constants which aren't exposed in the IR in1376// registers. Unpinned Constant instructions have their operands1377// cleared when the block is finished so that other blocks can't end1378// up referring to their registers.13791380LIR_Opr LIRGenerator::load_constant(Constant* x) {1381assert(!x->is_pinned(), "only for unpinned constants");1382_unpinned_constants.append(x);1383return load_constant(LIR_OprFact::value_type(x->type())->as_constant_ptr());1384}138513861387LIR_Opr LIRGenerator::load_constant(LIR_Const* c) {1388BasicType t = c->type();1389for (int i = 0; i < _constants.length(); i++) {1390LIR_Const* other = _constants.at(i);1391if (t == other->type()) {1392switch (t) {1393case T_INT:1394case T_FLOAT:1395if (c->as_jint_bits() != other->as_jint_bits()) continue;1396break;1397case T_LONG:1398case T_DOUBLE:1399if (c->as_jint_hi_bits() != other->as_jint_hi_bits()) continue;1400if (c->as_jint_lo_bits() != other->as_jint_lo_bits()) continue;1401break;1402case T_OBJECT:1403if (c->as_jobject() != other->as_jobject()) continue;1404break;1405}1406return _reg_for_constants.at(i);1407}1408}14091410LIR_Opr result = new_register(t);1411__ move((LIR_Opr)c, result);1412_constants.append(c);1413_reg_for_constants.append(result);1414return result;1415}14161417// Various barriers14181419void LIRGenerator::pre_barrier(LIR_Opr addr_opr, LIR_Opr pre_val,1420bool do_load, bool patch, CodeEmitInfo* info) {1421// Do the pre-write barrier, if any.1422switch (_bs->kind()) {1423#if INCLUDE_ALL_GCS1424case BarrierSet::G1SATBCT:1425case BarrierSet::G1SATBCTLogging:1426G1SATBCardTableModRef_pre_barrier(addr_opr, pre_val, do_load, patch, info);1427break;1428#endif // INCLUDE_ALL_GCS1429case BarrierSet::CardTableModRef:1430case BarrierSet::CardTableExtension:1431// No pre barriers1432break;1433case BarrierSet::ModRef:1434case BarrierSet::Other:1435// No pre barriers1436break;1437default :1438ShouldNotReachHere();14391440}1441}14421443void LIRGenerator::post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {1444switch (_bs->kind()) {1445#if INCLUDE_ALL_GCS1446case BarrierSet::G1SATBCT:1447case BarrierSet::G1SATBCTLogging:1448G1SATBCardTableModRef_post_barrier(addr, new_val);1449break;1450#endif // INCLUDE_ALL_GCS1451case BarrierSet::CardTableModRef:1452case BarrierSet::CardTableExtension:1453CardTableModRef_post_barrier(addr, new_val);1454break;1455case BarrierSet::ModRef:1456case BarrierSet::Other:1457// No post barriers1458break;1459default :1460ShouldNotReachHere();1461}1462}14631464////////////////////////////////////////////////////////////////////////1465#if INCLUDE_ALL_GCS14661467void LIRGenerator::G1SATBCardTableModRef_pre_barrier(LIR_Opr addr_opr, LIR_Opr pre_val,1468bool do_load, bool patch, CodeEmitInfo* info) {1469// First we test whether marking is in progress.1470BasicType flag_type;1471if (in_bytes(PtrQueue::byte_width_of_active()) == 4) {1472flag_type = T_INT;1473} else {1474guarantee(in_bytes(PtrQueue::byte_width_of_active()) == 1,1475"Assumption");1476flag_type = T_BYTE;1477}1478LIR_Opr thrd = getThreadPointer();1479LIR_Address* mark_active_flag_addr =1480new LIR_Address(thrd,1481in_bytes(JavaThread::satb_mark_queue_offset() +1482PtrQueue::byte_offset_of_active()),1483flag_type);1484// Read the marking-in-progress flag.1485LIR_Opr flag_val = new_register(T_INT);1486__ load(mark_active_flag_addr, flag_val);1487__ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));14881489LIR_PatchCode pre_val_patch_code = lir_patch_none;14901491CodeStub* slow;14921493if (do_load) {1494assert(pre_val == LIR_OprFact::illegalOpr, "sanity");1495assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");14961497if (patch)1498pre_val_patch_code = lir_patch_normal;14991500pre_val = new_register(T_OBJECT);15011502if (!addr_opr->is_address()) {1503assert(addr_opr->is_register(), "must be");1504addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));1505}1506slow = new G1PreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info);1507} else {1508assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");1509assert(pre_val->is_register(), "must be");1510assert(pre_val->type() == T_OBJECT, "must be an object");1511assert(info == NULL, "sanity");15121513slow = new G1PreBarrierStub(pre_val);1514}15151516__ branch(lir_cond_notEqual, T_INT, slow);1517__ branch_destination(slow->continuation());1518}15191520void LIRGenerator::G1SATBCardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {1521// If the "new_val" is a constant NULL, no barrier is necessary.1522if (new_val->is_constant() &&1523new_val->as_constant_ptr()->as_jobject() == NULL) return;15241525if (!new_val->is_register()) {1526LIR_Opr new_val_reg = new_register(T_OBJECT);1527if (new_val->is_constant()) {1528__ move(new_val, new_val_reg);1529} else {1530__ leal(new_val, new_val_reg);1531}1532new_val = new_val_reg;1533}1534assert(new_val->is_register(), "must be a register at this point");15351536if (addr->is_address()) {1537LIR_Address* address = addr->as_address_ptr();1538LIR_Opr ptr = new_pointer_register();1539if (!address->index()->is_valid() && address->disp() == 0) {1540__ move(address->base(), ptr);1541} else {1542assert(address->disp() != max_jint, "lea doesn't support patched addresses!");1543__ leal(addr, ptr);1544}1545addr = ptr;1546}1547assert(addr->is_register(), "must be a register at this point");15481549LIR_Opr xor_res = new_pointer_register();1550LIR_Opr xor_shift_res = new_pointer_register();1551if (TwoOperandLIRForm ) {1552__ move(addr, xor_res);1553__ logical_xor(xor_res, new_val, xor_res);1554__ move(xor_res, xor_shift_res);1555__ unsigned_shift_right(xor_shift_res,1556LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),1557xor_shift_res,1558LIR_OprDesc::illegalOpr());1559} else {1560__ logical_xor(addr, new_val, xor_res);1561__ unsigned_shift_right(xor_res,1562LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),1563xor_shift_res,1564LIR_OprDesc::illegalOpr());1565}15661567if (!new_val->is_register()) {1568LIR_Opr new_val_reg = new_register(T_OBJECT);1569__ leal(new_val, new_val_reg);1570new_val = new_val_reg;1571}1572assert(new_val->is_register(), "must be a register at this point");15731574__ cmp(lir_cond_notEqual, xor_shift_res, LIR_OprFact::intptrConst(NULL_WORD));15751576CodeStub* slow = new G1PostBarrierStub(addr, new_val);1577__ branch(lir_cond_notEqual, LP64_ONLY(T_LONG) NOT_LP64(T_INT), slow);1578__ branch_destination(slow->continuation());1579}15801581#endif // INCLUDE_ALL_GCS1582////////////////////////////////////////////////////////////////////////15831584void LIRGenerator::CardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {15851586assert(sizeof(*((CardTableModRefBS*)_bs)->byte_map_base) == sizeof(jbyte), "adjust this code");1587LIR_Const* card_table_base = new LIR_Const(((CardTableModRefBS*)_bs)->byte_map_base);1588if (addr->is_address()) {1589LIR_Address* address = addr->as_address_ptr();1590// ptr cannot be an object because we use this barrier for array card marks1591// and addr can point in the middle of an array.1592LIR_Opr ptr = new_pointer_register();1593if (!address->index()->is_valid() && address->disp() == 0) {1594__ move(address->base(), ptr);1595} else {1596assert(address->disp() != max_jint, "lea doesn't support patched addresses!");1597__ leal(addr, ptr);1598}1599addr = ptr;1600}1601assert(addr->is_register(), "must be a register at this point");16021603#ifdef CARDTABLEMODREF_POST_BARRIER_HELPER1604CardTableModRef_post_barrier_helper(addr, card_table_base);1605#else1606LIR_Opr tmp = new_pointer_register();1607if (TwoOperandLIRForm) {1608__ move(addr, tmp);1609__ unsigned_shift_right(tmp, CardTableModRefBS::card_shift, tmp);1610} else {1611__ unsigned_shift_right(addr, CardTableModRefBS::card_shift, tmp);1612}16131614if (UseConcMarkSweepGC && CMSPrecleaningEnabled) {1615__ membar_storestore();1616}16171618if (can_inline_as_constant(card_table_base)) {1619__ move(LIR_OprFact::intConst(0),1620new LIR_Address(tmp, card_table_base->as_jint(), T_BYTE));1621} else {1622__ move(LIR_OprFact::intConst(0),1623new LIR_Address(tmp, load_constant(card_table_base),1624T_BYTE));1625}1626#endif1627}162816291630//------------------------field access--------------------------------------16311632// Comment copied form templateTable_i486.cpp1633// ----------------------------------------------------------------------------1634// Volatile variables demand their effects be made known to all CPU's in1635// order. Store buffers on most chips allow reads & writes to reorder; the1636// JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of1637// memory barrier (i.e., it's not sufficient that the interpreter does not1638// reorder volatile references, the hardware also must not reorder them).1639//1640// According to the new Java Memory Model (JMM):1641// (1) All volatiles are serialized wrt to each other.1642// ALSO reads & writes act as aquire & release, so:1643// (2) A read cannot let unrelated NON-volatile memory refs that happen after1644// the read float up to before the read. It's OK for non-volatile memory refs1645// that happen before the volatile read to float down below it.1646// (3) Similar a volatile write cannot let unrelated NON-volatile memory refs1647// that happen BEFORE the write float down to after the write. It's OK for1648// non-volatile memory refs that happen after the volatile write to float up1649// before it.1650//1651// We only put in barriers around volatile refs (they are expensive), not1652// _between_ memory refs (that would require us to track the flavor of the1653// previous memory refs). Requirements (2) and (3) require some barriers1654// before volatile stores and after volatile loads. These nearly cover1655// requirement (1) but miss the volatile-store-volatile-load case. This final1656// case is placed after volatile-stores although it could just as well go1657// before volatile-loads.165816591660void LIRGenerator::do_StoreField(StoreField* x) {1661bool needs_patching = x->needs_patching();1662bool is_volatile = x->field()->is_volatile();1663BasicType field_type = x->field_type();1664bool is_oop = (field_type == T_ARRAY || field_type == T_OBJECT);16651666CodeEmitInfo* info = NULL;1667if (needs_patching) {1668assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");1669info = state_for(x, x->state_before());1670} else if (x->needs_null_check()) {1671NullCheck* nc = x->explicit_null_check();1672if (nc == NULL) {1673info = state_for(x);1674} else {1675info = state_for(nc);1676}1677}167816791680LIRItem object(x->obj(), this);1681LIRItem value(x->value(), this);16821683object.load_item();16841685if (is_volatile || needs_patching) {1686// load item if field is volatile (fewer special cases for volatiles)1687// load item if field not initialized1688// load item if field not constant1689// because of code patching we cannot inline constants1690if (field_type == T_BYTE || field_type == T_BOOLEAN) {1691value.load_byte_item();1692} else {1693value.load_item();1694}1695} else {1696value.load_for_store(field_type);1697}16981699set_no_result(x);17001701#ifndef PRODUCT1702if (PrintNotLoaded && needs_patching) {1703tty->print_cr(" ###class not loaded at store_%s bci %d",1704x->is_static() ? "static" : "field", x->printable_bci());1705}1706#endif17071708if (x->needs_null_check() &&1709(needs_patching ||1710MacroAssembler::needs_explicit_null_check(x->offset()))) {1711// Emit an explicit null check because the offset is too large.1712// If the class is not loaded and the object is NULL, we need to deoptimize to throw a1713// NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.1714__ null_check(object.result(), new CodeEmitInfo(info), /* deoptimize */ needs_patching);1715}17161717LIR_Address* address;1718if (needs_patching) {1719// we need to patch the offset in the instruction so don't allow1720// generate_address to try to be smart about emitting the -1.1721// Otherwise the patching code won't know how to find the1722// instruction to patch.1723address = new LIR_Address(object.result(), PATCHED_ADDR, field_type);1724} else {1725address = generate_address(object.result(), x->offset(), field_type);1726}17271728if (is_volatile && os::is_MP()) {1729__ membar_release();1730}17311732if (is_oop) {1733// Do the pre-write barrier, if any.1734pre_barrier(LIR_OprFact::address(address),1735LIR_OprFact::illegalOpr /* pre_val */,1736true /* do_load*/,1737needs_patching,1738(info ? new CodeEmitInfo(info) : NULL));1739}17401741if (is_volatile && !needs_patching) {1742volatile_field_store(value.result(), address, info);1743} else {1744LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;1745__ store(value.result(), address, info, patch_code);1746}17471748if (is_oop) {1749// Store to object so mark the card of the header1750post_barrier(object.result(), value.result());1751}17521753if (is_volatile && os::is_MP()) {1754__ membar();1755}1756}175717581759void LIRGenerator::do_LoadField(LoadField* x) {1760bool needs_patching = x->needs_patching();1761bool is_volatile = x->field()->is_volatile();1762BasicType field_type = x->field_type();17631764CodeEmitInfo* info = NULL;1765if (needs_patching) {1766assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");1767info = state_for(x, x->state_before());1768} else if (x->needs_null_check()) {1769NullCheck* nc = x->explicit_null_check();1770if (nc == NULL) {1771info = state_for(x);1772} else {1773info = state_for(nc);1774}1775}17761777LIRItem object(x->obj(), this);17781779object.load_item();17801781#ifndef PRODUCT1782if (PrintNotLoaded && needs_patching) {1783tty->print_cr(" ###class not loaded at load_%s bci %d",1784x->is_static() ? "static" : "field", x->printable_bci());1785}1786#endif17871788bool stress_deopt = StressLoopInvariantCodeMotion && info && info->deoptimize_on_exception();1789if (x->needs_null_check() &&1790(needs_patching ||1791MacroAssembler::needs_explicit_null_check(x->offset()) ||1792stress_deopt)) {1793LIR_Opr obj = object.result();1794if (stress_deopt) {1795obj = new_register(T_OBJECT);1796__ move(LIR_OprFact::oopConst(NULL), obj);1797}1798// Emit an explicit null check because the offset is too large.1799// If the class is not loaded and the object is NULL, we need to deoptimize to throw a1800// NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.1801__ null_check(obj, new CodeEmitInfo(info), /* deoptimize */ needs_patching);1802}18031804LIR_Opr reg = rlock_result(x, field_type);1805LIR_Address* address;1806if (needs_patching) {1807// we need to patch the offset in the instruction so don't allow1808// generate_address to try to be smart about emitting the -1.1809// Otherwise the patching code won't know how to find the1810// instruction to patch.1811address = new LIR_Address(object.result(), PATCHED_ADDR, field_type);1812} else {1813address = generate_address(object.result(), x->offset(), field_type);1814}18151816if (is_volatile && !needs_patching) {1817volatile_field_load(address, reg, info);1818} else {1819LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;1820__ load(address, reg, info, patch_code);1821}18221823if (is_volatile && os::is_MP()) {1824__ membar_acquire();1825}1826}182718281829//------------------------java.nio.Buffer.checkIndex------------------------18301831// int java.nio.Buffer.checkIndex(int)1832void LIRGenerator::do_NIOCheckIndex(Intrinsic* x) {1833// NOTE: by the time we are in checkIndex() we are guaranteed that1834// the buffer is non-null (because checkIndex is package-private and1835// only called from within other methods in the buffer).1836assert(x->number_of_arguments() == 2, "wrong type");1837LIRItem buf (x->argument_at(0), this);1838LIRItem index(x->argument_at(1), this);1839buf.load_item();1840index.load_item();18411842LIR_Opr result = rlock_result(x);1843if (GenerateRangeChecks) {1844CodeEmitInfo* info = state_for(x);1845CodeStub* stub = new RangeCheckStub(info, index.result(), true);1846if (index.result()->is_constant()) {1847cmp_mem_int(lir_cond_belowEqual, buf.result(), java_nio_Buffer::limit_offset(), index.result()->as_jint(), info);1848__ branch(lir_cond_belowEqual, T_INT, stub);1849} else {1850cmp_reg_mem(lir_cond_aboveEqual, index.result(), buf.result(),1851java_nio_Buffer::limit_offset(), T_INT, info);1852__ branch(lir_cond_aboveEqual, T_INT, stub);1853}1854__ move(index.result(), result);1855} else {1856// Just load the index into the result register1857__ move(index.result(), result);1858}1859}186018611862//------------------------array access--------------------------------------186318641865void LIRGenerator::do_ArrayLength(ArrayLength* x) {1866LIRItem array(x->array(), this);1867array.load_item();1868LIR_Opr reg = rlock_result(x);18691870CodeEmitInfo* info = NULL;1871if (x->needs_null_check()) {1872NullCheck* nc = x->explicit_null_check();1873if (nc == NULL) {1874info = state_for(x);1875} else {1876info = state_for(nc);1877}1878if (StressLoopInvariantCodeMotion && info->deoptimize_on_exception()) {1879LIR_Opr obj = new_register(T_OBJECT);1880__ move(LIR_OprFact::oopConst(NULL), obj);1881__ null_check(obj, new CodeEmitInfo(info));1882}1883}1884__ load(new LIR_Address(array.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), reg, info, lir_patch_none);1885}188618871888void LIRGenerator::do_LoadIndexed(LoadIndexed* x) {1889bool use_length = x->length() != NULL;1890LIRItem array(x->array(), this);1891LIRItem index(x->index(), this);1892LIRItem length(this);1893bool needs_range_check = x->compute_needs_range_check();18941895if (use_length && needs_range_check) {1896length.set_instruction(x->length());1897length.load_item();1898}18991900array.load_item();1901if (index.is_constant() && can_inline_as_constant(x->index())) {1902// let it be a constant1903index.dont_load_item();1904} else {1905index.load_item();1906}19071908CodeEmitInfo* range_check_info = state_for(x);1909CodeEmitInfo* null_check_info = NULL;1910if (x->needs_null_check()) {1911NullCheck* nc = x->explicit_null_check();1912if (nc != NULL) {1913null_check_info = state_for(nc);1914} else {1915null_check_info = range_check_info;1916}1917if (StressLoopInvariantCodeMotion && null_check_info->deoptimize_on_exception()) {1918LIR_Opr obj = new_register(T_OBJECT);1919__ move(LIR_OprFact::oopConst(NULL), obj);1920__ null_check(obj, new CodeEmitInfo(null_check_info));1921}1922}19231924// emit array address setup early so it schedules better1925LIR_Address* array_addr = emit_array_address(array.result(), index.result(), x->elt_type(), false);19261927if (GenerateRangeChecks && needs_range_check) {1928if (StressLoopInvariantCodeMotion && range_check_info->deoptimize_on_exception()) {1929__ branch(lir_cond_always, T_ILLEGAL, new RangeCheckStub(range_check_info, index.result()));1930} else if (use_length) {1931// TODO: use a (modified) version of array_range_check that does not require a1932// constant length to be loaded to a register1933__ cmp(lir_cond_belowEqual, length.result(), index.result());1934__ branch(lir_cond_belowEqual, T_INT, new RangeCheckStub(range_check_info, index.result()));1935} else {1936array_range_check(array.result(), index.result(), null_check_info, range_check_info);1937// The range check performs the null check, so clear it out for the load1938null_check_info = NULL;1939}1940}19411942__ move(array_addr, rlock_result(x, x->elt_type()), null_check_info);1943}194419451946void LIRGenerator::do_NullCheck(NullCheck* x) {1947if (x->can_trap()) {1948LIRItem value(x->obj(), this);1949value.load_item();1950CodeEmitInfo* info = state_for(x);1951__ null_check(value.result(), info);1952}1953}195419551956void LIRGenerator::do_TypeCast(TypeCast* x) {1957LIRItem value(x->obj(), this);1958value.load_item();1959// the result is the same as from the node we are casting1960set_result(x, value.result());1961}196219631964void LIRGenerator::do_Throw(Throw* x) {1965LIRItem exception(x->exception(), this);1966exception.load_item();1967set_no_result(x);1968LIR_Opr exception_opr = exception.result();1969CodeEmitInfo* info = state_for(x, x->state());19701971#ifndef PRODUCT1972if (PrintC1Statistics) {1973increment_counter(Runtime1::throw_count_address(), T_INT);1974}1975#endif19761977// check if the instruction has an xhandler in any of the nested scopes1978bool unwind = false;1979if (info->exception_handlers()->length() == 0) {1980// this throw is not inside an xhandler1981unwind = true;1982} else {1983// get some idea of the throw type1984bool type_is_exact = true;1985ciType* throw_type = x->exception()->exact_type();1986if (throw_type == NULL) {1987type_is_exact = false;1988throw_type = x->exception()->declared_type();1989}1990if (throw_type != NULL && throw_type->is_instance_klass()) {1991ciInstanceKlass* throw_klass = (ciInstanceKlass*)throw_type;1992unwind = !x->exception_handlers()->could_catch(throw_klass, type_is_exact);1993}1994}19951996// do null check before moving exception oop into fixed register1997// to avoid a fixed interval with an oop during the null check.1998// Use a copy of the CodeEmitInfo because debug information is1999// different for null_check and throw.2000if (GenerateCompilerNullChecks &&2001(x->exception()->as_NewInstance() == NULL && x->exception()->as_ExceptionObject() == NULL)) {2002// if the exception object wasn't created using new then it might be null.2003__ null_check(exception_opr, new CodeEmitInfo(info, x->state()->copy(ValueStack::ExceptionState, x->state()->bci())));2004}20052006if (compilation()->env()->jvmti_can_post_on_exceptions()) {2007// we need to go through the exception lookup path to get JVMTI2008// notification done2009unwind = false;2010}20112012// move exception oop into fixed register2013__ move(exception_opr, exceptionOopOpr());20142015if (unwind) {2016__ unwind_exception(exceptionOopOpr());2017} else {2018__ throw_exception(exceptionPcOpr(), exceptionOopOpr(), info);2019}2020}202120222023void LIRGenerator::do_RoundFP(RoundFP* x) {2024LIRItem input(x->input(), this);2025input.load_item();2026LIR_Opr input_opr = input.result();2027assert(input_opr->is_register(), "why round if value is not in a register?");2028assert(input_opr->is_single_fpu() || input_opr->is_double_fpu(), "input should be floating-point value");2029if (input_opr->is_single_fpu()) {2030set_result(x, round_item(input_opr)); // This code path not currently taken2031} else {2032LIR_Opr result = new_register(T_DOUBLE);2033set_vreg_flag(result, must_start_in_memory);2034__ roundfp(input_opr, LIR_OprFact::illegalOpr, result);2035set_result(x, result);2036}2037}20382039// Here UnsafeGetRaw may have x->base() and x->index() be int or long2040// on both 64 and 32 bits. Expecting x->base() to be always long on 64bit.2041void LIRGenerator::do_UnsafeGetRaw(UnsafeGetRaw* x) {2042LIRItem base(x->base(), this);2043LIRItem idx(this);20442045base.load_item();2046if (x->has_index()) {2047idx.set_instruction(x->index());2048idx.load_nonconstant();2049}20502051LIR_Opr reg = rlock_result(x, x->basic_type());20522053int log2_scale = 0;2054if (x->has_index()) {2055log2_scale = x->log2_scale();2056}20572058assert(!x->has_index() || idx.value() == x->index(), "should match");20592060LIR_Opr base_op = base.result();2061LIR_Opr index_op = idx.result();2062#ifndef _LP642063if (base_op->type() == T_LONG) {2064base_op = new_register(T_INT);2065__ convert(Bytecodes::_l2i, base.result(), base_op);2066}2067if (x->has_index()) {2068if (index_op->type() == T_LONG) {2069LIR_Opr long_index_op = index_op;2070if (index_op->is_constant()) {2071long_index_op = new_register(T_LONG);2072__ move(index_op, long_index_op);2073}2074index_op = new_register(T_INT);2075__ convert(Bytecodes::_l2i, long_index_op, index_op);2076} else {2077assert(x->index()->type()->tag() == intTag, "must be");2078}2079}2080// At this point base and index should be all ints.2081assert(base_op->type() == T_INT && !base_op->is_constant(), "base should be an non-constant int");2082assert(!x->has_index() || index_op->type() == T_INT, "index should be an int");2083#else2084if (x->has_index()) {2085if (index_op->type() == T_INT) {2086if (!index_op->is_constant()) {2087index_op = new_register(T_LONG);2088__ convert(Bytecodes::_i2l, idx.result(), index_op);2089}2090} else {2091assert(index_op->type() == T_LONG, "must be");2092if (index_op->is_constant()) {2093index_op = new_register(T_LONG);2094__ move(idx.result(), index_op);2095}2096}2097}2098// At this point base is a long non-constant2099// Index is a long register or a int constant.2100// We allow the constant to stay an int because that would allow us a more compact encoding by2101// embedding an immediate offset in the address expression. If we have a long constant, we have to2102// move it into a register first.2103assert(base_op->type() == T_LONG && !base_op->is_constant(), "base must be a long non-constant");2104assert(!x->has_index() || (index_op->type() == T_INT && index_op->is_constant()) ||2105(index_op->type() == T_LONG && !index_op->is_constant()), "unexpected index type");2106#endif21072108BasicType dst_type = x->basic_type();21092110LIR_Address* addr;2111if (index_op->is_constant()) {2112assert(log2_scale == 0, "must not have a scale");2113assert(index_op->type() == T_INT, "only int constants supported");2114addr = new LIR_Address(base_op, index_op->as_jint(), dst_type);2115} else {2116#if defined(X86) || defined(AARCH64)2117addr = new LIR_Address(base_op, index_op, LIR_Address::Scale(log2_scale), 0, dst_type);2118#elif defined(GENERATE_ADDRESS_IS_PREFERRED)2119addr = generate_address(base_op, index_op, log2_scale, 0, dst_type);2120#else2121if (index_op->is_illegal() || log2_scale == 0) {2122addr = new LIR_Address(base_op, index_op, dst_type);2123} else {2124LIR_Opr tmp = new_pointer_register();2125__ shift_left(index_op, log2_scale, tmp);2126addr = new LIR_Address(base_op, tmp, dst_type);2127}2128#endif2129}21302131if (x->may_be_unaligned() && (dst_type == T_LONG || dst_type == T_DOUBLE)) {2132__ unaligned_move(addr, reg);2133} else {2134if (dst_type == T_OBJECT && x->is_wide()) {2135__ move_wide(addr, reg);2136} else {2137__ move(addr, reg);2138}2139}2140}214121422143void LIRGenerator::do_UnsafePutRaw(UnsafePutRaw* x) {2144int log2_scale = 0;2145BasicType type = x->basic_type();21462147if (x->has_index()) {2148log2_scale = x->log2_scale();2149}21502151LIRItem base(x->base(), this);2152LIRItem value(x->value(), this);2153LIRItem idx(this);21542155base.load_item();2156if (x->has_index()) {2157idx.set_instruction(x->index());2158idx.load_item();2159}21602161if (type == T_BYTE || type == T_BOOLEAN) {2162value.load_byte_item();2163} else {2164value.load_item();2165}21662167set_no_result(x);21682169LIR_Opr base_op = base.result();2170LIR_Opr index_op = idx.result();21712172#ifdef GENERATE_ADDRESS_IS_PREFERRED2173LIR_Address* addr = generate_address(base_op, index_op, log2_scale, 0, x->basic_type());2174#else2175#ifndef _LP642176if (base_op->type() == T_LONG) {2177base_op = new_register(T_INT);2178__ convert(Bytecodes::_l2i, base.result(), base_op);2179}2180if (x->has_index()) {2181if (index_op->type() == T_LONG) {2182index_op = new_register(T_INT);2183__ convert(Bytecodes::_l2i, idx.result(), index_op);2184}2185}2186// At this point base and index should be all ints and not constants2187assert(base_op->type() == T_INT && !base_op->is_constant(), "base should be an non-constant int");2188assert(!x->has_index() || (index_op->type() == T_INT && !index_op->is_constant()), "index should be an non-constant int");2189#else2190if (x->has_index()) {2191if (index_op->type() == T_INT) {2192index_op = new_register(T_LONG);2193__ convert(Bytecodes::_i2l, idx.result(), index_op);2194}2195}2196// At this point base and index are long and non-constant2197assert(base_op->type() == T_LONG && !base_op->is_constant(), "base must be a non-constant long");2198assert(!x->has_index() || (index_op->type() == T_LONG && !index_op->is_constant()), "index must be a non-constant long");2199#endif22002201if (log2_scale != 0) {2202// temporary fix (platform dependent code without shift on Intel would be better)2203// TODO: ARM also allows embedded shift in the address2204LIR_Opr tmp = new_pointer_register();2205if (TwoOperandLIRForm) {2206__ move(index_op, tmp);2207index_op = tmp;2208}2209__ shift_left(index_op, log2_scale, tmp);2210if (!TwoOperandLIRForm) {2211index_op = tmp;2212}2213}22142215LIR_Address* addr = new LIR_Address(base_op, index_op, x->basic_type());2216#endif // !GENERATE_ADDRESS_IS_PREFERRED2217__ move(value.result(), addr);2218}221922202221void LIRGenerator::do_UnsafeGetObject(UnsafeGetObject* x) {2222BasicType type = x->basic_type();2223LIRItem src(x->object(), this);2224LIRItem off(x->offset(), this);22252226off.load_item();2227src.load_item();22282229LIR_Opr value = rlock_result(x, x->basic_type());22302231get_Object_unsafe(value, src.result(), off.result(), type, x->is_volatile());22322233#if INCLUDE_ALL_GCS2234// We might be reading the value of the referent field of a2235// Reference object in order to attach it back to the live2236// object graph. If G1 is enabled then we need to record2237// the value that is being returned in an SATB log buffer.2238//2239// We need to generate code similar to the following...2240//2241// if (offset == java_lang_ref_Reference::referent_offset) {2242// if (src != NULL) {2243// if (klass(src)->reference_type() != REF_NONE) {2244// pre_barrier(..., value, ...);2245// }2246// }2247// }22482249if (UseG1GC && type == T_OBJECT) {2250bool gen_pre_barrier = true; // Assume we need to generate pre_barrier.2251bool gen_offset_check = true; // Assume we need to generate the offset guard.2252bool gen_source_check = true; // Assume we need to check the src object for null.2253bool gen_type_check = true; // Assume we need to check the reference_type.22542255if (off.is_constant()) {2256jlong off_con = (off.type()->is_int() ?2257(jlong) off.get_jint_constant() :2258off.get_jlong_constant());225922602261if (off_con != (jlong) java_lang_ref_Reference::referent_offset) {2262// The constant offset is something other than referent_offset.2263// We can skip generating/checking the remaining guards and2264// skip generation of the code stub.2265gen_pre_barrier = false;2266} else {2267// The constant offset is the same as referent_offset -2268// we do not need to generate a runtime offset check.2269gen_offset_check = false;2270}2271}22722273// We don't need to generate stub if the source object is an array2274if (gen_pre_barrier && src.type()->is_array()) {2275gen_pre_barrier = false;2276}22772278if (gen_pre_barrier) {2279// We still need to continue with the checks.2280if (src.is_constant()) {2281ciObject* src_con = src.get_jobject_constant();2282guarantee(src_con != NULL, "no source constant");22832284if (src_con->is_null_object()) {2285// The constant src object is null - We can skip2286// generating the code stub.2287gen_pre_barrier = false;2288} else {2289// Non-null constant source object. We still have to generate2290// the slow stub - but we don't need to generate the runtime2291// null object check.2292gen_source_check = false;2293}2294}2295}2296if (gen_pre_barrier && !PatchALot) {2297// Can the klass of object be statically determined to be2298// a sub-class of Reference?2299ciType* type = src.value()->declared_type();2300if ((type != NULL) && type->is_loaded()) {2301if (type->is_subtype_of(compilation()->env()->Reference_klass())) {2302gen_type_check = false;2303} else if (type->is_klass() &&2304!compilation()->env()->Object_klass()->is_subtype_of(type->as_klass())) {2305// Not Reference and not Object klass.2306gen_pre_barrier = false;2307}2308}2309}23102311if (gen_pre_barrier) {2312LabelObj* Lcont = new LabelObj();23132314// We can have generate one runtime check here. Let's start with2315// the offset check.2316// Allocate temp register to src and load it here, otherwise2317// control flow below may confuse register allocator.2318LIR_Opr src_reg = new_register(T_OBJECT);2319__ move(src.result(), src_reg);2320if (gen_offset_check) {2321// if (offset != referent_offset) -> continue2322// If offset is an int then we can do the comparison with the2323// referent_offset constant; otherwise we need to move2324// referent_offset into a temporary register and generate2325// a reg-reg compare.23262327LIR_Opr referent_off;23282329if (off.type()->is_int()) {2330referent_off = LIR_OprFact::intConst(java_lang_ref_Reference::referent_offset);2331} else {2332assert(off.type()->is_long(), "what else?");2333referent_off = new_register(T_LONG);2334__ move(LIR_OprFact::longConst(java_lang_ref_Reference::referent_offset), referent_off);2335}2336__ cmp(lir_cond_notEqual, off.result(), referent_off);2337__ branch(lir_cond_notEqual, as_BasicType(off.type()), Lcont->label());2338}2339if (gen_source_check) {2340// offset is a const and equals referent offset2341// if (source == null) -> continue2342__ cmp(lir_cond_equal, src_reg, LIR_OprFact::oopConst(NULL));2343__ branch(lir_cond_equal, T_OBJECT, Lcont->label());2344}2345LIR_Opr src_klass = new_register(T_METADATA);2346if (gen_type_check) {2347// We have determined that offset == referent_offset && src != null.2348// if (src->_klass->_reference_type == REF_NONE) -> continue2349__ move(new LIR_Address(src_reg, oopDesc::klass_offset_in_bytes(), T_ADDRESS), src_klass);2350LIR_Address* reference_type_addr = new LIR_Address(src_klass, in_bytes(InstanceKlass::reference_type_offset()), T_BYTE);2351LIR_Opr reference_type = new_register(T_INT);2352__ move(reference_type_addr, reference_type);2353__ cmp(lir_cond_equal, reference_type, LIR_OprFact::intConst(REF_NONE));2354__ branch(lir_cond_equal, T_INT, Lcont->label());2355}2356{2357// We have determined that src->_klass->_reference_type != REF_NONE2358// so register the value in the referent field with the pre-barrier.2359pre_barrier(LIR_OprFact::illegalOpr /* addr_opr */,2360value /* pre_val */,2361false /* do_load */,2362false /* patch */,2363NULL /* info */);2364}2365__ branch_destination(Lcont->label());2366}2367}2368#endif // INCLUDE_ALL_GCS23692370if (x->is_volatile() && os::is_MP()) __ membar_acquire();2371}237223732374void LIRGenerator::do_UnsafePutObject(UnsafePutObject* x) {2375BasicType type = x->basic_type();2376LIRItem src(x->object(), this);2377LIRItem off(x->offset(), this);2378LIRItem data(x->value(), this);23792380src.load_item();2381if (type == T_BOOLEAN || type == T_BYTE) {2382data.load_byte_item();2383} else {2384data.load_item();2385}2386off.load_item();23872388set_no_result(x);23892390if (x->is_volatile() && os::is_MP()) __ membar_release();2391put_Object_unsafe(src.result(), off.result(), data.result(), type, x->is_volatile());2392if (x->is_volatile() && os::is_MP()) __ membar();2393}239423952396void LIRGenerator::do_UnsafePrefetch(UnsafePrefetch* x, bool is_store) {2397LIRItem src(x->object(), this);2398LIRItem off(x->offset(), this);23992400src.load_item();2401if (off.is_constant() && can_inline_as_constant(x->offset())) {2402// let it be a constant2403off.dont_load_item();2404} else {2405off.load_item();2406}24072408set_no_result(x);24092410LIR_Address* addr = generate_address(src.result(), off.result(), 0, 0, T_BYTE);2411__ prefetch(addr, is_store);2412}241324142415void LIRGenerator::do_UnsafePrefetchRead(UnsafePrefetchRead* x) {2416do_UnsafePrefetch(x, false);2417}241824192420void LIRGenerator::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {2421do_UnsafePrefetch(x, true);2422}242324242425void LIRGenerator::do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux) {2426int lng = x->length();24272428for (int i = 0; i < lng; i++) {2429SwitchRange* one_range = x->at(i);2430int low_key = one_range->low_key();2431int high_key = one_range->high_key();2432BlockBegin* dest = one_range->sux();2433if (low_key == high_key) {2434__ cmp(lir_cond_equal, value, low_key);2435__ branch(lir_cond_equal, T_INT, dest);2436} else if (high_key - low_key == 1) {2437__ cmp(lir_cond_equal, value, low_key);2438__ branch(lir_cond_equal, T_INT, dest);2439__ cmp(lir_cond_equal, value, high_key);2440__ branch(lir_cond_equal, T_INT, dest);2441} else {2442LabelObj* L = new LabelObj();2443__ cmp(lir_cond_less, value, low_key);2444__ branch(lir_cond_less, T_INT, L->label());2445__ cmp(lir_cond_lessEqual, value, high_key);2446__ branch(lir_cond_lessEqual, T_INT, dest);2447__ branch_destination(L->label());2448}2449}2450__ jump(default_sux);2451}245224532454SwitchRangeArray* LIRGenerator::create_lookup_ranges(TableSwitch* x) {2455SwitchRangeList* res = new SwitchRangeList();2456int len = x->length();2457if (len > 0) {2458BlockBegin* sux = x->sux_at(0);2459int key = x->lo_key();2460BlockBegin* default_sux = x->default_sux();2461SwitchRange* range = new SwitchRange(key, sux);2462for (int i = 0; i < len; i++, key++) {2463BlockBegin* new_sux = x->sux_at(i);2464if (sux == new_sux) {2465// still in same range2466range->set_high_key(key);2467} else {2468// skip tests which explicitly dispatch to the default2469if (sux != default_sux) {2470res->append(range);2471}2472range = new SwitchRange(key, new_sux);2473}2474sux = new_sux;2475}2476if (res->length() == 0 || res->last() != range) res->append(range);2477}2478return res;2479}248024812482// we expect the keys to be sorted by increasing value2483SwitchRangeArray* LIRGenerator::create_lookup_ranges(LookupSwitch* x) {2484SwitchRangeList* res = new SwitchRangeList();2485int len = x->length();2486if (len > 0) {2487BlockBegin* default_sux = x->default_sux();2488int key = x->key_at(0);2489BlockBegin* sux = x->sux_at(0);2490SwitchRange* range = new SwitchRange(key, sux);2491for (int i = 1; i < len; i++) {2492int new_key = x->key_at(i);2493BlockBegin* new_sux = x->sux_at(i);2494if (key+1 == new_key && sux == new_sux) {2495// still in same range2496range->set_high_key(new_key);2497} else {2498// skip tests which explicitly dispatch to the default2499if (range->sux() != default_sux) {2500res->append(range);2501}2502range = new SwitchRange(new_key, new_sux);2503}2504key = new_key;2505sux = new_sux;2506}2507if (res->length() == 0 || res->last() != range) res->append(range);2508}2509return res;2510}251125122513void LIRGenerator::do_TableSwitch(TableSwitch* x) {2514LIRItem tag(x->tag(), this);2515tag.load_item();2516set_no_result(x);25172518if (x->is_safepoint()) {2519__ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));2520}25212522// move values into phi locations2523move_to_phi(x->state());25242525int lo_key = x->lo_key();2526int hi_key = x->hi_key();2527int len = x->length();2528LIR_Opr value = tag.result();2529if (UseTableRanges) {2530do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());2531} else {2532for (int i = 0; i < len; i++) {2533__ cmp(lir_cond_equal, value, i + lo_key);2534__ branch(lir_cond_equal, T_INT, x->sux_at(i));2535}2536__ jump(x->default_sux());2537}2538}253925402541void LIRGenerator::do_LookupSwitch(LookupSwitch* x) {2542LIRItem tag(x->tag(), this);2543tag.load_item();2544set_no_result(x);25452546if (x->is_safepoint()) {2547__ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));2548}25492550// move values into phi locations2551move_to_phi(x->state());25522553LIR_Opr value = tag.result();2554if (UseTableRanges) {2555do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());2556} else {2557int len = x->length();2558for (int i = 0; i < len; i++) {2559__ cmp(lir_cond_equal, value, x->key_at(i));2560__ branch(lir_cond_equal, T_INT, x->sux_at(i));2561}2562__ jump(x->default_sux());2563}2564}256525662567void LIRGenerator::do_Goto(Goto* x) {2568set_no_result(x);25692570if (block()->next()->as_OsrEntry()) {2571// need to free up storage used for OSR entry point2572LIR_Opr osrBuffer = block()->next()->operand();2573BasicTypeList signature;2574signature.append(NOT_LP64(T_INT) LP64_ONLY(T_LONG)); // pass a pointer to osrBuffer2575CallingConvention* cc = frame_map()->c_calling_convention(&signature);2576__ move(osrBuffer, cc->args()->at(0));2577__ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end),2578getThreadTemp(), LIR_OprFact::illegalOpr, cc->args());2579}25802581if (x->is_safepoint()) {2582ValueStack* state = x->state_before() ? x->state_before() : x->state();25832584// increment backedge counter if needed2585CodeEmitInfo* info = state_for(x, state);2586increment_backedge_counter(info, x->profiled_bci());2587CodeEmitInfo* safepoint_info = state_for(x, state);2588__ safepoint(safepoint_poll_register(), safepoint_info);2589}25902591// Gotos can be folded Ifs, handle this case.2592if (x->should_profile()) {2593ciMethod* method = x->profiled_method();2594assert(method != NULL, "method should be set if branch is profiled");2595ciMethodData* md = method->method_data_or_null();2596assert(md != NULL, "Sanity");2597ciProfileData* data = md->bci_to_data(x->profiled_bci());2598assert(data != NULL, "must have profiling data");2599int offset;2600if (x->direction() == Goto::taken) {2601assert(data->is_BranchData(), "need BranchData for two-way branches");2602offset = md->byte_offset_of_slot(data, BranchData::taken_offset());2603} else if (x->direction() == Goto::not_taken) {2604assert(data->is_BranchData(), "need BranchData for two-way branches");2605offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());2606} else {2607assert(data->is_JumpData(), "need JumpData for branches");2608offset = md->byte_offset_of_slot(data, JumpData::taken_offset());2609}2610LIR_Opr md_reg = new_register(T_METADATA);2611__ metadata2reg(md->constant_encoding(), md_reg);26122613increment_counter(new LIR_Address(md_reg, offset,2614NOT_LP64(T_INT) LP64_ONLY(T_LONG)), DataLayout::counter_increment);2615}26162617// emit phi-instruction move after safepoint since this simplifies2618// describing the state as the safepoint.2619move_to_phi(x->state());26202621__ jump(x->default_sux());2622}26232624/**2625* Emit profiling code if needed for arguments, parameters, return value types2626*2627* @param md MDO the code will update at runtime2628* @param md_base_offset common offset in the MDO for this profile and subsequent ones2629* @param md_offset offset in the MDO (on top of md_base_offset) for this profile2630* @param profiled_k current profile2631* @param obj IR node for the object to be profiled2632* @param mdp register to hold the pointer inside the MDO (md + md_base_offset).2633* Set once we find an update to make and use for next ones.2634* @param not_null true if we know obj cannot be null2635* @param signature_at_call_k signature at call for obj2636* @param callee_signature_k signature of callee for obj2637* at call and callee signatures differ at method handle call2638* @return the only klass we know will ever be seen at this profile point2639*/2640ciKlass* LIRGenerator::profile_type(ciMethodData* md, int md_base_offset, int md_offset, intptr_t profiled_k,2641Value obj, LIR_Opr& mdp, bool not_null, ciKlass* signature_at_call_k,2642ciKlass* callee_signature_k) {2643ciKlass* result = NULL;2644bool do_null = !not_null && !TypeEntries::was_null_seen(profiled_k);2645bool do_update = !TypeEntries::is_type_unknown(profiled_k);2646// known not to be null or null bit already set and already set to2647// unknown: nothing we can do to improve profiling2648if (!do_null && !do_update) {2649return result;2650}26512652ciKlass* exact_klass = NULL;2653Compilation* comp = Compilation::current();2654if (do_update) {2655// try to find exact type, using CHA if possible, so that loading2656// the klass from the object can be avoided2657ciType* type = obj->exact_type();2658if (type == NULL) {2659type = obj->declared_type();2660type = comp->cha_exact_type(type);2661}2662assert(type == NULL || type->is_klass(), "type should be class");2663exact_klass = (type != NULL && type->is_loaded()) ? (ciKlass*)type : NULL;26642665do_update = exact_klass == NULL || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;2666}26672668if (!do_null && !do_update) {2669return result;2670}26712672ciKlass* exact_signature_k = NULL;2673if (do_update) {2674// Is the type from the signature exact (the only one possible)?2675exact_signature_k = signature_at_call_k->exact_klass();2676if (exact_signature_k == NULL) {2677exact_signature_k = comp->cha_exact_type(signature_at_call_k);2678} else {2679result = exact_signature_k;2680// Known statically. No need to emit any code: prevent2681// LIR_Assembler::emit_profile_type() from emitting useless code2682profiled_k = ciTypeEntries::with_status(result, profiled_k);2683}2684// exact_klass and exact_signature_k can be both non NULL but2685// different if exact_klass is loaded after the ciObject for2686// exact_signature_k is created.2687if (exact_klass == NULL && exact_signature_k != NULL && exact_klass != exact_signature_k) {2688// sometimes the type of the signature is better than the best type2689// the compiler has2690exact_klass = exact_signature_k;2691}2692if (callee_signature_k != NULL &&2693callee_signature_k != signature_at_call_k) {2694ciKlass* improved_klass = callee_signature_k->exact_klass();2695if (improved_klass == NULL) {2696improved_klass = comp->cha_exact_type(callee_signature_k);2697}2698if (exact_klass == NULL && improved_klass != NULL && exact_klass != improved_klass) {2699exact_klass = exact_signature_k;2700}2701}2702do_update = exact_klass == NULL || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;2703}27042705if (!do_null && !do_update) {2706return result;2707}27082709if (mdp == LIR_OprFact::illegalOpr) {2710mdp = new_register(T_METADATA);2711__ metadata2reg(md->constant_encoding(), mdp);2712if (md_base_offset != 0) {2713LIR_Address* base_type_address = new LIR_Address(mdp, md_base_offset, T_ADDRESS);2714mdp = new_pointer_register();2715__ leal(LIR_OprFact::address(base_type_address), mdp);2716}2717}2718LIRItem value(obj, this);2719value.load_item();2720__ profile_type(new LIR_Address(mdp, md_offset, T_METADATA),2721value.result(), exact_klass, profiled_k, new_pointer_register(), not_null, exact_signature_k != NULL);2722return result;2723}27242725// profile parameters on entry to the root of the compilation2726void LIRGenerator::profile_parameters(Base* x) {2727if (compilation()->profile_parameters()) {2728CallingConvention* args = compilation()->frame_map()->incoming_arguments();2729ciMethodData* md = scope()->method()->method_data_or_null();2730assert(md != NULL, "Sanity");27312732if (md->parameters_type_data() != NULL) {2733ciParametersTypeData* parameters_type_data = md->parameters_type_data();2734ciTypeStackSlotEntries* parameters = parameters_type_data->parameters();2735LIR_Opr mdp = LIR_OprFact::illegalOpr;2736for (int java_index = 0, i = 0, j = 0; j < parameters_type_data->number_of_parameters(); i++) {2737LIR_Opr src = args->at(i);2738assert(!src->is_illegal(), "check");2739BasicType t = src->type();2740if (t == T_OBJECT || t == T_ARRAY) {2741intptr_t profiled_k = parameters->type(j);2742Local* local = x->state()->local_at(java_index)->as_Local();2743ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),2744in_bytes(ParametersTypeData::type_offset(j)) - in_bytes(ParametersTypeData::type_offset(0)),2745profiled_k, local, mdp, false, local->declared_type()->as_klass(), NULL);2746// If the profile is known statically set it once for all and do not emit any code2747if (exact != NULL) {2748md->set_parameter_type(j, exact);2749}2750j++;2751}2752java_index += type2size[t];2753}2754}2755}2756}27572758void LIRGenerator::do_Base(Base* x) {2759__ std_entry(LIR_OprFact::illegalOpr);2760// Emit moves from physical registers / stack slots to virtual registers2761CallingConvention* args = compilation()->frame_map()->incoming_arguments();2762IRScope* irScope = compilation()->hir()->top_scope();2763int java_index = 0;2764for (int i = 0; i < args->length(); i++) {2765LIR_Opr src = args->at(i);2766assert(!src->is_illegal(), "check");2767BasicType t = src->type();27682769// Types which are smaller than int are passed as int, so2770// correct the type which passed.2771switch (t) {2772case T_BYTE:2773case T_BOOLEAN:2774case T_SHORT:2775case T_CHAR:2776t = T_INT;2777break;2778}27792780LIR_Opr dest = new_register(t);2781__ move(src, dest);27822783// Assign new location to Local instruction for this local2784Local* local = x->state()->local_at(java_index)->as_Local();2785assert(local != NULL, "Locals for incoming arguments must have been created");2786#ifndef __SOFTFP__2787// The java calling convention passes double as long and float as int.2788assert(as_ValueType(t)->tag() == local->type()->tag(), "check");2789#endif // __SOFTFP__2790local->set_operand(dest);2791_instruction_for_operand.at_put_grow(dest->vreg_number(), local, NULL);2792java_index += type2size[t];2793}27942795if (compilation()->env()->dtrace_method_probes()) {2796BasicTypeList signature;2797signature.append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread2798signature.append(T_METADATA); // Method*2799LIR_OprList* args = new LIR_OprList();2800args->append(getThreadPointer());2801LIR_Opr meth = new_register(T_METADATA);2802__ metadata2reg(method()->constant_encoding(), meth);2803args->append(meth);2804call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), voidType, NULL);2805}28062807if (method()->is_synchronized()) {2808LIR_Opr obj;2809if (method()->is_static()) {2810obj = new_register(T_OBJECT);2811__ oop2reg(method()->holder()->java_mirror()->constant_encoding(), obj);2812} else {2813Local* receiver = x->state()->local_at(0)->as_Local();2814assert(receiver != NULL, "must already exist");2815obj = receiver->operand();2816}2817assert(obj->is_valid(), "must be valid");28182819if (method()->is_synchronized() && GenerateSynchronizationCode) {2820LIR_Opr lock = new_register(T_INT);2821__ load_stack_address_monitor(0, lock);28222823CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), NULL, x->check_flag(Instruction::DeoptimizeOnException));2824CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);28252826// receiver is guaranteed non-NULL so don't need CodeEmitInfo2827__ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, NULL);2828}2829}28302831// increment invocation counters if needed2832if (!method()->is_accessor()) { // Accessors do not have MDOs, so no counting.2833profile_parameters(x);2834CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), NULL, false);2835increment_invocation_counter(info);2836}28372838// all blocks with a successor must end with an unconditional jump2839// to the successor even if they are consecutive2840__ jump(x->default_sux());2841}284228432844void LIRGenerator::do_OsrEntry(OsrEntry* x) {2845// construct our frame and model the production of incoming pointer2846// to the OSR buffer.2847__ osr_entry(LIR_Assembler::osrBufferPointer());2848LIR_Opr result = rlock_result(x);2849__ move(LIR_Assembler::osrBufferPointer(), result);2850}285128522853void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {2854assert(args->length() == arg_list->length(),2855err_msg_res("args=%d, arg_list=%d", args->length(), arg_list->length()));2856for (int i = x->has_receiver() ? 1 : 0; i < args->length(); i++) {2857LIRItem* param = args->at(i);2858LIR_Opr loc = arg_list->at(i);2859if (loc->is_register()) {2860param->load_item_force(loc);2861} else {2862LIR_Address* addr = loc->as_address_ptr();2863param->load_for_store(addr->type());2864if (addr->type() == T_OBJECT) {2865__ move_wide(param->result(), addr);2866} else2867if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {2868__ unaligned_move(param->result(), addr);2869} else {2870__ move(param->result(), addr);2871}2872}2873}28742875if (x->has_receiver()) {2876LIRItem* receiver = args->at(0);2877LIR_Opr loc = arg_list->at(0);2878if (loc->is_register()) {2879receiver->load_item_force(loc);2880} else {2881assert(loc->is_address(), "just checking");2882receiver->load_for_store(T_OBJECT);2883__ move_wide(receiver->result(), loc->as_address_ptr());2884}2885}2886}288728882889// Visits all arguments, returns appropriate items without loading them2890LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {2891LIRItemList* argument_items = new LIRItemList();2892if (x->has_receiver()) {2893LIRItem* receiver = new LIRItem(x->receiver(), this);2894argument_items->append(receiver);2895}2896for (int i = 0; i < x->number_of_arguments(); i++) {2897LIRItem* param = new LIRItem(x->argument_at(i), this);2898argument_items->append(param);2899}2900return argument_items;2901}290229032904// The invoke with receiver has following phases:2905// a) traverse and load/lock receiver;2906// b) traverse all arguments -> item-array (invoke_visit_argument)2907// c) push receiver on stack2908// d) load each of the items and push on stack2909// e) unlock receiver2910// f) move receiver into receiver-register %o02911// g) lock result registers and emit call operation2912//2913// Before issuing a call, we must spill-save all values on stack2914// that are in caller-save register. "spill-save" moves those registers2915// either in a free callee-save register or spills them if no free2916// callee save register is available.2917//2918// The problem is where to invoke spill-save.2919// - if invoked between e) and f), we may lock callee save2920// register in "spill-save" that destroys the receiver register2921// before f) is executed2922// - if we rearrange f) to be earlier (by loading %o0) it2923// may destroy a value on the stack that is currently in %o02924// and is waiting to be spilled2925// - if we keep the receiver locked while doing spill-save,2926// we cannot spill it as it is spill-locked2927//2928void LIRGenerator::do_Invoke(Invoke* x) {2929CallingConvention* cc = frame_map()->java_calling_convention(x->signature(), true);29302931LIR_OprList* arg_list = cc->args();2932LIRItemList* args = invoke_visit_arguments(x);2933LIR_Opr receiver = LIR_OprFact::illegalOpr;29342935// setup result register2936LIR_Opr result_register = LIR_OprFact::illegalOpr;2937if (x->type() != voidType) {2938#ifdef AARCH322939result_register = java_result_register_for(x->type());2940#else2941result_register = result_register_for(x->type());2942#endif2943}29442945CodeEmitInfo* info = state_for(x, x->state());29462947invoke_load_arguments(x, args, arg_list);29482949if (x->has_receiver()) {2950args->at(0)->load_item_force(LIR_Assembler::receiverOpr());2951receiver = args->at(0)->result();2952}29532954// emit invoke code2955bool optimized = x->target_is_loaded() && x->target_is_final();2956assert(receiver->is_illegal() || receiver->is_equal(LIR_Assembler::receiverOpr()), "must match");29572958// JSR 2922959// Preserve the SP over MethodHandle call sites, if needed.2960ciMethod* target = x->target();2961bool is_method_handle_invoke = (// %%% FIXME: Are both of these relevant?2962target->is_method_handle_intrinsic() ||2963target->is_compiled_lambda_form());2964if (is_method_handle_invoke) {2965info->set_is_method_handle_invoke(true);2966if(FrameMap::method_handle_invoke_SP_save_opr() != LIR_OprFact::illegalOpr) {2967__ move(FrameMap::stack_pointer(), FrameMap::method_handle_invoke_SP_save_opr());2968}2969}29702971switch (x->code()) {2972case Bytecodes::_invokestatic:2973__ call_static(target, result_register,2974SharedRuntime::get_resolve_static_call_stub(),2975arg_list, info);2976break;2977case Bytecodes::_invokespecial:2978case Bytecodes::_invokevirtual:2979case Bytecodes::_invokeinterface:2980// for final target we still produce an inline cache, in order2981// to be able to call mixed mode2982if (x->code() == Bytecodes::_invokespecial || optimized) {2983__ call_opt_virtual(target, receiver, result_register,2984SharedRuntime::get_resolve_opt_virtual_call_stub(),2985arg_list, info);2986} else if (x->vtable_index() < 0) {2987__ call_icvirtual(target, receiver, result_register,2988SharedRuntime::get_resolve_virtual_call_stub(),2989arg_list, info);2990} else {2991int entry_offset = InstanceKlass::vtable_start_offset() + x->vtable_index() * vtableEntry::size();2992int vtable_offset = entry_offset * wordSize + vtableEntry::method_offset_in_bytes();2993__ call_virtual(target, receiver, result_register, vtable_offset, arg_list, info);2994}2995break;2996case Bytecodes::_invokedynamic: {2997__ call_dynamic(target, receiver, result_register,2998SharedRuntime::get_resolve_static_call_stub(),2999arg_list, info);3000break;3001}3002default:3003fatal(err_msg("unexpected bytecode: %s", Bytecodes::name(x->code())));3004break;3005}30063007// JSR 2923008// Restore the SP after MethodHandle call sites, if needed.3009if (is_method_handle_invoke3010&& FrameMap::method_handle_invoke_SP_save_opr() != LIR_OprFact::illegalOpr) {3011__ move(FrameMap::method_handle_invoke_SP_save_opr(), FrameMap::stack_pointer());3012}30133014if (x->type()->is_float() || x->type()->is_double()) {3015// Force rounding of results from non-strictfp when in strictfp3016// scope (or when we don't know the strictness of the callee, to3017// be safe.)3018if (method()->is_strict()) {3019if (!x->target_is_loaded() || !x->target_is_strictfp()) {3020result_register = round_item(result_register);3021}3022}3023}30243025if (result_register->is_valid()) {3026LIR_Opr result = rlock_result(x);3027__ move(result_register, result);3028}3029}303030313032void LIRGenerator::do_FPIntrinsics(Intrinsic* x) {3033assert(x->number_of_arguments() == 1, "wrong type");3034LIRItem value (x->argument_at(0), this);3035LIR_Opr reg = rlock_result(x);3036value.load_item();3037LIR_Opr tmp = force_to_spill(value.result(), as_BasicType(x->type()));3038__ move(tmp, reg);3039}3040304130423043// Code for : x->x() {x->cond()} x->y() ? x->tval() : x->fval()3044void LIRGenerator::do_IfOp(IfOp* x) {3045#ifdef ASSERT3046{3047ValueTag xtag = x->x()->type()->tag();3048ValueTag ttag = x->tval()->type()->tag();3049assert(xtag == intTag || xtag == objectTag, "cannot handle others");3050assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");3051assert(ttag == x->fval()->type()->tag(), "cannot handle others");3052}3053#endif30543055LIRItem left(x->x(), this);3056LIRItem right(x->y(), this);3057left.load_item();3058if (can_inline_as_constant(right.value())) {3059right.dont_load_item();3060} else {3061right.load_item();3062}30633064LIRItem t_val(x->tval(), this);3065LIRItem f_val(x->fval(), this);3066t_val.dont_load_item();3067f_val.dont_load_item();3068LIR_Opr reg = rlock_result(x);30693070__ cmp(lir_cond(x->cond()), left.result(), right.result());3071__ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg, as_BasicType(x->x()->type()));3072}30733074#ifdef JFR_HAVE_INTRINSICS3075void LIRGenerator::do_ClassIDIntrinsic(Intrinsic* x) {3076CodeEmitInfo* info = state_for(x);3077CodeEmitInfo* info2 = new CodeEmitInfo(info); // Clone for the second null check30783079assert(info != NULL, "must have info");3080LIRItem arg(x->argument_at(0), this);30813082arg.load_item();3083LIR_Opr klass = new_register(T_METADATA);3084__ move(new LIR_Address(arg.result(), java_lang_Class::klass_offset_in_bytes(), T_ADDRESS), klass, info);3085LIR_Opr id = new_register(T_LONG);3086ByteSize offset = KLASS_TRACE_ID_OFFSET;3087LIR_Address* trace_id_addr = new LIR_Address(klass, in_bytes(offset), T_LONG);30883089__ move(trace_id_addr, id);3090__ logical_or(id, LIR_OprFact::longConst(0x01l), id);3091__ store(id, trace_id_addr);30923093#ifdef TRACE_ID_META_BITS3094__ logical_and(id, LIR_OprFact::longConst(~TRACE_ID_META_BITS), id);3095#endif3096#ifdef TRACE_ID_SHIFT3097__ unsigned_shift_right(id, TRACE_ID_SHIFT, id);3098#endif30993100__ move(id, rlock_result(x));3101}31023103void LIRGenerator::do_getEventWriter(Intrinsic* x) {3104LabelObj* L_end = new LabelObj();31053106LIR_Address* jobj_addr = new LIR_Address(getThreadPointer(),3107in_bytes(THREAD_LOCAL_WRITER_OFFSET_JFR),3108T_OBJECT);3109LIR_Opr result = rlock_result(x);3110__ move_wide(jobj_addr, result);3111__ cmp(lir_cond_equal, result, LIR_OprFact::oopConst(NULL));3112__ branch(lir_cond_equal, T_OBJECT, L_end->label());3113__ move_wide(new LIR_Address(result, T_OBJECT), result);31143115__ branch_destination(L_end->label());3116}3117#endif31183119void LIRGenerator::do_RuntimeCall(address routine, int expected_arguments, Intrinsic* x) {3120assert(x->number_of_arguments() == expected_arguments, "wrong type");3121LIR_Opr reg = result_register_for(x->type());3122__ call_runtime_leaf(routine, getThreadTemp(),3123reg, new LIR_OprList());3124LIR_Opr result = rlock_result(x);3125__ move(reg, result);3126}31273128#ifdef TRACE_HAVE_INTRINSICS3129void LIRGenerator::do_ThreadIDIntrinsic(Intrinsic* x) {3130LIR_Opr thread = getThreadPointer();3131LIR_Opr osthread = new_pointer_register();3132__ move(new LIR_Address(thread, in_bytes(JavaThread::osthread_offset()), osthread->type()), osthread);3133size_t thread_id_size = OSThread::thread_id_size();3134if (thread_id_size == (size_t) BytesPerLong) {3135LIR_Opr id = new_register(T_LONG);3136__ move(new LIR_Address(osthread, in_bytes(OSThread::thread_id_offset()), T_LONG), id);3137__ convert(Bytecodes::_l2i, id, rlock_result(x));3138} else if (thread_id_size == (size_t) BytesPerInt) {3139__ move(new LIR_Address(osthread, in_bytes(OSThread::thread_id_offset()), T_INT), rlock_result(x));3140} else {3141ShouldNotReachHere();3142}3143}31443145void LIRGenerator::do_ClassIDIntrinsic(Intrinsic* x) {3146CodeEmitInfo* info = state_for(x);3147CodeEmitInfo* info2 = new CodeEmitInfo(info); // Clone for the second null check3148BasicType klass_pointer_type = NOT_LP64(T_INT) LP64_ONLY(T_LONG);3149assert(info != NULL, "must have info");3150LIRItem arg(x->argument_at(1), this);3151arg.load_item();3152LIR_Opr klass = new_pointer_register();3153__ move(new LIR_Address(arg.result(), java_lang_Class::klass_offset_in_bytes(), klass_pointer_type), klass, info);3154LIR_Opr id = new_register(T_LONG);3155ByteSize offset = TRACE_ID_OFFSET;3156LIR_Address* trace_id_addr = new LIR_Address(klass, in_bytes(offset), T_LONG);3157__ move(trace_id_addr, id);3158__ logical_or(id, LIR_OprFact::longConst(0x01l), id);3159__ store(id, trace_id_addr);3160__ logical_and(id, LIR_OprFact::longConst(~0x3l), id);3161__ move(id, rlock_result(x));3162}3163#endif31643165void LIRGenerator::do_Intrinsic(Intrinsic* x) {3166switch (x->id()) {3167case vmIntrinsics::_intBitsToFloat :3168case vmIntrinsics::_doubleToRawLongBits :3169case vmIntrinsics::_longBitsToDouble :3170case vmIntrinsics::_floatToRawIntBits : {3171do_FPIntrinsics(x);3172break;3173}31743175#ifdef JFR_HAVE_INTRINSICS3176case vmIntrinsics::_getClassId:3177do_ClassIDIntrinsic(x);3178break;3179case vmIntrinsics::_getEventWriter:3180do_getEventWriter(x);3181break;3182case vmIntrinsics::_counterTime:3183do_RuntimeCall(CAST_FROM_FN_PTR(address, JFR_TIME_FUNCTION), 0, x);3184break;3185#endif31863187case vmIntrinsics::_currentTimeMillis:3188do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeMillis), 0, x);3189break;31903191case vmIntrinsics::_nanoTime:3192do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeNanos), 0, x);3193break;31943195case vmIntrinsics::_Object_init: do_RegisterFinalizer(x); break;3196case vmIntrinsics::_isInstance: do_isInstance(x); break;3197case vmIntrinsics::_getClass: do_getClass(x); break;3198case vmIntrinsics::_currentThread: do_currentThread(x); break;31993200case vmIntrinsics::_dlog: // fall through3201case vmIntrinsics::_dlog10: // fall through3202case vmIntrinsics::_dabs: // fall through3203case vmIntrinsics::_dsqrt: // fall through3204case vmIntrinsics::_dtan: // fall through3205case vmIntrinsics::_dsin : // fall through3206case vmIntrinsics::_dcos : // fall through3207case vmIntrinsics::_dexp : // fall through3208case vmIntrinsics::_dpow : do_MathIntrinsic(x); break;3209case vmIntrinsics::_arraycopy: do_ArrayCopy(x); break;32103211// java.nio.Buffer.checkIndex3212case vmIntrinsics::_checkIndex: do_NIOCheckIndex(x); break;32133214case vmIntrinsics::_compareAndSwapObject:3215do_CompareAndSwap(x, objectType);3216break;3217case vmIntrinsics::_compareAndSwapInt:3218do_CompareAndSwap(x, intType);3219break;3220case vmIntrinsics::_compareAndSwapLong:3221do_CompareAndSwap(x, longType);3222break;32233224case vmIntrinsics::_loadFence :3225if (os::is_MP()) __ membar_acquire();3226break;3227case vmIntrinsics::_storeFence:3228if (os::is_MP()) __ membar_release();3229break;3230case vmIntrinsics::_fullFence :3231if (os::is_MP()) __ membar();3232break;32333234case vmIntrinsics::_Reference_get:3235do_Reference_get(x);3236break;32373238case vmIntrinsics::_updateCRC32:3239case vmIntrinsics::_updateBytesCRC32:3240case vmIntrinsics::_updateByteBufferCRC32:3241do_update_CRC32(x);3242break;32433244default: ShouldNotReachHere(); break;3245}3246}32473248void LIRGenerator::profile_arguments(ProfileCall* x) {3249if (compilation()->profile_arguments()) {3250int bci = x->bci_of_invoke();3251ciMethodData* md = x->method()->method_data_or_null();3252ciProfileData* data = md->bci_to_data(bci);3253if (data != NULL) {3254if ((data->is_CallTypeData() && data->as_CallTypeData()->has_arguments()) ||3255(data->is_VirtualCallTypeData() && data->as_VirtualCallTypeData()->has_arguments())) {3256ByteSize extra = data->is_CallTypeData() ? CallTypeData::args_data_offset() : VirtualCallTypeData::args_data_offset();3257int base_offset = md->byte_offset_of_slot(data, extra);3258LIR_Opr mdp = LIR_OprFact::illegalOpr;3259ciTypeStackSlotEntries* args = data->is_CallTypeData() ? ((ciCallTypeData*)data)->args() : ((ciVirtualCallTypeData*)data)->args();32603261Bytecodes::Code bc = x->method()->java_code_at_bci(bci);3262int start = 0;3263int stop = data->is_CallTypeData() ? ((ciCallTypeData*)data)->number_of_arguments() : ((ciVirtualCallTypeData*)data)->number_of_arguments();3264if (x->callee()->is_loaded() && x->callee()->is_static() && Bytecodes::has_receiver(bc)) {3265// first argument is not profiled at call (method handle invoke)3266assert(x->method()->raw_code_at_bci(bci) == Bytecodes::_invokehandle, "invokehandle expected");3267start = 1;3268}3269ciSignature* callee_signature = x->callee()->signature();3270// method handle call to virtual method3271bool has_receiver = x->callee()->is_loaded() && !x->callee()->is_static() && !Bytecodes::has_receiver(bc);3272ciSignatureStream callee_signature_stream(callee_signature, has_receiver ? x->callee()->holder() : NULL);32733274bool ignored_will_link;3275ciSignature* signature_at_call = NULL;3276x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);3277ciSignatureStream signature_at_call_stream(signature_at_call);32783279// if called through method handle invoke, some arguments may have been popped3280for (int i = 0; i < stop && i+start < x->nb_profiled_args(); i++) {3281int off = in_bytes(TypeEntriesAtCall::argument_type_offset(i)) - in_bytes(TypeEntriesAtCall::args_data_offset());3282ciKlass* exact = profile_type(md, base_offset, off,3283args->type(i), x->profiled_arg_at(i+start), mdp,3284!x->arg_needs_null_check(i+start),3285signature_at_call_stream.next_klass(), callee_signature_stream.next_klass());3286if (exact != NULL) {3287md->set_argument_type(bci, i, exact);3288}3289}3290} else {3291#ifdef ASSERT3292Bytecodes::Code code = x->method()->raw_code_at_bci(x->bci_of_invoke());3293int n = x->nb_profiled_args();3294assert(MethodData::profile_parameters() && (MethodData::profile_arguments_jsr292_only() ||3295(x->inlined() && ((code == Bytecodes::_invokedynamic && n <= 1) || (code == Bytecodes::_invokehandle && n <= 2)))),3296"only at JSR292 bytecodes");3297#endif3298}3299}3300}3301}33023303// profile parameters on entry to an inlined method3304void LIRGenerator::profile_parameters_at_call(ProfileCall* x) {3305if (compilation()->profile_parameters() && x->inlined()) {3306ciMethodData* md = x->callee()->method_data_or_null();3307if (md != NULL) {3308ciParametersTypeData* parameters_type_data = md->parameters_type_data();3309if (parameters_type_data != NULL) {3310ciTypeStackSlotEntries* parameters = parameters_type_data->parameters();3311LIR_Opr mdp = LIR_OprFact::illegalOpr;3312bool has_receiver = !x->callee()->is_static();3313ciSignature* sig = x->callee()->signature();3314ciSignatureStream sig_stream(sig, has_receiver ? x->callee()->holder() : NULL);3315int i = 0; // to iterate on the Instructions3316Value arg = x->recv();3317bool not_null = false;3318int bci = x->bci_of_invoke();3319Bytecodes::Code bc = x->method()->java_code_at_bci(bci);3320// The first parameter is the receiver so that's what we start3321// with if it exists. One exception is method handle call to3322// virtual method: the receiver is in the args list3323if (arg == NULL || !Bytecodes::has_receiver(bc)) {3324i = 1;3325arg = x->profiled_arg_at(0);3326not_null = !x->arg_needs_null_check(0);3327}3328int k = 0; // to iterate on the profile data3329for (;;) {3330intptr_t profiled_k = parameters->type(k);3331ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),3332in_bytes(ParametersTypeData::type_offset(k)) - in_bytes(ParametersTypeData::type_offset(0)),3333profiled_k, arg, mdp, not_null, sig_stream.next_klass(), NULL);3334// If the profile is known statically set it once for all and do not emit any code3335if (exact != NULL) {3336md->set_parameter_type(k, exact);3337}3338k++;3339if (k >= parameters_type_data->number_of_parameters()) {3340#ifdef ASSERT3341int extra = 0;3342if (MethodData::profile_arguments() && TypeProfileParmsLimit != -1 &&3343x->nb_profiled_args() >= TypeProfileParmsLimit &&3344x->recv() != NULL && Bytecodes::has_receiver(bc)) {3345extra += 1;3346}3347assert(i == x->nb_profiled_args() - extra || (TypeProfileParmsLimit != -1 && TypeProfileArgsLimit > TypeProfileParmsLimit), "unused parameters?");3348#endif3349break;3350}3351arg = x->profiled_arg_at(i);3352not_null = !x->arg_needs_null_check(i);3353i++;3354}3355}3356}3357}3358}33593360void LIRGenerator::do_ProfileCall(ProfileCall* x) {3361// Need recv in a temporary register so it interferes with the other temporaries3362LIR_Opr recv = LIR_OprFact::illegalOpr;3363LIR_Opr mdo = new_register(T_METADATA);3364// tmp is used to hold the counters on SPARC3365LIR_Opr tmp = new_pointer_register();33663367if (x->nb_profiled_args() > 0) {3368profile_arguments(x);3369}33703371// profile parameters on inlined method entry including receiver3372if (x->recv() != NULL || x->nb_profiled_args() > 0) {3373profile_parameters_at_call(x);3374}33753376if (x->recv() != NULL) {3377LIRItem value(x->recv(), this);3378value.load_item();3379recv = new_register(T_OBJECT);3380__ move(value.result(), recv);3381}3382__ profile_call(x->method(), x->bci_of_invoke(), x->callee(), mdo, recv, tmp, x->known_holder());3383}33843385void LIRGenerator::do_ProfileReturnType(ProfileReturnType* x) {3386int bci = x->bci_of_invoke();3387ciMethodData* md = x->method()->method_data_or_null();3388ciProfileData* data = md->bci_to_data(bci);3389if (data != NULL) {3390assert(data->is_CallTypeData() || data->is_VirtualCallTypeData(), "wrong profile data type");3391ciReturnTypeEntry* ret = data->is_CallTypeData() ? ((ciCallTypeData*)data)->ret() : ((ciVirtualCallTypeData*)data)->ret();3392LIR_Opr mdp = LIR_OprFact::illegalOpr;33933394bool ignored_will_link;3395ciSignature* signature_at_call = NULL;3396x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);33973398// The offset within the MDO of the entry to update may be too large3399// to be used in load/store instructions on some platforms. So have3400// profile_type() compute the address of the profile in a register.3401ciKlass* exact = profile_type(md, md->byte_offset_of_slot(data, ret->type_offset()), 0,3402ret->type(), x->ret(), mdp,3403!x->needs_null_check(),3404signature_at_call->return_type()->as_klass(),3405x->callee()->signature()->return_type()->as_klass());3406if (exact != NULL) {3407md->set_return_type(bci, exact);3408}3409}3410}34113412void LIRGenerator::do_ProfileInvoke(ProfileInvoke* x) {3413// We can safely ignore accessors here, since c2 will inline them anyway,3414// accessors are also always mature.3415if (!x->inlinee()->is_accessor()) {3416CodeEmitInfo* info = state_for(x, x->state(), true);3417// Notify the runtime very infrequently only to take care of counter overflows3418increment_event_counter_impl(info, x->inlinee(), (1 << Tier23InlineeNotifyFreqLog) - 1, InvocationEntryBci, false, true);3419}3420}34213422void LIRGenerator::increment_event_counter(CodeEmitInfo* info, int bci, bool backedge) {3423int freq_log = 0;3424int level = compilation()->env()->comp_level();3425if (level == CompLevel_limited_profile) {3426freq_log = (backedge ? Tier2BackedgeNotifyFreqLog : Tier2InvokeNotifyFreqLog);3427} else if (level == CompLevel_full_profile) {3428freq_log = (backedge ? Tier3BackedgeNotifyFreqLog : Tier3InvokeNotifyFreqLog);3429} else {3430ShouldNotReachHere();3431}3432// Increment the appropriate invocation/backedge counter and notify the runtime.3433increment_event_counter_impl(info, info->scope()->method(), (1 << freq_log) - 1, bci, backedge, true);3434}34353436void LIRGenerator::increment_event_counter_impl(CodeEmitInfo* info,3437ciMethod *method, int frequency,3438int bci, bool backedge, bool notify) {3439assert(frequency == 0 || is_power_of_2(frequency + 1), "Frequency must be x^2 - 1 or 0");3440int level = _compilation->env()->comp_level();3441assert(level > CompLevel_simple, "Shouldn't be here");34423443int offset = -1;3444LIR_Opr counter_holder = NULL;3445if (level == CompLevel_limited_profile) {3446MethodCounters* counters_adr = method->ensure_method_counters();3447if (counters_adr == NULL) {3448bailout("method counters allocation failed");3449return;3450}3451counter_holder = new_pointer_register();3452__ move(LIR_OprFact::intptrConst(counters_adr), counter_holder);3453offset = in_bytes(backedge ? MethodCounters::backedge_counter_offset() :3454MethodCounters::invocation_counter_offset());3455} else if (level == CompLevel_full_profile) {3456counter_holder = new_register(T_METADATA);3457offset = in_bytes(backedge ? MethodData::backedge_counter_offset() :3458MethodData::invocation_counter_offset());3459ciMethodData* md = method->method_data_or_null();3460assert(md != NULL, "Sanity");3461__ metadata2reg(md->constant_encoding(), counter_holder);3462} else {3463ShouldNotReachHere();3464}3465LIR_Address* counter = new LIR_Address(counter_holder, offset, T_INT);3466LIR_Opr result = new_register(T_INT);3467__ load(counter, result);3468__ add(result, LIR_OprFact::intConst(InvocationCounter::count_increment), result);3469__ store(result, counter);3470if (notify) {3471LIR_Opr mask = load_immediate(frequency << InvocationCounter::count_shift, T_INT);3472LIR_Opr meth = new_register(T_METADATA);3473__ metadata2reg(method->constant_encoding(), meth);3474__ logical_and(result, mask, result);3475__ cmp(lir_cond_equal, result, LIR_OprFact::intConst(0));3476// The bci for info can point to cmp for if's we want the if bci3477CodeStub* overflow = new CounterOverflowStub(info, bci, meth);3478__ branch(lir_cond_equal, T_INT, overflow);3479__ branch_destination(overflow->continuation());3480}3481}34823483void LIRGenerator::do_RuntimeCall(RuntimeCall* x) {3484LIR_OprList* args = new LIR_OprList(x->number_of_arguments());3485BasicTypeList* signature = new BasicTypeList(x->number_of_arguments());34863487if (x->pass_thread()) {3488signature->append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread3489args->append(getThreadPointer());3490}34913492for (int i = 0; i < x->number_of_arguments(); i++) {3493Value a = x->argument_at(i);3494LIRItem* item = new LIRItem(a, this);3495item->load_item();3496args->append(item->result());3497signature->append(as_BasicType(a->type()));3498}34993500LIR_Opr result = call_runtime(signature, args, x->entry(), x->type(), NULL);3501if (x->type() == voidType) {3502set_no_result(x);3503} else {3504__ move(result, rlock_result(x));3505}3506}35073508#ifdef ASSERT3509void LIRGenerator::do_Assert(Assert *x) {3510ValueTag tag = x->x()->type()->tag();3511If::Condition cond = x->cond();35123513LIRItem xitem(x->x(), this);3514LIRItem yitem(x->y(), this);3515LIRItem* xin = &xitem;3516LIRItem* yin = &yitem;35173518assert(tag == intTag, "Only integer assertions are valid!");35193520xin->load_item();3521yin->dont_load_item();35223523set_no_result(x);35243525LIR_Opr left = xin->result();3526LIR_Opr right = yin->result();35273528__ lir_assert(lir_cond(x->cond()), left, right, x->message(), true);3529}3530#endif35313532void LIRGenerator::do_RangeCheckPredicate(RangeCheckPredicate *x) {353335343535Instruction *a = x->x();3536Instruction *b = x->y();3537if (!a || StressRangeCheckElimination) {3538assert(!b || StressRangeCheckElimination, "B must also be null");35393540CodeEmitInfo *info = state_for(x, x->state());3541CodeStub* stub = new PredicateFailedStub(info);35423543__ jump(stub);3544} else if (a->type()->as_IntConstant() && b->type()->as_IntConstant()) {3545int a_int = a->type()->as_IntConstant()->value();3546int b_int = b->type()->as_IntConstant()->value();35473548bool ok = false;35493550switch(x->cond()) {3551case Instruction::eql: ok = (a_int == b_int); break;3552case Instruction::neq: ok = (a_int != b_int); break;3553case Instruction::lss: ok = (a_int < b_int); break;3554case Instruction::leq: ok = (a_int <= b_int); break;3555case Instruction::gtr: ok = (a_int > b_int); break;3556case Instruction::geq: ok = (a_int >= b_int); break;3557case Instruction::aeq: ok = ((unsigned int)a_int >= (unsigned int)b_int); break;3558case Instruction::beq: ok = ((unsigned int)a_int <= (unsigned int)b_int); break;3559default: ShouldNotReachHere();3560}35613562if (ok) {35633564CodeEmitInfo *info = state_for(x, x->state());3565CodeStub* stub = new PredicateFailedStub(info);35663567__ jump(stub);3568}3569} else {35703571ValueTag tag = x->x()->type()->tag();3572If::Condition cond = x->cond();3573LIRItem xitem(x->x(), this);3574LIRItem yitem(x->y(), this);3575LIRItem* xin = &xitem;3576LIRItem* yin = &yitem;35773578assert(tag == intTag, "Only integer deoptimizations are valid!");35793580xin->load_item();3581yin->dont_load_item();3582set_no_result(x);35833584LIR_Opr left = xin->result();3585LIR_Opr right = yin->result();35863587CodeEmitInfo *info = state_for(x, x->state());3588CodeStub* stub = new PredicateFailedStub(info);35893590__ cmp(lir_cond(cond), left, right);3591__ branch(lir_cond(cond), right->type(), stub);3592}3593}359435953596LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) {3597LIRItemList args(1);3598LIRItem value(arg1, this);3599args.append(&value);3600BasicTypeList signature;3601signature.append(as_BasicType(arg1->type()));36023603return call_runtime(&signature, &args, entry, result_type, info);3604}360536063607LIR_Opr LIRGenerator::call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info) {3608LIRItemList args(2);3609LIRItem value1(arg1, this);3610LIRItem value2(arg2, this);3611args.append(&value1);3612args.append(&value2);3613BasicTypeList signature;3614signature.append(as_BasicType(arg1->type()));3615signature.append(as_BasicType(arg2->type()));36163617return call_runtime(&signature, &args, entry, result_type, info);3618}361936203621LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIR_OprList* args,3622address entry, ValueType* result_type, CodeEmitInfo* info) {3623// get a result register3624LIR_Opr phys_reg = LIR_OprFact::illegalOpr;3625LIR_Opr result = LIR_OprFact::illegalOpr;3626if (result_type->tag() != voidTag) {3627result = new_register(result_type);3628phys_reg = result_register_for(result_type);3629}36303631// move the arguments into the correct location3632CallingConvention* cc = frame_map()->c_calling_convention(signature);3633assert(cc->length() == args->length(), "argument mismatch");3634for (int i = 0; i < args->length(); i++) {3635LIR_Opr arg = args->at(i);3636LIR_Opr loc = cc->at(i);3637if (loc->is_register()) {3638__ move(arg, loc);3639} else {3640LIR_Address* addr = loc->as_address_ptr();3641// if (!can_store_as_constant(arg)) {3642// LIR_Opr tmp = new_register(arg->type());3643// __ move(arg, tmp);3644// arg = tmp;3645// }3646if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {3647__ unaligned_move(arg, addr);3648} else {3649__ move(arg, addr);3650}3651}3652}36533654if (info) {3655__ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);3656} else {3657__ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());3658}3659if (result->is_valid()) {3660__ move(phys_reg, result);3661}3662return result;3663}366436653666LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIRItemList* args,3667address entry, ValueType* result_type, CodeEmitInfo* info) {3668// get a result register3669LIR_Opr phys_reg = LIR_OprFact::illegalOpr;3670LIR_Opr result = LIR_OprFact::illegalOpr;3671if (result_type->tag() != voidTag) {3672result = new_register(result_type);3673phys_reg = result_register_for(result_type);3674}36753676// move the arguments into the correct location3677CallingConvention* cc = frame_map()->c_calling_convention(signature);36783679assert(cc->length() == args->length(), "argument mismatch");3680for (int i = 0; i < args->length(); i++) {3681LIRItem* arg = args->at(i);3682LIR_Opr loc = cc->at(i);3683if (loc->is_register()) {3684arg->load_item_force(loc);3685} else {3686LIR_Address* addr = loc->as_address_ptr();3687arg->load_for_store(addr->type());3688if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {3689__ unaligned_move(arg->result(), addr);3690} else {3691__ move(arg->result(), addr);3692}3693}3694}36953696if (info) {3697__ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);3698} else {3699__ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());3700}3701if (result->is_valid()) {3702__ move(phys_reg, result);3703}3704return result;3705}37063707void LIRGenerator::do_MemBar(MemBar* x) {3708if (os::is_MP()) {3709LIR_Code code = x->code();3710switch(code) {3711case lir_membar_acquire : __ membar_acquire(); break;3712case lir_membar_release : __ membar_release(); break;3713case lir_membar : __ membar(); break;3714case lir_membar_loadload : __ membar_loadload(); break;3715case lir_membar_storestore: __ membar_storestore(); break;3716case lir_membar_loadstore : __ membar_loadstore(); break;3717case lir_membar_storeload : __ membar_storeload(); break;3718default : ShouldNotReachHere(); break;3719}3720}3721}37223723LIR_Opr LIRGenerator::maybe_mask_boolean(StoreIndexed* x, LIR_Opr array, LIR_Opr value, CodeEmitInfo*& null_check_info) {3724if (x->check_boolean()) {3725LIR_Opr value_fixed = rlock_byte(T_BYTE);3726if (TwoOperandLIRForm) {3727__ move(value, value_fixed);3728__ logical_and(value_fixed, LIR_OprFact::intConst(1), value_fixed);3729} else {3730__ logical_and(value, LIR_OprFact::intConst(1), value_fixed);3731}3732LIR_Opr klass = new_register(T_METADATA);3733__ move(new LIR_Address(array, oopDesc::klass_offset_in_bytes(), T_ADDRESS), klass, null_check_info);3734null_check_info = NULL;3735LIR_Opr layout = new_register(T_INT);3736__ move(new LIR_Address(klass, in_bytes(Klass::layout_helper_offset()), T_INT), layout);3737int diffbit = Klass::layout_helper_boolean_diffbit();3738__ logical_and(layout, LIR_OprFact::intConst(diffbit), layout);3739__ cmp(lir_cond_notEqual, layout, LIR_OprFact::intConst(0));3740__ cmove(lir_cond_notEqual, value_fixed, value, value_fixed, T_BYTE);3741value = value_fixed;3742}3743return value;3744}374537463747