-
Notifications
You must be signed in to change notification settings - Fork 91
/
parse2.cpp
3502 lines (3174 loc) · 118 KB
/
parse2.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) 1998, 2019, 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 "precompiled.hpp"
#include "ci/ciMethodData.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "compiler/compileLog.hpp"
#include "interpreter/linkResolver.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/oop.inline.hpp"
#include "opto/addnode.hpp"
#include "opto/castnode.hpp"
#include "opto/convertnode.hpp"
#include "opto/divnode.hpp"
#include "opto/idealGraphPrinter.hpp"
#include "opto/idealKit.hpp"
#include "opto/matcher.hpp"
#include "opto/memnode.hpp"
#include "opto/mulnode.hpp"
#include "opto/opaquenode.hpp"
#include "opto/parse.hpp"
#include "opto/runtime.hpp"
#include "opto/valuetypenode.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/sharedRuntime.hpp"
#ifndef PRODUCT
extern int explicit_null_checks_inserted,
explicit_null_checks_elided;
#endif
Node* Parse::record_profile_for_speculation_at_array_load(Node* ld) {
// Feed unused profile data to type speculation
if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
ciKlass* array_type = NULL;
ciKlass* element_type = NULL;
ProfilePtrKind element_ptr = ProfileMaybeNull;
bool flat_array = true;
bool null_free_array = true;
method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
if (element_type != NULL || element_ptr != ProfileMaybeNull) {
ld = record_profile_for_speculation(ld, element_type, element_ptr);
}
}
return ld;
}
//---------------------------------array_load----------------------------------
void Parse::array_load(BasicType bt) {
const Type* elemtype = Type::TOP;
Node* adr = array_addressing(bt, 0, elemtype);
if (stopped()) return; // guaranteed null or range check
Node* idx = pop();
Node* ary = pop();
// Handle value type arrays
const TypeOopPtr* elemptr = elemtype->make_oopptr();
const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr();
if (elemtype->isa_valuetype() != NULL) {
C->set_flattened_accesses();
// Load from flattened value type array
Node* vt = ValueTypeNode::make_from_flattened(this, elemtype->value_klass(), ary, adr);
push(vt);
return;
} else if (elemptr != NULL && elemptr->is_valuetypeptr() && !elemptr->maybe_null()) {
// Load from non-flattened but flattenable value type array (elements can never be null)
bt = T_VALUETYPE;
} else if (!ary_t->is_not_flat()) {
// Cannot statically determine if array is flattened, emit runtime check
assert(ValueArrayFlatten && is_reference_type(bt) && elemptr->can_be_value_type() && !ary_t->klass_is_exact() && !ary_t->is_not_null_free() &&
(!elemptr->is_valuetypeptr() || elemptr->value_klass()->flatten_array()), "array can't be flattened");
Node* ctl = control();
IdealKit ideal(this);
IdealVariable res(ideal);
ideal.declarations_done();
Node* flattened = gen_flattened_array_test(ary);
ideal.if_then(flattened, BoolTest::ne, zerocon(flattened->bottom_type()->basic_type())); {
// flattened
sync_kit(ideal);
if (elemptr->is_valuetypeptr()) {
// Element type is known, cast and load from flattened representation
ciValueKlass* vk = elemptr->value_klass();
assert(vk->flatten_array() && elemptr->maybe_null(), "must be a flattenable and nullable array");
ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* never_null */ true);
const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, arytype));
Node* casted_adr = array_element_address(cast, idx, T_VALUETYPE, ary_t->size(), control());
// Re-execute flattened array load if buffering triggers deoptimization
PreserveReexecuteState preexecs(this);
jvms()->set_should_reexecute(true);
inc_sp(2);
Node* vt = ValueTypeNode::make_from_flattened(this, vk, cast, casted_adr)->allocate(this, false)->get_oop();
ideal.set(res, vt);
ideal.sync_kit(this);
} else {
Node* kls = load_object_klass(ary);
// Element type is unknown, emit runtime call
Node* k_adr = basic_plus_adr(kls, in_bytes(ArrayKlass::element_klass_offset()));
Node* elem_klass = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), k_adr, TypeInstPtr::KLASS));
Node* obj_size = NULL;
kill_dead_locals();
// Re-execute flattened array load if buffering triggers deoptimization
PreserveReexecuteState preexecs(this);
jvms()->set_bci(_bci);
jvms()->set_should_reexecute(true);
inc_sp(2);
Node* alloc_obj = new_instance(elem_klass, NULL, &obj_size, /*deoptimize_on_exception=*/true);
AllocateNode* alloc = AllocateNode::Ideal_allocation(alloc_obj, &_gvn);
assert(alloc->maybe_set_complete(&_gvn), "");
alloc->initialization()->set_complete_with_arraycopy();
// This membar keeps this access to an unknown flattened array
// correctly ordered with other unknown and known flattened
// array accesses.
insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
// Unknown value type might contain reference fields
if (false && !bs->array_copy_requires_gc_barriers(false, T_OBJECT, false, BarrierSetC2::Parsing)) {
// FIXME 8230656 also merge changes from 8238759 in
int base_off = sizeof(instanceOopDesc);
Node* dst_base = basic_plus_adr(alloc_obj, base_off);
Node* countx = obj_size;
countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong)));
assert(Klass::_lh_log2_element_size_shift == 0, "use shift in place");
Node* lhp = basic_plus_adr(kls, in_bytes(Klass::layout_helper_offset()));
Node* elem_shift = make_load(NULL, lhp, TypeInt::INT, T_INT, MemNode::unordered);
uint header = arrayOopDesc::base_offset_in_bytes(T_VALUETYPE);
Node* base = basic_plus_adr(ary, header);
idx = Compile::conv_I2X_index(&_gvn, idx, TypeInt::POS, control());
Node* scale = _gvn.transform(new LShiftXNode(idx, elem_shift));
Node* adr = basic_plus_adr(ary, base, scale);
access_clone(adr, dst_base, countx, false);
} else {
ideal.sync_kit(this);
ideal.make_leaf_call(OptoRuntime::load_unknown_value_Type(),
CAST_FROM_FN_PTR(address, OptoRuntime::load_unknown_value),
"load_unknown_value",
ary, idx, alloc_obj);
sync_kit(ideal);
}
// This makes sure no other thread sees a partially initialized buffered value
insert_mem_bar_volatile(Op_MemBarStoreStore, Compile::AliasIdxRaw, alloc->proj_out_or_null(AllocateNode::RawAddress));
// Same as MemBarCPUOrder above: keep this unknown flattened
// array access correctly ordered with other flattened array
// access
insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
// Prevent any use of the newly allocated value before it is
// fully initialized
alloc_obj = new CastPPNode(alloc_obj, _gvn.type(alloc_obj), true);
alloc_obj->set_req(0, control());
alloc_obj = _gvn.transform(alloc_obj);
const Type* unknown_value = elemptr->is_instptr()->cast_to_flat_array();
alloc_obj = _gvn.transform(new CheckCastPPNode(control(), alloc_obj, unknown_value));
ideal.sync_kit(this);
ideal.set(res, alloc_obj);
}
} ideal.else_(); {
// non-flattened
sync_kit(ideal);
const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
Node* ld = access_load_at(ary, adr, adr_type, elemptr, bt,
IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD, ctl);
ideal.sync_kit(this);
ideal.set(res, ld);
} ideal.end_if();
sync_kit(ideal);
Node* ld = _gvn.transform(ideal.value(res));
ld = record_profile_for_speculation_at_array_load(ld);
push_node(bt, ld);
return;
}
if (elemtype == TypeInt::BOOL) {
bt = T_BOOLEAN;
}
const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
Node* ld = access_load_at(ary, adr, adr_type, elemtype, bt,
IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
if (bt == T_VALUETYPE) {
// Loading a non-flattened (but flattenable) value type from an array
assert(!gvn().type(ld)->maybe_null(), "value type array elements should never be null");
if (elemptr->value_klass()->is_scalarizable()) {
ld = ValueTypeNode::make_from_oop(this, ld, elemptr->value_klass());
}
}
if (!ld->is_ValueType()) {
ld = record_profile_for_speculation_at_array_load(ld);
}
push_node(bt, ld);
}
//--------------------------------array_store----------------------------------
void Parse::array_store(BasicType bt) {
const Type* elemtype = Type::TOP;
Node* adr = array_addressing(bt, type2size[bt], elemtype);
if (stopped()) return; // guaranteed null or range check
Node* cast_val = NULL;
if (bt == T_OBJECT) {
cast_val = array_store_check();
if (stopped()) return;
}
Node* val = pop_node(bt); // Value to store
Node* idx = pop(); // Index in the array
Node* ary = pop(); // The array itself
const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr();
const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
if (elemtype == TypeInt::BOOL) {
bt = T_BOOLEAN;
} else if (bt == T_OBJECT) {
elemtype = elemtype->make_oopptr();
const Type* tval = _gvn.type(cast_val);
// We may have lost type information for 'val' here due to the casts
// emitted by the array_store_check code (see JDK-6312651)
// TODO Remove this code once JDK-6312651 is in.
const Type* tval_init = _gvn.type(val);
bool can_be_value_type = tval->isa_valuetype() || (tval != TypePtr::NULL_PTR && tval_init->is_oopptr()->can_be_value_type() && tval->is_oopptr()->can_be_value_type());
bool not_flattenable = !can_be_value_type || ((tval_init->is_valuetypeptr() || tval_init->isa_valuetype()) && !tval_init->value_klass()->flatten_array());
if (!ary_t->is_not_null_free() && !can_be_value_type && (!tval->maybe_null() || !tval_init->maybe_null())) {
// Storing a non-inline-type, mark array as not null-free.
// This is only legal for non-null stores because the array_store_check passes for null.
ary_t = ary_t->cast_to_not_null_free();
Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, ary_t));
replace_in_map(ary, cast);
ary = cast;
} else if (!ary_t->is_not_flat() && not_flattenable) {
// Storing a non-flattenable value, mark array as not flat.
ary_t = ary_t->cast_to_not_flat();
if (tval != TypePtr::NULL_PTR) {
// For NULL, this transformation is only valid after the null guard below
Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, ary_t));
replace_in_map(ary, cast);
ary = cast;
}
}
if (ary_t->elem()->isa_valuetype() != NULL) {
// Store to flattened value type array
C->set_flattened_accesses();
if (!cast_val->is_ValueType()) {
inc_sp(3);
cast_val = null_check(cast_val);
if (stopped()) return;
dec_sp(3);
cast_val = ValueTypeNode::make_from_oop(this, cast_val, ary_t->elem()->value_klass());
}
// Re-execute flattened array store if buffering triggers deoptimization
PreserveReexecuteState preexecs(this);
inc_sp(3);
jvms()->set_should_reexecute(true);
cast_val->as_ValueType()->store_flattened(this, ary, adr, NULL, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
return;
} else if (elemtype->is_valuetypeptr() && !elemtype->maybe_null()) {
// Store to non-flattened but flattenable value type array (elements can never be null)
if (!cast_val->is_ValueType() && tval->maybe_null()) {
inc_sp(3);
cast_val = null_check(cast_val);
if (stopped()) return;
dec_sp(3);
}
} else if (!ary_t->is_not_flat()) {
// Array might be flattened, emit runtime checks
assert(ValueArrayFlatten && !not_flattenable && elemtype->is_oopptr()->can_be_value_type() &&
!ary_t->klass_is_exact() && !ary_t->is_not_null_free(), "array can't be flattened");
IdealKit ideal(this);
Node* flattened = gen_flattened_array_test(ary);
ideal.if_then(flattened, BoolTest::ne, zerocon(flattened->bottom_type()->basic_type())); {
Node* val = cast_val;
// flattened
if (!val->is_ValueType() && tval->maybe_null()) {
// Add null check
sync_kit(ideal);
Node* null_ctl = top();
val = null_check_oop(val, &null_ctl);
if (null_ctl != top()) {
PreserveJVMState pjvms(this);
inc_sp(3);
set_control(null_ctl);
uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none);
dec_sp(3);
}
ideal.sync_kit(this);
}
// Try to determine the value klass
ciValueKlass* vk = NULL;
if (tval->isa_valuetype() || tval->is_valuetypeptr()) {
vk = tval->value_klass();
} else if (tval_init->isa_valuetype() || tval_init->is_valuetypeptr()) {
vk = tval_init->value_klass();
} else if (elemtype->is_valuetypeptr()) {
vk = elemtype->value_klass();
}
Node* casted_ary = ary;
if (vk != NULL && !stopped()) {
// Element type is known, cast and store to flattened representation
sync_kit(ideal);
assert(vk->flatten_array() && elemtype->maybe_null(), "must be a flattenable and nullable array");
ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* never_null */ true);
const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
casted_ary = _gvn.transform(new CheckCastPPNode(control(), casted_ary, arytype));
Node* casted_adr = array_element_address(casted_ary, idx, T_OBJECT, arytype->size(), control());
if (!val->is_ValueType()) {
assert(!gvn().type(val)->maybe_null(), "value type array elements should never be null");
val = ValueTypeNode::make_from_oop(this, val, vk);
}
// Re-execute flattened array store if buffering triggers deoptimization
PreserveReexecuteState preexecs(this);
inc_sp(3);
jvms()->set_should_reexecute(true);
val->as_ValueType()->store_flattened(this, casted_ary, casted_adr, NULL, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
ideal.sync_kit(this);
} else if (!ideal.ctrl()->is_top()) {
// Element type is unknown, emit runtime call
sync_kit(ideal);
// This membar keeps this access to an unknown flattened
// array correctly ordered with other unknown and known
// flattened array accesses.
insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
ideal.sync_kit(this);
ideal.make_leaf_call(OptoRuntime::store_unknown_value_Type(),
CAST_FROM_FN_PTR(address, OptoRuntime::store_unknown_value),
"store_unknown_value",
val, casted_ary, idx);
sync_kit(ideal);
// Same as MemBarCPUOrder above: keep this unknown
// flattened array access correctly ordered with other
// flattened array accesses.
insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
ideal.sync_kit(this);
}
}
ideal.else_();
{
// non-flattened
sync_kit(ideal);
gen_value_array_null_guard(ary, cast_val, 3);
inc_sp(3);
access_store_at(ary, adr, adr_type, cast_val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false);
dec_sp(3);
ideal.sync_kit(this);
}
ideal.end_if();
sync_kit(ideal);
return;
} else if (!ary_t->is_not_null_free()) {
// Array is not flattened but may be null free
assert(elemtype->is_oopptr()->can_be_value_type() && !ary_t->klass_is_exact(), "array can't be null free");
ary = gen_value_array_null_guard(ary, cast_val, 3, true);
}
}
inc_sp(3);
access_store_at(ary, adr, adr_type, val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
dec_sp(3);
}
//------------------------------array_addressing-------------------------------
// Pull array and index from the stack. Compute pointer-to-element.
Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
Node *idx = peek(0+vals); // Get from stack without popping
Node *ary = peek(1+vals); // in case of exception
// Null check the array base, with correct stack contents
ary = null_check(ary, T_ARRAY);
// Compile-time detect of null-exception?
if (stopped()) return top();
const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
const TypeInt* sizetype = arytype->size();
elemtype = arytype->elem();
if (UseUniqueSubclasses) {
const Type* el = elemtype->make_ptr();
if (el && el->isa_instptr()) {
const TypeInstPtr* toop = el->is_instptr();
if (toop->klass()->as_instance_klass()->unique_concrete_subklass()) {
// If we load from "AbstractClass[]" we must see "ConcreteSubClass".
const Type* subklass = Type::get_const_type(toop->klass());
elemtype = subklass->join_speculative(el);
}
}
}
// Check for big class initializers with all constant offsets
// feeding into a known-size array.
const TypeInt* idxtype = _gvn.type(idx)->is_int();
// See if the highest idx value is less than the lowest array bound,
// and if the idx value cannot be negative:
bool need_range_check = true;
if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) {
need_range_check = false;
if (C->log() != NULL) C->log()->elem("observe that='!need_range_check'");
}
ciKlass * arytype_klass = arytype->klass();
if ((arytype_klass != NULL) && (!arytype_klass->is_loaded())) {
// Only fails for some -Xcomp runs
// The class is unloaded. We have to run this bytecode in the interpreter.
uncommon_trap(Deoptimization::Reason_unloaded,
Deoptimization::Action_reinterpret,
arytype->klass(), "!loaded array");
return top();
}
// Do the range check
if (GenerateRangeChecks && need_range_check) {
Node* tst;
if (sizetype->_hi <= 0) {
// The greatest array bound is negative, so we can conclude that we're
// compiling unreachable code, but the unsigned compare trick used below
// only works with non-negative lengths. Instead, hack "tst" to be zero so
// the uncommon_trap path will always be taken.
tst = _gvn.intcon(0);
} else {
// Range is constant in array-oop, so we can use the original state of mem
Node* len = load_array_length(ary);
// Test length vs index (standard trick using unsigned compare)
Node* chk = _gvn.transform( new CmpUNode(idx, len) );
BoolTest::mask btest = BoolTest::lt;
tst = _gvn.transform( new BoolNode(chk, btest) );
}
RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
_gvn.set_type(rc, rc->Value(&_gvn));
if (!tst->is_Con()) {
record_for_igvn(rc);
}
set_control(_gvn.transform(new IfTrueNode(rc)));
// Branch to failure if out of bounds
{
PreserveJVMState pjvms(this);
set_control(_gvn.transform(new IfFalseNode(rc)));
if (C->allow_range_check_smearing()) {
// Do not use builtin_throw, since range checks are sometimes
// made more stringent by an optimistic transformation.
// This creates "tentative" range checks at this point,
// which are not guaranteed to throw exceptions.
// See IfNode::Ideal, is_range_check, adjust_check.
uncommon_trap(Deoptimization::Reason_range_check,
Deoptimization::Action_make_not_entrant,
NULL, "range_check");
} else {
// If we have already recompiled with the range-check-widening
// heroic optimization turned off, then we must really be throwing
// range check exceptions.
builtin_throw(Deoptimization::Reason_range_check, idx);
}
}
}
// Check for always knowing you are throwing a range-check exception
if (stopped()) return top();
// This could be an access to a value array. We can't tell if it's
// flat or not. Speculating it's not leads to a much simpler graph
// shape. Check profiling.
// For aastore, by the time we're here, the array store check should
// have already taken advantage of profiling to cast the array to an
// exact type reported by profiling
const TypeOopPtr* elemptr = elemtype->make_oopptr();
if (elemtype->isa_valuetype() == NULL &&
(elemptr == NULL || !elemptr->is_valuetypeptr() || elemptr->maybe_null()) &&
!arytype->is_not_flat()) {
assert(is_reference_type(type), "Only references");
// First check the speculative type
Deoptimization::DeoptReason reason = Deoptimization::Reason_speculate_class_check;
ciKlass* array_type = arytype->speculative_type();
if (too_many_traps_or_recompiles(reason) || array_type == NULL) {
// No speculative type, check profile data at this bci
array_type = NULL;
reason = Deoptimization::Reason_class_check;
if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
ciKlass* element_type = NULL;
ProfilePtrKind element_ptr = ProfileMaybeNull;
bool flat_array = true;
bool null_free_array = true;
method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
}
}
if (array_type != NULL) {
// Speculate that this array has the exact type reported by profile data
Node* better_ary = NULL;
Node* slow_ctl = type_check_receiver(ary, array_type, 1.0, &better_ary);
{ PreserveJVMState pjvms(this);
set_control(slow_ctl);
uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
}
replace_in_map(ary, better_ary);
ary = better_ary;
arytype = _gvn.type(ary)->is_aryptr();
elemtype = arytype->elem();
}
} else if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
// No need to speculate: feed profile data at this bci for the
// array to type speculation
ciKlass* array_type = NULL;
ciKlass* element_type = NULL;
ProfilePtrKind element_ptr = ProfileMaybeNull;
bool flat_array = true;
bool null_free_array = true;
method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
if (array_type != NULL) {
record_profile_for_speculation(ary, array_type, ProfileMaybeNull);
}
}
// We have no exact array type from profile data. Check profile data
// for a non null free or non flat array. Non null free implies non
// flat so check this one first. Speculating on a non null free
// array doesn't help aaload but could be profitable for a
// subsequent aastore.
elemptr = elemtype->make_oopptr();
if (!arytype->is_not_null_free() &&
elemtype->isa_valuetype() == NULL &&
(elemptr == NULL || !elemptr->is_valuetypeptr()) &&
UseArrayLoadStoreProfile) {
assert(is_reference_type(type), "");
bool null_free_array = true;
Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
if (arytype->speculative() != NULL &&
arytype->speculative()->is_aryptr()->is_not_null_free() &&
!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
null_free_array = false;
reason = Deoptimization::Reason_speculate_class_check;
} else if (!too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
ciKlass* array_type = NULL;
ciKlass* element_type = NULL;
ProfilePtrKind element_ptr = ProfileMaybeNull;
bool flat_array = true;
method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
reason = Deoptimization::Reason_class_check;
}
if (!null_free_array) {
Node* tst = gen_null_free_array_check(ary);
{
BuildCutout unless(this, tst, PROB_MAX);
uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
}
Node* better_ary = _gvn.transform(new CheckCastPPNode(control(), ary, arytype->cast_to_not_null_free()));
replace_in_map(ary, better_ary);
ary = better_ary;
arytype = _gvn.type(ary)->is_aryptr();
}
}
if (!arytype->is_not_flat() && elemtype->isa_valuetype() == NULL) {
assert(is_reference_type(type), "");
bool flat_array = true;
Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
if (arytype->speculative() != NULL &&
arytype->speculative()->is_aryptr()->is_not_flat() &&
!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
flat_array = false;
reason = Deoptimization::Reason_speculate_class_check;
} else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
ciKlass* array_type = NULL;
ciKlass* element_type = NULL;
ProfilePtrKind element_ptr = ProfileMaybeNull;
bool null_free_array = true;
method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
reason = Deoptimization::Reason_class_check;
}
if (!flat_array) {
Node* flattened = gen_flattened_array_test(ary);
Node* chk = NULL;
if (_gvn.type(flattened)->isa_int()) {
chk = _gvn.transform(new CmpINode(flattened, intcon(0)));
} else {
assert(_gvn.type(flattened)->isa_long(), "flattened property is int or long");
chk = _gvn.transform(new CmpLNode(flattened, longcon(0)));
}
Node* tst = _gvn.transform(new BoolNode(chk, BoolTest::eq));
{
BuildCutout unless(this, tst, PROB_MAX);
uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
}
Node* better_ary = _gvn.transform(new CheckCastPPNode(control(), ary, arytype->cast_to_not_flat()));
replace_in_map(ary, better_ary);
ary = better_ary;
arytype = _gvn.type(ary)->is_aryptr();
}
}
// Make array address computation control dependent to prevent it
// from floating above the range check during loop optimizations.
Node* ptr = array_element_address(ary, idx, type, sizetype, control());
assert(ptr != top(), "top should go hand-in-hand with stopped");
return ptr;
}
// returns IfNode
IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
Node *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
Node *tst = _gvn.transform(new BoolNode(cmp, mask));
IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
return iff;
}
// return Region node
Node* Parse::jump_if_join(Node* iffalse, Node* iftrue) {
Node *region = new RegionNode(3); // 2 results
record_for_igvn(region);
region->init_req(1, iffalse);
region->init_req(2, iftrue );
_gvn.set_type(region, Type::CONTROL);
region = _gvn.transform(region);
set_control (region);
return region;
}
// sentinel value for the target bci to mark never taken branches
// (according to profiling)
static const int never_reached = INT_MAX;
//------------------------------helper for tableswitch-------------------------
void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index, bool unc) {
// True branch, use existing map info
{ PreserveJVMState pjvms(this);
Node *iftrue = _gvn.transform( new IfTrueNode (iff) );
set_control( iftrue );
if (unc) {
repush_if_args();
uncommon_trap(Deoptimization::Reason_unstable_if,
Deoptimization::Action_reinterpret,
NULL,
"taken always");
} else {
assert(dest_bci_if_true != never_reached, "inconsistent dest");
profile_switch_case(prof_table_index);
merge_new_path(dest_bci_if_true);
}
}
// False branch
Node *iffalse = _gvn.transform( new IfFalseNode(iff) );
set_control( iffalse );
}
void Parse::jump_if_false_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index, bool unc) {
// True branch, use existing map info
{ PreserveJVMState pjvms(this);
Node *iffalse = _gvn.transform( new IfFalseNode (iff) );
set_control( iffalse );
if (unc) {
repush_if_args();
uncommon_trap(Deoptimization::Reason_unstable_if,
Deoptimization::Action_reinterpret,
NULL,
"taken never");
} else {
assert(dest_bci_if_true != never_reached, "inconsistent dest");
profile_switch_case(prof_table_index);
merge_new_path(dest_bci_if_true);
}
}
// False branch
Node *iftrue = _gvn.transform( new IfTrueNode(iff) );
set_control( iftrue );
}
void Parse::jump_if_always_fork(int dest_bci, int prof_table_index, bool unc) {
// False branch, use existing map and control()
if (unc) {
repush_if_args();
uncommon_trap(Deoptimization::Reason_unstable_if,
Deoptimization::Action_reinterpret,
NULL,
"taken never");
} else {
assert(dest_bci != never_reached, "inconsistent dest");
profile_switch_case(prof_table_index);
merge_new_path(dest_bci);
}
}
extern "C" {
static int jint_cmp(const void *i, const void *j) {
int a = *(jint *)i;
int b = *(jint *)j;
return a > b ? 1 : a < b ? -1 : 0;
}
}
// Default value for methodData switch indexing. Must be a negative value to avoid
// conflict with any legal switch index.
#define NullTableIndex -1
class SwitchRange : public StackObj {
// a range of integers coupled with a bci destination
jint _lo; // inclusive lower limit
jint _hi; // inclusive upper limit
int _dest;
int _table_index; // index into method data table
float _cnt; // how many times this range was hit according to profiling
public:
jint lo() const { return _lo; }
jint hi() const { return _hi; }
int dest() const { return _dest; }
int table_index() const { return _table_index; }
bool is_singleton() const { return _lo == _hi; }
float cnt() const { return _cnt; }
void setRange(jint lo, jint hi, int dest, int table_index, float cnt) {
assert(lo <= hi, "must be a non-empty range");
_lo = lo, _hi = hi; _dest = dest; _table_index = table_index; _cnt = cnt;
assert(_cnt >= 0, "");
}
bool adjoinRange(jint lo, jint hi, int dest, int table_index, float cnt, bool trim_ranges) {
assert(lo <= hi, "must be a non-empty range");
if (lo == _hi+1 && table_index == _table_index) {
// see merge_ranges() comment below
if (trim_ranges) {
if (cnt == 0) {
if (_cnt != 0) {
return false;
}
if (dest != _dest) {
_dest = never_reached;
}
} else {
if (_cnt == 0) {
return false;
}
if (dest != _dest) {
return false;
}
}
} else {
if (dest != _dest) {
return false;
}
}
_hi = hi;
_cnt += cnt;
return true;
}
return false;
}
void set (jint value, int dest, int table_index, float cnt) {
setRange(value, value, dest, table_index, cnt);
}
bool adjoin(jint value, int dest, int table_index, float cnt, bool trim_ranges) {
return adjoinRange(value, value, dest, table_index, cnt, trim_ranges);
}
bool adjoin(SwitchRange& other) {
return adjoinRange(other._lo, other._hi, other._dest, other._table_index, other._cnt, false);
}
void print() {
if (is_singleton())
tty->print(" {%d}=>%d (cnt=%f)", lo(), dest(), cnt());
else if (lo() == min_jint)
tty->print(" {..%d}=>%d (cnt=%f)", hi(), dest(), cnt());
else if (hi() == max_jint)
tty->print(" {%d..}=>%d (cnt=%f)", lo(), dest(), cnt());
else
tty->print(" {%d..%d}=>%d (cnt=%f)", lo(), hi(), dest(), cnt());
}
};
// We try to minimize the number of ranges and the size of the taken
// ones using profiling data. When ranges are created,
// SwitchRange::adjoinRange() only allows 2 adjoining ranges to merge
// if both were never hit or both were hit to build longer unreached
// ranges. Here, we now merge adjoining ranges with the same
// destination and finally set destination of unreached ranges to the
// special value never_reached because it can help minimize the number
// of tests that are necessary.
//
// For instance:
// [0, 1] to target1 sometimes taken
// [1, 2] to target1 never taken
// [2, 3] to target2 never taken
// would lead to:
// [0, 1] to target1 sometimes taken
// [1, 3] never taken
//
// (first 2 ranges to target1 are not merged)
static void merge_ranges(SwitchRange* ranges, int& rp) {
if (rp == 0) {
return;
}
int shift = 0;
for (int j = 0; j < rp; j++) {
SwitchRange& r1 = ranges[j-shift];
SwitchRange& r2 = ranges[j+1];
if (r1.adjoin(r2)) {
shift++;
} else if (shift > 0) {
ranges[j+1-shift] = r2;
}
}
rp -= shift;
for (int j = 0; j <= rp; j++) {
SwitchRange& r = ranges[j];
if (r.cnt() == 0 && r.dest() != never_reached) {
r.setRange(r.lo(), r.hi(), never_reached, r.table_index(), r.cnt());
}
}
}
//-------------------------------do_tableswitch--------------------------------
void Parse::do_tableswitch() {
Node* lookup = pop();
// Get information about tableswitch
int default_dest = iter().get_dest_table(0);
int lo_index = iter().get_int_table(1);
int hi_index = iter().get_int_table(2);
int len = hi_index - lo_index + 1;
if (len < 1) {
// If this is a backward branch, add safepoint
maybe_add_safepoint(default_dest);
merge(default_dest);
return;
}
ciMethodData* methodData = method()->method_data();
ciMultiBranchData* profile = NULL;
if (methodData->is_mature() && UseSwitchProfiling) {
ciProfileData* data = methodData->bci_to_data(bci());
if (data != NULL && data->is_MultiBranchData()) {
profile = (ciMultiBranchData*)data;
}
}
bool trim_ranges = !method_data_update() && !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
// generate decision tree, using trichotomy when possible
int rnum = len+2;
bool makes_backward_branch = false;
SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
int rp = -1;
if (lo_index != min_jint) {
uint cnt = 1;
if (profile != NULL) {
cnt = profile->default_count() / (hi_index != max_jint ? 2 : 1);
}
ranges[++rp].setRange(min_jint, lo_index-1, default_dest, NullTableIndex, cnt);
}
for (int j = 0; j < len; j++) {
jint match_int = lo_index+j;
int dest = iter().get_dest_table(j+3);
makes_backward_branch |= (dest <= bci());
int table_index = method_data_update() ? j : NullTableIndex;
uint cnt = 1;
if (profile != NULL) {
cnt = profile->count_at(j);
}
if (rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index, cnt, trim_ranges)) {
ranges[++rp].set(match_int, dest, table_index, cnt);
}
}
jint highest = lo_index+(len-1);
assert(ranges[rp].hi() == highest, "");
if (highest != max_jint) {
uint cnt = 1;
if (profile != NULL) {
cnt = profile->default_count() / (lo_index != min_jint ? 2 : 1);
}
if (!ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex, cnt, trim_ranges)) {
ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex, cnt);
}
}
assert(rp < len+2, "not too many ranges");
if (trim_ranges) {
merge_ranges(ranges, rp);
}
// Safepoint in case if backward branch observed
if( makes_backward_branch && UseLoopSafepoints )
add_safepoint();
jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
}
//------------------------------do_lookupswitch--------------------------------
void Parse::do_lookupswitch() {
Node *lookup = pop(); // lookup value
// Get information about lookupswitch
int default_dest = iter().get_dest_table(0);
int len = iter().get_int_table(1);
if (len < 1) { // If this is a backward branch, add safepoint
maybe_add_safepoint(default_dest);
merge(default_dest);
return;
}
ciMethodData* methodData = method()->method_data();
ciMultiBranchData* profile = NULL;
if (methodData->is_mature() && UseSwitchProfiling) {
ciProfileData* data = methodData->bci_to_data(bci());
if (data != NULL && data->is_MultiBranchData()) {
profile = (ciMultiBranchData*)data;
}
}
bool trim_ranges = !method_data_update() && !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
// generate decision tree, using trichotomy when possible
jint* table = NEW_RESOURCE_ARRAY(jint, len*3);
{
for (int j = 0; j < len; j++) {
table[3*j+0] = iter().get_int_table(2+2*j);
table[3*j+1] = iter().get_dest_table(2+2*j+1);
table[3*j+2] = profile == NULL ? 1 : profile->count_at(j);
}
qsort(table, len, 3*sizeof(table[0]), jint_cmp);
}
float defaults = 0;
jint prev = min_jint;
for (int j = 0; j < len; j++) {
jint match_int = table[3*j+0];
if (match_int != prev) {
defaults += (float)match_int - prev;
}
prev = match_int+1;
}
if (prev-1 != max_jint) {
defaults += (float)max_jint - prev + 1;
}
float default_cnt = 1;
if (profile != NULL) {
default_cnt = profile->default_count()/defaults;
}
int rnum = len*2+1;
bool makes_backward_branch = false;
SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
int rp = -1;
for (int j = 0; j < len; j++) {
jint match_int = table[3*j+0];
int dest = table[3*j+1];
int cnt = table[3*j+2];
int next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1;
int table_index = method_data_update() ? j : NullTableIndex;
makes_backward_branch |= (dest <= bci());
float c = default_cnt * ((float)match_int - next_lo);
if (match_int != next_lo && (rp < 0 || !ranges[rp].adjoinRange(next_lo, match_int-1, default_dest, NullTableIndex, c, trim_ranges))) {
assert(default_dest != never_reached, "sentinel value for dead destinations");
ranges[++rp].setRange(next_lo, match_int-1, default_dest, NullTableIndex, c);
}
if (rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index, cnt, trim_ranges)) {
assert(dest != never_reached, "sentinel value for dead destinations");
ranges[++rp].set(match_int, dest, table_index, cnt);
}
}
jint highest = table[3*(len-1)];
assert(ranges[rp].hi() == highest, "");
if (highest != max_jint &&