src/cpu/x86/vm/c1_LinearScan_x86.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "c1/c1_Instruction.hpp"
aoqi@0 27 #include "c1/c1_LinearScan.hpp"
aoqi@0 28 #include "utilities/bitMap.inline.hpp"
aoqi@0 29
aoqi@0 30
aoqi@0 31 //----------------------------------------------------------------------
aoqi@0 32 // Allocation of FPU stack slots (Intel x86 only)
aoqi@0 33 //----------------------------------------------------------------------
aoqi@0 34
aoqi@0 35 void LinearScan::allocate_fpu_stack() {
aoqi@0 36 // First compute which FPU registers are live at the start of each basic block
aoqi@0 37 // (To minimize the amount of work we have to do if we have to merge FPU stacks)
aoqi@0 38 if (ComputeExactFPURegisterUsage) {
aoqi@0 39 Interval* intervals_in_register, *intervals_in_memory;
aoqi@0 40 create_unhandled_lists(&intervals_in_register, &intervals_in_memory, is_in_fpu_register, NULL);
aoqi@0 41
aoqi@0 42 // ignore memory intervals by overwriting intervals_in_memory
aoqi@0 43 // the dummy interval is needed to enforce the walker to walk until the given id:
aoqi@0 44 // without it, the walker stops when the unhandled-list is empty -> live information
aoqi@0 45 // beyond this point would be incorrect.
aoqi@0 46 Interval* dummy_interval = new Interval(any_reg);
aoqi@0 47 dummy_interval->add_range(max_jint - 2, max_jint - 1);
aoqi@0 48 dummy_interval->set_next(Interval::end());
aoqi@0 49 intervals_in_memory = dummy_interval;
aoqi@0 50
aoqi@0 51 IntervalWalker iw(this, intervals_in_register, intervals_in_memory);
aoqi@0 52
aoqi@0 53 const int num_blocks = block_count();
aoqi@0 54 for (int i = 0; i < num_blocks; i++) {
aoqi@0 55 BlockBegin* b = block_at(i);
aoqi@0 56
aoqi@0 57 // register usage is only needed for merging stacks -> compute only
aoqi@0 58 // when more than one predecessor.
aoqi@0 59 // the block must not have any spill moves at the beginning (checked by assertions)
aoqi@0 60 // spill moves would use intervals that are marked as handled and so the usage bit
aoqi@0 61 // would been set incorrectly
aoqi@0 62
aoqi@0 63 // NOTE: the check for number_of_preds > 1 is necessary. A block with only one
aoqi@0 64 // predecessor may have spill moves at the begin of the block.
aoqi@0 65 // If an interval ends at the current instruction id, it is not possible
aoqi@0 66 // to decide if the register is live or not at the block begin -> the
aoqi@0 67 // register information would be incorrect.
aoqi@0 68 if (b->number_of_preds() > 1) {
aoqi@0 69 int id = b->first_lir_instruction_id();
aoqi@0 70 BitMap regs(FrameMap::nof_fpu_regs);
aoqi@0 71 regs.clear();
aoqi@0 72
aoqi@0 73 iw.walk_to(id); // walk after the first instruction (always a label) of the block
aoqi@0 74 assert(iw.current_position() == id, "did not walk completely to id");
aoqi@0 75
aoqi@0 76 // Only consider FPU values in registers
aoqi@0 77 Interval* interval = iw.active_first(fixedKind);
aoqi@0 78 while (interval != Interval::end()) {
aoqi@0 79 int reg = interval->assigned_reg();
aoqi@0 80 assert(reg >= pd_first_fpu_reg && reg <= pd_last_fpu_reg, "no fpu register");
aoqi@0 81 assert(interval->assigned_regHi() == -1, "must not have hi register (doubles stored in one register)");
aoqi@0 82 assert(interval->from() <= id && id < interval->to(), "interval out of range");
aoqi@0 83
aoqi@0 84 #ifndef PRODUCT
aoqi@0 85 if (TraceFPURegisterUsage) {
aoqi@0 86 tty->print("fpu reg %d is live because of ", reg - pd_first_fpu_reg); interval->print();
aoqi@0 87 }
aoqi@0 88 #endif
aoqi@0 89
aoqi@0 90 regs.set_bit(reg - pd_first_fpu_reg);
aoqi@0 91 interval = interval->next();
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 b->set_fpu_register_usage(regs);
aoqi@0 95
aoqi@0 96 #ifndef PRODUCT
aoqi@0 97 if (TraceFPURegisterUsage) {
aoqi@0 98 tty->print("FPU regs for block %d, LIR instr %d): ", b->block_id(), id); regs.print_on(tty); tty->cr();
aoqi@0 99 }
aoqi@0 100 #endif
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 FpuStackAllocator alloc(ir()->compilation(), this);
aoqi@0 106 _fpu_stack_allocator = &alloc;
aoqi@0 107 alloc.allocate();
aoqi@0 108 _fpu_stack_allocator = NULL;
aoqi@0 109 }
aoqi@0 110
aoqi@0 111
aoqi@0 112 FpuStackAllocator::FpuStackAllocator(Compilation* compilation, LinearScan* allocator)
aoqi@0 113 : _compilation(compilation)
aoqi@0 114 , _lir(NULL)
aoqi@0 115 , _pos(-1)
aoqi@0 116 , _allocator(allocator)
aoqi@0 117 , _sim(compilation)
aoqi@0 118 , _temp_sim(compilation)
aoqi@0 119 {}
aoqi@0 120
aoqi@0 121 void FpuStackAllocator::allocate() {
aoqi@0 122 int num_blocks = allocator()->block_count();
aoqi@0 123 for (int i = 0; i < num_blocks; i++) {
aoqi@0 124 // Set up to process block
aoqi@0 125 BlockBegin* block = allocator()->block_at(i);
aoqi@0 126 intArray* fpu_stack_state = block->fpu_stack_state();
aoqi@0 127
aoqi@0 128 #ifndef PRODUCT
aoqi@0 129 if (TraceFPUStack) {
aoqi@0 130 tty->cr();
aoqi@0 131 tty->print_cr("------- Begin of new Block %d -------", block->block_id());
aoqi@0 132 }
aoqi@0 133 #endif
aoqi@0 134
aoqi@0 135 assert(fpu_stack_state != NULL ||
aoqi@0 136 block->end()->as_Base() != NULL ||
aoqi@0 137 block->is_set(BlockBegin::exception_entry_flag),
aoqi@0 138 "FPU stack state must be present due to linear-scan order for FPU stack allocation");
aoqi@0 139 // note: exception handler entries always start with an empty fpu stack
aoqi@0 140 // because stack merging would be too complicated
aoqi@0 141
aoqi@0 142 if (fpu_stack_state != NULL) {
aoqi@0 143 sim()->read_state(fpu_stack_state);
aoqi@0 144 } else {
aoqi@0 145 sim()->clear();
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 #ifndef PRODUCT
aoqi@0 149 if (TraceFPUStack) {
aoqi@0 150 tty->print("Reading FPU state for block %d:", block->block_id());
aoqi@0 151 sim()->print();
aoqi@0 152 tty->cr();
aoqi@0 153 }
aoqi@0 154 #endif
aoqi@0 155
aoqi@0 156 allocate_block(block);
aoqi@0 157 CHECK_BAILOUT();
aoqi@0 158 }
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 void FpuStackAllocator::allocate_block(BlockBegin* block) {
aoqi@0 162 bool processed_merge = false;
aoqi@0 163 LIR_OpList* insts = block->lir()->instructions_list();
aoqi@0 164 set_lir(block->lir());
aoqi@0 165 set_pos(0);
aoqi@0 166
aoqi@0 167
aoqi@0 168 // Note: insts->length() may change during loop
aoqi@0 169 while (pos() < insts->length()) {
aoqi@0 170 LIR_Op* op = insts->at(pos());
aoqi@0 171 _debug_information_computed = false;
aoqi@0 172
aoqi@0 173 #ifndef PRODUCT
aoqi@0 174 if (TraceFPUStack) {
aoqi@0 175 op->print();
aoqi@0 176 }
aoqi@0 177 check_invalid_lir_op(op);
aoqi@0 178 #endif
aoqi@0 179
aoqi@0 180 LIR_OpBranch* branch = op->as_OpBranch();
aoqi@0 181 LIR_Op1* op1 = op->as_Op1();
aoqi@0 182 LIR_Op2* op2 = op->as_Op2();
aoqi@0 183 LIR_OpCall* opCall = op->as_OpCall();
aoqi@0 184
aoqi@0 185 if (branch != NULL && branch->block() != NULL) {
aoqi@0 186 if (!processed_merge) {
aoqi@0 187 // propagate stack at first branch to a successor
aoqi@0 188 processed_merge = true;
aoqi@0 189 bool required_merge = merge_fpu_stack_with_successors(block);
aoqi@0 190
aoqi@0 191 assert(!required_merge || branch->cond() == lir_cond_always, "splitting of critical edges should prevent FPU stack mismatches at cond branches");
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 } else if (op1 != NULL) {
aoqi@0 195 handle_op1(op1);
aoqi@0 196 } else if (op2 != NULL) {
aoqi@0 197 handle_op2(op2);
aoqi@0 198 } else if (opCall != NULL) {
aoqi@0 199 handle_opCall(opCall);
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 compute_debug_information(op);
aoqi@0 203
aoqi@0 204 set_pos(1 + pos());
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 // Propagate stack when block does not end with branch
aoqi@0 208 if (!processed_merge) {
aoqi@0 209 merge_fpu_stack_with_successors(block);
aoqi@0 210 }
aoqi@0 211 }
aoqi@0 212
aoqi@0 213
aoqi@0 214 void FpuStackAllocator::compute_debug_information(LIR_Op* op) {
aoqi@0 215 if (!_debug_information_computed && op->id() != -1 && allocator()->has_info(op->id())) {
aoqi@0 216 visitor.visit(op);
aoqi@0 217
aoqi@0 218 // exception handling
aoqi@0 219 if (allocator()->compilation()->has_exception_handlers()) {
aoqi@0 220 XHandlers* xhandlers = visitor.all_xhandler();
aoqi@0 221 int n = xhandlers->length();
aoqi@0 222 for (int k = 0; k < n; k++) {
aoqi@0 223 allocate_exception_handler(xhandlers->handler_at(k));
aoqi@0 224 }
aoqi@0 225 } else {
aoqi@0 226 assert(visitor.all_xhandler()->length() == 0, "missed exception handler");
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 // compute debug information
aoqi@0 230 int n = visitor.info_count();
aoqi@0 231 assert(n > 0, "should not visit operation otherwise");
aoqi@0 232
aoqi@0 233 for (int j = 0; j < n; j++) {
aoqi@0 234 CodeEmitInfo* info = visitor.info_at(j);
aoqi@0 235 // Compute debug information
aoqi@0 236 allocator()->compute_debug_info(info, op->id());
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239 _debug_information_computed = true;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 void FpuStackAllocator::allocate_exception_handler(XHandler* xhandler) {
aoqi@0 243 if (!sim()->is_empty()) {
aoqi@0 244 LIR_List* old_lir = lir();
aoqi@0 245 int old_pos = pos();
aoqi@0 246 intArray* old_state = sim()->write_state();
aoqi@0 247
aoqi@0 248 #ifndef PRODUCT
aoqi@0 249 if (TraceFPUStack) {
aoqi@0 250 tty->cr();
aoqi@0 251 tty->print_cr("------- begin of exception handler -------");
aoqi@0 252 }
aoqi@0 253 #endif
aoqi@0 254
aoqi@0 255 if (xhandler->entry_code() == NULL) {
aoqi@0 256 // need entry code to clear FPU stack
aoqi@0 257 LIR_List* entry_code = new LIR_List(_compilation);
aoqi@0 258 entry_code->jump(xhandler->entry_block());
aoqi@0 259 xhandler->set_entry_code(entry_code);
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 LIR_OpList* insts = xhandler->entry_code()->instructions_list();
aoqi@0 263 set_lir(xhandler->entry_code());
aoqi@0 264 set_pos(0);
aoqi@0 265
aoqi@0 266 // Note: insts->length() may change during loop
aoqi@0 267 while (pos() < insts->length()) {
aoqi@0 268 LIR_Op* op = insts->at(pos());
aoqi@0 269
aoqi@0 270 #ifndef PRODUCT
aoqi@0 271 if (TraceFPUStack) {
aoqi@0 272 op->print();
aoqi@0 273 }
aoqi@0 274 check_invalid_lir_op(op);
aoqi@0 275 #endif
aoqi@0 276
aoqi@0 277 switch (op->code()) {
aoqi@0 278 case lir_move:
aoqi@0 279 assert(op->as_Op1() != NULL, "must be LIR_Op1");
aoqi@0 280 assert(pos() != insts->length() - 1, "must not be last operation");
aoqi@0 281
aoqi@0 282 handle_op1((LIR_Op1*)op);
aoqi@0 283 break;
aoqi@0 284
aoqi@0 285 case lir_branch:
aoqi@0 286 assert(op->as_OpBranch()->cond() == lir_cond_always, "must be unconditional branch");
aoqi@0 287 assert(pos() == insts->length() - 1, "must be last operation");
aoqi@0 288
aoqi@0 289 // remove all remaining dead registers from FPU stack
aoqi@0 290 clear_fpu_stack(LIR_OprFact::illegalOpr);
aoqi@0 291 break;
aoqi@0 292
aoqi@0 293 default:
aoqi@0 294 // other operations not allowed in exception entry code
aoqi@0 295 ShouldNotReachHere();
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 set_pos(pos() + 1);
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 #ifndef PRODUCT
aoqi@0 302 if (TraceFPUStack) {
aoqi@0 303 tty->cr();
aoqi@0 304 tty->print_cr("------- end of exception handler -------");
aoqi@0 305 }
aoqi@0 306 #endif
aoqi@0 307
aoqi@0 308 set_lir(old_lir);
aoqi@0 309 set_pos(old_pos);
aoqi@0 310 sim()->read_state(old_state);
aoqi@0 311 }
aoqi@0 312 }
aoqi@0 313
aoqi@0 314
aoqi@0 315 int FpuStackAllocator::fpu_num(LIR_Opr opr) {
aoqi@0 316 assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");
aoqi@0 317 return opr->is_single_fpu() ? opr->fpu_regnr() : opr->fpu_regnrLo();
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 int FpuStackAllocator::tos_offset(LIR_Opr opr) {
aoqi@0 321 return sim()->offset_from_tos(fpu_num(opr));
aoqi@0 322 }
aoqi@0 323
aoqi@0 324
aoqi@0 325 LIR_Opr FpuStackAllocator::to_fpu_stack(LIR_Opr opr) {
aoqi@0 326 assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");
aoqi@0 327
aoqi@0 328 int stack_offset = tos_offset(opr);
aoqi@0 329 if (opr->is_single_fpu()) {
aoqi@0 330 return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();
aoqi@0 331 } else {
aoqi@0 332 assert(opr->is_double_fpu(), "shouldn't call this otherwise");
aoqi@0 333 return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();
aoqi@0 334 }
aoqi@0 335 }
aoqi@0 336
aoqi@0 337 LIR_Opr FpuStackAllocator::to_fpu_stack_top(LIR_Opr opr, bool dont_check_offset) {
aoqi@0 338 assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");
aoqi@0 339 assert(dont_check_offset || tos_offset(opr) == 0, "operand is not on stack top");
aoqi@0 340
aoqi@0 341 int stack_offset = 0;
aoqi@0 342 if (opr->is_single_fpu()) {
aoqi@0 343 return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();
aoqi@0 344 } else {
aoqi@0 345 assert(opr->is_double_fpu(), "shouldn't call this otherwise");
aoqi@0 346 return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();
aoqi@0 347 }
aoqi@0 348 }
aoqi@0 349
aoqi@0 350
aoqi@0 351
aoqi@0 352 void FpuStackAllocator::insert_op(LIR_Op* op) {
aoqi@0 353 lir()->insert_before(pos(), op);
aoqi@0 354 set_pos(1 + pos());
aoqi@0 355 }
aoqi@0 356
aoqi@0 357
aoqi@0 358 void FpuStackAllocator::insert_exchange(int offset) {
aoqi@0 359 if (offset > 0) {
aoqi@0 360 LIR_Op1* fxch_op = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);
aoqi@0 361 insert_op(fxch_op);
aoqi@0 362 sim()->swap(offset);
aoqi@0 363
aoqi@0 364 #ifndef PRODUCT
aoqi@0 365 if (TraceFPUStack) {
aoqi@0 366 tty->print("Exchanged register: %d New state: ", sim()->get_slot(0)); sim()->print(); tty->cr();
aoqi@0 367 }
aoqi@0 368 #endif
aoqi@0 369
aoqi@0 370 }
aoqi@0 371 }
aoqi@0 372
aoqi@0 373 void FpuStackAllocator::insert_exchange(LIR_Opr opr) {
aoqi@0 374 insert_exchange(tos_offset(opr));
aoqi@0 375 }
aoqi@0 376
aoqi@0 377
aoqi@0 378 void FpuStackAllocator::insert_free(int offset) {
aoqi@0 379 // move stack slot to the top of stack and then pop it
aoqi@0 380 insert_exchange(offset);
aoqi@0 381
aoqi@0 382 LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);
aoqi@0 383 insert_op(fpop);
aoqi@0 384 sim()->pop();
aoqi@0 385
aoqi@0 386 #ifndef PRODUCT
aoqi@0 387 if (TraceFPUStack) {
aoqi@0 388 tty->print("Inserted pop New state: "); sim()->print(); tty->cr();
aoqi@0 389 }
aoqi@0 390 #endif
aoqi@0 391 }
aoqi@0 392
aoqi@0 393
aoqi@0 394 void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr) {
aoqi@0 395 if (sim()->contains(fpu_num(opr))) {
aoqi@0 396 int res_slot = tos_offset(opr);
aoqi@0 397 insert_free(res_slot);
aoqi@0 398 }
aoqi@0 399 }
aoqi@0 400
aoqi@0 401 void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr, LIR_Opr ignore) {
aoqi@0 402 if (fpu_num(opr) != fpu_num(ignore) && sim()->contains(fpu_num(opr))) {
aoqi@0 403 int res_slot = tos_offset(opr);
aoqi@0 404 insert_free(res_slot);
aoqi@0 405 }
aoqi@0 406 }
aoqi@0 407
aoqi@0 408 void FpuStackAllocator::insert_copy(LIR_Opr from, LIR_Opr to) {
aoqi@0 409 int offset = tos_offset(from);
aoqi@0 410 LIR_Op1* fld = new LIR_Op1(lir_fld, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);
aoqi@0 411 insert_op(fld);
aoqi@0 412
aoqi@0 413 sim()->push(fpu_num(to));
aoqi@0 414
aoqi@0 415 #ifndef PRODUCT
aoqi@0 416 if (TraceFPUStack) {
aoqi@0 417 tty->print("Inserted copy (%d -> %d) New state: ", fpu_num(from), fpu_num(to)); sim()->print(); tty->cr();
aoqi@0 418 }
aoqi@0 419 #endif
aoqi@0 420 }
aoqi@0 421
aoqi@0 422 void FpuStackAllocator::do_rename(LIR_Opr from, LIR_Opr to) {
aoqi@0 423 sim()->rename(fpu_num(from), fpu_num(to));
aoqi@0 424 }
aoqi@0 425
aoqi@0 426 void FpuStackAllocator::do_push(LIR_Opr opr) {
aoqi@0 427 sim()->push(fpu_num(opr));
aoqi@0 428 }
aoqi@0 429
aoqi@0 430 void FpuStackAllocator::pop_if_last_use(LIR_Op* op, LIR_Opr opr) {
aoqi@0 431 assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");
aoqi@0 432 assert(tos_offset(opr) == 0, "can only pop stack top");
aoqi@0 433
aoqi@0 434 if (opr->is_last_use()) {
aoqi@0 435 op->set_fpu_pop_count(1);
aoqi@0 436 sim()->pop();
aoqi@0 437 }
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 void FpuStackAllocator::pop_always(LIR_Op* op, LIR_Opr opr) {
aoqi@0 441 assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");
aoqi@0 442 assert(tos_offset(opr) == 0, "can only pop stack top");
aoqi@0 443
aoqi@0 444 op->set_fpu_pop_count(1);
aoqi@0 445 sim()->pop();
aoqi@0 446 }
aoqi@0 447
aoqi@0 448 void FpuStackAllocator::clear_fpu_stack(LIR_Opr preserve) {
aoqi@0 449 int result_stack_size = (preserve->is_fpu_register() && !preserve->is_xmm_register() ? 1 : 0);
aoqi@0 450 while (sim()->stack_size() > result_stack_size) {
aoqi@0 451 assert(!sim()->slot_is_empty(0), "not allowed");
aoqi@0 452
aoqi@0 453 if (result_stack_size == 0 || sim()->get_slot(0) != fpu_num(preserve)) {
aoqi@0 454 insert_free(0);
aoqi@0 455 } else {
aoqi@0 456 // move "preserve" to bottom of stack so that all other stack slots can be popped
aoqi@0 457 insert_exchange(sim()->stack_size() - 1);
aoqi@0 458 }
aoqi@0 459 }
aoqi@0 460 }
aoqi@0 461
aoqi@0 462
aoqi@0 463 void FpuStackAllocator::handle_op1(LIR_Op1* op1) {
aoqi@0 464 LIR_Opr in = op1->in_opr();
aoqi@0 465 LIR_Opr res = op1->result_opr();
aoqi@0 466
aoqi@0 467 LIR_Opr new_in = in; // new operands relative to the actual fpu stack top
aoqi@0 468 LIR_Opr new_res = res;
aoqi@0 469
aoqi@0 470 // Note: this switch is processed for all LIR_Op1, regardless if they have FPU-arguments,
aoqi@0 471 // so checks for is_float_kind() are necessary inside the cases
aoqi@0 472 switch (op1->code()) {
aoqi@0 473
aoqi@0 474 case lir_return: {
aoqi@0 475 // FPU-Stack must only contain the (optional) fpu return value.
aoqi@0 476 // All remaining dead values are popped from the stack
aoqi@0 477 // If the input operand is a fpu-register, it is exchanged to the bottom of the stack
aoqi@0 478
aoqi@0 479 clear_fpu_stack(in);
aoqi@0 480 if (in->is_fpu_register() && !in->is_xmm_register()) {
aoqi@0 481 new_in = to_fpu_stack_top(in);
aoqi@0 482 }
aoqi@0 483
aoqi@0 484 break;
aoqi@0 485 }
aoqi@0 486
aoqi@0 487 case lir_move: {
aoqi@0 488 if (in->is_fpu_register() && !in->is_xmm_register()) {
aoqi@0 489 if (res->is_xmm_register()) {
aoqi@0 490 // move from fpu register to xmm register (necessary for operations that
aoqi@0 491 // are not available in the SSE instruction set)
aoqi@0 492 insert_exchange(in);
aoqi@0 493 new_in = to_fpu_stack_top(in);
aoqi@0 494 pop_always(op1, in);
aoqi@0 495
aoqi@0 496 } else if (res->is_fpu_register() && !res->is_xmm_register()) {
aoqi@0 497 // move from fpu-register to fpu-register:
aoqi@0 498 // * input and result register equal:
aoqi@0 499 // nothing to do
aoqi@0 500 // * input register is last use:
aoqi@0 501 // rename the input register to result register -> input register
aoqi@0 502 // not present on fpu-stack afterwards
aoqi@0 503 // * input register not last use:
aoqi@0 504 // duplicate input register to result register to preserve input
aoqi@0 505 //
aoqi@0 506 // Note: The LIR-Assembler does not produce any code for fpu register moves,
aoqi@0 507 // so input and result stack index must be equal
aoqi@0 508
aoqi@0 509 if (fpu_num(in) == fpu_num(res)) {
aoqi@0 510 // nothing to do
aoqi@0 511 } else if (in->is_last_use()) {
aoqi@0 512 insert_free_if_dead(res);//, in);
aoqi@0 513 do_rename(in, res);
aoqi@0 514 } else {
aoqi@0 515 insert_free_if_dead(res);
aoqi@0 516 insert_copy(in, res);
aoqi@0 517 }
aoqi@0 518 new_in = to_fpu_stack(res);
aoqi@0 519 new_res = new_in;
aoqi@0 520
aoqi@0 521 } else {
aoqi@0 522 // move from fpu-register to memory
aoqi@0 523 // input operand must be on top of stack
aoqi@0 524
aoqi@0 525 insert_exchange(in);
aoqi@0 526
aoqi@0 527 // create debug information here because afterwards the register may have been popped
aoqi@0 528 compute_debug_information(op1);
aoqi@0 529
aoqi@0 530 new_in = to_fpu_stack_top(in);
aoqi@0 531 pop_if_last_use(op1, in);
aoqi@0 532 }
aoqi@0 533
aoqi@0 534 } else if (res->is_fpu_register() && !res->is_xmm_register()) {
aoqi@0 535 // move from memory/constant to fpu register
aoqi@0 536 // result is pushed on the stack
aoqi@0 537
aoqi@0 538 insert_free_if_dead(res);
aoqi@0 539
aoqi@0 540 // create debug information before register is pushed
aoqi@0 541 compute_debug_information(op1);
aoqi@0 542
aoqi@0 543 do_push(res);
aoqi@0 544 new_res = to_fpu_stack_top(res);
aoqi@0 545 }
aoqi@0 546 break;
aoqi@0 547 }
aoqi@0 548
aoqi@0 549 case lir_neg: {
aoqi@0 550 if (in->is_fpu_register() && !in->is_xmm_register()) {
aoqi@0 551 assert(res->is_fpu_register() && !res->is_xmm_register(), "must be");
aoqi@0 552 assert(in->is_last_use(), "old value gets destroyed");
aoqi@0 553
aoqi@0 554 insert_free_if_dead(res, in);
aoqi@0 555 insert_exchange(in);
aoqi@0 556 new_in = to_fpu_stack_top(in);
aoqi@0 557
aoqi@0 558 do_rename(in, res);
aoqi@0 559 new_res = to_fpu_stack_top(res);
aoqi@0 560 }
aoqi@0 561 break;
aoqi@0 562 }
aoqi@0 563
aoqi@0 564 case lir_convert: {
aoqi@0 565 Bytecodes::Code bc = op1->as_OpConvert()->bytecode();
aoqi@0 566 switch (bc) {
aoqi@0 567 case Bytecodes::_d2f:
aoqi@0 568 case Bytecodes::_f2d:
aoqi@0 569 assert(res->is_fpu_register(), "must be");
aoqi@0 570 assert(in->is_fpu_register(), "must be");
aoqi@0 571
aoqi@0 572 if (!in->is_xmm_register() && !res->is_xmm_register()) {
aoqi@0 573 // this is quite the same as a move from fpu-register to fpu-register
aoqi@0 574 // Note: input and result operands must have different types
aoqi@0 575 if (fpu_num(in) == fpu_num(res)) {
aoqi@0 576 // nothing to do
aoqi@0 577 new_in = to_fpu_stack(in);
aoqi@0 578 } else if (in->is_last_use()) {
aoqi@0 579 insert_free_if_dead(res);//, in);
aoqi@0 580 new_in = to_fpu_stack(in);
aoqi@0 581 do_rename(in, res);
aoqi@0 582 } else {
aoqi@0 583 insert_free_if_dead(res);
aoqi@0 584 insert_copy(in, res);
aoqi@0 585 new_in = to_fpu_stack_top(in, true);
aoqi@0 586 }
aoqi@0 587 new_res = to_fpu_stack(res);
aoqi@0 588 }
aoqi@0 589
aoqi@0 590 break;
aoqi@0 591
aoqi@0 592 case Bytecodes::_i2f:
aoqi@0 593 case Bytecodes::_l2f:
aoqi@0 594 case Bytecodes::_i2d:
aoqi@0 595 case Bytecodes::_l2d:
aoqi@0 596 assert(res->is_fpu_register(), "must be");
aoqi@0 597 if (!res->is_xmm_register()) {
aoqi@0 598 insert_free_if_dead(res);
aoqi@0 599 do_push(res);
aoqi@0 600 new_res = to_fpu_stack_top(res);
aoqi@0 601 }
aoqi@0 602 break;
aoqi@0 603
aoqi@0 604 case Bytecodes::_f2i:
aoqi@0 605 case Bytecodes::_d2i:
aoqi@0 606 assert(in->is_fpu_register(), "must be");
aoqi@0 607 if (!in->is_xmm_register()) {
aoqi@0 608 insert_exchange(in);
aoqi@0 609 new_in = to_fpu_stack_top(in);
aoqi@0 610
aoqi@0 611 // TODO: update registes of stub
aoqi@0 612 }
aoqi@0 613 break;
aoqi@0 614
aoqi@0 615 case Bytecodes::_f2l:
aoqi@0 616 case Bytecodes::_d2l:
aoqi@0 617 assert(in->is_fpu_register(), "must be");
aoqi@0 618 if (!in->is_xmm_register()) {
aoqi@0 619 insert_exchange(in);
aoqi@0 620 new_in = to_fpu_stack_top(in);
aoqi@0 621 pop_always(op1, in);
aoqi@0 622 }
aoqi@0 623 break;
aoqi@0 624
aoqi@0 625 case Bytecodes::_i2l:
aoqi@0 626 case Bytecodes::_l2i:
aoqi@0 627 case Bytecodes::_i2b:
aoqi@0 628 case Bytecodes::_i2c:
aoqi@0 629 case Bytecodes::_i2s:
aoqi@0 630 // no fpu operands
aoqi@0 631 break;
aoqi@0 632
aoqi@0 633 default:
aoqi@0 634 ShouldNotReachHere();
aoqi@0 635 }
aoqi@0 636 break;
aoqi@0 637 }
aoqi@0 638
aoqi@0 639 case lir_roundfp: {
aoqi@0 640 assert(in->is_fpu_register() && !in->is_xmm_register(), "input must be in register");
aoqi@0 641 assert(res->is_stack(), "result must be on stack");
aoqi@0 642
aoqi@0 643 insert_exchange(in);
aoqi@0 644 new_in = to_fpu_stack_top(in);
aoqi@0 645 pop_if_last_use(op1, in);
aoqi@0 646 break;
aoqi@0 647 }
aoqi@0 648
aoqi@0 649 default: {
aoqi@0 650 assert(!in->is_float_kind() && !res->is_float_kind(), "missed a fpu-operation");
aoqi@0 651 }
aoqi@0 652 }
aoqi@0 653
aoqi@0 654 op1->set_in_opr(new_in);
aoqi@0 655 op1->set_result_opr(new_res);
aoqi@0 656 }
aoqi@0 657
aoqi@0 658 void FpuStackAllocator::handle_op2(LIR_Op2* op2) {
aoqi@0 659 LIR_Opr left = op2->in_opr1();
aoqi@0 660 if (!left->is_float_kind()) {
aoqi@0 661 return;
aoqi@0 662 }
aoqi@0 663 if (left->is_xmm_register()) {
aoqi@0 664 return;
aoqi@0 665 }
aoqi@0 666
aoqi@0 667 LIR_Opr right = op2->in_opr2();
aoqi@0 668 LIR_Opr res = op2->result_opr();
aoqi@0 669 LIR_Opr new_left = left; // new operands relative to the actual fpu stack top
aoqi@0 670 LIR_Opr new_right = right;
aoqi@0 671 LIR_Opr new_res = res;
aoqi@0 672
aoqi@0 673 assert(!left->is_xmm_register() && !right->is_xmm_register() && !res->is_xmm_register(), "not for xmm registers");
aoqi@0 674
aoqi@0 675 switch (op2->code()) {
aoqi@0 676 case lir_cmp:
aoqi@0 677 case lir_cmp_fd2i:
aoqi@0 678 case lir_ucmp_fd2i:
aoqi@0 679 case lir_assert: {
aoqi@0 680 assert(left->is_fpu_register(), "invalid LIR");
aoqi@0 681 assert(right->is_fpu_register(), "invalid LIR");
aoqi@0 682
aoqi@0 683 // the left-hand side must be on top of stack.
aoqi@0 684 // the right-hand side is never popped, even if is_last_use is set
aoqi@0 685 insert_exchange(left);
aoqi@0 686 new_left = to_fpu_stack_top(left);
aoqi@0 687 new_right = to_fpu_stack(right);
aoqi@0 688 pop_if_last_use(op2, left);
aoqi@0 689 break;
aoqi@0 690 }
aoqi@0 691
aoqi@0 692 case lir_mul_strictfp:
aoqi@0 693 case lir_div_strictfp: {
aoqi@0 694 assert(op2->tmp1_opr()->is_fpu_register(), "strict operations need temporary fpu stack slot");
aoqi@0 695 insert_free_if_dead(op2->tmp1_opr());
aoqi@0 696 assert(sim()->stack_size() <= 7, "at least one stack slot must be free");
aoqi@0 697 // fall-through: continue with the normal handling of lir_mul and lir_div
aoqi@0 698 }
aoqi@0 699 case lir_add:
aoqi@0 700 case lir_sub:
aoqi@0 701 case lir_mul:
aoqi@0 702 case lir_div: {
aoqi@0 703 assert(left->is_fpu_register(), "must be");
aoqi@0 704 assert(res->is_fpu_register(), "must be");
aoqi@0 705 assert(left->is_equal(res), "must be");
aoqi@0 706
aoqi@0 707 // either the left-hand or the right-hand side must be on top of stack
aoqi@0 708 // (if right is not a register, left must be on top)
aoqi@0 709 if (!right->is_fpu_register()) {
aoqi@0 710 insert_exchange(left);
aoqi@0 711 new_left = to_fpu_stack_top(left);
aoqi@0 712 } else {
aoqi@0 713 // no exchange necessary if right is alredy on top of stack
aoqi@0 714 if (tos_offset(right) == 0) {
aoqi@0 715 new_left = to_fpu_stack(left);
aoqi@0 716 new_right = to_fpu_stack_top(right);
aoqi@0 717 } else {
aoqi@0 718 insert_exchange(left);
aoqi@0 719 new_left = to_fpu_stack_top(left);
aoqi@0 720 new_right = to_fpu_stack(right);
aoqi@0 721 }
aoqi@0 722
aoqi@0 723 if (right->is_last_use()) {
aoqi@0 724 op2->set_fpu_pop_count(1);
aoqi@0 725
aoqi@0 726 if (tos_offset(right) == 0) {
aoqi@0 727 sim()->pop();
aoqi@0 728 } else {
aoqi@0 729 // if left is on top of stack, the result is placed in the stack
aoqi@0 730 // slot of right, so a renaming from right to res is necessary
aoqi@0 731 assert(tos_offset(left) == 0, "must be");
aoqi@0 732 sim()->pop();
aoqi@0 733 do_rename(right, res);
aoqi@0 734 }
aoqi@0 735 }
aoqi@0 736 }
aoqi@0 737 new_res = to_fpu_stack(res);
aoqi@0 738
aoqi@0 739 break;
aoqi@0 740 }
aoqi@0 741
aoqi@0 742 case lir_rem: {
aoqi@0 743 assert(left->is_fpu_register(), "must be");
aoqi@0 744 assert(right->is_fpu_register(), "must be");
aoqi@0 745 assert(res->is_fpu_register(), "must be");
aoqi@0 746 assert(left->is_equal(res), "must be");
aoqi@0 747
aoqi@0 748 // Must bring both operands to top of stack with following operand ordering:
aoqi@0 749 // * fpu stack before rem: ... right left
aoqi@0 750 // * fpu stack after rem: ... left
aoqi@0 751 if (tos_offset(right) != 1) {
aoqi@0 752 insert_exchange(right);
aoqi@0 753 insert_exchange(1);
aoqi@0 754 }
aoqi@0 755 insert_exchange(left);
aoqi@0 756 assert(tos_offset(right) == 1, "check");
aoqi@0 757 assert(tos_offset(left) == 0, "check");
aoqi@0 758
aoqi@0 759 new_left = to_fpu_stack_top(left);
aoqi@0 760 new_right = to_fpu_stack(right);
aoqi@0 761
aoqi@0 762 op2->set_fpu_pop_count(1);
aoqi@0 763 sim()->pop();
aoqi@0 764 do_rename(right, res);
aoqi@0 765
aoqi@0 766 new_res = to_fpu_stack_top(res);
aoqi@0 767 break;
aoqi@0 768 }
aoqi@0 769
aoqi@0 770 case lir_abs:
aoqi@0 771 case lir_sqrt: {
aoqi@0 772 // Right argument appears to be unused
aoqi@0 773 assert(right->is_illegal(), "must be");
aoqi@0 774 assert(left->is_fpu_register(), "must be");
aoqi@0 775 assert(res->is_fpu_register(), "must be");
aoqi@0 776 assert(left->is_last_use(), "old value gets destroyed");
aoqi@0 777
aoqi@0 778 insert_free_if_dead(res, left);
aoqi@0 779 insert_exchange(left);
aoqi@0 780 do_rename(left, res);
aoqi@0 781
aoqi@0 782 new_left = to_fpu_stack_top(res);
aoqi@0 783 new_res = new_left;
aoqi@0 784
aoqi@0 785 op2->set_fpu_stack_size(sim()->stack_size());
aoqi@0 786 break;
aoqi@0 787 }
aoqi@0 788
aoqi@0 789 case lir_log:
aoqi@0 790 case lir_log10: {
aoqi@0 791 // log and log10 need one temporary fpu stack slot, so
aoqi@0 792 // there is one temporary registers stored in temp of the
aoqi@0 793 // operation. the stack allocator must guarantee that the stack
aoqi@0 794 // slots are really free, otherwise there might be a stack
aoqi@0 795 // overflow.
aoqi@0 796 assert(right->is_illegal(), "must be");
aoqi@0 797 assert(left->is_fpu_register(), "must be");
aoqi@0 798 assert(res->is_fpu_register(), "must be");
aoqi@0 799 assert(op2->tmp1_opr()->is_fpu_register(), "must be");
aoqi@0 800
aoqi@0 801 insert_free_if_dead(op2->tmp1_opr());
aoqi@0 802 insert_free_if_dead(res, left);
aoqi@0 803 insert_exchange(left);
aoqi@0 804 do_rename(left, res);
aoqi@0 805
aoqi@0 806 new_left = to_fpu_stack_top(res);
aoqi@0 807 new_res = new_left;
aoqi@0 808
aoqi@0 809 op2->set_fpu_stack_size(sim()->stack_size());
aoqi@0 810 assert(sim()->stack_size() <= 7, "at least one stack slot must be free");
aoqi@0 811 break;
aoqi@0 812 }
aoqi@0 813
aoqi@0 814
aoqi@0 815 case lir_tan:
aoqi@0 816 case lir_sin:
aoqi@0 817 case lir_cos:
aoqi@0 818 case lir_exp: {
aoqi@0 819 // sin, cos and exp need two temporary fpu stack slots, so there are two temporary
aoqi@0 820 // registers (stored in right and temp of the operation).
aoqi@0 821 // the stack allocator must guarantee that the stack slots are really free,
aoqi@0 822 // otherwise there might be a stack overflow.
aoqi@0 823 assert(left->is_fpu_register(), "must be");
aoqi@0 824 assert(res->is_fpu_register(), "must be");
aoqi@0 825 // assert(left->is_last_use(), "old value gets destroyed");
aoqi@0 826 assert(right->is_fpu_register(), "right is used as the first temporary register");
aoqi@0 827 assert(op2->tmp1_opr()->is_fpu_register(), "temp is used as the second temporary register");
aoqi@0 828 assert(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");
aoqi@0 829
aoqi@0 830 insert_free_if_dead(right);
aoqi@0 831 insert_free_if_dead(op2->tmp1_opr());
aoqi@0 832
aoqi@0 833 insert_free_if_dead(res, left);
aoqi@0 834 insert_exchange(left);
aoqi@0 835 do_rename(left, res);
aoqi@0 836
aoqi@0 837 new_left = to_fpu_stack_top(res);
aoqi@0 838 new_res = new_left;
aoqi@0 839
aoqi@0 840 op2->set_fpu_stack_size(sim()->stack_size());
aoqi@0 841 assert(sim()->stack_size() <= 6, "at least two stack slots must be free");
aoqi@0 842 break;
aoqi@0 843 }
aoqi@0 844
aoqi@0 845 case lir_pow: {
aoqi@0 846 // pow needs two temporary fpu stack slots, so there are two temporary
aoqi@0 847 // registers (stored in tmp1 and tmp2 of the operation).
aoqi@0 848 // the stack allocator must guarantee that the stack slots are really free,
aoqi@0 849 // otherwise there might be a stack overflow.
aoqi@0 850 assert(left->is_fpu_register(), "must be");
aoqi@0 851 assert(right->is_fpu_register(), "must be");
aoqi@0 852 assert(res->is_fpu_register(), "must be");
aoqi@0 853
aoqi@0 854 assert(op2->tmp1_opr()->is_fpu_register(), "tmp1 is the first temporary register");
aoqi@0 855 assert(op2->tmp2_opr()->is_fpu_register(), "tmp2 is the second temporary register");
aoqi@0 856 assert(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");
aoqi@0 857 assert(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");
aoqi@0 858 assert(fpu_num(op2->tmp1_opr()) != fpu_num(op2->tmp2_opr()) && fpu_num(op2->tmp1_opr()) != fpu_num(res), "need distinct temp registers");
aoqi@0 859 assert(fpu_num(op2->tmp2_opr()) != fpu_num(res), "need distinct temp registers");
aoqi@0 860
aoqi@0 861 insert_free_if_dead(op2->tmp1_opr());
aoqi@0 862 insert_free_if_dead(op2->tmp2_opr());
aoqi@0 863
aoqi@0 864 // Must bring both operands to top of stack with following operand ordering:
aoqi@0 865 // * fpu stack before pow: ... right left
aoqi@0 866 // * fpu stack after pow: ... left
aoqi@0 867
aoqi@0 868 insert_free_if_dead(res, right);
aoqi@0 869
aoqi@0 870 if (tos_offset(right) != 1) {
aoqi@0 871 insert_exchange(right);
aoqi@0 872 insert_exchange(1);
aoqi@0 873 }
aoqi@0 874 insert_exchange(left);
aoqi@0 875 assert(tos_offset(right) == 1, "check");
aoqi@0 876 assert(tos_offset(left) == 0, "check");
aoqi@0 877
aoqi@0 878 new_left = to_fpu_stack_top(left);
aoqi@0 879 new_right = to_fpu_stack(right);
aoqi@0 880
aoqi@0 881 op2->set_fpu_stack_size(sim()->stack_size());
aoqi@0 882 assert(sim()->stack_size() <= 6, "at least two stack slots must be free");
aoqi@0 883
aoqi@0 884 sim()->pop();
aoqi@0 885
aoqi@0 886 do_rename(right, res);
aoqi@0 887
aoqi@0 888 new_res = to_fpu_stack_top(res);
aoqi@0 889 break;
aoqi@0 890 }
aoqi@0 891
aoqi@0 892 default: {
aoqi@0 893 assert(false, "missed a fpu-operation");
aoqi@0 894 }
aoqi@0 895 }
aoqi@0 896
aoqi@0 897 op2->set_in_opr1(new_left);
aoqi@0 898 op2->set_in_opr2(new_right);
aoqi@0 899 op2->set_result_opr(new_res);
aoqi@0 900 }
aoqi@0 901
aoqi@0 902 void FpuStackAllocator::handle_opCall(LIR_OpCall* opCall) {
aoqi@0 903 LIR_Opr res = opCall->result_opr();
aoqi@0 904
aoqi@0 905 // clear fpu-stack before call
aoqi@0 906 // it may contain dead values that could not have been remved by previous operations
aoqi@0 907 clear_fpu_stack(LIR_OprFact::illegalOpr);
aoqi@0 908 assert(sim()->is_empty(), "fpu stack must be empty now");
aoqi@0 909
aoqi@0 910 // compute debug information before (possible) fpu result is pushed
aoqi@0 911 compute_debug_information(opCall);
aoqi@0 912
aoqi@0 913 if (res->is_fpu_register() && !res->is_xmm_register()) {
aoqi@0 914 do_push(res);
aoqi@0 915 opCall->set_result_opr(to_fpu_stack_top(res));
aoqi@0 916 }
aoqi@0 917 }
aoqi@0 918
aoqi@0 919 #ifndef PRODUCT
aoqi@0 920 void FpuStackAllocator::check_invalid_lir_op(LIR_Op* op) {
aoqi@0 921 switch (op->code()) {
aoqi@0 922 case lir_24bit_FPU:
aoqi@0 923 case lir_reset_FPU:
aoqi@0 924 case lir_ffree:
aoqi@0 925 assert(false, "operations not allowed in lir. If one of these operations is needed, check if they have fpu operands");
aoqi@0 926 break;
aoqi@0 927
aoqi@0 928 case lir_fpop_raw:
aoqi@0 929 case lir_fxch:
aoqi@0 930 case lir_fld:
aoqi@0 931 assert(false, "operations only inserted by FpuStackAllocator");
aoqi@0 932 break;
aoqi@0 933 }
aoqi@0 934 }
aoqi@0 935 #endif
aoqi@0 936
aoqi@0 937
aoqi@0 938 void FpuStackAllocator::merge_insert_add(LIR_List* instrs, FpuStackSim* cur_sim, int reg) {
aoqi@0 939 LIR_Op1* move = new LIR_Op1(lir_move, LIR_OprFact::doubleConst(0), LIR_OprFact::double_fpu(reg)->make_fpu_stack_offset());
aoqi@0 940
aoqi@0 941 instrs->instructions_list()->push(move);
aoqi@0 942
aoqi@0 943 cur_sim->push(reg);
aoqi@0 944 move->set_result_opr(to_fpu_stack(move->result_opr()));
aoqi@0 945
aoqi@0 946 #ifndef PRODUCT
aoqi@0 947 if (TraceFPUStack) {
aoqi@0 948 tty->print("Added new register: %d New state: ", reg); cur_sim->print(); tty->cr();
aoqi@0 949 }
aoqi@0 950 #endif
aoqi@0 951 }
aoqi@0 952
aoqi@0 953 void FpuStackAllocator::merge_insert_xchg(LIR_List* instrs, FpuStackSim* cur_sim, int slot) {
aoqi@0 954 assert(slot > 0, "no exchange necessary");
aoqi@0 955
aoqi@0 956 LIR_Op1* fxch = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(slot));
aoqi@0 957 instrs->instructions_list()->push(fxch);
aoqi@0 958 cur_sim->swap(slot);
aoqi@0 959
aoqi@0 960 #ifndef PRODUCT
aoqi@0 961 if (TraceFPUStack) {
aoqi@0 962 tty->print("Exchanged register: %d New state: ", cur_sim->get_slot(slot)); cur_sim->print(); tty->cr();
aoqi@0 963 }
aoqi@0 964 #endif
aoqi@0 965 }
aoqi@0 966
aoqi@0 967 void FpuStackAllocator::merge_insert_pop(LIR_List* instrs, FpuStackSim* cur_sim) {
aoqi@0 968 int reg = cur_sim->get_slot(0);
aoqi@0 969
aoqi@0 970 LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);
aoqi@0 971 instrs->instructions_list()->push(fpop);
aoqi@0 972 cur_sim->pop(reg);
aoqi@0 973
aoqi@0 974 #ifndef PRODUCT
aoqi@0 975 if (TraceFPUStack) {
aoqi@0 976 tty->print("Removed register: %d New state: ", reg); cur_sim->print(); tty->cr();
aoqi@0 977 }
aoqi@0 978 #endif
aoqi@0 979 }
aoqi@0 980
aoqi@0 981 bool FpuStackAllocator::merge_rename(FpuStackSim* cur_sim, FpuStackSim* sux_sim, int start_slot, int change_slot) {
aoqi@0 982 int reg = cur_sim->get_slot(change_slot);
aoqi@0 983
aoqi@0 984 for (int slot = start_slot; slot >= 0; slot--) {
aoqi@0 985 int new_reg = sux_sim->get_slot(slot);
aoqi@0 986
aoqi@0 987 if (!cur_sim->contains(new_reg)) {
aoqi@0 988 cur_sim->set_slot(change_slot, new_reg);
aoqi@0 989
aoqi@0 990 #ifndef PRODUCT
aoqi@0 991 if (TraceFPUStack) {
aoqi@0 992 tty->print("Renamed register %d to %d New state: ", reg, new_reg); cur_sim->print(); tty->cr();
aoqi@0 993 }
aoqi@0 994 #endif
aoqi@0 995
aoqi@0 996 return true;
aoqi@0 997 }
aoqi@0 998 }
aoqi@0 999 return false;
aoqi@0 1000 }
aoqi@0 1001
aoqi@0 1002
aoqi@0 1003 void FpuStackAllocator::merge_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, FpuStackSim* sux_sim) {
aoqi@0 1004 #ifndef PRODUCT
aoqi@0 1005 if (TraceFPUStack) {
aoqi@0 1006 tty->cr();
aoqi@0 1007 tty->print("before merging: pred: "); cur_sim->print(); tty->cr();
aoqi@0 1008 tty->print(" sux: "); sux_sim->print(); tty->cr();
aoqi@0 1009 }
aoqi@0 1010
aoqi@0 1011 int slot;
aoqi@0 1012 for (slot = 0; slot < cur_sim->stack_size(); slot++) {
aoqi@0 1013 assert(!cur_sim->slot_is_empty(slot), "not handled by algorithm");
aoqi@0 1014 }
aoqi@0 1015 for (slot = 0; slot < sux_sim->stack_size(); slot++) {
aoqi@0 1016 assert(!sux_sim->slot_is_empty(slot), "not handled by algorithm");
aoqi@0 1017 }
aoqi@0 1018 #endif
aoqi@0 1019
aoqi@0 1020 // size difference between cur and sux that must be resolved by adding or removing values form the stack
aoqi@0 1021 int size_diff = cur_sim->stack_size() - sux_sim->stack_size();
aoqi@0 1022
aoqi@0 1023 if (!ComputeExactFPURegisterUsage) {
aoqi@0 1024 // add slots that are currently free, but used in successor
aoqi@0 1025 // When the exact FPU register usage is computed, the stack does
aoqi@0 1026 // not contain dead values at merging -> no values must be added
aoqi@0 1027
aoqi@0 1028 int sux_slot = sux_sim->stack_size() - 1;
aoqi@0 1029 while (size_diff < 0) {
aoqi@0 1030 assert(sux_slot >= 0, "slot out of bounds -> error in algorithm");
aoqi@0 1031
aoqi@0 1032 int reg = sux_sim->get_slot(sux_slot);
aoqi@0 1033 if (!cur_sim->contains(reg)) {
aoqi@0 1034 merge_insert_add(instrs, cur_sim, reg);
aoqi@0 1035 size_diff++;
aoqi@0 1036
aoqi@0 1037 if (sux_slot + size_diff != 0) {
aoqi@0 1038 merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);
aoqi@0 1039 }
aoqi@0 1040 }
aoqi@0 1041 sux_slot--;
aoqi@0 1042 }
aoqi@0 1043 }
aoqi@0 1044
aoqi@0 1045 assert(cur_sim->stack_size() >= sux_sim->stack_size(), "stack size must be equal or greater now");
aoqi@0 1046 assert(size_diff == cur_sim->stack_size() - sux_sim->stack_size(), "must be");
aoqi@0 1047
aoqi@0 1048 // stack merge algorithm:
aoqi@0 1049 // 1) as long as the current stack top is not in the right location (that meens
aoqi@0 1050 // it should not be on the stack top), exchange it into the right location
aoqi@0 1051 // 2) if the stack top is right, but the remaining stack is not ordered correctly,
aoqi@0 1052 // the stack top is exchanged away to get another value on top ->
aoqi@0 1053 // now step 1) can be continued
aoqi@0 1054 // the stack can also contain unused items -> these items are removed from stack
aoqi@0 1055
aoqi@0 1056 int finished_slot = sux_sim->stack_size() - 1;
aoqi@0 1057 while (finished_slot >= 0 || size_diff > 0) {
aoqi@0 1058 while (size_diff > 0 || (cur_sim->stack_size() > 0 && cur_sim->get_slot(0) != sux_sim->get_slot(0))) {
aoqi@0 1059 int reg = cur_sim->get_slot(0);
aoqi@0 1060 if (sux_sim->contains(reg)) {
aoqi@0 1061 int sux_slot = sux_sim->offset_from_tos(reg);
aoqi@0 1062 merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);
aoqi@0 1063
aoqi@0 1064 } else if (!merge_rename(cur_sim, sux_sim, finished_slot, 0)) {
aoqi@0 1065 assert(size_diff > 0, "must be");
aoqi@0 1066
aoqi@0 1067 merge_insert_pop(instrs, cur_sim);
aoqi@0 1068 size_diff--;
aoqi@0 1069 }
aoqi@0 1070 assert(cur_sim->stack_size() == 0 || cur_sim->get_slot(0) != reg, "register must have been changed");
aoqi@0 1071 }
aoqi@0 1072
aoqi@0 1073 while (finished_slot >= 0 && cur_sim->get_slot(finished_slot) == sux_sim->get_slot(finished_slot)) {
aoqi@0 1074 finished_slot--;
aoqi@0 1075 }
aoqi@0 1076
aoqi@0 1077 if (finished_slot >= 0) {
aoqi@0 1078 int reg = cur_sim->get_slot(finished_slot);
aoqi@0 1079
aoqi@0 1080 if (sux_sim->contains(reg) || !merge_rename(cur_sim, sux_sim, finished_slot, finished_slot)) {
aoqi@0 1081 assert(sux_sim->contains(reg) || size_diff > 0, "must be");
aoqi@0 1082 merge_insert_xchg(instrs, cur_sim, finished_slot);
aoqi@0 1083 }
aoqi@0 1084 assert(cur_sim->get_slot(finished_slot) != reg, "register must have been changed");
aoqi@0 1085 }
aoqi@0 1086 }
aoqi@0 1087
aoqi@0 1088 #ifndef PRODUCT
aoqi@0 1089 if (TraceFPUStack) {
aoqi@0 1090 tty->print("after merging: pred: "); cur_sim->print(); tty->cr();
aoqi@0 1091 tty->print(" sux: "); sux_sim->print(); tty->cr();
aoqi@0 1092 tty->cr();
aoqi@0 1093 }
aoqi@0 1094 #endif
aoqi@0 1095 assert(cur_sim->stack_size() == sux_sim->stack_size(), "stack size must be equal now");
aoqi@0 1096 }
aoqi@0 1097
aoqi@0 1098
aoqi@0 1099 void FpuStackAllocator::merge_cleanup_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, BitMap& live_fpu_regs) {
aoqi@0 1100 #ifndef PRODUCT
aoqi@0 1101 if (TraceFPUStack) {
aoqi@0 1102 tty->cr();
aoqi@0 1103 tty->print("before cleanup: state: "); cur_sim->print(); tty->cr();
aoqi@0 1104 tty->print(" live: "); live_fpu_regs.print_on(tty); tty->cr();
aoqi@0 1105 }
aoqi@0 1106 #endif
aoqi@0 1107
aoqi@0 1108 int slot = 0;
aoqi@0 1109 while (slot < cur_sim->stack_size()) {
aoqi@0 1110 int reg = cur_sim->get_slot(slot);
aoqi@0 1111 if (!live_fpu_regs.at(reg)) {
aoqi@0 1112 if (slot != 0) {
aoqi@0 1113 merge_insert_xchg(instrs, cur_sim, slot);
aoqi@0 1114 }
aoqi@0 1115 merge_insert_pop(instrs, cur_sim);
aoqi@0 1116 } else {
aoqi@0 1117 slot++;
aoqi@0 1118 }
aoqi@0 1119 }
aoqi@0 1120
aoqi@0 1121 #ifndef PRODUCT
aoqi@0 1122 if (TraceFPUStack) {
aoqi@0 1123 tty->print("after cleanup: state: "); cur_sim->print(); tty->cr();
aoqi@0 1124 tty->print(" live: "); live_fpu_regs.print_on(tty); tty->cr();
aoqi@0 1125 tty->cr();
aoqi@0 1126 }
aoqi@0 1127
aoqi@0 1128 // check if fpu stack only contains live registers
aoqi@0 1129 for (unsigned int i = 0; i < live_fpu_regs.size(); i++) {
aoqi@0 1130 if (live_fpu_regs.at(i) != cur_sim->contains(i)) {
aoqi@0 1131 tty->print_cr("mismatch between required and actual stack content");
aoqi@0 1132 break;
aoqi@0 1133 }
aoqi@0 1134 }
aoqi@0 1135 #endif
aoqi@0 1136 }
aoqi@0 1137
aoqi@0 1138
aoqi@0 1139 bool FpuStackAllocator::merge_fpu_stack_with_successors(BlockBegin* block) {
aoqi@0 1140 #ifndef PRODUCT
aoqi@0 1141 if (TraceFPUStack) {
aoqi@0 1142 tty->print_cr("Propagating FPU stack state for B%d at LIR_Op position %d to successors:",
aoqi@0 1143 block->block_id(), pos());
aoqi@0 1144 sim()->print();
aoqi@0 1145 tty->cr();
aoqi@0 1146 }
aoqi@0 1147 #endif
aoqi@0 1148
aoqi@0 1149 bool changed = false;
aoqi@0 1150 int number_of_sux = block->number_of_sux();
aoqi@0 1151
aoqi@0 1152 if (number_of_sux == 1 && block->sux_at(0)->number_of_preds() > 1) {
aoqi@0 1153 // The successor has at least two incoming edges, so a stack merge will be necessary
aoqi@0 1154 // If this block is the first predecessor, cleanup the current stack and propagate it
aoqi@0 1155 // If this block is not the first predecessor, a stack merge will be necessary
aoqi@0 1156
aoqi@0 1157 BlockBegin* sux = block->sux_at(0);
aoqi@0 1158 intArray* state = sux->fpu_stack_state();
aoqi@0 1159 LIR_List* instrs = new LIR_List(_compilation);
aoqi@0 1160
aoqi@0 1161 if (state != NULL) {
aoqi@0 1162 // Merge with a successors that already has a FPU stack state
aoqi@0 1163 // the block must only have one successor because critical edges must been split
aoqi@0 1164 FpuStackSim* cur_sim = sim();
aoqi@0 1165 FpuStackSim* sux_sim = temp_sim();
aoqi@0 1166 sux_sim->read_state(state);
aoqi@0 1167
aoqi@0 1168 merge_fpu_stack(instrs, cur_sim, sux_sim);
aoqi@0 1169
aoqi@0 1170 } else {
aoqi@0 1171 // propagate current FPU stack state to successor without state
aoqi@0 1172 // clean up stack first so that there are no dead values on the stack
aoqi@0 1173 if (ComputeExactFPURegisterUsage) {
aoqi@0 1174 FpuStackSim* cur_sim = sim();
aoqi@0 1175 BitMap live_fpu_regs = block->sux_at(0)->fpu_register_usage();
aoqi@0 1176 assert(live_fpu_regs.size() == FrameMap::nof_fpu_regs, "missing register usage");
aoqi@0 1177
aoqi@0 1178 merge_cleanup_fpu_stack(instrs, cur_sim, live_fpu_regs);
aoqi@0 1179 }
aoqi@0 1180
aoqi@0 1181 intArray* state = sim()->write_state();
aoqi@0 1182 if (TraceFPUStack) {
aoqi@0 1183 tty->print_cr("Setting FPU stack state of B%d (merge path)", sux->block_id());
aoqi@0 1184 sim()->print(); tty->cr();
aoqi@0 1185 }
aoqi@0 1186 sux->set_fpu_stack_state(state);
aoqi@0 1187 }
aoqi@0 1188
aoqi@0 1189 if (instrs->instructions_list()->length() > 0) {
aoqi@0 1190 lir()->insert_before(pos(), instrs);
aoqi@0 1191 set_pos(instrs->instructions_list()->length() + pos());
aoqi@0 1192 changed = true;
aoqi@0 1193 }
aoqi@0 1194
aoqi@0 1195 } else {
aoqi@0 1196 // Propagate unmodified Stack to successors where a stack merge is not necessary
aoqi@0 1197 intArray* state = sim()->write_state();
aoqi@0 1198 for (int i = 0; i < number_of_sux; i++) {
aoqi@0 1199 BlockBegin* sux = block->sux_at(i);
aoqi@0 1200
aoqi@0 1201 #ifdef ASSERT
aoqi@0 1202 for (int j = 0; j < sux->number_of_preds(); j++) {
aoqi@0 1203 assert(block == sux->pred_at(j), "all critical edges must be broken");
aoqi@0 1204 }
aoqi@0 1205
aoqi@0 1206 // check if new state is same
aoqi@0 1207 if (sux->fpu_stack_state() != NULL) {
aoqi@0 1208 intArray* sux_state = sux->fpu_stack_state();
aoqi@0 1209 assert(state->length() == sux_state->length(), "overwriting existing stack state");
aoqi@0 1210 for (int j = 0; j < state->length(); j++) {
aoqi@0 1211 assert(state->at(j) == sux_state->at(j), "overwriting existing stack state");
aoqi@0 1212 }
aoqi@0 1213 }
aoqi@0 1214 #endif
aoqi@0 1215 #ifndef PRODUCT
aoqi@0 1216 if (TraceFPUStack) {
aoqi@0 1217 tty->print_cr("Setting FPU stack state of B%d", sux->block_id());
aoqi@0 1218 sim()->print(); tty->cr();
aoqi@0 1219 }
aoqi@0 1220 #endif
aoqi@0 1221
aoqi@0 1222 sux->set_fpu_stack_state(state);
aoqi@0 1223 }
aoqi@0 1224 }
aoqi@0 1225
aoqi@0 1226 #ifndef PRODUCT
aoqi@0 1227 // assertions that FPU stack state conforms to all successors' states
aoqi@0 1228 intArray* cur_state = sim()->write_state();
aoqi@0 1229 for (int i = 0; i < number_of_sux; i++) {
aoqi@0 1230 BlockBegin* sux = block->sux_at(i);
aoqi@0 1231 intArray* sux_state = sux->fpu_stack_state();
aoqi@0 1232
aoqi@0 1233 assert(sux_state != NULL, "no fpu state");
aoqi@0 1234 assert(cur_state->length() == sux_state->length(), "incorrect length");
aoqi@0 1235 for (int i = 0; i < cur_state->length(); i++) {
aoqi@0 1236 assert(cur_state->at(i) == sux_state->at(i), "element not equal");
aoqi@0 1237 }
aoqi@0 1238 }
aoqi@0 1239 #endif
aoqi@0 1240
aoqi@0 1241 return changed;
aoqi@0 1242 }

mercurial