Path: blob/master/src/hotspot/share/opto/arraycopynode.cpp
40930 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/shared/barrierSet.hpp"26#include "gc/shared/c2/barrierSetC2.hpp"27#include "gc/shared/c2/cardTableBarrierSetC2.hpp"28#include "gc/shared/gc_globals.hpp"29#include "opto/arraycopynode.hpp"30#include "opto/graphKit.hpp"31#include "runtime/sharedRuntime.hpp"32#include "utilities/macros.hpp"33#include "utilities/powerOfTwo.hpp"3435ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)36: CallNode(arraycopy_type(), NULL, TypePtr::BOTTOM),37_kind(None),38_alloc_tightly_coupled(alloc_tightly_coupled),39_has_negative_length_guard(has_negative_length_guard),40_arguments_validated(false),41_src_type(TypeOopPtr::BOTTOM),42_dest_type(TypeOopPtr::BOTTOM) {43init_class_id(Class_ArrayCopy);44init_flags(Flag_is_macro);45C->add_macro_node(this);46}4748uint ArrayCopyNode::size_of() const { return sizeof(*this); }4950ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,51Node* src, Node* src_offset,52Node* dest, Node* dest_offset,53Node* length,54bool alloc_tightly_coupled,55bool has_negative_length_guard,56Node* src_klass, Node* dest_klass,57Node* src_length, Node* dest_length) {5859ArrayCopyNode* ac = new ArrayCopyNode(kit->C, alloc_tightly_coupled, has_negative_length_guard);60kit->set_predefined_input_for_runtime_call(ac);6162ac->init_req(ArrayCopyNode::Src, src);63ac->init_req(ArrayCopyNode::SrcPos, src_offset);64ac->init_req(ArrayCopyNode::Dest, dest);65ac->init_req(ArrayCopyNode::DestPos, dest_offset);66ac->init_req(ArrayCopyNode::Length, length);67ac->init_req(ArrayCopyNode::SrcLen, src_length);68ac->init_req(ArrayCopyNode::DestLen, dest_length);69ac->init_req(ArrayCopyNode::SrcKlass, src_klass);70ac->init_req(ArrayCopyNode::DestKlass, dest_klass);7172if (may_throw) {73ac->set_req(TypeFunc::I_O , kit->i_o());74kit->add_safepoint_edges(ac, false);75}7677return ac;78}7980void ArrayCopyNode::connect_outputs(GraphKit* kit, bool deoptimize_on_exception) {81kit->set_all_memory_call(this, true);82kit->set_control(kit->gvn().transform(new ProjNode(this,TypeFunc::Control)));83kit->set_i_o(kit->gvn().transform(new ProjNode(this, TypeFunc::I_O)));84kit->make_slow_call_ex(this, kit->env()->Throwable_klass(), true, deoptimize_on_exception);85kit->set_all_memory_call(this);86}8788#ifndef PRODUCT89const char* ArrayCopyNode::_kind_names[] = {"arraycopy", "arraycopy, validated arguments", "clone", "oop array clone", "CopyOf", "CopyOfRange"};9091void ArrayCopyNode::dump_spec(outputStream *st) const {92CallNode::dump_spec(st);93st->print(" (%s%s)", _kind_names[_kind], _alloc_tightly_coupled ? ", tightly coupled allocation" : "");94}9596void ArrayCopyNode::dump_compact_spec(outputStream* st) const {97st->print("%s%s", _kind_names[_kind], _alloc_tightly_coupled ? ",tight" : "");98}99#endif100101intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const {102// check that length is constant103Node* length = in(ArrayCopyNode::Length);104const Type* length_type = phase->type(length);105106if (length_type == Type::TOP) {107return -1;108}109110assert(is_clonebasic() || is_arraycopy() || is_copyof() || is_copyofrange(), "unexpected array copy type");111112return is_clonebasic() ? length->find_intptr_t_con(-1) : length->find_int_con(-1);113}114115int ArrayCopyNode::get_count(PhaseGVN *phase) const {116Node* src = in(ArrayCopyNode::Src);117const Type* src_type = phase->type(src);118119if (is_clonebasic()) {120if (src_type->isa_instptr()) {121const TypeInstPtr* inst_src = src_type->is_instptr();122ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();123// ciInstanceKlass::nof_nonstatic_fields() doesn't take injected124// fields into account. They are rare anyway so easier to simply125// skip instances with injected fields.126if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {127return -1;128}129int nb_fields = ik->nof_nonstatic_fields();130return nb_fields;131} else {132const TypeAryPtr* ary_src = src_type->isa_aryptr();133assert (ary_src != NULL, "not an array or instance?");134// clone passes a length as a rounded number of longs. If we're135// cloning an array we'll do it element by element. If the136// length input to ArrayCopyNode is constant, length of input137// array must be too.138139assert((get_length_if_constant(phase) == -1) != ary_src->size()->is_con() ||140phase->is_IterGVN() || phase->C->inlining_incrementally() || StressReflectiveCode, "inconsistent");141142if (ary_src->size()->is_con()) {143return ary_src->size()->get_con();144}145return -1;146}147}148149return get_length_if_constant(phase);150}151152Node* ArrayCopyNode::load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, const Type *type, BasicType bt) {153DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY;154C2AccessValuePtr addr(adr, adr_type);155C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);156Node* res = bs->load_at(access, type);157ctl = access.ctl();158return res;159}160161void ArrayCopyNode::store(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, Node* val, const Type *type, BasicType bt) {162DecoratorSet decorators = C2_WRITE_ACCESS | IN_HEAP | C2_ARRAY_COPY;163if (is_alloc_tightly_coupled()) {164decorators |= C2_TIGHTLY_COUPLED_ALLOC;165}166C2AccessValuePtr addr(adr, adr_type);167C2AccessValue value(val, type);168C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);169bs->store_at(access, value);170ctl = access.ctl();171}172173174Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {175if (!is_clonebasic()) {176return NULL;177}178179Node* base_src = in(ArrayCopyNode::Src);180Node* base_dest = in(ArrayCopyNode::Dest);181Node* ctl = in(TypeFunc::Control);182Node* in_mem = in(TypeFunc::Memory);183184const Type* src_type = phase->type(base_src);185const TypeInstPtr* inst_src = src_type->isa_instptr();186if (inst_src == NULL) {187return NULL;188}189190MergeMemNode* mem = phase->transform(MergeMemNode::make(in_mem))->as_MergeMem();191PhaseIterGVN* igvn = phase->is_IterGVN();192if (igvn != NULL) {193igvn->_worklist.push(mem);194}195196if (!inst_src->klass_is_exact()) {197ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();198assert(!ik->is_interface() && !ik->has_subklass(), "inconsistent klass hierarchy");199phase->C->dependencies()->assert_leaf_type(ik);200}201202ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();203assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");204205BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();206for (int i = 0; i < count; i++) {207ciField* field = ik->nonstatic_field_at(i);208const TypePtr* adr_type = phase->C->alias_type(field)->adr_type();209Node* off = phase->MakeConX(field->offset());210Node* next_src = phase->transform(new AddPNode(base_src,base_src,off));211Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off));212BasicType bt = field->layout_type();213214const Type *type;215if (bt == T_OBJECT) {216if (!field->type()->is_loaded()) {217type = TypeInstPtr::BOTTOM;218} else {219ciType* field_klass = field->type();220type = TypeOopPtr::make_from_klass(field_klass->as_klass());221}222} else {223type = Type::get_const_basic_type(bt);224}225226Node* v = load(bs, phase, ctl, mem, next_src, adr_type, type, bt);227store(bs, phase, ctl, mem, next_dest, adr_type, v, type, bt);228}229230if (!finish_transform(phase, can_reshape, ctl, mem)) {231// Return NodeSentinel to indicate that the transform failed232return NodeSentinel;233}234235return mem;236}237238bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape,239Node*& adr_src,240Node*& base_src,241Node*& adr_dest,242Node*& base_dest,243BasicType& copy_type,244const Type*& value_type,245bool& disjoint_bases) {246base_src = in(ArrayCopyNode::Src);247base_dest = in(ArrayCopyNode::Dest);248const Type* src_type = phase->type(base_src);249const TypeAryPtr* ary_src = src_type->isa_aryptr();250251Node* src_offset = in(ArrayCopyNode::SrcPos);252Node* dest_offset = in(ArrayCopyNode::DestPos);253254if (is_arraycopy() || is_copyofrange() || is_copyof()) {255const Type* dest_type = phase->type(base_dest);256const TypeAryPtr* ary_dest = dest_type->isa_aryptr();257258// newly allocated object is guaranteed to not overlap with source object259disjoint_bases = is_alloc_tightly_coupled();260261if (ary_src == NULL || ary_src->klass() == NULL ||262ary_dest == NULL || ary_dest->klass() == NULL) {263// We don't know if arguments are arrays264return false;265}266267BasicType src_elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();268BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();269if (is_reference_type(src_elem)) src_elem = T_OBJECT;270if (is_reference_type(dest_elem)) dest_elem = T_OBJECT;271272if (src_elem != dest_elem || dest_elem == T_VOID) {273// We don't know if arguments are arrays of the same type274return false;275}276277BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();278if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) {279// It's an object array copy but we can't emit the card marking280// that is needed281return false;282}283284value_type = ary_src->elem();285286uint shift = exact_log2(type2aelembytes(dest_elem));287uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);288289src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());290dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());291if (src_offset->is_top() || dest_offset->is_top()) {292// Offset is out of bounds (the ArrayCopyNode will be removed)293return false;294}295296Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));297Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));298299adr_src = phase->transform(new AddPNode(base_src, base_src, src_scale));300adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_scale));301302adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(header)));303adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(header)));304305copy_type = dest_elem;306} else {307assert(ary_src != NULL, "should be a clone");308assert(is_clonebasic(), "should be");309310disjoint_bases = true;311312adr_src = phase->transform(new AddPNode(base_src, base_src, src_offset));313adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_offset));314315BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();316if (is_reference_type(elem)) {317elem = T_OBJECT;318}319320BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();321if (bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) {322return false;323}324325// The address is offseted to an aligned address where a raw copy would start.326// If the clone copy is decomposed into load-stores - the address is adjusted to327// point at where the array starts.328const Type* toff = phase->type(src_offset);329int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();330int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;331assert(diff >= 0, "clone should not start after 1st array element");332if (diff > 0) {333adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));334adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));335}336copy_type = elem;337value_type = ary_src->elem();338}339return true;340}341342const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {343if (atp == TypeOopPtr::BOTTOM) {344atp = phase->type(n)->isa_ptr();345}346// adjust atp to be the correct array element address type347return atp->add_offset(Type::OffsetBot);348}349350void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {351Node* ctl = in(TypeFunc::Control);352if (!disjoint_bases && count > 1) {353Node* src_offset = in(ArrayCopyNode::SrcPos);354Node* dest_offset = in(ArrayCopyNode::DestPos);355assert(src_offset != NULL && dest_offset != NULL, "should be");356Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));357Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));358IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);359360phase->transform(iff);361362forward_ctl = phase->transform(new IfFalseNode(iff));363backward_ctl = phase->transform(new IfTrueNode(iff));364} else {365forward_ctl = ctl;366}367}368369Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,370bool can_reshape,371Node*& forward_ctl,372Node* mem,373const TypePtr* atp_src,374const TypePtr* atp_dest,375Node* adr_src,376Node* base_src,377Node* adr_dest,378Node* base_dest,379BasicType copy_type,380const Type* value_type,381int count) {382if (!forward_ctl->is_top()) {383// copy forward384MergeMemNode* mm = MergeMemNode::make(mem);385386if (count > 0) {387BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();388Node* v = load(bs, phase, forward_ctl, mm, adr_src, atp_src, value_type, copy_type);389store(bs, phase, forward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);390for (int i = 1; i < count; i++) {391Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);392Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));393Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));394v = load(bs, phase, forward_ctl, mm, next_src, atp_src, value_type, copy_type);395store(bs, phase, forward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);396}397} else if (can_reshape) {398PhaseIterGVN* igvn = phase->is_IterGVN();399igvn->_worklist.push(adr_src);400igvn->_worklist.push(adr_dest);401}402return mm;403}404return phase->C->top();405}406407Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,408bool can_reshape,409Node*& backward_ctl,410Node* mem,411const TypePtr* atp_src,412const TypePtr* atp_dest,413Node* adr_src,414Node* base_src,415Node* adr_dest,416Node* base_dest,417BasicType copy_type,418const Type* value_type,419int count) {420if (!backward_ctl->is_top()) {421// copy backward422MergeMemNode* mm = MergeMemNode::make(mem);423424BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();425assert(copy_type != T_OBJECT || !bs->array_copy_requires_gc_barriers(false, T_OBJECT, false, false, BarrierSetC2::Optimization), "only tightly coupled allocations for object arrays");426427if (count > 0) {428for (int i = count-1; i >= 1; i--) {429Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);430Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));431Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));432Node* v = load(bs, phase, backward_ctl, mm, next_src, atp_src, value_type, copy_type);433store(bs, phase, backward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);434}435Node* v = load(bs, phase, backward_ctl, mm, adr_src, atp_src, value_type, copy_type);436store(bs, phase, backward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);437} else if (can_reshape) {438PhaseIterGVN* igvn = phase->is_IterGVN();439igvn->_worklist.push(adr_src);440igvn->_worklist.push(adr_dest);441}442return phase->transform(mm);443}444return phase->C->top();445}446447bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,448Node* ctl, Node *mem) {449if (can_reshape) {450PhaseIterGVN* igvn = phase->is_IterGVN();451igvn->set_delay_transform(false);452if (is_clonebasic()) {453Node* out_mem = proj_out(TypeFunc::Memory);454455BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();456if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||457out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {458assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");459return false;460}461462igvn->replace_node(out_mem->raw_out(0), mem);463464Node* out_ctl = proj_out(TypeFunc::Control);465igvn->replace_node(out_ctl, ctl);466} else {467// replace fallthrough projections of the ArrayCopyNode by the468// new memory, control and the input IO.469CallProjections callprojs;470extract_projections(&callprojs, true, false);471472if (callprojs.fallthrough_ioproj != NULL) {473igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));474}475if (callprojs.fallthrough_memproj != NULL) {476igvn->replace_node(callprojs.fallthrough_memproj, mem);477}478if (callprojs.fallthrough_catchproj != NULL) {479igvn->replace_node(callprojs.fallthrough_catchproj, ctl);480}481482// The ArrayCopyNode is not disconnected. It still has the483// projections for the exception case. Replace current484// ArrayCopyNode with a dummy new one with a top() control so485// that this part of the graph stays consistent but is486// eventually removed.487488set_req(0, phase->C->top());489remove_dead_region(phase, can_reshape);490}491} else {492if (in(TypeFunc::Control) != ctl) {493// we can't return new memory and control from Ideal at parse time494assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");495phase->record_for_igvn(this);496return false;497}498}499return true;500}501502503Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {504if (remove_dead_region(phase, can_reshape)) return this;505506if (StressArrayCopyMacroNode && !can_reshape) {507phase->record_for_igvn(this);508return NULL;509}510511// See if it's a small array copy and we can inline it as512// loads/stores513// Here we can only do:514// - arraycopy if all arguments were validated before and we don't515// need card marking516// - clone for which we don't need to do card marking517518if (!is_clonebasic() && !is_arraycopy_validated() &&519!is_copyofrange_validated() && !is_copyof_validated()) {520return NULL;521}522523assert(in(TypeFunc::Control) != NULL &&524in(TypeFunc::Memory) != NULL &&525in(ArrayCopyNode::Src) != NULL &&526in(ArrayCopyNode::Dest) != NULL &&527in(ArrayCopyNode::Length) != NULL &&528in(ArrayCopyNode::SrcPos) != NULL &&529in(ArrayCopyNode::DestPos) != NULL, "broken inputs");530531if (in(TypeFunc::Control)->is_top() ||532in(TypeFunc::Memory)->is_top() ||533phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||534phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||535(in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||536(in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {537return NULL;538}539540int count = get_count(phase);541542if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {543return NULL;544}545546Node* mem = try_clone_instance(phase, can_reshape, count);547if (mem != NULL) {548return (mem == NodeSentinel) ? NULL : mem;549}550551Node* adr_src = NULL;552Node* base_src = NULL;553Node* adr_dest = NULL;554Node* base_dest = NULL;555BasicType copy_type = T_ILLEGAL;556const Type* value_type = NULL;557bool disjoint_bases = false;558559if (!prepare_array_copy(phase, can_reshape,560adr_src, base_src, adr_dest, base_dest,561copy_type, value_type, disjoint_bases)) {562return NULL;563}564565Node* src = in(ArrayCopyNode::Src);566Node* dest = in(ArrayCopyNode::Dest);567const TypePtr* atp_src = get_address_type(phase, _src_type, src);568const TypePtr* atp_dest = get_address_type(phase, _dest_type, dest);569Node* in_mem = in(TypeFunc::Memory);570571if (can_reshape) {572assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");573phase->is_IterGVN()->set_delay_transform(true);574}575576Node* backward_ctl = phase->C->top();577Node* forward_ctl = phase->C->top();578array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);579580Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,581in_mem,582atp_src, atp_dest,583adr_src, base_src, adr_dest, base_dest,584copy_type, value_type, count);585586Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,587in_mem,588atp_src, atp_dest,589adr_src, base_src, adr_dest, base_dest,590copy_type, value_type, count);591592Node* ctl = NULL;593if (!forward_ctl->is_top() && !backward_ctl->is_top()) {594ctl = new RegionNode(3);595ctl->init_req(1, forward_ctl);596ctl->init_req(2, backward_ctl);597ctl = phase->transform(ctl);598MergeMemNode* forward_mm = forward_mem->as_MergeMem();599MergeMemNode* backward_mm = backward_mem->as_MergeMem();600for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {601if (mms.memory() != mms.memory2()) {602Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));603phi->init_req(1, mms.memory());604phi->init_req(2, mms.memory2());605phi = phase->transform(phi);606mms.set_memory(phi);607}608}609mem = forward_mem;610} else if (!forward_ctl->is_top()) {611ctl = forward_ctl;612mem = forward_mem;613} else {614assert(!backward_ctl->is_top(), "no copy?");615ctl = backward_ctl;616mem = backward_mem;617}618619if (can_reshape) {620assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");621phase->is_IterGVN()->set_delay_transform(false);622}623624if (!finish_transform(phase, can_reshape, ctl, mem)) {625return NULL;626}627628return mem;629}630631bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {632Node* dest = in(ArrayCopyNode::Dest);633if (dest->is_top()) {634return false;635}636const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();637assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");638assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||639_src_type->is_known_instance(), "result of EA not recorded");640641if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {642assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");643return t_oop->instance_id() == _dest_type->instance_id();644}645646return CallNode::may_modify_arraycopy_helper(dest_t, t_oop, phase);647}648649bool ArrayCopyNode::may_modify_helper(const TypeOopPtr *t_oop, Node* n, PhaseTransform *phase, CallNode*& call) {650if (n != NULL &&651n->is_Call() &&652n->as_Call()->may_modify(t_oop, phase) &&653(n->as_Call()->is_ArrayCopy() || n->as_Call()->is_call_to_arraycopystub())) {654call = n->as_Call();655return true;656}657return false;658}659660bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, MemBarNode* mb, PhaseTransform *phase, ArrayCopyNode*& ac) {661662Node* c = mb->in(0);663664BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();665// step over g1 gc barrier if we're at e.g. a clone with ReduceInitialCardMarks off666c = bs->step_over_gc_barrier(c);667668CallNode* call = NULL;669guarantee(c != NULL, "step_over_gc_barrier failed, there must be something to step to.");670if (c->is_Region()) {671for (uint i = 1; i < c->req(); i++) {672if (c->in(i) != NULL) {673Node* n = c->in(i)->in(0);674if (may_modify_helper(t_oop, n, phase, call)) {675ac = call->isa_ArrayCopy();676assert(c == mb->in(0), "only for clone");677return true;678}679}680}681} else if (may_modify_helper(t_oop, c->in(0), phase, call)) {682ac = call->isa_ArrayCopy();683#ifdef ASSERT684bool use_ReduceInitialCardMarks = BarrierSet::barrier_set()->is_a(BarrierSet::CardTableBarrierSet) &&685static_cast<CardTableBarrierSetC2*>(bs)->use_ReduceInitialCardMarks();686assert(c == mb->in(0) || (ac != NULL && ac->is_clonebasic() && !use_ReduceInitialCardMarks), "only for clone");687#endif688return true;689} else if (mb->trailing_partial_array_copy()) {690return true;691}692693return false;694}695696// Does this array copy modify offsets between offset_lo and offset_hi697// in the destination array698// if must_modify is false, return true if the copy could write699// between offset_lo and offset_hi700// if must_modify is true, return true if the copy is guaranteed to701// write between offset_lo and offset_hi702bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const {703assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");704705Node* dest = in(Dest);706Node* dest_pos = in(DestPos);707Node* len = in(Length);708709const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();710const TypeInt *len_t = phase->type(len)->isa_int();711const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();712713if (dest_pos_t == NULL || len_t == NULL || ary_t == NULL) {714return !must_modify;715}716717BasicType ary_elem = ary_t->klass()->as_array_klass()->element_type()->basic_type();718uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);719uint elemsize = type2aelembytes(ary_elem);720721jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;722jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;723jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;724jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;725726if (must_modify) {727if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {728return true;729}730} else {731if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {732return true;733}734}735return false;736}737738// As an optimization, choose optimum vector size for copy length known at compile time.739int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, int const_len) {740int lane_count = ArrayCopyPartialInlineSize/type2aelembytes(type);741if (const_len > 0) {742int size_in_bytes = const_len * type2aelembytes(type);743if (size_in_bytes <= 16)744lane_count = 16/type2aelembytes(type);745else if (size_in_bytes > 16 && size_in_bytes <= 32)746lane_count = 32/type2aelembytes(type);747}748return lane_count;749}750751752