Path: blob/master/src/hotspot/share/gc/z/c2/zBarrierSetC2.cpp
40975 views
/*1* Copyright (c) 2015, 2019, 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*/2223#include "precompiled.hpp"24#include "classfile/javaClasses.hpp"25#include "gc/z/c2/zBarrierSetC2.hpp"26#include "gc/z/zBarrierSet.hpp"27#include "gc/z/zBarrierSetAssembler.hpp"28#include "gc/z/zBarrierSetRuntime.hpp"29#include "opto/arraycopynode.hpp"30#include "opto/addnode.hpp"31#include "opto/block.hpp"32#include "opto/compile.hpp"33#include "opto/graphKit.hpp"34#include "opto/machnode.hpp"35#include "opto/macro.hpp"36#include "opto/memnode.hpp"37#include "opto/node.hpp"38#include "opto/output.hpp"39#include "opto/regalloc.hpp"40#include "opto/rootnode.hpp"41#include "opto/type.hpp"42#include "utilities/growableArray.hpp"43#include "utilities/macros.hpp"4445class ZBarrierSetC2State : public ResourceObj {46private:47GrowableArray<ZLoadBarrierStubC2*>* _stubs;48Node_Array _live;4950public:51ZBarrierSetC2State(Arena* arena) :52_stubs(new (arena) GrowableArray<ZLoadBarrierStubC2*>(arena, 8, 0, NULL)),53_live(arena) {}5455GrowableArray<ZLoadBarrierStubC2*>* stubs() {56return _stubs;57}5859RegMask* live(const Node* node) {60if (!node->is_Mach()) {61// Don't need liveness for non-MachNodes62return NULL;63}6465const MachNode* const mach = node->as_Mach();66if (mach->barrier_data() == ZLoadBarrierElided) {67// Don't need liveness data for nodes without barriers68return NULL;69}7071RegMask* live = (RegMask*)_live[node->_idx];72if (live == NULL) {73live = new (Compile::current()->comp_arena()->Amalloc_D(sizeof(RegMask))) RegMask();74_live.map(node->_idx, (Node*)live);75}7677return live;78}79};8081static ZBarrierSetC2State* barrier_set_state() {82return reinterpret_cast<ZBarrierSetC2State*>(Compile::current()->barrier_set_state());83}8485ZLoadBarrierStubC2* ZLoadBarrierStubC2::create(const MachNode* node, Address ref_addr, Register ref, Register tmp, uint8_t barrier_data) {86ZLoadBarrierStubC2* const stub = new (Compile::current()->comp_arena()) ZLoadBarrierStubC2(node, ref_addr, ref, tmp, barrier_data);87if (!Compile::current()->output()->in_scratch_emit_size()) {88barrier_set_state()->stubs()->append(stub);89}9091return stub;92}9394ZLoadBarrierStubC2::ZLoadBarrierStubC2(const MachNode* node, Address ref_addr, Register ref, Register tmp, uint8_t barrier_data) :95_node(node),96_ref_addr(ref_addr),97_ref(ref),98_tmp(tmp),99_barrier_data(barrier_data),100_entry(),101_continuation() {102assert_different_registers(ref, ref_addr.base());103assert_different_registers(ref, ref_addr.index());104}105106Address ZLoadBarrierStubC2::ref_addr() const {107return _ref_addr;108}109110Register ZLoadBarrierStubC2::ref() const {111return _ref;112}113114Register ZLoadBarrierStubC2::tmp() const {115return _tmp;116}117118address ZLoadBarrierStubC2::slow_path() const {119DecoratorSet decorators = DECORATORS_NONE;120if (_barrier_data & ZLoadBarrierStrong) {121decorators |= ON_STRONG_OOP_REF;122}123if (_barrier_data & ZLoadBarrierWeak) {124decorators |= ON_WEAK_OOP_REF;125}126if (_barrier_data & ZLoadBarrierPhantom) {127decorators |= ON_PHANTOM_OOP_REF;128}129if (_barrier_data & ZLoadBarrierNoKeepalive) {130decorators |= AS_NO_KEEPALIVE;131}132return ZBarrierSetRuntime::load_barrier_on_oop_field_preloaded_addr(decorators);133}134135RegMask& ZLoadBarrierStubC2::live() const {136return *barrier_set_state()->live(_node);137}138139Label* ZLoadBarrierStubC2::entry() {140// The _entry will never be bound when in_scratch_emit_size() is true.141// However, we still need to return a label that is not bound now, but142// will eventually be bound. Any lable will do, as it will only act as143// a placeholder, so we return the _continuation label.144return Compile::current()->output()->in_scratch_emit_size() ? &_continuation : &_entry;145}146147Label* ZLoadBarrierStubC2::continuation() {148return &_continuation;149}150151void* ZBarrierSetC2::create_barrier_state(Arena* comp_arena) const {152return new (comp_arena) ZBarrierSetC2State(comp_arena);153}154155void ZBarrierSetC2::late_barrier_analysis() const {156analyze_dominating_barriers();157compute_liveness_at_stubs();158}159160void ZBarrierSetC2::emit_stubs(CodeBuffer& cb) const {161MacroAssembler masm(&cb);162GrowableArray<ZLoadBarrierStubC2*>* const stubs = barrier_set_state()->stubs();163164for (int i = 0; i < stubs->length(); i++) {165// Make sure there is enough space in the code buffer166if (cb.insts()->maybe_expand_to_ensure_remaining(PhaseOutput::MAX_inst_size) && cb.blob() == NULL) {167ciEnv::current()->record_failure("CodeCache is full");168return;169}170171ZBarrierSet::assembler()->generate_c2_load_barrier_stub(&masm, stubs->at(i));172}173174masm.flush();175}176177int ZBarrierSetC2::estimate_stub_size() const {178Compile* const C = Compile::current();179BufferBlob* const blob = C->output()->scratch_buffer_blob();180GrowableArray<ZLoadBarrierStubC2*>* const stubs = barrier_set_state()->stubs();181int size = 0;182183for (int i = 0; i < stubs->length(); i++) {184CodeBuffer cb(blob->content_begin(), (address)C->output()->scratch_locs_memory() - blob->content_begin());185MacroAssembler masm(&cb);186ZBarrierSet::assembler()->generate_c2_load_barrier_stub(&masm, stubs->at(i));187size += cb.insts_size();188}189190return size;191}192193static void set_barrier_data(C2Access& access) {194if (ZBarrierSet::barrier_needed(access.decorators(), access.type())) {195if (access.decorators() & ON_WEAK_OOP_REF) {196access.set_barrier_data(ZLoadBarrierWeak);197} else {198access.set_barrier_data(ZLoadBarrierStrong);199}200}201}202203Node* ZBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {204set_barrier_data(access);205return BarrierSetC2::load_at_resolved(access, val_type);206}207208Node* ZBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,209Node* new_val, const Type* val_type) const {210set_barrier_data(access);211return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, val_type);212}213214Node* ZBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,215Node* new_val, const Type* value_type) const {216set_barrier_data(access);217return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);218}219220Node* ZBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const {221set_barrier_data(access);222return BarrierSetC2::atomic_xchg_at_resolved(access, new_val, val_type);223}224225bool ZBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type,226bool is_clone, bool is_clone_instance,227ArrayCopyPhase phase) const {228if (phase == ArrayCopyPhase::Parsing) {229return false;230}231if (phase == ArrayCopyPhase::Optimization) {232return is_clone_instance;233}234// else ArrayCopyPhase::Expansion235return type == T_OBJECT || type == T_ARRAY;236}237238// This TypeFunc assumes a 64bit system239static const TypeFunc* clone_type() {240// Create input type (domain)241const Type** domain_fields = TypeTuple::fields(4);242domain_fields[TypeFunc::Parms + 0] = TypeInstPtr::NOTNULL; // src243domain_fields[TypeFunc::Parms + 1] = TypeInstPtr::NOTNULL; // dst244domain_fields[TypeFunc::Parms + 2] = TypeLong::LONG; // size lower245domain_fields[TypeFunc::Parms + 3] = Type::HALF; // size upper246const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + 4, domain_fields);247248// Create result type (range)249const Type** range_fields = TypeTuple::fields(0);250const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 0, range_fields);251252return TypeFunc::make(domain, range);253}254255void ZBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const {256Node* const src = ac->in(ArrayCopyNode::Src);257if (ac->is_clone_array()) {258// Clone primitive array259BarrierSetC2::clone_at_expansion(phase, ac);260return;261}262263// Clone instance264Node* const ctrl = ac->in(TypeFunc::Control);265Node* const mem = ac->in(TypeFunc::Memory);266Node* const dst = ac->in(ArrayCopyNode::Dest);267Node* const size = ac->in(ArrayCopyNode::Length);268269assert(ac->is_clone_inst(), "Sanity check");270assert(size->bottom_type()->is_long(), "Should be long");271272// The native clone we are calling here expects the instance size in words273// Add header/offset size to payload size to get instance size.274Node* const base_offset = phase->longcon(arraycopy_payload_base_offset(false) >> LogBytesPerLong);275Node* const full_size = phase->transform_later(new AddLNode(size, base_offset));276277Node* const call = phase->make_leaf_call(ctrl,278mem,279clone_type(),280ZBarrierSetRuntime::clone_addr(),281"ZBarrierSetRuntime::clone",282TypeRawPtr::BOTTOM,283src,284dst,285full_size,286phase->top());287phase->transform_later(call);288phase->igvn().replace_node(ac, call);289}290291// == Dominating barrier elision ==292293static bool block_has_safepoint(const Block* block, uint from, uint to) {294for (uint i = from; i < to; i++) {295if (block->get_node(i)->is_MachSafePoint()) {296// Safepoint found297return true;298}299}300301// Safepoint not found302return false;303}304305static bool block_has_safepoint(const Block* block) {306return block_has_safepoint(block, 0, block->number_of_nodes());307}308309static uint block_index(const Block* block, const Node* node) {310for (uint j = 0; j < block->number_of_nodes(); ++j) {311if (block->get_node(j) == node) {312return j;313}314}315ShouldNotReachHere();316return 0;317}318319void ZBarrierSetC2::analyze_dominating_barriers() const {320ResourceMark rm;321Compile* const C = Compile::current();322PhaseCFG* const cfg = C->cfg();323Block_List worklist;324Node_List mem_ops;325Node_List barrier_loads;326327// Step 1 - Find accesses, and track them in lists328for (uint i = 0; i < cfg->number_of_blocks(); ++i) {329const Block* const block = cfg->get_block(i);330for (uint j = 0; j < block->number_of_nodes(); ++j) {331const Node* const node = block->get_node(j);332if (!node->is_Mach()) {333continue;334}335336MachNode* const mach = node->as_Mach();337switch (mach->ideal_Opcode()) {338case Op_LoadP:339if ((mach->barrier_data() & ZLoadBarrierStrong) != 0) {340barrier_loads.push(mach);341}342if ((mach->barrier_data() & (ZLoadBarrierStrong | ZLoadBarrierNoKeepalive)) ==343ZLoadBarrierStrong) {344mem_ops.push(mach);345}346break;347case Op_CompareAndExchangeP:348case Op_CompareAndSwapP:349case Op_GetAndSetP:350if ((mach->barrier_data() & ZLoadBarrierStrong) != 0) {351barrier_loads.push(mach);352}353case Op_StoreP:354mem_ops.push(mach);355break;356357default:358break;359}360}361}362363// Step 2 - Find dominating accesses for each load364for (uint i = 0; i < barrier_loads.size(); i++) {365MachNode* const load = barrier_loads.at(i)->as_Mach();366const TypePtr* load_adr_type = NULL;367intptr_t load_offset = 0;368const Node* const load_obj = load->get_base_and_disp(load_offset, load_adr_type);369Block* const load_block = cfg->get_block_for_node(load);370const uint load_index = block_index(load_block, load);371372for (uint j = 0; j < mem_ops.size(); j++) {373MachNode* mem = mem_ops.at(j)->as_Mach();374const TypePtr* mem_adr_type = NULL;375intptr_t mem_offset = 0;376const Node* mem_obj = mem->get_base_and_disp(mem_offset, mem_adr_type);377Block* mem_block = cfg->get_block_for_node(mem);378uint mem_index = block_index(mem_block, mem);379380if (load_obj == NodeSentinel || mem_obj == NodeSentinel ||381load_obj == NULL || mem_obj == NULL ||382load_offset < 0 || mem_offset < 0) {383continue;384}385386if (mem_obj != load_obj || mem_offset != load_offset) {387// Not the same addresses, not a candidate388continue;389}390391if (load_block == mem_block) {392// Earlier accesses in the same block393if (mem_index < load_index && !block_has_safepoint(mem_block, mem_index + 1, load_index)) {394load->set_barrier_data(ZLoadBarrierElided);395}396} else if (mem_block->dominates(load_block)) {397// Dominating block? Look around for safepoints398ResourceMark rm;399Block_List stack;400VectorSet visited;401stack.push(load_block);402bool safepoint_found = block_has_safepoint(load_block);403while (!safepoint_found && stack.size() > 0) {404Block* block = stack.pop();405if (visited.test_set(block->_pre_order)) {406continue;407}408if (block_has_safepoint(block)) {409safepoint_found = true;410break;411}412if (block == mem_block) {413continue;414}415416// Push predecessor blocks417for (uint p = 1; p < block->num_preds(); ++p) {418Block* pred = cfg->get_block_for_node(block->pred(p));419stack.push(pred);420}421}422423if (!safepoint_found) {424load->set_barrier_data(ZLoadBarrierElided);425}426}427}428}429}430431// == Reduced spilling optimization ==432433void ZBarrierSetC2::compute_liveness_at_stubs() const {434ResourceMark rm;435Compile* const C = Compile::current();436Arena* const A = Thread::current()->resource_area();437PhaseCFG* const cfg = C->cfg();438PhaseRegAlloc* const regalloc = C->regalloc();439RegMask* const live = NEW_ARENA_ARRAY(A, RegMask, cfg->number_of_blocks() * sizeof(RegMask));440ZBarrierSetAssembler* const bs = ZBarrierSet::assembler();441Block_List worklist;442443for (uint i = 0; i < cfg->number_of_blocks(); ++i) {444new ((void*)(live + i)) RegMask();445worklist.push(cfg->get_block(i));446}447448while (worklist.size() > 0) {449const Block* const block = worklist.pop();450RegMask& old_live = live[block->_pre_order];451RegMask new_live;452453// Initialize to union of successors454for (uint i = 0; i < block->_num_succs; i++) {455const uint succ_id = block->_succs[i]->_pre_order;456new_live.OR(live[succ_id]);457}458459// Walk block backwards, computing liveness460for (int i = block->number_of_nodes() - 1; i >= 0; --i) {461const Node* const node = block->get_node(i);462463// Remove def bits464const OptoReg::Name first = bs->refine_register(node, regalloc->get_reg_first(node));465const OptoReg::Name second = bs->refine_register(node, regalloc->get_reg_second(node));466if (first != OptoReg::Bad) {467new_live.Remove(first);468}469if (second != OptoReg::Bad) {470new_live.Remove(second);471}472473// Add use bits474for (uint j = 1; j < node->req(); ++j) {475const Node* const use = node->in(j);476const OptoReg::Name first = bs->refine_register(use, regalloc->get_reg_first(use));477const OptoReg::Name second = bs->refine_register(use, regalloc->get_reg_second(use));478if (first != OptoReg::Bad) {479new_live.Insert(first);480}481if (second != OptoReg::Bad) {482new_live.Insert(second);483}484}485486// If this node tracks liveness, update it487RegMask* const regs = barrier_set_state()->live(node);488if (regs != NULL) {489regs->OR(new_live);490}491}492493// Now at block top, see if we have any changes494new_live.SUBTRACT(old_live);495if (new_live.is_NotEmpty()) {496// Liveness has refined, update and propagate to prior blocks497old_live.OR(new_live);498for (uint i = 1; i < block->num_preds(); ++i) {499Block* const pred = cfg->get_block_for_node(block->pred(i));500worklist.push(pred);501}502}503}504}505506507