-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathJ9TransformUtil.cpp
3284 lines (2917 loc) · 126 KB
/
J9TransformUtil.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 IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "optimizer/TransformUtil.hpp"
#include "compile/Compilation.hpp"
#include "compile/SymbolReferenceTable.hpp"
#if defined(J9VM_OPT_JITSERVER)
#include "control/CompilationRuntime.hpp"
#endif /* defined(J9VM_OPT_JITSERVER) */
#include "env/CompilerEnv.hpp"
#include "env/ClassTableCriticalSection.hpp"
#include "env/PersistentCHTable.hpp"
#include "il/AnyConst.hpp"
#include "il/Block.hpp"
#include "il/Block_inlines.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "infra/Assert.hpp"
#include "infra/Cfg.hpp"
#include "infra/String.hpp"
#include "il/StaticSymbol.hpp"
#include "il/StaticSymbol_inlines.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "ilgen/J9ByteCodeIterator.hpp"
#include "env/VMAccessCriticalSection.hpp"
#include "env/VMJ9.h"
#include "env/j9method.h"
#include "ras/DebugCounter.hpp"
#include "j9.h"
#include "optimizer/OMROptimization_inlines.hpp"
#include "optimizer/Structure.hpp"
#include "optimizer/HCRGuardAnalysis.hpp"
/**
* Walks the TR_RegionStructure counting loops to get the nesting depth of the block
*/
int32_t J9::TransformUtil::getLoopNestingDepth(TR::Compilation *comp, TR::Block *block)
{
TR_RegionStructure *region = block->getParentStructureIfExists(comp->getFlowGraph());
int32_t nestingDepth = 0;
while (region && region->isNaturalLoop())
{
nestingDepth++;
region = region->getParent();
}
return nestingDepth;
}
/*
* Generate trees for call to jitRetranslateCallerWithPrep to trigger recompilation from JIT-Compiled code.
*/
TR::TreeTop *
J9::TransformUtil::generateRetranslateCallerWithPrepTrees(TR::Node *node, TR_PersistentMethodInfo::InfoBits reason, TR::Compilation *comp)
{
TR::Node *callNode = TR::Node::createWithSymRef(node, TR::icall, 3, comp->getSymRefTab()->findOrCreateRuntimeHelper(TR_jitRetranslateCallerWithPrep, false, false, true));
callNode->setAndIncChild(0, TR::Node::create(node, TR::iconst, 0, reason));
callNode->setAndIncChild(1, TR::Node::createWithSymRef(node, TR::loadaddr, 0, comp->getSymRefTab()->findOrCreateStartPCSymbolRef()));
callNode->setAndIncChild(2, TR::Node::createWithSymRef(node, TR::loadaddr, 0, comp->getSymRefTab()->findOrCreateCompiledMethodSymbolRef()));
TR::TreeTop *tt = TR::TreeTop::create(comp, TR::Node::create(TR::treetop, 1, callNode));
return tt;
}
TR::Node *
J9::TransformUtil::generateArrayElementShiftAmountTrees(
TR::Compilation *comp,
TR::Node *object)
{
TR::Node* shiftAmount;
TR::SymbolReferenceTable* symRefTab = comp->getSymRefTab();
shiftAmount = TR::Node::createWithSymRef(TR::aloadi, 1, 1,object,symRefTab->findOrCreateVftSymbolRef());
shiftAmount = TR::Node::createWithSymRef(TR::aloadi, 1, 1,shiftAmount,symRefTab->findOrCreateArrayClassRomPtrSymbolRef());
shiftAmount = TR::Node::createWithSymRef(TR::iloadi, 1, 1,shiftAmount,symRefTab->findOrCreateIndexableSizeSymbolRef());
return shiftAmount;
}
//
// A few predicates describing shadow symbols that we can reason about at
// compile time. Note that "final field" here doesn't rule out a pointer to a
// Java object, as long as it always points at the same object.
//
// {{{
//
static bool isFinalFieldOfNativeStruct(TR::SymbolReference *symRef, TR::Compilation *comp)
{
switch (symRef->getReferenceNumber() - comp->getSymRefTab()->getNumHelperSymbols())
{
case TR::SymbolReferenceTable::componentClassSymbol:
case TR::SymbolReferenceTable::arrayClassRomPtrSymbol:
case TR::SymbolReferenceTable::indexableSizeSymbol:
case TR::SymbolReferenceTable::isArraySymbol:
case TR::SymbolReferenceTable::classRomPtrSymbol:
case TR::SymbolReferenceTable::ramStaticsFromClassSymbol:
TR_ASSERT(symRef->getSymbol()->isShadow(), "isFinalFieldOfNativeStruct expected shadow symbol");
return true;
default:
return false;
}
}
static bool isFinalFieldPointingAtNativeStruct(TR::SymbolReference *symRef, TR::Compilation *comp)
{
switch (symRef->getReferenceNumber() - comp->getSymRefTab()->getNumHelperSymbols())
{
case TR::SymbolReferenceTable::componentClassSymbol:
case TR::SymbolReferenceTable::arrayClassRomPtrSymbol:
case TR::SymbolReferenceTable::classRomPtrSymbol:
case TR::SymbolReferenceTable::classFromJavaLangClassSymbol:
case TR::SymbolReferenceTable::classFromJavaLangClassAsPrimitiveSymbol:
case TR::SymbolReferenceTable::ramStaticsFromClassSymbol:
case TR::SymbolReferenceTable::vftSymbol:
TR_ASSERT(symRef->getSymbol()->isShadow(), "isFinalFieldPointingAtNativeStruct expected shadow symbol");
return true;
default:
return false;
}
}
static bool isFinalFieldPointingAtRepresentableNativeStruct(TR::SymbolReference *symRef, TR::Compilation *comp)
{
// A "representable native struct" can be turned into a node that is not a field load,
// such as a const or a loadaddr. Most native structs are not "representable" because
// we don't have infrastructure for them such as AOT relocations.
//
switch (symRef->getReferenceNumber() - comp->getSymRefTab()->getNumHelperSymbols())
{
case TR::SymbolReferenceTable::componentClassSymbol:
case TR::SymbolReferenceTable::classFromJavaLangClassSymbol:
case TR::SymbolReferenceTable::classFromJavaLangClassAsPrimitiveSymbol:
// Note: We could also do vftSymbol, except replacing those with
// loadaddr mucks up indirect loads in ways the optimizer/codegen
// isn't expecting yet
TR_ASSERT(symRef->getSymbol()->isShadow(), "isFinalFieldPointingAtRepresentableNativeStruct expected shadow symbol");
return true;
default:
return false;
}
}
static bool isJavaField(TR::Symbol *symbol, int32_t cpIndex, TR::Compilation *comp)
{
if (symbol->isShadow() &&
(cpIndex >= 0 ||
// recognized fields are java fields
symbol->getRecognizedField() != TR::Symbol::UnknownField))
return true;
return false;
}
static bool isJavaField(TR::SymbolReference *symRef, TR::Compilation *comp)
{
return isJavaField(symRef->getSymbol(), symRef->getCPIndex(), comp);
}
static bool isFieldOfJavaObject(TR::SymbolReference *symRef, TR::Compilation *comp)
{
TR::Symbol *symbol = symRef->getSymbol();
if (isJavaField(symRef, comp))
return true;
else if (symbol->isShadow()) switch (symRef->getReferenceNumber() - comp->getSymRefTab()->getNumHelperSymbols())
{
case TR::SymbolReferenceTable::classFromJavaLangClassSymbol:
case TR::SymbolReferenceTable::classFromJavaLangClassAsPrimitiveSymbol:
case TR::SymbolReferenceTable::vftSymbol:
return true;
default:
return false;
}
return false;
}
static bool isNullValueAtAddress(TR::Compilation *comp, TR::DataType loadType, uintptr_t fieldAddress, TR::Symbol *field)
{
TR_J9VMBase *fej9 = comp->fej9();
switch (loadType)
{
case TR::Int8:
{
int8_t value = *(int8_t*)fieldAddress;
if (value == 0)
return true;
}
break;
case TR::Int16:
{
int16_t value = *(int16_t*)fieldAddress;
if (value == 0)
return true;
}
break;
case TR::Int32:
{
int32_t value = *(int32_t*)fieldAddress;
if (value == 0)
return true;
}
break;
case TR::Int64:
{
int64_t value = *(int64_t*)fieldAddress;
if (value == 0)
return true;
}
break;
case TR::Float:
{
float value = *(float*)fieldAddress;
// This will not fold -0.0 but will fold NaN
if (value == 0.0)
return true;
}
break;
case TR::Double:
{
double value = *(double*)fieldAddress;
// This will not fold -0.0 but will fold NaN
if (value == 0.0)
return true;
}
break;
case TR::Address:
{
TR_ASSERT_FATAL(field->isCollectedReference(), "Expecting a collectable reference\n");
uintptr_t value = fej9->getReferenceFieldAtAddress((uintptr_t)fieldAddress);
if (value == 0)
return true;
}
break;
default:
TR_ASSERT_FATAL(false, "Unknown type of field being dereferenced\n");
break;
}
return false;
}
bool J9::TransformUtil::avoidFoldingInstanceField(
uintptr_t object,
TR::Symbol *field,
uint32_t fieldOffset,
int cpIndex,
TR_ResolvedMethod *owningMethod,
TR::Compilation *comp)
{
TR_J9VMBase *fej9 = comp->fej9();
TR_ASSERT_FATAL(fej9->haveAccess(), "avoidFoldingInstanceField requires VM access\n");
TR_ASSERT_FATAL(
isJavaField(field, cpIndex, comp),
"avoidFoldingInstanceField: symbol %p is not a Java field shadow\n",
field);
TR_ASSERT_FATAL(
fej9->canDereferenceAtCompileTimeWithFieldSymbol(field, cpIndex, owningMethod),
"avoidFoldingInstanceField: symbol %p is never foldable (expected possibly foldable)\n", field);
if (owningMethod->isStable(cpIndex, comp) && !field->isFinal())
{
uintptr_t fieldAddress = object + fieldOffset;
TR::DataType loadType = field->getDataType();
if (isNullValueAtAddress(comp, loadType, fieldAddress, field))
return true;
}
switch (field->getRecognizedField())
{
// In the LambdaForm-based JSR292 implementation, CallSite declares a
// target field to be inherited by all subclasses, including
// MutableCallSite, and that field is declared final even though it's
// actually mutable in instances of MCS.
//
// Because it is a final field declared in a trusted JCL package, loads
// of this field would normally be folded to a known object. However,
// folding the target in the case of a MCS can result in the spurious
// removal of the MCS target guard, so refuse to fold in that case.
//
// VolatileCallSite also has a mutable target, but there should be no
// need to check for it here because its implementation of getTarget()
// reads it using Unsafe.getReferenceVolatile().
//
// This checks only for CallSite.target because field recognition is
// based on the defining class even when the class named in the bytecode
// is a subclass.
//
// In the non-LambdaForm implementation, MCS declares its own target
// field, which is not final (and is in fact volatile), so we would not
// attempt to fold it. Furthermore, MutableCallSite.target will not be
// recognized as CallSite.target. Therefore, loads of CallSite.target
// from MCS instances can be rejected unconditionally here without
// affecting the non-LambdaForm implementation.
//
case TR::Symbol::Java_lang_invoke_CallSite_target:
{
TR_OpaqueClassBlock *objectClass =
fej9->getObjectClass((uintptr_t)object);
const char * const mcsName = "java/lang/invoke/MutableCallSite";
TR_OpaqueClassBlock *mcsClass =
fej9->getSystemClassFromClassName(mcsName, strlen(mcsName));
// If MCS isn't loaded, then object isn't an instance of it.
return mcsClass != NULL
&& fej9->isInstanceOf(objectClass, mcsClass, true) != TR_no;
}
// MethodHandle.form can change even though it's declared final, so don't
// create a known object for it. Refinement doesn't rely on folding loads
// of this field to a known object. (LambdaForm.vmentry doesn't need to
// be listed here because it isn't even final.)
case TR::Symbol::Java_lang_invoke_MethodHandle_form:
return true;
default:
break;
}
return false;
}
bool J9::TransformUtil::avoidFoldingInstanceField(
uintptr_t object,
TR::SymbolReference *field,
TR::Compilation *comp)
{
return TR::TransformUtil::avoidFoldingInstanceField(
object,
field->getSymbol(),
field->getOffset(),
field->getCPIndex(),
field->getOwningMethod(comp),
comp);
}
static bool isFinalFieldPointingAtUnrepresentableNativeStruct(TR::SymbolReference *symRef, TR::Compilation *comp)
{
return isFinalFieldPointingAtNativeStruct(symRef, comp) && !isFinalFieldPointingAtRepresentableNativeStruct(symRef, comp);
}
static bool isArrayWithConstantElements(TR::SymbolReference *symRef, TR::Compilation *comp)
{
TR::Symbol *symbol = symRef->getSymbol();
if (symbol->isShadow() && !symRef->isUnresolved())
{
switch (symbol->getRecognizedField())
{
case TR::Symbol::Java_lang_invoke_BruteArgumentMoverHandle_extra:
case TR::Symbol::Java_lang_invoke_MethodType_ptypes:
case TR::Symbol::Java_lang_invoke_VarHandle_handleTable:
case TR::Symbol::Java_lang_invoke_MethodHandleImpl_LoopClauses_clauses:
case TR::Symbol::Java_lang_String_value:
return true;
default:
break;
}
}
return false;
}
static int32_t isArrayWithStableElements(int32_t cpIndex, TR_ResolvedMethod *owningMethod, TR::Compilation *comp)
{
TR_J9VMBase *fej9 = comp->fej9();
// First determine if we are dealing with an array
int32_t signatureLength = 0;
char *signature = owningMethod->classSignatureOfFieldOrStatic(cpIndex, signatureLength);
if (!signature || signature[0] != '[')
return 0;
// Then, check the stable annotation
if (!owningMethod->isStable(cpIndex, comp))
return 0;
// Finally, determine the rank
int32_t rank = 1;
for (; rank < signatureLength; rank++)
{
if (signature[rank] != '[')
break;
}
if (comp->getOption(TR_TraceOptDetails))
traceMsg(comp, "Stable array with rank %d: %.*s\n", rank, signatureLength, signature);
return rank;
}
static bool verifyFieldAccess(void *curStruct, TR::SymbolReference *field, bool isStableArrayElement, TR::Compilation *comp)
{
// Return true only if loading the given field from the given struct will
// itself produce a verifiable value. (Primitives are trivially verifiable.)
//
if (!curStruct)
return false;
TR_J9VMBase *fej9 = comp->fej9();
if (isJavaField(field, comp))
{
// For Java fields, a "verifiable" access is one where we can check
// whether curStruct is an object of the right type. If we can't even
// check that, then we shouldn't be in this function in the first place.
TR_OpaqueClassBlock *objectClass = fej9->getObjectClass((uintptr_t)curStruct);
TR_OpaqueClassBlock *fieldClass = NULL;
// Fabriated fields don't have valid cp index
if (field->getCPIndex() < 0 &&
field->getSymbol()->getRecognizedField() != TR::Symbol::UnknownField)
{
const char* className;
int32_t length;
className = field->getSymbol()->owningClassNameCharsForRecognizedField(length);
fieldClass = fej9->getClassFromSignature(className, length, field->getOwningMethod(comp));
}
else
fieldClass = field->getOwningMethod(comp)->getDeclaringClassFromFieldOrStatic(comp, field->getCPIndex());
if (fieldClass == NULL)
return false;
TR_YesNoMaybe objectContainsField = fej9->isInstanceOf(objectClass, fieldClass, true);
return objectContainsField == TR_yes;
}
else if (comp->getSymRefTab()->isImmutableArrayShadow(field) || isStableArrayElement)
{
TR_OpaqueClassBlock *arrayClass = fej9->getObjectClass((uintptr_t)curStruct);
if (!fej9->isClassArray(arrayClass) ||
(field->getSymbol()->isCollectedReference() &&
fej9->isPrimitiveArray(arrayClass)) ||
(!field->getSymbol()->isCollectedReference() &&
fej9->isReferenceArray(arrayClass)))
return false;
if (isStableArrayElement && fej9->isPrimitiveArray(arrayClass))
{
if (TR::Compiler->om.getArrayElementWidthInBytes(comp, (uintptr_t)curStruct) != field->getSymbol()->getSize())
return false;
}
return true;
}
else if (isFieldOfJavaObject(field, comp))
{
// For special shadows representing data in Java objects, we need to verify the Java object types.
TR_OpaqueClassBlock *objectClass = fej9->getObjectClass((uintptr_t)curStruct);
switch (field->getReferenceNumber() - comp->getSymRefTab()->getNumHelperSymbols())
{
case TR::SymbolReferenceTable::vftSymbol:
return true; // Every java object has a vft pointer
case TR::SymbolReferenceTable::classFromJavaLangClassSymbol:
case TR::SymbolReferenceTable::classFromJavaLangClassAsPrimitiveSymbol:
return objectClass == fej9->getClassClassPointer(objectClass);
default:
TR_ASSERT(false, "Cannot verify unknown field of java object");
return false;
}
}
else if (isFinalFieldOfNativeStruct(field, comp))
{
// These are implicitly verified by virtue of being verifiable
//
return true;
}
else
{
// Don't know how to verify this
//
return false;
}
return true;
}
/**
* Dereference through indirect load chain and return the address of field for curNode
*
* @param baseStruct The value of baseNode
* @param curNode The field to be dereferenced
* @param comp The compilation object needed in the dereference process
*
* @return The address of the field or NULL if dereference failed due to incorrect trees or types
*
* The concepts of "verified" and "verifiable" are used here, they're explained in
* J9::TransformUtil::transformIndirectLoadChainImpl
*/
static void *dereferenceStructPointerChain(void *baseStruct, TR::Node *baseNode, bool isBaseStableArray, TR::Node *curNode, TR::Compilation *comp)
{
if (baseNode == curNode)
{
TR_ASSERT(false, "dereferenceStructPointerChain has no idea what to dereference");
traceMsg(comp, "Caller has already dereferenced node %p, returning NULL as dereferenceStructPointerChain has no idea what to dereference\n", curNode);
return NULL;
}
else
{
TR_ASSERT(curNode != NULL, "Field node is NULL");
TR_ASSERT(curNode->getOpCode().hasSymbolReference(), "Node must have a symref");
TR::SymbolReference *symRef = curNode->getSymbolReference();
TR::Symbol *symbol = symRef->getSymbol();
TR::Node *addressChildNode = symbol->isArrayShadowSymbol() ? curNode->getFirstChild()->getFirstChild() : curNode->getFirstChild();
// The addressChildNode must has a symRef so that we can verify it
if (!addressChildNode->getOpCode().hasSymbolReference())
return NULL;
if (isBaseStableArray)
TR_ASSERT_FATAL(addressChildNode == baseNode, "We should have only one level of indirection for stable arrays\n");
// Use uintptr_t for pointer arithmetic operations and to save type conversions
uintptr_t curStruct = 0;
if (addressChildNode == baseNode)
{
// baseStruct is the value of baseNode, dereference is not needed
curStruct = (uintptr_t)baseStruct;
// baseStruct/baseNode are deemed verifiable by the caller }
}
else
{
TR::SymbolReference *addressChildSymRef = addressChildNode->getSymbolReference();
// Get the address of struct containing current field and dereference it
void* addressChildAddress = dereferenceStructPointerChain(baseStruct, baseNode, false, addressChildNode, comp);
if (addressChildAddress == NULL)
{
return NULL;
}
// Since we're going to dereference a field from addressChild, addressChild must be a java reference or a native struct
else if (addressChildSymRef->getSymbol()->isCollectedReference())
{
curStruct = comp->fej9()->getReferenceFieldAtAddress((uintptr_t)addressChildAddress);
}
else // Native struct
{
// Because addressChildAddress is going to be dereferenced, the field must be a final field pointing at native struct
TR_ASSERT(isFinalFieldPointingAtNativeStruct(addressChildSymRef, comp), "dereferenceStructPointerChain should be dealing with reference fields");
curStruct = *(uintptr_t*)addressChildAddress;
}
}
// Get the field address of curNode
if (curStruct)
{
if (verifyFieldAccess((void*)curStruct, symRef, isBaseStableArray, comp))
{
uintptr_t fieldAddress = 0;
// The offset of a java field is in its symRef
if (isJavaField(symRef, comp))
{
if (TR::TransformUtil::avoidFoldingInstanceField(curStruct, symRef, comp))
{
if (comp->getOption(TR_TraceOptDetails))
{
traceMsg(
comp,
"avoid folding load of field #%d from object at %p\n",
symRef->getReferenceNumber(),
(void*)curStruct);
}
return NULL;
}
fieldAddress = curStruct + symRef->getOffset();
}
else if (comp->getSymRefTab()->isImmutableArrayShadow(symRef) ||
isBaseStableArray)
{
TR::Node* offsetNode = curNode->getFirstChild()->getSecondChild();
if (!offsetNode->getOpCode().isLoadConst())
return NULL;
int64_t offset = 0;
if (offsetNode->getDataType() == TR::Int64)
offset = offsetNode->getUnsignedLongInt();
else
offset = offsetNode->getUnsignedInt();
uint64_t arrayLengthInBytes = TR::Compiler->om.getArrayLengthInBytes(comp, curStruct);
int64_t minOffset = 0;
int64_t maxOffset = arrayLengthInBytes;
if (!TR::Compiler->om.isOffHeapAllocationEnabled())
{
minOffset += TR::Compiler->om.contiguousArrayHeaderSizeInBytes();
maxOffset += TR::Compiler->om.contiguousArrayHeaderSizeInBytes();
}
// Check array bound
if (offset < minOffset ||
offset >= maxOffset)
{
traceMsg(comp, "Offset %d is out of bound [%d, %d] for %s on array shadow %p!\n", offset, minOffset, maxOffset, symRef->getName(comp->getDebug()), curNode);
return NULL;
}
fieldAddress = TR::Compiler->om.getAddressOfElement(comp, curStruct, offset);
if (!comp->getSymRefTab()->isImmutableArrayShadow(symRef))
{
// if stable array, check that we are loading from the beginning of an element that it's not Null
// size of the element was checked in verifyFieldAccess
TR::DataType type = symRef->getSymbol()->getDataType();
int32_t width = TR::Symbol::convertTypeToSize(type);
if (type == TR::Address)
width = TR::Compiler->om.sizeofReferenceField();
if ((fieldAddress % width) != 0 ||
isNullValueAtAddress(comp, type, fieldAddress, symRef->getSymbol()))
return NULL;
}
}
else
{
// Native struct
fieldAddress = curStruct + symRef->getOffset();
}
return (void*)fieldAddress;
}
else
{
traceMsg(comp, "Unable to verify field access to %s on %p!\n", symRef->getName(comp->getDebug()), curNode);
return NULL;
}
}
else
{
return NULL;
}
}
TR_ASSERT(0, "Should never get here");
return NULL;
}
#if defined(J9VM_OPT_JITSERVER)
/**
* Mimics dereferenceStructPointerChain to dereference an indirect load, but only for the first
* level of the chain; executed from the JITServer only
*/
static void *dereferenceStructPointer(TR::KnownObjectTable::Index baseKnownObject,
TR::Node *node,
TR::Node *baseExpression,
bool isBaseStableArray,
TR::Compilation *comp,
J9::TransformUtil::value *valuePtr)
{
TR_ASSERT(comp->isOutOfProcessCompilation(), "must be executed from the jitserver");
if (node == baseExpression)
{
TR_ASSERT(false, "dereferenceStructPointerChain has no idea what to dereference");
traceMsg(comp, "Caller has already dereferenced node %p, returning NULL as dereferenceStructPointerChain has no idea what to dereference\n", node);
return NULL;
}
TR_ASSERT(node != NULL, "Field node is NULL");
TR_ASSERT(node->getOpCode().hasSymbolReference(), "Node must have a symref");
TR_J9VMBase *fej9 = comp->fej9();
TR::SymbolReference *symRef = node->getSymbolReference();
TR::Symbol *field = symRef->getSymbol();
TR::Node *addressChildNode = field->isArrayShadowSymbol() ?
node->getFirstChild()->getFirstChild() :
node->getFirstChild();
// Abort if the indirection is more than a single level.
if (!addressChildNode->getOpCode().hasSymbolReference()
|| addressChildNode != baseExpression)
return NULL;
// We only consider the case where isJavaField is true for verifyFieldAccess
if (isJavaField(symRef, comp))
{
TR_OpaqueClassBlock *fieldClass = NULL;
if (symRef->getCPIndex() < 0 &&
field->getRecognizedField() != TR::Symbol::UnknownField)
{
const char* className;
int32_t length;
className = field->owningClassNameCharsForRecognizedField(length);
fieldClass = fej9->getClassFromSignature(className, length, symRef->getOwningMethod(comp));
}
else
fieldClass = symRef->getOwningMethod(comp)->getDeclaringClassFromFieldOrStatic(comp,
symRef->getCPIndex());
if (fieldClass == NULL)
return NULL;
TR_OpaqueClassBlock *objectClass =
fej9->getObjectClassFromKnownObjectIndex(comp, baseKnownObject);
// field access verified
if (fej9->isInstanceOf(objectClass, fieldClass, true) == TR_yes)
{
// Mimic avoidFoldingInstanceField by exiting in all cases where the method returns true:
// We should avoid folding instance field if:
// 1. the content of the fieldAddress is null; this is checked when we load the values
// 2. the field is of one of the two types below
if (field->getRecognizedField() == TR::Symbol::Java_lang_invoke_CallSite_target ||
field->getRecognizedField() == TR::Symbol::Java_lang_invoke_MethodHandle_form)
return NULL;
TR::DataType loadType = node->getDataType();
switch (loadType)
{
case TR::Int32:
case TR::Int64:
case TR::Float:
case TR::Double:
{
// not address
auto stream = comp->getStream();
stream->write(JITServer::MessageType::KnownObjectTable_getFieldAddressData,
baseKnownObject, symRef->getOffset());
J9::TransformUtil::value value = std::get<0>(stream->read<J9::TransformUtil::value>());
*valuePtr = value;
// Do the null check part of avoidFoldingConstantField
// We do not have to worry about the case of address; the returned knot index will
// be UNKNOWN in that case
if (isNullValueAtAddress(comp, loadType, (uintptr_t) valuePtr, field))
return NULL;
return valuePtr;
}
break;
case TR::Address:
{
if (isFinalFieldPointingAtRepresentableNativeStruct(symRef, comp) ||
isFinalFieldPointingAtNativeStruct(symRef, comp))
{
return NULL;
}
else if (field->isCollectedReference())
{
bool isArray = isArrayWithConstantElements(symRef, comp);
auto stream = comp->getStream();
stream->write(
JITServer::MessageType::KnownObjectTable_addFieldAddressFromBaseIndex,
baseKnownObject,
symRef->getOffset(),
isArray
);
auto recv = stream->read<TR::KnownObjectTable::Index, uintptr_t *>();
TR::KnownObjectTable::Index value = std::get<0>(recv);
uintptr_t *objectReferenceLocationClient = std::get<1>(recv);
comp->getKnownObjectTable()->updateKnownObjectTableAtServer(
value,
objectReferenceLocationClient,
isArray
);
valuePtr->idx = value;
return valuePtr;
}
}
break;
default:
return NULL;
}
}
}
return NULL;
}
#endif /* defined(J9VM_OPT_JITSERVER) */
bool J9::TransformUtil::foldFinalFieldsIn(TR_OpaqueClassBlock *clazz, const char *className, int32_t classNameLength, bool isStatic, TR::Compilation *comp)
{
TR::SimpleRegex *classRegex = comp->getOptions()->getClassesWithFoldableFinalFields();
if (classRegex)
{
// TR::SimpleRegex::match needs a NUL-terminated string
size_t size = classNameLength + 1;
char *name = (char*)comp->trMemory()->allocateMemory(size, stackAlloc);
strncpy(name, className, classNameLength);
name[size - 1] = '\0';
return TR::SimpleRegex::match(classRegex, name);
}
else if (classNameLength >= 21 && !strncmp(className, "jdk/internal/reflect/", 21))
return true;
else if (classNameLength >= 17 && !strncmp(className, "java/lang/invoke/", 17))
return true; // We can ONLY do this opt to fields that are never victimized by setAccessible
else if (classNameLength >= 18 && !strncmp(className, "java/lang/reflect/", 18))
return true;
else if (classNameLength >= 18 && !strncmp(className, "java/lang/foreign/", 18))
return true;
else if (classNameLength >= 30 && !strncmp(className, "java/lang/String$UnsafeHelpers", 30))
return true;
// Fold static final fields in java/lang/String* for string compression flag
else if (classNameLength >= 16 && !strncmp(className, "java/lang/String", 16))
return true;
else if (classNameLength >= 22 && !strncmp(className, "java/lang/StringBuffer", 22))
return true;
else if (classNameLength >= 23 && !strncmp(className, "java/lang/StringBuilder", 23))
return true;
else if (classNameLength >= 17 && !strncmp(className, "com/ibm/oti/vm/VM", 17))
return true;
else if (classNameLength >= 22 && !strncmp(className, "com/ibm/jit/JITHelpers", 22))
return true;
else if (classNameLength >= 23 && !strncmp(className, "java/lang/J9VMInternals", 23))
return true;
else if (classNameLength >= 34 && !strncmp(className, "java/util/concurrent/atomic/Atomic", 34))
return true;
else if (classNameLength >= 17 && !strncmp(className, "java/util/EnumMap", 17))
return true;
else if (classNameLength >= 38 && !strncmp(className, "java/util/concurrent/ThreadLocalRandom", 38))
return true;
else if (classNameLength >= 18 && !strncmp(className, "java/nio/ByteOrder", 18))
return true;
else if (classNameLength >= 13 && !strncmp(className, "java/nio/Bits", 13))
return true;
else if (classNameLength >= 20 && !strncmp(className, "jdk/incubator/vector", 20))
return true;
else if (classNameLength >= 22 && !strncmp(className, "jdk/internal/vm/vector", 22))
return true;
else if (classNameLength >= 14 && !strncmp(className, "java/lang/Byte", 14))
return true;
else if (classNameLength >= 15 && !strncmp(className, "java/lang/Short", 15))
return true;
else if (classNameLength >= 17 && !strncmp(className, "java/lang/Integer", 17))
return true;
else if (classNameLength >= 14 && !strncmp(className, "java/lang/Long", 14))
return true;
else if (classNameLength >= 15 && !strncmp(className, "java/lang/Float", 15))
return true;
else if (classNameLength >= 16 && !strncmp(className, "java/lang/Double", 16))
return true;
else if (classNameLength >= 17 && !strncmp(className, "java/lang/Boolean", 17))
return true;
if (classNameLength == 16 && !strncmp(className, "java/lang/System", 16))
return false;
static char *enableJCLFolding = feGetEnv("TR_EnableJCLStaticFinalFieldFolding");
if (enableJCLFolding
&& isStatic
&& comp->fej9()->isClassLibraryClass(clazz)
&& comp->fej9()->isClassInitialized(clazz))
{
return true;
}
static char *enableAggressiveFolding = feGetEnv("TR_EnableAggressiveStaticFinalFieldFolding");
if (enableAggressiveFolding
&& isStatic
&& comp->fej9()->isClassInitialized(clazz))
{
return true;
}
return false;
}
static bool changeIndirectLoadIntoConst(TR::Node *node, TR::ILOpCodes opCode, TR::Node **removedChild, TR::Compilation *comp)
{
// Note that this only does part of the job. Caller must actually set the
// constant value / symref / anything else that may be necessary.
//
TR::ILOpCode opCodeObject; opCodeObject.setOpCodeValue(opCode);
if (performTransformation(comp, "O^O transformIndirectLoadChain: change %s [%p] into %s\n", node->getOpCode().getName(), node, opCodeObject.getName()))
{
*removedChild = node->getFirstChild();
node->setNumChildren(0);
TR::Node::recreate(node, opCode);
node->setFlags(0);
return true;
}
return false;
}
/** \brief
* Entry point for folding allowlist'd static final fields.
* These typically belong to fundamental system/bootstrap classes.
*
* \param comp
* The compilation object.
*
* \param node
* The node which is a load of a static final field.
*
* \return
* True if the field is folded.
*/
bool
J9::TransformUtil::foldReliableStaticFinalField(TR::Compilation *comp, TR::Node *node)
{
TR_ASSERT(node->isLoadOfStaticFinalField(),
"Expecting load of static final field on %s %p",
node->getOpCode().getName(), node);
if (!node->getOpCode().isLoadVarDirect())
return false;
if (J9::TransformUtil::canFoldStaticFinalField(comp, node) == TR_yes)
{
return J9::TransformUtil::foldStaticFinalFieldImpl(comp, node);
}
return false;
}
bool
J9::TransformUtil::foldStaticFinalFieldAssumingProtection(TR::Compilation *comp, TR::Node *node)
{
TR_ASSERT(node->isLoadOfStaticFinalField(),
"Expecting load of static final field on %s %p",
node->getOpCode().getName(), node);
if (!node->getOpCode().isLoadVarDirect())
return false;
if (J9::TransformUtil::canFoldStaticFinalField(comp, node) != TR_no)
{
return J9::TransformUtil::foldStaticFinalFieldImpl(comp, node);
}
return false;
}
static bool
classHasFinalPutstaticOutsideClinit(TR::Compilation *comp, TR_OpaqueClassBlock *clazz)
{
TR_J9VMBase *fej9 = comp->fej9();
J9VMThread *vmThread = fej9->vmThread();
J9ROMClass *romClass = TR::Compiler->cls.romClassOf(clazz);
TR::Region &stackRegion = comp->trMemory()->currentStackRegion();
// Get all of the final fields declared by clazz from the VM.
J9Class *j9c = TR::Compiler->cls.convertClassOffsetToClassPtr(clazz);
uintptr_t numStaticFields =
fej9->_vmFunctionTable->getStaticFields(vmThread, romClass, NULL);
uintptr_t staticFieldsSize = numStaticFields * sizeof(J9ROMFieldShape*);
J9ROMFieldShape **staticFields =
(J9ROMFieldShape**)stackRegion.allocate(staticFieldsSize);
fej9->_vmFunctionTable->getStaticFields(vmThread, romClass, staticFields);
// Partition so that the final ones are at the beginning and the non-final
// ones follow.
uintptr_t numStaticFinalFields = 0;
for (uintptr_t i = 0; i < numStaticFields; i++)
{
if ((staticFields[i]->modifiers & J9AccFinal) != 0)
{
// Field i is final. Swap to add it to the end of the final prefix.
// Swapping is OK because we don't depend on the order of the fields.
TR_ASSERT_FATAL(numStaticFinalFields < numStaticFields, "out of bounds");
J9ROMFieldShape *tmp = staticFields[numStaticFinalFields];
staticFields[numStaticFinalFields] = staticFields[i];
staticFields[i] = tmp;
numStaticFinalFields++;
}
}
if (numStaticFinalFields == 0)
return false;
// Iterate the bytecode of all methods looking for a putstatic to a static
// field of clazz with a name and signature matching one in the final prefix
// of staticFields, i.e. staticFields[0..numStaticFinalFields].
J9UTF8 *className = J9ROMCLASS_CLASSNAME(romClass);
TR_ScratchList<TR_ResolvedMethod> resolvedMethods(comp->trMemory());
fej9->getResolvedMethods(comp->trMemory(), clazz, &resolvedMethods);
ListIterator<TR_ResolvedMethod> mIt(&resolvedMethods);
for (auto *method = mIt.getFirst(); method != NULL; method = mIt.getNext())
{
if (method->isNative() || method->isAbstract())
continue; // method has no bytecode