-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathJ9TransformUtil.cpp
2355 lines (2127 loc) · 97.4 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 (c) 2000, 2020 IBM Corp. and others
*
* 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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#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 "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 "il/StaticSymbol.hpp"
#include "il/StaticSymbol_inlines.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.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::SymbolReference *symRef, TR::Compilation *comp)
{
TR::Symbol *symbol = symRef->getSymbol();
if (symbol->isShadow() &&
(symRef->getCPIndex() >= 0 ||
// recognized fields are java fields
symbol->getRecognizedField() != TR::Symbol::UnknownField))
return true;
return false;
}
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 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_arguments:
case TR::Symbol::Java_lang_invoke_VarHandle_handleTable:
case TR::Symbol::Java_lang_String_value:
return true;
default:
break;
}
}
return false;
}
static bool verifyFieldAccess(void *curStruct, TR::SymbolReference *field, 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))
{
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;
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, 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;
// 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, 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, comp))
{
uintptr_t fieldAddress = 0;
// The offset of a java field is in its symRef
if (isJavaField(symRef, comp))
{
fieldAddress = curStruct + symRef->getOffset();
}
else if (comp->getSymRefTab()->isImmutableArrayShadow(symRef))
{
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 = TR::Compiler->om.contiguousArrayHeaderSizeInBytes();
int64_t maxOffset = arrayLengthInBytes + 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);
}
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;
}
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)
return TR::SimpleRegex::match(classRegex, className);
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 >= 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 >= 18 && !strncmp(className, "java/nio/ByteOrder", 18))
return true;
else if (classNameLength >= 13 && !strncmp(className, "java/nio/Bits", 13))
return true;
if (classNameLength == 16 && !strncmp(className, "java/lang/System", 16))
return false;
static char *enableJCLFolding = feGetEnv("TR_EnableJCLStaticFinalFieldFolding");
if ((enableJCLFolding || comp->getOption(TR_AggressiveOpts))
&& 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;
}
TR::Node *
J9::TransformUtil::transformIndirectLoad(TR::Compilation *comp, TR::Node *node)
{
// TODO: This code does lots of refcount decrementing, which is not
// safe if we don't anchor the child trees first!
//
static char *enableTransformIndirectLoad = feGetEnv("TR_enableTransformIndirectLoad");
if (!enableTransformIndirectLoad)
return NULL;
TR_J9VMBase *fej9 = comp->fej9();
TR_ASSERT(node->getOpCode().isLoadIndirect(), "Expecting indirect load; found %s %p", node->getOpCode().getName(), node);
TR::SymbolReference *symRef = node->getSymbolReference();
TR::Symbol *sym = node->getSymbol();
if (!symRef->isUnresolved()
&& sym->isShadow())
{
TR::Node *baseObject = node->getFirstChild();
// Start with a few field-specific goodies
//
switch (sym->getRecognizedField())
{
case TR::Symbol::Java_lang_invoke_MethodHandle_thunks:
case TR::Symbol::Java_lang_invoke_DynamicInvokerHandle_site:
case TR::Symbol::Java_lang_invoke_MutableCallSiteDynamicInvokerHandle_mutableSite:
if (!node->isNonNull() && performTransformation(comp, "O^O transformIndirectLoad: [%p] recognized field is never null\n", node))
{
node->setIsNull(false);
node->setIsNonNull(true);
}
break;
default:
// Here, we can check a few cases where the "base object" is not
// actually an object at all.
//
if (symRef == comp->getSymRefTab()->findJavaLangClassFromClassSymbolRef()
&& !symRef->hasKnownObjectIndex()
&& baseObject->getOpCodeValue() == TR::loadaddr
&& !baseObject->getSymbolReference()->isUnresolved())
{
TR::SymbolReference *improvedSymRef = node->getSymbolReference();
TR::KnownObjectTable *knot = comp->getOrCreateKnownObjectTable();
if (knot)
{
#if defined(J9VM_OPT_JITSERVER)
if (comp->isOutOfProcessCompilation())
{
auto stream = TR::CompilationInfo::getStream();
stream->write(JITServer::MessageType::KnownObjectTable_createSymRefWithKnownObject,
baseObject->getSymbol()->castToStaticSymbol()->getStaticAddress());
auto recv = stream->read<TR::KnownObjectTable::Index, uintptr_t*>();
TR::KnownObjectTable::Index knotIndex = std::get<0>(recv);
uintptr_t *objectPointerReference = std::get<1>(recv);
if (knotIndex != TR::KnownObjectTable::UNKNOWN)
{
knot->updateKnownObjectTableAtServer(knotIndex, objectPointerReference);
improvedSymRef = comp->getSymRefTab()->findOrCreateSymRefWithKnownObject(node->getSymbolReference(), knotIndex);
}
}
else
#endif /* defined(J9VM_OPT_JITSERVER) */
{
TR::VMAccessCriticalSection createSymRefWithKnownObject(comp->fej9());
uintptr_t jlClass = (uintptr_t)J9VM_J9CLASS_TO_HEAPCLASS((J9Class*)baseObject->getSymbol()->castToStaticSymbol()->getStaticAddress());
TR_ASSERT(jlClass, "java/lang/Class reference from heap class must be non null");
TR::KnownObjectTable::Index knotIndex = knot->getOrCreateIndexAt(&jlClass);
if (knotIndex != TR::KnownObjectTable::UNKNOWN)
{
improvedSymRef = comp->getSymRefTab()->findOrCreateSymRefWithKnownObject(node->getSymbolReference(), knotIndex);
}
}
}
if (improvedSymRef->hasKnownObjectIndex()
&& performTransformation(comp, "O^O transformIndirectLoad: [%p] use object-specific symref #%d (=obj%d) for load of java/lang/Class\n",
node,
improvedSymRef->getReferenceNumber(),
improvedSymRef->getKnownObjectIndex()))
{
node->setSymbolReference(improvedSymRef);
node->setIsNull(false);
node->setIsNonNull(true);
return node;
}
}
break;
}
// Check for loads of final primitive fields on objects that have finished initializing.
//
if (1) //sym->isFinal())
{
J9Class *fieldClass = (J9Class*)symRef->getOwningMethod(comp)->getClassFromFieldOrStatic(comp, symRef->getCPIndex());
if (!fieldClass)
return NULL;
int32_t len;
char * name = fej9->getClassNameChars((TR_OpaqueClassBlock*)fieldClass, len);
if (sym->getRecognizedField() != TR::Symbol::assertionsDisabled
&& !J9::TransformUtil::foldFinalFieldsIn((TR_OpaqueClassBlock *)fieldClass, name, len, sym->isStaticField(), comp))
return NULL;
// Note that the load type can differ from the type of the symbol, eg.
// for sub-integer types. The sub-integer types are included below
// just for completeness, but we likely never hit them.
//
TR::DataType loadType = node->getDataType();
bool typeIsConstible = false;
bool symrefIsImprovable = false;
switch (loadType)
{
case TR::Int32:
case TR::Int64:
typeIsConstible = true;
break;
case TR::Address:
symrefIsImprovable = !symRef->hasKnownObjectIndex();
break;
default:
break;
}
TR::Node *firstDereference = NULL;
TR::Node *secondDereference = NULL;
if (isFinalFieldOfNativeStruct(symRef, comp))
{
// Ok, this is a bit complicated.
//
// We have an expression y.z where y is a native struct and z is
// some final field within that native struct. Because y is a
// child node of y.z we can assume it has already been simplified
// because the optimizer does such things in a bottom-up fashion.
// If y happens to be a so-called "representable" native struct
// like a J9Class, then we're in luck because we can just read the
// field out of y at compile time.
//
// However, we also want to handle situations where y is not
// "representable", meaning it can only appear in the trees as a
// field load from some other base object x. In that case, we
// must handle the whole expression x.y.z in one chunk, because
// we can't simplify x.y nor y.z independently.
//
// Generalizing, we can end up with an arbitrarily long reference
// chain a.b.c...y.z where b...y are final fields pointing at
// unrepresentable native structs, and we must collapse this whole
// chain in one chunk, or not at all.
//
// The following logic detects such chains, and finishes with
// baseObject = a (the representable base expression), and
// firstDereference = a.b and secondDereference = a.b.c (or NULL,
// if the expression is just a.b and a is representable).
// Note that our analysis starts from the "z" dereference, because
// that is the root TR::Node in this expression tree, and works its
// way down the tree to "a".
if (typeIsConstible) // can't yet tell whether the expression chain results in a Java object, so just do primitives
{
while (baseObject->getOpCode().isLoadIndirect() && isFinalFieldPointingAtUnrepresentableNativeStruct(baseObject->getSymbolReference(), comp))
{
secondDereference = firstDereference;
firstDereference = baseObject;
baseObject = firstDereference->getFirstChild();
}
// at this point, baseObject is some representable expression
J9Class *clazz = NULL;
if (baseObject->getOpCode().isLoadIndirect())
{
if (isFinalFieldPointingAtNativeStruct(baseObject->getSymbolReference(), comp))
{
TR_ASSERT(isFinalFieldPointingAtRepresentableNativeStruct(baseObject->getSymbolReference(), comp), "Base expression pointing at native struct must be representable"); // Good luck to the poor soul doing triage trying to figure out what this means
// Don't bother continuing. Under normal circumstances, a
// representable base object expression should already
// have gone through this process and been simplified into
// a const or loadaddr.
dumpOptDetails(comp, "Could have transformed %p if representable baseObject %p had already been simplified\n", node, baseObject);
return NULL;
}
else
{
// baseObject is an expression resulting in a Java object reference.
// The logic below handles figuring out the address of the base Java
// object. Proceed with that, then do the dereference below.
}
}
else if (baseObject->chkClassPointerConstant())
{
clazz = (J9Class*)baseObject->getAddress();
}
else if (baseObject->getOpCodeValue() == TR::loadaddr
&& baseObject->getSymbol()->isClassObject())
{
clazz = (J9Class*)baseObject->getSymbol()->castToStaticSymbol()->getStaticAddress();
}
else
{
// TODO: The above checks are too conservative. There's no
// need to check for class pointers explicitly. Once we know
// it's a representable object, we can do getAddress, getInt,
// getLongInt, or getStaticAddress depending on the opcode,
// and use whatever we get back from that to start dereferencing.
dumpOptDetails(comp, "Can't yet transform %p based on representable baseObject %p that isn't a J9Class\n", node, baseObject);
return NULL;
}
if (clazz && performTransformation(comp, "O^O transformIndirectLoad: [%p] evaluate native structure walk into const from %s, based on class pointer node %p\n", node, node->getSymbolReference()->getName(comp->getDebug()), baseObject))
{
// We have clazz.a.b.c.d.primitiveField
// We can simplify this into a const.
//
uint8_t *structure = (uint8_t*)dereferenceStructPointerChain(clazz, baseObject, node->getFirstChild(), comp);
if (verifyFieldAccess(structure, node->getSymbolReference(), comp)) switch (loadType)
{
case TR::Int32:
node = TR::Node::iconst(node, *(int32_t*)(structure + node->getSymbolReference()->getOffset()));
break;
case TR::Int64:
node = TR::Node::lconst(node, *(int64_t*)(structure + node->getSymbolReference()->getOffset()));
break;
default:
TR_ASSERT(0, "Unexpected native field type %s", node->getDataType().toString());
break;
}
return node;
}
}
}
uintptr_t *baseObjectRefLocation = NULL;
if (baseObject->getOpCode().hasSymbolReference() && baseObject->getSymbolReference()->hasKnownObjectIndex())
{
baseObjectRefLocation = baseObject->getSymbolReference()->getKnownObjectReferenceLocation(comp);
}
else if (baseObject->getOpCode().isLoadVarDirect() && baseObject->getSymbol()->isStatic())
{
TR::StaticSymbol *baseSym = baseObject->getSymbol()->castToStaticSymbol();
if (!baseObject->getSymbolReference()->isUnresolved())
{
// Note: There is an implicit assumption here that if we can reach
// a const object, then its constructor must have completed and its
// final fields will never change again. Since an object can't be
// reached through the constant pool or call site table until its
// constructor completes, there is no known case where this is false,
// but if one ever arises, we'll need to check for that case here.
//
if (baseSym->isFixedObjectRef())
baseObjectRefLocation = (uintptr_t*)baseSym->getStaticAddress();
}
}
if (!baseObjectRefLocation
#if defined(J9VM_OPT_JITSERVER)
|| comp->isOutOfProcessCompilation() // The following code requires VM access that's not supported at the server
#endif /* defined(J9VM_OPT_JITSERVER) */
)
return NULL; // Nothing left we can do
//
// Now we know we have an indirect from an identifiable location.
// We can use this information to simplify loads of final fields.
//
if (isFinalFieldOfNativeStruct(symRef, comp))
{
if (baseObjectRefLocation && typeIsConstible)
{
if (performTransformation(comp, "O^O transformIndirectLoad: [%p] evaluate native structure walk into const from %s, based on known object node %p\n", node, node->getSymbolReference()->getName(comp->getDebug()), baseObject))
{
// We have object.a.b.c.d.primitiveField, where a.b.c.d walks through some native data structure.
// We can simplify this into a const.
//
bool loadFailed = false;
uint8_t *structure;
{
// First dereference with VM access to dig a pointer out of an object
TR::VMAccessCriticalSection digPointerOutOfObject(comp->fej9());
uintptr_t baseObjectRef = baseObject->getSymbol()->isStatic() ?
comp->fej9()->getStaticReferenceFieldAtAddress((uintptr_t)baseObjectRefLocation) :
*baseObjectRefLocation;
uintptr_t fieldAddress = baseObjectRef + firstDereference->getSymbolReference()->getOffset();
switch (firstDereference->getDataType())
{
case TR::Int32:
{
uint32_t *intAddress = (uint32_t*)fieldAddress;
structure = (uint8_t*)(uintptr_t)(*intAddress);
}
break;
case TR::Int64:
{
uint64_t *longAddress = (uint64_t*)fieldAddress;
structure = (uint8_t*)(uintptr_t)(*longAddress);
}
break;
case TR::Address:
{
uintptr_t *refAddress = (uintptr_t*)fieldAddress;
structure = (uint8_t*)(uintptr_t)(*refAddress);
}
break;
default:
loadFailed = true;
break;
}
}
TR_ASSERT(!loadFailed, "Expected int, long, or address load");
TR_ASSERT(secondDereference, "Expected expression %p like a.b...c where a is a Java object, b points at a native struct, and c is a field in a native struct", node);
if (secondDereference)
structure = (uint8_t*)dereferenceStructPointerChain(structure, firstDereference, node->getFirstChild(), comp);
if (verifyFieldAccess(structure, node->getSymbolReference(), comp))
{
switch (loadType)
{
case TR::Int32:
node = TR::Node::iconst(node, *(int32_t*)(structure + node->getSymbolReference()->getOffset()));
break;
case TR::Int64:
node = TR::Node::lconst(node, *(int64_t*)(structure + node->getSymbolReference()->getOffset()));
break;
default:
TR_ASSERT(0, "Unexpected native field type %s", node->getDataType().toString());
break;
}
}
return node;
}
}
}
else if (fej9->isFinalFieldPointingAtJ9Class(symRef, comp)
&& isFinalFieldPointingAtRepresentableNativeStruct(symRef, comp)
&& !comp->compileRelocatableCode())
{
TR_OpaqueClassBlock *clazz;
{
TR::VMAccessCriticalSection getClassFromJavaLangClassCS(comp->fej9());
clazz = fej9->getClassFromJavaLangClass(*baseObjectRefLocation);
}
TR::Node *clazzNode = TR::Node::createWithSymRef(node, TR::loadaddr, 0,
comp->getSymRefTab()->findOrCreateClassSymbol(comp->getMethodSymbol(), -1, clazz));
if (performTransformation(comp, "O^O transformIndirectLoad: [%p] turn load of J9Class into %s %p\n", node, clazzNode->getOpCode().getName(), clazzNode))
return clazzNode;
}
else if (sym->isFinal() && !comp->compileRelocatableCode()) // Constructor can set different values for the same field in different runs on AOT
{
int32_t fieldOffset = symRef->getOffset() - TR::Compiler->om.objectHeaderSizeInBytes(); // blah
if (typeIsConstible)
{
if (performTransformation(comp, "O^O transformIndirectLoad: [%p] turn final %s %s into load const\n", node, node->getOpCode().getName(), symRef->getName(comp->getDebug())))
{
TR::VMAccessCriticalSection recreate(comp->fej9());
uintptr_t baseObjectRef = baseObject->getSymbol()->isStatic() ?
comp->fej9()->getStaticReferenceFieldAtAddress((uintptr_t)baseObjectRefLocation) :
*baseObjectRefLocation;
node->getAndDecChild(0);
node->setNumChildren(0);
switch (loadType)
{
case TR::Int32:
TR::Node::recreate(node, TR::iconst);
node->setInt(fej9->getInt32FieldAt(baseObjectRef, fieldOffset));
break;
case TR::Int64:
TR::Node::recreate(node, TR::lconst);
node->setLongInt(fej9->getInt64FieldAt(baseObjectRef, fieldOffset));
break;
default:
TR_ASSERT(0, "Unexpected type %s", node->getDataType().toString());
break;
}
return node;
}
}
else if (symrefIsImprovable)
{
uintptr_t targetObjectReference = 0;
TR::SymbolReference *improvedSymRef = node->getSymbolReference();
TR::KnownObjectTable *knot = comp->getOrCreateKnownObjectTable();
#if defined(J9VM_OPT_JITSERVER)
if (comp->isOutOfProcessCompilation())
{
bool knotEnabled = (knot != NULL);
auto stream = TR::CompilationInfo::getStream();
stream->write(JITServer::MessageType::KnownObjectTable_getReferenceField,
baseObject->getSymbol()->isStatic(), baseObjectRefLocation, fieldOffset, knotEnabled);
auto recv = stream->read<TR::KnownObjectTable::Index, uintptr_t*, uintptr_t>();
TR::KnownObjectTable::Index knotIndex = std::get<0>(recv);
uintptr_t *objectPointerReference = std::get<1>(recv);
targetObjectReference = std::get<2>(recv);
if (knot && (knotIndex != TR::KnownObjectTable::UNKNOWN))
{
knot->updateKnownObjectTableAtServer(knotIndex, objectPointerReference);
improvedSymRef = comp->getSymRefTab()->findOrCreateSymRefWithKnownObject(node->getSymbolReference(), knotIndex);
}
}
else
#endif /* defined(J9VM_OPT_JITSERVER) */
{
TR::VMAccessCriticalSection getReferenceField(comp->fej9());
uintptr_t baseObjectRef = baseObject->getSymbol()->isStatic() ?
comp->fej9()->getStaticReferenceFieldAtAddress((uintptr_t)baseObjectRefLocation) :
*baseObjectRefLocation;
targetObjectReference = fej9->getReferenceFieldAt(baseObjectRef, fieldOffset);
if (knot)
{
TR::KnownObjectTable::Index knotIndex = knot->getOrCreateIndexAt(&targetObjectReference);
if (knotIndex != TR::KnownObjectTable::UNKNOWN)
improvedSymRef = comp->getSymRefTab()->findOrCreateSymRefWithKnownObject(node->getSymbolReference(), knotIndex);
}
}
if (targetObjectReference)
{
if (improvedSymRef->hasKnownObjectIndex()
&& performTransformation(comp, "O^O transformIndirectLoad: [%p] use object-specific symref #%d (=obj%d) for %s of final field %s\n",
node, improvedSymRef->getReferenceNumber(), improvedSymRef->getKnownObjectIndex(), node->getOpCode().getName(), symRef->getName(comp->getDebug())))
{
node->setSymbolReference(improvedSymRef);
node->setIsNull(false);
node->setIsNonNull(true);
}
}
else
{
switch (improvedSymRef->getSymbol()->getRecognizedField())
{
// J9VMInternals.jitHelpers is initialized after the class has been
// initialized via an Unsafe helper - don't fold null in to improve perf
case TR::Symbol::Java_lang_J9VMInternals_jitHelpers:
return NULL;
default:
if (performTransformation(comp, "O^O transformIndirectLoad: [%p] field is null - change to aconst NULL\n", node))
{
node->getAndDecChild(0);
node->setNumChildren(0);
TR::Node::recreate(node, TR::aconst);
node->setAddress(0);
node->setIsNull(true);
node->setIsNonNull(false);
}
}
}
return node;
}
}
}
}
return NULL;
}
/** \brief
* Entry point for folding whitelist'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)
{