1 /*
2 * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
4 * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
28 #include "gc/shenandoah/mode/shenandoahMode.hpp"
29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
30 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
32 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
33 #include "gc/shenandoah/shenandoahRuntime.hpp"
34 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
35 #include "interpreter/interp_masm.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "runtime/javaThread.hpp"
38 #include "runtime/sharedRuntime.hpp"
39 #ifdef COMPILER1
40 #include "c1/c1_LIRAssembler.hpp"
41 #include "c1/c1_MacroAssembler.hpp"
42 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
43 #endif
44 #ifdef COMPILER2
45 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
46 #include "opto/output.hpp"
47 #endif
48
49 #define __ masm->
50
51 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
52 Register src, Register dst, Register count, RegSet saved_regs) {
53 if (is_oop) {
54 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
55 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
56
57 Label done;
58
59 // Avoid calling runtime if count == 0
60 __ beqz(count, done);
61
62 // Is GC active?
63 Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
64 assert_different_registers(src, dst, count, t0);
65
66 assert(!saved_regs.contains(t0), "Sanity: about to clobber t0");
67
68 __ lbu(t0, gc_state);
69 if (ShenandoahSATBBarrier && dest_uninitialized) {
70 __ test_bit(t0, t0, ShenandoahHeap::HAS_FORWARDED_BITPOS);
71 __ beqz(t0, done);
72 } else {
73 __ andi(t0, t0, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
74 __ beqz(t0, done);
75 }
76
77 __ push_call_clobbered_registers();
78 // If arguments are not in proper places, shuffle them.
79 // Doing this via the stack is the most straight-forward way to avoid
80 // accidentally smashing any register.
81 if (c_rarg0 != src || c_rarg1 != dst || c_rarg2 != count) {
82 __ push_reg(RegSet::of(src), sp);
83 __ push_reg(RegSet::of(dst), sp);
84 __ push_reg(RegSet::of(count), sp);
85 __ pop_reg(RegSet::of(c_rarg2), sp);
86 __ pop_reg(RegSet::of(c_rarg1), sp);
87 __ pop_reg(RegSet::of(c_rarg0), sp);
88 }
89 address target = nullptr;
90 if (UseCompressedOops) {
91 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop);
92 } else {
93 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop);
94 }
95 __ call_VM_leaf(target, 3);
96 __ pop_call_clobbered_registers();
97 __ bind(done);
98 }
99 }
100 }
101
102 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
103 Register start, Register count, Register tmp) {
104 if (ShenandoahCardBarrier && is_oop) {
105 gen_write_ref_array_post_barrier(masm, decorators, start, count, tmp);
106 }
107 }
108
109 void ShenandoahBarrierSetAssembler::satb_barrier(MacroAssembler* masm,
110 Register obj,
111 Register pre_val,
112 Register thread,
113 Register tmp1,
114 Register tmp2) {
115 assert(ShenandoahSATBBarrier, "Should be checked by caller");
116 assert(thread == xthread, "must be");
117
118 Label done;
119 Label runtime;
120
121 assert_different_registers(obj, pre_val, tmp1, tmp2);
122 assert(pre_val != noreg && tmp1 != noreg && tmp2 != noreg, "expecting a register");
123
124 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
125 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
126
127 // Is marking active?
128 Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
129 __ lbu(t1, gc_state);
130 __ test_bit(t1, t1, ShenandoahHeap::MARKING_BITPOS);
131 __ beqz(t1, done);
132
133 // Do we need to load the previous value?
134 if (obj != noreg) {
135 if (UseCompressedOops) {
136 __ lwu(pre_val, Address(obj, 0));
137 __ decode_heap_oop(pre_val);
138 } else {
139 __ ld(pre_val, Address(obj, 0));
140 }
141 }
142
143 // Is the previous value null?
144 __ beqz(pre_val, done);
145
146 // Can we store original value in the thread's buffer?
147 // Is index == 0?
148 // (The index field is typed as size_t.)
149 __ ld(tmp1, index); // tmp := *index_adr
150 __ beqz(tmp1, runtime); // tmp == 0? If yes, goto runtime
151
152 __ subi(tmp1, tmp1, wordSize); // tmp := tmp - wordSize
153 __ sd(tmp1, index); // *index_adr := tmp
154 __ ld(tmp2, buffer);
155 __ add(tmp1, tmp1, tmp2); // tmp := tmp + *buffer_adr
156
157 // Record the previous value
158 __ sd(pre_val, Address(tmp1, 0));
159 __ j(done);
160
161 // Slow-path call.
162 __ bind(runtime);
163 __ enter();
164 __ push_call_clobbered_registers();
165 if (c_rarg0 != pre_val) {
166 __ mv(c_rarg0, pre_val);
167 }
168 // Calling with super_call_VM_leaf with c_rarg0 bypasses interpreter checks and avoids any moves.
169 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), c_rarg0);
170 __ pop_call_clobbered_registers();
171 __ leave();
172
173 __ bind(done);
174 }
175
176 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm,
177 Register dst,
178 Address load_addr,
179 DecoratorSet decorators) {
180 assert(ShenandoahLoadRefBarrier, "Should be enabled");
181 assert(dst != t1 && load_addr.base() != t1, "need t1");
182 assert_different_registers(load_addr.base(), t0, t1);
183
184 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
185 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
186 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
187 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
188 bool is_narrow = UseCompressedOops && !is_native;
189
190 Label heap_stable, not_cset;
191 Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
192 __ lbu(t1, gc_state);
193
194 // Check for heap stability
195 if (is_strong) {
196 __ test_bit(t1, t1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
197 __ beqz(t1, heap_stable);
198 } else {
199 Label lrb;
200 __ test_bit(t0, t1, ShenandoahHeap::WEAK_ROOTS_BITPOS);
201 __ bnez(t0, lrb);
202 __ test_bit(t0, t1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
203 __ beqz(t0, heap_stable);
204 __ bind(lrb);
205 }
206
207 // use x11 for load address
208 Register result_dst = dst;
209 if (dst == x11) {
210 __ mv(t1, dst);
211 dst = t1;
212 }
213
214 // Save x10 and x11, unless it is an output register
215 RegSet saved_regs = RegSet::of(x10, x11) - result_dst;
216 __ push_reg(saved_regs, sp);
217 __ la(x11, load_addr);
218 __ mv(x10, dst);
219
220 // Test for in-cset
221 if (is_strong) {
222 __ mv(t1, ShenandoahHeap::in_cset_fast_test_addr());
223 __ srli(t0, x10, ShenandoahHeapRegion::region_size_bytes_shift_jint());
224 __ add(t1, t1, t0);
225 __ lbu(t1, Address(t1));
226 __ test_bit(t0, t1, 0);
227 __ beqz(t0, not_cset);
228 }
229
230 // Slow-path call
231 __ enter();
232 __ push_call_clobbered_registers();
233 address target = nullptr;
234 if (is_strong) {
235 if (is_narrow) {
236 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
237 } else {
238 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
239 }
240 } else if (is_weak) {
241 if (is_narrow) {
242 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
243 } else {
244 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
245 }
246 } else {
247 assert(is_phantom, "only remaining strength");
248 assert(!is_narrow, "phantom access cannot be narrow");
249 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
250 }
251 // Calling with super_call_VM_leaf with c_rarg0/1 bypasses interpreter checks and avoids any moves.
252 __ super_call_VM_leaf(target, c_rarg0, c_rarg1);
253 __ mv(t0, x10);
254 __ pop_call_clobbered_registers();
255 __ mv(x10, t0);
256 __ leave();
257
258 __ bind(not_cset);
259 __ mv(result_dst, x10);
260 __ pop_reg(saved_regs, sp);
261
262 __ bind(heap_stable);
263 }
264
265 //
266 // Arguments:
267 //
268 // Inputs:
269 // src: oop location to load from, might be clobbered
270 //
271 // Output:
272 // dst: oop loaded from src location
273 //
274 // Kill:
275 // x30 (tmp reg)
276 //
277 // Alias:
278 // dst: x30 (might use x30 as temporary output register to avoid clobbering src)
279 //
280 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm,
281 DecoratorSet decorators,
282 BasicType type,
283 Register dst,
284 Address src,
285 Register tmp1,
286 Register tmp2) {
287 // 1: non-reference load, no additional barrier is needed
288 if (!is_reference_type(type)) {
289 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
290 return;
291 }
292
293 // 2: load a reference from src location and apply LRB if needed
294 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
295 Register result_dst = dst;
296
297 // Preserve src location for LRB
298 RegSet saved_regs;
299 if (dst == src.base()) {
300 dst = (src.base() == x28) ? x29 : x28;
301 saved_regs = RegSet::of(dst);
302 __ push_reg(saved_regs, sp);
303 }
304 assert_different_registers(dst, src.base());
305
306 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
307
308 load_reference_barrier(masm, dst, src, decorators);
309
310 if (dst != result_dst) {
311 __ mv(result_dst, dst);
312 dst = result_dst;
313 }
314
315 if (saved_regs.bits() != 0) {
316 __ pop_reg(saved_regs, sp);
317 }
318 } else {
319 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
320 }
321
322 // 3: apply keep-alive barrier if needed
323 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
324 satb_barrier(masm /* masm */,
325 noreg /* obj */,
326 dst /* pre_val */,
327 xthread /* thread */,
328 tmp1 /* tmp1 */,
329 tmp2 /* tmp2 */);
330 }
331 }
332
333 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj) {
334 assert(ShenandoahCardBarrier, "Should have been checked by caller");
335
336 __ srli(obj, obj, CardTable::card_shift());
337
338 assert(CardTable::dirty_card_val() == 0, "must be");
339
340 Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
341 __ ld(t1, curr_ct_holder_addr);
342 __ add(t1, obj, t1);
343
344 if (UseCondCardMark) {
345 Label L_already_dirty;
346 __ lbu(t0, Address(t1));
347 __ beqz(t0, L_already_dirty);
348 __ sb(zr, Address(t1));
349 __ bind(L_already_dirty);
350 } else {
351 __ sb(zr, Address(t1));
352 }
353 }
354
355 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
356 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
357 // 1: non-reference types require no barriers
358 if (!is_reference_type(type)) {
359 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
360 return;
361 }
362
363 // Flatten object address right away for simplicity: likely needed by barriers
364 if (dst.offset() == 0) {
365 if (dst.base() != tmp3) {
366 __ mv(tmp3, dst.base());
367 }
368 } else {
369 __ la(tmp3, dst);
370 }
371
372 // 2: pre-barrier: SATB needs the previous value
373 if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
374 satb_barrier(masm,
375 tmp3 /* obj */,
376 tmp2 /* pre_val */,
377 xthread /* thread */,
378 tmp1 /* tmp */,
379 t0 /* tmp2 */);
380 }
381
382 // Store!
383 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg);
384
385 // 3: post-barrier: card barrier needs store address
386 bool storing_non_null = (val != noreg);
387 if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
388 card_barrier(masm, tmp3);
389 }
390 }
391
392 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
393 Register obj, Register tmp, Label& slowpath) {
394 Label done;
395 // Resolve jobject
396 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
397
398 // Check for null.
399 __ beqz(obj, done);
400
401 assert(obj != t1, "need t1");
402 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
403 __ lbu(t1, gc_state);
404
405 // Check for heap in evacuation phase
406 __ test_bit(t0, t1, ShenandoahHeap::EVACUATION_BITPOS);
407 __ bnez(t0, slowpath);
408
409 __ bind(done);
410 }
411
412 void ShenandoahBarrierSetAssembler::try_peek_weak_handle_in_nmethod(MacroAssembler *masm, Register weak_handle,
413 Register obj, Register tmp, Label& slow_path) {
414 assert_different_registers(weak_handle, tmp, noreg);
415 assert_different_registers(obj, tmp, noreg);
416
417
418 Label done;
419
420 // Peek weak handle using the standard implementation.
421 BarrierSetAssembler::try_peek_weak_handle_in_nmethod(masm, weak_handle, obj, tmp, slow_path);
422
423 // Check if the reference is null, and if it is, take the fast path.
424 __ beqz(obj, done);
425
426 Address gc_state(xthread, ShenandoahThreadLocalData::gc_state_offset());
427 __ lbu(tmp, gc_state);
428
429 // Check if the heap is under weak-reference/roots processing, in
430 // which case we need to take the slow path.
431 __ test_bit(tmp, tmp, ShenandoahHeap::WEAK_ROOTS_BITPOS);
432 __ bnez(tmp, slow_path);
433 __ bind(done);
434 }
435
436 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
437 Register start, Register count, Register tmp) {
438 assert(ShenandoahCardBarrier, "Did you mean to enable ShenandoahCardBarrier?");
439
440 Label L_loop, L_done;
441 const Register end = count;
442
443 // Zero count? Nothing to do.
444 __ beqz(count, L_done);
445
446 // end = start + count << LogBytesPerHeapOop
447 // last element address to make inclusive
448 __ shadd(end, count, start, tmp, LogBytesPerHeapOop);
449 __ subi(end, end, BytesPerHeapOop);
450 __ srli(start, start, CardTable::card_shift());
451 __ srli(end, end, CardTable::card_shift());
452
453 // number of bytes to copy
454 __ sub(count, end, start);
455
456 Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
457 __ ld(tmp, curr_ct_holder_addr);
458 __ add(start, start, tmp);
459
460 __ bind(L_loop);
461 __ add(tmp, start, count);
462 __ sb(zr, Address(tmp));
463 __ subi(count, count, 1);
464 __ bgez(count, L_loop);
465 __ bind(L_done);
466 }
467
468 #undef __
469
470 #ifdef COMPILER1
471
472 #define __ ce->masm()->
473
474 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_stub(LIR_Assembler* ce, ShenandoahKeepaliveBarrierStub* stub) {
475 __ bind(*stub->entry());
476
477 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
478
479 Register obj = stub->obj()->as_register();
480
481 if (stub->do_load()) {
482 ce->mem2reg(stub->addr(), stub->obj(), T_OBJECT, lir_patch_none, nullptr, false /* wide */);
483 }
484 __ beqz(obj, *stub->continuation(), /* is_far */ true);
485
486 ce->store_parameter(obj, 0);
487 __ far_call(RuntimeAddress(bs->keepalive_barrier_stub()));
488 __ j(*stub->continuation());
489 }
490
491 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
492 __ bind(*stub->entry());
493
494 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*) BarrierSet::barrier_set()->barrier_set_c1();
495
496 Register obj = stub->obj()->as_register();
497 Register addr = stub->addr()->as_pointer_register();
498 Register slow_result = stub->slow_result()->as_register();
499 assert_different_registers(obj, addr, slow_result);
500 assert(slow_result == x10, "C1 must know about our slow call result register");
501
502 ce->store_parameter(obj, 0);
503 ce->store_parameter(addr, 1);
504 __ far_call(RuntimeAddress(bs->load_reference_barrier_stub(stub->decorators())));
505 if (obj != slow_result) {
506 __ mv(obj, slow_result);
507 }
508
509 __ j(*stub->continuation());
510 }
511
512 #undef __
513
514 #define __ sasm->
515
516 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_runtime_stub(StubAssembler* sasm) {
517 __ prologue("shenandoah_keepalive_barrier", false);
518 const Register tmp_obj = x10;
519 const Register tmp1 = x11;
520 const Register tmp2 = x12;
521 __ push_reg(RegSet::of(tmp1, tmp2, tmp_obj), sp);
522 __ load_parameter(0, tmp_obj);
523 satb_barrier(sasm, noreg, tmp_obj, xthread, tmp1, tmp2);
524 __ pop_reg(RegSet::of(tmp1, tmp2, tmp_obj), sp);
525 __ epilogue();
526 }
527
528 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
529 __ prologue("shenandoah_load_reference_barrier", false);
530 const Register tmp_obj = x10;
531 const Register tmp_addr = x11;
532 __ push_reg(RegSet::of(tmp_addr), sp);
533 __ load_parameter(0, tmp_obj);
534 __ load_parameter(1, tmp_addr);
535 load_reference_barrier(sasm, tmp_obj, Address(tmp_addr, 0), decorators);
536 __ pop_reg(RegSet::of(tmp_addr), sp);
537 __ epilogue();
538 }
539
540 #undef __
541
542 #endif // COMPILER1
543
544 #ifdef COMPILER2
545
546 #undef __
547 #define __ masm->
548
549 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, Register tmp1, Register tmp2, bool is_narrow) {
550 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
551 if (is_narrow) {
552 __ lwu(dst, src);
553 } else {
554 __ ld(dst, src);
555 }
556
557 ShenandoahBarrierStubC2::load_post(masm, node, dst, src, tmp1, tmp2, is_narrow);
558 }
559
560 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm, Address dst, bool dst_narrow,
561 Register src, bool src_narrow, Register tmp1, Register tmp2, Register tmp3) {
562
563 ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp1, tmp2, tmp3, dst_narrow);
564
565 // Do the actual store
566 if (dst_narrow) {
567 if (!src_narrow) {
568 // Need to encode into tmp, because we cannot clobber src.
569 assert(tmp1 != noreg, "need temp register");
570 if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
571 __ encode_heap_oop(tmp1, src);
572 } else {
573 __ encode_heap_oop_not_null(tmp1, src);
574 }
575 src = tmp1;
576 }
577 __ sw(src, dst);
578 } else {
579 __ sd(src, dst);
580 }
581
582 ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp2, tmp3);
583 }
584
585 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm, Register res, Register addr,
586 Register oldval, Register newval, Register tmp1, Register tmp2, Register tmp3, bool exchange, bool narrow, bool is_acquire) {
587 const Assembler::Aqrl acquire = is_acquire ? Assembler::aq : Assembler::relaxed;
588 const Assembler::Aqrl release = Assembler::rl;
589 const Assembler::operand_size size = narrow ? Assembler::uint32 : Assembler::int64;
590
591 ShenandoahBarrierStubC2::load_store_pre(masm, node, Address(addr), tmp1, tmp2, tmp3, narrow);
592
593 // CAS!
594 __ cmpxchg(addr, oldval, newval, size, acquire, release, /* result */ res, !exchange /* result_as_bool */);
595
596 ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), tmp2, tmp3);
597 }
598
599 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register preval,
600 Register newval, Register addr, Register tmp1, Register tmp2, Register tmp3, bool is_acquire) {
601 const bool is_narrow = node->bottom_type()->isa_narrowoop();
602
603 ShenandoahBarrierStubC2::load_store_pre(masm, node, Address(addr, 0), tmp1, tmp2, tmp3, is_narrow);
604
605 if (is_narrow) {
606 if (is_acquire) {
607 __ atomic_xchgalwu(preval, newval, addr);
608 } else {
609 __ atomic_xchgwu(preval, newval, addr);
610 }
611 } else {
612 if (is_acquire) {
613 __ atomic_xchgal(preval, newval, addr);
614 } else {
615 __ atomic_xchg(preval, newval, addr);
616 }
617 }
618
619 ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), tmp2, tmp3);
620 }
621
622 #undef __
623 #define __ masm.
624
625 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address address, Register tmp1, Register tmp2) {
626 assert(CardTable::dirty_card_val() == 0, "must be");
627 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
628
629 // tmp1 = card table base (holder)
630 Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
631 __ ld(tmp1, curr_ct_holder_addr);
632
633 // tmp1 = effective address
634 __ la(tmp2, address);
635
636 // tmp2 = &card_table[ addr >> CardTable::card_shift() ] ; card index
637 __ srli(tmp2, tmp2, CardTable::card_shift());
638 __ add(tmp2, tmp2, tmp1);
639
640 if (UseCondCardMark) {
641 Label L_already_dirty;
642 __ lbu(tmp1, Address(tmp2));
643 __ beqz(tmp1, L_already_dirty);
644 __ sb(zr, Address(tmp2));
645 __ bind(L_already_dirty);
646 } else {
647 __ sb(zr, Address(tmp2));
648 }
649 }
650
651 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp) {
652 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
653
654 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(test_state)));
655 __ lbu(tmp, gc_state_fast);
656 __ beqz(tmp, *continuation());
657 __ j(*entry());
658
659 // This is were the slowpath stub will return to or the code above will
660 // jump to if the checks are false
661 __ bind(*continuation());
662 }
663
664 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
665 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
666 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
667
668 __ bind(*entry());
669
670 // If we need to load ourselves, do it here.
671 if (_do_load) {
672 if (_narrow) {
673 __ lwu(_obj, _addr);
674 } else {
675 __ ld(_obj, _addr);
676 }
677 }
678
679 // If the object is null, there is no point in applying barriers.
680 maybe_far_jump_if_zero(masm, _obj);
681
682 // We need to make sure that loads done by callers survive across slow-path calls.
683 // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
684 bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
685 if (!_do_load || needs_both_barriers) {
686 preserve(_obj);
687 }
688
689 // Go for barriers. Barriers can return straight to continuation, as long
690 // as another barrier is not needed and we can reach the fastpath.
691 if (needs_both_barriers) {
692 keepalive(masm, nullptr);
693 lrb(masm);
694 } else if (_needs_keep_alive_barrier) {
695 keepalive(masm, continuation());
696 } else if (_needs_load_ref_barrier) {
697 lrb(masm);
698 } else {
699 ShouldNotReachHere();
700 }
701 }
702
703 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
704 Label L_short_jump;
705 __ bnez(reg, L_short_jump);
706 __ j(*continuation());
707 __ bind(L_short_jump);
708 }
709
710 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
711 Address index(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
712 Address buffer(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
713 Label L_through, L_slowpath;
714
715 // If another barrier is enabled as well, do a runtime check for a specific barrier.
716 if (_needs_load_ref_barrier) {
717 assert(L_done == nullptr, "L_done is always null when _needs_load_ref_barrier is true");
718 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::MARKING)));
719 __ lbu(_tmp1, gc_state_fast);
720 __ beqz(_tmp1, L_through);
721 }
722
723 // Fast-path: put object into buffer.
724 // If buffer is already full, go slow.
725 __ ld(_tmp1, index);
726 __ beqz(_tmp1, L_slowpath);
727 __ subi(_tmp1, _tmp1, wordSize);
728 __ sd(_tmp1, index);
729 __ ld(_tmp2, buffer);
730
731 // Store the object in queue.
732 // If object is narrow, we need to decode it before inserting.
733 __ add(_tmp1, _tmp1, _tmp2);
734 if (_narrow) {
735 __ decode_heap_oop_not_null(_tmp2, _obj);
736 __ sd(_tmp2, Address(_tmp1));
737 } else {
738 __ sd(_obj, Address(_tmp1));
739 }
740
741 // Fast-path exits here.
742 if (L_done != nullptr) {
743 __ j(*L_done);
744 } else {
745 __ j(L_through);
746 }
747
748 // Slow-path: call runtime to handle.
749 __ bind(L_slowpath);
750
751 {
752 SaveLiveRegisters slr(&masm, this);
753
754 // Go to runtime and handle the rest there.
755 __ mv(c_rarg0, _obj);
756 __ la(ra, RuntimeAddress(keepalive_runtime_entry_addr()));
757 __ jalr(ra);
758 }
759 if (L_done != nullptr) {
760 __ j(*L_done);
761 } else {
762 __ bind(L_through);
763 }
764 }
765
766 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
767 Label L_slow;
768
769 // If another barrier is enabled as well, do a runtime check for a specific barrier.
770 if (_needs_keep_alive_barrier) {
771 char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
772 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(state_to_check)));
773 __ lbu(_tmp1, gc_state_fast);
774 maybe_far_jump_if_zero(masm, _tmp1);
775 }
776
777 // If weak references are being processed, weak/phantom loads need to go slow,
778 // regardless of their cset status.
779 if (_needs_load_ref_weak_barrier) {
780 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::WEAK_ROOTS)));
781 __ lbu(_tmp1, gc_state_fast);
782 __ bnez(_tmp1, L_slow);
783 }
784
785 // Cset-check. Fall-through to slow if in collection set.
786 if (_narrow) {
787 __ decode_heap_oop_not_null(_tmp2, _obj);
788 } else {
789 __ mv(_tmp2, _obj);
790 }
791
792 __ mv(_tmp1, ShenandoahHeap::in_cset_fast_test_addr());
793 __ srli(_tmp2, _tmp2, ShenandoahHeapRegion::region_size_bytes_shift_jint());
794 __ add(_tmp1, _tmp1, _tmp2);
795 __ lbu(_tmp1, Address(_tmp1, 0));
796 maybe_far_jump_if_zero(masm, _tmp1);
797
798 // Slow path
799 __ bind(L_slow);
800
801 // Obj is the result, need to temporarily stop preserving it.
802 bool is_obj_preserved = is_preserved(_obj);
803 if (is_obj_preserved) {
804 dont_preserve(_obj);
805 }
806 {
807 SaveLiveRegisters slr(&masm, this);
808
809 // Shuffle in the arguments. The end result should be:
810 // c_rarg0 <- obj
811 // c_rarg1 <- lea(addr)
812 if (c_rarg0 == _obj) {
813 __ la(c_rarg1, _addr);
814 } else if (c_rarg1 == _obj) {
815 __ mv(_tmp1, c_rarg1);
816 __ la(c_rarg1, _addr);
817 __ mv(c_rarg0, _tmp1);
818 } else {
819 assert_different_registers(c_rarg1, _obj);
820 __ la(c_rarg1, _addr);
821 __ mv(c_rarg0, _obj);
822 }
823
824 // Go to runtime and handle the rest there.
825 __ la(ra, RuntimeAddress(lrb_runtime_entry_addr()));
826 __ jalr(ra);
827
828 // Save the result where needed. Narrow entries return narrowOop (32 bits)
829 // we need to zero the upper 32 bits of x10.
830 if (_narrow) {
831 __ zext_w(_obj, x10);
832 } else {
833 __ mv(_obj, x10);
834 }
835 }
836 if (is_obj_preserved) {
837 preserve(_obj);
838 }
839
840 __ j(*continuation());
841 }
842
843 int ShenandoahBarrierStubC2::available_gp_registers() {
844 Unimplemented(); // Not used
845 return 0;
846 }
847
848 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
849 Unimplemented(); // Not used
850 return true;
851 }
852
853 void ShenandoahBarrierStubC2::post_init() {
854 // Do nothing.
855 }
856
857 #endif // COMPILER2