945 }
946 }
947 ShouldNotReachHere();
948 return (LoadNode*)NULL;
949 }
950
951 LoadLNode* LoadLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt) {
952 bool require_atomic = true;
953 return new (C) LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), require_atomic);
954 }
955
956
957
958
959 //------------------------------hash-------------------------------------------
960 uint LoadNode::hash() const {
961 // unroll addition of interesting fields
962 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
963 }
964
965 //---------------------------can_see_stored_value------------------------------
966 // This routine exists to make sure this set of tests is done the same
967 // everywhere. We need to make a coordinated change: first LoadNode::Ideal
968 // will change the graph shape in a way which makes memory alive twice at the
969 // same time (uses the Oracle model of aliasing), then some
970 // LoadXNode::Identity will fold things back to the equivalence-class model
971 // of aliasing.
972 Node* MemNode::can_see_stored_value(Node* st, PhaseTransform* phase) const {
973 Node* ld_adr = in(MemNode::Address);
974 intptr_t ld_off = 0;
975 AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off);
976 const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr();
977 Compile::AliasType* atp = (tp != NULL) ? phase->C->alias_type(tp) : NULL;
978 // This is more general than load from boxing objects.
979 if (phase->C->eliminate_boxing() && (atp != NULL) &&
980 (atp->index() >= Compile::AliasIdxRaw) &&
981 (atp->field() != NULL) && !atp->field()->is_volatile()) {
982 uint alias_idx = atp->index();
983 bool final = atp->field()->is_final();
984 Node* result = NULL;
985 Node* current = st;
986 // Skip through chains of MemBarNodes checking the MergeMems for
987 // new states for the slice of this load. Stop once any other
988 // kind of node is encountered. Loads from final memory can skip
989 // through any kind of MemBar but normal loads shouldn't skip
990 // through MemBarAcquire since the could allow them to move out of
991 // a synchronized region.
992 while (current->is_Proj()) {
993 int opc = current->in(0)->Opcode();
994 if ((final && (opc == Op_MemBarAcquire || opc == Op_MemBarAcquireLock)) ||
995 opc == Op_MemBarRelease || opc == Op_MemBarCPUOrder ||
996 opc == Op_MemBarReleaseLock) {
997 Node* mem = current->in(0)->in(TypeFunc::Memory);
998 if (mem->is_MergeMem()) {
999 MergeMemNode* merge = mem->as_MergeMem();
1000 Node* new_st = merge->memory_at(alias_idx);
1001 if (new_st == merge->base_memory()) {
1002 // Keep searching
1003 current = new_st;
1004 continue;
1005 }
1006 // Save the new memory state for the slice and fall through
1007 // to exit.
1008 result = new_st;
1009 }
1010 }
1011 break;
1012 }
1013 if (result != NULL) {
1014 st = result;
1015 }
1016 }
1017
1018
1019 // Loop around twice in the case Load -> Initialize -> Store.
1020 // (See PhaseIterGVN::add_users_to_worklist, which knows about this case.)
1021 for (int trip = 0; trip <= 1; trip++) {
1022
1023 if (st->is_Store()) {
1024 Node* st_adr = st->in(MemNode::Address);
1025 if (!phase->eqv(st_adr, ld_adr)) {
1026 // Try harder before giving up... Match raw and non-raw pointers.
1027 intptr_t st_off = 0;
1028 AllocateNode* alloc = AllocateNode::Ideal_allocation(st_adr, phase, st_off);
1029 if (alloc == NULL) return NULL;
1030 if (alloc != ld_alloc) return NULL;
1031 if (ld_off != st_off) return NULL;
1032 // At this point we have proven something like this setup:
1033 // A = Allocate(...)
1034 // L = LoadQ(, AddP(CastPP(, A.Parm),, #Off))
1035 // S = StoreQ(, AddP(, A.Parm , #Off), V)
1036 // (Actually, we haven't yet proven the Q's are the same.)
1037 // In other words, we are loading from a casted version of
1038 // the same pointer-and-offset that we stored to.
1560 // (Folds up the 2nd indirection in aClassConstant.getModifiers().)
1561 assert(this->Opcode() == Op_LoadI, "must load an int from _modifier_flags");
1562 return TypeInt::make(klass->modifier_flags());
1563 }
1564 if (tkls->offset() == in_bytes(Klass::access_flags_offset())) {
1565 // The field is Klass::_access_flags. Return its (constant) value.
1566 // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).)
1567 assert(this->Opcode() == Op_LoadI, "must load an int from _access_flags");
1568 return TypeInt::make(klass->access_flags());
1569 }
1570 if (tkls->offset() == in_bytes(Klass::layout_helper_offset())) {
1571 // The field is Klass::_layout_helper. Return its constant value if known.
1572 assert(this->Opcode() == Op_LoadI, "must load an int from _layout_helper");
1573 return TypeInt::make(klass->layout_helper());
1574 }
1575
1576 // No match.
1577 return NULL;
1578 }
1579
1580 //------------------------------Value-----------------------------------------
1581 const Type *LoadNode::Value( PhaseTransform *phase ) const {
1582 // Either input is TOP ==> the result is TOP
1583 Node* mem = in(MemNode::Memory);
1584 const Type *t1 = phase->type(mem);
1585 if (t1 == Type::TOP) return Type::TOP;
1586 Node* adr = in(MemNode::Address);
1587 const TypePtr* tp = phase->type(adr)->isa_ptr();
1588 if (tp == NULL || tp->empty()) return Type::TOP;
1589 int off = tp->offset();
1590 assert(off != Type::OffsetTop, "case covered by TypePtr::empty");
1591 Compile* C = phase->C;
1592
1593 // Try to guess loaded type from pointer type
1594 if (tp->base() == Type::AryPtr) {
1595 const Type *t = tp->is_aryptr()->elem();
1596 // Don't do this for integer types. There is only potential profit if
1597 // the element type t is lower than _type; that is, for int types, if _type is
1598 // more restrictive than t. This only happens here if one is short and the other
1599 // char (both 16 bits), and in those cases we've made an intentional decision
1600 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
1601 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
1602 //
1603 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
1604 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
1605 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
1606 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
1607 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
1608 // In fact, that could have been the original type of p1, and p1 could have
1609 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
1610 // expression (LShiftL quux 3) independently optimized to the constant 8.
1611 if ((t->isa_int() == NULL) && (t->isa_long() == NULL)
1612 && (_type->isa_vect() == NULL)
1613 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
1614 // t might actually be lower than _type, if _type is a unique
1615 // concrete subclass of abstract class t.
|
945 }
946 }
947 ShouldNotReachHere();
948 return (LoadNode*)NULL;
949 }
950
951 LoadLNode* LoadLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt) {
952 bool require_atomic = true;
953 return new (C) LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), require_atomic);
954 }
955
956
957
958
959 //------------------------------hash-------------------------------------------
960 uint LoadNode::hash() const {
961 // unroll addition of interesting fields
962 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
963 }
964
965 static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) {
966 if ((atp != NULL) && (atp->index() >= Compile::AliasIdxRaw)) {
967 bool non_volatile = (atp->field() != NULL) && !atp->field()->is_volatile();
968 bool is_stable_ary = FoldStableValues &&
969 (tp != NULL) && (tp->isa_aryptr() != NULL) &&
970 tp->isa_aryptr()->is_stable();
971
972 return (eliminate_boxing && non_volatile) || is_stable_ary;
973 }
974
975 return false;
976 }
977
978 //---------------------------can_see_stored_value------------------------------
979 // This routine exists to make sure this set of tests is done the same
980 // everywhere. We need to make a coordinated change: first LoadNode::Ideal
981 // will change the graph shape in a way which makes memory alive twice at the
982 // same time (uses the Oracle model of aliasing), then some
983 // LoadXNode::Identity will fold things back to the equivalence-class model
984 // of aliasing.
985 Node* MemNode::can_see_stored_value(Node* st, PhaseTransform* phase) const {
986 Node* ld_adr = in(MemNode::Address);
987 intptr_t ld_off = 0;
988 AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off);
989 const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr();
990 Compile::AliasType* atp = (tp != NULL) ? phase->C->alias_type(tp) : NULL;
991 // This is more general than load from boxing objects.
992 if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) {
993 uint alias_idx = atp->index();
994 bool final = !atp->is_rewritable();
995 Node* result = NULL;
996 Node* current = st;
997 // Skip through chains of MemBarNodes checking the MergeMems for
998 // new states for the slice of this load. Stop once any other
999 // kind of node is encountered. Loads from final memory can skip
1000 // through any kind of MemBar but normal loads shouldn't skip
1001 // through MemBarAcquire since the could allow them to move out of
1002 // a synchronized region.
1003 while (current->is_Proj()) {
1004 int opc = current->in(0)->Opcode();
1005 if ((final && (opc == Op_MemBarAcquire || opc == Op_MemBarAcquireLock)) ||
1006 opc == Op_MemBarRelease || opc == Op_MemBarCPUOrder ||
1007 opc == Op_MemBarReleaseLock) {
1008 Node* mem = current->in(0)->in(TypeFunc::Memory);
1009 if (mem->is_MergeMem()) {
1010 MergeMemNode* merge = mem->as_MergeMem();
1011 Node* new_st = merge->memory_at(alias_idx);
1012 if (new_st == merge->base_memory()) {
1013 // Keep searching
1014 current = new_st;
1015 continue;
1016 }
1017 // Save the new memory state for the slice and fall through
1018 // to exit.
1019 result = new_st;
1020 }
1021 }
1022 break;
1023 }
1024 if (result != NULL) {
1025 st = result;
1026 }
1027 }
1028
1029 // Loop around twice in the case Load -> Initialize -> Store.
1030 // (See PhaseIterGVN::add_users_to_worklist, which knows about this case.)
1031 for (int trip = 0; trip <= 1; trip++) {
1032
1033 if (st->is_Store()) {
1034 Node* st_adr = st->in(MemNode::Address);
1035 if (!phase->eqv(st_adr, ld_adr)) {
1036 // Try harder before giving up... Match raw and non-raw pointers.
1037 intptr_t st_off = 0;
1038 AllocateNode* alloc = AllocateNode::Ideal_allocation(st_adr, phase, st_off);
1039 if (alloc == NULL) return NULL;
1040 if (alloc != ld_alloc) return NULL;
1041 if (ld_off != st_off) return NULL;
1042 // At this point we have proven something like this setup:
1043 // A = Allocate(...)
1044 // L = LoadQ(, AddP(CastPP(, A.Parm),, #Off))
1045 // S = StoreQ(, AddP(, A.Parm , #Off), V)
1046 // (Actually, we haven't yet proven the Q's are the same.)
1047 // In other words, we are loading from a casted version of
1048 // the same pointer-and-offset that we stored to.
1570 // (Folds up the 2nd indirection in aClassConstant.getModifiers().)
1571 assert(this->Opcode() == Op_LoadI, "must load an int from _modifier_flags");
1572 return TypeInt::make(klass->modifier_flags());
1573 }
1574 if (tkls->offset() == in_bytes(Klass::access_flags_offset())) {
1575 // The field is Klass::_access_flags. Return its (constant) value.
1576 // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).)
1577 assert(this->Opcode() == Op_LoadI, "must load an int from _access_flags");
1578 return TypeInt::make(klass->access_flags());
1579 }
1580 if (tkls->offset() == in_bytes(Klass::layout_helper_offset())) {
1581 // The field is Klass::_layout_helper. Return its constant value if known.
1582 assert(this->Opcode() == Op_LoadI, "must load an int from _layout_helper");
1583 return TypeInt::make(klass->layout_helper());
1584 }
1585
1586 // No match.
1587 return NULL;
1588 }
1589
1590 // Try to constant-fold a stable array element.
1591 static const Type* fold_stable_ary_elem(const TypeAryPtr* ary, Node* adr, BasicType loadbt) {
1592 // Make sure the reference is not into the header, by comparing
1593 // the offset against the offset of the start of the array's data.
1594 // Different array types begin at slightly different offsets (12 vs. 16).
1595 // We choose T_BYTE as an example base type that is least restrictive
1596 // as to alignment, which will therefore produce the smallest
1597 // possible base offset.
1598 int off = ary->offset();
1599 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1600 const bool off_in_header = ((uint)off < (uint)min_base_off);
1601 if (!off_in_header && off != Type::OffsetBot &&
1602 adr->is_AddP() && adr->in(AddPNode::Offset)->is_Con() &&
1603 ary->is_stable() && ary->const_oop() != NULL) {
1604 // Decode the results of GraphKit::array_element_address.
1605 ciArray* aobj = ary->const_oop()->as_array();
1606 ciConstant con = aobj->element_value_by_offset(off);
1607
1608 if (con.basic_type() != T_ILLEGAL && !con.is_null_or_zero()) {
1609 const Type* con_type = Type::make_from_constant(con);
1610 if (con_type != NULL) {
1611 if (con_type->isa_aryptr()) {
1612 // Join with the array element type, in case it is also stable.
1613 int dim = ary->stable_dimension();
1614 con_type = con_type->is_aryptr()->cast_to_stable(true, dim-1);
1615 }
1616 if (loadbt == T_NARROWOOP && con_type->isa_oopptr()) {
1617 con_type = con_type->make_narrowoop();
1618 }
1619 #ifndef PRODUCT
1620 if (TraceIterativeGVN) {
1621 tty->print("FoldStableValues: array element [off=%d]: con_type=", off);
1622 con_type->dump(); tty->cr();
1623 }
1624 #endif //PRODUCT
1625 return con_type;
1626 }
1627 }
1628 }
1629
1630 return NULL;
1631 }
1632
1633 //------------------------------Value-----------------------------------------
1634 const Type *LoadNode::Value( PhaseTransform *phase ) const {
1635 // Either input is TOP ==> the result is TOP
1636 Node* mem = in(MemNode::Memory);
1637 const Type *t1 = phase->type(mem);
1638 if (t1 == Type::TOP) return Type::TOP;
1639 Node* adr = in(MemNode::Address);
1640 const TypePtr* tp = phase->type(adr)->isa_ptr();
1641 if (tp == NULL || tp->empty()) return Type::TOP;
1642 int off = tp->offset();
1643 assert(off != Type::OffsetTop, "case covered by TypePtr::empty");
1644 Compile* C = phase->C;
1645
1646 // Try to guess loaded type from pointer type
1647 if (tp->isa_aryptr()) {
1648 const TypeAryPtr* ary = tp->is_aryptr();
1649 const Type *t = ary->elem();
1650
1651 // Try to constant-fold a stable array element.
1652 if (FoldStableValues) {
1653 const Type* con_type = fold_stable_ary_elem(ary, adr, memory_type());
1654 if (con_type != NULL) {
1655 return con_type;
1656 }
1657 }
1658
1659 // Don't do this for integer types. There is only potential profit if
1660 // the element type t is lower than _type; that is, for int types, if _type is
1661 // more restrictive than t. This only happens here if one is short and the other
1662 // char (both 16 bits), and in those cases we've made an intentional decision
1663 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
1664 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
1665 //
1666 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
1667 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
1668 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
1669 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
1670 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
1671 // In fact, that could have been the original type of p1, and p1 could have
1672 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
1673 // expression (LShiftL quux 3) independently optimized to the constant 8.
1674 if ((t->isa_int() == NULL) && (t->isa_long() == NULL)
1675 && (_type->isa_vect() == NULL)
1676 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
1677 // t might actually be lower than _type, if _type is a unique
1678 // concrete subclass of abstract class t.
|