Path: blob/master/src/hotspot/share/opto/arraycopynode.cpp
64440 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();191if (can_reshape) {192phase->is_IterGVN()->_worklist.push(mem);193}194195if (!inst_src->klass_is_exact()) {196ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();197assert(!ik->is_interface(), "inconsistent klass hierarchy");198if (ik->has_subklass()) {199// Concurrent class loading.200// Fail fast and return NodeSentinel to indicate that the transform failed.201return NodeSentinel;202} else {203phase->C->dependencies()->assert_leaf_type(ik);204}205}206207ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();208assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");209210BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();211for (int i = 0; i < count; i++) {212ciField* field = ik->nonstatic_field_at(i);213const TypePtr* adr_type = phase->C->alias_type(field)->adr_type();214Node* off = phase->MakeConX(field->offset());215Node* next_src = phase->transform(new AddPNode(base_src,base_src,off));216Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off));217BasicType bt = field->layout_type();218219const Type *type;220if (bt == T_OBJECT) {221if (!field->type()->is_loaded()) {222type = TypeInstPtr::BOTTOM;223} else {224ciType* field_klass = field->type();225type = TypeOopPtr::make_from_klass(field_klass->as_klass());226}227} else {228type = Type::get_const_basic_type(bt);229}230231Node* v = load(bs, phase, ctl, mem, next_src, adr_type, type, bt);232store(bs, phase, ctl, mem, next_dest, adr_type, v, type, bt);233}234235if (!finish_transform(phase, can_reshape, ctl, mem)) {236// Return NodeSentinel to indicate that the transform failed237return NodeSentinel;238}239240return mem;241}242243bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape,244Node*& adr_src,245Node*& base_src,246Node*& adr_dest,247Node*& base_dest,248BasicType& copy_type,249const Type*& value_type,250bool& disjoint_bases) {251base_src = in(ArrayCopyNode::Src);252base_dest = in(ArrayCopyNode::Dest);253const Type* src_type = phase->type(base_src);254const TypeAryPtr* ary_src = src_type->isa_aryptr();255256Node* src_offset = in(ArrayCopyNode::SrcPos);257Node* dest_offset = in(ArrayCopyNode::DestPos);258259if (is_arraycopy() || is_copyofrange() || is_copyof()) {260const Type* dest_type = phase->type(base_dest);261const TypeAryPtr* ary_dest = dest_type->isa_aryptr();262263// newly allocated object is guaranteed to not overlap with source object264disjoint_bases = is_alloc_tightly_coupled();265266if (ary_src == NULL || ary_src->klass() == NULL ||267ary_dest == NULL || ary_dest->klass() == NULL) {268// We don't know if arguments are arrays269return false;270}271272BasicType src_elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();273BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();274if (is_reference_type(src_elem)) src_elem = T_OBJECT;275if (is_reference_type(dest_elem)) dest_elem = T_OBJECT;276277if (src_elem != dest_elem || dest_elem == T_VOID) {278// We don't know if arguments are arrays of the same type279return false;280}281282BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();283if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) {284// It's an object array copy but we can't emit the card marking285// that is needed286return false;287}288289value_type = ary_src->elem();290291uint shift = exact_log2(type2aelembytes(dest_elem));292uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);293294src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());295if (src_offset->is_top()) {296// Offset is out of bounds (the ArrayCopyNode will be removed)297return false;298}299dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());300if (dest_offset->is_top()) {301// Offset is out of bounds (the ArrayCopyNode will be removed)302if (can_reshape) {303// record src_offset, so it can be deleted later (if it is dead)304phase->is_IterGVN()->_worklist.push(src_offset);305}306return false;307}308309Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));310Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));311312adr_src = phase->transform(new AddPNode(base_src, base_src, src_scale));313adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_scale));314315adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(header)));316adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(header)));317318copy_type = dest_elem;319} else {320assert(ary_src != NULL, "should be a clone");321assert(is_clonebasic(), "should be");322323disjoint_bases = true;324325BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();326if (is_reference_type(elem)) {327elem = T_OBJECT;328}329330BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();331if (bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) {332return false;333}334335adr_src = phase->transform(new AddPNode(base_src, base_src, src_offset));336adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_offset));337338// The address is offseted to an aligned address where a raw copy would start.339// If the clone copy is decomposed into load-stores - the address is adjusted to340// point at where the array starts.341const Type* toff = phase->type(src_offset);342int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();343int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;344assert(diff >= 0, "clone should not start after 1st array element");345if (diff > 0) {346adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));347adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));348}349copy_type = elem;350value_type = ary_src->elem();351}352return true;353}354355const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {356if (atp == TypeOopPtr::BOTTOM) {357atp = phase->type(n)->isa_ptr();358}359// adjust atp to be the correct array element address type360return atp->add_offset(Type::OffsetBot);361}362363void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {364Node* ctl = in(TypeFunc::Control);365if (!disjoint_bases && count > 1) {366Node* src_offset = in(ArrayCopyNode::SrcPos);367Node* dest_offset = in(ArrayCopyNode::DestPos);368assert(src_offset != NULL && dest_offset != NULL, "should be");369Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));370Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));371IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);372373phase->transform(iff);374375forward_ctl = phase->transform(new IfFalseNode(iff));376backward_ctl = phase->transform(new IfTrueNode(iff));377} else {378forward_ctl = ctl;379}380}381382Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,383bool can_reshape,384Node*& forward_ctl,385Node* mem,386const TypePtr* atp_src,387const TypePtr* atp_dest,388Node* adr_src,389Node* base_src,390Node* adr_dest,391Node* base_dest,392BasicType copy_type,393const Type* value_type,394int count) {395if (!forward_ctl->is_top()) {396// copy forward397MergeMemNode* mm = MergeMemNode::make(mem);398399if (count > 0) {400BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();401Node* v = load(bs, phase, forward_ctl, mm, adr_src, atp_src, value_type, copy_type);402store(bs, phase, forward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);403for (int i = 1; i < count; i++) {404Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);405Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));406Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));407v = load(bs, phase, forward_ctl, mm, next_src, atp_src, value_type, copy_type);408store(bs, phase, forward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);409}410} else if (can_reshape) {411PhaseIterGVN* igvn = phase->is_IterGVN();412igvn->_worklist.push(adr_src);413igvn->_worklist.push(adr_dest);414}415return mm;416}417return phase->C->top();418}419420Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,421bool can_reshape,422Node*& backward_ctl,423Node* mem,424const TypePtr* atp_src,425const TypePtr* atp_dest,426Node* adr_src,427Node* base_src,428Node* adr_dest,429Node* base_dest,430BasicType copy_type,431const Type* value_type,432int count) {433if (!backward_ctl->is_top()) {434// copy backward435MergeMemNode* mm = MergeMemNode::make(mem);436437BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();438assert(copy_type != T_OBJECT || !bs->array_copy_requires_gc_barriers(false, T_OBJECT, false, false, BarrierSetC2::Optimization), "only tightly coupled allocations for object arrays");439440if (count > 0) {441for (int i = count-1; i >= 1; i--) {442Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);443Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));444Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));445Node* v = load(bs, phase, backward_ctl, mm, next_src, atp_src, value_type, copy_type);446store(bs, phase, backward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);447}448Node* v = load(bs, phase, backward_ctl, mm, adr_src, atp_src, value_type, copy_type);449store(bs, phase, backward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);450} else if (can_reshape) {451PhaseIterGVN* igvn = phase->is_IterGVN();452igvn->_worklist.push(adr_src);453igvn->_worklist.push(adr_dest);454}455return phase->transform(mm);456}457return phase->C->top();458}459460bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,461Node* ctl, Node *mem) {462if (can_reshape) {463PhaseIterGVN* igvn = phase->is_IterGVN();464igvn->set_delay_transform(false);465if (is_clonebasic()) {466Node* out_mem = proj_out(TypeFunc::Memory);467468BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();469if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||470out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {471assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");472return false;473}474475igvn->replace_node(out_mem->raw_out(0), mem);476477Node* out_ctl = proj_out(TypeFunc::Control);478igvn->replace_node(out_ctl, ctl);479} else {480// replace fallthrough projections of the ArrayCopyNode by the481// new memory, control and the input IO.482CallProjections callprojs;483extract_projections(&callprojs, true, false);484485if (callprojs.fallthrough_ioproj != NULL) {486igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));487}488if (callprojs.fallthrough_memproj != NULL) {489igvn->replace_node(callprojs.fallthrough_memproj, mem);490}491if (callprojs.fallthrough_catchproj != NULL) {492igvn->replace_node(callprojs.fallthrough_catchproj, ctl);493}494495// The ArrayCopyNode is not disconnected. It still has the496// projections for the exception case. Replace current497// ArrayCopyNode with a dummy new one with a top() control so498// that this part of the graph stays consistent but is499// eventually removed.500501set_req(0, phase->C->top());502remove_dead_region(phase, can_reshape);503}504} else {505if (in(TypeFunc::Control) != ctl) {506// we can't return new memory and control from Ideal at parse time507assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");508phase->record_for_igvn(this);509return false;510}511}512return true;513}514515516Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {517if (remove_dead_region(phase, can_reshape)) return this;518519if (StressArrayCopyMacroNode && !can_reshape) {520phase->record_for_igvn(this);521return NULL;522}523524// See if it's a small array copy and we can inline it as525// loads/stores526// Here we can only do:527// - arraycopy if all arguments were validated before and we don't528// need card marking529// - clone for which we don't need to do card marking530531if (!is_clonebasic() && !is_arraycopy_validated() &&532!is_copyofrange_validated() && !is_copyof_validated()) {533return NULL;534}535536assert(in(TypeFunc::Control) != NULL &&537in(TypeFunc::Memory) != NULL &&538in(ArrayCopyNode::Src) != NULL &&539in(ArrayCopyNode::Dest) != NULL &&540in(ArrayCopyNode::Length) != NULL &&541in(ArrayCopyNode::SrcPos) != NULL &&542in(ArrayCopyNode::DestPos) != NULL, "broken inputs");543544if (in(TypeFunc::Control)->is_top() ||545in(TypeFunc::Memory)->is_top() ||546phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||547phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||548(in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||549(in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {550return NULL;551}552553int count = get_count(phase);554555if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {556return NULL;557}558559Node* mem = try_clone_instance(phase, can_reshape, count);560if (mem != NULL) {561return (mem == NodeSentinel) ? NULL : mem;562}563564Node* adr_src = NULL;565Node* base_src = NULL;566Node* adr_dest = NULL;567Node* base_dest = NULL;568BasicType copy_type = T_ILLEGAL;569const Type* value_type = NULL;570bool disjoint_bases = false;571572if (!prepare_array_copy(phase, can_reshape,573adr_src, base_src, adr_dest, base_dest,574copy_type, value_type, disjoint_bases)) {575assert(adr_src == NULL, "no node can be left behind");576assert(adr_dest == NULL, "no node can be left behind");577return NULL;578}579580Node* src = in(ArrayCopyNode::Src);581Node* dest = in(ArrayCopyNode::Dest);582const TypePtr* atp_src = get_address_type(phase, _src_type, src);583const TypePtr* atp_dest = get_address_type(phase, _dest_type, dest);584Node* in_mem = in(TypeFunc::Memory);585586if (can_reshape) {587assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");588phase->is_IterGVN()->set_delay_transform(true);589}590591Node* backward_ctl = phase->C->top();592Node* forward_ctl = phase->C->top();593array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);594595Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,596in_mem,597atp_src, atp_dest,598adr_src, base_src, adr_dest, base_dest,599copy_type, value_type, count);600601Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,602in_mem,603atp_src, atp_dest,604adr_src, base_src, adr_dest, base_dest,605copy_type, value_type, count);606607Node* ctl = NULL;608if (!forward_ctl->is_top() && !backward_ctl->is_top()) {609ctl = new RegionNode(3);610ctl->init_req(1, forward_ctl);611ctl->init_req(2, backward_ctl);612ctl = phase->transform(ctl);613MergeMemNode* forward_mm = forward_mem->as_MergeMem();614MergeMemNode* backward_mm = backward_mem->as_MergeMem();615for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {616if (mms.memory() != mms.memory2()) {617Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));618phi->init_req(1, mms.memory());619phi->init_req(2, mms.memory2());620phi = phase->transform(phi);621mms.set_memory(phi);622}623}624mem = forward_mem;625} else if (!forward_ctl->is_top()) {626ctl = forward_ctl;627mem = forward_mem;628} else {629assert(!backward_ctl->is_top(), "no copy?");630ctl = backward_ctl;631mem = backward_mem;632}633634if (can_reshape) {635assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");636phase->is_IterGVN()->set_delay_transform(false);637}638639if (!finish_transform(phase, can_reshape, ctl, mem)) {640if (can_reshape) {641// put in worklist, so that if it happens to be dead it is removed642phase->is_IterGVN()->_worklist.push(mem);643}644return NULL;645}646647return mem;648}649650bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {651Node* dest = in(ArrayCopyNode::Dest);652if (dest->is_top()) {653return false;654}655const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();656assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");657assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||658_src_type->is_known_instance(), "result of EA not recorded");659660if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {661assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");662return t_oop->instance_id() == _dest_type->instance_id();663}664665return CallNode::may_modify_arraycopy_helper(dest_t, t_oop, phase);666}667668bool ArrayCopyNode::may_modify_helper(const TypeOopPtr *t_oop, Node* n, PhaseTransform *phase, CallNode*& call) {669if (n != NULL &&670n->is_Call() &&671n->as_Call()->may_modify(t_oop, phase) &&672(n->as_Call()->is_ArrayCopy() || n->as_Call()->is_call_to_arraycopystub())) {673call = n->as_Call();674return true;675}676return false;677}678679bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, MemBarNode* mb, PhaseTransform *phase, ArrayCopyNode*& ac) {680681Node* c = mb->in(0);682683BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();684// step over g1 gc barrier if we're at e.g. a clone with ReduceInitialCardMarks off685c = bs->step_over_gc_barrier(c);686687CallNode* call = NULL;688guarantee(c != NULL, "step_over_gc_barrier failed, there must be something to step to.");689if (c->is_Region()) {690for (uint i = 1; i < c->req(); i++) {691if (c->in(i) != NULL) {692Node* n = c->in(i)->in(0);693if (may_modify_helper(t_oop, n, phase, call)) {694ac = call->isa_ArrayCopy();695assert(c == mb->in(0), "only for clone");696return true;697}698}699}700} else if (may_modify_helper(t_oop, c->in(0), phase, call)) {701ac = call->isa_ArrayCopy();702#ifdef ASSERT703bool use_ReduceInitialCardMarks = BarrierSet::barrier_set()->is_a(BarrierSet::CardTableBarrierSet) &&704static_cast<CardTableBarrierSetC2*>(bs)->use_ReduceInitialCardMarks();705assert(c == mb->in(0) || (ac != NULL && ac->is_clonebasic() && !use_ReduceInitialCardMarks), "only for clone");706#endif707return true;708} else if (mb->trailing_partial_array_copy()) {709return true;710}711712return false;713}714715// Does this array copy modify offsets between offset_lo and offset_hi716// in the destination array717// if must_modify is false, return true if the copy could write718// between offset_lo and offset_hi719// if must_modify is true, return true if the copy is guaranteed to720// write between offset_lo and offset_hi721bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const {722assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");723724Node* dest = in(Dest);725Node* dest_pos = in(DestPos);726Node* len = in(Length);727728const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();729const TypeInt *len_t = phase->type(len)->isa_int();730const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();731732if (dest_pos_t == NULL || len_t == NULL || ary_t == NULL) {733return !must_modify;734}735736BasicType ary_elem = ary_t->klass()->as_array_klass()->element_type()->basic_type();737uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);738uint elemsize = type2aelembytes(ary_elem);739740jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;741jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;742jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;743jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;744745if (must_modify) {746if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {747return true;748}749} else {750if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {751return true;752}753}754return false;755}756757// As an optimization, choose optimum vector size for copy length known at compile time.758int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, int const_len) {759int lane_count = ArrayOperationPartialInlineSize/type2aelembytes(type);760if (const_len > 0) {761int size_in_bytes = const_len * type2aelembytes(type);762if (size_in_bytes <= 16)763lane_count = 16/type2aelembytes(type);764else if (size_in_bytes > 16 && size_in_bytes <= 32)765lane_count = 32/type2aelembytes(type);766}767return lane_count;768}769770771