< prev index next >

src/hotspot/cpu/x86/c1_MacroAssembler_x86.cpp

Print this page

 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "c1/c1_MacroAssembler.hpp"
 27 #include "c1/c1_Runtime1.hpp"
 28 #include "code/compiledIC.hpp"
 29 #include "compiler/compilerDefinitions.inline.hpp"
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "gc/shared/barrierSetAssembler.hpp"
 32 #include "gc/shared/collectedHeap.hpp"
 33 #include "gc/shared/tlab_globals.hpp"
 34 #include "interpreter/interpreter.hpp"
 35 #include "oops/arrayOop.hpp"
 36 #include "oops/markWord.hpp"
 37 #include "runtime/basicLock.hpp"

 38 #include "runtime/globals.hpp"
 39 #include "runtime/os.hpp"
 40 #include "runtime/sharedRuntime.hpp"
 41 #include "runtime/stubRoutines.hpp"
 42 #include "utilities/checkedCast.hpp"
 43 #include "utilities/globalDefinitions.hpp"
 44 
 45 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register tmp, Label& slow_case) {
 46   const int aligned_mask = BytesPerWord -1;
 47   const int hdr_offset = oopDesc::mark_offset_in_bytes();
 48   assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction");
 49   assert_different_registers(hdr, obj, disp_hdr, tmp);
 50   int null_check_offset = -1;
 51 
 52   verify_oop(obj);
 53 
 54   // save object being locked into the BasicObjectLock
 55   movptr(Address(disp_hdr, BasicObjectLock::obj_offset()), obj);
 56 
 57   null_check_offset = offset();

 61     testb(Address(hdr, Klass::misc_flags_offset()), KlassFlags::_misc_is_value_based_class);
 62     jcc(Assembler::notZero, slow_case);
 63   }
 64 
 65   if (LockingMode == LM_LIGHTWEIGHT) {
 66 #ifdef _LP64
 67     const Register thread = r15_thread;
 68     lightweight_lock(disp_hdr, obj, hdr, thread, tmp, slow_case);
 69 #else
 70     // Implicit null check.
 71     movptr(hdr, Address(obj, oopDesc::mark_offset_in_bytes()));
 72     // Lacking registers and thread on x86_32. Always take slow path.
 73     jmp(slow_case);
 74 #endif
 75   } else  if (LockingMode == LM_LEGACY) {
 76     Label done;
 77     // Load object header
 78     movptr(hdr, Address(obj, hdr_offset));
 79     // and mark it as unlocked
 80     orptr(hdr, markWord::unlocked_value);




 81     // save unlocked object header into the displaced header location on the stack
 82     movptr(Address(disp_hdr, 0), hdr);
 83     // test if object header is still the same (i.e. unlocked), and if so, store the
 84     // displaced header address in the object header - if it is not the same, get the
 85     // object header instead
 86     MacroAssembler::lock(); // must be immediately before cmpxchg!
 87     cmpxchgptr(disp_hdr, Address(obj, hdr_offset));
 88     // if the object header was the same, we're done
 89     jcc(Assembler::equal, done);
 90     // if the object header was not the same, it is now in the hdr register
 91     // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
 92     //
 93     // 1) (hdr & aligned_mask) == 0
 94     // 2) rsp <= hdr
 95     // 3) hdr <= rsp + page_size
 96     //
 97     // these 3 tests can be done by evaluating the following expression:
 98     //
 99     // (hdr - rsp) & (aligned_mask - page_size)
100     //

154     jcc(Assembler::notEqual, slow_case);
155     // done
156   }
157   bind(done);
158   dec_held_monitor_count();
159 }
160 
161 
162 // Defines obj, preserves var_size_in_bytes
163 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
164   if (UseTLAB) {
165     tlab_allocate(noreg, obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
166   } else {
167     jmp(slow_case);
168   }
169 }
170 
171 
172 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
173   assert_different_registers(obj, klass, len);
174   movptr(Address(obj, oopDesc::mark_offset_in_bytes()), checked_cast<int32_t>(markWord::prototype().value()));








