src/cpu/x86/vm/c1_LinearScan_x86.cpp

Fri, 26 Jun 2009 07:26:10 -0700

author
twisti
date
Fri, 26 Jun 2009 07:26:10 -0700
changeset 1259
18a08a7e16b5
parent 435
a61af66fc99e
child 1388
ff1a29907b6c
permissions
-rw-r--r--

5057225: Remove useless I2L conversions
Summary: The optimizer should be told to normalize (AndL (ConvI2L x) 0xFF) to (ConvI2L (AndI x 0xFF)), and then the existing matcher rule will work for free.
Reviewed-by: kvn

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

mercurial