forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc2_MacroAssembler_x86.cpp
More file actions
7285 lines (6577 loc) · 265 KB
/
Copy pathc2_MacroAssembler_x86.cpp
File metadata and controls
7285 lines (6577 loc) · 265 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "asm/assembler.hpp"
#include "asm/assembler.inline.hpp"
#include "gc/shared/barrierSet.hpp"
#include "gc/shared/barrierSetAssembler.hpp"
#include "oops/methodData.hpp"
#include "opto/c2_MacroAssembler.hpp"
#include "opto/intrinsicnode.hpp"
#include "opto/output.hpp"
#include "opto/opcodes.hpp"
#include "opto/subnode.hpp"
#include "runtime/globals.hpp"
#include "runtime/objectMonitor.hpp"
#include "runtime/objectMonitorTable.hpp"
#include "runtime/stubRoutines.hpp"
#include "runtime/synchronizer.hpp"
#include "utilities/checkedCast.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/powerOfTwo.hpp"
#include "utilities/sizes.hpp"
#ifdef PRODUCT
#define BLOCK_COMMENT(str) /* nothing */
#define STOP(error) stop(error)
#else
#define BLOCK_COMMENT(str) block_comment(str)
#define STOP(error) block_comment(error); stop(error)
#endif
// C2 compiled method's prolog code.
void C2_MacroAssembler::verified_entry(int framesize, int stack_bang_size, bool fp_mode_24b, bool is_stub) {
assert(stack_bang_size >= framesize || stack_bang_size <= 0, "stack bang size incorrect");
assert((framesize & (StackAlignmentInBytes-1)) == 0, "frame size not aligned");
// Remove word for return addr
framesize -= wordSize;
stack_bang_size -= wordSize;
// Calls to C2R adapters often do not accept exceptional returns.
// We require that their callers must bang for them. But be careful, because
// some VM calls (such as call site linkage) can use several kilobytes of
// stack. But the stack safety zone should account for that.
// See bugs 4446381, 4468289, 4497237.
if (stack_bang_size > 0) {
generate_stack_overflow_check(stack_bang_size);
// We always push rbp, so that on return to interpreter rbp, will be
// restored correctly and we can correct the stack.
push(rbp);
// Save caller's stack pointer into RBP if the frame pointer is preserved.
if (PreserveFramePointer) {
mov(rbp, rsp);
}
// Remove word for ebp
framesize -= wordSize;
// Create frame
if (framesize) {
subptr(rsp, framesize);
}
} else {
subptr(rsp, framesize);
// Save RBP register now.
framesize -= wordSize;
movptr(Address(rsp, framesize), rbp);
// Save caller's stack pointer into RBP if the frame pointer is preserved.
if (PreserveFramePointer) {
movptr(rbp, rsp);
if (framesize > 0) {
addptr(rbp, framesize);
}
}
}
if (VerifyStackAtCalls) { // Majik cookie to verify stack depth
framesize -= wordSize;
movptr(Address(rsp, framesize), (int32_t)0xbadb100d);
}
#ifdef ASSERT
if (VerifyStackAtCalls) {
Label L;
push(rax);
mov(rax, rsp);
andptr(rax, StackAlignmentInBytes-1);
cmpptr(rax, StackAlignmentInBytes-wordSize);
pop(rax);
jcc(Assembler::equal, L);
STOP("Stack is not properly aligned!");
bind(L);
}
#endif
if (!is_stub) {
BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
// We put the non-hot code of the nmethod entry barrier out-of-line in a stub.
Label dummy_slow_path;
Label dummy_continuation;
Label* slow_path = &dummy_slow_path;
Label* continuation = &dummy_continuation;
if (!Compile::current()->output()->in_scratch_emit_size()) {
// Use real labels from actual stub when not emitting code for the purpose of measuring its size
C2EntryBarrierStub* stub = new (Compile::current()->comp_arena()) C2EntryBarrierStub();
Compile::current()->output()->add_stub(stub);
slow_path = &stub->entry();
continuation = &stub->continuation();
}
bs->nmethod_entry_barrier(this, slow_path, continuation);
}
}
inline Assembler::AvxVectorLen C2_MacroAssembler::vector_length_encoding(int vlen_in_bytes) {
switch (vlen_in_bytes) {
case 4: // fall-through
case 8: // fall-through
case 16: return Assembler::AVX_128bit;
case 32: return Assembler::AVX_256bit;
case 64: return Assembler::AVX_512bit;
default: {
ShouldNotReachHere();
return Assembler::AVX_NoVec;
}
}
}
// fast_lock and fast_unlock used by C2
// Because the transitions from emitted code to the runtime
// monitorenter/exit helper stubs are so slow it's critical that
// we inline both the lock-stack fast path and the inflated fast path.
//
// See also: cmpFastLock and cmpFastUnlock.
//
// What follows is a specialized inline transliteration of the code
// in enter() and exit(). If we're concerned about I$ bloat another
// option would be to emit TrySlowEnter and TrySlowExit methods
// at startup-time. These methods would accept arguments as
// (rax,=Obj, rbx=Self, rcx=box, rdx=Scratch) and return success-failure
// indications in the icc.ZFlag. fast_lock and fast_unlock would simply
// marshal the arguments and emit calls to TrySlowEnter and TrySlowExit.
// In practice, however, the # of lock sites is bounded and is usually small.
// Besides the call overhead, TrySlowEnter and TrySlowExit might suffer
// if the processor uses simple bimodal branch predictors keyed by EIP
// Since the helper routines would be called from multiple synchronization
// sites.
//
// An even better approach would be write "MonitorEnter()" and "MonitorExit()"
// in java - using j.u.c and unsafe - and just bind the lock and unlock sites
// to those specialized methods. That'd give us a mostly platform-independent
// implementation that the JITs could optimize and inline at their pleasure.
// Done correctly, the only time we'd need to cross to native could would be
// to park() or unpark() threads. We'd also need a few more unsafe operators
// to (a) prevent compiler-JIT reordering of non-volatile accesses, and
// (b) explicit barriers or fence operations.
//
// TODO:
//
// * Arrange for C2 to pass "Self" into fast_lock and fast_unlock in one of the registers (scr).
// This avoids manifesting the Self pointer in the fast_lock and fast_unlock terminals.
// Given TLAB allocation, Self is usually manifested in a register, so passing it into
// the lock operators would typically be faster than reifying Self.
//
// * Ideally I'd define the primitives as:
// fast_lock (nax Obj, nax box, EAX tmp, nax scr) where box, tmp and scr are KILLED.
// fast_unlock (nax Obj, EAX box, nax tmp) where box and tmp are KILLED
// Unfortunately ADLC bugs prevent us from expressing the ideal form.
// Instead, we're stuck with a rather awkward and brittle register assignments below.
// Furthermore the register assignments are overconstrained, possibly resulting in
// sub-optimal code near the synchronization site.
//
// * Eliminate the sp-proximity tests and just use "== Self" tests instead.
// Alternately, use a better sp-proximity test.
//
// * Currently ObjectMonitor._Owner can hold either an sp value or a (THREAD *) value.
// Either one is sufficient to uniquely identify a thread.
// TODO: eliminate use of sp in _owner and use get_thread(tr) instead.
//
// * Intrinsify notify() and notifyAll() for the common cases where the
// object is locked by the calling thread but the waitlist is empty.
// avoid the expensive JNI call to JVM_Notify() and JVM_NotifyAll().
//
// * use jccb and jmpb instead of jcc and jmp to improve code density.
// But beware of excessive branch density on AMD Opterons.
//
// * Both fast_lock and fast_unlock set the ICC.ZF to indicate success
// or failure of the fast path. If the fast path fails then we pass
// control to the slow path, typically in C. In fast_lock and
// fast_unlock we often branch to DONE_LABEL, just to find that C2
// will emit a conditional branch immediately after the node.
// So we have branches to branches and lots of ICC.ZF games.
// Instead, it might be better to have C2 pass a "FailureLabel"
// into fast_lock and fast_unlock. In the case of success, control
// will drop through the node. ICC.ZF is undefined at exit.
// In the case of failure, the node will branch directly to the
// FailureLabel
// obj: object to lock
// box: on-stack box address -- KILLED
// rax: tmp -- KILLED
// t : tmp -- KILLED
void C2_MacroAssembler::fast_lock(Register obj, Register box, Register rax_reg,
Register t, Register thread) {
assert(rax_reg == rax, "Used for CAS");
assert_different_registers(obj, box, rax_reg, t, thread);
// Handle inflated monitor.
Label inflated;
// Finish fast lock successfully. ZF value is irrelevant.
Label locked;
// Finish fast lock unsuccessfully. MUST jump with ZF == 0
Label slow_path;
if (UseObjectMonitorTable) {
// Clear cache in case fast locking succeeds or we need to take the slow-path.
movptr(Address(box, BasicLock::object_monitor_cache_offset_in_bytes()), 0);
}
if (DiagnoseSyncOnValueBasedClasses != 0) {
load_klass(rax_reg, obj, t);
testb(Address(rax_reg, Klass::misc_flags_offset()), KlassFlags::_misc_is_value_based_class);
jcc(Assembler::notZero, slow_path);
}
const Register mark = t;
{ // Fast Lock
Label push;
const Register top = UseObjectMonitorTable ? rax_reg : box;
// Load the mark.
movptr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
// Prefetch top.
movl(top, Address(thread, JavaThread::lock_stack_top_offset()));
// Check for monitor (0b10).
testptr(mark, markWord::monitor_value);
jcc(Assembler::notZero, inflated);
// Check if lock-stack is full.
cmpl(top, LockStack::end_offset() - 1);
jcc(Assembler::greater, slow_path);
// Check if recursive.
cmpptr(obj, Address(thread, top, Address::times_1, -oopSize));
jccb(Assembler::equal, push);
// Try to lock. Transition lock bits 0b01 => 0b00
movptr(rax_reg, mark);
orptr(rax_reg, markWord::unlocked_value);
andptr(mark, ~(int32_t)markWord::unlocked_value);
lock(); cmpxchgptr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
jcc(Assembler::notEqual, slow_path);
if (UseObjectMonitorTable) {
// Need to reload top, clobbered by CAS.
movl(top, Address(thread, JavaThread::lock_stack_top_offset()));
}
bind(push);
// After successful lock, push object on lock-stack.
movptr(Address(thread, top), obj);
addl(Address(thread, JavaThread::lock_stack_top_offset()), oopSize);
jmp(locked);
}
{ // Handle inflated monitor.
bind(inflated);
const Register monitor = t;
if (!UseObjectMonitorTable) {
assert(mark == monitor, "should be the same here");
} else {
const Register hash = t;
Label monitor_found;
// Look for the monitor in the om_cache.
ByteSize cache_offset = JavaThread::om_cache_oops_offset();
ByteSize monitor_offset = OMCache::oop_to_monitor_difference();
const int num_unrolled = OMCache::CAPACITY;
for (int i = 0; i < num_unrolled; i++) {
movptr(monitor, Address(thread, cache_offset + monitor_offset));
cmpptr(obj, Address(thread, cache_offset));
jccb(Assembler::equal, monitor_found);
cache_offset = cache_offset + OMCache::oop_to_oop_difference();
}
// Look for the monitor in the table.
// Get the hash code.
movptr(hash, Address(obj, oopDesc::mark_offset_in_bytes()));
shrq(hash, markWord::hash_shift);
andq(hash, markWord::hash_mask);
// Get the table and calculate the bucket's address.
lea(rax_reg, ExternalAddress(ObjectMonitorTable::current_table_address()));
movptr(rax_reg, Address(rax_reg));
andq(hash, Address(rax_reg, ObjectMonitorTable::table_capacity_mask_offset()));
movptr(rax_reg, Address(rax_reg, ObjectMonitorTable::table_buckets_offset()));
// Read the monitor from the bucket.
movptr(monitor, Address(rax_reg, hash, Address::times_ptr));
// Check if the monitor in the bucket is special (empty, tombstone or removed)
cmpptr(monitor, ObjectMonitorTable::SpecialPointerValues::below_is_special);
jcc(Assembler::below, slow_path);
// Check if object matches.
movptr(rax_reg, Address(monitor, ObjectMonitor::object_offset()));
BarrierSetAssembler* bs_asm = BarrierSet::barrier_set()->barrier_set_assembler();
bs_asm->try_resolve_weak_handle_in_c2(this, rax_reg, slow_path);
cmpptr(rax_reg, obj);
jcc(Assembler::notEqual, slow_path);
bind(monitor_found);
}
const ByteSize monitor_tag = in_ByteSize(UseObjectMonitorTable ? 0 : checked_cast<int>(markWord::monitor_value));
const Address recursions_address(monitor, ObjectMonitor::recursions_offset() - monitor_tag);
const Address owner_address(monitor, ObjectMonitor::owner_offset() - monitor_tag);
Label monitor_locked;
// Lock the monitor.
if (UseObjectMonitorTable) {
// Cache the monitor for unlock before trashing box. On failure to acquire
// the lock, the slow path will reset the entry accordingly (see CacheSetter).
movptr(Address(box, BasicLock::object_monitor_cache_offset_in_bytes()), monitor);
}
// Try to CAS owner (no owner => current thread's _monitor_owner_id).
xorptr(rax_reg, rax_reg);
movptr(box, Address(thread, JavaThread::monitor_owner_id_offset()));
lock(); cmpxchgptr(box, owner_address);
jccb(Assembler::equal, monitor_locked);
// Check if recursive.
cmpptr(box, rax_reg);
jccb(Assembler::notEqual, slow_path);
// Recursive.
increment(recursions_address);
bind(monitor_locked);
}
bind(locked);
// Set ZF = 1
xorl(rax_reg, rax_reg);
#ifdef ASSERT
// Check that locked label is reached with ZF set.
Label zf_correct;
Label zf_bad_zero;
jcc(Assembler::zero, zf_correct);
jmp(zf_bad_zero);
#endif
bind(slow_path);
#ifdef ASSERT
// Check that slow_path label is reached with ZF not set.
jcc(Assembler::notZero, zf_correct);
stop("Fast Lock ZF != 0");
bind(zf_bad_zero);
stop("Fast Lock ZF != 1");
bind(zf_correct);
#endif
// C2 uses the value of ZF to determine the continuation.
}
// obj: object to lock
// rax: tmp -- KILLED
// t : tmp - cannot be obj nor rax -- KILLED
//
// Some commentary on balanced locking:
//
// fast_lock and fast_unlock are emitted only for provably balanced lock sites.
// Methods that don't have provably balanced locking are forced to run in the
// interpreter - such methods won't be compiled to use fast_lock and fast_unlock.
// The interpreter provides two properties:
// I1: At return-time the interpreter automatically and quietly unlocks any
// objects acquired in the current activation (frame). Recall that the
// interpreter maintains an on-stack list of locks currently held by
// a frame.
// I2: If a method attempts to unlock an object that is not held by the
// frame the interpreter throws IMSX.
//
// Lets say A(), which has provably balanced locking, acquires O and then calls B().
// B() doesn't have provably balanced locking so it runs in the interpreter.
// Control returns to A() and A() unlocks O. By I1 and I2, above, we know that O
// is still locked by A().
//
// The only other source of unbalanced locking would be JNI. The "Java Native Interface
// Specification" states that an object locked by JNI's MonitorEnter should not be
// unlocked by "normal" java-level locking and vice-versa. The specification doesn't
// specify what will occur if a program engages in such mixed-mode locking, however.
// Arguably given that the spec legislates the JNI case as undefined our implementation
// could reasonably *avoid* checking owner in fast_unlock().
// In the interest of performance we elide m->Owner==Self check in unlock.
// A perfectly viable alternative is to elide the owner check except when
// Xcheck:jni is enabled.
void C2_MacroAssembler::fast_unlock(Register obj, Register reg_rax, Register t, Register thread) {
assert(reg_rax == rax, "Used for CAS");
assert_different_registers(obj, reg_rax, t);
// Handle inflated monitor.
Label inflated, inflated_check_lock_stack;
// Finish fast unlock successfully. MUST jump with ZF == 1
Label unlocked, slow_path;
const Register mark = t;
const Register monitor = t;
const Register top = UseObjectMonitorTable ? t : reg_rax;
const Register box = reg_rax;
Label dummy;
C2FastUnlockStub* stub = nullptr;
if (!Compile::current()->output()->in_scratch_emit_size()) {
stub = new (Compile::current()->comp_arena()) C2FastUnlockStub(obj, mark, reg_rax, thread);
Compile::current()->output()->add_stub(stub);
}
Label& push_and_slow_path = stub == nullptr ? dummy : stub->push_and_slow_path();
{ // Fast Unlock
// Load top.
movl(top, Address(thread, JavaThread::lock_stack_top_offset()));
if (!UseObjectMonitorTable) {
// Prefetch mark.
movptr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
}
// Check if obj is top of lock-stack.
cmpptr(obj, Address(thread, top, Address::times_1, -oopSize));
// Top of lock stack was not obj. Must be monitor.
jcc(Assembler::notEqual, inflated_check_lock_stack);
// Pop lock-stack.
DEBUG_ONLY(movptr(Address(thread, top, Address::times_1, -oopSize), 0);)
subl(Address(thread, JavaThread::lock_stack_top_offset()), oopSize);
// Check if recursive.
cmpptr(obj, Address(thread, top, Address::times_1, -2 * oopSize));
jcc(Assembler::equal, unlocked);
// We elide the monitor check, let the CAS fail instead.
if (UseObjectMonitorTable) {
// Load mark.
movptr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
}
// Try to unlock. Transition lock bits 0b00 => 0b01
movptr(reg_rax, mark);
andptr(reg_rax, ~(int32_t)markWord::lock_mask);
orptr(mark, markWord::unlocked_value);
lock(); cmpxchgptr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
jcc(Assembler::notEqual, push_and_slow_path);
jmp(unlocked);
}
{ // Handle inflated monitor.
bind(inflated_check_lock_stack);
#ifdef ASSERT
Label check_done;
subl(top, oopSize);
cmpl(top, in_bytes(JavaThread::lock_stack_base_offset()));
jcc(Assembler::below, check_done);
cmpptr(obj, Address(thread, top));
jcc(Assembler::notEqual, inflated_check_lock_stack);
stop("Fast Unlock lock on stack");
bind(check_done);
if (UseObjectMonitorTable) {
movptr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
}
testptr(mark, markWord::monitor_value);
jcc(Assembler::notZero, inflated);
stop("Fast Unlock not monitor");
#endif
bind(inflated);
if (!UseObjectMonitorTable) {
assert(mark == monitor, "should be the same here");
} else {
// Uses ObjectMonitorTable. Look for the monitor in our BasicLock on the stack.
movptr(monitor, Address(box, BasicLock::object_monitor_cache_offset_in_bytes()));
// null check with ZF == 0, no valid pointer below alignof(ObjectMonitor*)
cmpptr(monitor, alignof(ObjectMonitor*));
jcc(Assembler::below, slow_path);
}
const ByteSize monitor_tag = in_ByteSize(UseObjectMonitorTable ? 0 : checked_cast<int>(markWord::monitor_value));
const Address recursions_address{monitor, ObjectMonitor::recursions_offset() - monitor_tag};
const Address succ_address{monitor, ObjectMonitor::succ_offset() - monitor_tag};
const Address entry_list_address{monitor, ObjectMonitor::entry_list_offset() - monitor_tag};
const Address owner_address{monitor, ObjectMonitor::owner_offset() - monitor_tag};
Label recursive;
// Check if recursive.
cmpptr(recursions_address, 0);
jcc(Assembler::notZero, recursive);
// Set owner to null.
// Release to satisfy the JMM
movptr(owner_address, NULL_WORD);
// We need a full fence after clearing owner to avoid stranding.
// StoreLoad achieves this.
membar(StoreLoad);
// Check if the entry_list is empty.
cmpptr(entry_list_address, NULL_WORD);
jcc(Assembler::zero, unlocked); // If so we are done.
// Check if there is a successor.
cmpptr(succ_address, NULL_WORD);
jcc(Assembler::notZero, unlocked); // If so we are done.
// Save the monitor pointer in the current thread, so we can try to
// reacquire the lock in SharedRuntime::monitor_exit_helper().
if (!UseObjectMonitorTable) {
andptr(monitor, ~(int32_t)markWord::monitor_value);
}
movptr(Address(thread, JavaThread::unlocked_inflated_monitor_offset()), monitor);
orl(t, 1); // Fast Unlock ZF = 0
jmpb(slow_path);
// Recursive unlock.
bind(recursive);
decrement(recursions_address);
}
bind(unlocked);
xorl(t, t); // Fast Unlock ZF = 1
#ifdef ASSERT
// Check that unlocked label is reached with ZF set.
Label zf_correct;
Label zf_bad_zero;
jcc(Assembler::zero, zf_correct);
jmp(zf_bad_zero);
#endif
bind(slow_path);
if (stub != nullptr) {
bind(stub->slow_path_continuation());
}
#ifdef ASSERT
// Check that stub->continuation() label is reached with ZF not set.
jcc(Assembler::notZero, zf_correct);
stop("Fast Unlock ZF != 0");
bind(zf_bad_zero);
stop("Fast Unlock ZF != 1");
bind(zf_correct);
#endif
// C2 uses the value of ZF to determine the continuation.
}
static void abort_verify_int_in_range(uint idx, jint val, jint lo, jint hi) {
fatal("Invalid CastII, idx: %u, val: %d, lo: %d, hi: %d", idx, val, lo, hi);
}
static void reconstruct_frame_pointer_helper(MacroAssembler* masm, Register dst) {
const int framesize = Compile::current()->output()->frame_size_in_bytes();
masm->movptr(dst, rsp);
if (framesize > 2 * wordSize) {
masm->addptr(dst, framesize - 2 * wordSize);
}
}
void C2_MacroAssembler::reconstruct_frame_pointer(Register rtmp) {
if (PreserveFramePointer) {
// frame pointer is valid
#ifdef ASSERT
// Verify frame pointer value in rbp.
reconstruct_frame_pointer_helper(this, rtmp);
Label L_success;
cmpq(rbp, rtmp);
jccb(Assembler::equal, L_success);
STOP("frame pointer mismatch");
bind(L_success);
#endif // ASSERT
} else {
reconstruct_frame_pointer_helper(this, rbp);
}
}
void C2_MacroAssembler::verify_int_in_range(uint idx, const TypeInt* t, Register val) {
jint lo = t->_lo;
jint hi = t->_hi;
assert(lo < hi, "type should not be empty or constant, idx: %u, lo: %d, hi: %d", idx, lo, hi);
if (t == TypeInt::INT) {
return;
}
BLOCK_COMMENT("CastII {");
Label fail;
Label succeed;
if (lo != min_jint) {
cmpl(val, lo);
jccb(Assembler::less, fail);
}
if (hi != max_jint) {
cmpl(val, hi);
jccb(Assembler::greater, fail);
}
jmpb(succeed);
bind(fail);
movl(c_rarg0, idx);
movl(c_rarg1, val);
movl(c_rarg2, lo);
movl(c_rarg3, hi);
reconstruct_frame_pointer(rscratch1);
call(RuntimeAddress(CAST_FROM_FN_PTR(address, abort_verify_int_in_range)));
hlt();
bind(succeed);
BLOCK_COMMENT("} // CastII");
}
static void abort_verify_long_in_range(uint idx, jlong val, jlong lo, jlong hi) {
fatal("Invalid CastLL, idx: %u, val: " JLONG_FORMAT ", lo: " JLONG_FORMAT ", hi: " JLONG_FORMAT, idx, val, lo, hi);
}
void C2_MacroAssembler::verify_long_in_range(uint idx, const TypeLong* t, Register val, Register tmp) {
jlong lo = t->_lo;
jlong hi = t->_hi;
assert(lo < hi, "type should not be empty or constant, idx: %u, lo: " JLONG_FORMAT ", hi: " JLONG_FORMAT, idx, lo, hi);
if (t == TypeLong::LONG) {
return;
}
BLOCK_COMMENT("CastLL {");
Label fail;
Label succeed;
auto cmp_val = [&](jlong bound) {
if (is_simm32(bound)) {
cmpq(val, checked_cast<int>(bound));
} else {
mov64(tmp, bound);
cmpq(val, tmp);
}
};
if (lo != min_jlong) {
cmp_val(lo);
jccb(Assembler::less, fail);
}
if (hi != max_jlong) {
cmp_val(hi);
jccb(Assembler::greater, fail);
}
jmpb(succeed);
bind(fail);
movl(c_rarg0, idx);
movq(c_rarg1, val);
mov64(c_rarg2, lo);
mov64(c_rarg3, hi);
reconstruct_frame_pointer(rscratch1);
call(RuntimeAddress(CAST_FROM_FN_PTR(address, abort_verify_long_in_range)));
hlt();
bind(succeed);
BLOCK_COMMENT("} // CastLL");
}
//-------------------------------------------------------------------------------------------
// Generic instructions support for use in .ad files C2 code generation
void C2_MacroAssembler::vabsnegd(int opcode, XMMRegister dst, XMMRegister src) {
if (dst != src) {
movdqu(dst, src);
}
if (opcode == Op_AbsVD) {
andpd(dst, ExternalAddress(StubRoutines::x86::vector_double_sign_mask()), noreg);
} else {
assert((opcode == Op_NegVD),"opcode should be Op_NegD");
xorpd(dst, ExternalAddress(StubRoutines::x86::vector_double_sign_flip()), noreg);
}
}
void C2_MacroAssembler::vabsnegd(int opcode, XMMRegister dst, XMMRegister src, int vector_len) {
if (opcode == Op_AbsVD) {
vandpd(dst, src, ExternalAddress(StubRoutines::x86::vector_double_sign_mask()), vector_len, noreg);
} else {
assert((opcode == Op_NegVD),"opcode should be Op_NegD");
vxorpd(dst, src, ExternalAddress(StubRoutines::x86::vector_double_sign_flip()), vector_len, noreg);
}
}
void C2_MacroAssembler::vabsnegf(int opcode, XMMRegister dst, XMMRegister src) {
if (dst != src) {
movdqu(dst, src);
}
if (opcode == Op_AbsVF) {
andps(dst, ExternalAddress(StubRoutines::x86::vector_float_sign_mask()), noreg);
} else {
assert((opcode == Op_NegVF),"opcode should be Op_NegF");
xorps(dst, ExternalAddress(StubRoutines::x86::vector_float_sign_flip()), noreg);
}
}
void C2_MacroAssembler::vabsnegf(int opcode, XMMRegister dst, XMMRegister src, int vector_len) {
if (opcode == Op_AbsVF) {
vandps(dst, src, ExternalAddress(StubRoutines::x86::vector_float_sign_mask()), vector_len, noreg);
} else {
assert((opcode == Op_NegVF),"opcode should be Op_NegF");
vxorps(dst, src, ExternalAddress(StubRoutines::x86::vector_float_sign_flip()), vector_len, noreg);
}
}
void C2_MacroAssembler::pminmax(int opcode, BasicType elem_bt, XMMRegister dst, XMMRegister src, XMMRegister tmp) {
assert(opcode == Op_MinV || opcode == Op_MaxV, "sanity");
assert(tmp == xnoreg || elem_bt == T_LONG, "unused");
if (opcode == Op_MinV) {
if (elem_bt == T_BYTE) {
pminsb(dst, src);
} else if (elem_bt == T_SHORT) {
pminsw(dst, src);
} else if (elem_bt == T_INT) {
pminsd(dst, src);
} else {
assert(elem_bt == T_LONG, "required");
assert(tmp == xmm0, "required");
assert_different_registers(dst, src, tmp);
movdqu(xmm0, dst);
pcmpgtq(xmm0, src);
blendvpd(dst, src); // xmm0 as mask
}
} else { // opcode == Op_MaxV
if (elem_bt == T_BYTE) {
pmaxsb(dst, src);
} else if (elem_bt == T_SHORT) {
pmaxsw(dst, src);
} else if (elem_bt == T_INT) {
pmaxsd(dst, src);
} else {
assert(elem_bt == T_LONG, "required");
assert(tmp == xmm0, "required");
assert_different_registers(dst, src, tmp);
movdqu(xmm0, src);
pcmpgtq(xmm0, dst);
blendvpd(dst, src); // xmm0 as mask
}
}
}
void C2_MacroAssembler::vpuminmax(int opcode, BasicType elem_bt, XMMRegister dst,
XMMRegister src1, Address src2, int vlen_enc) {
assert(opcode == Op_UMinV || opcode == Op_UMaxV, "sanity");
if (opcode == Op_UMinV) {
switch(elem_bt) {
case T_BYTE: vpminub(dst, src1, src2, vlen_enc); break;
case T_SHORT: vpminuw(dst, src1, src2, vlen_enc); break;
case T_INT: vpminud(dst, src1, src2, vlen_enc); break;
case T_LONG: evpminuq(dst, k0, src1, src2, false, vlen_enc); break;
default: fatal("Unsupported type %s", type2name(elem_bt)); break;
}
} else {
assert(opcode == Op_UMaxV, "required");
switch(elem_bt) {
case T_BYTE: vpmaxub(dst, src1, src2, vlen_enc); break;
case T_SHORT: vpmaxuw(dst, src1, src2, vlen_enc); break;
case T_INT: vpmaxud(dst, src1, src2, vlen_enc); break;
case T_LONG: evpmaxuq(dst, k0, src1, src2, false, vlen_enc); break;
default: fatal("Unsupported type %s", type2name(elem_bt)); break;
}
}
}
void C2_MacroAssembler::vpuminmaxq(int opcode, XMMRegister dst, XMMRegister src1, XMMRegister src2, XMMRegister xtmp1, XMMRegister xtmp2, int vlen_enc) {
// For optimality, leverage a full vector width of 512 bits
// for operations over smaller vector sizes on AVX512 targets.
if (VM_Version::supports_evex() && !VM_Version::supports_avx512vl()) {
if (opcode == Op_UMaxV) {
evpmaxuq(dst, k0, src1, src2, false, Assembler::AVX_512bit);
} else {
assert(opcode == Op_UMinV, "required");
evpminuq(dst, k0, src1, src2, false, Assembler::AVX_512bit);
}
} else {
// T1 = -1
vpcmpeqq(xtmp1, xtmp1, xtmp1, vlen_enc);
// T1 = -1 << 63
vpsllq(xtmp1, xtmp1, 63, vlen_enc);
// Convert SRC2 to signed value i.e. T2 = T1 + SRC2
vpaddq(xtmp2, xtmp1, src2, vlen_enc);
// Convert SRC1 to signed value i.e. T1 = T1 + SRC1
vpaddq(xtmp1, xtmp1, src1, vlen_enc);
// Mask = T2 > T1
vpcmpgtq(xtmp1, xtmp2, xtmp1, vlen_enc);
if (opcode == Op_UMaxV) {
// Res = Mask ? Src2 : Src1
vpblendvb(dst, src1, src2, xtmp1, vlen_enc);
} else {
// Res = Mask ? Src1 : Src2
vpblendvb(dst, src2, src1, xtmp1, vlen_enc);
}
}
}
void C2_MacroAssembler::vpuminmax(int opcode, BasicType elem_bt, XMMRegister dst,
XMMRegister src1, XMMRegister src2, int vlen_enc) {
assert(opcode == Op_UMinV || opcode == Op_UMaxV, "sanity");
if (opcode == Op_UMinV) {
switch(elem_bt) {
case T_BYTE: vpminub(dst, src1, src2, vlen_enc); break;
case T_SHORT: vpminuw(dst, src1, src2, vlen_enc); break;
case T_INT: vpminud(dst, src1, src2, vlen_enc); break;
case T_LONG: evpminuq(dst, k0, src1, src2, false, vlen_enc); break;
default: fatal("Unsupported type %s", type2name(elem_bt)); break;
}
} else {
assert(opcode == Op_UMaxV, "required");
switch(elem_bt) {
case T_BYTE: vpmaxub(dst, src1, src2, vlen_enc); break;
case T_SHORT: vpmaxuw(dst, src1, src2, vlen_enc); break;
case T_INT: vpmaxud(dst, src1, src2, vlen_enc); break;
case T_LONG: evpmaxuq(dst, k0, src1, src2, false, vlen_enc); break;
default: fatal("Unsupported type %s", type2name(elem_bt)); break;
}
}
}
void C2_MacroAssembler::vpminmax(int opcode, BasicType elem_bt,
XMMRegister dst, XMMRegister src1, XMMRegister src2,
int vlen_enc) {
assert(opcode == Op_MinV || opcode == Op_MaxV, "sanity");
if (opcode == Op_MinV) {
if (elem_bt == T_BYTE) {
vpminsb(dst, src1, src2, vlen_enc);
} else if (elem_bt == T_SHORT) {
vpminsw(dst, src1, src2, vlen_enc);
} else if (elem_bt == T_INT) {
vpminsd(dst, src1, src2, vlen_enc);
} else {
assert(elem_bt == T_LONG, "required");
if (UseAVX > 2 && (vlen_enc == Assembler::AVX_512bit || VM_Version::supports_avx512vl())) {
vpminsq(dst, src1, src2, vlen_enc);
} else {
assert_different_registers(dst, src1, src2);
vpcmpgtq(dst, src1, src2, vlen_enc);
vblendvpd(dst, src1, src2, dst, vlen_enc);
}
}
} else { // opcode == Op_MaxV
if (elem_bt == T_BYTE) {
vpmaxsb(dst, src1, src2, vlen_enc);
} else if (elem_bt == T_SHORT) {
vpmaxsw(dst, src1, src2, vlen_enc);
} else if (elem_bt == T_INT) {
vpmaxsd(dst, src1, src2, vlen_enc);
} else {
assert(elem_bt == T_LONG, "required");
if (UseAVX > 2 && (vlen_enc == Assembler::AVX_512bit || VM_Version::supports_avx512vl())) {
vpmaxsq(dst, src1, src2, vlen_enc);
} else {
assert_different_registers(dst, src1, src2);
vpcmpgtq(dst, src1, src2, vlen_enc);
vblendvpd(dst, src2, src1, dst, vlen_enc);
}
}
}
}
// Float/Double min max
void C2_MacroAssembler::vminmax_fp(int opcode, BasicType elem_bt,
XMMRegister dst, XMMRegister a, XMMRegister b,
XMMRegister tmp, XMMRegister atmp, XMMRegister btmp,
int vlen_enc) {
assert(UseAVX > 0, "required");
assert(opcode == Op_MinV || opcode == Op_MinReductionV ||
opcode == Op_MaxV || opcode == Op_MaxReductionV, "sanity");
assert(elem_bt == T_FLOAT || elem_bt == T_DOUBLE, "sanity");
assert_different_registers(a, tmp, atmp, btmp);
assert_different_registers(b, tmp, atmp, btmp);
bool is_min = (opcode == Op_MinV || opcode == Op_MinReductionV);
bool is_double_word = is_double_word_type(elem_bt);
/* Note on 'non-obvious' assembly sequence:
*
* While there are vminps/vmaxps instructions, there are two important differences between hardware
* and Java on how they handle floats:
* a. -0.0 and +0.0 are considered equal (vminps/vmaxps will return second parameter when inputs are equal)
* b. NaN is not necesarily propagated (vminps/vmaxps will return second parameter when either input is NaN)
*
* It is still more efficient to use vminps/vmaxps, but with some pre/post-processing:
* a. -0.0/+0.0: Bias negative (positive) numbers to second parameter before vminps (vmaxps)
* (only useful when signs differ, noop otherwise)
* b. NaN: Check if it was the first parameter that had the NaN (with vcmp[UNORD_Q])
* Following pseudo code describes the algorithm for max[FD] (Min algorithm is on similar lines):
* btmp = (b < +0.0) ? a : b
* atmp = (b < +0.0) ? b : a
* Tmp = Max_Float(atmp , btmp)
* Res = (atmp == NaN) ? atmp : Tmp
*/
void (MacroAssembler::*vblend)(XMMRegister, XMMRegister, XMMRegister, XMMRegister, int, bool, XMMRegister);
void (MacroAssembler::*vmaxmin)(XMMRegister, XMMRegister, XMMRegister, int);
void (MacroAssembler::*vcmp)(XMMRegister, XMMRegister, XMMRegister, int, int);
XMMRegister mask;
if (!is_double_word && is_min) {
mask = a;
vblend = &MacroAssembler::vblendvps;
vmaxmin = &MacroAssembler::vminps;
vcmp = &MacroAssembler::vcmpps;
} else if (!is_double_word && !is_min) {
mask = b;
vblend = &MacroAssembler::vblendvps;
vmaxmin = &MacroAssembler::vmaxps;
vcmp = &MacroAssembler::vcmpps;
} else if (is_double_word && is_min) {
mask = a;
vblend = &MacroAssembler::vblendvpd;
vmaxmin = &MacroAssembler::vminpd;
vcmp = &MacroAssembler::vcmppd;
} else {
assert(is_double_word && !is_min, "sanity");
mask = b;
vblend = &MacroAssembler::vblendvpd;
vmaxmin = &MacroAssembler::vmaxpd;
vcmp = &MacroAssembler::vcmppd;
}
// Make sure EnableX86ECoreOpts isn't disabled on register overlaps
XMMRegister maxmin, scratch;
if (dst == btmp) {
maxmin = btmp;
scratch = tmp;
} else {
maxmin = tmp;
scratch = btmp;
}
bool precompute_mask = EnableX86ECoreOpts && UseAVX>1;
if (precompute_mask && !is_double_word) {
vpsrad(tmp, mask, 32, vlen_enc);
mask = tmp;
} else if (precompute_mask && is_double_word) {
vpxor(tmp, tmp, tmp, vlen_enc);
vpcmpgtq(tmp, tmp, mask, vlen_enc);
mask = tmp;
}
(this->*vblend)(atmp, a, b, mask, vlen_enc, !precompute_mask, btmp);
(this->*vblend)(btmp, b, a, mask, vlen_enc, !precompute_mask, tmp);
(this->*vmaxmin)(maxmin, atmp, btmp, vlen_enc);
(this->*vcmp)(scratch, atmp, atmp, Assembler::UNORD_Q, vlen_enc);
(this->*vblend)(dst, maxmin, atmp, scratch, vlen_enc, false, scratch);
}
void C2_MacroAssembler::evminmax_fp(int opcode, BasicType elem_bt,
XMMRegister dst, XMMRegister a, XMMRegister b,
KRegister ktmp, XMMRegister atmp, XMMRegister btmp,
int vlen_enc) {
assert(UseAVX > 2, "required");
assert(opcode == Op_MinV || opcode == Op_MinReductionV ||
opcode == Op_MaxV || opcode == Op_MaxReductionV, "sanity");
assert(elem_bt == T_FLOAT || elem_bt == T_DOUBLE, "sanity");