175 #ifdef _LP64
176   if (UseCompressedClassPointers) { // Take care not to kill klass
177     movptr(t1, klass);
178     encode_klass_not_null(t1, rscratch1);
179     movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1);
180   } else
181 #endif
182   {
183     movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass);
184   }
185 
186   if (len->is_valid()) {
187     movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len);
188 #ifdef _LP64
189     int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt;
190     if (!is_aligned(base_offset, BytesPerWord)) {
191       assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned");
192       // Clear gap/first 4 bytes following the length field.
193       xorl(t1, t1);
194       movl(Address(obj, base_offset), t1);

298 
299   initialize_header(obj, klass, len, t1, t2);
300 
301   // clear rest of allocated space
302   if (zero_array) {
303     const Register len_zero = len;
304     // Align-up to word boundary, because we clear the 4 bytes potentially
305     // following the length field in initialize_header().
306     int base_offset = align_up(base_offset_in_bytes, BytesPerWord);
307     initialize_body(obj, arr_size, base_offset, len_zero);
308   }
309 
310   if (CURRENT_ENV->dtrace_alloc_probes()) {
311     assert(obj == rax, "must be");
312     call(RuntimeAddress(Runtime1::entry_for(C1StubId::dtrace_object_alloc_id)));
313   }
314 
315   verify_oop(obj);
316 }
317 
318 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) {
319   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
320   // Make sure there is enough stack space for this method's activation.
321   // Note that we do this before doing an enter(). This matches the
322   // ordering of C2's stack overflow check / rsp decrement and allows
323   // the SharedRuntime stack overflow handling to be consistent
324   // between the two compilers.
325   generate_stack_overflow_check(bang_size_in_bytes);
326 
327   push(rbp);
328   if (PreserveFramePointer) {
329     mov(rbp, rsp);
330   }
331 #if !defined(_LP64) && defined(COMPILER2)
332   if (UseSSE < 2 && !CompilerConfig::is_c1_only_no_jvmci()) {
333     // c2 leaves fpu stack dirty. Clean it on entry
334     empty_FPU_stack();
335   }
336 #endif // !_LP64 && COMPILER2
337   decrement(rsp, frame_size_in_bytes); // does not emit code for frame_size == 0























338 
339   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
340   // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
341   bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */);
342 }
343 
344 
345 void C1_MacroAssembler::remove_frame(int frame_size_in_bytes) {
346   increment(rsp, frame_size_in_bytes);  // Does not emit code for frame_size == 0
347   pop(rbp);
348 }
349 
350 
351 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
352   if (breakAtEntry || VerifyFPU) {
353     // Verified Entry first instruction should be 5 bytes long for correct
354     // patching by patch_verified_entry().
355     //
356     // Breakpoint and VerifyFPU have one byte first instruction.
357     // Also first instruction will be one byte "push(rbp)" if stack banging
358     // code is not generated (see build_frame() above).
359     // For all these cases generate long instruction first.
360     fat_nop();
361   }
362   if (breakAtEntry) int3();
363   // build frame
364   IA32_ONLY( verify_FPU(0, "method_entry"); )
365 }
366 


























































