Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/cpu/x86/vm/c1_MacroAssembler_x86.cpp
32285 views
/*1* Copyright (c) 1999, 2013, 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 "c1/c1_MacroAssembler.hpp"26#include "c1/c1_Runtime1.hpp"27#include "classfile/systemDictionary.hpp"28#include "gc_interface/collectedHeap.hpp"29#include "interpreter/interpreter.hpp"30#include "oops/arrayOop.hpp"31#include "oops/markOop.hpp"32#include "runtime/basicLock.hpp"33#include "runtime/biasedLocking.hpp"34#include "runtime/os.hpp"35#include "runtime/stubRoutines.hpp"3637int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register scratch, Label& slow_case) {38const int aligned_mask = BytesPerWord -1;39const int hdr_offset = oopDesc::mark_offset_in_bytes();40assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction");41assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");42Label done;43int null_check_offset = -1;4445verify_oop(obj);4647// save object being locked into the BasicObjectLock48movptr(Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()), obj);4950if (UseBiasedLocking) {51assert(scratch != noreg, "should have scratch register at this point");52null_check_offset = biased_locking_enter(disp_hdr, obj, hdr, scratch, false, done, &slow_case);53} else {54null_check_offset = offset();55}5657// Load object header58movptr(hdr, Address(obj, hdr_offset));59// and mark it as unlocked60orptr(hdr, markOopDesc::unlocked_value);61// save unlocked object header into the displaced header location on the stack62movptr(Address(disp_hdr, 0), hdr);63// test if object header is still the same (i.e. unlocked), and if so, store the64// displaced header address in the object header - if it is not the same, get the65// object header instead66if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg!67cmpxchgptr(disp_hdr, Address(obj, hdr_offset));68// if the object header was the same, we're done69if (PrintBiasedLockingStatistics) {70cond_inc32(Assembler::equal,71ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr()));72}73jcc(Assembler::equal, done);74// if the object header was not the same, it is now in the hdr register75// => test if it is a stack pointer into the same stack (recursive locking), i.e.:76//77// 1) (hdr & aligned_mask) == 078// 2) rsp <= hdr79// 3) hdr <= rsp + page_size80//81// these 3 tests can be done by evaluating the following expression:82//83// (hdr - rsp) & (aligned_mask - page_size)84//85// assuming both the stack pointer and page_size have their least86// significant 2 bits cleared and page_size is a power of 287subptr(hdr, rsp);88andptr(hdr, aligned_mask - os::vm_page_size());89// for recursive locking, the result is zero => save it in the displaced header90// location (NULL in the displaced hdr location indicates recursive locking)91movptr(Address(disp_hdr, 0), hdr);92// otherwise we don't care about the result and handle locking via runtime call93jcc(Assembler::notZero, slow_case);94// done95bind(done);96return null_check_offset;97}9899100void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {101const int aligned_mask = BytesPerWord -1;102const int hdr_offset = oopDesc::mark_offset_in_bytes();103assert(disp_hdr == rax, "disp_hdr must be rax, for the cmpxchg instruction");104assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");105Label done;106107if (UseBiasedLocking) {108// load object109movptr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));110biased_locking_exit(obj, hdr, done);111}112113// load displaced header114movptr(hdr, Address(disp_hdr, 0));115// if the loaded hdr is NULL we had recursive locking116testptr(hdr, hdr);117// if we had recursive locking, we are done118jcc(Assembler::zero, done);119if (!UseBiasedLocking) {120// load object121movptr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));122}123verify_oop(obj);124// test if object header is pointing to the displaced header, and if so, restore125// the displaced header in the object - if the object header is not pointing to126// the displaced header, get the object header instead127if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg!128cmpxchgptr(hdr, Address(obj, hdr_offset));129// if the object header was not pointing to the displaced header,130// we do unlocking via runtime call131jcc(Assembler::notEqual, slow_case);132// done133bind(done);134}135136137// Defines obj, preserves var_size_in_bytes138void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {139if (UseTLAB) {140tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);141} else {142eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);143incr_allocated_bytes(noreg, var_size_in_bytes, con_size_in_bytes, t1);144}145}146147148void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {149assert_different_registers(obj, klass, len);150if (UseBiasedLocking && !len->is_valid()) {151assert_different_registers(obj, klass, len, t1, t2);152movptr(t1, Address(klass, Klass::prototype_header_offset()));153movptr(Address(obj, oopDesc::mark_offset_in_bytes()), t1);154} else {155// This assumes that all prototype bits fit in an int32_t156movptr(Address(obj, oopDesc::mark_offset_in_bytes ()), (int32_t)(intptr_t)markOopDesc::prototype());157}158#ifdef _LP64159if (UseCompressedClassPointers) { // Take care not to kill klass160movptr(t1, klass);161encode_klass_not_null(t1);162movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1);163} else164#endif165{166movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass);167}168169if (len->is_valid()) {170movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len);171}172#ifdef _LP64173else if (UseCompressedClassPointers) {174xorptr(t1, t1);175store_klass_gap(obj, t1);176}177#endif178}179180181// preserves obj, destroys len_in_bytes182void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) {183Label done;184assert(obj != len_in_bytes && obj != t1 && t1 != len_in_bytes, "registers must be different");185assert((hdr_size_in_bytes & (BytesPerWord - 1)) == 0, "header size is not a multiple of BytesPerWord");186Register index = len_in_bytes;187// index is positive and ptr sized188subptr(index, hdr_size_in_bytes);189jcc(Assembler::zero, done);190// initialize topmost word, divide index by 2, check if odd and test if zero191// note: for the remaining code to work, index must be a multiple of BytesPerWord192#ifdef ASSERT193{ Label L;194testptr(index, BytesPerWord - 1);195jcc(Assembler::zero, L);196stop("index is not a multiple of BytesPerWord");197bind(L);198}199#endif200xorptr(t1, t1); // use _zero reg to clear memory (shorter code)201if (UseIncDec) {202shrptr(index, 3); // divide by 8/16 and set carry flag if bit 2 was set203} else {204shrptr(index, 2); // use 2 instructions to avoid partial flag stall205shrptr(index, 1);206}207#ifndef _LP64208// index could have been not a multiple of 8 (i.e., bit 2 was set)209{ Label even;210// note: if index was a multiple of 8, than it cannot211// be 0 now otherwise it must have been 0 before212// => if it is even, we don't need to check for 0 again213jcc(Assembler::carryClear, even);214// clear topmost word (no jump needed if conditional assignment would work here)215movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 0*BytesPerWord), t1);216// index could be 0 now, need to check again217jcc(Assembler::zero, done);218bind(even);219}220#endif // !_LP64221// initialize remaining object fields: rdx is a multiple of 2 now222{ Label loop;223bind(loop);224movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 1*BytesPerWord), t1);225NOT_LP64(movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 2*BytesPerWord), t1);)226decrement(index);227jcc(Assembler::notZero, loop);228}229230// done231bind(done);232}233234235void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {236assert(obj == rax, "obj must be in rax, for cmpxchg");237assert_different_registers(obj, t1, t2); // XXX really?238assert(header_size >= 0 && object_size >= header_size, "illegal sizes");239240try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);241242initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2);243}244245void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2) {246assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,247"con_size_in_bytes is not multiple of alignment");248const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;249250initialize_header(obj, klass, noreg, t1, t2);251252// clear rest of allocated space253const Register t1_zero = t1;254const Register index = t2;255const int threshold = 6 * BytesPerWord; // approximate break even point for code size (see comments below)256if (var_size_in_bytes != noreg) {257mov(index, var_size_in_bytes);258initialize_body(obj, index, hdr_size_in_bytes, t1_zero);259} else if (con_size_in_bytes <= threshold) {260// use explicit null stores261// code size = 2 + 3*n bytes (n = number of fields to clear)262xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code)263for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord)264movptr(Address(obj, i), t1_zero);265} else if (con_size_in_bytes > hdr_size_in_bytes) {266// use loop to null out the fields267// code size = 16 bytes for even n (n = number of fields to clear)268// initialize last object field first if odd number of fields269xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code)270movptr(index, (con_size_in_bytes - hdr_size_in_bytes) >> 3);271// initialize last object field if constant size is odd272if (((con_size_in_bytes - hdr_size_in_bytes) & 4) != 0)273movptr(Address(obj, con_size_in_bytes - (1*BytesPerWord)), t1_zero);274// initialize remaining object fields: rdx is a multiple of 2275{ Label loop;276bind(loop);277movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - (1*BytesPerWord)),278t1_zero);279NOT_LP64(movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - (2*BytesPerWord)),280t1_zero);)281decrement(index);282jcc(Assembler::notZero, loop);283}284}285286if (CURRENT_ENV->dtrace_alloc_probes()) {287assert(obj == rax, "must be");288call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));289}290291verify_oop(obj);292}293294void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, Address::ScaleFactor f, Register klass, Label& slow_case) {295assert(obj == rax, "obj must be in rax, for cmpxchg");296assert_different_registers(obj, len, t1, t2, klass);297298// determine alignment mask299assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");300301// check for negative or excessive length302cmpptr(len, (int32_t)max_array_allocation_length);303jcc(Assembler::above, slow_case);304305const Register arr_size = t2; // okay to be the same306// align object end307movptr(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask);308lea(arr_size, Address(arr_size, len, f));309andptr(arr_size, ~MinObjAlignmentInBytesMask);310311try_allocate(obj, arr_size, 0, t1, t2, slow_case);312313initialize_header(obj, klass, len, t1, t2);314315// clear rest of allocated space316const Register len_zero = len;317initialize_body(obj, arr_size, header_size * BytesPerWord, len_zero);318319if (CURRENT_ENV->dtrace_alloc_probes()) {320assert(obj == rax, "must be");321call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));322}323324verify_oop(obj);325}326327328329void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {330verify_oop(receiver);331// explicit NULL check not needed since load from [klass_offset] causes a trap332// check against inline cache333assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check");334int start_offset = offset();335336if (UseCompressedClassPointers) {337load_klass(rscratch1, receiver);338cmpptr(rscratch1, iCache);339} else {340cmpptr(iCache, Address(receiver, oopDesc::klass_offset_in_bytes()));341}342// if icache check fails, then jump to runtime routine343// Note: RECEIVER must still contain the receiver!344jump_cc(Assembler::notEqual,345RuntimeAddress(SharedRuntime::get_ic_miss_stub()));346const int ic_cmp_size = LP64_ONLY(10) NOT_LP64(9);347assert(UseCompressedClassPointers || offset() - start_offset == ic_cmp_size, "check alignment in emit_method_entry");348}349350351void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) {352assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");353// Make sure there is enough stack space for this method's activation.354// Note that we do this before doing an enter(). This matches the355// ordering of C2's stack overflow check / rsp decrement and allows356// the SharedRuntime stack overflow handling to be consistent357// between the two compilers.358generate_stack_overflow_check(bang_size_in_bytes);359360push(rbp);361if (PreserveFramePointer) {362mov(rbp, rsp);363}364#ifdef TIERED365// c2 leaves fpu stack dirty. Clean it on entry366if (UseSSE < 2 ) {367empty_FPU_stack();368}369#endif // TIERED370decrement(rsp, frame_size_in_bytes); // does not emit code for frame_size == 0371}372373374void C1_MacroAssembler::remove_frame(int frame_size_in_bytes) {375increment(rsp, frame_size_in_bytes); // Does not emit code for frame_size == 0376pop(rbp);377}378379380void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) {381if (C1Breakpoint) int3();382inline_cache_check(receiver, ic_klass);383}384385386void C1_MacroAssembler::verified_entry() {387if (C1Breakpoint || VerifyFPU || !UseStackBanging) {388// Verified Entry first instruction should be 5 bytes long for correct389// patching by patch_verified_entry().390//391// C1Breakpoint and VerifyFPU have one byte first instruction.392// Also first instruction will be one byte "push(rbp)" if stack banging393// code is not generated (see build_frame() above).394// For all these cases generate long instruction first.395fat_nop();396}397if (C1Breakpoint)int3();398// build frame399verify_FPU(0, "method_entry");400}401402403#ifndef PRODUCT404405void C1_MacroAssembler::verify_stack_oop(int stack_offset) {406if (!VerifyOops) return;407verify_oop_addr(Address(rsp, stack_offset));408}409410void C1_MacroAssembler::verify_not_null_oop(Register r) {411if (!VerifyOops) return;412Label not_null;413testptr(r, r);414jcc(Assembler::notZero, not_null);415stop("non-null oop required");416bind(not_null);417verify_oop(r);418}419420void C1_MacroAssembler::invalidate_registers(bool inv_rax, bool inv_rbx, bool inv_rcx, bool inv_rdx, bool inv_rsi, bool inv_rdi) {421#ifdef ASSERT422if (inv_rax) movptr(rax, 0xDEAD);423if (inv_rbx) movptr(rbx, 0xDEAD);424if (inv_rcx) movptr(rcx, 0xDEAD);425if (inv_rdx) movptr(rdx, 0xDEAD);426if (inv_rsi) movptr(rsi, 0xDEAD);427if (inv_rdi) movptr(rdi, 0xDEAD);428#endif429}430431#endif // ifndef PRODUCT432433434