Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/c1/c1_LinearScan.cpp
32285 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_CFGPrinter.hpp"26#include "c1/c1_CodeStubs.hpp"27#include "c1/c1_Compilation.hpp"28#include "c1/c1_FrameMap.hpp"29#include "c1/c1_IR.hpp"30#include "c1/c1_LIRGenerator.hpp"31#include "c1/c1_LinearScan.hpp"32#include "c1/c1_ValueStack.hpp"33#include "utilities/bitMap.inline.hpp"34#ifdef TARGET_ARCH_x8635# include "vmreg_x86.inline.hpp"36#endif37#ifdef TARGET_ARCH_aarch3238# include "vmreg_aarch32.inline.hpp"39# include "vm_version_aarch32.hpp"40#endif41#ifdef TARGET_ARCH_aarch6442# include "vmreg_aarch64.inline.hpp"43#endif44#ifdef TARGET_ARCH_sparc45# include "vmreg_sparc.inline.hpp"46#endif47#ifdef TARGET_ARCH_zero48# include "vmreg_zero.inline.hpp"49#endif50#ifdef TARGET_ARCH_arm51# include "vmreg_arm.inline.hpp"52#endif53#ifdef TARGET_ARCH_ppc54# include "vmreg_ppc.inline.hpp"55#endif565758#ifndef PRODUCT5960static LinearScanStatistic _stat_before_alloc;61static LinearScanStatistic _stat_after_asign;62static LinearScanStatistic _stat_final;6364static LinearScanTimers _total_timer;6566// helper macro for short definition of timer67#define TIME_LINEAR_SCAN(timer_name) TraceTime _block_timer("", _total_timer.timer(LinearScanTimers::timer_name), TimeLinearScan || TimeEachLinearScan, Verbose);6869// helper macro for short definition of trace-output inside code70#define TRACE_LINEAR_SCAN(level, code) \71if (TraceLinearScanLevel >= level) { \72code; \73}7475#else7677#define TIME_LINEAR_SCAN(timer_name)78#define TRACE_LINEAR_SCAN(level, code)7980#endif8182// Map BasicType to spill size in 32-bit words, matching VMReg's notion of words83#ifdef _LP6484static int type2spill_size[T_CONFLICT+1]={ -1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 0, 2, 1, 2, 1, -1};85#else86static int type2spill_size[T_CONFLICT+1]={ -1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 0, 1, -1, 1, 1, -1};87#endif888990// Implementation of LinearScan9192LinearScan::LinearScan(IR* ir, LIRGenerator* gen, FrameMap* frame_map)93: _compilation(ir->compilation())94, _ir(ir)95, _gen(gen)96, _frame_map(frame_map)97, _num_virtual_regs(gen->max_virtual_register_number())98, _has_fpu_registers(false)99, _num_calls(-1)100, _max_spills(0)101, _unused_spill_slot(-1)102, _intervals(0) // initialized later with correct length103, _new_intervals_from_allocation(new IntervalList())104, _sorted_intervals(NULL)105, _needs_full_resort(false)106, _lir_ops(0) // initialized later with correct length107, _block_of_op(0) // initialized later with correct length108, _has_info(0)109, _has_call(0)110, _scope_value_cache(0) // initialized later with correct length111, _interval_in_loop(0, 0) // initialized later with correct length112, _cached_blocks(*ir->linear_scan_order())113#ifdef X86114, _fpu_stack_allocator(NULL)115#endif116{117assert(this->ir() != NULL, "check if valid");118assert(this->compilation() != NULL, "check if valid");119assert(this->gen() != NULL, "check if valid");120assert(this->frame_map() != NULL, "check if valid");121}122123124// ********** functions for converting LIR-Operands to register numbers125//126// Emulate a flat register file comprising physical integer registers,127// physical floating-point registers and virtual registers, in that order.128// Virtual registers already have appropriate numbers, since V0 is129// the number of physical registers.130// Returns -1 for hi word if opr is a single word operand.131//132// Note: the inverse operation (calculating an operand for register numbers)133// is done in calc_operand_for_interval()134135int LinearScan::reg_num(LIR_Opr opr) {136assert(opr->is_register(), "should not call this otherwise");137138if (opr->is_virtual_register()) {139assert(opr->vreg_number() >= nof_regs, "found a virtual register with a fixed-register number");140return opr->vreg_number();141} else if (opr->is_single_cpu()) {142return opr->cpu_regnr();143} else if (opr->is_double_cpu()) {144return opr->cpu_regnrLo();145#ifdef X86146} else if (opr->is_single_xmm()) {147return opr->fpu_regnr() + pd_first_xmm_reg;148} else if (opr->is_double_xmm()) {149return opr->fpu_regnrLo() + pd_first_xmm_reg;150#endif151} else if (opr->is_single_fpu()) {152return opr->fpu_regnr() + pd_first_fpu_reg;153} else if (opr->is_double_fpu()) {154return opr->fpu_regnrLo() + pd_first_fpu_reg;155} else {156ShouldNotReachHere();157return -1;158}159}160161int LinearScan::reg_numHi(LIR_Opr opr) {162assert(opr->is_register(), "should not call this otherwise");163164if (opr->is_virtual_register()) {165return -1;166} else if (opr->is_single_cpu()) {167return -1;168} else if (opr->is_double_cpu()) {169return opr->cpu_regnrHi();170#ifdef X86171} else if (opr->is_single_xmm()) {172return -1;173} else if (opr->is_double_xmm()) {174return -1;175#endif176} else if (opr->is_single_fpu()) {177return -1;178} else if (opr->is_double_fpu()) {179return opr->fpu_regnrHi() + pd_first_fpu_reg;180} else {181ShouldNotReachHere();182return -1;183}184}185186187// ********** functions for classification of intervals188189bool LinearScan::is_precolored_interval(const Interval* i) {190return i->reg_num() < LinearScan::nof_regs;191}192193bool LinearScan::is_virtual_interval(const Interval* i) {194return i->reg_num() >= LIR_OprDesc::vreg_base;195}196197bool LinearScan::is_precolored_cpu_interval(const Interval* i) {198return i->reg_num() < LinearScan::nof_cpu_regs;199}200201bool LinearScan::is_virtual_cpu_interval(const Interval* i) {202#if !defined(AARCH32) && (defined(__SOFTFP__) || defined(E500V2))203return i->reg_num() >= LIR_OprDesc::vreg_base;204#else205return i->reg_num() >= LIR_OprDesc::vreg_base && (AARCH32_ONLY(!hasFPU() ||) (i->type() != T_FLOAT && i->type() != T_DOUBLE));206#endif // __SOFTFP__ or E500V2207}208209bool LinearScan::is_precolored_fpu_interval(const Interval* i) {210return i->reg_num() >= LinearScan::nof_cpu_regs && i->reg_num() < LinearScan::nof_regs;211}212213bool LinearScan::is_virtual_fpu_interval(const Interval* i) {214#if !defined(AARCH32) && (defined(__SOFTFP__) || defined(E500V2))215return false;216#else217return i->reg_num() >= LIR_OprDesc::vreg_base && (i->type() == T_FLOAT || i->type() == T_DOUBLE) AARCH32_ONLY(&& hasFPU());218#endif // __SOFTFP__ or E500V2219}220221bool LinearScan::is_in_fpu_register(const Interval* i) {222// fixed intervals not needed for FPU stack allocation223return i->reg_num() >= nof_regs && pd_first_fpu_reg <= i->assigned_reg() && i->assigned_reg() <= pd_last_fpu_reg;224}225226bool LinearScan::is_oop_interval(const Interval* i) {227// fixed intervals never contain oops228return i->reg_num() >= nof_regs && i->type() == T_OBJECT;229}230231232// ********** General helper functions233234// compute next unused stack index that can be used for spilling235int LinearScan::allocate_spill_slot(bool double_word) {236int spill_slot;237if (double_word) {238if ((_max_spills & 1) == 1) {239// alignment of double-word values240// the hole because of the alignment is filled with the next single-word value241assert(_unused_spill_slot == -1, "wasting a spill slot");242_unused_spill_slot = _max_spills;243_max_spills++;244}245spill_slot = _max_spills;246_max_spills += 2;247248} else if (_unused_spill_slot != -1) {249// re-use hole that was the result of a previous double-word alignment250spill_slot = _unused_spill_slot;251_unused_spill_slot = -1;252253} else {254spill_slot = _max_spills;255_max_spills++;256}257258int result = spill_slot + LinearScan::nof_regs + frame_map()->argcount();259260// the class OopMapValue uses only 11 bits for storing the name of the261// oop location. So a stack slot bigger than 2^11 leads to an overflow262// that is not reported in product builds. Prevent this by checking the263// spill slot here (altough this value and the later used location name264// are slightly different)265if (result > 2000) {266bailout("too many stack slots used");267}268269return result;270}271272void LinearScan::assign_spill_slot(Interval* it) {273// assign the canonical spill slot of the parent (if a part of the interval274// is already spilled) or allocate a new spill slot275if (it->canonical_spill_slot() >= 0) {276it->assign_reg(it->canonical_spill_slot());277} else {278int spill = allocate_spill_slot(type2spill_size[it->type()] == 2);279it->set_canonical_spill_slot(spill);280it->assign_reg(spill);281}282}283284void LinearScan::propagate_spill_slots() {285if (!frame_map()->finalize_frame(max_spills())) {286bailout("frame too large");287}288}289290// create a new interval with a predefined reg_num291// (only used for parent intervals that are created during the building phase)292Interval* LinearScan::create_interval(int reg_num) {293assert(_intervals.at(reg_num) == NULL, "overwriting exisiting interval");294295Interval* interval = new Interval(reg_num);296_intervals.at_put(reg_num, interval);297298// assign register number for precolored intervals299if (reg_num < LIR_OprDesc::vreg_base) {300interval->assign_reg(reg_num);301}302return interval;303}304305// assign a new reg_num to the interval and append it to the list of intervals306// (only used for child intervals that are created during register allocation)307void LinearScan::append_interval(Interval* it) {308it->set_reg_num(_intervals.length());309_intervals.append(it);310_new_intervals_from_allocation->append(it);311}312313// copy the vreg-flags if an interval is split314void LinearScan::copy_register_flags(Interval* from, Interval* to) {315if (gen()->is_vreg_flag_set(from->reg_num(), LIRGenerator::byte_reg)) {316gen()->set_vreg_flag(to->reg_num(), LIRGenerator::byte_reg);317}318if (gen()->is_vreg_flag_set(from->reg_num(), LIRGenerator::callee_saved)) {319gen()->set_vreg_flag(to->reg_num(), LIRGenerator::callee_saved);320}321322// Note: do not copy the must_start_in_memory flag because it is not necessary for child323// intervals (only the very beginning of the interval must be in memory)324}325326327// ********** spill move optimization328// eliminate moves from register to stack if stack slot is known to be correct329330// called during building of intervals331void LinearScan::change_spill_definition_pos(Interval* interval, int def_pos) {332assert(interval->is_split_parent(), "can only be called for split parents");333334switch (interval->spill_state()) {335case noDefinitionFound:336assert(interval->spill_definition_pos() == -1, "must no be set before");337interval->set_spill_definition_pos(def_pos);338interval->set_spill_state(oneDefinitionFound);339break;340341case oneDefinitionFound:342assert(def_pos <= interval->spill_definition_pos(), "positions are processed in reverse order when intervals are created");343if (def_pos < interval->spill_definition_pos() - 2) {344// second definition found, so no spill optimization possible for this interval345interval->set_spill_state(noOptimization);346} else {347// two consecutive definitions (because of two-operand LIR form)348assert(block_of_op_with_id(def_pos) == block_of_op_with_id(interval->spill_definition_pos()), "block must be equal");349}350break;351352case noOptimization:353// nothing to do354break;355356default:357assert(false, "other states not allowed at this time");358}359}360361// called during register allocation362void LinearScan::change_spill_state(Interval* interval, int spill_pos) {363switch (interval->spill_state()) {364case oneDefinitionFound: {365int def_loop_depth = block_of_op_with_id(interval->spill_definition_pos())->loop_depth();366int spill_loop_depth = block_of_op_with_id(spill_pos)->loop_depth();367368if (def_loop_depth < spill_loop_depth) {369// the loop depth of the spilling position is higher then the loop depth370// at the definition of the interval -> move write to memory out of loop371// by storing at definitin of the interval372interval->set_spill_state(storeAtDefinition);373} else {374// the interval is currently spilled only once, so for now there is no375// reason to store the interval at the definition376interval->set_spill_state(oneMoveInserted);377}378break;379}380381case oneMoveInserted: {382// the interval is spilled more then once, so it is better to store it to383// memory at the definition384interval->set_spill_state(storeAtDefinition);385break;386}387388case storeAtDefinition:389case startInMemory:390case noOptimization:391case noDefinitionFound:392// nothing to do393break;394395default:396assert(false, "other states not allowed at this time");397}398}399400401bool LinearScan::must_store_at_definition(const Interval* i) {402return i->is_split_parent() && i->spill_state() == storeAtDefinition;403}404405// called once before asignment of register numbers406void LinearScan::eliminate_spill_moves() {407TIME_LINEAR_SCAN(timer_eliminate_spill_moves);408TRACE_LINEAR_SCAN(3, tty->print_cr("***** Eliminating unnecessary spill moves"));409410// collect all intervals that must be stored after their definion.411// the list is sorted by Interval::spill_definition_pos412Interval* interval;413Interval* temp_list;414create_unhandled_lists(&interval, &temp_list, must_store_at_definition, NULL);415416#ifdef ASSERT417Interval* prev = NULL;418Interval* temp = interval;419while (temp != Interval::end()) {420assert(temp->spill_definition_pos() > 0, "invalid spill definition pos");421if (prev != NULL) {422assert(temp->from() >= prev->from(), "intervals not sorted");423assert(temp->spill_definition_pos() >= prev->spill_definition_pos(), "when intervals are sorted by from, then they must also be sorted by spill_definition_pos");424}425426assert(temp->canonical_spill_slot() >= LinearScan::nof_regs, "interval has no spill slot assigned");427assert(temp->spill_definition_pos() >= temp->from(), "invalid order");428assert(temp->spill_definition_pos() <= temp->from() + 2, "only intervals defined once at their start-pos can be optimized");429430TRACE_LINEAR_SCAN(4, tty->print_cr("interval %d (from %d to %d) must be stored at %d", temp->reg_num(), temp->from(), temp->to(), temp->spill_definition_pos()));431432temp = temp->next();433}434#endif435436LIR_InsertionBuffer insertion_buffer;437int num_blocks = block_count();438for (int i = 0; i < num_blocks; i++) {439BlockBegin* block = block_at(i);440LIR_OpList* instructions = block->lir()->instructions_list();441int num_inst = instructions->length();442bool has_new = false;443444// iterate all instructions of the block. skip the first because it is always a label445for (int j = 1; j < num_inst; j++) {446LIR_Op* op = instructions->at(j);447int op_id = op->id();448449if (op_id == -1) {450// remove move from register to stack if the stack slot is guaranteed to be correct.451// only moves that have been inserted by LinearScan can be removed.452assert(op->code() == lir_move, "only moves can have a op_id of -1");453assert(op->as_Op1() != NULL, "move must be LIR_Op1");454assert(op->as_Op1()->result_opr()->is_virtual(), "LinearScan inserts only moves to virtual registers");455456LIR_Op1* op1 = (LIR_Op1*)op;457Interval* interval = interval_at(op1->result_opr()->vreg_number());458459if (interval->assigned_reg() >= LinearScan::nof_regs && interval->always_in_memory()) {460// move target is a stack slot that is always correct, so eliminate instruction461TRACE_LINEAR_SCAN(4, tty->print_cr("eliminating move from interval %d to %d", op1->in_opr()->vreg_number(), op1->result_opr()->vreg_number()));462instructions->at_put(j, NULL); // NULL-instructions are deleted by assign_reg_num463}464465} else {466// insert move from register to stack just after the beginning of the interval467assert(interval == Interval::end() || interval->spill_definition_pos() >= op_id, "invalid order");468assert(interval == Interval::end() || (interval->is_split_parent() && interval->spill_state() == storeAtDefinition), "invalid interval");469470while (interval != Interval::end() && interval->spill_definition_pos() == op_id) {471if (!has_new) {472// prepare insertion buffer (appended when all instructions of the block are processed)473insertion_buffer.init(block->lir());474has_new = true;475}476477LIR_Opr from_opr = operand_for_interval(interval);478LIR_Opr to_opr = canonical_spill_opr(interval);479assert(from_opr->is_fixed_cpu() || from_opr->is_fixed_fpu(), "from operand must be a register");480assert(to_opr->is_stack(), "to operand must be a stack slot");481482insertion_buffer.move(j, from_opr, to_opr);483TRACE_LINEAR_SCAN(4, tty->print_cr("inserting move after definition of interval %d to stack slot %d at op_id %d", interval->reg_num(), interval->canonical_spill_slot() - LinearScan::nof_regs, op_id));484485interval = interval->next();486}487}488} // end of instruction iteration489490if (has_new) {491block->lir()->append(&insertion_buffer);492}493} // end of block iteration494495assert(interval == Interval::end(), "missed an interval");496}497498499// ********** Phase 1: number all instructions in all blocks500// Compute depth-first and linear scan block orders, and number LIR_Op nodes for linear scan.501502void LinearScan::number_instructions() {503{504// dummy-timer to measure the cost of the timer itself505// (this time is then subtracted from all other timers to get the real value)506TIME_LINEAR_SCAN(timer_do_nothing);507}508TIME_LINEAR_SCAN(timer_number_instructions);509510// Assign IDs to LIR nodes and build a mapping, lir_ops, from ID to LIR_Op node.511int num_blocks = block_count();512int num_instructions = 0;513int i;514for (i = 0; i < num_blocks; i++) {515num_instructions += block_at(i)->lir()->instructions_list()->length();516}517518// initialize with correct length519_lir_ops = LIR_OpArray(num_instructions);520_block_of_op = BlockBeginArray(num_instructions);521522int op_id = 0;523int idx = 0;524525for (i = 0; i < num_blocks; i++) {526BlockBegin* block = block_at(i);527block->set_first_lir_instruction_id(op_id);528LIR_OpList* instructions = block->lir()->instructions_list();529530int num_inst = instructions->length();531for (int j = 0; j < num_inst; j++) {532LIR_Op* op = instructions->at(j);533op->set_id(op_id);534535_lir_ops.at_put(idx, op);536_block_of_op.at_put(idx, block);537assert(lir_op_with_id(op_id) == op, "must match");538539idx++;540op_id += 2; // numbering of lir_ops by two541}542block->set_last_lir_instruction_id(op_id - 2);543}544assert(idx == num_instructions, "must match");545assert(idx * 2 == op_id, "must match");546547_has_call = BitMap(num_instructions); _has_call.clear();548_has_info = BitMap(num_instructions); _has_info.clear();549}550551552// ********** Phase 2: compute local live sets separately for each block553// (sets live_gen and live_kill for each block)554555void LinearScan::set_live_gen_kill(Value value, LIR_Op* op, BitMap& live_gen, BitMap& live_kill) {556LIR_Opr opr = value->operand();557Constant* con = value->as_Constant();558559// check some asumptions about debug information560assert(!value->type()->is_illegal(), "if this local is used by the interpreter it shouldn't be of indeterminate type");561assert(con == NULL || opr->is_virtual() || opr->is_constant() || opr->is_illegal(), "asumption: Constant instructions have only constant operands");562assert(con != NULL || opr->is_virtual(), "asumption: non-Constant instructions have only virtual operands");563564if ((con == NULL || con->is_pinned()) && opr->is_register()) {565assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");566int reg = opr->vreg_number();567if (!live_kill.at(reg)) {568live_gen.set_bit(reg);569TRACE_LINEAR_SCAN(4, tty->print_cr(" Setting live_gen for value %c%d, LIR op_id %d, register number %d", value->type()->tchar(), value->id(), op->id(), reg));570}571}572}573574575void LinearScan::compute_local_live_sets() {576TIME_LINEAR_SCAN(timer_compute_local_live_sets);577578int num_blocks = block_count();579int live_size = live_set_size();580bool local_has_fpu_registers = false;581int local_num_calls = 0;582LIR_OpVisitState visitor;583584BitMap2D local_interval_in_loop = BitMap2D(_num_virtual_regs, num_loops());585local_interval_in_loop.clear();586587// iterate all blocks588for (int i = 0; i < num_blocks; i++) {589BlockBegin* block = block_at(i);590591BitMap live_gen(live_size); live_gen.clear();592BitMap live_kill(live_size); live_kill.clear();593594if (block->is_set(BlockBegin::exception_entry_flag)) {595// Phi functions at the begin of an exception handler are596// implicitly defined (= killed) at the beginning of the block.597for_each_phi_fun(block, phi,598live_kill.set_bit(phi->operand()->vreg_number())599);600}601602LIR_OpList* instructions = block->lir()->instructions_list();603int num_inst = instructions->length();604605// iterate all instructions of the block. skip the first because it is always a label606assert(visitor.no_operands(instructions->at(0)), "first operation must always be a label");607for (int j = 1; j < num_inst; j++) {608LIR_Op* op = instructions->at(j);609610// visit operation to collect all operands611visitor.visit(op);612613if (visitor.has_call()) {614_has_call.set_bit(op->id() >> 1);615local_num_calls++;616}617if (visitor.info_count() > 0) {618_has_info.set_bit(op->id() >> 1);619}620621// iterate input operands of instruction622int k, n, reg;623n = visitor.opr_count(LIR_OpVisitState::inputMode);624for (k = 0; k < n; k++) {625LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::inputMode, k);626assert(opr->is_register(), "visitor should only return register operands");627628if (opr->is_virtual_register()) {629assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");630reg = opr->vreg_number();631if (!live_kill.at(reg)) {632live_gen.set_bit(reg);633TRACE_LINEAR_SCAN(4, tty->print_cr(" Setting live_gen for register %d at instruction %d", reg, op->id()));634}635if (block->loop_index() >= 0) {636local_interval_in_loop.set_bit(reg, block->loop_index());637}638local_has_fpu_registers = local_has_fpu_registers || opr->is_virtual_fpu();639}640641#ifdef ASSERT642// fixed intervals are never live at block boundaries, so643// they need not be processed in live sets.644// this is checked by these assertions to be sure about it.645// the entry block may have incoming values in registers, which is ok.646if (!opr->is_virtual_register() && block != ir()->start()) {647reg = reg_num(opr);648if (is_processed_reg_num(reg)) {649assert(live_kill.at(reg), "using fixed register that is not defined in this block");650}651reg = reg_numHi(opr);652if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {653assert(live_kill.at(reg), "using fixed register that is not defined in this block");654}655}656#endif657}658659// Add uses of live locals from interpreter's point of view for proper debug information generation660n = visitor.info_count();661for (k = 0; k < n; k++) {662CodeEmitInfo* info = visitor.info_at(k);663ValueStack* stack = info->stack();664for_each_state_value(stack, value,665set_live_gen_kill(value, op, live_gen, live_kill)666);667}668669// iterate temp operands of instruction670n = visitor.opr_count(LIR_OpVisitState::tempMode);671for (k = 0; k < n; k++) {672LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::tempMode, k);673assert(opr->is_register(), "visitor should only return register operands");674675if (opr->is_virtual_register()) {676assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");677reg = opr->vreg_number();678live_kill.set_bit(reg);679if (block->loop_index() >= 0) {680local_interval_in_loop.set_bit(reg, block->loop_index());681}682local_has_fpu_registers = local_has_fpu_registers || opr->is_virtual_fpu();683}684685#ifdef ASSERT686// fixed intervals are never live at block boundaries, so687// they need not be processed in live sets688// process them only in debug mode so that this can be checked689if (!opr->is_virtual_register()) {690reg = reg_num(opr);691if (is_processed_reg_num(reg)) {692live_kill.set_bit(reg_num(opr));693}694reg = reg_numHi(opr);695if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {696live_kill.set_bit(reg);697}698}699#endif700}701702// iterate output operands of instruction703n = visitor.opr_count(LIR_OpVisitState::outputMode);704for (k = 0; k < n; k++) {705LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::outputMode, k);706assert(opr->is_register(), "visitor should only return register operands");707708if (opr->is_virtual_register()) {709assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");710reg = opr->vreg_number();711live_kill.set_bit(reg);712if (block->loop_index() >= 0) {713local_interval_in_loop.set_bit(reg, block->loop_index());714}715local_has_fpu_registers = local_has_fpu_registers || opr->is_virtual_fpu();716}717718#ifdef ASSERT719// fixed intervals are never live at block boundaries, so720// they need not be processed in live sets721// process them only in debug mode so that this can be checked722if (!opr->is_virtual_register()) {723reg = reg_num(opr);724if (is_processed_reg_num(reg)) {725live_kill.set_bit(reg_num(opr));726}727reg = reg_numHi(opr);728if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {729live_kill.set_bit(reg);730}731}732#endif733}734} // end of instruction iteration735736block->set_live_gen (live_gen);737block->set_live_kill(live_kill);738block->set_live_in (BitMap(live_size)); block->live_in().clear();739block->set_live_out (BitMap(live_size)); block->live_out().clear();740741TRACE_LINEAR_SCAN(4, tty->print("live_gen B%d ", block->block_id()); print_bitmap(block->live_gen()));742TRACE_LINEAR_SCAN(4, tty->print("live_kill B%d ", block->block_id()); print_bitmap(block->live_kill()));743} // end of block iteration744745// propagate local calculated information into LinearScan object746_has_fpu_registers = local_has_fpu_registers;747compilation()->set_has_fpu_code(local_has_fpu_registers);748749_num_calls = local_num_calls;750_interval_in_loop = local_interval_in_loop;751}752753754// ********** Phase 3: perform a backward dataflow analysis to compute global live sets755// (sets live_in and live_out for each block)756757void LinearScan::compute_global_live_sets() {758TIME_LINEAR_SCAN(timer_compute_global_live_sets);759760int num_blocks = block_count();761bool change_occurred;762bool change_occurred_in_block;763int iteration_count = 0;764BitMap live_out(live_set_size()); live_out.clear(); // scratch set for calculations765766// Perform a backward dataflow analysis to compute live_out and live_in for each block.767// The loop is executed until a fixpoint is reached (no changes in an iteration)768// Exception handlers must be processed because not all live values are769// present in the state array, e.g. because of global value numbering770do {771change_occurred = false;772773// iterate all blocks in reverse order774for (int i = num_blocks - 1; i >= 0; i--) {775BlockBegin* block = block_at(i);776777change_occurred_in_block = false;778779// live_out(block) is the union of live_in(sux), for successors sux of block780int n = block->number_of_sux();781int e = block->number_of_exception_handlers();782if (n + e > 0) {783// block has successors784if (n > 0) {785live_out.set_from(block->sux_at(0)->live_in());786for (int j = 1; j < n; j++) {787live_out.set_union(block->sux_at(j)->live_in());788}789} else {790live_out.clear();791}792for (int j = 0; j < e; j++) {793live_out.set_union(block->exception_handler_at(j)->live_in());794}795796if (!block->live_out().is_same(live_out)) {797// A change occurred. Swap the old and new live out sets to avoid copying.798BitMap temp = block->live_out();799block->set_live_out(live_out);800live_out = temp;801802change_occurred = true;803change_occurred_in_block = true;804}805}806807if (iteration_count == 0 || change_occurred_in_block) {808// live_in(block) is the union of live_gen(block) with (live_out(block) & !live_kill(block))809// note: live_in has to be computed only in first iteration or if live_out has changed!810BitMap live_in = block->live_in();811live_in.set_from(block->live_out());812live_in.set_difference(block->live_kill());813live_in.set_union(block->live_gen());814}815816#ifndef PRODUCT817if (TraceLinearScanLevel >= 4) {818char c = ' ';819if (iteration_count == 0 || change_occurred_in_block) {820c = '*';821}822tty->print("(%d) live_in%c B%d ", iteration_count, c, block->block_id()); print_bitmap(block->live_in());823tty->print("(%d) live_out%c B%d ", iteration_count, c, block->block_id()); print_bitmap(block->live_out());824}825#endif826}827iteration_count++;828829if (change_occurred && iteration_count > 50) {830BAILOUT("too many iterations in compute_global_live_sets");831}832} while (change_occurred);833834835#ifdef ASSERT836// check that fixed intervals are not live at block boundaries837// (live set must be empty at fixed intervals)838for (int i = 0; i < num_blocks; i++) {839BlockBegin* block = block_at(i);840for (int j = 0; j < LIR_OprDesc::vreg_base; j++) {841assert(block->live_in().at(j) == false, "live_in set of fixed register must be empty");842assert(block->live_out().at(j) == false, "live_out set of fixed register must be empty");843assert(block->live_gen().at(j) == false, "live_gen set of fixed register must be empty");844}845}846#endif847848// check that the live_in set of the first block is empty849BitMap live_in_args(ir()->start()->live_in().size());850live_in_args.clear();851if (!ir()->start()->live_in().is_same(live_in_args)) {852#ifdef ASSERT853tty->print_cr("Error: live_in set of first block must be empty (when this fails, virtual registers are used before they are defined)");854tty->print_cr("affected registers:");855print_bitmap(ir()->start()->live_in());856857// print some additional information to simplify debugging858for (unsigned int i = 0; i < ir()->start()->live_in().size(); i++) {859if (ir()->start()->live_in().at(i)) {860Instruction* instr = gen()->instruction_for_vreg(i);861tty->print_cr("* vreg %d (HIR instruction %c%d)", i, instr == NULL ? ' ' : instr->type()->tchar(), instr == NULL ? 0 : instr->id());862863for (int j = 0; j < num_blocks; j++) {864BlockBegin* block = block_at(j);865if (block->live_gen().at(i)) {866tty->print_cr(" used in block B%d", block->block_id());867}868if (block->live_kill().at(i)) {869tty->print_cr(" defined in block B%d", block->block_id());870}871}872}873}874875#endif876// when this fails, virtual registers are used before they are defined.877assert(false, "live_in set of first block must be empty");878// bailout of if this occurs in product mode.879bailout("live_in set of first block not empty");880}881}882883884// ********** Phase 4: build intervals885// (fills the list _intervals)886887void LinearScan::add_use(Value value, int from, int to, IntervalUseKind use_kind) {888assert(!value->type()->is_illegal(), "if this value is used by the interpreter it shouldn't be of indeterminate type");889LIR_Opr opr = value->operand();890Constant* con = value->as_Constant();891892if ((con == NULL || con->is_pinned()) && opr->is_register()) {893assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");894add_use(opr, from, to, use_kind);895}896}897898899void LinearScan::add_def(LIR_Opr opr, int def_pos, IntervalUseKind use_kind) {900TRACE_LINEAR_SCAN(2, tty->print(" def "); opr->print(tty); tty->print_cr(" def_pos %d (%d)", def_pos, use_kind));901assert(opr->is_register(), "should not be called otherwise");902903if (opr->is_virtual_register()) {904assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");905add_def(opr->vreg_number(), def_pos, use_kind, opr->type_register());906907} else {908int reg = reg_num(opr);909if (is_processed_reg_num(reg)) {910add_def(reg, def_pos, use_kind, opr->type_register());911}912reg = reg_numHi(opr);913if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {914add_def(reg, def_pos, use_kind, opr->type_register());915}916}917}918919void LinearScan::add_use(LIR_Opr opr, int from, int to, IntervalUseKind use_kind) {920TRACE_LINEAR_SCAN(2, tty->print(" use "); opr->print(tty); tty->print_cr(" from %d to %d (%d)", from, to, use_kind));921assert(opr->is_register(), "should not be called otherwise");922923if (opr->is_virtual_register()) {924assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");925add_use(opr->vreg_number(), from, to, use_kind, opr->type_register());926927} else {928int reg = reg_num(opr);929if (is_processed_reg_num(reg)) {930add_use(reg, from, to, use_kind, opr->type_register());931}932reg = reg_numHi(opr);933if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {934add_use(reg, from, to, use_kind, opr->type_register());935}936}937}938939void LinearScan::add_temp(LIR_Opr opr, int temp_pos, IntervalUseKind use_kind) {940TRACE_LINEAR_SCAN(2, tty->print(" temp "); opr->print(tty); tty->print_cr(" temp_pos %d (%d)", temp_pos, use_kind));941assert(opr->is_register(), "should not be called otherwise");942943if (opr->is_virtual_register()) {944assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");945add_temp(opr->vreg_number(), temp_pos, use_kind, opr->type_register());946947} else {948int reg = reg_num(opr);949if (is_processed_reg_num(reg)) {950add_temp(reg, temp_pos, use_kind, opr->type_register());951}952reg = reg_numHi(opr);953if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {954add_temp(reg, temp_pos, use_kind, opr->type_register());955}956}957}958959960void LinearScan::add_def(int reg_num, int def_pos, IntervalUseKind use_kind, BasicType type) {961Interval* interval = interval_at(reg_num);962if (interval != NULL) {963assert(interval->reg_num() == reg_num, "wrong interval");964965if (type != T_ILLEGAL) {966interval->set_type(type);967}968969Range* r = interval->first();970if (r->from() <= def_pos) {971// Update the starting point (when a range is first created for a use, its972// start is the beginning of the current block until a def is encountered.)973r->set_from(def_pos);974interval->add_use_pos(def_pos, use_kind);975976} else {977// Dead value - make vacuous interval978// also add use_kind for dead intervals979interval->add_range(def_pos, def_pos + 1);980interval->add_use_pos(def_pos, use_kind);981TRACE_LINEAR_SCAN(2, tty->print_cr("Warning: def of reg %d at %d occurs without use", reg_num, def_pos));982}983984} else {985// Dead value - make vacuous interval986// also add use_kind for dead intervals987interval = create_interval(reg_num);988if (type != T_ILLEGAL) {989interval->set_type(type);990}991992interval->add_range(def_pos, def_pos + 1);993interval->add_use_pos(def_pos, use_kind);994TRACE_LINEAR_SCAN(2, tty->print_cr("Warning: dead value %d at %d in live intervals", reg_num, def_pos));995}996997change_spill_definition_pos(interval, def_pos);998if (use_kind == noUse && interval->spill_state() <= startInMemory) {999// detection of method-parameters and roundfp-results1000// TODO: move this directly to position where use-kind is computed1001interval->set_spill_state(startInMemory);1002}1003}10041005void LinearScan::add_use(int reg_num, int from, int to, IntervalUseKind use_kind, BasicType type) {1006Interval* interval = interval_at(reg_num);1007if (interval == NULL) {1008interval = create_interval(reg_num);1009}1010assert(interval->reg_num() == reg_num, "wrong interval");10111012if (type != T_ILLEGAL) {1013interval->set_type(type);1014}10151016interval->add_range(from, to);1017interval->add_use_pos(to, use_kind);1018}10191020void LinearScan::add_temp(int reg_num, int temp_pos, IntervalUseKind use_kind, BasicType type) {1021Interval* interval = interval_at(reg_num);1022if (interval == NULL) {1023interval = create_interval(reg_num);1024}1025assert(interval->reg_num() == reg_num, "wrong interval");10261027if (type != T_ILLEGAL) {1028interval->set_type(type);1029}10301031interval->add_range(temp_pos, temp_pos + 1);1032interval->add_use_pos(temp_pos, use_kind);1033}103410351036// the results of this functions are used for optimizing spilling and reloading1037// if the functions return shouldHaveRegister and the interval is spilled,1038// it is not reloaded to a register.1039IntervalUseKind LinearScan::use_kind_of_output_operand(LIR_Op* op, LIR_Opr opr) {1040if (op->code() == lir_move) {1041assert(op->as_Op1() != NULL, "lir_move must be LIR_Op1");1042LIR_Op1* move = (LIR_Op1*)op;1043LIR_Opr res = move->result_opr();1044bool result_in_memory = res->is_virtual() && gen()->is_vreg_flag_set(res->vreg_number(), LIRGenerator::must_start_in_memory);10451046if (result_in_memory) {1047// Begin of an interval with must_start_in_memory set.1048// This interval will always get a stack slot first, so return noUse.1049return noUse;10501051} else if (move->in_opr()->is_stack()) {1052// method argument (condition must be equal to handle_method_arguments)1053return noUse;10541055} else if (move->in_opr()->is_register() && move->result_opr()->is_register()) {1056// Move from register to register1057if (block_of_op_with_id(op->id())->is_set(BlockBegin::osr_entry_flag)) {1058// special handling of phi-function moves inside osr-entry blocks1059// input operand must have a register instead of output operand (leads to better register allocation)1060return shouldHaveRegister;1061}1062}1063}10641065if (opr->is_virtual() &&1066gen()->is_vreg_flag_set(opr->vreg_number(), LIRGenerator::must_start_in_memory)) {1067// result is a stack-slot, so prevent immediate reloading1068return noUse;1069}10701071// all other operands require a register1072return mustHaveRegister;1073}10741075IntervalUseKind LinearScan::use_kind_of_input_operand(LIR_Op* op, LIR_Opr opr) {1076if (op->code() == lir_move) {1077assert(op->as_Op1() != NULL, "lir_move must be LIR_Op1");1078LIR_Op1* move = (LIR_Op1*)op;1079LIR_Opr res = move->result_opr();1080bool result_in_memory = res->is_virtual() && gen()->is_vreg_flag_set(res->vreg_number(), LIRGenerator::must_start_in_memory);10811082if (result_in_memory) {1083// Move to an interval with must_start_in_memory set.1084// To avoid moves from stack to stack (not allowed) force the input operand to a register1085return mustHaveRegister;10861087} else if (move->in_opr()->is_register() && move->result_opr()->is_register()) {1088// Move from register to register1089if (block_of_op_with_id(op->id())->is_set(BlockBegin::osr_entry_flag)) {1090// special handling of phi-function moves inside osr-entry blocks1091// input operand must have a register instead of output operand (leads to better register allocation)1092return mustHaveRegister;1093}10941095// The input operand is not forced to a register (moves from stack to register are allowed),1096// but it is faster if the input operand is in a register1097return shouldHaveRegister;1098}1099}110011011102#if defined(X86)1103if (op->code() == lir_cmove) {1104// conditional moves can handle stack operands1105assert(op->result_opr()->is_register(), "result must always be in a register");1106return shouldHaveRegister;1107}11081109// optimizations for second input operand of arithmehtic operations on Intel1110// this operand is allowed to be on the stack in some cases1111BasicType opr_type = opr->type_register();1112if (opr_type == T_FLOAT || opr_type == T_DOUBLE) {1113if ((UseSSE == 1 && opr_type == T_FLOAT) || UseSSE >= 2) {1114// SSE float instruction (T_DOUBLE only supported with SSE2)1115switch (op->code()) {1116case lir_cmp:1117case lir_add:1118case lir_sub:1119case lir_mul:1120case lir_div:1121{1122assert(op->as_Op2() != NULL, "must be LIR_Op2");1123LIR_Op2* op2 = (LIR_Op2*)op;1124if (op2->in_opr1() != op2->in_opr2() && op2->in_opr2() == opr) {1125assert((op2->result_opr()->is_register() || op->code() == lir_cmp) && op2->in_opr1()->is_register(), "cannot mark second operand as stack if others are not in register");1126return shouldHaveRegister;1127}1128}1129}1130} else {1131// FPU stack float instruction1132switch (op->code()) {1133case lir_add:1134case lir_sub:1135case lir_mul:1136case lir_div:1137{1138assert(op->as_Op2() != NULL, "must be LIR_Op2");1139LIR_Op2* op2 = (LIR_Op2*)op;1140if (op2->in_opr1() != op2->in_opr2() && op2->in_opr2() == opr) {1141assert((op2->result_opr()->is_register() || op->code() == lir_cmp) && op2->in_opr1()->is_register(), "cannot mark second operand as stack if others are not in register");1142return shouldHaveRegister;1143}1144}1145}1146}1147// We want to sometimes use logical operations on pointers, in particular in GC barriers.1148// Since 64bit logical operations do not current support operands on stack, we have to make sure1149// T_OBJECT doesn't get spilled along with T_LONG.1150} else if (opr_type != T_LONG LP64_ONLY(&& opr_type != T_OBJECT)) {1151// integer instruction (note: long operands must always be in register)1152switch (op->code()) {1153case lir_cmp:1154case lir_add:1155case lir_sub:1156case lir_logic_and:1157case lir_logic_or:1158case lir_logic_xor:1159{1160assert(op->as_Op2() != NULL, "must be LIR_Op2");1161LIR_Op2* op2 = (LIR_Op2*)op;1162if (op2->in_opr1() != op2->in_opr2() && op2->in_opr2() == opr) {1163assert((op2->result_opr()->is_register() || op->code() == lir_cmp) && op2->in_opr1()->is_register(), "cannot mark second operand as stack if others are not in register");1164return shouldHaveRegister;1165}1166}1167}1168}1169#endif // X8611701171// all other operands require a register1172return mustHaveRegister;1173}117411751176void LinearScan::handle_method_arguments(LIR_Op* op) {1177// special handling for method arguments (moves from stack to virtual register):1178// the interval gets no register assigned, but the stack slot.1179// it is split before the first use by the register allocator.11801181if (op->code() == lir_move) {1182assert(op->as_Op1() != NULL, "must be LIR_Op1");1183LIR_Op1* move = (LIR_Op1*)op;11841185if (move->in_opr()->is_stack()) {1186#ifdef ASSERT1187int arg_size = compilation()->method()->arg_size();1188LIR_Opr o = move->in_opr();1189if (o->is_single_stack()) {1190assert(o->single_stack_ix() >= 0 && o->single_stack_ix() < arg_size, "out of range");1191} else if (o->is_double_stack()) {1192assert(o->double_stack_ix() >= 0 && o->double_stack_ix() < arg_size, "out of range");1193} else {1194ShouldNotReachHere();1195}11961197assert(move->id() > 0, "invalid id");1198assert(block_of_op_with_id(move->id())->number_of_preds() == 0, "move from stack must be in first block");1199assert(move->result_opr()->is_virtual(), "result of move must be a virtual register");12001201TRACE_LINEAR_SCAN(4, tty->print_cr("found move from stack slot %d to vreg %d", o->is_single_stack() ? o->single_stack_ix() : o->double_stack_ix(), reg_num(move->result_opr())));1202#endif12031204Interval* interval = interval_at(reg_num(move->result_opr()));12051206int stack_slot = LinearScan::nof_regs + (move->in_opr()->is_single_stack() ? move->in_opr()->single_stack_ix() : move->in_opr()->double_stack_ix());1207interval->set_canonical_spill_slot(stack_slot);1208interval->assign_reg(stack_slot);1209}1210}1211}12121213void LinearScan::handle_doubleword_moves(LIR_Op* op) {1214// special handling for doubleword move from memory to register:1215// in this case the registers of the input address and the result1216// registers must not overlap -> add a temp range for the input registers1217if (op->code() == lir_move) {1218assert(op->as_Op1() != NULL, "must be LIR_Op1");1219LIR_Op1* move = (LIR_Op1*)op;12201221if (move->result_opr()->is_double_cpu() && move->in_opr()->is_pointer()) {1222LIR_Address* address = move->in_opr()->as_address_ptr();1223if (address != NULL) {1224if (address->base()->is_valid()) {1225add_temp(address->base(), op->id(), noUse);1226}1227if (address->index()->is_valid()) {1228add_temp(address->index(), op->id(), noUse);1229}1230}1231}1232}1233}12341235void LinearScan::add_register_hints(LIR_Op* op) {1236switch (op->code()) {1237case lir_move: // fall through1238case lir_convert: {1239assert(op->as_Op1() != NULL, "lir_move, lir_convert must be LIR_Op1");1240LIR_Op1* move = (LIR_Op1*)op;12411242LIR_Opr move_from = move->in_opr();1243LIR_Opr move_to = move->result_opr();12441245if (move_to->is_register() && move_from->is_register()) {1246Interval* from = interval_at(reg_num(move_from));1247Interval* to = interval_at(reg_num(move_to));1248if (from != NULL && to != NULL) {1249to->set_register_hint(from);1250TRACE_LINEAR_SCAN(4, tty->print_cr("operation at op_id %d: added hint from interval %d to %d", move->id(), from->reg_num(), to->reg_num()));1251}1252}1253break;1254}1255case lir_cmove: {1256assert(op->as_Op2() != NULL, "lir_cmove must be LIR_Op2");1257LIR_Op2* cmove = (LIR_Op2*)op;12581259LIR_Opr move_from = cmove->in_opr1();1260LIR_Opr move_to = cmove->result_opr();12611262if (move_to->is_register() && move_from->is_register()) {1263Interval* from = interval_at(reg_num(move_from));1264Interval* to = interval_at(reg_num(move_to));1265if (from != NULL && to != NULL) {1266to->set_register_hint(from);1267TRACE_LINEAR_SCAN(4, tty->print_cr("operation at op_id %d: added hint from interval %d to %d", cmove->id(), from->reg_num(), to->reg_num()));1268}1269}1270break;1271}1272}1273}127412751276void LinearScan::build_intervals() {1277TIME_LINEAR_SCAN(timer_build_intervals);12781279// initialize interval list with expected number of intervals1280// (32 is added to have some space for split children without having to resize the list)1281_intervals = IntervalList(num_virtual_regs() + 32);1282// initialize all slots that are used by build_intervals1283_intervals.at_put_grow(num_virtual_regs() - 1, NULL, NULL);12841285// create a list with all caller-save registers (cpu, fpu, xmm)1286// when an instruction is a call, a temp range is created for all these registers1287int num_caller_save_registers = 0;1288int caller_save_registers[LinearScan::nof_regs];12891290int i;1291for (i = 0; i < FrameMap::nof_caller_save_cpu_regs(); i++) {1292LIR_Opr opr = FrameMap::caller_save_cpu_reg_at(i);1293assert(opr->is_valid() && opr->is_register(), "FrameMap should not return invalid operands");1294assert(reg_numHi(opr) == -1, "missing addition of range for hi-register");1295caller_save_registers[num_caller_save_registers++] = reg_num(opr);1296}12971298// temp ranges for fpu registers are only created when the method has1299// virtual fpu operands. Otherwise no allocation for fpu registers is1300// perfomed and so the temp ranges would be useless1301if (has_fpu_registers()) {1302#ifdef X861303if (UseSSE < 2) {1304#endif1305for (i = 0; i < FrameMap::nof_caller_save_fpu_regs; i++) {1306LIR_Opr opr = FrameMap::caller_save_fpu_reg_at(i);1307assert(opr->is_valid() && opr->is_register(), "FrameMap should not return invalid operands");1308assert(reg_numHi(opr) == -1, "missing addition of range for hi-register");1309caller_save_registers[num_caller_save_registers++] = reg_num(opr);1310}1311#ifdef X861312}1313if (UseSSE > 0) {1314for (i = 0; i < FrameMap::nof_caller_save_xmm_regs; i++) {1315LIR_Opr opr = FrameMap::caller_save_xmm_reg_at(i);1316assert(opr->is_valid() && opr->is_register(), "FrameMap should not return invalid operands");1317assert(reg_numHi(opr) == -1, "missing addition of range for hi-register");1318caller_save_registers[num_caller_save_registers++] = reg_num(opr);1319}1320}1321#endif1322}1323assert(num_caller_save_registers <= LinearScan::nof_regs, "out of bounds");132413251326LIR_OpVisitState visitor;13271328// iterate all blocks in reverse order1329for (i = block_count() - 1; i >= 0; i--) {1330BlockBegin* block = block_at(i);1331LIR_OpList* instructions = block->lir()->instructions_list();1332int block_from = block->first_lir_instruction_id();1333int block_to = block->last_lir_instruction_id();13341335assert(block_from == instructions->at(0)->id(), "must be");1336assert(block_to == instructions->at(instructions->length() - 1)->id(), "must be");13371338// Update intervals for registers live at the end of this block;1339BitMap live = block->live_out();1340int size = (int)live.size();1341for (int number = (int)live.get_next_one_offset(0, size); number < size; number = (int)live.get_next_one_offset(number + 1, size)) {1342assert(live.at(number), "should not stop here otherwise");1343assert(number >= LIR_OprDesc::vreg_base, "fixed intervals must not be live on block bounds");1344TRACE_LINEAR_SCAN(2, tty->print_cr("live in %d to %d", number, block_to + 2));13451346add_use(number, block_from, block_to + 2, noUse, T_ILLEGAL);13471348// add special use positions for loop-end blocks when the1349// interval is used anywhere inside this loop. It's possible1350// that the block was part of a non-natural loop, so it might1351// have an invalid loop index.1352if (block->is_set(BlockBegin::linear_scan_loop_end_flag) &&1353block->loop_index() != -1 &&1354is_interval_in_loop(number, block->loop_index())) {1355interval_at(number)->add_use_pos(block_to + 1, loopEndMarker);1356}1357}13581359// iterate all instructions of the block in reverse order.1360// skip the first instruction because it is always a label1361// definitions of intervals are processed before uses1362assert(visitor.no_operands(instructions->at(0)), "first operation must always be a label");1363for (int j = instructions->length() - 1; j >= 1; j--) {1364LIR_Op* op = instructions->at(j);1365int op_id = op->id();13661367// visit operation to collect all operands1368visitor.visit(op);13691370// add a temp range for each register if operation destroys caller-save registers1371if (visitor.has_call()) {1372for (int k = 0; k < num_caller_save_registers; k++) {1373add_temp(caller_save_registers[k], op_id, noUse, T_ILLEGAL);1374}1375TRACE_LINEAR_SCAN(4, tty->print_cr("operation destroys all caller-save registers"));1376}13771378// Add any platform dependent temps1379pd_add_temps(op);13801381// visit definitions (output and temp operands)1382int k, n;1383n = visitor.opr_count(LIR_OpVisitState::outputMode);1384for (k = 0; k < n; k++) {1385LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::outputMode, k);1386assert(opr->is_register(), "visitor should only return register operands");1387add_def(opr, op_id, use_kind_of_output_operand(op, opr));1388}13891390n = visitor.opr_count(LIR_OpVisitState::tempMode);1391for (k = 0; k < n; k++) {1392LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::tempMode, k);1393assert(opr->is_register(), "visitor should only return register operands");1394add_temp(opr, op_id, mustHaveRegister);1395}13961397// visit uses (input operands)1398n = visitor.opr_count(LIR_OpVisitState::inputMode);1399for (k = 0; k < n; k++) {1400LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::inputMode, k);1401assert(opr->is_register(), "visitor should only return register operands");1402add_use(opr, block_from, op_id, use_kind_of_input_operand(op, opr));1403}14041405// Add uses of live locals from interpreter's point of view for proper1406// debug information generation1407// Treat these operands as temp values (if the life range is extended1408// to a call site, the value would be in a register at the call otherwise)1409n = visitor.info_count();1410for (k = 0; k < n; k++) {1411CodeEmitInfo* info = visitor.info_at(k);1412ValueStack* stack = info->stack();1413for_each_state_value(stack, value,1414add_use(value, block_from, op_id + 1, noUse);1415);1416}14171418// special steps for some instructions (especially moves)1419handle_method_arguments(op);1420handle_doubleword_moves(op);1421add_register_hints(op);14221423} // end of instruction iteration1424} // end of block iteration142514261427// add the range [0, 1[ to all fixed intervals1428// -> the register allocator need not handle unhandled fixed intervals1429for (int n = 0; n < LinearScan::nof_regs; n++) {1430Interval* interval = interval_at(n);1431if (interval != NULL) {1432interval->add_range(0, 1);1433}1434}1435}143614371438// ********** Phase 5: actual register allocation14391440int LinearScan::interval_cmp(Interval** a, Interval** b) {1441if (*a != NULL) {1442if (*b != NULL) {1443return (*a)->from() - (*b)->from();1444} else {1445return -1;1446}1447} else {1448if (*b != NULL) {1449return 1;1450} else {1451return 0;1452}1453}1454}14551456#ifndef PRODUCT1457bool LinearScan::is_sorted(IntervalArray* intervals) {1458int from = -1;1459int i, j;1460for (i = 0; i < intervals->length(); i ++) {1461Interval* it = intervals->at(i);1462if (it != NULL) {1463if (from > it->from()) {1464assert(false, "");1465return false;1466}1467from = it->from();1468}1469}14701471// check in both directions if sorted list and unsorted list contain same intervals1472for (i = 0; i < interval_count(); i++) {1473if (interval_at(i) != NULL) {1474int num_found = 0;1475for (j = 0; j < intervals->length(); j++) {1476if (interval_at(i) == intervals->at(j)) {1477num_found++;1478}1479}1480assert(num_found == 1, "lists do not contain same intervals");1481}1482}1483for (j = 0; j < intervals->length(); j++) {1484int num_found = 0;1485for (i = 0; i < interval_count(); i++) {1486if (interval_at(i) == intervals->at(j)) {1487num_found++;1488}1489}1490assert(num_found == 1, "lists do not contain same intervals");1491}14921493return true;1494}1495#endif14961497void LinearScan::add_to_list(Interval** first, Interval** prev, Interval* interval) {1498if (*prev != NULL) {1499(*prev)->set_next(interval);1500} else {1501*first = interval;1502}1503*prev = interval;1504}15051506void LinearScan::create_unhandled_lists(Interval** list1, Interval** list2, bool (is_list1)(const Interval* i), bool (is_list2)(const Interval* i)) {1507assert(is_sorted(_sorted_intervals), "interval list is not sorted");15081509*list1 = *list2 = Interval::end();15101511Interval* list1_prev = NULL;1512Interval* list2_prev = NULL;1513Interval* v;15141515const int n = _sorted_intervals->length();1516for (int i = 0; i < n; i++) {1517v = _sorted_intervals->at(i);1518if (v == NULL) continue;15191520if (is_list1(v)) {1521add_to_list(list1, &list1_prev, v);1522} else if (is_list2 == NULL || is_list2(v)) {1523add_to_list(list2, &list2_prev, v);1524}1525}15261527if (list1_prev != NULL) list1_prev->set_next(Interval::end());1528if (list2_prev != NULL) list2_prev->set_next(Interval::end());15291530assert(list1_prev == NULL || list1_prev->next() == Interval::end(), "linear list ends not with sentinel");1531assert(list2_prev == NULL || list2_prev->next() == Interval::end(), "linear list ends not with sentinel");1532}153315341535void LinearScan::sort_intervals_before_allocation() {1536TIME_LINEAR_SCAN(timer_sort_intervals_before);15371538if (_needs_full_resort) {1539// There is no known reason why this should occur but just in case...1540assert(false, "should never occur");1541// Re-sort existing interval list because an Interval::from() has changed1542_sorted_intervals->sort(interval_cmp);1543_needs_full_resort = false;1544}15451546IntervalList* unsorted_list = &_intervals;1547int unsorted_len = unsorted_list->length();1548int sorted_len = 0;1549int unsorted_idx;1550int sorted_idx = 0;1551int sorted_from_max = -1;15521553// calc number of items for sorted list (sorted list must not contain NULL values)1554for (unsorted_idx = 0; unsorted_idx < unsorted_len; unsorted_idx++) {1555if (unsorted_list->at(unsorted_idx) != NULL) {1556sorted_len++;1557}1558}1559IntervalArray* sorted_list = new IntervalArray(sorted_len);15601561// special sorting algorithm: the original interval-list is almost sorted,1562// only some intervals are swapped. So this is much faster than a complete QuickSort1563for (unsorted_idx = 0; unsorted_idx < unsorted_len; unsorted_idx++) {1564Interval* cur_interval = unsorted_list->at(unsorted_idx);15651566if (cur_interval != NULL) {1567int cur_from = cur_interval->from();15681569if (sorted_from_max <= cur_from) {1570sorted_list->at_put(sorted_idx++, cur_interval);1571sorted_from_max = cur_interval->from();1572} else {1573// the asumption that the intervals are already sorted failed,1574// so this interval must be sorted in manually1575int j;1576for (j = sorted_idx - 1; j >= 0 && cur_from < sorted_list->at(j)->from(); j--) {1577sorted_list->at_put(j + 1, sorted_list->at(j));1578}1579sorted_list->at_put(j + 1, cur_interval);1580sorted_idx++;1581}1582}1583}1584_sorted_intervals = sorted_list;1585assert(is_sorted(_sorted_intervals), "intervals unsorted");1586}15871588void LinearScan::sort_intervals_after_allocation() {1589TIME_LINEAR_SCAN(timer_sort_intervals_after);15901591if (_needs_full_resort) {1592// Re-sort existing interval list because an Interval::from() has changed1593_sorted_intervals->sort(interval_cmp);1594_needs_full_resort = false;1595}15961597IntervalArray* old_list = _sorted_intervals;1598IntervalList* new_list = _new_intervals_from_allocation;1599int old_len = old_list->length();1600int new_len = new_list->length();16011602if (new_len == 0) {1603// no intervals have been added during allocation, so sorted list is already up to date1604assert(is_sorted(_sorted_intervals), "intervals unsorted");1605return;1606}16071608// conventional sort-algorithm for new intervals1609new_list->sort(interval_cmp);16101611// merge old and new list (both already sorted) into one combined list1612IntervalArray* combined_list = new IntervalArray(old_len + new_len);1613int old_idx = 0;1614int new_idx = 0;16151616while (old_idx + new_idx < old_len + new_len) {1617if (new_idx >= new_len || (old_idx < old_len && old_list->at(old_idx)->from() <= new_list->at(new_idx)->from())) {1618combined_list->at_put(old_idx + new_idx, old_list->at(old_idx));1619old_idx++;1620} else {1621combined_list->at_put(old_idx + new_idx, new_list->at(new_idx));1622new_idx++;1623}1624}16251626_sorted_intervals = combined_list;1627assert(is_sorted(_sorted_intervals), "intervals unsorted");1628}162916301631void LinearScan::allocate_registers() {1632TIME_LINEAR_SCAN(timer_allocate_registers);16331634Interval* precolored_cpu_intervals, *not_precolored_cpu_intervals;1635Interval* precolored_fpu_intervals, *not_precolored_fpu_intervals;16361637// allocate cpu registers1638create_unhandled_lists(&precolored_cpu_intervals, ¬_precolored_cpu_intervals,1639is_precolored_cpu_interval, is_virtual_cpu_interval);16401641// allocate fpu registers1642create_unhandled_lists(&precolored_fpu_intervals, ¬_precolored_fpu_intervals,1643is_precolored_fpu_interval, is_virtual_fpu_interval);16441645// the fpu interval allocation cannot be moved down below with the fpu section as1646// the cpu_lsw.walk() changes interval positions.16471648LinearScanWalker cpu_lsw(this, precolored_cpu_intervals, not_precolored_cpu_intervals);1649cpu_lsw.walk();1650cpu_lsw.finish_allocation();16511652if (has_fpu_registers()) {1653LinearScanWalker fpu_lsw(this, precolored_fpu_intervals, not_precolored_fpu_intervals);1654fpu_lsw.walk();1655fpu_lsw.finish_allocation();1656}1657}165816591660// ********** Phase 6: resolve data flow1661// (insert moves at edges between blocks if intervals have been split)16621663// wrapper for Interval::split_child_at_op_id that performs a bailout in product mode1664// instead of returning NULL1665Interval* LinearScan::split_child_at_op_id(Interval* interval, int op_id, LIR_OpVisitState::OprMode mode) {1666Interval* result = interval->split_child_at_op_id(op_id, mode);1667if (result != NULL) {1668return result;1669}16701671assert(false, "must find an interval, but do a clean bailout in product mode");1672result = new Interval(LIR_OprDesc::vreg_base);1673result->assign_reg(0);1674result->set_type(T_INT);1675BAILOUT_("LinearScan: interval is NULL", result);1676}167716781679Interval* LinearScan::interval_at_block_begin(BlockBegin* block, int reg_num) {1680assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");1681assert(interval_at(reg_num) != NULL, "no interval found");16821683return split_child_at_op_id(interval_at(reg_num), block->first_lir_instruction_id(), LIR_OpVisitState::outputMode);1684}16851686Interval* LinearScan::interval_at_block_end(BlockBegin* block, int reg_num) {1687assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");1688assert(interval_at(reg_num) != NULL, "no interval found");16891690return split_child_at_op_id(interval_at(reg_num), block->last_lir_instruction_id() + 1, LIR_OpVisitState::outputMode);1691}16921693Interval* LinearScan::interval_at_op_id(int reg_num, int op_id) {1694assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");1695assert(interval_at(reg_num) != NULL, "no interval found");16961697return split_child_at_op_id(interval_at(reg_num), op_id, LIR_OpVisitState::inputMode);1698}169917001701void LinearScan::resolve_collect_mappings(BlockBegin* from_block, BlockBegin* to_block, MoveResolver &move_resolver) {1702DEBUG_ONLY(move_resolver.check_empty());17031704const int num_regs = num_virtual_regs();1705const int size = live_set_size();1706const BitMap live_at_edge = to_block->live_in();17071708// visit all registers where the live_at_edge bit is set1709for (int r = (int)live_at_edge.get_next_one_offset(0, size); r < size; r = (int)live_at_edge.get_next_one_offset(r + 1, size)) {1710assert(r < num_regs, "live information set for not exisiting interval");1711assert(from_block->live_out().at(r) && to_block->live_in().at(r), "interval not live at this edge");17121713Interval* from_interval = interval_at_block_end(from_block, r);1714Interval* to_interval = interval_at_block_begin(to_block, r);17151716if (from_interval != to_interval && (from_interval->assigned_reg() != to_interval->assigned_reg() || from_interval->assigned_regHi() != to_interval->assigned_regHi())) {1717// need to insert move instruction1718move_resolver.add_mapping(from_interval, to_interval);1719}1720}1721}172217231724void LinearScan::resolve_find_insert_pos(BlockBegin* from_block, BlockBegin* to_block, MoveResolver &move_resolver) {1725if (from_block->number_of_sux() <= 1) {1726TRACE_LINEAR_SCAN(4, tty->print_cr("inserting moves at end of from_block B%d", from_block->block_id()));17271728LIR_OpList* instructions = from_block->lir()->instructions_list();1729LIR_OpBranch* branch = instructions->last()->as_OpBranch();1730if (branch != NULL) {1731// insert moves before branch1732assert(branch->cond() == lir_cond_always, "block does not end with an unconditional jump");1733move_resolver.set_insert_position(from_block->lir(), instructions->length() - 2);1734} else {1735move_resolver.set_insert_position(from_block->lir(), instructions->length() - 1);1736}17371738} else {1739TRACE_LINEAR_SCAN(4, tty->print_cr("inserting moves at beginning of to_block B%d", to_block->block_id()));1740#ifdef ASSERT1741assert(from_block->lir()->instructions_list()->at(0)->as_OpLabel() != NULL, "block does not start with a label");17421743// because the number of predecessor edges matches the number of1744// successor edges, blocks which are reached by switch statements1745// may have be more than one predecessor but it will be guaranteed1746// that all predecessors will be the same.1747for (int i = 0; i < to_block->number_of_preds(); i++) {1748assert(from_block == to_block->pred_at(i), "all critical edges must be broken");1749}1750#endif17511752move_resolver.set_insert_position(to_block->lir(), 0);1753}1754}175517561757// insert necessary moves (spilling or reloading) at edges between blocks if interval has been split1758void LinearScan::resolve_data_flow() {1759TIME_LINEAR_SCAN(timer_resolve_data_flow);17601761int num_blocks = block_count();1762MoveResolver move_resolver(this);1763BitMap block_completed(num_blocks); block_completed.clear();1764BitMap already_resolved(num_blocks); already_resolved.clear();17651766int i;1767for (i = 0; i < num_blocks; i++) {1768BlockBegin* block = block_at(i);17691770// check if block has only one predecessor and only one successor1771if (block->number_of_preds() == 1 && block->number_of_sux() == 1 && block->number_of_exception_handlers() == 0) {1772LIR_OpList* instructions = block->lir()->instructions_list();1773assert(instructions->at(0)->code() == lir_label, "block must start with label");1774assert(instructions->last()->code() == lir_branch, "block with successors must end with branch");1775assert(instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block with successor must end with unconditional branch");17761777// check if block is empty (only label and branch)1778if (instructions->length() == 2) {1779BlockBegin* pred = block->pred_at(0);1780BlockBegin* sux = block->sux_at(0);17811782// prevent optimization of two consecutive blocks1783if (!block_completed.at(pred->linear_scan_number()) && !block_completed.at(sux->linear_scan_number())) {1784TRACE_LINEAR_SCAN(3, tty->print_cr("**** optimizing empty block B%d (pred: B%d, sux: B%d)", block->block_id(), pred->block_id(), sux->block_id()));1785block_completed.set_bit(block->linear_scan_number());17861787// directly resolve between pred and sux (without looking at the empty block between)1788resolve_collect_mappings(pred, sux, move_resolver);1789if (move_resolver.has_mappings()) {1790move_resolver.set_insert_position(block->lir(), 0);1791move_resolver.resolve_and_append_moves();1792}1793}1794}1795}1796}179717981799for (i = 0; i < num_blocks; i++) {1800if (!block_completed.at(i)) {1801BlockBegin* from_block = block_at(i);1802already_resolved.set_from(block_completed);18031804int num_sux = from_block->number_of_sux();1805for (int s = 0; s < num_sux; s++) {1806BlockBegin* to_block = from_block->sux_at(s);18071808// check for duplicate edges between the same blocks (can happen with switch blocks)1809if (!already_resolved.at(to_block->linear_scan_number())) {1810TRACE_LINEAR_SCAN(3, tty->print_cr("**** processing edge between B%d and B%d", from_block->block_id(), to_block->block_id()));1811already_resolved.set_bit(to_block->linear_scan_number());18121813// collect all intervals that have been split between from_block and to_block1814resolve_collect_mappings(from_block, to_block, move_resolver);1815if (move_resolver.has_mappings()) {1816resolve_find_insert_pos(from_block, to_block, move_resolver);1817move_resolver.resolve_and_append_moves();1818}1819}1820}1821}1822}1823}182418251826void LinearScan::resolve_exception_entry(BlockBegin* block, int reg_num, MoveResolver &move_resolver) {1827if (interval_at(reg_num) == NULL) {1828// if a phi function is never used, no interval is created -> ignore this1829return;1830}18311832Interval* interval = interval_at_block_begin(block, reg_num);1833int reg = interval->assigned_reg();1834int regHi = interval->assigned_regHi();18351836if ((reg < nof_regs && interval->always_in_memory()) ||1837(use_fpu_stack_allocation() && reg >= pd_first_fpu_reg && reg <= pd_last_fpu_reg)) {1838// the interval is split to get a short range that is located on the stack1839// in the following two cases:1840// * the interval started in memory (e.g. method parameter), but is currently in a register1841// this is an optimization for exception handling that reduces the number of moves that1842// are necessary for resolving the states when an exception uses this exception handler1843// * the interval would be on the fpu stack at the begin of the exception handler1844// this is not allowed because of the complicated fpu stack handling on Intel18451846// range that will be spilled to memory1847int from_op_id = block->first_lir_instruction_id();1848int to_op_id = from_op_id + 1; // short live range of length 11849assert(interval->from() <= from_op_id && interval->to() >= to_op_id,1850"no split allowed between exception entry and first instruction");18511852if (interval->from() != from_op_id) {1853// the part before from_op_id is unchanged1854interval = interval->split(from_op_id);1855interval->assign_reg(reg, regHi);1856append_interval(interval);1857} else {1858_needs_full_resort = true;1859}1860assert(interval->from() == from_op_id, "must be true now");18611862Interval* spilled_part = interval;1863if (interval->to() != to_op_id) {1864// the part after to_op_id is unchanged1865spilled_part = interval->split_from_start(to_op_id);1866append_interval(spilled_part);1867move_resolver.add_mapping(spilled_part, interval);1868}1869assign_spill_slot(spilled_part);18701871assert(spilled_part->from() == from_op_id && spilled_part->to() == to_op_id, "just checking");1872}1873}18741875void LinearScan::resolve_exception_entry(BlockBegin* block, MoveResolver &move_resolver) {1876assert(block->is_set(BlockBegin::exception_entry_flag), "should not call otherwise");1877DEBUG_ONLY(move_resolver.check_empty());18781879// visit all registers where the live_in bit is set1880int size = live_set_size();1881for (int r = (int)block->live_in().get_next_one_offset(0, size); r < size; r = (int)block->live_in().get_next_one_offset(r + 1, size)) {1882resolve_exception_entry(block, r, move_resolver);1883}18841885// the live_in bits are not set for phi functions of the xhandler entry, so iterate them separately1886for_each_phi_fun(block, phi,1887resolve_exception_entry(block, phi->operand()->vreg_number(), move_resolver)1888);18891890if (move_resolver.has_mappings()) {1891// insert moves after first instruction1892move_resolver.set_insert_position(block->lir(), 0);1893move_resolver.resolve_and_append_moves();1894}1895}189618971898void LinearScan::resolve_exception_edge(XHandler* handler, int throwing_op_id, int reg_num, Phi* phi, MoveResolver &move_resolver) {1899if (interval_at(reg_num) == NULL) {1900// if a phi function is never used, no interval is created -> ignore this1901return;1902}19031904// the computation of to_interval is equal to resolve_collect_mappings,1905// but from_interval is more complicated because of phi functions1906BlockBegin* to_block = handler->entry_block();1907Interval* to_interval = interval_at_block_begin(to_block, reg_num);19081909if (phi != NULL) {1910// phi function of the exception entry block1911// no moves are created for this phi function in the LIR_Generator, so the1912// interval at the throwing instruction must be searched using the operands1913// of the phi function1914Value from_value = phi->operand_at(handler->phi_operand());19151916// with phi functions it can happen that the same from_value is used in1917// multiple mappings, so notify move-resolver that this is allowed1918move_resolver.set_multiple_reads_allowed();19191920Constant* con = from_value->as_Constant();1921if (con != NULL && !con->is_pinned()) {1922// unpinned constants may have no register, so add mapping from constant to interval1923move_resolver.add_mapping(LIR_OprFact::value_type(con->type()), to_interval);1924} else {1925// search split child at the throwing op_id1926Interval* from_interval = interval_at_op_id(from_value->operand()->vreg_number(), throwing_op_id);1927move_resolver.add_mapping(from_interval, to_interval);1928}19291930} else {1931// no phi function, so use reg_num also for from_interval1932// search split child at the throwing op_id1933Interval* from_interval = interval_at_op_id(reg_num, throwing_op_id);1934if (from_interval != to_interval) {1935// optimization to reduce number of moves: when to_interval is on stack and1936// the stack slot is known to be always correct, then no move is necessary1937if (!from_interval->always_in_memory() || from_interval->canonical_spill_slot() != to_interval->assigned_reg()) {1938move_resolver.add_mapping(from_interval, to_interval);1939}1940}1941}1942}19431944void LinearScan::resolve_exception_edge(XHandler* handler, int throwing_op_id, MoveResolver &move_resolver) {1945TRACE_LINEAR_SCAN(4, tty->print_cr("resolving exception handler B%d: throwing_op_id=%d", handler->entry_block()->block_id(), throwing_op_id));19461947DEBUG_ONLY(move_resolver.check_empty());1948assert(handler->lir_op_id() == -1, "already processed this xhandler");1949DEBUG_ONLY(handler->set_lir_op_id(throwing_op_id));1950assert(handler->entry_code() == NULL, "code already present");19511952// visit all registers where the live_in bit is set1953BlockBegin* block = handler->entry_block();1954int size = live_set_size();1955for (int r = (int)block->live_in().get_next_one_offset(0, size); r < size; r = (int)block->live_in().get_next_one_offset(r + 1, size)) {1956resolve_exception_edge(handler, throwing_op_id, r, NULL, move_resolver);1957}19581959// the live_in bits are not set for phi functions of the xhandler entry, so iterate them separately1960for_each_phi_fun(block, phi,1961resolve_exception_edge(handler, throwing_op_id, phi->operand()->vreg_number(), phi, move_resolver)1962);19631964if (move_resolver.has_mappings()) {1965LIR_List* entry_code = new LIR_List(compilation());1966move_resolver.set_insert_position(entry_code, 0);1967move_resolver.resolve_and_append_moves();19681969entry_code->jump(handler->entry_block());1970handler->set_entry_code(entry_code);1971}1972}197319741975void LinearScan::resolve_exception_handlers() {1976MoveResolver move_resolver(this);1977LIR_OpVisitState visitor;1978int num_blocks = block_count();19791980int i;1981for (i = 0; i < num_blocks; i++) {1982BlockBegin* block = block_at(i);1983if (block->is_set(BlockBegin::exception_entry_flag)) {1984resolve_exception_entry(block, move_resolver);1985}1986}19871988for (i = 0; i < num_blocks; i++) {1989BlockBegin* block = block_at(i);1990LIR_List* ops = block->lir();1991int num_ops = ops->length();19921993// iterate all instructions of the block. skip the first because it is always a label1994assert(visitor.no_operands(ops->at(0)), "first operation must always be a label");1995for (int j = 1; j < num_ops; j++) {1996LIR_Op* op = ops->at(j);1997int op_id = op->id();19981999if (op_id != -1 && has_info(op_id)) {2000// visit operation to collect all operands2001visitor.visit(op);2002assert(visitor.info_count() > 0, "should not visit otherwise");20032004XHandlers* xhandlers = visitor.all_xhandler();2005int n = xhandlers->length();2006for (int k = 0; k < n; k++) {2007resolve_exception_edge(xhandlers->handler_at(k), op_id, move_resolver);2008}20092010#ifdef ASSERT2011} else {2012visitor.visit(op);2013assert(visitor.all_xhandler()->length() == 0, "missed exception handler");2014#endif2015}2016}2017}2018}201920202021// ********** Phase 7: assign register numbers back to LIR2022// (includes computation of debug information and oop maps)20232024VMReg LinearScan::vm_reg_for_interval(Interval* interval) {2025VMReg reg = interval->cached_vm_reg();2026if (!reg->is_valid() ) {2027reg = vm_reg_for_operand(operand_for_interval(interval));2028interval->set_cached_vm_reg(reg);2029}2030assert(reg == vm_reg_for_operand(operand_for_interval(interval)), "wrong cached value");2031return reg;2032}20332034VMReg LinearScan::vm_reg_for_operand(LIR_Opr opr) {2035assert(opr->is_oop(), "currently only implemented for oop operands");2036return frame_map()->regname(opr);2037}203820392040LIR_Opr LinearScan::operand_for_interval(Interval* interval) {2041LIR_Opr opr = interval->cached_opr();2042if (opr->is_illegal()) {2043opr = calc_operand_for_interval(interval);2044interval->set_cached_opr(opr);2045}20462047assert(opr == calc_operand_for_interval(interval), "wrong cached value");2048return opr;2049}20502051LIR_Opr LinearScan::calc_operand_for_interval(const Interval* interval) {2052int assigned_reg = interval->assigned_reg();2053BasicType type = interval->type();20542055if (assigned_reg >= nof_regs) {2056// stack slot2057assert(interval->assigned_regHi() == any_reg, "must not have hi register");2058return LIR_OprFact::stack(assigned_reg - nof_regs, type);20592060} else {2061// register2062switch (type) {2063case T_OBJECT: {2064assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");2065assert(interval->assigned_regHi() == any_reg, "must not have hi register");2066return LIR_OprFact::single_cpu_oop(assigned_reg);2067}20682069case T_ADDRESS: {2070assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");2071assert(interval->assigned_regHi() == any_reg, "must not have hi register");2072return LIR_OprFact::single_cpu_address(assigned_reg);2073}20742075case T_METADATA: {2076assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");2077assert(interval->assigned_regHi() == any_reg, "must not have hi register");2078return LIR_OprFact::single_cpu_metadata(assigned_reg);2079}20802081#ifdef __SOFTFP__2082case T_FLOAT: // fall through2083#if defined(AARCH32)2084if(hasFPU()) {2085assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");2086assert(interval->assigned_regHi() == any_reg, "must not have hi register");2087return LIR_OprFact::single_fpu(assigned_reg - pd_first_fpu_reg);2088}2089#endif2090#endif // __SOFTFP__2091case T_INT: {2092assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");2093assert(interval->assigned_regHi() == any_reg, "must not have hi register");2094return LIR_OprFact::single_cpu(assigned_reg);2095}20962097#ifdef __SOFTFP__2098case T_DOUBLE: // fall through2099#if defined(AARCH32)2100if(hasFPU()) {2101assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");2102assert(interval->assigned_regHi() >= pd_first_fpu_reg && interval->assigned_regHi() <= pd_last_fpu_reg, "no fpu register");2103assert(assigned_reg % 2 == 0 && assigned_reg + 1 == interval->assigned_regHi(), "must be sequential and even");2104return LIR_OprFact::double_fpu(assigned_reg - pd_first_fpu_reg, interval->assigned_regHi() - pd_first_fpu_reg);2105}2106#endif2107#endif // __SOFTFP__2108case T_LONG: {2109int assigned_regHi = interval->assigned_regHi();2110assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");2111assert(num_physical_regs(T_LONG) == 1 ||2112(assigned_regHi >= pd_first_cpu_reg && assigned_regHi <= pd_last_cpu_reg), "no cpu register");21132114assert(assigned_reg != assigned_regHi, "invalid allocation");2115assert(num_physical_regs(T_LONG) == 1 || assigned_reg < assigned_regHi,2116"register numbers must be sorted (ensure that e.g. a move from eax,ebx to ebx,eax can not occur)");2117assert((assigned_regHi != any_reg) ^ (num_physical_regs(T_LONG) == 1), "must be match");2118if (requires_adjacent_regs(T_LONG)) {2119assert(assigned_reg % 2 == 0 && assigned_reg + 1 == assigned_regHi, "must be sequential and even");2120}21212122#ifdef _LP642123return LIR_OprFact::double_cpu(assigned_reg, assigned_reg);2124#else2125#if defined(SPARC) || defined(PPC)2126return LIR_OprFact::double_cpu(assigned_regHi, assigned_reg);2127#else2128return LIR_OprFact::double_cpu(assigned_reg, assigned_regHi);2129#endif // SPARC2130#endif // LP642131}21322133#ifndef __SOFTFP__2134case T_FLOAT: {2135#ifdef X862136if (UseSSE >= 1) {2137assert(assigned_reg >= pd_first_xmm_reg && assigned_reg <= pd_last_xmm_reg, "no xmm register");2138assert(interval->assigned_regHi() == any_reg, "must not have hi register");2139return LIR_OprFact::single_xmm(assigned_reg - pd_first_xmm_reg);2140}2141#endif21422143assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");2144assert(interval->assigned_regHi() == any_reg, "must not have hi register");2145return LIR_OprFact::single_fpu(assigned_reg - pd_first_fpu_reg);2146}21472148case T_DOUBLE: {2149#ifdef X862150if (UseSSE >= 2) {2151assert(assigned_reg >= pd_first_xmm_reg && assigned_reg <= pd_last_xmm_reg, "no xmm register");2152assert(interval->assigned_regHi() == any_reg, "must not have hi register (double xmm values are stored in one register)");2153return LIR_OprFact::double_xmm(assigned_reg - pd_first_xmm_reg);2154}2155#endif21562157#ifdef SPARC2158assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");2159assert(interval->assigned_regHi() >= pd_first_fpu_reg && interval->assigned_regHi() <= pd_last_fpu_reg, "no fpu register");2160assert(assigned_reg % 2 == 0 && assigned_reg + 1 == interval->assigned_regHi(), "must be sequential and even");2161LIR_Opr result = LIR_OprFact::double_fpu(interval->assigned_regHi() - pd_first_fpu_reg, assigned_reg - pd_first_fpu_reg);2162#elif defined(ARM32) || defined(AARCH32)2163assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");2164assert(interval->assigned_regHi() >= pd_first_fpu_reg && interval->assigned_regHi() <= pd_last_fpu_reg, "no fpu register");2165assert(assigned_reg % 2 == 0 && assigned_reg + 1 == interval->assigned_regHi(), "must be sequential and even");2166LIR_Opr result = LIR_OprFact::double_fpu(assigned_reg - pd_first_fpu_reg, interval->assigned_regHi() - pd_first_fpu_reg);2167#else2168assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");2169assert(interval->assigned_regHi() == any_reg, "must not have hi register (double fpu values are stored in one register on Intel)");2170LIR_Opr result = LIR_OprFact::double_fpu(assigned_reg - pd_first_fpu_reg);2171#endif2172return result;2173}2174#endif // __SOFTFP__21752176default: {2177ShouldNotReachHere();2178return LIR_OprFact::illegalOpr;2179}2180}2181}2182}21832184LIR_Opr LinearScan::canonical_spill_opr(Interval* interval) {2185assert(interval->canonical_spill_slot() >= nof_regs, "canonical spill slot not set");2186return LIR_OprFact::stack(interval->canonical_spill_slot() - nof_regs, interval->type());2187}21882189LIR_Opr LinearScan::color_lir_opr(LIR_Opr opr, int op_id, LIR_OpVisitState::OprMode mode) {2190assert(opr->is_virtual(), "should not call this otherwise");21912192Interval* interval = interval_at(opr->vreg_number());2193assert(interval != NULL, "interval must exist");21942195if (op_id != -1) {2196#ifdef ASSERT2197BlockBegin* block = block_of_op_with_id(op_id);2198if (block->number_of_sux() <= 1 && op_id == block->last_lir_instruction_id()) {2199// check if spill moves could have been appended at the end of this block, but2200// before the branch instruction. So the split child information for this branch would2201// be incorrect.2202LIR_OpBranch* branch = block->lir()->instructions_list()->last()->as_OpBranch();2203if (branch != NULL) {2204if (block->live_out().at(opr->vreg_number())) {2205assert(branch->cond() == lir_cond_always, "block does not end with an unconditional jump");2206assert(false, "can't get split child for the last branch of a block because the information would be incorrect (moves are inserted before the branch in resolve_data_flow)");2207}2208}2209}2210#endif22112212// operands are not changed when an interval is split during allocation,2213// so search the right interval here2214interval = split_child_at_op_id(interval, op_id, mode);2215}22162217LIR_Opr res = operand_for_interval(interval);22182219#if defined(X86) || defined(AARCH64)2220// new semantic for is_last_use: not only set on definite end of interval,2221// but also before hole2222// This may still miss some cases (e.g. for dead values), but it is not necessary that the2223// last use information is completely correct2224// information is only needed for fpu stack allocation2225if (res->is_fpu_register()) {2226if (opr->is_last_use() || op_id == interval->to() || (op_id != -1 && interval->has_hole_between(op_id, op_id + 1))) {2227assert(op_id == -1 || !is_block_begin(op_id), "holes at begin of block may also result from control flow");2228res = res->make_last_use();2229}2230}2231#endif22322233assert(!gen()->is_vreg_flag_set(opr->vreg_number(), LIRGenerator::callee_saved) || !FrameMap::is_caller_save_register(res), "bad allocation");22342235return res;2236}223722382239#ifdef ASSERT2240// some methods used to check correctness of debug information22412242void assert_no_register_values(GrowableArray<ScopeValue*>* values) {2243if (values == NULL) {2244return;2245}22462247for (int i = 0; i < values->length(); i++) {2248ScopeValue* value = values->at(i);22492250if (value->is_location()) {2251Location location = ((LocationValue*)value)->location();2252assert(location.where() == Location::on_stack, "value is in register");2253}2254}2255}22562257void assert_no_register_values(GrowableArray<MonitorValue*>* values) {2258if (values == NULL) {2259return;2260}22612262for (int i = 0; i < values->length(); i++) {2263MonitorValue* value = values->at(i);22642265if (value->owner()->is_location()) {2266Location location = ((LocationValue*)value->owner())->location();2267assert(location.where() == Location::on_stack, "owner is in register");2268}2269assert(value->basic_lock().where() == Location::on_stack, "basic_lock is in register");2270}2271}22722273void assert_equal(Location l1, Location l2) {2274assert(l1.where() == l2.where() && l1.type() == l2.type() && l1.offset() == l2.offset(), "");2275}22762277void assert_equal(ScopeValue* v1, ScopeValue* v2) {2278if (v1->is_location()) {2279assert(v2->is_location(), "");2280assert_equal(((LocationValue*)v1)->location(), ((LocationValue*)v2)->location());2281} else if (v1->is_constant_int()) {2282assert(v2->is_constant_int(), "");2283assert(((ConstantIntValue*)v1)->value() == ((ConstantIntValue*)v2)->value(), "");2284} else if (v1->is_constant_double()) {2285assert(v2->is_constant_double(), "");2286assert(((ConstantDoubleValue*)v1)->value() == ((ConstantDoubleValue*)v2)->value(), "");2287} else if (v1->is_constant_long()) {2288assert(v2->is_constant_long(), "");2289assert(((ConstantLongValue*)v1)->value() == ((ConstantLongValue*)v2)->value(), "");2290} else if (v1->is_constant_oop()) {2291assert(v2->is_constant_oop(), "");2292assert(((ConstantOopWriteValue*)v1)->value() == ((ConstantOopWriteValue*)v2)->value(), "");2293} else {2294ShouldNotReachHere();2295}2296}22972298void assert_equal(MonitorValue* m1, MonitorValue* m2) {2299assert_equal(m1->owner(), m2->owner());2300assert_equal(m1->basic_lock(), m2->basic_lock());2301}23022303void assert_equal(IRScopeDebugInfo* d1, IRScopeDebugInfo* d2) {2304assert(d1->scope() == d2->scope(), "not equal");2305assert(d1->bci() == d2->bci(), "not equal");23062307if (d1->locals() != NULL) {2308assert(d1->locals() != NULL && d2->locals() != NULL, "not equal");2309assert(d1->locals()->length() == d2->locals()->length(), "not equal");2310for (int i = 0; i < d1->locals()->length(); i++) {2311assert_equal(d1->locals()->at(i), d2->locals()->at(i));2312}2313} else {2314assert(d1->locals() == NULL && d2->locals() == NULL, "not equal");2315}23162317if (d1->expressions() != NULL) {2318assert(d1->expressions() != NULL && d2->expressions() != NULL, "not equal");2319assert(d1->expressions()->length() == d2->expressions()->length(), "not equal");2320for (int i = 0; i < d1->expressions()->length(); i++) {2321assert_equal(d1->expressions()->at(i), d2->expressions()->at(i));2322}2323} else {2324assert(d1->expressions() == NULL && d2->expressions() == NULL, "not equal");2325}23262327if (d1->monitors() != NULL) {2328assert(d1->monitors() != NULL && d2->monitors() != NULL, "not equal");2329assert(d1->monitors()->length() == d2->monitors()->length(), "not equal");2330for (int i = 0; i < d1->monitors()->length(); i++) {2331assert_equal(d1->monitors()->at(i), d2->monitors()->at(i));2332}2333} else {2334assert(d1->monitors() == NULL && d2->monitors() == NULL, "not equal");2335}23362337if (d1->caller() != NULL) {2338assert(d1->caller() != NULL && d2->caller() != NULL, "not equal");2339assert_equal(d1->caller(), d2->caller());2340} else {2341assert(d1->caller() == NULL && d2->caller() == NULL, "not equal");2342}2343}23442345void check_stack_depth(CodeEmitInfo* info, int stack_end) {2346if (info->stack()->bci() != SynchronizationEntryBCI && !info->scope()->method()->is_native()) {2347Bytecodes::Code code = info->scope()->method()->java_code_at_bci(info->stack()->bci());2348switch (code) {2349case Bytecodes::_ifnull : // fall through2350case Bytecodes::_ifnonnull : // fall through2351case Bytecodes::_ifeq : // fall through2352case Bytecodes::_ifne : // fall through2353case Bytecodes::_iflt : // fall through2354case Bytecodes::_ifge : // fall through2355case Bytecodes::_ifgt : // fall through2356case Bytecodes::_ifle : // fall through2357case Bytecodes::_if_icmpeq : // fall through2358case Bytecodes::_if_icmpne : // fall through2359case Bytecodes::_if_icmplt : // fall through2360case Bytecodes::_if_icmpge : // fall through2361case Bytecodes::_if_icmpgt : // fall through2362case Bytecodes::_if_icmple : // fall through2363case Bytecodes::_if_acmpeq : // fall through2364case Bytecodes::_if_acmpne :2365assert(stack_end >= -Bytecodes::depth(code), "must have non-empty expression stack at if bytecode");2366break;2367}2368}2369}23702371#endif // ASSERT237223732374IntervalWalker* LinearScan::init_compute_oop_maps() {2375// setup lists of potential oops for walking2376Interval* oop_intervals;2377Interval* non_oop_intervals;23782379create_unhandled_lists(&oop_intervals, &non_oop_intervals, is_oop_interval, NULL);23802381// intervals that have no oops inside need not to be processed2382// to ensure a walking until the last instruction id, add a dummy interval2383// with a high operation id2384non_oop_intervals = new Interval(any_reg);2385non_oop_intervals->add_range(max_jint - 2, max_jint - 1);23862387return new IntervalWalker(this, oop_intervals, non_oop_intervals);2388}238923902391OopMap* LinearScan::compute_oop_map(IntervalWalker* iw, LIR_Op* op, CodeEmitInfo* info, bool is_call_site) {2392TRACE_LINEAR_SCAN(3, tty->print_cr("creating oop map at op_id %d", op->id()));23932394// walk before the current operation -> intervals that start at2395// the operation (= output operands of the operation) are not2396// included in the oop map2397iw->walk_before(op->id());23982399int frame_size = frame_map()->framesize();2400int arg_count = frame_map()->oop_map_arg_count();2401OopMap* map = new OopMap(frame_size, arg_count);24022403// Iterate through active intervals2404for (Interval* interval = iw->active_first(fixedKind); interval != Interval::end(); interval = interval->next()) {2405int assigned_reg = interval->assigned_reg();24062407assert(interval->current_from() <= op->id() && op->id() <= interval->current_to(), "interval should not be active otherwise");2408assert(interval->assigned_regHi() == any_reg, "oop must be single word");2409assert(interval->reg_num() >= LIR_OprDesc::vreg_base, "fixed interval found");24102411// Check if this range covers the instruction. Intervals that2412// start or end at the current operation are not included in the2413// oop map, except in the case of patching moves. For patching2414// moves, any intervals which end at this instruction are included2415// in the oop map since we may safepoint while doing the patch2416// before we've consumed the inputs.2417if (op->is_patching() || op->id() < interval->current_to()) {24182419// caller-save registers must not be included into oop-maps at calls2420assert(!is_call_site || assigned_reg >= nof_regs || !is_caller_save(assigned_reg), "interval is in a caller-save register at a call -> register will be overwritten");24212422VMReg name = vm_reg_for_interval(interval);2423set_oop(map, name);24242425// Spill optimization: when the stack value is guaranteed to be always correct,2426// then it must be added to the oop map even if the interval is currently in a register2427if (interval->always_in_memory() &&2428op->id() > interval->spill_definition_pos() &&2429interval->assigned_reg() != interval->canonical_spill_slot()) {2430assert(interval->spill_definition_pos() > 0, "position not set correctly");2431assert(interval->canonical_spill_slot() >= LinearScan::nof_regs, "no spill slot assigned");2432assert(interval->assigned_reg() < LinearScan::nof_regs, "interval is on stack, so stack slot is registered twice");24332434set_oop(map, frame_map()->slot_regname(interval->canonical_spill_slot() - LinearScan::nof_regs));2435}2436}2437}24382439// add oops from lock stack2440assert(info->stack() != NULL, "CodeEmitInfo must always have a stack");2441int locks_count = info->stack()->total_locks_size();2442for (int i = 0; i < locks_count; i++) {2443set_oop(map, frame_map()->monitor_object_regname(i));2444}24452446return map;2447}244824492450void LinearScan::compute_oop_map(IntervalWalker* iw, const LIR_OpVisitState &visitor, LIR_Op* op) {2451assert(visitor.info_count() > 0, "no oop map needed");24522453// compute oop_map only for first CodeEmitInfo2454// because it is (in most cases) equal for all other infos of the same operation2455CodeEmitInfo* first_info = visitor.info_at(0);2456OopMap* first_oop_map = compute_oop_map(iw, op, first_info, visitor.has_call());24572458for (int i = 0; i < visitor.info_count(); i++) {2459CodeEmitInfo* info = visitor.info_at(i);2460OopMap* oop_map = first_oop_map;24612462// compute worst case interpreter size in case of a deoptimization2463_compilation->update_interpreter_frame_size(info->interpreter_frame_size());24642465if (info->stack()->locks_size() != first_info->stack()->locks_size()) {2466// this info has a different number of locks then the precomputed oop map2467// (possible for lock and unlock instructions) -> compute oop map with2468// correct lock information2469oop_map = compute_oop_map(iw, op, info, visitor.has_call());2470}24712472if (info->_oop_map == NULL) {2473info->_oop_map = oop_map;2474} else {2475// a CodeEmitInfo can not be shared between different LIR-instructions2476// because interval splitting can occur anywhere between two instructions2477// and so the oop maps must be different2478// -> check if the already set oop_map is exactly the one calculated for this operation2479assert(info->_oop_map == oop_map, "same CodeEmitInfo used for multiple LIR instructions");2480}2481}2482}248324842485// frequently used constants2486// Allocate them with new so they are never destroyed (otherwise, a2487// forced exit could destroy these objects while they are still in2488// use).2489ConstantOopWriteValue* LinearScan::_oop_null_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantOopWriteValue(NULL);2490ConstantIntValue* LinearScan::_int_m1_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(-1);2491ConstantIntValue* LinearScan::_int_0_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(0);2492ConstantIntValue* LinearScan::_int_1_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(1);2493ConstantIntValue* LinearScan::_int_2_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(2);2494LocationValue* _illegal_value = new (ResourceObj::C_HEAP, mtCompiler) LocationValue(Location());24952496void LinearScan::init_compute_debug_info() {2497// cache for frequently used scope values2498// (cpu registers and stack slots)2499_scope_value_cache = ScopeValueArray((LinearScan::nof_cpu_regs + frame_map()->argcount() + max_spills()) * 2, NULL);2500}25012502MonitorValue* LinearScan::location_for_monitor_index(int monitor_index) {2503Location loc;2504if (!frame_map()->location_for_monitor_object(monitor_index, &loc)) {2505bailout("too large frame");2506}2507ScopeValue* object_scope_value = new LocationValue(loc);25082509if (!frame_map()->location_for_monitor_lock(monitor_index, &loc)) {2510bailout("too large frame");2511}2512return new MonitorValue(object_scope_value, loc);2513}25142515LocationValue* LinearScan::location_for_name(int name, Location::Type loc_type) {2516Location loc;2517if (!frame_map()->locations_for_slot(name, loc_type, &loc)) {2518bailout("too large frame");2519}2520return new LocationValue(loc);2521}252225232524int LinearScan::append_scope_value_for_constant(LIR_Opr opr, GrowableArray<ScopeValue*>* scope_values) {2525assert(opr->is_constant(), "should not be called otherwise");25262527LIR_Const* c = opr->as_constant_ptr();2528BasicType t = c->type();2529switch (t) {2530case T_OBJECT: {2531jobject value = c->as_jobject();2532if (value == NULL) {2533scope_values->append(_oop_null_scope_value);2534} else {2535scope_values->append(new ConstantOopWriteValue(c->as_jobject()));2536}2537return 1;2538}25392540case T_INT: // fall through2541case T_FLOAT: {2542int value = c->as_jint_bits();2543switch (value) {2544case -1: scope_values->append(_int_m1_scope_value); break;2545case 0: scope_values->append(_int_0_scope_value); break;2546case 1: scope_values->append(_int_1_scope_value); break;2547case 2: scope_values->append(_int_2_scope_value); break;2548default: scope_values->append(new ConstantIntValue(c->as_jint_bits())); break;2549}2550return 1;2551}25522553case T_LONG: // fall through2554case T_DOUBLE: {2555#ifdef _LP642556scope_values->append(_int_0_scope_value);2557scope_values->append(new ConstantLongValue(c->as_jlong_bits()));2558#else2559if (hi_word_offset_in_bytes > lo_word_offset_in_bytes) {2560scope_values->append(new ConstantIntValue(c->as_jint_hi_bits()));2561scope_values->append(new ConstantIntValue(c->as_jint_lo_bits()));2562} else {2563scope_values->append(new ConstantIntValue(c->as_jint_lo_bits()));2564scope_values->append(new ConstantIntValue(c->as_jint_hi_bits()));2565}2566#endif2567return 2;2568}25692570case T_ADDRESS: {2571#ifdef _LP642572scope_values->append(new ConstantLongValue(c->as_jint()));2573#else2574scope_values->append(new ConstantIntValue(c->as_jint()));2575#endif2576return 1;2577}25782579default:2580ShouldNotReachHere();2581return -1;2582}2583}25842585int LinearScan::append_scope_value_for_operand(LIR_Opr opr, GrowableArray<ScopeValue*>* scope_values) {2586if (opr->is_single_stack()) {2587int stack_idx = opr->single_stack_ix();2588bool is_oop = opr->is_oop_register();2589int cache_idx = (stack_idx + LinearScan::nof_cpu_regs) * 2 + (is_oop ? 1 : 0);25902591ScopeValue* sv = _scope_value_cache.at(cache_idx);2592if (sv == NULL) {2593Location::Type loc_type = is_oop ? Location::oop : Location::normal;2594sv = location_for_name(stack_idx, loc_type);2595_scope_value_cache.at_put(cache_idx, sv);2596}25972598// check if cached value is correct2599DEBUG_ONLY(assert_equal(sv, location_for_name(stack_idx, is_oop ? Location::oop : Location::normal)));26002601scope_values->append(sv);2602return 1;26032604} else if (opr->is_single_cpu()) {2605bool is_oop = opr->is_oop_register();2606int cache_idx = opr->cpu_regnr() * 2 + (is_oop ? 1 : 0);2607Location::Type int_loc_type = NOT_LP64(Location::normal) LP64_ONLY(Location::int_in_long);26082609ScopeValue* sv = _scope_value_cache.at(cache_idx);2610if (sv == NULL) {2611Location::Type loc_type = is_oop ? Location::oop : int_loc_type;2612VMReg rname = frame_map()->regname(opr);2613sv = new LocationValue(Location::new_reg_loc(loc_type, rname));2614_scope_value_cache.at_put(cache_idx, sv);2615}26162617// check if cached value is correct2618DEBUG_ONLY(assert_equal(sv, new LocationValue(Location::new_reg_loc(is_oop ? Location::oop : int_loc_type, frame_map()->regname(opr)))));26192620scope_values->append(sv);2621return 1;26222623#ifdef X862624} else if (opr->is_single_xmm()) {2625VMReg rname = opr->as_xmm_float_reg()->as_VMReg();2626LocationValue* sv = new LocationValue(Location::new_reg_loc(Location::normal, rname));26272628scope_values->append(sv);2629return 1;2630#endif26312632} else if (opr->is_single_fpu()) {2633#ifdef X862634// the exact location of fpu stack values is only known2635// during fpu stack allocation, so the stack allocator object2636// must be present2637assert(use_fpu_stack_allocation(), "should not have float stack values without fpu stack allocation (all floats must be SSE2)");2638assert(_fpu_stack_allocator != NULL, "must be present");2639opr = _fpu_stack_allocator->to_fpu_stack(opr);2640#endif26412642Location::Type loc_type = float_saved_as_double ? Location::float_in_dbl : Location::normal;2643VMReg rname = frame_map()->fpu_regname(opr->fpu_regnr());2644#ifndef __SOFTFP__2645#ifndef VM_LITTLE_ENDIAN2646if (! float_saved_as_double) {2647// On big endian system, we may have an issue if float registers use only2648// the low half of the (same) double registers.2649// Both the float and the double could have the same regnr but would correspond2650// to two different addresses once saved.26512652// get next safely (no assertion checks)2653VMReg next = VMRegImpl::as_VMReg(1+rname->value());2654if (next->is_reg() &&2655(next->as_FloatRegister() == rname->as_FloatRegister())) {2656// the back-end does use the same numbering for the double and the float2657rname = next; // VMReg for the low bits, e.g. the real VMReg for the float2658}2659}2660#endif2661#endif2662LocationValue* sv = new LocationValue(Location::new_reg_loc(loc_type, rname));26632664scope_values->append(sv);2665return 1;26662667} else {2668// double-size operands26692670ScopeValue* first;2671ScopeValue* second;26722673if (opr->is_double_stack()) {2674#ifdef _LP642675Location loc1;2676Location::Type loc_type = opr->type() == T_LONG ? Location::lng : Location::dbl;2677if (!frame_map()->locations_for_slot(opr->double_stack_ix(), loc_type, &loc1, NULL)) {2678bailout("too large frame");2679}2680// Does this reverse on x86 vs. sparc?2681first = new LocationValue(loc1);2682second = _int_0_scope_value;2683#else2684Location loc1, loc2;2685if (!frame_map()->locations_for_slot(opr->double_stack_ix(), Location::normal, &loc1, &loc2)) {2686bailout("too large frame");2687}2688first = new LocationValue(loc1);2689second = new LocationValue(loc2);2690#endif // _LP6426912692} else if (opr->is_double_cpu()) {2693#ifdef _LP642694VMReg rname_first = opr->as_register_lo()->as_VMReg();2695first = new LocationValue(Location::new_reg_loc(Location::lng, rname_first));2696second = _int_0_scope_value;2697#else2698VMReg rname_first = opr->as_register_lo()->as_VMReg();2699VMReg rname_second = opr->as_register_hi()->as_VMReg();27002701if (hi_word_offset_in_bytes < lo_word_offset_in_bytes) {2702// lo/hi and swapped relative to first and second, so swap them2703VMReg tmp = rname_first;2704rname_first = rname_second;2705rname_second = tmp;2706}27072708first = new LocationValue(Location::new_reg_loc(Location::normal, rname_first));2709second = new LocationValue(Location::new_reg_loc(Location::normal, rname_second));2710#endif //_LP64271127122713#ifdef X862714} else if (opr->is_double_xmm()) {2715assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation");2716VMReg rname_first = opr->as_xmm_double_reg()->as_VMReg();2717# ifdef _LP642718first = new LocationValue(Location::new_reg_loc(Location::dbl, rname_first));2719second = _int_0_scope_value;2720# else2721first = new LocationValue(Location::new_reg_loc(Location::normal, rname_first));2722// %%% This is probably a waste but we'll keep things as they were for now2723if (true) {2724VMReg rname_second = rname_first->next();2725second = new LocationValue(Location::new_reg_loc(Location::normal, rname_second));2726}2727# endif2728#endif27292730} else if (opr->is_double_fpu()) {2731// On SPARC, fpu_regnrLo/fpu_regnrHi represents the two halves of2732// the double as float registers in the native ordering. On X86,2733// fpu_regnrLo is a FPU stack slot whose VMReg represents2734// the low-order word of the double and fpu_regnrLo + 1 is the2735// name for the other half. *first and *second must represent the2736// least and most significant words, respectively.27372738#ifdef X862739// the exact location of fpu stack values is only known2740// during fpu stack allocation, so the stack allocator object2741// must be present2742assert(use_fpu_stack_allocation(), "should not have float stack values without fpu stack allocation (all floats must be SSE2)");2743assert(_fpu_stack_allocator != NULL, "must be present");2744opr = _fpu_stack_allocator->to_fpu_stack(opr);27452746assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation (only fpu_regnrLo is used)");2747#endif2748#ifdef SPARC2749assert(opr->fpu_regnrLo() == opr->fpu_regnrHi() + 1, "assumed in calculation (only fpu_regnrHi is used)");2750#endif2751#if defined(ARM32) || defined(AARCH32)2752assert(opr->fpu_regnrHi() == opr->fpu_regnrLo() + 1, "assumed in calculation (only fpu_regnrLo is used)");2753#endif // ARM32 || AARCH322754#ifdef PPC2755assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation (only fpu_regnrHi is used)");2756#endif27572758#ifdef VM_LITTLE_ENDIAN2759VMReg rname_first = frame_map()->fpu_regname(opr->fpu_regnrLo());2760#else2761VMReg rname_first = frame_map()->fpu_regname(opr->fpu_regnrHi());2762#endif27632764#ifdef _LP642765first = new LocationValue(Location::new_reg_loc(Location::dbl, rname_first));2766second = _int_0_scope_value;2767#else2768first = new LocationValue(Location::new_reg_loc(Location::normal, rname_first));2769// %%% This is probably a waste but we'll keep things as they were for now2770if (true) {2771VMReg rname_second = rname_first->next();2772second = new LocationValue(Location::new_reg_loc(Location::normal, rname_second));2773}2774#endif27752776} else {2777ShouldNotReachHere();2778first = NULL;2779second = NULL;2780}27812782assert(first != NULL && second != NULL, "must be set");2783// The convention the interpreter uses is that the second local2784// holds the first raw word of the native double representation.2785// This is actually reasonable, since locals and stack arrays2786// grow downwards in all implementations.2787// (If, on some machine, the interpreter's Java locals or stack2788// were to grow upwards, the embedded doubles would be word-swapped.)2789scope_values->append(second);2790scope_values->append(first);2791return 2;2792}2793}279427952796int LinearScan::append_scope_value(int op_id, Value value, GrowableArray<ScopeValue*>* scope_values) {2797if (value != NULL) {2798LIR_Opr opr = value->operand();2799Constant* con = value->as_Constant();28002801assert(con == NULL || opr->is_virtual() || opr->is_constant() || opr->is_illegal(), "asumption: Constant instructions have only constant operands (or illegal if constant is optimized away)");2802assert(con != NULL || opr->is_virtual(), "asumption: non-Constant instructions have only virtual operands");28032804if (con != NULL && !con->is_pinned() && !opr->is_constant()) {2805// Unpinned constants may have a virtual operand for a part of the lifetime2806// or may be illegal when it was optimized away,2807// so always use a constant operand2808opr = LIR_OprFact::value_type(con->type());2809}2810assert(opr->is_virtual() || opr->is_constant(), "other cases not allowed here");28112812if (opr->is_virtual()) {2813LIR_OpVisitState::OprMode mode = LIR_OpVisitState::inputMode;28142815BlockBegin* block = block_of_op_with_id(op_id);2816if (block->number_of_sux() == 1 && op_id == block->last_lir_instruction_id()) {2817// generating debug information for the last instruction of a block.2818// if this instruction is a branch, spill moves are inserted before this branch2819// and so the wrong operand would be returned (spill moves at block boundaries are not2820// considered in the live ranges of intervals)2821// Solution: use the first op_id of the branch target block instead.2822if (block->lir()->instructions_list()->last()->as_OpBranch() != NULL) {2823if (block->live_out().at(opr->vreg_number())) {2824op_id = block->sux_at(0)->first_lir_instruction_id();2825mode = LIR_OpVisitState::outputMode;2826}2827}2828}28292830// Get current location of operand2831// The operand must be live because debug information is considered when building the intervals2832// if the interval is not live, color_lir_opr will cause an assertion failure2833opr = color_lir_opr(opr, op_id, mode);2834assert(!has_call(op_id) || opr->is_stack() || !is_caller_save(reg_num(opr)), "can not have caller-save register operands at calls");28352836// Append to ScopeValue array2837return append_scope_value_for_operand(opr, scope_values);28382839} else {2840assert(value->as_Constant() != NULL, "all other instructions have only virtual operands");2841assert(opr->is_constant(), "operand must be constant");28422843return append_scope_value_for_constant(opr, scope_values);2844}2845} else {2846// append a dummy value because real value not needed2847scope_values->append(_illegal_value);2848return 1;2849}2850}285128522853IRScopeDebugInfo* LinearScan::compute_debug_info_for_scope(int op_id, IRScope* cur_scope, ValueStack* cur_state, ValueStack* innermost_state) {2854IRScopeDebugInfo* caller_debug_info = NULL;28552856ValueStack* caller_state = cur_state->caller_state();2857if (caller_state != NULL) {2858// process recursively to compute outermost scope first2859caller_debug_info = compute_debug_info_for_scope(op_id, cur_scope->caller(), caller_state, innermost_state);2860}28612862// initialize these to null.2863// If we don't need deopt info or there are no locals, expressions or monitors,2864// then these get recorded as no information and avoids the allocation of 0 length arrays.2865GrowableArray<ScopeValue*>* locals = NULL;2866GrowableArray<ScopeValue*>* expressions = NULL;2867GrowableArray<MonitorValue*>* monitors = NULL;28682869// describe local variable values2870int nof_locals = cur_state->locals_size();2871if (nof_locals > 0) {2872locals = new GrowableArray<ScopeValue*>(nof_locals);28732874int pos = 0;2875while (pos < nof_locals) {2876assert(pos < cur_state->locals_size(), "why not?");28772878Value local = cur_state->local_at(pos);2879pos += append_scope_value(op_id, local, locals);28802881assert(locals->length() == pos, "must match");2882}2883assert(locals->length() == cur_scope->method()->max_locals(), "wrong number of locals");2884assert(locals->length() == cur_state->locals_size(), "wrong number of locals");2885} else if (cur_scope->method()->max_locals() > 0) {2886assert(cur_state->kind() == ValueStack::EmptyExceptionState, "should be");2887nof_locals = cur_scope->method()->max_locals();2888locals = new GrowableArray<ScopeValue*>(nof_locals);2889for(int i = 0; i < nof_locals; i++) {2890locals->append(_illegal_value);2891}2892}28932894// describe expression stack2895int nof_stack = cur_state->stack_size();2896if (nof_stack > 0) {2897expressions = new GrowableArray<ScopeValue*>(nof_stack);28982899int pos = 0;2900while (pos < nof_stack) {2901Value expression = cur_state->stack_at_inc(pos);2902append_scope_value(op_id, expression, expressions);29032904assert(expressions->length() == pos, "must match");2905}2906assert(expressions->length() == cur_state->stack_size(), "wrong number of stack entries");2907}29082909// describe monitors2910int nof_locks = cur_state->locks_size();2911if (nof_locks > 0) {2912int lock_offset = cur_state->caller_state() != NULL ? cur_state->caller_state()->total_locks_size() : 0;2913monitors = new GrowableArray<MonitorValue*>(nof_locks);2914for (int i = 0; i < nof_locks; i++) {2915monitors->append(location_for_monitor_index(lock_offset + i));2916}2917}29182919return new IRScopeDebugInfo(cur_scope, cur_state->bci(), locals, expressions, monitors, caller_debug_info);2920}292129222923void LinearScan::compute_debug_info(CodeEmitInfo* info, int op_id) {2924TRACE_LINEAR_SCAN(3, tty->print_cr("creating debug information at op_id %d", op_id));29252926IRScope* innermost_scope = info->scope();2927ValueStack* innermost_state = info->stack();29282929assert(innermost_scope != NULL && innermost_state != NULL, "why is it missing?");29302931DEBUG_ONLY(check_stack_depth(info, innermost_state->stack_size()));29322933if (info->_scope_debug_info == NULL) {2934// compute debug information2935info->_scope_debug_info = compute_debug_info_for_scope(op_id, innermost_scope, innermost_state, innermost_state);2936} else {2937// debug information already set. Check that it is correct from the current point of view2938DEBUG_ONLY(assert_equal(info->_scope_debug_info, compute_debug_info_for_scope(op_id, innermost_scope, innermost_state, innermost_state)));2939}2940}294129422943void LinearScan::assign_reg_num(LIR_OpList* instructions, IntervalWalker* iw) {2944LIR_OpVisitState visitor;2945int num_inst = instructions->length();2946bool has_dead = false;29472948for (int j = 0; j < num_inst; j++) {2949LIR_Op* op = instructions->at(j);2950if (op == NULL) { // this can happen when spill-moves are removed in eliminate_spill_moves2951has_dead = true;2952continue;2953}2954int op_id = op->id();29552956// visit instruction to get list of operands2957visitor.visit(op);29582959// iterate all modes of the visitor and process all virtual operands2960for_each_visitor_mode(mode) {2961int n = visitor.opr_count(mode);2962for (int k = 0; k < n; k++) {2963LIR_Opr opr = visitor.opr_at(mode, k);2964if (opr->is_virtual_register()) {2965visitor.set_opr_at(mode, k, color_lir_opr(opr, op_id, mode));2966}2967}2968}29692970if (visitor.info_count() > 0) {2971// exception handling2972if (compilation()->has_exception_handlers()) {2973XHandlers* xhandlers = visitor.all_xhandler();2974int n = xhandlers->length();2975for (int k = 0; k < n; k++) {2976XHandler* handler = xhandlers->handler_at(k);2977if (handler->entry_code() != NULL) {2978assign_reg_num(handler->entry_code()->instructions_list(), NULL);2979}2980}2981} else {2982assert(visitor.all_xhandler()->length() == 0, "missed exception handler");2983}29842985// compute oop map2986assert(iw != NULL, "needed for compute_oop_map");2987compute_oop_map(iw, visitor, op);29882989// compute debug information2990if (!use_fpu_stack_allocation()) {2991// compute debug information if fpu stack allocation is not needed.2992// when fpu stack allocation is needed, the debug information can not2993// be computed here because the exact location of fpu operands is not known2994// -> debug information is created inside the fpu stack allocator2995int n = visitor.info_count();2996for (int k = 0; k < n; k++) {2997compute_debug_info(visitor.info_at(k), op_id);2998}2999}3000}30013002#ifdef ASSERT3003// make sure we haven't made the op invalid.3004op->verify();3005#endif30063007// remove useless moves3008if (op->code() == lir_move) {3009assert(op->as_Op1() != NULL, "move must be LIR_Op1");3010LIR_Op1* move = (LIR_Op1*)op;3011LIR_Opr src = move->in_opr();3012LIR_Opr dst = move->result_opr();3013if (dst == src ||3014!dst->is_pointer() && !src->is_pointer() &&3015src->is_same_register(dst)) {3016instructions->at_put(j, NULL);3017has_dead = true;3018}3019}3020}30213022if (has_dead) {3023// iterate all instructions of the block and remove all null-values.3024int insert_point = 0;3025for (int j = 0; j < num_inst; j++) {3026LIR_Op* op = instructions->at(j);3027if (op != NULL) {3028if (insert_point != j) {3029instructions->at_put(insert_point, op);3030}3031insert_point++;3032}3033}3034instructions->truncate(insert_point);3035}3036}30373038void LinearScan::assign_reg_num() {3039TIME_LINEAR_SCAN(timer_assign_reg_num);30403041init_compute_debug_info();3042IntervalWalker* iw = init_compute_oop_maps();30433044int num_blocks = block_count();3045for (int i = 0; i < num_blocks; i++) {3046BlockBegin* block = block_at(i);3047assign_reg_num(block->lir()->instructions_list(), iw);3048}3049}305030513052void LinearScan::do_linear_scan() {3053NOT_PRODUCT(_total_timer.begin_method());30543055number_instructions();30563057NOT_PRODUCT(print_lir(1, "Before Register Allocation"));30583059compute_local_live_sets();3060compute_global_live_sets();3061CHECK_BAILOUT();30623063build_intervals();3064CHECK_BAILOUT();3065sort_intervals_before_allocation();30663067NOT_PRODUCT(print_intervals("Before Register Allocation"));3068NOT_PRODUCT(LinearScanStatistic::compute(this, _stat_before_alloc));30693070allocate_registers();3071CHECK_BAILOUT();30723073resolve_data_flow();3074if (compilation()->has_exception_handlers()) {3075resolve_exception_handlers();3076}3077// fill in number of spill slots into frame_map3078propagate_spill_slots();3079CHECK_BAILOUT();30803081NOT_PRODUCT(print_intervals("After Register Allocation"));3082NOT_PRODUCT(print_lir(2, "LIR after register allocation:"));30833084sort_intervals_after_allocation();30853086DEBUG_ONLY(verify());30873088eliminate_spill_moves();3089assign_reg_num();3090CHECK_BAILOUT();30913092NOT_PRODUCT(print_lir(2, "LIR after assignment of register numbers:"));3093NOT_PRODUCT(LinearScanStatistic::compute(this, _stat_after_asign));30943095{ TIME_LINEAR_SCAN(timer_allocate_fpu_stack);30963097if (use_fpu_stack_allocation()) {3098allocate_fpu_stack(); // Only has effect on Intel3099NOT_PRODUCT(print_lir(2, "LIR after FPU stack allocation:"));3100}3101}31023103{ TIME_LINEAR_SCAN(timer_optimize_lir);31043105EdgeMoveOptimizer::optimize(ir()->code());3106ControlFlowOptimizer::optimize(ir()->code());3107// check that cfg is still correct after optimizations3108ir()->verify();3109}31103111NOT_PRODUCT(print_lir(1, "Before Code Generation", false));3112NOT_PRODUCT(LinearScanStatistic::compute(this, _stat_final));3113NOT_PRODUCT(_total_timer.end_method(this));3114}311531163117// ********** Printing functions31183119#ifndef PRODUCT31203121void LinearScan::print_timers(double total) {3122_total_timer.print(total);3123}31243125void LinearScan::print_statistics() {3126_stat_before_alloc.print("before allocation");3127_stat_after_asign.print("after assignment of register");3128_stat_final.print("after optimization");3129}31303131void LinearScan::print_bitmap(BitMap& b) {3132for (unsigned int i = 0; i < b.size(); i++) {3133if (b.at(i)) tty->print("%d ", i);3134}3135tty->cr();3136}31373138void LinearScan::print_intervals(const char* label) {3139if (TraceLinearScanLevel >= 1) {3140int i;3141tty->cr();3142tty->print_cr("%s", label);31433144for (i = 0; i < interval_count(); i++) {3145Interval* interval = interval_at(i);3146if (interval != NULL) {3147interval->print();3148}3149}31503151tty->cr();3152tty->print_cr("--- Basic Blocks ---");3153for (i = 0; i < block_count(); i++) {3154BlockBegin* block = block_at(i);3155tty->print("B%d [%d, %d, %d, %d] ", block->block_id(), block->first_lir_instruction_id(), block->last_lir_instruction_id(), block->loop_index(), block->loop_depth());3156}3157tty->cr();3158tty->cr();3159}31603161if (PrintCFGToFile) {3162CFGPrinter::print_intervals(&_intervals, label);3163}3164}31653166void LinearScan::print_lir(int level, const char* label, bool hir_valid) {3167if (TraceLinearScanLevel >= level) {3168tty->cr();3169tty->print_cr("%s", label);3170print_LIR(ir()->linear_scan_order());3171tty->cr();3172}31733174if (level == 1 && PrintCFGToFile) {3175CFGPrinter::print_cfg(ir()->linear_scan_order(), label, hir_valid, true);3176}3177}31783179#endif //PRODUCT318031813182// ********** verification functions for allocation3183// (check that all intervals have a correct register and that no registers are overwritten)3184#ifdef ASSERT31853186void LinearScan::verify() {3187TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying intervals ******************************************"));3188verify_intervals();31893190TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying that no oops are in fixed intervals ****************"));3191verify_no_oops_in_fixed_intervals();31923193TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying that unpinned constants are not alive across block boundaries"));3194verify_constants();31953196TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying register allocation ********************************"));3197verify_registers();31983199TRACE_LINEAR_SCAN(2, tty->print_cr("********* no errors found **********************************************"));3200}32013202void LinearScan::verify_intervals() {3203int len = interval_count();3204bool has_error = false;32053206for (int i = 0; i < len; i++) {3207Interval* i1 = interval_at(i);3208if (i1 == NULL) continue;32093210i1->check_split_children();32113212if (i1->reg_num() != i) {3213tty->print_cr("Interval %d is on position %d in list", i1->reg_num(), i); i1->print(); tty->cr();3214has_error = true;3215}32163217if (i1->reg_num() >= LIR_OprDesc::vreg_base && i1->type() == T_ILLEGAL) {3218tty->print_cr("Interval %d has no type assigned", i1->reg_num()); i1->print(); tty->cr();3219has_error = true;3220}32213222if (i1->assigned_reg() == any_reg) {3223tty->print_cr("Interval %d has no register assigned", i1->reg_num()); i1->print(); tty->cr();3224has_error = true;3225}32263227if (i1->assigned_reg() == i1->assigned_regHi()) {3228tty->print_cr("Interval %d: low and high register equal", i1->reg_num()); i1->print(); tty->cr();3229has_error = true;3230}32313232if (!is_processed_reg_num(i1->assigned_reg())) {3233tty->print_cr("Can not have an Interval for an ignored register"); i1->print(); tty->cr();3234has_error = true;3235}32363237if (i1->first() == Range::end()) {3238tty->print_cr("Interval %d has no Range", i1->reg_num()); i1->print(); tty->cr();3239has_error = true;3240}32413242for (Range* r = i1->first(); r != Range::end(); r = r->next()) {3243if (r->from() >= r->to()) {3244tty->print_cr("Interval %d has zero length range", i1->reg_num()); i1->print(); tty->cr();3245has_error = true;3246}3247}32483249for (int j = i + 1; j < len; j++) {3250Interval* i2 = interval_at(j);3251if (i2 == NULL) continue;32523253// special intervals that are created in MoveResolver3254// -> ignore them because the range information has no meaning there3255if (i1->from() == 1 && i1->to() == 2) continue;3256if (i2->from() == 1 && i2->to() == 2) continue;32573258int r1 = i1->assigned_reg();3259int r1Hi = i1->assigned_regHi();3260int r2 = i2->assigned_reg();3261int r2Hi = i2->assigned_regHi();3262if (i1->intersects(i2) && (r1 == r2 || r1 == r2Hi || (r1Hi != any_reg && (r1Hi == r2 || r1Hi == r2Hi)))) {3263tty->print_cr("Intervals %d and %d overlap and have the same register assigned", i1->reg_num(), i2->reg_num());3264i1->print(); tty->cr();3265i2->print(); tty->cr();3266has_error = true;3267}3268}3269}32703271assert(has_error == false, "register allocation invalid");3272}327332743275void LinearScan::verify_no_oops_in_fixed_intervals() {3276Interval* fixed_intervals;3277Interval* other_intervals;3278create_unhandled_lists(&fixed_intervals, &other_intervals, is_precolored_cpu_interval, NULL);32793280// to ensure a walking until the last instruction id, add a dummy interval3281// with a high operation id3282other_intervals = new Interval(any_reg);3283other_intervals->add_range(max_jint - 2, max_jint - 1);3284IntervalWalker* iw = new IntervalWalker(this, fixed_intervals, other_intervals);32853286LIR_OpVisitState visitor;3287for (int i = 0; i < block_count(); i++) {3288BlockBegin* block = block_at(i);32893290LIR_OpList* instructions = block->lir()->instructions_list();32913292for (int j = 0; j < instructions->length(); j++) {3293LIR_Op* op = instructions->at(j);3294int op_id = op->id();32953296visitor.visit(op);32973298if (visitor.info_count() > 0) {3299iw->walk_before(op->id());3300bool check_live = true;3301if (op->code() == lir_move) {3302LIR_Op1* move = (LIR_Op1*)op;3303check_live = (move->patch_code() == lir_patch_none);3304}3305LIR_OpBranch* branch = op->as_OpBranch();3306if (branch != NULL && branch->stub() != NULL && branch->stub()->is_exception_throw_stub()) {3307// Don't bother checking the stub in this case since the3308// exception stub will never return to normal control flow.3309check_live = false;3310}33113312// Make sure none of the fixed registers is live across an3313// oopmap since we can't handle that correctly.3314if (check_live) {3315for (Interval* interval = iw->active_first(fixedKind);3316interval != Interval::end();3317interval = interval->next()) {3318if (interval->current_to() > op->id() + 1) {3319// This interval is live out of this op so make sure3320// that this interval represents some value that's3321// referenced by this op either as an input or output.3322bool ok = false;3323for_each_visitor_mode(mode) {3324int n = visitor.opr_count(mode);3325for (int k = 0; k < n; k++) {3326LIR_Opr opr = visitor.opr_at(mode, k);3327if (opr->is_fixed_cpu()) {3328if (interval_at(reg_num(opr)) == interval) {3329ok = true;3330break;3331}3332int hi = reg_numHi(opr);3333if (hi != -1 && interval_at(hi) == interval) {3334ok = true;3335break;3336}3337}3338}3339}3340assert(ok, "fixed intervals should never be live across an oopmap point");3341}3342}3343}3344}33453346// oop-maps at calls do not contain registers, so check is not needed3347if (!visitor.has_call()) {33483349for_each_visitor_mode(mode) {3350int n = visitor.opr_count(mode);3351for (int k = 0; k < n; k++) {3352LIR_Opr opr = visitor.opr_at(mode, k);33533354if (opr->is_fixed_cpu() && opr->is_oop()) {3355// operand is a non-virtual cpu register and contains an oop3356TRACE_LINEAR_SCAN(4, op->print_on(tty); tty->print("checking operand "); opr->print(); tty->cr());33573358Interval* interval = interval_at(reg_num(opr));3359assert(interval != NULL, "no interval");33603361if (mode == LIR_OpVisitState::inputMode) {3362if (interval->to() >= op_id + 1) {3363assert(interval->to() < op_id + 2 ||3364interval->has_hole_between(op_id, op_id + 2),3365"oop input operand live after instruction");3366}3367} else if (mode == LIR_OpVisitState::outputMode) {3368if (interval->from() <= op_id - 1) {3369assert(interval->has_hole_between(op_id - 1, op_id),3370"oop input operand live after instruction");3371}3372}3373}3374}3375}3376}3377}3378}3379}338033813382void LinearScan::verify_constants() {3383int num_regs = num_virtual_regs();3384int size = live_set_size();3385int num_blocks = block_count();33863387for (int i = 0; i < num_blocks; i++) {3388BlockBegin* block = block_at(i);3389BitMap live_at_edge = block->live_in();33903391// visit all registers where the live_at_edge bit is set3392for (int r = (int)live_at_edge.get_next_one_offset(0, size); r < size; r = (int)live_at_edge.get_next_one_offset(r + 1, size)) {3393TRACE_LINEAR_SCAN(4, tty->print("checking interval %d of block B%d", r, block->block_id()));33943395Value value = gen()->instruction_for_vreg(r);33963397assert(value != NULL, "all intervals live across block boundaries must have Value");3398assert(value->operand()->is_register() && value->operand()->is_virtual(), "value must have virtual operand");3399assert(value->operand()->vreg_number() == r, "register number must match");3400// TKR assert(value->as_Constant() == NULL || value->is_pinned(), "only pinned constants can be alive accross block boundaries");3401}3402}3403}340434053406class RegisterVerifier: public StackObj {3407private:3408LinearScan* _allocator;3409BlockList _work_list; // all blocks that must be processed3410IntervalsList _saved_states; // saved information of previous check34113412// simplified access to methods of LinearScan3413Compilation* compilation() const { return _allocator->compilation(); }3414Interval* interval_at(int reg_num) const { return _allocator->interval_at(reg_num); }3415int reg_num(LIR_Opr opr) const { return _allocator->reg_num(opr); }34163417// currently, only registers are processed3418int state_size() { return LinearScan::nof_regs; }34193420// accessors3421IntervalList* state_for_block(BlockBegin* block) { return _saved_states.at(block->block_id()); }3422void set_state_for_block(BlockBegin* block, IntervalList* saved_state) { _saved_states.at_put(block->block_id(), saved_state); }3423void add_to_work_list(BlockBegin* block) { if (!_work_list.contains(block)) _work_list.append(block); }34243425// helper functions3426IntervalList* copy(IntervalList* input_state);3427void state_put(IntervalList* input_state, int reg, Interval* interval);3428bool check_state(IntervalList* input_state, int reg, Interval* interval);34293430void process_block(BlockBegin* block);3431void process_xhandler(XHandler* xhandler, IntervalList* input_state);3432void process_successor(BlockBegin* block, IntervalList* input_state);3433void process_operations(LIR_List* ops, IntervalList* input_state);34343435public:3436RegisterVerifier(LinearScan* allocator)3437: _allocator(allocator)3438, _work_list(16)3439, _saved_states(BlockBegin::number_of_blocks(), NULL)3440{ }34413442void verify(BlockBegin* start);3443};344434453446// entry function from LinearScan that starts the verification3447void LinearScan::verify_registers() {3448RegisterVerifier verifier(this);3449verifier.verify(block_at(0));3450}345134523453void RegisterVerifier::verify(BlockBegin* start) {3454// setup input registers (method arguments) for first block3455IntervalList* input_state = new IntervalList(state_size(), NULL);3456CallingConvention* args = compilation()->frame_map()->incoming_arguments();3457for (int n = 0; n < args->length(); n++) {3458LIR_Opr opr = args->at(n);3459if (opr->is_register()) {3460Interval* interval = interval_at(reg_num(opr));34613462if (interval->assigned_reg() < state_size()) {3463input_state->at_put(interval->assigned_reg(), interval);3464}3465if (interval->assigned_regHi() != LinearScan::any_reg && interval->assigned_regHi() < state_size()) {3466input_state->at_put(interval->assigned_regHi(), interval);3467}3468}3469}34703471set_state_for_block(start, input_state);3472add_to_work_list(start);34733474// main loop for verification3475do {3476BlockBegin* block = _work_list.at(0);3477_work_list.remove_at(0);34783479process_block(block);3480} while (!_work_list.is_empty());3481}34823483void RegisterVerifier::process_block(BlockBegin* block) {3484TRACE_LINEAR_SCAN(2, tty->cr(); tty->print_cr("process_block B%d", block->block_id()));34853486// must copy state because it is modified3487IntervalList* input_state = copy(state_for_block(block));34883489if (TraceLinearScanLevel >= 4) {3490tty->print_cr("Input-State of intervals:");3491tty->print(" ");3492for (int i = 0; i < state_size(); i++) {3493if (input_state->at(i) != NULL) {3494tty->print(" %4d", input_state->at(i)->reg_num());3495} else {3496tty->print(" __");3497}3498}3499tty->cr();3500tty->cr();3501}35023503// process all operations of the block3504process_operations(block->lir(), input_state);35053506// iterate all successors3507for (int i = 0; i < block->number_of_sux(); i++) {3508process_successor(block->sux_at(i), input_state);3509}3510}35113512void RegisterVerifier::process_xhandler(XHandler* xhandler, IntervalList* input_state) {3513TRACE_LINEAR_SCAN(2, tty->print_cr("process_xhandler B%d", xhandler->entry_block()->block_id()));35143515// must copy state because it is modified3516input_state = copy(input_state);35173518if (xhandler->entry_code() != NULL) {3519process_operations(xhandler->entry_code(), input_state);3520}3521process_successor(xhandler->entry_block(), input_state);3522}35233524void RegisterVerifier::process_successor(BlockBegin* block, IntervalList* input_state) {3525IntervalList* saved_state = state_for_block(block);35263527if (saved_state != NULL) {3528// this block was already processed before.3529// check if new input_state is consistent with saved_state35303531bool saved_state_correct = true;3532for (int i = 0; i < state_size(); i++) {3533if (input_state->at(i) != saved_state->at(i)) {3534// current input_state and previous saved_state assume a different3535// interval in this register -> assume that this register is invalid3536if (saved_state->at(i) != NULL) {3537// invalidate old calculation only if it assumed that3538// register was valid. when the register was already invalid,3539// then the old calculation was correct.3540saved_state_correct = false;3541saved_state->at_put(i, NULL);35423543TRACE_LINEAR_SCAN(4, tty->print_cr("process_successor B%d: invalidating slot %d", block->block_id(), i));3544}3545}3546}35473548if (saved_state_correct) {3549// already processed block with correct input_state3550TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: previous visit already correct", block->block_id()));3551} else {3552// must re-visit this block3553TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: must re-visit because input state changed", block->block_id()));3554add_to_work_list(block);3555}35563557} else {3558// block was not processed before, so set initial input_state3559TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: initial visit", block->block_id()));35603561set_state_for_block(block, copy(input_state));3562add_to_work_list(block);3563}3564}356535663567IntervalList* RegisterVerifier::copy(IntervalList* input_state) {3568IntervalList* copy_state = new IntervalList(input_state->length());3569copy_state->push_all(input_state);3570return copy_state;3571}35723573void RegisterVerifier::state_put(IntervalList* input_state, int reg, Interval* interval) {3574if (reg != LinearScan::any_reg && reg < state_size()) {3575if (interval != NULL) {3576TRACE_LINEAR_SCAN(4, tty->print_cr(" reg[%d] = %d", reg, interval->reg_num()));3577} else if (input_state->at(reg) != NULL) {3578TRACE_LINEAR_SCAN(4, tty->print_cr(" reg[%d] = NULL", reg));3579}35803581input_state->at_put(reg, interval);3582}3583}35843585bool RegisterVerifier::check_state(IntervalList* input_state, int reg, Interval* interval) {3586if (reg != LinearScan::any_reg && reg < state_size()) {3587if (input_state->at(reg) != interval) {3588tty->print_cr("!! Error in register allocation: register %d does not contain interval %d", reg, interval->reg_num());3589return true;3590}3591}3592return false;3593}35943595void RegisterVerifier::process_operations(LIR_List* ops, IntervalList* input_state) {3596// visit all instructions of the block3597LIR_OpVisitState visitor;3598bool has_error = false;35993600for (int i = 0; i < ops->length(); i++) {3601LIR_Op* op = ops->at(i);3602visitor.visit(op);36033604TRACE_LINEAR_SCAN(4, op->print_on(tty));36053606// check if input operands are correct3607int j;3608int n = visitor.opr_count(LIR_OpVisitState::inputMode);3609for (j = 0; j < n; j++) {3610LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::inputMode, j);3611if (opr->is_register() && LinearScan::is_processed_reg_num(reg_num(opr))) {3612Interval* interval = interval_at(reg_num(opr));3613if (op->id() != -1) {3614interval = interval->split_child_at_op_id(op->id(), LIR_OpVisitState::inputMode);3615}36163617has_error |= check_state(input_state, interval->assigned_reg(), interval->split_parent());3618has_error |= check_state(input_state, interval->assigned_regHi(), interval->split_parent());36193620// When an operand is marked with is_last_use, then the fpu stack allocator3621// removes the register from the fpu stack -> the register contains no value3622if (opr->is_last_use()) {3623state_put(input_state, interval->assigned_reg(), NULL);3624state_put(input_state, interval->assigned_regHi(), NULL);3625}3626}3627}36283629// invalidate all caller save registers at calls3630if (visitor.has_call()) {3631for (j = 0; j < FrameMap::nof_caller_save_cpu_regs(); j++) {3632state_put(input_state, reg_num(FrameMap::caller_save_cpu_reg_at(j)), NULL);3633}3634for (j = 0; j < FrameMap::nof_caller_save_fpu_regs; j++) {3635state_put(input_state, reg_num(FrameMap::caller_save_fpu_reg_at(j)), NULL);3636}36373638#ifdef X863639for (j = 0; j < FrameMap::nof_caller_save_xmm_regs; j++) {3640state_put(input_state, reg_num(FrameMap::caller_save_xmm_reg_at(j)), NULL);3641}3642#endif3643}36443645// process xhandler before output and temp operands3646XHandlers* xhandlers = visitor.all_xhandler();3647n = xhandlers->length();3648for (int k = 0; k < n; k++) {3649process_xhandler(xhandlers->handler_at(k), input_state);3650}36513652// set temp operands (some operations use temp operands also as output operands, so can't set them NULL)3653n = visitor.opr_count(LIR_OpVisitState::tempMode);3654for (j = 0; j < n; j++) {3655LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::tempMode, j);3656if (opr->is_register() && LinearScan::is_processed_reg_num(reg_num(opr))) {3657Interval* interval = interval_at(reg_num(opr));3658if (op->id() != -1) {3659interval = interval->split_child_at_op_id(op->id(), LIR_OpVisitState::tempMode);3660}36613662state_put(input_state, interval->assigned_reg(), interval->split_parent());3663state_put(input_state, interval->assigned_regHi(), interval->split_parent());3664}3665}36663667// set output operands3668n = visitor.opr_count(LIR_OpVisitState::outputMode);3669for (j = 0; j < n; j++) {3670LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::outputMode, j);3671if (opr->is_register() && LinearScan::is_processed_reg_num(reg_num(opr))) {3672Interval* interval = interval_at(reg_num(opr));3673if (op->id() != -1) {3674interval = interval->split_child_at_op_id(op->id(), LIR_OpVisitState::outputMode);3675}36763677state_put(input_state, interval->assigned_reg(), interval->split_parent());3678state_put(input_state, interval->assigned_regHi(), interval->split_parent());3679}3680}3681}3682assert(has_error == false, "Error in register allocation");3683}36843685#endif // ASSERT3686368736883689// **** Implementation of MoveResolver ******************************36903691MoveResolver::MoveResolver(LinearScan* allocator) :3692_allocator(allocator),3693_multiple_reads_allowed(false),3694_mapping_from(8),3695_mapping_from_opr(8),3696_mapping_to(8),3697_insert_list(NULL),3698_insert_idx(-1),3699_insertion_buffer()3700{3701for (int i = 0; i < LinearScan::nof_regs; i++) {3702_register_blocked[i] = 0;3703}3704DEBUG_ONLY(check_empty());3705}370637073708#ifdef ASSERT37093710void MoveResolver::check_empty() {3711assert(_mapping_from.length() == 0 && _mapping_from_opr.length() == 0 && _mapping_to.length() == 0, "list must be empty before and after processing");3712for (int i = 0; i < LinearScan::nof_regs; i++) {3713assert(register_blocked(i) == 0, "register map must be empty before and after processing");3714}3715assert(_multiple_reads_allowed == false, "must have default value");3716}37173718void MoveResolver::verify_before_resolve() {3719assert(_mapping_from.length() == _mapping_from_opr.length(), "length must be equal");3720assert(_mapping_from.length() == _mapping_to.length(), "length must be equal");3721assert(_insert_list != NULL && _insert_idx != -1, "insert position not set");37223723int i, j;3724if (!_multiple_reads_allowed) {3725for (i = 0; i < _mapping_from.length(); i++) {3726for (j = i + 1; j < _mapping_from.length(); j++) {3727assert(_mapping_from.at(i) == NULL || _mapping_from.at(i) != _mapping_from.at(j), "cannot read from same interval twice");3728}3729}3730}37313732for (i = 0; i < _mapping_to.length(); i++) {3733for (j = i + 1; j < _mapping_to.length(); j++) {3734assert(_mapping_to.at(i) != _mapping_to.at(j), "cannot write to same interval twice");3735}3736}373737383739BitMap used_regs(LinearScan::nof_regs + allocator()->frame_map()->argcount() + allocator()->max_spills());3740used_regs.clear();3741if (!_multiple_reads_allowed) {3742for (i = 0; i < _mapping_from.length(); i++) {3743Interval* it = _mapping_from.at(i);3744if (it != NULL) {3745assert(!used_regs.at(it->assigned_reg()), "cannot read from same register twice");3746used_regs.set_bit(it->assigned_reg());37473748if (it->assigned_regHi() != LinearScan::any_reg) {3749assert(!used_regs.at(it->assigned_regHi()), "cannot read from same register twice");3750used_regs.set_bit(it->assigned_regHi());3751}3752}3753}3754}37553756used_regs.clear();3757for (i = 0; i < _mapping_to.length(); i++) {3758Interval* it = _mapping_to.at(i);3759assert(!used_regs.at(it->assigned_reg()), "cannot write to same register twice");3760used_regs.set_bit(it->assigned_reg());37613762if (it->assigned_regHi() != LinearScan::any_reg) {3763assert(!used_regs.at(it->assigned_regHi()), "cannot write to same register twice");3764used_regs.set_bit(it->assigned_regHi());3765}3766}37673768used_regs.clear();3769for (i = 0; i < _mapping_from.length(); i++) {3770Interval* it = _mapping_from.at(i);3771if (it != NULL && it->assigned_reg() >= LinearScan::nof_regs) {3772used_regs.set_bit(it->assigned_reg());3773}3774}3775for (i = 0; i < _mapping_to.length(); i++) {3776Interval* it = _mapping_to.at(i);3777assert(!used_regs.at(it->assigned_reg()) || it->assigned_reg() == _mapping_from.at(i)->assigned_reg(), "stack slots used in _mapping_from must be disjoint to _mapping_to");3778}3779}37803781#endif // ASSERT378237833784// mark assigned_reg and assigned_regHi of the interval as blocked3785void MoveResolver::block_registers(Interval* it) {3786int reg = it->assigned_reg();3787if (reg < LinearScan::nof_regs) {3788assert(_multiple_reads_allowed || register_blocked(reg) == 0, "register already marked as used");3789set_register_blocked(reg, 1);3790}3791reg = it->assigned_regHi();3792if (reg != LinearScan::any_reg && reg < LinearScan::nof_regs) {3793assert(_multiple_reads_allowed || register_blocked(reg) == 0, "register already marked as used");3794set_register_blocked(reg, 1);3795}3796}37973798// mark assigned_reg and assigned_regHi of the interval as unblocked3799void MoveResolver::unblock_registers(Interval* it) {3800int reg = it->assigned_reg();3801if (reg < LinearScan::nof_regs) {3802assert(register_blocked(reg) > 0, "register already marked as unused");3803set_register_blocked(reg, -1);3804}3805reg = it->assigned_regHi();3806if (reg != LinearScan::any_reg && reg < LinearScan::nof_regs) {3807assert(register_blocked(reg) > 0, "register already marked as unused");3808set_register_blocked(reg, -1);3809}3810}38113812// check if assigned_reg and assigned_regHi of the to-interval are not blocked (or only blocked by from)3813bool MoveResolver::save_to_process_move(Interval* from, Interval* to) {3814int from_reg = -1;3815int from_regHi = -1;3816if (from != NULL) {3817from_reg = from->assigned_reg();3818from_regHi = from->assigned_regHi();3819}38203821int reg = to->assigned_reg();3822if (reg < LinearScan::nof_regs) {3823if (register_blocked(reg) > 1 || (register_blocked(reg) == 1 && reg != from_reg && reg != from_regHi)) {3824return false;3825}3826}3827reg = to->assigned_regHi();3828if (reg != LinearScan::any_reg && reg < LinearScan::nof_regs) {3829if (register_blocked(reg) > 1 || (register_blocked(reg) == 1 && reg != from_reg && reg != from_regHi)) {3830return false;3831}3832}38333834return true;3835}383638373838void MoveResolver::create_insertion_buffer(LIR_List* list) {3839assert(!_insertion_buffer.initialized(), "overwriting existing buffer");3840_insertion_buffer.init(list);3841}38423843void MoveResolver::append_insertion_buffer() {3844if (_insertion_buffer.initialized()) {3845_insertion_buffer.lir_list()->append(&_insertion_buffer);3846}3847assert(!_insertion_buffer.initialized(), "must be uninitialized now");38483849_insert_list = NULL;3850_insert_idx = -1;3851}38523853void MoveResolver::insert_move(Interval* from_interval, Interval* to_interval) {3854assert(from_interval->reg_num() != to_interval->reg_num(), "from and to interval equal");3855assert(from_interval->type() == to_interval->type(), "move between different types");3856assert(_insert_list != NULL && _insert_idx != -1, "must setup insert position first");3857assert(_insertion_buffer.lir_list() == _insert_list, "wrong insertion buffer");38583859LIR_Opr from_opr = LIR_OprFact::virtual_register(from_interval->reg_num(), from_interval->type());3860LIR_Opr to_opr = LIR_OprFact::virtual_register(to_interval->reg_num(), to_interval->type());38613862if (!_multiple_reads_allowed) {3863// the last_use flag is an optimization for FPU stack allocation. When the same3864// input interval is used in more than one move, then it is too difficult to determine3865// if this move is really the last use.3866from_opr = from_opr->make_last_use();3867}3868_insertion_buffer.move(_insert_idx, from_opr, to_opr);38693870TRACE_LINEAR_SCAN(4, tty->print_cr("MoveResolver: inserted move from register %d (%d, %d) to %d (%d, %d)", from_interval->reg_num(), from_interval->assigned_reg(), from_interval->assigned_regHi(), to_interval->reg_num(), to_interval->assigned_reg(), to_interval->assigned_regHi()));3871}38723873void MoveResolver::insert_move(LIR_Opr from_opr, Interval* to_interval) {3874assert(from_opr->type() == to_interval->type(), "move between different types");3875assert(_insert_list != NULL && _insert_idx != -1, "must setup insert position first");3876assert(_insertion_buffer.lir_list() == _insert_list, "wrong insertion buffer");38773878LIR_Opr to_opr = LIR_OprFact::virtual_register(to_interval->reg_num(), to_interval->type());3879_insertion_buffer.move(_insert_idx, from_opr, to_opr);38803881TRACE_LINEAR_SCAN(4, tty->print("MoveResolver: inserted move from constant "); from_opr->print(); tty->print_cr(" to %d (%d, %d)", to_interval->reg_num(), to_interval->assigned_reg(), to_interval->assigned_regHi()));3882}388338843885void MoveResolver::resolve_mappings() {3886TRACE_LINEAR_SCAN(4, tty->print_cr("MoveResolver: resolving mappings for Block B%d, index %d", _insert_list->block() != NULL ? _insert_list->block()->block_id() : -1, _insert_idx));3887DEBUG_ONLY(verify_before_resolve());38883889// Block all registers that are used as input operands of a move.3890// When a register is blocked, no move to this register is emitted.3891// This is necessary for detecting cycles in moves.3892int i;3893for (i = _mapping_from.length() - 1; i >= 0; i--) {3894Interval* from_interval = _mapping_from.at(i);3895if (from_interval != NULL) {3896block_registers(from_interval);3897}3898}38993900int spill_candidate = -1;3901while (_mapping_from.length() > 0) {3902bool processed_interval = false;39033904for (i = _mapping_from.length() - 1; i >= 0; i--) {3905Interval* from_interval = _mapping_from.at(i);3906Interval* to_interval = _mapping_to.at(i);39073908if (save_to_process_move(from_interval, to_interval)) {3909// this inverval can be processed because target is free3910if (from_interval != NULL) {3911insert_move(from_interval, to_interval);3912unblock_registers(from_interval);3913} else {3914insert_move(_mapping_from_opr.at(i), to_interval);3915}3916_mapping_from.remove_at(i);3917_mapping_from_opr.remove_at(i);3918_mapping_to.remove_at(i);39193920processed_interval = true;3921} else if (from_interval != NULL && from_interval->assigned_reg() < LinearScan::nof_regs) {3922// this interval cannot be processed now because target is not free3923// it starts in a register, so it is a possible candidate for spilling3924spill_candidate = i;3925}3926}39273928if (!processed_interval) {3929// no move could be processed because there is a cycle in the move list3930// (e.g. r1 -> r2, r2 -> r1), so one interval must be spilled to memory3931assert(spill_candidate != -1, "no interval in register for spilling found");39323933// create a new spill interval and assign a stack slot to it3934Interval* from_interval = _mapping_from.at(spill_candidate);3935Interval* spill_interval = new Interval(-1);3936spill_interval->set_type(from_interval->type());39373938// add a dummy range because real position is difficult to calculate3939// Note: this range is a special case when the integrity of the allocation is checked3940spill_interval->add_range(1, 2);39413942// do not allocate a new spill slot for temporary interval, but3943// use spill slot assigned to from_interval. Otherwise moves from3944// one stack slot to another can happen (not allowed by LIR_Assembler3945int spill_slot = from_interval->canonical_spill_slot();3946if (spill_slot < 0) {3947spill_slot = allocator()->allocate_spill_slot(type2spill_size[spill_interval->type()] == 2);3948from_interval->set_canonical_spill_slot(spill_slot);3949}3950spill_interval->assign_reg(spill_slot);3951allocator()->append_interval(spill_interval);39523953TRACE_LINEAR_SCAN(4, tty->print_cr("created new Interval %d for spilling", spill_interval->reg_num()));39543955// insert a move from register to stack and update the mapping3956insert_move(from_interval, spill_interval);3957_mapping_from.at_put(spill_candidate, spill_interval);3958unblock_registers(from_interval);3959}3960}39613962// reset to default value3963_multiple_reads_allowed = false;39643965// check that all intervals have been processed3966DEBUG_ONLY(check_empty());3967}396839693970void MoveResolver::set_insert_position(LIR_List* insert_list, int insert_idx) {3971TRACE_LINEAR_SCAN(4, tty->print_cr("MoveResolver: setting insert position to Block B%d, index %d", insert_list->block() != NULL ? insert_list->block()->block_id() : -1, insert_idx));3972assert(_insert_list == NULL && _insert_idx == -1, "use move_insert_position instead of set_insert_position when data already set");39733974create_insertion_buffer(insert_list);3975_insert_list = insert_list;3976_insert_idx = insert_idx;3977}39783979void MoveResolver::move_insert_position(LIR_List* insert_list, int insert_idx) {3980TRACE_LINEAR_SCAN(4, tty->print_cr("MoveResolver: moving insert position to Block B%d, index %d", insert_list->block() != NULL ? insert_list->block()->block_id() : -1, insert_idx));39813982if (_insert_list != NULL && (insert_list != _insert_list || insert_idx != _insert_idx)) {3983// insert position changed -> resolve current mappings3984resolve_mappings();3985}39863987if (insert_list != _insert_list) {3988// block changed -> append insertion_buffer because it is3989// bound to a specific block and create a new insertion_buffer3990append_insertion_buffer();3991create_insertion_buffer(insert_list);3992}39933994_insert_list = insert_list;3995_insert_idx = insert_idx;3996}39973998void MoveResolver::add_mapping(Interval* from_interval, Interval* to_interval) {3999TRACE_LINEAR_SCAN(4, tty->print_cr("MoveResolver: adding mapping from %d (%d, %d) to %d (%d, %d)", from_interval->reg_num(), from_interval->assigned_reg(), from_interval->assigned_regHi(), to_interval->reg_num(), to_interval->assigned_reg(), to_interval->assigned_regHi()));40004001_mapping_from.append(from_interval);4002_mapping_from_opr.append(LIR_OprFact::illegalOpr);4003_mapping_to.append(to_interval);4004}400540064007void MoveResolver::add_mapping(LIR_Opr from_opr, Interval* to_interval) {4008TRACE_LINEAR_SCAN(4, tty->print("MoveResolver: adding mapping from "); from_opr->print(); tty->print_cr(" to %d (%d, %d)", to_interval->reg_num(), to_interval->assigned_reg(), to_interval->assigned_regHi()));4009assert(from_opr->is_constant(), "only for constants");40104011_mapping_from.append(NULL);4012_mapping_from_opr.append(from_opr);4013_mapping_to.append(to_interval);4014}40154016void MoveResolver::resolve_and_append_moves() {4017if (has_mappings()) {4018resolve_mappings();4019}4020append_insertion_buffer();4021}4022402340244025// **** Implementation of Range *************************************40264027Range::Range(int from, int to, Range* next) :4028_from(from),4029_to(to),4030_next(next)4031{4032}40334034// initialize sentinel4035Range* Range::_end = NULL;4036void Range::initialize(Arena* arena) {4037_end = new (arena) Range(max_jint, max_jint, NULL);4038}40394040int Range::intersects_at(Range* r2) const {4041const Range* r1 = this;40424043assert(r1 != NULL && r2 != NULL, "null ranges not allowed");4044assert(r1 != _end && r2 != _end, "empty ranges not allowed");40454046do {4047if (r1->from() < r2->from()) {4048if (r1->to() <= r2->from()) {4049r1 = r1->next(); if (r1 == _end) return -1;4050} else {4051return r2->from();4052}4053} else if (r2->from() < r1->from()) {4054if (r2->to() <= r1->from()) {4055r2 = r2->next(); if (r2 == _end) return -1;4056} else {4057return r1->from();4058}4059} else { // r1->from() == r2->from()4060if (r1->from() == r1->to()) {4061r1 = r1->next(); if (r1 == _end) return -1;4062} else if (r2->from() == r2->to()) {4063r2 = r2->next(); if (r2 == _end) return -1;4064} else {4065return r1->from();4066}4067}4068} while (true);4069}40704071#ifndef PRODUCT4072void Range::print(outputStream* out) const {4073out->print("[%d, %d[ ", _from, _to);4074}4075#endif4076407740784079// **** Implementation of Interval **********************************40804081// initialize sentinel4082Interval* Interval::_end = NULL;4083void Interval::initialize(Arena* arena) {4084Range::initialize(arena);4085_end = new (arena) Interval(-1);4086}40874088Interval::Interval(int reg_num) :4089_reg_num(reg_num),4090_type(T_ILLEGAL),4091_first(Range::end()),4092_use_pos_and_kinds(12),4093_current(Range::end()),4094_next(_end),4095_state(invalidState),4096_assigned_reg(LinearScan::any_reg),4097_assigned_regHi(LinearScan::any_reg),4098_cached_to(-1),4099_cached_opr(LIR_OprFact::illegalOpr),4100_cached_vm_reg(VMRegImpl::Bad()),4101_split_children(0),4102_canonical_spill_slot(-1),4103_insert_move_when_activated(false),4104_register_hint(NULL),4105_spill_state(noDefinitionFound),4106_spill_definition_pos(-1)4107{4108_split_parent = this;4109_current_split_child = this;4110}41114112int Interval::calc_to() {4113assert(_first != Range::end(), "interval has no range");41144115Range* r = _first;4116while (r->next() != Range::end()) {4117r = r->next();4118}4119return r->to();4120}412141224123#ifdef ASSERT4124// consistency check of split-children4125void Interval::check_split_children() {4126if (_split_children.length() > 0) {4127assert(is_split_parent(), "only split parents can have children");41284129for (int i = 0; i < _split_children.length(); i++) {4130Interval* i1 = _split_children.at(i);41314132assert(i1->split_parent() == this, "not a split child of this interval");4133assert(i1->type() == type(), "must be equal for all split children");4134assert(i1->canonical_spill_slot() == canonical_spill_slot(), "must be equal for all split children");41354136for (int j = i + 1; j < _split_children.length(); j++) {4137Interval* i2 = _split_children.at(j);41384139assert(i1->reg_num() != i2->reg_num(), "same register number");41404141if (i1->from() < i2->from()) {4142assert(i1->to() <= i2->from() && i1->to() < i2->to(), "intervals overlapping");4143} else {4144assert(i2->from() < i1->from(), "intervals start at same op_id");4145assert(i2->to() <= i1->from() && i2->to() < i1->to(), "intervals overlapping");4146}4147}4148}4149}4150}4151#endif // ASSERT41524153Interval* Interval::register_hint(bool search_split_child) const {4154if (!search_split_child) {4155return _register_hint;4156}41574158if (_register_hint != NULL) {4159assert(_register_hint->is_split_parent(), "ony split parents are valid hint registers");41604161if (_register_hint->assigned_reg() >= 0 && _register_hint->assigned_reg() < LinearScan::nof_regs) {4162return _register_hint;41634164} else if (_register_hint->_split_children.length() > 0) {4165// search the first split child that has a register assigned4166int len = _register_hint->_split_children.length();4167for (int i = 0; i < len; i++) {4168Interval* cur = _register_hint->_split_children.at(i);41694170if (cur->assigned_reg() >= 0 && cur->assigned_reg() < LinearScan::nof_regs) {4171return cur;4172}4173}4174}4175}41764177// no hint interval found that has a register assigned4178return NULL;4179}418041814182Interval* Interval::split_child_at_op_id(int op_id, LIR_OpVisitState::OprMode mode) {4183assert(is_split_parent(), "can only be called for split parents");4184assert(op_id >= 0, "invalid op_id (method can not be called for spill moves)");41854186Interval* result;4187if (_split_children.length() == 0) {4188result = this;4189} else {4190result = NULL;4191int len = _split_children.length();41924193// in outputMode, the end of the interval (op_id == cur->to()) is not valid4194int to_offset = (mode == LIR_OpVisitState::outputMode ? 0 : 1);41954196int i;4197for (i = 0; i < len; i++) {4198Interval* cur = _split_children.at(i);4199if (cur->from() <= op_id && op_id < cur->to() + to_offset) {4200if (i > 0) {4201// exchange current split child to start of list (faster access for next call)4202_split_children.at_put(i, _split_children.at(0));4203_split_children.at_put(0, cur);4204}42054206// interval found4207result = cur;4208break;4209}4210}42114212#ifdef ASSERT4213for (i = 0; i < len; i++) {4214Interval* tmp = _split_children.at(i);4215if (tmp != result && tmp->from() <= op_id && op_id < tmp->to() + to_offset) {4216tty->print_cr("two valid result intervals found for op_id %d: %d and %d", op_id, result->reg_num(), tmp->reg_num());4217result->print();4218tmp->print();4219assert(false, "two valid result intervals found");4220}4221}4222#endif4223}42244225assert(result != NULL, "no matching interval found");4226assert(result->covers(op_id, mode), "op_id not covered by interval");42274228return result;4229}423042314232// returns the last split child that ends before the given op_id4233Interval* Interval::split_child_before_op_id(int op_id) {4234assert(op_id >= 0, "invalid op_id");42354236Interval* parent = split_parent();4237Interval* result = NULL;42384239int len = parent->_split_children.length();4240assert(len > 0, "no split children available");42414242for (int i = len - 1; i >= 0; i--) {4243Interval* cur = parent->_split_children.at(i);4244if (cur->to() <= op_id && (result == NULL || result->to() < cur->to())) {4245result = cur;4246}4247}42484249assert(result != NULL, "no split child found");4250return result;4251}425242534254// checks if op_id is covered by any split child4255bool Interval::split_child_covers(int op_id, LIR_OpVisitState::OprMode mode) {4256assert(is_split_parent(), "can only be called for split parents");4257assert(op_id >= 0, "invalid op_id (method can not be called for spill moves)");42584259if (_split_children.length() == 0) {4260// simple case if interval was not split4261return covers(op_id, mode);42624263} else {4264// extended case: check all split children4265int len = _split_children.length();4266for (int i = 0; i < len; i++) {4267Interval* cur = _split_children.at(i);4268if (cur->covers(op_id, mode)) {4269return true;4270}4271}4272return false;4273}4274}427542764277// Note: use positions are sorted descending -> first use has highest index4278int Interval::first_usage(IntervalUseKind min_use_kind) const {4279assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");42804281for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {4282if (_use_pos_and_kinds.at(i + 1) >= min_use_kind) {4283return _use_pos_and_kinds.at(i);4284}4285}4286return max_jint;4287}42884289int Interval::next_usage(IntervalUseKind min_use_kind, int from) const {4290assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");42914292for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {4293if (_use_pos_and_kinds.at(i) >= from && _use_pos_and_kinds.at(i + 1) >= min_use_kind) {4294return _use_pos_and_kinds.at(i);4295}4296}4297return max_jint;4298}42994300int Interval::next_usage_exact(IntervalUseKind exact_use_kind, int from) const {4301assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");43024303for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {4304if (_use_pos_and_kinds.at(i) >= from && _use_pos_and_kinds.at(i + 1) == exact_use_kind) {4305return _use_pos_and_kinds.at(i);4306}4307}4308return max_jint;4309}43104311int Interval::previous_usage(IntervalUseKind min_use_kind, int from) const {4312assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");43134314int prev = 0;4315for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {4316if (_use_pos_and_kinds.at(i) > from) {4317return prev;4318}4319if (_use_pos_and_kinds.at(i + 1) >= min_use_kind) {4320prev = _use_pos_and_kinds.at(i);4321}4322}4323return prev;4324}43254326void Interval::add_use_pos(int pos, IntervalUseKind use_kind) {4327assert(covers(pos, LIR_OpVisitState::inputMode), "use position not covered by live range");43284329// do not add use positions for precolored intervals because4330// they are never used4331if (use_kind != noUse && reg_num() >= LIR_OprDesc::vreg_base) {4332#ifdef ASSERT4333assert(_use_pos_and_kinds.length() % 2 == 0, "must be");4334for (int i = 0; i < _use_pos_and_kinds.length(); i += 2) {4335assert(pos <= _use_pos_and_kinds.at(i), "already added a use-position with lower position");4336assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");4337if (i > 0) {4338assert(_use_pos_and_kinds.at(i) < _use_pos_and_kinds.at(i - 2), "not sorted descending");4339}4340}4341#endif43424343// Note: add_use is called in descending order, so list gets sorted4344// automatically by just appending new use positions4345int len = _use_pos_and_kinds.length();4346if (len == 0 || _use_pos_and_kinds.at(len - 2) > pos) {4347_use_pos_and_kinds.append(pos);4348_use_pos_and_kinds.append(use_kind);4349} else if (_use_pos_and_kinds.at(len - 1) < use_kind) {4350assert(_use_pos_and_kinds.at(len - 2) == pos, "list not sorted correctly");4351_use_pos_and_kinds.at_put(len - 1, use_kind);4352}4353}4354}43554356void Interval::add_range(int from, int to) {4357assert(from < to, "invalid range");4358assert(first() == Range::end() || to < first()->next()->from(), "not inserting at begin of interval");4359assert(from <= first()->to(), "not inserting at begin of interval");43604361if (first()->from() <= to) {4362// join intersecting ranges4363first()->set_from(MIN2(from, first()->from()));4364first()->set_to (MAX2(to, first()->to()));4365} else {4366// insert new range4367_first = new Range(from, to, first());4368}4369}43704371Interval* Interval::new_split_child() {4372// allocate new interval4373Interval* result = new Interval(-1);4374result->set_type(type());43754376Interval* parent = split_parent();4377result->_split_parent = parent;4378result->set_register_hint(parent);43794380// insert new interval in children-list of parent4381if (parent->_split_children.length() == 0) {4382assert(is_split_parent(), "list must be initialized at first split");43834384parent->_split_children = IntervalList(4);4385parent->_split_children.append(this);4386}4387parent->_split_children.append(result);43884389return result;4390}43914392// split this interval at the specified position and return4393// the remainder as a new interval.4394//4395// when an interval is split, a bi-directional link is established between the original interval4396// (the split parent) and the intervals that are split off this interval (the split children)4397// When a split child is split again, the new created interval is also a direct child4398// of the original parent (there is no tree of split children stored, but a flat list)4399// All split children are spilled to the same stack slot (stored in _canonical_spill_slot)4400//4401// Note: The new interval has no valid reg_num4402Interval* Interval::split(int split_pos) {4403assert(LinearScan::is_virtual_interval(this), "cannot split fixed intervals");44044405// allocate new interval4406Interval* result = new_split_child();44074408// split the ranges4409Range* prev = NULL;4410Range* cur = _first;4411while (cur != Range::end() && cur->to() <= split_pos) {4412prev = cur;4413cur = cur->next();4414}4415assert(cur != Range::end(), "split interval after end of last range");44164417if (cur->from() < split_pos) {4418result->_first = new Range(split_pos, cur->to(), cur->next());4419cur->set_to(split_pos);4420cur->set_next(Range::end());44214422} else {4423assert(prev != NULL, "split before start of first range");4424result->_first = cur;4425prev->set_next(Range::end());4426}4427result->_current = result->_first;4428_cached_to = -1; // clear cached value44294430// split list of use positions4431int total_len = _use_pos_and_kinds.length();4432int start_idx = total_len - 2;4433while (start_idx >= 0 && _use_pos_and_kinds.at(start_idx) < split_pos) {4434start_idx -= 2;4435}44364437intStack new_use_pos_and_kinds(total_len - start_idx);4438int i;4439for (i = start_idx + 2; i < total_len; i++) {4440new_use_pos_and_kinds.append(_use_pos_and_kinds.at(i));4441}44424443_use_pos_and_kinds.truncate(start_idx + 2);4444result->_use_pos_and_kinds = _use_pos_and_kinds;4445_use_pos_and_kinds = new_use_pos_and_kinds;44464447#ifdef ASSERT4448assert(_use_pos_and_kinds.length() % 2 == 0, "must have use kind for each use pos");4449assert(result->_use_pos_and_kinds.length() % 2 == 0, "must have use kind for each use pos");4450assert(_use_pos_and_kinds.length() + result->_use_pos_and_kinds.length() == total_len, "missed some entries");44514452for (i = 0; i < _use_pos_and_kinds.length(); i += 2) {4453assert(_use_pos_and_kinds.at(i) < split_pos, "must be");4454assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");4455}4456for (i = 0; i < result->_use_pos_and_kinds.length(); i += 2) {4457assert(result->_use_pos_and_kinds.at(i) >= split_pos, "must be");4458assert(result->_use_pos_and_kinds.at(i + 1) >= firstValidKind && result->_use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");4459}4460#endif44614462return result;4463}44644465// split this interval at the specified position and return4466// the head as a new interval (the original interval is the tail)4467//4468// Currently, only the first range can be split, and the new interval4469// must not have split positions4470Interval* Interval::split_from_start(int split_pos) {4471assert(LinearScan::is_virtual_interval(this), "cannot split fixed intervals");4472assert(split_pos > from() && split_pos < to(), "can only split inside interval");4473assert(split_pos > _first->from() && split_pos <= _first->to(), "can only split inside first range");4474assert(first_usage(noUse) > split_pos, "can not split when use positions are present");44754476// allocate new interval4477Interval* result = new_split_child();44784479// the new created interval has only one range (checked by assertion above),4480// so the splitting of the ranges is very simple4481result->add_range(_first->from(), split_pos);44824483if (split_pos == _first->to()) {4484assert(_first->next() != Range::end(), "must not be at end");4485_first = _first->next();4486} else {4487_first->set_from(split_pos);4488}44894490return result;4491}449244934494// returns true if the op_id is inside the interval4495bool Interval::covers(int op_id, LIR_OpVisitState::OprMode mode) const {4496Range* cur = _first;44974498while (cur != Range::end() && cur->to() < op_id) {4499cur = cur->next();4500}4501if (cur != Range::end()) {4502assert(cur->to() != cur->next()->from(), "ranges not separated");45034504if (mode == LIR_OpVisitState::outputMode) {4505return cur->from() <= op_id && op_id < cur->to();4506} else {4507return cur->from() <= op_id && op_id <= cur->to();4508}4509}4510return false;4511}45124513// returns true if the interval has any hole between hole_from and hole_to4514// (even if the hole has only the length 1)4515bool Interval::has_hole_between(int hole_from, int hole_to) {4516assert(hole_from < hole_to, "check");4517assert(from() <= hole_from && hole_to <= to(), "index out of interval");45184519Range* cur = _first;4520while (cur != Range::end()) {4521assert(cur->to() < cur->next()->from(), "no space between ranges");45224523// hole-range starts before this range -> hole4524if (hole_from < cur->from()) {4525return true;45264527// hole-range completely inside this range -> no hole4528} else if (hole_to <= cur->to()) {4529return false;45304531// overlapping of hole-range with this range -> hole4532} else if (hole_from <= cur->to()) {4533return true;4534}45354536cur = cur->next();4537}45384539return false;4540}454145424543#ifndef PRODUCT4544void Interval::print(outputStream* out) const {4545const char* SpillState2Name[] = { "no definition", "no spill store", "one spill store", "store at definition", "start in memory", "no optimization" };4546const char* UseKind2Name[] = { "N", "L", "S", "M" };45474548const char* type_name;4549LIR_Opr opr = LIR_OprFact::illegal();4550if (reg_num() < LIR_OprDesc::vreg_base) {4551type_name = "fixed";4552// need a temporary operand for fixed intervals because type() cannot be called4553if (assigned_reg() >= pd_first_cpu_reg && assigned_reg() <= pd_last_cpu_reg) {4554opr = LIR_OprFact::single_cpu(assigned_reg());4555} else if (assigned_reg() >= pd_first_fpu_reg && assigned_reg() <= pd_last_fpu_reg) {4556opr = LIR_OprFact::single_fpu(assigned_reg() - pd_first_fpu_reg);4557#ifdef X864558} else if (assigned_reg() >= pd_first_xmm_reg && assigned_reg() <= pd_last_xmm_reg) {4559opr = LIR_OprFact::single_xmm(assigned_reg() - pd_first_xmm_reg);4560#endif4561} else {4562#if !defined(AARCH64)4563ShouldNotReachHere();4564#endif4565}4566} else {4567type_name = type2name(type());4568if (assigned_reg() != -1 &&4569(LinearScan::num_physical_regs(type()) == 1 || assigned_regHi() != -1)) {4570opr = LinearScan::calc_operand_for_interval(this);4571}4572}45734574out->print("%d %s ", reg_num(), type_name);4575if (opr->is_valid()) {4576out->print("\"");4577opr->print(out);4578out->print("\" ");4579}4580out->print("%d %d ", split_parent()->reg_num(), (register_hint(false) != NULL ? register_hint(false)->reg_num() : -1));45814582// print ranges4583Range* cur = _first;4584while (cur != Range::end()) {4585cur->print(out);4586cur = cur->next();4587assert(cur != NULL, "range list not closed with range sentinel");4588}45894590// print use positions4591int prev = 0;4592assert(_use_pos_and_kinds.length() % 2 == 0, "must be");4593for (int i =_use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {4594assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");4595assert(prev < _use_pos_and_kinds.at(i), "use positions not sorted");45964597out->print("%d %s ", _use_pos_and_kinds.at(i), UseKind2Name[_use_pos_and_kinds.at(i + 1)]);4598prev = _use_pos_and_kinds.at(i);4599}46004601out->print(" \"%s\"", SpillState2Name[spill_state()]);4602out->cr();4603}4604#endif4605460646074608// **** Implementation of IntervalWalker ****************************46094610IntervalWalker::IntervalWalker(LinearScan* allocator, Interval* unhandled_fixed_first, Interval* unhandled_any_first)4611: _compilation(allocator->compilation())4612, _allocator(allocator)4613{4614_unhandled_first[fixedKind] = unhandled_fixed_first;4615_unhandled_first[anyKind] = unhandled_any_first;4616_active_first[fixedKind] = Interval::end();4617_inactive_first[fixedKind] = Interval::end();4618_active_first[anyKind] = Interval::end();4619_inactive_first[anyKind] = Interval::end();4620_current_position = -1;4621_current = NULL;4622next_interval();4623}462446254626// append interval at top of list4627void IntervalWalker::append_unsorted(Interval** list, Interval* interval) {4628interval->set_next(*list); *list = interval;4629}463046314632// append interval in order of current range from()4633void IntervalWalker::append_sorted(Interval** list, Interval* interval) {4634Interval* prev = NULL;4635Interval* cur = *list;4636while (cur->current_from() < interval->current_from()) {4637prev = cur; cur = cur->next();4638}4639if (prev == NULL) {4640*list = interval;4641} else {4642prev->set_next(interval);4643}4644interval->set_next(cur);4645}46464647void IntervalWalker::append_to_unhandled(Interval** list, Interval* interval) {4648assert(interval->from() >= current()->current_from(), "cannot append new interval before current walk position");46494650Interval* prev = NULL;4651Interval* cur = *list;4652while (cur->from() < interval->from() || (cur->from() == interval->from() && cur->first_usage(noUse) < interval->first_usage(noUse))) {4653prev = cur; cur = cur->next();4654}4655if (prev == NULL) {4656*list = interval;4657} else {4658prev->set_next(interval);4659}4660interval->set_next(cur);4661}466246634664inline bool IntervalWalker::remove_from_list(Interval** list, Interval* i) {4665while (*list != Interval::end() && *list != i) {4666list = (*list)->next_addr();4667}4668if (*list != Interval::end()) {4669assert(*list == i, "check");4670*list = (*list)->next();4671return true;4672} else {4673return false;4674}4675}46764677void IntervalWalker::remove_from_list(Interval* i) {4678bool deleted;46794680if (i->state() == activeState) {4681deleted = remove_from_list(active_first_addr(anyKind), i);4682} else {4683assert(i->state() == inactiveState, "invalid state");4684deleted = remove_from_list(inactive_first_addr(anyKind), i);4685}46864687assert(deleted, "interval has not been found in list");4688}468946904691void IntervalWalker::walk_to(IntervalState state, int from) {4692assert (state == activeState || state == inactiveState, "wrong state");4693for_each_interval_kind(kind) {4694Interval** prev = state == activeState ? active_first_addr(kind) : inactive_first_addr(kind);4695Interval* next = *prev;4696while (next->current_from() <= from) {4697Interval* cur = next;4698next = cur->next();46994700bool range_has_changed = false;4701while (cur->current_to() <= from) {4702cur->next_range();4703range_has_changed = true;4704}47054706// also handle move from inactive list to active list4707range_has_changed = range_has_changed || (state == inactiveState && cur->current_from() <= from);47084709if (range_has_changed) {4710// remove cur from list4711*prev = next;4712if (cur->current_at_end()) {4713// move to handled state (not maintained as a list)4714cur->set_state(handledState);4715interval_moved(cur, kind, state, handledState);4716} else if (cur->current_from() <= from){4717// sort into active list4718append_sorted(active_first_addr(kind), cur);4719cur->set_state(activeState);4720if (*prev == cur) {4721assert(state == activeState, "check");4722prev = cur->next_addr();4723}4724interval_moved(cur, kind, state, activeState);4725} else {4726// sort into inactive list4727append_sorted(inactive_first_addr(kind), cur);4728cur->set_state(inactiveState);4729if (*prev == cur) {4730assert(state == inactiveState, "check");4731prev = cur->next_addr();4732}4733interval_moved(cur, kind, state, inactiveState);4734}4735} else {4736prev = cur->next_addr();4737continue;4738}4739}4740}4741}474247434744void IntervalWalker::next_interval() {4745IntervalKind kind;4746Interval* any = _unhandled_first[anyKind];4747Interval* fixed = _unhandled_first[fixedKind];47484749if (any != Interval::end()) {4750// intervals may start at same position -> prefer fixed interval4751kind = fixed != Interval::end() && fixed->from() <= any->from() ? fixedKind : anyKind;47524753assert (kind == fixedKind && fixed->from() <= any->from() ||4754kind == anyKind && any->from() <= fixed->from(), "wrong interval!!!");4755assert(any == Interval::end() || fixed == Interval::end() || any->from() != fixed->from() || kind == fixedKind, "if fixed and any-Interval start at same position, fixed must be processed first");47564757} else if (fixed != Interval::end()) {4758kind = fixedKind;4759} else {4760_current = NULL; return;4761}4762_current_kind = kind;4763_current = _unhandled_first[kind];4764_unhandled_first[kind] = _current->next();4765_current->set_next(Interval::end());4766_current->rewind_range();4767}476847694770void IntervalWalker::walk_to(int lir_op_id) {4771assert(_current_position <= lir_op_id, "can not walk backwards");4772while (current() != NULL) {4773bool is_active = current()->from() <= lir_op_id;4774int id = is_active ? current()->from() : lir_op_id;47754776TRACE_LINEAR_SCAN(2, if (_current_position < id) { tty->cr(); tty->print_cr("walk_to(%d) **************************************************************", id); })47774778// set _current_position prior to call of walk_to4779_current_position = id;47804781// call walk_to even if _current_position == id4782walk_to(activeState, id);4783walk_to(inactiveState, id);47844785if (is_active) {4786current()->set_state(activeState);4787if (activate_current()) {4788append_sorted(active_first_addr(current_kind()), current());4789interval_moved(current(), current_kind(), unhandledState, activeState);4790}47914792next_interval();4793} else {4794return;4795}4796}4797}47984799void IntervalWalker::interval_moved(Interval* interval, IntervalKind kind, IntervalState from, IntervalState to) {4800#ifndef PRODUCT4801if (TraceLinearScanLevel >= 4) {4802#define print_state(state) \4803switch(state) {\4804case unhandledState: tty->print("unhandled"); break;\4805case activeState: tty->print("active"); break;\4806case inactiveState: tty->print("inactive"); break;\4807case handledState: tty->print("handled"); break;\4808default: ShouldNotReachHere(); \4809}48104811print_state(from); tty->print(" to "); print_state(to);4812tty->fill_to(23);4813interval->print();48144815#undef print_state4816}4817#endif4818}4819482048214822// **** Implementation of LinearScanWalker **************************48234824LinearScanWalker::LinearScanWalker(LinearScan* allocator, Interval* unhandled_fixed_first, Interval* unhandled_any_first)4825: IntervalWalker(allocator, unhandled_fixed_first, unhandled_any_first)4826, _move_resolver(allocator)4827{4828for (int i = 0; i < LinearScan::nof_regs; i++) {4829_spill_intervals[i] = new IntervalList(2);4830}4831}483248334834inline void LinearScanWalker::init_use_lists(bool only_process_use_pos) {4835for (int i = _first_reg; i <= _last_reg; i++) {4836_use_pos[i] = max_jint;48374838if (!only_process_use_pos) {4839_block_pos[i] = max_jint;4840_spill_intervals[i]->clear();4841}4842}4843}48444845inline void LinearScanWalker::exclude_from_use(int reg) {4846assert(reg < LinearScan::nof_regs, "interval must have a register assigned (stack slots not allowed)");4847if (reg >= _first_reg && reg <= _last_reg) {4848_use_pos[reg] = 0;4849}4850}4851inline void LinearScanWalker::exclude_from_use(Interval* i) {4852assert(i->assigned_reg() != any_reg, "interval has no register assigned");48534854exclude_from_use(i->assigned_reg());4855exclude_from_use(i->assigned_regHi());4856}48574858inline void LinearScanWalker::set_use_pos(int reg, Interval* i, int use_pos, bool only_process_use_pos) {4859assert(use_pos != 0, "must use exclude_from_use to set use_pos to 0");48604861if (reg >= _first_reg && reg <= _last_reg) {4862if (_use_pos[reg] > use_pos) {4863_use_pos[reg] = use_pos;4864}4865if (!only_process_use_pos) {4866_spill_intervals[reg]->append(i);4867}4868}4869}4870inline void LinearScanWalker::set_use_pos(Interval* i, int use_pos, bool only_process_use_pos) {4871assert(i->assigned_reg() != any_reg, "interval has no register assigned");4872if (use_pos != -1) {4873set_use_pos(i->assigned_reg(), i, use_pos, only_process_use_pos);4874set_use_pos(i->assigned_regHi(), i, use_pos, only_process_use_pos);4875}4876}48774878inline void LinearScanWalker::set_block_pos(int reg, Interval* i, int block_pos) {4879if (reg >= _first_reg && reg <= _last_reg) {4880if (_block_pos[reg] > block_pos) {4881_block_pos[reg] = block_pos;4882}4883if (_use_pos[reg] > block_pos) {4884_use_pos[reg] = block_pos;4885}4886}4887}4888inline void LinearScanWalker::set_block_pos(Interval* i, int block_pos) {4889assert(i->assigned_reg() != any_reg, "interval has no register assigned");4890if (block_pos != -1) {4891set_block_pos(i->assigned_reg(), i, block_pos);4892set_block_pos(i->assigned_regHi(), i, block_pos);4893}4894}489548964897void LinearScanWalker::free_exclude_active_fixed() {4898Interval* list = active_first(fixedKind);4899while (list != Interval::end()) {4900assert(list->assigned_reg() < LinearScan::nof_regs, "active interval must have a register assigned");4901exclude_from_use(list);4902list = list->next();4903}4904}49054906void LinearScanWalker::free_exclude_active_any() {4907Interval* list = active_first(anyKind);4908while (list != Interval::end()) {4909exclude_from_use(list);4910list = list->next();4911}4912}49134914void LinearScanWalker::free_collect_inactive_fixed(Interval* cur) {4915Interval* list = inactive_first(fixedKind);4916while (list != Interval::end()) {4917if (cur->to() <= list->current_from()) {4918assert(list->current_intersects_at(cur) == -1, "must not intersect");4919set_use_pos(list, list->current_from(), true);4920} else {4921set_use_pos(list, list->current_intersects_at(cur), true);4922}4923list = list->next();4924}4925}49264927void LinearScanWalker::free_collect_inactive_any(Interval* cur) {4928Interval* list = inactive_first(anyKind);4929while (list != Interval::end()) {4930set_use_pos(list, list->current_intersects_at(cur), true);4931list = list->next();4932}4933}49344935void LinearScanWalker::free_collect_unhandled(IntervalKind kind, Interval* cur) {4936Interval* list = unhandled_first(kind);4937while (list != Interval::end()) {4938set_use_pos(list, list->intersects_at(cur), true);4939if (kind == fixedKind && cur->to() <= list->from()) {4940set_use_pos(list, list->from(), true);4941}4942list = list->next();4943}4944}49454946void LinearScanWalker::spill_exclude_active_fixed() {4947Interval* list = active_first(fixedKind);4948while (list != Interval::end()) {4949exclude_from_use(list);4950list = list->next();4951}4952}49534954void LinearScanWalker::spill_block_unhandled_fixed(Interval* cur) {4955Interval* list = unhandled_first(fixedKind);4956while (list != Interval::end()) {4957set_block_pos(list, list->intersects_at(cur));4958list = list->next();4959}4960}49614962void LinearScanWalker::spill_block_inactive_fixed(Interval* cur) {4963Interval* list = inactive_first(fixedKind);4964while (list != Interval::end()) {4965if (cur->to() > list->current_from()) {4966set_block_pos(list, list->current_intersects_at(cur));4967} else {4968assert(list->current_intersects_at(cur) == -1, "invalid optimization: intervals intersect");4969}49704971list = list->next();4972}4973}49744975void LinearScanWalker::spill_collect_active_any() {4976Interval* list = active_first(anyKind);4977while (list != Interval::end()) {4978set_use_pos(list, MIN2(list->next_usage(loopEndMarker, _current_position), list->to()), false);4979list = list->next();4980}4981}49824983void LinearScanWalker::spill_collect_inactive_any(Interval* cur) {4984Interval* list = inactive_first(anyKind);4985while (list != Interval::end()) {4986if (list->current_intersects(cur)) {4987set_use_pos(list, MIN2(list->next_usage(loopEndMarker, _current_position), list->to()), false);4988}4989list = list->next();4990}4991}499249934994void LinearScanWalker::insert_move(int op_id, Interval* src_it, Interval* dst_it) {4995// output all moves here. When source and target are equal, the move is4996// optimized away later in assign_reg_nums49974998op_id = (op_id + 1) & ~1;4999BlockBegin* op_block = allocator()->block_of_op_with_id(op_id);5000assert(op_id > 0 && allocator()->block_of_op_with_id(op_id - 2) == op_block, "cannot insert move at block boundary");50015002// calculate index of instruction inside instruction list of current block5003// the minimal index (for a block with no spill moves) can be calculated because the5004// numbering of instructions is known.5005// When the block already contains spill moves, the index must be increased until the5006// correct index is reached.5007LIR_OpList* list = op_block->lir()->instructions_list();5008int index = (op_id - list->at(0)->id()) / 2;5009assert(list->at(index)->id() <= op_id, "error in calculation");50105011while (list->at(index)->id() != op_id) {5012index++;5013assert(0 <= index && index < list->length(), "index out of bounds");5014}5015assert(1 <= index && index < list->length(), "index out of bounds");5016assert(list->at(index)->id() == op_id, "error in calculation");50175018// insert new instruction before instruction at position index5019_move_resolver.move_insert_position(op_block->lir(), index - 1);5020_move_resolver.add_mapping(src_it, dst_it);5021}502250235024int LinearScanWalker::find_optimal_split_pos(BlockBegin* min_block, BlockBegin* max_block, int max_split_pos) {5025int from_block_nr = min_block->linear_scan_number();5026int to_block_nr = max_block->linear_scan_number();50275028assert(0 <= from_block_nr && from_block_nr < block_count(), "out of range");5029assert(0 <= to_block_nr && to_block_nr < block_count(), "out of range");5030assert(from_block_nr < to_block_nr, "must cross block boundary");50315032// Try to split at end of max_block. If this would be after5033// max_split_pos, then use the begin of max_block5034int optimal_split_pos = max_block->last_lir_instruction_id() + 2;5035if (optimal_split_pos > max_split_pos) {5036optimal_split_pos = max_block->first_lir_instruction_id();5037}50385039int min_loop_depth = max_block->loop_depth();5040for (int i = to_block_nr - 1; i >= from_block_nr; i--) {5041BlockBegin* cur = block_at(i);50425043if (cur->loop_depth() < min_loop_depth) {5044// block with lower loop-depth found -> split at the end of this block5045min_loop_depth = cur->loop_depth();5046optimal_split_pos = cur->last_lir_instruction_id() + 2;5047}5048}5049assert(optimal_split_pos > allocator()->max_lir_op_id() || allocator()->is_block_begin(optimal_split_pos), "algorithm must move split pos to block boundary");50505051return optimal_split_pos;5052}505350545055int LinearScanWalker::find_optimal_split_pos(Interval* it, int min_split_pos, int max_split_pos, bool do_loop_optimization) {5056int optimal_split_pos = -1;5057if (min_split_pos == max_split_pos) {5058// trivial case, no optimization of split position possible5059TRACE_LINEAR_SCAN(4, tty->print_cr(" min-pos and max-pos are equal, no optimization possible"));5060optimal_split_pos = min_split_pos;50615062} else {5063assert(min_split_pos < max_split_pos, "must be true then");5064assert(min_split_pos > 0, "cannot access min_split_pos - 1 otherwise");50655066// reason for using min_split_pos - 1: when the minimal split pos is exactly at the5067// beginning of a block, then min_split_pos is also a possible split position.5068// Use the block before as min_block, because then min_block->last_lir_instruction_id() + 2 == min_split_pos5069BlockBegin* min_block = allocator()->block_of_op_with_id(min_split_pos - 1);50705071// reason for using max_split_pos - 1: otherwise there would be an assertion failure5072// when an interval ends at the end of the last block of the method5073// (in this case, max_split_pos == allocator()->max_lir_op_id() + 2, and there is no5074// block at this op_id)5075BlockBegin* max_block = allocator()->block_of_op_with_id(max_split_pos - 1);50765077assert(min_block->linear_scan_number() <= max_block->linear_scan_number(), "invalid order");5078if (min_block == max_block) {5079// split position cannot be moved to block boundary, so split as late as possible5080TRACE_LINEAR_SCAN(4, tty->print_cr(" cannot move split pos to block boundary because min_pos and max_pos are in same block"));5081optimal_split_pos = max_split_pos;50825083} else if (it->has_hole_between(max_split_pos - 1, max_split_pos) && !allocator()->is_block_begin(max_split_pos)) {5084// Do not move split position if the interval has a hole before max_split_pos.5085// Intervals resulting from Phi-Functions have more than one definition (marked5086// as mustHaveRegister) with a hole before each definition. When the register is needed5087// for the second definition, an earlier reloading is unnecessary.5088TRACE_LINEAR_SCAN(4, tty->print_cr(" interval has hole just before max_split_pos, so splitting at max_split_pos"));5089optimal_split_pos = max_split_pos;50905091} else {5092// seach optimal block boundary between min_split_pos and max_split_pos5093TRACE_LINEAR_SCAN(4, tty->print_cr(" moving split pos to optimal block boundary between block B%d and B%d", min_block->block_id(), max_block->block_id()));50945095if (do_loop_optimization) {5096// Loop optimization: if a loop-end marker is found between min- and max-position,5097// then split before this loop5098int loop_end_pos = it->next_usage_exact(loopEndMarker, min_block->last_lir_instruction_id() + 2);5099TRACE_LINEAR_SCAN(4, tty->print_cr(" loop optimization: loop end found at pos %d", loop_end_pos));51005101assert(loop_end_pos > min_split_pos, "invalid order");5102if (loop_end_pos < max_split_pos) {5103// loop-end marker found between min- and max-position5104// if it is not the end marker for the same loop as the min-position, then move5105// the max-position to this loop block.5106// Desired result: uses tagged as shouldHaveRegister inside a loop cause a reloading5107// of the interval (normally, only mustHaveRegister causes a reloading)5108BlockBegin* loop_block = allocator()->block_of_op_with_id(loop_end_pos);51095110TRACE_LINEAR_SCAN(4, tty->print_cr(" interval is used in loop that ends in block B%d, so trying to move max_block back from B%d to B%d", loop_block->block_id(), max_block->block_id(), loop_block->block_id()));5111assert(loop_block != min_block, "loop_block and min_block must be different because block boundary is needed between");51125113optimal_split_pos = find_optimal_split_pos(min_block, loop_block, loop_block->last_lir_instruction_id() + 2);5114if (optimal_split_pos == loop_block->last_lir_instruction_id() + 2) {5115optimal_split_pos = -1;5116TRACE_LINEAR_SCAN(4, tty->print_cr(" loop optimization not necessary"));5117} else {5118TRACE_LINEAR_SCAN(4, tty->print_cr(" loop optimization successful"));5119}5120}5121}51225123if (optimal_split_pos == -1) {5124// not calculated by loop optimization5125optimal_split_pos = find_optimal_split_pos(min_block, max_block, max_split_pos);5126}5127}5128}5129TRACE_LINEAR_SCAN(4, tty->print_cr(" optimal split position: %d", optimal_split_pos));51305131return optimal_split_pos;5132}513351345135/*5136split an interval at the optimal position between min_split_pos and5137max_split_pos in two parts:51381) the left part has already a location assigned51392) the right part is sorted into to the unhandled-list5140*/5141void LinearScanWalker::split_before_usage(Interval* it, int min_split_pos, int max_split_pos) {5142TRACE_LINEAR_SCAN(2, tty->print ("----- splitting interval: "); it->print());5143TRACE_LINEAR_SCAN(2, tty->print_cr(" between %d and %d", min_split_pos, max_split_pos));51445145assert(it->from() < min_split_pos, "cannot split at start of interval");5146assert(current_position() < min_split_pos, "cannot split before current position");5147assert(min_split_pos <= max_split_pos, "invalid order");5148assert(max_split_pos <= it->to(), "cannot split after end of interval");51495150int optimal_split_pos = find_optimal_split_pos(it, min_split_pos, max_split_pos, true);51515152assert(min_split_pos <= optimal_split_pos && optimal_split_pos <= max_split_pos, "out of range");5153assert(optimal_split_pos <= it->to(), "cannot split after end of interval");5154assert(optimal_split_pos > it->from(), "cannot split at start of interval");51555156if (optimal_split_pos == it->to() && it->next_usage(mustHaveRegister, min_split_pos) == max_jint) {5157// the split position would be just before the end of the interval5158// -> no split at all necessary5159TRACE_LINEAR_SCAN(4, tty->print_cr(" no split necessary because optimal split position is at end of interval"));5160return;5161}51625163// must calculate this before the actual split is performed and before split position is moved to odd op_id5164bool move_necessary = !allocator()->is_block_begin(optimal_split_pos) && !it->has_hole_between(optimal_split_pos - 1, optimal_split_pos);51655166if (!allocator()->is_block_begin(optimal_split_pos)) {5167// move position before actual instruction (odd op_id)5168optimal_split_pos = (optimal_split_pos - 1) | 1;5169}51705171TRACE_LINEAR_SCAN(4, tty->print_cr(" splitting at position %d", optimal_split_pos));5172assert(allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 1), "split pos must be odd when not on block boundary");5173assert(!allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 0), "split pos must be even on block boundary");51745175Interval* split_part = it->split(optimal_split_pos);51765177allocator()->append_interval(split_part);5178allocator()->copy_register_flags(it, split_part);5179split_part->set_insert_move_when_activated(move_necessary);5180append_to_unhandled(unhandled_first_addr(anyKind), split_part);51815182TRACE_LINEAR_SCAN(2, tty->print_cr(" split interval in two parts (insert_move_when_activated: %d)", move_necessary));5183TRACE_LINEAR_SCAN(2, tty->print (" "); it->print());5184TRACE_LINEAR_SCAN(2, tty->print (" "); split_part->print());5185}51865187/*5188split an interval at the optimal position between min_split_pos and5189max_split_pos in two parts:51901) the left part has already a location assigned51912) the right part is always on the stack and therefore ignored in further processing5192*/5193void LinearScanWalker::split_for_spilling(Interval* it) {5194// calculate allowed range of splitting position5195int max_split_pos = current_position();5196int min_split_pos = MAX2(it->previous_usage(shouldHaveRegister, max_split_pos) + 1, it->from());51975198TRACE_LINEAR_SCAN(2, tty->print ("----- splitting and spilling interval: "); it->print());5199TRACE_LINEAR_SCAN(2, tty->print_cr(" between %d and %d", min_split_pos, max_split_pos));52005201assert(it->state() == activeState, "why spill interval that is not active?");5202assert(it->from() <= min_split_pos, "cannot split before start of interval");5203assert(min_split_pos <= max_split_pos, "invalid order");5204assert(max_split_pos < it->to(), "cannot split at end end of interval");5205assert(current_position() < it->to(), "interval must not end before current position");52065207if (min_split_pos == it->from()) {5208// the whole interval is never used, so spill it entirely to memory5209TRACE_LINEAR_SCAN(2, tty->print_cr(" spilling entire interval because split pos is at beginning of interval"));5210assert(it->first_usage(shouldHaveRegister) > current_position(), "interval must not have use position before current_position");52115212allocator()->assign_spill_slot(it);5213allocator()->change_spill_state(it, min_split_pos);52145215// Also kick parent intervals out of register to memory when they have no use5216// position. This avoids short interval in register surrounded by intervals in5217// memory -> avoid useless moves from memory to register and back5218Interval* parent = it;5219while (parent != NULL && parent->is_split_child()) {5220parent = parent->split_child_before_op_id(parent->from());52215222if (parent->assigned_reg() < LinearScan::nof_regs) {5223if (parent->first_usage(shouldHaveRegister) == max_jint) {5224// parent is never used, so kick it out of its assigned register5225TRACE_LINEAR_SCAN(4, tty->print_cr(" kicking out interval %d out of its register because it is never used", parent->reg_num()));5226allocator()->assign_spill_slot(parent);5227} else {5228// do not go further back because the register is actually used by the interval5229parent = NULL;5230}5231}5232}52335234} else {5235// search optimal split pos, split interval and spill only the right hand part5236int optimal_split_pos = find_optimal_split_pos(it, min_split_pos, max_split_pos, false);52375238assert(min_split_pos <= optimal_split_pos && optimal_split_pos <= max_split_pos, "out of range");5239assert(optimal_split_pos < it->to(), "cannot split at end of interval");5240assert(optimal_split_pos >= it->from(), "cannot split before start of interval");52415242if (!allocator()->is_block_begin(optimal_split_pos)) {5243// move position before actual instruction (odd op_id)5244optimal_split_pos = (optimal_split_pos - 1) | 1;5245}52465247TRACE_LINEAR_SCAN(4, tty->print_cr(" splitting at position %d", optimal_split_pos));5248assert(allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 1), "split pos must be odd when not on block boundary");5249assert(!allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 0), "split pos must be even on block boundary");52505251Interval* spilled_part = it->split(optimal_split_pos);5252allocator()->append_interval(spilled_part);5253allocator()->assign_spill_slot(spilled_part);5254allocator()->change_spill_state(spilled_part, optimal_split_pos);52555256if (!allocator()->is_block_begin(optimal_split_pos)) {5257TRACE_LINEAR_SCAN(4, tty->print_cr(" inserting move from interval %d to %d", it->reg_num(), spilled_part->reg_num()));5258insert_move(optimal_split_pos, it, spilled_part);5259}52605261// the current_split_child is needed later when moves are inserted for reloading5262assert(spilled_part->current_split_child() == it, "overwriting wrong current_split_child");5263spilled_part->make_current_split_child();52645265TRACE_LINEAR_SCAN(2, tty->print_cr(" split interval in two parts"));5266TRACE_LINEAR_SCAN(2, tty->print (" "); it->print());5267TRACE_LINEAR_SCAN(2, tty->print (" "); spilled_part->print());5268}5269}527052715272void LinearScanWalker::split_stack_interval(Interval* it) {5273int min_split_pos = current_position() + 1;5274int max_split_pos = MIN2(it->first_usage(shouldHaveRegister), it->to());52755276split_before_usage(it, min_split_pos, max_split_pos);5277}52785279void LinearScanWalker::split_when_partial_register_available(Interval* it, int register_available_until) {5280int min_split_pos = MAX2(it->previous_usage(shouldHaveRegister, register_available_until), it->from() + 1);5281int max_split_pos = register_available_until;52825283split_before_usage(it, min_split_pos, max_split_pos);5284}52855286void LinearScanWalker::split_and_spill_interval(Interval* it) {5287assert(it->state() == activeState || it->state() == inactiveState, "other states not allowed");52885289int current_pos = current_position();5290if (it->state() == inactiveState) {5291// the interval is currently inactive, so no spill slot is needed for now.5292// when the split part is activated, the interval has a new chance to get a register,5293// so in the best case no stack slot is necessary5294assert(it->has_hole_between(current_pos - 1, current_pos + 1), "interval can not be inactive otherwise");5295split_before_usage(it, current_pos + 1, current_pos + 1);52965297} else {5298// search the position where the interval must have a register and split5299// at the optimal position before.5300// The new created part is added to the unhandled list and will get a register5301// when it is activated5302int min_split_pos = current_pos + 1;5303int max_split_pos = MIN2(it->next_usage(mustHaveRegister, min_split_pos), it->to());53045305split_before_usage(it, min_split_pos, max_split_pos);53065307assert(it->next_usage(mustHaveRegister, current_pos) == max_jint, "the remaining part is spilled to stack and therefore has no register");5308split_for_spilling(it);5309}5310}531153125313int LinearScanWalker::find_free_reg(int reg_needed_until, int interval_to, int hint_reg, int ignore_reg, bool* need_split) {5314int min_full_reg = any_reg;5315int max_partial_reg = any_reg;53165317for (int i = _first_reg; i <= _last_reg; i++) {5318if (i == ignore_reg) {5319// this register must be ignored53205321} else if (_use_pos[i] >= interval_to) {5322// this register is free for the full interval5323if (min_full_reg == any_reg || i == hint_reg || (_use_pos[i] < _use_pos[min_full_reg] && min_full_reg != hint_reg)) {5324min_full_reg = i;5325}5326} else if (_use_pos[i] > reg_needed_until) {5327// this register is at least free until reg_needed_until5328if (max_partial_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_partial_reg] && max_partial_reg != hint_reg)) {5329max_partial_reg = i;5330}5331}5332}53335334if (min_full_reg != any_reg) {5335return min_full_reg;5336} else if (max_partial_reg != any_reg) {5337*need_split = true;5338return max_partial_reg;5339} else {5340return any_reg;5341}5342}53435344int LinearScanWalker::find_free_double_reg(int reg_needed_until, int interval_to, int hint_reg, bool* need_split) {5345assert((_last_reg - _first_reg + 1) % 2 == 0, "adjust algorithm");53465347int min_full_reg = any_reg;5348int max_partial_reg = any_reg;53495350for (int i = _first_reg; i < _last_reg; i+=2) {5351if (_use_pos[i] >= interval_to && _use_pos[i + 1] >= interval_to) {5352// this register is free for the full interval5353if (min_full_reg == any_reg || i == hint_reg || (_use_pos[i] < _use_pos[min_full_reg] && min_full_reg != hint_reg)) {5354min_full_reg = i;5355}5356} else if (_use_pos[i] > reg_needed_until && _use_pos[i + 1] > reg_needed_until) {5357// this register is at least free until reg_needed_until5358if (max_partial_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_partial_reg] && max_partial_reg != hint_reg)) {5359max_partial_reg = i;5360}5361}5362}53635364if (min_full_reg != any_reg) {5365return min_full_reg;5366} else if (max_partial_reg != any_reg) {5367*need_split = true;5368return max_partial_reg;5369} else {5370return any_reg;5371}5372}537353745375bool LinearScanWalker::alloc_free_reg(Interval* cur) {5376TRACE_LINEAR_SCAN(2, tty->print("trying to find free register for "); cur->print());53775378init_use_lists(true);5379free_exclude_active_fixed();5380free_exclude_active_any();5381free_collect_inactive_fixed(cur);5382free_collect_inactive_any(cur);5383// free_collect_unhandled(fixedKind, cur);5384assert(unhandled_first(fixedKind) == Interval::end(), "must not have unhandled fixed intervals because all fixed intervals have a use at position 0");53855386// _use_pos contains the start of the next interval that has this register assigned5387// (either as a fixed register or a normal allocated register in the past)5388// only intervals overlapping with cur are processed, non-overlapping invervals can be ignored safely5389TRACE_LINEAR_SCAN(4, tty->print_cr(" state of registers:"));5390TRACE_LINEAR_SCAN(4, for (int i = _first_reg; i <= _last_reg; i++) tty->print_cr(" reg %d: use_pos: %d", i, _use_pos[i]));53915392int hint_reg, hint_regHi;5393Interval* register_hint = cur->register_hint();5394if (register_hint != NULL) {5395hint_reg = register_hint->assigned_reg();5396hint_regHi = register_hint->assigned_regHi();53975398if (allocator()->is_precolored_cpu_interval(register_hint)) {5399assert(hint_reg != any_reg && hint_regHi == any_reg, "must be for fixed intervals");5400hint_regHi = hint_reg + 1; // connect e.g. eax-edx5401}5402TRACE_LINEAR_SCAN(4, tty->print(" hint registers %d, %d from interval ", hint_reg, hint_regHi); register_hint->print());54035404} else {5405hint_reg = any_reg;5406hint_regHi = any_reg;5407}5408assert(hint_reg == any_reg || hint_reg != hint_regHi, "hint reg and regHi equal");5409assert(cur->assigned_reg() == any_reg && cur->assigned_regHi() == any_reg, "register already assigned to interval");54105411// the register must be free at least until this position5412int reg_needed_until = cur->from() + 1;5413int interval_to = cur->to();54145415bool need_split = false;5416int split_pos = -1;5417int reg = any_reg;5418int regHi = any_reg;54195420if (_adjacent_regs) {5421reg = find_free_double_reg(reg_needed_until, interval_to, hint_reg, &need_split);5422regHi = reg + 1;5423if (reg == any_reg) {5424return false;5425}5426split_pos = MIN2(_use_pos[reg], _use_pos[regHi]);54275428} else {5429reg = find_free_reg(reg_needed_until, interval_to, hint_reg, any_reg, &need_split);5430if (reg == any_reg) {5431return false;5432}5433split_pos = _use_pos[reg];54345435if (_num_phys_regs == 2) {5436regHi = find_free_reg(reg_needed_until, interval_to, hint_regHi, reg, &need_split);54375438if (_use_pos[reg] < interval_to && regHi == any_reg) {5439// do not split interval if only one register can be assigned until the split pos5440// (when one register is found for the whole interval, split&spill is only5441// performed for the hi register)5442return false;54435444} else if (regHi != any_reg) {5445split_pos = MIN2(split_pos, _use_pos[regHi]);54465447// sort register numbers to prevent e.g. a move from eax,ebx to ebx,eax5448if (reg > regHi) {5449int temp = reg;5450reg = regHi;5451regHi = temp;5452}5453}5454}5455}54565457cur->assign_reg(reg, regHi);5458TRACE_LINEAR_SCAN(2, tty->print_cr("selected register %d, %d", reg, regHi));54595460assert(split_pos > 0, "invalid split_pos");5461if (need_split) {5462// register not available for full interval, so split it5463split_when_partial_register_available(cur, split_pos);5464}54655466// only return true if interval is completely assigned5467return _num_phys_regs == 1 || regHi != any_reg;5468}546954705471int LinearScanWalker::find_locked_reg(int reg_needed_until, int interval_to, int hint_reg, int ignore_reg, bool* need_split) {5472int max_reg = any_reg;54735474for (int i = _first_reg; i <= _last_reg; i++) {5475if (i == ignore_reg) {5476// this register must be ignored54775478} else if (_use_pos[i] > reg_needed_until) {5479if (max_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_reg] && max_reg != hint_reg)) {5480max_reg = i;5481}5482}5483}54845485if (max_reg != any_reg && _block_pos[max_reg] <= interval_to) {5486*need_split = true;5487}54885489return max_reg;5490}54915492int LinearScanWalker::find_locked_double_reg(int reg_needed_until, int interval_to, int hint_reg, bool* need_split) {5493assert((_last_reg - _first_reg + 1) % 2 == 0, "adjust algorithm");54945495int max_reg = any_reg;54965497for (int i = _first_reg; i < _last_reg; i+=2) {5498if (_use_pos[i] > reg_needed_until && _use_pos[i + 1] > reg_needed_until) {5499if (max_reg == any_reg || _use_pos[i] > _use_pos[max_reg]) {5500max_reg = i;5501}5502}5503}55045505if (_block_pos[max_reg] <= interval_to || _block_pos[max_reg + 1] <= interval_to) {5506*need_split = true;5507}55085509return max_reg;5510}55115512void LinearScanWalker::split_and_spill_intersecting_intervals(int reg, int regHi) {5513assert(reg != any_reg, "no register assigned");55145515for (int i = 0; i < _spill_intervals[reg]->length(); i++) {5516Interval* it = _spill_intervals[reg]->at(i);5517remove_from_list(it);5518split_and_spill_interval(it);5519}55205521if (regHi != any_reg) {5522IntervalList* processed = _spill_intervals[reg];5523for (int i = 0; i < _spill_intervals[regHi]->length(); i++) {5524Interval* it = _spill_intervals[regHi]->at(i);5525if (processed->index_of(it) == -1) {5526remove_from_list(it);5527split_and_spill_interval(it);5528}5529}5530}5531}553255335534// Split an Interval and spill it to memory so that cur can be placed in a register5535void LinearScanWalker::alloc_locked_reg(Interval* cur) {5536TRACE_LINEAR_SCAN(2, tty->print("need to split and spill to get register for "); cur->print());55375538// collect current usage of registers5539init_use_lists(false);5540spill_exclude_active_fixed();5541// spill_block_unhandled_fixed(cur);5542assert(unhandled_first(fixedKind) == Interval::end(), "must not have unhandled fixed intervals because all fixed intervals have a use at position 0");5543spill_block_inactive_fixed(cur);5544spill_collect_active_any();5545spill_collect_inactive_any(cur);55465547#ifndef PRODUCT5548if (TraceLinearScanLevel >= 4) {5549tty->print_cr(" state of registers:");5550for (int i = _first_reg; i <= _last_reg; i++) {5551tty->print(" reg %d: use_pos: %d, block_pos: %d, intervals: ", i, _use_pos[i], _block_pos[i]);5552for (int j = 0; j < _spill_intervals[i]->length(); j++) {5553tty->print("%d ", _spill_intervals[i]->at(j)->reg_num());5554}5555tty->cr();5556}5557}5558#endif55595560// the register must be free at least until this position5561int reg_needed_until = MIN2(cur->first_usage(mustHaveRegister), cur->from() + 1);5562int interval_to = cur->to();5563assert (reg_needed_until > 0 && reg_needed_until < max_jint, "interval has no use");55645565int split_pos = 0;5566int use_pos = 0;5567bool need_split = false;5568int reg, regHi;55695570if (_adjacent_regs) {5571reg = find_locked_double_reg(reg_needed_until, interval_to, any_reg, &need_split);5572regHi = reg + 1;55735574if (reg != any_reg) {5575use_pos = MIN2(_use_pos[reg], _use_pos[regHi]);5576split_pos = MIN2(_block_pos[reg], _block_pos[regHi]);5577}5578} else {5579reg = find_locked_reg(reg_needed_until, interval_to, any_reg, cur->assigned_reg(), &need_split);5580regHi = any_reg;55815582if (reg != any_reg) {5583use_pos = _use_pos[reg];5584split_pos = _block_pos[reg];55855586if (_num_phys_regs == 2) {5587if (cur->assigned_reg() != any_reg) {5588regHi = reg;5589reg = cur->assigned_reg();5590} else {5591regHi = find_locked_reg(reg_needed_until, interval_to, any_reg, reg, &need_split);5592if (regHi != any_reg) {5593use_pos = MIN2(use_pos, _use_pos[regHi]);5594split_pos = MIN2(split_pos, _block_pos[regHi]);5595}5596}55975598if (regHi != any_reg && reg > regHi) {5599// sort register numbers to prevent e.g. a move from eax,ebx to ebx,eax5600int temp = reg;5601reg = regHi;5602regHi = temp;5603}5604}5605}5606}56075608if (reg == any_reg || (_num_phys_regs == 2 && regHi == any_reg) || use_pos <= cur->first_usage(mustHaveRegister)) {5609// the first use of cur is later than the spilling position -> spill cur5610TRACE_LINEAR_SCAN(4, tty->print_cr("able to spill current interval. first_usage(register): %d, use_pos: %d", cur->first_usage(mustHaveRegister), use_pos));56115612if (cur->first_usage(mustHaveRegister) <= cur->from() + 1) {5613assert(false, "cannot spill interval that is used in first instruction (possible reason: no register found)");5614// assign a reasonable register and do a bailout in product mode to avoid errors5615allocator()->assign_spill_slot(cur);5616BAILOUT("LinearScan: no register found");5617}56185619split_and_spill_interval(cur);5620} else {5621TRACE_LINEAR_SCAN(4, tty->print_cr("decided to use register %d, %d", reg, regHi));5622assert(reg != any_reg && (_num_phys_regs == 1 || regHi != any_reg), "no register found");5623assert(split_pos > 0, "invalid split_pos");5624assert(need_split == false || split_pos > cur->from(), "splitting interval at from");56255626cur->assign_reg(reg, regHi);5627if (need_split) {5628// register not available for full interval, so split it5629split_when_partial_register_available(cur, split_pos);5630}56315632// perform splitting and spilling for all affected intervalls5633split_and_spill_intersecting_intervals(reg, regHi);5634}5635}56365637bool LinearScanWalker::no_allocation_possible(Interval* cur) {5638#if defined(X86)5639// fast calculation of intervals that can never get a register because the5640// the next instruction is a call that blocks all registers5641// Note: this does not work if callee-saved registers are available (e.g. on Sparc)56425643// check if this interval is the result of a split operation5644// (an interval got a register until this position)5645int pos = cur->from();5646if ((pos & 1) == 1) {5647// the current instruction is a call that blocks all registers5648if (pos < allocator()->max_lir_op_id() && allocator()->has_call(pos + 1)) {5649TRACE_LINEAR_SCAN(4, tty->print_cr(" free register cannot be available because all registers blocked by following call"));56505651// safety check that there is really no register available5652assert(alloc_free_reg(cur) == false, "found a register for this interval");5653return true;5654}56555656}5657#endif5658return false;5659}56605661void LinearScanWalker::init_vars_for_alloc(Interval* cur) {5662BasicType type = cur->type();5663_num_phys_regs = LinearScan::num_physical_regs(type);5664_adjacent_regs = LinearScan::requires_adjacent_regs(type);56655666if (pd_init_regs_for_alloc(cur)) {5667// the appropriate register range was selected.5668} else if (type == T_FLOAT || type == T_DOUBLE) {5669_first_reg = pd_first_fpu_reg;5670_last_reg = pd_last_fpu_reg;5671} else {5672_first_reg = pd_first_cpu_reg;5673_last_reg = FrameMap::last_cpu_reg();5674}56755676assert(0 <= _first_reg && _first_reg < LinearScan::nof_regs, "out of range");5677assert(0 <= _last_reg && _last_reg < LinearScan::nof_regs, "out of range");5678}567956805681bool LinearScanWalker::is_move(LIR_Op* op, Interval* from, Interval* to) {5682if (op->code() != lir_move) {5683return false;5684}5685assert(op->as_Op1() != NULL, "move must be LIR_Op1");56865687LIR_Opr in = ((LIR_Op1*)op)->in_opr();5688LIR_Opr res = ((LIR_Op1*)op)->result_opr();5689return in->is_virtual() && res->is_virtual() && in->vreg_number() == from->reg_num() && res->vreg_number() == to->reg_num();5690}56915692// optimization (especially for phi functions of nested loops):5693// assign same spill slot to non-intersecting intervals5694void LinearScanWalker::combine_spilled_intervals(Interval* cur) {5695if (cur->is_split_child()) {5696// optimization is only suitable for split parents5697return;5698}56995700Interval* register_hint = cur->register_hint(false);5701if (register_hint == NULL) {5702// cur is not the target of a move, otherwise register_hint would be set5703return;5704}5705assert(register_hint->is_split_parent(), "register hint must be split parent");57065707if (cur->spill_state() != noOptimization || register_hint->spill_state() != noOptimization) {5708// combining the stack slots for intervals where spill move optimization is applied5709// is not benefitial and would cause problems5710return;5711}57125713int begin_pos = cur->from();5714int end_pos = cur->to();5715if (end_pos > allocator()->max_lir_op_id() || (begin_pos & 1) != 0 || (end_pos & 1) != 0) {5716// safety check that lir_op_with_id is allowed5717return;5718}57195720if (!is_move(allocator()->lir_op_with_id(begin_pos), register_hint, cur) || !is_move(allocator()->lir_op_with_id(end_pos), cur, register_hint)) {5721// cur and register_hint are not connected with two moves5722return;5723}57245725Interval* begin_hint = register_hint->split_child_at_op_id(begin_pos, LIR_OpVisitState::inputMode);5726Interval* end_hint = register_hint->split_child_at_op_id(end_pos, LIR_OpVisitState::outputMode);5727if (begin_hint == end_hint || begin_hint->to() != begin_pos || end_hint->from() != end_pos) {5728// register_hint must be split, otherwise the re-writing of use positions does not work5729return;5730}57315732assert(begin_hint->assigned_reg() != any_reg, "must have register assigned");5733assert(end_hint->assigned_reg() == any_reg, "must not have register assigned");5734assert(cur->first_usage(mustHaveRegister) == begin_pos, "must have use position at begin of interval because of move");5735assert(end_hint->first_usage(mustHaveRegister) == end_pos, "must have use position at begin of interval because of move");57365737if (begin_hint->assigned_reg() < LinearScan::nof_regs) {5738// register_hint is not spilled at begin_pos, so it would not be benefitial to immediately spill cur5739return;5740}5741assert(register_hint->canonical_spill_slot() != -1, "must be set when part of interval was spilled");57425743// modify intervals such that cur gets the same stack slot as register_hint5744// delete use positions to prevent the intervals to get a register at beginning5745cur->set_canonical_spill_slot(register_hint->canonical_spill_slot());5746cur->remove_first_use_pos();5747end_hint->remove_first_use_pos();5748}574957505751// allocate a physical register or memory location to an interval5752bool LinearScanWalker::activate_current() {5753Interval* cur = current();5754bool result = true;57555756TRACE_LINEAR_SCAN(2, tty->print ("+++++ activating interval "); cur->print());5757TRACE_LINEAR_SCAN(4, tty->print_cr(" split_parent: %d, insert_move_when_activated: %d", cur->split_parent()->reg_num(), cur->insert_move_when_activated()));57585759if (cur->assigned_reg() >= LinearScan::nof_regs) {5760// activating an interval that has a stack slot assigned -> split it at first use position5761// used for method parameters5762TRACE_LINEAR_SCAN(4, tty->print_cr(" interval has spill slot assigned (method parameter) -> split it before first use"));57635764split_stack_interval(cur);5765result = false;57665767} else if (allocator()->gen()->is_vreg_flag_set(cur->reg_num(), LIRGenerator::must_start_in_memory)) {5768// activating an interval that must start in a stack slot, but may get a register later5769// used for lir_roundfp: rounding is done by store to stack and reload later5770TRACE_LINEAR_SCAN(4, tty->print_cr(" interval must start in stack slot -> split it before first use"));5771assert(cur->assigned_reg() == any_reg && cur->assigned_regHi() == any_reg, "register already assigned");57725773allocator()->assign_spill_slot(cur);5774split_stack_interval(cur);5775result = false;57765777} else if (cur->assigned_reg() == any_reg) {5778// interval has not assigned register -> normal allocation5779// (this is the normal case for most intervals)5780TRACE_LINEAR_SCAN(4, tty->print_cr(" normal allocation of register"));57815782// assign same spill slot to non-intersecting intervals5783combine_spilled_intervals(cur);57845785init_vars_for_alloc(cur);5786if (no_allocation_possible(cur) || !alloc_free_reg(cur)) {5787// no empty register available.5788// split and spill another interval so that this interval gets a register5789alloc_locked_reg(cur);5790}57915792// spilled intervals need not be move to active-list5793if (cur->assigned_reg() >= LinearScan::nof_regs) {5794result = false;5795}5796}57975798// load spilled values that become active from stack slot to register5799if (cur->insert_move_when_activated()) {5800assert(cur->is_split_child(), "must be");5801assert(cur->current_split_child() != NULL, "must be");5802assert(cur->current_split_child()->reg_num() != cur->reg_num(), "cannot insert move between same interval");5803TRACE_LINEAR_SCAN(4, tty->print_cr("Inserting move from interval %d to %d because insert_move_when_activated is set", cur->current_split_child()->reg_num(), cur->reg_num()));58045805insert_move(cur->from(), cur->current_split_child(), cur);5806}5807cur->make_current_split_child();58085809return result; // true = interval is moved to active list5810}581158125813// Implementation of EdgeMoveOptimizer58145815EdgeMoveOptimizer::EdgeMoveOptimizer() :5816_edge_instructions(4),5817_edge_instructions_idx(4)5818{5819}58205821void EdgeMoveOptimizer::optimize(BlockList* code) {5822EdgeMoveOptimizer optimizer = EdgeMoveOptimizer();58235824// ignore the first block in the list (index 0 is not processed)5825for (int i = code->length() - 1; i >= 1; i--) {5826BlockBegin* block = code->at(i);58275828if (block->number_of_preds() > 1 && !block->is_set(BlockBegin::exception_entry_flag)) {5829optimizer.optimize_moves_at_block_end(block);5830}5831if (block->number_of_sux() == 2) {5832optimizer.optimize_moves_at_block_begin(block);5833}5834}5835}583658375838// clear all internal data structures5839void EdgeMoveOptimizer::init_instructions() {5840_edge_instructions.clear();5841_edge_instructions_idx.clear();5842}58435844// append a lir-instruction-list and the index of the current operation in to the list5845void EdgeMoveOptimizer::append_instructions(LIR_OpList* instructions, int instructions_idx) {5846_edge_instructions.append(instructions);5847_edge_instructions_idx.append(instructions_idx);5848}58495850// return the current operation of the given edge (predecessor or successor)5851LIR_Op* EdgeMoveOptimizer::instruction_at(int edge) {5852LIR_OpList* instructions = _edge_instructions.at(edge);5853int idx = _edge_instructions_idx.at(edge);58545855if (idx < instructions->length()) {5856return instructions->at(idx);5857} else {5858return NULL;5859}5860}58615862// removes the current operation of the given edge (predecessor or successor)5863void EdgeMoveOptimizer::remove_cur_instruction(int edge, bool decrement_index) {5864LIR_OpList* instructions = _edge_instructions.at(edge);5865int idx = _edge_instructions_idx.at(edge);5866instructions->remove_at(idx);58675868if (decrement_index) {5869_edge_instructions_idx.at_put(edge, idx - 1);5870}5871}587258735874bool EdgeMoveOptimizer::operations_different(LIR_Op* op1, LIR_Op* op2) {5875if (op1 == NULL || op2 == NULL) {5876// at least one block is already empty -> no optimization possible5877return true;5878}58795880if (op1->code() == lir_move && op2->code() == lir_move) {5881assert(op1->as_Op1() != NULL, "move must be LIR_Op1");5882assert(op2->as_Op1() != NULL, "move must be LIR_Op1");5883LIR_Op1* move1 = (LIR_Op1*)op1;5884LIR_Op1* move2 = (LIR_Op1*)op2;5885if (move1->info() == move2->info() && move1->in_opr() == move2->in_opr() && move1->result_opr() == move2->result_opr()) {5886// these moves are exactly equal and can be optimized5887return false;5888}58895890} else if (op1->code() == lir_fxch && op2->code() == lir_fxch) {5891assert(op1->as_Op1() != NULL, "fxch must be LIR_Op1");5892assert(op2->as_Op1() != NULL, "fxch must be LIR_Op1");5893LIR_Op1* fxch1 = (LIR_Op1*)op1;5894LIR_Op1* fxch2 = (LIR_Op1*)op2;5895if (fxch1->in_opr()->as_jint() == fxch2->in_opr()->as_jint()) {5896// equal FPU stack operations can be optimized5897return false;5898}58995900} else if (op1->code() == lir_fpop_raw && op2->code() == lir_fpop_raw) {5901// equal FPU stack operations can be optimized5902return false;5903}59045905// no optimization possible5906return true;5907}59085909void EdgeMoveOptimizer::optimize_moves_at_block_end(BlockBegin* block) {5910TRACE_LINEAR_SCAN(4, tty->print_cr("optimizing moves at end of block B%d", block->block_id()));59115912if (block->is_predecessor(block)) {5913// currently we can't handle this correctly.5914return;5915}59165917init_instructions();5918int num_preds = block->number_of_preds();5919assert(num_preds > 1, "do not call otherwise");5920assert(!block->is_set(BlockBegin::exception_entry_flag), "exception handlers not allowed");59215922// setup a list with the lir-instructions of all predecessors5923int i;5924for (i = 0; i < num_preds; i++) {5925BlockBegin* pred = block->pred_at(i);5926LIR_OpList* pred_instructions = pred->lir()->instructions_list();59275928if (pred->number_of_sux() != 1) {5929// this can happen with switch-statements where multiple edges are between5930// the same blocks.5931return;5932}59335934assert(pred->number_of_sux() == 1, "can handle only one successor");5935assert(pred->sux_at(0) == block, "invalid control flow");5936assert(pred_instructions->last()->code() == lir_branch, "block with successor must end with branch");5937assert(pred_instructions->last()->as_OpBranch() != NULL, "branch must be LIR_OpBranch");5938assert(pred_instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block must end with unconditional branch");59395940if (pred_instructions->last()->info() != NULL) {5941// can not optimize instructions when debug info is needed5942return;5943}59445945// ignore the unconditional branch at the end of the block5946append_instructions(pred_instructions, pred_instructions->length() - 2);5947}594859495950// process lir-instructions while all predecessors end with the same instruction5951while (true) {5952LIR_Op* op = instruction_at(0);5953for (i = 1; i < num_preds; i++) {5954if (operations_different(op, instruction_at(i))) {5955// these instructions are different and cannot be optimized ->5956// no further optimization possible5957return;5958}5959}59605961TRACE_LINEAR_SCAN(4, tty->print("found instruction that is equal in all %d predecessors: ", num_preds); op->print());59625963// insert the instruction at the beginning of the current block5964block->lir()->insert_before(1, op);59655966// delete the instruction at the end of all predecessors5967for (i = 0; i < num_preds; i++) {5968remove_cur_instruction(i, true);5969}5970}5971}597259735974void EdgeMoveOptimizer::optimize_moves_at_block_begin(BlockBegin* block) {5975TRACE_LINEAR_SCAN(4, tty->print_cr("optimization moves at begin of block B%d", block->block_id()));59765977init_instructions();5978int num_sux = block->number_of_sux();59795980LIR_OpList* cur_instructions = block->lir()->instructions_list();59815982assert(num_sux == 2, "method should not be called otherwise");5983assert(cur_instructions->last()->code() == lir_branch, "block with successor must end with branch");5984assert(cur_instructions->last()->as_OpBranch() != NULL, "branch must be LIR_OpBranch");5985assert(cur_instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block must end with unconditional branch");59865987if (cur_instructions->last()->info() != NULL) {5988// can no optimize instructions when debug info is needed5989return;5990}59915992LIR_Op* branch = cur_instructions->at(cur_instructions->length() - 2);5993if (branch->info() != NULL || (branch->code() != lir_branch && branch->code() != lir_cond_float_branch)) {5994// not a valid case for optimization5995// currently, only blocks that end with two branches (conditional branch followed5996// by unconditional branch) are optimized5997return;5998}59996000// now it is guaranteed that the block ends with two branch instructions.6001// the instructions are inserted at the end of the block before these two branches6002int insert_idx = cur_instructions->length() - 2;60036004int i;6005#ifdef ASSERT6006for (i = insert_idx - 1; i >= 0; i--) {6007LIR_Op* op = cur_instructions->at(i);6008if ((op->code() == lir_branch || op->code() == lir_cond_float_branch) && ((LIR_OpBranch*)op)->block() != NULL) {6009assert(false, "block with two successors can have only two branch instructions");6010}6011}6012#endif60136014// setup a list with the lir-instructions of all successors6015for (i = 0; i < num_sux; i++) {6016BlockBegin* sux = block->sux_at(i);6017LIR_OpList* sux_instructions = sux->lir()->instructions_list();60186019assert(sux_instructions->at(0)->code() == lir_label, "block must start with label");60206021if (sux->number_of_preds() != 1) {6022// this can happen with switch-statements where multiple edges are between6023// the same blocks.6024return;6025}6026assert(sux->pred_at(0) == block, "invalid control flow");6027assert(!sux->is_set(BlockBegin::exception_entry_flag), "exception handlers not allowed");60286029// ignore the label at the beginning of the block6030append_instructions(sux_instructions, 1);6031}60326033// process lir-instructions while all successors begin with the same instruction6034while (true) {6035LIR_Op* op = instruction_at(0);6036for (i = 1; i < num_sux; i++) {6037if (operations_different(op, instruction_at(i))) {6038// these instructions are different and cannot be optimized ->6039// no further optimization possible6040return;6041}6042}60436044TRACE_LINEAR_SCAN(4, tty->print("----- found instruction that is equal in all %d successors: ", num_sux); op->print());60456046// insert instruction at end of current block6047block->lir()->insert_before(insert_idx, op);6048insert_idx++;60496050// delete the instructions at the beginning of all successors6051for (i = 0; i < num_sux; i++) {6052remove_cur_instruction(i, false);6053}6054}6055}605660576058// Implementation of ControlFlowOptimizer60596060ControlFlowOptimizer::ControlFlowOptimizer() :6061_original_preds(4)6062{6063}60646065void ControlFlowOptimizer::optimize(BlockList* code) {6066ControlFlowOptimizer optimizer = ControlFlowOptimizer();60676068// push the OSR entry block to the end so that we're not jumping over it.6069BlockBegin* osr_entry = code->at(0)->end()->as_Base()->osr_entry();6070if (osr_entry) {6071int index = osr_entry->linear_scan_number();6072assert(code->at(index) == osr_entry, "wrong index");6073code->remove_at(index);6074code->append(osr_entry);6075}60766077optimizer.reorder_short_loops(code);6078optimizer.delete_empty_blocks(code);6079optimizer.delete_unnecessary_jumps(code);6080optimizer.delete_jumps_to_return(code);6081}60826083void ControlFlowOptimizer::reorder_short_loop(BlockList* code, BlockBegin* header_block, int header_idx) {6084int i = header_idx + 1;6085int max_end = MIN2(header_idx + ShortLoopSize, code->length());6086while (i < max_end && code->at(i)->loop_depth() >= header_block->loop_depth()) {6087i++;6088}60896090if (i == code->length() || code->at(i)->loop_depth() < header_block->loop_depth()) {6091int end_idx = i - 1;6092BlockBegin* end_block = code->at(end_idx);60936094if (end_block->number_of_sux() == 1 && end_block->sux_at(0) == header_block) {6095// short loop from header_idx to end_idx found -> reorder blocks such that6096// the header_block is the last block instead of the first block of the loop6097TRACE_LINEAR_SCAN(1, tty->print_cr("Reordering short loop: length %d, header B%d, end B%d",6098end_idx - header_idx + 1,6099header_block->block_id(), end_block->block_id()));61006101for (int j = header_idx; j < end_idx; j++) {6102code->at_put(j, code->at(j + 1));6103}6104code->at_put(end_idx, header_block);61056106// correct the flags so that any loop alignment occurs in the right place.6107assert(code->at(end_idx)->is_set(BlockBegin::backward_branch_target_flag), "must be backward branch target");6108code->at(end_idx)->clear(BlockBegin::backward_branch_target_flag);6109code->at(header_idx)->set(BlockBegin::backward_branch_target_flag);6110}6111}6112}61136114void ControlFlowOptimizer::reorder_short_loops(BlockList* code) {6115for (int i = code->length() - 1; i >= 0; i--) {6116BlockBegin* block = code->at(i);61176118if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {6119reorder_short_loop(code, block, i);6120}6121}61226123DEBUG_ONLY(verify(code));6124}61256126// only blocks with exactly one successor can be deleted. Such blocks6127// must always end with an unconditional branch to this successor6128bool ControlFlowOptimizer::can_delete_block(BlockBegin* block) {6129if (block->number_of_sux() != 1 || block->number_of_exception_handlers() != 0 || block->is_entry_block()) {6130return false;6131}61326133LIR_OpList* instructions = block->lir()->instructions_list();61346135assert(instructions->length() >= 2, "block must have label and branch");6136assert(instructions->at(0)->code() == lir_label, "first instruction must always be a label");6137assert(instructions->last()->as_OpBranch() != NULL, "last instrcution must always be a branch");6138assert(instructions->last()->as_OpBranch()->cond() == lir_cond_always, "branch must be unconditional");6139assert(instructions->last()->as_OpBranch()->block() == block->sux_at(0), "branch target must be the successor");61406141// block must have exactly one successor61426143if (instructions->length() == 2 && instructions->last()->info() == NULL) {6144return true;6145}6146return false;6147}61486149// substitute branch targets in all branch-instructions of this blocks6150void ControlFlowOptimizer::substitute_branch_target(BlockBegin* block, BlockBegin* target_from, BlockBegin* target_to) {6151TRACE_LINEAR_SCAN(3, tty->print_cr("Deleting empty block: substituting from B%d to B%d inside B%d", target_from->block_id(), target_to->block_id(), block->block_id()));61526153LIR_OpList* instructions = block->lir()->instructions_list();61546155assert(instructions->at(0)->code() == lir_label, "first instruction must always be a label");6156for (int i = instructions->length() - 1; i >= 1; i--) {6157LIR_Op* op = instructions->at(i);61586159if (op->code() == lir_branch || op->code() == lir_cond_float_branch) {6160assert(op->as_OpBranch() != NULL, "branch must be of type LIR_OpBranch");6161LIR_OpBranch* branch = (LIR_OpBranch*)op;61626163if (branch->block() == target_from) {6164branch->change_block(target_to);6165}6166if (branch->ublock() == target_from) {6167branch->change_ublock(target_to);6168}6169}6170}6171}61726173void ControlFlowOptimizer::delete_empty_blocks(BlockList* code) {6174int old_pos = 0;6175int new_pos = 0;6176int num_blocks = code->length();61776178while (old_pos < num_blocks) {6179BlockBegin* block = code->at(old_pos);61806181if (can_delete_block(block)) {6182BlockBegin* new_target = block->sux_at(0);61836184// propagate backward branch target flag for correct code alignment6185if (block->is_set(BlockBegin::backward_branch_target_flag)) {6186new_target->set(BlockBegin::backward_branch_target_flag);6187}61886189// collect a list with all predecessors that contains each predecessor only once6190// the predecessors of cur are changed during the substitution, so a copy of the6191// predecessor list is necessary6192int j;6193_original_preds.clear();6194for (j = block->number_of_preds() - 1; j >= 0; j--) {6195BlockBegin* pred = block->pred_at(j);6196if (_original_preds.index_of(pred) == -1) {6197_original_preds.append(pred);6198}6199}62006201for (j = _original_preds.length() - 1; j >= 0; j--) {6202BlockBegin* pred = _original_preds.at(j);6203substitute_branch_target(pred, block, new_target);6204pred->substitute_sux(block, new_target);6205}6206} else {6207// adjust position of this block in the block list if blocks before6208// have been deleted6209if (new_pos != old_pos) {6210code->at_put(new_pos, code->at(old_pos));6211}6212new_pos++;6213}6214old_pos++;6215}6216code->truncate(new_pos);62176218DEBUG_ONLY(verify(code));6219}62206221void ControlFlowOptimizer::delete_unnecessary_jumps(BlockList* code) {6222// skip the last block because there a branch is always necessary6223for (int i = code->length() - 2; i >= 0; i--) {6224BlockBegin* block = code->at(i);6225LIR_OpList* instructions = block->lir()->instructions_list();62266227LIR_Op* last_op = instructions->last();6228if (last_op->code() == lir_branch) {6229assert(last_op->as_OpBranch() != NULL, "branch must be of type LIR_OpBranch");6230LIR_OpBranch* last_branch = (LIR_OpBranch*)last_op;62316232assert(last_branch->block() != NULL, "last branch must always have a block as target");6233assert(last_branch->label() == last_branch->block()->label(), "must be equal");62346235if (last_branch->info() == NULL) {6236if (last_branch->block() == code->at(i + 1)) {62376238TRACE_LINEAR_SCAN(3, tty->print_cr("Deleting unconditional branch at end of block B%d", block->block_id()));62396240// delete last branch instruction6241instructions->truncate(instructions->length() - 1);62426243} else {6244LIR_Op* prev_op = instructions->at(instructions->length() - 2);6245if (prev_op->code() == lir_branch || prev_op->code() == lir_cond_float_branch) {6246assert(prev_op->as_OpBranch() != NULL, "branch must be of type LIR_OpBranch");6247LIR_OpBranch* prev_branch = (LIR_OpBranch*)prev_op;62486249if (prev_branch->stub() == NULL) {62506251LIR_Op2* prev_cmp = NULL;62526253for(int j = instructions->length() - 3; j >= 0 && prev_cmp == NULL; j--) {6254prev_op = instructions->at(j);6255if (prev_op->code() == lir_cmp) {6256assert(prev_op->as_Op2() != NULL, "branch must be of type LIR_Op2");6257prev_cmp = (LIR_Op2*)prev_op;6258assert(prev_branch->cond() == prev_cmp->condition(), "should be the same");6259}6260}6261assert(prev_cmp != NULL, "should have found comp instruction for branch");6262if (prev_branch->block() == code->at(i + 1) && prev_branch->info() == NULL) {62636264TRACE_LINEAR_SCAN(3, tty->print_cr("Negating conditional branch and deleting unconditional branch at end of block B%d", block->block_id()));62656266// eliminate a conditional branch to the immediate successor6267prev_branch->change_block(last_branch->block());6268prev_branch->negate_cond();6269prev_cmp->set_condition(prev_branch->cond());6270instructions->truncate(instructions->length() - 1);6271}6272}6273}6274}6275}6276}6277}62786279DEBUG_ONLY(verify(code));6280}62816282void ControlFlowOptimizer::delete_jumps_to_return(BlockList* code) {6283#ifdef ASSERT6284BitMap return_converted(BlockBegin::number_of_blocks());6285return_converted.clear();6286#endif62876288for (int i = code->length() - 1; i >= 0; i--) {6289BlockBegin* block = code->at(i);6290LIR_OpList* cur_instructions = block->lir()->instructions_list();6291LIR_Op* cur_last_op = cur_instructions->last();62926293assert(cur_instructions->at(0)->code() == lir_label, "first instruction must always be a label");6294if (cur_instructions->length() == 2 && cur_last_op->code() == lir_return) {6295// the block contains only a label and a return6296// if a predecessor ends with an unconditional jump to this block, then the jump6297// can be replaced with a return instruction6298//6299// Note: the original block with only a return statement cannot be deleted completely6300// because the predecessors might have other (conditional) jumps to this block6301// -> this may lead to unnecesary return instructions in the final code63026303assert(cur_last_op->info() == NULL, "return instructions do not have debug information");6304assert(block->number_of_sux() == 0 ||6305(return_converted.at(block->block_id()) && block->number_of_sux() == 1),6306"blocks that end with return must not have successors");63076308assert(cur_last_op->as_Op1() != NULL, "return must be LIR_Op1");6309LIR_Opr return_opr = ((LIR_Op1*)cur_last_op)->in_opr();63106311for (int j = block->number_of_preds() - 1; j >= 0; j--) {6312BlockBegin* pred = block->pred_at(j);6313LIR_OpList* pred_instructions = pred->lir()->instructions_list();6314LIR_Op* pred_last_op = pred_instructions->last();63156316if (pred_last_op->code() == lir_branch) {6317assert(pred_last_op->as_OpBranch() != NULL, "branch must be LIR_OpBranch");6318LIR_OpBranch* pred_last_branch = (LIR_OpBranch*)pred_last_op;63196320if (pred_last_branch->block() == block && pred_last_branch->cond() == lir_cond_always && pred_last_branch->info() == NULL) {6321// replace the jump to a return with a direct return6322// Note: currently the edge between the blocks is not deleted6323pred_instructions->at_put(pred_instructions->length() - 1, new LIR_Op1(lir_return, return_opr));6324#ifdef ASSERT6325return_converted.set_bit(pred->block_id());6326#endif6327}6328}6329}6330}6331}6332}633363346335#ifdef ASSERT6336void ControlFlowOptimizer::verify(BlockList* code) {6337for (int i = 0; i < code->length(); i++) {6338BlockBegin* block = code->at(i);6339LIR_OpList* instructions = block->lir()->instructions_list();63406341int j;6342for (j = 0; j < instructions->length(); j++) {6343LIR_OpBranch* op_branch = instructions->at(j)->as_OpBranch();63446345if (op_branch != NULL) {6346assert(op_branch->block() == NULL || code->index_of(op_branch->block()) != -1, "branch target not valid");6347assert(op_branch->ublock() == NULL || code->index_of(op_branch->ublock()) != -1, "branch target not valid");6348}6349}63506351for (j = 0; j < block->number_of_sux() - 1; j++) {6352BlockBegin* sux = block->sux_at(j);6353assert(code->index_of(sux) != -1, "successor not valid");6354}63556356for (j = 0; j < block->number_of_preds() - 1; j++) {6357BlockBegin* pred = block->pred_at(j);6358assert(code->index_of(pred) != -1, "successor not valid");6359}6360}6361}6362#endif636363646365#ifndef PRODUCT63666367// Implementation of LinearStatistic63686369const char* LinearScanStatistic::counter_name(int counter_idx) {6370switch (counter_idx) {6371case counter_method: return "compiled methods";6372case counter_fpu_method: return "methods using fpu";6373case counter_loop_method: return "methods with loops";6374case counter_exception_method:return "methods with xhandler";63756376case counter_loop: return "loops";6377case counter_block: return "blocks";6378case counter_loop_block: return "blocks inside loop";6379case counter_exception_block: return "exception handler entries";6380case counter_interval: return "intervals";6381case counter_fixed_interval: return "fixed intervals";6382case counter_range: return "ranges";6383case counter_fixed_range: return "fixed ranges";6384case counter_use_pos: return "use positions";6385case counter_fixed_use_pos: return "fixed use positions";6386case counter_spill_slots: return "spill slots";63876388// counter for classes of lir instructions6389case counter_instruction: return "total instructions";6390case counter_label: return "labels";6391case counter_entry: return "method entries";6392case counter_return: return "method returns";6393case counter_call: return "method calls";6394case counter_move: return "moves";6395case counter_cmp: return "compare";6396case counter_cond_branch: return "conditional branches";6397case counter_uncond_branch: return "unconditional branches";6398case counter_stub_branch: return "branches to stub";6399case counter_alu: return "artithmetic + logic";6400case counter_alloc: return "allocations";6401case counter_sync: return "synchronisation";6402case counter_throw: return "throw";6403case counter_unwind: return "unwind";6404case counter_typecheck: return "type+null-checks";6405case counter_fpu_stack: return "fpu-stack";6406case counter_misc_inst: return "other instructions";6407case counter_other_inst: return "misc. instructions";64086409// counter for different types of moves6410case counter_move_total: return "total moves";6411case counter_move_reg_reg: return "register->register";6412case counter_move_reg_stack: return "register->stack";6413case counter_move_stack_reg: return "stack->register";6414case counter_move_stack_stack:return "stack->stack";6415case counter_move_reg_mem: return "register->memory";6416case counter_move_mem_reg: return "memory->register";6417case counter_move_const_any: return "constant->any";64186419case blank_line_1: return "";6420case blank_line_2: return "";64216422default: ShouldNotReachHere(); return "";6423}6424}64256426LinearScanStatistic::Counter LinearScanStatistic::base_counter(int counter_idx) {6427if (counter_idx == counter_fpu_method || counter_idx == counter_loop_method || counter_idx == counter_exception_method) {6428return counter_method;6429} else if (counter_idx == counter_loop_block || counter_idx == counter_exception_block) {6430return counter_block;6431} else if (counter_idx >= counter_instruction && counter_idx <= counter_other_inst) {6432return counter_instruction;6433} else if (counter_idx >= counter_move_total && counter_idx <= counter_move_const_any) {6434return counter_move_total;6435}6436return invalid_counter;6437}64386439LinearScanStatistic::LinearScanStatistic() {6440for (int i = 0; i < number_of_counters; i++) {6441_counters_sum[i] = 0;6442_counters_max[i] = -1;6443}64446445}64466447// add the method-local numbers to the total sum6448void LinearScanStatistic::sum_up(LinearScanStatistic &method_statistic) {6449for (int i = 0; i < number_of_counters; i++) {6450_counters_sum[i] += method_statistic._counters_sum[i];6451_counters_max[i] = MAX2(_counters_max[i], method_statistic._counters_sum[i]);6452}6453}64546455void LinearScanStatistic::print(const char* title) {6456if (CountLinearScan || TraceLinearScanLevel > 0) {6457tty->cr();6458tty->print_cr("***** LinearScan statistic - %s *****", title);64596460for (int i = 0; i < number_of_counters; i++) {6461if (_counters_sum[i] > 0 || _counters_max[i] >= 0) {6462tty->print("%25s: %8d", counter_name(i), _counters_sum[i]);64636464if (base_counter(i) != invalid_counter) {6465tty->print(" (%5.1f%%) ", _counters_sum[i] * 100.0 / _counters_sum[base_counter(i)]);6466} else {6467tty->print(" ");6468}64696470if (_counters_max[i] >= 0) {6471tty->print("%8d", _counters_max[i]);6472}6473}6474tty->cr();6475}6476}6477}64786479void LinearScanStatistic::collect(LinearScan* allocator) {6480inc_counter(counter_method);6481if (allocator->has_fpu_registers()) {6482inc_counter(counter_fpu_method);6483}6484if (allocator->num_loops() > 0) {6485inc_counter(counter_loop_method);6486}6487inc_counter(counter_loop, allocator->num_loops());6488inc_counter(counter_spill_slots, allocator->max_spills());64896490int i;6491for (i = 0; i < allocator->interval_count(); i++) {6492Interval* cur = allocator->interval_at(i);64936494if (cur != NULL) {6495inc_counter(counter_interval);6496inc_counter(counter_use_pos, cur->num_use_positions());6497if (LinearScan::is_precolored_interval(cur)) {6498inc_counter(counter_fixed_interval);6499inc_counter(counter_fixed_use_pos, cur->num_use_positions());6500}65016502Range* range = cur->first();6503while (range != Range::end()) {6504inc_counter(counter_range);6505if (LinearScan::is_precolored_interval(cur)) {6506inc_counter(counter_fixed_range);6507}6508range = range->next();6509}6510}6511}65126513bool has_xhandlers = false;6514// Note: only count blocks that are in code-emit order6515for (i = 0; i < allocator->ir()->code()->length(); i++) {6516BlockBegin* cur = allocator->ir()->code()->at(i);65176518inc_counter(counter_block);6519if (cur->loop_depth() > 0) {6520inc_counter(counter_loop_block);6521}6522if (cur->is_set(BlockBegin::exception_entry_flag)) {6523inc_counter(counter_exception_block);6524has_xhandlers = true;6525}65266527LIR_OpList* instructions = cur->lir()->instructions_list();6528for (int j = 0; j < instructions->length(); j++) {6529LIR_Op* op = instructions->at(j);65306531inc_counter(counter_instruction);65326533switch (op->code()) {6534case lir_label: inc_counter(counter_label); break;6535case lir_std_entry:6536case lir_osr_entry: inc_counter(counter_entry); break;6537case lir_return: inc_counter(counter_return); break;65386539case lir_rtcall:6540case lir_static_call:6541case lir_optvirtual_call:6542case lir_virtual_call: inc_counter(counter_call); break;65436544case lir_move: {6545inc_counter(counter_move);6546inc_counter(counter_move_total);65476548LIR_Opr in = op->as_Op1()->in_opr();6549LIR_Opr res = op->as_Op1()->result_opr();6550if (in->is_register()) {6551if (res->is_register()) {6552inc_counter(counter_move_reg_reg);6553} else if (res->is_stack()) {6554inc_counter(counter_move_reg_stack);6555} else if (res->is_address()) {6556inc_counter(counter_move_reg_mem);6557} else {6558ShouldNotReachHere();6559}6560} else if (in->is_stack()) {6561if (res->is_register()) {6562inc_counter(counter_move_stack_reg);6563} else {6564inc_counter(counter_move_stack_stack);6565}6566} else if (in->is_address()) {6567assert(res->is_register(), "must be");6568inc_counter(counter_move_mem_reg);6569} else if (in->is_constant()) {6570inc_counter(counter_move_const_any);6571} else {6572ShouldNotReachHere();6573}6574break;6575}65766577case lir_cmp: inc_counter(counter_cmp); break;65786579case lir_branch:6580case lir_cond_float_branch: {6581LIR_OpBranch* branch = op->as_OpBranch();6582if (branch->block() == NULL) {6583inc_counter(counter_stub_branch);6584} else if (branch->cond() == lir_cond_always) {6585inc_counter(counter_uncond_branch);6586} else {6587inc_counter(counter_cond_branch);6588}6589break;6590}65916592case lir_neg:6593case lir_add:6594case lir_sub:6595case lir_mul:6596case lir_mul_strictfp:6597case lir_div:6598case lir_div_strictfp:6599case lir_rem:6600case lir_sqrt:6601case lir_sin:6602case lir_cos:6603case lir_abs:6604case lir_log10:6605case lir_log:6606case lir_pow:6607case lir_exp:6608case lir_logic_and:6609case lir_logic_or:6610case lir_logic_xor:6611case lir_shl:6612case lir_shr:6613case lir_ushr: inc_counter(counter_alu); break;66146615case lir_alloc_object:6616case lir_alloc_array: inc_counter(counter_alloc); break;66176618case lir_monaddr:6619case lir_lock:6620case lir_unlock: inc_counter(counter_sync); break;66216622case lir_throw: inc_counter(counter_throw); break;66236624case lir_unwind: inc_counter(counter_unwind); break;66256626case lir_null_check:6627case lir_leal:6628case lir_instanceof:6629case lir_checkcast:6630case lir_store_check: inc_counter(counter_typecheck); break;66316632case lir_fpop_raw:6633case lir_fxch:6634case lir_fld: inc_counter(counter_fpu_stack); break;66356636case lir_nop:6637case lir_push:6638case lir_pop:6639case lir_convert:6640case lir_roundfp:6641case lir_cmove: inc_counter(counter_misc_inst); break;66426643default: inc_counter(counter_other_inst); break;6644}6645}6646}66476648if (has_xhandlers) {6649inc_counter(counter_exception_method);6650}6651}66526653void LinearScanStatistic::compute(LinearScan* allocator, LinearScanStatistic &global_statistic) {6654if (CountLinearScan || TraceLinearScanLevel > 0) {66556656LinearScanStatistic local_statistic = LinearScanStatistic();66576658local_statistic.collect(allocator);6659global_statistic.sum_up(local_statistic);66606661if (TraceLinearScanLevel > 2) {6662local_statistic.print("current local statistic");6663}6664}6665}666666676668// Implementation of LinearTimers66696670LinearScanTimers::LinearScanTimers() {6671for (int i = 0; i < number_of_timers; i++) {6672timer(i)->reset();6673}6674}66756676const char* LinearScanTimers::timer_name(int idx) {6677switch (idx) {6678case timer_do_nothing: return "Nothing (Time Check)";6679case timer_number_instructions: return "Number Instructions";6680case timer_compute_local_live_sets: return "Local Live Sets";6681case timer_compute_global_live_sets: return "Global Live Sets";6682case timer_build_intervals: return "Build Intervals";6683case timer_sort_intervals_before: return "Sort Intervals Before";6684case timer_allocate_registers: return "Allocate Registers";6685case timer_resolve_data_flow: return "Resolve Data Flow";6686case timer_sort_intervals_after: return "Sort Intervals After";6687case timer_eliminate_spill_moves: return "Spill optimization";6688case timer_assign_reg_num: return "Assign Reg Num";6689case timer_allocate_fpu_stack: return "Allocate FPU Stack";6690case timer_optimize_lir: return "Optimize LIR";6691default: ShouldNotReachHere(); return "";6692}6693}66946695void LinearScanTimers::begin_method() {6696if (TimeEachLinearScan) {6697// reset all timers to measure only current method6698for (int i = 0; i < number_of_timers; i++) {6699timer(i)->reset();6700}6701}6702}67036704void LinearScanTimers::end_method(LinearScan* allocator) {6705if (TimeEachLinearScan) {67066707double c = timer(timer_do_nothing)->seconds();6708double total = 0;6709for (int i = 1; i < number_of_timers; i++) {6710total += timer(i)->seconds() - c;6711}67126713if (total >= 0.0005) {6714// print all information in one line for automatic processing6715tty->print("@"); allocator->compilation()->method()->print_name();67166717tty->print("@ %d ", allocator->compilation()->method()->code_size());6718tty->print("@ %d ", allocator->block_at(allocator->block_count() - 1)->last_lir_instruction_id() / 2);6719tty->print("@ %d ", allocator->block_count());6720tty->print("@ %d ", allocator->num_virtual_regs());6721tty->print("@ %d ", allocator->interval_count());6722tty->print("@ %d ", allocator->_num_calls);6723tty->print("@ %d ", allocator->num_loops());67246725tty->print("@ %6.6f ", total);6726for (int i = 1; i < number_of_timers; i++) {6727tty->print("@ %4.1f ", ((timer(i)->seconds() - c) / total) * 100);6728}6729tty->cr();6730}6731}6732}67336734void LinearScanTimers::print(double total_time) {6735if (TimeLinearScan) {6736// correction value: sum of dummy-timer that only measures the time that6737// is necesary to start and stop itself6738double c = timer(timer_do_nothing)->seconds();67396740for (int i = 0; i < number_of_timers; i++) {6741double t = timer(i)->seconds();6742tty->print_cr(" %25s: %6.3f s (%4.1f%%) corrected: %6.3f s (%4.1f%%)", timer_name(i), t, (t / total_time) * 100.0, t - c, (t - c) / (total_time - 2 * number_of_timers * c) * 100);6743}6744}6745}67466747#endif // #ifndef PRODUCT674867496750