-
Notifications
You must be signed in to change notification settings - Fork 746
/
Copy pathInlinerTempForJ9.cpp
7938 lines (6756 loc) · 363 KB
/
InlinerTempForJ9.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 <algorithm>
#include "j9cfg.h"
#include "optimizer/Inliner.hpp"
#include "optimizer/J9Inliner.hpp"
#include "optimizer/J9EstimateCodeSize.hpp"
#include "optimizer/VectorAPIExpansion.hpp"
#include "env/KnownObjectTable.hpp"
#include "compile/InlineBlock.hpp"
#include "compile/Method.hpp"
#include "compile/OSRData.hpp"
#include "compile/ResolvedMethod.hpp"
#include "env/CompilerEnv.hpp"
#include "env/CHTable.hpp"
#include "env/PersistentCHTable.hpp"
#include "env/VMJ9.h"
#include "env/jittypes.h"
#include "il/Block.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ParameterSymbol.hpp"
#include "il/StaticSymbol.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "optimizer/CallInfo.hpp"
#include "optimizer/J9CallGraph.hpp"
#include "optimizer/PreExistence.hpp"
#include "optimizer/Structure.hpp"
#include "codegen/CodeGenerator.hpp"
#include "codegen/CodeGenerator_inlines.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "ilgen/IlGenRequest.hpp"
#include "ilgen/IlGeneratorMethodDetails.hpp"
#include "ilgen/IlGeneratorMethodDetails_inlines.hpp"
#include "optimizer/Structure.hpp" // TR_RegionAnalysis
#include "optimizer/StructuralAnalysis.hpp"
#include "control/Recompilation.hpp" //TR_PersistentJittedBodyInfo
#include "control/RecompilationInfo.hpp" //TR_PersistentJittedBodyInfo
#include "optimizer/EstimateCodeSize.hpp"
#include "env/VMJ9.h"
#include "runtime/J9Profiler.hpp"
#include "ras/DebugCounter.hpp"
#include "j9consts.h"
#include "optimizer/TransformUtil.hpp"
namespace TR { class SimpleRegex; }
#define OPT_DETAILS "O^O INLINER: "
// == Hack markers ==
// To conserve owning method indexes, we share TR_ResolvedMethods even where
// the owning method differs. Usually that is done only for methods that the
// inliner never sees, but if we get aggressive and share TR_ResolvedMethods
// that are exposed to the inliner, then the inliner needs to make sure it
// doesn't rely on the "owning method" accurately representing the calling method.
//
// This is a fragile design that needs some more thought. We should either
// eliminate the limitations that motivate sharing in the first place, or else
// take the time to modify inliner (and everyone else) so it doesn't rely on
// owning method information to indicate the caller.
//
// For now, we mark such code with this macro.
//
#define OWNING_METHOD_MAY_NOT_BE_THE_CALLER (1)
#define MIN_NUM_CALLERS 20
#define SIZE_MULTIPLIER 4
#define FANIN_OTHER_BUCKET_THRESHOLD 0.5
#define DEFAULT_CONST_CLASS_WEIGHT 10
#undef TRACE_CSI_IN_INLINER
const char* TR_PrexArgument::priorKnowledgeStrings[] = { "", "(preexistent) ", "(fixed-class) ", "(known-object) " };
static bool isWarm(TR::Compilation *comp)
{
return comp->getMethodHotness() >= warm;
}
static bool isHot(TR::Compilation *comp)
{
return comp->getMethodHotness() >= hot;
}
static bool isScorching(TR::Compilation *comp)
{
return ((comp->getMethodHotness() >= scorching) || ((comp->getMethodHotness() >= veryHot) && comp->isProfilingCompilation())) ;
}
static int32_t getJ9InitialBytecodeSize(TR_ResolvedMethod * feMethod, TR::ResolvedMethodSymbol * methodSymbol, TR::Compilation *comp)
{
int32_t size = feMethod->maxBytecodeIndex();
if (methodSymbol && methodSymbol->getRecognizedMethod() == TR::java_util_ArrayList_remove)
{
size >>= 1;
}
if (feMethod->getRecognizedMethod() == TR::java_lang_String_indexOf_String_int ||
feMethod->getRecognizedMethod() == TR::java_lang_String_init_String ||
feMethod->getRecognizedMethod() == TR::java_lang_String_indexOf_fast ||
feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_subMulSetScale ||
feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_addAddMulSetScale ||
feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_mulSetScale ||
feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_noLLOverflowAdd ||
feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_noLLOverflowMul ||
feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_subMulAddAddMulSetScale ||
feMethod->getRecognizedMethod() == TR::com_ibm_ws_webcontainer_channel_WCCByteBufferOutputStream_printUnencoded ||
feMethod->getRecognizedMethod() == TR::java_lang_String_equals)
{
size >>= 1;
}
else if (((TR_ResolvedJ9Method*)feMethod)->isDAAWrapperMethod())
{
size = 1;
}
else if (((TR_ResolvedJ9Method*)feMethod)->isDAAIntrinsicMethod())
{
size >>= 3;
}
else if (feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_valueOf )
{
size >>= 2;
}
else if (feMethod->getRecognizedMethod() == TR::java_math_BigDecimal_add ||
feMethod->getRecognizedMethod() == TR::java_lang_String_init_int_String_int_String_String ||
feMethod->getRecognizedMethod() == TR::com_ibm_jit_DecimalFormatHelper_formatAsDouble ||
feMethod->getRecognizedMethod() == TR::com_ibm_jit_DecimalFormatHelper_formatAsFloat)
{
size >>= 3;
}
else if (strncmp(feMethod->nameChars(), "toString", 8) == 0 ||
strncmp(feMethod->nameChars(), "multiLeafArrayCopy", 18) == 0)
{
size >>= 1;
}
else if (!comp->getOption(TR_DisableAdaptiveDumbInliner))
{
if (methodSymbol && !methodSymbol->mayHaveInlineableCall() && size <= 5) // favor the inlining of methods that are very small
size = 0;
}
TR_J9EstimateCodeSize::adjustEstimateForStringCompression(feMethod, size, TR_J9EstimateCodeSize::STRING_COMPRESSION_ADJUSTMENT_FACTOR);
return size;
}
static bool insideIntPipelineForEach(TR_ResolvedMethod *method, TR::Compilation *comp)
{
const char *sig = "accept";
bool returnValue = true; //default is true since if first method is IntPipeline.forEach true is returned
//Searches up the owning method chain until IntPipeline.forEach is found
//If the first method passed into this function is IntPipeline$Head.forEach or IntPipeline.forEach, true is returned
//since IntPipeline$Head.forEach and IntPipeline.forEach needs to be inlined for JIT GPU.
//If not the method name is checked to see if it is called accept.
//If the method in the chain just before IntPipeline.forEach is accept, true is also returned
//This tries to inline accept and the methods inside accept
//IntPipelineHead.forEach must also be inlined.
if (method && comp->getOptions()->getEnableGPU(TR_EnableGPU) && comp->hasIntStreamForEach())
{
if (method->getRecognizedMethod() == TR::java_util_stream_IntPipelineHead_forEach)
return true;
while (method)
{
if (method->getRecognizedMethod() == TR::java_util_stream_IntPipeline_forEach)
return returnValue;
//If the current method is accept, true is returned if the next method is IntPipeline.forEach
//Otherwise, if the next method is IntPipeline.forEach, false is returned
if (strncmp(method->nameChars(), sig, strlen(sig)) == 0)
returnValue = true;
else
returnValue = false;
method = method->owningMethod();
}
}
return false;
}
bool
TR_J9InlinerPolicy::inlineRecognizedMethod(TR::RecognizedMethod method)
{
// if (method ==
// TR::java_lang_String_init_String_char)
// return false;
if (comp()->cg()->suppressInliningOfRecognizedMethod(method))
return false;
if (comp()->isConverterMethod(method) &&
comp()->canTransformConverterMethod(method))
return false;
// Check for memoizing methods
if (comp()->getOption(TR_DisableDememoization) || (comp()->getMethodHotness() < hot)) // TODO: This should actually check whether EA will run, but that's not crucial for correctness
{
switch (method)
{
case TR::java_lang_Integer_valueOf:
comp()->getMethodSymbol()->setHasNews(true);
return true;
default:
break;
}
}
else if (method == TR::java_lang_Integer_valueOf)
return false;
if (willBeInlinedInCodeGen(method))
return false;
return true;
}
int32_t
TR_J9InlinerPolicy::getInitialBytecodeSize(TR_ResolvedMethod *feMethod, TR::ResolvedMethodSymbol * methodSymbol, TR::Compilation *comp)
{
return getJ9InitialBytecodeSize(feMethod, methodSymbol, comp);
}
bool
TR_J9InlinerPolicy::aggressivelyInlineInLoops()
{
return _aggressivelyInlineInLoops;
}
void
TR_J9InlinerPolicy::determineAggressionInLoops(TR::ResolvedMethodSymbol *callerSymbol)
{
if (isHot(comp()) && OMR_InlinerPolicy::getInitialBytecodeSize(callerSymbol, comp()) < 100)
_aggressivelyInlineInLoops = true;
}
void
TR_J9InlinerPolicy::determineInliningHeuristic(TR::ResolvedMethodSymbol *callerSymbol)
{
determineAggressionInLoops(callerSymbol);
return;
}
void
TR_MultipleCallTargetInliner::NodeEstimate::operator ()(TR_CallTarget *ct, TR::Compilation *comp)
{
static const char *qq1 = feGetEnv("TR_NodeEstimateNumerator");
static const uint32_t userNumer = ( qq1 ) ? atoi(qq1) : 1;
uint32_t numer = userNumer;
if (!qq1)
numer = comp->getOptLevel() >= hot ? 1 : 4;
static const char *qq2 = feGetEnv("TR_NodeEstimateDenominator");
static const uint32_t denom = ( qq2 ) ? atoi(qq2) : 1;
int32_t size = (numer * getJ9InitialBytecodeSize(ct->_calleeMethod, 0, comp))/denom;
// only scale the inlining size when the method is non-empty - conversion of
// NaN/Inf to int is undefined and an empty method partially inlined instead
// of fully inlined is still empty
if(ct->_isPartialInliningCandidate && ct->_fullSize != 0)
{
size = size * ((float)(ct->_partialSize)/(float)(ct->_fullSize));
}
_nodeEstimate += size;
}
bool
TR_J9InlinerPolicy::mustBeInlinedEvenInDebug(TR_ResolvedMethod * calleeMethod, TR::TreeTop *callNodeTreeTop)
{
if (calleeMethod)
{
switch (calleeMethod->convertToMethod()->getMandatoryRecognizedMethod())
{
// call to invokeExactTargetAddress are generated out of thin air by our JSR292
// implementation, but we never want the VM or anyone else to know this so we must
// always inline the implementation
case TR::java_lang_invoke_MethodHandle_invokeExactTargetAddress:
{
TR::TreeTop *scanTT = callNodeTreeTop->getNextTreeTop();
TR::Node *nextCall = NULL;
while (scanTT &&
scanTT->getNode()->getByteCodeInfo().getByteCodeIndex() == callNodeTreeTop->getNode()->getByteCodeInfo().getByteCodeIndex() &&
scanTT->getNode()->getByteCodeInfo().getCallerIndex() == callNodeTreeTop->getNode()->getByteCodeInfo().getCallerIndex())
{
TR::Node *scanNode = scanTT->getNode();
if (scanNode && (scanNode->getOpCode().isCheck() || scanNode->getOpCodeValue() == TR::treetop))
scanNode = scanNode->getFirstChild();
if (scanNode->getOpCode().isCall())
{
nextCall = scanNode;
break;
}
scanTT = scanTT->getNextTreeTop();
}
debugTrace(tracer(), "considering nextOperation node n%dn", nextCall->getGlobalIndex());
if (nextCall && nextCall->getOpCode().hasSymbolReference() &&
nextCall->getSymbolReference()->getSymbol()->getMethodSymbol()->isComputedVirtual())
return true;
}
default:
break;
}
}
return false;
}
/** Test for methods that we wish to inline whenever possible.
Identify methods for which the benefits of inlining them into the caller
are particularly significant and which might not otherwise be chosen by
the inliner.
*/
bool
TR_J9InlinerPolicy::alwaysWorthInlining(TR_ResolvedMethod * calleeMethod, TR::Node *callNode)
{
if (!calleeMethod)
return false;
if (isInlineableJNI(calleeMethod, callNode))
return true;
if (((TR_ResolvedJ9Method*)calleeMethod)->isDAAWrapperMethod())
return true;
if (isJSR292AlwaysWorthInlining(calleeMethod))
return true;
switch (calleeMethod->getRecognizedMethod())
{
case TR::sun_misc_Unsafe_getAndAddLong:
case TR::sun_misc_Unsafe_getAndSetLong:
return comp()->target().is32Bit();
case TR::java_lang_J9VMInternals_fastIdentityHashCode:
case TR::java_lang_Class_getSuperclass:
case TR::java_lang_String_regionMatchesInternal:
case TR::java_lang_String_regionMatches:
case TR::java_lang_Class_newInstance:
case TR::jdk_internal_util_Preconditions_checkIndex:
// AbstractMemorySegmentImpl.reinterpret methods call Reflection.getCallerClass
case TR::jdk_internal_foreign_AbstractMemorySegmentImpl_reinterpret:
// we rely on inlining compareAndSwap so we see the inner native call and can special case it
case TR::com_ibm_jit_JITHelpers_compareAndSwapIntInObject:
case TR::com_ibm_jit_JITHelpers_compareAndSwapLongInObject:
case TR::com_ibm_jit_JITHelpers_compareAndSwapObjectInObject:
case TR::com_ibm_jit_JITHelpers_compareAndSwapIntInArray:
case TR::com_ibm_jit_JITHelpers_compareAndSwapLongInArray:
case TR::com_ibm_jit_JITHelpers_compareAndSwapObjectInArray:
case TR::com_ibm_jit_JITHelpers_jitHelpers:
case TR::com_ibm_jit_JITHelpers_intrinsicIndexOfLatin1:
case TR::com_ibm_jit_JITHelpers_intrinsicIndexOfUTF16:
case TR::java_lang_String_charAt:
case TR::java_lang_String_charAtInternal_I:
case TR::java_lang_String_charAtInternal_IB:
case TR::java_lang_String_checkIndex:
case TR::java_lang_String_coder:
case TR::java_lang_String_isLatin1:
case TR::java_lang_String_length:
case TR::java_lang_String_lengthInternal:
case TR::java_lang_String_isCompressed:
case TR::java_lang_StringBuffer_capacityInternal:
case TR::java_lang_StringBuffer_lengthInternalUnsynchronized:
case TR::java_lang_StringBuilder_capacityInternal:
case TR::java_lang_StringBuilder_lengthInternal:
case TR::java_lang_StringUTF16_charAt:
case TR::java_lang_StringUTF16_checkIndex:
case TR::java_lang_StringUTF16_length:
case TR::java_lang_StringUTF16_newBytesFor:
case TR::java_util_HashMap_get:
case TR::java_util_HashMap_getNode:
case TR::java_util_HashMap_getNode_Object:
case TR::java_lang_String_getChars_charArray:
case TR::java_lang_String_getChars_byteArray:
case TR::java_lang_Integer_toUnsignedLong:
case TR::java_nio_Bits_byteOrder:
case TR::java_nio_ByteOrder_nativeOrder:
return true;
case TR::jdk_internal_misc_Unsafe_compareAndExchangeInt:
case TR::jdk_internal_misc_Unsafe_compareAndExchangeLong:
case TR::jdk_internal_misc_Unsafe_compareAndExchangeReference:
return false;
/* In Java9 the compareAndSwap[Int|Long|Object] and copyMemory enums match
* both sun.misc.Unsafe and jdk.internal.misc.Unsafe. The sun.misc.Unsafe
* methods are simple wrappers to call jdk.internal impls, and we want to
* inline them. Since the same code can run with Java8 classes where
* sun.misc.Unsafe has the JNI impl, we need to differentiate by testing
* with isNative(). If it is native, then we don't need to inline it as it
* will be handled elsewhere.
*
* Starting from Java12, compareAndExchangeObject was changed from being a
* native to being a simple wrapper to call compareAndExchangeReference.
* The enum matches both cases and we only want to force inlining on the
* non-native case. If the native case reaches here, it means it already
* failed the isInlineableJNI check and should not be force inlined.
*/
case TR::jdk_internal_misc_Unsafe_compareAndExchangeObject:
case TR::sun_misc_Unsafe_compareAndSwapInt_jlObjectJII_Z:
case TR::sun_misc_Unsafe_compareAndSwapLong_jlObjectJJJ_Z:
case TR::sun_misc_Unsafe_compareAndSwapObject_jlObjectJjlObjectjlObject_Z:
case TR::sun_misc_Unsafe_copyMemory:
return !calleeMethod->isNative();
default:
break;
}
if (!strncmp(calleeMethod->classNameChars(), "java/util/concurrent/atomic/", strlen("java/util/concurrent/atomic/")))
{
return true;
}
int32_t length = calleeMethod->classNameLength();
char* className = calleeMethod->classNameChars();
if (length == 24 && !strncmp(className, "jdk/internal/misc/Unsafe", 24))
return true;
else if (length == 15 && !strncmp(className, "sun/misc/Unsafe", 15))
return true;
if (!comp()->getOption(TR_DisableForceInlineAnnotations) &&
comp()->fej9()->isForceInline(calleeMethod))
{
if (comp()->trace(OMR::inlining))
traceMsg(comp(), "@ForceInline was specified for %s, in alwaysWorthInlining\n", calleeMethod->signature(comp()->trMemory()));
return true;
}
if (calleeMethod->getRecognizedMethod() == TR::unknownMethod &&
comp()->fej9()->isIntrinsicCandidate(calleeMethod) &&
!comp()->getOption(TR_DisableInliningUnrecognizedIntrinsics))
{
if (comp()->trace(OMR::inlining))
traceMsg(comp(), "@IntrinsicCandidate was specified for %s, in alwaysWorthInlining\n", calleeMethod->signature(comp()->trMemory()));
return true;
}
return false;
}
/*
* Check if there is any method handle invoke left in the method body
*/
static bool checkForRemainingInlineableJSR292(TR::Compilation *comp, TR::ResolvedMethodSymbol *methodSymbol)
{
TR::NodeChecklist visited(comp);
for (TR::TreeTop *tt = methodSymbol->getFirstTreeTop(); tt ; tt = tt->getNextTreeTop())
{
TR::Node *ttNode = tt->getNode();
if (ttNode->getNumChildren() > 0)
{
TR::Node *node = ttNode->getFirstChild();
if (node->getOpCode().isCall() && !visited.contains(node))
{
visited.add(node);
if (node->getSymbolReference()->getSymbol()->getResolvedMethodSymbol())
{
TR_ResolvedMethod * resolvedMethod = node->getSymbolReference()->getSymbol()->getResolvedMethodSymbol()->getResolvedMethod();
if (!node->isTheVirtualCallNodeForAGuardedInlinedCall() &&
(comp->fej9()->isLambdaFormGeneratedMethod(resolvedMethod) ||
resolvedMethod->getRecognizedMethod() == TR::java_lang_invoke_MethodHandle_invokeBasic ||
resolvedMethod->convertToMethod()->isArchetypeSpecimen() ||
resolvedMethod->getRecognizedMethod() == TR::java_lang_invoke_MethodHandle_invokeExact))
{
return true;
}
}
}
}
}
return false;
}
void
TR_J9InlinerUtil::requestAdditionalOptimizations(TR_CallTarget *calltarget)
{
#if !defined(J9VM_OPT_OPENJDK_METHODHANDLE)
if (calltarget->_myCallSite->getDepth() == -1 // only do this for top level callee to prevent exponential walk of inlined trees
&& checkForRemainingInlineableJSR292(comp(), calltarget->_calleeSymbol))
{
_inliner->getOptimizer()->setRequestOptimization(OMR::methodHandleInvokeInliningGroup);
if (comp()->trace(OMR::inlining))
heuristicTrace(tracer(),"Requesting one more pass of targeted inlining due to method handle invoke in %s\n", tracer()->traceSignature(calltarget->_calleeSymbol));
}
#endif
}
void
TR_J9InlinerUtil::adjustByteCodeSize(TR_ResolvedMethod *calleeResolvedMethod, bool isInLoop, TR::Block *block, int &bytecodeSize)
{
traceMsg(comp(), "Reached new code \n");
int32_t blockNestingDepth = 1;
if (isInLoop)
{
char *tmptmp=0;
if (calleeResolvedMethod)
tmptmp = TR::Compiler->cls.classSignature(comp(), calleeResolvedMethod->containingClass(),trMemory());
bool doit = false;
if (((TR_J9InlinerPolicy *)inliner()->getPolicy())->aggressivelyInlineInLoops())
{
doit = true;
}
if (doit && calleeResolvedMethod && !strcmp(tmptmp,"Ljava/math/BigDecimal;"))
{
traceMsg(comp(), "Reached code for block nesting depth %d\n", blockNestingDepth);
if ((isInLoop || (blockNestingDepth > 1)) &&
(bytecodeSize > 10))
{
if (comp()->trace(OMR::inlining))
heuristicTrace(tracer(),"Exceeds Size Threshold: Scaled down size for call block %d from %d to %d\n", block->getNumber(), bytecodeSize, 10);
bytecodeSize = 15;
}
}
else
heuristicTrace(tracer(),"Omitting Big Decimal method from size readjustment, calleeResolvedMethod = %p, tmptmp =%s",calleeResolvedMethod, tmptmp);
}
}
TR::Node *
TR_J9InlinerPolicy::genCompressedRefs(TR::Node * address, bool genTT, int32_t isLoad)
{
static char *pEnv = feGetEnv("TR_UseTranslateInTrees");
if (performTransformation(comp(), "O^O Inliner: Generating compressedRefs anchor for node [%p]\n", address))
{
TR::Node *value = address;
if (pEnv && (isLoad < 0)) // store
value = address->getSecondChild();
TR::Node *newAddress = TR::Node::createCompressedRefsAnchor(value);
//traceMsg(comp(), "compressedRefs anchor %p generated\n", newAddress);
if (!pEnv && genTT)
{
if (!newAddress->getOpCode().isTreeTop())
newAddress = TR::Node::create(TR::treetop, 1, newAddress);
}
else
return newAddress;
}
return NULL;
}
TR::Node *
TR_J9InlinerPolicy::createUnsafeAddressWithOffset(TR::Node * unsafeCall)
{
if (comp()->target().is64Bit())
{
TR::Node *constNode = TR::Node::lconst(unsafeCall, ~(J9_SUN_FIELD_OFFSET_MASK));
return TR::Node::create(TR::aladd, 2, unsafeCall->getChild(1), TR::Node::create(TR::land, 2, unsafeCall->getChild(2), constNode));
}
return TR::Node::create(TR::aiadd, 2, unsafeCall->getChild(1), TR::Node::create(TR::iand, 2, TR::Node::create(TR::l2i, 1, unsafeCall->getChild(2)), TR::Node::iconst(unsafeCall, ~(J9_SUN_FIELD_OFFSET_MASK))));
}
void
TR_J9InlinerPolicy::createTempsForUnsafeCall( TR::TreeTop *callNodeTreeTop, TR::Node * unsafeCallNode )
{
//This method is intended to replace and generalize createTempsForUnsafePutGet
//Will go through all children of the unsafeCallNode and create a store tree that child, insert it before the call, and then replace the call's child with a load of the symref.
for (int32_t i = 0 ; i < unsafeCallNode->getNumChildren() ; ++i)
{
TR::Node *child = unsafeCallNode->getChild(i);
//create a store of the correct type and insert before call.
TR::DataType dataType = child->getDataType();
TR::SymbolReference *newSymbolReference = comp()->getSymRefTab()->createTemporary(comp()->getMethodSymbol(), dataType);
TR::Node *storeNode = TR::Node::createWithSymRef(comp()->il.opCodeForDirectStore(dataType), 1, 1, child, newSymbolReference);
TR::TreeTop *storeTree = TR::TreeTop::create(comp(), storeNode);
debugTrace(tracer(),"Creating store node %p with child %p",storeNode,child);
callNodeTreeTop->insertBefore(storeTree);
// Replace the old child with a load of the new sym ref
TR::Node *value = TR::Node::createWithSymRef(child, comp()->il.opCodeForDirectLoad(dataType), 0, newSymbolReference);
debugTrace(tracer(),"Replacing callnode %p child %p with %p",unsafeCallNode,unsafeCallNode->getChild(i),value);
unsafeCallNode->setAndIncChild(i, value);
child->recursivelyDecReferenceCount();
}
}
void
TR_J9InlinerPolicy::createTempsForUnsafePutGet(TR::Node*& unsafeAddress,
TR::Node* unsafeCall,
TR::TreeTop* callNodeTreeTop,
TR::Node*& offset,
TR::SymbolReference*& newSymbolReferenceForAddress,
bool isUnsafeGet)
{
TR::Node *oldUnsafeAddress = unsafeAddress;
TR::DataType dataType = unsafeAddress->getDataType();
TR::SymbolReference *newSymbolReference =
comp()->getSymRefTab()->createTemporary(comp()->getMethodSymbol(), dataType);
newSymbolReferenceForAddress = newSymbolReference;
TR::Node *storeNode =
TR::Node::createWithSymRef(comp()->il.opCodeForDirectStore(unsafeAddress->getDataType()),
1, 1, unsafeAddress, newSymbolReference);
TR::TreeTop *storeTree = TR::TreeTop::create(comp(), storeNode);
if (tracer()->debugLevel())
{
debugTrace(tracer(), "\tIn createTempsForUnsafePutGet. inserting store Tree before callNodeTT:\n");
comp()->getDebug()->print(comp()->getOutFile(), storeTree);
}
callNodeTreeTop->insertTreeTopsBeforeMe(storeTree);
// Replace the old child with a load of the new sym ref
unsafeAddress =
TR::Node::createWithSymRef(unsafeAddress,
comp()->il.opCodeForDirectLoad(unsafeAddress->getDataType()),
0, newSymbolReference);
debugTrace(tracer(), "\tIn createTempsForUnsafePutGet. replacing unsafeCall ( %p) child %p with %p\n", unsafeCall, unsafeCall->getChild(1), unsafeAddress);
unsafeCall->setAndIncChild(1, unsafeAddress);
TR::Node *oldOffset = offset;
dataType = offset->getDataType();
newSymbolReference = comp()->getSymRefTab()->createTemporary(comp()->getMethodSymbol(),
dataType);
storeNode = TR::Node::createWithSymRef(comp()->il.opCodeForDirectStore(offset->getDataType()),
1, 1, offset, newSymbolReference);
storeTree = TR::TreeTop::create(comp(), storeNode);
if (tracer()->debugLevel())
{
traceMsg(comp(), "\tIn createTempsForUnsafePutGet. inserting store Tree before callNodeTT 2:\n");
comp()->getDebug()->print(comp()->getOutFile(), storeTree);
}
callNodeTreeTop->insertTreeTopsBeforeMe(storeTree);
// Replace the old child with a load of the new sym ref
offset = TR::Node::createWithSymRef(offset,
comp()->il.opCodeForDirectLoad(offset->getDataType()),
0, newSymbolReference);
debugTrace(tracer(), "\tIn createTempsForUnsafePutGet. replacing unsafeCall ( %p) child %p with %p\n", unsafeCall, unsafeCall->getChild(2), offset);
unsafeCall->setAndIncChild(2, offset);
if (!isUnsafeGet)
{
TR::Node *value = unsafeCall->getChild(3);
TR::Node *oldValue = value;
dataType = value->getDataType();
newSymbolReference =
comp()->getSymRefTab()->createTemporary(comp()->getMethodSymbol(), dataType);
storeNode = TR::Node::createWithSymRef(comp()->il.opCodeForDirectStore(value->getDataType()), 1, 1, value, newSymbolReference);
storeTree = TR::TreeTop::create(comp(), storeNode);
callNodeTreeTop->insertTreeTopsBeforeMe(storeTree);
// Replace the old child with a load of the new sym ref
value = TR::Node::createWithSymRef(value,
comp()->il.opCodeForDirectLoad(value->getDataType()),
0, newSymbolReference);
unsafeCall->setAndIncChild(3, value);
oldValue->recursivelyDecReferenceCount();
}
oldUnsafeAddress->recursivelyDecReferenceCount();
oldOffset->recursivelyDecReferenceCount();
}
TR::TreeTop*
TR_J9InlinerPolicy::genClassCheckForUnsafeGetPut(TR::Node* offset, bool branchIfLowTagged)
{
// The low bit of the offset is set - "tagged" - if the object being dereferenced
// is a java/lang/Class object. This is because this is a special case when
// an extra level of indirection is necessary
bool isILoad = (offset->getOpCodeValue() == TR::iload);
TR::Node *lowTag = NULL;
if (isILoad)
lowTag = TR::Node::create(TR::iand, 2, offset, TR::Node::iconst(1));
else
lowTag = TR::Node::create(TR::land, 2, offset, TR::Node::lconst(1));
TR::ILOpCodes op = branchIfLowTagged ? (isILoad ? TR::ificmpeq : TR::iflcmpeq)
: (isILoad ? TR::ificmpne : TR::iflcmpne);
// Create the if to check if an extra level of indirection is needed
TR::Node *cmp = TR::Node::createif(op, lowTag, lowTag->getSecondChild(), NULL);
TR::TreeTop* lowTagCmpTree = TR::TreeTop::create(comp(), cmp);
return lowTagCmpTree;
}
TR::TreeTop*
TR_J9InlinerPolicy::genDirectAccessCodeForUnsafeGetPut(TR::Node* callNode,
bool conversionNeeded, bool isUnsafeGet)
{
//Generate the code for the direct access
TR::Node *directAccessNode = callNode->duplicateTree();
TR::TreeTop *directAccessTreeTop = TR::TreeTop::create(comp(), directAccessNode, NULL, NULL);
TR::Node* firstChild = directAccessNode->getFirstChild();
if (isUnsafeGet) {
firstChild = firstChild->getFirstChild();
//if there is a conversion node we need to go one level deeper
if (conversionNeeded)
firstChild = firstChild->getFirstChild();
}
else {
// if there is an anchor, the store is one level below
if (directAccessNode->getOpCodeValue() == TR::compressedRefs)
firstChild = firstChild->getFirstChild();
}
TR_ASSERT(((firstChild->getOpCodeValue() == TR::aiadd) ||
(firstChild->getOpCodeValue() == TR::aladd)), "Unexpected opcode in unsafe access\n");
TR::Node *grandChild = firstChild->getSecondChild();
firstChild->setAndIncChild(1, grandChild->getFirstChild());
grandChild->recursivelyDecReferenceCount();
// If a conversion is needed, the 'callNode' was constructed earlier on in createUnsafe(get/put)WithOffset
// While some of the children end up in the final trees, this constructed callNode does not.
// We need to dec the reference count otherwise a child of the callNode that ends up in the final trees will have an extra refcount and will cause
// an assert
if(conversionNeeded)
{
for(int32_t i=0 ; i< callNode->getNumChildren(); i++)
{
debugTrace(tracer(), "\t In genDirectAccessCodeForUnsafeGetPut, recursively dec'ing refcount of %p:\n", callNode->getChild(i));
callNode->getChild(i)->recursivelyDecReferenceCount();
}
}
return directAccessTreeTop;
}
TR::TreeTop*
TR_J9InlinerPolicy::genIndirectAccessCodeForUnsafeGetPut(TR::Node* directAccessOrTempStoreNode, TR::Node* unsafeAddress)
{
// Generate the indirect access code in the java/lang/Class case. First modify unsafeAddress which is a descendant
// of directAccessOrTempStoreNode and then make a copy of directAccessOrTempStoreNode.
TR::Node *oldAddressFirstChild = unsafeAddress->getFirstChild();
TR::Node *addressFirstChild =
TR::Node::createWithSymRef(TR::aloadi, 1, 1, oldAddressFirstChild,
comp()->getSymRefTab()->findOrCreateClassFromJavaLangClassSymbolRef());
addressFirstChild =
TR::Node::createWithSymRef(TR::aloadi, 1, 1, addressFirstChild,
comp()->getSymRefTab()->findOrCreateRamStaticsFromClassSymbolRef());
unsafeAddress->setAndIncChild(0, addressFirstChild);
oldAddressFirstChild->recursivelyDecReferenceCount();
TR::Node* indirectAccessOrTempStoreNode = directAccessOrTempStoreNode->duplicateTree();
// The directAccessNode has been replaced with the corresponding get (load)/put (store) node at this point. The
// purpose of this function is to create the indirect access (static access) for an unsafe get/put operation
// since in general we do not know whether the target of our get/put operation is to a static or instance field.
// As such for the indirect access (static access) case we need to reassign the symbol reference after duplicating
// the direct access node.
TR::Symbol* directSymbol = directAccessOrTempStoreNode->getSymbolReference()->getSymbol();
if (!directSymbol->isUnsafeShadowSymbol())
{
// We may have generated a store to a temp in case of a get operation
directSymbol = directAccessOrTempStoreNode->getFirstChild()->getSymbolReference()->getSymbol();
}
// Sanity check. Note this is fatal because under concurrent scavenge we could potentially miss a read barrier here.
TR_ASSERT_FATAL(directSymbol->isUnsafeShadowSymbol(), "Expected to find an unsafe symbol for the get/put operation.");
TR::Node* indirectAccessNode = indirectAccessOrTempStoreNode;
TR::Symbol* indirectSymbol = indirectAccessOrTempStoreNode->getSymbolReference()->getSymbol();
if (!indirectSymbol->isUnsafeShadowSymbol())
{
// We may have generated a store to a temp in case of a get operation
indirectAccessNode = indirectAccessOrTempStoreNode->getFirstChild();
}
TR::SymbolReference* indirectSymRef = comp()->getSymRefTab()->findOrCreateUnsafeSymbolRef(directSymbol->getDataType(), true, true, directSymbol->getMemoryOrdering());
indirectAccessNode->setSymbolReference(indirectSymRef);
return TR::TreeTop::create(comp(), indirectAccessOrTempStoreNode, NULL, NULL);
}
TR::Block *
TR_J9InlinerPolicy::createUnsafeGetPutCallDiamond(TR::TreeTop* callNodeTreeTop,
TR::TreeTop* comparisonTree,
TR::TreeTop* branchTargetTree,
TR::TreeTop* fallThroughTree)
{
// Connect the trees/add blocks etc. properly and split the original block
TR::Block * joinBlock =
callNodeTreeTop->getEnclosingBlock()
->createConditionalBlocksBeforeTree(callNodeTreeTop,
comparisonTree,
branchTargetTree,
fallThroughTree,
comp()->getFlowGraph(), false, false);
return joinBlock;
}
void
TR_J9InlinerPolicy::createAnchorNodesForUnsafeGetPut(TR::TreeTop* treeTop,
TR::DataType type, bool isUnsafeGet)
{
if (comp()->useCompressedPointers() && (type == TR::Address))
{
// create the anchor node only for the non-tagged case
TR::Node* node = treeTop->getNode();
TR::TreeTop *compRefTT =
TR::TreeTop::create(comp(), genCompressedRefs(isUnsafeGet?node->getFirstChild():node,
false));
TR::TreeTop *prevTT = treeTop->getPrevTreeTop();
if (prevTT != NULL) prevTT->join(compRefTT);
compRefTT->join(isUnsafeGet ? treeTop : treeTop->getNextTreeTop());
}
}
void
TR_J9InlinerPolicy::genCodeForUnsafeGetPut(TR::Node* unsafeAddress,
TR::Node* unsafeOffset,
TR::DataType type,
TR::TreeTop* callNodeTreeTop,
TR::TreeTop* prevTreeTop,
TR::SymbolReference* newSymbolReferenceForAddress,
TR::TreeTop* directAccessTreeTop,
TR::TreeTop* arrayDirectAccessTreeTop,
TR::TreeTop* indirectAccessTreeTop,
TR::TreeTop* directAccessWithConversionTreeTop,
bool needNullCheck, bool isUnsafeGet,
bool conversionNeeded,
bool arrayBlockNeeded, bool typeTestsNeeded,
TR::Node* orderedCallNode = NULL)
{
TR::CFG *cfg = comp()->getFlowGraph();
TR_OpaqueClassBlock *javaLangClass = comp()->getClassClassPointer(/* isVettedForAOT = */ true);
// There are 6 possible cases determining which checks and blocks should be added to the generated IL trees:
// 1.) typeTestsNeeded && arrayBlockNeeded
// (i.e.: object type is unknown at compile time AND (offheap OR data element size < 4 (byte, short)))
// - tests: NULL test, Array test, Lowtag test
// - blocks: arrayDirectAccess, directAccess, indirectAccess, directAccessWithConversion (if data element size < 4)
// 2.) typeTestsNeeded && !arrayBlockNeeded && javaLangClass != NULL
// (i.e.: object type is unknown at compile time AND gencon AND data element size >= 4 (int, long) AND able to get java/lang/Class)
// - tests: Lowtag test, NULL test, Class test
// - blocks: directAccess, indirectAccess
// 3.) typeTestsNeeded && !arrayBlockNeeded && javaLangClass == NULL
// (i.e.: object type is unknown at compile time AND gencon AND data element size >= 4 (int, long) AND unable to get java/lang/Class)
// - tests: NULL test, Array test, Lowtag test
// - blocks: directAccess, indirectAccess
// 4.) !typeTestsNeeded && arrayBlockNeeded
// (i.e.: object is known to be array at compile time AND (offheap OR data element size < 4 (byte, short)))
// - tests: NULL test
// - blocks: arrayDirectAccess, directAccess, directAccessWithConversion (if data element size < 4)
// *NOTE: Special Case*
// When conversionNeeded is true and offheap is not enabled, both the NULL and non-NULL cases need to be handled by
// direct access with conversion (which in the non-offheap case, is the same as array access). Thus, in this case:
// - test: none
// - blocks: arrayDirectAccess
// 5.) !typeTestsNeeded && !arrayBlockNeeded && conversionNeeded
// (i.e.: object is known to be non-array at compile time AND data element size < 4 (byte, short))
// - tests: NULL test
// - blocks: directAccess, directAccessWithConversion
// 6.) !typeTestsNeeded && !arrayBlockNeeded && !conversionNeeded
// (i.e.: object is known to be non-array at compile time AND data element size >= 4 (int, long))
// - test: none
// - block: directAccess
TR::Node *nullComparisonNode = NULL;
TR::TreeTop *nullComparisonTree = NULL;
TR::Block *nullComparisonBlock = NULL;
if (typeTestsNeeded || // CASES (1), (2), (3)
(arrayBlockNeeded && (conversionNeeded == (bool)directAccessWithConversionTreeTop)) || // CASE (4) excluding special case
(!arrayBlockNeeded && conversionNeeded)) // CASE (5)
{
// Generate the tree for the null test
TR::Node *addrLoad =
TR::Node::createWithSymRef(unsafeAddress,
comp()->il.opCodeForDirectLoad(unsafeAddress->getDataType()),
0, newSymbolReferenceForAddress);
nullComparisonNode = TR::Node::createif(TR::ifacmpeq, addrLoad, TR::Node::create(addrLoad, TR::aconst, 0, 0), NULL);
nullComparisonTree = TR::TreeTop::create(comp(), nullComparisonNode);
nullComparisonBlock = prevTreeTop->getEnclosingBlock();
}
// If low-order bit of offset is set, perform indirect access. In all cases where
// a runtime lowtag test is needed (CASES 1, 2, and 3), the direct access code is placed
// on the fallthrough path, so it will ALWAYS need to take the branch to the indirect
// access block if the bit is set.
TR::TreeTop *lowTagCmpTree = typeTestsNeeded ? // CASES (1), (2), (3)
genClassCheckForUnsafeGetPut(unsafeOffset, true) : NULL;
TR::TreeTop *firstComparisonTree;
TR::TreeTop *branchTargetTree;
TR::TreeTop *fallThroughTree;
// Determine overall layout of IL - which test to place first (null test of object or
// setting of low-order bit of offset) - and whether direct or indirect access IL will
// be placed on the fall-through path of the first branch.
//
if (typeTestsNeeded && arrayBlockNeeded) // CASE (1)
{
firstComparisonTree = nullComparisonTree;
branchTargetTree = directAccessWithConversionTreeTop ? directAccessWithConversionTreeTop : arrayDirectAccessTreeTop;
fallThroughTree = directAccessTreeTop;
}
else if (typeTestsNeeded && !arrayBlockNeeded && javaLangClass != NULL) // CASE (2)
{
firstComparisonTree = lowTagCmpTree;
branchTargetTree = indirectAccessTreeTop;
fallThroughTree = directAccessTreeTop;
}
else if (typeTestsNeeded && !arrayBlockNeeded) // CASE (3)
{
firstComparisonTree = nullComparisonTree;
branchTargetTree = indirectAccessTreeTop;
fallThroughTree = directAccessTreeTop;
}
else if (!typeTestsNeeded && arrayBlockNeeded) // CASE (4)
{
//SPECIAL CASE
if (!directAccessWithConversionTreeTop && conversionNeeded)
{
firstComparisonTree = NULL;
branchTargetTree = NULL;
fallThroughTree = arrayDirectAccessTreeTop;
}
else
{
firstComparisonTree = nullComparisonTree;
branchTargetTree = directAccessWithConversionTreeTop ? directAccessWithConversionTreeTop
: directAccessTreeTop;
fallThroughTree = arrayDirectAccessTreeTop;
}
}
else if (conversionNeeded) // CASE (5)
{
firstComparisonTree = nullComparisonTree;
branchTargetTree = directAccessWithConversionTreeTop;
fallThroughTree = directAccessTreeTop;
}
else // CASE (6)
{
firstComparisonTree = NULL;
branchTargetTree = NULL;
fallThroughTree = directAccessTreeTop;
}
TR::Block *joinBlock = NULL;
if (firstComparisonTree) // All except CASE (6) and CASE (4) special case
{
joinBlock = createUnsafeGetPutCallDiamond(callNodeTreeTop, firstComparisonTree, branchTargetTree, fallThroughTree);
debugTrace(tracer(), "\t In genCodeForUnsafeGetPut, joinBlock is %d\n", joinBlock->getNumber());
}