Path: blob/master/src/hotspot/cpu/x86/c1_LinearScan_x86.cpp
41144 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_Instruction.hpp"26#include "c1/c1_LinearScan.hpp"27#include "utilities/bitMap.inline.hpp"282930#ifdef _LP6431void LinearScan::allocate_fpu_stack() {32// No FPU stack used on x86-6433}34#else35//----------------------------------------------------------------------36// Allocation of FPU stack slots (Intel x86 only)37//----------------------------------------------------------------------3839void LinearScan::allocate_fpu_stack() {40// First compute which FPU registers are live at the start of each basic block41// (To minimize the amount of work we have to do if we have to merge FPU stacks)42if (ComputeExactFPURegisterUsage) {43Interval* intervals_in_register, *intervals_in_memory;44create_unhandled_lists(&intervals_in_register, &intervals_in_memory, is_in_fpu_register, NULL);4546// ignore memory intervals by overwriting intervals_in_memory47// the dummy interval is needed to enforce the walker to walk until the given id:48// without it, the walker stops when the unhandled-list is empty -> live information49// beyond this point would be incorrect.50Interval* dummy_interval = new Interval(any_reg);51dummy_interval->add_range(max_jint - 2, max_jint - 1);52dummy_interval->set_next(Interval::end());53intervals_in_memory = dummy_interval;5455IntervalWalker iw(this, intervals_in_register, intervals_in_memory);5657const int num_blocks = block_count();58for (int i = 0; i < num_blocks; i++) {59BlockBegin* b = block_at(i);6061// register usage is only needed for merging stacks -> compute only62// when more than one predecessor.63// the block must not have any spill moves at the beginning (checked by assertions)64// spill moves would use intervals that are marked as handled and so the usage bit65// would been set incorrectly6667// NOTE: the check for number_of_preds > 1 is necessary. A block with only one68// predecessor may have spill moves at the begin of the block.69// If an interval ends at the current instruction id, it is not possible70// to decide if the register is live or not at the block begin -> the71// register information would be incorrect.72if (b->number_of_preds() > 1) {73int id = b->first_lir_instruction_id();74ResourceBitMap regs(FrameMap::nof_fpu_regs);7576iw.walk_to(id); // walk after the first instruction (always a label) of the block77assert(iw.current_position() == id, "did not walk completely to id");7879// Only consider FPU values in registers80Interval* interval = iw.active_first(fixedKind);81while (interval != Interval::end()) {82int reg = interval->assigned_reg();83assert(reg >= pd_first_fpu_reg && reg <= pd_last_fpu_reg, "no fpu register");84assert(interval->assigned_regHi() == -1, "must not have hi register (doubles stored in one register)");85assert(interval->from() <= id && id < interval->to(), "interval out of range");8687#ifndef PRODUCT88if (TraceFPURegisterUsage) {89tty->print("fpu reg %d is live because of ", reg - pd_first_fpu_reg); interval->print();90}91#endif9293regs.set_bit(reg - pd_first_fpu_reg);94interval = interval->next();95}9697b->set_fpu_register_usage(regs);9899#ifndef PRODUCT100if (TraceFPURegisterUsage) {101tty->print("FPU regs for block %d, LIR instr %d): ", b->block_id(), id); regs.print_on(tty); tty->cr();102}103#endif104}105}106}107108FpuStackAllocator alloc(ir()->compilation(), this);109_fpu_stack_allocator = &alloc;110alloc.allocate();111_fpu_stack_allocator = NULL;112}113114115FpuStackAllocator::FpuStackAllocator(Compilation* compilation, LinearScan* allocator)116: _compilation(compilation)117, _allocator(allocator)118, _lir(NULL)119, _pos(-1)120, _sim(compilation)121, _temp_sim(compilation)122{}123124void FpuStackAllocator::allocate() {125int num_blocks = allocator()->block_count();126for (int i = 0; i < num_blocks; i++) {127// Set up to process block128BlockBegin* block = allocator()->block_at(i);129intArray* fpu_stack_state = block->fpu_stack_state();130131#ifndef PRODUCT132if (TraceFPUStack) {133tty->cr();134tty->print_cr("------- Begin of new Block %d -------", block->block_id());135}136#endif137138assert(fpu_stack_state != NULL ||139block->end()->as_Base() != NULL ||140block->is_set(BlockBegin::exception_entry_flag),141"FPU stack state must be present due to linear-scan order for FPU stack allocation");142// note: exception handler entries always start with an empty fpu stack143// because stack merging would be too complicated144145if (fpu_stack_state != NULL) {146sim()->read_state(fpu_stack_state);147} else {148sim()->clear();149}150151#ifndef PRODUCT152if (TraceFPUStack) {153tty->print("Reading FPU state for block %d:", block->block_id());154sim()->print();155tty->cr();156}157#endif158159allocate_block(block);160CHECK_BAILOUT();161}162}163164void FpuStackAllocator::allocate_block(BlockBegin* block) {165bool processed_merge = false;166LIR_OpList* insts = block->lir()->instructions_list();167set_lir(block->lir());168set_pos(0);169170171// Note: insts->length() may change during loop172while (pos() < insts->length()) {173LIR_Op* op = insts->at(pos());174_debug_information_computed = false;175176#ifndef PRODUCT177if (TraceFPUStack) {178op->print();179}180check_invalid_lir_op(op);181#endif182183LIR_OpBranch* branch = op->as_OpBranch();184LIR_Op1* op1 = op->as_Op1();185LIR_Op2* op2 = op->as_Op2();186LIR_OpCall* opCall = op->as_OpCall();187188if (branch != NULL && branch->block() != NULL) {189if (!processed_merge) {190// propagate stack at first branch to a successor191processed_merge = true;192bool required_merge = merge_fpu_stack_with_successors(block);193194assert(!required_merge || branch->cond() == lir_cond_always, "splitting of critical edges should prevent FPU stack mismatches at cond branches");195}196197} else if (op1 != NULL) {198handle_op1(op1);199} else if (op2 != NULL) {200handle_op2(op2);201} else if (opCall != NULL) {202handle_opCall(opCall);203}204205compute_debug_information(op);206207set_pos(1 + pos());208}209210// Propagate stack when block does not end with branch211if (!processed_merge) {212merge_fpu_stack_with_successors(block);213}214}215216217void FpuStackAllocator::compute_debug_information(LIR_Op* op) {218if (!_debug_information_computed && op->id() != -1 && allocator()->has_info(op->id())) {219visitor.visit(op);220221// exception handling222if (allocator()->compilation()->has_exception_handlers()) {223XHandlers* xhandlers = visitor.all_xhandler();224int n = xhandlers->length();225for (int k = 0; k < n; k++) {226allocate_exception_handler(xhandlers->handler_at(k));227}228} else {229assert(visitor.all_xhandler()->length() == 0, "missed exception handler");230}231232// compute debug information233int n = visitor.info_count();234assert(n > 0, "should not visit operation otherwise");235236for (int j = 0; j < n; j++) {237CodeEmitInfo* info = visitor.info_at(j);238// Compute debug information239allocator()->compute_debug_info(info, op->id());240}241}242_debug_information_computed = true;243}244245void FpuStackAllocator::allocate_exception_handler(XHandler* xhandler) {246if (!sim()->is_empty()) {247LIR_List* old_lir = lir();248int old_pos = pos();249intArray* old_state = sim()->write_state();250251#ifndef PRODUCT252if (TraceFPUStack) {253tty->cr();254tty->print_cr("------- begin of exception handler -------");255}256#endif257258if (xhandler->entry_code() == NULL) {259// need entry code to clear FPU stack260LIR_List* entry_code = new LIR_List(_compilation);261entry_code->jump(xhandler->entry_block());262xhandler->set_entry_code(entry_code);263}264265LIR_OpList* insts = xhandler->entry_code()->instructions_list();266set_lir(xhandler->entry_code());267set_pos(0);268269// Note: insts->length() may change during loop270while (pos() < insts->length()) {271LIR_Op* op = insts->at(pos());272273#ifndef PRODUCT274if (TraceFPUStack) {275op->print();276}277check_invalid_lir_op(op);278#endif279280switch (op->code()) {281case lir_move:282assert(op->as_Op1() != NULL, "must be LIR_Op1");283assert(pos() != insts->length() - 1, "must not be last operation");284285handle_op1((LIR_Op1*)op);286break;287288case lir_branch:289assert(op->as_OpBranch()->cond() == lir_cond_always, "must be unconditional branch");290assert(pos() == insts->length() - 1, "must be last operation");291292// remove all remaining dead registers from FPU stack293clear_fpu_stack(LIR_OprFact::illegalOpr);294break;295296default:297// other operations not allowed in exception entry code298ShouldNotReachHere();299}300301set_pos(pos() + 1);302}303304#ifndef PRODUCT305if (TraceFPUStack) {306tty->cr();307tty->print_cr("------- end of exception handler -------");308}309#endif310311set_lir(old_lir);312set_pos(old_pos);313sim()->read_state(old_state);314}315}316317318int FpuStackAllocator::fpu_num(LIR_Opr opr) {319assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");320return opr->is_single_fpu() ? opr->fpu_regnr() : opr->fpu_regnrLo();321}322323int FpuStackAllocator::tos_offset(LIR_Opr opr) {324return sim()->offset_from_tos(fpu_num(opr));325}326327328LIR_Opr FpuStackAllocator::to_fpu_stack(LIR_Opr opr) {329assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");330331int stack_offset = tos_offset(opr);332if (opr->is_single_fpu()) {333return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();334} else {335assert(opr->is_double_fpu(), "shouldn't call this otherwise");336return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();337}338}339340LIR_Opr FpuStackAllocator::to_fpu_stack_top(LIR_Opr opr, bool dont_check_offset) {341assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");342assert(dont_check_offset || tos_offset(opr) == 0, "operand is not on stack top");343344int stack_offset = 0;345if (opr->is_single_fpu()) {346return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();347} else {348assert(opr->is_double_fpu(), "shouldn't call this otherwise");349return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();350}351}352353354355void FpuStackAllocator::insert_op(LIR_Op* op) {356lir()->insert_before(pos(), op);357set_pos(1 + pos());358}359360361void FpuStackAllocator::insert_exchange(int offset) {362if (offset > 0) {363LIR_Op1* fxch_op = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);364insert_op(fxch_op);365sim()->swap(offset);366367#ifndef PRODUCT368if (TraceFPUStack) {369tty->print("Exchanged register: %d New state: ", sim()->get_slot(0)); sim()->print(); tty->cr();370}371#endif372373}374}375376void FpuStackAllocator::insert_exchange(LIR_Opr opr) {377insert_exchange(tos_offset(opr));378}379380381void FpuStackAllocator::insert_free(int offset) {382// move stack slot to the top of stack and then pop it383insert_exchange(offset);384385LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);386insert_op(fpop);387sim()->pop();388389#ifndef PRODUCT390if (TraceFPUStack) {391tty->print("Inserted pop New state: "); sim()->print(); tty->cr();392}393#endif394}395396397void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr) {398if (sim()->contains(fpu_num(opr))) {399int res_slot = tos_offset(opr);400insert_free(res_slot);401}402}403404void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr, LIR_Opr ignore) {405if (fpu_num(opr) != fpu_num(ignore) && sim()->contains(fpu_num(opr))) {406int res_slot = tos_offset(opr);407insert_free(res_slot);408}409}410411void FpuStackAllocator::insert_copy(LIR_Opr from, LIR_Opr to) {412int offset = tos_offset(from);413LIR_Op1* fld = new LIR_Op1(lir_fld, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);414insert_op(fld);415416sim()->push(fpu_num(to));417418#ifndef PRODUCT419if (TraceFPUStack) {420tty->print("Inserted copy (%d -> %d) New state: ", fpu_num(from), fpu_num(to)); sim()->print(); tty->cr();421}422#endif423}424425void FpuStackAllocator::do_rename(LIR_Opr from, LIR_Opr to) {426sim()->rename(fpu_num(from), fpu_num(to));427}428429void FpuStackAllocator::do_push(LIR_Opr opr) {430sim()->push(fpu_num(opr));431}432433void FpuStackAllocator::pop_if_last_use(LIR_Op* op, LIR_Opr opr) {434assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");435assert(tos_offset(opr) == 0, "can only pop stack top");436437if (opr->is_last_use()) {438op->set_fpu_pop_count(1);439sim()->pop();440}441}442443void FpuStackAllocator::pop_always(LIR_Op* op, LIR_Opr opr) {444assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");445assert(tos_offset(opr) == 0, "can only pop stack top");446447op->set_fpu_pop_count(1);448sim()->pop();449}450451void FpuStackAllocator::clear_fpu_stack(LIR_Opr preserve) {452int result_stack_size = (preserve->is_fpu_register() && !preserve->is_xmm_register() ? 1 : 0);453while (sim()->stack_size() > result_stack_size) {454assert(!sim()->slot_is_empty(0), "not allowed");455456if (result_stack_size == 0 || sim()->get_slot(0) != fpu_num(preserve)) {457insert_free(0);458} else {459// move "preserve" to bottom of stack so that all other stack slots can be popped460insert_exchange(sim()->stack_size() - 1);461}462}463}464465466void FpuStackAllocator::handle_op1(LIR_Op1* op1) {467LIR_Opr in = op1->in_opr();468LIR_Opr res = op1->result_opr();469470LIR_Opr new_in = in; // new operands relative to the actual fpu stack top471LIR_Opr new_res = res;472473// Note: this switch is processed for all LIR_Op1, regardless if they have FPU-arguments,474// so checks for is_float_kind() are necessary inside the cases475switch (op1->code()) {476477case lir_return: {478// FPU-Stack must only contain the (optional) fpu return value.479// All remaining dead values are popped from the stack480// If the input operand is a fpu-register, it is exchanged to the bottom of the stack481482clear_fpu_stack(in);483if (in->is_fpu_register() && !in->is_xmm_register()) {484new_in = to_fpu_stack_top(in);485}486487break;488}489490case lir_move: {491if (in->is_fpu_register() && !in->is_xmm_register()) {492if (res->is_xmm_register()) {493// move from fpu register to xmm register (necessary for operations that494// are not available in the SSE instruction set)495insert_exchange(in);496new_in = to_fpu_stack_top(in);497pop_always(op1, in);498499} else if (res->is_fpu_register() && !res->is_xmm_register()) {500// move from fpu-register to fpu-register:501// * input and result register equal:502// nothing to do503// * input register is last use:504// rename the input register to result register -> input register505// not present on fpu-stack afterwards506// * input register not last use:507// duplicate input register to result register to preserve input508//509// Note: The LIR-Assembler does not produce any code for fpu register moves,510// so input and result stack index must be equal511512if (fpu_num(in) == fpu_num(res)) {513// nothing to do514} else if (in->is_last_use()) {515insert_free_if_dead(res);//, in);516do_rename(in, res);517} else {518insert_free_if_dead(res);519insert_copy(in, res);520}521new_in = to_fpu_stack(res);522new_res = new_in;523524} else {525// move from fpu-register to memory526// input operand must be on top of stack527528insert_exchange(in);529530// create debug information here because afterwards the register may have been popped531compute_debug_information(op1);532533new_in = to_fpu_stack_top(in);534pop_if_last_use(op1, in);535}536537} else if (res->is_fpu_register() && !res->is_xmm_register()) {538// move from memory/constant to fpu register539// result is pushed on the stack540541insert_free_if_dead(res);542543// create debug information before register is pushed544compute_debug_information(op1);545546do_push(res);547new_res = to_fpu_stack_top(res);548}549break;550}551552case lir_convert: {553Bytecodes::Code bc = op1->as_OpConvert()->bytecode();554switch (bc) {555case Bytecodes::_d2f:556case Bytecodes::_f2d:557assert(res->is_fpu_register(), "must be");558assert(in->is_fpu_register(), "must be");559560if (!in->is_xmm_register() && !res->is_xmm_register()) {561// this is quite the same as a move from fpu-register to fpu-register562// Note: input and result operands must have different types563if (fpu_num(in) == fpu_num(res)) {564// nothing to do565new_in = to_fpu_stack(in);566} else if (in->is_last_use()) {567insert_free_if_dead(res);//, in);568new_in = to_fpu_stack(in);569do_rename(in, res);570} else {571insert_free_if_dead(res);572insert_copy(in, res);573new_in = to_fpu_stack_top(in, true);574}575new_res = to_fpu_stack(res);576}577578break;579580case Bytecodes::_i2f:581case Bytecodes::_l2f:582case Bytecodes::_i2d:583case Bytecodes::_l2d:584assert(res->is_fpu_register(), "must be");585if (!res->is_xmm_register()) {586insert_free_if_dead(res);587do_push(res);588new_res = to_fpu_stack_top(res);589}590break;591592case Bytecodes::_f2i:593case Bytecodes::_d2i:594assert(in->is_fpu_register(), "must be");595if (!in->is_xmm_register()) {596insert_exchange(in);597new_in = to_fpu_stack_top(in);598599// TODO: update registes of stub600}601break;602603case Bytecodes::_f2l:604case Bytecodes::_d2l:605assert(in->is_fpu_register(), "must be");606if (!in->is_xmm_register()) {607insert_exchange(in);608new_in = to_fpu_stack_top(in);609pop_always(op1, in);610}611break;612613case Bytecodes::_i2l:614case Bytecodes::_l2i:615case Bytecodes::_i2b:616case Bytecodes::_i2c:617case Bytecodes::_i2s:618// no fpu operands619break;620621default:622ShouldNotReachHere();623}624break;625}626627case lir_roundfp: {628assert(in->is_fpu_register() && !in->is_xmm_register(), "input must be in register");629assert(res->is_stack(), "result must be on stack");630631insert_exchange(in);632new_in = to_fpu_stack_top(in);633pop_if_last_use(op1, in);634break;635}636637default: {638assert(!in->is_float_kind() && !res->is_float_kind(), "missed a fpu-operation");639}640}641642op1->set_in_opr(new_in);643op1->set_result_opr(new_res);644}645646void FpuStackAllocator::handle_op2(LIR_Op2* op2) {647LIR_Opr left = op2->in_opr1();648if (!left->is_float_kind()) {649return;650}651if (left->is_xmm_register()) {652return;653}654655LIR_Opr right = op2->in_opr2();656LIR_Opr res = op2->result_opr();657LIR_Opr new_left = left; // new operands relative to the actual fpu stack top658LIR_Opr new_right = right;659LIR_Opr new_res = res;660661assert(!left->is_xmm_register() && !right->is_xmm_register() && !res->is_xmm_register(), "not for xmm registers");662663switch (op2->code()) {664case lir_cmp:665case lir_cmp_fd2i:666case lir_ucmp_fd2i:667case lir_assert: {668assert(left->is_fpu_register(), "invalid LIR");669assert(right->is_fpu_register(), "invalid LIR");670671// the left-hand side must be on top of stack.672// the right-hand side is never popped, even if is_last_use is set673insert_exchange(left);674new_left = to_fpu_stack_top(left);675new_right = to_fpu_stack(right);676pop_if_last_use(op2, left);677break;678}679680case lir_mul:681case lir_div: {682if (res->is_double_fpu()) {683assert(op2->tmp1_opr()->is_fpu_register(), "strict operations need temporary fpu stack slot");684insert_free_if_dead(op2->tmp1_opr());685assert(sim()->stack_size() <= 7, "at least one stack slot must be free");686}687// fall-through: continue with the normal handling of lir_mul and lir_div688}689case lir_add:690case lir_sub: {691assert(left->is_fpu_register(), "must be");692assert(res->is_fpu_register(), "must be");693assert(left->is_equal(res), "must be");694695// either the left-hand or the right-hand side must be on top of stack696// (if right is not a register, left must be on top)697if (!right->is_fpu_register()) {698insert_exchange(left);699new_left = to_fpu_stack_top(left);700} else {701// no exchange necessary if right is alredy on top of stack702if (tos_offset(right) == 0) {703new_left = to_fpu_stack(left);704new_right = to_fpu_stack_top(right);705} else {706insert_exchange(left);707new_left = to_fpu_stack_top(left);708new_right = to_fpu_stack(right);709}710711if (right->is_last_use()) {712op2->set_fpu_pop_count(1);713714if (tos_offset(right) == 0) {715sim()->pop();716} else {717// if left is on top of stack, the result is placed in the stack718// slot of right, so a renaming from right to res is necessary719assert(tos_offset(left) == 0, "must be");720sim()->pop();721do_rename(right, res);722}723}724}725new_res = to_fpu_stack(res);726727break;728}729730case lir_rem: {731assert(left->is_fpu_register(), "must be");732assert(right->is_fpu_register(), "must be");733assert(res->is_fpu_register(), "must be");734assert(left->is_equal(res), "must be");735736// Must bring both operands to top of stack with following operand ordering:737// * fpu stack before rem: ... right left738// * fpu stack after rem: ... left739if (tos_offset(right) != 1) {740insert_exchange(right);741insert_exchange(1);742}743insert_exchange(left);744assert(tos_offset(right) == 1, "check");745assert(tos_offset(left) == 0, "check");746747new_left = to_fpu_stack_top(left);748new_right = to_fpu_stack(right);749750op2->set_fpu_pop_count(1);751sim()->pop();752do_rename(right, res);753754new_res = to_fpu_stack_top(res);755break;756}757758case lir_abs:759case lir_sqrt:760case lir_neg: {761// Right argument appears to be unused762assert(right->is_illegal(), "must be");763assert(left->is_fpu_register(), "must be");764assert(res->is_fpu_register(), "must be");765assert(left->is_last_use(), "old value gets destroyed");766767insert_free_if_dead(res, left);768insert_exchange(left);769do_rename(left, res);770771new_left = to_fpu_stack_top(res);772new_res = new_left;773774op2->set_fpu_stack_size(sim()->stack_size());775break;776}777778default: {779assert(false, "missed a fpu-operation");780}781}782783op2->set_in_opr1(new_left);784op2->set_in_opr2(new_right);785op2->set_result_opr(new_res);786}787788void FpuStackAllocator::handle_opCall(LIR_OpCall* opCall) {789LIR_Opr res = opCall->result_opr();790791// clear fpu-stack before call792// it may contain dead values that could not have been remved by previous operations793clear_fpu_stack(LIR_OprFact::illegalOpr);794assert(sim()->is_empty(), "fpu stack must be empty now");795796// compute debug information before (possible) fpu result is pushed797compute_debug_information(opCall);798799if (res->is_fpu_register() && !res->is_xmm_register()) {800do_push(res);801opCall->set_result_opr(to_fpu_stack_top(res));802}803}804805#ifndef PRODUCT806void FpuStackAllocator::check_invalid_lir_op(LIR_Op* op) {807switch (op->code()) {808case lir_fpop_raw:809case lir_fxch:810case lir_fld:811assert(false, "operations only inserted by FpuStackAllocator");812break;813814default:815break;816}817}818#endif819820821void FpuStackAllocator::merge_insert_add(LIR_List* instrs, FpuStackSim* cur_sim, int reg) {822LIR_Op1* move = new LIR_Op1(lir_move, LIR_OprFact::doubleConst(0), LIR_OprFact::double_fpu(reg)->make_fpu_stack_offset());823824instrs->instructions_list()->push(move);825826cur_sim->push(reg);827move->set_result_opr(to_fpu_stack(move->result_opr()));828829#ifndef PRODUCT830if (TraceFPUStack) {831tty->print("Added new register: %d New state: ", reg); cur_sim->print(); tty->cr();832}833#endif834}835836void FpuStackAllocator::merge_insert_xchg(LIR_List* instrs, FpuStackSim* cur_sim, int slot) {837assert(slot > 0, "no exchange necessary");838839LIR_Op1* fxch = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(slot));840instrs->instructions_list()->push(fxch);841cur_sim->swap(slot);842843#ifndef PRODUCT844if (TraceFPUStack) {845tty->print("Exchanged register: %d New state: ", cur_sim->get_slot(slot)); cur_sim->print(); tty->cr();846}847#endif848}849850void FpuStackAllocator::merge_insert_pop(LIR_List* instrs, FpuStackSim* cur_sim) {851int reg = cur_sim->get_slot(0);852853LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);854instrs->instructions_list()->push(fpop);855cur_sim->pop(reg);856857#ifndef PRODUCT858if (TraceFPUStack) {859tty->print("Removed register: %d New state: ", reg); cur_sim->print(); tty->cr();860}861#endif862}863864bool FpuStackAllocator::merge_rename(FpuStackSim* cur_sim, FpuStackSim* sux_sim, int start_slot, int change_slot) {865int reg = cur_sim->get_slot(change_slot);866867for (int slot = start_slot; slot >= 0; slot--) {868int new_reg = sux_sim->get_slot(slot);869870if (!cur_sim->contains(new_reg)) {871cur_sim->set_slot(change_slot, new_reg);872873#ifndef PRODUCT874if (TraceFPUStack) {875tty->print("Renamed register %d to %d New state: ", reg, new_reg); cur_sim->print(); tty->cr();876}877#endif878879return true;880}881}882return false;883}884885886void FpuStackAllocator::merge_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, FpuStackSim* sux_sim) {887#ifndef PRODUCT888if (TraceFPUStack) {889tty->cr();890tty->print("before merging: pred: "); cur_sim->print(); tty->cr();891tty->print(" sux: "); sux_sim->print(); tty->cr();892}893894int slot;895for (slot = 0; slot < cur_sim->stack_size(); slot++) {896assert(!cur_sim->slot_is_empty(slot), "not handled by algorithm");897}898for (slot = 0; slot < sux_sim->stack_size(); slot++) {899assert(!sux_sim->slot_is_empty(slot), "not handled by algorithm");900}901#endif902903// size difference between cur and sux that must be resolved by adding or removing values form the stack904int size_diff = cur_sim->stack_size() - sux_sim->stack_size();905906if (!ComputeExactFPURegisterUsage) {907// add slots that are currently free, but used in successor908// When the exact FPU register usage is computed, the stack does909// not contain dead values at merging -> no values must be added910911int sux_slot = sux_sim->stack_size() - 1;912while (size_diff < 0) {913assert(sux_slot >= 0, "slot out of bounds -> error in algorithm");914915int reg = sux_sim->get_slot(sux_slot);916if (!cur_sim->contains(reg)) {917merge_insert_add(instrs, cur_sim, reg);918size_diff++;919920if (sux_slot + size_diff != 0) {921merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);922}923}924sux_slot--;925}926}927928assert(cur_sim->stack_size() >= sux_sim->stack_size(), "stack size must be equal or greater now");929assert(size_diff == cur_sim->stack_size() - sux_sim->stack_size(), "must be");930931// stack merge algorithm:932// 1) as long as the current stack top is not in the right location (that meens933// it should not be on the stack top), exchange it into the right location934// 2) if the stack top is right, but the remaining stack is not ordered correctly,935// the stack top is exchanged away to get another value on top ->936// now step 1) can be continued937// the stack can also contain unused items -> these items are removed from stack938939int finished_slot = sux_sim->stack_size() - 1;940while (finished_slot >= 0 || size_diff > 0) {941while (size_diff > 0 || (cur_sim->stack_size() > 0 && cur_sim->get_slot(0) != sux_sim->get_slot(0))) {942int reg = cur_sim->get_slot(0);943if (sux_sim->contains(reg)) {944int sux_slot = sux_sim->offset_from_tos(reg);945merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);946947} else if (!merge_rename(cur_sim, sux_sim, finished_slot, 0)) {948assert(size_diff > 0, "must be");949950merge_insert_pop(instrs, cur_sim);951size_diff--;952}953assert(cur_sim->stack_size() == 0 || cur_sim->get_slot(0) != reg, "register must have been changed");954}955956while (finished_slot >= 0 && cur_sim->get_slot(finished_slot) == sux_sim->get_slot(finished_slot)) {957finished_slot--;958}959960if (finished_slot >= 0) {961int reg = cur_sim->get_slot(finished_slot);962963if (sux_sim->contains(reg) || !merge_rename(cur_sim, sux_sim, finished_slot, finished_slot)) {964assert(sux_sim->contains(reg) || size_diff > 0, "must be");965merge_insert_xchg(instrs, cur_sim, finished_slot);966}967assert(cur_sim->get_slot(finished_slot) != reg, "register must have been changed");968}969}970971#ifndef PRODUCT972if (TraceFPUStack) {973tty->print("after merging: pred: "); cur_sim->print(); tty->cr();974tty->print(" sux: "); sux_sim->print(); tty->cr();975tty->cr();976}977#endif978assert(cur_sim->stack_size() == sux_sim->stack_size(), "stack size must be equal now");979}980981982void FpuStackAllocator::merge_cleanup_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, BitMap& live_fpu_regs) {983#ifndef PRODUCT984if (TraceFPUStack) {985tty->cr();986tty->print("before cleanup: state: "); cur_sim->print(); tty->cr();987tty->print(" live: "); live_fpu_regs.print_on(tty); tty->cr();988}989#endif990991int slot = 0;992while (slot < cur_sim->stack_size()) {993int reg = cur_sim->get_slot(slot);994if (!live_fpu_regs.at(reg)) {995if (slot != 0) {996merge_insert_xchg(instrs, cur_sim, slot);997}998merge_insert_pop(instrs, cur_sim);999} else {1000slot++;1001}1002}10031004#ifndef PRODUCT1005if (TraceFPUStack) {1006tty->print("after cleanup: state: "); cur_sim->print(); tty->cr();1007tty->print(" live: "); live_fpu_regs.print_on(tty); tty->cr();1008tty->cr();1009}10101011// check if fpu stack only contains live registers1012for (unsigned int i = 0; i < live_fpu_regs.size(); i++) {1013if (live_fpu_regs.at(i) != cur_sim->contains(i)) {1014tty->print_cr("mismatch between required and actual stack content");1015break;1016}1017}1018#endif1019}102010211022bool FpuStackAllocator::merge_fpu_stack_with_successors(BlockBegin* block) {1023#ifndef PRODUCT1024if (TraceFPUStack) {1025tty->print_cr("Propagating FPU stack state for B%d at LIR_Op position %d to successors:",1026block->block_id(), pos());1027sim()->print();1028tty->cr();1029}1030#endif10311032bool changed = false;1033int number_of_sux = block->number_of_sux();10341035if (number_of_sux == 1 && block->sux_at(0)->number_of_preds() > 1) {1036// The successor has at least two incoming edges, so a stack merge will be necessary1037// If this block is the first predecessor, cleanup the current stack and propagate it1038// If this block is not the first predecessor, a stack merge will be necessary10391040BlockBegin* sux = block->sux_at(0);1041intArray* state = sux->fpu_stack_state();1042LIR_List* instrs = new LIR_List(_compilation);10431044if (state != NULL) {1045// Merge with a successors that already has a FPU stack state1046// the block must only have one successor because critical edges must been split1047FpuStackSim* cur_sim = sim();1048FpuStackSim* sux_sim = temp_sim();1049sux_sim->read_state(state);10501051merge_fpu_stack(instrs, cur_sim, sux_sim);10521053} else {1054// propagate current FPU stack state to successor without state1055// clean up stack first so that there are no dead values on the stack1056if (ComputeExactFPURegisterUsage) {1057FpuStackSim* cur_sim = sim();1058ResourceBitMap live_fpu_regs = block->sux_at(0)->fpu_register_usage();1059assert(live_fpu_regs.size() == FrameMap::nof_fpu_regs, "missing register usage");10601061merge_cleanup_fpu_stack(instrs, cur_sim, live_fpu_regs);1062}10631064intArray* state = sim()->write_state();1065if (TraceFPUStack) {1066tty->print_cr("Setting FPU stack state of B%d (merge path)", sux->block_id());1067sim()->print(); tty->cr();1068}1069sux->set_fpu_stack_state(state);1070}10711072if (instrs->instructions_list()->length() > 0) {1073lir()->insert_before(pos(), instrs);1074set_pos(instrs->instructions_list()->length() + pos());1075changed = true;1076}10771078} else {1079// Propagate unmodified Stack to successors where a stack merge is not necessary1080intArray* state = sim()->write_state();1081for (int i = 0; i < number_of_sux; i++) {1082BlockBegin* sux = block->sux_at(i);10831084#ifdef ASSERT1085for (int j = 0; j < sux->number_of_preds(); j++) {1086assert(block == sux->pred_at(j), "all critical edges must be broken");1087}10881089// check if new state is same1090if (sux->fpu_stack_state() != NULL) {1091intArray* sux_state = sux->fpu_stack_state();1092assert(state->length() == sux_state->length(), "overwriting existing stack state");1093for (int j = 0; j < state->length(); j++) {1094assert(state->at(j) == sux_state->at(j), "overwriting existing stack state");1095}1096}1097#endif1098#ifndef PRODUCT1099if (TraceFPUStack) {1100tty->print_cr("Setting FPU stack state of B%d", sux->block_id());1101sim()->print(); tty->cr();1102}1103#endif11041105sux->set_fpu_stack_state(state);1106}1107}11081109#ifndef PRODUCT1110// assertions that FPU stack state conforms to all successors' states1111intArray* cur_state = sim()->write_state();1112for (int i = 0; i < number_of_sux; i++) {1113BlockBegin* sux = block->sux_at(i);1114intArray* sux_state = sux->fpu_stack_state();11151116assert(sux_state != NULL, "no fpu state");1117assert(cur_state->length() == sux_state->length(), "incorrect length");1118for (int i = 0; i < cur_state->length(); i++) {1119assert(cur_state->at(i) == sux_state->at(i), "element not equal");1120}1121}1122#endif11231124return changed;1125}1126#endif // _LP64112711281129