367 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
368   // rbp, + 0: link
369   //     + 1: return address
370   //     + 2: argument with offset 0
371   //     + 3: argument with offset 1
372   //     + 4: ...
373 
374   movptr(reg, Address(rbp, (offset_in_words + 2) * BytesPerWord));
375 }
376 
377 #ifndef PRODUCT
378 
379 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
380   if (!VerifyOops) return;
381   verify_oop_addr(Address(rsp, stack_offset));
382 }
383 
384 void C1_MacroAssembler::verify_not_null_oop(Register r) {
385   if (!VerifyOops) return;
386   Label not_null;

 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "c1/c1_MacroAssembler.hpp"
 27 #include "c1/c1_Runtime1.hpp"
 28 #include "code/compiledIC.hpp"
 29 #include "compiler/compilerDefinitions.inline.hpp"
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "gc/shared/barrierSetAssembler.hpp"
 32 #include "gc/shared/collectedHeap.hpp"
 33 #include "gc/shared/tlab_globals.hpp"
 34 #include "interpreter/interpreter.hpp"
 35 #include "oops/arrayOop.hpp"
 36 #include "oops/markWord.hpp"
 37 #include "runtime/basicLock.hpp"
 38 #include "runtime/frame.inline.hpp"
 39 #include "runtime/globals.hpp"
 40 #include "runtime/os.hpp"
 41 #include "runtime/sharedRuntime.hpp"
 42 #include "runtime/stubRoutines.hpp"
 43 #include "utilities/checkedCast.hpp"
 44 #include "utilities/globalDefinitions.hpp"
 45 
 46 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register tmp, Label& slow_case) {
 47   const int aligned_mask = BytesPerWord -1;
 48   const int hdr_offset = oopDesc::mark_offset_in_bytes();
 49   assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction");
 50   assert_different_registers(hdr, obj, disp_hdr, tmp);
 51   int null_check_offset = -1;
 52 
 53   verify_oop(obj);
 54 
 55   // save object being locked into the BasicObjectLock
 56   movptr(Address(disp_hdr, BasicObjectLock::obj_offset()), obj);
 57 
 58   null_check_offset = offset();

 62     testb(Address(hdr, Klass::misc_flags_offset()), KlassFlags::_misc_is_value_based_class);
 63     jcc(Assembler::notZero, slow_case);
 64   }
 65 
 66   if (LockingMode == LM_LIGHTWEIGHT) {
 67 #ifdef _LP64
 68     const Register thread = r15_thread;
 69     lightweight_lock(disp_hdr, obj, hdr, thread, tmp, slow_case);
 70 #else
 71     // Implicit null check.
 72     movptr(hdr, Address(obj, oopDesc::mark_offset_in_bytes()));
 73     // Lacking registers and thread on x86_32. Always take slow path.
 74     jmp(slow_case);
 75 #endif
 76   } else  if (LockingMode == LM_LEGACY) {
 77     Label done;
 78     // Load object header
 79     movptr(hdr, Address(obj, hdr_offset));
 80     // and mark it as unlocked
 81     orptr(hdr, markWord::unlocked_value);
 82     if (EnableValhalla) {
 83       // Mask inline_type bit such that we go to the slow path if object is an inline type
 84       andptr(hdr, ~((int) markWord::inline_type_bit_in_place));
 85     }
 86     // save unlocked object header into the displaced header location on the stack
 87     movptr(Address(disp_hdr, 0), hdr);
 88     // test if object header is still the same (i.e. unlocked), and if so, store the
 89     // displaced header address in the object header - if it is not the same, get the
 90     // object header instead
 91     MacroAssembler::lock(); // must be immediately before cmpxchg!
 92     cmpxchgptr(disp_hdr, Address(obj, hdr_offset));
 93     // if the object header was the same, we're done
 94     jcc(Assembler::equal, done);
 95     // if the object header was not the same, it is now in the hdr register
 96     // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
 97     //
 98     // 1) (hdr & aligned_mask) == 0
 99     // 2) rsp <= hdr
100     // 3) hdr <= rsp + page_size
101     //
102     // these 3 tests can be done by evaluating the following expression:
103     //
104     // (hdr - rsp) & (aligned_mask - page_size)
105     //

159     jcc(Assembler::notEqual, slow_case);
160     // done
161   }
162   bind(done);
163   dec_held_monitor_count();
164 }
165 
166 
167 // Defines obj, preserves var_size_in_bytes
168 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
169   if (UseTLAB) {
170     tlab_allocate(noreg, obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
171   } else {
172     jmp(slow_case);
173   }
174 }
175 
176 
177 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
178   assert_different_registers(obj, klass, len);
179   if (EnableValhalla) {
180     // Need to copy markWord::prototype header for klass
181     assert_different_registers(obj, klass, len, t1, t2);
182     movptr(t1, Address(klass, Klass::prototype_header_offset()));
183     movptr(Address(obj, oopDesc::mark_offset_in_bytes()), t1);
184   } else {
185     // This assumes that all prototype bits fit in an int32_t
186     movptr(Address(obj, oopDesc::mark_offset_in_bytes()), checked_cast<int32_t>(markWord::prototype().value()));
187   }
188 #ifdef _LP64
189   if (UseCompressedClassPointers) { // Take care not to kill klass
190     movptr(t1, klass);
191     encode_klass_not_null(t1, rscratch1);
192     movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1);
193   } else
194 #endif
195   {
196     movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass);
197   }
198 
199   if (len->is_valid()) {
200     movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len);
201 #ifdef _LP64
202     int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt;
203     if (!is_aligned(base_offset, BytesPerWord)) {
204       assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned");
205       // Clear gap/first 4 bytes following the length field.
206       xorl(t1, t1);
207       movl(Address(obj, base_offset), t1);

311 
312   initialize_header(obj, klass, len, t1, t2);
313 
314   // clear rest of allocated space
315   if (zero_array) {
316     const Register len_zero = len;
317     // Align-up to word boundary, because we clear the 4 bytes potentially
318     // following the length field in initialize_header().
319     int base_offset = align_up(base_offset_in_bytes, BytesPerWord);
320     initialize_body(obj, arr_size, base_offset, len_zero);
321   }
322 
323   if (CURRENT_ENV->dtrace_alloc_probes()) {
324     assert(obj == rax, "must be");
325     call(RuntimeAddress(Runtime1::entry_for(C1StubId::dtrace_object_alloc_id)));
326   }
327 
328   verify_oop(obj);
329 }
330 
331 void C1_MacroAssembler::build_frame_helper(int frame_size_in_bytes, int sp_offset_for_orig_pc, int sp_inc, bool reset_orig_pc, bool needs_stack_repair) {








332   push(rbp);
333   if (PreserveFramePointer) {
334     mov(rbp, rsp);
335   }
336 #if !defined(_LP64) && defined(COMPILER2)
337   if (UseSSE < 2 && !CompilerConfig::is_c1_only_no_jvmci()) {
338       // c2 leaves fpu stack dirty. Clean it on entry
339       empty_FPU_stack();
340     }
341 #endif // !_LP64 && COMPILER2
342   decrement(rsp, frame_size_in_bytes);
343 
344   if (needs_stack_repair) {
345     // Save stack increment (also account for fixed framesize and rbp)
346     assert((sp_inc & (StackAlignmentInBytes-1)) == 0, "stack increment not aligned");
347     int real_frame_size = sp_inc + frame_size_in_bytes + wordSize;
348     movptr(Address(rsp, frame_size_in_bytes - wordSize), real_frame_size);
349   }
350   if (reset_orig_pc) {
351     // Zero orig_pc to detect deoptimization during buffering in the entry points
352     movptr(Address(rsp, sp_offset_for_orig_pc), 0);
353   }
354 }
355 
356 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes, int sp_offset_for_orig_pc, bool needs_stack_repair, bool has_scalarized_args, Label* verified_inline_entry_label) {
357   // Make sure there is enough stack space for this method's activation.
358   // Note that we do this before doing an enter(). This matches the
359   // ordering of C2's stack overflow check / rsp decrement and allows
360   // the SharedRuntime stack overflow handling to be consistent
361   // between the two compilers.
362   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
363   generate_stack_overflow_check(bang_size_in_bytes);
364 
365   build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, has_scalarized_args, needs_stack_repair);
366 
367   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
368   // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
369   bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */);

