c1_LinearScan.cpp revision 3752
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A#include "precompiled.hpp"
0N/A#include "c1/c1_CFGPrinter.hpp"
0N/A#include "c1/c1_CodeStubs.hpp"
0N/A#include "c1/c1_Compilation.hpp"
0N/A#include "c1/c1_FrameMap.hpp"
0N/A#include "c1/c1_LIRGenerator.hpp"
0N/A#include "c1/c1_LinearScan.hpp"
0N/A#include "c1/c1_ValueStack.hpp"
0N/A#include "utilities/bitMap.inline.hpp"
0N/A#ifdef TARGET_ARCH_x86
0N/A# include "vmreg_x86.inline.hpp"
0N/A#ifdef TARGET_ARCH_sparc
0N/A# include "vmreg_sparc.inline.hpp"
0N/A#ifdef TARGET_ARCH_zero
0N/A# include "vmreg_zero.inline.hpp"
0N/A#ifdef TARGET_ARCH_arm
0N/A# include "vmreg_arm.inline.hpp"
0N/A#ifdef TARGET_ARCH_ppc
0N/A# include "vmreg_ppc.inline.hpp"
0N/A #define TIME_LINEAR_SCAN(timer_name) TraceTime _block_timer("", _total_timer.timer(LinearScanTimers::timer_name), TimeLinearScan || TimeEachLinearScan, Verbose);
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/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 , _has_fpu_registers(false)
0N/A , _max_spills(0)
0N/A , _needs_full_resort(false)
0N/A assert(opr->vreg_number() >= nof_regs, "found a virtual register with a fixed-register number");
0N/A return i->reg_num() >= LIR_OprDesc::vreg_base && (i->type() != T_FLOAT && i->type() != T_DOUBLE);
0N/A return i->reg_num() >= LIR_OprDesc::vreg_base && (i->type() == T_FLOAT || i->type() == T_DOUBLE);
0N/A return i->reg_num() >= nof_regs && pd_first_fpu_reg <= i->assigned_reg() && i->assigned_reg() <= pd_last_fpu_reg;
0N/A int spill_slot;
0N/A if (double_word) {
0N/A _max_spills++;
0N/A _max_spills++;
0N/A case noDefinitionFound:
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 assert(block_of_op_with_id(def_pos) == block_of_op_with_id(interval->spill_definition_pos()), "block must be equal");
0N/A case noOptimization:
0N/A case oneDefinitionFound: {
0N/A case oneMoveInserted: {
0N/A case storeAtDefinition:
0N/A case startInMemory:
0N/A case noOptimization:
0N/A case noDefinitionFound:
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 assert(temp->canonical_spill_slot() >= LinearScan::nof_regs, "interval has no spill slot assigned");
0N/A assert(temp->spill_definition_pos() <= temp->from() + 2, "only intervals defined once at their start-pos can be optimized");
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 for (int i = 0; i < num_blocks; i++) {
0N/A assert(op->as_Op1()->result_opr()->is_virtual(), "LinearScan inserts only moves to virtual registers");
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 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 assert(from_opr->is_fixed_cpu() || from_opr->is_fixed_fpu(), "from operand must be a register");
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 int num_instructions = 0;
0N/A for (i = 0; i < num_blocks; i++) {
0N/A for (i = 0; i < num_blocks; i++) {
0N/Avoid LinearScan::set_live_gen_kill(Value value, LIR_Op* op, BitMap& live_gen, BitMap& live_kill) {
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 assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
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 bool local_has_fpu_registers = false;
0N/A int local_num_calls = 0;
0N/A for (int i = 0; i < num_blocks; i++) {
2167N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A TRACE_LINEAR_SCAN(4, tty->print_cr(" Setting live_gen for register %d at instruction %d", reg, op->id()));
0N/A // Add uses of live locals from interpreter's point of view for proper debug information generation
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
0N/A assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
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 bool change_occurred;
0N/A bool change_occurred_in_block;
0N/A int iteration_count = 0;
0N/A change_occurred = false;
0N/A change_occurred_in_block = false;
0N/A change_occurred = true;
0N/A change_occurred_in_block = true;
#ifndef PRODUCT
tty->print("(%d) live_in%c B%d ", iteration_count, c, block->block_id()); print_bitmap(block->live_in());
tty->print("(%d) live_out%c B%d ", iteration_count, c, block->block_id()); print_bitmap(block->live_out());
} while (change_occurred);
#ifdef ASSERT
for (int i = 0; i < num_blocks; i++) {
#ifdef ASSERT
tty->print_cr("Error: live_in set of first block must be empty (when this fails, virtual registers are used before they are defined)");
tty->print_cr("* vreg %d (HIR instruction %c%d)", i, instr == NULL ? ' ' : instr->type()->tchar(), instr == NULL ? 0 : instr->id());
for (int j = 0; j < num_blocks; j++) {
assert(!value->type()->is_illegal(), "if this value is used by the interpreter it shouldn't be of indeterminate type");
assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
TRACE_LINEAR_SCAN(2, tty->print(" def "); opr->print(tty); tty->print_cr(" def_pos %d (%d)", def_pos, use_kind));
assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
TRACE_LINEAR_SCAN(2, tty->print(" use "); opr->print(tty); tty->print_cr(" from %d to %d (%d)", from, to, use_kind));
assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
TRACE_LINEAR_SCAN(2, tty->print(" temp "); opr->print(tty); tty->print_cr(" temp_pos %d (%d)", temp_pos, use_kind));
assert(reg_num(opr) == opr->vreg_number() && !is_valid_reg_num(reg_numHi(opr)), "invalid optimization below");
TRACE_LINEAR_SCAN(2, tty->print_cr("Warning: def of reg %d at %d occurs without use", reg_num, def_pos));
TRACE_LINEAR_SCAN(2, tty->print_cr("Warning: dead value %d at %d in live intervals", reg_num, def_pos));
bool result_in_memory = res->is_virtual() && gen()->is_vreg_flag_set(res->vreg_number(), LIRGenerator::must_start_in_memory);
if (result_in_memory) {
return noUse;
return noUse;
// input operand must have a register instead of output operand (leads to better register allocation)
return shouldHaveRegister;
return noUse;
return mustHaveRegister;
bool result_in_memory = res->is_virtual() && gen()->is_vreg_flag_set(res->vreg_number(), LIRGenerator::must_start_in_memory);
if (result_in_memory) {
return mustHaveRegister;
// input operand must have a register instead of output operand (leads to better register allocation)
return mustHaveRegister;
return shouldHaveRegister;
#ifdef X86
return shouldHaveRegister;
case lir_cmp:
case lir_add:
case lir_sub:
case lir_mul:
case lir_div:
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");
return shouldHaveRegister;
case lir_add:
case lir_sub:
case lir_mul:
case lir_div:
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");
return shouldHaveRegister;
case lir_cmp:
case lir_add:
case lir_sub:
case lir_logic_and:
case lir_logic_or:
case lir_logic_xor:
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");
return shouldHaveRegister;
return mustHaveRegister;
#ifdef ASSERT
if (o->is_single_stack()) {
} else if (o->is_double_stack()) {
assert(block_of_op_with_id(move->id())->number_of_preds() == 0, "move from stack must be in first block");
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())));
int stack_slot = LinearScan::nof_regs + (move->in_opr()->is_single_stack() ? move->in_opr()->single_stack_ix() : move->in_opr()->double_stack_ix());
case lir_convert: {
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()));
case lir_cmove: {
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()));
int num_caller_save_registers = 0;
if (has_fpu_registers()) {
#ifdef X86
#ifdef X86
if (UseSSE > 0) {
for (int number = (int)live.get_next_one_offset(0, size); number < size; number = (int)live.get_next_one_offset(number + 1, size)) {
for (int k = 0; k < num_caller_save_registers; k++) {
if (*a != NULL) {
if (*b != NULL) {
if (*b != NULL) {
#ifndef PRODUCT
for (i = 0; i < interval_count(); i++) {
int num_found = 0;
num_found++;
int num_found = 0;
for (i = 0; i < interval_count(); i++) {
num_found++;
void LinearScan::create_unhandled_lists(Interval** list1, Interval** list2, bool (is_list1)(const Interval* i), bool (is_list2)(const Interval* i)) {
Interval* v;
if (v == NULL) continue;
if (is_list1(v)) {
assert(list1_prev == NULL || list1_prev->next() == Interval::end(), "linear list ends not with sentinel");
assert(list2_prev == NULL || list2_prev->next() == Interval::end(), "linear list ends not with sentinel");
if (_needs_full_resort) {
_needs_full_resort = false;
int sorted_len = 0;
int unsorted_idx;
int sorted_idx = 0;
sorted_len++;
sorted_idx++;
if (_needs_full_resort) {
_needs_full_resort = false;
if (new_len == 0) {
int old_idx = 0;
int new_idx = 0;
if (new_idx >= new_len || (old_idx < old_len && old_list->at(old_idx)->from() <= new_list->at(new_idx)->from())) {
old_idx++;
new_idx++;
create_unhandled_lists(&precolored_cpu_intervals, ¬_precolored_cpu_intervals, is_precolored_cpu_interval, is_virtual_cpu_interval);
if (has_fpu_registers()) {
create_unhandled_lists(&precolored_fpu_intervals, ¬_precolored_fpu_intervals, is_precolored_fpu_interval, is_virtual_fpu_interval);
#ifdef ASSERT
create_unhandled_lists(&precolored_fpu_intervals, ¬_precolored_fpu_intervals, is_precolored_fpu_interval, is_virtual_fpu_interval);
if (has_fpu_registers()) {
Interval* LinearScan::split_child_at_op_id(Interval* interval, int op_id, LIR_OpVisitState::OprMode mode) {
return result;
assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");
return split_child_at_op_id(interval_at(reg_num), block->first_lir_instruction_id(), LIR_OpVisitState::outputMode);
assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");
return split_child_at_op_id(interval_at(reg_num), block->last_lir_instruction_id() + 1, LIR_OpVisitState::outputMode);
assert(LinearScan::nof_regs <= reg_num && reg_num < num_virtual_regs(), "register number out of bounds");
void LinearScan::resolve_collect_mappings(BlockBegin* from_block, BlockBegin* to_block, MoveResolver &move_resolver) {
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)) {
assert(from_block->live_out().at(r) && to_block->live_in().at(r), "interval not live at this edge");
if (from_interval != to_interval && (from_interval->assigned_reg() != to_interval->assigned_reg() || from_interval->assigned_regHi() != to_interval->assigned_regHi())) {
void LinearScan::resolve_find_insert_pos(BlockBegin* from_block, BlockBegin* to_block, MoveResolver &move_resolver) {
TRACE_LINEAR_SCAN(4, tty->print_cr("inserting moves at end of from_block B%d", from_block->block_id()));
TRACE_LINEAR_SCAN(4, tty->print_cr("inserting moves at beginning of to_block B%d", to_block->block_id()));
#ifdef ASSERT
assert(from_block->lir()->instructions_list()->at(0)->as_OpLabel() != NULL, "block does not start with a label");
// insert necessary moves (spilling or reloading) at edges between blocks if interval has been split
for (i = 0; i < num_blocks; i++) {
if (block->number_of_preds() == 1 && block->number_of_sux() == 1 && block->number_of_exception_handlers() == 0) {
assert(instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block with successor must end with unconditional branch");
if (!block_completed.at(pred->linear_scan_number()) && !block_completed.at(sux->linear_scan_number())) {
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()));
for (i = 0; i < num_blocks; i++) {
for (int s = 0; s < num_sux; s++) {
TRACE_LINEAR_SCAN(3, tty->print_cr("**** processing edge between B%d and B%d", from_block->block_id(), to_block->block_id()));
void LinearScan::resolve_exception_entry(BlockBegin* block, int reg_num, MoveResolver &move_resolver) {
_needs_full_resort = true;
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)) {
void LinearScan::resolve_exception_edge(XHandler* handler, int throwing_op_id, int reg_num, Phi* phi, MoveResolver &move_resolver) {
if (!from_interval->always_in_memory() || from_interval->canonical_spill_slot() != to_interval->assigned_reg()) {
void LinearScan::resolve_exception_edge(XHandler* handler, int throwing_op_id, MoveResolver &move_resolver) {
TRACE_LINEAR_SCAN(4, tty->print_cr("resolving exception handler B%d: throwing_op_id=%d", handler->entry_block()->block_id(), throwing_op_id));
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)) {
for (i = 0; i < num_blocks; i++) {
for (i = 0; i < num_blocks; i++) {
#ifdef ASSERT
return reg;
return opr;
switch (type) {
case T_OBJECT: {
case T_ADDRESS: {
#ifdef __SOFTFP__
case T_INT: {
#ifdef __SOFTFP__
case T_LONG: {
#ifdef _LP64
#ifndef __SOFTFP__
case T_FLOAT: {
#ifdef X86
case T_DOUBLE: {
#ifdef X86
assert(interval->assigned_regHi() == any_reg, "must not have hi register (double xmm values are stored in one register)");
#ifdef SPARC
assert(interval->assigned_regHi() >= pd_first_fpu_reg && interval->assigned_regHi() <= pd_last_fpu_reg, "no fpu register");
assert(assigned_reg % 2 == 0 && assigned_reg + 1 == interval->assigned_regHi(), "must be sequential and even");
LIR_Opr result = LIR_OprFact::double_fpu(interval->assigned_regHi() - pd_first_fpu_reg, assigned_reg - pd_first_fpu_reg);
assert(interval->assigned_regHi() >= pd_first_fpu_reg && interval->assigned_regHi() <= pd_last_fpu_reg, "no fpu register");
assert(assigned_reg % 2 == 0 && assigned_reg + 1 == interval->assigned_regHi(), "must be sequential and even");
LIR_Opr result = LIR_OprFact::double_fpu(assigned_reg - pd_first_fpu_reg, interval->assigned_regHi() - pd_first_fpu_reg);
assert(interval->assigned_regHi() == any_reg, "must not have hi register (double fpu values are stored in one register on Intel)");
return result;
#ifdef ASSERT
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)");
#ifdef X86
if (opr->is_last_use() || op_id == interval->to() || (op_id != -1 && interval->has_hole_between(op_id, op_id + 1))) {
assert(op_id == -1 || !is_block_begin(op_id), "holes at begin of block may also result from control flow");
assert(!gen()->is_vreg_flag_set(opr->vreg_number(), LIRGenerator::callee_saved) || !FrameMap::is_caller_save_register(res), "bad allocation");
return res;
#ifdef ASSERT
switch (code) {
assert(stack_end >= -Bytecodes::depth(code), "must have non-empty expression stack at if bytecode");
OopMap* LinearScan::compute_oop_map(IntervalWalker* iw, LIR_Op* op, CodeEmitInfo* info, bool is_call_site) {
bool is_patch_info = false;
for (Interval* interval = iw->active_first(fixedKind); interval != Interval::end(); interval = interval->next()) {
assert(interval->current_from() <= op->id() && op->id() <= interval->current_to(), "interval should not be active otherwise");
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");
assert(interval->assigned_reg() < LinearScan::nof_regs, "interval is on stack, so stack slot is registered twice");
for (int i = 0; i < locks_count; i++) {
return map;
ConstantOopWriteValue* LinearScan::_oop_null_scope_value = new (ResourceObj::C_HEAP) ConstantOopWriteValue(NULL);
_scope_value_cache = ScopeValueArray((LinearScan::nof_cpu_regs + frame_map()->argcount() + max_spills()) * 2, NULL);
int LinearScan::append_scope_value_for_constant(LIR_Opr opr, GrowableArray<ScopeValue*>* scope_values) {
case T_OBJECT: {
case T_FLOAT: {
switch (value) {
case T_DOUBLE: {
#ifdef _LP64
case T_ADDRESS: {
#ifdef _LP64
int LinearScan::append_scope_value_for_operand(LIR_Opr opr, GrowableArray<ScopeValue*>* scope_values) {
DEBUG_ONLY(assert_equal(sv, location_for_name(stack_idx, is_oop ? Location::oop : Location::normal)));
DEBUG_ONLY(assert_equal(sv, new LocationValue(Location::new_reg_loc(is_oop ? Location::oop : int_loc_type, frame_map()->regname(opr)))));
#ifdef X86
#ifdef X86
assert(use_fpu_stack_allocation(), "should not have float stack values without fpu stack allocation (all floats must be SSE2)");
#ifndef __SOFTFP__
#ifndef VM_LITTLE_ENDIAN
if (! float_saved_as_double) {
#ifdef _LP64
#ifdef _LP64
#ifdef X86
# ifdef _LP64
// On SPARC, fpu_regnrLo/fpu_regnrHi represents the two halves of
#ifdef X86
assert(use_fpu_stack_allocation(), "should not have float stack values without fpu stack allocation (all floats must be SSE2)");
assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation (only fpu_regnrLo is used)");
#ifdef SPARC
assert(opr->fpu_regnrLo() == opr->fpu_regnrHi() + 1, "assumed in calculation (only fpu_regnrHi is used)");
#ifdef ARM
assert(opr->fpu_regnrHi() == opr->fpu_regnrLo() + 1, "assumed in calculation (only fpu_regnrLo is used)");
#ifdef PPC
assert(opr->fpu_regnrLo() == opr->fpu_regnrHi(), "assumed in calculation (only fpu_regnrHi is used)");
#ifdef VM_LITTLE_ENDIAN
#ifdef _LP64
int LinearScan::append_scope_value(int op_id, Value value, GrowableArray<ScopeValue*>* scope_values) {
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)");
assert(con != NULL || opr->is_virtual(), "asumption: non-Constant instructions have only virtual operands");
assert(!has_call(op_id) || opr->is_stack() || !is_caller_save(reg_num(opr)), "can not have caller-save register operands at calls");
IRScopeDebugInfo* LinearScan::compute_debug_info_for_scope(int op_id, IRScope* cur_scope, ValueStack* cur_state, ValueStack* innermost_state) {
caller_debug_info = compute_debug_info_for_scope(op_id, cur_scope->caller(), caller_state, innermost_state);
if (nof_locals > 0) {
int pos = 0;
for(int i = 0; i < nof_locals; i++) {
if (nof_stack > 0) {
int pos = 0;
if (nof_locks > 0) {
int lock_offset = cur_state->caller_state() != NULL ? cur_state->caller_state()->total_locks_size() : 0;
for (int i = 0; i < nof_locks; i++) {
return new IRScopeDebugInfo(cur_scope, cur_state->bci(), locals, expressions, monitors, caller_debug_info);
info->_scope_debug_info = compute_debug_info_for_scope(op_id, innermost_scope, innermost_state, innermost_state);
DEBUG_ONLY(assert_equal(info->_scope_debug_info, compute_debug_info_for_scope(op_id, innermost_scope, innermost_state, innermost_state)));
bool has_dead = false;
for (int j = 0; j < num_inst; j++) {
has_dead = true;
if (!use_fpu_stack_allocation()) {
#ifdef ASSERT
has_dead = true;
if (has_dead) {
int insert_point = 0;
for (int j = 0; j < num_inst; j++) {
if (insert_point != j) {
insert_point++;
for (int i = 0; i < num_blocks; i++) {
if (use_fpu_stack_allocation()) {
#ifndef PRODUCT
for (unsigned int i = 0; i < b.size(); i++) {
for (i = 0; i < interval_count(); i++) {
for (i = 0; i < block_count(); i++) {
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());
if (PrintCFGToFile) {
#ifdef ASSERT
TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying intervals ******************************************"));
TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying that no oops are in fixed intervals ****************"));
TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying that unpinned constants are not alive across block boundaries"));
TRACE_LINEAR_SCAN(2, tty->print_cr("********* verifying register allocation ********************************"));
TRACE_LINEAR_SCAN(2, tty->print_cr("********* no errors found **********************************************"));
bool has_error = false;
for (int i = 0; i < len; i++) {
has_error = true;
has_error = true;
has_error = true;
has_error = true;
has_error = true;
has_error = true;
has_error = true;
if (i1->intersects(i2) && (r1 == r2 || r1 == r2Hi || (r1Hi != any_reg && (r1Hi == r2 || r1Hi == r2Hi)))) {
tty->print_cr("Intervals %d and %d overlap and have the same register assigned", i1->reg_num(), i2->reg_num());
has_error = true;
for (int i = 0; i < block_count(); i++) {
bool check_live = true;
check_live = false;
if (check_live) {
bool ok = false;
ok = true;
ok = true;
for (int i = 0; i < num_blocks; i++) {
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)) {
assert(value->operand()->is_register() && value->operand()->is_virtual(), "value must have virtual operand");
// TKR assert(value->as_Constant() == NULL || value->is_pinned(), "only pinned constants can be alive accross block boundaries");
void set_state_for_block(BlockBegin* block, IntervalList* saved_state) { _saved_states.at_put(block->block_id(), saved_state); }
void add_to_work_list(BlockBegin* block) { if (!_work_list.contains(block)) _work_list.append(block); }
if (interval->assigned_regHi() != LinearScan::any_reg && interval->assigned_regHi() < state_size()) {
for (int i = 0; i < state_size(); i++) {
bool saved_state_correct = true;
for (int i = 0; i < state_size(); i++) {
saved_state_correct = false;
TRACE_LINEAR_SCAN(4, tty->print_cr("process_successor B%d: invalidating slot %d", block->block_id(), i));
if (saved_state_correct) {
TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: previous visit already correct", block->block_id()));
TRACE_LINEAR_SCAN(2, tty->print_cr("process_successor B%d: must re-visit because input state changed", block->block_id()));
return copy_state;
tty->print_cr("!! Error in register allocation: register %d does not contain interval %d", reg, interval->reg_num());
bool has_error = false;
#ifdef X86
// set temp operands (some operations use temp operands also as output operands, so can't set them NULL)
_multiple_reads_allowed(false),
_register_blocked[i] = 0;
#ifdef ASSERT
assert(_mapping_from.length() == 0 && _mapping_from_opr.length() == 0 && _mapping_to.length() == 0, "list must be empty before and after processing");
if (!_multiple_reads_allowed) {
assert(_mapping_from.at(i) == NULL || _mapping_from.at(i) != _mapping_from.at(j), "cannot read from same interval twice");
BitMap used_regs(LinearScan::nof_regs + allocator()->frame_map()->argcount() + allocator()->max_spills());
if (!_multiple_reads_allowed) {
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");
// check if assigned_reg and assigned_regHi of the to-interval are not blocked (or only blocked by from)
if (register_blocked(reg) > 1 || (register_blocked(reg) == 1 && reg != from_reg && reg != from_regHi)) {
if (register_blocked(reg) > 1 || (register_blocked(reg) == 1 && reg != from_reg && reg != from_regHi)) {
if (!_multiple_reads_allowed) {
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()));
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()));
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));
bool processed_interval = false;
processed_interval = true;
spill_candidate = i;
if (!processed_interval) {
if (spill_slot < 0) {
TRACE_LINEAR_SCAN(4, tty->print_cr("created new Interval %d for spilling", spill_interval->reg_num()));
_multiple_reads_allowed = false;
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));
assert(_insert_list == NULL && _insert_idx == -1, "use move_insert_position instead of set_insert_position when data already set");
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));
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()));
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()));
if (has_mappings()) {
#ifndef PRODUCT
_split_children(0),
_insert_move_when_activated(false),
_split_parent = this;
_current_split_child = this;
r = r->next();
return r->to();
#ifdef ASSERT
assert(i1->canonical_spill_slot() == canonical_spill_slot(), "must be equal for all split children");
if (!search_split_child) {
return _register_hint;
return _register_hint;
for (int i = 0; i < len; i++) {
return cur;
return NULL;
result = this;
for (i = 0; i < len; i++) {
#ifdef ASSERT
for (i = 0; i < len; i++) {
tty->print_cr("two valid result intervals found for op_id %d: %d and %d", op_id, result->reg_num(), tmp->reg_num());
return result;
return result;
for (int i = 0; i < len; i++) {
return max_jint;
return max_jint;
return max_jint;
int prev = 0;
return prev;
return prev;
#ifdef ASSERT
assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
assert(first() == Range::end() || to < first()->next()->from(), "not inserting at begin of interval");
return result;
#ifdef ASSERT
assert(_use_pos_and_kinds.length() + result->_use_pos_and_kinds.length() == total_len, "missed some entries");
assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
assert(result->_use_pos_and_kinds.at(i + 1) >= firstValidKind && result->_use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
return result;
assert(split_pos > _first->from() && split_pos <= _first->to(), "can only split inside first range");
return result;
#ifndef PRODUCT
const char* SpillState2Name[] = { "no definition", "no spill store", "one spill store", "store at definition", "start in memory", "no optimization" };
const char* type_name;
#ifdef X86
out->print("%d %d ", split_parent()->reg_num(), (register_hint(false) != NULL ? register_hint(false)->reg_num() : -1));
int prev = 0;
assert(_use_pos_and_kinds.at(i + 1) >= firstValidKind && _use_pos_and_kinds.at(i + 1) <= lastValidKind, "invalid use kind");
IntervalWalker::IntervalWalker(LinearScan* allocator, Interval* unhandled_fixed_first, Interval* unhandled_any_first)
assert(interval->from() >= current()->current_from(), "cannot append new interval before current walk position");
while (cur->from() < interval->from() || (cur->from() == interval->from() && cur->first_usage(noUse) < interval->first_usage(noUse))) {
bool deleted;
bool range_has_changed = false;
range_has_changed = true;
if (range_has_changed) {
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");
TRACE_LINEAR_SCAN(2, if (_current_position < id) { tty->cr(); tty->print_cr("walk_to(%d) **************************************************************", id); })
if (is_active) {
if (activate_current()) {
void IntervalWalker::interval_moved(Interval* interval, IntervalKind kind, IntervalState from, IntervalState to) {
#ifndef PRODUCT
switch(state) {\
default: ShouldNotReachHere(); \
LinearScanWalker::LinearScanWalker(LinearScan* allocator, Interval* unhandled_fixed_first, Interval* unhandled_any_first)
if (!only_process_use_pos) {
assert(reg < LinearScan::nof_regs, "interval must have a register assigned (stack slots not allowed)");
inline void LinearScanWalker::set_use_pos(int reg, Interval* i, int use_pos, bool only_process_use_pos) {
if (!only_process_use_pos) {
assert(list->assigned_reg() < LinearScan::nof_regs, "active interval must have a register assigned");
assert(op_id > 0 && allocator()->block_of_op_with_id(op_id - 2) == op_block, "cannot insert move at block boundary");
index++;
int LinearScanWalker::find_optimal_split_pos(BlockBegin* min_block, BlockBegin* max_block, int max_split_pos) {
assert(optimal_split_pos > allocator()->max_lir_op_id() || allocator()->is_block_begin(optimal_split_pos), "algorithm must move split pos to block boundary");
return optimal_split_pos;
int LinearScanWalker::find_optimal_split_pos(Interval* it, int min_split_pos, int max_split_pos, bool do_loop_optimization) {
// Use the block before as min_block, because then min_block->last_lir_instruction_id() + 2 == min_split_pos
TRACE_LINEAR_SCAN(4, tty->print_cr(" cannot move split pos to block boundary because min_pos and max_pos are in same block"));
} else if (it->has_hole_between(max_split_pos - 1, max_split_pos) && !allocator()->is_block_begin(max_split_pos)) {
TRACE_LINEAR_SCAN(4, tty->print_cr(" interval has hole just before max_split_pos, so splitting at max_split_pos"));
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()));
if (do_loop_optimization) {
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()));
assert(loop_block != min_block, "loop_block and min_block must be different because block boundary is needed between");
optimal_split_pos = find_optimal_split_pos(min_block, loop_block, loop_block->last_lir_instruction_id() + 2);
return optimal_split_pos;
TRACE_LINEAR_SCAN(4, tty->print_cr(" no split necessary because optimal split position is at end of interval"));
// must calculate this before the actual split is performed and before split position is moved to odd op_id
bool move_necessary = !allocator()->is_block_begin(optimal_split_pos) && !it->has_hole_between(optimal_split_pos - 1, optimal_split_pos);
assert(allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 1), "split pos must be odd when not on block boundary");
assert(!allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 0), "split pos must be even on block boundary");
TRACE_LINEAR_SCAN(2, tty->print_cr(" split interval in two parts (insert_move_when_activated: %d)", move_necessary));
TRACE_LINEAR_SCAN(2, tty->print_cr(" spilling entire interval because split pos is at beginning of interval"));
assert(it->first_usage(shouldHaveRegister) > current_position(), "interval must not have use position before current_position");
TRACE_LINEAR_SCAN(4, tty->print_cr(" kicking out interval %d out of its register because it is never used", parent->reg_num()));
assert(allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 1), "split pos must be odd when not on block boundary");
assert(!allocator()->is_block_begin(optimal_split_pos) || (optimal_split_pos % 2 == 0), "split pos must be even on block boundary");
TRACE_LINEAR_SCAN(4, tty->print_cr(" inserting move from interval %d to %d", it->reg_num(), spilled_part->reg_num()));
void LinearScanWalker::split_when_partial_register_available(Interval* it, int register_available_until) {
int min_split_pos = MAX2(it->previous_usage(shouldHaveRegister, register_available_until), it->from() + 1);
assert(it->has_hole_between(current_pos - 1, current_pos + 1), "interval can not be inactive otherwise");
assert(it->next_usage(mustHaveRegister, current_pos) == max_jint, "the remaining part is spilled to stack and therefore has no register");
int LinearScanWalker::find_free_reg(int reg_needed_until, int interval_to, int hint_reg, int ignore_reg, bool* need_split) {
if (i == ignore_reg) {
if (min_full_reg == any_reg || i == hint_reg || (_use_pos[i] < _use_pos[min_full_reg] && min_full_reg != hint_reg)) {
min_full_reg = i;
if (max_partial_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_partial_reg] && max_partial_reg != hint_reg)) {
max_partial_reg = i;
return min_full_reg;
*need_split = true;
return max_partial_reg;
return any_reg;
int LinearScanWalker::find_free_double_reg(int reg_needed_until, int interval_to, int hint_reg, bool* need_split) {
if (min_full_reg == any_reg || i == hint_reg || (_use_pos[i] < _use_pos[min_full_reg] && min_full_reg != hint_reg)) {
min_full_reg = i;
if (max_partial_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_partial_reg] && max_partial_reg != hint_reg)) {
max_partial_reg = i;
return min_full_reg;
*need_split = true;
return max_partial_reg;
return any_reg;
init_use_lists(true);
assert(unhandled_first(fixedKind) == Interval::end(), "must not have unhandled fixed intervals because all fixed intervals have a use at position 0");
// only intervals overlapping with cur are processed, non-overlapping invervals can be ignored safely
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]));
TRACE_LINEAR_SCAN(4, tty->print(" hint registers %d, %d from interval ", hint_reg, hint_regHi); register_hint->print());
assert(cur->assigned_reg() == any_reg && cur->assigned_regHi() == any_reg, "register already assigned to interval");
bool need_split = false;
if (_adjacent_regs) {
if (need_split) {
int LinearScanWalker::find_locked_reg(int reg_needed_until, int interval_to, int hint_reg, int ignore_reg, bool* need_split) {
if (i == ignore_reg) {
if (max_reg == any_reg || i == hint_reg || (_use_pos[i] > _use_pos[max_reg] && max_reg != hint_reg)) {
max_reg = i;
*need_split = true;
return max_reg;
int LinearScanWalker::find_locked_double_reg(int reg_needed_until, int interval_to, int hint_reg, bool* need_split) {
max_reg = i;
*need_split = true;
return max_reg;
init_use_lists(false);
assert(unhandled_first(fixedKind) == Interval::end(), "must not have unhandled fixed intervals because all fixed intervals have a use at position 0");
#ifndef PRODUCT
int split_pos = 0;
int use_pos = 0;
bool need_split = false;
if (_adjacent_regs) {
if (reg == any_reg || (_num_phys_regs == 2 && regHi == any_reg) || use_pos <= cur->first_usage(mustHaveRegister)) {
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));
assert(false, "cannot spill interval that is used in first instruction (possible reason: no register found)");
if (need_split) {
#ifdef X86
TRACE_LINEAR_SCAN(4, tty->print_cr(" free register cannot be available because all registers blocked by following call"));
return in->is_virtual() && res->is_virtual() && in->vreg_number() == from->reg_num() && res->vreg_number() == to->reg_num();
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)) {
assert(cur->first_usage(mustHaveRegister) == begin_pos, "must have use position at begin of interval because of move");
assert(end_hint->first_usage(mustHaveRegister) == end_pos, "must have use position at begin of interval because of move");
// register_hint is not spilled at begin_pos, so it would not be benefitial to immediately spill cur
assert(register_hint->canonical_spill_slot() != -1, "must be set when part of interval was spilled");
bool result = true;
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()));
TRACE_LINEAR_SCAN(4, tty->print_cr(" interval has spill slot assigned (method parameter) -> split it before first use"));
result = false;
} else if (allocator()->gen()->is_vreg_flag_set(cur->reg_num(), LIRGenerator::must_start_in_memory)) {
TRACE_LINEAR_SCAN(4, tty->print_cr(" interval must start in stack slot -> split it before first use"));
assert(cur->assigned_reg() == any_reg && cur->assigned_regHi() == any_reg, "register already assigned");
result = false;
result = false;
assert(cur->current_split_child()->reg_num() != cur->reg_num(), "cannot insert move between same interval");
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()));
return NULL;
if (decrement_index) {
if (move1->info() == move2->info() && move1->in_opr() == move2->in_opr() && move1->result_opr() == move2->result_opr()) {
for (i = 0; i < num_preds; i++) {
assert(pred_instructions->last()->code() == lir_branch, "block with successor must end with branch");
assert(pred_instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block must end with unconditional branch");
TRACE_LINEAR_SCAN(4, tty->print("found instruction that is equal in all %d predecessors: ", num_preds); op->print());
for (i = 0; i < num_preds; i++) {
remove_cur_instruction(i, true);
assert(cur_instructions->last()->code() == lir_branch, "block with successor must end with branch");
assert(cur_instructions->last()->as_OpBranch()->cond() == lir_cond_always, "block must end with unconditional branch");
if (branch->info() != NULL || (branch->code() != lir_branch && branch->code() != lir_cond_float_branch)) {
#ifdef ASSERT
if ((op->code() == lir_branch || op->code() == lir_cond_float_branch) && ((LIR_OpBranch*)op)->block() != NULL) {
for (i = 0; i < num_sux; i++) {
TRACE_LINEAR_SCAN(4, tty->print("----- found instruction that is equal in all %d successors: ", num_sux); op->print());
insert_idx++;
for (i = 0; i < num_sux; i++) {
remove_cur_instruction(i, false);
if (osr_entry) {
void ControlFlowOptimizer::reorder_short_loop(BlockList* code, BlockBegin* header_block, int header_idx) {
assert(code->at(end_idx)->is_set(BlockBegin::backward_branch_target_flag), "must be backward branch target");
if (block->number_of_sux() != 1 || block->number_of_exception_handlers() != 0 || block->is_entry_block()) {
assert(instructions->last()->as_OpBranch()->cond() == lir_cond_always, "branch must be unconditional");
assert(instructions->last()->as_OpBranch()->block() == block->sux_at(0), "branch target must be the successor");
void ControlFlowOptimizer::substitute_branch_target(BlockBegin* block, BlockBegin* target_from, BlockBegin* target_to) {
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()));
int old_pos = 0;
int new_pos = 0;
new_pos++;
old_pos++;
TRACE_LINEAR_SCAN(3, tty->print_cr("Deleting unconditional branch at end of block B%d", block->block_id()));
TRACE_LINEAR_SCAN(3, tty->print_cr("Negating conditional branch and deleting unconditional branch at end of block B%d", block->block_id()));
#ifdef ASSERT
if (pred_last_branch->block() == block && pred_last_branch->cond() == lir_cond_always && pred_last_branch->info() == NULL) {
#ifdef ASSERT
#ifdef ASSERT
assert(op_branch->block() == NULL || code->index_of(op_branch->block()) != -1, "branch target not valid");
assert(op_branch->ublock() == NULL || code->index_of(op_branch->ublock()) != -1, "branch target not valid");
#ifndef PRODUCT
switch (counter_idx) {
if (counter_idx == counter_fpu_method || counter_idx == counter_loop_method || counter_idx == counter_exception_method) {
return counter_method;
return counter_block;
return counter_instruction;
return counter_move_total;
return invalid_counter;
for (int i = 0; i < number_of_counters; i++) {
_counters_sum[i] = 0;
for (int i = 0; i < number_of_counters; i++) {
for (int i = 0; i < number_of_counters; i++) {
if (_counters_max[i] >= 0) {
bool has_xhandlers = false;
has_xhandlers = true;
case lir_std_entry:
case lir_rtcall:
case lir_static_call:
case lir_optvirtual_call:
case lir_move: {
case lir_branch:
case lir_cond_float_branch: {
case lir_neg:
case lir_add:
case lir_sub:
case lir_mul:
case lir_mul_strictfp:
case lir_div:
case lir_div_strictfp:
case lir_rem:
case lir_sqrt:
case lir_sin:
case lir_cos:
case lir_abs:
case lir_log10:
case lir_log:
case lir_pow:
case lir_exp:
case lir_logic_and:
case lir_logic_or:
case lir_logic_xor:
case lir_shl:
case lir_shr:
case lir_alloc_object:
case lir_monaddr:
case lir_lock:
case lir_null_check:
case lir_leal:
case lir_instanceof:
case lir_checkcast:
case lir_fpop_raw:
case lir_fxch:
case lir_nop:
case lir_push:
case lir_pop:
case lir_convert:
case lir_roundfp:
if (has_xhandlers) {
for (int i = 0; i < number_of_timers; i++) {
switch (idx) {
if (TimeEachLinearScan) {
for (int i = 0; i < number_of_timers; i++) {
if (TimeEachLinearScan) {
double total = 0;
tty->print("@ %d ", allocator->block_at(allocator->block_count() - 1)->last_lir_instruction_id() / 2);
if (TimeLinearScan) {
for (int i = 0; i < number_of_timers; i++) {
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);