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 "c1/c1_Compilation.hpp"
26 #include "c1/c1_Defs.hpp"
27 #include "c1/c1_FrameMap.hpp"
28 #include "c1/c1_Instruction.hpp"
29 #include "c1/c1_LIRAssembler.hpp"
30 #include "c1/c1_LIRGenerator.hpp"
31 #include "c1/c1_ValueStack.hpp"
32 #include "ci/ciArrayKlass.hpp"
33 #include "ci/ciInstance.hpp"
34 #include "ci/ciObjArray.hpp"
35 #include "ci/ciUtilities.hpp"
36 #include "compiler/compilerDefinitions.inline.hpp"
37 #include "compiler/compilerOracle.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
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 #ifdef ASSERT
209 if (opr->is_virtual()) {
210 _gen->_instruction_for_operand.at_put_grow(opr->vreg_number(), value(), nullptr);
211 }
212 #endif
213
214 _result = opr;
215 }
216
217 void LIRItem::load_item() {
218 if (result()->is_illegal()) {
219 // update the items result
220 _result = value()->operand();
221 }
222 if (!result()->is_register()) {
223 LIR_Opr reg = _gen->new_register(value()->type());
224 __ move(result(), reg);
225 if (result()->is_constant()) {
226 _result = reg;
227 } else {
228 set_result(reg);
229 }
230 }
231 }
232
233
234 void LIRItem::load_for_store(BasicType type) {
235 if (_gen->can_store_as_constant(value(), type)) {
236 _result = value()->operand();
237 if (!_result->is_constant()) {
605 assert(right_op != result_op, "malformed");
606 __ move(left_op, result_op);
607 left_op = result_op;
608 }
609
610 switch(code) {
611 case Bytecodes::_iand:
612 case Bytecodes::_land: __ logical_and(left_op, right_op, result_op); break;
613
614 case Bytecodes::_ior:
615 case Bytecodes::_lor: __ logical_or(left_op, right_op, result_op); break;
616
617 case Bytecodes::_ixor:
618 case Bytecodes::_lxor: __ logical_xor(left_op, right_op, result_op); break;
619
620 default: ShouldNotReachHere();
621 }
622 }
623
624
625 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) {
626 // for slow path, use debug info for state after successful locking
627 CodeStub* slow_path = new MonitorEnterStub(object, lock, info);
628 __ load_stack_address_monitor(monitor_no, lock);
629 // for handling NullPointerException, use debug info representing just the lock stack before this monitorenter
630 __ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception);
631 }
632
633
634 void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {
635 // setup registers
636 LIR_Opr hdr = lock;
637 lock = new_hdr;
638 CodeStub* slow_path = new MonitorExitStub(lock, LockingMode != LM_MONITOR, monitor_no);
639 __ load_stack_address_monitor(monitor_no, lock);
640 __ unlock_object(hdr, object, lock, scratch, slow_path);
641 }
642
643 #ifndef PRODUCT
644 void LIRGenerator::print_if_not_loaded(const NewInstance* new_instance) {
645 if (PrintNotLoaded && !new_instance->klass()->is_loaded()) {
646 tty->print_cr(" ###class not loaded at new bci %d", new_instance->printable_bci());
647 } else if (PrintNotLoaded && (!CompilerConfig::is_c1_only_no_jvmci() && new_instance->is_unresolved())) {
648 tty->print_cr(" ###class not resolved at new bci %d", new_instance->printable_bci());
649 }
650 }
651 #endif
652
653 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) {
654 klass2reg_with_patching(klass_reg, klass, info, is_unresolved);
655 // If klass is not loaded we do not know if the klass has finalizers:
656 if (UseFastNewInstance && klass->is_loaded()
657 && !Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
658
659 StubId stub_id = klass->is_initialized() ? StubId::c1_fast_new_instance_id : StubId::c1_fast_new_instance_init_check_id;
660
661 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id);
662
663 assert(klass->is_loaded(), "must be loaded");
664 // allocate space for instance
665 assert(klass->size_helper() > 0, "illegal instance size");
666 const int instance_size = align_object_size(klass->size_helper());
667 __ allocate_object(dst, scratch1, scratch2, scratch3, scratch4,
668 oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path);
669 } else {
670 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, StubId::c1_new_instance_id);
671 __ branch(lir_cond_always, slow_path);
672 __ branch_destination(slow_path->continuation());
673 }
674 }
675
676
677 static bool is_constant_zero(Instruction* inst) {
678 IntConstant* c = inst->type()->as_IntConstant();
679 if (c) {
680 return (c->value() == 0);
681 }
682 return false;
683 }
684
685
686 static bool positive_constant(Instruction* inst) {
687 IntConstant* c = inst->type()->as_IntConstant();
688 if (c) {
689 return (c->value() >= 0);
690 }
691 return false;
751 if (src_type != nullptr) {
752 if (src_type->element_type()->is_subtype_of(dst_type->element_type())) {
753 is_exact = true;
754 expected_type = dst_type;
755 }
756 }
757 }
758 // at least pass along a good guess
759 if (expected_type == nullptr) expected_type = dst_exact_type;
760 if (expected_type == nullptr) expected_type = src_declared_type;
761 if (expected_type == nullptr) expected_type = dst_declared_type;
762
763 src_objarray = (src_exact_type && src_exact_type->is_obj_array_klass()) || (src_declared_type && src_declared_type->is_obj_array_klass());
764 dst_objarray = (dst_exact_type && dst_exact_type->is_obj_array_klass()) || (dst_declared_type && dst_declared_type->is_obj_array_klass());
765 }
766
767 // if a probable array type has been identified, figure out if any
768 // of the required checks for a fast case can be elided.
769 int flags = LIR_OpArrayCopy::all_flags;
770
771 if (!src_objarray)
772 flags &= ~LIR_OpArrayCopy::src_objarray;
773 if (!dst_objarray)
774 flags &= ~LIR_OpArrayCopy::dst_objarray;
775
776 if (!x->arg_needs_null_check(0))
777 flags &= ~LIR_OpArrayCopy::src_null_check;
778 if (!x->arg_needs_null_check(2))
779 flags &= ~LIR_OpArrayCopy::dst_null_check;
780
781
782 if (expected_type != nullptr) {
783 Value length_limit = nullptr;
784
785 IfOp* ifop = length->as_IfOp();
786 if (ifop != nullptr) {
787 // look for expressions like min(v, a.length) which ends up as
788 // x > y ? y : x or x >= y ? y : x
789 if ((ifop->cond() == If::gtr || ifop->cond() == If::geq) &&
790 ifop->x() == ifop->fval() &&
858 BasicType t = expected_type->element_type()->basic_type();
859 int element_size = type2aelembytes(t);
860 if (((arrayOopDesc::base_offset_in_bytes(t) + (uint)s_offs * element_size) % HeapWordSize == 0) &&
861 ((arrayOopDesc::base_offset_in_bytes(t) + (uint)d_offs * element_size) % HeapWordSize == 0)) {
862 flags &= ~LIR_OpArrayCopy::unaligned;
863 }
864 }
865 } else if (src_pos == dst_pos || is_constant_zero(dst_pos)) {
866 // src and dest positions are the same, or dst is zero so assume
867 // nonoverlapping copy.
868 flags &= ~LIR_OpArrayCopy::overlapping;
869 }
870
871 if (src == dst) {
872 // moving within a single array so no type checks are needed
873 if (flags & LIR_OpArrayCopy::type_check) {
874 flags &= ~LIR_OpArrayCopy::type_check;
875 }
876 }
877 *flagsp = flags;
878 *expected_typep = (ciArrayKlass*)expected_type;
879 }
880
881
882 LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) {
883 assert(type2size[t] == type2size[value->type()],
884 "size mismatch: t=%s, value->type()=%s", type2name(t), type2name(value->type()));
885 if (!value->is_register()) {
886 // force into a register
887 LIR_Opr r = new_register(value->type());
888 __ move(value, r);
889 value = r;
890 }
891
892 // create a spill location
893 LIR_Opr tmp = new_register(t);
894 set_vreg_flag(tmp, LIRGenerator::must_start_in_memory);
895
896 // move from register to spill
897 __ move(value, tmp);
1443 }
1444 return _vreg_flags.at(vreg_num, f);
1445 }
1446
1447
1448 // Block local constant handling. This code is useful for keeping
1449 // unpinned constants and constants which aren't exposed in the IR in
1450 // registers. Unpinned Constant instructions have their operands
1451 // cleared when the block is finished so that other blocks can't end
1452 // up referring to their registers.
1453
1454 LIR_Opr LIRGenerator::load_constant(Constant* x) {
1455 assert(!x->is_pinned(), "only for unpinned constants");
1456 _unpinned_constants.append(x);
1457 return load_constant(LIR_OprFact::value_type(x->type())->as_constant_ptr());
1458 }
1459
1460
1461 LIR_Opr LIRGenerator::load_constant(LIR_Const* c) {
1462 BasicType t = c->type();
1463 for (int i = 0; i < _constants.length(); i++) {
1464 LIR_Const* other = _constants.at(i);
1465 if (t == other->type()) {
1466 switch (t) {
1467 case T_INT:
1468 case T_FLOAT:
1469 if (c->as_jint_bits() != other->as_jint_bits()) continue;
1470 break;
1471 case T_LONG:
1472 case T_DOUBLE:
1473 if (c->as_jint_hi_bits() != other->as_jint_hi_bits()) continue;
1474 if (c->as_jint_lo_bits() != other->as_jint_lo_bits()) continue;
1475 break;
1476 case T_OBJECT:
1477 if (c->as_jobject() != other->as_jobject()) continue;
1478 break;
1479 default:
1480 break;
1481 }
1482 return _reg_for_constants.at(i);
1483 }
1484 }
1485
1486 LIR_Opr result = new_register(t);
1487 __ move((LIR_Opr)c, result);
1488 _constants.append(c);
1489 _reg_for_constants.append(result);
1490 return result;
1491 }
1492
1493 //------------------------field access--------------------------------------
1494
1495 void LIRGenerator::do_CompareAndSwap(Intrinsic* x, ValueType* type) {
1496 assert(x->number_of_arguments() == 4, "wrong type");
1497 LIRItem obj (x->argument_at(0), this); // object
1498 LIRItem offset(x->argument_at(1), this); // offset of field
1499 LIRItem cmp (x->argument_at(2), this); // value to compare with field
1500 LIRItem val (x->argument_at(3), this); // replace field with val if matches cmp
1501 assert(obj.type()->tag() == objectTag, "invalid type");
1502 assert(cmp.type()->tag() == type->tag(), "invalid type");
1503 assert(val.type()->tag() == type->tag(), "invalid type");
1504
1505 LIR_Opr result = access_atomic_cmpxchg_at(IN_HEAP, as_BasicType(type),
1506 obj, offset, cmp, val);
1507 set_result(x, result);
1508 }
1509
1510 // Comment copied form templateTable_i486.cpp
1511 // ----------------------------------------------------------------------------
1512 // Volatile variables demand their effects be made known to all CPU's in
1513 // order. Store buffers on most chips allow reads & writes to reorder; the
1514 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
1515 // memory barrier (i.e., it's not sufficient that the interpreter does not
1516 // reorder volatile references, the hardware also must not reorder them).
1517 //
1518 // According to the new Java Memory Model (JMM):
1519 // (1) All volatiles are serialized wrt to each other.
1520 // ALSO reads & writes act as acquire & release, so:
1521 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
1522 // the read float up to before the read. It's OK for non-volatile memory refs
1523 // that happen before the volatile read to float down below it.
1524 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
1525 // that happen BEFORE the write float down to after the write. It's OK for
1526 // non-volatile memory refs that happen after the volatile write to float up
1527 // before it.
1528 //
1529 // We only put in barriers around volatile refs (they are expensive), not
1530 // _between_ memory refs (that would require us to track the flavor of the
1531 // previous memory refs). Requirements (2) and (3) require some barriers
1532 // before volatile stores and after volatile loads. These nearly cover
1533 // requirement (1) but miss the volatile-store-volatile-load case. This final
1534 // case is placed after volatile-stores although it could just as well go
1535 // before volatile-loads.
1536
1537
1538 void LIRGenerator::do_StoreField(StoreField* x) {
1539 bool needs_patching = x->needs_patching();
1540 bool is_volatile = x->field()->is_volatile();
1541 BasicType field_type = x->field_type();
1542
1543 CodeEmitInfo* info = nullptr;
1544 if (needs_patching) {
1545 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access");
1546 info = state_for(x, x->state_before());
1547 } else if (x->needs_null_check()) {
1548 NullCheck* nc = x->explicit_null_check();
1549 if (nc == nullptr) {
1550 info = state_for(x);
1551 } else {
1552 info = state_for(nc);
1553 }
1554 }
1555
1556 LIRItem object(x->obj(), this);
1557 LIRItem value(x->value(), this);
1558
1559 object.load_item();
1560
1561 if (is_volatile || needs_patching) {
1562 // load item if field is volatile (fewer special cases for volatiles)
1563 // load item if field not initialized
1564 // load item if field not constant
1565 // because of code patching we cannot inline constants
1566 if (field_type == T_BYTE || field_type == T_BOOLEAN) {
1567 value.load_byte_item();
1568 } else {
1569 value.load_item();
1570 }
1571 } else {
1572 value.load_for_store(field_type);
1573 }
1574
1575 set_no_result(x);
1576
1577 #ifndef PRODUCT
1578 if (PrintNotLoaded && needs_patching) {
1579 tty->print_cr(" ###class not loaded at store_%s bci %d",
1580 x->is_static() ? "static" : "field", x->printable_bci());
1581 }
1582 #endif
1583
1584 if (x->needs_null_check() &&
1585 (needs_patching ||
1586 MacroAssembler::needs_explicit_null_check(x->offset()))) {
1587 // Emit an explicit null check because the offset is too large.
1588 // If the class is not loaded and the object is null, we need to deoptimize to throw a
1589 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
1590 __ null_check(object.result(), new CodeEmitInfo(info), /* deoptimize */ needs_patching);
1591 }
1592
1593 DecoratorSet decorators = IN_HEAP;
1594 if (is_volatile) {
1595 decorators |= MO_SEQ_CST;
1596 }
1597 if (needs_patching) {
1598 decorators |= C1_NEEDS_PATCHING;
1599 }
1600
1601 access_store_at(decorators, field_type, object, LIR_OprFact::intConst(x->offset()),
1602 value.result(), info != nullptr ? new CodeEmitInfo(info) : nullptr, info);
1603 }
1604
1605 void LIRGenerator::do_StoreIndexed(StoreIndexed* x) {
1606 assert(x->is_pinned(),"");
1607 bool needs_range_check = x->compute_needs_range_check();
1608 bool use_length = x->length() != nullptr;
1609 bool obj_store = is_reference_type(x->elt_type());
1610 bool needs_store_check = obj_store && (x->value()->as_Constant() == nullptr ||
1611 !get_jobject_constant(x->value())->is_null_object() ||
1612 x->should_profile());
1613
1614 LIRItem array(x->array(), this);
1615 LIRItem index(x->index(), this);
1616 LIRItem value(x->value(), this);
1617 LIRItem length(this);
1618
1619 array.load_item();
1620 index.load_nonconstant();
1621
1622 if (use_length && needs_range_check) {
1623 length.set_instruction(x->length());
1624 length.load_item();
1625
1626 }
1627 if (needs_store_check || x->check_boolean()) {
1628 value.load_item();
1629 } else {
1630 value.load_for_store(x->elt_type());
1631 }
1632
1633 set_no_result(x);
1634
1635 // the CodeEmitInfo must be duplicated for each different
1636 // LIR-instruction because spilling can occur anywhere between two
1637 // instructions and so the debug information must be different
1638 CodeEmitInfo* range_check_info = state_for(x);
1639 CodeEmitInfo* null_check_info = nullptr;
1640 if (x->needs_null_check()) {
1641 null_check_info = new CodeEmitInfo(range_check_info);
1642 }
1643
1644 if (needs_range_check) {
1645 if (use_length) {
1646 __ cmp(lir_cond_belowEqual, length.result(), index.result());
1647 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result()));
1648 } else {
1649 array_range_check(array.result(), index.result(), null_check_info, range_check_info);
1650 // range_check also does the null check
1651 null_check_info = nullptr;
1652 }
1653 }
1654
1655 if (GenerateArrayStoreCheck && needs_store_check) {
1656 CodeEmitInfo* store_check_info = new CodeEmitInfo(range_check_info);
1657 array_store_check(value.result(), array.result(), store_check_info, x->profiled_method(), x->profiled_bci());
1658 }
1659
1660 DecoratorSet decorators = IN_HEAP | IS_ARRAY;
1661 if (x->check_boolean()) {
1662 decorators |= C1_MASK_BOOLEAN;
1663 }
1664
1665 access_store_at(decorators, x->elt_type(), array, index.result(), value.result(),
1666 nullptr, null_check_info);
1667 }
1668
1669 void LIRGenerator::access_load_at(DecoratorSet decorators, BasicType type,
1670 LIRItem& base, LIR_Opr offset, LIR_Opr result,
1671 CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) {
1672 decorators |= ACCESS_READ;
1673 LIRAccess access(this, decorators, base, offset, type, patch_info, load_emit_info);
1674 if (access.is_raw()) {
1675 _barrier_set->BarrierSetC1::load_at(access, result);
1676 } else {
1677 _barrier_set->load_at(access, result);
1678 }
1679 }
1680
1681 void LIRGenerator::access_load(DecoratorSet decorators, BasicType type,
1682 LIR_Opr addr, LIR_Opr result) {
1683 decorators |= ACCESS_READ;
1684 LIRAccess access(this, decorators, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, type);
1685 access.set_resolved_addr(addr);
1686 if (access.is_raw()) {
1687 _barrier_set->BarrierSetC1::load(access, result);
1688 } else {
1689 _barrier_set->load(access, result);
1690 }
1691 }
1692
1693 void LIRGenerator::access_store_at(DecoratorSet decorators, BasicType type,
1694 LIRItem& base, LIR_Opr offset, LIR_Opr value,
1695 CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info) {
1696 decorators |= ACCESS_WRITE;
1697 LIRAccess access(this, decorators, base, offset, type, patch_info, store_emit_info);
1698 if (access.is_raw()) {
1699 _barrier_set->BarrierSetC1::store_at(access, value);
1700 } else {
1701 _barrier_set->store_at(access, value);
1702 }
1703 }
1704
1705 LIR_Opr LIRGenerator::access_atomic_cmpxchg_at(DecoratorSet decorators, BasicType type,
1706 LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value) {
1707 decorators |= ACCESS_READ;
1708 decorators |= ACCESS_WRITE;
1709 // Atomic operations are SEQ_CST by default
1710 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0;
1711 LIRAccess access(this, decorators, base, offset, type);
1712 if (access.is_raw()) {
1713 return _barrier_set->BarrierSetC1::atomic_cmpxchg_at(access, cmp_value, new_value);
1714 } else {
1715 return _barrier_set->atomic_cmpxchg_at(access, cmp_value, new_value);
1716 }
1717 }
1728 } else {
1729 return _barrier_set->atomic_xchg_at(access, value);
1730 }
1731 }
1732
1733 LIR_Opr LIRGenerator::access_atomic_add_at(DecoratorSet decorators, BasicType type,
1734 LIRItem& base, LIRItem& offset, LIRItem& value) {
1735 decorators |= ACCESS_READ;
1736 decorators |= ACCESS_WRITE;
1737 // Atomic operations are SEQ_CST by default
1738 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0;
1739 LIRAccess access(this, decorators, base, offset, type);
1740 if (access.is_raw()) {
1741 return _barrier_set->BarrierSetC1::atomic_add_at(access, value);
1742 } else {
1743 return _barrier_set->atomic_add_at(access, value);
1744 }
1745 }
1746
1747 void LIRGenerator::do_LoadField(LoadField* x) {
1748 bool needs_patching = x->needs_patching();
1749 bool is_volatile = x->field()->is_volatile();
1750 BasicType field_type = x->field_type();
1751
1752 CodeEmitInfo* info = nullptr;
1753 if (needs_patching) {
1754 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access");
1755 info = state_for(x, x->state_before());
1756 } else if (x->needs_null_check()) {
1757 NullCheck* nc = x->explicit_null_check();
1758 if (nc == nullptr) {
1759 info = state_for(x);
1760 } else {
1761 info = state_for(nc);
1762 }
1763 }
1764
1765 LIRItem object(x->obj(), this);
1766
1767 object.load_item();
1768
1769 #ifndef PRODUCT
1780 stress_deopt)) {
1781 LIR_Opr obj = object.result();
1782 if (stress_deopt) {
1783 obj = new_register(T_OBJECT);
1784 __ move(LIR_OprFact::oopConst(nullptr), obj);
1785 }
1786 // Emit an explicit null check because the offset is too large.
1787 // If the class is not loaded and the object is null, we need to deoptimize to throw a
1788 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
1789 __ null_check(obj, new CodeEmitInfo(info), /* deoptimize */ needs_patching);
1790 }
1791
1792 DecoratorSet decorators = IN_HEAP;
1793 if (is_volatile) {
1794 decorators |= MO_SEQ_CST;
1795 }
1796 if (needs_patching) {
1797 decorators |= C1_NEEDS_PATCHING;
1798 }
1799
1800 LIR_Opr result = rlock_result(x, field_type);
1801 access_load_at(decorators, field_type,
1802 object, LIR_OprFact::intConst(x->offset()), result,
1803 info ? new CodeEmitInfo(info) : nullptr, info);
1804 }
1805
1806 // int/long jdk.internal.util.Preconditions.checkIndex
1807 void LIRGenerator::do_PreconditionsCheckIndex(Intrinsic* x, BasicType type) {
1808 assert(x->number_of_arguments() == 3, "wrong type");
1809 LIRItem index(x->argument_at(0), this);
1810 LIRItem length(x->argument_at(1), this);
1811 LIRItem oobef(x->argument_at(2), this);
1812
1813 index.load_item();
1814 length.load_item();
1815 oobef.load_item();
1816
1817 LIR_Opr result = rlock_result(x);
1818 // x->state() is created from copy_state_for_exception, it does not contains arguments
1819 // we should prepare them before entering into interpreter mode due to deoptimization.
1928 __ move(LIR_OprFact::oopConst(nullptr), obj);
1929 __ null_check(obj, new CodeEmitInfo(null_check_info));
1930 }
1931 }
1932
1933 if (needs_range_check) {
1934 if (StressLoopInvariantCodeMotion && range_check_info->deoptimize_on_exception()) {
1935 __ branch(lir_cond_always, new RangeCheckStub(range_check_info, index.result(), array.result()));
1936 } else if (use_length) {
1937 // TODO: use a (modified) version of array_range_check that does not require a
1938 // constant length to be loaded to a register
1939 __ cmp(lir_cond_belowEqual, length.result(), index.result());
1940 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result()));
1941 } else {
1942 array_range_check(array.result(), index.result(), null_check_info, range_check_info);
1943 // The range check performs the null check, so clear it out for the load
1944 null_check_info = nullptr;
1945 }
1946 }
1947
1948 DecoratorSet decorators = IN_HEAP | IS_ARRAY;
1949
1950 LIR_Opr result = rlock_result(x, x->elt_type());
1951 access_load_at(decorators, x->elt_type(),
1952 array, index.result(), result,
1953 nullptr, null_check_info);
1954 }
1955
1956
1957 void LIRGenerator::do_NullCheck(NullCheck* x) {
1958 if (x->can_trap()) {
1959 LIRItem value(x->obj(), this);
1960 value.load_item();
1961 CodeEmitInfo* info = state_for(x);
1962 __ null_check(value.result(), info);
1963 }
1964 }
1965
1966
1967 void LIRGenerator::do_TypeCast(TypeCast* x) {
1968 LIRItem value(x->obj(), this);
1969 value.load_item();
1970 // the result is the same as from the node we are casting
1971 set_result(x, value.result());
1972 }
1973
2408 bool do_update = !TypeEntries::is_type_unknown(profiled_k);
2409 // known not to be null or null bit already set and already set to
2410 // unknown: nothing we can do to improve profiling
2411 if (!do_null && !do_update) {
2412 return result;
2413 }
2414
2415 ciKlass* exact_klass = nullptr;
2416 Compilation* comp = Compilation::current();
2417 if (do_update) {
2418 // try to find exact type, using CHA if possible, so that loading
2419 // the klass from the object can be avoided
2420 ciType* type = obj->exact_type();
2421 if (type == nullptr) {
2422 type = obj->declared_type();
2423 type = comp->cha_exact_type(type);
2424 }
2425 assert(type == nullptr || type->is_klass(), "type should be class");
2426 exact_klass = (type != nullptr && type->is_loaded()) ? (ciKlass*)type : nullptr;
2427
2428 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2429 }
2430
2431 if (!do_null && !do_update) {
2432 return result;
2433 }
2434
2435 ciKlass* exact_signature_k = nullptr;
2436 if (do_update) {
2437 // Is the type from the signature exact (the only one possible)?
2438 exact_signature_k = signature_at_call_k->exact_klass();
2439 if (exact_signature_k == nullptr) {
2440 exact_signature_k = comp->cha_exact_type(signature_at_call_k);
2441 } else {
2442 result = exact_signature_k;
2443 // Known statically. No need to emit any code: prevent
2444 // LIR_Assembler::emit_profile_type() from emitting useless code
2445 profiled_k = ciTypeEntries::with_status(result, profiled_k);
2446 }
2447 // exact_klass and exact_signature_k can be both non null but
2448 // different if exact_klass is loaded after the ciObject for
2449 // exact_signature_k is created.
2450 if (exact_klass == nullptr && exact_signature_k != nullptr && exact_klass != exact_signature_k) {
2451 // sometimes the type of the signature is better than the best type
2452 // the compiler has
2453 exact_klass = exact_signature_k;
2454 }
2455 if (callee_signature_k != nullptr &&
2456 callee_signature_k != signature_at_call_k) {
2457 ciKlass* improved_klass = callee_signature_k->exact_klass();
2458 if (improved_klass == nullptr) {
2459 improved_klass = comp->cha_exact_type(callee_signature_k);
2460 }
2461 if (exact_klass == nullptr && improved_klass != nullptr && exact_klass != improved_klass) {
2462 exact_klass = exact_signature_k;
2463 }
2464 }
2465 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2466 }
2467
2468 if (!do_null && !do_update) {
2469 return result;
2470 }
2471
2472 if (mdp == LIR_OprFact::illegalOpr) {
2473 mdp = new_register(T_METADATA);
2474 __ metadata2reg(md->constant_encoding(), mdp);
2475 if (md_base_offset != 0) {
2476 LIR_Address* base_type_address = new LIR_Address(mdp, md_base_offset, T_ADDRESS);
2477 mdp = new_pointer_register();
2478 __ leal(LIR_OprFact::address(base_type_address), mdp);
2479 }
2480 }
2481 LIRItem value(obj, this);
2482 value.load_item();
2483 __ profile_type(new LIR_Address(mdp, md_offset, T_METADATA),
2484 value.result(), exact_klass, profiled_k, new_pointer_register(), not_null, exact_signature_k != nullptr);
2501 assert(!src->is_illegal(), "check");
2502 BasicType t = src->type();
2503 if (is_reference_type(t)) {
2504 intptr_t profiled_k = parameters->type(j);
2505 Local* local = x->state()->local_at(java_index)->as_Local();
2506 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),
2507 in_bytes(ParametersTypeData::type_offset(j)) - in_bytes(ParametersTypeData::type_offset(0)),
2508 profiled_k, local, mdp, false, local->declared_type()->as_klass(), nullptr);
2509 // If the profile is known statically set it once for all and do not emit any code
2510 if (exact != nullptr) {
2511 md->set_parameter_type(j, exact);
2512 }
2513 j++;
2514 }
2515 java_index += type2size[t];
2516 }
2517 }
2518 }
2519 }
2520
2521 void LIRGenerator::do_Base(Base* x) {
2522 __ std_entry(LIR_OprFact::illegalOpr);
2523 // Emit moves from physical registers / stack slots to virtual registers
2524 CallingConvention* args = compilation()->frame_map()->incoming_arguments();
2525 IRScope* irScope = compilation()->hir()->top_scope();
2526 int java_index = 0;
2527 for (int i = 0; i < args->length(); i++) {
2528 LIR_Opr src = args->at(i);
2529 assert(!src->is_illegal(), "check");
2530 BasicType t = src->type();
2531
2532 // Types which are smaller than int are passed as int, so
2533 // correct the type which passed.
2534 switch (t) {
2535 case T_BYTE:
2536 case T_BOOLEAN:
2537 case T_SHORT:
2538 case T_CHAR:
2539 t = T_INT;
2540 break;
2583 }
2584 assert(obj->is_valid(), "must be valid");
2585
2586 if (method()->is_synchronized()) {
2587 LIR_Opr lock = syncLockOpr();
2588 __ load_stack_address_monitor(0, lock);
2589
2590 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, x->check_flag(Instruction::DeoptimizeOnException));
2591 CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);
2592
2593 // receiver is guaranteed non-null so don't need CodeEmitInfo
2594 __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, nullptr);
2595 }
2596 }
2597 // increment invocation counters if needed
2598 if (!method()->is_accessor()) { // Accessors do not have MDOs, so no counting.
2599 profile_parameters(x);
2600 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, false);
2601 increment_invocation_counter(info);
2602 }
2603
2604 // all blocks with a successor must end with an unconditional jump
2605 // to the successor even if they are consecutive
2606 __ jump(x->default_sux());
2607 }
2608
2609
2610 void LIRGenerator::do_OsrEntry(OsrEntry* x) {
2611 // construct our frame and model the production of incoming pointer
2612 // to the OSR buffer.
2613 __ osr_entry(LIR_Assembler::osrBufferPointer());
2614 LIR_Opr result = rlock_result(x);
2615 __ move(LIR_Assembler::osrBufferPointer(), result);
2616 }
2617
2618
2619 void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {
2620 assert(args->length() == arg_list->length(),
2621 "args=%d, arg_list=%d", args->length(), arg_list->length());
2622 for (int i = x->has_receiver() ? 1 : 0; i < args->length(); i++) {
2623 LIRItem* param = args->at(i);
2624 LIR_Opr loc = arg_list->at(i);
2625 if (loc->is_register()) {
2626 param->load_item_force(loc);
2627 } else {
2628 LIR_Address* addr = loc->as_address_ptr();
2629 param->load_for_store(addr->type());
2630 if (addr->type() == T_OBJECT) {
2631 __ move_wide(param->result(), addr);
2632 } else
2633 __ move(param->result(), addr);
2634 }
2635 }
2636
2637 if (x->has_receiver()) {
2638 LIRItem* receiver = args->at(0);
2639 LIR_Opr loc = arg_list->at(0);
2640 if (loc->is_register()) {
2641 receiver->load_item_force(loc);
2642 } else {
2643 assert(loc->is_address(), "just checking");
2644 receiver->load_for_store(T_OBJECT);
2645 __ move_wide(receiver->result(), loc->as_address_ptr());
2646 }
2647 }
2648 }
2649
2650
2651 // Visits all arguments, returns appropriate items without loading them
2652 LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {
2653 LIRItemList* argument_items = new LIRItemList();
2654 if (x->has_receiver()) {
2780 __ move(tmp, reg);
2781 }
2782
2783
2784
2785 // Code for : x->x() {x->cond()} x->y() ? x->tval() : x->fval()
2786 void LIRGenerator::do_IfOp(IfOp* x) {
2787 #ifdef ASSERT
2788 {
2789 ValueTag xtag = x->x()->type()->tag();
2790 ValueTag ttag = x->tval()->type()->tag();
2791 assert(xtag == intTag || xtag == objectTag, "cannot handle others");
2792 assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");
2793 assert(ttag == x->fval()->type()->tag(), "cannot handle others");
2794 }
2795 #endif
2796
2797 LIRItem left(x->x(), this);
2798 LIRItem right(x->y(), this);
2799 left.load_item();
2800 if (can_inline_as_constant(right.value())) {
2801 right.dont_load_item();
2802 } else {
2803 right.load_item();
2804 }
2805
2806 LIRItem t_val(x->tval(), this);
2807 LIRItem f_val(x->fval(), this);
2808 t_val.dont_load_item();
2809 f_val.dont_load_item();
2810 LIR_Opr reg = rlock_result(x);
2811
2812 __ cmp(lir_cond(x->cond()), left.result(), right.result());
2813 __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg, as_BasicType(x->x()->type()));
2814 }
2815
2816 void LIRGenerator::do_RuntimeCall(address routine, Intrinsic* x) {
2817 assert(x->number_of_arguments() == 0, "wrong type");
2818 // Enforce computation of _reserved_argument_area_size which is required on some platforms.
2819 BasicTypeList signature;
2820 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
2821 LIR_Opr reg = result_register_for(x->type());
2822 __ call_runtime_leaf(routine, getThreadTemp(),
2823 reg, new LIR_OprList());
2824 LIR_Opr result = rlock_result(x);
2825 __ move(reg, result);
2826 }
2827
2828
2829
2830 void LIRGenerator::do_Intrinsic(Intrinsic* x) {
2831 switch (x->id()) {
2832 case vmIntrinsics::_intBitsToFloat :
2833 case vmIntrinsics::_doubleToRawLongBits :
3068 if (x->recv() != nullptr || x->nb_profiled_args() > 0) {
3069 profile_parameters_at_call(x);
3070 }
3071
3072 if (x->recv() != nullptr) {
3073 LIRItem value(x->recv(), this);
3074 value.load_item();
3075 recv = new_register(T_OBJECT);
3076 __ move(value.result(), recv);
3077 }
3078 __ profile_call(x->method(), x->bci_of_invoke(), x->callee(), mdo, recv, tmp, x->known_holder());
3079 }
3080
3081 void LIRGenerator::do_ProfileReturnType(ProfileReturnType* x) {
3082 int bci = x->bci_of_invoke();
3083 ciMethodData* md = x->method()->method_data_or_null();
3084 assert(md != nullptr, "Sanity");
3085 ciProfileData* data = md->bci_to_data(bci);
3086 if (data != nullptr) {
3087 assert(data->is_CallTypeData() || data->is_VirtualCallTypeData(), "wrong profile data type");
3088 ciReturnTypeEntry* ret = data->is_CallTypeData() ? ((ciCallTypeData*)data)->ret() : ((ciVirtualCallTypeData*)data)->ret();
3089 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3090
3091 bool ignored_will_link;
3092 ciSignature* signature_at_call = nullptr;
3093 x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);
3094
3095 // The offset within the MDO of the entry to update may be too large
3096 // to be used in load/store instructions on some platforms. So have
3097 // profile_type() compute the address of the profile in a register.
3098 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(data, ret->type_offset()), 0,
3099 ret->type(), x->ret(), mdp,
3100 !x->needs_null_check(),
3101 signature_at_call->return_type()->as_klass(),
3102 x->callee()->signature()->return_type()->as_klass());
3103 if (exact != nullptr) {
3104 md->set_return_type(bci, exact);
3105 }
3106 }
3107 }
3108
3109 void LIRGenerator::do_ProfileInvoke(ProfileInvoke* x) {
3110 // We can safely ignore accessors here, since c2 will inline them anyway,
3111 // accessors are also always mature.
3112 if (!x->inlinee()->is_accessor()) {
3113 CodeEmitInfo* info = state_for(x, x->state(), true);
3114 // Notify the runtime very infrequently only to take care of counter overflows
3115 int freq_log = Tier23InlineeNotifyFreqLog;
3116 double scale;
3117 if (_method->has_option_value(CompileCommandEnum::CompileThresholdScaling, scale)) {
3118 freq_log = CompilerConfig::scaled_freq_log(freq_log, scale);
3119 }
3120 increment_event_counter_impl(info, x->inlinee(), LIR_OprFact::intConst(InvocationCounter::count_increment), right_n_bits(freq_log), InvocationEntryBci, false, true);
3121 }
3122 }
3123
3124 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) {
3125 if (compilation()->is_profiling()) {
3126 #if defined(X86) && !defined(_LP64)
3127 // BEWARE! On 32-bit x86 cmp clobbers its left argument so we need a temp copy.
3128 LIR_Opr left_copy = new_register(left->type());
|
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 "c1/c1_Compilation.hpp"
26 #include "c1/c1_Defs.hpp"
27 #include "c1/c1_FrameMap.hpp"
28 #include "c1/c1_Instruction.hpp"
29 #include "c1/c1_LIRAssembler.hpp"
30 #include "c1/c1_LIRGenerator.hpp"
31 #include "c1/c1_ValueStack.hpp"
32 #include "ci/ciArrayKlass.hpp"
33 #include "ci/ciFlatArrayKlass.hpp"
34 #include "ci/ciInlineKlass.hpp"
35 #include "ci/ciInstance.hpp"
36 #include "ci/ciObjArray.hpp"
37 #include "ci/ciObjArrayKlass.hpp"
38 #include "ci/ciUtilities.hpp"
39 #include "compiler/compilerDefinitions.inline.hpp"
40 #include "compiler/compilerOracle.hpp"
41 #include "gc/shared/barrierSet.hpp"
42 #include "gc/shared/c1/barrierSetC1.hpp"
43 #include "oops/klass.inline.hpp"
44 #include "oops/methodCounters.hpp"
45 #include "runtime/sharedRuntime.hpp"
46 #include "runtime/stubRoutines.hpp"
47 #include "runtime/vm_version.hpp"
48 #include "utilities/bitMap.inline.hpp"
49 #include "utilities/macros.hpp"
50 #include "utilities/powerOfTwo.hpp"
51
52 #ifdef ASSERT
53 #define __ gen()->lir(__FILE__, __LINE__)->
54 #else
55 #define __ gen()->lir()->
56 #endif
57
201 }
202
203
204 //--------------------------------------------------------------
205 // LIRItem
206
207 void LIRItem::set_result(LIR_Opr opr) {
208 assert(value()->operand()->is_illegal() || value()->operand()->is_constant(), "operand should never change");
209 value()->set_operand(opr);
210
211 #ifdef ASSERT
212 if (opr->is_virtual()) {
213 _gen->_instruction_for_operand.at_put_grow(opr->vreg_number(), value(), nullptr);
214 }
215 #endif
216
217 _result = opr;
218 }
219
220 void LIRItem::load_item() {
221 assert(!_gen->in_conditional_code(), "LIRItem cannot be loaded in conditional code");
222
223 if (result()->is_illegal()) {
224 // update the items result
225 _result = value()->operand();
226 }
227 if (!result()->is_register()) {
228 LIR_Opr reg = _gen->new_register(value()->type());
229 __ move(result(), reg);
230 if (result()->is_constant()) {
231 _result = reg;
232 } else {
233 set_result(reg);
234 }
235 }
236 }
237
238
239 void LIRItem::load_for_store(BasicType type) {
240 if (_gen->can_store_as_constant(value(), type)) {
241 _result = value()->operand();
242 if (!_result->is_constant()) {
610 assert(right_op != result_op, "malformed");
611 __ move(left_op, result_op);
612 left_op = result_op;
613 }
614
615 switch(code) {
616 case Bytecodes::_iand:
617 case Bytecodes::_land: __ logical_and(left_op, right_op, result_op); break;
618
619 case Bytecodes::_ior:
620 case Bytecodes::_lor: __ logical_or(left_op, right_op, result_op); break;
621
622 case Bytecodes::_ixor:
623 case Bytecodes::_lxor: __ logical_xor(left_op, right_op, result_op); break;
624
625 default: ShouldNotReachHere();
626 }
627 }
628
629
630 void LIRGenerator::monitor_enter(LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no,
631 CodeEmitInfo* info_for_exception, CodeEmitInfo* info, CodeStub* throw_ie_stub) {
632 // for slow path, use debug info for state after successful locking
633 CodeStub* slow_path = new MonitorEnterStub(object, lock, info, throw_ie_stub, scratch);
634 __ load_stack_address_monitor(monitor_no, lock);
635 // for handling NullPointerException, use debug info representing just the lock stack before this monitorenter
636 __ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception, throw_ie_stub);
637 }
638
639
640 void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {
641 // setup registers
642 LIR_Opr hdr = lock;
643 lock = new_hdr;
644 CodeStub* slow_path = new MonitorExitStub(lock, LockingMode != LM_MONITOR, monitor_no);
645 __ load_stack_address_monitor(monitor_no, lock);
646 __ unlock_object(hdr, object, lock, scratch, slow_path);
647 }
648
649 #ifndef PRODUCT
650 void LIRGenerator::print_if_not_loaded(const NewInstance* new_instance) {
651 if (PrintNotLoaded && !new_instance->klass()->is_loaded()) {
652 tty->print_cr(" ###class not loaded at new bci %d", new_instance->printable_bci());
653 } else if (PrintNotLoaded && (!CompilerConfig::is_c1_only_no_jvmci() && new_instance->is_unresolved())) {
654 tty->print_cr(" ###class not resolved at new bci %d", new_instance->printable_bci());
655 }
656 }
657 #endif
658
659 void LIRGenerator::new_instance(LIR_Opr dst, ciInstanceKlass* klass, bool is_unresolved, bool allow_inline, LIR_Opr scratch1, LIR_Opr scratch2, LIR_Opr scratch3, LIR_Opr scratch4, LIR_Opr klass_reg, CodeEmitInfo* info) {
660 if (allow_inline) {
661 assert(!is_unresolved && klass->is_loaded(), "inline type klass should be resolved");
662 __ metadata2reg(klass->constant_encoding(), klass_reg);
663 } else {
664 klass2reg_with_patching(klass_reg, klass, info, is_unresolved);
665 }
666 // If klass is not loaded we do not know if the klass has finalizers or is an unexpected inline klass
667 if (UseFastNewInstance && klass->is_loaded() && (allow_inline || !klass->is_inlinetype())
668 && !Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
669
670 StubId stub_id = klass->is_initialized() ? StubId::c1_fast_new_instance_id : StubId::c1_fast_new_instance_init_check_id;
671
672 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id);
673
674 assert(klass->is_loaded(), "must be loaded");
675 // allocate space for instance
676 assert(klass->size_helper() > 0, "illegal instance size");
677 const int instance_size = align_object_size(klass->size_helper());
678 __ allocate_object(dst, scratch1, scratch2, scratch3, scratch4,
679 oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path);
680 } else {
681 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, StubId::c1_new_instance_id);
682 __ jump(slow_path);
683 __ branch_destination(slow_path->continuation());
684 }
685 }
686
687
688 static bool is_constant_zero(Instruction* inst) {
689 IntConstant* c = inst->type()->as_IntConstant();
690 if (c) {
691 return (c->value() == 0);
692 }
693 return false;
694 }
695
696
697 static bool positive_constant(Instruction* inst) {
698 IntConstant* c = inst->type()->as_IntConstant();
699 if (c) {
700 return (c->value() >= 0);
701 }
702 return false;
762 if (src_type != nullptr) {
763 if (src_type->element_type()->is_subtype_of(dst_type->element_type())) {
764 is_exact = true;
765 expected_type = dst_type;
766 }
767 }
768 }
769 // at least pass along a good guess
770 if (expected_type == nullptr) expected_type = dst_exact_type;
771 if (expected_type == nullptr) expected_type = src_declared_type;
772 if (expected_type == nullptr) expected_type = dst_declared_type;
773
774 src_objarray = (src_exact_type && src_exact_type->is_obj_array_klass()) || (src_declared_type && src_declared_type->is_obj_array_klass());
775 dst_objarray = (dst_exact_type && dst_exact_type->is_obj_array_klass()) || (dst_declared_type && dst_declared_type->is_obj_array_klass());
776 }
777
778 // if a probable array type has been identified, figure out if any
779 // of the required checks for a fast case can be elided.
780 int flags = LIR_OpArrayCopy::all_flags;
781
782 if (!src->is_loaded_flat_array() && !dst->is_loaded_flat_array()) {
783 flags &= ~LIR_OpArrayCopy::always_slow_path;
784 }
785 if (!src->maybe_flat_array()) {
786 flags &= ~LIR_OpArrayCopy::src_inlinetype_check;
787 }
788 if (!dst->maybe_flat_array() && !dst->maybe_null_free_array()) {
789 flags &= ~LIR_OpArrayCopy::dst_inlinetype_check;
790 }
791
792 if (!src_objarray)
793 flags &= ~LIR_OpArrayCopy::src_objarray;
794 if (!dst_objarray)
795 flags &= ~LIR_OpArrayCopy::dst_objarray;
796
797 if (!x->arg_needs_null_check(0))
798 flags &= ~LIR_OpArrayCopy::src_null_check;
799 if (!x->arg_needs_null_check(2))
800 flags &= ~LIR_OpArrayCopy::dst_null_check;
801
802
803 if (expected_type != nullptr) {
804 Value length_limit = nullptr;
805
806 IfOp* ifop = length->as_IfOp();
807 if (ifop != nullptr) {
808 // look for expressions like min(v, a.length) which ends up as
809 // x > y ? y : x or x >= y ? y : x
810 if ((ifop->cond() == If::gtr || ifop->cond() == If::geq) &&
811 ifop->x() == ifop->fval() &&
879 BasicType t = expected_type->element_type()->basic_type();
880 int element_size = type2aelembytes(t);
881 if (((arrayOopDesc::base_offset_in_bytes(t) + (uint)s_offs * element_size) % HeapWordSize == 0) &&
882 ((arrayOopDesc::base_offset_in_bytes(t) + (uint)d_offs * element_size) % HeapWordSize == 0)) {
883 flags &= ~LIR_OpArrayCopy::unaligned;
884 }
885 }
886 } else if (src_pos == dst_pos || is_constant_zero(dst_pos)) {
887 // src and dest positions are the same, or dst is zero so assume
888 // nonoverlapping copy.
889 flags &= ~LIR_OpArrayCopy::overlapping;
890 }
891
892 if (src == dst) {
893 // moving within a single array so no type checks are needed
894 if (flags & LIR_OpArrayCopy::type_check) {
895 flags &= ~LIR_OpArrayCopy::type_check;
896 }
897 }
898 *flagsp = flags;
899
900 // TODO 8366668
901 if (expected_type != nullptr && expected_type->is_obj_array_klass()) {
902 expected_type = ciArrayKlass::make(expected_type->as_array_klass()->element_klass(), false, true, true);
903 }
904
905 *expected_typep = (ciArrayKlass*)expected_type;
906 }
907
908
909 LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) {
910 assert(type2size[t] == type2size[value->type()],
911 "size mismatch: t=%s, value->type()=%s", type2name(t), type2name(value->type()));
912 if (!value->is_register()) {
913 // force into a register
914 LIR_Opr r = new_register(value->type());
915 __ move(value, r);
916 value = r;
917 }
918
919 // create a spill location
920 LIR_Opr tmp = new_register(t);
921 set_vreg_flag(tmp, LIRGenerator::must_start_in_memory);
922
923 // move from register to spill
924 __ move(value, tmp);
1470 }
1471 return _vreg_flags.at(vreg_num, f);
1472 }
1473
1474
1475 // Block local constant handling. This code is useful for keeping
1476 // unpinned constants and constants which aren't exposed in the IR in
1477 // registers. Unpinned Constant instructions have their operands
1478 // cleared when the block is finished so that other blocks can't end
1479 // up referring to their registers.
1480
1481 LIR_Opr LIRGenerator::load_constant(Constant* x) {
1482 assert(!x->is_pinned(), "only for unpinned constants");
1483 _unpinned_constants.append(x);
1484 return load_constant(LIR_OprFact::value_type(x->type())->as_constant_ptr());
1485 }
1486
1487
1488 LIR_Opr LIRGenerator::load_constant(LIR_Const* c) {
1489 BasicType t = c->type();
1490 for (int i = 0; i < _constants.length() && !in_conditional_code(); i++) {
1491 LIR_Const* other = _constants.at(i);
1492 if (t == other->type()) {
1493 switch (t) {
1494 case T_INT:
1495 case T_FLOAT:
1496 if (c->as_jint_bits() != other->as_jint_bits()) continue;
1497 break;
1498 case T_LONG:
1499 case T_DOUBLE:
1500 if (c->as_jint_hi_bits() != other->as_jint_hi_bits()) continue;
1501 if (c->as_jint_lo_bits() != other->as_jint_lo_bits()) continue;
1502 break;
1503 case T_OBJECT:
1504 if (c->as_jobject() != other->as_jobject()) continue;
1505 break;
1506 default:
1507 break;
1508 }
1509 return _reg_for_constants.at(i);
1510 }
1511 }
1512
1513 LIR_Opr result = new_register(t);
1514 __ move((LIR_Opr)c, result);
1515 if (!in_conditional_code()) {
1516 _constants.append(c);
1517 _reg_for_constants.append(result);
1518 }
1519 return result;
1520 }
1521
1522 void LIRGenerator::set_in_conditional_code(bool v) {
1523 assert(v != _in_conditional_code, "must change state");
1524 _in_conditional_code = v;
1525 }
1526
1527
1528 //------------------------field access--------------------------------------
1529
1530 void LIRGenerator::do_CompareAndSwap(Intrinsic* x, ValueType* type) {
1531 assert(x->number_of_arguments() == 4, "wrong type");
1532 LIRItem obj (x->argument_at(0), this); // object
1533 LIRItem offset(x->argument_at(1), this); // offset of field
1534 LIRItem cmp (x->argument_at(2), this); // value to compare with field
1535 LIRItem val (x->argument_at(3), this); // replace field with val if matches cmp
1536 assert(obj.type()->tag() == objectTag, "invalid type");
1537 assert(cmp.type()->tag() == type->tag(), "invalid type");
1538 assert(val.type()->tag() == type->tag(), "invalid type");
1539
1540 LIR_Opr result = access_atomic_cmpxchg_at(IN_HEAP, as_BasicType(type),
1541 obj, offset, cmp, val);
1542 set_result(x, result);
1543 }
1544
1545 // Returns a int/long value with the null marker bit set
1546 static LIR_Opr null_marker_mask(BasicType bt, ciField* field) {
1547 assert(field->null_marker_offset() != -1, "field does not have null marker");
1548 int nm_offset = field->null_marker_offset() - field->offset_in_bytes();
1549 jlong null_marker = 1ULL << (nm_offset << LogBitsPerByte);
1550 return (bt == T_LONG) ? LIR_OprFact::longConst(null_marker) : LIR_OprFact::intConst(null_marker);
1551 }
1552
1553 // Comment copied form templateTable_i486.cpp
1554 // ----------------------------------------------------------------------------
1555 // Volatile variables demand their effects be made known to all CPU's in
1556 // order. Store buffers on most chips allow reads & writes to reorder; the
1557 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
1558 // memory barrier (i.e., it's not sufficient that the interpreter does not
1559 // reorder volatile references, the hardware also must not reorder them).
1560 //
1561 // According to the new Java Memory Model (JMM):
1562 // (1) All volatiles are serialized wrt to each other.
1563 // ALSO reads & writes act as acquire & release, so:
1564 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
1565 // the read float up to before the read. It's OK for non-volatile memory refs
1566 // that happen before the volatile read to float down below it.
1567 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
1568 // that happen BEFORE the write float down to after the write. It's OK for
1569 // non-volatile memory refs that happen after the volatile write to float up
1570 // before it.
1571 //
1572 // We only put in barriers around volatile refs (they are expensive), not
1573 // _between_ memory refs (that would require us to track the flavor of the
1574 // previous memory refs). Requirements (2) and (3) require some barriers
1575 // before volatile stores and after volatile loads. These nearly cover
1576 // requirement (1) but miss the volatile-store-volatile-load case. This final
1577 // case is placed after volatile-stores although it could just as well go
1578 // before volatile-loads.
1579
1580
1581 void LIRGenerator::do_StoreField(StoreField* x) {
1582 ciField* field = x->field();
1583 bool needs_patching = x->needs_patching();
1584 bool is_volatile = field->is_volatile();
1585 BasicType field_type = x->field_type();
1586
1587 CodeEmitInfo* info = nullptr;
1588 if (needs_patching) {
1589 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access");
1590 info = state_for(x, x->state_before());
1591 } else if (x->needs_null_check()) {
1592 NullCheck* nc = x->explicit_null_check();
1593 if (nc == nullptr) {
1594 info = state_for(x);
1595 } else {
1596 info = state_for(nc);
1597 }
1598 }
1599
1600 LIRItem object(x->obj(), this);
1601 LIRItem value(x->value(), this);
1602
1603 object.load_item();
1604
1605 if (field->is_flat()) {
1606 value.load_item();
1607 } else {
1608 if (is_volatile || needs_patching) {
1609 // load item if field is volatile (fewer special cases for volatiles)
1610 // load item if field not initialized
1611 // load item if field not constant
1612 // because of code patching we cannot inline constants
1613 if (field_type == T_BYTE || field_type == T_BOOLEAN) {
1614 value.load_byte_item();
1615 } else {
1616 value.load_item();
1617 }
1618 } else {
1619 value.load_for_store(field_type);
1620 }
1621 }
1622
1623 set_no_result(x);
1624
1625 #ifndef PRODUCT
1626 if (PrintNotLoaded && needs_patching) {
1627 tty->print_cr(" ###class not loaded at store_%s bci %d",
1628 x->is_static() ? "static" : "field", x->printable_bci());
1629 }
1630 #endif
1631
1632 if (x->needs_null_check() &&
1633 (needs_patching ||
1634 MacroAssembler::needs_explicit_null_check(x->offset()))) {
1635 // Emit an explicit null check because the offset is too large.
1636 // If the class is not loaded and the object is null, we need to deoptimize to throw a
1637 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
1638 __ null_check(object.result(), new CodeEmitInfo(info), /* deoptimize */ needs_patching);
1639 }
1640
1641 DecoratorSet decorators = IN_HEAP;
1642 if (is_volatile) {
1643 decorators |= MO_SEQ_CST;
1644 }
1645 if (needs_patching) {
1646 decorators |= C1_NEEDS_PATCHING;
1647 }
1648
1649 if (field->is_flat()) {
1650 ciInlineKlass* vk = field->type()->as_inline_klass();
1651
1652 #ifdef ASSERT
1653 bool is_naturally_atomic = vk->nof_declared_nonstatic_fields() <= 1;
1654 bool needs_atomic_access = !field->is_null_free() || (field->is_volatile() && !is_naturally_atomic);
1655 assert(needs_atomic_access, "No atomic access required");
1656 // ZGC does not support compressed oops, so only one oop can be in the payload which is written by a "normal" oop store.
1657 assert(!vk->contains_oops() || !UseZGC, "ZGC does not support embedded oops in flat fields");
1658 #endif
1659
1660 // Zero the payload
1661 BasicType bt = vk->atomic_size_to_basic_type(field->is_null_free());
1662 LIR_Opr payload = new_register((bt == T_LONG) ? bt : T_INT);
1663 LIR_Opr zero = (bt == T_LONG) ? LIR_OprFact::longConst(0) : LIR_OprFact::intConst(0);
1664 __ move(zero, payload);
1665
1666 bool is_constant_null = value.is_constant() && value.value()->is_null_obj();
1667 if (!is_constant_null) {
1668 LabelObj* L_isNull = new LabelObj();
1669 bool needs_null_check = !value.is_constant() || value.value()->is_null_obj();
1670 if (needs_null_check) {
1671 __ cmp(lir_cond_equal, value.result(), LIR_OprFact::oopConst(nullptr));
1672 __ branch(lir_cond_equal, L_isNull->label());
1673 }
1674 // Load payload (if not empty) and set null marker (if not null-free)
1675 if (!vk->is_empty()) {
1676 access_load_at(decorators, bt, value, LIR_OprFact::intConst(vk->payload_offset()), payload);
1677 }
1678 if (!field->is_null_free()) {
1679 __ logical_or(payload, null_marker_mask(bt, field), payload);
1680 }
1681 if (needs_null_check) {
1682 __ branch_destination(L_isNull->label());
1683 }
1684 }
1685 access_store_at(decorators, bt, object, LIR_OprFact::intConst(x->offset()), payload,
1686 // Make sure to emit an implicit null check and pass the information
1687 // that this is a flat store that might require gc barriers for oop fields.
1688 info != nullptr ? new CodeEmitInfo(info) : nullptr, info, vk);
1689 return;
1690 }
1691
1692 access_store_at(decorators, field_type, object, LIR_OprFact::intConst(x->offset()),
1693 value.result(), info != nullptr ? new CodeEmitInfo(info) : nullptr, info);
1694 }
1695
1696 // FIXME -- I can't find any other way to pass an address to access_load_at().
1697 class TempResolvedAddress: public Instruction {
1698 public:
1699 TempResolvedAddress(ValueType* type, LIR_Opr addr) : Instruction(type) {
1700 set_operand(addr);
1701 }
1702 virtual void input_values_do(ValueVisitor*) {}
1703 virtual void visit(InstructionVisitor* v) {}
1704 virtual const char* name() const { return "TempResolvedAddress"; }
1705 };
1706
1707 LIR_Opr LIRGenerator::get_and_load_element_address(LIRItem& array, LIRItem& index) {
1708 ciType* array_type = array.value()->declared_type();
1709 ciFlatArrayKlass* flat_array_klass = array_type->as_flat_array_klass();
1710 assert(flat_array_klass->is_loaded(), "must be");
1711
1712 int array_header_size = flat_array_klass->array_header_in_bytes();
1713 int shift = flat_array_klass->log2_element_size();
1714
1715 #ifndef _LP64
1716 LIR_Opr index_op = new_register(T_INT);
1717 // FIXME -- on 32-bit, the shift below can overflow, so we need to check that
1718 // the top (shift+1) bits of index_op must be zero, or
1719 // else throw ArrayIndexOutOfBoundsException
1720 if (index.result()->is_constant()) {
1721 jint const_index = index.result()->as_jint();
1722 __ move(LIR_OprFact::intConst(const_index << shift), index_op);
1723 } else {
1724 __ shift_left(index_op, shift, index.result());
1725 }
1726 #else
1727 LIR_Opr index_op = new_register(T_LONG);
1728 if (index.result()->is_constant()) {
1729 jint const_index = index.result()->as_jint();
1730 __ move(LIR_OprFact::longConst(const_index << shift), index_op);
1731 } else {
1732 __ convert(Bytecodes::_i2l, index.result(), index_op);
1733 // Need to shift manually, as LIR_Address can scale only up to 3.
1734 __ shift_left(index_op, shift, index_op);
1735 }
1736 #endif
1737
1738 LIR_Opr elm_op = new_pointer_register();
1739 LIR_Address* elm_address = generate_address(array.result(), index_op, 0, array_header_size, T_ADDRESS);
1740 __ leal(LIR_OprFact::address(elm_address), elm_op);
1741 return elm_op;
1742 }
1743
1744 void LIRGenerator::access_sub_element(LIRItem& array, LIRItem& index, LIR_Opr& result, ciField* field, int sub_offset) {
1745 assert(field != nullptr, "Need a subelement type specified");
1746
1747 // Find the starting address of the source (inside the array)
1748 LIR_Opr elm_op = get_and_load_element_address(array, index);
1749
1750 BasicType subelt_type = field->type()->basic_type();
1751 TempResolvedAddress* elm_resolved_addr = new TempResolvedAddress(as_ValueType(subelt_type), elm_op);
1752 LIRItem elm_item(elm_resolved_addr, this);
1753
1754 DecoratorSet decorators = IN_HEAP;
1755 access_load_at(decorators, subelt_type,
1756 elm_item, LIR_OprFact::intConst(sub_offset), result,
1757 nullptr, nullptr);
1758 }
1759
1760 void LIRGenerator::access_flat_array(bool is_load, LIRItem& array, LIRItem& index, LIRItem& obj_item,
1761 ciField* field, int sub_offset) {
1762 assert(sub_offset == 0 || field != nullptr, "Sanity check");
1763
1764 // Find the starting address of the source (inside the array)
1765 LIR_Opr elm_op = get_and_load_element_address(array, index);
1766
1767 ciInlineKlass* elem_klass = nullptr;
1768 if (field != nullptr) {
1769 elem_klass = field->type()->as_inline_klass();
1770 } else {
1771 elem_klass = array.value()->declared_type()->as_flat_array_klass()->element_klass()->as_inline_klass();
1772 }
1773 for (int i = 0; i < elem_klass->nof_nonstatic_fields(); i++) {
1774 ciField* inner_field = elem_klass->nonstatic_field_at(i);
1775 assert(!inner_field->is_flat(), "flat fields must have been expanded");
1776 int obj_offset = inner_field->offset_in_bytes();
1777 int elm_offset = obj_offset - elem_klass->payload_offset() + sub_offset; // object header is not stored in array.
1778 BasicType field_type = inner_field->type()->basic_type();
1779
1780 // Types which are smaller than int are still passed in an int register.
1781 BasicType reg_type = field_type;
1782 switch (reg_type) {
1783 case T_BYTE:
1784 case T_BOOLEAN:
1785 case T_SHORT:
1786 case T_CHAR:
1787 reg_type = T_INT;
1788 break;
1789 default:
1790 break;
1791 }
1792
1793 LIR_Opr temp = new_register(reg_type);
1794 TempResolvedAddress* elm_resolved_addr = new TempResolvedAddress(as_ValueType(field_type), elm_op);
1795 LIRItem elm_item(elm_resolved_addr, this);
1796
1797 DecoratorSet decorators = IN_HEAP;
1798 if (is_load) {
1799 access_load_at(decorators, field_type,
1800 elm_item, LIR_OprFact::intConst(elm_offset), temp,
1801 nullptr, nullptr);
1802 access_store_at(decorators, field_type,
1803 obj_item, LIR_OprFact::intConst(obj_offset), temp,
1804 nullptr, nullptr);
1805 } else {
1806 access_load_at(decorators, field_type,
1807 obj_item, LIR_OprFact::intConst(obj_offset), temp,
1808 nullptr, nullptr);
1809 access_store_at(decorators, field_type,
1810 elm_item, LIR_OprFact::intConst(elm_offset), temp,
1811 nullptr, nullptr);
1812 }
1813 }
1814 }
1815
1816 void LIRGenerator::check_flat_array(LIR_Opr array, LIR_Opr value, CodeStub* slow_path) {
1817 LIR_Opr tmp = new_register(T_METADATA);
1818 __ check_flat_array(array, value, tmp, slow_path);
1819 }
1820
1821 void LIRGenerator::check_null_free_array(LIRItem& array, LIRItem& value, CodeEmitInfo* info) {
1822 LabelObj* L_end = new LabelObj();
1823 LIR_Opr tmp = new_register(T_METADATA);
1824 __ check_null_free_array(array.result(), tmp);
1825 __ branch(lir_cond_equal, L_end->label());
1826 __ null_check(value.result(), info);
1827 __ branch_destination(L_end->label());
1828 }
1829
1830 bool LIRGenerator::needs_flat_array_store_check(StoreIndexed* x) {
1831 if (x->elt_type() == T_OBJECT && x->array()->maybe_flat_array()) {
1832 ciType* type = x->value()->declared_type();
1833 if (type != nullptr && type->is_klass()) {
1834 ciKlass* klass = type->as_klass();
1835 if (!klass->can_be_inline_klass() || (klass->is_inlinetype() && !klass->as_inline_klass()->maybe_flat_in_array())) {
1836 // This is known to be a non-flat object. If the array is a flat array,
1837 // it will be caught by the code generated by array_store_check().
1838 return false;
1839 }
1840 }
1841 // We're not 100% sure, so let's do the flat_array_store_check.
1842 return true;
1843 }
1844 return false;
1845 }
1846
1847 bool LIRGenerator::needs_null_free_array_store_check(StoreIndexed* x) {
1848 return x->elt_type() == T_OBJECT && x->array()->maybe_null_free_array();
1849 }
1850
1851 void LIRGenerator::do_StoreIndexed(StoreIndexed* x) {
1852 assert(x->is_pinned(),"");
1853 assert(x->elt_type() != T_ARRAY, "never used");
1854 bool is_loaded_flat_array = x->array()->is_loaded_flat_array();
1855 bool needs_range_check = x->compute_needs_range_check();
1856 bool use_length = x->length() != nullptr;
1857 bool obj_store = is_reference_type(x->elt_type());
1858 bool needs_store_check = obj_store && !(is_loaded_flat_array && x->is_exact_flat_array_store()) &&
1859 (x->value()->as_Constant() == nullptr ||
1860 !get_jobject_constant(x->value())->is_null_object());
1861
1862 LIRItem array(x->array(), this);
1863 LIRItem index(x->index(), this);
1864 LIRItem value(x->value(), this);
1865 LIRItem length(this);
1866
1867 array.load_item();
1868 index.load_nonconstant();
1869
1870 if (use_length && needs_range_check) {
1871 length.set_instruction(x->length());
1872 length.load_item();
1873 }
1874
1875 if (needs_store_check || x->check_boolean()
1876 || is_loaded_flat_array || needs_flat_array_store_check(x) || needs_null_free_array_store_check(x)) {
1877 value.load_item();
1878 } else {
1879 value.load_for_store(x->elt_type());
1880 }
1881
1882 set_no_result(x);
1883
1884 // the CodeEmitInfo must be duplicated for each different
1885 // LIR-instruction because spilling can occur anywhere between two
1886 // instructions and so the debug information must be different
1887 CodeEmitInfo* range_check_info = state_for(x);
1888 CodeEmitInfo* null_check_info = nullptr;
1889 if (x->needs_null_check()) {
1890 null_check_info = new CodeEmitInfo(range_check_info);
1891 }
1892
1893 if (needs_range_check) {
1894 if (use_length) {
1895 __ cmp(lir_cond_belowEqual, length.result(), index.result());
1896 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result()));
1897 } else {
1898 array_range_check(array.result(), index.result(), null_check_info, range_check_info);
1899 // range_check also does the null check
1900 null_check_info = nullptr;
1901 }
1902 }
1903
1904 if (x->should_profile()) {
1905 if (is_loaded_flat_array) {
1906 // No need to profile a store to a flat array of known type. This can happen if
1907 // the type only became known after optimizations (for example, after the PhiSimplifier).
1908 x->set_should_profile(false);
1909 } else {
1910 int bci = x->profiled_bci();
1911 ciMethodData* md = x->profiled_method()->method_data();
1912 assert(md != nullptr, "Sanity");
1913 ciProfileData* data = md->bci_to_data(bci);
1914 assert(data != nullptr && data->is_ArrayStoreData(), "incorrect profiling entry");
1915 ciArrayStoreData* store_data = (ciArrayStoreData*)data;
1916 profile_array_type(x, md, store_data);
1917 assert(store_data->is_ArrayStoreData(), "incorrect profiling entry");
1918 if (x->array()->maybe_null_free_array()) {
1919 profile_null_free_array(array, md, store_data);
1920 }
1921 }
1922 }
1923
1924 if (GenerateArrayStoreCheck && needs_store_check) {
1925 CodeEmitInfo* store_check_info = new CodeEmitInfo(range_check_info);
1926 array_store_check(value.result(), array.result(), store_check_info, x->profiled_method(), x->profiled_bci());
1927 }
1928
1929 if (is_loaded_flat_array) {
1930 // TODO 8350865 This is currently dead code
1931 if (!x->value()->is_null_free()) {
1932 __ null_check(value.result(), new CodeEmitInfo(range_check_info));
1933 }
1934 // If array element is an empty inline type, no need to copy anything
1935 if (!x->array()->declared_type()->as_flat_array_klass()->element_klass()->as_inline_klass()->is_empty()) {
1936 access_flat_array(false, array, index, value);
1937 }
1938 } else {
1939 StoreFlattenedArrayStub* slow_path = nullptr;
1940
1941 if (needs_flat_array_store_check(x)) {
1942 // Check if we indeed have a flat array
1943 index.load_item();
1944 slow_path = new StoreFlattenedArrayStub(array.result(), index.result(), value.result(), state_for(x, x->state_before()));
1945 check_flat_array(array.result(), value.result(), slow_path);
1946 set_in_conditional_code(true);
1947 } else if (needs_null_free_array_store_check(x)) {
1948 CodeEmitInfo* info = new CodeEmitInfo(range_check_info);
1949 check_null_free_array(array, value, info);
1950 }
1951
1952 DecoratorSet decorators = IN_HEAP | IS_ARRAY;
1953 if (x->check_boolean()) {
1954 decorators |= C1_MASK_BOOLEAN;
1955 }
1956
1957 access_store_at(decorators, x->elt_type(), array, index.result(), value.result(), nullptr, null_check_info);
1958 if (slow_path != nullptr) {
1959 __ branch_destination(slow_path->continuation());
1960 set_in_conditional_code(false);
1961 }
1962 }
1963 }
1964
1965 void LIRGenerator::access_load_at(DecoratorSet decorators, BasicType type,
1966 LIRItem& base, LIR_Opr offset, LIR_Opr result,
1967 CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) {
1968 decorators |= ACCESS_READ;
1969 LIRAccess access(this, decorators, base, offset, type, patch_info, load_emit_info);
1970 if (access.is_raw()) {
1971 _barrier_set->BarrierSetC1::load_at(access, result);
1972 } else {
1973 _barrier_set->load_at(access, result);
1974 }
1975 }
1976
1977 void LIRGenerator::access_load(DecoratorSet decorators, BasicType type,
1978 LIR_Opr addr, LIR_Opr result) {
1979 decorators |= ACCESS_READ;
1980 LIRAccess access(this, decorators, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, type);
1981 access.set_resolved_addr(addr);
1982 if (access.is_raw()) {
1983 _barrier_set->BarrierSetC1::load(access, result);
1984 } else {
1985 _barrier_set->load(access, result);
1986 }
1987 }
1988
1989 void LIRGenerator::access_store_at(DecoratorSet decorators, BasicType type,
1990 LIRItem& base, LIR_Opr offset, LIR_Opr value,
1991 CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info,
1992 ciInlineKlass* vk) {
1993 decorators |= ACCESS_WRITE;
1994 LIRAccess access(this, decorators, base, offset, type, patch_info, store_emit_info, vk);
1995 if (access.is_raw()) {
1996 _barrier_set->BarrierSetC1::store_at(access, value);
1997 } else {
1998 _barrier_set->store_at(access, value);
1999 }
2000 }
2001
2002 LIR_Opr LIRGenerator::access_atomic_cmpxchg_at(DecoratorSet decorators, BasicType type,
2003 LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value) {
2004 decorators |= ACCESS_READ;
2005 decorators |= ACCESS_WRITE;
2006 // Atomic operations are SEQ_CST by default
2007 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0;
2008 LIRAccess access(this, decorators, base, offset, type);
2009 if (access.is_raw()) {
2010 return _barrier_set->BarrierSetC1::atomic_cmpxchg_at(access, cmp_value, new_value);
2011 } else {
2012 return _barrier_set->atomic_cmpxchg_at(access, cmp_value, new_value);
2013 }
2014 }
2025 } else {
2026 return _barrier_set->atomic_xchg_at(access, value);
2027 }
2028 }
2029
2030 LIR_Opr LIRGenerator::access_atomic_add_at(DecoratorSet decorators, BasicType type,
2031 LIRItem& base, LIRItem& offset, LIRItem& value) {
2032 decorators |= ACCESS_READ;
2033 decorators |= ACCESS_WRITE;
2034 // Atomic operations are SEQ_CST by default
2035 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0;
2036 LIRAccess access(this, decorators, base, offset, type);
2037 if (access.is_raw()) {
2038 return _barrier_set->BarrierSetC1::atomic_add_at(access, value);
2039 } else {
2040 return _barrier_set->atomic_add_at(access, value);
2041 }
2042 }
2043
2044 void LIRGenerator::do_LoadField(LoadField* x) {
2045 ciField* field = x->field();
2046 bool needs_patching = x->needs_patching();
2047 bool is_volatile = field->is_volatile();
2048 BasicType field_type = x->field_type();
2049
2050 CodeEmitInfo* info = nullptr;
2051 if (needs_patching) {
2052 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access");
2053 info = state_for(x, x->state_before());
2054 } else if (x->needs_null_check()) {
2055 NullCheck* nc = x->explicit_null_check();
2056 if (nc == nullptr) {
2057 info = state_for(x);
2058 } else {
2059 info = state_for(nc);
2060 }
2061 }
2062
2063 LIRItem object(x->obj(), this);
2064
2065 object.load_item();
2066
2067 #ifndef PRODUCT
2078 stress_deopt)) {
2079 LIR_Opr obj = object.result();
2080 if (stress_deopt) {
2081 obj = new_register(T_OBJECT);
2082 __ move(LIR_OprFact::oopConst(nullptr), obj);
2083 }
2084 // Emit an explicit null check because the offset is too large.
2085 // If the class is not loaded and the object is null, we need to deoptimize to throw a
2086 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
2087 __ null_check(obj, new CodeEmitInfo(info), /* deoptimize */ needs_patching);
2088 }
2089
2090 DecoratorSet decorators = IN_HEAP;
2091 if (is_volatile) {
2092 decorators |= MO_SEQ_CST;
2093 }
2094 if (needs_patching) {
2095 decorators |= C1_NEEDS_PATCHING;
2096 }
2097
2098 if (field->is_flat()) {
2099 ciInlineKlass* vk = field->type()->as_inline_klass();
2100 #ifdef ASSERT
2101 bool is_naturally_atomic = vk->nof_declared_nonstatic_fields() <= 1;
2102 bool needs_atomic_access = !field->is_null_free() || (field->is_volatile() && !is_naturally_atomic);
2103 assert(needs_atomic_access, "No atomic access required");
2104 assert(x->state_before() != nullptr, "Needs state before");
2105 #endif
2106
2107 // Allocate buffer (we can't easily do this conditionally on the null check below
2108 // because branches added in the LIR are opaque to the register allocator).
2109 NewInstance* buffer = new NewInstance(vk, x->state_before(), false, true);
2110 do_NewInstance(buffer);
2111 LIRItem dest(buffer, this);
2112
2113 // Copy the payload to the buffer
2114 BasicType bt = vk->atomic_size_to_basic_type(field->is_null_free());
2115 LIR_Opr payload = new_register((bt == T_LONG) ? bt : T_INT);
2116 access_load_at(decorators, bt, object, LIR_OprFact::intConst(field->offset_in_bytes()), payload,
2117 // Make sure to emit an implicit null check
2118 info ? new CodeEmitInfo(info) : nullptr, info);
2119 access_store_at(decorators, bt, dest, LIR_OprFact::intConst(vk->payload_offset()), payload);
2120
2121 if (field->is_null_free()) {
2122 set_result(x, buffer->operand());
2123 } else {
2124 // Check the null marker and set result to null if it's not set
2125 __ logical_and(payload, null_marker_mask(bt, field), payload);
2126 __ cmp(lir_cond_equal, payload, (bt == T_LONG) ? LIR_OprFact::longConst(0) : LIR_OprFact::intConst(0));
2127 __ cmove(lir_cond_equal, LIR_OprFact::oopConst(nullptr), buffer->operand(), rlock_result(x), T_OBJECT);
2128 }
2129
2130 // Ensure the copy is visible before any subsequent store that publishes the buffer.
2131 __ membar_storestore();
2132 return;
2133 }
2134
2135 LIR_Opr result = rlock_result(x, field_type);
2136 access_load_at(decorators, field_type,
2137 object, LIR_OprFact::intConst(x->offset()), result,
2138 info ? new CodeEmitInfo(info) : nullptr, info);
2139 }
2140
2141 // int/long jdk.internal.util.Preconditions.checkIndex
2142 void LIRGenerator::do_PreconditionsCheckIndex(Intrinsic* x, BasicType type) {
2143 assert(x->number_of_arguments() == 3, "wrong type");
2144 LIRItem index(x->argument_at(0), this);
2145 LIRItem length(x->argument_at(1), this);
2146 LIRItem oobef(x->argument_at(2), this);
2147
2148 index.load_item();
2149 length.load_item();
2150 oobef.load_item();
2151
2152 LIR_Opr result = rlock_result(x);
2153 // x->state() is created from copy_state_for_exception, it does not contains arguments
2154 // we should prepare them before entering into interpreter mode due to deoptimization.
2263 __ move(LIR_OprFact::oopConst(nullptr), obj);
2264 __ null_check(obj, new CodeEmitInfo(null_check_info));
2265 }
2266 }
2267
2268 if (needs_range_check) {
2269 if (StressLoopInvariantCodeMotion && range_check_info->deoptimize_on_exception()) {
2270 __ branch(lir_cond_always, new RangeCheckStub(range_check_info, index.result(), array.result()));
2271 } else if (use_length) {
2272 // TODO: use a (modified) version of array_range_check that does not require a
2273 // constant length to be loaded to a register
2274 __ cmp(lir_cond_belowEqual, length.result(), index.result());
2275 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result()));
2276 } else {
2277 array_range_check(array.result(), index.result(), null_check_info, range_check_info);
2278 // The range check performs the null check, so clear it out for the load
2279 null_check_info = nullptr;
2280 }
2281 }
2282
2283 ciMethodData* md = nullptr;
2284 ciArrayLoadData* load_data = nullptr;
2285 if (x->should_profile()) {
2286 if (x->array()->is_loaded_flat_array()) {
2287 // No need to profile a load from a flat array of known type. This can happen if
2288 // the type only became known after optimizations (for example, after the PhiSimplifier).
2289 x->set_should_profile(false);
2290 } else {
2291 int bci = x->profiled_bci();
2292 md = x->profiled_method()->method_data();
2293 assert(md != nullptr, "Sanity");
2294 ciProfileData* data = md->bci_to_data(bci);
2295 assert(data != nullptr && data->is_ArrayLoadData(), "incorrect profiling entry");
2296 load_data = (ciArrayLoadData*)data;
2297 profile_array_type(x, md, load_data);
2298 }
2299 }
2300
2301 Value element;
2302 if (x->vt() != nullptr) {
2303 assert(x->array()->is_loaded_flat_array(), "must be");
2304 // Find the destination address (of the NewInlineTypeInstance).
2305 LIRItem obj_item(x->vt(), this);
2306
2307 access_flat_array(true, array, index, obj_item,
2308 x->delayed() == nullptr ? 0 : x->delayed()->field(),
2309 x->delayed() == nullptr ? 0 : x->delayed()->offset());
2310 set_no_result(x);
2311 } else if (x->delayed() != nullptr) {
2312 assert(x->array()->is_loaded_flat_array(), "must be");
2313 LIR_Opr result = rlock_result(x, x->delayed()->field()->type()->basic_type());
2314 access_sub_element(array, index, result, x->delayed()->field(), x->delayed()->offset());
2315 } else {
2316 LIR_Opr result = rlock_result(x, x->elt_type());
2317 LoadFlattenedArrayStub* slow_path = nullptr;
2318
2319 if (x->should_profile() && x->array()->maybe_null_free_array()) {
2320 profile_null_free_array(array, md, load_data);
2321 }
2322
2323 if (x->elt_type() == T_OBJECT && x->array()->maybe_flat_array()) {
2324 assert(x->delayed() == nullptr, "Delayed LoadIndexed only apply to loaded_flat_arrays");
2325 index.load_item();
2326 // if we are loading from a flat array, load it using a runtime call
2327 slow_path = new LoadFlattenedArrayStub(array.result(), index.result(), result, state_for(x, x->state_before()));
2328 check_flat_array(array.result(), LIR_OprFact::illegalOpr, slow_path);
2329 set_in_conditional_code(true);
2330 }
2331
2332 DecoratorSet decorators = IN_HEAP | IS_ARRAY;
2333 access_load_at(decorators, x->elt_type(),
2334 array, index.result(), result,
2335 nullptr, null_check_info);
2336
2337 if (slow_path != nullptr) {
2338 __ branch_destination(slow_path->continuation());
2339 set_in_conditional_code(false);
2340 }
2341
2342 element = x;
2343 }
2344
2345 if (x->should_profile()) {
2346 profile_element_type(element, md, load_data);
2347 }
2348 }
2349
2350
2351 void LIRGenerator::do_NullCheck(NullCheck* x) {
2352 if (x->can_trap()) {
2353 LIRItem value(x->obj(), this);
2354 value.load_item();
2355 CodeEmitInfo* info = state_for(x);
2356 __ null_check(value.result(), info);
2357 }
2358 }
2359
2360
2361 void LIRGenerator::do_TypeCast(TypeCast* x) {
2362 LIRItem value(x->obj(), this);
2363 value.load_item();
2364 // the result is the same as from the node we are casting
2365 set_result(x, value.result());
2366 }
2367
2802 bool do_update = !TypeEntries::is_type_unknown(profiled_k);
2803 // known not to be null or null bit already set and already set to
2804 // unknown: nothing we can do to improve profiling
2805 if (!do_null && !do_update) {
2806 return result;
2807 }
2808
2809 ciKlass* exact_klass = nullptr;
2810 Compilation* comp = Compilation::current();
2811 if (do_update) {
2812 // try to find exact type, using CHA if possible, so that loading
2813 // the klass from the object can be avoided
2814 ciType* type = obj->exact_type();
2815 if (type == nullptr) {
2816 type = obj->declared_type();
2817 type = comp->cha_exact_type(type);
2818 }
2819 assert(type == nullptr || type->is_klass(), "type should be class");
2820 exact_klass = (type != nullptr && type->is_loaded()) ? (ciKlass*)type : nullptr;
2821
2822 // TODO 8366668
2823 if (exact_klass != nullptr && exact_klass->is_obj_array_klass()) {
2824 if (exact_klass->as_obj_array_klass()->element_klass()->is_inlinetype()) {
2825 // Could be flat, null free etc.
2826 exact_klass = nullptr;
2827 } else {
2828 exact_klass = ciObjArrayKlass::make(exact_klass->as_array_klass()->element_klass(), true);
2829 }
2830 }
2831
2832 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2833 }
2834
2835 if (!do_null && !do_update) {
2836 return result;
2837 }
2838
2839 ciKlass* exact_signature_k = nullptr;
2840 if (do_update && signature_at_call_k != nullptr) {
2841 // Is the type from the signature exact (the only one possible)?
2842 exact_signature_k = signature_at_call_k->exact_klass();
2843 if (exact_signature_k == nullptr) {
2844 exact_signature_k = comp->cha_exact_type(signature_at_call_k);
2845 } else {
2846 result = exact_signature_k;
2847 // Known statically. No need to emit any code: prevent
2848 // LIR_Assembler::emit_profile_type() from emitting useless code
2849 profiled_k = ciTypeEntries::with_status(result, profiled_k);
2850 }
2851 // exact_klass and exact_signature_k can be both non null but
2852 // different if exact_klass is loaded after the ciObject for
2853 // exact_signature_k is created.
2854 if (exact_klass == nullptr && exact_signature_k != nullptr && exact_klass != exact_signature_k) {
2855 // sometimes the type of the signature is better than the best type
2856 // the compiler has
2857 exact_klass = exact_signature_k;
2858 }
2859 if (callee_signature_k != nullptr &&
2860 callee_signature_k != signature_at_call_k) {
2861 ciKlass* improved_klass = callee_signature_k->exact_klass();
2862 if (improved_klass == nullptr) {
2863 improved_klass = comp->cha_exact_type(callee_signature_k);
2864 }
2865 if (exact_klass == nullptr && improved_klass != nullptr && exact_klass != improved_klass) {
2866 exact_klass = exact_signature_k;
2867 }
2868 }
2869
2870 // TODO 8366668
2871 if (exact_klass != nullptr && exact_klass->is_obj_array_klass()) {
2872 if (exact_klass->as_obj_array_klass()->element_klass()->is_inlinetype()) {
2873 // Could be flat, null free etc.
2874 exact_klass = nullptr;
2875 } else {
2876 exact_klass = ciObjArrayKlass::make(exact_klass->as_array_klass()->element_klass(), true);
2877 }
2878 }
2879
2880 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2881 }
2882
2883 if (!do_null && !do_update) {
2884 return result;
2885 }
2886
2887 if (mdp == LIR_OprFact::illegalOpr) {
2888 mdp = new_register(T_METADATA);
2889 __ metadata2reg(md->constant_encoding(), mdp);
2890 if (md_base_offset != 0) {
2891 LIR_Address* base_type_address = new LIR_Address(mdp, md_base_offset, T_ADDRESS);
2892 mdp = new_pointer_register();
2893 __ leal(LIR_OprFact::address(base_type_address), mdp);
2894 }
2895 }
2896 LIRItem value(obj, this);
2897 value.load_item();
2898 __ profile_type(new LIR_Address(mdp, md_offset, T_METADATA),
2899 value.result(), exact_klass, profiled_k, new_pointer_register(), not_null, exact_signature_k != nullptr);
2916 assert(!src->is_illegal(), "check");
2917 BasicType t = src->type();
2918 if (is_reference_type(t)) {
2919 intptr_t profiled_k = parameters->type(j);
2920 Local* local = x->state()->local_at(java_index)->as_Local();
2921 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),
2922 in_bytes(ParametersTypeData::type_offset(j)) - in_bytes(ParametersTypeData::type_offset(0)),
2923 profiled_k, local, mdp, false, local->declared_type()->as_klass(), nullptr);
2924 // If the profile is known statically set it once for all and do not emit any code
2925 if (exact != nullptr) {
2926 md->set_parameter_type(j, exact);
2927 }
2928 j++;
2929 }
2930 java_index += type2size[t];
2931 }
2932 }
2933 }
2934 }
2935
2936 void LIRGenerator::profile_flags(ciMethodData* md, ciProfileData* data, int flag, LIR_Condition condition) {
2937 assert(md != nullptr && data != nullptr, "should have been initialized");
2938 LIR_Opr mdp = new_register(T_METADATA);
2939 __ metadata2reg(md->constant_encoding(), mdp);
2940 LIR_Address* addr = new LIR_Address(mdp, md->byte_offset_of_slot(data, DataLayout::flags_offset()), T_BYTE);
2941 LIR_Opr flags = new_register(T_INT);
2942 __ move(addr, flags);
2943 if (condition != lir_cond_always) {
2944 LIR_Opr update = new_register(T_INT);
2945 __ cmove(condition, LIR_OprFact::intConst(0), LIR_OprFact::intConst(flag), update, T_INT);
2946 } else {
2947 __ logical_or(flags, LIR_OprFact::intConst(flag), flags);
2948 }
2949 __ store(flags, addr);
2950 }
2951
2952 template <class ArrayData> void LIRGenerator::profile_null_free_array(LIRItem array, ciMethodData* md, ArrayData* load_store) {
2953 assert(compilation()->profile_array_accesses(), "array access profiling is disabled");
2954 LabelObj* L_end = new LabelObj();
2955 LIR_Opr tmp = new_register(T_METADATA);
2956 __ check_null_free_array(array.result(), tmp);
2957
2958 profile_flags(md, load_store, ArrayStoreData::null_free_array_byte_constant(), lir_cond_equal);
2959 }
2960
2961 template <class ArrayData> void LIRGenerator::profile_array_type(AccessIndexed* x, ciMethodData*& md, ArrayData*& load_store) {
2962 assert(compilation()->profile_array_accesses(), "array access profiling is disabled");
2963 LIR_Opr mdp = LIR_OprFact::illegalOpr;
2964 profile_type(md, md->byte_offset_of_slot(load_store, ArrayData::array_offset()), 0,
2965 load_store->array()->type(), x->array(), mdp, true, nullptr, nullptr);
2966 }
2967
2968 void LIRGenerator::profile_element_type(Value element, ciMethodData* md, ciArrayLoadData* load_data) {
2969 assert(compilation()->profile_array_accesses(), "array access profiling is disabled");
2970 assert(md != nullptr && load_data != nullptr, "should have been initialized");
2971 LIR_Opr mdp = LIR_OprFact::illegalOpr;
2972 profile_type(md, md->byte_offset_of_slot(load_data, ArrayLoadData::element_offset()), 0,
2973 load_data->element()->type(), element, mdp, false, nullptr, nullptr);
2974 }
2975
2976 void LIRGenerator::do_Base(Base* x) {
2977 __ std_entry(LIR_OprFact::illegalOpr);
2978 // Emit moves from physical registers / stack slots to virtual registers
2979 CallingConvention* args = compilation()->frame_map()->incoming_arguments();
2980 IRScope* irScope = compilation()->hir()->top_scope();
2981 int java_index = 0;
2982 for (int i = 0; i < args->length(); i++) {
2983 LIR_Opr src = args->at(i);
2984 assert(!src->is_illegal(), "check");
2985 BasicType t = src->type();
2986
2987 // Types which are smaller than int are passed as int, so
2988 // correct the type which passed.
2989 switch (t) {
2990 case T_BYTE:
2991 case T_BOOLEAN:
2992 case T_SHORT:
2993 case T_CHAR:
2994 t = T_INT;
2995 break;
3038 }
3039 assert(obj->is_valid(), "must be valid");
3040
3041 if (method()->is_synchronized()) {
3042 LIR_Opr lock = syncLockOpr();
3043 __ load_stack_address_monitor(0, lock);
3044
3045 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, x->check_flag(Instruction::DeoptimizeOnException));
3046 CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);
3047
3048 // receiver is guaranteed non-null so don't need CodeEmitInfo
3049 __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, nullptr);
3050 }
3051 }
3052 // increment invocation counters if needed
3053 if (!method()->is_accessor()) { // Accessors do not have MDOs, so no counting.
3054 profile_parameters(x);
3055 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, false);
3056 increment_invocation_counter(info);
3057 }
3058 if (method()->has_scalarized_args()) {
3059 // Check if deoptimization was triggered (i.e. orig_pc was set) while buffering scalarized inline type arguments
3060 // in the entry point (see comments in frame::deoptimize). If so, deoptimize only now that we have the right state.
3061 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, 0), nullptr, false);
3062 CodeStub* deopt_stub = new DeoptimizeStub(info, Deoptimization::Reason_none, Deoptimization::Action_none);
3063 __ append(new LIR_Op0(lir_check_orig_pc));
3064 __ branch(lir_cond_notEqual, deopt_stub);
3065 }
3066
3067 // all blocks with a successor must end with an unconditional jump
3068 // to the successor even if they are consecutive
3069 __ jump(x->default_sux());
3070 }
3071
3072
3073 void LIRGenerator::do_OsrEntry(OsrEntry* x) {
3074 // construct our frame and model the production of incoming pointer
3075 // to the OSR buffer.
3076 __ osr_entry(LIR_Assembler::osrBufferPointer());
3077 LIR_Opr result = rlock_result(x);
3078 __ move(LIR_Assembler::osrBufferPointer(), result);
3079 }
3080
3081 void LIRGenerator::invoke_load_one_argument(LIRItem* param, LIR_Opr loc) {
3082 if (loc->is_register()) {
3083 param->load_item_force(loc);
3084 } else {
3085 LIR_Address* addr = loc->as_address_ptr();
3086 param->load_for_store(addr->type());
3087 if (addr->type() == T_OBJECT) {
3088 __ move_wide(param->result(), addr);
3089 } else {
3090 __ move(param->result(), addr);
3091 }
3092 }
3093 }
3094
3095 void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {
3096 assert(args->length() == arg_list->length(),
3097 "args=%d, arg_list=%d", args->length(), arg_list->length());
3098 for (int i = x->has_receiver() ? 1 : 0; i < args->length(); i++) {
3099 LIRItem* param = args->at(i);
3100 LIR_Opr loc = arg_list->at(i);
3101 invoke_load_one_argument(param, loc);
3102 }
3103
3104 if (x->has_receiver()) {
3105 LIRItem* receiver = args->at(0);
3106 LIR_Opr loc = arg_list->at(0);
3107 if (loc->is_register()) {
3108 receiver->load_item_force(loc);
3109 } else {
3110 assert(loc->is_address(), "just checking");
3111 receiver->load_for_store(T_OBJECT);
3112 __ move_wide(receiver->result(), loc->as_address_ptr());
3113 }
3114 }
3115 }
3116
3117
3118 // Visits all arguments, returns appropriate items without loading them
3119 LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {
3120 LIRItemList* argument_items = new LIRItemList();
3121 if (x->has_receiver()) {
3247 __ move(tmp, reg);
3248 }
3249
3250
3251
3252 // Code for : x->x() {x->cond()} x->y() ? x->tval() : x->fval()
3253 void LIRGenerator::do_IfOp(IfOp* x) {
3254 #ifdef ASSERT
3255 {
3256 ValueTag xtag = x->x()->type()->tag();
3257 ValueTag ttag = x->tval()->type()->tag();
3258 assert(xtag == intTag || xtag == objectTag, "cannot handle others");
3259 assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");
3260 assert(ttag == x->fval()->type()->tag(), "cannot handle others");
3261 }
3262 #endif
3263
3264 LIRItem left(x->x(), this);
3265 LIRItem right(x->y(), this);
3266 left.load_item();
3267 if (can_inline_as_constant(right.value()) && !x->substitutability_check()) {
3268 right.dont_load_item();
3269 } else {
3270 // substitutability_check() needs to use right as a base register.
3271 right.load_item();
3272 }
3273
3274 LIRItem t_val(x->tval(), this);
3275 LIRItem f_val(x->fval(), this);
3276 t_val.dont_load_item();
3277 f_val.dont_load_item();
3278
3279 if (x->substitutability_check()) {
3280 substitutability_check(x, left, right, t_val, f_val);
3281 } else {
3282 LIR_Opr reg = rlock_result(x);
3283 __ cmp(lir_cond(x->cond()), left.result(), right.result());
3284 __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg, as_BasicType(x->x()->type()));
3285 }
3286 }
3287
3288 void LIRGenerator::substitutability_check(IfOp* x, LIRItem& left, LIRItem& right, LIRItem& t_val, LIRItem& f_val) {
3289 assert(x->cond() == If::eql || x->cond() == If::neq, "must be");
3290 bool is_acmpeq = (x->cond() == If::eql);
3291 LIR_Opr equal_result = is_acmpeq ? t_val.result() : f_val.result();
3292 LIR_Opr not_equal_result = is_acmpeq ? f_val.result() : t_val.result();
3293 LIR_Opr result = rlock_result(x);
3294 CodeEmitInfo* info = state_for(x, x->state_before());
3295
3296 substitutability_check_common(x->x(), x->y(), left, right, equal_result, not_equal_result, result, info);
3297 }
3298
3299 void LIRGenerator::substitutability_check(If* x, LIRItem& left, LIRItem& right) {
3300 LIR_Opr equal_result = LIR_OprFact::intConst(1);
3301 LIR_Opr not_equal_result = LIR_OprFact::intConst(0);
3302 LIR_Opr result = new_register(T_INT);
3303 CodeEmitInfo* info = state_for(x, x->state_before());
3304
3305 substitutability_check_common(x->x(), x->y(), left, right, equal_result, not_equal_result, result, info);
3306
3307 assert(x->cond() == If::eql || x->cond() == If::neq, "must be");
3308 __ cmp(lir_cond(x->cond()), result, equal_result);
3309 }
3310
3311 void LIRGenerator::substitutability_check_common(Value left_val, Value right_val, LIRItem& left, LIRItem& right,
3312 LIR_Opr equal_result, LIR_Opr not_equal_result, LIR_Opr result,
3313 CodeEmitInfo* info) {
3314 LIR_Opr tmp1 = LIR_OprFact::illegalOpr;
3315 LIR_Opr tmp2 = LIR_OprFact::illegalOpr;
3316 LIR_Opr left_klass_op = LIR_OprFact::illegalOpr;
3317 LIR_Opr right_klass_op = LIR_OprFact::illegalOpr;
3318
3319 ciKlass* left_klass = left_val ->as_loaded_klass_or_null();
3320 ciKlass* right_klass = right_val->as_loaded_klass_or_null();
3321
3322 if ((left_klass == nullptr || right_klass == nullptr) ||// The klass is still unloaded, or came from a Phi node.
3323 !left_klass->is_inlinetype() || !right_klass->is_inlinetype()) {
3324 init_temps_for_substitutability_check(tmp1, tmp2);
3325 }
3326
3327 if (left_klass != nullptr && left_klass->is_inlinetype() && left_klass == right_klass) {
3328 // No need to load klass -- the operands are statically known to be the same inline klass.
3329 } else {
3330 BasicType t_klass = UseCompressedOops ? T_INT : T_METADATA;
3331 left_klass_op = new_register(t_klass);
3332 right_klass_op = new_register(t_klass);
3333 }
3334
3335 CodeStub* slow_path = new SubstitutabilityCheckStub(left.result(), right.result(), info);
3336 __ substitutability_check(result, left.result(), right.result(), equal_result, not_equal_result,
3337 tmp1, tmp2,
3338 left_klass, right_klass, left_klass_op, right_klass_op, info, slow_path);
3339 }
3340
3341 void LIRGenerator::do_RuntimeCall(address routine, Intrinsic* x) {
3342 assert(x->number_of_arguments() == 0, "wrong type");
3343 // Enforce computation of _reserved_argument_area_size which is required on some platforms.
3344 BasicTypeList signature;
3345 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
3346 LIR_Opr reg = result_register_for(x->type());
3347 __ call_runtime_leaf(routine, getThreadTemp(),
3348 reg, new LIR_OprList());
3349 LIR_Opr result = rlock_result(x);
3350 __ move(reg, result);
3351 }
3352
3353
3354
3355 void LIRGenerator::do_Intrinsic(Intrinsic* x) {
3356 switch (x->id()) {
3357 case vmIntrinsics::_intBitsToFloat :
3358 case vmIntrinsics::_doubleToRawLongBits :
3593 if (x->recv() != nullptr || x->nb_profiled_args() > 0) {
3594 profile_parameters_at_call(x);
3595 }
3596
3597 if (x->recv() != nullptr) {
3598 LIRItem value(x->recv(), this);
3599 value.load_item();
3600 recv = new_register(T_OBJECT);
3601 __ move(value.result(), recv);
3602 }
3603 __ profile_call(x->method(), x->bci_of_invoke(), x->callee(), mdo, recv, tmp, x->known_holder());
3604 }
3605
3606 void LIRGenerator::do_ProfileReturnType(ProfileReturnType* x) {
3607 int bci = x->bci_of_invoke();
3608 ciMethodData* md = x->method()->method_data_or_null();
3609 assert(md != nullptr, "Sanity");
3610 ciProfileData* data = md->bci_to_data(bci);
3611 if (data != nullptr) {
3612 assert(data->is_CallTypeData() || data->is_VirtualCallTypeData(), "wrong profile data type");
3613 ciSingleTypeEntry* ret = data->is_CallTypeData() ? ((ciCallTypeData*)data)->ret() : ((ciVirtualCallTypeData*)data)->ret();
3614 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3615
3616 bool ignored_will_link;
3617 ciSignature* signature_at_call = nullptr;
3618 x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);
3619
3620 // The offset within the MDO of the entry to update may be too large
3621 // to be used in load/store instructions on some platforms. So have
3622 // profile_type() compute the address of the profile in a register.
3623 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(data, ret->type_offset()), 0,
3624 ret->type(), x->ret(), mdp,
3625 !x->needs_null_check(),
3626 signature_at_call->return_type()->as_klass(),
3627 x->callee()->signature()->return_type()->as_klass());
3628 if (exact != nullptr) {
3629 md->set_return_type(bci, exact);
3630 }
3631 }
3632 }
3633
3634 bool LIRGenerator::profile_inline_klass(ciMethodData* md, ciProfileData* data, Value value, int flag) {
3635 ciKlass* klass = value->as_loaded_klass_or_null();
3636 if (klass != nullptr) {
3637 if (klass->is_inlinetype()) {
3638 profile_flags(md, data, flag, lir_cond_always);
3639 } else if (klass->can_be_inline_klass()) {
3640 return false;
3641 }
3642 } else {
3643 return false;
3644 }
3645 return true;
3646 }
3647
3648
3649 void LIRGenerator::do_ProfileACmpTypes(ProfileACmpTypes* x) {
3650 ciMethod* method = x->method();
3651 assert(method != nullptr, "method should be set if branch is profiled");
3652 ciMethodData* md = method->method_data_or_null();
3653 assert(md != nullptr, "Sanity");
3654 ciProfileData* data = md->bci_to_data(x->bci());
3655 assert(data != nullptr, "must have profiling data");
3656 assert(data->is_ACmpData(), "need BranchData for two-way branches");
3657 ciACmpData* acmp = (ciACmpData*)data;
3658 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3659 profile_type(md, md->byte_offset_of_slot(acmp, ACmpData::left_offset()), 0,
3660 acmp->left()->type(), x->left(), mdp, !x->left_maybe_null(), nullptr, nullptr);
3661 int flags_offset = md->byte_offset_of_slot(data, DataLayout::flags_offset());
3662 if (!profile_inline_klass(md, acmp, x->left(), ACmpData::left_inline_type_byte_constant())) {
3663 LIR_Opr mdp = new_register(T_METADATA);
3664 __ metadata2reg(md->constant_encoding(), mdp);
3665 LIRItem value(x->left(), this);
3666 value.load_item();
3667 __ profile_inline_type(new LIR_Address(mdp, flags_offset, T_INT), value.result(), ACmpData::left_inline_type_byte_constant(), new_register(T_INT), !x->left_maybe_null());
3668 }
3669 profile_type(md, md->byte_offset_of_slot(acmp, ACmpData::left_offset()),
3670 in_bytes(ACmpData::right_offset()) - in_bytes(ACmpData::left_offset()),
3671 acmp->right()->type(), x->right(), mdp, !x->right_maybe_null(), nullptr, nullptr);
3672 if (!profile_inline_klass(md, acmp, x->right(), ACmpData::right_inline_type_byte_constant())) {
3673 LIR_Opr mdp = new_register(T_METADATA);
3674 __ metadata2reg(md->constant_encoding(), mdp);
3675 LIRItem value(x->right(), this);
3676 value.load_item();
3677 __ profile_inline_type(new LIR_Address(mdp, flags_offset, T_INT), value.result(), ACmpData::right_inline_type_byte_constant(), new_register(T_INT), !x->left_maybe_null());
3678 }
3679 }
3680
3681 void LIRGenerator::do_ProfileInvoke(ProfileInvoke* x) {
3682 // We can safely ignore accessors here, since c2 will inline them anyway,
3683 // accessors are also always mature.
3684 if (!x->inlinee()->is_accessor()) {
3685 CodeEmitInfo* info = state_for(x, x->state(), true);
3686 // Notify the runtime very infrequently only to take care of counter overflows
3687 int freq_log = Tier23InlineeNotifyFreqLog;
3688 double scale;
3689 if (_method->has_option_value(CompileCommandEnum::CompileThresholdScaling, scale)) {
3690 freq_log = CompilerConfig::scaled_freq_log(freq_log, scale);
3691 }
3692 increment_event_counter_impl(info, x->inlinee(), LIR_OprFact::intConst(InvocationCounter::count_increment), right_n_bits(freq_log), InvocationEntryBci, false, true);
3693 }
3694 }
3695
3696 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) {
3697 if (compilation()->is_profiling()) {
3698 #if defined(X86) && !defined(_LP64)
3699 // BEWARE! On 32-bit x86 cmp clobbers its left argument so we need a temp copy.
3700 LIR_Opr left_copy = new_register(left->type());
|