370 
371   if (verified_inline_entry_label != nullptr) {
372     // Jump here from the scalarized entry points that already created the frame.
373     bind(*verified_inline_entry_label);
374   }
375 }
376 

377 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
378   if (breakAtEntry || VerifyFPU) {
379     // Verified Entry first instruction should be 5 bytes long for correct
380     // patching by patch_verified_entry().
381     //
382     // Breakpoint and VerifyFPU have one byte first instruction.
383     // Also first instruction will be one byte "push(rbp)" if stack banging
384     // code is not generated (see build_frame() above).
385     // For all these cases generate long instruction first.
386     fat_nop();
387   }
388   if (breakAtEntry) int3();
389   // build frame
390   IA32_ONLY( verify_FPU(0, "method_entry"); )
391 }
392 
393 int C1_MacroAssembler::scalarized_entry(const CompiledEntrySignature* ces, int frame_size_in_bytes, int bang_size_in_bytes, int sp_offset_for_orig_pc, Label& verified_inline_entry_label, bool is_inline_ro_entry) {
394   assert(InlineTypePassFieldsAsArgs, "sanity");
395   // Make sure there is enough stack space for this method's activation.
396   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
397   generate_stack_overflow_check(bang_size_in_bytes);
398 
399   GrowableArray<SigEntry>* sig    = ces->sig();
400   GrowableArray<SigEntry>* sig_cc = is_inline_ro_entry ? ces->sig_cc_ro() : ces->sig_cc();
401   VMRegPair* regs      = ces->regs();
402   VMRegPair* regs_cc   = is_inline_ro_entry ? ces->regs_cc_ro() : ces->regs_cc();
403   int args_on_stack    = ces->args_on_stack();
404   int args_on_stack_cc = is_inline_ro_entry ? ces->args_on_stack_cc_ro() : ces->args_on_stack_cc();
405 
406   assert(sig->length() <= sig_cc->length(), "Zero-sized inline class not allowed!");
407   BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sig_cc->length());
408   int args_passed = sig->length();
409   int args_passed_cc = SigEntry::fill_sig_bt(sig_cc, sig_bt);
410 
411   // Create a temp frame so we can call into the runtime. It must be properly set up to accommodate GC.
412   build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, true, ces->c1_needs_stack_repair());
413 
414   // The runtime call might safepoint, make sure nmethod entry barrier is executed
415   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
416   // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
417   bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */);
418 
419   // FIXME -- call runtime only if we cannot in-line allocate all the incoming inline type args.
420   movptr(rbx, (intptr_t)(ces->method()));
421   if (is_inline_ro_entry) {
422     call(RuntimeAddress(Runtime1::entry_for(C1StubId::buffer_inline_args_no_receiver_id)));
423   } else {
424     call(RuntimeAddress(Runtime1::entry_for(C1StubId::buffer_inline_args_id)));
425   }
426   int rt_call_offset = offset();
427 
428   // Remove the temp frame
429   addptr(rsp, frame_size_in_bytes);
430   pop(rbp);
431 
432   // Check if we need to extend the stack for packing
433   int sp_inc = 0;
434   if (args_on_stack > args_on_stack_cc) {
435     sp_inc = extend_stack_for_inline_args(args_on_stack);
436   }
437 
438   shuffle_inline_args(true, is_inline_ro_entry, sig_cc,
439                       args_passed_cc, args_on_stack_cc, regs_cc, // from
440                       args_passed, args_on_stack, regs,          // to
441                       sp_inc, rax);
442 
443   // Create the real frame. Below jump will then skip over the stack banging and frame
444   // setup code in the verified_inline_entry (which has a different real_frame_size).
445   build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, sp_inc, false, ces->c1_needs_stack_repair());
446 
447   jmp(verified_inline_entry_label);
448   return rt_call_offset;
449 }
450 
451 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
452   // rbp, + 0: link
453   //     + 1: return address
454   //     + 2: argument with offset 0
455   //     + 3: argument with offset 1
456   //     + 4: ...
457 
458   movptr(reg, Address(rbp, (offset_in_words + 2) * BytesPerWord));
459 }
460 
461 #ifndef PRODUCT
462 
463 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
464   if (!VerifyOops) return;
465   verify_oop_addr(Address(rsp, stack_offset));
466 }
467 
468 void C1_MacroAssembler::verify_not_null_oop(Register r) {
469   if (!VerifyOops) return;
470   Label not_null;
< prev index next >