-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
c2_MacroAssembler_riscv.cpp
2247 lines (1962 loc) · 72.7 KB
/
c2_MacroAssembler_riscv.cpp
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, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. 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 "precompiled.hpp"
#include "asm/assembler.hpp"
#include "asm/assembler.inline.hpp"
#include "opto/c2_MacroAssembler.hpp"
#include "opto/compile.hpp"
#include "opto/intrinsicnode.hpp"
#include "opto/output.hpp"
#include "opto/subnode.hpp"
#include "runtime/stubRoutines.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
#define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
void C2_MacroAssembler::fast_lock(Register objectReg, Register boxReg, Register tmp1Reg,
Register tmp2Reg) {
// Use cr register to indicate the fast_lock result: zero for success; non-zero for failure.
Register flag = t1;
Register oop = objectReg;
Register box = boxReg;
Register disp_hdr = tmp1Reg;
Register tmp = tmp2Reg;
Label cont;
Label object_has_monitor;
Label count, no_count;
assert_different_registers(oop, box, tmp, disp_hdr, t0);
// Load markWord from object into displaced_header.
ld(disp_hdr, Address(oop, oopDesc::mark_offset_in_bytes()));
if (DiagnoseSyncOnValueBasedClasses != 0) {
load_klass(flag, oop);
lwu(flag, Address(flag, Klass::access_flags_offset()));
test_bit(flag, flag, exact_log2(JVM_ACC_IS_VALUE_BASED_CLASS), tmp /* tmp */);
bnez(flag, cont, true /* is_far */);
}
// Check for existing monitor
test_bit(t0, disp_hdr, exact_log2(markWord::monitor_value));
bnez(t0, object_has_monitor);
if (LockingMode == LM_MONITOR) {
mv(flag, 1); // Set non-zero flag to indicate 'failure' -> take slow-path
j(cont);
} else if (LockingMode == LM_LEGACY) {
// Set tmp to be (markWord of object | UNLOCK_VALUE).
ori(tmp, disp_hdr, markWord::unlocked_value);
// Initialize the box. (Must happen before we update the object mark!)
sd(tmp, Address(box, BasicLock::displaced_header_offset_in_bytes()));
// Compare object markWord with an unlocked value (tmp) and if
// equal exchange the stack address of our box with object markWord.
// On failure disp_hdr contains the possibly locked markWord.
cmpxchg(/*memory address*/oop, /*expected value*/tmp, /*new value*/box, Assembler::int64, Assembler::aq,
Assembler::rl, /*result*/disp_hdr);
mv(flag, zr);
beq(disp_hdr, tmp, cont); // prepare zero flag and goto cont if we won the cas
assert(oopDesc::mark_offset_in_bytes() == 0, "offset of _mark is not 0");
// If the compare-and-exchange succeeded, then we found an unlocked
// object, will have now locked it will continue at label cont
// We did not see an unlocked object so try the fast recursive case.
// Check if the owner is self by comparing the value in the
// markWord of object (disp_hdr) with the stack pointer.
sub(disp_hdr, disp_hdr, sp);
mv(tmp, (intptr_t) (~(os::vm_page_size()-1) | (uintptr_t)markWord::lock_mask_in_place));
// If (mark & lock_mask) == 0 and mark - sp < page_size, we are stack-locking and goto cont,
// hence we can store 0 as the displaced header in the box, which indicates that it is a
// recursive lock.
andr(tmp/*==0?*/, disp_hdr, tmp);
sd(tmp/*==0, perhaps*/, Address(box, BasicLock::displaced_header_offset_in_bytes()));
mv(flag, tmp); // we can use the value of tmp as the result here
j(cont);
} else {
assert(LockingMode == LM_LIGHTWEIGHT, "");
Label slow;
lightweight_lock(oop, disp_hdr, tmp, t0, slow);
// Indicate success on completion.
mv(flag, zr);
j(count);
bind(slow);
mv(flag, 1); // Set non-zero flag to indicate 'failure' -> take slow-path
j(no_count);
}
// Handle existing monitor.
bind(object_has_monitor);
// The object's monitor m is unlocked iff m->owner == NULL,
// otherwise m->owner may contain a thread or a stack address.
//
// Try to CAS m->owner from NULL to current thread.
add(tmp, disp_hdr, (in_bytes(ObjectMonitor::owner_offset()) - markWord::monitor_value));
cmpxchg(/*memory address*/tmp, /*expected value*/zr, /*new value*/xthread, Assembler::int64, Assembler::aq,
Assembler::rl, /*result*/flag); // cas succeeds if flag == zr(expected)
if (LockingMode != LM_LIGHTWEIGHT) {
// Store a non-null value into the box to avoid looking like a re-entrant
// lock. The fast-path monitor unlock code checks for
// markWord::monitor_value so use markWord::unused_mark which has the
// relevant bit set, and also matches ObjectSynchronizer::slow_enter.
mv(tmp, (address)markWord::unused_mark().value());
sd(tmp, Address(box, BasicLock::displaced_header_offset_in_bytes()));
}
beqz(flag, cont); // CAS success means locking succeeded
bne(flag, xthread, cont); // Check for recursive locking
// Recursive lock case
mv(flag, zr);
increment(Address(disp_hdr, in_bytes(ObjectMonitor::recursions_offset()) - markWord::monitor_value), 1, t0, tmp);
bind(cont);
// zero flag indicates success
// non-zero flag indicates failure
bnez(flag, no_count);
bind(count);
increment(Address(xthread, JavaThread::held_monitor_count_offset()), 1, t0, tmp);
bind(no_count);
}
void C2_MacroAssembler::fast_unlock(Register objectReg, Register boxReg, Register tmp1Reg,
Register tmp2Reg) {
// Use cr register to indicate the fast_unlock result: zero for success; non-zero for failure.
Register flag = t1;
Register oop = objectReg;
Register box = boxReg;
Register disp_hdr = tmp1Reg;
Register tmp = tmp2Reg;
Label cont;
Label object_has_monitor;
Label count, no_count;
assert_different_registers(oop, box, tmp, disp_hdr, flag);
if (LockingMode == LM_LEGACY) {
// Find the lock address and load the displaced header from the stack.
ld(disp_hdr, Address(box, BasicLock::displaced_header_offset_in_bytes()));
// If the displaced header is 0, we have a recursive unlock.
mv(flag, disp_hdr);
beqz(disp_hdr, cont);
}
// Handle existing monitor.
ld(tmp, Address(oop, oopDesc::mark_offset_in_bytes()));
test_bit(t0, tmp, exact_log2(markWord::monitor_value));
bnez(t0, object_has_monitor);
if (LockingMode == LM_MONITOR) {
mv(flag, 1); // Set non-zero flag to indicate 'failure' -> take slow path
j(cont);
} else if (LockingMode == LM_LEGACY) {
// Check if it is still a light weight lock, this is true if we
// see the stack address of the basicLock in the markWord of the
// object.
cmpxchg(/*memory address*/oop, /*expected value*/box, /*new value*/disp_hdr, Assembler::int64, Assembler::relaxed,
Assembler::rl, /*result*/tmp);
xorr(flag, box, tmp); // box == tmp if cas succeeds
j(cont);
} else {
assert(LockingMode == LM_LIGHTWEIGHT, "");
Label slow;
lightweight_unlock(oop, tmp, box, disp_hdr, slow);
// Indicate success on completion.
mv(flag, zr);
j(count);
bind(slow);
mv(flag, 1); // Set non-zero flag to indicate 'failure' -> take slow path
j(no_count);
}
assert(oopDesc::mark_offset_in_bytes() == 0, "offset of _mark is not 0");
// Handle existing monitor.
bind(object_has_monitor);
STATIC_ASSERT(markWord::monitor_value <= INT_MAX);
add(tmp, tmp, -(int)markWord::monitor_value); // monitor
if (LockingMode == LM_LIGHTWEIGHT) {
// If the owner is anonymous, we need to fix it -- in an outline stub.
Register tmp2 = disp_hdr;
ld(tmp2, Address(tmp, ObjectMonitor::owner_offset()));
test_bit(t0, tmp2, exact_log2(ObjectMonitor::ANONYMOUS_OWNER));
C2HandleAnonOMOwnerStub* stub = new (Compile::current()->comp_arena()) C2HandleAnonOMOwnerStub(tmp, tmp2);
Compile::current()->output()->add_stub(stub);
bnez(t0, stub->entry(), /* is_far */ true);
bind(stub->continuation());
}
ld(disp_hdr, Address(tmp, ObjectMonitor::recursions_offset()));
Label notRecursive;
beqz(disp_hdr, notRecursive); // Will be 0 if not recursive.
// Recursive lock
addi(disp_hdr, disp_hdr, -1);
sd(disp_hdr, Address(tmp, ObjectMonitor::recursions_offset()));
mv(flag, zr);
j(cont);
bind(notRecursive);
ld(flag, Address(tmp, ObjectMonitor::EntryList_offset()));
ld(disp_hdr, Address(tmp, ObjectMonitor::cxq_offset()));
orr(flag, flag, disp_hdr); // Will be 0 if both are 0.
bnez(flag, cont);
// need a release store here
la(tmp, Address(tmp, ObjectMonitor::owner_offset()));
membar(MacroAssembler::LoadStore | MacroAssembler::StoreStore);
sd(zr, Address(tmp)); // set unowned
bind(cont);
// zero flag indicates success
// non-zero flag indicates failure
bnez(flag, no_count);
bind(count);
decrement(Address(xthread, JavaThread::held_monitor_count_offset()), 1, t0, tmp);
bind(no_count);
}
// short string
// StringUTF16.indexOfChar
// StringLatin1.indexOfChar
void C2_MacroAssembler::string_indexof_char_short(Register str1, Register cnt1,
Register ch, Register result,
bool isL)
{
Register ch1 = t0;
Register index = t1;
BLOCK_COMMENT("string_indexof_char_short {");
Label LOOP, LOOP1, LOOP4, LOOP8;
Label MATCH, MATCH1, MATCH2, MATCH3,
MATCH4, MATCH5, MATCH6, MATCH7, NOMATCH;
mv(result, -1);
mv(index, zr);
bind(LOOP);
addi(t0, index, 8);
ble(t0, cnt1, LOOP8);
addi(t0, index, 4);
ble(t0, cnt1, LOOP4);
j(LOOP1);
bind(LOOP8);
isL ? lbu(ch1, Address(str1, 0)) : lhu(ch1, Address(str1, 0));
beq(ch, ch1, MATCH);
isL ? lbu(ch1, Address(str1, 1)) : lhu(ch1, Address(str1, 2));
beq(ch, ch1, MATCH1);
isL ? lbu(ch1, Address(str1, 2)) : lhu(ch1, Address(str1, 4));
beq(ch, ch1, MATCH2);
isL ? lbu(ch1, Address(str1, 3)) : lhu(ch1, Address(str1, 6));
beq(ch, ch1, MATCH3);
isL ? lbu(ch1, Address(str1, 4)) : lhu(ch1, Address(str1, 8));
beq(ch, ch1, MATCH4);
isL ? lbu(ch1, Address(str1, 5)) : lhu(ch1, Address(str1, 10));
beq(ch, ch1, MATCH5);
isL ? lbu(ch1, Address(str1, 6)) : lhu(ch1, Address(str1, 12));
beq(ch, ch1, MATCH6);
isL ? lbu(ch1, Address(str1, 7)) : lhu(ch1, Address(str1, 14));
beq(ch, ch1, MATCH7);
addi(index, index, 8);
addi(str1, str1, isL ? 8 : 16);
blt(index, cnt1, LOOP);
j(NOMATCH);
bind(LOOP4);
isL ? lbu(ch1, Address(str1, 0)) : lhu(ch1, Address(str1, 0));
beq(ch, ch1, MATCH);
isL ? lbu(ch1, Address(str1, 1)) : lhu(ch1, Address(str1, 2));
beq(ch, ch1, MATCH1);
isL ? lbu(ch1, Address(str1, 2)) : lhu(ch1, Address(str1, 4));
beq(ch, ch1, MATCH2);
isL ? lbu(ch1, Address(str1, 3)) : lhu(ch1, Address(str1, 6));
beq(ch, ch1, MATCH3);
addi(index, index, 4);
addi(str1, str1, isL ? 4 : 8);
bge(index, cnt1, NOMATCH);
bind(LOOP1);
isL ? lbu(ch1, Address(str1)) : lhu(ch1, Address(str1));
beq(ch, ch1, MATCH);
addi(index, index, 1);
addi(str1, str1, isL ? 1 : 2);
blt(index, cnt1, LOOP1);
j(NOMATCH);
bind(MATCH1);
addi(index, index, 1);
j(MATCH);
bind(MATCH2);
addi(index, index, 2);
j(MATCH);
bind(MATCH3);
addi(index, index, 3);
j(MATCH);
bind(MATCH4);
addi(index, index, 4);
j(MATCH);
bind(MATCH5);
addi(index, index, 5);
j(MATCH);
bind(MATCH6);
addi(index, index, 6);
j(MATCH);
bind(MATCH7);
addi(index, index, 7);
bind(MATCH);
mv(result, index);
bind(NOMATCH);
BLOCK_COMMENT("} string_indexof_char_short");
}
// StringUTF16.indexOfChar
// StringLatin1.indexOfChar
void C2_MacroAssembler::string_indexof_char(Register str1, Register cnt1,
Register ch, Register result,
Register tmp1, Register tmp2,
Register tmp3, Register tmp4,
bool isL)
{
Label CH1_LOOP, HIT, NOMATCH, DONE, DO_LONG;
Register ch1 = t0;
Register orig_cnt = t1;
Register mask1 = tmp3;
Register mask2 = tmp2;
Register match_mask = tmp1;
Register trailing_char = tmp4;
Register unaligned_elems = tmp4;
BLOCK_COMMENT("string_indexof_char {");
beqz(cnt1, NOMATCH);
addi(t0, cnt1, isL ? -32 : -16);
bgtz(t0, DO_LONG);
string_indexof_char_short(str1, cnt1, ch, result, isL);
j(DONE);
bind(DO_LONG);
mv(orig_cnt, cnt1);
if (AvoidUnalignedAccesses) {
Label ALIGNED;
andi(unaligned_elems, str1, 0x7);
beqz(unaligned_elems, ALIGNED);
sub(unaligned_elems, unaligned_elems, 8);
neg(unaligned_elems, unaligned_elems);
if (!isL) {
srli(unaligned_elems, unaligned_elems, 1);
}
// do unaligned part per element
string_indexof_char_short(str1, unaligned_elems, ch, result, isL);
bgez(result, DONE);
mv(orig_cnt, cnt1);
sub(cnt1, cnt1, unaligned_elems);
bind(ALIGNED);
}
// duplicate ch
if (isL) {
slli(ch1, ch, 8);
orr(ch, ch1, ch);
}
slli(ch1, ch, 16);
orr(ch, ch1, ch);
slli(ch1, ch, 32);
orr(ch, ch1, ch);
if (!isL) {
slli(cnt1, cnt1, 1);
}
uint64_t mask0101 = UCONST64(0x0101010101010101);
uint64_t mask0001 = UCONST64(0x0001000100010001);
mv(mask1, isL ? mask0101 : mask0001);
uint64_t mask7f7f = UCONST64(0x7f7f7f7f7f7f7f7f);
uint64_t mask7fff = UCONST64(0x7fff7fff7fff7fff);
mv(mask2, isL ? mask7f7f : mask7fff);
bind(CH1_LOOP);
ld(ch1, Address(str1));
addi(str1, str1, 8);
addi(cnt1, cnt1, -8);
compute_match_mask(ch1, ch, match_mask, mask1, mask2);
bnez(match_mask, HIT);
bgtz(cnt1, CH1_LOOP);
j(NOMATCH);
bind(HIT);
ctzc_bit(trailing_char, match_mask, isL, ch1, result);
srli(trailing_char, trailing_char, 3);
addi(cnt1, cnt1, 8);
ble(cnt1, trailing_char, NOMATCH);
// match case
if (!isL) {
srli(cnt1, cnt1, 1);
srli(trailing_char, trailing_char, 1);
}
sub(result, orig_cnt, cnt1);
add(result, result, trailing_char);
j(DONE);
bind(NOMATCH);
mv(result, -1);
bind(DONE);
BLOCK_COMMENT("} string_indexof_char");
}
typedef void (MacroAssembler::* load_chr_insn)(Register rd, const Address &adr, Register temp);
// Search for needle in haystack and return index or -1
// x10: result
// x11: haystack
// x12: haystack_len
// x13: needle
// x14: needle_len
void C2_MacroAssembler::string_indexof(Register haystack, Register needle,
Register haystack_len, Register needle_len,
Register tmp1, Register tmp2,
Register tmp3, Register tmp4,
Register tmp5, Register tmp6,
Register result, int ae)
{
assert(ae != StrIntrinsicNode::LU, "Invalid encoding");
Label LINEARSEARCH, LINEARSTUB, DONE, NOMATCH;
Register ch1 = t0;
Register ch2 = t1;
Register nlen_tmp = tmp1; // needle len tmp
Register hlen_tmp = tmp2; // haystack len tmp
Register result_tmp = tmp4;
bool isLL = ae == StrIntrinsicNode::LL;
bool needle_isL = ae == StrIntrinsicNode::LL || ae == StrIntrinsicNode::UL;
bool haystack_isL = ae == StrIntrinsicNode::LL || ae == StrIntrinsicNode::LU;
int needle_chr_shift = needle_isL ? 0 : 1;
int haystack_chr_shift = haystack_isL ? 0 : 1;
int needle_chr_size = needle_isL ? 1 : 2;
int haystack_chr_size = haystack_isL ? 1 : 2;
load_chr_insn needle_load_1chr = needle_isL ? (load_chr_insn)&MacroAssembler::lbu :
(load_chr_insn)&MacroAssembler::lhu;
load_chr_insn haystack_load_1chr = haystack_isL ? (load_chr_insn)&MacroAssembler::lbu :
(load_chr_insn)&MacroAssembler::lhu;
BLOCK_COMMENT("string_indexof {");
// Note, inline_string_indexOf() generates checks:
// if (pattern.count > src.count) return -1;
// if (pattern.count == 0) return 0;
// We have two strings, a source string in haystack, haystack_len and a pattern string
// in needle, needle_len. Find the first occurrence of pattern in source or return -1.
// For larger pattern and source we use a simplified Boyer Moore algorithm.
// With a small pattern and source we use linear scan.
// needle_len >=8 && needle_len < 256 && needle_len < haystack_len/4, use bmh algorithm.
sub(result_tmp, haystack_len, needle_len);
// needle_len < 8, use linear scan
sub(t0, needle_len, 8);
bltz(t0, LINEARSEARCH);
// needle_len >= 256, use linear scan
sub(t0, needle_len, 256);
bgez(t0, LINEARSTUB);
// needle_len >= haystack_len/4, use linear scan
srli(t0, haystack_len, 2);
bge(needle_len, t0, LINEARSTUB);
// Boyer-Moore-Horspool introduction:
// The Boyer Moore alogorithm is based on the description here:-
//
// http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
//
// This describes and algorithm with 2 shift rules. The 'Bad Character' rule
// and the 'Good Suffix' rule.
//
// These rules are essentially heuristics for how far we can shift the
// pattern along the search string.
//
// The implementation here uses the 'Bad Character' rule only because of the
// complexity of initialisation for the 'Good Suffix' rule.
//
// This is also known as the Boyer-Moore-Horspool algorithm:
//
// http://en.wikipedia.org/wiki/Boyer-Moore-Horspool_algorithm
//
// #define ASIZE 256
//
// int bm(unsigned char *pattern, int m, unsigned char *src, int n) {
// int i, j;
// unsigned c;
// unsigned char bc[ASIZE];
//
// /* Preprocessing */
// for (i = 0; i < ASIZE; ++i)
// bc[i] = m;
// for (i = 0; i < m - 1; ) {
// c = pattern[i];
// ++i;
// // c < 256 for Latin1 string, so, no need for branch
// #ifdef PATTERN_STRING_IS_LATIN1
// bc[c] = m - i;
// #else
// if (c < ASIZE) bc[c] = m - i;
// #endif
// }
//
// /* Searching */
// j = 0;
// while (j <= n - m) {
// c = src[i+j];
// if (pattern[m-1] == c)
// int k;
// for (k = m - 2; k >= 0 && pattern[k] == src[k + j]; --k);
// if (k < 0) return j;
// // c < 256 for Latin1 string, so, no need for branch
// #ifdef SOURCE_STRING_IS_LATIN1_AND_PATTERN_STRING_IS_LATIN1
// // LL case: (c< 256) always true. Remove branch
// j += bc[pattern[j+m-1]];
// #endif
// #ifdef SOURCE_STRING_IS_UTF_AND_PATTERN_STRING_IS_UTF
// // UU case: need if (c<ASIZE) check. Skip 1 character if not.
// if (c < ASIZE)
// j += bc[pattern[j+m-1]];
// else
// j += 1
// #endif
// #ifdef SOURCE_IS_UTF_AND_PATTERN_IS_LATIN1
// // UL case: need if (c<ASIZE) check. Skip <pattern length> if not.
// if (c < ASIZE)
// j += bc[pattern[j+m-1]];
// else
// j += m
// #endif
// }
// return -1;
// }
// temp register:t0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, result
Label BCLOOP, BCSKIP, BMLOOPSTR2, BMLOOPSTR1, BMSKIP, BMADV, BMMATCH,
BMLOOPSTR1_LASTCMP, BMLOOPSTR1_CMP, BMLOOPSTR1_AFTER_LOAD, BM_INIT_LOOP;
Register haystack_end = haystack_len;
Register skipch = tmp2;
// pattern length is >=8, so, we can read at least 1 register for cases when
// UTF->Latin1 conversion is not needed(8 LL or 4UU) and half register for
// UL case. We'll re-read last character in inner pre-loop code to have
// single outer pre-loop load
const int firstStep = isLL ? 7 : 3;
const int ASIZE = 256;
const int STORE_BYTES = 8; // 8 bytes stored per instruction(sd)
sub(sp, sp, ASIZE);
// init BC offset table with default value: needle_len
slli(t0, needle_len, 8);
orr(t0, t0, needle_len); // [63...16][needle_len][needle_len]
slli(tmp1, t0, 16);
orr(t0, tmp1, t0); // [63...32][needle_len][needle_len][needle_len][needle_len]
slli(tmp1, t0, 32);
orr(tmp5, tmp1, t0); // tmp5: 8 elements [needle_len]
mv(ch1, sp); // ch1 is t0
mv(tmp6, ASIZE / STORE_BYTES); // loop iterations
bind(BM_INIT_LOOP);
// for (i = 0; i < ASIZE; ++i)
// bc[i] = m;
for (int i = 0; i < 4; i++) {
sd(tmp5, Address(ch1, i * wordSize));
}
add(ch1, ch1, 32);
sub(tmp6, tmp6, 4);
bgtz(tmp6, BM_INIT_LOOP);
sub(nlen_tmp, needle_len, 1); // m - 1, index of the last element in pattern
Register orig_haystack = tmp5;
mv(orig_haystack, haystack);
// result_tmp = tmp4
shadd(haystack_end, result_tmp, haystack, haystack_end, haystack_chr_shift);
sub(ch2, needle_len, 1); // bc offset init value, ch2 is t1
mv(tmp3, needle);
// for (i = 0; i < m - 1; ) {
// c = pattern[i];
// ++i;
// // c < 256 for Latin1 string, so, no need for branch
// #ifdef PATTERN_STRING_IS_LATIN1
// bc[c] = m - i;
// #else
// if (c < ASIZE) bc[c] = m - i;
// #endif
// }
bind(BCLOOP);
(this->*needle_load_1chr)(ch1, Address(tmp3), noreg);
add(tmp3, tmp3, needle_chr_size);
if (!needle_isL) {
// ae == StrIntrinsicNode::UU
mv(tmp6, ASIZE);
bgeu(ch1, tmp6, BCSKIP);
}
add(tmp4, sp, ch1);
sb(ch2, Address(tmp4)); // store skip offset to BC offset table
bind(BCSKIP);
sub(ch2, ch2, 1); // for next pattern element, skip distance -1
bgtz(ch2, BCLOOP);
// tmp6: pattern end, address after needle
shadd(tmp6, needle_len, needle, tmp6, needle_chr_shift);
if (needle_isL == haystack_isL) {
// load last 8 bytes (8LL/4UU symbols)
ld(tmp6, Address(tmp6, -wordSize));
} else {
// UL: from UTF-16(source) search Latin1(pattern)
lwu(tmp6, Address(tmp6, -wordSize / 2)); // load last 4 bytes(4 symbols)
// convert Latin1 to UTF. eg: 0x0000abcd -> 0x0a0b0c0d
// We'll have to wait until load completed, but it's still faster than per-character loads+checks
srli(tmp3, tmp6, BitsPerByte * (wordSize / 2 - needle_chr_size)); // pattern[m-1], eg:0x0000000a
slli(ch2, tmp6, XLEN - 24);
srli(ch2, ch2, XLEN - 8); // pattern[m-2], 0x0000000b
slli(ch1, tmp6, XLEN - 16);
srli(ch1, ch1, XLEN - 8); // pattern[m-3], 0x0000000c
andi(tmp6, tmp6, 0xff); // pattern[m-4], 0x0000000d
slli(ch2, ch2, 16);
orr(ch2, ch2, ch1); // 0x00000b0c
slli(result, tmp3, 48); // use result as temp register
orr(tmp6, tmp6, result); // 0x0a00000d
slli(result, ch2, 16);
orr(tmp6, tmp6, result); // UTF-16:0x0a0b0c0d
}
// i = m - 1;
// skipch = j + i;
// if (skipch == pattern[m - 1]
// for (k = m - 2; k >= 0 && pattern[k] == src[k + j]; --k);
// else
// move j with bad char offset table
bind(BMLOOPSTR2);
// compare pattern to source string backward
shadd(result, nlen_tmp, haystack, result, haystack_chr_shift);
(this->*haystack_load_1chr)(skipch, Address(result), noreg);
sub(nlen_tmp, nlen_tmp, firstStep); // nlen_tmp is positive here, because needle_len >= 8
if (needle_isL == haystack_isL) {
// re-init tmp3. It's for free because it's executed in parallel with
// load above. Alternative is to initialize it before loop, but it'll
// affect performance on in-order systems with 2 or more ld/st pipelines
srli(tmp3, tmp6, BitsPerByte * (wordSize - needle_chr_size)); // UU/LL: pattern[m-1]
}
if (!isLL) { // UU/UL case
slli(ch2, nlen_tmp, 1); // offsets in bytes
}
bne(tmp3, skipch, BMSKIP); // if not equal, skipch is bad char
add(result, haystack, isLL ? nlen_tmp : ch2);
// load 8 bytes from source string
// if isLL is false then read granularity can be 2
load_long_misaligned(ch2, Address(result), ch1, isLL ? 1 : 2); // can use ch1 as temp register here as it will be trashed by next mv anyway
mv(ch1, tmp6);
if (isLL) {
j(BMLOOPSTR1_AFTER_LOAD);
} else {
sub(nlen_tmp, nlen_tmp, 1); // no need to branch for UU/UL case. cnt1 >= 8
j(BMLOOPSTR1_CMP);
}
bind(BMLOOPSTR1);
shadd(ch1, nlen_tmp, needle, ch1, needle_chr_shift);
(this->*needle_load_1chr)(ch1, Address(ch1), noreg);
shadd(ch2, nlen_tmp, haystack, ch2, haystack_chr_shift);
(this->*haystack_load_1chr)(ch2, Address(ch2), noreg);
bind(BMLOOPSTR1_AFTER_LOAD);
sub(nlen_tmp, nlen_tmp, 1);
bltz(nlen_tmp, BMLOOPSTR1_LASTCMP);
bind(BMLOOPSTR1_CMP);
beq(ch1, ch2, BMLOOPSTR1);
bind(BMSKIP);
if (!isLL) {
// if we've met UTF symbol while searching Latin1 pattern, then we can
// skip needle_len symbols
if (needle_isL != haystack_isL) {
mv(result_tmp, needle_len);
} else {
mv(result_tmp, 1);
}
mv(t0, ASIZE);
bgeu(skipch, t0, BMADV);
}
add(result_tmp, sp, skipch);
lbu(result_tmp, Address(result_tmp)); // load skip offset
bind(BMADV);
sub(nlen_tmp, needle_len, 1);
// move haystack after bad char skip offset
shadd(haystack, result_tmp, haystack, result, haystack_chr_shift);
ble(haystack, haystack_end, BMLOOPSTR2);
add(sp, sp, ASIZE);
j(NOMATCH);
bind(BMLOOPSTR1_LASTCMP);
bne(ch1, ch2, BMSKIP);
bind(BMMATCH);
sub(result, haystack, orig_haystack);
if (!haystack_isL) {
srli(result, result, 1);
}
add(sp, sp, ASIZE);
j(DONE);
bind(LINEARSTUB);
sub(t0, needle_len, 16); // small patterns still should be handled by simple algorithm
bltz(t0, LINEARSEARCH);
mv(result, zr);
RuntimeAddress stub = nullptr;
if (isLL) {
stub = RuntimeAddress(StubRoutines::riscv::string_indexof_linear_ll());
assert(stub.target() != nullptr, "string_indexof_linear_ll stub has not been generated");
} else if (needle_isL) {
stub = RuntimeAddress(StubRoutines::riscv::string_indexof_linear_ul());
assert(stub.target() != nullptr, "string_indexof_linear_ul stub has not been generated");
} else {
stub = RuntimeAddress(StubRoutines::riscv::string_indexof_linear_uu());
assert(stub.target() != nullptr, "string_indexof_linear_uu stub has not been generated");
}
address call = trampoline_call(stub);
if (call == nullptr) {
DEBUG_ONLY(reset_labels(LINEARSEARCH, DONE, NOMATCH));
ciEnv::current()->record_failure("CodeCache is full");
return;
}
j(DONE);
bind(NOMATCH);
mv(result, -1);
j(DONE);
bind(LINEARSEARCH);
string_indexof_linearscan(haystack, needle, haystack_len, needle_len, tmp1, tmp2, tmp3, tmp4, -1, result, ae);
bind(DONE);
BLOCK_COMMENT("} string_indexof");
}
// string_indexof
// result: x10
// src: x11
// src_count: x12
// pattern: x13
// pattern_count: x14 or 1/2/3/4
void C2_MacroAssembler::string_indexof_linearscan(Register haystack, Register needle,
Register haystack_len, Register needle_len,
Register tmp1, Register tmp2,
Register tmp3, Register tmp4,
int needle_con_cnt, Register result, int ae)
{
// Note:
// needle_con_cnt > 0 means needle_len register is invalid, needle length is constant
// for UU/LL: needle_con_cnt[1, 4], UL: needle_con_cnt = 1
assert(needle_con_cnt <= 4, "Invalid needle constant count");
assert(ae != StrIntrinsicNode::LU, "Invalid encoding");
Register ch1 = t0;
Register ch2 = t1;
Register hlen_neg = haystack_len, nlen_neg = needle_len;
Register nlen_tmp = tmp1, hlen_tmp = tmp2, result_tmp = tmp4;
bool isLL = ae == StrIntrinsicNode::LL;
bool needle_isL = ae == StrIntrinsicNode::LL || ae == StrIntrinsicNode::UL;
bool haystack_isL = ae == StrIntrinsicNode::LL || ae == StrIntrinsicNode::LU;
int needle_chr_shift = needle_isL ? 0 : 1;
int haystack_chr_shift = haystack_isL ? 0 : 1;
int needle_chr_size = needle_isL ? 1 : 2;
int haystack_chr_size = haystack_isL ? 1 : 2;
load_chr_insn needle_load_1chr = needle_isL ? (load_chr_insn)&MacroAssembler::lbu :
(load_chr_insn)&MacroAssembler::lhu;
load_chr_insn haystack_load_1chr = haystack_isL ? (load_chr_insn)&MacroAssembler::lbu :
(load_chr_insn)&MacroAssembler::lhu;
load_chr_insn load_2chr = isLL ? (load_chr_insn)&MacroAssembler::lhu : (load_chr_insn)&MacroAssembler::lwu;
load_chr_insn load_4chr = isLL ? (load_chr_insn)&MacroAssembler::lwu : (load_chr_insn)&MacroAssembler::ld;
Label DO1, DO2, DO3, MATCH, NOMATCH, DONE;
Register first = tmp3;
if (needle_con_cnt == -1) {
Label DOSHORT, FIRST_LOOP, STR2_NEXT, STR1_LOOP, STR1_NEXT;
sub(t0, needle_len, needle_isL == haystack_isL ? 4 : 2);
bltz(t0, DOSHORT);
(this->*needle_load_1chr)(first, Address(needle), noreg);
slli(t0, needle_len, needle_chr_shift);
add(needle, needle, t0);
neg(nlen_neg, t0);
slli(t0, result_tmp, haystack_chr_shift);
add(haystack, haystack, t0);
neg(hlen_neg, t0);
bind(FIRST_LOOP);
add(t0, haystack, hlen_neg);
(this->*haystack_load_1chr)(ch2, Address(t0), noreg);
beq(first, ch2, STR1_LOOP);
bind(STR2_NEXT);
add(hlen_neg, hlen_neg, haystack_chr_size);
blez(hlen_neg, FIRST_LOOP);
j(NOMATCH);
bind(STR1_LOOP);
add(nlen_tmp, nlen_neg, needle_chr_size);
add(hlen_tmp, hlen_neg, haystack_chr_size);
bgez(nlen_tmp, MATCH);
bind(STR1_NEXT);
add(ch1, needle, nlen_tmp);
(this->*needle_load_1chr)(ch1, Address(ch1), noreg);
add(ch2, haystack, hlen_tmp);
(this->*haystack_load_1chr)(ch2, Address(ch2), noreg);
bne(ch1, ch2, STR2_NEXT);
add(nlen_tmp, nlen_tmp, needle_chr_size);
add(hlen_tmp, hlen_tmp, haystack_chr_size);
bltz(nlen_tmp, STR1_NEXT);
j(MATCH);
bind(DOSHORT);
if (needle_isL == haystack_isL) {
sub(t0, needle_len, 2);
bltz(t0, DO1);
bgtz(t0, DO3);
}
}
if (needle_con_cnt == 4) {
Label CH1_LOOP;
(this->*load_4chr)(ch1, Address(needle), noreg);
sub(result_tmp, haystack_len, 4);
slli(tmp3, result_tmp, haystack_chr_shift); // result as tmp
add(haystack, haystack, tmp3);
neg(hlen_neg, tmp3);
if (AvoidUnalignedAccesses) {
// preload first value, then we will read by 1 character per loop, instead of four
// just shifting previous ch2 right by size of character in bits
add(tmp3, haystack, hlen_neg);
(this->*load_4chr)(ch2, Address(tmp3), noreg);
if (isLL) {
// need to erase 1 most significant byte in 32-bit value of ch2
slli(ch2, ch2, 40);
srli(ch2, ch2, 32);
} else {
slli(ch2, ch2, 16); // 2 most significant bytes will be erased by this operation
}
}
bind(CH1_LOOP);
add(tmp3, haystack, hlen_neg);
if (AvoidUnalignedAccesses) {
srli(ch2, ch2, isLL ? 8 : 16);
(this->*haystack_load_1chr)(tmp3, Address(tmp3, isLL ? 3 : 6), noreg);
slli(tmp3, tmp3, isLL ? 24 : 48);
add(ch2, ch2, tmp3);
} else {
(this->*load_4chr)(ch2, Address(tmp3), noreg);
}
beq(ch1, ch2, MATCH);
add(hlen_neg, hlen_neg, haystack_chr_size);
blez(hlen_neg, CH1_LOOP);
j(NOMATCH);
}
if ((needle_con_cnt == -1 && needle_isL == haystack_isL) || needle_con_cnt == 2) {
Label CH1_LOOP;
BLOCK_COMMENT("string_indexof DO2 {");
bind(DO2);
(this->*load_2chr)(ch1, Address(needle), noreg);
if (needle_con_cnt == 2) {
sub(result_tmp, haystack_len, 2);
}
slli(tmp3, result_tmp, haystack_chr_shift);
add(haystack, haystack, tmp3);
neg(hlen_neg, tmp3);
if (AvoidUnalignedAccesses) {
// preload first value, then we will read by 1 character per loop, instead of two
// just shifting previous ch2 right by size of character in bits
add(tmp3, haystack, hlen_neg);
(this->*haystack_load_1chr)(ch2, Address(tmp3), noreg);
slli(ch2, ch2, isLL ? 8 : 16);
}
bind(CH1_LOOP);
add(tmp3, haystack, hlen_neg);
if (AvoidUnalignedAccesses) {
srli(ch2, ch2, isLL ? 8 : 16);
(this->*haystack_load_1chr)(tmp3, Address(tmp3, isLL ? 1 : 2), noreg);
slli(tmp3, tmp3, isLL ? 8 : 16);
add(ch2, ch2, tmp3);
} else {
(this->*load_2chr)(ch2, Address(tmp3), noreg);
}
beq(ch1, ch2, MATCH);
add(hlen_neg, hlen_neg, haystack_chr_size);
blez(hlen_neg, CH1_LOOP);
j(NOMATCH);
BLOCK_COMMENT("} string_indexof DO2");
}
if ((needle_con_cnt == -1 && needle_isL == haystack_isL) || needle_con_cnt == 3) {
Label FIRST_LOOP, STR2_NEXT, STR1_LOOP;
BLOCK_COMMENT("string_indexof DO3 {");
bind(DO3);
(this->*load_2chr)(first, Address(needle), noreg);
(this->*needle_load_1chr)(ch1, Address(needle, 2 * needle_chr_size), noreg);
if (needle_con_cnt == 3) {
sub(result_tmp, haystack_len, 3);
}
slli(hlen_tmp, result_tmp, haystack_chr_shift);
add(haystack, haystack, hlen_tmp);
neg(hlen_neg, hlen_tmp);
bind(FIRST_LOOP);
add(ch2, haystack, hlen_neg);
if (AvoidUnalignedAccesses) {
(this->*haystack_load_1chr)(tmp2, Address(ch2, isLL ? 1 : 2), noreg); // we need a temp register, we can safely use hlen_tmp here, which is a synonym for tmp2
(this->*haystack_load_1chr)(ch2, Address(ch2), noreg);
slli(tmp2, tmp2, isLL ? 8 : 16);
add(ch2, ch2, tmp2);
} else {
(this->*load_2chr)(ch2, Address(ch2), noreg);
}
beq(first, ch2, STR1_LOOP);
bind(STR2_NEXT);
add(hlen_neg, hlen_neg, haystack_chr_size);
blez(hlen_neg, FIRST_LOOP);
j(NOMATCH);
bind(STR1_LOOP);
add(hlen_tmp, hlen_neg, 2 * haystack_chr_size);
add(ch2, haystack, hlen_tmp);
(this->*haystack_load_1chr)(ch2, Address(ch2), noreg);