1873 p = p->caller(); |
1873 p = p->caller(); |
1874 } |
1874 } |
1875 log->tail(tag); |
1875 log->tail(tag); |
1876 } |
1876 } |
1877 } |
1877 } |
1878 |
|
1879 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled) |
|
1880 : CallNode(arraycopy_type(), NULL, TypeRawPtr::BOTTOM), |
|
1881 _alloc_tightly_coupled(alloc_tightly_coupled), |
|
1882 _kind(None), |
|
1883 _arguments_validated(false) { |
|
1884 init_class_id(Class_ArrayCopy); |
|
1885 init_flags(Flag_is_macro); |
|
1886 C->add_macro_node(this); |
|
1887 } |
|
1888 |
|
1889 uint ArrayCopyNode::size_of() const { return sizeof(*this); } |
|
1890 |
|
1891 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw, |
|
1892 Node* src, Node* src_offset, |
|
1893 Node* dest, Node* dest_offset, |
|
1894 Node* length, |
|
1895 bool alloc_tightly_coupled, |
|
1896 Node* src_klass, Node* dest_klass, |
|
1897 Node* src_length, Node* dest_length) { |
|
1898 |
|
1899 ArrayCopyNode* ac = new ArrayCopyNode(kit->C, alloc_tightly_coupled); |
|
1900 Node* prev_mem = kit->set_predefined_input_for_runtime_call(ac); |
|
1901 |
|
1902 ac->init_req(ArrayCopyNode::Src, src); |
|
1903 ac->init_req(ArrayCopyNode::SrcPos, src_offset); |
|
1904 ac->init_req(ArrayCopyNode::Dest, dest); |
|
1905 ac->init_req(ArrayCopyNode::DestPos, dest_offset); |
|
1906 ac->init_req(ArrayCopyNode::Length, length); |
|
1907 ac->init_req(ArrayCopyNode::SrcLen, src_length); |
|
1908 ac->init_req(ArrayCopyNode::DestLen, dest_length); |
|
1909 ac->init_req(ArrayCopyNode::SrcKlass, src_klass); |
|
1910 ac->init_req(ArrayCopyNode::DestKlass, dest_klass); |
|
1911 |
|
1912 if (may_throw) { |
|
1913 ac->set_req(TypeFunc::I_O , kit->i_o()); |
|
1914 kit->add_safepoint_edges(ac, false); |
|
1915 } |
|
1916 |
|
1917 return ac; |
|
1918 } |
|
1919 |
|
1920 void ArrayCopyNode::connect_outputs(GraphKit* kit) { |
|
1921 kit->set_all_memory_call(this, true); |
|
1922 kit->set_control(kit->gvn().transform(new ProjNode(this,TypeFunc::Control))); |
|
1923 kit->set_i_o(kit->gvn().transform(new ProjNode(this, TypeFunc::I_O))); |
|
1924 kit->make_slow_call_ex(this, kit->env()->Throwable_klass(), true); |
|
1925 kit->set_all_memory_call(this); |
|
1926 } |
|
1927 |
|
1928 #ifndef PRODUCT |
|
1929 const char* ArrayCopyNode::_kind_names[] = {"arraycopy", "arraycopy, validated arguments", "clone", "oop array clone", "CopyOf", "CopyOfRange"}; |
|
1930 void ArrayCopyNode::dump_spec(outputStream *st) const { |
|
1931 CallNode::dump_spec(st); |
|
1932 st->print(" (%s%s)", _kind_names[_kind], _alloc_tightly_coupled ? ", tightly coupled allocation" : ""); |
|
1933 } |
|
1934 #endif |
|
1935 |
|
1936 int ArrayCopyNode::get_count(PhaseGVN *phase) const { |
|
1937 Node* src = in(ArrayCopyNode::Src); |
|
1938 const Type* src_type = phase->type(src); |
|
1939 |
|
1940 assert(is_clonebasic(), "unexpected arraycopy type"); |
|
1941 if (src_type->isa_instptr()) { |
|
1942 const TypeInstPtr* inst_src = src_type->is_instptr(); |
|
1943 ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); |
|
1944 // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected |
|
1945 // fields into account. They are rare anyway so easier to simply |
|
1946 // skip instances with injected fields. |
|
1947 if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) { |
|
1948 return -1; |
|
1949 } |
|
1950 int nb_fields = ik->nof_nonstatic_fields(); |
|
1951 return nb_fields; |
|
1952 } |
|
1953 return -1; |
|
1954 } |
|
1955 |
|
1956 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) { |
|
1957 assert(is_clonebasic(), "unexpected arraycopy type"); |
|
1958 |
|
1959 Node* src = in(ArrayCopyNode::Src); |
|
1960 Node* dest = in(ArrayCopyNode::Dest); |
|
1961 Node* ctl = in(TypeFunc::Control); |
|
1962 Node* in_mem = in(TypeFunc::Memory); |
|
1963 |
|
1964 const Type* src_type = phase->type(src); |
|
1965 const Type* dest_type = phase->type(dest); |
|
1966 |
|
1967 assert(src->is_AddP(), "should be base + off"); |
|
1968 assert(dest->is_AddP(), "should be base + off"); |
|
1969 Node* base_src = src->in(AddPNode::Base); |
|
1970 Node* base_dest = dest->in(AddPNode::Base); |
|
1971 |
|
1972 MergeMemNode* mem = MergeMemNode::make(in_mem); |
|
1973 |
|
1974 const TypeInstPtr* inst_src = src_type->is_instptr(); |
|
1975 |
|
1976 if (!inst_src->klass_is_exact()) { |
|
1977 ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); |
|
1978 assert(!ik->is_interface() && !ik->has_subklass(), "inconsistent klass hierarchy"); |
|
1979 phase->C->dependencies()->assert_leaf_type(ik); |
|
1980 } |
|
1981 |
|
1982 ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); |
|
1983 assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields"); |
|
1984 |
|
1985 for (int i = 0; i < count; i++) { |
|
1986 ciField* field = ik->nonstatic_field_at(i); |
|
1987 int fieldidx = phase->C->alias_type(field)->index(); |
|
1988 const TypePtr* adr_type = phase->C->alias_type(field)->adr_type(); |
|
1989 Node* off = phase->MakeConX(field->offset()); |
|
1990 Node* next_src = phase->transform(new AddPNode(base_src,base_src,off)); |
|
1991 Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off)); |
|
1992 BasicType bt = field->layout_type(); |
|
1993 |
|
1994 const Type *type; |
|
1995 if (bt == T_OBJECT) { |
|
1996 if (!field->type()->is_loaded()) { |
|
1997 type = TypeInstPtr::BOTTOM; |
|
1998 } else { |
|
1999 ciType* field_klass = field->type(); |
|
2000 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); |
|
2001 } |
|
2002 } else { |
|
2003 type = Type::get_const_basic_type(bt); |
|
2004 } |
|
2005 |
|
2006 Node* v = LoadNode::make(*phase, ctl, mem->memory_at(fieldidx), next_src, adr_type, type, bt, MemNode::unordered); |
|
2007 v = phase->transform(v); |
|
2008 Node* s = StoreNode::make(*phase, ctl, mem->memory_at(fieldidx), next_dest, adr_type, v, bt, MemNode::unordered); |
|
2009 s = phase->transform(s); |
|
2010 mem->set_memory_at(fieldidx, s); |
|
2011 } |
|
2012 |
|
2013 if (!finish_transform(phase, can_reshape, ctl, mem)) { |
|
2014 return NULL; |
|
2015 } |
|
2016 |
|
2017 return mem; |
|
2018 } |
|
2019 |
|
2020 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape, |
|
2021 Node* ctl, Node *mem) { |
|
2022 if (can_reshape) { |
|
2023 PhaseIterGVN* igvn = phase->is_IterGVN(); |
|
2024 assert(is_clonebasic(), "unexpected arraycopy type"); |
|
2025 Node* out_mem = proj_out(TypeFunc::Memory); |
|
2026 |
|
2027 if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() || |
|
2028 out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) { |
|
2029 assert(!GraphKit::use_ReduceInitialCardMarks(), "can only happen with card marking"); |
|
2030 return false; |
|
2031 } |
|
2032 |
|
2033 igvn->replace_node(out_mem->raw_out(0), mem); |
|
2034 |
|
2035 Node* out_ctl = proj_out(TypeFunc::Control); |
|
2036 igvn->replace_node(out_ctl, ctl); |
|
2037 } |
|
2038 return true; |
|
2039 } |
|
2040 |
|
2041 |
|
2042 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) { |
|
2043 if (remove_dead_region(phase, can_reshape)) return this; |
|
2044 |
|
2045 if (StressArrayCopyMacroNode && !can_reshape) return NULL; |
|
2046 |
|
2047 // See if it's a small array copy and we can inline it as |
|
2048 // loads/stores |
|
2049 // Here we can only do: |
|
2050 // - clone for which we don't need to do card marking |
|
2051 |
|
2052 if (!is_clonebasic()) { |
|
2053 return NULL; |
|
2054 } |
|
2055 |
|
2056 if (in(TypeFunc::Control)->is_top() || in(TypeFunc::Memory)->is_top()) { |
|
2057 return NULL; |
|
2058 } |
|
2059 |
|
2060 int count = get_count(phase); |
|
2061 |
|
2062 if (count < 0 || count > ArrayCopyLoadStoreMaxElem) { |
|
2063 return NULL; |
|
2064 } |
|
2065 |
|
2066 Node* mem = try_clone_instance(phase, can_reshape, count); |
|
2067 return mem; |
|
2068 } |
|