0N/A/*
2273N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "c1/c1_CFGPrinter.hpp"
1879N/A#include "c1/c1_CodeStubs.hpp"
1879N/A#include "c1/c1_Compilation.hpp"
1879N/A#include "c1/c1_FrameMap.hpp"
1879N/A#include "c1/c1_IR.hpp"
1879N/A#include "c1/c1_LIRGenerator.hpp"
1879N/A#include "c1/c1_LinearScan.hpp"
1879N/A#include "c1/c1_ValueStack.hpp"
1879N/A#include "utilities/bitMap.inline.hpp"
1879N/A#ifdef TARGET_ARCH_x86
1879N/A# include "vmreg_x86.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_sparc
1879N/A# include "vmreg_sparc.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_zero
1879N/A# include "vmreg_zero.inline.hpp"
1879N/A#endif
2073N/A#ifdef TARGET_ARCH_arm
2073N/A# include "vmreg_arm.inline.hpp"
2073N/A#endif
2073N/A#ifdef TARGET_ARCH_ppc
2073N/A# include "vmreg_ppc.inline.hpp"
2073N/A#endif
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/A static LinearScanStatistic _stat_before_alloc;
0N/A static LinearScanStatistic _stat_after_asign;
0N/A static LinearScanStatistic _stat_final;
0N/A
0N/A static LinearScanTimers _total_timer;
0N/A
0N/A // helper macro for short definition of timer
0N/A #define TIME_LINEAR_SCAN(timer_name) TraceTime _block_timer("", _total_timer.timer(LinearScanTimers::timer_name), TimeLinearScan || TimeEachLinearScan, Verbose);
0N/A
0N/A // helper macro for short definition of trace-output inside code
0N/A #define TRACE_LINEAR_SCAN(level, code) \
0N/A if (TraceLinearScanLevel >= level) { \
0N/A code; \
0N/A }
0N/A
0N/A#else
0N/A
0N/A #define TIME_LINEAR_SCAN(timer_name)
0N/A #define TRACE_LINEAR_SCAN(level, code)
0N/A
0N/A#endif
0N/A
0N/A// Map BasicType to spill size in 32-bit words, matching VMReg's notion of words
0N/A#ifdef _LP64
0N/Astatic int type2spill_size[T_CONFLICT+1]={ -1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 0, 1, -1};
0N/A#else
0N/Astatic int type2spill_size[T_CONFLICT+1]={ -1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 0, 1, -1};
0N/A#endif
0N/A
0N/A
0N/A// Implementation of LinearScan
0N/A
0N/ALinearScan::LinearScan(IR* ir, LIRGenerator* gen, FrameMap* frame_map)
0N/A : _compilation(ir->compilation())
0N/A , _ir(ir)
0N/A , _gen(gen)
0N/A , _frame_map(frame_map)
0N/A , _num_virtual_regs(gen->max_virtual_register_number())
0N/A , _has_fpu_registers(false)
0N/A , _num_calls(-1)
0N/A , _max_spills(0)
0N/A , _unused_spill_slot(-1)
0N/A , _intervals(0) // initialized later with correct length
0N/A , _new_intervals_from_allocation(new IntervalList())
0N/A , _sorted_intervals(NULL)
1969N/A , _needs_full_resort(false)
0N/A , _lir_ops(0) // initialized later with correct length
0N/A , _block_of_op(0) // initialized later with correct length
0N/A , _has_info(0)
0N/A , _has_call(0)
0N/A , _scope_value_cache(0) // initialized later with correct length
0N/A , _interval_in_loop(0, 0) // initialized later with correct length
0N/A , _cached_blocks(*ir->linear_scan_order())
304N/A#ifdef X86
0N/A , _fpu_stack_allocator(NULL)
0N/A#endif
0N/A{
0N/A assert(this->ir() != NULL, "check if valid");
0N/A assert(this->compilation() != NULL, "check if valid");
0N/A assert(this->gen() != NULL, "check if valid");
0N/A assert(this->frame_map() != NULL, "check if valid");
0N/A}
0N/A
0N/A
0N/A// ********** functions for converting LIR-Operands to register numbers
0N/A//
0N/A// Emulate a flat register file comprising physical integer registers,
0N/A// physical floating-point registers and virtual registers, in that order.
0N/A// Virtual registers already have appropriate numbers, since V0 is
0N/A// the number of physical registers.
0N/A// Returns -1 for hi word if opr is a single word operand.
0N/A//
0N/A// Note: the inverse operation (calculating an operand for register numbers)
0N/A// is done in calc_operand_for_interval()
0N/A
0N/Aint LinearScan::reg_num(LIR_Opr opr) {
0N/A assert(opr->is_register(), "should not call this otherwise");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A assert(opr->vreg_number() >= nof_regs, "found a virtual register with a fixed-register number");
0N/A return opr->vreg_number();
0N/A } else if (opr->is_single_cpu()) {
0N/A return opr->cpu_regnr();
0N/A } else if (opr->is_double_cpu()) {
0N/A return opr->cpu_regnrLo();
304N/A#ifdef X86
0N/A } else if (opr->is_single_xmm()) {
0N/A return opr->fpu_regnr() + pd_first_xmm_reg;
0N/A } else if (opr->is_double_xmm()) {
0N/A return opr->fpu_regnrLo() + pd_first_xmm_reg;
0N/A#endif
0N/A } else if (opr->is_single_fpu()) {
0N/A return opr->fpu_regnr() + pd_first_fpu_reg;
0N/A } else if (opr->is_double_fpu()) {
0N/A return opr->fpu_regnrLo() + pd_first_fpu_reg;
0N/A } else {
0N/A ShouldNotReachHere();
304N/A return -1;
0N/A }
0N/A}
0N/A
0N/Aint LinearScan::reg_numHi(LIR_Opr opr) {
0N/A assert(opr->is_register(), "should not call this otherwise");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A return -1;
0N/A } else if (opr->is_single_cpu()) {
0N/A return -1;
0N/A } else if (opr->is_double_cpu()) {
0N/A return opr->cpu_regnrHi();
304N/A#ifdef X86
0N/A } else if (opr->is_single_xmm()) {
0N/A return -1;
0N/A } else if (opr->is_double_xmm()) {
0N/A return -1;
0N/A#endif
0N/A } else if (opr->is_single_fpu()) {
0N/A return -1;
0N/A } else if (opr->is_double_fpu()) {
0N/A return opr->fpu_regnrHi() + pd_first_fpu_reg;
0N/A } else {
0N/A ShouldNotReachHere();
304N/A return -1;
0N/A }
0N/A}
0N/A
0N/A
0N/A// ********** functions for classification of intervals
0N/A
0N/Abool LinearScan::is_precolored_interval(const Interval* i) {
0N/A return i->reg_num() < LinearScan::nof_regs;
0N/A}
0N/A
0N/Abool LinearScan::is_virtual_interval(const Interval* i) {
0N/A return i->reg_num() >= LIR_OprDesc::vreg_base;
0N/A}
0N/A
0N/Abool LinearScan::is_precolored_cpu_interval(const Interval* i) {
0N/A return i->reg_num() < LinearScan::nof_cpu_regs;
0N/A}
0N/A
0N/Abool LinearScan::is_virtual_cpu_interval(const Interval* i) {
1601N/A#if defined(__SOFTFP__) || defined(E500V2)
1601N/A return i->reg_num() >= LIR_OprDesc::vreg_base;
1601N/A#else
0N/A return i->reg_num() >= LIR_OprDesc::vreg_base && (i->type() != T_FLOAT && i->type() != T_DOUBLE);
1601N/A#endif // __SOFTFP__ or E500V2
0N/A}
0N/A
0N/Abool LinearScan::is_precolored_fpu_interval(const Interval* i) {
0N/A return i->reg_num() >= LinearScan::nof_cpu_regs && i->reg_num() < LinearScan::nof_regs;
0N/A}
0N/A
0N/Abool LinearScan::is_virtual_fpu_interval(const Interval* i) {
1601N/A#if defined(__SOFTFP__) || defined(E500V2)
1601N/A return false;
1601N/A#else
0N/A return i->reg_num() >= LIR_OprDesc::vreg_base && (i->type() == T_FLOAT || i->type() == T_DOUBLE);
1601N/A#endif // __SOFTFP__ or E500V2
0N/A}
0N/A
0N/Abool LinearScan::is_in_fpu_register(const Interval* i) {
0N/A // fixed intervals not needed for FPU stack allocation
0N/A return i->reg_num() >= nof_regs && pd_first_fpu_reg <= i->assigned_reg() && i->assigned_reg() <= pd_last_fpu_reg;
0N/A}
0N/A
0N/Abool LinearScan::is_oop_interval(const Interval* i) {
0N/A // fixed intervals never contain oops
0N/A return i->reg_num() >= nof_regs && i->type() == T_OBJECT;
0N/A}
0N/A
0N/A
0N/A// ********** General helper functions
0N/A
0N/A// compute next unused stack index that can be used for spilling
0N/Aint LinearScan::allocate_spill_slot(bool double_word) {
0N/A int spill_slot;
0N/A if (double_word) {
0N/A if ((_max_spills & 1) == 1) {
0N/A // alignment of double-word values
0N/A // the hole because of the alignment is filled with the next single-word value
0N/A assert(_unused_spill_slot == -1, "wasting a spill slot");
0N/A _unused_spill_slot = _max_spills;
0N/A _max_spills++;
0N/A }
0N/A spill_slot = _max_spills;
0N/A _max_spills += 2;
0N/A
0N/A } else if (_unused_spill_slot != -1) {
0N/A // re-use hole that was the result of a previous double-word alignment
0N/A spill_slot = _unused_spill_slot;
0N/A _unused_spill_slot = -1;
0N/A
0N/A } else {
0N/A spill_slot = _max_spills;
0N/A _max_spills++;
0N/A }
0N/A
0N/A int result = spill_slot + LinearScan::nof_regs + frame_map()->argcount();
0N/A
0N/A // the class OopMapValue uses only 11 bits for storing the name of the
0N/A // oop location. So a stack slot bigger than 2^11 leads to an overflow
0N/A // that is not reported in product builds. Prevent this by checking the
0N/A // spill slot here (altough this value and the later used location name
0N/A // are slightly different)
0N/A if (result > 2000) {
0N/A bailout("too many stack slots used");
0N/A }
0N/A
0N/A return result;
0N/A}
0N/A
0N/Avoid LinearScan::assign_spill_slot(Interval* it) {
0N/A // assign the canonical spill slot of the parent (if a part of the interval
0N/A // is already spilled) or allocate a new spill slot
0N/A if (it->canonical_spill_slot() >= 0) {
0N/A it->assign_reg(it->canonical_spill_slot());
0N/A } else {
0N/A int spill = allocate_spill_slot(type2spill_size[it->type()] == 2);
0N/A it->set_canonical_spill_slot(spill);
0N/A it->assign_reg(spill);
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::propagate_spill_slots() {
0N/A if (!frame_map()->finalize_frame(max_spills())) {
0N/A bailout("frame too large");
0N/A }
0N/A}
0N/A
0N/A// create a new interval with a predefined reg_num
0N/A// (only used for parent intervals that are created during the building phase)
0N/AInterval* LinearScan::create_interval(int reg_num) {
0N/A assert(_intervals.at(reg_num) == NULL, "overwriting exisiting interval");
0N/A
0N/A Interval* interval = new Interval(reg_num);
0N/A _intervals.at_put(reg_num, interval);
0N/A
0N/A // assign register number for precolored intervals
0N/A if (reg_num < LIR_OprDesc::vreg_base) {
0N/A interval->assign_reg(reg_num);
0N/A }
0N/A return interval;
0N/A}
0N/A
0N/A// assign a new reg_num to the interval and append it to the list of intervals
0N/A// (only used for child intervals that are created during register allocation)
0N/Avoid LinearScan::append_interval(Interval* it) {
0N/A it->set_reg_num(_intervals.length());
0N/A _intervals.append(it);
0N/A _new_intervals_from_allocation->append(it);
0N/A}
0N/A
0N/A// copy the vreg-flags if an interval is split
0N/Avoid LinearScan::copy_register_flags(Interval* from, Interval* to) {
0N/A if (gen()->is_vreg_flag_set(from->reg_num(), LIRGenerator::byte_reg)) {
0N/A gen()->set_vreg_flag(to->reg_num(), LIRGenerator::byte_reg);
0N/A }
0N/A if (gen()->is_vreg_flag_set(from->reg_num(), LIRGenerator::callee_saved)) {
0N/A gen()->set_vreg_flag(to->reg_num(), LIRGenerator::callee_saved);
0N/A }
0N/A
0N/A // Note: do not copy the must_start_in_memory flag because it is not necessary for child
0N/A // intervals (only the very beginning of the interval must be in memory)
0N/A}
0N/A
0N/A
0N/A// ********** spill move optimization
0N/A// eliminate moves from register to stack if stack slot is known to be correct
0N/A
0N/A// called during building of intervals
0N/Avoid LinearScan::change_spill_definition_pos(Interval* interval, int def_pos) {
0N/A assert(interval->is_split_parent(), "can only be called for split parents");
0N/A
0N/A switch (interval->spill_state()) {
0N/A case noDefinitionFound:
0N/A assert(interval->spill_definition_pos() == -1, "must no be set before");
0N/A interval->set_spill_definition_pos(def_pos);
0N/A interval->set_spill_state(oneDefinitionFound);
0N/A break;
0N/A
0N/A case oneDefinitionFound:
0N/A assert(def_pos <= interval->spill_definition_pos(), "positions are processed in reverse order when intervals are created");
0N/A if (def_pos < interval->spill_definition_pos() - 2) {
0N/A // second definition found, so no spill optimization possible for this interval
0N/A interval->set_spill_state(noOptimization);
0N/A } else {
0N/A // two consecutive definitions (because of two-operand LIR form)
0N/A assert(block_of_op_with_id(def_pos) == block_of_op_with_id(interval->spill_definition_pos()), "block must be equal");
0N/A }
0N/A break;
0N/A
0N/A case noOptimization:
0N/A // nothing to do
0N/A break;
0N/A
0N/A default:
0N/A assert(false, "other states not allowed at this time");
0N/A }
0N/A}
0N/A
0N/A// called during register allocation
0N/Avoid LinearScan::change_spill_state(Interval* interval, int spill_pos) {
0N/A switch (interval->spill_state()) {
0N/A case oneDefinitionFound: {
0N/A int def_loop_depth = block_of_op_with_id(interval->spill_definition_pos())->loop_depth();
0N/A int spill_loop_depth = block_of_op_with_id(spill_pos)->loop_depth();
0N/A
0N/A if (def_loop_depth < spill_loop_depth) {
0N/A // the loop depth of the spilling position is higher then the loop depth
0N/A // at the definition of the interval -> move write to memory out of loop
0N/A // by storing at definitin of the interval
0N/A interval->set_spill_state(storeAtDefinition);
0N/A } else {
0N/A // the interval is currently spilled only once, so for now there is no
0N/A // reason to store the interval at the definition
0N/A interval->set_spill_state(oneMoveInserted);
0N/A }
0N/A break;
0N/A }
0N/A
0N/A case oneMoveInserted: {
0N/A // the interval is spilled more then once, so it is better to store it to
0N/A // memory at the definition
0N/A interval->set_spill_state(storeAtDefinition);
0N/A break;
0N/A }
0N/A
0N/A case storeAtDefinition:
0N/A case startInMemory:
0N/A case noOptimization:
0N/A case noDefinitionFound:
0N/A // nothing to do
0N/A break;
0N/A
0N/A default:
0N/A assert(false, "other states not allowed at this time");
0N/A }
0N/A}
0N/A
0N/A
0N/Abool LinearScan::must_store_at_definition(const Interval* i) {
0N/A return i->is_split_parent() && i->spill_state() == storeAtDefinition;
0N/A}
0N/A
0N/A// called once before asignment of register numbers
0N/Avoid LinearScan::eliminate_spill_moves() {
0N/A TIME_LINEAR_SCAN(timer_eliminate_spill_moves);
0N/A TRACE_LINEAR_SCAN(3, tty->print_cr("***** Eliminating unnecessary spill moves"));
0N/A
0N/A // collect all intervals that must be stored after their definion.
0N/A // the list is sorted by Interval::spill_definition_pos
0N/A Interval* interval;
0N/A Interval* temp_list;
0N/A create_unhandled_lists(&interval, &temp_list, must_store_at_definition, NULL);
0N/A
0N/A#ifdef ASSERT
0N/A Interval* prev = NULL;
0N/A Interval* temp = interval;
0N/A while (temp != Interval::end()) {
0N/A assert(temp->spill_definition_pos() > 0, "invalid spill definition pos");
0N/A if (prev != NULL) {
0N/A assert(temp->from() >= prev->from(), "intervals not sorted");
0N/A assert(temp->spill_definition_pos() >= prev->spill_definition_pos(), "when intervals are sorted by from, then they must also be sorted by spill_definition_pos");
0N/A }
0N/A
0N/A assert(temp->canonical_spill_slot() >= LinearScan::nof_regs, "interval has no spill slot assigned");
0N/A assert(temp->spill_definition_pos() >= temp->from(), "invalid order");
0N/A assert(temp->spill_definition_pos() <= temp->from() + 2, "only intervals defined once at their start-pos can be optimized");
0N/A
0N/A TRACE_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()));
0N/A
0N/A temp = temp->next();
0N/A }
0N/A#endif
0N/A
0N/A LIR_InsertionBuffer insertion_buffer;
0N/A int num_blocks = block_count();
0N/A for (int i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A int num_inst = instructions->length();
0N/A bool has_new = false;
0N/A
0N/A // iterate all instructions of the block. skip the first because it is always a label
0N/A for (int j = 1; j < num_inst; j++) {
0N/A LIR_Op* op = instructions->at(j);
0N/A int op_id = op->id();
0N/A
0N/A if (op_id == -1) {
0N/A // remove move from register to stack if the stack slot is guaranteed to be correct.
0N/A // only moves that have been inserted by LinearScan can be removed.
0N/A assert(op->code() == lir_move, "only moves can have a op_id of -1");
0N/A assert(op->as_Op1() != NULL, "move must be LIR_Op1");
0N/A assert(op->as_Op1()->result_opr()->is_virtual(), "LinearScan inserts only moves to virtual registers");
0N/A
0N/A LIR_Op1* op1 = (LIR_Op1*)op;
0N/A Interval* interval = interval_at(op1->result_opr()->vreg_number());
0N/A
0N/A if (interval->assigned_reg() >= LinearScan::nof_regs && interval->always_in_memory()) {
0N/A // move target is a stack slot that is always correct, so eliminate instruction
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("eliminating move from interval %d to %d", op1->in_opr()->vreg_number(), op1->result_opr()->vreg_number()));
0N/A instructions->at_put(j, NULL); // NULL-instructions are deleted by assign_reg_num
0N/A }
0N/A
0N/A } else {
0N/A // insert move from register to stack just after the beginning of the interval
0N/A assert(interval == Interval::end() || interval->spill_definition_pos() >= op_id, "invalid order");
0N/A assert(interval == Interval::end() || (interval->is_split_parent() && interval->spill_state() == storeAtDefinition), "invalid interval");
0N/A
0N/A while (interval != Interval::end() && interval->spill_definition_pos() == op_id) {
0N/A if (!has_new) {
0N/A // prepare insertion buffer (appended when all instructions of the block are processed)
0N/A insertion_buffer.init(block->lir());
0N/A has_new = true;
0N/A }
0N/A
0N/A LIR_Opr from_opr = operand_for_interval(interval);
0N/A LIR_Opr to_opr = canonical_spill_opr(interval);
0N/A assert(from_opr->is_fixed_cpu() || from_opr->is_fixed_fpu(), "from operand must be a register");
0N/A assert(to_opr->is_stack(), "to operand must be a stack slot");
0N/A
0N/A insertion_buffer.move(j, from_opr, to_opr);
0N/A TRACE_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));
0N/A
0N/A interval = interval->next();
0N/A }
0N/A }
0N/A } // end of instruction iteration
0N/A
0N/A if (has_new) {
0N/A block->lir()->append(&insertion_buffer);
0N/A }
0N/A } // end of block iteration
0N/A
0N/A assert(interval == Interval::end(), "missed an interval");
0N/A}
0N/A
0N/A
0N/A// ********** Phase 1: number all instructions in all blocks
0N/A// Compute depth-first and linear scan block orders, and number LIR_Op nodes for linear scan.
0N/A
0N/Avoid LinearScan::number_instructions() {
0N/A {
0N/A // dummy-timer to measure the cost of the timer itself
0N/A // (this time is then subtracted from all other timers to get the real value)
0N/A TIME_LINEAR_SCAN(timer_do_nothing);
0N/A }
0N/A TIME_LINEAR_SCAN(timer_number_instructions);
0N/A
0N/A // Assign IDs to LIR nodes and build a mapping, lir_ops, from ID to LIR_Op node.
0N/A int num_blocks = block_count();
0N/A int num_instructions = 0;
0N/A int i;
0N/A for (i = 0; i < num_blocks; i++) {
0N/A num_instructions += block_at(i)->lir()->instructions_list()->length();
0N/A }
0N/A
0N/A // initialize with correct length
0N/A _lir_ops = LIR_OpArray(num_instructions);
0N/A _block_of_op = BlockBeginArray(num_instructions);
0N/A
0N/A int op_id = 0;
0N/A int idx = 0;
0N/A
0N/A for (i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A block->set_first_lir_instruction_id(op_id);
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A
0N/A int num_inst = instructions->length();
0N/A for (int j = 0; j < num_inst; j++) {
0N/A LIR_Op* op = instructions->at(j);
0N/A op->set_id(op_id);
0N/A
0N/A _lir_ops.at_put(idx, op);
0N/A _block_of_op.at_put(idx, block);
0N/A assert(lir_op_with_id(op_id) == op, "must match");
0N/A
0N/A idx++;
0N/A op_id += 2; // numbering of lir_ops by two
0N/A }
0N/A block->set_last_lir_instruction_id(op_id - 2);
0N/A }
0N/A assert(idx == num_instructions, "must match");
0N/A assert(idx * 2 == op_id, "must match");
0N/A
0N/A _has_call = BitMap(num_instructions); _has_call.clear();
0N/A _has_info = BitMap(num_instructions); _has_info.clear();
0N/A}
0N/A
0N/A
0N/A// ********** Phase 2: compute local live sets separately for each block
0N/A// (sets live_gen and live_kill for each block)
0N/A
0N/Avoid LinearScan::set_live_gen_kill(Value value, LIR_Op* op, BitMap& live_gen, BitMap& live_kill) {
0N/A LIR_Opr opr = value->operand();
0N/A Constant* con = value->as_Constant();
0N/A
0N/A // check some asumptions about debug information
0N/A assert(!value->type()->is_illegal(), "if this local is used by the interpreter it shouldn't be of indeterminate type");
0N/A assert(con == NULL || opr->is_virtual() || opr->is_constant() || opr->is_illegal(), "asumption: Constant instructions have only constant operands");
0N/A assert(con != NULL || opr->is_virtual(), "asumption: non-Constant instructions have only virtual operands");
0N/A
0N/A if ((con == NULL || con->is_pinned()) && opr->is_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A int reg = opr->vreg_number();
0N/A if (!live_kill.at(reg)) {
0N/A live_gen.set_bit(reg);
0N/A TRACE_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));
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::compute_local_live_sets() {
0N/A TIME_LINEAR_SCAN(timer_compute_local_live_sets);
0N/A
0N/A int num_blocks = block_count();
0N/A int live_size = live_set_size();
0N/A bool local_has_fpu_registers = false;
0N/A int local_num_calls = 0;
0N/A LIR_OpVisitState visitor;
0N/A
0N/A BitMap2D local_interval_in_loop = BitMap2D(_num_virtual_regs, num_loops());
0N/A local_interval_in_loop.clear();
0N/A
0N/A // iterate all blocks
0N/A for (int i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A
0N/A BitMap live_gen(live_size); live_gen.clear();
0N/A BitMap live_kill(live_size); live_kill.clear();
0N/A
0N/A if (block->is_set(BlockBegin::exception_entry_flag)) {
0N/A // Phi functions at the begin of an exception handler are
0N/A // implicitly defined (= killed) at the beginning of the block.
0N/A for_each_phi_fun(block, phi,
0N/A live_kill.set_bit(phi->operand()->vreg_number())
0N/A );
0N/A }
0N/A
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A int num_inst = instructions->length();
0N/A
0N/A // iterate all instructions of the block. skip the first because it is always a label
0N/A assert(visitor.no_operands(instructions->at(0)), "first operation must always be a label");
0N/A for (int j = 1; j < num_inst; j++) {
0N/A LIR_Op* op = instructions->at(j);
0N/A
0N/A // visit operation to collect all operands
0N/A visitor.visit(op);
0N/A
0N/A if (visitor.has_call()) {
0N/A _has_call.set_bit(op->id() >> 1);
0N/A local_num_calls++;
0N/A }
0N/A if (visitor.info_count() > 0) {
0N/A _has_info.set_bit(op->id() >> 1);
0N/A }
0N/A
0N/A // iterate input operands of instruction
0N/A int k, n, reg;
0N/A n = visitor.opr_count(LIR_OpVisitState::inputMode);
0N/A for (k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::inputMode, k);
0N/A assert(opr->is_register(), "visitor should only return register operands");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A reg = opr->vreg_number();
0N/A if (!live_kill.at(reg)) {
0N/A live_gen.set_bit(reg);
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" Setting live_gen for register %d at instruction %d", reg, op->id()));
0N/A }
0N/A if (block->loop_index() >= 0) {
0N/A local_interval_in_loop.set_bit(reg, block->loop_index());
0N/A }
0N/A local_has_fpu_registers = local_has_fpu_registers || opr->is_virtual_fpu();
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // fixed intervals are never live at block boundaries, so
0N/A // they need not be processed in live sets.
0N/A // this is checked by these assertions to be sure about it.
0N/A // the entry block may have incoming values in registers, which is ok.
0N/A if (!opr->is_virtual_register() && block != ir()->start()) {
0N/A reg = reg_num(opr);
0N/A if (is_processed_reg_num(reg)) {
0N/A assert(live_kill.at(reg), "using fixed register that is not defined in this block");
0N/A }
0N/A reg = reg_numHi(opr);
0N/A if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {
0N/A assert(live_kill.at(reg), "using fixed register that is not defined in this block");
0N/A }
0N/A }
0N/A#endif
0N/A }
0N/A
0N/A // Add uses of live locals from interpreter's point of view for proper debug information generation
0N/A n = visitor.info_count();
0N/A for (k = 0; k < n; k++) {
0N/A CodeEmitInfo* info = visitor.info_at(k);
0N/A ValueStack* stack = info->stack();
0N/A for_each_state_value(stack, value,
0N/A set_live_gen_kill(value, op, live_gen, live_kill)
0N/A );
0N/A }
0N/A
0N/A // iterate temp operands of instruction
0N/A n = visitor.opr_count(LIR_OpVisitState::tempMode);
0N/A for (k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::tempMode, k);
0N/A assert(opr->is_register(), "visitor should only return register operands");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A reg = opr->vreg_number();
0N/A live_kill.set_bit(reg);
0N/A if (block->loop_index() >= 0) {
0N/A local_interval_in_loop.set_bit(reg, block->loop_index());
0N/A }
0N/A local_has_fpu_registers = local_has_fpu_registers || opr->is_virtual_fpu();
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // fixed intervals are never live at block boundaries, so
0N/A // they need not be processed in live sets
0N/A // process them only in debug mode so that this can be checked
0N/A if (!opr->is_virtual_register()) {
0N/A reg = reg_num(opr);
0N/A if (is_processed_reg_num(reg)) {
0N/A live_kill.set_bit(reg_num(opr));
0N/A }
0N/A reg = reg_numHi(opr);
0N/A if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {
0N/A live_kill.set_bit(reg);
0N/A }
0N/A }
0N/A#endif
0N/A }
0N/A
0N/A // iterate output operands of instruction
0N/A n = visitor.opr_count(LIR_OpVisitState::outputMode);
0N/A for (k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::outputMode, k);
0N/A assert(opr->is_register(), "visitor should only return register operands");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A reg = opr->vreg_number();
0N/A live_kill.set_bit(reg);
0N/A if (block->loop_index() >= 0) {
0N/A local_interval_in_loop.set_bit(reg, block->loop_index());
0N/A }
0N/A local_has_fpu_registers = local_has_fpu_registers || opr->is_virtual_fpu();
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // fixed intervals are never live at block boundaries, so
0N/A // they need not be processed in live sets
0N/A // process them only in debug mode so that this can be checked
0N/A if (!opr->is_virtual_register()) {
0N/A reg = reg_num(opr);
0N/A if (is_processed_reg_num(reg)) {
0N/A live_kill.set_bit(reg_num(opr));
0N/A }
0N/A reg = reg_numHi(opr);
0N/A if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {
0N/A live_kill.set_bit(reg);
0N/A }
0N/A }
0N/A#endif
0N/A }
0N/A } // end of instruction iteration
0N/A
0N/A block->set_live_gen (live_gen);
0N/A block->set_live_kill(live_kill);
0N/A block->set_live_in (BitMap(live_size)); block->live_in().clear();
0N/A block->set_live_out (BitMap(live_size)); block->live_out().clear();
0N/A
0N/A TRACE_LINEAR_SCAN(4, tty->print("live_gen B%d ", block->block_id()); print_bitmap(block->live_gen()));
0N/A TRACE_LINEAR_SCAN(4, tty->print("live_kill B%d ", block->block_id()); print_bitmap(block->live_kill()));
0N/A } // end of block iteration
0N/A
0N/A // propagate local calculated information into LinearScan object
0N/A _has_fpu_registers = local_has_fpu_registers;
0N/A compilation()->set_has_fpu_code(local_has_fpu_registers);
0N/A
0N/A _num_calls = local_num_calls;
0N/A _interval_in_loop = local_interval_in_loop;
0N/A}
0N/A
0N/A
0N/A// ********** Phase 3: perform a backward dataflow analysis to compute global live sets
0N/A// (sets live_in and live_out for each block)
0N/A
0N/Avoid LinearScan::compute_global_live_sets() {
0N/A TIME_LINEAR_SCAN(timer_compute_global_live_sets);
0N/A
0N/A int num_blocks = block_count();
0N/A bool change_occurred;
0N/A bool change_occurred_in_block;
0N/A int iteration_count = 0;
0N/A BitMap live_out(live_set_size()); live_out.clear(); // scratch set for calculations
0N/A
0N/A // Perform a backward dataflow analysis to compute live_out and live_in for each block.
0N/A // The loop is executed until a fixpoint is reached (no changes in an iteration)
0N/A // Exception handlers must be processed because not all live values are
0N/A // present in the state array, e.g. because of global value numbering
0N/A do {
0N/A change_occurred = false;
0N/A
0N/A // iterate all blocks in reverse order
0N/A for (int i = num_blocks - 1; i >= 0; i--) {
0N/A BlockBegin* block = block_at(i);
0N/A
0N/A change_occurred_in_block = false;
0N/A
0N/A // live_out(block) is the union of live_in(sux), for successors sux of block
0N/A int n = block->number_of_sux();
0N/A int e = block->number_of_exception_handlers();
0N/A if (n + e > 0) {
0N/A // block has successors
0N/A if (n > 0) {
0N/A live_out.set_from(block->sux_at(0)->live_in());
0N/A for (int j = 1; j < n; j++) {
0N/A live_out.set_union(block->sux_at(j)->live_in());
0N/A }
0N/A } else {
0N/A live_out.clear();
0N/A }
0N/A for (int j = 0; j < e; j++) {
0N/A live_out.set_union(block->exception_handler_at(j)->live_in());
0N/A }
0N/A
0N/A if (!block->live_out().is_same(live_out)) {
0N/A // A change occurred. Swap the old and new live out sets to avoid copying.
0N/A BitMap temp = block->live_out();
0N/A block->set_live_out(live_out);
0N/A live_out = temp;
0N/A
0N/A change_occurred = true;
0N/A change_occurred_in_block = true;
0N/A }
0N/A }
0N/A
0N/A if (iteration_count == 0 || change_occurred_in_block) {
0N/A // live_in(block) is the union of live_gen(block) with (live_out(block) & !live_kill(block))
0N/A // note: live_in has to be computed only in first iteration or if live_out has changed!
0N/A BitMap live_in = block->live_in();
0N/A live_in.set_from(block->live_out());
0N/A live_in.set_difference(block->live_kill());
0N/A live_in.set_union(block->live_gen());
0N/A }
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceLinearScanLevel >= 4) {
0N/A char c = ' ';
0N/A if (iteration_count == 0 || change_occurred_in_block) {
0N/A c = '*';
0N/A }
0N/A tty->print("(%d) live_in%c B%d ", iteration_count, c, block->block_id()); print_bitmap(block->live_in());
0N/A tty->print("(%d) live_out%c B%d ", iteration_count, c, block->block_id()); print_bitmap(block->live_out());
0N/A }
0N/A#endif
0N/A }
0N/A iteration_count++;
0N/A
0N/A if (change_occurred && iteration_count > 50) {
0N/A BAILOUT("too many iterations in compute_global_live_sets");
0N/A }
0N/A } while (change_occurred);
0N/A
0N/A
0N/A#ifdef ASSERT
0N/A // check that fixed intervals are not live at block boundaries
0N/A // (live set must be empty at fixed intervals)
0N/A for (int i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A for (int j = 0; j < LIR_OprDesc::vreg_base; j++) {
0N/A assert(block->live_in().at(j) == false, "live_in set of fixed register must be empty");
0N/A assert(block->live_out().at(j) == false, "live_out set of fixed register must be empty");
0N/A assert(block->live_gen().at(j) == false, "live_gen set of fixed register must be empty");
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A // check that the live_in set of the first block is empty
0N/A BitMap live_in_args(ir()->start()->live_in().size());
0N/A live_in_args.clear();
0N/A if (!ir()->start()->live_in().is_same(live_in_args)) {
0N/A#ifdef ASSERT
0N/A tty->print_cr("Error: live_in set of first block must be empty (when this fails, virtual registers are used before they are defined)");
0N/A tty->print_cr("affected registers:");
0N/A print_bitmap(ir()->start()->live_in());
0N/A
0N/A // print some additional information to simplify debugging
0N/A for (unsigned int i = 0; i < ir()->start()->live_in().size(); i++) {
0N/A if (ir()->start()->live_in().at(i)) {
0N/A Instruction* instr = gen()->instruction_for_vreg(i);
0N/A tty->print_cr("* vreg %d (HIR instruction %c%d)", i, instr == NULL ? ' ' : instr->type()->tchar(), instr == NULL ? 0 : instr->id());
0N/A
0N/A for (int j = 0; j < num_blocks; j++) {
0N/A BlockBegin* block = block_at(j);
0N/A if (block->live_gen().at(i)) {
0N/A tty->print_cr(" used in block B%d", block->block_id());
0N/A }
0N/A if (block->live_kill().at(i)) {
0N/A tty->print_cr(" defined in block B%d", block->block_id());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A#endif
0N/A // when this fails, virtual registers are used before they are defined.
0N/A assert(false, "live_in set of first block must be empty");
0N/A // bailout of if this occurs in product mode.
0N/A bailout("live_in set of first block not empty");
0N/A }
0N/A}
0N/A
0N/A
0N/A// ********** Phase 4: build intervals
0N/A// (fills the list _intervals)
0N/A
0N/Avoid LinearScan::add_use(Value value, int from, int to, IntervalUseKind use_kind) {
0N/A assert(!value->type()->is_illegal(), "if this value is used by the interpreter it shouldn't be of indeterminate type");
0N/A LIR_Opr opr = value->operand();
0N/A Constant* con = value->as_Constant();
0N/A
0N/A if ((con == NULL || con->is_pinned()) && opr->is_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A add_use(opr, from, to, use_kind);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::add_def(LIR_Opr opr, int def_pos, IntervalUseKind use_kind) {
0N/A TRACE_LINEAR_SCAN(2, tty->print(" def "); opr->print(tty); tty->print_cr(" def_pos %d (%d)", def_pos, use_kind));
0N/A assert(opr->is_register(), "should not be called otherwise");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A add_def(opr->vreg_number(), def_pos, use_kind, opr->type_register());
0N/A
0N/A } else {
0N/A int reg = reg_num(opr);
0N/A if (is_processed_reg_num(reg)) {
0N/A add_def(reg, def_pos, use_kind, opr->type_register());
0N/A }
0N/A reg = reg_numHi(opr);
0N/A if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {
0N/A add_def(reg, def_pos, use_kind, opr->type_register());
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::add_use(LIR_Opr opr, int from, int to, IntervalUseKind use_kind) {
0N/A TRACE_LINEAR_SCAN(2, tty->print(" use "); opr->print(tty); tty->print_cr(" from %d to %d (%d)", from, to, use_kind));
0N/A assert(opr->is_register(), "should not be called otherwise");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A add_use(opr->vreg_number(), from, to, use_kind, opr->type_register());
0N/A
0N/A } else {
0N/A int reg = reg_num(opr);
0N/A if (is_processed_reg_num(reg)) {
0N/A add_use(reg, from, to, use_kind, opr->type_register());
0N/A }
0N/A reg = reg_numHi(opr);
0N/A if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {
0N/A add_use(reg, from, to, use_kind, opr->type_register());
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::add_temp(LIR_Opr opr, int temp_pos, IntervalUseKind use_kind) {
0N/A TRACE_LINEAR_SCAN(2, tty->print(" temp "); opr->print(tty); tty->print_cr(" temp_pos %d (%d)", temp_pos, use_kind));
0N/A assert(opr->is_register(), "should not be called otherwise");
0N/A
0N/A if (opr->is_virtual_register()) {
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A add_temp(opr->vreg_number(), temp_pos, use_kind, opr->type_register());
0N/A
0N/A } else {
0N/A int reg = reg_num(opr);
0N/A if (is_processed_reg_num(reg)) {
0N/A add_temp(reg, temp_pos, use_kind, opr->type_register());
0N/A }
0N/A reg = reg_numHi(opr);
0N/A if (is_valid_reg_num(reg) && is_processed_reg_num(reg)) {
0N/A add_temp(reg, temp_pos, use_kind, opr->type_register());
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::add_def(int reg_num, int def_pos, IntervalUseKind use_kind, BasicType type) {
0N/A Interval* interval = interval_at(reg_num);
0N/A if (interval != NULL) {
0N/A assert(interval->reg_num() == reg_num, "wrong interval");
0N/A
0N/A if (type != T_ILLEGAL) {
0N/A interval->set_type(type);
0N/A }
0N/A
0N/A Range* r = interval->first();
0N/A if (r->from() <= def_pos) {
0N/A // Update the starting point (when a range is first created for a use, its
0N/A // start is the beginning of the current block until a def is encountered.)
0N/A r->set_from(def_pos);
0N/A interval->add_use_pos(def_pos, use_kind);
0N/A
0N/A } else {
0N/A // Dead value - make vacuous interval
0N/A // also add use_kind for dead intervals
0N/A interval->add_range(def_pos, def_pos + 1);
0N/A interval->add_use_pos(def_pos, use_kind);
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("Warning: def of reg %d at %d occurs without use", reg_num, def_pos));
0N/A }
0N/A
0N/A } else {
0N/A // Dead value - make vacuous interval
0N/A // also add use_kind for dead intervals
0N/A interval = create_interval(reg_num);
0N/A if (type != T_ILLEGAL) {
0N/A interval->set_type(type);
0N/A }
0N/A
0N/A interval->add_range(def_pos, def_pos + 1);
0N/A interval->add_use_pos(def_pos, use_kind);
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("Warning: dead value %d at %d in live intervals", reg_num, def_pos));
0N/A }
0N/A
0N/A change_spill_definition_pos(interval, def_pos);
0N/A if (use_kind == noUse && interval->spill_state() <= startInMemory) {
0N/A // detection of method-parameters and roundfp-results
0N/A // TODO: move this directly to position where use-kind is computed
0N/A interval->set_spill_state(startInMemory);
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::add_use(int reg_num, int from, int to, IntervalUseKind use_kind, BasicType type) {
0N/A Interval* interval = interval_at(reg_num);
0N/A if (interval == NULL) {
0N/A interval = create_interval(reg_num);
0N/A }
0N/A assert(interval->reg_num() == reg_num, "wrong interval");
0N/A
0N/A if (type != T_ILLEGAL) {
0N/A interval->set_type(type);
0N/A }
0N/A
0N/A interval->add_range(from, to);
0N/A interval->add_use_pos(to, use_kind);
0N/A}
0N/A
0N/Avoid LinearScan::add_temp(int reg_num, int temp_pos, IntervalUseKind use_kind, BasicType type) {
0N/A Interval* interval = interval_at(reg_num);
0N/A if (interval == NULL) {
0N/A interval = create_interval(reg_num);
0N/A }
0N/A assert(interval->reg_num() == reg_num, "wrong interval");
0N/A
0N/A if (type != T_ILLEGAL) {
0N/A interval->set_type(type);
0N/A }
0N/A
0N/A interval->add_range(temp_pos, temp_pos + 1);
0N/A interval->add_use_pos(temp_pos, use_kind);
0N/A}
0N/A
0N/A
0N/A// the results of this functions are used for optimizing spilling and reloading
0N/A// if the functions return shouldHaveRegister and the interval is spilled,
0N/A// it is not reloaded to a register.
0N/AIntervalUseKind LinearScan::use_kind_of_output_operand(LIR_Op* op, LIR_Opr opr) {
0N/A if (op->code() == lir_move) {
0N/A assert(op->as_Op1() != NULL, "lir_move must be LIR_Op1");
0N/A LIR_Op1* move = (LIR_Op1*)op;
0N/A LIR_Opr res = move->result_opr();
0N/A bool result_in_memory = res->is_virtual() && gen()->is_vreg_flag_set(res->vreg_number(), LIRGenerator::must_start_in_memory);
0N/A
0N/A if (result_in_memory) {
0N/A // Begin of an interval with must_start_in_memory set.
0N/A // This interval will always get a stack slot first, so return noUse.
0N/A return noUse;
0N/A
0N/A } else if (move->in_opr()->is_stack()) {
0N/A // method argument (condition must be equal to handle_method_arguments)
0N/A return noUse;
0N/A
0N/A } else if (move->in_opr()->is_register() && move->result_opr()->is_register()) {
0N/A // Move from register to register
0N/A if (block_of_op_with_id(op->id())->is_set(BlockBegin::osr_entry_flag)) {
0N/A // special handling of phi-function moves inside osr-entry blocks
0N/A // input operand must have a register instead of output operand (leads to better register allocation)
0N/A return shouldHaveRegister;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (opr->is_virtual() &&
0N/A gen()->is_vreg_flag_set(opr->vreg_number(), LIRGenerator::must_start_in_memory)) {
0N/A // result is a stack-slot, so prevent immediate reloading
0N/A return noUse;
0N/A }
0N/A
0N/A // all other operands require a register
0N/A return mustHaveRegister;
0N/A}
0N/A
0N/AIntervalUseKind LinearScan::use_kind_of_input_operand(LIR_Op* op, LIR_Opr opr) {
0N/A if (op->code() == lir_move) {
0N/A assert(op->as_Op1() != NULL, "lir_move must be LIR_Op1");
0N/A LIR_Op1* move = (LIR_Op1*)op;
0N/A LIR_Opr res = move->result_opr();
0N/A bool result_in_memory = res->is_virtual() && gen()->is_vreg_flag_set(res->vreg_number(), LIRGenerator::must_start_in_memory);
0N/A
0N/A if (result_in_memory) {
0N/A // Move to an interval with must_start_in_memory set.
0N/A // To avoid moves from stack to stack (not allowed) force the input operand to a register
0N/A return mustHaveRegister;
0N/A
0N/A } else if (move->in_opr()->is_register() && move->result_opr()->is_register()) {
0N/A // Move from register to register
0N/A if (block_of_op_with_id(op->id())->is_set(BlockBegin::osr_entry_flag)) {
0N/A // special handling of phi-function moves inside osr-entry blocks
0N/A // input operand must have a register instead of output operand (leads to better register allocation)
0N/A return mustHaveRegister;
0N/A }
0N/A
0N/A // The input operand is not forced to a register (moves from stack to register are allowed),
0N/A // but it is faster if the input operand is in a register
0N/A return shouldHaveRegister;
0N/A }
0N/A }
0N/A
0N/A
304N/A#ifdef X86
0N/A if (op->code() == lir_cmove) {
0N/A // conditional moves can handle stack operands
0N/A assert(op->result_opr()->is_register(), "result must always be in a register");
0N/A return shouldHaveRegister;
0N/A }
0N/A
0N/A // optimizations for second input operand of arithmehtic operations on Intel
0N/A // this operand is allowed to be on the stack in some cases
0N/A BasicType opr_type = opr->type_register();
0N/A if (opr_type == T_FLOAT || opr_type == T_DOUBLE) {
0N/A if ((UseSSE == 1 && opr_type == T_FLOAT) || UseSSE >= 2) {
0N/A // SSE float instruction (T_DOUBLE only supported with SSE2)
0N/A switch (op->code()) {
0N/A case lir_cmp:
0N/A case lir_add:
0N/A case lir_sub:
0N/A case lir_mul:
0N/A case lir_div:
0N/A {
0N/A assert(op->as_Op2() != NULL, "must be LIR_Op2");
0N/A LIR_Op2* op2 = (LIR_Op2*)op;
0N/A if (op2->in_opr1() != op2->in_opr2() && op2->in_opr2() == opr) {
0N/A assert((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");
0N/A return shouldHaveRegister;
0N/A }
0N/A }
0N/A }
0N/A } else {
0N/A // FPU stack float instruction
0N/A switch (op->code()) {
0N/A case lir_add:
0N/A case lir_sub:
0N/A case lir_mul:
0N/A case lir_div:
0N/A {
0N/A assert(op->as_Op2() != NULL, "must be LIR_Op2");
0N/A LIR_Op2* op2 = (LIR_Op2*)op;
0N/A if (op2->in_opr1() != op2->in_opr2() && op2->in_opr2() == opr) {
0N/A assert((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");
0N/A return shouldHaveRegister;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A } else if (opr_type != T_LONG) {
0N/A // integer instruction (note: long operands must always be in register)
0N/A switch (op->code()) {
0N/A case lir_cmp:
0N/A case lir_add:
0N/A case lir_sub:
0N/A case lir_logic_and:
0N/A case lir_logic_or:
0N/A case lir_logic_xor:
0N/A {
0N/A assert(op->as_Op2() != NULL, "must be LIR_Op2");
0N/A LIR_Op2* op2 = (LIR_Op2*)op;
0N/A if (op2->in_opr1() != op2->in_opr2() && op2->in_opr2() == opr) {
0N/A assert((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");
0N/A return shouldHaveRegister;
0N/A }
0N/A }
0N/A }
0N/A }
304N/A#endif // X86
0N/A
0N/A // all other operands require a register
0N/A return mustHaveRegister;
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::handle_method_arguments(LIR_Op* op) {
0N/A // special handling for method arguments (moves from stack to virtual register):
0N/A // the interval gets no register assigned, but the stack slot.
0N/A // it is split before the first use by the register allocator.
0N/A
0N/A if (op->code() == lir_move) {
0N/A assert(op->as_Op1() != NULL, "must be LIR_Op1");
0N/A LIR_Op1* move = (LIR_Op1*)op;
0N/A
0N/A if (move->in_opr()->is_stack()) {
0N/A#ifdef ASSERT
0N/A int arg_size = compilation()->method()->arg_size();
0N/A LIR_Opr o = move->in_opr();
0N/A if (o->is_single_stack()) {
0N/A assert(o->single_stack_ix() >= 0 && o->single_stack_ix() < arg_size, "out of range");
0N/A } else if (o->is_double_stack()) {
0N/A assert(o->double_stack_ix() >= 0 && o->double_stack_ix() < arg_size, "out of range");
0N/A } else {
0N/A ShouldNotReachHere();
0N/A }
0N/A
0N/A assert(move->id() > 0, "invalid id");
0N/A assert(block_of_op_with_id(move->id())->number_of_preds() == 0, "move from stack must be in first block");
0N/A assert(move->result_opr()->is_virtual(), "result of move must be a virtual register");
0N/A
0N/A TRACE_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())));
0N/A#endif
0N/A
0N/A Interval* interval = interval_at(reg_num(move->result_opr()));
0N/A
0N/A int stack_slot = LinearScan::nof_regs + (move->in_opr()->is_single_stack() ? move->in_opr()->single_stack_ix() : move->in_opr()->double_stack_ix());
0N/A interval->set_canonical_spill_slot(stack_slot);
0N/A interval->assign_reg(stack_slot);
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::handle_doubleword_moves(LIR_Op* op) {
0N/A // special handling for doubleword move from memory to register:
0N/A // in this case the registers of the input address and the result
0N/A // registers must not overlap -> add a temp range for the input registers
0N/A if (op->code() == lir_move) {
0N/A assert(op->as_Op1() != NULL, "must be LIR_Op1");
0N/A LIR_Op1* move = (LIR_Op1*)op;
0N/A
0N/A if (move->result_opr()->is_double_cpu() && move->in_opr()->is_pointer()) {
0N/A LIR_Address* address = move->in_opr()->as_address_ptr();
0N/A if (address != NULL) {
0N/A if (address->base()->is_valid()) {
0N/A add_temp(address->base(), op->id(), noUse);
0N/A }
0N/A if (address->index()->is_valid()) {
0N/A add_temp(address->index(), op->id(), noUse);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::add_register_hints(LIR_Op* op) {
0N/A switch (op->code()) {
0N/A case lir_move: // fall through
0N/A case lir_convert: {
0N/A assert(op->as_Op1() != NULL, "lir_move, lir_convert must be LIR_Op1");
0N/A LIR_Op1* move = (LIR_Op1*)op;
0N/A
0N/A LIR_Opr move_from = move->in_opr();
0N/A LIR_Opr move_to = move->result_opr();
0N/A
0N/A if (move_to->is_register() && move_from->is_register()) {
0N/A Interval* from = interval_at(reg_num(move_from));
0N/A Interval* to = interval_at(reg_num(move_to));
0N/A if (from != NULL && to != NULL) {
0N/A to->set_register_hint(from);
0N/A TRACE_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()));
0N/A }
0N/A }
0N/A break;
0N/A }
0N/A case lir_cmove: {
0N/A assert(op->as_Op2() != NULL, "lir_cmove must be LIR_Op2");
0N/A LIR_Op2* cmove = (LIR_Op2*)op;
0N/A
0N/A LIR_Opr move_from = cmove->in_opr1();
0N/A LIR_Opr move_to = cmove->result_opr();
0N/A
0N/A if (move_to->is_register() && move_from->is_register()) {
0N/A Interval* from = interval_at(reg_num(move_from));
0N/A Interval* to = interval_at(reg_num(move_to));
0N/A if (from != NULL && to != NULL) {
0N/A to->set_register_hint(from);
0N/A TRACE_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()));
0N/A }
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::build_intervals() {
0N/A TIME_LINEAR_SCAN(timer_build_intervals);
0N/A
0N/A // initialize interval list with expected number of intervals
0N/A // (32 is added to have some space for split children without having to resize the list)
0N/A _intervals = IntervalList(num_virtual_regs() + 32);
0N/A // initialize all slots that are used by build_intervals
0N/A _intervals.at_put_grow(num_virtual_regs() - 1, NULL, NULL);
0N/A
0N/A // create a list with all caller-save registers (cpu, fpu, xmm)
0N/A // when an instruction is a call, a temp range is created for all these registers
0N/A int num_caller_save_registers = 0;
0N/A int caller_save_registers[LinearScan::nof_regs];
0N/A
0N/A int i;
1909N/A for (i = 0; i < FrameMap::nof_caller_save_cpu_regs(); i++) {
0N/A LIR_Opr opr = FrameMap::caller_save_cpu_reg_at(i);
0N/A assert(opr->is_valid() && opr->is_register(), "FrameMap should not return invalid operands");
0N/A assert(reg_numHi(opr) == -1, "missing addition of range for hi-register");
0N/A caller_save_registers[num_caller_save_registers++] = reg_num(opr);
0N/A }
0N/A
0N/A // temp ranges for fpu registers are only created when the method has
0N/A // virtual fpu operands. Otherwise no allocation for fpu registers is
0N/A // perfomed and so the temp ranges would be useless
0N/A if (has_fpu_registers()) {
304N/A#ifdef X86
0N/A if (UseSSE < 2) {
0N/A#endif
0N/A for (i = 0; i < FrameMap::nof_caller_save_fpu_regs; i++) {
0N/A LIR_Opr opr = FrameMap::caller_save_fpu_reg_at(i);
0N/A assert(opr->is_valid() && opr->is_register(), "FrameMap should not return invalid operands");
0N/A assert(reg_numHi(opr) == -1, "missing addition of range for hi-register");
0N/A caller_save_registers[num_caller_save_registers++] = reg_num(opr);
0N/A }
304N/A#ifdef X86
0N/A }
0N/A if (UseSSE > 0) {
0N/A for (i = 0; i < FrameMap::nof_caller_save_xmm_regs; i++) {
0N/A LIR_Opr opr = FrameMap::caller_save_xmm_reg_at(i);
0N/A assert(opr->is_valid() && opr->is_register(), "FrameMap should not return invalid operands");
0N/A assert(reg_numHi(opr) == -1, "missing addition of range for hi-register");
0N/A caller_save_registers[num_caller_save_registers++] = reg_num(opr);
0N/A }
0N/A }
0N/A#endif
0N/A }
0N/A assert(num_caller_save_registers <= LinearScan::nof_regs, "out of bounds");
0N/A
0N/A
0N/A LIR_OpVisitState visitor;
0N/A
0N/A // iterate all blocks in reverse order
0N/A for (i = block_count() - 1; i >= 0; i--) {
0N/A BlockBegin* block = block_at(i);
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A int block_from = block->first_lir_instruction_id();
0N/A int block_to = block->last_lir_instruction_id();
0N/A
0N/A assert(block_from == instructions->at(0)->id(), "must be");
0N/A assert(block_to == instructions->at(instructions->length() - 1)->id(), "must be");
0N/A
0N/A // Update intervals for registers live at the end of this block;
0N/A BitMap live = block->live_out();
304N/A int size = (int)live.size();
304N/A for (int number = (int)live.get_next_one_offset(0, size); number < size; number = (int)live.get_next_one_offset(number + 1, size)) {
0N/A assert(live.at(number), "should not stop here otherwise");
0N/A assert(number >= LIR_OprDesc::vreg_base, "fixed intervals must not be live on block bounds");
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("live in %d to %d", number, block_to + 2));
0N/A
0N/A add_use(number, block_from, block_to + 2, noUse, T_ILLEGAL);
0N/A
0N/A // add special use positions for loop-end blocks when the
0N/A // interval is used anywhere inside this loop. It's possible
0N/A // that the block was part of a non-natural loop, so it might
0N/A // have an invalid loop index.
0N/A if (block->is_set(BlockBegin::linear_scan_loop_end_flag) &&
0N/A block->loop_index() != -1 &&
0N/A is_interval_in_loop(number, block->loop_index())) {
0N/A interval_at(number)->add_use_pos(block_to + 1, loopEndMarker);
0N/A }
0N/A }
0N/A
0N/A // iterate all instructions of the block in reverse order.
0N/A // skip the first instruction because it is always a label
0N/A // definitions of intervals are processed before uses
0N/A assert(visitor.no_operands(instructions->at(0)), "first operation must always be a label");
0N/A for (int j = instructions->length() - 1; j >= 1; j--) {
0N/A LIR_Op* op = instructions->at(j);
0N/A int op_id = op->id();
0N/A
0N/A // visit operation to collect all operands
0N/A visitor.visit(op);
0N/A
0N/A // add a temp range for each register if operation destroys caller-save registers
0N/A if (visitor.has_call()) {
0N/A for (int k = 0; k < num_caller_save_registers; k++) {
0N/A add_temp(caller_save_registers[k], op_id, noUse, T_ILLEGAL);
0N/A }
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("operation destroys all caller-save registers"));
0N/A }
0N/A
0N/A // Add any platform dependent temps
0N/A pd_add_temps(op);
0N/A
0N/A // visit definitions (output and temp operands)
0N/A int k, n;
0N/A n = visitor.opr_count(LIR_OpVisitState::outputMode);
0N/A for (k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::outputMode, k);
0N/A assert(opr->is_register(), "visitor should only return register operands");
0N/A add_def(opr, op_id, use_kind_of_output_operand(op, opr));
0N/A }
0N/A
0N/A n = visitor.opr_count(LIR_OpVisitState::tempMode);
0N/A for (k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::tempMode, k);
0N/A assert(opr->is_register(), "visitor should only return register operands");
0N/A add_temp(opr, op_id, mustHaveRegister);
0N/A }
0N/A
0N/A // visit uses (input operands)
0N/A n = visitor.opr_count(LIR_OpVisitState::inputMode);
0N/A for (k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::inputMode, k);
0N/A assert(opr->is_register(), "visitor should only return register operands");
0N/A add_use(opr, block_from, op_id, use_kind_of_input_operand(op, opr));
0N/A }
0N/A
0N/A // Add uses of live locals from interpreter's point of view for proper
0N/A // debug information generation
0N/A // Treat these operands as temp values (if the life range is extended
0N/A // to a call site, the value would be in a register at the call otherwise)
0N/A n = visitor.info_count();
0N/A for (k = 0; k < n; k++) {
0N/A CodeEmitInfo* info = visitor.info_at(k);
0N/A ValueStack* stack = info->stack();
0N/A for_each_state_value(stack, value,
0N/A add_use(value, block_from, op_id + 1, noUse);
0N/A );
0N/A }
0N/A
0N/A // special steps for some instructions (especially moves)
0N/A handle_method_arguments(op);
0N/A handle_doubleword_moves(op);
0N/A add_register_hints(op);
0N/A
0N/A } // end of instruction iteration
0N/A } // end of block iteration
0N/A
0N/A
0N/A // add the range [0, 1[ to all fixed intervals
0N/A // -> the register allocator need not handle unhandled fixed intervals
0N/A for (int n = 0; n < LinearScan::nof_regs; n++) {
0N/A Interval* interval = interval_at(n);
0N/A if (interval != NULL) {
0N/A interval->add_range(0, 1);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// ********** Phase 5: actual register allocation
0N/A
0N/Aint LinearScan::interval_cmp(Interval** a, Interval** b) {
0N/A if (*a != NULL) {
0N/A if (*b != NULL) {
0N/A return (*a)->from() - (*b)->from();
0N/A } else {
0N/A return -1;
0N/A }
0N/A } else {
0N/A if (*b != NULL) {
0N/A return 1;
0N/A } else {
0N/A return 0;
0N/A }
0N/A }
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/Abool LinearScan::is_sorted(IntervalArray* intervals) {
0N/A int from = -1;
0N/A int i, j;
0N/A for (i = 0; i < intervals->length(); i ++) {
0N/A Interval* it = intervals->at(i);
0N/A if (it != NULL) {
0N/A if (from > it->from()) {
0N/A assert(false, "");
0N/A return false;
0N/A }
0N/A from = it->from();
0N/A }
0N/A }
0N/A
0N/A // check in both directions if sorted list and unsorted list contain same intervals
0N/A for (i = 0; i < interval_count(); i++) {
0N/A if (interval_at(i) != NULL) {
0N/A int num_found = 0;
0N/A for (j = 0; j < intervals->length(); j++) {
0N/A if (interval_at(i) == intervals->at(j)) {
0N/A num_found++;
0N/A }
0N/A }
0N/A assert(num_found == 1, "lists do not contain same intervals");
0N/A }
0N/A }
0N/A for (j = 0; j < intervals->length(); j++) {
0N/A int num_found = 0;
0N/A for (i = 0; i < interval_count(); i++) {
0N/A if (interval_at(i) == intervals->at(j)) {
0N/A num_found++;
0N/A }
0N/A }
0N/A assert(num_found == 1, "lists do not contain same intervals");
0N/A }
0N/A
0N/A return true;
0N/A}
0N/A#endif
0N/A
0N/Avoid LinearScan::add_to_list(Interval** first, Interval** prev, Interval* interval) {
0N/A if (*prev != NULL) {
0N/A (*prev)->set_next(interval);
0N/A } else {
0N/A *first = interval;
0N/A }
0N/A *prev = interval;
0N/A}
0N/A
0N/Avoid LinearScan::create_unhandled_lists(Interval** list1, Interval** list2, bool (is_list1)(const Interval* i), bool (is_list2)(const Interval* i)) {
0N/A assert(is_sorted(_sorted_intervals), "interval list is not sorted");
0N/A
0N/A *list1 = *list2 = Interval::end();
0N/A
0N/A Interval* list1_prev = NULL;
0N/A Interval* list2_prev = NULL;
0N/A Interval* v;
0N/A
0N/A const int n = _sorted_intervals->length();
0N/A for (int i = 0; i < n; i++) {
0N/A v = _sorted_intervals->at(i);
0N/A if (v == NULL) continue;
0N/A
0N/A if (is_list1(v)) {
0N/A add_to_list(list1, &list1_prev, v);
0N/A } else if (is_list2 == NULL || is_list2(v)) {
0N/A add_to_list(list2, &list2_prev, v);
0N/A }
0N/A }
0N/A
0N/A if (list1_prev != NULL) list1_prev->set_next(Interval::end());
0N/A if (list2_prev != NULL) list2_prev->set_next(Interval::end());
0N/A
0N/A assert(list1_prev == NULL || list1_prev->next() == Interval::end(), "linear list ends not with sentinel");
0N/A assert(list2_prev == NULL || list2_prev->next() == Interval::end(), "linear list ends not with sentinel");
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::sort_intervals_before_allocation() {
0N/A TIME_LINEAR_SCAN(timer_sort_intervals_before);
0N/A
1969N/A if (_needs_full_resort) {
1969N/A // There is no known reason why this should occur but just in case...
1969N/A assert(false, "should never occur");
1969N/A // Re-sort existing interval list because an Interval::from() has changed
1969N/A _sorted_intervals->sort(interval_cmp);
1969N/A _needs_full_resort = false;
1969N/A }
1969N/A
0N/A IntervalList* unsorted_list = &_intervals;
0N/A int unsorted_len = unsorted_list->length();
0N/A int sorted_len = 0;
0N/A int unsorted_idx;
0N/A int sorted_idx = 0;
0N/A int sorted_from_max = -1;
0N/A
0N/A // calc number of items for sorted list (sorted list must not contain NULL values)
0N/A for (unsorted_idx = 0; unsorted_idx < unsorted_len; unsorted_idx++) {
0N/A if (unsorted_list->at(unsorted_idx) != NULL) {
0N/A sorted_len++;
0N/A }
0N/A }
0N/A IntervalArray* sorted_list = new IntervalArray(sorted_len);
0N/A
0N/A // special sorting algorithm: the original interval-list is almost sorted,
0N/A // only some intervals are swapped. So this is much faster than a complete QuickSort
0N/A for (unsorted_idx = 0; unsorted_idx < unsorted_len; unsorted_idx++) {
0N/A Interval* cur_interval = unsorted_list->at(unsorted_idx);
0N/A
0N/A if (cur_interval != NULL) {
0N/A int cur_from = cur_interval->from();
0N/A
0N/A if (sorted_from_max <= cur_from) {
0N/A sorted_list->at_put(sorted_idx++, cur_interval);
0N/A sorted_from_max = cur_interval->from();
0N/A } else {
0N/A // the asumption that the intervals are already sorted failed,
0N/A // so this interval must be sorted in manually
0N/A int j;
0N/A for (j = sorted_idx - 1; j >= 0 && cur_from < sorted_list->at(j)->from(); j--) {
0N/A sorted_list->at_put(j + 1, sorted_list->at(j));
0N/A }
0N/A sorted_list->at_put(j + 1, cur_interval);
0N/A sorted_idx++;
0N/A }
0N/A }
0N/A }
0N/A _sorted_intervals = sorted_list;
1969N/A assert(is_sorted(_sorted_intervals), "intervals unsorted");
0N/A}
0N/A
0N/Avoid LinearScan::sort_intervals_after_allocation() {
0N/A TIME_LINEAR_SCAN(timer_sort_intervals_after);
0N/A
1969N/A if (_needs_full_resort) {
1969N/A // Re-sort existing interval list because an Interval::from() has changed
1969N/A _sorted_intervals->sort(interval_cmp);
1969N/A _needs_full_resort = false;
1969N/A }
1969N/A
0N/A IntervalArray* old_list = _sorted_intervals;
0N/A IntervalList* new_list = _new_intervals_from_allocation;
0N/A int old_len = old_list->length();
0N/A int new_len = new_list->length();
0N/A
0N/A if (new_len == 0) {
0N/A // no intervals have been added during allocation, so sorted list is already up to date
1969N/A assert(is_sorted(_sorted_intervals), "intervals unsorted");
0N/A return;
0N/A }
0N/A
0N/A // conventional sort-algorithm for new intervals
0N/A new_list->sort(interval_cmp);
0N/A
0N/A // merge old and new list (both already sorted) into one combined list
0N/A IntervalArray* combined_list = new IntervalArray(old_len + new_len);
0N/A int old_idx = 0;
0N/A int new_idx = 0;
0N/A
0N/A while (old_idx + new_idx < old_len + new_len) {
0N/A if (new_idx >= new_len || (old_idx < old_len && old_list->at(old_idx)->from() <= new_list->at(new_idx)->from())) {
0N/A combined_list->at_put(old_idx + new_idx, old_list->at(old_idx));
0N/A old_idx++;
0N/A } else {
0N/A combined_list->at_put(old_idx + new_idx, new_list->at(new_idx));
0N/A new_idx++;
0N/A }
0N/A }
0N/A
0N/A _sorted_intervals = combined_list;
1969N/A assert(is_sorted(_sorted_intervals), "intervals unsorted");
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::allocate_registers() {
0N/A TIME_LINEAR_SCAN(timer_allocate_registers);
0N/A
0N/A Interval* precolored_cpu_intervals, *not_precolored_cpu_intervals;
0N/A Interval* precolored_fpu_intervals, *not_precolored_fpu_intervals;
0N/A
0N/A create_unhandled_lists(&precolored_cpu_intervals, &not_precolored_cpu_intervals, is_precolored_cpu_interval, is_virtual_cpu_interval);
0N/A if (has_fpu_registers()) {
0N/A create_unhandled_lists(&precolored_fpu_intervals, &not_precolored_fpu_intervals, is_precolored_fpu_interval, is_virtual_fpu_interval);
0N/A#ifdef ASSERT
0N/A } else {
0N/A // fpu register allocation is omitted because no virtual fpu registers are present
0N/A // just check this again...
0N/A create_unhandled_lists(&precolored_fpu_intervals, &not_precolored_fpu_intervals, is_precolored_fpu_interval, is_virtual_fpu_interval);
0N/A assert(not_precolored_fpu_intervals == Interval::end(), "missed an uncolored fpu interval");
0N/A#endif
0N/A }
0N/A
0N/A // allocate cpu registers
0N/A LinearScanWalker cpu_lsw(this, precolored_cpu_intervals, not_precolored_cpu_intervals);
0N/A cpu_lsw.walk();
0N/A cpu_lsw.finish_allocation();
0N/A
0N/A if (has_fpu_registers()) {
0N/A // allocate fpu registers
0N/A LinearScanWalker fpu_lsw(this, precolored_fpu_intervals, not_precolored_fpu_intervals);
0N/A fpu_lsw.walk();
0N/A fpu_lsw.finish_allocation();
0N/A }
0N/A}
0N/A
0N/A
0N/A// ********** Phase 6: resolve data flow
0N/A// (insert moves at edges between blocks if intervals have been split)
0N/A
0N/A// wrapper for Interval::split_child_at_op_id that performs a bailout in product mode
0N/A// instead of returning NULL
0N/AInterval* LinearScan::split_child_at_op_id(Interval* interval, int op_id, LIR_OpVisitState::OprMode mode) {
0N/A Interval* result = interval->split_child_at_op_id(op_id, mode);
0N/A if (result != NULL) {
0N/A return result;
0N/A }
0N/A
0N/A assert(false, "must find an interval, but do a clean bailout in product mode");
0N/A result = new Interval(LIR_OprDesc::vreg_base);
0N/A result->assign_reg(0);
0N/A result->set_type(T_INT);
0N/A BAILOUT_("LinearScan: interval is NULL", result);
0N/A}
0N/A
0N/A
0N/AInterval* LinearScan::interval_at_block_begin(BlockBegin* block, int reg_num) {
0N/A assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");
0N/A assert(interval_at(reg_num) != NULL, "no interval found");
0N/A
0N/A return split_child_at_op_id(interval_at(reg_num), block->first_lir_instruction_id(), LIR_OpVisitState::outputMode);
0N/A}
0N/A
0N/AInterval* LinearScan::interval_at_block_end(BlockBegin* block, int reg_num) {
0N/A assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");
0N/A assert(interval_at(reg_num) != NULL, "no interval found");
0N/A
0N/A return split_child_at_op_id(interval_at(reg_num), block->last_lir_instruction_id() + 1, LIR_OpVisitState::outputMode);
0N/A}
0N/A
0N/AInterval* LinearScan::interval_at_op_id(int reg_num, int op_id) {
0N/A assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");
0N/A assert(interval_at(reg_num) != NULL, "no interval found");
0N/A
0N/A return split_child_at_op_id(interval_at(reg_num), op_id, LIR_OpVisitState::inputMode);
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::resolve_collect_mappings(BlockBegin* from_block, BlockBegin* to_block, MoveResolver &move_resolver) {
0N/A DEBUG_ONLY(move_resolver.check_empty());
0N/A
0N/A const int num_regs = num_virtual_regs();
0N/A const int size = live_set_size();
0N/A const BitMap live_at_edge = to_block->live_in();
0N/A
0N/A // visit all registers where the live_at_edge bit is set
304N/A for (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)) {
0N/A assert(r < num_regs, "live information set for not exisiting interval");
0N/A assert(from_block->live_out().at(r) && to_block->live_in().at(r), "interval not live at this edge");
0N/A
0N/A Interval* from_interval = interval_at_block_end(from_block, r);
0N/A Interval* to_interval = interval_at_block_begin(to_block, r);
0N/A
0N/A if (from_interval != to_interval && (from_interval->assigned_reg() != to_interval->assigned_reg() || from_interval->assigned_regHi() != to_interval->assigned_regHi())) {
0N/A // need to insert move instruction
0N/A move_resolver.add_mapping(from_interval, to_interval);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::resolve_find_insert_pos(BlockBegin* from_block, BlockBegin* to_block, MoveResolver &move_resolver) {
0N/A if (from_block->number_of_sux() <= 1) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("inserting moves at end of from_block B%d", from_block->block_id()));
0N/A
0N/A LIR_OpList* instructions = from_block->lir()->instructions_list();
0N/A LIR_OpBranch* branch = instructions->last()->as_OpBranch();
0N/A if (branch != NULL) {
0N/A // insert moves before branch
0N/A assert(branch->cond() == lir_cond_always, "block does not end with an unconditional jump");
0N/A move_resolver.set_insert_position(from_block->lir(), instructions->length() - 2);
0N/A } else {
0N/A move_resolver.set_insert_position(from_block->lir(), instructions->length() - 1);
0N/A }
0N/A
0N/A } else {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("inserting moves at beginning of to_block B%d", to_block->block_id()));
0N/A#ifdef ASSERT
0N/A assert(from_block->lir()->instructions_list()->at(0)->as_OpLabel() != NULL, "block does not start with a label");
0N/A
0N/A // because the number of predecessor edges matches the number of
0N/A // successor edges, blocks which are reached by switch statements
0N/A // may have be more than one predecessor but it will be guaranteed
0N/A // that all predecessors will be the same.
0N/A for (int i = 0; i < to_block->number_of_preds(); i++) {
0N/A assert(from_block == to_block->pred_at(i), "all critical edges must be broken");
0N/A }
0N/A#endif
0N/A
0N/A move_resolver.set_insert_position(to_block->lir(), 0);
0N/A }
0N/A}
0N/A
0N/A
0N/A// insert necessary moves (spilling or reloading) at edges between blocks if interval has been split
0N/Avoid LinearScan::resolve_data_flow() {
0N/A TIME_LINEAR_SCAN(timer_resolve_data_flow);
0N/A
0N/A int num_blocks = block_count();
0N/A MoveResolver move_resolver(this);
0N/A BitMap block_completed(num_blocks); block_completed.clear();
0N/A BitMap already_resolved(num_blocks); already_resolved.clear();
0N/A
0N/A int i;
0N/A for (i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A
0N/A // check if block has only one predecessor and only one successor
0N/A if (block->number_of_preds() == 1 && block->number_of_sux() == 1 && block->number_of_exception_handlers() == 0) {
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A assert(instructions->at(0)->code() == lir_label, "block must start with label");
0N/A assert(instructions->last()->code() == lir_branch, "block with successors must end with branch");
0N/A assert(instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block with successor must end with unconditional branch");
0N/A
0N/A // check if block is empty (only label and branch)
0N/A if (instructions->length() == 2) {
0N/A BlockBegin* pred = block->pred_at(0);
0N/A BlockBegin* sux = block->sux_at(0);
0N/A
0N/A // prevent optimization of two consecutive blocks
0N/A if (!block_completed.at(pred->linear_scan_number()) && !block_completed.at(sux->linear_scan_number())) {
0N/A TRACE_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()));
0N/A block_completed.set_bit(block->linear_scan_number());
0N/A
0N/A // directly resolve between pred and sux (without looking at the empty block between)
0N/A resolve_collect_mappings(pred, sux, move_resolver);
0N/A if (move_resolver.has_mappings()) {
0N/A move_resolver.set_insert_position(block->lir(), 0);
0N/A move_resolver.resolve_and_append_moves();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A for (i = 0; i < num_blocks; i++) {
0N/A if (!block_completed.at(i)) {
0N/A BlockBegin* from_block = block_at(i);
0N/A already_resolved.set_from(block_completed);
0N/A
0N/A int num_sux = from_block->number_of_sux();
0N/A for (int s = 0; s < num_sux; s++) {
0N/A BlockBegin* to_block = from_block->sux_at(s);
0N/A
0N/A // check for duplicate edges between the same blocks (can happen with switch blocks)
0N/A if (!already_resolved.at(to_block->linear_scan_number())) {
0N/A TRACE_LINEAR_SCAN(3, tty->print_cr("**** processing edge between B%d and B%d", from_block->block_id(), to_block->block_id()));
0N/A already_resolved.set_bit(to_block->linear_scan_number());
0N/A
0N/A // collect all intervals that have been split between from_block and to_block
0N/A resolve_collect_mappings(from_block, to_block, move_resolver);
0N/A if (move_resolver.has_mappings()) {
0N/A resolve_find_insert_pos(from_block, to_block, move_resolver);
0N/A move_resolver.resolve_and_append_moves();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::resolve_exception_entry(BlockBegin* block, int reg_num, MoveResolver &move_resolver) {
0N/A if (interval_at(reg_num) == NULL) {
0N/A // if a phi function is never used, no interval is created -> ignore this
0N/A return;
0N/A }
0N/A
0N/A Interval* interval = interval_at_block_begin(block, reg_num);
0N/A int reg = interval->assigned_reg();
0N/A int regHi = interval->assigned_regHi();
0N/A
0N/A if ((reg < nof_regs && interval->always_in_memory()) ||
0N/A (use_fpu_stack_allocation() && reg >= pd_first_fpu_reg && reg <= pd_last_fpu_reg)) {
0N/A // the interval is split to get a short range that is located on the stack
0N/A // in the following two cases:
0N/A // * the interval started in memory (e.g. method parameter), but is currently in a register
0N/A // this is an optimization for exception handling that reduces the number of moves that
0N/A // are necessary for resolving the states when an exception uses this exception handler
0N/A // * the interval would be on the fpu stack at the begin of the exception handler
0N/A // this is not allowed because of the complicated fpu stack handling on Intel
0N/A
0N/A // range that will be spilled to memory
0N/A int from_op_id = block->first_lir_instruction_id();
0N/A int to_op_id = from_op_id + 1; // short live range of length 1
0N/A assert(interval->from() <= from_op_id && interval->to() >= to_op_id,
0N/A "no split allowed between exception entry and first instruction");
0N/A
0N/A if (interval->from() != from_op_id) {
0N/A // the part before from_op_id is unchanged
0N/A interval = interval->split(from_op_id);
0N/A interval->assign_reg(reg, regHi);
0N/A append_interval(interval);
1969N/A } else {
1969N/A _needs_full_resort = true;
0N/A }
0N/A assert(interval->from() == from_op_id, "must be true now");
0N/A
0N/A Interval* spilled_part = interval;
0N/A if (interval->to() != to_op_id) {
0N/A // the part after to_op_id is unchanged
0N/A spilled_part = interval->split_from_start(to_op_id);
0N/A append_interval(spilled_part);
0N/A move_resolver.add_mapping(spilled_part, interval);
0N/A }
0N/A assign_spill_slot(spilled_part);
0N/A
0N/A assert(spilled_part->from() == from_op_id && spilled_part->to() == to_op_id, "just checking");
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::resolve_exception_entry(BlockBegin* block, MoveResolver &move_resolver) {
0N/A assert(block->is_set(BlockBegin::exception_entry_flag), "should not call otherwise");
0N/A DEBUG_ONLY(move_resolver.check_empty());
0N/A
0N/A // visit all registers where the live_in bit is set
0N/A int size = live_set_size();
304N/A for (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)) {
0N/A resolve_exception_entry(block, r, move_resolver);
0N/A }
0N/A
0N/A // the live_in bits are not set for phi functions of the xhandler entry, so iterate them separately
0N/A for_each_phi_fun(block, phi,
0N/A resolve_exception_entry(block, phi->operand()->vreg_number(), move_resolver)
0N/A );
0N/A
0N/A if (move_resolver.has_mappings()) {
0N/A // insert moves after first instruction
3577N/A move_resolver.set_insert_position(block->lir(), 0);
0N/A move_resolver.resolve_and_append_moves();
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::resolve_exception_edge(XHandler* handler, int throwing_op_id, int reg_num, Phi* phi, MoveResolver &move_resolver) {
0N/A if (interval_at(reg_num) == NULL) {
0N/A // if a phi function is never used, no interval is created -> ignore this
0N/A return;
0N/A }
0N/A
0N/A // the computation of to_interval is equal to resolve_collect_mappings,
0N/A // but from_interval is more complicated because of phi functions
0N/A BlockBegin* to_block = handler->entry_block();
0N/A Interval* to_interval = interval_at_block_begin(to_block, reg_num);
0N/A
0N/A if (phi != NULL) {
0N/A // phi function of the exception entry block
0N/A // no moves are created for this phi function in the LIR_Generator, so the
0N/A // interval at the throwing instruction must be searched using the operands
0N/A // of the phi function
0N/A Value from_value = phi->operand_at(handler->phi_operand());
0N/A
0N/A // with phi functions it can happen that the same from_value is used in
0N/A // multiple mappings, so notify move-resolver that this is allowed
0N/A move_resolver.set_multiple_reads_allowed();
0N/A
0N/A Constant* con = from_value->as_Constant();
0N/A if (con != NULL && !con->is_pinned()) {
0N/A // unpinned constants may have no register, so add mapping from constant to interval
0N/A move_resolver.add_mapping(LIR_OprFact::value_type(con->type()), to_interval);
0N/A } else {
0N/A // search split child at the throwing op_id
0N/A Interval* from_interval = interval_at_op_id(from_value->operand()->vreg_number(), throwing_op_id);
0N/A move_resolver.add_mapping(from_interval, to_interval);
0N/A }
0N/A
0N/A } else {
0N/A // no phi function, so use reg_num also for from_interval
0N/A // search split child at the throwing op_id
0N/A Interval* from_interval = interval_at_op_id(reg_num, throwing_op_id);
0N/A if (from_interval != to_interval) {
0N/A // optimization to reduce number of moves: when to_interval is on stack and
0N/A // the stack slot is known to be always correct, then no move is necessary
0N/A if (!from_interval->always_in_memory() || from_interval->canonical_spill_slot() != to_interval->assigned_reg()) {
0N/A move_resolver.add_mapping(from_interval, to_interval);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::resolve_exception_edge(XHandler* handler, int throwing_op_id, MoveResolver &move_resolver) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("resolving exception handler B%d: throwing_op_id=%d", handler->entry_block()->block_id(), throwing_op_id));
0N/A
0N/A DEBUG_ONLY(move_resolver.check_empty());
0N/A assert(handler->lir_op_id() == -1, "already processed this xhandler");
0N/A DEBUG_ONLY(handler->set_lir_op_id(throwing_op_id));
0N/A assert(handler->entry_code() == NULL, "code already present");
0N/A
0N/A // visit all registers where the live_in bit is set
0N/A BlockBegin* block = handler->entry_block();
0N/A int size = live_set_size();
304N/A for (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)) {
0N/A resolve_exception_edge(handler, throwing_op_id, r, NULL, move_resolver);
0N/A }
0N/A
0N/A // the live_in bits are not set for phi functions of the xhandler entry, so iterate them separately
0N/A for_each_phi_fun(block, phi,
0N/A resolve_exception_edge(handler, throwing_op_id, phi->operand()->vreg_number(), phi, move_resolver)
0N/A );
0N/A
0N/A if (move_resolver.has_mappings()) {
0N/A LIR_List* entry_code = new LIR_List(compilation());
0N/A move_resolver.set_insert_position(entry_code, 0);
0N/A move_resolver.resolve_and_append_moves();
0N/A
0N/A entry_code->jump(handler->entry_block());
0N/A handler->set_entry_code(entry_code);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::resolve_exception_handlers() {
0N/A MoveResolver move_resolver(this);
0N/A LIR_OpVisitState visitor;
0N/A int num_blocks = block_count();
0N/A
0N/A int i;
0N/A for (i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A if (block->is_set(BlockBegin::exception_entry_flag)) {
0N/A resolve_exception_entry(block, move_resolver);
0N/A }
0N/A }
0N/A
0N/A for (i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A LIR_List* ops = block->lir();
0N/A int num_ops = ops->length();
0N/A
0N/A // iterate all instructions of the block. skip the first because it is always a label
0N/A assert(visitor.no_operands(ops->at(0)), "first operation must always be a label");
0N/A for (int j = 1; j < num_ops; j++) {
0N/A LIR_Op* op = ops->at(j);
0N/A int op_id = op->id();
0N/A
0N/A if (op_id != -1 && has_info(op_id)) {
0N/A // visit operation to collect all operands
0N/A visitor.visit(op);
0N/A assert(visitor.info_count() > 0, "should not visit otherwise");
0N/A
0N/A XHandlers* xhandlers = visitor.all_xhandler();
0N/A int n = xhandlers->length();
0N/A for (int k = 0; k < n; k++) {
0N/A resolve_exception_edge(xhandlers->handler_at(k), op_id, move_resolver);
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A } else {
0N/A visitor.visit(op);
0N/A assert(visitor.all_xhandler()->length() == 0, "missed exception handler");
0N/A#endif
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// ********** Phase 7: assign register numbers back to LIR
0N/A// (includes computation of debug information and oop maps)
0N/A
0N/AVMReg LinearScan::vm_reg_for_interval(Interval* interval) {
0N/A VMReg reg = interval->cached_vm_reg();
0N/A if (!reg->is_valid() ) {
0N/A reg = vm_reg_for_operand(operand_for_interval(interval));
0N/A interval->set_cached_vm_reg(reg);
0N/A }
0N/A assert(reg == vm_reg_for_operand(operand_for_interval(interval)), "wrong cached value");
0N/A return reg;
0N/A}
0N/A
0N/AVMReg LinearScan::vm_reg_for_operand(LIR_Opr opr) {
0N/A assert(opr->is_oop(), "currently only implemented for oop operands");
0N/A return frame_map()->regname(opr);
0N/A}
0N/A
0N/A
0N/ALIR_Opr LinearScan::operand_for_interval(Interval* interval) {
0N/A LIR_Opr opr = interval->cached_opr();
0N/A if (opr->is_illegal()) {
0N/A opr = calc_operand_for_interval(interval);
0N/A interval->set_cached_opr(opr);
0N/A }
0N/A
0N/A assert(opr == calc_operand_for_interval(interval), "wrong cached value");
0N/A return opr;
0N/A}
0N/A
0N/ALIR_Opr LinearScan::calc_operand_for_interval(const Interval* interval) {
0N/A int assigned_reg = interval->assigned_reg();
0N/A BasicType type = interval->type();
0N/A
0N/A if (assigned_reg >= nof_regs) {
0N/A // stack slot
0N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register");
0N/A return LIR_OprFact::stack(assigned_reg - nof_regs, type);
0N/A
0N/A } else {
0N/A // register
0N/A switch (type) {
0N/A case T_OBJECT: {
0N/A assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");
0N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register");
0N/A return LIR_OprFact::single_cpu_oop(assigned_reg);
0N/A }
0N/A
1736N/A case T_ADDRESS: {
1736N/A assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");
1736N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register");
1736N/A return LIR_OprFact::single_cpu_address(assigned_reg);
1736N/A }
1736N/A
1601N/A#ifdef __SOFTFP__
1601N/A case T_FLOAT: // fall through
1601N/A#endif // __SOFTFP__
0N/A case T_INT: {
0N/A assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");
0N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register");
0N/A return LIR_OprFact::single_cpu(assigned_reg);
0N/A }
0N/A
1601N/A#ifdef __SOFTFP__
1601N/A case T_DOUBLE: // fall through
1601N/A#endif // __SOFTFP__
0N/A case T_LONG: {
0N/A int assigned_regHi = interval->assigned_regHi();
0N/A assert(assigned_reg >= pd_first_cpu_reg && assigned_reg <= pd_last_cpu_reg, "no cpu register");
0N/A assert(num_physical_regs(T_LONG) == 1 ||
0N/A (assigned_regHi >= pd_first_cpu_reg && assigned_regHi <= pd_last_cpu_reg), "no cpu register");
0N/A
0N/A assert(assigned_reg != assigned_regHi, "invalid allocation");
0N/A assert(num_physical_regs(T_LONG) == 1 || assigned_reg < assigned_regHi,
0N/A "register numbers must be sorted (ensure that e.g. a move from eax,ebx to ebx,eax can not occur)");
0N/A assert((assigned_regHi != any_reg) ^ (num_physical_regs(T_LONG) == 1), "must be match");
0N/A if (requires_adjacent_regs(T_LONG)) {
0N/A assert(assigned_reg % 2 == 0 && assigned_reg + 1 == assigned_regHi, "must be sequential and even");
0N/A }
0N/A
0N/A#ifdef _LP64
0N/A return LIR_OprFact::double_cpu(assigned_reg, assigned_reg);
0N/A#else
1601N/A#if defined(SPARC) || defined(PPC)
0N/A return LIR_OprFact::double_cpu(assigned_regHi, assigned_reg);
0N/A#else
0N/A return LIR_OprFact::double_cpu(assigned_reg, assigned_regHi);
304N/A#endif // SPARC
304N/A#endif // LP64
0N/A }
0N/A
1601N/A#ifndef __SOFTFP__
0N/A case T_FLOAT: {
304N/A#ifdef X86
0N/A if (UseSSE >= 1) {
0N/A assert(assigned_reg >= pd_first_xmm_reg && assigned_reg <= pd_last_xmm_reg, "no xmm register");
0N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register");
0N/A return LIR_OprFact::single_xmm(assigned_reg - pd_first_xmm_reg);
0N/A }
0N/A#endif
0N/A
0N/A assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");
0N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register");
0N/A return LIR_OprFact::single_fpu(assigned_reg - pd_first_fpu_reg);
0N/A }
0N/A
0N/A case T_DOUBLE: {
304N/A#ifdef X86
0N/A if (UseSSE >= 2) {
0N/A assert(assigned_reg >= pd_first_xmm_reg && assigned_reg <= pd_last_xmm_reg, "no xmm register");
0N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register (double xmm values are stored in one register)");
0N/A return LIR_OprFact::double_xmm(assigned_reg - pd_first_xmm_reg);
0N/A }
0N/A#endif
0N/A
0N/A#ifdef SPARC
0N/A assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");
0N/A assert(interval->assigned_regHi() >= pd_first_fpu_reg && interval->assigned_regHi() <= pd_last_fpu_reg, "no fpu register");
0N/A assert(assigned_reg % 2 == 0 && assigned_reg + 1 == interval->assigned_regHi(), "must be sequential and even");
0N/A LIR_Opr result = LIR_OprFact::double_fpu(interval->assigned_regHi() - pd_first_fpu_reg, assigned_reg - pd_first_fpu_reg);
1601N/A#elif defined(ARM)
1601N/A assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");
1601N/A assert(interval->assigned_regHi() >= pd_first_fpu_reg && interval->assigned_regHi() <= pd_last_fpu_reg, "no fpu register");
1601N/A assert(assigned_reg % 2 == 0 && assigned_reg + 1 == interval->assigned_regHi(), "must be sequential and even");
1601N/A LIR_Opr result = LIR_OprFact::double_fpu(assigned_reg - pd_first_fpu_reg, interval->assigned_regHi() - pd_first_fpu_reg);
0N/A#else
0N/A assert(assigned_reg >= pd_first_fpu_reg && assigned_reg <= pd_last_fpu_reg, "no fpu register");
0N/A assert(interval->assigned_regHi() == any_reg, "must not have hi register (double fpu values are stored in one register on Intel)");
0N/A LIR_Opr result = LIR_OprFact::double_fpu(assigned_reg - pd_first_fpu_reg);
0N/A#endif
0N/A return result;
0N/A }
1601N/A#endif // __SOFTFP__
0N/A
0N/A default: {
0N/A ShouldNotReachHere();
0N/A return LIR_OprFact::illegalOpr;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/ALIR_Opr LinearScan::canonical_spill_opr(Interval* interval) {
0N/A assert(interval->canonical_spill_slot() >= nof_regs, "canonical spill slot not set");
0N/A return LIR_OprFact::stack(interval->canonical_spill_slot() - nof_regs, interval->type());
0N/A}
0N/A
0N/ALIR_Opr LinearScan::color_lir_opr(LIR_Opr opr, int op_id, LIR_OpVisitState::OprMode mode) {
0N/A assert(opr->is_virtual(), "should not call this otherwise");
0N/A
0N/A Interval* interval = interval_at(opr->vreg_number());
0N/A assert(interval != NULL, "interval must exist");
0N/A
0N/A if (op_id != -1) {
0N/A#ifdef ASSERT
0N/A BlockBegin* block = block_of_op_with_id(op_id);
0N/A if (block->number_of_sux() <= 1 && op_id == block->last_lir_instruction_id()) {
0N/A // check if spill moves could have been appended at the end of this block, but
0N/A // before the branch instruction. So the split child information for this branch would
0N/A // be incorrect.
0N/A LIR_OpBranch* branch = block->lir()->instructions_list()->last()->as_OpBranch();
0N/A if (branch != NULL) {
0N/A if (block->live_out().at(opr->vreg_number())) {
0N/A assert(branch->cond() == lir_cond_always, "block does not end with an unconditional jump");
0N/A assert(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)");
0N/A }
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A // operands are not changed when an interval is split during allocation,
0N/A // so search the right interval here
0N/A interval = split_child_at_op_id(interval, op_id, mode);
0N/A }
0N/A
0N/A LIR_Opr res = operand_for_interval(interval);
0N/A
304N/A#ifdef X86
0N/A // new semantic for is_last_use: not only set on definite end of interval,
0N/A // but also before hole
0N/A // This may still miss some cases (e.g. for dead values), but it is not necessary that the
0N/A // last use information is completely correct
0N/A // information is only needed for fpu stack allocation
0N/A if (res->is_fpu_register()) {
0N/A if (opr->is_last_use() || op_id == interval->to() || (op_id != -1 && interval->has_hole_between(op_id, op_id + 1))) {
0N/A assert(op_id == -1 || !is_block_begin(op_id), "holes at begin of block may also result from control flow");
0N/A res = res->make_last_use();
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A assert(!gen()->is_vreg_flag_set(opr->vreg_number(), LIRGenerator::callee_saved) || !FrameMap::is_caller_save_register(res), "bad allocation");
0N/A
0N/A return res;
0N/A}
0N/A
0N/A
0N/A#ifdef ASSERT
0N/A// some methods used to check correctness of debug information
0N/A
0N/Avoid assert_no_register_values(GrowableArray<ScopeValue*>* values) {
0N/A if (values == NULL) {
0N/A return;
0N/A }
0N/A
0N/A for (int i = 0; i < values->length(); i++) {
0N/A ScopeValue* value = values->at(i);
0N/A
0N/A if (value->is_location()) {
0N/A Location location = ((LocationValue*)value)->location();
0N/A assert(location.where() == Location::on_stack, "value is in register");
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid assert_no_register_values(GrowableArray<MonitorValue*>* values) {
0N/A if (values == NULL) {
0N/A return;
0N/A }
0N/A
0N/A for (int i = 0; i < values->length(); i++) {
0N/A MonitorValue* value = values->at(i);
0N/A
0N/A if (value->owner()->is_location()) {
0N/A Location location = ((LocationValue*)value->owner())->location();
0N/A assert(location.where() == Location::on_stack, "owner is in register");
0N/A }
0N/A assert(value->basic_lock().where() == Location::on_stack, "basic_lock is in register");
0N/A }
0N/A}
0N/A
0N/Avoid assert_equal(Location l1, Location l2) {
0N/A assert(l1.where() == l2.where() && l1.type() == l2.type() && l1.offset() == l2.offset(), "");
0N/A}
0N/A
0N/Avoid assert_equal(ScopeValue* v1, ScopeValue* v2) {
0N/A if (v1->is_location()) {
0N/A assert(v2->is_location(), "");
0N/A assert_equal(((LocationValue*)v1)->location(), ((LocationValue*)v2)->location());
0N/A } else if (v1->is_constant_int()) {
0N/A assert(v2->is_constant_int(), "");
0N/A assert(((ConstantIntValue*)v1)->value() == ((ConstantIntValue*)v2)->value(), "");
0N/A } else if (v1->is_constant_double()) {
0N/A assert(v2->is_constant_double(), "");
0N/A assert(((ConstantDoubleValue*)v1)->value() == ((ConstantDoubleValue*)v2)->value(), "");
0N/A } else if (v1->is_constant_long()) {
0N/A assert(v2->is_constant_long(), "");
0N/A assert(((ConstantLongValue*)v1)->value() == ((ConstantLongValue*)v2)->value(), "");
0N/A } else if (v1->is_constant_oop()) {
0N/A assert(v2->is_constant_oop(), "");
0N/A assert(((ConstantOopWriteValue*)v1)->value() == ((ConstantOopWriteValue*)v2)->value(), "");
0N/A } else {
0N/A ShouldNotReachHere();
0N/A }
0N/A}
0N/A
0N/Avoid assert_equal(MonitorValue* m1, MonitorValue* m2) {
0N/A assert_equal(m1->owner(), m2->owner());
0N/A assert_equal(m1->basic_lock(), m2->basic_lock());
0N/A}
0N/A
0N/Avoid assert_equal(IRScopeDebugInfo* d1, IRScopeDebugInfo* d2) {
0N/A assert(d1->scope() == d2->scope(), "not equal");
0N/A assert(d1->bci() == d2->bci(), "not equal");
0N/A
0N/A if (d1->locals() != NULL) {
0N/A assert(d1->locals() != NULL && d2->locals() != NULL, "not equal");
0N/A assert(d1->locals()->length() == d2->locals()->length(), "not equal");
0N/A for (int i = 0; i < d1->locals()->length(); i++) {
0N/A assert_equal(d1->locals()->at(i), d2->locals()->at(i));
0N/A }
0N/A } else {
0N/A assert(d1->locals() == NULL && d2->locals() == NULL, "not equal");
0N/A }
0N/A
0N/A if (d1->expressions() != NULL) {
0N/A assert(d1->expressions() != NULL && d2->expressions() != NULL, "not equal");
0N/A assert(d1->expressions()->length() == d2->expressions()->length(), "not equal");
0N/A for (int i = 0; i < d1->expressions()->length(); i++) {
0N/A assert_equal(d1->expressions()->at(i), d2->expressions()->at(i));
0N/A }
0N/A } else {
0N/A assert(d1->expressions() == NULL && d2->expressions() == NULL, "not equal");
0N/A }
0N/A
0N/A if (d1->monitors() != NULL) {
0N/A assert(d1->monitors() != NULL && d2->monitors() != NULL, "not equal");
0N/A assert(d1->monitors()->length() == d2->monitors()->length(), "not equal");
0N/A for (int i = 0; i < d1->monitors()->length(); i++) {
0N/A assert_equal(d1->monitors()->at(i), d2->monitors()->at(i));
0N/A }
0N/A } else {
0N/A assert(d1->monitors() == NULL && d2->monitors() == NULL, "not equal");
0N/A }
0N/A
0N/A if (d1->caller() != NULL) {
0N/A assert(d1->caller() != NULL && d2->caller() != NULL, "not equal");
0N/A assert_equal(d1->caller(), d2->caller());
0N/A } else {
0N/A assert(d1->caller() == NULL && d2->caller() == NULL, "not equal");
0N/A }
0N/A}
0N/A
0N/Avoid check_stack_depth(CodeEmitInfo* info, int stack_end) {
1739N/A if (info->stack()->bci() != SynchronizationEntryBCI && !info->scope()->method()->is_native()) {
1739N/A Bytecodes::Code code = info->scope()->method()->java_code_at_bci(info->stack()->bci());
0N/A switch (code) {
0N/A case Bytecodes::_ifnull : // fall through
0N/A case Bytecodes::_ifnonnull : // fall through
0N/A case Bytecodes::_ifeq : // fall through
0N/A case Bytecodes::_ifne : // fall through
0N/A case Bytecodes::_iflt : // fall through
0N/A case Bytecodes::_ifge : // fall through
0N/A case Bytecodes::_ifgt : // fall through
0N/A case Bytecodes::_ifle : // fall through
0N/A case Bytecodes::_if_icmpeq : // fall through
0N/A case Bytecodes::_if_icmpne : // fall through
0N/A case Bytecodes::_if_icmplt : // fall through
0N/A case Bytecodes::_if_icmpge : // fall through
0N/A case Bytecodes::_if_icmpgt : // fall through
0N/A case Bytecodes::_if_icmple : // fall through
0N/A case Bytecodes::_if_acmpeq : // fall through
0N/A case Bytecodes::_if_acmpne :
0N/A assert(stack_end >= -Bytecodes::depth(code), "must have non-empty expression stack at if bytecode");
0N/A break;
0N/A }
0N/A }
0N/A}
0N/A
0N/A#endif // ASSERT
0N/A
0N/A
0N/AIntervalWalker* LinearScan::init_compute_oop_maps() {
0N/A // setup lists of potential oops for walking
0N/A Interval* oop_intervals;
0N/A Interval* non_oop_intervals;
0N/A
0N/A create_unhandled_lists(&oop_intervals, &non_oop_intervals, is_oop_interval, NULL);
0N/A
0N/A // intervals that have no oops inside need not to be processed
0N/A // to ensure a walking until the last instruction id, add a dummy interval
0N/A // with a high operation id
0N/A non_oop_intervals = new Interval(any_reg);
0N/A non_oop_intervals->add_range(max_jint - 2, max_jint - 1);
0N/A
0N/A return new IntervalWalker(this, oop_intervals, non_oop_intervals);
0N/A}
0N/A
0N/A
0N/AOopMap* LinearScan::compute_oop_map(IntervalWalker* iw, LIR_Op* op, CodeEmitInfo* info, bool is_call_site) {
0N/A TRACE_LINEAR_SCAN(3, tty->print_cr("creating oop map at op_id %d", op->id()));
0N/A
0N/A // walk before the current operation -> intervals that start at
0N/A // the operation (= output operands of the operation) are not
0N/A // included in the oop map
0N/A iw->walk_before(op->id());
0N/A
0N/A int frame_size = frame_map()->framesize();
0N/A int arg_count = frame_map()->oop_map_arg_count();
0N/A OopMap* map = new OopMap(frame_size, arg_count);
0N/A
0N/A // Check if this is a patch site.
0N/A bool is_patch_info = false;
0N/A if (op->code() == lir_move) {
0N/A assert(!is_call_site, "move must not be a call site");
0N/A assert(op->as_Op1() != NULL, "move must be LIR_Op1");
0N/A LIR_Op1* move = (LIR_Op1*)op;
0N/A
0N/A is_patch_info = move->patch_code() != lir_patch_none;
0N/A }
0N/A
0N/A // Iterate through active intervals
0N/A for (Interval* interval = iw->active_first(fixedKind); interval != Interval::end(); interval = interval->next()) {
0N/A int assigned_reg = interval->assigned_reg();
0N/A
0N/A assert(interval->current_from() <= op->id() && op->id() <= interval->current_to(), "interval should not be active otherwise");
0N/A assert(interval->assigned_regHi() == any_reg, "oop must be single word");
0N/A assert(interval->reg_num() >= LIR_OprDesc::vreg_base, "fixed interval found");
0N/A
0N/A // Check if this range covers the instruction. Intervals that
0N/A // start or end at the current operation are not included in the
0N/A // oop map, except in the case of patching moves. For patching
0N/A // moves, any intervals which end at this instruction are included
0N/A // in the oop map since we may safepoint while doing the patch
0N/A // before we've consumed the inputs.
0N/A if (is_patch_info || op->id() < interval->current_to()) {
0N/A
0N/A // caller-save registers must not be included into oop-maps at calls
0N/A assert(!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");
0N/A
0N/A VMReg name = vm_reg_for_interval(interval);
2742N/A set_oop(map, name);
0N/A
0N/A // Spill optimization: when the stack value is guaranteed to be always correct,
0N/A // then it must be added to the oop map even if the interval is currently in a register
0N/A if (interval->always_in_memory() &&
0N/A op->id() > interval->spill_definition_pos() &&
0N/A interval->assigned_reg() != interval->canonical_spill_slot()) {
0N/A assert(interval->spill_definition_pos() > 0, "position not set correctly");
0N/A assert(interval->canonical_spill_slot() >= LinearScan::nof_regs, "no spill slot assigned");
0N/A assert(interval->assigned_reg() < LinearScan::nof_regs, "interval is on stack, so stack slot is registered twice");
0N/A
2742N/A set_oop(map, frame_map()->slot_regname(interval->canonical_spill_slot() - LinearScan::nof_regs));
0N/A }
0N/A }
0N/A }
0N/A
0N/A // add oops from lock stack
0N/A assert(info->stack() != NULL, "CodeEmitInfo must always have a stack");
1739N/A int locks_count = info->stack()->total_locks_size();
0N/A for (int i = 0; i < locks_count; i++) {
2742N/A set_oop(map, frame_map()->monitor_object_regname(i));
0N/A }
0N/A
0N/A return map;
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::compute_oop_map(IntervalWalker* iw, const LIR_OpVisitState &visitor, LIR_Op* op) {
0N/A assert(visitor.info_count() > 0, "no oop map needed");
0N/A
0N/A // compute oop_map only for first CodeEmitInfo
0N/A // because it is (in most cases) equal for all other infos of the same operation
0N/A CodeEmitInfo* first_info = visitor.info_at(0);
0N/A OopMap* first_oop_map = compute_oop_map(iw, op, first_info, visitor.has_call());
0N/A
0N/A for (int i = 0; i < visitor.info_count(); i++) {
0N/A CodeEmitInfo* info = visitor.info_at(i);
0N/A OopMap* oop_map = first_oop_map;
0N/A
0N/A if (info->stack()->locks_size() != first_info->stack()->locks_size()) {
0N/A // this info has a different number of locks then the precomputed oop map
0N/A // (possible for lock and unlock instructions) -> compute oop map with
0N/A // correct lock information
0N/A oop_map = compute_oop_map(iw, op, info, visitor.has_call());
0N/A }
0N/A
0N/A if (info->_oop_map == NULL) {
0N/A info->_oop_map = oop_map;
0N/A } else {
0N/A // a CodeEmitInfo can not be shared between different LIR-instructions
0N/A // because interval splitting can occur anywhere between two instructions
0N/A // and so the oop maps must be different
0N/A // -> check if the already set oop_map is exactly the one calculated for this operation
0N/A assert(info->_oop_map == oop_map, "same CodeEmitInfo used for multiple LIR instructions");
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// frequently used constants
3237N/A// Allocate them with new so they are never destroyed (otherwise, a
3237N/A// forced exit could destroy these objects while they are still in
3237N/A// use).
3863N/AConstantOopWriteValue* LinearScan::_oop_null_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantOopWriteValue(NULL);
3863N/AConstantIntValue* LinearScan::_int_m1_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(-1);
3863N/AConstantIntValue* LinearScan::_int_0_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(0);
3863N/AConstantIntValue* LinearScan::_int_1_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(1);
3863N/AConstantIntValue* LinearScan::_int_2_scope_value = new (ResourceObj::C_HEAP, mtCompiler) ConstantIntValue(2);
3863N/ALocationValue* _illegal_value = new (ResourceObj::C_HEAP, mtCompiler) LocationValue(Location());
0N/A
0N/Avoid LinearScan::init_compute_debug_info() {
0N/A // cache for frequently used scope values
0N/A // (cpu registers and stack slots)
0N/A _scope_value_cache = ScopeValueArray((LinearScan::nof_cpu_regs + frame_map()->argcount() + max_spills()) * 2, NULL);
0N/A}
0N/A
0N/AMonitorValue* LinearScan::location_for_monitor_index(int monitor_index) {
0N/A Location loc;
0N/A if (!frame_map()->location_for_monitor_object(monitor_index, &loc)) {
0N/A bailout("too large frame");
0N/A }
0N/A ScopeValue* object_scope_value = new LocationValue(loc);
0N/A
0N/A if (!frame_map()->location_for_monitor_lock(monitor_index, &loc)) {
0N/A bailout("too large frame");
0N/A }
0N/A return new MonitorValue(object_scope_value, loc);
0N/A}
0N/A
0N/ALocationValue* LinearScan::location_for_name(int name, Location::Type loc_type) {
0N/A Location loc;
0N/A if (!frame_map()->locations_for_slot(name, loc_type, &loc)) {
0N/A bailout("too large frame");
0N/A }
0N/A return new LocationValue(loc);
0N/A}
0N/A
0N/A
0N/Aint LinearScan::append_scope_value_for_constant(LIR_Opr opr, GrowableArray<ScopeValue*>* scope_values) {
0N/A assert(opr->is_constant(), "should not be called otherwise");
0N/A
0N/A LIR_Const* c = opr->as_constant_ptr();
0N/A BasicType t = c->type();
0N/A switch (t) {
0N/A case T_OBJECT: {
0N/A jobject value = c->as_jobject();
0N/A if (value == NULL) {
3237N/A scope_values->append(_oop_null_scope_value);
0N/A } else {
0N/A scope_values->append(new ConstantOopWriteValue(c->as_jobject()));
0N/A }
0N/A return 1;
0N/A }
0N/A
0N/A case T_INT: // fall through
0N/A case T_FLOAT: {
0N/A int value = c->as_jint_bits();
0N/A switch (value) {
3237N/A case -1: scope_values->append(_int_m1_scope_value); break;
3237N/A case 0: scope_values->append(_int_0_scope_value); break;
3237N/A case 1: scope_values->append(_int_1_scope_value); break;
3237N/A case 2: scope_values->append(_int_2_scope_value); break;
0N/A default: scope_values->append(new ConstantIntValue(c->as_jint_bits())); break;
0N/A }
0N/A return 1;
0N/A }
0N/A
0N/A case T_LONG: // fall through
0N/A case T_DOUBLE: {
1060N/A#ifdef _LP64
3237N/A scope_values->append(_int_0_scope_value);
1060N/A scope_values->append(new ConstantLongValue(c->as_jlong_bits()));
1060N/A#else
0N/A if (hi_word_offset_in_bytes > lo_word_offset_in_bytes) {
0N/A scope_values->append(new ConstantIntValue(c->as_jint_hi_bits()));
0N/A scope_values->append(new ConstantIntValue(c->as_jint_lo_bits()));
0N/A } else {
0N/A scope_values->append(new ConstantIntValue(c->as_jint_lo_bits()));
0N/A scope_values->append(new ConstantIntValue(c->as_jint_hi_bits()));
0N/A }
1060N/A#endif
0N/A return 2;
0N/A }
0N/A
1297N/A case T_ADDRESS: {
1297N/A#ifdef _LP64
1297N/A scope_values->append(new ConstantLongValue(c->as_jint()));
1297N/A#else
1297N/A scope_values->append(new ConstantIntValue(c->as_jint()));
1297N/A#endif
1297N/A return 1;
1297N/A }
1297N/A
0N/A default:
0N/A ShouldNotReachHere();
304N/A return -1;
0N/A }
0N/A}
0N/A
0N/Aint LinearScan::append_scope_value_for_operand(LIR_Opr opr, GrowableArray<ScopeValue*>* scope_values) {
0N/A if (opr->is_single_stack()) {
0N/A int stack_idx = opr->single_stack_ix();
0N/A bool is_oop = opr->is_oop_register();
0N/A int cache_idx = (stack_idx + LinearScan::nof_cpu_regs) * 2 + (is_oop ? 1 : 0);
0N/A
0N/A ScopeValue* sv = _scope_value_cache.at(cache_idx);
0N/A if (sv == NULL) {
0N/A Location::Type loc_type = is_oop ? Location::oop : Location::normal;
0N/A sv = location_for_name(stack_idx, loc_type);
0N/A _scope_value_cache.at_put(cache_idx, sv);
0N/A }
0N/A
0N/A // check if cached value is correct
0N/A DEBUG_ONLY(assert_equal(sv, location_for_name(stack_idx, is_oop ? Location::oop : Location::normal)));
0N/A
0N/A scope_values->append(sv);
0N/A return 1;
0N/A
0N/A } else if (opr->is_single_cpu()) {
0N/A bool is_oop = opr->is_oop_register();
0N/A int cache_idx = opr->cpu_regnr() * 2 + (is_oop ? 1 : 0);
1060N/A Location::Type int_loc_type = NOT_LP64(Location::normal) LP64_ONLY(Location::int_in_long);
0N/A
0N/A ScopeValue* sv = _scope_value_cache.at(cache_idx);
0N/A if (sv == NULL) {
1060N/A Location::Type loc_type = is_oop ? Location::oop : int_loc_type;
0N/A VMReg rname = frame_map()->regname(opr);
0N/A sv = new LocationValue(Location::new_reg_loc(loc_type, rname));
0N/A _scope_value_cache.at_put(cache_idx, sv);
0N/A }
0N/A
0N/A // check if cached value is correct
1060N/A DEBUG_ONLY(assert_equal(sv, new LocationValue(Location::new_reg_loc(is_oop ? Location::oop : int_loc_type, frame_map()->regname(opr)))));
0N/A
0N/A scope_values->append(sv);
0N/A return 1;
0N/A
304N/A#ifdef X86
0N/A } else if (opr->is_single_xmm()) {
0N/A VMReg rname = opr->as_xmm_float_reg()->as_VMReg();
0N/A LocationValue* sv = new LocationValue(Location::new_reg_loc(Location::normal, rname));
0N/A
0N/A scope_values->append(sv);
0N/A return 1;
0N/A#endif
0N/A
0N/A } else if (opr->is_single_fpu()) {
304N/A#ifdef X86
0N/A // the exact location of fpu stack values is only known
0N/A // during fpu stack allocation, so the stack allocator object
0N/A // must be present
0N/A assert(use_fpu_stack_allocation(), "should not have float stack values without fpu stack allocation (all floats must be SSE2)");
0N/A assert(_fpu_stack_allocator != NULL, "must be present");
0N/A opr = _fpu_stack_allocator->to_fpu_stack(opr);
0N/A#endif
0N/A
0N/A Location::Type loc_type = float_saved_as_double ? Location::float_in_dbl : Location::normal;
0N/A VMReg rname = frame_map()->fpu_regname(opr->fpu_regnr());
2835N/A#ifndef __SOFTFP__
2835N/A#ifndef VM_LITTLE_ENDIAN
2835N/A if (! float_saved_as_double) {
2835N/A // On big endian system, we may have an issue if float registers use only
2835N/A // the low half of the (same) double registers.
2835N/A // Both the float and the double could have the same regnr but would correspond
2835N/A // to two different addresses once saved.
2835N/A
2835N/A // get next safely (no assertion checks)
2835N/A VMReg next = VMRegImpl::as_VMReg(1+rname->value());
2835N/A if (next->is_reg() &&
2835N/A (next->as_FloatRegister() == rname->as_FloatRegister())) {
2835N/A // the back-end does use the same numbering for the double and the float
2835N/A rname = next; // VMReg for the low bits, e.g. the real VMReg for the float
2835N/A }
2835N/A }
2835N/A#endif
2835N/A#endif
0N/A LocationValue* sv = new LocationValue(Location::new_reg_loc(loc_type, rname));
0N/A
0N/A scope_values->append(sv);
0N/A return 1;
0N/A
0N/A } else {
0N/A // double-size operands
0N/A
0N/A ScopeValue* first;
0N/A ScopeValue* second;
0N/A
0N/A if (opr->is_double_stack()) {
304N/A#ifdef _LP64
304N/A Location loc1;
304N/A Location::Type loc_type = opr->type() == T_LONG ? Location::lng : Location::dbl;
304N/A if (!frame_map()->locations_for_slot(opr->double_stack_ix(), loc_type, &loc1, NULL)) {
304N/A bailout("too large frame");
304N/A }
304N/A // Does this reverse on x86 vs. sparc?
304N/A first = new LocationValue(loc1);
3237N/A second = _int_0_scope_value;
304N/A#else
0N/A Location loc1, loc2;
0N/A if (!frame_map()->locations_for_slot(opr->double_stack_ix(), Location::normal, &loc1, &loc2)) {
0N/A bailout("too large frame");
0N/A }
0N/A first = new LocationValue(loc1);
0N/A second = new LocationValue(loc2);
304N/A#endif // _LP64
0N/A
0N/A } else if (opr->is_double_cpu()) {
0N/A#ifdef _LP64
0N/A VMReg rname_first = opr->as_register_lo()->as_VMReg();
0N/A first = new LocationValue(Location::new_reg_loc(Location::lng, rname_first));
3237N/A second = _int_0_scope_value;
0N/A#else
0N/A VMReg rname_first = opr->as_register_lo()->as_VMReg();
0N/A VMReg rname_second = opr->as_register_hi()->as_VMReg();
0N/A
0N/A if (hi_word_offset_in_bytes < lo_word_offset_in_bytes) {
0N/A // lo/hi and swapped relative to first and second, so swap them
0N/A VMReg tmp = rname_first;
0N/A rname_first = rname_second;
0N/A rname_second = tmp;
0N/A }
0N/A
0N/A first = new LocationValue(Location::new_reg_loc(Location::normal, rname_first));
0N/A second = new LocationValue(Location::new_reg_loc(Location::normal, rname_second));
304N/A#endif //_LP64
304N/A
304N/A
304N/A#ifdef X86
0N/A } else if (opr->is_double_xmm()) {
0N/A assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation");
0N/A VMReg rname_first = opr->as_xmm_double_reg()->as_VMReg();
1369N/A# ifdef _LP64
1369N/A first = new LocationValue(Location::new_reg_loc(Location::dbl, rname_first));
3237N/A second = _int_0_scope_value;
1369N/A# else
0N/A first = new LocationValue(Location::new_reg_loc(Location::normal, rname_first));
0N/A // %%% This is probably a waste but we'll keep things as they were for now
0N/A if (true) {
0N/A VMReg rname_second = rname_first->next();
0N/A second = new LocationValue(Location::new_reg_loc(Location::normal, rname_second));
0N/A }
1369N/A# endif
0N/A#endif
0N/A
0N/A } else if (opr->is_double_fpu()) {
0N/A // On SPARC, fpu_regnrLo/fpu_regnrHi represents the two halves of
304N/A // the double as float registers in the native ordering. On X86,
0N/A // fpu_regnrLo is a FPU stack slot whose VMReg represents
0N/A // the low-order word of the double and fpu_regnrLo + 1 is the
0N/A // name for the other half. *first and *second must represent the
0N/A // least and most significant words, respectively.
0N/A
304N/A#ifdef X86
0N/A // the exact location of fpu stack values is only known
0N/A // during fpu stack allocation, so the stack allocator object
0N/A // must be present
0N/A assert(use_fpu_stack_allocation(), "should not have float stack values without fpu stack allocation (all floats must be SSE2)");
0N/A assert(_fpu_stack_allocator != NULL, "must be present");
0N/A opr = _fpu_stack_allocator->to_fpu_stack(opr);
0N/A
2192N/A assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation (only fpu_regnrLo is used)");
0N/A#endif
0N/A#ifdef SPARC
0N/A assert(opr->fpu_regnrLo() == opr->fpu_regnrHi() + 1, "assumed in calculation (only fpu_regnrHi is used)");
0N/A#endif
1601N/A#ifdef ARM
1601N/A assert(opr->fpu_regnrHi() == opr->fpu_regnrLo() + 1, "assumed in calculation (only fpu_regnrLo is used)");
1601N/A#endif
1601N/A#ifdef PPC
1601N/A assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation (only fpu_regnrHi is used)");
1601N/A#endif
0N/A
2192N/A#ifdef VM_LITTLE_ENDIAN
2192N/A VMReg rname_first = frame_map()->fpu_regname(opr->fpu_regnrLo());
2192N/A#else
0N/A VMReg rname_first = frame_map()->fpu_regname(opr->fpu_regnrHi());
2192N/A#endif
2192N/A
1369N/A#ifdef _LP64
1369N/A first = new LocationValue(Location::new_reg_loc(Location::dbl, rname_first));
3237N/A second = _int_0_scope_value;
1369N/A#else
0N/A first = new LocationValue(Location::new_reg_loc(Location::normal, rname_first));
0N/A // %%% This is probably a waste but we'll keep things as they were for now
0N/A if (true) {
0N/A VMReg rname_second = rname_first->next();
0N/A second = new LocationValue(Location::new_reg_loc(Location::normal, rname_second));
0N/A }
1369N/A#endif
0N/A
0N/A } else {
0N/A ShouldNotReachHere();
0N/A first = NULL;
0N/A second = NULL;
0N/A }
0N/A
0N/A assert(first != NULL && second != NULL, "must be set");
0N/A // The convention the interpreter uses is that the second local
0N/A // holds the first raw word of the native double representation.
0N/A // This is actually reasonable, since locals and stack arrays
0N/A // grow downwards in all implementations.
0N/A // (If, on some machine, the interpreter's Java locals or stack
0N/A // were to grow upwards, the embedded doubles would be word-swapped.)
0N/A scope_values->append(second);
0N/A scope_values->append(first);
0N/A return 2;
0N/A }
0N/A}
0N/A
0N/A
0N/Aint LinearScan::append_scope_value(int op_id, Value value, GrowableArray<ScopeValue*>* scope_values) {
0N/A if (value != NULL) {
0N/A LIR_Opr opr = value->operand();
0N/A Constant* con = value->as_Constant();
0N/A
0N/A assert(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)");
0N/A assert(con != NULL || opr->is_virtual(), "asumption: non-Constant instructions have only virtual operands");
0N/A
0N/A if (con != NULL && !con->is_pinned() && !opr->is_constant()) {
0N/A // Unpinned constants may have a virtual operand for a part of the lifetime
0N/A // or may be illegal when it was optimized away,
0N/A // so always use a constant operand
0N/A opr = LIR_OprFact::value_type(con->type());
0N/A }
0N/A assert(opr->is_virtual() || opr->is_constant(), "other cases not allowed here");
0N/A
0N/A if (opr->is_virtual()) {
0N/A LIR_OpVisitState::OprMode mode = LIR_OpVisitState::inputMode;
0N/A
0N/A BlockBegin* block = block_of_op_with_id(op_id);
0N/A if (block->number_of_sux() == 1 && op_id == block->last_lir_instruction_id()) {
0N/A // generating debug information for the last instruction of a block.
0N/A // if this instruction is a branch, spill moves are inserted before this branch
0N/A // and so the wrong operand would be returned (spill moves at block boundaries are not
0N/A // considered in the live ranges of intervals)
0N/A // Solution: use the first op_id of the branch target block instead.
0N/A if (block->lir()->instructions_list()->last()->as_OpBranch() != NULL) {
0N/A if (block->live_out().at(opr->vreg_number())) {
0N/A op_id = block->sux_at(0)->first_lir_instruction_id();
0N/A mode = LIR_OpVisitState::outputMode;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Get current location of operand
0N/A // The operand must be live because debug information is considered when building the intervals
0N/A // if the interval is not live, color_lir_opr will cause an assertion failure
0N/A opr = color_lir_opr(opr, op_id, mode);
0N/A assert(!has_call(op_id) || opr->is_stack() || !is_caller_save(reg_num(opr)), "can not have caller-save register operands at calls");
0N/A
0N/A // Append to ScopeValue array
0N/A return append_scope_value_for_operand(opr, scope_values);
0N/A
0N/A } else {
0N/A assert(value->as_Constant() != NULL, "all other instructions have only virtual operands");
0N/A assert(opr->is_constant(), "operand must be constant");
0N/A
0N/A return append_scope_value_for_constant(opr, scope_values);
0N/A }
0N/A } else {
0N/A // append a dummy value because real value not needed
3237N/A scope_values->append(_illegal_value);
0N/A return 1;
0N/A }
0N/A}
0N/A
0N/A
1739N/AIRScopeDebugInfo* LinearScan::compute_debug_info_for_scope(int op_id, IRScope* cur_scope, ValueStack* cur_state, ValueStack* innermost_state) {
0N/A IRScopeDebugInfo* caller_debug_info = NULL;
1739N/A
1739N/A ValueStack* caller_state = cur_state->caller_state();
0N/A if (caller_state != NULL) {
0N/A // process recursively to compute outermost scope first
1739N/A caller_debug_info = compute_debug_info_for_scope(op_id, cur_scope->caller(), caller_state, innermost_state);
0N/A }
0N/A
0N/A // initialize these to null.
0N/A // If we don't need deopt info or there are no locals, expressions or monitors,
0N/A // then these get recorded as no information and avoids the allocation of 0 length arrays.
0N/A GrowableArray<ScopeValue*>* locals = NULL;
0N/A GrowableArray<ScopeValue*>* expressions = NULL;
0N/A GrowableArray<MonitorValue*>* monitors = NULL;
0N/A
0N/A // describe local variable values
1739N/A int nof_locals = cur_state->locals_size();
0N/A if (nof_locals > 0) {
0N/A locals = new GrowableArray<ScopeValue*>(nof_locals);
0N/A
0N/A int pos = 0;
0N/A while (pos < nof_locals) {
0N/A assert(pos < cur_state->locals_size(), "why not?");
0N/A
0N/A Value local = cur_state->local_at(pos);
0N/A pos += append_scope_value(op_id, local, locals);
0N/A
0N/A assert(locals->length() == pos, "must match");
0N/A }
0N/A assert(locals->length() == cur_scope->method()->max_locals(), "wrong number of locals");
0N/A assert(locals->length() == cur_state->locals_size(), "wrong number of locals");
1739N/A } else if (cur_scope->method()->max_locals() > 0) {
1739N/A assert(cur_state->kind() == ValueStack::EmptyExceptionState, "should be");
1739N/A nof_locals = cur_scope->method()->max_locals();
1739N/A locals = new GrowableArray<ScopeValue*>(nof_locals);
1739N/A for(int i = 0; i < nof_locals; i++) {
3237N/A locals->append(_illegal_value);
1739N/A }
1739N/A }
0N/A
0N/A // describe expression stack
1739N/A int nof_stack = cur_state->stack_size();
0N/A if (nof_stack > 0) {
0N/A expressions = new GrowableArray<ScopeValue*>(nof_stack);
0N/A
1739N/A int pos = 0;
1739N/A while (pos < nof_stack) {
1739N/A Value expression = cur_state->stack_at_inc(pos);
0N/A append_scope_value(op_id, expression, expressions);
0N/A
1739N/A assert(expressions->length() == pos, "must match");
1739N/A }
1739N/A assert(expressions->length() == cur_state->stack_size(), "wrong number of stack entries");
0N/A }
0N/A
0N/A // describe monitors
1739N/A int nof_locks = cur_state->locks_size();
0N/A if (nof_locks > 0) {
1739N/A int lock_offset = cur_state->caller_state() != NULL ? cur_state->caller_state()->total_locks_size() : 0;
0N/A monitors = new GrowableArray<MonitorValue*>(nof_locks);
1739N/A for (int i = 0; i < nof_locks; i++) {
1739N/A monitors->append(location_for_monitor_index(lock_offset + i));
1739N/A }
1739N/A }
1739N/A
1739N/A return new IRScopeDebugInfo(cur_scope, cur_state->bci(), locals, expressions, monitors, caller_debug_info);
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::compute_debug_info(CodeEmitInfo* info, int op_id) {
0N/A TRACE_LINEAR_SCAN(3, tty->print_cr("creating debug information at op_id %d", op_id));
0N/A
0N/A IRScope* innermost_scope = info->scope();
0N/A ValueStack* innermost_state = info->stack();
0N/A
0N/A assert(innermost_scope != NULL && innermost_state != NULL, "why is it missing?");
0N/A
1739N/A DEBUG_ONLY(check_stack_depth(info, innermost_state->stack_size()));
0N/A
0N/A if (info->_scope_debug_info == NULL) {
0N/A // compute debug information
1739N/A info->_scope_debug_info = compute_debug_info_for_scope(op_id, innermost_scope, innermost_state, innermost_state);
0N/A } else {
0N/A // debug information already set. Check that it is correct from the current point of view
1739N/A DEBUG_ONLY(assert_equal(info->_scope_debug_info, compute_debug_info_for_scope(op_id, innermost_scope, innermost_state, innermost_state)));
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::assign_reg_num(LIR_OpList* instructions, IntervalWalker* iw) {
0N/A LIR_OpVisitState visitor;
0N/A int num_inst = instructions->length();
0N/A bool has_dead = false;
0N/A
0N/A for (int j = 0; j < num_inst; j++) {
0N/A LIR_Op* op = instructions->at(j);
0N/A if (op == NULL) { // this can happen when spill-moves are removed in eliminate_spill_moves
0N/A has_dead = true;
0N/A continue;
0N/A }
0N/A int op_id = op->id();
0N/A
0N/A // visit instruction to get list of operands
0N/A visitor.visit(op);
0N/A
0N/A // iterate all modes of the visitor and process all virtual operands
0N/A for_each_visitor_mode(mode) {
0N/A int n = visitor.opr_count(mode);
0N/A for (int k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(mode, k);
0N/A if (opr->is_virtual_register()) {
0N/A visitor.set_opr_at(mode, k, color_lir_opr(opr, op_id, mode));
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (visitor.info_count() > 0) {
0N/A // exception handling
0N/A if (compilation()->has_exception_handlers()) {
0N/A XHandlers* xhandlers = visitor.all_xhandler();
0N/A int n = xhandlers->length();
0N/A for (int k = 0; k < n; k++) {
0N/A XHandler* handler = xhandlers->handler_at(k);
0N/A if (handler->entry_code() != NULL) {
0N/A assign_reg_num(handler->entry_code()->instructions_list(), NULL);
0N/A }
0N/A }
0N/A } else {
0N/A assert(visitor.all_xhandler()->length() == 0, "missed exception handler");
0N/A }
0N/A
0N/A // compute oop map
0N/A assert(iw != NULL, "needed for compute_oop_map");
0N/A compute_oop_map(iw, visitor, op);
0N/A
0N/A // compute debug information
0N/A if (!use_fpu_stack_allocation()) {
0N/A // compute debug information if fpu stack allocation is not needed.
0N/A // when fpu stack allocation is needed, the debug information can not
0N/A // be computed here because the exact location of fpu operands is not known
0N/A // -> debug information is created inside the fpu stack allocator
0N/A int n = visitor.info_count();
0N/A for (int k = 0; k < n; k++) {
0N/A compute_debug_info(visitor.info_at(k), op_id);
0N/A }
0N/A }
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // make sure we haven't made the op invalid.
0N/A op->verify();
0N/A#endif
0N/A
0N/A // remove useless moves
0N/A if (op->code() == lir_move) {
0N/A assert(op->as_Op1() != NULL, "move must be LIR_Op1");
0N/A LIR_Op1* move = (LIR_Op1*)op;
0N/A LIR_Opr src = move->in_opr();
0N/A LIR_Opr dst = move->result_opr();
0N/A if (dst == src ||
0N/A !dst->is_pointer() && !src->is_pointer() &&
0N/A src->is_same_register(dst)) {
0N/A instructions->at_put(j, NULL);
0N/A has_dead = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (has_dead) {
0N/A // iterate all instructions of the block and remove all null-values.
0N/A int insert_point = 0;
0N/A for (int j = 0; j < num_inst; j++) {
0N/A LIR_Op* op = instructions->at(j);
0N/A if (op != NULL) {
0N/A if (insert_point != j) {
0N/A instructions->at_put(insert_point, op);
0N/A }
0N/A insert_point++;
0N/A }
0N/A }
0N/A instructions->truncate(insert_point);
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::assign_reg_num() {
0N/A TIME_LINEAR_SCAN(timer_assign_reg_num);
0N/A
0N/A init_compute_debug_info();
0N/A IntervalWalker* iw = init_compute_oop_maps();
0N/A
0N/A int num_blocks = block_count();
0N/A for (int i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A assign_reg_num(block->lir()->instructions_list(), iw);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::do_linear_scan() {
0N/A NOT_PRODUCT(_total_timer.begin_method());
0N/A
0N/A number_instructions();
0N/A
0N/A NOT_PRODUCT(print_lir(1, "Before Register Allocation"));
0N/A
0N/A compute_local_live_sets();
0N/A compute_global_live_sets();
0N/A CHECK_BAILOUT();
0N/A
0N/A build_intervals();
0N/A CHECK_BAILOUT();
0N/A sort_intervals_before_allocation();
0N/A
0N/A NOT_PRODUCT(print_intervals("Before Register Allocation"));
0N/A NOT_PRODUCT(LinearScanStatistic::compute(this, _stat_before_alloc));
0N/A
0N/A allocate_registers();
0N/A CHECK_BAILOUT();
0N/A
0N/A resolve_data_flow();
0N/A if (compilation()->has_exception_handlers()) {
0N/A resolve_exception_handlers();
0N/A }
0N/A // fill in number of spill slots into frame_map
0N/A propagate_spill_slots();
0N/A CHECK_BAILOUT();
0N/A
0N/A NOT_PRODUCT(print_intervals("After Register Allocation"));
0N/A NOT_PRODUCT(print_lir(2, "LIR after register allocation:"));
0N/A
0N/A sort_intervals_after_allocation();
722N/A
722N/A DEBUG_ONLY(verify());
722N/A
0N/A eliminate_spill_moves();
0N/A assign_reg_num();
0N/A CHECK_BAILOUT();
0N/A
0N/A NOT_PRODUCT(print_lir(2, "LIR after assignment of register numbers:"));
0N/A NOT_PRODUCT(LinearScanStatistic::compute(this, _stat_after_asign));
0N/A
0N/A { TIME_LINEAR_SCAN(timer_allocate_fpu_stack);
0N/A
0N/A if (use_fpu_stack_allocation()) {
0N/A allocate_fpu_stack(); // Only has effect on Intel
0N/A NOT_PRODUCT(print_lir(2, "LIR after FPU stack allocation:"));
0N/A }
0N/A }
0N/A
0N/A { TIME_LINEAR_SCAN(timer_optimize_lir);
0N/A
0N/A EdgeMoveOptimizer::optimize(ir()->code());
0N/A ControlFlowOptimizer::optimize(ir()->code());
0N/A // check that cfg is still correct after optimizations
0N/A ir()->verify();
0N/A }
0N/A
0N/A NOT_PRODUCT(print_lir(1, "Before Code Generation", false));
0N/A NOT_PRODUCT(LinearScanStatistic::compute(this, _stat_final));
0N/A NOT_PRODUCT(_total_timer.end_method(this));
0N/A}
0N/A
0N/A
0N/A// ********** Printing functions
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Avoid LinearScan::print_timers(double total) {
0N/A _total_timer.print(total);
0N/A}
0N/A
0N/Avoid LinearScan::print_statistics() {
0N/A _stat_before_alloc.print("before allocation");
0N/A _stat_after_asign.print("after assignment of register");
0N/A _stat_final.print("after optimization");
0N/A}
0N/A
0N/Avoid LinearScan::print_bitmap(BitMap& b) {
0N/A for (unsigned int i = 0; i < b.size(); i++) {
0N/A if (b.at(i)) tty->print("%d ", i);
0N/A }
0N/A tty->cr();
0N/A}
0N/A
0N/Avoid LinearScan::print_intervals(const char* label) {
0N/A if (TraceLinearScanLevel >= 1) {
0N/A int i;
0N/A tty->cr();
0N/A tty->print_cr("%s", label);
0N/A
0N/A for (i = 0; i < interval_count(); i++) {
0N/A Interval* interval = interval_at(i);
0N/A if (interval != NULL) {
0N/A interval->print();
0N/A }
0N/A }
0N/A
0N/A tty->cr();
0N/A tty->print_cr("--- Basic Blocks ---");
0N/A for (i = 0; i < block_count(); i++) {
0N/A BlockBegin* block = block_at(i);
0N/A tty->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());
0N/A }
0N/A tty->cr();
0N/A tty->cr();
0N/A }
0N/A
0N/A if (PrintCFGToFile) {
0N/A CFGPrinter::print_intervals(&_intervals, label);
0N/A }
0N/A}
0N/A
0N/Avoid LinearScan::print_lir(int level, const char* label, bool hir_valid) {
0N/A if (TraceLinearScanLevel >= level) {
0N/A tty->cr();
0N/A tty->print_cr("%s", label);
0N/A print_LIR(ir()->linear_scan_order());
0N/A tty->cr();
0N/A }
0N/A
0N/A if (level == 1 && PrintCFGToFile) {
0N/A CFGPrinter::print_cfg(ir()->linear_scan_order(), label, hir_valid, true);
0N/A }
0N/A}
0N/A
0N/A#endif //PRODUCT
0N/A
0N/A
0N/A// ********** verification functions for allocation
0N/A// (check that all intervals have a correct register and that no registers are overwritten)
0N/A#ifdef ASSERT
0N/A
0N/Avoid LinearScan::verify() {
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying intervals ******************************************"));
0N/A verify_intervals();
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying that no oops are in fixed intervals ****************"));
0N/A verify_no_oops_in_fixed_intervals();
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying that unpinned constants are not alive across block boundaries"));
0N/A verify_constants();
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying register allocation ********************************"));
0N/A verify_registers();
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("********* no errors found **********************************************"));
0N/A}
0N/A
0N/Avoid LinearScan::verify_intervals() {
0N/A int len = interval_count();
0N/A bool has_error = false;
0N/A
0N/A for (int i = 0; i < len; i++) {
0N/A Interval* i1 = interval_at(i);
0N/A if (i1 == NULL) continue;
0N/A
0N/A i1->check_split_children();
0N/A
0N/A if (i1->reg_num() != i) {
0N/A tty->print_cr("Interval %d is on position %d in list", i1->reg_num(), i); i1->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A
0N/A if (i1->reg_num() >= LIR_OprDesc::vreg_base && i1->type() == T_ILLEGAL) {
0N/A tty->print_cr("Interval %d has no type assigned", i1->reg_num()); i1->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A
0N/A if (i1->assigned_reg() == any_reg) {
0N/A tty->print_cr("Interval %d has no register assigned", i1->reg_num()); i1->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A
0N/A if (i1->assigned_reg() == i1->assigned_regHi()) {
0N/A tty->print_cr("Interval %d: low and high register equal", i1->reg_num()); i1->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A
0N/A if (!is_processed_reg_num(i1->assigned_reg())) {
0N/A tty->print_cr("Can not have an Interval for an ignored register"); i1->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A
0N/A if (i1->first() == Range::end()) {
0N/A tty->print_cr("Interval %d has no Range", i1->reg_num()); i1->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A
0N/A for (Range* r = i1->first(); r != Range::end(); r = r->next()) {
0N/A if (r->from() >= r->to()) {
0N/A tty->print_cr("Interval %d has zero length range", i1->reg_num()); i1->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A }
0N/A
0N/A for (int j = i + 1; j < len; j++) {
0N/A Interval* i2 = interval_at(j);
0N/A if (i2 == NULL) continue;
0N/A
0N/A // special intervals that are created in MoveResolver
0N/A // -> ignore them because the range information has no meaning there
0N/A if (i1->from() == 1 && i1->to() == 2) continue;
0N/A if (i2->from() == 1 && i2->to() == 2) continue;
0N/A
0N/A int r1 = i1->assigned_reg();
0N/A int r1Hi = i1->assigned_regHi();
0N/A int r2 = i2->assigned_reg();
0N/A int r2Hi = i2->assigned_regHi();
0N/A if (i1->intersects(i2) && (r1 == r2 || r1 == r2Hi || (r1Hi != any_reg && (r1Hi == r2 || r1Hi == r2Hi)))) {
0N/A tty->print_cr("Intervals %d and %d overlap and have the same register assigned", i1->reg_num(), i2->reg_num());
0N/A i1->print(); tty->cr();
0N/A i2->print(); tty->cr();
0N/A has_error = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A assert(has_error == false, "register allocation invalid");
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::verify_no_oops_in_fixed_intervals() {
722N/A Interval* fixed_intervals;
722N/A Interval* other_intervals;
722N/A create_unhandled_lists(&fixed_intervals, &other_intervals, is_precolored_cpu_interval, NULL);
722N/A
722N/A // to ensure a walking until the last instruction id, add a dummy interval
722N/A // with a high operation id
722N/A other_intervals = new Interval(any_reg);
722N/A other_intervals->add_range(max_jint - 2, max_jint - 1);
722N/A IntervalWalker* iw = new IntervalWalker(this, fixed_intervals, other_intervals);
722N/A
0N/A LIR_OpVisitState visitor;
0N/A for (int i = 0; i < block_count(); i++) {
0N/A BlockBegin* block = block_at(i);
0N/A
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A
0N/A for (int j = 0; j < instructions->length(); j++) {
0N/A LIR_Op* op = instructions->at(j);
0N/A int op_id = op->id();
0N/A
0N/A visitor.visit(op);
0N/A
722N/A if (visitor.info_count() > 0) {
722N/A iw->walk_before(op->id());
722N/A bool check_live = true;
722N/A if (op->code() == lir_move) {
722N/A LIR_Op1* move = (LIR_Op1*)op;
722N/A check_live = (move->patch_code() == lir_patch_none);
722N/A }
722N/A LIR_OpBranch* branch = op->as_OpBranch();
722N/A if (branch != NULL && branch->stub() != NULL && branch->stub()->is_exception_throw_stub()) {
722N/A // Don't bother checking the stub in this case since the
722N/A // exception stub will never return to normal control flow.
722N/A check_live = false;
722N/A }
722N/A
722N/A // Make sure none of the fixed registers is live across an
722N/A // oopmap since we can't handle that correctly.
722N/A if (check_live) {
722N/A for (Interval* interval = iw->active_first(fixedKind);
722N/A interval != Interval::end();
722N/A interval = interval->next()) {
722N/A if (interval->current_to() > op->id() + 1) {
722N/A // This interval is live out of this op so make sure
722N/A // that this interval represents some value that's
722N/A // referenced by this op either as an input or output.
722N/A bool ok = false;
722N/A for_each_visitor_mode(mode) {
722N/A int n = visitor.opr_count(mode);
722N/A for (int k = 0; k < n; k++) {
722N/A LIR_Opr opr = visitor.opr_at(mode, k);
722N/A if (opr->is_fixed_cpu()) {
722N/A if (interval_at(reg_num(opr)) == interval) {
722N/A ok = true;
722N/A break;
722N/A }
722N/A int hi = reg_numHi(opr);
722N/A if (hi != -1 && interval_at(hi) == interval) {
722N/A ok = true;
722N/A break;
722N/A }
722N/A }
722N/A }
722N/A }
722N/A assert(ok, "fixed intervals should never be live across an oopmap point");
722N/A }
722N/A }
722N/A }
722N/A }
722N/A
0N/A // oop-maps at calls do not contain registers, so check is not needed
0N/A if (!visitor.has_call()) {
0N/A
0N/A for_each_visitor_mode(mode) {
0N/A int n = visitor.opr_count(mode);
0N/A for (int k = 0; k < n; k++) {
0N/A LIR_Opr opr = visitor.opr_at(mode, k);
0N/A
0N/A if (opr->is_fixed_cpu() && opr->is_oop()) {
0N/A // operand is a non-virtual cpu register and contains an oop
0N/A TRACE_LINEAR_SCAN(4, op->print_on(tty); tty->print("checking operand "); opr->print(); tty->cr());
0N/A
0N/A Interval* interval = interval_at(reg_num(opr));
0N/A assert(interval != NULL, "no interval");
0N/A
0N/A if (mode == LIR_OpVisitState::inputMode) {
0N/A if (interval->to() >= op_id + 1) {
0N/A assert(interval->to() < op_id + 2 ||
0N/A interval->has_hole_between(op_id, op_id + 2),
0N/A "oop input operand live after instruction");
0N/A }
0N/A } else if (mode == LIR_OpVisitState::outputMode) {
0N/A if (interval->from() <= op_id - 1) {
0N/A assert(interval->has_hole_between(op_id - 1, op_id),
0N/A "oop input operand live after instruction");
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScan::verify_constants() {
0N/A int num_regs = num_virtual_regs();
0N/A int size = live_set_size();
0N/A int num_blocks = block_count();
0N/A
0N/A for (int i = 0; i < num_blocks; i++) {
0N/A BlockBegin* block = block_at(i);
0N/A BitMap live_at_edge = block->live_in();
0N/A
0N/A // visit all registers where the live_at_edge bit is set
304N/A for (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)) {
0N/A TRACE_LINEAR_SCAN(4, tty->print("checking interval %d of block B%d", r, block->block_id()));
0N/A
0N/A Value value = gen()->instruction_for_vreg(r);
0N/A
0N/A assert(value != NULL, "all intervals live across block boundaries must have Value");
0N/A assert(value->operand()->is_register() && value->operand()->is_virtual(), "value must have virtual operand");
0N/A assert(value->operand()->vreg_number() == r, "register number must match");
0N/A // TKR assert(value->as_Constant() == NULL || value->is_pinned(), "only pinned constants can be alive accross block boundaries");
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Aclass RegisterVerifier: public StackObj {
0N/A private:
0N/A LinearScan* _allocator;
0N/A BlockList _work_list; // all blocks that must be processed
0N/A IntervalsList _saved_states; // saved information of previous check
0N/A
0N/A // simplified access to methods of LinearScan
0N/A Compilation* compilation() const { return _allocator->compilation(); }
0N/A Interval* interval_at(int reg_num) const { return _allocator->interval_at(reg_num); }
0N/A int reg_num(LIR_Opr opr) const { return _allocator->reg_num(opr); }
0N/A
0N/A // currently, only registers are processed
0N/A int state_size() { return LinearScan::nof_regs; }
0N/A
0N/A // accessors
0N/A IntervalList* state_for_block(BlockBegin* block) { return _saved_states.at(block->block_id()); }
0N/A void set_state_for_block(BlockBegin* block, IntervalList* saved_state) { _saved_states.at_put(block->block_id(), saved_state); }
0N/A void add_to_work_list(BlockBegin* block) { if (!_work_list.contains(block)) _work_list.append(block); }
0N/A
0N/A // helper functions
0N/A IntervalList* copy(IntervalList* input_state);
0N/A void state_put(IntervalList* input_state, int reg, Interval* interval);
0N/A bool check_state(IntervalList* input_state, int reg, Interval* interval);
0N/A
0N/A void process_block(BlockBegin* block);
0N/A void process_xhandler(XHandler* xhandler, IntervalList* input_state);
0N/A void process_successor(BlockBegin* block, IntervalList* input_state);
0N/A void process_operations(LIR_List* ops, IntervalList* input_state);
0N/A
0N/A public:
0N/A RegisterVerifier(LinearScan* allocator)
0N/A : _allocator(allocator)
0N/A , _work_list(16)
0N/A , _saved_states(BlockBegin::number_of_blocks(), NULL)
0N/A { }
0N/A
0N/A void verify(BlockBegin* start);
0N/A};
0N/A
0N/A
0N/A// entry function from LinearScan that starts the verification
0N/Avoid LinearScan::verify_registers() {
0N/A RegisterVerifier verifier(this);
0N/A verifier.verify(block_at(0));
0N/A}
0N/A
0N/A
0N/Avoid RegisterVerifier::verify(BlockBegin* start) {
0N/A // setup input registers (method arguments) for first block
0N/A IntervalList* input_state = new IntervalList(state_size(), NULL);
0N/A CallingConvention* args = compilation()->frame_map()->incoming_arguments();
0N/A for (int n = 0; n < args->length(); n++) {
0N/A LIR_Opr opr = args->at(n);
0N/A if (opr->is_register()) {
0N/A Interval* interval = interval_at(reg_num(opr));
0N/A
0N/A if (interval->assigned_reg() < state_size()) {
0N/A input_state->at_put(interval->assigned_reg(), interval);
0N/A }
0N/A if (interval->assigned_regHi() != LinearScan::any_reg && interval->assigned_regHi() < state_size()) {
0N/A input_state->at_put(interval->assigned_regHi(), interval);
0N/A }
0N/A }
0N/A }
0N/A
0N/A set_state_for_block(start, input_state);
0N/A add_to_work_list(start);
0N/A
0N/A // main loop for verification
0N/A do {
0N/A BlockBegin* block = _work_list.at(0);
0N/A _work_list.remove_at(0);
0N/A
0N/A process_block(block);
0N/A } while (!_work_list.is_empty());
0N/A}
0N/A
0N/Avoid RegisterVerifier::process_block(BlockBegin* block) {
0N/A TRACE_LINEAR_SCAN(2, tty->cr(); tty->print_cr("process_block B%d", block->block_id()));
0N/A
0N/A // must copy state because it is modified
0N/A IntervalList* input_state = copy(state_for_block(block));
0N/A
0N/A if (TraceLinearScanLevel >= 4) {
0N/A tty->print_cr("Input-State of intervals:");
0N/A tty->print(" ");
0N/A for (int i = 0; i < state_size(); i++) {
0N/A if (input_state->at(i) != NULL) {
0N/A tty->print(" %4d", input_state->at(i)->reg_num());
0N/A } else {
0N/A tty->print(" __");
0N/A }
0N/A }
0N/A tty->cr();
0N/A tty->cr();
0N/A }
0N/A
0N/A // process all operations of the block
0N/A process_operations(block->lir(), input_state);
0N/A
0N/A // iterate all successors
0N/A for (int i = 0; i < block->number_of_sux(); i++) {
0N/A process_successor(block->sux_at(i), input_state);
0N/A }
0N/A}
0N/A
0N/Avoid RegisterVerifier::process_xhandler(XHandler* xhandler, IntervalList* input_state) {
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("process_xhandler B%d", xhandler->entry_block()->block_id()));
0N/A
0N/A // must copy state because it is modified
0N/A input_state = copy(input_state);
0N/A
0N/A if (xhandler->entry_code() != NULL) {
0N/A process_operations(xhandler->entry_code(), input_state);
0N/A }
0N/A process_successor(xhandler->entry_block(), input_state);
0N/A}
0N/A
0N/Avoid RegisterVerifier::process_successor(BlockBegin* block, IntervalList* input_state) {
0N/A IntervalList* saved_state = state_for_block(block);
0N/A
0N/A if (saved_state != NULL) {
0N/A // this block was already processed before.
0N/A // check if new input_state is consistent with saved_state
0N/A
0N/A bool saved_state_correct = true;
0N/A for (int i = 0; i < state_size(); i++) {
0N/A if (input_state->at(i) != saved_state->at(i)) {
0N/A // current input_state and previous saved_state assume a different
0N/A // interval in this register -> assume that this register is invalid
0N/A if (saved_state->at(i) != NULL) {
0N/A // invalidate old calculation only if it assumed that
0N/A // register was valid. when the register was already invalid,
0N/A // then the old calculation was correct.
0N/A saved_state_correct = false;
0N/A saved_state->at_put(i, NULL);
0N/A
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("process_successor B%d: invalidating slot %d", block->block_id(), i));
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (saved_state_correct) {
0N/A // already processed block with correct input_state
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: previous visit already correct", block->block_id()));
0N/A } else {
0N/A // must re-visit this block
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: must re-visit because input state changed", block->block_id()));
0N/A add_to_work_list(block);
0N/A }
0N/A
0N/A } else {
0N/A // block was not processed before, so set initial input_state
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: initial visit", block->block_id()));
0N/A
0N/A set_state_for_block(block, copy(input_state));
0N/A add_to_work_list(block);
0N/A }
0N/A}
0N/A
0N/A
0N/AIntervalList* RegisterVerifier::copy(IntervalList* input_state) {
0N/A IntervalList* copy_state = new IntervalList(input_state->length());
0N/A copy_state->push_all(input_state);
0N/A return copy_state;
0N/A}
0N/A
0N/Avoid RegisterVerifier::state_put(IntervalList* input_state, int reg, Interval* interval) {
0N/A if (reg != LinearScan::any_reg && reg < state_size()) {
0N/A if (interval != NULL) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" reg[%d] = %d", reg, interval->reg_num()));
0N/A } else if (input_state->at(reg) != NULL) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" reg[%d] = NULL", reg));
0N/A }
0N/A
0N/A input_state->at_put(reg, interval);
0N/A }
0N/A}
0N/A
0N/Abool RegisterVerifier::check_state(IntervalList* input_state, int reg, Interval* interval) {
0N/A if (reg != LinearScan::any_reg && reg < state_size()) {
0N/A if (input_state->at(reg) != interval) {
0N/A tty->print_cr("!! Error in register allocation: register %d does not contain interval %d", reg, interval->reg_num());
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/Avoid RegisterVerifier::process_operations(LIR_List* ops, IntervalList* input_state) {
0N/A // visit all instructions of the block
0N/A LIR_OpVisitState visitor;
0N/A bool has_error = false;
0N/A
0N/A for (int i = 0; i < ops->length(); i++) {
0N/A LIR_Op* op = ops->at(i);
0N/A visitor.visit(op);
0N/A
0N/A TRACE_LINEAR_SCAN(4, op->print_on(tty));
0N/A
0N/A // check if input operands are correct
0N/A int j;
0N/A int n = visitor.opr_count(LIR_OpVisitState::inputMode);
0N/A for (j = 0; j < n; j++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::inputMode, j);
0N/A if (opr->is_register() && LinearScan::is_processed_reg_num(reg_num(opr))) {
0N/A Interval* interval = interval_at(reg_num(opr));
0N/A if (op->id() != -1) {
0N/A interval = interval->split_child_at_op_id(op->id(), LIR_OpVisitState::inputMode);
0N/A }
0N/A
0N/A has_error |= check_state(input_state, interval->assigned_reg(), interval->split_parent());
0N/A has_error |= check_state(input_state, interval->assigned_regHi(), interval->split_parent());
0N/A
0N/A // When an operand is marked with is_last_use, then the fpu stack allocator
0N/A // removes the register from the fpu stack -> the register contains no value
0N/A if (opr->is_last_use()) {
0N/A state_put(input_state, interval->assigned_reg(), NULL);
0N/A state_put(input_state, interval->assigned_regHi(), NULL);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // invalidate all caller save registers at calls
0N/A if (visitor.has_call()) {
1909N/A for (j = 0; j < FrameMap::nof_caller_save_cpu_regs(); j++) {
0N/A state_put(input_state, reg_num(FrameMap::caller_save_cpu_reg_at(j)), NULL);
0N/A }
0N/A for (j = 0; j < FrameMap::nof_caller_save_fpu_regs; j++) {
0N/A state_put(input_state, reg_num(FrameMap::caller_save_fpu_reg_at(j)), NULL);
0N/A }
0N/A
304N/A#ifdef X86
0N/A for (j = 0; j < FrameMap::nof_caller_save_xmm_regs; j++) {
0N/A state_put(input_state, reg_num(FrameMap::caller_save_xmm_reg_at(j)), NULL);
0N/A }
0N/A#endif
0N/A }
0N/A
0N/A // process xhandler before output and temp operands
0N/A XHandlers* xhandlers = visitor.all_xhandler();
0N/A n = xhandlers->length();
0N/A for (int k = 0; k < n; k++) {
0N/A process_xhandler(xhandlers->handler_at(k), input_state);
0N/A }
0N/A
0N/A // set temp operands (some operations use temp operands also as output operands, so can't set them NULL)
0N/A n = visitor.opr_count(LIR_OpVisitState::tempMode);
0N/A for (j = 0; j < n; j++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::tempMode, j);
0N/A if (opr->is_register() && LinearScan::is_processed_reg_num(reg_num(opr))) {
0N/A Interval* interval = interval_at(reg_num(opr));
0N/A if (op->id() != -1) {
0N/A interval = interval->split_child_at_op_id(op->id(), LIR_OpVisitState::tempMode);
0N/A }
0N/A
0N/A state_put(input_state, interval->assigned_reg(), interval->split_parent());
0N/A state_put(input_state, interval->assigned_regHi(), interval->split_parent());
0N/A }
0N/A }
0N/A
0N/A // set output operands
0N/A n = visitor.opr_count(LIR_OpVisitState::outputMode);
0N/A for (j = 0; j < n; j++) {
0N/A LIR_Opr opr = visitor.opr_at(LIR_OpVisitState::outputMode, j);
0N/A if (opr->is_register() && LinearScan::is_processed_reg_num(reg_num(opr))) {
0N/A Interval* interval = interval_at(reg_num(opr));
0N/A if (op->id() != -1) {
0N/A interval = interval->split_child_at_op_id(op->id(), LIR_OpVisitState::outputMode);
0N/A }
0N/A
0N/A state_put(input_state, interval->assigned_reg(), interval->split_parent());
0N/A state_put(input_state, interval->assigned_regHi(), interval->split_parent());
0N/A }
0N/A }
0N/A }
0N/A assert(has_error == false, "Error in register allocation");
0N/A}
0N/A
0N/A#endif // ASSERT
0N/A
0N/A
0N/A
0N/A// **** Implementation of MoveResolver ******************************
0N/A
0N/AMoveResolver::MoveResolver(LinearScan* allocator) :
0N/A _allocator(allocator),
0N/A _multiple_reads_allowed(false),
0N/A _mapping_from(8),
0N/A _mapping_from_opr(8),
0N/A _mapping_to(8),
0N/A _insert_list(NULL),
0N/A _insert_idx(-1),
0N/A _insertion_buffer()
0N/A{
0N/A for (int i = 0; i < LinearScan::nof_regs; i++) {
0N/A _register_blocked[i] = 0;
0N/A }
0N/A DEBUG_ONLY(check_empty());
0N/A}
0N/A
0N/A
0N/A#ifdef ASSERT
0N/A
0N/Avoid MoveResolver::check_empty() {
0N/A assert(_mapping_from.length() == 0 && _mapping_from_opr.length() == 0 && _mapping_to.length() == 0, "list must be empty before and after processing");
0N/A for (int i = 0; i < LinearScan::nof_regs; i++) {
0N/A assert(register_blocked(i) == 0, "register map must be empty before and after processing");
0N/A }
0N/A assert(_multiple_reads_allowed == false, "must have default value");
0N/A}
0N/A
0N/Avoid MoveResolver::verify_before_resolve() {
0N/A assert(_mapping_from.length() == _mapping_from_opr.length(), "length must be equal");
0N/A assert(_mapping_from.length() == _mapping_to.length(), "length must be equal");
0N/A assert(_insert_list != NULL && _insert_idx != -1, "insert position not set");
0N/A
0N/A int i, j;
0N/A if (!_multiple_reads_allowed) {
0N/A for (i = 0; i < _mapping_from.length(); i++) {
0N/A for (j = i + 1; j < _mapping_from.length(); j++) {
0N/A assert(_mapping_from.at(i) == NULL || _mapping_from.at(i) != _mapping_from.at(j), "cannot read from same interval twice");
0N/A }
0N/A }
0N/A }
0N/A
0N/A for (i = 0; i < _mapping_to.length(); i++) {
0N/A for (j = i + 1; j < _mapping_to.length(); j++) {
0N/A assert(_mapping_to.at(i) != _mapping_to.at(j), "cannot write to same interval twice");
0N/A }
0N/A }
0N/A
0N/A
0N/A BitMap used_regs(LinearScan::nof_regs + allocator()->frame_map()->argcount() + allocator()->max_spills());
0N/A used_regs.clear();
0N/A if (!_multiple_reads_allowed) {
0N/A for (i = 0; i < _mapping_from.length(); i++) {
0N/A Interval* it = _mapping_from.at(i);
0N/A if (it != NULL) {
0N/A assert(!used_regs.at(it->assigned_reg()), "cannot read from same register twice");
0N/A used_regs.set_bit(it->assigned_reg());
0N/A
0N/A if (it->assigned_regHi() != LinearScan::any_reg) {
0N/A assert(!used_regs.at(it->assigned_regHi()), "cannot read from same register twice");
0N/A used_regs.set_bit(it->assigned_regHi());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A used_regs.clear();
0N/A for (i = 0; i < _mapping_to.length(); i++) {
0N/A Interval* it = _mapping_to.at(i);
0N/A assert(!used_regs.at(it->assigned_reg()), "cannot write to same register twice");
0N/A used_regs.set_bit(it->assigned_reg());
0N/A
0N/A if (it->assigned_regHi() != LinearScan::any_reg) {
0N/A assert(!used_regs.at(it->assigned_regHi()), "cannot write to same register twice");
0N/A used_regs.set_bit(it->assigned_regHi());
0N/A }
0N/A }
0N/A
0N/A used_regs.clear();
0N/A for (i = 0; i < _mapping_from.length(); i++) {
0N/A Interval* it = _mapping_from.at(i);
0N/A if (it != NULL && it->assigned_reg() >= LinearScan::nof_regs) {
0N/A used_regs.set_bit(it->assigned_reg());
0N/A }
0N/A }
0N/A for (i = 0; i < _mapping_to.length(); i++) {
0N/A Interval* it = _mapping_to.at(i);
0N/A assert(!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");
0N/A }
0N/A}
0N/A
0N/A#endif // ASSERT
0N/A
0N/A
0N/A// mark assigned_reg and assigned_regHi of the interval as blocked
0N/Avoid MoveResolver::block_registers(Interval* it) {
0N/A int reg = it->assigned_reg();
0N/A if (reg < LinearScan::nof_regs) {
0N/A assert(_multiple_reads_allowed || register_blocked(reg) == 0, "register already marked as used");
0N/A set_register_blocked(reg, 1);
0N/A }
0N/A reg = it->assigned_regHi();
0N/A if (reg != LinearScan::any_reg && reg < LinearScan::nof_regs) {
0N/A assert(_multiple_reads_allowed || register_blocked(reg) == 0, "register already marked as used");
0N/A set_register_blocked(reg, 1);
0N/A }
0N/A}
0N/A
0N/A// mark assigned_reg and assigned_regHi of the interval as unblocked
0N/Avoid MoveResolver::unblock_registers(Interval* it) {
0N/A int reg = it->assigned_reg();
0N/A if (reg < LinearScan::nof_regs) {
0N/A assert(register_blocked(reg) > 0, "register already marked as unused");
0N/A set_register_blocked(reg, -1);
0N/A }
0N/A reg = it->assigned_regHi();
0N/A if (reg != LinearScan::any_reg && reg < LinearScan::nof_regs) {
0N/A assert(register_blocked(reg) > 0, "register already marked as unused");
0N/A set_register_blocked(reg, -1);
0N/A }
0N/A}
0N/A
0N/A// check if assigned_reg and assigned_regHi of the to-interval are not blocked (or only blocked by from)
0N/Abool MoveResolver::save_to_process_move(Interval* from, Interval* to) {
0N/A int from_reg = -1;
0N/A int from_regHi = -1;
0N/A if (from != NULL) {
0N/A from_reg = from->assigned_reg();
0N/A from_regHi = from->assigned_regHi();
0N/A }
0N/A
0N/A int reg = to->assigned_reg();
0N/A if (reg < LinearScan::nof_regs) {
0N/A if (register_blocked(reg) > 1 || (register_blocked(reg) == 1 && reg != from_reg && reg != from_regHi)) {
0N/A return false;
0N/A }
0N/A }
0N/A reg = to->assigned_regHi();
0N/A if (reg != LinearScan::any_reg && reg < LinearScan::nof_regs) {
0N/A if (register_blocked(reg) > 1 || (register_blocked(reg) == 1 && reg != from_reg && reg != from_regHi)) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A return true;
0N/A}
0N/A
0N/A
0N/Avoid MoveResolver::create_insertion_buffer(LIR_List* list) {
0N/A assert(!_insertion_buffer.initialized(), "overwriting existing buffer");
0N/A _insertion_buffer.init(list);
0N/A}
0N/A
0N/Avoid MoveResolver::append_insertion_buffer() {
0N/A if (_insertion_buffer.initialized()) {
0N/A _insertion_buffer.lir_list()->append(&_insertion_buffer);
0N/A }
0N/A assert(!_insertion_buffer.initialized(), "must be uninitialized now");
0N/A
0N/A _insert_list = NULL;
0N/A _insert_idx = -1;
0N/A}
0N/A
0N/Avoid MoveResolver::insert_move(Interval* from_interval, Interval* to_interval) {
0N/A assert(from_interval->reg_num() != to_interval->reg_num(), "from and to interval equal");
0N/A assert(from_interval->type() == to_interval->type(), "move between different types");
0N/A assert(_insert_list != NULL && _insert_idx != -1, "must setup insert position first");
0N/A assert(_insertion_buffer.lir_list() == _insert_list, "wrong insertion buffer");
0N/A
0N/A LIR_Opr from_opr = LIR_OprFact::virtual_register(from_interval->reg_num(), from_interval->type());
0N/A LIR_Opr to_opr = LIR_OprFact::virtual_register(to_interval->reg_num(), to_interval->type());
0N/A
0N/A if (!_multiple_reads_allowed) {
0N/A // the last_use flag is an optimization for FPU stack allocation. When the same
0N/A // input interval is used in more than one move, then it is too difficult to determine
0N/A // if this move is really the last use.
0N/A from_opr = from_opr->make_last_use();
0N/A }
0N/A _insertion_buffer.move(_insert_idx, from_opr, to_opr);
0N/A
0N/A TRACE_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()));
0N/A}
0N/A
0N/Avoid MoveResolver::insert_move(LIR_Opr from_opr, Interval* to_interval) {
0N/A assert(from_opr->type() == to_interval->type(), "move between different types");
0N/A assert(_insert_list != NULL && _insert_idx != -1, "must setup insert position first");
0N/A assert(_insertion_buffer.lir_list() == _insert_list, "wrong insertion buffer");
0N/A
0N/A LIR_Opr to_opr = LIR_OprFact::virtual_register(to_interval->reg_num(), to_interval->type());
0N/A _insertion_buffer.move(_insert_idx, from_opr, to_opr);
0N/A
0N/A TRACE_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()));
0N/A}
0N/A
0N/A
0N/Avoid MoveResolver::resolve_mappings() {
0N/A TRACE_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));
0N/A DEBUG_ONLY(verify_before_resolve());
0N/A
0N/A // Block all registers that are used as input operands of a move.
0N/A // When a register is blocked, no move to this register is emitted.
0N/A // This is necessary for detecting cycles in moves.
0N/A int i;
0N/A for (i = _mapping_from.length() - 1; i >= 0; i--) {
0N/A Interval* from_interval = _mapping_from.at(i);
0N/A if (from_interval != NULL) {
0N/A block_registers(from_interval);
0N/A }
0N/A }
0N/A
0N/A int spill_candidate = -1;
0N/A while (_mapping_from.length() > 0) {
0N/A bool processed_interval = false;
0N/A
0N/A for (i = _mapping_from.length() - 1; i >= 0; i--) {
0N/A Interval* from_interval = _mapping_from.at(i);
0N/A Interval* to_interval = _mapping_to.at(i);
0N/A
0N/A if (save_to_process_move(from_interval, to_interval)) {
0N/A // this inverval can be processed because target is free
0N/A if (from_interval != NULL) {
0N/A insert_move(from_interval, to_interval);
0N/A unblock_registers(from_interval);
0N/A } else {
0N/A insert_move(_mapping_from_opr.at(i), to_interval);
0N/A }
0N/A _mapping_from.remove_at(i);
0N/A _mapping_from_opr.remove_at(i);
0N/A _mapping_to.remove_at(i);
0N/A
0N/A processed_interval = true;
0N/A } else if (from_interval != NULL && from_interval->assigned_reg() < LinearScan::nof_regs) {
0N/A // this interval cannot be processed now because target is not free
0N/A // it starts in a register, so it is a possible candidate for spilling
0N/A spill_candidate = i;
0N/A }
0N/A }
0N/A
0N/A if (!processed_interval) {
0N/A // no move could be processed because there is a cycle in the move list
0N/A // (e.g. r1 -> r2, r2 -> r1), so one interval must be spilled to memory
0N/A assert(spill_candidate != -1, "no interval in register for spilling found");
0N/A
0N/A // create a new spill interval and assign a stack slot to it
0N/A Interval* from_interval = _mapping_from.at(spill_candidate);
0N/A Interval* spill_interval = new Interval(-1);
0N/A spill_interval->set_type(from_interval->type());
0N/A
0N/A // add a dummy range because real position is difficult to calculate
0N/A // Note: this range is a special case when the integrity of the allocation is checked
0N/A spill_interval->add_range(1, 2);
0N/A
0N/A // do not allocate a new spill slot for temporary interval, but
0N/A // use spill slot assigned to from_interval. Otherwise moves from
0N/A // one stack slot to another can happen (not allowed by LIR_Assembler
0N/A int spill_slot = from_interval->canonical_spill_slot();
0N/A if (spill_slot < 0) {
0N/A spill_slot = allocator()->allocate_spill_slot(type2spill_size[spill_interval->type()] == 2);
0N/A from_interval->set_canonical_spill_slot(spill_slot);
0N/A }
0N/A spill_interval->assign_reg(spill_slot);
0N/A allocator()->append_interval(spill_interval);
0N/A
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("created new Interval %d for spilling", spill_interval->reg_num()));
0N/A
0N/A // insert a move from register to stack and update the mapping
0N/A insert_move(from_interval, spill_interval);
0N/A _mapping_from.at_put(spill_candidate, spill_interval);
0N/A unblock_registers(from_interval);
0N/A }
0N/A }
0N/A
0N/A // reset to default value
0N/A _multiple_reads_allowed = false;
0N/A
0N/A // check that all intervals have been processed
0N/A DEBUG_ONLY(check_empty());
0N/A}
0N/A
0N/A
0N/Avoid MoveResolver::set_insert_position(LIR_List* insert_list, int insert_idx) {
0N/A TRACE_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));
0N/A assert(_insert_list == NULL && _insert_idx == -1, "use move_insert_position instead of set_insert_position when data already set");
0N/A
0N/A create_insertion_buffer(insert_list);
0N/A _insert_list = insert_list;
0N/A _insert_idx = insert_idx;
0N/A}
0N/A
0N/Avoid MoveResolver::move_insert_position(LIR_List* insert_list, int insert_idx) {
0N/A TRACE_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));
0N/A
0N/A if (_insert_list != NULL && (insert_list != _insert_list || insert_idx != _insert_idx)) {
0N/A // insert position changed -> resolve current mappings
0N/A resolve_mappings();
0N/A }
0N/A
0N/A if (insert_list != _insert_list) {
0N/A // block changed -> append insertion_buffer because it is
0N/A // bound to a specific block and create a new insertion_buffer
0N/A append_insertion_buffer();
0N/A create_insertion_buffer(insert_list);
0N/A }
0N/A
0N/A _insert_list = insert_list;
0N/A _insert_idx = insert_idx;
0N/A}
0N/A
0N/Avoid MoveResolver::add_mapping(Interval* from_interval, Interval* to_interval) {
0N/A TRACE_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()));
0N/A
0N/A _mapping_from.append(from_interval);
0N/A _mapping_from_opr.append(LIR_OprFact::illegalOpr);
0N/A _mapping_to.append(to_interval);
0N/A}
0N/A
0N/A
0N/Avoid MoveResolver::add_mapping(LIR_Opr from_opr, Interval* to_interval) {
0N/A TRACE_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()));
0N/A assert(from_opr->is_constant(), "only for constants");
0N/A
0N/A _mapping_from.append(NULL);
0N/A _mapping_from_opr.append(from_opr);
0N/A _mapping_to.append(to_interval);
0N/A}
0N/A
0N/Avoid MoveResolver::resolve_and_append_moves() {
0N/A if (has_mappings()) {
0N/A resolve_mappings();
0N/A }
0N/A append_insertion_buffer();
0N/A}
0N/A
0N/A
0N/A
0N/A// **** Implementation of Range *************************************
0N/A
0N/ARange::Range(int from, int to, Range* next) :
0N/A _from(from),
0N/A _to(to),
0N/A _next(next)
0N/A{
0N/A}
0N/A
0N/A// initialize sentinel
0N/ARange* Range::_end = NULL;
1504N/Avoid Range::initialize(Arena* arena) {
1504N/A _end = new (arena) Range(max_jint, max_jint, NULL);
0N/A}
0N/A
0N/Aint Range::intersects_at(Range* r2) const {
0N/A const Range* r1 = this;
0N/A
0N/A assert(r1 != NULL && r2 != NULL, "null ranges not allowed");
0N/A assert(r1 != _end && r2 != _end, "empty ranges not allowed");
0N/A
0N/A do {
0N/A if (r1->from() < r2->from()) {
0N/A if (r1->to() <= r2->from()) {
0N/A r1 = r1->next(); if (r1 == _end) return -1;
0N/A } else {
0N/A return r2->from();
0N/A }
0N/A } else if (r2->from() < r1->from()) {
0N/A if (r2->to() <= r1->from()) {
0N/A r2 = r2->next(); if (r2 == _end) return -1;
0N/A } else {
0N/A return r1->from();
0N/A }
0N/A } else { // r1->from() == r2->from()
0N/A if (r1->from() == r1->to()) {
0N/A r1 = r1->next(); if (r1 == _end) return -1;
0N/A } else if (r2->from() == r2->to()) {
0N/A r2 = r2->next(); if (r2 == _end) return -1;
0N/A } else {
0N/A return r1->from();
0N/A }
0N/A }
0N/A } while (true);
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/Avoid Range::print(outputStream* out) const {
0N/A out->print("[%d, %d[ ", _from, _to);
0N/A}
0N/A#endif
0N/A
0N/A
0N/A
0N/A// **** Implementation of Interval **********************************
0N/A
0N/A// initialize sentinel
0N/AInterval* Interval::_end = NULL;
1504N/Avoid Interval::initialize(Arena* arena) {
1504N/A Range::initialize(arena);
1504N/A _end = new (arena) Interval(-1);
0N/A}
0N/A
0N/AInterval::Interval(int reg_num) :
0N/A _reg_num(reg_num),
0N/A _type(T_ILLEGAL),
0N/A _first(Range::end()),
0N/A _use_pos_and_kinds(12),
0N/A _current(Range::end()),
0N/A _next(_end),
0N/A _state(invalidState),
0N/A _assigned_reg(LinearScan::any_reg),
0N/A _assigned_regHi(LinearScan::any_reg),
0N/A _cached_to(-1),
0N/A _cached_opr(LIR_OprFact::illegalOpr),
0N/A _cached_vm_reg(VMRegImpl::Bad()),
0N/A _split_children(0),
0N/A _canonical_spill_slot(-1),
0N/A _insert_move_when_activated(false),
0N/A _register_hint(NULL),
0N/A _spill_state(noDefinitionFound),
0N/A _spill_definition_pos(-1)
0N/A{
0N/A _split_parent = this;
0N/A _current_split_child = this;
0N/A}
0N/A
0N/Aint Interval::calc_to() {
0N/A assert(_first != Range::end(), "interval has no range");
0N/A
0N/A Range* r = _first;
0N/A while (r->next() != Range::end()) {
0N/A r = r->next();
0N/A }
0N/A return r->to();
0N/A}
0N/A
0N/A
0N/A#ifdef ASSERT
0N/A// consistency check of split-children
0N/Avoid Interval::check_split_children() {
0N/A if (_split_children.length() > 0) {
0N/A assert(is_split_parent(), "only split parents can have children");
0N/A
0N/A for (int i = 0; i < _split_children.length(); i++) {
0N/A Interval* i1 = _split_children.at(i);
0N/A
0N/A assert(i1->split_parent() == this, "not a split child of this interval");
0N/A assert(i1->type() == type(), "must be equal for all split children");
0N/A assert(i1->canonical_spill_slot() == canonical_spill_slot(), "must be equal for all split children");
0N/A
0N/A for (int j = i + 1; j < _split_children.length(); j++) {
0N/A Interval* i2 = _split_children.at(j);
0N/A
0N/A assert(i1->reg_num() != i2->reg_num(), "same register number");
0N/A
0N/A if (i1->from() < i2->from()) {
0N/A assert(i1->to() <= i2->from() && i1->to() < i2->to(), "intervals overlapping");
0N/A } else {
0N/A assert(i2->from() < i1->from(), "intervals start at same op_id");
0N/A assert(i2->to() <= i1->from() && i2->to() < i1->to(), "intervals overlapping");
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A#endif // ASSERT
0N/A
0N/AInterval* Interval::register_hint(bool search_split_child) const {
0N/A if (!search_split_child) {
0N/A return _register_hint;
0N/A }
0N/A
0N/A if (_register_hint != NULL) {
0N/A assert(_register_hint->is_split_parent(), "ony split parents are valid hint registers");
0N/A
0N/A if (_register_hint->assigned_reg() >= 0 && _register_hint->assigned_reg() < LinearScan::nof_regs) {
0N/A return _register_hint;
0N/A
0N/A } else if (_register_hint->_split_children.length() > 0) {
0N/A // search the first split child that has a register assigned
0N/A int len = _register_hint->_split_children.length();
0N/A for (int i = 0; i < len; i++) {
0N/A Interval* cur = _register_hint->_split_children.at(i);
0N/A
0N/A if (cur->assigned_reg() >= 0 && cur->assigned_reg() < LinearScan::nof_regs) {
0N/A return cur;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // no hint interval found that has a register assigned
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/AInterval* Interval::split_child_at_op_id(int op_id, LIR_OpVisitState::OprMode mode) {
0N/A assert(is_split_parent(), "can only be called for split parents");
0N/A assert(op_id >= 0, "invalid op_id (method can not be called for spill moves)");
0N/A
0N/A Interval* result;
0N/A if (_split_children.length() == 0) {
0N/A result = this;
0N/A } else {
0N/A result = NULL;
0N/A int len = _split_children.length();
0N/A
0N/A // in outputMode, the end of the interval (op_id == cur->to()) is not valid
0N/A int to_offset = (mode == LIR_OpVisitState::outputMode ? 0 : 1);
0N/A
0N/A int i;
0N/A for (i = 0; i < len; i++) {
0N/A Interval* cur = _split_children.at(i);
0N/A if (cur->from() <= op_id && op_id < cur->to() + to_offset) {
0N/A if (i > 0) {
0N/A // exchange current split child to start of list (faster access for next call)
0N/A _split_children.at_put(i, _split_children.at(0));
0N/A _split_children.at_put(0, cur);
0N/A }
0N/A
0N/A // interval found
0N/A result = cur;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A for (i = 0; i < len; i++) {
0N/A Interval* tmp = _split_children.at(i);
0N/A if (tmp != result && tmp->from() <= op_id && op_id < tmp->to() + to_offset) {
0N/A tty->print_cr("two valid result intervals found for op_id %d: %d and %d", op_id, result->reg_num(), tmp->reg_num());
0N/A result->print();
0N/A tmp->print();
0N/A assert(false, "two valid result intervals found");
0N/A }
0N/A }
0N/A#endif
0N/A }
0N/A
0N/A assert(result != NULL, "no matching interval found");
0N/A assert(result->covers(op_id, mode), "op_id not covered by interval");
0N/A
0N/A return result;
0N/A}
0N/A
0N/A
0N/A// returns the last split child that ends before the given op_id
0N/AInterval* Interval::split_child_before_op_id(int op_id) {
0N/A assert(op_id >= 0, "invalid op_id");
0N/A
0N/A Interval* parent = split_parent();
0N/A Interval* result = NULL;
0N/A
0N/A int len = parent->_split_children.length();
0N/A assert(len > 0, "no split children available");
0N/A
0N/A for (int i = len - 1; i >= 0; i--) {
0N/A Interval* cur = parent->_split_children.at(i);
0N/A if (cur->to() <= op_id && (result == NULL || result->to() < cur->to())) {
0N/A result = cur;
0N/A }
0N/A }
0N/A
0N/A assert(result != NULL, "no split child found");
0N/A return result;
0N/A}
0N/A
0N/A
0N/A// checks if op_id is covered by any split child
0N/Abool Interval::split_child_covers(int op_id, LIR_OpVisitState::OprMode mode) {
0N/A assert(is_split_parent(), "can only be called for split parents");
0N/A assert(op_id >= 0, "invalid op_id (method can not be called for spill moves)");
0N/A
0N/A if (_split_children.length() == 0) {
0N/A // simple case if interval was not split
0N/A return covers(op_id, mode);
0N/A
0N/A } else {
0N/A // extended case: check all split children
0N/A int len = _split_children.length();
0N/A for (int i = 0; i < len; i++) {
0N/A Interval* cur = _split_children.at(i);
0N/A if (cur->covers(op_id, mode)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A}
0N/A
0N/A
0N/A// Note: use positions are sorted descending -> first use has highest index
0N/Aint Interval::first_usage(IntervalUseKind min_use_kind) const {
0N/A assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");
0N/A
0N/A for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {
0N/A if (_use_pos_and_kinds.at(i + 1) >= min_use_kind) {
0N/A return _use_pos_and_kinds.at(i);
0N/A }
0N/A }
0N/A return max_jint;
0N/A}
0N/A
0N/Aint Interval::next_usage(IntervalUseKind min_use_kind, int from) const {
0N/A assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");
0N/A
0N/A for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {
0N/A if (_use_pos_and_kinds.at(i) >= from && _use_pos_and_kinds.at(i + 1) >= min_use_kind) {
0N/A return _use_pos_and_kinds.at(i);
0N/A }
0N/A }
0N/A return max_jint;
0N/A}
0N/A
0N/Aint Interval::next_usage_exact(IntervalUseKind exact_use_kind, int from) const {
0N/A assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");
0N/A
0N/A for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {
0N/A if (_use_pos_and_kinds.at(i) >= from && _use_pos_and_kinds.at(i + 1) == exact_use_kind) {
0N/A return _use_pos_and_kinds.at(i);
0N/A }
0N/A }
0N/A return max_jint;
0N/A}
0N/A
0N/Aint Interval::previous_usage(IntervalUseKind min_use_kind, int from) const {
0N/A assert(LinearScan::is_virtual_interval(this), "cannot access use positions for fixed intervals");
0N/A
0N/A int prev = 0;
0N/A for (int i = _use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {
0N/A if (_use_pos_and_kinds.at(i) > from) {
0N/A return prev;
0N/A }
0N/A if (_use_pos_and_kinds.at(i + 1) >= min_use_kind) {
0N/A prev = _use_pos_and_kinds.at(i);
0N/A }
0N/A }
0N/A return prev;
0N/A}
0N/A
0N/Avoid Interval::add_use_pos(int pos, IntervalUseKind use_kind) {
0N/A assert(covers(pos, LIR_OpVisitState::inputMode), "use position not covered by live range");
0N/A
0N/A // do not add use positions for precolored intervals because
0N/A // they are never used
0N/A if (use_kind != noUse && reg_num() >= LIR_OprDesc::vreg_base) {
0N/A#ifdef ASSERT
0N/A assert(_use_pos_and_kinds.length() % 2 == 0, "must be");
0N/A for (int i = 0; i < _use_pos_and_kinds.length(); i += 2) {
0N/A assert(pos <= _use_pos_and_kinds.at(i), "already added a use-position with lower position");
0N/A assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
0N/A if (i > 0) {
0N/A assert(_use_pos_and_kinds.at(i) < _use_pos_and_kinds.at(i - 2), "not sorted descending");
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A // Note: add_use is called in descending order, so list gets sorted
0N/A // automatically by just appending new use positions
0N/A int len = _use_pos_and_kinds.length();
0N/A if (len == 0 || _use_pos_and_kinds.at(len - 2) > pos) {
0N/A _use_pos_and_kinds.append(pos);
0N/A _use_pos_and_kinds.append(use_kind);
0N/A } else if (_use_pos_and_kinds.at(len - 1) < use_kind) {
0N/A assert(_use_pos_and_kinds.at(len - 2) == pos, "list not sorted correctly");
0N/A _use_pos_and_kinds.at_put(len - 1, use_kind);
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid Interval::add_range(int from, int to) {
0N/A assert(from < to, "invalid range");
0N/A assert(first() == Range::end() || to < first()->next()->from(), "not inserting at begin of interval");
0N/A assert(from <= first()->to(), "not inserting at begin of interval");
0N/A
0N/A if (first()->from() <= to) {
0N/A // join intersecting ranges
0N/A first()->set_from(MIN2(from, first()->from()));
0N/A first()->set_to (MAX2(to, first()->to()));
0N/A } else {
0N/A // insert new range
0N/A _first = new Range(from, to, first());
0N/A }
0N/A}
0N/A
0N/AInterval* Interval::new_split_child() {
0N/A // allocate new interval
0N/A Interval* result = new Interval(-1);
0N/A result->set_type(type());
0N/A
0N/A Interval* parent = split_parent();
0N/A result->_split_parent = parent;
0N/A result->set_register_hint(parent);
0N/A
0N/A // insert new interval in children-list of parent
0N/A if (parent->_split_children.length() == 0) {
0N/A assert(is_split_parent(), "list must be initialized at first split");
0N/A
0N/A parent->_split_children = IntervalList(4);
0N/A parent->_split_children.append(this);
0N/A }
0N/A parent->_split_children.append(result);
0N/A
0N/A return result;
0N/A}
0N/A
0N/A// split this interval at the specified position and return
0N/A// the remainder as a new interval.
0N/A//
0N/A// when an interval is split, a bi-directional link is established between the original interval
0N/A// (the split parent) and the intervals that are split off this interval (the split children)
0N/A// When a split child is split again, the new created interval is also a direct child
0N/A// of the original parent (there is no tree of split children stored, but a flat list)
0N/A// All split children are spilled to the same stack slot (stored in _canonical_spill_slot)
0N/A//
0N/A// Note: The new interval has no valid reg_num
0N/AInterval* Interval::split(int split_pos) {
0N/A assert(LinearScan::is_virtual_interval(this), "cannot split fixed intervals");
0N/A
0N/A // allocate new interval
0N/A Interval* result = new_split_child();
0N/A
0N/A // split the ranges
0N/A Range* prev = NULL;
0N/A Range* cur = _first;
0N/A while (cur != Range::end() && cur->to() <= split_pos) {
0N/A prev = cur;
0N/A cur = cur->next();
0N/A }
0N/A assert(cur != Range::end(), "split interval after end of last range");
0N/A
0N/A if (cur->from() < split_pos) {
0N/A result->_first = new Range(split_pos, cur->to(), cur->next());
0N/A cur->set_to(split_pos);
0N/A cur->set_next(Range::end());
0N/A
0N/A } else {
0N/A assert(prev != NULL, "split before start of first range");
0N/A result->_first = cur;
0N/A prev->set_next(Range::end());
0N/A }
0N/A result->_current = result->_first;
0N/A _cached_to = -1; // clear cached value
0N/A
0N/A // split list of use positions
0N/A int total_len = _use_pos_and_kinds.length();
0N/A int start_idx = total_len - 2;
0N/A while (start_idx >= 0 && _use_pos_and_kinds.at(start_idx) < split_pos) {
0N/A start_idx -= 2;
0N/A }
0N/A
0N/A intStack new_use_pos_and_kinds(total_len - start_idx);
0N/A int i;
0N/A for (i = start_idx + 2; i < total_len; i++) {
0N/A new_use_pos_and_kinds.append(_use_pos_and_kinds.at(i));
0N/A }
0N/A
0N/A _use_pos_and_kinds.truncate(start_idx + 2);
0N/A result->_use_pos_and_kinds = _use_pos_and_kinds;
0N/A _use_pos_and_kinds = new_use_pos_and_kinds;
0N/A
0N/A#ifdef ASSERT
0N/A assert(_use_pos_and_kinds.length() % 2 == 0, "must have use kind for each use pos");
0N/A assert(result->_use_pos_and_kinds.length() % 2 == 0, "must have use kind for each use pos");
0N/A assert(_use_pos_and_kinds.length() + result->_use_pos_and_kinds.length() == total_len, "missed some entries");
0N/A
0N/A for (i = 0; i < _use_pos_and_kinds.length(); i += 2) {
0N/A assert(_use_pos_and_kinds.at(i) < split_pos, "must be");
0N/A assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
0N/A }
0N/A for (i = 0; i < result->_use_pos_and_kinds.length(); i += 2) {
0N/A assert(result->_use_pos_and_kinds.at(i) >= split_pos, "must be");
0N/A assert(result->_use_pos_and_kinds.at(i + 1) >= firstValidKind && result->_use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
0N/A }
0N/A#endif
0N/A
0N/A return result;
0N/A}
0N/A
0N/A// split this interval at the specified position and return
0N/A// the head as a new interval (the original interval is the tail)
0N/A//
0N/A// Currently, only the first range can be split, and the new interval
0N/A// must not have split positions
0N/AInterval* Interval::split_from_start(int split_pos) {
0N/A assert(LinearScan::is_virtual_interval(this), "cannot split fixed intervals");
0N/A assert(split_pos > from() && split_pos < to(), "can only split inside interval");
0N/A assert(split_pos > _first->from() && split_pos <= _first->to(), "can only split inside first range");
0N/A assert(first_usage(noUse) > split_pos, "can not split when use positions are present");
0N/A
0N/A // allocate new interval
0N/A Interval* result = new_split_child();
0N/A
0N/A // the new created interval has only one range (checked by assertion above),
0N/A // so the splitting of the ranges is very simple
0N/A result->add_range(_first->from(), split_pos);
0N/A
0N/A if (split_pos == _first->to()) {
0N/A assert(_first->next() != Range::end(), "must not be at end");
0N/A _first = _first->next();
0N/A } else {
0N/A _first->set_from(split_pos);
0N/A }
0N/A
0N/A return result;
0N/A}
0N/A
0N/A
0N/A// returns true if the op_id is inside the interval
0N/Abool Interval::covers(int op_id, LIR_OpVisitState::OprMode mode) const {
0N/A Range* cur = _first;
0N/A
0N/A while (cur != Range::end() && cur->to() < op_id) {
0N/A cur = cur->next();
0N/A }
0N/A if (cur != Range::end()) {
0N/A assert(cur->to() != cur->next()->from(), "ranges not separated");
0N/A
0N/A if (mode == LIR_OpVisitState::outputMode) {
0N/A return cur->from() <= op_id && op_id < cur->to();
0N/A } else {
0N/A return cur->from() <= op_id && op_id <= cur->to();
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A// returns true if the interval has any hole between hole_from and hole_to
0N/A// (even if the hole has only the length 1)
0N/Abool Interval::has_hole_between(int hole_from, int hole_to) {
0N/A assert(hole_from < hole_to, "check");
0N/A assert(from() <= hole_from && hole_to <= to(), "index out of interval");
0N/A
0N/A Range* cur = _first;
0N/A while (cur != Range::end()) {
0N/A assert(cur->to() < cur->next()->from(), "no space between ranges");
0N/A
0N/A // hole-range starts before this range -> hole
0N/A if (hole_from < cur->from()) {
0N/A return true;
0N/A
0N/A // hole-range completely inside this range -> no hole
0N/A } else if (hole_to <= cur->to()) {
0N/A return false;
0N/A
0N/A // overlapping of hole-range with this range -> hole
0N/A } else if (hole_from <= cur->to()) {
0N/A return true;
0N/A }
0N/A
0N/A cur = cur->next();
0N/A }
0N/A
0N/A return false;
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/Avoid Interval::print(outputStream* out) const {
0N/A const char* SpillState2Name[] = { "no definition", "no spill store", "one spill store", "store at definition", "start in memory", "no optimization" };
0N/A const char* UseKind2Name[] = { "N", "L", "S", "M" };
0N/A
0N/A const char* type_name;
0N/A LIR_Opr opr = LIR_OprFact::illegal();
0N/A if (reg_num() < LIR_OprDesc::vreg_base) {
0N/A type_name = "fixed";
0N/A // need a temporary operand for fixed intervals because type() cannot be called
0N/A if (assigned_reg() >= pd_first_cpu_reg && assigned_reg() <= pd_last_cpu_reg) {
0N/A opr = LIR_OprFact::single_cpu(assigned_reg());
0N/A } else if (assigned_reg() >= pd_first_fpu_reg && assigned_reg() <= pd_last_fpu_reg) {
0N/A opr = LIR_OprFact::single_fpu(assigned_reg() - pd_first_fpu_reg);
304N/A#ifdef X86
0N/A } else if (assigned_reg() >= pd_first_xmm_reg && assigned_reg() <= pd_last_xmm_reg) {
0N/A opr = LIR_OprFact::single_xmm(assigned_reg() - pd_first_xmm_reg);
0N/A#endif
0N/A } else {
0N/A ShouldNotReachHere();
0N/A }
0N/A } else {
0N/A type_name = type2name(type());
1969N/A if (assigned_reg() != -1 &&
1969N/A (LinearScan::num_physical_regs(type()) == 1 || assigned_regHi() != -1)) {
0N/A opr = LinearScan::calc_operand_for_interval(this);
0N/A }
0N/A }
0N/A
0N/A out->print("%d %s ", reg_num(), type_name);
0N/A if (opr->is_valid()) {
0N/A out->print("\"");
0N/A opr->print(out);
0N/A out->print("\" ");
0N/A }
0N/A out->print("%d %d ", split_parent()->reg_num(), (register_hint(false) != NULL ? register_hint(false)->reg_num() : -1));
0N/A
0N/A // print ranges
0N/A Range* cur = _first;
0N/A while (cur != Range::end()) {
0N/A cur->print(out);
0N/A cur = cur->next();
0N/A assert(cur != NULL, "range list not closed with range sentinel");
0N/A }
0N/A
0N/A // print use positions
0N/A int prev = 0;
0N/A assert(_use_pos_and_kinds.length() % 2 == 0, "must be");
0N/A for (int i =_use_pos_and_kinds.length() - 2; i >= 0; i -= 2) {
0N/A assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
0N/A assert(prev < _use_pos_and_kinds.at(i), "use positions not sorted");
0N/A
0N/A out->print("%d %s ", _use_pos_and_kinds.at(i), UseKind2Name[_use_pos_and_kinds.at(i + 1)]);
0N/A prev = _use_pos_and_kinds.at(i);
0N/A }
0N/A
0N/A out->print(" \"%s\"", SpillState2Name[spill_state()]);
0N/A out->cr();
0N/A}
0N/A#endif
0N/A
0N/A
0N/A
0N/A// **** Implementation of IntervalWalker ****************************
0N/A
0N/AIntervalWalker::IntervalWalker(LinearScan* allocator, Interval* unhandled_fixed_first, Interval* unhandled_any_first)
0N/A : _compilation(allocator->compilation())
0N/A , _allocator(allocator)
0N/A{
0N/A _unhandled_first[fixedKind] = unhandled_fixed_first;
0N/A _unhandled_first[anyKind] = unhandled_any_first;
0N/A _active_first[fixedKind] = Interval::end();
0N/A _inactive_first[fixedKind] = Interval::end();
0N/A _active_first[anyKind] = Interval::end();
0N/A _inactive_first[anyKind] = Interval::end();
0N/A _current_position = -1;
0N/A _current = NULL;
0N/A next_interval();
0N/A}
0N/A
0N/A
0N/A// append interval at top of list
0N/Avoid IntervalWalker::append_unsorted(Interval** list, Interval* interval) {
0N/A interval->set_next(*list); *list = interval;
0N/A}
0N/A
0N/A
0N/A// append interval in order of current range from()
0N/Avoid IntervalWalker::append_sorted(Interval** list, Interval* interval) {
0N/A Interval* prev = NULL;
0N/A Interval* cur = *list;
0N/A while (cur->current_from() < interval->current_from()) {
0N/A prev = cur; cur = cur->next();
0N/A }
0N/A if (prev == NULL) {
0N/A *list = interval;
0N/A } else {
0N/A prev->set_next(interval);
0N/A }
0N/A interval->set_next(cur);
0N/A}
0N/A
0N/Avoid IntervalWalker::append_to_unhandled(Interval** list, Interval* interval) {
0N/A assert(interval->from() >= current()->current_from(), "cannot append new interval before current walk position");
0N/A
0N/A Interval* prev = NULL;
0N/A Interval* cur = *list;
0N/A while (cur->from() < interval->from() || (cur->from() == interval->from() && cur->first_usage(noUse) < interval->first_usage(noUse))) {
0N/A prev = cur; cur = cur->next();
0N/A }
0N/A if (prev == NULL) {
0N/A *list = interval;
0N/A } else {
0N/A prev->set_next(interval);
0N/A }
0N/A interval->set_next(cur);
0N/A}
0N/A
0N/A
0N/Ainline bool IntervalWalker::remove_from_list(Interval** list, Interval* i) {
0N/A while (*list != Interval::end() && *list != i) {
0N/A list = (*list)->next_addr();
0N/A }
0N/A if (*list != Interval::end()) {
0N/A assert(*list == i, "check");
0N/A *list = (*list)->next();
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A}
0N/A
0N/Avoid IntervalWalker::remove_from_list(Interval* i) {
0N/A bool deleted;
0N/A
0N/A if (i->state() == activeState) {
0N/A deleted = remove_from_list(active_first_addr(anyKind), i);
0N/A } else {
0N/A assert(i->state() == inactiveState, "invalid state");
0N/A deleted = remove_from_list(inactive_first_addr(anyKind), i);
0N/A }
0N/A
0N/A assert(deleted, "interval has not been found in list");
0N/A}
0N/A
0N/A
0N/Avoid IntervalWalker::walk_to(IntervalState state, int from) {
0N/A assert (state == activeState || state == inactiveState, "wrong state");
0N/A for_each_interval_kind(kind) {
0N/A Interval** prev = state == activeState ? active_first_addr(kind) : inactive_first_addr(kind);
0N/A Interval* next = *prev;
0N/A while (next->current_from() <= from) {
0N/A Interval* cur = next;
0N/A next = cur->next();
0N/A
0N/A bool range_has_changed = false;
0N/A while (cur->current_to() <= from) {
0N/A cur->next_range();
0N/A range_has_changed = true;
0N/A }
0N/A
0N/A // also handle move from inactive list to active list
0N/A range_has_changed = range_has_changed || (state == inactiveState && cur->current_from() <= from);
0N/A
0N/A if (range_has_changed) {
0N/A // remove cur from list
0N/A *prev = next;
0N/A if (cur->current_at_end()) {
0N/A // move to handled state (not maintained as a list)
0N/A cur->set_state(handledState);
0N/A interval_moved(cur, kind, state, handledState);
0N/A } else if (cur->current_from() <= from){
0N/A // sort into active list
0N/A append_sorted(active_first_addr(kind), cur);
0N/A cur->set_state(activeState);
0N/A if (*prev == cur) {
0N/A assert(state == activeState, "check");
0N/A prev = cur->next_addr();
0N/A }
0N/A interval_moved(cur, kind, state, activeState);
0N/A } else {
0N/A // sort into inactive list
0N/A append_sorted(inactive_first_addr(kind), cur);
0N/A cur->set_state(inactiveState);
0N/A if (*prev == cur) {
0N/A assert(state == inactiveState, "check");
0N/A prev = cur->next_addr();
0N/A }
0N/A interval_moved(cur, kind, state, inactiveState);
0N/A }
0N/A } else {
0N/A prev = cur->next_addr();
0N/A continue;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid IntervalWalker::next_interval() {
0N/A IntervalKind kind;
0N/A Interval* any = _unhandled_first[anyKind];
0N/A Interval* fixed = _unhandled_first[fixedKind];
0N/A
0N/A if (any != Interval::end()) {
0N/A // intervals may start at same position -> prefer fixed interval
0N/A kind = fixed != Interval::end() && fixed->from() <= any->from() ? fixedKind : anyKind;
0N/A
0N/A assert (kind == fixedKind && fixed->from() <= any->from() ||
0N/A kind == anyKind && any->from() <= fixed->from(), "wrong interval!!!");
0N/A assert(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");
0N/A
0N/A } else if (fixed != Interval::end()) {
0N/A kind = fixedKind;
0N/A } else {
0N/A _current = NULL; return;
0N/A }
0N/A _current_kind = kind;
0N/A _current = _unhandled_first[kind];
0N/A _unhandled_first[kind] = _current->next();
0N/A _current->set_next(Interval::end());
0N/A _current->rewind_range();
0N/A}
0N/A
0N/A
0N/Avoid IntervalWalker::walk_to(int lir_op_id) {
0N/A assert(_current_position <= lir_op_id, "can not walk backwards");
0N/A while (current() != NULL) {
0N/A bool is_active = current()->from() <= lir_op_id;
0N/A int id = is_active ? current()->from() : lir_op_id;
0N/A
0N/A TRACE_LINEAR_SCAN(2, if (_current_position < id) { tty->cr(); tty->print_cr("walk_to(%d) **************************************************************", id); })
0N/A
0N/A // set _current_position prior to call of walk_to
0N/A _current_position = id;
0N/A
0N/A // call walk_to even if _current_position == id
0N/A walk_to(activeState, id);
0N/A walk_to(inactiveState, id);
0N/A
0N/A if (is_active) {
0N/A current()->set_state(activeState);
0N/A if (activate_current()) {
0N/A append_sorted(active_first_addr(current_kind()), current());
0N/A interval_moved(current(), current_kind(), unhandledState, activeState);
0N/A }
0N/A
0N/A next_interval();
0N/A } else {
0N/A return;
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid IntervalWalker::interval_moved(Interval* interval, IntervalKind kind, IntervalState from, IntervalState to) {
0N/A#ifndef PRODUCT
0N/A if (TraceLinearScanLevel >= 4) {
0N/A #define print_state(state) \
0N/A switch(state) {\
0N/A case unhandledState: tty->print("unhandled"); break;\
0N/A case activeState: tty->print("active"); break;\
0N/A case inactiveState: tty->print("inactive"); break;\
0N/A case handledState: tty->print("handled"); break;\
0N/A default: ShouldNotReachHere(); \
0N/A }
0N/A
0N/A print_state(from); tty->print(" to "); print_state(to);
0N/A tty->fill_to(23);
0N/A interval->print();
0N/A
0N/A #undef print_state
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A
0N/A
0N/A// **** Implementation of LinearScanWalker **************************
0N/A
0N/ALinearScanWalker::LinearScanWalker(LinearScan* allocator, Interval* unhandled_fixed_first, Interval* unhandled_any_first)
0N/A : IntervalWalker(allocator, unhandled_fixed_first, unhandled_any_first)
0N/A , _move_resolver(allocator)
0N/A{
0N/A for (int i = 0; i < LinearScan::nof_regs; i++) {
0N/A _spill_intervals[i] = new IntervalList(2);
0N/A }
0N/A}
0N/A
0N/A
0N/Ainline void LinearScanWalker::init_use_lists(bool only_process_use_pos) {
0N/A for (int i = _first_reg; i <= _last_reg; i++) {
0N/A _use_pos[i] = max_jint;
0N/A
0N/A if (!only_process_use_pos) {
0N/A _block_pos[i] = max_jint;
0N/A _spill_intervals[i]->clear();
0N/A }
0N/A }
0N/A}
0N/A
0N/Ainline void LinearScanWalker::exclude_from_use(int reg) {
0N/A assert(reg < LinearScan::nof_regs, "interval must have a register assigned (stack slots not allowed)");
0N/A if (reg >= _first_reg && reg <= _last_reg) {
0N/A _use_pos[reg] = 0;
0N/A }
0N/A}
0N/Ainline void LinearScanWalker::exclude_from_use(Interval* i) {
0N/A assert(i->assigned_reg() != any_reg, "interval has no register assigned");
0N/A
0N/A exclude_from_use(i->assigned_reg());
0N/A exclude_from_use(i->assigned_regHi());
0N/A}
0N/A
0N/Ainline void LinearScanWalker::set_use_pos(int reg, Interval* i, int use_pos, bool only_process_use_pos) {
0N/A assert(use_pos != 0, "must use exclude_from_use to set use_pos to 0");
0N/A
0N/A if (reg >= _first_reg && reg <= _last_reg) {
0N/A if (_use_pos[reg] > use_pos) {
0N/A _use_pos[reg] = use_pos;
0N/A }
0N/A if (!only_process_use_pos) {
0N/A _spill_intervals[reg]->append(i);
0N/A }
0N/A }
0N/A}
0N/Ainline void LinearScanWalker::set_use_pos(Interval* i, int use_pos, bool only_process_use_pos) {
0N/A assert(i->assigned_reg() != any_reg, "interval has no register assigned");
0N/A if (use_pos != -1) {
0N/A set_use_pos(i->assigned_reg(), i, use_pos, only_process_use_pos);
0N/A set_use_pos(i->assigned_regHi(), i, use_pos, only_process_use_pos);
0N/A }
0N/A}
0N/A
0N/Ainline void LinearScanWalker::set_block_pos(int reg, Interval* i, int block_pos) {
0N/A if (reg >= _first_reg && reg <= _last_reg) {
0N/A if (_block_pos[reg] > block_pos) {
0N/A _block_pos[reg] = block_pos;
0N/A }
0N/A if (_use_pos[reg] > block_pos) {
0N/A _use_pos[reg] = block_pos;
0N/A }
0N/A }
0N/A}
0N/Ainline void LinearScanWalker::set_block_pos(Interval* i, int block_pos) {
0N/A assert(i->assigned_reg() != any_reg, "interval has no register assigned");
0N/A if (block_pos != -1) {
0N/A set_block_pos(i->assigned_reg(), i, block_pos);
0N/A set_block_pos(i->assigned_regHi(), i, block_pos);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScanWalker::free_exclude_active_fixed() {
0N/A Interval* list = active_first(fixedKind);
0N/A while (list != Interval::end()) {
0N/A assert(list->assigned_reg() < LinearScan::nof_regs, "active interval must have a register assigned");
0N/A exclude_from_use(list);
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::free_exclude_active_any() {
0N/A Interval* list = active_first(anyKind);
0N/A while (list != Interval::end()) {
0N/A exclude_from_use(list);
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::free_collect_inactive_fixed(Interval* cur) {
0N/A Interval* list = inactive_first(fixedKind);
0N/A while (list != Interval::end()) {
0N/A if (cur->to() <= list->current_from()) {
0N/A assert(list->current_intersects_at(cur) == -1, "must not intersect");
0N/A set_use_pos(list, list->current_from(), true);
0N/A } else {
0N/A set_use_pos(list, list->current_intersects_at(cur), true);
0N/A }
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::free_collect_inactive_any(Interval* cur) {
0N/A Interval* list = inactive_first(anyKind);
0N/A while (list != Interval::end()) {
0N/A set_use_pos(list, list->current_intersects_at(cur), true);
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::free_collect_unhandled(IntervalKind kind, Interval* cur) {
0N/A Interval* list = unhandled_first(kind);
0N/A while (list != Interval::end()) {
0N/A set_use_pos(list, list->intersects_at(cur), true);
0N/A if (kind == fixedKind && cur->to() <= list->from()) {
0N/A set_use_pos(list, list->from(), true);
0N/A }
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::spill_exclude_active_fixed() {
0N/A Interval* list = active_first(fixedKind);
0N/A while (list != Interval::end()) {
0N/A exclude_from_use(list);
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::spill_block_unhandled_fixed(Interval* cur) {
0N/A Interval* list = unhandled_first(fixedKind);
0N/A while (list != Interval::end()) {
0N/A set_block_pos(list, list->intersects_at(cur));
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::spill_block_inactive_fixed(Interval* cur) {
0N/A Interval* list = inactive_first(fixedKind);
0N/A while (list != Interval::end()) {
0N/A if (cur->to() > list->current_from()) {
0N/A set_block_pos(list, list->current_intersects_at(cur));
0N/A } else {
0N/A assert(list->current_intersects_at(cur) == -1, "invalid optimization: intervals intersect");
0N/A }
0N/A
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::spill_collect_active_any() {
0N/A Interval* list = active_first(anyKind);
0N/A while (list != Interval::end()) {
0N/A set_use_pos(list, MIN2(list->next_usage(loopEndMarker, _current_position), list->to()), false);
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanWalker::spill_collect_inactive_any(Interval* cur) {
0N/A Interval* list = inactive_first(anyKind);
0N/A while (list != Interval::end()) {
0N/A if (list->current_intersects(cur)) {
0N/A set_use_pos(list, MIN2(list->next_usage(loopEndMarker, _current_position), list->to()), false);
0N/A }
0N/A list = list->next();
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScanWalker::insert_move(int op_id, Interval* src_it, Interval* dst_it) {
0N/A // output all moves here. When source and target are equal, the move is
0N/A // optimized away later in assign_reg_nums
0N/A
0N/A op_id = (op_id + 1) & ~1;
0N/A BlockBegin* op_block = allocator()->block_of_op_with_id(op_id);
0N/A assert(op_id > 0 && allocator()->block_of_op_with_id(op_id - 2) == op_block, "cannot insert move at block boundary");
0N/A
0N/A // calculate index of instruction inside instruction list of current block
0N/A // the minimal index (for a block with no spill moves) can be calculated because the
0N/A // numbering of instructions is known.
0N/A // When the block already contains spill moves, the index must be increased until the
0N/A // correct index is reached.
0N/A LIR_OpList* list = op_block->lir()->instructions_list();
0N/A int index = (op_id - list->at(0)->id()) / 2;
0N/A assert(list->at(index)->id() <= op_id, "error in calculation");
0N/A
0N/A while (list->at(index)->id() != op_id) {
0N/A index++;
0N/A assert(0 <= index && index < list->length(), "index out of bounds");
0N/A }
0N/A assert(1 <= index && index < list->length(), "index out of bounds");
0N/A assert(list->at(index)->id() == op_id, "error in calculation");
0N/A
0N/A // insert new instruction before instruction at position index
0N/A _move_resolver.move_insert_position(op_block->lir(), index - 1);
0N/A _move_resolver.add_mapping(src_it, dst_it);
0N/A}
0N/A
0N/A
0N/Aint LinearScanWalker::find_optimal_split_pos(BlockBegin* min_block, BlockBegin* max_block, int max_split_pos) {
0N/A int from_block_nr = min_block->linear_scan_number();
0N/A int to_block_nr = max_block->linear_scan_number();
0N/A
0N/A assert(0 <= from_block_nr && from_block_nr < block_count(), "out of range");
0N/A assert(0 <= to_block_nr && to_block_nr < block_count(), "out of range");
0N/A assert(from_block_nr < to_block_nr, "must cross block boundary");
0N/A
0N/A // Try to split at end of max_block. If this would be after
0N/A // max_split_pos, then use the begin of max_block
0N/A int optimal_split_pos = max_block->last_lir_instruction_id() + 2;
0N/A if (optimal_split_pos > max_split_pos) {
0N/A optimal_split_pos = max_block->first_lir_instruction_id();
0N/A }
0N/A
0N/A int min_loop_depth = max_block->loop_depth();
0N/A for (int i = to_block_nr - 1; i >= from_block_nr; i--) {
0N/A BlockBegin* cur = block_at(i);
0N/A
0N/A if (cur->loop_depth() < min_loop_depth) {
0N/A // block with lower loop-depth found -> split at the end of this block
0N/A min_loop_depth = cur->loop_depth();
0N/A optimal_split_pos = cur->last_lir_instruction_id() + 2;
0N/A }
0N/A }
0N/A assert(optimal_split_pos > allocator()->max_lir_op_id() || allocator()->is_block_begin(optimal_split_pos), "algorithm must move split pos to block boundary");
0N/A
0N/A return optimal_split_pos;
0N/A}
0N/A
0N/A
0N/Aint LinearScanWalker::find_optimal_split_pos(Interval* it, int min_split_pos, int max_split_pos, bool do_loop_optimization) {
0N/A int optimal_split_pos = -1;
0N/A if (min_split_pos == max_split_pos) {
0N/A // trivial case, no optimization of split position possible
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" min-pos and max-pos are equal, no optimization possible"));
0N/A optimal_split_pos = min_split_pos;
0N/A
0N/A } else {
0N/A assert(min_split_pos < max_split_pos, "must be true then");
0N/A assert(min_split_pos > 0, "cannot access min_split_pos - 1 otherwise");
0N/A
0N/A // reason for using min_split_pos - 1: when the minimal split pos is exactly at the
0N/A // beginning of a block, then min_split_pos is also a possible split position.
0N/A // Use the block before as min_block, because then min_block->last_lir_instruction_id() + 2 == min_split_pos
0N/A BlockBegin* min_block = allocator()->block_of_op_with_id(min_split_pos - 1);
0N/A
0N/A // reason for using max_split_pos - 1: otherwise there would be an assertion failure
0N/A // when an interval ends at the end of the last block of the method
0N/A // (in this case, max_split_pos == allocator()->max_lir_op_id() + 2, and there is no
0N/A // block at this op_id)
0N/A BlockBegin* max_block = allocator()->block_of_op_with_id(max_split_pos - 1);
0N/A
0N/A assert(min_block->linear_scan_number() <= max_block->linear_scan_number(), "invalid order");
0N/A if (min_block == max_block) {
0N/A // split position cannot be moved to block boundary, so split as late as possible
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" cannot move split pos to block boundary because min_pos and max_pos are in same block"));
0N/A optimal_split_pos = max_split_pos;
0N/A
0N/A } else if (it->has_hole_between(max_split_pos - 1, max_split_pos) && !allocator()->is_block_begin(max_split_pos)) {
0N/A // Do not move split position if the interval has a hole before max_split_pos.
0N/A // Intervals resulting from Phi-Functions have more than one definition (marked
0N/A // as mustHaveRegister) with a hole before each definition. When the register is needed
0N/A // for the second definition, an earlier reloading is unnecessary.
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" interval has hole just before max_split_pos, so splitting at max_split_pos"));
0N/A optimal_split_pos = max_split_pos;
0N/A
0N/A } else {
0N/A // seach optimal block boundary between min_split_pos and max_split_pos
0N/A TRACE_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()));
0N/A
0N/A if (do_loop_optimization) {
0N/A // Loop optimization: if a loop-end marker is found between min- and max-position,
0N/A // then split before this loop
0N/A int loop_end_pos = it->next_usage_exact(loopEndMarker, min_block->last_lir_instruction_id() + 2);
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" loop optimization: loop end found at pos %d", loop_end_pos));
0N/A
0N/A assert(loop_end_pos > min_split_pos, "invalid order");
0N/A if (loop_end_pos < max_split_pos) {
0N/A // loop-end marker found between min- and max-position
0N/A // if it is not the end marker for the same loop as the min-position, then move
0N/A // the max-position to this loop block.
0N/A // Desired result: uses tagged as shouldHaveRegister inside a loop cause a reloading
0N/A // of the interval (normally, only mustHaveRegister causes a reloading)
0N/A BlockBegin* loop_block = allocator()->block_of_op_with_id(loop_end_pos);
0N/A
0N/A TRACE_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()));
0N/A assert(loop_block != min_block, "loop_block and min_block must be different because block boundary is needed between");
0N/A
0N/A optimal_split_pos = find_optimal_split_pos(min_block, loop_block, loop_block->last_lir_instruction_id() + 2);
0N/A if (optimal_split_pos == loop_block->last_lir_instruction_id() + 2) {
0N/A optimal_split_pos = -1;
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" loop optimization not necessary"));
0N/A } else {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" loop optimization successful"));
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (optimal_split_pos == -1) {
0N/A // not calculated by loop optimization
0N/A optimal_split_pos = find_optimal_split_pos(min_block, max_block, max_split_pos);
0N/A }
0N/A }
0N/A }
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" optimal split position: %d", optimal_split_pos));
0N/A
0N/A return optimal_split_pos;
0N/A}
0N/A
0N/A
0N/A/*
0N/A split an interval at the optimal position between min_split_pos and
0N/A max_split_pos in two parts:
0N/A 1) the left part has already a location assigned
0N/A 2) the right part is sorted into to the unhandled-list
0N/A*/
0N/Avoid LinearScanWalker::split_before_usage(Interval* it, int min_split_pos, int max_split_pos) {
0N/A TRACE_LINEAR_SCAN(2, tty->print ("----- splitting interval: "); it->print());
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr(" between %d and %d", min_split_pos, max_split_pos));
0N/A
0N/A assert(it->from() < min_split_pos, "cannot split at start of interval");
0N/A assert(current_position() < min_split_pos, "cannot split before current position");
0N/A assert(min_split_pos <= max_split_pos, "invalid order");
0N/A assert(max_split_pos <= it->to(), "cannot split after end of interval");
0N/A
0N/A int optimal_split_pos = find_optimal_split_pos(it, min_split_pos, max_split_pos, true);
0N/A
0N/A assert(min_split_pos <= optimal_split_pos && optimal_split_pos <= max_split_pos, "out of range");
0N/A assert(optimal_split_pos <= it->to(), "cannot split after end of interval");
0N/A assert(optimal_split_pos > it->from(), "cannot split at start of interval");
0N/A
0N/A if (optimal_split_pos == it->to() && it->next_usage(mustHaveRegister, min_split_pos) == max_jint) {
0N/A // the split position would be just before the end of the interval
0N/A // -> no split at all necessary
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" no split necessary because optimal split position is at end of interval"));
0N/A return;
0N/A }
0N/A
0N/A // must calculate this before the actual split is performed and before split position is moved to odd op_id
0N/A bool move_necessary = !allocator()->is_block_begin(optimal_split_pos) && !it->has_hole_between(optimal_split_pos - 1, optimal_split_pos);
0N/A
0N/A if (!allocator()->is_block_begin(optimal_split_pos)) {
0N/A // move position before actual instruction (odd op_id)
0N/A optimal_split_pos = (optimal_split_pos - 1) | 1;
0N/A }
0N/A
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" splitting at position %d", optimal_split_pos));
0N/A assert(allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 1), "split pos must be odd when not on block boundary");
0N/A assert(!allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 0), "split pos must be even on block boundary");
0N/A
0N/A Interval* split_part = it->split(optimal_split_pos);
0N/A
0N/A allocator()->append_interval(split_part);
0N/A allocator()->copy_register_flags(it, split_part);
0N/A split_part->set_insert_move_when_activated(move_necessary);
0N/A append_to_unhandled(unhandled_first_addr(anyKind), split_part);
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr(" split interval in two parts (insert_move_when_activated: %d)", move_necessary));
0N/A TRACE_LINEAR_SCAN(2, tty->print (" "); it->print());
0N/A TRACE_LINEAR_SCAN(2, tty->print (" "); split_part->print());
0N/A}
0N/A
0N/A/*
0N/A split an interval at the optimal position between min_split_pos and
0N/A max_split_pos in two parts:
0N/A 1) the left part has already a location assigned
0N/A 2) the right part is always on the stack and therefore ignored in further processing
0N/A*/
0N/Avoid LinearScanWalker::split_for_spilling(Interval* it) {
0N/A // calculate allowed range of splitting position
0N/A int max_split_pos = current_position();
0N/A int min_split_pos = MAX2(it->previous_usage(shouldHaveRegister, max_split_pos) + 1, it->from());
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print ("----- splitting and spilling interval: "); it->print());
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr(" between %d and %d", min_split_pos, max_split_pos));
0N/A
0N/A assert(it->state() == activeState, "why spill interval that is not active?");
0N/A assert(it->from() <= min_split_pos, "cannot split before start of interval");
0N/A assert(min_split_pos <= max_split_pos, "invalid order");
0N/A assert(max_split_pos < it->to(), "cannot split at end end of interval");
0N/A assert(current_position() < it->to(), "interval must not end before current position");
0N/A
0N/A if (min_split_pos == it->from()) {
0N/A // the whole interval is never used, so spill it entirely to memory
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr(" spilling entire interval because split pos is at beginning of interval"));
0N/A assert(it->first_usage(shouldHaveRegister) > current_position(), "interval must not have use position before current_position");
0N/A
0N/A allocator()->assign_spill_slot(it);
0N/A allocator()->change_spill_state(it, min_split_pos);
0N/A
0N/A // Also kick parent intervals out of register to memory when they have no use
0N/A // position. This avoids short interval in register surrounded by intervals in
0N/A // memory -> avoid useless moves from memory to register and back
0N/A Interval* parent = it;
0N/A while (parent != NULL && parent->is_split_child()) {
0N/A parent = parent->split_child_before_op_id(parent->from());
0N/A
0N/A if (parent->assigned_reg() < LinearScan::nof_regs) {
0N/A if (parent->first_usage(shouldHaveRegister) == max_jint) {
0N/A // parent is never used, so kick it out of its assigned register
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" kicking out interval %d out of its register because it is never used", parent->reg_num()));
0N/A allocator()->assign_spill_slot(parent);
0N/A } else {
0N/A // do not go further back because the register is actually used by the interval
0N/A parent = NULL;
0N/A }
0N/A }
0N/A }
0N/A
0N/A } else {
0N/A // search optimal split pos, split interval and spill only the right hand part
0N/A int optimal_split_pos = find_optimal_split_pos(it, min_split_pos, max_split_pos, false);
0N/A
0N/A assert(min_split_pos <= optimal_split_pos && optimal_split_pos <= max_split_pos, "out of range");
0N/A assert(optimal_split_pos < it->to(), "cannot split at end of interval");
0N/A assert(optimal_split_pos >= it->from(), "cannot split before start of interval");
0N/A
0N/A if (!allocator()->is_block_begin(optimal_split_pos)) {
0N/A // move position before actual instruction (odd op_id)
0N/A optimal_split_pos = (optimal_split_pos - 1) | 1;
0N/A }
0N/A
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" splitting at position %d", optimal_split_pos));
0N/A assert(allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 1), "split pos must be odd when not on block boundary");
0N/A assert(!allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 0), "split pos must be even on block boundary");
0N/A
0N/A Interval* spilled_part = it->split(optimal_split_pos);
0N/A allocator()->append_interval(spilled_part);
0N/A allocator()->assign_spill_slot(spilled_part);
0N/A allocator()->change_spill_state(spilled_part, optimal_split_pos);
0N/A
0N/A if (!allocator()->is_block_begin(optimal_split_pos)) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" inserting move from interval %d to %d", it->reg_num(), spilled_part->reg_num()));
0N/A insert_move(optimal_split_pos, it, spilled_part);
0N/A }
0N/A
0N/A // the current_split_child is needed later when moves are inserted for reloading
0N/A assert(spilled_part->current_split_child() == it, "overwriting wrong current_split_child");
0N/A spilled_part->make_current_split_child();
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr(" split interval in two parts"));
0N/A TRACE_LINEAR_SCAN(2, tty->print (" "); it->print());
0N/A TRACE_LINEAR_SCAN(2, tty->print (" "); spilled_part->print());
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid LinearScanWalker::split_stack_interval(Interval* it) {
0N/A int min_split_pos = current_position() + 1;
0N/A int max_split_pos = MIN2(it->first_usage(shouldHaveRegister), it->to());
0N/A
0N/A split_before_usage(it, min_split_pos, max_split_pos);
0N/A}
0N/A
0N/Avoid LinearScanWalker::split_when_partial_register_available(Interval* it, int register_available_until) {
0N/A int min_split_pos = MAX2(it->previous_usage(shouldHaveRegister, register_available_until), it->from() + 1);
0N/A int max_split_pos = register_available_until;
0N/A
0N/A split_before_usage(it, min_split_pos, max_split_pos);
0N/A}
0N/A
0N/Avoid LinearScanWalker::split_and_spill_interval(Interval* it) {
0N/A assert(it->state() == activeState || it->state() == inactiveState, "other states not allowed");
0N/A
0N/A int current_pos = current_position();
0N/A if (it->state() == inactiveState) {
0N/A // the interval is currently inactive, so no spill slot is needed for now.
0N/A // when the split part is activated, the interval has a new chance to get a register,
0N/A // so in the best case no stack slot is necessary
0N/A assert(it->has_hole_between(current_pos - 1, current_pos + 1), "interval can not be inactive otherwise");
0N/A split_before_usage(it, current_pos + 1, current_pos + 1);
0N/A
0N/A } else {
0N/A // search the position where the interval must have a register and split
0N/A // at the optimal position before.
0N/A // The new created part is added to the unhandled list and will get a register
0N/A // when it is activated
0N/A int min_split_pos = current_pos + 1;
0N/A int max_split_pos = MIN2(it->next_usage(mustHaveRegister, min_split_pos), it->to());
0N/A
0N/A split_before_usage(it, min_split_pos, max_split_pos);
0N/A
0N/A assert(it->next_usage(mustHaveRegister, current_pos) == max_jint, "the remaining part is spilled to stack and therefore has no register");
0N/A split_for_spilling(it);
0N/A }
0N/A}
0N/A
0N/A
0N/Aint LinearScanWalker::find_free_reg(int reg_needed_until, int interval_to, int hint_reg, int ignore_reg, bool* need_split) {
0N/A int min_full_reg = any_reg;
0N/A int max_partial_reg = any_reg;
0N/A
0N/A for (int i = _first_reg; i <= _last_reg; i++) {
0N/A if (i == ignore_reg) {
0N/A // this register must be ignored
0N/A
0N/A } else if (_use_pos[i] >= interval_to) {
0N/A // this register is free for the full interval
0N/A if (min_full_reg == any_reg || i == hint_reg || (_use_pos[i] < _use_pos[min_full_reg] && min_full_reg != hint_reg)) {
0N/A min_full_reg = i;
0N/A }
0N/A } else if (_use_pos[i] > reg_needed_until) {
0N/A // this register is at least free until reg_needed_until
0N/A if (max_partial_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_partial_reg] && max_partial_reg != hint_reg)) {
0N/A max_partial_reg = i;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (min_full_reg != any_reg) {
0N/A return min_full_reg;
0N/A } else if (max_partial_reg != any_reg) {
0N/A *need_split = true;
0N/A return max_partial_reg;
0N/A } else {
0N/A return any_reg;
0N/A }
0N/A}
0N/A
0N/Aint LinearScanWalker::find_free_double_reg(int reg_needed_until, int interval_to, int hint_reg, bool* need_split) {
0N/A assert((_last_reg - _first_reg + 1) % 2 == 0, "adjust algorithm");
0N/A
0N/A int min_full_reg = any_reg;
0N/A int max_partial_reg = any_reg;
0N/A
0N/A for (int i = _first_reg; i < _last_reg; i+=2) {
0N/A if (_use_pos[i] >= interval_to && _use_pos[i + 1] >= interval_to) {
0N/A // this register is free for the full interval
0N/A if (min_full_reg == any_reg || i == hint_reg || (_use_pos[i] < _use_pos[min_full_reg] && min_full_reg != hint_reg)) {
0N/A min_full_reg = i;
0N/A }
0N/A } else if (_use_pos[i] > reg_needed_until && _use_pos[i + 1] > reg_needed_until) {
0N/A // this register is at least free until reg_needed_until
0N/A if (max_partial_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_partial_reg] && max_partial_reg != hint_reg)) {
0N/A max_partial_reg = i;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (min_full_reg != any_reg) {
0N/A return min_full_reg;
0N/A } else if (max_partial_reg != any_reg) {
0N/A *need_split = true;
0N/A return max_partial_reg;
0N/A } else {
0N/A return any_reg;
0N/A }
0N/A}
0N/A
0N/A
0N/Abool LinearScanWalker::alloc_free_reg(Interval* cur) {
0N/A TRACE_LINEAR_SCAN(2, tty->print("trying to find free register for "); cur->print());
0N/A
0N/A init_use_lists(true);
0N/A free_exclude_active_fixed();
0N/A free_exclude_active_any();
0N/A free_collect_inactive_fixed(cur);
0N/A free_collect_inactive_any(cur);
0N/A// free_collect_unhandled(fixedKind, cur);
0N/A assert(unhandled_first(fixedKind) == Interval::end(), "must not have unhandled fixed intervals because all fixed intervals have a use at position 0");
0N/A
0N/A // _use_pos contains the start of the next interval that has this register assigned
0N/A // (either as a fixed register or a normal allocated register in the past)
0N/A // only intervals overlapping with cur are processed, non-overlapping invervals can be ignored safely
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" state of registers:"));
0N/A TRACE_LINEAR_SCAN(4, for (int i = _first_reg; i <= _last_reg; i++) tty->print_cr(" reg %d: use_pos: %d", i, _use_pos[i]));
0N/A
0N/A int hint_reg, hint_regHi;
0N/A Interval* register_hint = cur->register_hint();
0N/A if (register_hint != NULL) {
0N/A hint_reg = register_hint->assigned_reg();
0N/A hint_regHi = register_hint->assigned_regHi();
0N/A
0N/A if (allocator()->is_precolored_cpu_interval(register_hint)) {
0N/A assert(hint_reg != any_reg && hint_regHi == any_reg, "must be for fixed intervals");
0N/A hint_regHi = hint_reg + 1; // connect e.g. eax-edx
0N/A }
0N/A TRACE_LINEAR_SCAN(4, tty->print(" hint registers %d, %d from interval ", hint_reg, hint_regHi); register_hint->print());
0N/A
0N/A } else {
0N/A hint_reg = any_reg;
0N/A hint_regHi = any_reg;
0N/A }
0N/A assert(hint_reg == any_reg || hint_reg != hint_regHi, "hint reg and regHi equal");
0N/A assert(cur->assigned_reg() == any_reg && cur->assigned_regHi() == any_reg, "register already assigned to interval");
0N/A
0N/A // the register must be free at least until this position
0N/A int reg_needed_until = cur->from() + 1;
0N/A int interval_to = cur->to();
0N/A
0N/A bool need_split = false;
0N/A int split_pos = -1;
0N/A int reg = any_reg;
0N/A int regHi = any_reg;
0N/A
0N/A if (_adjacent_regs) {
0N/A reg = find_free_double_reg(reg_needed_until, interval_to, hint_reg, &need_split);
0N/A regHi = reg + 1;
0N/A if (reg == any_reg) {
0N/A return false;
0N/A }
0N/A split_pos = MIN2(_use_pos[reg], _use_pos[regHi]);
0N/A
0N/A } else {
0N/A reg = find_free_reg(reg_needed_until, interval_to, hint_reg, any_reg, &need_split);
0N/A if (reg == any_reg) {
0N/A return false;
0N/A }
0N/A split_pos = _use_pos[reg];
0N/A
0N/A if (_num_phys_regs == 2) {
0N/A regHi = find_free_reg(reg_needed_until, interval_to, hint_regHi, reg, &need_split);
0N/A
0N/A if (_use_pos[reg] < interval_to && regHi == any_reg) {
0N/A // do not split interval if only one register can be assigned until the split pos
0N/A // (when one register is found for the whole interval, split&spill is only
0N/A // performed for the hi register)
0N/A return false;
0N/A
0N/A } else if (regHi != any_reg) {
0N/A split_pos = MIN2(split_pos, _use_pos[regHi]);
0N/A
0N/A // sort register numbers to prevent e.g. a move from eax,ebx to ebx,eax
0N/A if (reg > regHi) {
0N/A int temp = reg;
0N/A reg = regHi;
0N/A regHi = temp;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A cur->assign_reg(reg, regHi);
0N/A TRACE_LINEAR_SCAN(2, tty->print_cr("selected register %d, %d", reg, regHi));
0N/A
0N/A assert(split_pos > 0, "invalid split_pos");
0N/A if (need_split) {
0N/A // register not available for full interval, so split it
0N/A split_when_partial_register_available(cur, split_pos);
0N/A }
0N/A
0N/A // only return true if interval is completely assigned
0N/A return _num_phys_regs == 1 || regHi != any_reg;
0N/A}
0N/A
0N/A
0N/Aint LinearScanWalker::find_locked_reg(int reg_needed_until, int interval_to, int hint_reg, int ignore_reg, bool* need_split) {
0N/A int max_reg = any_reg;
0N/A
0N/A for (int i = _first_reg; i <= _last_reg; i++) {
0N/A if (i == ignore_reg) {
0N/A // this register must be ignored
0N/A
0N/A } else if (_use_pos[i] > reg_needed_until) {
0N/A if (max_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_reg] && max_reg != hint_reg)) {
0N/A max_reg = i;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (max_reg != any_reg && _block_pos[max_reg] <= interval_to) {
0N/A *need_split = true;
0N/A }
0N/A
0N/A return max_reg;
0N/A}
0N/A
0N/Aint LinearScanWalker::find_locked_double_reg(int reg_needed_until, int interval_to, int hint_reg, bool* need_split) {
0N/A assert((_last_reg - _first_reg + 1) % 2 == 0, "adjust algorithm");
0N/A
0N/A int max_reg = any_reg;
0N/A
0N/A for (int i = _first_reg; i < _last_reg; i+=2) {
0N/A if (_use_pos[i] > reg_needed_until && _use_pos[i + 1] > reg_needed_until) {
0N/A if (max_reg == any_reg || _use_pos[i] > _use_pos[max_reg]) {
0N/A max_reg = i;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (_block_pos[max_reg] <= interval_to || _block_pos[max_reg + 1] <= interval_to) {
0N/A *need_split = true;
0N/A }
0N/A
0N/A return max_reg;
0N/A}
0N/A
0N/Avoid LinearScanWalker::split_and_spill_intersecting_intervals(int reg, int regHi) {
0N/A assert(reg != any_reg, "no register assigned");
0N/A
0N/A for (int i = 0; i < _spill_intervals[reg]->length(); i++) {
0N/A Interval* it = _spill_intervals[reg]->at(i);
0N/A remove_from_list(it);
0N/A split_and_spill_interval(it);
0N/A }
0N/A
0N/A if (regHi != any_reg) {
0N/A IntervalList* processed = _spill_intervals[reg];
0N/A for (int i = 0; i < _spill_intervals[regHi]->length(); i++) {
0N/A Interval* it = _spill_intervals[regHi]->at(i);
0N/A if (processed->index_of(it) == -1) {
0N/A remove_from_list(it);
0N/A split_and_spill_interval(it);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Split an Interval and spill it to memory so that cur can be placed in a register
0N/Avoid LinearScanWalker::alloc_locked_reg(Interval* cur) {
0N/A TRACE_LINEAR_SCAN(2, tty->print("need to split and spill to get register for "); cur->print());
0N/A
0N/A // collect current usage of registers
0N/A init_use_lists(false);
0N/A spill_exclude_active_fixed();
0N/A// spill_block_unhandled_fixed(cur);
0N/A assert(unhandled_first(fixedKind) == Interval::end(), "must not have unhandled fixed intervals because all fixed intervals have a use at position 0");
0N/A spill_block_inactive_fixed(cur);
0N/A spill_collect_active_any();
0N/A spill_collect_inactive_any(cur);
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceLinearScanLevel >= 4) {
0N/A tty->print_cr(" state of registers:");
0N/A for (int i = _first_reg; i <= _last_reg; i++) {
0N/A tty->print(" reg %d: use_pos: %d, block_pos: %d, intervals: ", i, _use_pos[i], _block_pos[i]);
0N/A for (int j = 0; j < _spill_intervals[i]->length(); j++) {
0N/A tty->print("%d ", _spill_intervals[i]->at(j)->reg_num());
0N/A }
0N/A tty->cr();
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A // the register must be free at least until this position
0N/A int reg_needed_until = MIN2(cur->first_usage(mustHaveRegister), cur->from() + 1);
0N/A int interval_to = cur->to();
0N/A assert (reg_needed_until > 0 && reg_needed_until < max_jint, "interval has no use");
0N/A
0N/A int split_pos = 0;
0N/A int use_pos = 0;
0N/A bool need_split = false;
0N/A int reg, regHi;
0N/A
0N/A if (_adjacent_regs) {
0N/A reg = find_locked_double_reg(reg_needed_until, interval_to, any_reg, &need_split);
0N/A regHi = reg + 1;
0N/A
0N/A if (reg != any_reg) {
0N/A use_pos = MIN2(_use_pos[reg], _use_pos[regHi]);
0N/A split_pos = MIN2(_block_pos[reg], _block_pos[regHi]);
0N/A }
0N/A } else {
0N/A reg = find_locked_reg(reg_needed_until, interval_to, any_reg, cur->assigned_reg(), &need_split);
0N/A regHi = any_reg;
0N/A
0N/A if (reg != any_reg) {
0N/A use_pos = _use_pos[reg];
0N/A split_pos = _block_pos[reg];
0N/A
0N/A if (_num_phys_regs == 2) {
0N/A if (cur->assigned_reg() != any_reg) {
0N/A regHi = reg;
0N/A reg = cur->assigned_reg();
0N/A } else {
0N/A regHi = find_locked_reg(reg_needed_until, interval_to, any_reg, reg, &need_split);
0N/A if (regHi != any_reg) {
0N/A use_pos = MIN2(use_pos, _use_pos[regHi]);
0N/A split_pos = MIN2(split_pos, _block_pos[regHi]);
0N/A }
0N/A }
0N/A
0N/A if (regHi != any_reg && reg > regHi) {
0N/A // sort register numbers to prevent e.g. a move from eax,ebx to ebx,eax
0N/A int temp = reg;
0N/A reg = regHi;
0N/A regHi = temp;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (reg == any_reg || (_num_phys_regs == 2 && regHi == any_reg) || use_pos <= cur->first_usage(mustHaveRegister)) {
0N/A // the first use of cur is later than the spilling position -> spill cur
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("able to spill current interval. first_usage(register): %d, use_pos: %d", cur->first_usage(mustHaveRegister), use_pos));
0N/A
0N/A if (cur->first_usage(mustHaveRegister) <= cur->from() + 1) {
0N/A assert(false, "cannot spill interval that is used in first instruction (possible reason: no register found)");
0N/A // assign a reasonable register and do a bailout in product mode to avoid errors
0N/A allocator()->assign_spill_slot(cur);
0N/A BAILOUT("LinearScan: no register found");
0N/A }
0N/A
0N/A split_and_spill_interval(cur);
0N/A } else {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("decided to use register %d, %d", reg, regHi));
0N/A assert(reg != any_reg && (_num_phys_regs == 1 || regHi != any_reg), "no register found");
0N/A assert(split_pos > 0, "invalid split_pos");
0N/A assert(need_split == false || split_pos > cur->from(), "splitting interval at from");
0N/A
0N/A cur->assign_reg(reg, regHi);
0N/A if (need_split) {
0N/A // register not available for full interval, so split it
0N/A split_when_partial_register_available(cur, split_pos);
0N/A }
0N/A
0N/A // perform splitting and spilling for all affected intervalls
0N/A split_and_spill_intersecting_intervals(reg, regHi);
0N/A }
0N/A}
0N/A
0N/Abool LinearScanWalker::no_allocation_possible(Interval* cur) {
304N/A#ifdef X86
0N/A // fast calculation of intervals that can never get a register because the
0N/A // the next instruction is a call that blocks all registers
0N/A // Note: this does not work if callee-saved registers are available (e.g. on Sparc)
0N/A
0N/A // check if this interval is the result of a split operation
0N/A // (an interval got a register until this position)
0N/A int pos = cur->from();
0N/A if ((pos & 1) == 1) {
0N/A // the current instruction is a call that blocks all registers
0N/A if (pos < allocator()->max_lir_op_id() && allocator()->has_call(pos + 1)) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" free register cannot be available because all registers blocked by following call"));
0N/A
0N/A // safety check that there is really no register available
0N/A assert(alloc_free_reg(cur) == false, "found a register for this interval");
0N/A return true;
0N/A }
0N/A
0N/A }
0N/A#endif
0N/A return false;
0N/A}
0N/A
0N/Avoid LinearScanWalker::init_vars_for_alloc(Interval* cur) {
0N/A BasicType type = cur->type();
0N/A _num_phys_regs = LinearScan::num_physical_regs(type);
0N/A _adjacent_regs = LinearScan::requires_adjacent_regs(type);
0N/A
0N/A if (pd_init_regs_for_alloc(cur)) {
0N/A // the appropriate register range was selected.
0N/A } else if (type == T_FLOAT || type == T_DOUBLE) {
0N/A _first_reg = pd_first_fpu_reg;
0N/A _last_reg = pd_last_fpu_reg;
0N/A } else {
0N/A _first_reg = pd_first_cpu_reg;
1909N/A _last_reg = FrameMap::last_cpu_reg();
0N/A }
0N/A
0N/A assert(0 <= _first_reg && _first_reg < LinearScan::nof_regs, "out of range");
0N/A assert(0 <= _last_reg && _last_reg < LinearScan::nof_regs, "out of range");
0N/A}
0N/A
0N/A
0N/Abool LinearScanWalker::is_move(LIR_Op* op, Interval* from, Interval* to) {
0N/A if (op->code() != lir_move) {
0N/A return false;
0N/A }
0N/A assert(op->as_Op1() != NULL, "move must be LIR_Op1");
0N/A
0N/A LIR_Opr in = ((LIR_Op1*)op)->in_opr();
0N/A LIR_Opr res = ((LIR_Op1*)op)->result_opr();
0N/A return in->is_virtual() && res->is_virtual() && in->vreg_number() == from->reg_num() && res->vreg_number() == to->reg_num();
0N/A}
0N/A
0N/A// optimization (especially for phi functions of nested loops):
0N/A// assign same spill slot to non-intersecting intervals
0N/Avoid LinearScanWalker::combine_spilled_intervals(Interval* cur) {
0N/A if (cur->is_split_child()) {
0N/A // optimization is only suitable for split parents
0N/A return;
0N/A }
0N/A
0N/A Interval* register_hint = cur->register_hint(false);
0N/A if (register_hint == NULL) {
0N/A // cur is not the target of a move, otherwise register_hint would be set
0N/A return;
0N/A }
0N/A assert(register_hint->is_split_parent(), "register hint must be split parent");
0N/A
0N/A if (cur->spill_state() != noOptimization || register_hint->spill_state() != noOptimization) {
0N/A // combining the stack slots for intervals where spill move optimization is applied
0N/A // is not benefitial and would cause problems
0N/A return;
0N/A }
0N/A
0N/A int begin_pos = cur->from();
0N/A int end_pos = cur->to();
0N/A if (end_pos > allocator()->max_lir_op_id() || (begin_pos & 1) != 0 || (end_pos & 1) != 0) {
0N/A // safety check that lir_op_with_id is allowed
0N/A return;
0N/A }
0N/A
0N/A if (!is_move(allocator()->lir_op_with_id(begin_pos), register_hint, cur) || !is_move(allocator()->lir_op_with_id(end_pos), cur, register_hint)) {
0N/A // cur and register_hint are not connected with two moves
0N/A return;
0N/A }
0N/A
0N/A Interval* begin_hint = register_hint->split_child_at_op_id(begin_pos, LIR_OpVisitState::inputMode);
0N/A Interval* end_hint = register_hint->split_child_at_op_id(end_pos, LIR_OpVisitState::outputMode);
0N/A if (begin_hint == end_hint || begin_hint->to() != begin_pos || end_hint->from() != end_pos) {
0N/A // register_hint must be split, otherwise the re-writing of use positions does not work
0N/A return;
0N/A }
0N/A
0N/A assert(begin_hint->assigned_reg() != any_reg, "must have register assigned");
0N/A assert(end_hint->assigned_reg() == any_reg, "must not have register assigned");
0N/A assert(cur->first_usage(mustHaveRegister) == begin_pos, "must have use position at begin of interval because of move");
0N/A assert(end_hint->first_usage(mustHaveRegister) == end_pos, "must have use position at begin of interval because of move");
0N/A
0N/A if (begin_hint->assigned_reg() < LinearScan::nof_regs) {
0N/A // register_hint is not spilled at begin_pos, so it would not be benefitial to immediately spill cur
0N/A return;
0N/A }
0N/A assert(register_hint->canonical_spill_slot() != -1, "must be set when part of interval was spilled");
0N/A
0N/A // modify intervals such that cur gets the same stack slot as register_hint
0N/A // delete use positions to prevent the intervals to get a register at beginning
0N/A cur->set_canonical_spill_slot(register_hint->canonical_spill_slot());
0N/A cur->remove_first_use_pos();
0N/A end_hint->remove_first_use_pos();
0N/A}
0N/A
0N/A
0N/A// allocate a physical register or memory location to an interval
0N/Abool LinearScanWalker::activate_current() {
0N/A Interval* cur = current();
0N/A bool result = true;
0N/A
0N/A TRACE_LINEAR_SCAN(2, tty->print ("+++++ activating interval "); cur->print());
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" split_parent: %d, insert_move_when_activated: %d", cur->split_parent()->reg_num(), cur->insert_move_when_activated()));
0N/A
0N/A if (cur->assigned_reg() >= LinearScan::nof_regs) {
0N/A // activating an interval that has a stack slot assigned -> split it at first use position
0N/A // used for method parameters
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" interval has spill slot assigned (method parameter) -> split it before first use"));
0N/A
0N/A split_stack_interval(cur);
0N/A result = false;
0N/A
0N/A } else if (allocator()->gen()->is_vreg_flag_set(cur->reg_num(), LIRGenerator::must_start_in_memory)) {
0N/A // activating an interval that must start in a stack slot, but may get a register later
0N/A // used for lir_roundfp: rounding is done by store to stack and reload later
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" interval must start in stack slot -> split it before first use"));
0N/A assert(cur->assigned_reg() == any_reg && cur->assigned_regHi() == any_reg, "register already assigned");
0N/A
0N/A allocator()->assign_spill_slot(cur);
0N/A split_stack_interval(cur);
0N/A result = false;
0N/A
0N/A } else if (cur->assigned_reg() == any_reg) {
0N/A // interval has not assigned register -> normal allocation
0N/A // (this is the normal case for most intervals)
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" normal allocation of register"));
0N/A
0N/A // assign same spill slot to non-intersecting intervals
0N/A combine_spilled_intervals(cur);
0N/A
0N/A init_vars_for_alloc(cur);
0N/A if (no_allocation_possible(cur) || !alloc_free_reg(cur)) {
0N/A // no empty register available.
0N/A // split and spill another interval so that this interval gets a register
0N/A alloc_locked_reg(cur);
0N/A }
0N/A
0N/A // spilled intervals need not be move to active-list
0N/A if (cur->assigned_reg() >= LinearScan::nof_regs) {
0N/A result = false;
0N/A }
0N/A }
0N/A
0N/A // load spilled values that become active from stack slot to register
0N/A if (cur->insert_move_when_activated()) {
0N/A assert(cur->is_split_child(), "must be");
0N/A assert(cur->current_split_child() != NULL, "must be");
0N/A assert(cur->current_split_child()->reg_num() != cur->reg_num(), "cannot insert move between same interval");
0N/A TRACE_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()));
0N/A
0N/A insert_move(cur->from(), cur->current_split_child(), cur);
0N/A }
0N/A cur->make_current_split_child();
0N/A
0N/A return result; // true = interval is moved to active list
0N/A}
0N/A
0N/A
0N/A// Implementation of EdgeMoveOptimizer
0N/A
0N/AEdgeMoveOptimizer::EdgeMoveOptimizer() :
0N/A _edge_instructions(4),
0N/A _edge_instructions_idx(4)
0N/A{
0N/A}
0N/A
0N/Avoid EdgeMoveOptimizer::optimize(BlockList* code) {
0N/A EdgeMoveOptimizer optimizer = EdgeMoveOptimizer();
0N/A
0N/A // ignore the first block in the list (index 0 is not processed)
0N/A for (int i = code->length() - 1; i >= 1; i--) {
0N/A BlockBegin* block = code->at(i);
0N/A
0N/A if (block->number_of_preds() > 1 && !block->is_set(BlockBegin::exception_entry_flag)) {
0N/A optimizer.optimize_moves_at_block_end(block);
0N/A }
0N/A if (block->number_of_sux() == 2) {
0N/A optimizer.optimize_moves_at_block_begin(block);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// clear all internal data structures
0N/Avoid EdgeMoveOptimizer::init_instructions() {
0N/A _edge_instructions.clear();
0N/A _edge_instructions_idx.clear();
0N/A}
0N/A
0N/A// append a lir-instruction-list and the index of the current operation in to the list
0N/Avoid EdgeMoveOptimizer::append_instructions(LIR_OpList* instructions, int instructions_idx) {
0N/A _edge_instructions.append(instructions);
0N/A _edge_instructions_idx.append(instructions_idx);
0N/A}
0N/A
0N/A// return the current operation of the given edge (predecessor or successor)
0N/ALIR_Op* EdgeMoveOptimizer::instruction_at(int edge) {
0N/A LIR_OpList* instructions = _edge_instructions.at(edge);
0N/A int idx = _edge_instructions_idx.at(edge);
0N/A
0N/A if (idx < instructions->length()) {
0N/A return instructions->at(idx);
0N/A } else {
0N/A return NULL;
0N/A }
0N/A}
0N/A
0N/A// removes the current operation of the given edge (predecessor or successor)
0N/Avoid EdgeMoveOptimizer::remove_cur_instruction(int edge, bool decrement_index) {
0N/A LIR_OpList* instructions = _edge_instructions.at(edge);
0N/A int idx = _edge_instructions_idx.at(edge);
0N/A instructions->remove_at(idx);
0N/A
0N/A if (decrement_index) {
0N/A _edge_instructions_idx.at_put(edge, idx - 1);
0N/A }
0N/A}
0N/A
0N/A
0N/Abool EdgeMoveOptimizer::operations_different(LIR_Op* op1, LIR_Op* op2) {
0N/A if (op1 == NULL || op2 == NULL) {
0N/A // at least one block is already empty -> no optimization possible
0N/A return true;
0N/A }
0N/A
0N/A if (op1->code() == lir_move && op2->code() == lir_move) {
0N/A assert(op1->as_Op1() != NULL, "move must be LIR_Op1");
0N/A assert(op2->as_Op1() != NULL, "move must be LIR_Op1");
0N/A LIR_Op1* move1 = (LIR_Op1*)op1;
0N/A LIR_Op1* move2 = (LIR_Op1*)op2;
0N/A if (move1->info() == move2->info() && move1->in_opr() == move2->in_opr() && move1->result_opr() == move2->result_opr()) {
0N/A // these moves are exactly equal and can be optimized
0N/A return false;
0N/A }
0N/A
0N/A } else if (op1->code() == lir_fxch && op2->code() == lir_fxch) {
0N/A assert(op1->as_Op1() != NULL, "fxch must be LIR_Op1");
0N/A assert(op2->as_Op1() != NULL, "fxch must be LIR_Op1");
0N/A LIR_Op1* fxch1 = (LIR_Op1*)op1;
0N/A LIR_Op1* fxch2 = (LIR_Op1*)op2;
0N/A if (fxch1->in_opr()->as_jint() == fxch2->in_opr()->as_jint()) {
0N/A // equal FPU stack operations can be optimized
0N/A return false;
0N/A }
0N/A
0N/A } else if (op1->code() == lir_fpop_raw && op2->code() == lir_fpop_raw) {
0N/A // equal FPU stack operations can be optimized
0N/A return false;
0N/A }
0N/A
0N/A // no optimization possible
0N/A return true;
0N/A}
0N/A
0N/Avoid EdgeMoveOptimizer::optimize_moves_at_block_end(BlockBegin* block) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("optimizing moves at end of block B%d", block->block_id()));
0N/A
0N/A if (block->is_predecessor(block)) {
0N/A // currently we can't handle this correctly.
0N/A return;
0N/A }
0N/A
0N/A init_instructions();
0N/A int num_preds = block->number_of_preds();
0N/A assert(num_preds > 1, "do not call otherwise");
0N/A assert(!block->is_set(BlockBegin::exception_entry_flag), "exception handlers not allowed");
0N/A
0N/A // setup a list with the lir-instructions of all predecessors
0N/A int i;
0N/A for (i = 0; i < num_preds; i++) {
0N/A BlockBegin* pred = block->pred_at(i);
0N/A LIR_OpList* pred_instructions = pred->lir()->instructions_list();
0N/A
0N/A if (pred->number_of_sux() != 1) {
0N/A // this can happen with switch-statements where multiple edges are between
0N/A // the same blocks.
0N/A return;
0N/A }
0N/A
0N/A assert(pred->number_of_sux() == 1, "can handle only one successor");
0N/A assert(pred->sux_at(0) == block, "invalid control flow");
0N/A assert(pred_instructions->last()->code() == lir_branch, "block with successor must end with branch");
0N/A assert(pred_instructions->last()->as_OpBranch() != NULL, "branch must be LIR_OpBranch");
0N/A assert(pred_instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block must end with unconditional branch");
0N/A
0N/A if (pred_instructions->last()->info() != NULL) {
0N/A // can not optimize instructions when debug info is needed
0N/A return;
0N/A }
0N/A
0N/A // ignore the unconditional branch at the end of the block
0N/A append_instructions(pred_instructions, pred_instructions->length() - 2);
0N/A }
0N/A
0N/A
0N/A // process lir-instructions while all predecessors end with the same instruction
0N/A while (true) {
0N/A LIR_Op* op = instruction_at(0);
0N/A for (i = 1; i < num_preds; i++) {
0N/A if (operations_different(op, instruction_at(i))) {
0N/A // these instructions are different and cannot be optimized ->
0N/A // no further optimization possible
0N/A return;
0N/A }
0N/A }
0N/A
0N/A TRACE_LINEAR_SCAN(4, tty->print("found instruction that is equal in all %d predecessors: ", num_preds); op->print());
0N/A
0N/A // insert the instruction at the beginning of the current block
0N/A block->lir()->insert_before(1, op);
0N/A
0N/A // delete the instruction at the end of all predecessors
0N/A for (i = 0; i < num_preds; i++) {
0N/A remove_cur_instruction(i, true);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid EdgeMoveOptimizer::optimize_moves_at_block_begin(BlockBegin* block) {
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr("optimization moves at begin of block B%d", block->block_id()));
0N/A
0N/A init_instructions();
0N/A int num_sux = block->number_of_sux();
0N/A
0N/A LIR_OpList* cur_instructions = block->lir()->instructions_list();
0N/A
0N/A assert(num_sux == 2, "method should not be called otherwise");
0N/A assert(cur_instructions->last()->code() == lir_branch, "block with successor must end with branch");
0N/A assert(cur_instructions->last()->as_OpBranch() != NULL, "branch must be LIR_OpBranch");
0N/A assert(cur_instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block must end with unconditional branch");
0N/A
0N/A if (cur_instructions->last()->info() != NULL) {
0N/A // can no optimize instructions when debug info is needed
0N/A return;
0N/A }
0N/A
0N/A LIR_Op* branch = cur_instructions->at(cur_instructions->length() - 2);
0N/A if (branch->info() != NULL || (branch->code() != lir_branch && branch->code() != lir_cond_float_branch)) {
0N/A // not a valid case for optimization
0N/A // currently, only blocks that end with two branches (conditional branch followed
0N/A // by unconditional branch) are optimized
0N/A return;
0N/A }
0N/A
0N/A // now it is guaranteed that the block ends with two branch instructions.
0N/A // the instructions are inserted at the end of the block before these two branches
0N/A int insert_idx = cur_instructions->length() - 2;
0N/A
0N/A int i;
0N/A#ifdef ASSERT
0N/A for (i = insert_idx - 1; i >= 0; i--) {
0N/A LIR_Op* op = cur_instructions->at(i);
0N/A if ((op->code() == lir_branch || op->code() == lir_cond_float_branch) && ((LIR_OpBranch*)op)->block() != NULL) {
0N/A assert(false, "block with two successors can have only two branch instructions");
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A // setup a list with the lir-instructions of all successors
0N/A for (i = 0; i < num_sux; i++) {
0N/A BlockBegin* sux = block->sux_at(i);
0N/A LIR_OpList* sux_instructions = sux->lir()->instructions_list();
0N/A
0N/A assert(sux_instructions->at(0)->code() == lir_label, "block must start with label");
0N/A
0N/A if (sux->number_of_preds() != 1) {
0N/A // this can happen with switch-statements where multiple edges are between
0N/A // the same blocks.
0N/A return;
0N/A }
0N/A assert(sux->pred_at(0) == block, "invalid control flow");
0N/A assert(!sux->is_set(BlockBegin::exception_entry_flag), "exception handlers not allowed");
0N/A
0N/A // ignore the label at the beginning of the block
0N/A append_instructions(sux_instructions, 1);
0N/A }
0N/A
0N/A // process lir-instructions while all successors begin with the same instruction
0N/A while (true) {
0N/A LIR_Op* op = instruction_at(0);
0N/A for (i = 1; i < num_sux; i++) {
0N/A if (operations_different(op, instruction_at(i))) {
0N/A // these instructions are different and cannot be optimized ->
0N/A // no further optimization possible
0N/A return;
0N/A }
0N/A }
0N/A
0N/A TRACE_LINEAR_SCAN(4, tty->print("----- found instruction that is equal in all %d successors: ", num_sux); op->print());
0N/A
0N/A // insert instruction at end of current block
0N/A block->lir()->insert_before(insert_idx, op);
0N/A insert_idx++;
0N/A
0N/A // delete the instructions at the beginning of all successors
0N/A for (i = 0; i < num_sux; i++) {
0N/A remove_cur_instruction(i, false);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Implementation of ControlFlowOptimizer
0N/A
0N/AControlFlowOptimizer::ControlFlowOptimizer() :
0N/A _original_preds(4)
0N/A{
0N/A}
0N/A
0N/Avoid ControlFlowOptimizer::optimize(BlockList* code) {
0N/A ControlFlowOptimizer optimizer = ControlFlowOptimizer();
0N/A
0N/A // push the OSR entry block to the end so that we're not jumping over it.
0N/A BlockBegin* osr_entry = code->at(0)->end()->as_Base()->osr_entry();
0N/A if (osr_entry) {
0N/A int index = osr_entry->linear_scan_number();
0N/A assert(code->at(index) == osr_entry, "wrong index");
0N/A code->remove_at(index);
0N/A code->append(osr_entry);
0N/A }
0N/A
0N/A optimizer.reorder_short_loops(code);
0N/A optimizer.delete_empty_blocks(code);
0N/A optimizer.delete_unnecessary_jumps(code);
0N/A optimizer.delete_jumps_to_return(code);
0N/A}
0N/A
0N/Avoid ControlFlowOptimizer::reorder_short_loop(BlockList* code, BlockBegin* header_block, int header_idx) {
0N/A int i = header_idx + 1;
0N/A int max_end = MIN2(header_idx + ShortLoopSize, code->length());
0N/A while (i < max_end && code->at(i)->loop_depth() >= header_block->loop_depth()) {
0N/A i++;
0N/A }
0N/A
0N/A if (i == code->length() || code->at(i)->loop_depth() < header_block->loop_depth()) {
0N/A int end_idx = i - 1;
0N/A BlockBegin* end_block = code->at(end_idx);
0N/A
0N/A if (end_block->number_of_sux() == 1 && end_block->sux_at(0) == header_block) {
0N/A // short loop from header_idx to end_idx found -> reorder blocks such that
0N/A // the header_block is the last block instead of the first block of the loop
0N/A TRACE_LINEAR_SCAN(1, tty->print_cr("Reordering short loop: length %d, header B%d, end B%d",
0N/A end_idx - header_idx + 1,
0N/A header_block->block_id(), end_block->block_id()));
0N/A
0N/A for (int j = header_idx; j < end_idx; j++) {
0N/A code->at_put(j, code->at(j + 1));
0N/A }
0N/A code->at_put(end_idx, header_block);
0N/A
0N/A // correct the flags so that any loop alignment occurs in the right place.
0N/A assert(code->at(end_idx)->is_set(BlockBegin::backward_branch_target_flag), "must be backward branch target");
0N/A code->at(end_idx)->clear(BlockBegin::backward_branch_target_flag);
0N/A code->at(header_idx)->set(BlockBegin::backward_branch_target_flag);
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid ControlFlowOptimizer::reorder_short_loops(BlockList* code) {
0N/A for (int i = code->length() - 1; i >= 0; i--) {
0N/A BlockBegin* block = code->at(i);
0N/A
0N/A if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
0N/A reorder_short_loop(code, block, i);
0N/A }
0N/A }
0N/A
0N/A DEBUG_ONLY(verify(code));
0N/A}
0N/A
0N/A// only blocks with exactly one successor can be deleted. Such blocks
0N/A// must always end with an unconditional branch to this successor
0N/Abool ControlFlowOptimizer::can_delete_block(BlockBegin* block) {
0N/A if (block->number_of_sux() != 1 || block->number_of_exception_handlers() != 0 || block->is_entry_block()) {
0N/A return false;
0N/A }
0N/A
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A
0N/A assert(instructions->length() >= 2, "block must have label and branch");
0N/A assert(instructions->at(0)->code() == lir_label, "first instruction must always be a label");
0N/A assert(instructions->last()->as_OpBranch() != NULL, "last instrcution must always be a branch");
0N/A assert(instructions->last()->as_OpBranch()->cond() == lir_cond_always, "branch must be unconditional");
0N/A assert(instructions->last()->as_OpBranch()->block() == block->sux_at(0), "branch target must be the successor");
0N/A
0N/A // block must have exactly one successor
0N/A
0N/A if (instructions->length() == 2 && instructions->last()->info() == NULL) {
0N/A return true;
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A// substitute branch targets in all branch-instructions of this blocks
0N/Avoid ControlFlowOptimizer::substitute_branch_target(BlockBegin* block, BlockBegin* target_from, BlockBegin* target_to) {
0N/A TRACE_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()));
0N/A
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A
0N/A assert(instructions->at(0)->code() == lir_label, "first instruction must always be a label");
0N/A for (int i = instructions->length() - 1; i >= 1; i--) {
0N/A LIR_Op* op = instructions->at(i);
0N/A
0N/A if (op->code() == lir_branch || op->code() == lir_cond_float_branch) {
0N/A assert(op->as_OpBranch() != NULL, "branch must be of type LIR_OpBranch");
0N/A LIR_OpBranch* branch = (LIR_OpBranch*)op;
0N/A
0N/A if (branch->block() == target_from) {
0N/A branch->change_block(target_to);
0N/A }
0N/A if (branch->ublock() == target_from) {
0N/A branch->change_ublock(target_to);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid ControlFlowOptimizer::delete_empty_blocks(BlockList* code) {
0N/A int old_pos = 0;
0N/A int new_pos = 0;
0N/A int num_blocks = code->length();
0N/A
0N/A while (old_pos < num_blocks) {
0N/A BlockBegin* block = code->at(old_pos);
0N/A
0N/A if (can_delete_block(block)) {
0N/A BlockBegin* new_target = block->sux_at(0);
0N/A
0N/A // propagate backward branch target flag for correct code alignment
0N/A if (block->is_set(BlockBegin::backward_branch_target_flag)) {
0N/A new_target->set(BlockBegin::backward_branch_target_flag);
0N/A }
0N/A
0N/A // collect a list with all predecessors that contains each predecessor only once
0N/A // the predecessors of cur are changed during the substitution, so a copy of the
0N/A // predecessor list is necessary
0N/A int j;
0N/A _original_preds.clear();
0N/A for (j = block->number_of_preds() - 1; j >= 0; j--) {
0N/A BlockBegin* pred = block->pred_at(j);
0N/A if (_original_preds.index_of(pred) == -1) {
0N/A _original_preds.append(pred);
0N/A }
0N/A }
0N/A
0N/A for (j = _original_preds.length() - 1; j >= 0; j--) {
0N/A BlockBegin* pred = _original_preds.at(j);
0N/A substitute_branch_target(pred, block, new_target);
0N/A pred->substitute_sux(block, new_target);
0N/A }
0N/A } else {
0N/A // adjust position of this block in the block list if blocks before
0N/A // have been deleted
0N/A if (new_pos != old_pos) {
0N/A code->at_put(new_pos, code->at(old_pos));
0N/A }
0N/A new_pos++;
0N/A }
0N/A old_pos++;
0N/A }
0N/A code->truncate(new_pos);
0N/A
0N/A DEBUG_ONLY(verify(code));
0N/A}
0N/A
0N/Avoid ControlFlowOptimizer::delete_unnecessary_jumps(BlockList* code) {
0N/A // skip the last block because there a branch is always necessary
0N/A for (int i = code->length() - 2; i >= 0; i--) {
0N/A BlockBegin* block = code->at(i);
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A
0N/A LIR_Op* last_op = instructions->last();
0N/A if (last_op->code() == lir_branch) {
0N/A assert(last_op->as_OpBranch() != NULL, "branch must be of type LIR_OpBranch");
0N/A LIR_OpBranch* last_branch = (LIR_OpBranch*)last_op;
0N/A
0N/A assert(last_branch->block() != NULL, "last branch must always have a block as target");
0N/A assert(last_branch->label() == last_branch->block()->label(), "must be equal");
0N/A
0N/A if (last_branch->info() == NULL) {
0N/A if (last_branch->block() == code->at(i + 1)) {
0N/A
0N/A TRACE_LINEAR_SCAN(3, tty->print_cr("Deleting unconditional branch at end of block B%d", block->block_id()));
0N/A
0N/A // delete last branch instruction
0N/A instructions->truncate(instructions->length() - 1);
0N/A
0N/A } else {
0N/A LIR_Op* prev_op = instructions->at(instructions->length() - 2);
0N/A if (prev_op->code() == lir_branch || prev_op->code() == lir_cond_float_branch) {
0N/A assert(prev_op->as_OpBranch() != NULL, "branch must be of type LIR_OpBranch");
0N/A LIR_OpBranch* prev_branch = (LIR_OpBranch*)prev_op;
0N/A
1601N/A LIR_Op2* prev_cmp = NULL;
1601N/A
1601N/A for(int j = instructions->length() - 3; j >= 0 && prev_cmp == NULL; j--) {
1601N/A prev_op = instructions->at(j);
1601N/A if(prev_op->code() == lir_cmp) {
1601N/A assert(prev_op->as_Op2() != NULL, "branch must be of type LIR_Op2");
1601N/A prev_cmp = (LIR_Op2*)prev_op;
1601N/A assert(prev_branch->cond() == prev_cmp->condition(), "should be the same");
1601N/A }
1601N/A }
1601N/A assert(prev_cmp != NULL, "should have found comp instruction for branch");
0N/A if (prev_branch->block() == code->at(i + 1) && prev_branch->info() == NULL) {
0N/A
0N/A TRACE_LINEAR_SCAN(3, tty->print_cr("Negating conditional branch and deleting unconditional branch at end of block B%d", block->block_id()));
0N/A
0N/A // eliminate a conditional branch to the immediate successor
0N/A prev_branch->change_block(last_branch->block());
0N/A prev_branch->negate_cond();
1601N/A prev_cmp->set_condition(prev_branch->cond());
0N/A instructions->truncate(instructions->length() - 1);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A DEBUG_ONLY(verify(code));
0N/A}
0N/A
0N/Avoid ControlFlowOptimizer::delete_jumps_to_return(BlockList* code) {
0N/A#ifdef ASSERT
0N/A BitMap return_converted(BlockBegin::number_of_blocks());
0N/A return_converted.clear();
0N/A#endif
0N/A
0N/A for (int i = code->length() - 1; i >= 0; i--) {
0N/A BlockBegin* block = code->at(i);
0N/A LIR_OpList* cur_instructions = block->lir()->instructions_list();
0N/A LIR_Op* cur_last_op = cur_instructions->last();
0N/A
0N/A assert(cur_instructions->at(0)->code() == lir_label, "first instruction must always be a label");
0N/A if (cur_instructions->length() == 2 && cur_last_op->code() == lir_return) {
0N/A // the block contains only a label and a return
0N/A // if a predecessor ends with an unconditional jump to this block, then the jump
0N/A // can be replaced with a return instruction
0N/A //
0N/A // Note: the original block with only a return statement cannot be deleted completely
0N/A // because the predecessors might have other (conditional) jumps to this block
0N/A // -> this may lead to unnecesary return instructions in the final code
0N/A
0N/A assert(cur_last_op->info() == NULL, "return instructions do not have debug information");
0N/A assert(block->number_of_sux() == 0 ||
0N/A (return_converted.at(block->block_id()) && block->number_of_sux() == 1),
0N/A "blocks that end with return must not have successors");
0N/A
0N/A assert(cur_last_op->as_Op1() != NULL, "return must be LIR_Op1");
0N/A LIR_Opr return_opr = ((LIR_Op1*)cur_last_op)->in_opr();
0N/A
0N/A for (int j = block->number_of_preds() - 1; j >= 0; j--) {
0N/A BlockBegin* pred = block->pred_at(j);
0N/A LIR_OpList* pred_instructions = pred->lir()->instructions_list();
0N/A LIR_Op* pred_last_op = pred_instructions->last();
0N/A
0N/A if (pred_last_op->code() == lir_branch) {
0N/A assert(pred_last_op->as_OpBranch() != NULL, "branch must be LIR_OpBranch");
0N/A LIR_OpBranch* pred_last_branch = (LIR_OpBranch*)pred_last_op;
0N/A
0N/A if (pred_last_branch->block() == block && pred_last_branch->cond() == lir_cond_always && pred_last_branch->info() == NULL) {
0N/A // replace the jump to a return with a direct return
0N/A // Note: currently the edge between the blocks is not deleted
0N/A pred_instructions->at_put(pred_instructions->length() - 1, new LIR_Op1(lir_return, return_opr));
0N/A#ifdef ASSERT
0N/A return_converted.set_bit(pred->block_id());
0N/A#endif
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A#ifdef ASSERT
0N/Avoid ControlFlowOptimizer::verify(BlockList* code) {
0N/A for (int i = 0; i < code->length(); i++) {
0N/A BlockBegin* block = code->at(i);
0N/A LIR_OpList* instructions = block->lir()->instructions_list();
0N/A
0N/A int j;
0N/A for (j = 0; j < instructions->length(); j++) {
0N/A LIR_OpBranch* op_branch = instructions->at(j)->as_OpBranch();
0N/A
0N/A if (op_branch != NULL) {
0N/A assert(op_branch->block() == NULL || code->index_of(op_branch->block()) != -1, "branch target not valid");
0N/A assert(op_branch->ublock() == NULL || code->index_of(op_branch->ublock()) != -1, "branch target not valid");
0N/A }
0N/A }
0N/A
0N/A for (j = 0; j < block->number_of_sux() - 1; j++) {
0N/A BlockBegin* sux = block->sux_at(j);
0N/A assert(code->index_of(sux) != -1, "successor not valid");
0N/A }
0N/A
0N/A for (j = 0; j < block->number_of_preds() - 1; j++) {
0N/A BlockBegin* pred = block->pred_at(j);
0N/A assert(code->index_of(pred) != -1, "successor not valid");
0N/A }
0N/A }
0N/A}
0N/A#endif
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/A// Implementation of LinearStatistic
0N/A
0N/Aconst char* LinearScanStatistic::counter_name(int counter_idx) {
0N/A switch (counter_idx) {
0N/A case counter_method: return "compiled methods";
0N/A case counter_fpu_method: return "methods using fpu";
0N/A case counter_loop_method: return "methods with loops";
0N/A case counter_exception_method:return "methods with xhandler";
0N/A
0N/A case counter_loop: return "loops";
0N/A case counter_block: return "blocks";
0N/A case counter_loop_block: return "blocks inside loop";
0N/A case counter_exception_block: return "exception handler entries";
0N/A case counter_interval: return "intervals";
0N/A case counter_fixed_interval: return "fixed intervals";
0N/A case counter_range: return "ranges";
0N/A case counter_fixed_range: return "fixed ranges";
0N/A case counter_use_pos: return "use positions";
0N/A case counter_fixed_use_pos: return "fixed use positions";
0N/A case counter_spill_slots: return "spill slots";
0N/A
0N/A // counter for classes of lir instructions
0N/A case counter_instruction: return "total instructions";
0N/A case counter_label: return "labels";
0N/A case counter_entry: return "method entries";
0N/A case counter_return: return "method returns";
0N/A case counter_call: return "method calls";
0N/A case counter_move: return "moves";
0N/A case counter_cmp: return "compare";
0N/A case counter_cond_branch: return "conditional branches";
0N/A case counter_uncond_branch: return "unconditional branches";
0N/A case counter_stub_branch: return "branches to stub";
0N/A case counter_alu: return "artithmetic + logic";
0N/A case counter_alloc: return "allocations";
0N/A case counter_sync: return "synchronisation";
0N/A case counter_throw: return "throw";
0N/A case counter_unwind: return "unwind";
0N/A case counter_typecheck: return "type+null-checks";
0N/A case counter_fpu_stack: return "fpu-stack";
0N/A case counter_misc_inst: return "other instructions";
0N/A case counter_other_inst: return "misc. instructions";
0N/A
0N/A // counter for different types of moves
0N/A case counter_move_total: return "total moves";
0N/A case counter_move_reg_reg: return "register->register";
0N/A case counter_move_reg_stack: return "register->stack";
0N/A case counter_move_stack_reg: return "stack->register";
0N/A case counter_move_stack_stack:return "stack->stack";
0N/A case counter_move_reg_mem: return "register->memory";
0N/A case counter_move_mem_reg: return "memory->register";
0N/A case counter_move_const_any: return "constant->any";
0N/A
0N/A case blank_line_1: return "";
0N/A case blank_line_2: return "";
0N/A
0N/A default: ShouldNotReachHere(); return "";
0N/A }
0N/A}
0N/A
0N/ALinearScanStatistic::Counter LinearScanStatistic::base_counter(int counter_idx) {
0N/A if (counter_idx == counter_fpu_method || counter_idx == counter_loop_method || counter_idx == counter_exception_method) {
0N/A return counter_method;
0N/A } else if (counter_idx == counter_loop_block || counter_idx == counter_exception_block) {
0N/A return counter_block;
0N/A } else if (counter_idx >= counter_instruction && counter_idx <= counter_other_inst) {
0N/A return counter_instruction;
0N/A } else if (counter_idx >= counter_move_total && counter_idx <= counter_move_const_any) {
0N/A return counter_move_total;
0N/A }
0N/A return invalid_counter;
0N/A}
0N/A
0N/ALinearScanStatistic::LinearScanStatistic() {
0N/A for (int i = 0; i < number_of_counters; i++) {
0N/A _counters_sum[i] = 0;
0N/A _counters_max[i] = -1;
0N/A }
0N/A
0N/A}
0N/A
0N/A// add the method-local numbers to the total sum
0N/Avoid LinearScanStatistic::sum_up(LinearScanStatistic &method_statistic) {
0N/A for (int i = 0; i < number_of_counters; i++) {
0N/A _counters_sum[i] += method_statistic._counters_sum[i];
0N/A _counters_max[i] = MAX2(_counters_max[i], method_statistic._counters_sum[i]);
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanStatistic::print(const char* title) {
0N/A if (CountLinearScan || TraceLinearScanLevel > 0) {
0N/A tty->cr();
0N/A tty->print_cr("***** LinearScan statistic - %s *****", title);
0N/A
0N/A for (int i = 0; i < number_of_counters; i++) {
0N/A if (_counters_sum[i] > 0 || _counters_max[i] >= 0) {
0N/A tty->print("%25s: %8d", counter_name(i), _counters_sum[i]);
0N/A
0N/A if (base_counter(i) != invalid_counter) {
0N/A tty->print(" (%5.1f%%) ", _counters_sum[i] * 100.0 / _counters_sum[base_counter(i)]);
0N/A } else {
0N/A tty->print(" ");
0N/A }
0N/A
0N/A if (_counters_max[i] >= 0) {
0N/A tty->print("%8d", _counters_max[i]);
0N/A }
0N/A }
0N/A tty->cr();
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanStatistic::collect(LinearScan* allocator) {
0N/A inc_counter(counter_method);
0N/A if (allocator->has_fpu_registers()) {
0N/A inc_counter(counter_fpu_method);
0N/A }
0N/A if (allocator->num_loops() > 0) {
0N/A inc_counter(counter_loop_method);
0N/A }
0N/A inc_counter(counter_loop, allocator->num_loops());
0N/A inc_counter(counter_spill_slots, allocator->max_spills());
0N/A
0N/A int i;
0N/A for (i = 0; i < allocator->interval_count(); i++) {
0N/A Interval* cur = allocator->interval_at(i);
0N/A
0N/A if (cur != NULL) {
0N/A inc_counter(counter_interval);
0N/A inc_counter(counter_use_pos, cur->num_use_positions());
0N/A if (LinearScan::is_precolored_interval(cur)) {
0N/A inc_counter(counter_fixed_interval);
0N/A inc_counter(counter_fixed_use_pos, cur->num_use_positions());
0N/A }
0N/A
0N/A Range* range = cur->first();
0N/A while (range != Range::end()) {
0N/A inc_counter(counter_range);
0N/A if (LinearScan::is_precolored_interval(cur)) {
0N/A inc_counter(counter_fixed_range);
0N/A }
0N/A range = range->next();
0N/A }
0N/A }
0N/A }
0N/A
0N/A bool has_xhandlers = false;
0N/A // Note: only count blocks that are in code-emit order
0N/A for (i = 0; i < allocator->ir()->code()->length(); i++) {
0N/A BlockBegin* cur = allocator->ir()->code()->at(i);
0N/A
0N/A inc_counter(counter_block);
0N/A if (cur->loop_depth() > 0) {
0N/A inc_counter(counter_loop_block);
0N/A }
0N/A if (cur->is_set(BlockBegin::exception_entry_flag)) {
0N/A inc_counter(counter_exception_block);
0N/A has_xhandlers = true;
0N/A }
0N/A
0N/A LIR_OpList* instructions = cur->lir()->instructions_list();
0N/A for (int j = 0; j < instructions->length(); j++) {
0N/A LIR_Op* op = instructions->at(j);
0N/A
0N/A inc_counter(counter_instruction);
0N/A
0N/A switch (op->code()) {
0N/A case lir_label: inc_counter(counter_label); break;
0N/A case lir_std_entry:
0N/A case lir_osr_entry: inc_counter(counter_entry); break;
0N/A case lir_return: inc_counter(counter_return); break;
0N/A
0N/A case lir_rtcall:
0N/A case lir_static_call:
0N/A case lir_optvirtual_call:
0N/A case lir_virtual_call: inc_counter(counter_call); break;
0N/A
0N/A case lir_move: {
0N/A inc_counter(counter_move);
0N/A inc_counter(counter_move_total);
0N/A
0N/A LIR_Opr in = op->as_Op1()->in_opr();
0N/A LIR_Opr res = op->as_Op1()->result_opr();
0N/A if (in->is_register()) {
0N/A if (res->is_register()) {
0N/A inc_counter(counter_move_reg_reg);
0N/A } else if (res->is_stack()) {
0N/A inc_counter(counter_move_reg_stack);
0N/A } else if (res->is_address()) {
0N/A inc_counter(counter_move_reg_mem);
0N/A } else {
0N/A ShouldNotReachHere();
0N/A }
0N/A } else if (in->is_stack()) {
0N/A if (res->is_register()) {
0N/A inc_counter(counter_move_stack_reg);
0N/A } else {
0N/A inc_counter(counter_move_stack_stack);
0N/A }
0N/A } else if (in->is_address()) {
0N/A assert(res->is_register(), "must be");
0N/A inc_counter(counter_move_mem_reg);
0N/A } else if (in->is_constant()) {
0N/A inc_counter(counter_move_const_any);
0N/A } else {
0N/A ShouldNotReachHere();
0N/A }
0N/A break;
0N/A }
0N/A
0N/A case lir_cmp: inc_counter(counter_cmp); break;
0N/A
0N/A case lir_branch:
0N/A case lir_cond_float_branch: {
0N/A LIR_OpBranch* branch = op->as_OpBranch();
0N/A if (branch->block() == NULL) {
0N/A inc_counter(counter_stub_branch);
0N/A } else if (branch->cond() == lir_cond_always) {
0N/A inc_counter(counter_uncond_branch);
0N/A } else {
0N/A inc_counter(counter_cond_branch);
0N/A }
0N/A break;
0N/A }
0N/A
0N/A case lir_neg:
0N/A case lir_add:
0N/A case lir_sub:
0N/A case lir_mul:
0N/A case lir_mul_strictfp:
0N/A case lir_div:
0N/A case lir_div_strictfp:
0N/A case lir_rem:
0N/A case lir_sqrt:
0N/A case lir_sin:
0N/A case lir_cos:
0N/A case lir_abs:
0N/A case lir_log10:
0N/A case lir_log:
3752N/A case lir_pow:
3752N/A case lir_exp:
0N/A case lir_logic_and:
0N/A case lir_logic_or:
0N/A case lir_logic_xor:
0N/A case lir_shl:
0N/A case lir_shr:
0N/A case lir_ushr: inc_counter(counter_alu); break;
0N/A
0N/A case lir_alloc_object:
0N/A case lir_alloc_array: inc_counter(counter_alloc); break;
0N/A
0N/A case lir_monaddr:
0N/A case lir_lock:
0N/A case lir_unlock: inc_counter(counter_sync); break;
0N/A
0N/A case lir_throw: inc_counter(counter_throw); break;
0N/A
0N/A case lir_unwind: inc_counter(counter_unwind); break;
0N/A
0N/A case lir_null_check:
0N/A case lir_leal:
0N/A case lir_instanceof:
0N/A case lir_checkcast:
0N/A case lir_store_check: inc_counter(counter_typecheck); break;
0N/A
0N/A case lir_fpop_raw:
0N/A case lir_fxch:
0N/A case lir_fld: inc_counter(counter_fpu_stack); break;
0N/A
0N/A case lir_nop:
0N/A case lir_push:
0N/A case lir_pop:
0N/A case lir_convert:
0N/A case lir_roundfp:
0N/A case lir_cmove: inc_counter(counter_misc_inst); break;
0N/A
0N/A default: inc_counter(counter_other_inst); break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (has_xhandlers) {
0N/A inc_counter(counter_exception_method);
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanStatistic::compute(LinearScan* allocator, LinearScanStatistic &global_statistic) {
0N/A if (CountLinearScan || TraceLinearScanLevel > 0) {
0N/A
0N/A LinearScanStatistic local_statistic = LinearScanStatistic();
0N/A
0N/A local_statistic.collect(allocator);
0N/A global_statistic.sum_up(local_statistic);
0N/A
0N/A if (TraceLinearScanLevel > 2) {
0N/A local_statistic.print("current local statistic");
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Implementation of LinearTimers
0N/A
0N/ALinearScanTimers::LinearScanTimers() {
0N/A for (int i = 0; i < number_of_timers; i++) {
0N/A timer(i)->reset();
0N/A }
0N/A}
0N/A
0N/Aconst char* LinearScanTimers::timer_name(int idx) {
0N/A switch (idx) {
0N/A case timer_do_nothing: return "Nothing (Time Check)";
0N/A case timer_number_instructions: return "Number Instructions";
0N/A case timer_compute_local_live_sets: return "Local Live Sets";
0N/A case timer_compute_global_live_sets: return "Global Live Sets";
0N/A case timer_build_intervals: return "Build Intervals";
0N/A case timer_sort_intervals_before: return "Sort Intervals Before";
0N/A case timer_allocate_registers: return "Allocate Registers";
0N/A case timer_resolve_data_flow: return "Resolve Data Flow";
0N/A case timer_sort_intervals_after: return "Sort Intervals After";
0N/A case timer_eliminate_spill_moves: return "Spill optimization";
0N/A case timer_assign_reg_num: return "Assign Reg Num";
0N/A case timer_allocate_fpu_stack: return "Allocate FPU Stack";
0N/A case timer_optimize_lir: return "Optimize LIR";
0N/A default: ShouldNotReachHere(); return "";
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanTimers::begin_method() {
0N/A if (TimeEachLinearScan) {
0N/A // reset all timers to measure only current method
0N/A for (int i = 0; i < number_of_timers; i++) {
0N/A timer(i)->reset();
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanTimers::end_method(LinearScan* allocator) {
0N/A if (TimeEachLinearScan) {
0N/A
0N/A double c = timer(timer_do_nothing)->seconds();
0N/A double total = 0;
0N/A for (int i = 1; i < number_of_timers; i++) {
0N/A total += timer(i)->seconds() - c;
0N/A }
0N/A
0N/A if (total >= 0.0005) {
0N/A // print all information in one line for automatic processing
0N/A tty->print("@"); allocator->compilation()->method()->print_name();
0N/A
0N/A tty->print("@ %d ", allocator->compilation()->method()->code_size());
0N/A tty->print("@ %d ", allocator->block_at(allocator->block_count() - 1)->last_lir_instruction_id() / 2);
0N/A tty->print("@ %d ", allocator->block_count());
0N/A tty->print("@ %d ", allocator->num_virtual_regs());
0N/A tty->print("@ %d ", allocator->interval_count());
0N/A tty->print("@ %d ", allocator->_num_calls);
0N/A tty->print("@ %d ", allocator->num_loops());
0N/A
0N/A tty->print("@ %6.6f ", total);
0N/A for (int i = 1; i < number_of_timers; i++) {
0N/A tty->print("@ %4.1f ", ((timer(i)->seconds() - c) / total) * 100);
0N/A }
0N/A tty->cr();
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid LinearScanTimers::print(double total_time) {
0N/A if (TimeLinearScan) {
0N/A // correction value: sum of dummy-timer that only measures the time that
0N/A // is necesary to start and stop itself
0N/A double c = timer(timer_do_nothing)->seconds();
0N/A
0N/A for (int i = 0; i < number_of_timers; i++) {
0N/A double t = timer(i)->seconds();
0N/A tty->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);
0N/A }
0N/A }
0N/A}
0N/A
0N/A#endif // #ifndef PRODUCT