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