1 /* 2 * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "c1/c1_Compilation.hpp" 27 #include "c1/c1_Defs.hpp" 28 #include "c1/c1_FrameMap.hpp" 29 #include "c1/c1_Instruction.hpp" 30 #include "c1/c1_LIRAssembler.hpp" 31 #include "c1/c1_LIRGenerator.hpp" 32 #include "c1/c1_ValueStack.hpp" 33 #include "ci/ciArrayKlass.hpp" 34 #include "ci/ciInstance.hpp" 35 #include "ci/ciObjArray.hpp" 36 #include "ci/ciUtilities.hpp" 37 #include "compiler/compilerDefinitions.inline.hpp" 38 #include "gc/shared/barrierSet.hpp" 39 #include "gc/shared/c1/barrierSetC1.hpp" 40 #include "oops/klass.inline.hpp" 41 #include "oops/methodCounters.hpp" 42 #include "runtime/sharedRuntime.hpp" 43 #include "runtime/stubRoutines.hpp" 44 #include "runtime/vm_version.hpp" 45 #include "utilities/bitMap.inline.hpp" 46 #include "utilities/macros.hpp" 47 #include "utilities/powerOfTwo.hpp" 48 49 #ifdef ASSERT 50 #define __ gen()->lir(__FILE__, __LINE__)-> 51 #else 52 #define __ gen()->lir()-> 53 #endif 54 55 #ifndef PATCHED_ADDR 56 #define PATCHED_ADDR (max_jint) 57 #endif 58 59 void PhiResolverState::reset() { 60 _virtual_operands.clear(); 61 _other_operands.clear(); 62 _vreg_table.clear(); 63 } 64 65 66 //-------------------------------------------------------------- 67 // PhiResolver 68 69 // Resolves cycles: 70 // 71 // r1 := r2 becomes temp := r1 72 // r2 := r1 r1 := r2 73 // r2 := temp 74 // and orders moves: 75 // 76 // r2 := r3 becomes r1 := r2 77 // r1 := r2 r2 := r3 78 79 PhiResolver::PhiResolver(LIRGenerator* gen) 80 : _gen(gen) 81 , _state(gen->resolver_state()) 82 , _loop(nullptr) 83 , _temp(LIR_OprFact::illegalOpr) 84 { 85 // reinitialize the shared state arrays 86 _state.reset(); 87 } 88 89 90 void PhiResolver::emit_move(LIR_Opr src, LIR_Opr dest) { 91 assert(src->is_valid(), ""); 92 assert(dest->is_valid(), ""); 93 __ move(src, dest); 94 } 95 96 97 void PhiResolver::move_temp_to(LIR_Opr dest) { 98 assert(_temp->is_valid(), ""); 99 emit_move(_temp, dest); 100 NOT_PRODUCT(_temp = LIR_OprFact::illegalOpr); 101 } 102 103 104 void PhiResolver::move_to_temp(LIR_Opr src) { 105 assert(_temp->is_illegal(), ""); 106 _temp = _gen->new_register(src->type()); 107 emit_move(src, _temp); 108 } 109 110 111 // Traverse assignment graph in depth first order and generate moves in post order 112 // ie. two assignments: b := c, a := b start with node c: 113 // Call graph: move(null, c) -> move(c, b) -> move(b, a) 114 // Generates moves in this order: move b to a and move c to b 115 // ie. cycle a := b, b := a start with node a 116 // Call graph: move(null, a) -> move(a, b) -> move(b, a) 117 // Generates moves in this order: move b to temp, move a to b, move temp to a 118 void PhiResolver::move(ResolveNode* src, ResolveNode* dest) { 119 if (!dest->visited()) { 120 dest->set_visited(); 121 for (int i = dest->no_of_destinations()-1; i >= 0; i --) { 122 move(dest, dest->destination_at(i)); 123 } 124 } else if (!dest->start_node()) { 125 // cylce in graph detected 126 assert(_loop == nullptr, "only one loop valid!"); 127 _loop = dest; 128 move_to_temp(src->operand()); 129 return; 130 } // else dest is a start node 131 132 if (!dest->assigned()) { 133 if (_loop == dest) { 134 move_temp_to(dest->operand()); 135 dest->set_assigned(); 136 } else if (src != nullptr) { 137 emit_move(src->operand(), dest->operand()); 138 dest->set_assigned(); 139 } 140 } 141 } 142 143 144 PhiResolver::~PhiResolver() { 145 int i; 146 // resolve any cycles in moves from and to virtual registers 147 for (i = virtual_operands().length() - 1; i >= 0; i --) { 148 ResolveNode* node = virtual_operands().at(i); 149 if (!node->visited()) { 150 _loop = nullptr; 151 move(nullptr, node); 152 node->set_start_node(); 153 assert(_temp->is_illegal(), "move_temp_to() call missing"); 154 } 155 } 156 157 // generate move for move from non virtual register to abitrary destination 158 for (i = other_operands().length() - 1; i >= 0; i --) { 159 ResolveNode* node = other_operands().at(i); 160 for (int j = node->no_of_destinations() - 1; j >= 0; j --) { 161 emit_move(node->operand(), node->destination_at(j)->operand()); 162 } 163 } 164 } 165 166 167 ResolveNode* PhiResolver::create_node(LIR_Opr opr, bool source) { 168 ResolveNode* node; 169 if (opr->is_virtual()) { 170 int vreg_num = opr->vreg_number(); 171 node = vreg_table().at_grow(vreg_num, nullptr); 172 assert(node == nullptr || node->operand() == opr, ""); 173 if (node == nullptr) { 174 node = new ResolveNode(opr); 175 vreg_table().at_put(vreg_num, node); 176 } 177 // Make sure that all virtual operands show up in the list when 178 // they are used as the source of a move. 179 if (source && !virtual_operands().contains(node)) { 180 virtual_operands().append(node); 181 } 182 } else { 183 assert(source, ""); 184 node = new ResolveNode(opr); 185 other_operands().append(node); 186 } 187 return node; 188 } 189 190 191 void PhiResolver::move(LIR_Opr src, LIR_Opr dest) { 192 assert(dest->is_virtual(), ""); 193 // tty->print("move "); src->print(); tty->print(" to "); dest->print(); tty->cr(); 194 assert(src->is_valid(), ""); 195 assert(dest->is_valid(), ""); 196 ResolveNode* source = source_node(src); 197 source->append(destination_node(dest)); 198 } 199 200 201 //-------------------------------------------------------------- 202 // LIRItem 203 204 void LIRItem::set_result(LIR_Opr opr) { 205 assert(value()->operand()->is_illegal() || value()->operand()->is_constant(), "operand should never change"); 206 value()->set_operand(opr); 207 208 if (opr->is_virtual()) { 209 _gen->_instruction_for_operand.at_put_grow(opr->vreg_number(), value(), nullptr); 210 } 211 212 _result = opr; 213 } 214 215 void LIRItem::load_item() { 216 if (result()->is_illegal()) { 217 // update the items result 218 _result = value()->operand(); 219 } 220 if (!result()->is_register()) { 221 LIR_Opr reg = _gen->new_register(value()->type()); 222 __ move(result(), reg); 223 if (result()->is_constant()) { 224 _result = reg; 225 } else { 226 set_result(reg); 227 } 228 } 229 } 230 231 232 void LIRItem::load_for_store(BasicType type) { 233 if (_gen->can_store_as_constant(value(), type)) { 234 _result = value()->operand(); 235 if (!_result->is_constant()) { 236 _result = LIR_OprFact::value_type(value()->type()); 237 } 238 } else if (type == T_BYTE || type == T_BOOLEAN) { 239 load_byte_item(); 240 } else { 241 load_item(); 242 } 243 } 244 245 void LIRItem::load_item_force(LIR_Opr reg) { 246 LIR_Opr r = result(); 247 if (r != reg) { 248 #if !defined(ARM) && !defined(E500V2) 249 if (r->type() != reg->type()) { 250 // moves between different types need an intervening spill slot 251 r = _gen->force_to_spill(r, reg->type()); 252 } 253 #endif 254 __ move(r, reg); 255 _result = reg; 256 } 257 } 258 259 ciObject* LIRItem::get_jobject_constant() const { 260 ObjectType* oc = type()->as_ObjectType(); 261 if (oc) { 262 return oc->constant_value(); 263 } 264 return nullptr; 265 } 266 267 268 jint LIRItem::get_jint_constant() const { 269 assert(is_constant() && value() != nullptr, ""); 270 assert(type()->as_IntConstant() != nullptr, "type check"); 271 return type()->as_IntConstant()->value(); 272 } 273 274 275 jint LIRItem::get_address_constant() const { 276 assert(is_constant() && value() != nullptr, ""); 277 assert(type()->as_AddressConstant() != nullptr, "type check"); 278 return type()->as_AddressConstant()->value(); 279 } 280 281 282 jfloat LIRItem::get_jfloat_constant() const { 283 assert(is_constant() && value() != nullptr, ""); 284 assert(type()->as_FloatConstant() != nullptr, "type check"); 285 return type()->as_FloatConstant()->value(); 286 } 287 288 289 jdouble LIRItem::get_jdouble_constant() const { 290 assert(is_constant() && value() != nullptr, ""); 291 assert(type()->as_DoubleConstant() != nullptr, "type check"); 292 return type()->as_DoubleConstant()->value(); 293 } 294 295 296 jlong LIRItem::get_jlong_constant() const { 297 assert(is_constant() && value() != nullptr, ""); 298 assert(type()->as_LongConstant() != nullptr, "type check"); 299 return type()->as_LongConstant()->value(); 300 } 301 302 303 304 //-------------------------------------------------------------- 305 306 307 void LIRGenerator::block_do_prolog(BlockBegin* block) { 308 #ifndef PRODUCT 309 if (PrintIRWithLIR) { 310 block->print(); 311 } 312 #endif 313 314 // set up the list of LIR instructions 315 assert(block->lir() == nullptr, "LIR list already computed for this block"); 316 _lir = new LIR_List(compilation(), block); 317 block->set_lir(_lir); 318 319 __ branch_destination(block->label()); 320 321 if (LIRTraceExecution && 322 Compilation::current()->hir()->start()->block_id() != block->block_id() && 323 !block->is_set(BlockBegin::exception_entry_flag)) { 324 assert(block->lir()->instructions_list()->length() == 1, "should come right after br_dst"); 325 trace_block_entry(block); 326 } 327 } 328 329 330 void LIRGenerator::block_do_epilog(BlockBegin* block) { 331 #ifndef PRODUCT 332 if (PrintIRWithLIR) { 333 tty->cr(); 334 } 335 #endif 336 337 // LIR_Opr for unpinned constants shouldn't be referenced by other 338 // blocks so clear them out after processing the block. 339 for (int i = 0; i < _unpinned_constants.length(); i++) { 340 _unpinned_constants.at(i)->clear_operand(); 341 } 342 _unpinned_constants.trunc_to(0); 343 344 // clear our any registers for other local constants 345 _constants.trunc_to(0); 346 _reg_for_constants.trunc_to(0); 347 } 348 349 350 void LIRGenerator::block_do(BlockBegin* block) { 351 CHECK_BAILOUT(); 352 353 block_do_prolog(block); 354 set_block(block); 355 356 for (Instruction* instr = block; instr != nullptr; instr = instr->next()) { 357 if (instr->is_pinned()) do_root(instr); 358 } 359 360 set_block(nullptr); 361 block_do_epilog(block); 362 } 363 364 365 //-------------------------LIRGenerator----------------------------- 366 367 // This is where the tree-walk starts; instr must be root; 368 void LIRGenerator::do_root(Value instr) { 369 CHECK_BAILOUT(); 370 371 InstructionMark im(compilation(), instr); 372 373 assert(instr->is_pinned(), "use only with roots"); 374 assert(instr->subst() == instr, "shouldn't have missed substitution"); 375 376 instr->visit(this); 377 378 assert(!instr->has_uses() || instr->operand()->is_valid() || 379 instr->as_Constant() != nullptr || bailed_out(), "invalid item set"); 380 } 381 382 383 // This is called for each node in tree; the walk stops if a root is reached 384 void LIRGenerator::walk(Value instr) { 385 InstructionMark im(compilation(), instr); 386 //stop walk when encounter a root 387 if ((instr->is_pinned() && instr->as_Phi() == nullptr) || instr->operand()->is_valid()) { 388 assert(instr->operand() != LIR_OprFact::illegalOpr || instr->as_Constant() != nullptr, "this root has not yet been visited"); 389 } else { 390 assert(instr->subst() == instr, "shouldn't have missed substitution"); 391 instr->visit(this); 392 // assert(instr->use_count() > 0 || instr->as_Phi() != nullptr, "leaf instruction must have a use"); 393 } 394 } 395 396 397 CodeEmitInfo* LIRGenerator::state_for(Instruction* x, ValueStack* state, bool ignore_xhandler) { 398 assert(state != nullptr, "state must be defined"); 399 400 #ifndef PRODUCT 401 state->verify(); 402 #endif 403 404 ValueStack* s = state; 405 for_each_state(s) { 406 if (s->kind() == ValueStack::EmptyExceptionState) { 407 assert(s->stack_size() == 0 && s->locals_size() == 0 && (s->locks_size() == 0 || s->locks_size() == 1), "state must be empty"); 408 continue; 409 } 410 411 int index; 412 Value value; 413 for_each_stack_value(s, index, value) { 414 assert(value->subst() == value, "missed substitution"); 415 if (!value->is_pinned() && value->as_Constant() == nullptr && value->as_Local() == nullptr) { 416 walk(value); 417 assert(value->operand()->is_valid(), "must be evaluated now"); 418 } 419 } 420 421 int bci = s->bci(); 422 IRScope* scope = s->scope(); 423 ciMethod* method = scope->method(); 424 425 MethodLivenessResult liveness = method->liveness_at_bci(bci); 426 if (bci == SynchronizationEntryBCI) { 427 if (x->as_ExceptionObject() || x->as_Throw()) { 428 // all locals are dead on exit from the synthetic unlocker 429 liveness.clear(); 430 } else { 431 assert(x->as_MonitorEnter() || x->as_ProfileInvoke(), "only other cases are MonitorEnter and ProfileInvoke"); 432 } 433 } 434 if (!liveness.is_valid()) { 435 // Degenerate or breakpointed method. 436 bailout("Degenerate or breakpointed method"); 437 } else { 438 assert((int)liveness.size() == s->locals_size(), "error in use of liveness"); 439 for_each_local_value(s, index, value) { 440 assert(value->subst() == value, "missed substitution"); 441 if (liveness.at(index) && !value->type()->is_illegal()) { 442 if (!value->is_pinned() && value->as_Constant() == nullptr && value->as_Local() == nullptr) { 443 walk(value); 444 assert(value->operand()->is_valid(), "must be evaluated now"); 445 } 446 } else { 447 // null out this local so that linear scan can assume that all non-null values are live. 448 s->invalidate_local(index); 449 } 450 } 451 } 452 } 453 454 return new CodeEmitInfo(state, ignore_xhandler ? nullptr : x->exception_handlers(), x->check_flag(Instruction::DeoptimizeOnException)); 455 } 456 457 458 CodeEmitInfo* LIRGenerator::state_for(Instruction* x) { 459 return state_for(x, x->exception_state()); 460 } 461 462 463 void LIRGenerator::klass2reg_with_patching(LIR_Opr r, ciMetadata* obj, CodeEmitInfo* info, bool need_resolve) { 464 /* C2 relies on constant pool entries being resolved (ciTypeFlow), so if tiered compilation 465 * is active and the class hasn't yet been resolved we need to emit a patch that resolves 466 * the class. */ 467 if ((!CompilerConfig::is_c1_only_no_jvmci() && need_resolve) || !obj->is_loaded() || PatchALot) { 468 assert(info != nullptr, "info must be set if class is not loaded"); 469 __ klass2reg_patch(nullptr, r, info); 470 } else { 471 // no patching needed 472 __ metadata2reg(obj->constant_encoding(), r); 473 } 474 } 475 476 477 void LIRGenerator::array_range_check(LIR_Opr array, LIR_Opr index, 478 CodeEmitInfo* null_check_info, CodeEmitInfo* range_check_info) { 479 CodeStub* stub = new RangeCheckStub(range_check_info, index, array); 480 if (index->is_constant()) { 481 cmp_mem_int(lir_cond_belowEqual, array, arrayOopDesc::length_offset_in_bytes(), 482 index->as_jint(), null_check_info); 483 __ branch(lir_cond_belowEqual, stub); // forward branch 484 } else { 485 cmp_reg_mem(lir_cond_aboveEqual, index, array, 486 arrayOopDesc::length_offset_in_bytes(), T_INT, null_check_info); 487 __ branch(lir_cond_aboveEqual, stub); // forward branch 488 } 489 } 490 491 void LIRGenerator::arithmetic_op(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp_op, CodeEmitInfo* info) { 492 LIR_Opr result_op = result; 493 LIR_Opr left_op = left; 494 LIR_Opr right_op = right; 495 496 if (two_operand_lir_form && left_op != result_op) { 497 assert(right_op != result_op, "malformed"); 498 __ move(left_op, result_op); 499 left_op = result_op; 500 } 501 502 switch(code) { 503 case Bytecodes::_dadd: 504 case Bytecodes::_fadd: 505 case Bytecodes::_ladd: 506 case Bytecodes::_iadd: __ add(left_op, right_op, result_op); break; 507 case Bytecodes::_fmul: 508 case Bytecodes::_lmul: __ mul(left_op, right_op, result_op); break; 509 510 case Bytecodes::_dmul: __ mul(left_op, right_op, result_op, tmp_op); break; 511 512 case Bytecodes::_imul: 513 { 514 bool did_strength_reduce = false; 515 516 if (right->is_constant()) { 517 jint c = right->as_jint(); 518 if (c > 0 && is_power_of_2(c)) { 519 // do not need tmp here 520 __ shift_left(left_op, exact_log2(c), result_op); 521 did_strength_reduce = true; 522 } else { 523 did_strength_reduce = strength_reduce_multiply(left_op, c, result_op, tmp_op); 524 } 525 } 526 // we couldn't strength reduce so just emit the multiply 527 if (!did_strength_reduce) { 528 __ mul(left_op, right_op, result_op); 529 } 530 } 531 break; 532 533 case Bytecodes::_dsub: 534 case Bytecodes::_fsub: 535 case Bytecodes::_lsub: 536 case Bytecodes::_isub: __ sub(left_op, right_op, result_op); break; 537 538 case Bytecodes::_fdiv: __ div (left_op, right_op, result_op); break; 539 // ldiv and lrem are implemented with a direct runtime call 540 541 case Bytecodes::_ddiv: __ div(left_op, right_op, result_op, tmp_op); break; 542 543 case Bytecodes::_drem: 544 case Bytecodes::_frem: __ rem (left_op, right_op, result_op); break; 545 546 default: ShouldNotReachHere(); 547 } 548 } 549 550 551 void LIRGenerator::arithmetic_op_int(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) { 552 arithmetic_op(code, result, left, right, tmp); 553 } 554 555 556 void LIRGenerator::arithmetic_op_long(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) { 557 arithmetic_op(code, result, left, right, LIR_OprFact::illegalOpr, info); 558 } 559 560 561 void LIRGenerator::arithmetic_op_fpu(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) { 562 arithmetic_op(code, result, left, right, tmp); 563 } 564 565 566 void LIRGenerator::shift_op(Bytecodes::Code code, LIR_Opr result_op, LIR_Opr value, LIR_Opr count, LIR_Opr tmp) { 567 568 if (two_operand_lir_form && value != result_op 569 // Only 32bit right shifts require two operand form on S390. 570 S390_ONLY(&& (code == Bytecodes::_ishr || code == Bytecodes::_iushr))) { 571 assert(count != result_op, "malformed"); 572 __ move(value, result_op); 573 value = result_op; 574 } 575 576 assert(count->is_constant() || count->is_register(), "must be"); 577 switch(code) { 578 case Bytecodes::_ishl: 579 case Bytecodes::_lshl: __ shift_left(value, count, result_op, tmp); break; 580 case Bytecodes::_ishr: 581 case Bytecodes::_lshr: __ shift_right(value, count, result_op, tmp); break; 582 case Bytecodes::_iushr: 583 case Bytecodes::_lushr: __ unsigned_shift_right(value, count, result_op, tmp); break; 584 default: ShouldNotReachHere(); 585 } 586 } 587 588 589 void LIRGenerator::logic_op (Bytecodes::Code code, LIR_Opr result_op, LIR_Opr left_op, LIR_Opr right_op) { 590 if (two_operand_lir_form && left_op != result_op) { 591 assert(right_op != result_op, "malformed"); 592 __ move(left_op, result_op); 593 left_op = result_op; 594 } 595 596 switch(code) { 597 case Bytecodes::_iand: 598 case Bytecodes::_land: __ logical_and(left_op, right_op, result_op); break; 599 600 case Bytecodes::_ior: 601 case Bytecodes::_lor: __ logical_or(left_op, right_op, result_op); break; 602 603 case Bytecodes::_ixor: 604 case Bytecodes::_lxor: __ logical_xor(left_op, right_op, result_op); break; 605 606 default: ShouldNotReachHere(); 607 } 608 } 609 610 611 void LIRGenerator::monitor_enter(LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no, CodeEmitInfo* info_for_exception, CodeEmitInfo* info) { 612 if (!GenerateSynchronizationCode) return; 613 // for slow path, use debug info for state after successful locking 614 CodeStub* slow_path = new MonitorEnterStub(object, lock, info); 615 __ load_stack_address_monitor(monitor_no, lock); 616 // for handling NullPointerException, use debug info representing just the lock stack before this monitorenter 617 __ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception); 618 } 619 620 621 void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) { 622 if (!GenerateSynchronizationCode) return; 623 // setup registers 624 LIR_Opr hdr = lock; 625 lock = new_hdr; 626 CodeStub* slow_path = new MonitorExitStub(lock, LockingMode != LM_MONITOR, monitor_no); 627 __ load_stack_address_monitor(monitor_no, lock); 628 __ unlock_object(hdr, object, lock, scratch, slow_path); 629 } 630 631 #ifndef PRODUCT 632 void LIRGenerator::print_if_not_loaded(const NewInstance* new_instance) { 633 if (PrintNotLoaded && !new_instance->klass()->is_loaded()) { 634 tty->print_cr(" ###class not loaded at new bci %d", new_instance->printable_bci()); 635 } else if (PrintNotLoaded && (!CompilerConfig::is_c1_only_no_jvmci() && new_instance->is_unresolved())) { 636 tty->print_cr(" ###class not resolved at new bci %d", new_instance->printable_bci()); 637 } 638 } 639 #endif 640 641 void LIRGenerator::new_instance(LIR_Opr dst, ciInstanceKlass* klass, bool is_unresolved, LIR_Opr scratch1, LIR_Opr scratch2, LIR_Opr scratch3, LIR_Opr scratch4, LIR_Opr klass_reg, CodeEmitInfo* info) { 642 klass2reg_with_patching(klass_reg, klass, info, is_unresolved); 643 // If klass is not loaded we do not know if the klass has finalizers: 644 if (UseFastNewInstance && klass->is_loaded() 645 && !Klass::layout_helper_needs_slow_path(klass->layout_helper())) { 646 647 Runtime1::StubID stub_id = klass->is_initialized() ? Runtime1::fast_new_instance_id : Runtime1::fast_new_instance_init_check_id; 648 649 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id); 650 651 assert(klass->is_loaded(), "must be loaded"); 652 // allocate space for instance 653 assert(klass->size_helper() > 0, "illegal instance size"); 654 const int instance_size = align_object_size(klass->size_helper()); 655 __ allocate_object(dst, scratch1, scratch2, scratch3, scratch4, 656 oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path); 657 } else { 658 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, Runtime1::new_instance_id); 659 __ branch(lir_cond_always, slow_path); 660 __ branch_destination(slow_path->continuation()); 661 } 662 } 663 664 665 static bool is_constant_zero(Instruction* inst) { 666 IntConstant* c = inst->type()->as_IntConstant(); 667 if (c) { 668 return (c->value() == 0); 669 } 670 return false; 671 } 672 673 674 static bool positive_constant(Instruction* inst) { 675 IntConstant* c = inst->type()->as_IntConstant(); 676 if (c) { 677 return (c->value() >= 0); 678 } 679 return false; 680 } 681 682 683 static ciArrayKlass* as_array_klass(ciType* type) { 684 if (type != nullptr && type->is_array_klass() && type->is_loaded()) { 685 return (ciArrayKlass*)type; 686 } else { 687 return nullptr; 688 } 689 } 690 691 static ciType* phi_declared_type(Phi* phi) { 692 ciType* t = phi->operand_at(0)->declared_type(); 693 if (t == nullptr) { 694 return nullptr; 695 } 696 for(int i = 1; i < phi->operand_count(); i++) { 697 if (t != phi->operand_at(i)->declared_type()) { 698 return nullptr; 699 } 700 } 701 return t; 702 } 703 704 void LIRGenerator::arraycopy_helper(Intrinsic* x, int* flagsp, ciArrayKlass** expected_typep) { 705 Instruction* src = x->argument_at(0); 706 Instruction* src_pos = x->argument_at(1); 707 Instruction* dst = x->argument_at(2); 708 Instruction* dst_pos = x->argument_at(3); 709 Instruction* length = x->argument_at(4); 710 711 // first try to identify the likely type of the arrays involved 712 ciArrayKlass* expected_type = nullptr; 713 bool is_exact = false, src_objarray = false, dst_objarray = false; 714 { 715 ciArrayKlass* src_exact_type = as_array_klass(src->exact_type()); 716 ciArrayKlass* src_declared_type = as_array_klass(src->declared_type()); 717 Phi* phi; 718 if (src_declared_type == nullptr && (phi = src->as_Phi()) != nullptr) { 719 src_declared_type = as_array_klass(phi_declared_type(phi)); 720 } 721 ciArrayKlass* dst_exact_type = as_array_klass(dst->exact_type()); 722 ciArrayKlass* dst_declared_type = as_array_klass(dst->declared_type()); 723 if (dst_declared_type == nullptr && (phi = dst->as_Phi()) != nullptr) { 724 dst_declared_type = as_array_klass(phi_declared_type(phi)); 725 } 726 727 if (src_exact_type != nullptr && src_exact_type == dst_exact_type) { 728 // the types exactly match so the type is fully known 729 is_exact = true; 730 expected_type = src_exact_type; 731 } else if (dst_exact_type != nullptr && dst_exact_type->is_obj_array_klass()) { 732 ciArrayKlass* dst_type = (ciArrayKlass*) dst_exact_type; 733 ciArrayKlass* src_type = nullptr; 734 if (src_exact_type != nullptr && src_exact_type->is_obj_array_klass()) { 735 src_type = (ciArrayKlass*) src_exact_type; 736 } else if (src_declared_type != nullptr && src_declared_type->is_obj_array_klass()) { 737 src_type = (ciArrayKlass*) src_declared_type; 738 } 739 if (src_type != nullptr) { 740 if (src_type->element_type()->is_subtype_of(dst_type->element_type())) { 741 is_exact = true; 742 expected_type = dst_type; 743 } 744 } 745 } 746 // at least pass along a good guess 747 if (expected_type == nullptr) expected_type = dst_exact_type; 748 if (expected_type == nullptr) expected_type = src_declared_type; 749 if (expected_type == nullptr) expected_type = dst_declared_type; 750 751 src_objarray = (src_exact_type && src_exact_type->is_obj_array_klass()) || (src_declared_type && src_declared_type->is_obj_array_klass()); 752 dst_objarray = (dst_exact_type && dst_exact_type->is_obj_array_klass()) || (dst_declared_type && dst_declared_type->is_obj_array_klass()); 753 } 754 755 // if a probable array type has been identified, figure out if any 756 // of the required checks for a fast case can be elided. 757 int flags = LIR_OpArrayCopy::all_flags; 758 759 if (!src_objarray) 760 flags &= ~LIR_OpArrayCopy::src_objarray; 761 if (!dst_objarray) 762 flags &= ~LIR_OpArrayCopy::dst_objarray; 763 764 if (!x->arg_needs_null_check(0)) 765 flags &= ~LIR_OpArrayCopy::src_null_check; 766 if (!x->arg_needs_null_check(2)) 767 flags &= ~LIR_OpArrayCopy::dst_null_check; 768 769 770 if (expected_type != nullptr) { 771 Value length_limit = nullptr; 772 773 IfOp* ifop = length->as_IfOp(); 774 if (ifop != nullptr) { 775 // look for expressions like min(v, a.length) which ends up as 776 // x > y ? y : x or x >= y ? y : x 777 if ((ifop->cond() == If::gtr || ifop->cond() == If::geq) && 778 ifop->x() == ifop->fval() && 779 ifop->y() == ifop->tval()) { 780 length_limit = ifop->y(); 781 } 782 } 783 784 // try to skip null checks and range checks 785 NewArray* src_array = src->as_NewArray(); 786 if (src_array != nullptr) { 787 flags &= ~LIR_OpArrayCopy::src_null_check; 788 if (length_limit != nullptr && 789 src_array->length() == length_limit && 790 is_constant_zero(src_pos)) { 791 flags &= ~LIR_OpArrayCopy::src_range_check; 792 } 793 } 794 795 NewArray* dst_array = dst->as_NewArray(); 796 if (dst_array != nullptr) { 797 flags &= ~LIR_OpArrayCopy::dst_null_check; 798 if (length_limit != nullptr && 799 dst_array->length() == length_limit && 800 is_constant_zero(dst_pos)) { 801 flags &= ~LIR_OpArrayCopy::dst_range_check; 802 } 803 } 804 805 // check from incoming constant values 806 if (positive_constant(src_pos)) 807 flags &= ~LIR_OpArrayCopy::src_pos_positive_check; 808 if (positive_constant(dst_pos)) 809 flags &= ~LIR_OpArrayCopy::dst_pos_positive_check; 810 if (positive_constant(length)) 811 flags &= ~LIR_OpArrayCopy::length_positive_check; 812 813 // see if the range check can be elided, which might also imply 814 // that src or dst is non-null. 815 ArrayLength* al = length->as_ArrayLength(); 816 if (al != nullptr) { 817 if (al->array() == src) { 818 // it's the length of the source array 819 flags &= ~LIR_OpArrayCopy::length_positive_check; 820 flags &= ~LIR_OpArrayCopy::src_null_check; 821 if (is_constant_zero(src_pos)) 822 flags &= ~LIR_OpArrayCopy::src_range_check; 823 } 824 if (al->array() == dst) { 825 // it's the length of the destination array 826 flags &= ~LIR_OpArrayCopy::length_positive_check; 827 flags &= ~LIR_OpArrayCopy::dst_null_check; 828 if (is_constant_zero(dst_pos)) 829 flags &= ~LIR_OpArrayCopy::dst_range_check; 830 } 831 } 832 if (is_exact) { 833 flags &= ~LIR_OpArrayCopy::type_check; 834 } 835 } 836 837 IntConstant* src_int = src_pos->type()->as_IntConstant(); 838 IntConstant* dst_int = dst_pos->type()->as_IntConstant(); 839 if (src_int && dst_int) { 840 int s_offs = src_int->value(); 841 int d_offs = dst_int->value(); 842 if (src_int->value() >= dst_int->value()) { 843 flags &= ~LIR_OpArrayCopy::overlapping; 844 } 845 if (expected_type != nullptr) { 846 BasicType t = expected_type->element_type()->basic_type(); 847 int element_size = type2aelembytes(t); 848 if (((arrayOopDesc::base_offset_in_bytes(t) + (uint)s_offs * element_size) % HeapWordSize == 0) && 849 ((arrayOopDesc::base_offset_in_bytes(t) + (uint)d_offs * element_size) % HeapWordSize == 0)) { 850 flags &= ~LIR_OpArrayCopy::unaligned; 851 } 852 } 853 } else if (src_pos == dst_pos || is_constant_zero(dst_pos)) { 854 // src and dest positions are the same, or dst is zero so assume 855 // nonoverlapping copy. 856 flags &= ~LIR_OpArrayCopy::overlapping; 857 } 858 859 if (src == dst) { 860 // moving within a single array so no type checks are needed 861 if (flags & LIR_OpArrayCopy::type_check) { 862 flags &= ~LIR_OpArrayCopy::type_check; 863 } 864 } 865 *flagsp = flags; 866 *expected_typep = (ciArrayKlass*)expected_type; 867 } 868 869 870 LIR_Opr LIRGenerator::round_item(LIR_Opr opr) { 871 assert(opr->is_register(), "why spill if item is not register?"); 872 873 if (strict_fp_requires_explicit_rounding) { 874 #ifdef IA32 875 if (UseSSE < 1 && opr->is_single_fpu()) { 876 LIR_Opr result = new_register(T_FLOAT); 877 set_vreg_flag(result, must_start_in_memory); 878 assert(opr->is_register(), "only a register can be spilled"); 879 assert(opr->value_type()->is_float(), "rounding only for floats available"); 880 __ roundfp(opr, LIR_OprFact::illegalOpr, result); 881 return result; 882 } 883 #else 884 Unimplemented(); 885 #endif // IA32 886 } 887 return opr; 888 } 889 890 891 LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) { 892 assert(type2size[t] == type2size[value->type()], 893 "size mismatch: t=%s, value->type()=%s", type2name(t), type2name(value->type())); 894 if (!value->is_register()) { 895 // force into a register 896 LIR_Opr r = new_register(value->type()); 897 __ move(value, r); 898 value = r; 899 } 900 901 // create a spill location 902 LIR_Opr tmp = new_register(t); 903 set_vreg_flag(tmp, LIRGenerator::must_start_in_memory); 904 905 // move from register to spill 906 __ move(value, tmp); 907 return tmp; 908 } 909 910 void LIRGenerator::profile_branch(If* if_instr, If::Condition cond) { 911 if (if_instr->should_profile()) { 912 ciMethod* method = if_instr->profiled_method(); 913 assert(method != nullptr, "method should be set if branch is profiled"); 914 ciMethodData* md = method->method_data_or_null(); 915 assert(md != nullptr, "Sanity"); 916 ciProfileData* data = md->bci_to_data(if_instr->profiled_bci()); 917 assert(data != nullptr, "must have profiling data"); 918 assert(data->is_BranchData(), "need BranchData for two-way branches"); 919 int taken_count_offset = md->byte_offset_of_slot(data, BranchData::taken_offset()); 920 int not_taken_count_offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset()); 921 if (if_instr->is_swapped()) { 922 int t = taken_count_offset; 923 taken_count_offset = not_taken_count_offset; 924 not_taken_count_offset = t; 925 } 926 927 LIR_Opr md_reg = new_register(T_METADATA); 928 __ metadata2reg(md->constant_encoding(), md_reg); 929 930 LIR_Opr data_offset_reg = new_pointer_register(); 931 __ cmove(lir_cond(cond), 932 LIR_OprFact::intptrConst(taken_count_offset), 933 LIR_OprFact::intptrConst(not_taken_count_offset), 934 data_offset_reg, as_BasicType(if_instr->x()->type())); 935 936 // MDO cells are intptr_t, so the data_reg width is arch-dependent. 937 LIR_Opr data_reg = new_pointer_register(); 938 LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type()); 939 __ move(data_addr, data_reg); 940 // Use leal instead of add to avoid destroying condition codes on x86 941 LIR_Address* fake_incr_value = new LIR_Address(data_reg, DataLayout::counter_increment, T_INT); 942 __ leal(LIR_OprFact::address(fake_incr_value), data_reg); 943 __ move(data_reg, data_addr); 944 } 945 } 946 947 // Phi technique: 948 // This is about passing live values from one basic block to the other. 949 // In code generated with Java it is rather rare that more than one 950 // value is on the stack from one basic block to the other. 951 // We optimize our technique for efficient passing of one value 952 // (of type long, int, double..) but it can be extended. 953 // When entering or leaving a basic block, all registers and all spill 954 // slots are release and empty. We use the released registers 955 // and spill slots to pass the live values from one block 956 // to the other. The topmost value, i.e., the value on TOS of expression 957 // stack is passed in registers. All other values are stored in spilling 958 // area. Every Phi has an index which designates its spill slot 959 // At exit of a basic block, we fill the register(s) and spill slots. 960 // At entry of a basic block, the block_prolog sets up the content of phi nodes 961 // and locks necessary registers and spilling slots. 962 963 964 // move current value to referenced phi function 965 void LIRGenerator::move_to_phi(PhiResolver* resolver, Value cur_val, Value sux_val) { 966 Phi* phi = sux_val->as_Phi(); 967 // cur_val can be null without phi being null in conjunction with inlining 968 if (phi != nullptr && cur_val != nullptr && cur_val != phi && !phi->is_illegal()) { 969 if (phi->is_local()) { 970 for (int i = 0; i < phi->operand_count(); i++) { 971 Value op = phi->operand_at(i); 972 if (op != nullptr && op->type()->is_illegal()) { 973 bailout("illegal phi operand"); 974 } 975 } 976 } 977 Phi* cur_phi = cur_val->as_Phi(); 978 if (cur_phi != nullptr && cur_phi->is_illegal()) { 979 // Phi and local would need to get invalidated 980 // (which is unexpected for Linear Scan). 981 // But this case is very rare so we simply bail out. 982 bailout("propagation of illegal phi"); 983 return; 984 } 985 LIR_Opr operand = cur_val->operand(); 986 if (operand->is_illegal()) { 987 assert(cur_val->as_Constant() != nullptr || cur_val->as_Local() != nullptr, 988 "these can be produced lazily"); 989 operand = operand_for_instruction(cur_val); 990 } 991 resolver->move(operand, operand_for_instruction(phi)); 992 } 993 } 994 995 996 // Moves all stack values into their PHI position 997 void LIRGenerator::move_to_phi(ValueStack* cur_state) { 998 BlockBegin* bb = block(); 999 if (bb->number_of_sux() == 1) { 1000 BlockBegin* sux = bb->sux_at(0); 1001 assert(sux->number_of_preds() > 0, "invalid CFG"); 1002 1003 // a block with only one predecessor never has phi functions 1004 if (sux->number_of_preds() > 1) { 1005 PhiResolver resolver(this); 1006 1007 ValueStack* sux_state = sux->state(); 1008 Value sux_value; 1009 int index; 1010 1011 assert(cur_state->scope() == sux_state->scope(), "not matching"); 1012 assert(cur_state->locals_size() == sux_state->locals_size(), "not matching"); 1013 assert(cur_state->stack_size() == sux_state->stack_size(), "not matching"); 1014 1015 for_each_stack_value(sux_state, index, sux_value) { 1016 move_to_phi(&resolver, cur_state->stack_at(index), sux_value); 1017 } 1018 1019 for_each_local_value(sux_state, index, sux_value) { 1020 move_to_phi(&resolver, cur_state->local_at(index), sux_value); 1021 } 1022 1023 assert(cur_state->caller_state() == sux_state->caller_state(), "caller states must be equal"); 1024 } 1025 } 1026 } 1027 1028 1029 LIR_Opr LIRGenerator::new_register(BasicType type) { 1030 int vreg_num = _virtual_register_number; 1031 // Add a little fudge factor for the bailout since the bailout is only checked periodically. This allows us to hand out 1032 // a few extra registers before we really run out which helps to avoid to trip over assertions. 1033 if (vreg_num + 20 >= LIR_Opr::vreg_max) { 1034 bailout("out of virtual registers in LIR generator"); 1035 if (vreg_num + 2 >= LIR_Opr::vreg_max) { 1036 // Wrap it around and continue until bailout really happens to avoid hitting assertions. 1037 _virtual_register_number = LIR_Opr::vreg_base; 1038 vreg_num = LIR_Opr::vreg_base; 1039 } 1040 } 1041 _virtual_register_number += 1; 1042 LIR_Opr vreg = LIR_OprFact::virtual_register(vreg_num, type); 1043 assert(vreg != LIR_OprFact::illegal(), "ran out of virtual registers"); 1044 return vreg; 1045 } 1046 1047 1048 // Try to lock using register in hint 1049 LIR_Opr LIRGenerator::rlock(Value instr) { 1050 return new_register(instr->type()); 1051 } 1052 1053 1054 // does an rlock and sets result 1055 LIR_Opr LIRGenerator::rlock_result(Value x) { 1056 LIR_Opr reg = rlock(x); 1057 set_result(x, reg); 1058 return reg; 1059 } 1060 1061 1062 // does an rlock and sets result 1063 LIR_Opr LIRGenerator::rlock_result(Value x, BasicType type) { 1064 LIR_Opr reg; 1065 switch (type) { 1066 case T_BYTE: 1067 case T_BOOLEAN: 1068 reg = rlock_byte(type); 1069 break; 1070 default: 1071 reg = rlock(x); 1072 break; 1073 } 1074 1075 set_result(x, reg); 1076 return reg; 1077 } 1078 1079 1080 //--------------------------------------------------------------------- 1081 ciObject* LIRGenerator::get_jobject_constant(Value value) { 1082 ObjectType* oc = value->type()->as_ObjectType(); 1083 if (oc) { 1084 return oc->constant_value(); 1085 } 1086 return nullptr; 1087 } 1088 1089 1090 void LIRGenerator::do_ExceptionObject(ExceptionObject* x) { 1091 assert(block()->is_set(BlockBegin::exception_entry_flag), "ExceptionObject only allowed in exception handler block"); 1092 assert(block()->next() == x, "ExceptionObject must be first instruction of block"); 1093 1094 // no moves are created for phi functions at the begin of exception 1095 // handlers, so assign operands manually here 1096 for_each_phi_fun(block(), phi, 1097 if (!phi->is_illegal()) { operand_for_instruction(phi); }); 1098 1099 LIR_Opr thread_reg = getThreadPointer(); 1100 __ move_wide(new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT), 1101 exceptionOopOpr()); 1102 __ move_wide(LIR_OprFact::oopConst(nullptr), 1103 new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT)); 1104 __ move_wide(LIR_OprFact::oopConst(nullptr), 1105 new LIR_Address(thread_reg, in_bytes(JavaThread::exception_pc_offset()), T_OBJECT)); 1106 1107 LIR_Opr result = new_register(T_OBJECT); 1108 __ move(exceptionOopOpr(), result); 1109 set_result(x, result); 1110 } 1111 1112 1113 //---------------------------------------------------------------------- 1114 //---------------------------------------------------------------------- 1115 //---------------------------------------------------------------------- 1116 //---------------------------------------------------------------------- 1117 // visitor functions 1118 //---------------------------------------------------------------------- 1119 //---------------------------------------------------------------------- 1120 //---------------------------------------------------------------------- 1121 //---------------------------------------------------------------------- 1122 1123 void LIRGenerator::do_Phi(Phi* x) { 1124 // phi functions are never visited directly 1125 ShouldNotReachHere(); 1126 } 1127 1128 1129 // Code for a constant is generated lazily unless the constant is frequently used and can't be inlined. 1130 void LIRGenerator::do_Constant(Constant* x) { 1131 if (x->state_before() != nullptr) { 1132 // Any constant with a ValueStack requires patching so emit the patch here 1133 LIR_Opr reg = rlock_result(x); 1134 CodeEmitInfo* info = state_for(x, x->state_before()); 1135 __ oop2reg_patch(nullptr, reg, info); 1136 } else if (x->use_count() > 1 && !can_inline_as_constant(x)) { 1137 if (!x->is_pinned()) { 1138 // unpinned constants are handled specially so that they can be 1139 // put into registers when they are used multiple times within a 1140 // block. After the block completes their operand will be 1141 // cleared so that other blocks can't refer to that register. 1142 set_result(x, load_constant(x)); 1143 } else { 1144 LIR_Opr res = x->operand(); 1145 if (!res->is_valid()) { 1146 res = LIR_OprFact::value_type(x->type()); 1147 } 1148 if (res->is_constant()) { 1149 LIR_Opr reg = rlock_result(x); 1150 __ move(res, reg); 1151 } else { 1152 set_result(x, res); 1153 } 1154 } 1155 } else { 1156 set_result(x, LIR_OprFact::value_type(x->type())); 1157 } 1158 } 1159 1160 1161 void LIRGenerator::do_Local(Local* x) { 1162 // operand_for_instruction has the side effect of setting the result 1163 // so there's no need to do it here. 1164 operand_for_instruction(x); 1165 } 1166 1167 1168 void LIRGenerator::do_Return(Return* x) { 1169 if (compilation()->env()->dtrace_method_probes()) { 1170 BasicTypeList signature; 1171 signature.append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread 1172 signature.append(T_METADATA); // Method* 1173 LIR_OprList* args = new LIR_OprList(); 1174 args->append(getThreadPointer()); 1175 LIR_Opr meth = new_register(T_METADATA); 1176 __ metadata2reg(method()->constant_encoding(), meth); 1177 args->append(meth); 1178 call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), voidType, nullptr); 1179 } 1180 1181 if (x->type()->is_void()) { 1182 __ return_op(LIR_OprFact::illegalOpr); 1183 } else { 1184 LIR_Opr reg = result_register_for(x->type(), /*callee=*/true); 1185 LIRItem result(x->result(), this); 1186 1187 result.load_item_force(reg); 1188 __ return_op(result.result()); 1189 } 1190 set_no_result(x); 1191 } 1192 1193 // Example: ref.get() 1194 // Combination of LoadField and g1 pre-write barrier 1195 void LIRGenerator::do_Reference_get(Intrinsic* x) { 1196 1197 const int referent_offset = java_lang_ref_Reference::referent_offset(); 1198 1199 assert(x->number_of_arguments() == 1, "wrong type"); 1200 1201 LIRItem reference(x->argument_at(0), this); 1202 reference.load_item(); 1203 1204 // need to perform the null check on the reference object 1205 CodeEmitInfo* info = nullptr; 1206 if (x->needs_null_check()) { 1207 info = state_for(x); 1208 } 1209 1210 LIR_Opr result = rlock_result(x, T_OBJECT); 1211 access_load_at(IN_HEAP | ON_WEAK_OOP_REF, T_OBJECT, 1212 reference, LIR_OprFact::intConst(referent_offset), result, 1213 nullptr, info); 1214 } 1215 1216 // Example: clazz.isInstance(object) 1217 void LIRGenerator::do_isInstance(Intrinsic* x) { 1218 assert(x->number_of_arguments() == 2, "wrong type"); 1219 1220 // TODO could try to substitute this node with an equivalent InstanceOf 1221 // if clazz is known to be a constant Class. This will pick up newly found 1222 // constants after HIR construction. I'll leave this to a future change. 1223 1224 // as a first cut, make a simple leaf call to runtime to stay platform independent. 1225 // could follow the aastore example in a future change. 1226 1227 LIRItem clazz(x->argument_at(0), this); 1228 LIRItem object(x->argument_at(1), this); 1229 clazz.load_item(); 1230 object.load_item(); 1231 LIR_Opr result = rlock_result(x); 1232 1233 // need to perform null check on clazz 1234 if (x->needs_null_check()) { 1235 CodeEmitInfo* info = state_for(x); 1236 __ null_check(clazz.result(), info); 1237 } 1238 1239 LIR_Opr call_result = call_runtime(clazz.value(), object.value(), 1240 CAST_FROM_FN_PTR(address, Runtime1::is_instance_of), 1241 x->type(), 1242 nullptr); // null CodeEmitInfo results in a leaf call 1243 __ move(call_result, result); 1244 } 1245 1246 void LIRGenerator::load_klass(LIR_Opr obj, LIR_Opr klass, CodeEmitInfo* null_check_info) { 1247 __ load_klass(obj, klass, null_check_info); 1248 } 1249 1250 // Example: object.getClass () 1251 void LIRGenerator::do_getClass(Intrinsic* x) { 1252 assert(x->number_of_arguments() == 1, "wrong type"); 1253 1254 LIRItem rcvr(x->argument_at(0), this); 1255 rcvr.load_item(); 1256 LIR_Opr temp = new_register(T_ADDRESS); 1257 LIR_Opr result = rlock_result(x); 1258 1259 // need to perform the null check on the rcvr 1260 CodeEmitInfo* info = nullptr; 1261 if (x->needs_null_check()) { 1262 info = state_for(x); 1263 } 1264 1265 LIR_Opr klass = new_register(T_METADATA); 1266 load_klass(rcvr.result(), klass, info); 1267 __ move_wide(new LIR_Address(klass, in_bytes(Klass::java_mirror_offset()), T_ADDRESS), temp); 1268 // mirror = ((OopHandle)mirror)->resolve(); 1269 access_load(IN_NATIVE, T_OBJECT, 1270 LIR_OprFact::address(new LIR_Address(temp, T_OBJECT)), result); 1271 } 1272 1273 // java.lang.Class::isPrimitive() 1274 void LIRGenerator::do_isPrimitive(Intrinsic* x) { 1275 assert(x->number_of_arguments() == 1, "wrong type"); 1276 1277 LIRItem rcvr(x->argument_at(0), this); 1278 rcvr.load_item(); 1279 LIR_Opr temp = new_register(T_METADATA); 1280 LIR_Opr result = rlock_result(x); 1281 1282 CodeEmitInfo* info = nullptr; 1283 if (x->needs_null_check()) { 1284 info = state_for(x); 1285 } 1286 1287 __ move(new LIR_Address(rcvr.result(), java_lang_Class::klass_offset(), T_ADDRESS), temp, info); 1288 __ cmp(lir_cond_notEqual, temp, LIR_OprFact::metadataConst(0)); 1289 __ cmove(lir_cond_notEqual, LIR_OprFact::intConst(0), LIR_OprFact::intConst(1), result, T_BOOLEAN); 1290 } 1291 1292 // Example: Foo.class.getModifiers() 1293 void LIRGenerator::do_getModifiers(Intrinsic* x) { 1294 assert(x->number_of_arguments() == 1, "wrong type"); 1295 1296 LIRItem receiver(x->argument_at(0), this); 1297 receiver.load_item(); 1298 LIR_Opr result = rlock_result(x); 1299 1300 CodeEmitInfo* info = nullptr; 1301 if (x->needs_null_check()) { 1302 info = state_for(x); 1303 } 1304 1305 // While reading off the universal constant mirror is less efficient than doing 1306 // another branch and returning the constant answer, this branchless code runs into 1307 // much less risk of confusion for C1 register allocator. The choice of the universe 1308 // object here is correct as long as it returns the same modifiers we would expect 1309 // from the primitive class itself. See spec for Class.getModifiers that provides 1310 // the typed array klasses with similar modifiers as their component types. 1311 1312 Klass* univ_klass_obj = Universe::byteArrayKlassObj(); 1313 assert(univ_klass_obj->modifier_flags() == (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC), "Sanity"); 1314 LIR_Opr prim_klass = LIR_OprFact::metadataConst(univ_klass_obj); 1315 1316 LIR_Opr recv_klass = new_register(T_METADATA); 1317 __ move(new LIR_Address(receiver.result(), java_lang_Class::klass_offset(), T_ADDRESS), recv_klass, info); 1318 1319 // Check if this is a Java mirror of primitive type, and select the appropriate klass. 1320 LIR_Opr klass = new_register(T_METADATA); 1321 __ cmp(lir_cond_equal, recv_klass, LIR_OprFact::metadataConst(0)); 1322 __ cmove(lir_cond_equal, prim_klass, recv_klass, klass, T_ADDRESS); 1323 1324 // Get the answer. 1325 __ move(new LIR_Address(klass, in_bytes(Klass::modifier_flags_offset()), T_INT), result); 1326 } 1327 1328 void LIRGenerator::do_addressOf(Intrinsic* x) { 1329 assert(x->number_of_arguments() == 1, "wrong type"); 1330 LIR_Opr reg = rlock_result(x); 1331 1332 LIRItem value(x->argument_at(0), this); 1333 value.load_item(); 1334 1335 #ifdef _LP64 1336 __ move(value.result(), reg, NULL); 1337 #else 1338 LIR_Opr res = new_register(T_INT); 1339 __ move(value.result(), res, NULL); 1340 __ convert(Bytecodes::_i2l, res, reg); 1341 #endif 1342 } 1343 1344 void LIRGenerator::do_sizeOf(Intrinsic* x) { 1345 assert(x->number_of_arguments() == 1, "wrong type"); 1346 do_sizeOf_impl(x, 0); 1347 } 1348 1349 void LIRGenerator::do_getObjectSize(Intrinsic* x) { 1350 assert(x->number_of_arguments() == 3, "wrong type"); 1351 do_sizeOf_impl(x, 2); 1352 } 1353 1354 void LIRGenerator::do_sizeOf_impl(Intrinsic* x, int arg_idx) { 1355 LIR_Opr result_reg = rlock_result(x); 1356 1357 LIRItem value(x->argument_at(arg_idx), this); 1358 value.load_item(); 1359 1360 LIR_Opr klass = new_register(T_METADATA); 1361 load_klass(value.result(), klass, nullptr); 1362 LIR_Opr layout = new_register(T_INT); 1363 __ move(new LIR_Address(klass, in_bytes(Klass::layout_helper_offset()), T_INT), layout); 1364 1365 LabelObj* L_done = new LabelObj(); 1366 LabelObj* L_array = new LabelObj(); 1367 1368 __ cmp(lir_cond_lessEqual, layout, 0); 1369 __ branch(lir_cond_lessEqual, L_array->label()); 1370 1371 // Instance case: the layout helper gives us instance size almost directly, 1372 // but we need to mask out the _lh_instance_slow_path_bit. 1373 1374 assert((int) Klass::_lh_instance_slow_path_bit < BytesPerLong, "clear bit"); 1375 1376 LIR_Opr mask = load_immediate(~(jint) right_n_bits(LogBytesPerLong), T_INT); 1377 __ logical_and(layout, mask, layout); 1378 __ convert(Bytecodes::_i2l, layout, result_reg); 1379 1380 __ branch(lir_cond_always, L_done->label()); 1381 1382 // Array case: size is round(header + element_size*arraylength). 1383 // Since arraylength is different for every array instance, we have to 1384 // compute the whole thing at runtime. 1385 1386 __ branch_destination(L_array->label()); 1387 1388 int round_mask = MinObjAlignmentInBytes - 1; 1389 1390 // Figure out header sizes first. 1391 LIR_Opr hss = load_immediate(Klass::_lh_header_size_shift, T_INT); 1392 LIR_Opr hsm = load_immediate(Klass::_lh_header_size_mask, T_INT); 1393 1394 LIR_Opr header_size = new_register(T_INT); 1395 __ move(layout, header_size); 1396 LIR_Opr tmp = new_register(T_INT); 1397 __ unsigned_shift_right(header_size, hss, header_size, tmp); 1398 __ logical_and(header_size, hsm, header_size); 1399 __ add(header_size, LIR_OprFact::intConst(round_mask), header_size); 1400 1401 // Figure out the array length in bytes 1402 assert(Klass::_lh_log2_element_size_shift == 0, "use shift in place"); 1403 LIR_Opr l2esm = load_immediate(Klass::_lh_log2_element_size_mask, T_INT); 1404 __ logical_and(layout, l2esm, layout); 1405 1406 LIR_Opr length_int = new_register(T_INT); 1407 __ move(new LIR_Address(value.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), length_int); 1408 1409 #ifdef _LP64 1410 LIR_Opr length = new_register(T_LONG); 1411 __ convert(Bytecodes::_i2l, length_int, length); 1412 #endif 1413 1414 // Shift-left awkwardness. Normally it is just: 1415 // __ shift_left(length, layout, length); 1416 // But C1 cannot perform shift_left with non-constant count, so we end up 1417 // doing the per-bit loop dance here. x86_32 also does not know how to shift 1418 // longs, so we have to act on ints. 1419 LabelObj* L_shift_loop = new LabelObj(); 1420 LabelObj* L_shift_exit = new LabelObj(); 1421 1422 __ branch_destination(L_shift_loop->label()); 1423 __ cmp(lir_cond_equal, layout, 0); 1424 __ branch(lir_cond_equal, L_shift_exit->label()); 1425 1426 #ifdef _LP64 1427 __ shift_left(length, 1, length); 1428 #else 1429 __ shift_left(length_int, 1, length_int); 1430 #endif 1431 1432 __ sub(layout, LIR_OprFact::intConst(1), layout); 1433 1434 __ branch(lir_cond_always, L_shift_loop->label()); 1435 __ branch_destination(L_shift_exit->label()); 1436 1437 // Mix all up, round, and push to the result. 1438 #ifdef _LP64 1439 LIR_Opr header_size_long = new_register(T_LONG); 1440 __ convert(Bytecodes::_i2l, header_size, header_size_long); 1441 __ add(length, header_size_long, length); 1442 if (round_mask != 0) { 1443 LIR_Opr round_mask_opr = load_immediate(~(jlong)round_mask, T_LONG); 1444 __ logical_and(length, round_mask_opr, length); 1445 } 1446 __ move(length, result_reg); 1447 #else 1448 __ add(length_int, header_size, length_int); 1449 if (round_mask != 0) { 1450 LIR_Opr round_mask_opr = load_immediate(~round_mask, T_INT); 1451 __ logical_and(length_int, round_mask_opr, length_int); 1452 } 1453 __ convert(Bytecodes::_i2l, length_int, result_reg); 1454 #endif 1455 1456 __ branch_destination(L_done->label()); 1457 } 1458 1459 void LIRGenerator::do_scopedValueCache(Intrinsic* x) { 1460 do_JavaThreadField(x, JavaThread::scopedValueCache_offset()); 1461 } 1462 1463 // Example: Thread.currentCarrierThread() 1464 void LIRGenerator::do_currentCarrierThread(Intrinsic* x) { 1465 do_JavaThreadField(x, JavaThread::threadObj_offset()); 1466 } 1467 1468 void LIRGenerator::do_vthread(Intrinsic* x) { 1469 do_JavaThreadField(x, JavaThread::vthread_offset()); 1470 } 1471 1472 void LIRGenerator::do_JavaThreadField(Intrinsic* x, ByteSize offset) { 1473 assert(x->number_of_arguments() == 0, "wrong type"); 1474 LIR_Opr temp = new_register(T_ADDRESS); 1475 LIR_Opr reg = rlock_result(x); 1476 __ move(new LIR_Address(getThreadPointer(), in_bytes(offset), T_ADDRESS), temp); 1477 access_load(IN_NATIVE, T_OBJECT, 1478 LIR_OprFact::address(new LIR_Address(temp, T_OBJECT)), reg); 1479 } 1480 1481 void LIRGenerator::do_RegisterFinalizer(Intrinsic* x) { 1482 assert(x->number_of_arguments() == 1, "wrong type"); 1483 LIRItem receiver(x->argument_at(0), this); 1484 1485 receiver.load_item(); 1486 BasicTypeList signature; 1487 signature.append(T_OBJECT); // receiver 1488 LIR_OprList* args = new LIR_OprList(); 1489 args->append(receiver.result()); 1490 CodeEmitInfo* info = state_for(x, x->state()); 1491 call_runtime(&signature, args, 1492 CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::register_finalizer_id)), 1493 voidType, info); 1494 1495 set_no_result(x); 1496 } 1497 1498 1499 //------------------------local access-------------------------------------- 1500 1501 LIR_Opr LIRGenerator::operand_for_instruction(Instruction* x) { 1502 if (x->operand()->is_illegal()) { 1503 Constant* c = x->as_Constant(); 1504 if (c != nullptr) { 1505 x->set_operand(LIR_OprFact::value_type(c->type())); 1506 } else { 1507 assert(x->as_Phi() || x->as_Local() != nullptr, "only for Phi and Local"); 1508 // allocate a virtual register for this local or phi 1509 x->set_operand(rlock(x)); 1510 _instruction_for_operand.at_put_grow(x->operand()->vreg_number(), x, nullptr); 1511 } 1512 } 1513 return x->operand(); 1514 } 1515 1516 1517 Instruction* LIRGenerator::instruction_for_opr(LIR_Opr opr) { 1518 if (opr->is_virtual()) { 1519 return instruction_for_vreg(opr->vreg_number()); 1520 } 1521 return nullptr; 1522 } 1523 1524 1525 Instruction* LIRGenerator::instruction_for_vreg(int reg_num) { 1526 if (reg_num < _instruction_for_operand.length()) { 1527 return _instruction_for_operand.at(reg_num); 1528 } 1529 return nullptr; 1530 } 1531 1532 1533 void LIRGenerator::set_vreg_flag(int vreg_num, VregFlag f) { 1534 if (_vreg_flags.size_in_bits() == 0) { 1535 BitMap2D temp(100, num_vreg_flags); 1536 _vreg_flags = temp; 1537 } 1538 _vreg_flags.at_put_grow(vreg_num, f, true); 1539 } 1540 1541 bool LIRGenerator::is_vreg_flag_set(int vreg_num, VregFlag f) { 1542 if (!_vreg_flags.is_valid_index(vreg_num, f)) { 1543 return false; 1544 } 1545 return _vreg_flags.at(vreg_num, f); 1546 } 1547 1548 1549 // Block local constant handling. This code is useful for keeping 1550 // unpinned constants and constants which aren't exposed in the IR in 1551 // registers. Unpinned Constant instructions have their operands 1552 // cleared when the block is finished so that other blocks can't end 1553 // up referring to their registers. 1554 1555 LIR_Opr LIRGenerator::load_constant(Constant* x) { 1556 assert(!x->is_pinned(), "only for unpinned constants"); 1557 _unpinned_constants.append(x); 1558 return load_constant(LIR_OprFact::value_type(x->type())->as_constant_ptr()); 1559 } 1560 1561 1562 LIR_Opr LIRGenerator::load_constant(LIR_Const* c) { 1563 BasicType t = c->type(); 1564 for (int i = 0; i < _constants.length(); i++) { 1565 LIR_Const* other = _constants.at(i); 1566 if (t == other->type()) { 1567 switch (t) { 1568 case T_INT: 1569 case T_FLOAT: 1570 if (c->as_jint_bits() != other->as_jint_bits()) continue; 1571 break; 1572 case T_LONG: 1573 case T_DOUBLE: 1574 if (c->as_jint_hi_bits() != other->as_jint_hi_bits()) continue; 1575 if (c->as_jint_lo_bits() != other->as_jint_lo_bits()) continue; 1576 break; 1577 case T_OBJECT: 1578 if (c->as_jobject() != other->as_jobject()) continue; 1579 break; 1580 default: 1581 break; 1582 } 1583 return _reg_for_constants.at(i); 1584 } 1585 } 1586 1587 LIR_Opr result = new_register(t); 1588 __ move((LIR_Opr)c, result); 1589 _constants.append(c); 1590 _reg_for_constants.append(result); 1591 return result; 1592 } 1593 1594 //------------------------field access-------------------------------------- 1595 1596 void LIRGenerator::do_CompareAndSwap(Intrinsic* x, ValueType* type) { 1597 assert(x->number_of_arguments() == 4, "wrong type"); 1598 LIRItem obj (x->argument_at(0), this); // object 1599 LIRItem offset(x->argument_at(1), this); // offset of field 1600 LIRItem cmp (x->argument_at(2), this); // value to compare with field 1601 LIRItem val (x->argument_at(3), this); // replace field with val if matches cmp 1602 assert(obj.type()->tag() == objectTag, "invalid type"); 1603 assert(cmp.type()->tag() == type->tag(), "invalid type"); 1604 assert(val.type()->tag() == type->tag(), "invalid type"); 1605 1606 LIR_Opr result = access_atomic_cmpxchg_at(IN_HEAP, as_BasicType(type), 1607 obj, offset, cmp, val); 1608 set_result(x, result); 1609 } 1610 1611 // Comment copied form templateTable_i486.cpp 1612 // ---------------------------------------------------------------------------- 1613 // Volatile variables demand their effects be made known to all CPU's in 1614 // order. Store buffers on most chips allow reads & writes to reorder; the 1615 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of 1616 // memory barrier (i.e., it's not sufficient that the interpreter does not 1617 // reorder volatile references, the hardware also must not reorder them). 1618 // 1619 // According to the new Java Memory Model (JMM): 1620 // (1) All volatiles are serialized wrt to each other. 1621 // ALSO reads & writes act as acquire & release, so: 1622 // (2) A read cannot let unrelated NON-volatile memory refs that happen after 1623 // the read float up to before the read. It's OK for non-volatile memory refs 1624 // that happen before the volatile read to float down below it. 1625 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs 1626 // that happen BEFORE the write float down to after the write. It's OK for 1627 // non-volatile memory refs that happen after the volatile write to float up 1628 // before it. 1629 // 1630 // We only put in barriers around volatile refs (they are expensive), not 1631 // _between_ memory refs (that would require us to track the flavor of the 1632 // previous memory refs). Requirements (2) and (3) require some barriers 1633 // before volatile stores and after volatile loads. These nearly cover 1634 // requirement (1) but miss the volatile-store-volatile-load case. This final 1635 // case is placed after volatile-stores although it could just as well go 1636 // before volatile-loads. 1637 1638 1639 void LIRGenerator::do_StoreField(StoreField* x) { 1640 bool needs_patching = x->needs_patching(); 1641 bool is_volatile = x->field()->is_volatile(); 1642 BasicType field_type = x->field_type(); 1643 1644 CodeEmitInfo* info = nullptr; 1645 if (needs_patching) { 1646 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access"); 1647 info = state_for(x, x->state_before()); 1648 } else if (x->needs_null_check()) { 1649 NullCheck* nc = x->explicit_null_check(); 1650 if (nc == nullptr) { 1651 info = state_for(x); 1652 } else { 1653 info = state_for(nc); 1654 } 1655 } 1656 1657 LIRItem object(x->obj(), this); 1658 LIRItem value(x->value(), this); 1659 1660 object.load_item(); 1661 1662 if (is_volatile || needs_patching) { 1663 // load item if field is volatile (fewer special cases for volatiles) 1664 // load item if field not initialized 1665 // load item if field not constant 1666 // because of code patching we cannot inline constants 1667 if (field_type == T_BYTE || field_type == T_BOOLEAN) { 1668 value.load_byte_item(); 1669 } else { 1670 value.load_item(); 1671 } 1672 } else { 1673 value.load_for_store(field_type); 1674 } 1675 1676 set_no_result(x); 1677 1678 #ifndef PRODUCT 1679 if (PrintNotLoaded && needs_patching) { 1680 tty->print_cr(" ###class not loaded at store_%s bci %d", 1681 x->is_static() ? "static" : "field", x->printable_bci()); 1682 } 1683 #endif 1684 1685 if (x->needs_null_check() && 1686 (needs_patching || 1687 MacroAssembler::needs_explicit_null_check(x->offset()))) { 1688 // Emit an explicit null check because the offset is too large. 1689 // If the class is not loaded and the object is null, we need to deoptimize to throw a 1690 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code. 1691 __ null_check(object.result(), new CodeEmitInfo(info), /* deoptimize */ needs_patching); 1692 } 1693 1694 DecoratorSet decorators = IN_HEAP; 1695 if (is_volatile) { 1696 decorators |= MO_SEQ_CST; 1697 } 1698 if (needs_patching) { 1699 decorators |= C1_NEEDS_PATCHING; 1700 } 1701 1702 access_store_at(decorators, field_type, object, LIR_OprFact::intConst(x->offset()), 1703 value.result(), info != nullptr ? new CodeEmitInfo(info) : nullptr, info); 1704 } 1705 1706 void LIRGenerator::do_StoreIndexed(StoreIndexed* x) { 1707 assert(x->is_pinned(),""); 1708 bool needs_range_check = x->compute_needs_range_check(); 1709 bool use_length = x->length() != nullptr; 1710 bool obj_store = is_reference_type(x->elt_type()); 1711 bool needs_store_check = obj_store && (x->value()->as_Constant() == nullptr || 1712 !get_jobject_constant(x->value())->is_null_object() || 1713 x->should_profile()); 1714 1715 LIRItem array(x->array(), this); 1716 LIRItem index(x->index(), this); 1717 LIRItem value(x->value(), this); 1718 LIRItem length(this); 1719 1720 array.load_item(); 1721 index.load_nonconstant(); 1722 1723 if (use_length && needs_range_check) { 1724 length.set_instruction(x->length()); 1725 length.load_item(); 1726 1727 } 1728 if (needs_store_check || x->check_boolean()) { 1729 value.load_item(); 1730 } else { 1731 value.load_for_store(x->elt_type()); 1732 } 1733 1734 set_no_result(x); 1735 1736 // the CodeEmitInfo must be duplicated for each different 1737 // LIR-instruction because spilling can occur anywhere between two 1738 // instructions and so the debug information must be different 1739 CodeEmitInfo* range_check_info = state_for(x); 1740 CodeEmitInfo* null_check_info = nullptr; 1741 if (x->needs_null_check()) { 1742 null_check_info = new CodeEmitInfo(range_check_info); 1743 } 1744 1745 if (needs_range_check) { 1746 if (use_length) { 1747 __ cmp(lir_cond_belowEqual, length.result(), index.result()); 1748 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result())); 1749 } else { 1750 array_range_check(array.result(), index.result(), null_check_info, range_check_info); 1751 // range_check also does the null check 1752 null_check_info = nullptr; 1753 } 1754 } 1755 1756 if (GenerateArrayStoreCheck && needs_store_check) { 1757 CodeEmitInfo* store_check_info = new CodeEmitInfo(range_check_info); 1758 array_store_check(value.result(), array.result(), store_check_info, x->profiled_method(), x->profiled_bci()); 1759 } 1760 1761 DecoratorSet decorators = IN_HEAP | IS_ARRAY; 1762 if (x->check_boolean()) { 1763 decorators |= C1_MASK_BOOLEAN; 1764 } 1765 1766 access_store_at(decorators, x->elt_type(), array, index.result(), value.result(), 1767 nullptr, null_check_info); 1768 } 1769 1770 void LIRGenerator::access_load_at(DecoratorSet decorators, BasicType type, 1771 LIRItem& base, LIR_Opr offset, LIR_Opr result, 1772 CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) { 1773 decorators |= ACCESS_READ; 1774 LIRAccess access(this, decorators, base, offset, type, patch_info, load_emit_info); 1775 if (access.is_raw()) { 1776 _barrier_set->BarrierSetC1::load_at(access, result); 1777 } else { 1778 _barrier_set->load_at(access, result); 1779 } 1780 } 1781 1782 void LIRGenerator::access_load(DecoratorSet decorators, BasicType type, 1783 LIR_Opr addr, LIR_Opr result) { 1784 decorators |= ACCESS_READ; 1785 LIRAccess access(this, decorators, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, type); 1786 access.set_resolved_addr(addr); 1787 if (access.is_raw()) { 1788 _barrier_set->BarrierSetC1::load(access, result); 1789 } else { 1790 _barrier_set->load(access, result); 1791 } 1792 } 1793 1794 void LIRGenerator::access_store_at(DecoratorSet decorators, BasicType type, 1795 LIRItem& base, LIR_Opr offset, LIR_Opr value, 1796 CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info) { 1797 decorators |= ACCESS_WRITE; 1798 LIRAccess access(this, decorators, base, offset, type, patch_info, store_emit_info); 1799 if (access.is_raw()) { 1800 _barrier_set->BarrierSetC1::store_at(access, value); 1801 } else { 1802 _barrier_set->store_at(access, value); 1803 } 1804 } 1805 1806 LIR_Opr LIRGenerator::access_atomic_cmpxchg_at(DecoratorSet decorators, BasicType type, 1807 LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value) { 1808 decorators |= ACCESS_READ; 1809 decorators |= ACCESS_WRITE; 1810 // Atomic operations are SEQ_CST by default 1811 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0; 1812 LIRAccess access(this, decorators, base, offset, type); 1813 if (access.is_raw()) { 1814 return _barrier_set->BarrierSetC1::atomic_cmpxchg_at(access, cmp_value, new_value); 1815 } else { 1816 return _barrier_set->atomic_cmpxchg_at(access, cmp_value, new_value); 1817 } 1818 } 1819 1820 LIR_Opr LIRGenerator::access_atomic_xchg_at(DecoratorSet decorators, BasicType type, 1821 LIRItem& base, LIRItem& offset, LIRItem& value) { 1822 decorators |= ACCESS_READ; 1823 decorators |= ACCESS_WRITE; 1824 // Atomic operations are SEQ_CST by default 1825 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0; 1826 LIRAccess access(this, decorators, base, offset, type); 1827 if (access.is_raw()) { 1828 return _barrier_set->BarrierSetC1::atomic_xchg_at(access, value); 1829 } else { 1830 return _barrier_set->atomic_xchg_at(access, value); 1831 } 1832 } 1833 1834 LIR_Opr LIRGenerator::access_atomic_add_at(DecoratorSet decorators, BasicType type, 1835 LIRItem& base, LIRItem& offset, LIRItem& value) { 1836 decorators |= ACCESS_READ; 1837 decorators |= ACCESS_WRITE; 1838 // Atomic operations are SEQ_CST by default 1839 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0; 1840 LIRAccess access(this, decorators, base, offset, type); 1841 if (access.is_raw()) { 1842 return _barrier_set->BarrierSetC1::atomic_add_at(access, value); 1843 } else { 1844 return _barrier_set->atomic_add_at(access, value); 1845 } 1846 } 1847 1848 void LIRGenerator::do_LoadField(LoadField* x) { 1849 bool needs_patching = x->needs_patching(); 1850 bool is_volatile = x->field()->is_volatile(); 1851 BasicType field_type = x->field_type(); 1852 1853 CodeEmitInfo* info = nullptr; 1854 if (needs_patching) { 1855 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access"); 1856 info = state_for(x, x->state_before()); 1857 } else if (x->needs_null_check()) { 1858 NullCheck* nc = x->explicit_null_check(); 1859 if (nc == nullptr) { 1860 info = state_for(x); 1861 } else { 1862 info = state_for(nc); 1863 } 1864 } 1865 1866 LIRItem object(x->obj(), this); 1867 1868 object.load_item(); 1869 1870 #ifndef PRODUCT 1871 if (PrintNotLoaded && needs_patching) { 1872 tty->print_cr(" ###class not loaded at load_%s bci %d", 1873 x->is_static() ? "static" : "field", x->printable_bci()); 1874 } 1875 #endif 1876 1877 bool stress_deopt = StressLoopInvariantCodeMotion && info && info->deoptimize_on_exception(); 1878 if (x->needs_null_check() && 1879 (needs_patching || 1880 MacroAssembler::needs_explicit_null_check(x->offset()) || 1881 stress_deopt)) { 1882 LIR_Opr obj = object.result(); 1883 if (stress_deopt) { 1884 obj = new_register(T_OBJECT); 1885 __ move(LIR_OprFact::oopConst(nullptr), obj); 1886 } 1887 // Emit an explicit null check because the offset is too large. 1888 // If the class is not loaded and the object is null, we need to deoptimize to throw a 1889 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code. 1890 __ null_check(obj, new CodeEmitInfo(info), /* deoptimize */ needs_patching); 1891 } 1892 1893 DecoratorSet decorators = IN_HEAP; 1894 if (is_volatile) { 1895 decorators |= MO_SEQ_CST; 1896 } 1897 if (needs_patching) { 1898 decorators |= C1_NEEDS_PATCHING; 1899 } 1900 1901 LIR_Opr result = rlock_result(x, field_type); 1902 access_load_at(decorators, field_type, 1903 object, LIR_OprFact::intConst(x->offset()), result, 1904 info ? new CodeEmitInfo(info) : nullptr, info); 1905 } 1906 1907 // int/long jdk.internal.util.Preconditions.checkIndex 1908 void LIRGenerator::do_PreconditionsCheckIndex(Intrinsic* x, BasicType type) { 1909 assert(x->number_of_arguments() == 3, "wrong type"); 1910 LIRItem index(x->argument_at(0), this); 1911 LIRItem length(x->argument_at(1), this); 1912 LIRItem oobef(x->argument_at(2), this); 1913 1914 index.load_item(); 1915 length.load_item(); 1916 oobef.load_item(); 1917 1918 LIR_Opr result = rlock_result(x); 1919 // x->state() is created from copy_state_for_exception, it does not contains arguments 1920 // we should prepare them before entering into interpreter mode due to deoptimization. 1921 ValueStack* state = x->state(); 1922 for (int i = 0; i < x->number_of_arguments(); i++) { 1923 Value arg = x->argument_at(i); 1924 state->push(arg->type(), arg); 1925 } 1926 CodeEmitInfo* info = state_for(x, state); 1927 1928 LIR_Opr len = length.result(); 1929 LIR_Opr zero; 1930 if (type == T_INT) { 1931 zero = LIR_OprFact::intConst(0); 1932 if (length.result()->is_constant()){ 1933 len = LIR_OprFact::intConst(length.result()->as_jint()); 1934 } 1935 } else { 1936 assert(type == T_LONG, "sanity check"); 1937 zero = LIR_OprFact::longConst(0); 1938 if (length.result()->is_constant()){ 1939 len = LIR_OprFact::longConst(length.result()->as_jlong()); 1940 } 1941 } 1942 // C1 can not handle the case that comparing index with constant value while condition 1943 // is neither lir_cond_equal nor lir_cond_notEqual, see LIR_Assembler::comp_op. 1944 LIR_Opr zero_reg = new_register(type); 1945 __ move(zero, zero_reg); 1946 #if defined(X86) && !defined(_LP64) 1947 // BEWARE! On 32-bit x86 cmp clobbers its left argument so we need a temp copy. 1948 LIR_Opr index_copy = new_register(index.type()); 1949 // index >= 0 1950 __ move(index.result(), index_copy); 1951 __ cmp(lir_cond_less, index_copy, zero_reg); 1952 __ branch(lir_cond_less, new DeoptimizeStub(info, Deoptimization::Reason_range_check, 1953 Deoptimization::Action_make_not_entrant)); 1954 // index < length 1955 __ move(index.result(), index_copy); 1956 __ cmp(lir_cond_greaterEqual, index_copy, len); 1957 __ branch(lir_cond_greaterEqual, new DeoptimizeStub(info, Deoptimization::Reason_range_check, 1958 Deoptimization::Action_make_not_entrant)); 1959 #else 1960 // index >= 0 1961 __ cmp(lir_cond_less, index.result(), zero_reg); 1962 __ branch(lir_cond_less, new DeoptimizeStub(info, Deoptimization::Reason_range_check, 1963 Deoptimization::Action_make_not_entrant)); 1964 // index < length 1965 __ cmp(lir_cond_greaterEqual, index.result(), len); 1966 __ branch(lir_cond_greaterEqual, new DeoptimizeStub(info, Deoptimization::Reason_range_check, 1967 Deoptimization::Action_make_not_entrant)); 1968 #endif 1969 __ move(index.result(), result); 1970 } 1971 1972 //------------------------array access-------------------------------------- 1973 1974 1975 void LIRGenerator::do_ArrayLength(ArrayLength* x) { 1976 LIRItem array(x->array(), this); 1977 array.load_item(); 1978 LIR_Opr reg = rlock_result(x); 1979 1980 CodeEmitInfo* info = nullptr; 1981 if (x->needs_null_check()) { 1982 NullCheck* nc = x->explicit_null_check(); 1983 if (nc == nullptr) { 1984 info = state_for(x); 1985 } else { 1986 info = state_for(nc); 1987 } 1988 if (StressLoopInvariantCodeMotion && info->deoptimize_on_exception()) { 1989 LIR_Opr obj = new_register(T_OBJECT); 1990 __ move(LIR_OprFact::oopConst(nullptr), obj); 1991 __ null_check(obj, new CodeEmitInfo(info)); 1992 } 1993 } 1994 __ load(new LIR_Address(array.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), reg, info, lir_patch_none); 1995 } 1996 1997 1998 void LIRGenerator::do_LoadIndexed(LoadIndexed* x) { 1999 bool use_length = x->length() != nullptr; 2000 LIRItem array(x->array(), this); 2001 LIRItem index(x->index(), this); 2002 LIRItem length(this); 2003 bool needs_range_check = x->compute_needs_range_check(); 2004 2005 if (use_length && needs_range_check) { 2006 length.set_instruction(x->length()); 2007 length.load_item(); 2008 } 2009 2010 array.load_item(); 2011 if (index.is_constant() && can_inline_as_constant(x->index())) { 2012 // let it be a constant 2013 index.dont_load_item(); 2014 } else { 2015 index.load_item(); 2016 } 2017 2018 CodeEmitInfo* range_check_info = state_for(x); 2019 CodeEmitInfo* null_check_info = nullptr; 2020 if (x->needs_null_check()) { 2021 NullCheck* nc = x->explicit_null_check(); 2022 if (nc != nullptr) { 2023 null_check_info = state_for(nc); 2024 } else { 2025 null_check_info = range_check_info; 2026 } 2027 if (StressLoopInvariantCodeMotion && null_check_info->deoptimize_on_exception()) { 2028 LIR_Opr obj = new_register(T_OBJECT); 2029 __ move(LIR_OprFact::oopConst(nullptr), obj); 2030 __ null_check(obj, new CodeEmitInfo(null_check_info)); 2031 } 2032 } 2033 2034 if (needs_range_check) { 2035 if (StressLoopInvariantCodeMotion && range_check_info->deoptimize_on_exception()) { 2036 __ branch(lir_cond_always, new RangeCheckStub(range_check_info, index.result(), array.result())); 2037 } else if (use_length) { 2038 // TODO: use a (modified) version of array_range_check that does not require a 2039 // constant length to be loaded to a register 2040 __ cmp(lir_cond_belowEqual, length.result(), index.result()); 2041 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result())); 2042 } else { 2043 array_range_check(array.result(), index.result(), null_check_info, range_check_info); 2044 // The range check performs the null check, so clear it out for the load 2045 null_check_info = nullptr; 2046 } 2047 } 2048 2049 DecoratorSet decorators = IN_HEAP | IS_ARRAY; 2050 2051 LIR_Opr result = rlock_result(x, x->elt_type()); 2052 access_load_at(decorators, x->elt_type(), 2053 array, index.result(), result, 2054 nullptr, null_check_info); 2055 } 2056 2057 2058 void LIRGenerator::do_NullCheck(NullCheck* x) { 2059 if (x->can_trap()) { 2060 LIRItem value(x->obj(), this); 2061 value.load_item(); 2062 CodeEmitInfo* info = state_for(x); 2063 __ null_check(value.result(), info); 2064 } 2065 } 2066 2067 2068 void LIRGenerator::do_TypeCast(TypeCast* x) { 2069 LIRItem value(x->obj(), this); 2070 value.load_item(); 2071 // the result is the same as from the node we are casting 2072 set_result(x, value.result()); 2073 } 2074 2075 2076 void LIRGenerator::do_Throw(Throw* x) { 2077 LIRItem exception(x->exception(), this); 2078 exception.load_item(); 2079 set_no_result(x); 2080 LIR_Opr exception_opr = exception.result(); 2081 CodeEmitInfo* info = state_for(x, x->state()); 2082 2083 #ifndef PRODUCT 2084 if (PrintC1Statistics) { 2085 increment_counter(Runtime1::throw_count_address(), T_INT); 2086 } 2087 #endif 2088 2089 // check if the instruction has an xhandler in any of the nested scopes 2090 bool unwind = false; 2091 if (info->exception_handlers()->length() == 0) { 2092 // this throw is not inside an xhandler 2093 unwind = true; 2094 } else { 2095 // get some idea of the throw type 2096 bool type_is_exact = true; 2097 ciType* throw_type = x->exception()->exact_type(); 2098 if (throw_type == nullptr) { 2099 type_is_exact = false; 2100 throw_type = x->exception()->declared_type(); 2101 } 2102 if (throw_type != nullptr && throw_type->is_instance_klass()) { 2103 ciInstanceKlass* throw_klass = (ciInstanceKlass*)throw_type; 2104 unwind = !x->exception_handlers()->could_catch(throw_klass, type_is_exact); 2105 } 2106 } 2107 2108 // do null check before moving exception oop into fixed register 2109 // to avoid a fixed interval with an oop during the null check. 2110 // Use a copy of the CodeEmitInfo because debug information is 2111 // different for null_check and throw. 2112 if (x->exception()->as_NewInstance() == nullptr && x->exception()->as_ExceptionObject() == nullptr) { 2113 // if the exception object wasn't created using new then it might be null. 2114 __ null_check(exception_opr, new CodeEmitInfo(info, x->state()->copy(ValueStack::ExceptionState, x->state()->bci()))); 2115 } 2116 2117 if (compilation()->env()->jvmti_can_post_on_exceptions()) { 2118 // we need to go through the exception lookup path to get JVMTI 2119 // notification done 2120 unwind = false; 2121 } 2122 2123 // move exception oop into fixed register 2124 __ move(exception_opr, exceptionOopOpr()); 2125 2126 if (unwind) { 2127 __ unwind_exception(exceptionOopOpr()); 2128 } else { 2129 __ throw_exception(exceptionPcOpr(), exceptionOopOpr(), info); 2130 } 2131 } 2132 2133 2134 void LIRGenerator::do_RoundFP(RoundFP* x) { 2135 assert(strict_fp_requires_explicit_rounding, "not required"); 2136 2137 LIRItem input(x->input(), this); 2138 input.load_item(); 2139 LIR_Opr input_opr = input.result(); 2140 assert(input_opr->is_register(), "why round if value is not in a register?"); 2141 assert(input_opr->is_single_fpu() || input_opr->is_double_fpu(), "input should be floating-point value"); 2142 if (input_opr->is_single_fpu()) { 2143 set_result(x, round_item(input_opr)); // This code path not currently taken 2144 } else { 2145 LIR_Opr result = new_register(T_DOUBLE); 2146 set_vreg_flag(result, must_start_in_memory); 2147 __ roundfp(input_opr, LIR_OprFact::illegalOpr, result); 2148 set_result(x, result); 2149 } 2150 } 2151 2152 2153 void LIRGenerator::do_UnsafeGet(UnsafeGet* x) { 2154 BasicType type = x->basic_type(); 2155 LIRItem src(x->object(), this); 2156 LIRItem off(x->offset(), this); 2157 2158 off.load_item(); 2159 src.load_item(); 2160 2161 DecoratorSet decorators = IN_HEAP | C1_UNSAFE_ACCESS; 2162 2163 if (x->is_volatile()) { 2164 decorators |= MO_SEQ_CST; 2165 } 2166 if (type == T_BOOLEAN) { 2167 decorators |= C1_MASK_BOOLEAN; 2168 } 2169 if (is_reference_type(type)) { 2170 decorators |= ON_UNKNOWN_OOP_REF; 2171 } 2172 2173 LIR_Opr result = rlock_result(x, type); 2174 if (!x->is_raw()) { 2175 access_load_at(decorators, type, src, off.result(), result); 2176 } else { 2177 // Currently it is only used in GraphBuilder::setup_osr_entry_block. 2178 // It reads the value from [src + offset] directly. 2179 #ifdef _LP64 2180 LIR_Opr offset = new_register(T_LONG); 2181 __ convert(Bytecodes::_i2l, off.result(), offset); 2182 #else 2183 LIR_Opr offset = off.result(); 2184 #endif 2185 LIR_Address* addr = new LIR_Address(src.result(), offset, type); 2186 if (is_reference_type(type)) { 2187 __ move_wide(addr, result); 2188 } else { 2189 __ move(addr, result); 2190 } 2191 } 2192 } 2193 2194 2195 void LIRGenerator::do_UnsafePut(UnsafePut* x) { 2196 BasicType type = x->basic_type(); 2197 LIRItem src(x->object(), this); 2198 LIRItem off(x->offset(), this); 2199 LIRItem data(x->value(), this); 2200 2201 src.load_item(); 2202 if (type == T_BOOLEAN || type == T_BYTE) { 2203 data.load_byte_item(); 2204 } else { 2205 data.load_item(); 2206 } 2207 off.load_item(); 2208 2209 set_no_result(x); 2210 2211 DecoratorSet decorators = IN_HEAP | C1_UNSAFE_ACCESS; 2212 if (is_reference_type(type)) { 2213 decorators |= ON_UNKNOWN_OOP_REF; 2214 } 2215 if (x->is_volatile()) { 2216 decorators |= MO_SEQ_CST; 2217 } 2218 access_store_at(decorators, type, src, off.result(), data.result()); 2219 } 2220 2221 void LIRGenerator::do_UnsafeGetAndSet(UnsafeGetAndSet* x) { 2222 BasicType type = x->basic_type(); 2223 LIRItem src(x->object(), this); 2224 LIRItem off(x->offset(), this); 2225 LIRItem value(x->value(), this); 2226 2227 DecoratorSet decorators = IN_HEAP | C1_UNSAFE_ACCESS | MO_SEQ_CST; 2228 2229 if (is_reference_type(type)) { 2230 decorators |= ON_UNKNOWN_OOP_REF; 2231 } 2232 2233 LIR_Opr result; 2234 if (x->is_add()) { 2235 result = access_atomic_add_at(decorators, type, src, off, value); 2236 } else { 2237 result = access_atomic_xchg_at(decorators, type, src, off, value); 2238 } 2239 set_result(x, result); 2240 } 2241 2242 void LIRGenerator::do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux) { 2243 int lng = x->length(); 2244 2245 for (int i = 0; i < lng; i++) { 2246 C1SwitchRange* one_range = x->at(i); 2247 int low_key = one_range->low_key(); 2248 int high_key = one_range->high_key(); 2249 BlockBegin* dest = one_range->sux(); 2250 if (low_key == high_key) { 2251 __ cmp(lir_cond_equal, value, low_key); 2252 __ branch(lir_cond_equal, dest); 2253 } else if (high_key - low_key == 1) { 2254 __ cmp(lir_cond_equal, value, low_key); 2255 __ branch(lir_cond_equal, dest); 2256 __ cmp(lir_cond_equal, value, high_key); 2257 __ branch(lir_cond_equal, dest); 2258 } else { 2259 LabelObj* L = new LabelObj(); 2260 __ cmp(lir_cond_less, value, low_key); 2261 __ branch(lir_cond_less, L->label()); 2262 __ cmp(lir_cond_lessEqual, value, high_key); 2263 __ branch(lir_cond_lessEqual, dest); 2264 __ branch_destination(L->label()); 2265 } 2266 } 2267 __ jump(default_sux); 2268 } 2269 2270 2271 SwitchRangeArray* LIRGenerator::create_lookup_ranges(TableSwitch* x) { 2272 SwitchRangeList* res = new SwitchRangeList(); 2273 int len = x->length(); 2274 if (len > 0) { 2275 BlockBegin* sux = x->sux_at(0); 2276 int low = x->lo_key(); 2277 BlockBegin* default_sux = x->default_sux(); 2278 C1SwitchRange* range = new C1SwitchRange(low, sux); 2279 for (int i = 0; i < len; i++) { 2280 int key = low + i; 2281 BlockBegin* new_sux = x->sux_at(i); 2282 if (sux == new_sux) { 2283 // still in same range 2284 range->set_high_key(key); 2285 } else { 2286 // skip tests which explicitly dispatch to the default 2287 if (sux != default_sux) { 2288 res->append(range); 2289 } 2290 range = new C1SwitchRange(key, new_sux); 2291 } 2292 sux = new_sux; 2293 } 2294 if (res->length() == 0 || res->last() != range) res->append(range); 2295 } 2296 return res; 2297 } 2298 2299 2300 // we expect the keys to be sorted by increasing value 2301 SwitchRangeArray* LIRGenerator::create_lookup_ranges(LookupSwitch* x) { 2302 SwitchRangeList* res = new SwitchRangeList(); 2303 int len = x->length(); 2304 if (len > 0) { 2305 BlockBegin* default_sux = x->default_sux(); 2306 int key = x->key_at(0); 2307 BlockBegin* sux = x->sux_at(0); 2308 C1SwitchRange* range = new C1SwitchRange(key, sux); 2309 for (int i = 1; i < len; i++) { 2310 int new_key = x->key_at(i); 2311 BlockBegin* new_sux = x->sux_at(i); 2312 if (key+1 == new_key && sux == new_sux) { 2313 // still in same range 2314 range->set_high_key(new_key); 2315 } else { 2316 // skip tests which explicitly dispatch to the default 2317 if (range->sux() != default_sux) { 2318 res->append(range); 2319 } 2320 range = new C1SwitchRange(new_key, new_sux); 2321 } 2322 key = new_key; 2323 sux = new_sux; 2324 } 2325 if (res->length() == 0 || res->last() != range) res->append(range); 2326 } 2327 return res; 2328 } 2329 2330 2331 void LIRGenerator::do_TableSwitch(TableSwitch* x) { 2332 LIRItem tag(x->tag(), this); 2333 tag.load_item(); 2334 set_no_result(x); 2335 2336 if (x->is_safepoint()) { 2337 __ safepoint(safepoint_poll_register(), state_for(x, x->state_before())); 2338 } 2339 2340 // move values into phi locations 2341 move_to_phi(x->state()); 2342 2343 int lo_key = x->lo_key(); 2344 int len = x->length(); 2345 assert(lo_key <= (lo_key + (len - 1)), "integer overflow"); 2346 LIR_Opr value = tag.result(); 2347 2348 if (compilation()->env()->comp_level() == CompLevel_full_profile && UseSwitchProfiling) { 2349 ciMethod* method = x->state()->scope()->method(); 2350 ciMethodData* md = method->method_data_or_null(); 2351 assert(md != nullptr, "Sanity"); 2352 ciProfileData* data = md->bci_to_data(x->state()->bci()); 2353 assert(data != nullptr, "must have profiling data"); 2354 assert(data->is_MultiBranchData(), "bad profile data?"); 2355 int default_count_offset = md->byte_offset_of_slot(data, MultiBranchData::default_count_offset()); 2356 LIR_Opr md_reg = new_register(T_METADATA); 2357 __ metadata2reg(md->constant_encoding(), md_reg); 2358 LIR_Opr data_offset_reg = new_pointer_register(); 2359 LIR_Opr tmp_reg = new_pointer_register(); 2360 2361 __ move(LIR_OprFact::intptrConst(default_count_offset), data_offset_reg); 2362 for (int i = 0; i < len; i++) { 2363 int count_offset = md->byte_offset_of_slot(data, MultiBranchData::case_count_offset(i)); 2364 __ cmp(lir_cond_equal, value, i + lo_key); 2365 __ move(data_offset_reg, tmp_reg); 2366 __ cmove(lir_cond_equal, 2367 LIR_OprFact::intptrConst(count_offset), 2368 tmp_reg, 2369 data_offset_reg, T_INT); 2370 } 2371 2372 LIR_Opr data_reg = new_pointer_register(); 2373 LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type()); 2374 __ move(data_addr, data_reg); 2375 __ add(data_reg, LIR_OprFact::intptrConst(1), data_reg); 2376 __ move(data_reg, data_addr); 2377 } 2378 2379 if (UseTableRanges) { 2380 do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux()); 2381 } else { 2382 for (int i = 0; i < len; i++) { 2383 __ cmp(lir_cond_equal, value, i + lo_key); 2384 __ branch(lir_cond_equal, x->sux_at(i)); 2385 } 2386 __ jump(x->default_sux()); 2387 } 2388 } 2389 2390 2391 void LIRGenerator::do_LookupSwitch(LookupSwitch* x) { 2392 LIRItem tag(x->tag(), this); 2393 tag.load_item(); 2394 set_no_result(x); 2395 2396 if (x->is_safepoint()) { 2397 __ safepoint(safepoint_poll_register(), state_for(x, x->state_before())); 2398 } 2399 2400 // move values into phi locations 2401 move_to_phi(x->state()); 2402 2403 LIR_Opr value = tag.result(); 2404 int len = x->length(); 2405 2406 if (compilation()->env()->comp_level() == CompLevel_full_profile && UseSwitchProfiling) { 2407 ciMethod* method = x->state()->scope()->method(); 2408 ciMethodData* md = method->method_data_or_null(); 2409 assert(md != nullptr, "Sanity"); 2410 ciProfileData* data = md->bci_to_data(x->state()->bci()); 2411 assert(data != nullptr, "must have profiling data"); 2412 assert(data->is_MultiBranchData(), "bad profile data?"); 2413 int default_count_offset = md->byte_offset_of_slot(data, MultiBranchData::default_count_offset()); 2414 LIR_Opr md_reg = new_register(T_METADATA); 2415 __ metadata2reg(md->constant_encoding(), md_reg); 2416 LIR_Opr data_offset_reg = new_pointer_register(); 2417 LIR_Opr tmp_reg = new_pointer_register(); 2418 2419 __ move(LIR_OprFact::intptrConst(default_count_offset), data_offset_reg); 2420 for (int i = 0; i < len; i++) { 2421 int count_offset = md->byte_offset_of_slot(data, MultiBranchData::case_count_offset(i)); 2422 __ cmp(lir_cond_equal, value, x->key_at(i)); 2423 __ move(data_offset_reg, tmp_reg); 2424 __ cmove(lir_cond_equal, 2425 LIR_OprFact::intptrConst(count_offset), 2426 tmp_reg, 2427 data_offset_reg, T_INT); 2428 } 2429 2430 LIR_Opr data_reg = new_pointer_register(); 2431 LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type()); 2432 __ move(data_addr, data_reg); 2433 __ add(data_reg, LIR_OprFact::intptrConst(1), data_reg); 2434 __ move(data_reg, data_addr); 2435 } 2436 2437 if (UseTableRanges) { 2438 do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux()); 2439 } else { 2440 int len = x->length(); 2441 for (int i = 0; i < len; i++) { 2442 __ cmp(lir_cond_equal, value, x->key_at(i)); 2443 __ branch(lir_cond_equal, x->sux_at(i)); 2444 } 2445 __ jump(x->default_sux()); 2446 } 2447 } 2448 2449 2450 void LIRGenerator::do_Goto(Goto* x) { 2451 set_no_result(x); 2452 2453 if (block()->next()->as_OsrEntry()) { 2454 // need to free up storage used for OSR entry point 2455 LIR_Opr osrBuffer = block()->next()->operand(); 2456 BasicTypeList signature; 2457 signature.append(NOT_LP64(T_INT) LP64_ONLY(T_LONG)); // pass a pointer to osrBuffer 2458 CallingConvention* cc = frame_map()->c_calling_convention(&signature); 2459 __ move(osrBuffer, cc->args()->at(0)); 2460 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end), 2461 getThreadTemp(), LIR_OprFact::illegalOpr, cc->args()); 2462 } 2463 2464 if (x->is_safepoint()) { 2465 ValueStack* state = x->state_before() ? x->state_before() : x->state(); 2466 2467 // increment backedge counter if needed 2468 CodeEmitInfo* info = state_for(x, state); 2469 increment_backedge_counter(info, x->profiled_bci()); 2470 CodeEmitInfo* safepoint_info = state_for(x, state); 2471 __ safepoint(safepoint_poll_register(), safepoint_info); 2472 } 2473 2474 // Gotos can be folded Ifs, handle this case. 2475 if (x->should_profile()) { 2476 ciMethod* method = x->profiled_method(); 2477 assert(method != nullptr, "method should be set if branch is profiled"); 2478 ciMethodData* md = method->method_data_or_null(); 2479 assert(md != nullptr, "Sanity"); 2480 ciProfileData* data = md->bci_to_data(x->profiled_bci()); 2481 assert(data != nullptr, "must have profiling data"); 2482 int offset; 2483 if (x->direction() == Goto::taken) { 2484 assert(data->is_BranchData(), "need BranchData for two-way branches"); 2485 offset = md->byte_offset_of_slot(data, BranchData::taken_offset()); 2486 } else if (x->direction() == Goto::not_taken) { 2487 assert(data->is_BranchData(), "need BranchData for two-way branches"); 2488 offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset()); 2489 } else { 2490 assert(data->is_JumpData(), "need JumpData for branches"); 2491 offset = md->byte_offset_of_slot(data, JumpData::taken_offset()); 2492 } 2493 LIR_Opr md_reg = new_register(T_METADATA); 2494 __ metadata2reg(md->constant_encoding(), md_reg); 2495 2496 increment_counter(new LIR_Address(md_reg, offset, 2497 NOT_LP64(T_INT) LP64_ONLY(T_LONG)), DataLayout::counter_increment); 2498 } 2499 2500 // emit phi-instruction move after safepoint since this simplifies 2501 // describing the state as the safepoint. 2502 move_to_phi(x->state()); 2503 2504 __ jump(x->default_sux()); 2505 } 2506 2507 /** 2508 * Emit profiling code if needed for arguments, parameters, return value types 2509 * 2510 * @param md MDO the code will update at runtime 2511 * @param md_base_offset common offset in the MDO for this profile and subsequent ones 2512 * @param md_offset offset in the MDO (on top of md_base_offset) for this profile 2513 * @param profiled_k current profile 2514 * @param obj IR node for the object to be profiled 2515 * @param mdp register to hold the pointer inside the MDO (md + md_base_offset). 2516 * Set once we find an update to make and use for next ones. 2517 * @param not_null true if we know obj cannot be null 2518 * @param signature_at_call_k signature at call for obj 2519 * @param callee_signature_k signature of callee for obj 2520 * at call and callee signatures differ at method handle call 2521 * @return the only klass we know will ever be seen at this profile point 2522 */ 2523 ciKlass* LIRGenerator::profile_type(ciMethodData* md, int md_base_offset, int md_offset, intptr_t profiled_k, 2524 Value obj, LIR_Opr& mdp, bool not_null, ciKlass* signature_at_call_k, 2525 ciKlass* callee_signature_k) { 2526 ciKlass* result = nullptr; 2527 bool do_null = !not_null && !TypeEntries::was_null_seen(profiled_k); 2528 bool do_update = !TypeEntries::is_type_unknown(profiled_k); 2529 // known not to be null or null bit already set and already set to 2530 // unknown: nothing we can do to improve profiling 2531 if (!do_null && !do_update) { 2532 return result; 2533 } 2534 2535 ciKlass* exact_klass = nullptr; 2536 Compilation* comp = Compilation::current(); 2537 if (do_update) { 2538 // try to find exact type, using CHA if possible, so that loading 2539 // the klass from the object can be avoided 2540 ciType* type = obj->exact_type(); 2541 if (type == nullptr) { 2542 type = obj->declared_type(); 2543 type = comp->cha_exact_type(type); 2544 } 2545 assert(type == nullptr || type->is_klass(), "type should be class"); 2546 exact_klass = (type != nullptr && type->is_loaded()) ? (ciKlass*)type : nullptr; 2547 2548 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass; 2549 } 2550 2551 if (!do_null && !do_update) { 2552 return result; 2553 } 2554 2555 ciKlass* exact_signature_k = nullptr; 2556 if (do_update) { 2557 // Is the type from the signature exact (the only one possible)? 2558 exact_signature_k = signature_at_call_k->exact_klass(); 2559 if (exact_signature_k == nullptr) { 2560 exact_signature_k = comp->cha_exact_type(signature_at_call_k); 2561 } else { 2562 result = exact_signature_k; 2563 // Known statically. No need to emit any code: prevent 2564 // LIR_Assembler::emit_profile_type() from emitting useless code 2565 profiled_k = ciTypeEntries::with_status(result, profiled_k); 2566 } 2567 // exact_klass and exact_signature_k can be both non null but 2568 // different if exact_klass is loaded after the ciObject for 2569 // exact_signature_k is created. 2570 if (exact_klass == nullptr && exact_signature_k != nullptr && exact_klass != exact_signature_k) { 2571 // sometimes the type of the signature is better than the best type 2572 // the compiler has 2573 exact_klass = exact_signature_k; 2574 } 2575 if (callee_signature_k != nullptr && 2576 callee_signature_k != signature_at_call_k) { 2577 ciKlass* improved_klass = callee_signature_k->exact_klass(); 2578 if (improved_klass == nullptr) { 2579 improved_klass = comp->cha_exact_type(callee_signature_k); 2580 } 2581 if (exact_klass == nullptr && improved_klass != nullptr && exact_klass != improved_klass) { 2582 exact_klass = exact_signature_k; 2583 } 2584 } 2585 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass; 2586 } 2587 2588 if (!do_null && !do_update) { 2589 return result; 2590 } 2591 2592 if (mdp == LIR_OprFact::illegalOpr) { 2593 mdp = new_register(T_METADATA); 2594 __ metadata2reg(md->constant_encoding(), mdp); 2595 if (md_base_offset != 0) { 2596 LIR_Address* base_type_address = new LIR_Address(mdp, md_base_offset, T_ADDRESS); 2597 mdp = new_pointer_register(); 2598 __ leal(LIR_OprFact::address(base_type_address), mdp); 2599 } 2600 } 2601 LIRItem value(obj, this); 2602 value.load_item(); 2603 __ profile_type(new LIR_Address(mdp, md_offset, T_METADATA), 2604 value.result(), exact_klass, profiled_k, new_pointer_register(), not_null, exact_signature_k != nullptr); 2605 return result; 2606 } 2607 2608 // profile parameters on entry to the root of the compilation 2609 void LIRGenerator::profile_parameters(Base* x) { 2610 if (compilation()->profile_parameters()) { 2611 CallingConvention* args = compilation()->frame_map()->incoming_arguments(); 2612 ciMethodData* md = scope()->method()->method_data_or_null(); 2613 assert(md != nullptr, "Sanity"); 2614 2615 if (md->parameters_type_data() != nullptr) { 2616 ciParametersTypeData* parameters_type_data = md->parameters_type_data(); 2617 ciTypeStackSlotEntries* parameters = parameters_type_data->parameters(); 2618 LIR_Opr mdp = LIR_OprFact::illegalOpr; 2619 for (int java_index = 0, i = 0, j = 0; j < parameters_type_data->number_of_parameters(); i++) { 2620 LIR_Opr src = args->at(i); 2621 assert(!src->is_illegal(), "check"); 2622 BasicType t = src->type(); 2623 if (is_reference_type(t)) { 2624 intptr_t profiled_k = parameters->type(j); 2625 Local* local = x->state()->local_at(java_index)->as_Local(); 2626 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)), 2627 in_bytes(ParametersTypeData::type_offset(j)) - in_bytes(ParametersTypeData::type_offset(0)), 2628 profiled_k, local, mdp, false, local->declared_type()->as_klass(), nullptr); 2629 // If the profile is known statically set it once for all and do not emit any code 2630 if (exact != nullptr) { 2631 md->set_parameter_type(j, exact); 2632 } 2633 j++; 2634 } 2635 java_index += type2size[t]; 2636 } 2637 } 2638 } 2639 } 2640 2641 void LIRGenerator::do_Base(Base* x) { 2642 __ std_entry(LIR_OprFact::illegalOpr); 2643 // Emit moves from physical registers / stack slots to virtual registers 2644 CallingConvention* args = compilation()->frame_map()->incoming_arguments(); 2645 IRScope* irScope = compilation()->hir()->top_scope(); 2646 int java_index = 0; 2647 for (int i = 0; i < args->length(); i++) { 2648 LIR_Opr src = args->at(i); 2649 assert(!src->is_illegal(), "check"); 2650 BasicType t = src->type(); 2651 2652 // Types which are smaller than int are passed as int, so 2653 // correct the type which passed. 2654 switch (t) { 2655 case T_BYTE: 2656 case T_BOOLEAN: 2657 case T_SHORT: 2658 case T_CHAR: 2659 t = T_INT; 2660 break; 2661 default: 2662 break; 2663 } 2664 2665 LIR_Opr dest = new_register(t); 2666 __ move(src, dest); 2667 2668 // Assign new location to Local instruction for this local 2669 Local* local = x->state()->local_at(java_index)->as_Local(); 2670 assert(local != nullptr, "Locals for incoming arguments must have been created"); 2671 #ifndef __SOFTFP__ 2672 // The java calling convention passes double as long and float as int. 2673 assert(as_ValueType(t)->tag() == local->type()->tag(), "check"); 2674 #endif // __SOFTFP__ 2675 local->set_operand(dest); 2676 _instruction_for_operand.at_put_grow(dest->vreg_number(), local, nullptr); 2677 java_index += type2size[t]; 2678 } 2679 2680 if (compilation()->env()->dtrace_method_probes()) { 2681 BasicTypeList signature; 2682 signature.append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread 2683 signature.append(T_METADATA); // Method* 2684 LIR_OprList* args = new LIR_OprList(); 2685 args->append(getThreadPointer()); 2686 LIR_Opr meth = new_register(T_METADATA); 2687 __ metadata2reg(method()->constant_encoding(), meth); 2688 args->append(meth); 2689 call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), voidType, nullptr); 2690 } 2691 2692 if (method()->is_synchronized()) { 2693 LIR_Opr obj; 2694 if (method()->is_static()) { 2695 obj = new_register(T_OBJECT); 2696 __ oop2reg(method()->holder()->java_mirror()->constant_encoding(), obj); 2697 } else { 2698 Local* receiver = x->state()->local_at(0)->as_Local(); 2699 assert(receiver != nullptr, "must already exist"); 2700 obj = receiver->operand(); 2701 } 2702 assert(obj->is_valid(), "must be valid"); 2703 2704 if (method()->is_synchronized() && GenerateSynchronizationCode) { 2705 LIR_Opr lock = syncLockOpr(); 2706 __ load_stack_address_monitor(0, lock); 2707 2708 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, x->check_flag(Instruction::DeoptimizeOnException)); 2709 CodeStub* slow_path = new MonitorEnterStub(obj, lock, info); 2710 2711 // receiver is guaranteed non-null so don't need CodeEmitInfo 2712 __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, nullptr); 2713 } 2714 } 2715 // increment invocation counters if needed 2716 if (!method()->is_accessor()) { // Accessors do not have MDOs, so no counting. 2717 profile_parameters(x); 2718 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, false); 2719 increment_invocation_counter(info); 2720 } 2721 2722 // all blocks with a successor must end with an unconditional jump 2723 // to the successor even if they are consecutive 2724 __ jump(x->default_sux()); 2725 } 2726 2727 2728 void LIRGenerator::do_OsrEntry(OsrEntry* x) { 2729 // construct our frame and model the production of incoming pointer 2730 // to the OSR buffer. 2731 __ osr_entry(LIR_Assembler::osrBufferPointer()); 2732 LIR_Opr result = rlock_result(x); 2733 __ move(LIR_Assembler::osrBufferPointer(), result); 2734 } 2735 2736 2737 void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) { 2738 assert(args->length() == arg_list->length(), 2739 "args=%d, arg_list=%d", args->length(), arg_list->length()); 2740 for (int i = x->has_receiver() ? 1 : 0; i < args->length(); i++) { 2741 LIRItem* param = args->at(i); 2742 LIR_Opr loc = arg_list->at(i); 2743 if (loc->is_register()) { 2744 param->load_item_force(loc); 2745 } else { 2746 LIR_Address* addr = loc->as_address_ptr(); 2747 param->load_for_store(addr->type()); 2748 if (addr->type() == T_OBJECT) { 2749 __ move_wide(param->result(), addr); 2750 } else 2751 __ move(param->result(), addr); 2752 } 2753 } 2754 2755 if (x->has_receiver()) { 2756 LIRItem* receiver = args->at(0); 2757 LIR_Opr loc = arg_list->at(0); 2758 if (loc->is_register()) { 2759 receiver->load_item_force(loc); 2760 } else { 2761 assert(loc->is_address(), "just checking"); 2762 receiver->load_for_store(T_OBJECT); 2763 __ move_wide(receiver->result(), loc->as_address_ptr()); 2764 } 2765 } 2766 } 2767 2768 2769 // Visits all arguments, returns appropriate items without loading them 2770 LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) { 2771 LIRItemList* argument_items = new LIRItemList(); 2772 if (x->has_receiver()) { 2773 LIRItem* receiver = new LIRItem(x->receiver(), this); 2774 argument_items->append(receiver); 2775 } 2776 for (int i = 0; i < x->number_of_arguments(); i++) { 2777 LIRItem* param = new LIRItem(x->argument_at(i), this); 2778 argument_items->append(param); 2779 } 2780 return argument_items; 2781 } 2782 2783 2784 // The invoke with receiver has following phases: 2785 // a) traverse and load/lock receiver; 2786 // b) traverse all arguments -> item-array (invoke_visit_argument) 2787 // c) push receiver on stack 2788 // d) load each of the items and push on stack 2789 // e) unlock receiver 2790 // f) move receiver into receiver-register %o0 2791 // g) lock result registers and emit call operation 2792 // 2793 // Before issuing a call, we must spill-save all values on stack 2794 // that are in caller-save register. "spill-save" moves those registers 2795 // either in a free callee-save register or spills them if no free 2796 // callee save register is available. 2797 // 2798 // The problem is where to invoke spill-save. 2799 // - if invoked between e) and f), we may lock callee save 2800 // register in "spill-save" that destroys the receiver register 2801 // before f) is executed 2802 // - if we rearrange f) to be earlier (by loading %o0) it 2803 // may destroy a value on the stack that is currently in %o0 2804 // and is waiting to be spilled 2805 // - if we keep the receiver locked while doing spill-save, 2806 // we cannot spill it as it is spill-locked 2807 // 2808 void LIRGenerator::do_Invoke(Invoke* x) { 2809 CallingConvention* cc = frame_map()->java_calling_convention(x->signature(), true); 2810 2811 LIR_OprList* arg_list = cc->args(); 2812 LIRItemList* args = invoke_visit_arguments(x); 2813 LIR_Opr receiver = LIR_OprFact::illegalOpr; 2814 2815 // setup result register 2816 LIR_Opr result_register = LIR_OprFact::illegalOpr; 2817 if (x->type() != voidType) { 2818 result_register = result_register_for(x->type()); 2819 } 2820 2821 CodeEmitInfo* info = state_for(x, x->state()); 2822 2823 invoke_load_arguments(x, args, arg_list); 2824 2825 if (x->has_receiver()) { 2826 args->at(0)->load_item_force(LIR_Assembler::receiverOpr()); 2827 receiver = args->at(0)->result(); 2828 } 2829 2830 // emit invoke code 2831 assert(receiver->is_illegal() || receiver->is_equal(LIR_Assembler::receiverOpr()), "must match"); 2832 2833 // JSR 292 2834 // Preserve the SP over MethodHandle call sites, if needed. 2835 ciMethod* target = x->target(); 2836 bool is_method_handle_invoke = (// %%% FIXME: Are both of these relevant? 2837 target->is_method_handle_intrinsic() || 2838 target->is_compiled_lambda_form()); 2839 if (is_method_handle_invoke) { 2840 info->set_is_method_handle_invoke(true); 2841 if(FrameMap::method_handle_invoke_SP_save_opr() != LIR_OprFact::illegalOpr) { 2842 __ move(FrameMap::stack_pointer(), FrameMap::method_handle_invoke_SP_save_opr()); 2843 } 2844 } 2845 2846 switch (x->code()) { 2847 case Bytecodes::_invokestatic: 2848 __ call_static(target, result_register, 2849 SharedRuntime::get_resolve_static_call_stub(), 2850 arg_list, info); 2851 break; 2852 case Bytecodes::_invokespecial: 2853 case Bytecodes::_invokevirtual: 2854 case Bytecodes::_invokeinterface: 2855 // for loaded and final (method or class) target we still produce an inline cache, 2856 // in order to be able to call mixed mode 2857 if (x->code() == Bytecodes::_invokespecial || x->target_is_final()) { 2858 __ call_opt_virtual(target, receiver, result_register, 2859 SharedRuntime::get_resolve_opt_virtual_call_stub(), 2860 arg_list, info); 2861 } else { 2862 __ call_icvirtual(target, receiver, result_register, 2863 SharedRuntime::get_resolve_virtual_call_stub(), 2864 arg_list, info); 2865 } 2866 break; 2867 case Bytecodes::_invokedynamic: { 2868 __ call_dynamic(target, receiver, result_register, 2869 SharedRuntime::get_resolve_static_call_stub(), 2870 arg_list, info); 2871 break; 2872 } 2873 default: 2874 fatal("unexpected bytecode: %s", Bytecodes::name(x->code())); 2875 break; 2876 } 2877 2878 // JSR 292 2879 // Restore the SP after MethodHandle call sites, if needed. 2880 if (is_method_handle_invoke 2881 && FrameMap::method_handle_invoke_SP_save_opr() != LIR_OprFact::illegalOpr) { 2882 __ move(FrameMap::method_handle_invoke_SP_save_opr(), FrameMap::stack_pointer()); 2883 } 2884 2885 if (result_register->is_valid()) { 2886 LIR_Opr result = rlock_result(x); 2887 __ move(result_register, result); 2888 } 2889 } 2890 2891 2892 void LIRGenerator::do_FPIntrinsics(Intrinsic* x) { 2893 assert(x->number_of_arguments() == 1, "wrong type"); 2894 LIRItem value (x->argument_at(0), this); 2895 LIR_Opr reg = rlock_result(x); 2896 value.load_item(); 2897 LIR_Opr tmp = force_to_spill(value.result(), as_BasicType(x->type())); 2898 __ move(tmp, reg); 2899 } 2900 2901 2902 2903 // Code for : x->x() {x->cond()} x->y() ? x->tval() : x->fval() 2904 void LIRGenerator::do_IfOp(IfOp* x) { 2905 #ifdef ASSERT 2906 { 2907 ValueTag xtag = x->x()->type()->tag(); 2908 ValueTag ttag = x->tval()->type()->tag(); 2909 assert(xtag == intTag || xtag == objectTag, "cannot handle others"); 2910 assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others"); 2911 assert(ttag == x->fval()->type()->tag(), "cannot handle others"); 2912 } 2913 #endif 2914 2915 LIRItem left(x->x(), this); 2916 LIRItem right(x->y(), this); 2917 left.load_item(); 2918 if (can_inline_as_constant(right.value())) { 2919 right.dont_load_item(); 2920 } else { 2921 right.load_item(); 2922 } 2923 2924 LIRItem t_val(x->tval(), this); 2925 LIRItem f_val(x->fval(), this); 2926 t_val.dont_load_item(); 2927 f_val.dont_load_item(); 2928 LIR_Opr reg = rlock_result(x); 2929 2930 __ cmp(lir_cond(x->cond()), left.result(), right.result()); 2931 __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg, as_BasicType(x->x()->type())); 2932 } 2933 2934 void LIRGenerator::do_RuntimeCall(address routine, Intrinsic* x) { 2935 assert(x->number_of_arguments() == 0, "wrong type"); 2936 // Enforce computation of _reserved_argument_area_size which is required on some platforms. 2937 BasicTypeList signature; 2938 CallingConvention* cc = frame_map()->c_calling_convention(&signature); 2939 LIR_Opr reg = result_register_for(x->type()); 2940 __ call_runtime_leaf(routine, getThreadTemp(), 2941 reg, new LIR_OprList()); 2942 LIR_Opr result = rlock_result(x); 2943 __ move(reg, result); 2944 } 2945 2946 2947 2948 void LIRGenerator::do_Intrinsic(Intrinsic* x) { 2949 switch (x->id()) { 2950 case vmIntrinsics::_intBitsToFloat : 2951 case vmIntrinsics::_doubleToRawLongBits : 2952 case vmIntrinsics::_longBitsToDouble : 2953 case vmIntrinsics::_floatToRawIntBits : { 2954 do_FPIntrinsics(x); 2955 break; 2956 } 2957 2958 #ifdef JFR_HAVE_INTRINSICS 2959 case vmIntrinsics::_counterTime: 2960 do_RuntimeCall(CAST_FROM_FN_PTR(address, JfrTime::time_function()), x); 2961 break; 2962 #endif 2963 2964 case vmIntrinsics::_currentTimeMillis: 2965 do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeMillis), x); 2966 break; 2967 2968 case vmIntrinsics::_nanoTime: 2969 do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeNanos), x); 2970 break; 2971 2972 case vmIntrinsics::_Object_init: do_RegisterFinalizer(x); break; 2973 case vmIntrinsics::_isInstance: do_isInstance(x); break; 2974 case vmIntrinsics::_isPrimitive: do_isPrimitive(x); break; 2975 case vmIntrinsics::_getModifiers: do_getModifiers(x); break; 2976 case vmIntrinsics::_getClass: do_getClass(x); break; 2977 case vmIntrinsics::_getObjectSize: do_getObjectSize(x); break; 2978 case vmIntrinsics::_currentCarrierThread: do_currentCarrierThread(x); break; 2979 case vmIntrinsics::_currentThread: do_vthread(x); break; 2980 case vmIntrinsics::_scopedValueCache: do_scopedValueCache(x); break; 2981 case vmIntrinsics::_shipilev_magic_sizeOf: do_sizeOf(x); break; 2982 case vmIntrinsics::_shipilev_magic_addressOf: do_addressOf(x); break; 2983 2984 case vmIntrinsics::_dlog: // fall through 2985 case vmIntrinsics::_dlog10: // fall through 2986 case vmIntrinsics::_dabs: // fall through 2987 case vmIntrinsics::_dsqrt: // fall through 2988 case vmIntrinsics::_dsqrt_strict: // fall through 2989 case vmIntrinsics::_dtan: // fall through 2990 case vmIntrinsics::_dsin : // fall through 2991 case vmIntrinsics::_dcos : // fall through 2992 case vmIntrinsics::_dexp : // fall through 2993 case vmIntrinsics::_dpow : do_MathIntrinsic(x); break; 2994 case vmIntrinsics::_arraycopy: do_ArrayCopy(x); break; 2995 2996 case vmIntrinsics::_fmaD: do_FmaIntrinsic(x); break; 2997 case vmIntrinsics::_fmaF: do_FmaIntrinsic(x); break; 2998 2999 // Use java.lang.Math intrinsics code since it works for these intrinsics too. 3000 case vmIntrinsics::_floatToFloat16: // fall through 3001 case vmIntrinsics::_float16ToFloat: do_MathIntrinsic(x); break; 3002 3003 case vmIntrinsics::_Preconditions_checkIndex: 3004 do_PreconditionsCheckIndex(x, T_INT); 3005 break; 3006 case vmIntrinsics::_Preconditions_checkLongIndex: 3007 do_PreconditionsCheckIndex(x, T_LONG); 3008 break; 3009 3010 case vmIntrinsics::_compareAndSetReference: 3011 do_CompareAndSwap(x, objectType); 3012 break; 3013 case vmIntrinsics::_compareAndSetInt: 3014 do_CompareAndSwap(x, intType); 3015 break; 3016 case vmIntrinsics::_compareAndSetLong: 3017 do_CompareAndSwap(x, longType); 3018 break; 3019 3020 case vmIntrinsics::_loadFence : 3021 __ membar_acquire(); 3022 break; 3023 case vmIntrinsics::_storeFence: 3024 __ membar_release(); 3025 break; 3026 case vmIntrinsics::_storeStoreFence: 3027 __ membar_storestore(); 3028 break; 3029 case vmIntrinsics::_fullFence : 3030 __ membar(); 3031 break; 3032 case vmIntrinsics::_onSpinWait: 3033 __ on_spin_wait(); 3034 break; 3035 case vmIntrinsics::_Reference_get: 3036 do_Reference_get(x); 3037 break; 3038 3039 case vmIntrinsics::_updateCRC32: 3040 case vmIntrinsics::_updateBytesCRC32: 3041 case vmIntrinsics::_updateByteBufferCRC32: 3042 do_update_CRC32(x); 3043 break; 3044 3045 case vmIntrinsics::_updateBytesCRC32C: 3046 case vmIntrinsics::_updateDirectByteBufferCRC32C: 3047 do_update_CRC32C(x); 3048 break; 3049 3050 case vmIntrinsics::_vectorizedMismatch: 3051 do_vectorizedMismatch(x); 3052 break; 3053 3054 case vmIntrinsics::_blackhole: 3055 do_blackhole(x); 3056 break; 3057 3058 default: ShouldNotReachHere(); break; 3059 } 3060 } 3061 3062 void LIRGenerator::profile_arguments(ProfileCall* x) { 3063 if (compilation()->profile_arguments()) { 3064 int bci = x->bci_of_invoke(); 3065 ciMethodData* md = x->method()->method_data_or_null(); 3066 assert(md != nullptr, "Sanity"); 3067 ciProfileData* data = md->bci_to_data(bci); 3068 if (data != nullptr) { 3069 if ((data->is_CallTypeData() && data->as_CallTypeData()->has_arguments()) || 3070 (data->is_VirtualCallTypeData() && data->as_VirtualCallTypeData()->has_arguments())) { 3071 ByteSize extra = data->is_CallTypeData() ? CallTypeData::args_data_offset() : VirtualCallTypeData::args_data_offset(); 3072 int base_offset = md->byte_offset_of_slot(data, extra); 3073 LIR_Opr mdp = LIR_OprFact::illegalOpr; 3074 ciTypeStackSlotEntries* args = data->is_CallTypeData() ? ((ciCallTypeData*)data)->args() : ((ciVirtualCallTypeData*)data)->args(); 3075 3076 Bytecodes::Code bc = x->method()->java_code_at_bci(bci); 3077 int start = 0; 3078 int stop = data->is_CallTypeData() ? ((ciCallTypeData*)data)->number_of_arguments() : ((ciVirtualCallTypeData*)data)->number_of_arguments(); 3079 if (x->callee()->is_loaded() && x->callee()->is_static() && Bytecodes::has_receiver(bc)) { 3080 // first argument is not profiled at call (method handle invoke) 3081 assert(x->method()->raw_code_at_bci(bci) == Bytecodes::_invokehandle, "invokehandle expected"); 3082 start = 1; 3083 } 3084 ciSignature* callee_signature = x->callee()->signature(); 3085 // method handle call to virtual method 3086 bool has_receiver = x->callee()->is_loaded() && !x->callee()->is_static() && !Bytecodes::has_receiver(bc); 3087 ciSignatureStream callee_signature_stream(callee_signature, has_receiver ? x->callee()->holder() : nullptr); 3088 3089 bool ignored_will_link; 3090 ciSignature* signature_at_call = nullptr; 3091 x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call); 3092 ciSignatureStream signature_at_call_stream(signature_at_call); 3093 3094 // if called through method handle invoke, some arguments may have been popped 3095 for (int i = 0; i < stop && i+start < x->nb_profiled_args(); i++) { 3096 int off = in_bytes(TypeEntriesAtCall::argument_type_offset(i)) - in_bytes(TypeEntriesAtCall::args_data_offset()); 3097 ciKlass* exact = profile_type(md, base_offset, off, 3098 args->type(i), x->profiled_arg_at(i+start), mdp, 3099 !x->arg_needs_null_check(i+start), 3100 signature_at_call_stream.next_klass(), callee_signature_stream.next_klass()); 3101 if (exact != nullptr) { 3102 md->set_argument_type(bci, i, exact); 3103 } 3104 } 3105 } else { 3106 #ifdef ASSERT 3107 Bytecodes::Code code = x->method()->raw_code_at_bci(x->bci_of_invoke()); 3108 int n = x->nb_profiled_args(); 3109 assert(MethodData::profile_parameters() && (MethodData::profile_arguments_jsr292_only() || 3110 (x->inlined() && ((code == Bytecodes::_invokedynamic && n <= 1) || (code == Bytecodes::_invokehandle && n <= 2)))), 3111 "only at JSR292 bytecodes"); 3112 #endif 3113 } 3114 } 3115 } 3116 } 3117 3118 // profile parameters on entry to an inlined method 3119 void LIRGenerator::profile_parameters_at_call(ProfileCall* x) { 3120 if (compilation()->profile_parameters() && x->inlined()) { 3121 ciMethodData* md = x->callee()->method_data_or_null(); 3122 if (md != nullptr) { 3123 ciParametersTypeData* parameters_type_data = md->parameters_type_data(); 3124 if (parameters_type_data != nullptr) { 3125 ciTypeStackSlotEntries* parameters = parameters_type_data->parameters(); 3126 LIR_Opr mdp = LIR_OprFact::illegalOpr; 3127 bool has_receiver = !x->callee()->is_static(); 3128 ciSignature* sig = x->callee()->signature(); 3129 ciSignatureStream sig_stream(sig, has_receiver ? x->callee()->holder() : nullptr); 3130 int i = 0; // to iterate on the Instructions 3131 Value arg = x->recv(); 3132 bool not_null = false; 3133 int bci = x->bci_of_invoke(); 3134 Bytecodes::Code bc = x->method()->java_code_at_bci(bci); 3135 // The first parameter is the receiver so that's what we start 3136 // with if it exists. One exception is method handle call to 3137 // virtual method: the receiver is in the args list 3138 if (arg == nullptr || !Bytecodes::has_receiver(bc)) { 3139 i = 1; 3140 arg = x->profiled_arg_at(0); 3141 not_null = !x->arg_needs_null_check(0); 3142 } 3143 int k = 0; // to iterate on the profile data 3144 for (;;) { 3145 intptr_t profiled_k = parameters->type(k); 3146 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)), 3147 in_bytes(ParametersTypeData::type_offset(k)) - in_bytes(ParametersTypeData::type_offset(0)), 3148 profiled_k, arg, mdp, not_null, sig_stream.next_klass(), nullptr); 3149 // If the profile is known statically set it once for all and do not emit any code 3150 if (exact != nullptr) { 3151 md->set_parameter_type(k, exact); 3152 } 3153 k++; 3154 if (k >= parameters_type_data->number_of_parameters()) { 3155 #ifdef ASSERT 3156 int extra = 0; 3157 if (MethodData::profile_arguments() && TypeProfileParmsLimit != -1 && 3158 x->nb_profiled_args() >= TypeProfileParmsLimit && 3159 x->recv() != nullptr && Bytecodes::has_receiver(bc)) { 3160 extra += 1; 3161 } 3162 assert(i == x->nb_profiled_args() - extra || (TypeProfileParmsLimit != -1 && TypeProfileArgsLimit > TypeProfileParmsLimit), "unused parameters?"); 3163 #endif 3164 break; 3165 } 3166 arg = x->profiled_arg_at(i); 3167 not_null = !x->arg_needs_null_check(i); 3168 i++; 3169 } 3170 } 3171 } 3172 } 3173 } 3174 3175 void LIRGenerator::do_ProfileCall(ProfileCall* x) { 3176 // Need recv in a temporary register so it interferes with the other temporaries 3177 LIR_Opr recv = LIR_OprFact::illegalOpr; 3178 LIR_Opr mdo = new_register(T_METADATA); 3179 // tmp is used to hold the counters on SPARC 3180 LIR_Opr tmp = new_pointer_register(); 3181 3182 if (x->nb_profiled_args() > 0) { 3183 profile_arguments(x); 3184 } 3185 3186 // profile parameters on inlined method entry including receiver 3187 if (x->recv() != nullptr || x->nb_profiled_args() > 0) { 3188 profile_parameters_at_call(x); 3189 } 3190 3191 if (x->recv() != nullptr) { 3192 LIRItem value(x->recv(), this); 3193 value.load_item(); 3194 recv = new_register(T_OBJECT); 3195 __ move(value.result(), recv); 3196 } 3197 __ profile_call(x->method(), x->bci_of_invoke(), x->callee(), mdo, recv, tmp, x->known_holder()); 3198 } 3199 3200 void LIRGenerator::do_ProfileReturnType(ProfileReturnType* x) { 3201 int bci = x->bci_of_invoke(); 3202 ciMethodData* md = x->method()->method_data_or_null(); 3203 assert(md != nullptr, "Sanity"); 3204 ciProfileData* data = md->bci_to_data(bci); 3205 if (data != nullptr) { 3206 assert(data->is_CallTypeData() || data->is_VirtualCallTypeData(), "wrong profile data type"); 3207 ciReturnTypeEntry* ret = data->is_CallTypeData() ? ((ciCallTypeData*)data)->ret() : ((ciVirtualCallTypeData*)data)->ret(); 3208 LIR_Opr mdp = LIR_OprFact::illegalOpr; 3209 3210 bool ignored_will_link; 3211 ciSignature* signature_at_call = nullptr; 3212 x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call); 3213 3214 // The offset within the MDO of the entry to update may be too large 3215 // to be used in load/store instructions on some platforms. So have 3216 // profile_type() compute the address of the profile in a register. 3217 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(data, ret->type_offset()), 0, 3218 ret->type(), x->ret(), mdp, 3219 !x->needs_null_check(), 3220 signature_at_call->return_type()->as_klass(), 3221 x->callee()->signature()->return_type()->as_klass()); 3222 if (exact != nullptr) { 3223 md->set_return_type(bci, exact); 3224 } 3225 } 3226 } 3227 3228 void LIRGenerator::do_ProfileInvoke(ProfileInvoke* x) { 3229 // We can safely ignore accessors here, since c2 will inline them anyway, 3230 // accessors are also always mature. 3231 if (!x->inlinee()->is_accessor()) { 3232 CodeEmitInfo* info = state_for(x, x->state(), true); 3233 // Notify the runtime very infrequently only to take care of counter overflows 3234 int freq_log = Tier23InlineeNotifyFreqLog; 3235 double scale; 3236 if (_method->has_option_value(CompileCommand::CompileThresholdScaling, scale)) { 3237 freq_log = CompilerConfig::scaled_freq_log(freq_log, scale); 3238 } 3239 increment_event_counter_impl(info, x->inlinee(), LIR_OprFact::intConst(InvocationCounter::count_increment), right_n_bits(freq_log), InvocationEntryBci, false, true); 3240 } 3241 } 3242 3243 void LIRGenerator::increment_backedge_counter_conditionally(LIR_Condition cond, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info, int left_bci, int right_bci, int bci) { 3244 if (compilation()->is_profiling()) { 3245 #if defined(X86) && !defined(_LP64) 3246 // BEWARE! On 32-bit x86 cmp clobbers its left argument so we need a temp copy. 3247 LIR_Opr left_copy = new_register(left->type()); 3248 __ move(left, left_copy); 3249 __ cmp(cond, left_copy, right); 3250 #else 3251 __ cmp(cond, left, right); 3252 #endif 3253 LIR_Opr step = new_register(T_INT); 3254 LIR_Opr plus_one = LIR_OprFact::intConst(InvocationCounter::count_increment); 3255 LIR_Opr zero = LIR_OprFact::intConst(0); 3256 __ cmove(cond, 3257 (left_bci < bci) ? plus_one : zero, 3258 (right_bci < bci) ? plus_one : zero, 3259 step, left->type()); 3260 increment_backedge_counter(info, step, bci); 3261 } 3262 } 3263 3264 3265 void LIRGenerator::increment_event_counter(CodeEmitInfo* info, LIR_Opr step, int bci, bool backedge) { 3266 int freq_log = 0; 3267 int level = compilation()->env()->comp_level(); 3268 if (level == CompLevel_limited_profile) { 3269 freq_log = (backedge ? Tier2BackedgeNotifyFreqLog : Tier2InvokeNotifyFreqLog); 3270 } else if (level == CompLevel_full_profile) { 3271 freq_log = (backedge ? Tier3BackedgeNotifyFreqLog : Tier3InvokeNotifyFreqLog); 3272 } else { 3273 ShouldNotReachHere(); 3274 } 3275 // Increment the appropriate invocation/backedge counter and notify the runtime. 3276 double scale; 3277 if (_method->has_option_value(CompileCommand::CompileThresholdScaling, scale)) { 3278 freq_log = CompilerConfig::scaled_freq_log(freq_log, scale); 3279 } 3280 increment_event_counter_impl(info, info->scope()->method(), step, right_n_bits(freq_log), bci, backedge, true); 3281 } 3282 3283 void LIRGenerator::increment_event_counter_impl(CodeEmitInfo* info, 3284 ciMethod *method, LIR_Opr step, int frequency, 3285 int bci, bool backedge, bool notify) { 3286 assert(frequency == 0 || is_power_of_2(frequency + 1), "Frequency must be x^2 - 1 or 0"); 3287 int level = _compilation->env()->comp_level(); 3288 assert(level > CompLevel_simple, "Shouldn't be here"); 3289 3290 int offset = -1; 3291 LIR_Opr counter_holder; 3292 if (level == CompLevel_limited_profile) { 3293 MethodCounters* counters_adr = method->ensure_method_counters(); 3294 if (counters_adr == nullptr) { 3295 bailout("method counters allocation failed"); 3296 return; 3297 } 3298 counter_holder = new_pointer_register(); 3299 __ move(LIR_OprFact::intptrConst(counters_adr), counter_holder); 3300 offset = in_bytes(backedge ? MethodCounters::backedge_counter_offset() : 3301 MethodCounters::invocation_counter_offset()); 3302 } else if (level == CompLevel_full_profile) { 3303 counter_holder = new_register(T_METADATA); 3304 offset = in_bytes(backedge ? MethodData::backedge_counter_offset() : 3305 MethodData::invocation_counter_offset()); 3306 ciMethodData* md = method->method_data_or_null(); 3307 assert(md != nullptr, "Sanity"); 3308 __ metadata2reg(md->constant_encoding(), counter_holder); 3309 } else { 3310 ShouldNotReachHere(); 3311 } 3312 LIR_Address* counter = new LIR_Address(counter_holder, offset, T_INT); 3313 LIR_Opr result = new_register(T_INT); 3314 __ load(counter, result); 3315 __ add(result, step, result); 3316 __ store(result, counter); 3317 if (notify && (!backedge || UseOnStackReplacement)) { 3318 LIR_Opr meth = LIR_OprFact::metadataConst(method->constant_encoding()); 3319 // The bci for info can point to cmp for if's we want the if bci 3320 CodeStub* overflow = new CounterOverflowStub(info, bci, meth); 3321 int freq = frequency << InvocationCounter::count_shift; 3322 if (freq == 0) { 3323 if (!step->is_constant()) { 3324 __ cmp(lir_cond_notEqual, step, LIR_OprFact::intConst(0)); 3325 __ branch(lir_cond_notEqual, overflow); 3326 } else { 3327 __ branch(lir_cond_always, overflow); 3328 } 3329 } else { 3330 LIR_Opr mask = load_immediate(freq, T_INT); 3331 if (!step->is_constant()) { 3332 // If step is 0, make sure the overflow check below always fails 3333 __ cmp(lir_cond_notEqual, step, LIR_OprFact::intConst(0)); 3334 __ cmove(lir_cond_notEqual, result, LIR_OprFact::intConst(InvocationCounter::count_increment), result, T_INT); 3335 } 3336 __ logical_and(result, mask, result); 3337 __ cmp(lir_cond_equal, result, LIR_OprFact::intConst(0)); 3338 __ branch(lir_cond_equal, overflow); 3339 } 3340 __ branch_destination(overflow->continuation()); 3341 } 3342 } 3343 3344 void LIRGenerator::do_RuntimeCall(RuntimeCall* x) { 3345 LIR_OprList* args = new LIR_OprList(x->number_of_arguments()); 3346 BasicTypeList* signature = new BasicTypeList(x->number_of_arguments()); 3347 3348 if (x->pass_thread()) { 3349 signature->append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread 3350 args->append(getThreadPointer()); 3351 } 3352 3353 for (int i = 0; i < x->number_of_arguments(); i++) { 3354 Value a = x->argument_at(i); 3355 LIRItem* item = new LIRItem(a, this); 3356 item->load_item(); 3357 args->append(item->result()); 3358 signature->append(as_BasicType(a->type())); 3359 } 3360 3361 LIR_Opr result = call_runtime(signature, args, x->entry(), x->type(), nullptr); 3362 if (x->type() == voidType) { 3363 set_no_result(x); 3364 } else { 3365 __ move(result, rlock_result(x)); 3366 } 3367 } 3368 3369 #ifdef ASSERT 3370 void LIRGenerator::do_Assert(Assert *x) { 3371 ValueTag tag = x->x()->type()->tag(); 3372 If::Condition cond = x->cond(); 3373 3374 LIRItem xitem(x->x(), this); 3375 LIRItem yitem(x->y(), this); 3376 LIRItem* xin = &xitem; 3377 LIRItem* yin = &yitem; 3378 3379 assert(tag == intTag, "Only integer assertions are valid!"); 3380 3381 xin->load_item(); 3382 yin->dont_load_item(); 3383 3384 set_no_result(x); 3385 3386 LIR_Opr left = xin->result(); 3387 LIR_Opr right = yin->result(); 3388 3389 __ lir_assert(lir_cond(x->cond()), left, right, x->message(), true); 3390 } 3391 #endif 3392 3393 void LIRGenerator::do_RangeCheckPredicate(RangeCheckPredicate *x) { 3394 3395 3396 Instruction *a = x->x(); 3397 Instruction *b = x->y(); 3398 if (!a || StressRangeCheckElimination) { 3399 assert(!b || StressRangeCheckElimination, "B must also be null"); 3400 3401 CodeEmitInfo *info = state_for(x, x->state()); 3402 CodeStub* stub = new PredicateFailedStub(info); 3403 3404 __ jump(stub); 3405 } else if (a->type()->as_IntConstant() && b->type()->as_IntConstant()) { 3406 int a_int = a->type()->as_IntConstant()->value(); 3407 int b_int = b->type()->as_IntConstant()->value(); 3408 3409 bool ok = false; 3410 3411 switch(x->cond()) { 3412 case Instruction::eql: ok = (a_int == b_int); break; 3413 case Instruction::neq: ok = (a_int != b_int); break; 3414 case Instruction::lss: ok = (a_int < b_int); break; 3415 case Instruction::leq: ok = (a_int <= b_int); break; 3416 case Instruction::gtr: ok = (a_int > b_int); break; 3417 case Instruction::geq: ok = (a_int >= b_int); break; 3418 case Instruction::aeq: ok = ((unsigned int)a_int >= (unsigned int)b_int); break; 3419 case Instruction::beq: ok = ((unsigned int)a_int <= (unsigned int)b_int); break; 3420 default: ShouldNotReachHere(); 3421 } 3422 3423 if (ok) { 3424 3425 CodeEmitInfo *info = state_for(x, x->state()); 3426 CodeStub* stub = new PredicateFailedStub(info); 3427 3428 __ jump(stub); 3429 } 3430 } else { 3431 3432 ValueTag tag = x->x()->type()->tag(); 3433 If::Condition cond = x->cond(); 3434 LIRItem xitem(x->x(), this); 3435 LIRItem yitem(x->y(), this); 3436 LIRItem* xin = &xitem; 3437 LIRItem* yin = &yitem; 3438 3439 assert(tag == intTag, "Only integer deoptimizations are valid!"); 3440 3441 xin->load_item(); 3442 yin->dont_load_item(); 3443 set_no_result(x); 3444 3445 LIR_Opr left = xin->result(); 3446 LIR_Opr right = yin->result(); 3447 3448 CodeEmitInfo *info = state_for(x, x->state()); 3449 CodeStub* stub = new PredicateFailedStub(info); 3450 3451 __ cmp(lir_cond(cond), left, right); 3452 __ branch(lir_cond(cond), stub); 3453 } 3454 } 3455 3456 void LIRGenerator::do_blackhole(Intrinsic *x) { 3457 assert(!x->has_receiver(), "Should have been checked before: only static methods here"); 3458 for (int c = 0; c < x->number_of_arguments(); c++) { 3459 // Load the argument 3460 LIRItem vitem(x->argument_at(c), this); 3461 vitem.load_item(); 3462 // ...and leave it unused. 3463 } 3464 } 3465 3466 LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) { 3467 LIRItemList args(1); 3468 LIRItem value(arg1, this); 3469 args.append(&value); 3470 BasicTypeList signature; 3471 signature.append(as_BasicType(arg1->type())); 3472 3473 return call_runtime(&signature, &args, entry, result_type, info); 3474 } 3475 3476 3477 LIR_Opr LIRGenerator::call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info) { 3478 LIRItemList args(2); 3479 LIRItem value1(arg1, this); 3480 LIRItem value2(arg2, this); 3481 args.append(&value1); 3482 args.append(&value2); 3483 BasicTypeList signature; 3484 signature.append(as_BasicType(arg1->type())); 3485 signature.append(as_BasicType(arg2->type())); 3486 3487 return call_runtime(&signature, &args, entry, result_type, info); 3488 } 3489 3490 3491 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIR_OprList* args, 3492 address entry, ValueType* result_type, CodeEmitInfo* info) { 3493 // get a result register 3494 LIR_Opr phys_reg = LIR_OprFact::illegalOpr; 3495 LIR_Opr result = LIR_OprFact::illegalOpr; 3496 if (result_type->tag() != voidTag) { 3497 result = new_register(result_type); 3498 phys_reg = result_register_for(result_type); 3499 } 3500 3501 // move the arguments into the correct location 3502 CallingConvention* cc = frame_map()->c_calling_convention(signature); 3503 assert(cc->length() == args->length(), "argument mismatch"); 3504 for (int i = 0; i < args->length(); i++) { 3505 LIR_Opr arg = args->at(i); 3506 LIR_Opr loc = cc->at(i); 3507 if (loc->is_register()) { 3508 __ move(arg, loc); 3509 } else { 3510 LIR_Address* addr = loc->as_address_ptr(); 3511 // if (!can_store_as_constant(arg)) { 3512 // LIR_Opr tmp = new_register(arg->type()); 3513 // __ move(arg, tmp); 3514 // arg = tmp; 3515 // } 3516 __ move(arg, addr); 3517 } 3518 } 3519 3520 if (info) { 3521 __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info); 3522 } else { 3523 __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args()); 3524 } 3525 if (result->is_valid()) { 3526 __ move(phys_reg, result); 3527 } 3528 return result; 3529 } 3530 3531 3532 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIRItemList* args, 3533 address entry, ValueType* result_type, CodeEmitInfo* info) { 3534 // get a result register 3535 LIR_Opr phys_reg = LIR_OprFact::illegalOpr; 3536 LIR_Opr result = LIR_OprFact::illegalOpr; 3537 if (result_type->tag() != voidTag) { 3538 result = new_register(result_type); 3539 phys_reg = result_register_for(result_type); 3540 } 3541 3542 // move the arguments into the correct location 3543 CallingConvention* cc = frame_map()->c_calling_convention(signature); 3544 3545 assert(cc->length() == args->length(), "argument mismatch"); 3546 for (int i = 0; i < args->length(); i++) { 3547 LIRItem* arg = args->at(i); 3548 LIR_Opr loc = cc->at(i); 3549 if (loc->is_register()) { 3550 arg->load_item_force(loc); 3551 } else { 3552 LIR_Address* addr = loc->as_address_ptr(); 3553 arg->load_for_store(addr->type()); 3554 __ move(arg->result(), addr); 3555 } 3556 } 3557 3558 if (info) { 3559 __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info); 3560 } else { 3561 __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args()); 3562 } 3563 if (result->is_valid()) { 3564 __ move(phys_reg, result); 3565 } 3566 return result; 3567 } 3568 3569 void LIRGenerator::do_MemBar(MemBar* x) { 3570 LIR_Code code = x->code(); 3571 switch(code) { 3572 case lir_membar_acquire : __ membar_acquire(); break; 3573 case lir_membar_release : __ membar_release(); break; 3574 case lir_membar : __ membar(); break; 3575 case lir_membar_loadload : __ membar_loadload(); break; 3576 case lir_membar_storestore: __ membar_storestore(); break; 3577 case lir_membar_loadstore : __ membar_loadstore(); break; 3578 case lir_membar_storeload : __ membar_storeload(); break; 3579 default : ShouldNotReachHere(); break; 3580 } 3581 } 3582 3583 LIR_Opr LIRGenerator::mask_boolean(LIR_Opr array, LIR_Opr value, CodeEmitInfo*& null_check_info) { 3584 LIR_Opr value_fixed = rlock_byte(T_BYTE); 3585 if (two_operand_lir_form) { 3586 __ move(value, value_fixed); 3587 __ logical_and(value_fixed, LIR_OprFact::intConst(1), value_fixed); 3588 } else { 3589 __ logical_and(value, LIR_OprFact::intConst(1), value_fixed); 3590 } 3591 LIR_Opr klass = new_register(T_METADATA); 3592 load_klass(array, klass, null_check_info); 3593 null_check_info = nullptr; 3594 LIR_Opr layout = new_register(T_INT); 3595 __ move(new LIR_Address(klass, in_bytes(Klass::layout_helper_offset()), T_INT), layout); 3596 int diffbit = Klass::layout_helper_boolean_diffbit(); 3597 __ logical_and(layout, LIR_OprFact::intConst(diffbit), layout); 3598 __ cmp(lir_cond_notEqual, layout, LIR_OprFact::intConst(0)); 3599 __ cmove(lir_cond_notEqual, value_fixed, value, value_fixed, T_BYTE); 3600 value = value_fixed; 3601 return value; 3602 }