Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/cpu/x86/vm/c1_LinearScan_x86.cpp
32285 views
/*1* Copyright (c) 2005, 2014, 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//----------------------------------------------------------------------31// Allocation of FPU stack slots (Intel x86 only)32//----------------------------------------------------------------------3334void LinearScan::allocate_fpu_stack() {35// First compute which FPU registers are live at the start of each basic block36// (To minimize the amount of work we have to do if we have to merge FPU stacks)37if (ComputeExactFPURegisterUsage) {38Interval* intervals_in_register, *intervals_in_memory;39create_unhandled_lists(&intervals_in_register, &intervals_in_memory, is_in_fpu_register, NULL);4041// ignore memory intervals by overwriting intervals_in_memory42// the dummy interval is needed to enforce the walker to walk until the given id:43// without it, the walker stops when the unhandled-list is empty -> live information44// beyond this point would be incorrect.45Interval* dummy_interval = new Interval(any_reg);46dummy_interval->add_range(max_jint - 2, max_jint - 1);47dummy_interval->set_next(Interval::end());48intervals_in_memory = dummy_interval;4950IntervalWalker iw(this, intervals_in_register, intervals_in_memory);5152const int num_blocks = block_count();53for (int i = 0; i < num_blocks; i++) {54BlockBegin* b = block_at(i);5556// register usage is only needed for merging stacks -> compute only57// when more than one predecessor.58// the block must not have any spill moves at the beginning (checked by assertions)59// spill moves would use intervals that are marked as handled and so the usage bit60// would been set incorrectly6162// NOTE: the check for number_of_preds > 1 is necessary. A block with only one63// predecessor may have spill moves at the begin of the block.64// If an interval ends at the current instruction id, it is not possible65// to decide if the register is live or not at the block begin -> the66// register information would be incorrect.67if (b->number_of_preds() > 1) {68int id = b->first_lir_instruction_id();69BitMap regs(FrameMap::nof_fpu_regs);70regs.clear();7172iw.walk_to(id); // walk after the first instruction (always a label) of the block73assert(iw.current_position() == id, "did not walk completely to id");7475// Only consider FPU values in registers76Interval* interval = iw.active_first(fixedKind);77while (interval != Interval::end()) {78int reg = interval->assigned_reg();79assert(reg >= pd_first_fpu_reg && reg <= pd_last_fpu_reg, "no fpu register");80assert(interval->assigned_regHi() == -1, "must not have hi register (doubles stored in one register)");81assert(interval->from() <= id && id < interval->to(), "interval out of range");8283#ifndef PRODUCT84if (TraceFPURegisterUsage) {85tty->print("fpu reg %d is live because of ", reg - pd_first_fpu_reg); interval->print();86}87#endif8889regs.set_bit(reg - pd_first_fpu_reg);90interval = interval->next();91}9293b->set_fpu_register_usage(regs);9495#ifndef PRODUCT96if (TraceFPURegisterUsage) {97tty->print("FPU regs for block %d, LIR instr %d): ", b->block_id(), id); regs.print_on(tty); tty->cr();98}99#endif100}101}102}103104FpuStackAllocator alloc(ir()->compilation(), this);105_fpu_stack_allocator = &alloc;106alloc.allocate();107_fpu_stack_allocator = NULL;108}109110111FpuStackAllocator::FpuStackAllocator(Compilation* compilation, LinearScan* allocator)112: _compilation(compilation)113, _lir(NULL)114, _pos(-1)115, _allocator(allocator)116, _sim(compilation)117, _temp_sim(compilation)118{}119120void FpuStackAllocator::allocate() {121int num_blocks = allocator()->block_count();122for (int i = 0; i < num_blocks; i++) {123// Set up to process block124BlockBegin* block = allocator()->block_at(i);125intArray* fpu_stack_state = block->fpu_stack_state();126127#ifndef PRODUCT128if (TraceFPUStack) {129tty->cr();130tty->print_cr("------- Begin of new Block %d -------", block->block_id());131}132#endif133134assert(fpu_stack_state != NULL ||135block->end()->as_Base() != NULL ||136block->is_set(BlockBegin::exception_entry_flag),137"FPU stack state must be present due to linear-scan order for FPU stack allocation");138// note: exception handler entries always start with an empty fpu stack139// because stack merging would be too complicated140141if (fpu_stack_state != NULL) {142sim()->read_state(fpu_stack_state);143} else {144sim()->clear();145}146147#ifndef PRODUCT148if (TraceFPUStack) {149tty->print("Reading FPU state for block %d:", block->block_id());150sim()->print();151tty->cr();152}153#endif154155allocate_block(block);156CHECK_BAILOUT();157}158}159160void FpuStackAllocator::allocate_block(BlockBegin* block) {161bool processed_merge = false;162LIR_OpList* insts = block->lir()->instructions_list();163set_lir(block->lir());164set_pos(0);165166167// Note: insts->length() may change during loop168while (pos() < insts->length()) {169LIR_Op* op = insts->at(pos());170_debug_information_computed = false;171172#ifndef PRODUCT173if (TraceFPUStack) {174op->print();175}176check_invalid_lir_op(op);177#endif178179LIR_OpBranch* branch = op->as_OpBranch();180LIR_Op1* op1 = op->as_Op1();181LIR_Op2* op2 = op->as_Op2();182LIR_OpCall* opCall = op->as_OpCall();183184if (branch != NULL && branch->block() != NULL) {185if (!processed_merge) {186// propagate stack at first branch to a successor187processed_merge = true;188bool required_merge = merge_fpu_stack_with_successors(block);189190assert(!required_merge || branch->cond() == lir_cond_always, "splitting of critical edges should prevent FPU stack mismatches at cond branches");191}192193} else if (op1 != NULL) {194handle_op1(op1);195} else if (op2 != NULL) {196handle_op2(op2);197} else if (opCall != NULL) {198handle_opCall(opCall);199}200201compute_debug_information(op);202203set_pos(1 + pos());204}205206// Propagate stack when block does not end with branch207if (!processed_merge) {208merge_fpu_stack_with_successors(block);209}210}211212213void FpuStackAllocator::compute_debug_information(LIR_Op* op) {214if (!_debug_information_computed && op->id() != -1 && allocator()->has_info(op->id())) {215visitor.visit(op);216217// exception handling218if (allocator()->compilation()->has_exception_handlers()) {219XHandlers* xhandlers = visitor.all_xhandler();220int n = xhandlers->length();221for (int k = 0; k < n; k++) {222allocate_exception_handler(xhandlers->handler_at(k));223}224} else {225assert(visitor.all_xhandler()->length() == 0, "missed exception handler");226}227228// compute debug information229int n = visitor.info_count();230assert(n > 0, "should not visit operation otherwise");231232for (int j = 0; j < n; j++) {233CodeEmitInfo* info = visitor.info_at(j);234// Compute debug information235allocator()->compute_debug_info(info, op->id());236}237}238_debug_information_computed = true;239}240241void FpuStackAllocator::allocate_exception_handler(XHandler* xhandler) {242if (!sim()->is_empty()) {243LIR_List* old_lir = lir();244int old_pos = pos();245intArray* old_state = sim()->write_state();246247#ifndef PRODUCT248if (TraceFPUStack) {249tty->cr();250tty->print_cr("------- begin of exception handler -------");251}252#endif253254if (xhandler->entry_code() == NULL) {255// need entry code to clear FPU stack256LIR_List* entry_code = new LIR_List(_compilation);257entry_code->jump(xhandler->entry_block());258xhandler->set_entry_code(entry_code);259}260261LIR_OpList* insts = xhandler->entry_code()->instructions_list();262set_lir(xhandler->entry_code());263set_pos(0);264265// Note: insts->length() may change during loop266while (pos() < insts->length()) {267LIR_Op* op = insts->at(pos());268269#ifndef PRODUCT270if (TraceFPUStack) {271op->print();272}273check_invalid_lir_op(op);274#endif275276switch (op->code()) {277case lir_move:278assert(op->as_Op1() != NULL, "must be LIR_Op1");279assert(pos() != insts->length() - 1, "must not be last operation");280281handle_op1((LIR_Op1*)op);282break;283284case lir_branch:285assert(op->as_OpBranch()->cond() == lir_cond_always, "must be unconditional branch");286assert(pos() == insts->length() - 1, "must be last operation");287288// remove all remaining dead registers from FPU stack289clear_fpu_stack(LIR_OprFact::illegalOpr);290break;291292default:293// other operations not allowed in exception entry code294ShouldNotReachHere();295}296297set_pos(pos() + 1);298}299300#ifndef PRODUCT301if (TraceFPUStack) {302tty->cr();303tty->print_cr("------- end of exception handler -------");304}305#endif306307set_lir(old_lir);308set_pos(old_pos);309sim()->read_state(old_state);310}311}312313314int FpuStackAllocator::fpu_num(LIR_Opr opr) {315assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");316return opr->is_single_fpu() ? opr->fpu_regnr() : opr->fpu_regnrLo();317}318319int FpuStackAllocator::tos_offset(LIR_Opr opr) {320return sim()->offset_from_tos(fpu_num(opr));321}322323324LIR_Opr FpuStackAllocator::to_fpu_stack(LIR_Opr opr) {325assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");326327int stack_offset = tos_offset(opr);328if (opr->is_single_fpu()) {329return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();330} else {331assert(opr->is_double_fpu(), "shouldn't call this otherwise");332return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();333}334}335336LIR_Opr FpuStackAllocator::to_fpu_stack_top(LIR_Opr opr, bool dont_check_offset) {337assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");338assert(dont_check_offset || tos_offset(opr) == 0, "operand is not on stack top");339340int stack_offset = 0;341if (opr->is_single_fpu()) {342return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();343} else {344assert(opr->is_double_fpu(), "shouldn't call this otherwise");345return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();346}347}348349350351void FpuStackAllocator::insert_op(LIR_Op* op) {352lir()->insert_before(pos(), op);353set_pos(1 + pos());354}355356357void FpuStackAllocator::insert_exchange(int offset) {358if (offset > 0) {359LIR_Op1* fxch_op = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);360insert_op(fxch_op);361sim()->swap(offset);362363#ifndef PRODUCT364if (TraceFPUStack) {365tty->print("Exchanged register: %d New state: ", sim()->get_slot(0)); sim()->print(); tty->cr();366}367#endif368369}370}371372void FpuStackAllocator::insert_exchange(LIR_Opr opr) {373insert_exchange(tos_offset(opr));374}375376377void FpuStackAllocator::insert_free(int offset) {378// move stack slot to the top of stack and then pop it379insert_exchange(offset);380381LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);382insert_op(fpop);383sim()->pop();384385#ifndef PRODUCT386if (TraceFPUStack) {387tty->print("Inserted pop New state: "); sim()->print(); tty->cr();388}389#endif390}391392393void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr) {394if (sim()->contains(fpu_num(opr))) {395int res_slot = tos_offset(opr);396insert_free(res_slot);397}398}399400void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr, LIR_Opr ignore) {401if (fpu_num(opr) != fpu_num(ignore) && sim()->contains(fpu_num(opr))) {402int res_slot = tos_offset(opr);403insert_free(res_slot);404}405}406407void FpuStackAllocator::insert_copy(LIR_Opr from, LIR_Opr to) {408int offset = tos_offset(from);409LIR_Op1* fld = new LIR_Op1(lir_fld, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);410insert_op(fld);411412sim()->push(fpu_num(to));413414#ifndef PRODUCT415if (TraceFPUStack) {416tty->print("Inserted copy (%d -> %d) New state: ", fpu_num(from), fpu_num(to)); sim()->print(); tty->cr();417}418#endif419}420421void FpuStackAllocator::do_rename(LIR_Opr from, LIR_Opr to) {422sim()->rename(fpu_num(from), fpu_num(to));423}424425void FpuStackAllocator::do_push(LIR_Opr opr) {426sim()->push(fpu_num(opr));427}428429void FpuStackAllocator::pop_if_last_use(LIR_Op* op, LIR_Opr opr) {430assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");431assert(tos_offset(opr) == 0, "can only pop stack top");432433if (opr->is_last_use()) {434op->set_fpu_pop_count(1);435sim()->pop();436}437}438439void FpuStackAllocator::pop_always(LIR_Op* op, LIR_Opr opr) {440assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");441assert(tos_offset(opr) == 0, "can only pop stack top");442443op->set_fpu_pop_count(1);444sim()->pop();445}446447void FpuStackAllocator::clear_fpu_stack(LIR_Opr preserve) {448int result_stack_size = (preserve->is_fpu_register() && !preserve->is_xmm_register() ? 1 : 0);449while (sim()->stack_size() > result_stack_size) {450assert(!sim()->slot_is_empty(0), "not allowed");451452if (result_stack_size == 0 || sim()->get_slot(0) != fpu_num(preserve)) {453insert_free(0);454} else {455// move "preserve" to bottom of stack so that all other stack slots can be popped456insert_exchange(sim()->stack_size() - 1);457}458}459}460461462void FpuStackAllocator::handle_op1(LIR_Op1* op1) {463LIR_Opr in = op1->in_opr();464LIR_Opr res = op1->result_opr();465466LIR_Opr new_in = in; // new operands relative to the actual fpu stack top467LIR_Opr new_res = res;468469// Note: this switch is processed for all LIR_Op1, regardless if they have FPU-arguments,470// so checks for is_float_kind() are necessary inside the cases471switch (op1->code()) {472473case lir_return: {474// FPU-Stack must only contain the (optional) fpu return value.475// All remaining dead values are popped from the stack476// If the input operand is a fpu-register, it is exchanged to the bottom of the stack477478clear_fpu_stack(in);479if (in->is_fpu_register() && !in->is_xmm_register()) {480new_in = to_fpu_stack_top(in);481}482483break;484}485486case lir_move: {487if (in->is_fpu_register() && !in->is_xmm_register()) {488if (res->is_xmm_register()) {489// move from fpu register to xmm register (necessary for operations that490// are not available in the SSE instruction set)491insert_exchange(in);492new_in = to_fpu_stack_top(in);493pop_always(op1, in);494495} else if (res->is_fpu_register() && !res->is_xmm_register()) {496// move from fpu-register to fpu-register:497// * input and result register equal:498// nothing to do499// * input register is last use:500// rename the input register to result register -> input register501// not present on fpu-stack afterwards502// * input register not last use:503// duplicate input register to result register to preserve input504//505// Note: The LIR-Assembler does not produce any code for fpu register moves,506// so input and result stack index must be equal507508if (fpu_num(in) == fpu_num(res)) {509// nothing to do510} else if (in->is_last_use()) {511insert_free_if_dead(res);//, in);512do_rename(in, res);513} else {514insert_free_if_dead(res);515insert_copy(in, res);516}517new_in = to_fpu_stack(res);518new_res = new_in;519520} else {521// move from fpu-register to memory522// input operand must be on top of stack523524insert_exchange(in);525526// create debug information here because afterwards the register may have been popped527compute_debug_information(op1);528529new_in = to_fpu_stack_top(in);530pop_if_last_use(op1, in);531}532533} else if (res->is_fpu_register() && !res->is_xmm_register()) {534// move from memory/constant to fpu register535// result is pushed on the stack536537insert_free_if_dead(res);538539// create debug information before register is pushed540compute_debug_information(op1);541542do_push(res);543new_res = to_fpu_stack_top(res);544}545break;546}547548case lir_neg: {549if (in->is_fpu_register() && !in->is_xmm_register()) {550assert(res->is_fpu_register() && !res->is_xmm_register(), "must be");551assert(in->is_last_use(), "old value gets destroyed");552553insert_free_if_dead(res, in);554insert_exchange(in);555new_in = to_fpu_stack_top(in);556557do_rename(in, res);558new_res = to_fpu_stack_top(res);559}560break;561}562563case lir_convert: {564Bytecodes::Code bc = op1->as_OpConvert()->bytecode();565switch (bc) {566case Bytecodes::_d2f:567case Bytecodes::_f2d:568assert(res->is_fpu_register(), "must be");569assert(in->is_fpu_register(), "must be");570571if (!in->is_xmm_register() && !res->is_xmm_register()) {572// this is quite the same as a move from fpu-register to fpu-register573// Note: input and result operands must have different types574if (fpu_num(in) == fpu_num(res)) {575// nothing to do576new_in = to_fpu_stack(in);577} else if (in->is_last_use()) {578insert_free_if_dead(res);//, in);579new_in = to_fpu_stack(in);580do_rename(in, res);581} else {582insert_free_if_dead(res);583insert_copy(in, res);584new_in = to_fpu_stack_top(in, true);585}586new_res = to_fpu_stack(res);587}588589break;590591case Bytecodes::_i2f:592case Bytecodes::_l2f:593case Bytecodes::_i2d:594case Bytecodes::_l2d:595assert(res->is_fpu_register(), "must be");596if (!res->is_xmm_register()) {597insert_free_if_dead(res);598do_push(res);599new_res = to_fpu_stack_top(res);600}601break;602603case Bytecodes::_f2i:604case Bytecodes::_d2i:605assert(in->is_fpu_register(), "must be");606if (!in->is_xmm_register()) {607insert_exchange(in);608new_in = to_fpu_stack_top(in);609610// TODO: update registes of stub611}612break;613614case Bytecodes::_f2l:615case Bytecodes::_d2l:616assert(in->is_fpu_register(), "must be");617if (!in->is_xmm_register()) {618insert_exchange(in);619new_in = to_fpu_stack_top(in);620pop_always(op1, in);621}622break;623624case Bytecodes::_i2l:625case Bytecodes::_l2i:626case Bytecodes::_i2b:627case Bytecodes::_i2c:628case Bytecodes::_i2s:629// no fpu operands630break;631632default:633ShouldNotReachHere();634}635break;636}637638case lir_roundfp: {639assert(in->is_fpu_register() && !in->is_xmm_register(), "input must be in register");640assert(res->is_stack(), "result must be on stack");641642insert_exchange(in);643new_in = to_fpu_stack_top(in);644pop_if_last_use(op1, in);645break;646}647648default: {649assert(!in->is_float_kind() && !res->is_float_kind(), "missed a fpu-operation");650}651}652653op1->set_in_opr(new_in);654op1->set_result_opr(new_res);655}656657void FpuStackAllocator::handle_op2(LIR_Op2* op2) {658LIR_Opr left = op2->in_opr1();659if (!left->is_float_kind()) {660return;661}662if (left->is_xmm_register()) {663return;664}665666LIR_Opr right = op2->in_opr2();667LIR_Opr res = op2->result_opr();668LIR_Opr new_left = left; // new operands relative to the actual fpu stack top669LIR_Opr new_right = right;670LIR_Opr new_res = res;671672assert(!left->is_xmm_register() && !right->is_xmm_register() && !res->is_xmm_register(), "not for xmm registers");673674switch (op2->code()) {675case lir_cmp:676case lir_cmp_fd2i:677case lir_ucmp_fd2i:678case lir_assert: {679assert(left->is_fpu_register(), "invalid LIR");680assert(right->is_fpu_register(), "invalid LIR");681682// the left-hand side must be on top of stack.683// the right-hand side is never popped, even if is_last_use is set684insert_exchange(left);685new_left = to_fpu_stack_top(left);686new_right = to_fpu_stack(right);687pop_if_last_use(op2, left);688break;689}690691case lir_mul_strictfp:692case lir_div_strictfp: {693assert(op2->tmp1_opr()->is_fpu_register(), "strict operations need temporary fpu stack slot");694insert_free_if_dead(op2->tmp1_opr());695assert(sim()->stack_size() <= 7, "at least one stack slot must be free");696// fall-through: continue with the normal handling of lir_mul and lir_div697}698case lir_add:699case lir_sub:700case lir_mul:701case lir_div: {702assert(left->is_fpu_register(), "must be");703assert(res->is_fpu_register(), "must be");704assert(left->is_equal(res), "must be");705706// either the left-hand or the right-hand side must be on top of stack707// (if right is not a register, left must be on top)708if (!right->is_fpu_register()) {709insert_exchange(left);710new_left = to_fpu_stack_top(left);711} else {712// no exchange necessary if right is alredy on top of stack713if (tos_offset(right) == 0) {714new_left = to_fpu_stack(left);715new_right = to_fpu_stack_top(right);716} else {717insert_exchange(left);718new_left = to_fpu_stack_top(left);719new_right = to_fpu_stack(right);720}721722if (right->is_last_use()) {723op2->set_fpu_pop_count(1);724725if (tos_offset(right) == 0) {726sim()->pop();727} else {728// if left is on top of stack, the result is placed in the stack729// slot of right, so a renaming from right to res is necessary730assert(tos_offset(left) == 0, "must be");731sim()->pop();732do_rename(right, res);733}734}735}736new_res = to_fpu_stack(res);737738break;739}740741case lir_rem: {742assert(left->is_fpu_register(), "must be");743assert(right->is_fpu_register(), "must be");744assert(res->is_fpu_register(), "must be");745assert(left->is_equal(res), "must be");746747// Must bring both operands to top of stack with following operand ordering:748// * fpu stack before rem: ... right left749// * fpu stack after rem: ... left750if (tos_offset(right) != 1) {751insert_exchange(right);752insert_exchange(1);753}754insert_exchange(left);755assert(tos_offset(right) == 1, "check");756assert(tos_offset(left) == 0, "check");757758new_left = to_fpu_stack_top(left);759new_right = to_fpu_stack(right);760761op2->set_fpu_pop_count(1);762sim()->pop();763do_rename(right, res);764765new_res = to_fpu_stack_top(res);766break;767}768769case lir_abs:770case lir_sqrt: {771// Right argument appears to be unused772assert(right->is_illegal(), "must be");773assert(left->is_fpu_register(), "must be");774assert(res->is_fpu_register(), "must be");775assert(left->is_last_use(), "old value gets destroyed");776777insert_free_if_dead(res, left);778insert_exchange(left);779do_rename(left, res);780781new_left = to_fpu_stack_top(res);782new_res = new_left;783784op2->set_fpu_stack_size(sim()->stack_size());785break;786}787788case lir_log:789case lir_log10: {790// log and log10 need one temporary fpu stack slot, so791// there is one temporary registers stored in temp of the792// operation. the stack allocator must guarantee that the stack793// slots are really free, otherwise there might be a stack794// overflow.795assert(right->is_illegal(), "must be");796assert(left->is_fpu_register(), "must be");797assert(res->is_fpu_register(), "must be");798assert(op2->tmp1_opr()->is_fpu_register(), "must be");799800insert_free_if_dead(op2->tmp1_opr());801insert_free_if_dead(res, left);802insert_exchange(left);803do_rename(left, res);804805new_left = to_fpu_stack_top(res);806new_res = new_left;807808op2->set_fpu_stack_size(sim()->stack_size());809assert(sim()->stack_size() <= 7, "at least one stack slot must be free");810break;811}812813814case lir_tan:815case lir_sin:816case lir_cos:817case lir_exp: {818// sin, cos and exp need two temporary fpu stack slots, so there are two temporary819// registers (stored in right and temp of the operation).820// the stack allocator must guarantee that the stack slots are really free,821// otherwise there might be a stack overflow.822assert(left->is_fpu_register(), "must be");823assert(res->is_fpu_register(), "must be");824// assert(left->is_last_use(), "old value gets destroyed");825assert(right->is_fpu_register(), "right is used as the first temporary register");826assert(op2->tmp1_opr()->is_fpu_register(), "temp is used as the second temporary register");827assert(fpu_num(left) != fpu_num(right) && fpu_num(right) != fpu_num(op2->tmp1_opr()) && fpu_num(op2->tmp1_opr()) != fpu_num(res), "need distinct temp registers");828829insert_free_if_dead(right);830insert_free_if_dead(op2->tmp1_opr());831832insert_free_if_dead(res, left);833insert_exchange(left);834do_rename(left, res);835836new_left = to_fpu_stack_top(res);837new_res = new_left;838839op2->set_fpu_stack_size(sim()->stack_size());840assert(sim()->stack_size() <= 6, "at least two stack slots must be free");841break;842}843844case lir_pow: {845// pow needs two temporary fpu stack slots, so there are two temporary846// registers (stored in tmp1 and tmp2 of the operation).847// the stack allocator must guarantee that the stack slots are really free,848// otherwise there might be a stack overflow.849assert(left->is_fpu_register(), "must be");850assert(right->is_fpu_register(), "must be");851assert(res->is_fpu_register(), "must be");852853assert(op2->tmp1_opr()->is_fpu_register(), "tmp1 is the first temporary register");854assert(op2->tmp2_opr()->is_fpu_register(), "tmp2 is the second temporary register");855assert(fpu_num(left) != fpu_num(right) && fpu_num(left) != fpu_num(op2->tmp1_opr()) && fpu_num(left) != fpu_num(op2->tmp2_opr()) && fpu_num(left) != fpu_num(res), "need distinct temp registers");856assert(fpu_num(right) != fpu_num(op2->tmp1_opr()) && fpu_num(right) != fpu_num(op2->tmp2_opr()) && fpu_num(right) != fpu_num(res), "need distinct temp registers");857assert(fpu_num(op2->tmp1_opr()) != fpu_num(op2->tmp2_opr()) && fpu_num(op2->tmp1_opr()) != fpu_num(res), "need distinct temp registers");858assert(fpu_num(op2->tmp2_opr()) != fpu_num(res), "need distinct temp registers");859860insert_free_if_dead(op2->tmp1_opr());861insert_free_if_dead(op2->tmp2_opr());862863// Must bring both operands to top of stack with following operand ordering:864// * fpu stack before pow: ... right left865// * fpu stack after pow: ... left866867insert_free_if_dead(res, right);868869if (tos_offset(right) != 1) {870insert_exchange(right);871insert_exchange(1);872}873insert_exchange(left);874assert(tos_offset(right) == 1, "check");875assert(tos_offset(left) == 0, "check");876877new_left = to_fpu_stack_top(left);878new_right = to_fpu_stack(right);879880op2->set_fpu_stack_size(sim()->stack_size());881assert(sim()->stack_size() <= 6, "at least two stack slots must be free");882883sim()->pop();884885do_rename(right, res);886887new_res = to_fpu_stack_top(res);888break;889}890891default: {892assert(false, "missed a fpu-operation");893}894}895896op2->set_in_opr1(new_left);897op2->set_in_opr2(new_right);898op2->set_result_opr(new_res);899}900901void FpuStackAllocator::handle_opCall(LIR_OpCall* opCall) {902LIR_Opr res = opCall->result_opr();903904// clear fpu-stack before call905// it may contain dead values that could not have been remved by previous operations906clear_fpu_stack(LIR_OprFact::illegalOpr);907assert(sim()->is_empty(), "fpu stack must be empty now");908909// compute debug information before (possible) fpu result is pushed910compute_debug_information(opCall);911912if (res->is_fpu_register() && !res->is_xmm_register()) {913do_push(res);914opCall->set_result_opr(to_fpu_stack_top(res));915}916}917918#ifndef PRODUCT919void FpuStackAllocator::check_invalid_lir_op(LIR_Op* op) {920switch (op->code()) {921case lir_24bit_FPU:922case lir_reset_FPU:923case lir_ffree:924assert(false, "operations not allowed in lir. If one of these operations is needed, check if they have fpu operands");925break;926927case lir_fpop_raw:928case lir_fxch:929case lir_fld:930assert(false, "operations only inserted by FpuStackAllocator");931break;932}933}934#endif935936937void FpuStackAllocator::merge_insert_add(LIR_List* instrs, FpuStackSim* cur_sim, int reg) {938LIR_Op1* move = new LIR_Op1(lir_move, LIR_OprFact::doubleConst(0), LIR_OprFact::double_fpu(reg)->make_fpu_stack_offset());939940instrs->instructions_list()->push(move);941942cur_sim->push(reg);943move->set_result_opr(to_fpu_stack(move->result_opr()));944945#ifndef PRODUCT946if (TraceFPUStack) {947tty->print("Added new register: %d New state: ", reg); cur_sim->print(); tty->cr();948}949#endif950}951952void FpuStackAllocator::merge_insert_xchg(LIR_List* instrs, FpuStackSim* cur_sim, int slot) {953assert(slot > 0, "no exchange necessary");954955LIR_Op1* fxch = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(slot));956instrs->instructions_list()->push(fxch);957cur_sim->swap(slot);958959#ifndef PRODUCT960if (TraceFPUStack) {961tty->print("Exchanged register: %d New state: ", cur_sim->get_slot(slot)); cur_sim->print(); tty->cr();962}963#endif964}965966void FpuStackAllocator::merge_insert_pop(LIR_List* instrs, FpuStackSim* cur_sim) {967int reg = cur_sim->get_slot(0);968969LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);970instrs->instructions_list()->push(fpop);971cur_sim->pop(reg);972973#ifndef PRODUCT974if (TraceFPUStack) {975tty->print("Removed register: %d New state: ", reg); cur_sim->print(); tty->cr();976}977#endif978}979980bool FpuStackAllocator::merge_rename(FpuStackSim* cur_sim, FpuStackSim* sux_sim, int start_slot, int change_slot) {981int reg = cur_sim->get_slot(change_slot);982983for (int slot = start_slot; slot >= 0; slot--) {984int new_reg = sux_sim->get_slot(slot);985986if (!cur_sim->contains(new_reg)) {987cur_sim->set_slot(change_slot, new_reg);988989#ifndef PRODUCT990if (TraceFPUStack) {991tty->print("Renamed register %d to %d New state: ", reg, new_reg); cur_sim->print(); tty->cr();992}993#endif994995return true;996}997}998return false;999}100010011002void FpuStackAllocator::merge_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, FpuStackSim* sux_sim) {1003#ifndef PRODUCT1004if (TraceFPUStack) {1005tty->cr();1006tty->print("before merging: pred: "); cur_sim->print(); tty->cr();1007tty->print(" sux: "); sux_sim->print(); tty->cr();1008}10091010int slot;1011for (slot = 0; slot < cur_sim->stack_size(); slot++) {1012assert(!cur_sim->slot_is_empty(slot), "not handled by algorithm");1013}1014for (slot = 0; slot < sux_sim->stack_size(); slot++) {1015assert(!sux_sim->slot_is_empty(slot), "not handled by algorithm");1016}1017#endif10181019// size difference between cur and sux that must be resolved by adding or removing values form the stack1020int size_diff = cur_sim->stack_size() - sux_sim->stack_size();10211022if (!ComputeExactFPURegisterUsage) {1023// add slots that are currently free, but used in successor1024// When the exact FPU register usage is computed, the stack does1025// not contain dead values at merging -> no values must be added10261027int sux_slot = sux_sim->stack_size() - 1;1028while (size_diff < 0) {1029assert(sux_slot >= 0, "slot out of bounds -> error in algorithm");10301031int reg = sux_sim->get_slot(sux_slot);1032if (!cur_sim->contains(reg)) {1033merge_insert_add(instrs, cur_sim, reg);1034size_diff++;10351036if (sux_slot + size_diff != 0) {1037merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);1038}1039}1040sux_slot--;1041}1042}10431044assert(cur_sim->stack_size() >= sux_sim->stack_size(), "stack size must be equal or greater now");1045assert(size_diff == cur_sim->stack_size() - sux_sim->stack_size(), "must be");10461047// stack merge algorithm:1048// 1) as long as the current stack top is not in the right location (that meens1049// it should not be on the stack top), exchange it into the right location1050// 2) if the stack top is right, but the remaining stack is not ordered correctly,1051// the stack top is exchanged away to get another value on top ->1052// now step 1) can be continued1053// the stack can also contain unused items -> these items are removed from stack10541055int finished_slot = sux_sim->stack_size() - 1;1056while (finished_slot >= 0 || size_diff > 0) {1057while (size_diff > 0 || (cur_sim->stack_size() > 0 && cur_sim->get_slot(0) != sux_sim->get_slot(0))) {1058int reg = cur_sim->get_slot(0);1059if (sux_sim->contains(reg)) {1060int sux_slot = sux_sim->offset_from_tos(reg);1061merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);10621063} else if (!merge_rename(cur_sim, sux_sim, finished_slot, 0)) {1064assert(size_diff > 0, "must be");10651066merge_insert_pop(instrs, cur_sim);1067size_diff--;1068}1069assert(cur_sim->stack_size() == 0 || cur_sim->get_slot(0) != reg, "register must have been changed");1070}10711072while (finished_slot >= 0 && cur_sim->get_slot(finished_slot) == sux_sim->get_slot(finished_slot)) {1073finished_slot--;1074}10751076if (finished_slot >= 0) {1077int reg = cur_sim->get_slot(finished_slot);10781079if (sux_sim->contains(reg) || !merge_rename(cur_sim, sux_sim, finished_slot, finished_slot)) {1080assert(sux_sim->contains(reg) || size_diff > 0, "must be");1081merge_insert_xchg(instrs, cur_sim, finished_slot);1082}1083assert(cur_sim->get_slot(finished_slot) != reg, "register must have been changed");1084}1085}10861087#ifndef PRODUCT1088if (TraceFPUStack) {1089tty->print("after merging: pred: "); cur_sim->print(); tty->cr();1090tty->print(" sux: "); sux_sim->print(); tty->cr();1091tty->cr();1092}1093#endif1094assert(cur_sim->stack_size() == sux_sim->stack_size(), "stack size must be equal now");1095}109610971098void FpuStackAllocator::merge_cleanup_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, BitMap& live_fpu_regs) {1099#ifndef PRODUCT1100if (TraceFPUStack) {1101tty->cr();1102tty->print("before cleanup: state: "); cur_sim->print(); tty->cr();1103tty->print(" live: "); live_fpu_regs.print_on(tty); tty->cr();1104}1105#endif11061107int slot = 0;1108while (slot < cur_sim->stack_size()) {1109int reg = cur_sim->get_slot(slot);1110if (!live_fpu_regs.at(reg)) {1111if (slot != 0) {1112merge_insert_xchg(instrs, cur_sim, slot);1113}1114merge_insert_pop(instrs, cur_sim);1115} else {1116slot++;1117}1118}11191120#ifndef PRODUCT1121if (TraceFPUStack) {1122tty->print("after cleanup: state: "); cur_sim->print(); tty->cr();1123tty->print(" live: "); live_fpu_regs.print_on(tty); tty->cr();1124tty->cr();1125}11261127// check if fpu stack only contains live registers1128for (unsigned int i = 0; i < live_fpu_regs.size(); i++) {1129if (live_fpu_regs.at(i) != cur_sim->contains(i)) {1130tty->print_cr("mismatch between required and actual stack content");1131break;1132}1133}1134#endif1135}113611371138bool FpuStackAllocator::merge_fpu_stack_with_successors(BlockBegin* block) {1139#ifndef PRODUCT1140if (TraceFPUStack) {1141tty->print_cr("Propagating FPU stack state for B%d at LIR_Op position %d to successors:",1142block->block_id(), pos());1143sim()->print();1144tty->cr();1145}1146#endif11471148bool changed = false;1149int number_of_sux = block->number_of_sux();11501151if (number_of_sux == 1 && block->sux_at(0)->number_of_preds() > 1) {1152// The successor has at least two incoming edges, so a stack merge will be necessary1153// If this block is the first predecessor, cleanup the current stack and propagate it1154// If this block is not the first predecessor, a stack merge will be necessary11551156BlockBegin* sux = block->sux_at(0);1157intArray* state = sux->fpu_stack_state();1158LIR_List* instrs = new LIR_List(_compilation);11591160if (state != NULL) {1161// Merge with a successors that already has a FPU stack state1162// the block must only have one successor because critical edges must been split1163FpuStackSim* cur_sim = sim();1164FpuStackSim* sux_sim = temp_sim();1165sux_sim->read_state(state);11661167merge_fpu_stack(instrs, cur_sim, sux_sim);11681169} else {1170// propagate current FPU stack state to successor without state1171// clean up stack first so that there are no dead values on the stack1172if (ComputeExactFPURegisterUsage) {1173FpuStackSim* cur_sim = sim();1174BitMap live_fpu_regs = block->sux_at(0)->fpu_register_usage();1175assert(live_fpu_regs.size() == FrameMap::nof_fpu_regs, "missing register usage");11761177merge_cleanup_fpu_stack(instrs, cur_sim, live_fpu_regs);1178}11791180intArray* state = sim()->write_state();1181if (TraceFPUStack) {1182tty->print_cr("Setting FPU stack state of B%d (merge path)", sux->block_id());1183sim()->print(); tty->cr();1184}1185sux->set_fpu_stack_state(state);1186}11871188if (instrs->instructions_list()->length() > 0) {1189lir()->insert_before(pos(), instrs);1190set_pos(instrs->instructions_list()->length() + pos());1191changed = true;1192}11931194} else {1195// Propagate unmodified Stack to successors where a stack merge is not necessary1196intArray* state = sim()->write_state();1197for (int i = 0; i < number_of_sux; i++) {1198BlockBegin* sux = block->sux_at(i);11991200#ifdef ASSERT1201for (int j = 0; j < sux->number_of_preds(); j++) {1202assert(block == sux->pred_at(j), "all critical edges must be broken");1203}12041205// check if new state is same1206if (sux->fpu_stack_state() != NULL) {1207intArray* sux_state = sux->fpu_stack_state();1208assert(state->length() == sux_state->length(), "overwriting existing stack state");1209for (int j = 0; j < state->length(); j++) {1210assert(state->at(j) == sux_state->at(j), "overwriting existing stack state");1211}1212}1213#endif1214#ifndef PRODUCT1215if (TraceFPUStack) {1216tty->print_cr("Setting FPU stack state of B%d", sux->block_id());1217sim()->print(); tty->cr();1218}1219#endif12201221sux->set_fpu_stack_state(state);1222}1223}12241225#ifndef PRODUCT1226// assertions that FPU stack state conforms to all successors' states1227intArray* cur_state = sim()->write_state();1228for (int i = 0; i < number_of_sux; i++) {1229BlockBegin* sux = block->sux_at(i);1230intArray* sux_state = sux->fpu_stack_state();12311232assert(sux_state != NULL, "no fpu state");1233assert(cur_state->length() == sux_state->length(), "incorrect length");1234for (int i = 0; i < cur_state->length(); i++) {1235assert(cur_state->at(i) == sux_state->at(i), "element not equal");1236}1237}1238#endif12391240return changed;1241}124212431244