Path: blob/master/src/hotspot/cpu/x86/c2_intelJccErratum_x86.cpp
41144 views
/*1* Copyright (c) 2020, 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 "asm/macroAssembler.hpp"26#include "c2_intelJccErratum_x86.hpp"27#include "opto/cfgnode.hpp"28#include "opto/compile.hpp"29#include "opto/machnode.hpp"30#include "opto/node.hpp"31#include "opto/output.hpp"32#include "opto/regalloc.hpp"33#include "utilities/align.hpp"34#include "utilities/debug.hpp"3536// Compute which 32 byte boundary an address corresponds to37uintptr_t IntelJccErratum::boundary(uintptr_t addr) {38return addr >> 5;39}4041bool IntelJccErratum::is_crossing_or_ending_at_32_byte_boundary(uintptr_t start_pc, uintptr_t end_pc) {42int jcc_size = int(end_pc - start_pc);43assert(jcc_size <= largest_jcc_size(), "invalid jcc size: %d", jcc_size);44return boundary(start_pc) != boundary(end_pc);45}4647bool IntelJccErratum::is_jcc_erratum_branch(const MachNode* node) {48if (node->is_MachCall() && !node->is_MachCallJava()) {49return true;50}51return node->is_MachBranch();52}5354int IntelJccErratum::jcc_erratum_taint_node(MachNode* node, PhaseRegAlloc* regalloc) {55node->add_flag(Node::PD::Flag_intel_jcc_erratum);56return node->size(regalloc);57}5859int IntelJccErratum::tag_affected_machnodes(Compile* C, PhaseCFG* cfg, PhaseRegAlloc* regalloc) {60ResourceMark rm;61int nop_size = 0;62MachNode* last_m = NULL;6364for (uint i = 0; i < cfg->number_of_blocks(); ++i) {65const Block* const block = cfg->get_block(i);66for (uint j = 0; j < block->number_of_nodes(); ++j) {67const Node* const node = block->get_node(j);68if (!node->is_Mach()) {69continue;70}71MachNode* m = node->as_Mach();72if (is_jcc_erratum_branch(m)) {73// Found a root jcc erratum branch, flag it as problematic74nop_size += jcc_erratum_taint_node(m, regalloc);7576if (!m->is_MachReturn() && !m->is_MachCall()) {77// We might fuse a problematic jcc erratum branch with a preceding78// ALU instruction - we must catch such problematic macro fusions79// and flag the ALU instruction as problematic too.80for (uint k = 1; k < m->req(); ++k) {81const Node* const use = m->in(k);82if (use == last_m && !m->is_MachReturn()) {83// Flag fused conditions too84nop_size += jcc_erratum_taint_node(last_m, regalloc);85}86}87}88last_m = NULL;89} else {90last_m = m;91}92}93}94return nop_size;95}9697int IntelJccErratum::compute_padding(uintptr_t current_offset, const MachNode* mach, Block* block, uint index_in_block, PhaseRegAlloc* regalloc) {98int jcc_size = mach->size(regalloc);99if (index_in_block < block->number_of_nodes() - 1) {100Node* next = block->get_node(index_in_block + 1);101if (next->is_Mach() && (next->as_Mach()->flags() & Node::PD::Flag_intel_jcc_erratum)) {102jcc_size += mach->size(regalloc);103}104}105if (jcc_size > largest_jcc_size()) {106// Let's not try fixing this for nodes that seem unreasonably large107return false;108}109if (is_crossing_or_ending_at_32_byte_boundary(current_offset, current_offset + jcc_size)) {110return int(align_up(current_offset, 32) - current_offset);111} else {112return 0;113}114}115116#define __ _masm.117118uintptr_t IntelJccErratumAlignment::pc() {119return (uintptr_t)__ pc();120}121122IntelJccErratumAlignment::IntelJccErratumAlignment(MacroAssembler& masm, int jcc_size) :123_masm(masm),124_start_pc(pc()) {125if (!VM_Version::has_intel_jcc_erratum()) {126return;127}128129if (Compile::current()->output()->in_scratch_emit_size()) {130// When we measure the size of this 32 byte alignment, we apply a conservative guess.131__ nop(jcc_size);132} else if (IntelJccErratum::is_crossing_or_ending_at_32_byte_boundary(_start_pc, _start_pc + jcc_size)) {133// The affected branch might get slowed down by micro code mitigations134// as it could be susceptible to the erratum. Place nops until the next135// 32 byte boundary to make sure the branch will be cached.136const int alignment_nops = (int)(align_up(_start_pc, 32) - _start_pc);137__ nop(alignment_nops);138_start_pc = pc();139}140}141142IntelJccErratumAlignment::~IntelJccErratumAlignment() {143if (!VM_Version::has_intel_jcc_erratum() ||144Compile::current()->output()->in_scratch_emit_size()) {145return;146}147148assert(!IntelJccErratum::is_crossing_or_ending_at_32_byte_boundary(_start_pc, pc()), "Invalid jcc_size estimate");149}150151152